# OpenWrt

* [Install on X86](#install-on-x86)
* [run as Container](#run-as-container)
  * [macvlan - access host](#macvlan---access-host)
  * [ipvlan](#ipvlan)
* [run as VM](#run-as-vm)
  * [QEMU NIC](#qemu-nic)
* [Docker](#docker)
* [network](#network)
  * [DSA](#dsa)
  * [/etc/config/](#etcconfig)
* [DHCP/DNS](#dhcpdns)
* [QoS](#qos)
  * [SQM](#sqm)
  * [nftables](#nftables)
* [Tailscale](#tailscale)
* [Mesh](#mesh)
  * [bat-adv](#bat-adv)
  * [Mode 802.11s](#mode-80211s)
  * [Mode AP - 802.11r](#mode-ap---80211r)
* [Switch Chip](#switch-chip)
  * [Bridged AP Setup](#bridged-ap-setup)
* [Controller - OpenWISP](#controller---openwisp)
* [Compile](#compile)
  * [Version](#version)
* [Wireshark](#wireshark)

## Install on X86

<https://openwrt.org/docs/guide-user/installation/openwrt\\_x86>

```
dd if=openwrt-21.02.0-x86-64-generic-ext4-combined.img bs=1M of=/dev/sdX

opkg update
opkg install lsblk parted losetup resize2fs
echo fix | parted -l ---pretend-input-tty
parted -s /dev/sda resizepart 2 100% 
losetup /dev/loop1 /dev/sda2
resize2fs -f /dev/loop1
```

## run as Container

* <https://supes.top/docker%E7%89%88openwrt%E6%97%81%E8%B7%AF%E7%94%B1%E5%AE%89%E8%A3%85%E8%AE%BE%E7%BD%AE%E6%95%99%E7%A8%8B/>
* <https://mlapp.cn/376.html>

```
ip link set vlan.10 promisc on
docker network create -d macvlan --subnet=10.0.0.0/8 --gateway=10.0.0.1 -o parent=vlan.10 macnet
docker network ls && docker network inspect macnet
docker run --restart unless-stopped --name openwrt -d --network macnet --privileged sulinggg/openwrt:x86_64 /sbin/init # root/password

docker exec -it openwrt /bin/sh # vim /etc/config/network // edit ip/gw & restart

config interface 'lan'
        option type 'bridge'
        option ifname 'eth0'
        option proto 'static'
        option ipaddr '10.19.0.3'
        option netmask '255.0.0.0'
        option gateway '10.0.0.1'
        option broadcast '10.255.255.255'
        option dns '10.0.0.1'

 docker network inspect macnet
```

### macvlan - access host

<https://stackoverflow.com/questions/49600665/docker-macvlan-network-inside-container-is-not-reaching-to-its-own-host>

```
docker network create -d macvlan -o parent=eno1 \
--subnet 192.168.1.0/24 \
--gateway 192.168.1.1 \
--ip-range 192.168.1.192/27 \
--aux-address 'host=192.168.1.223' \
mynet

ip link add macnet-shim link vlan.10 type macvlan  mode bridge
ip addr add 10.19.0.1/8 dev macnet-shim
ip link set macnet-shim up
ip route add 10.0.0.1/8 dev macnet-shim
ip link show macnet-shim || ip link delete macnet-shim
```

macvlan/ipvlan: <https://sreeninet.wordpress.com/2016/05/29/docker-macvlan-and-ipvlan-network-plugins/>

### ipvlan

<https://docs.docker.com/network/ipvlan/#ipvlan-l2-mode-example-usage>

```
docker network  create  -d ipvlan \
    --subnet=10.0.0.0/8 \
    --gateway=10.0.0.1 \
    --ip-range=10.19.1.0/24 \
    -o ipvlan_mode=l2 \
    -o parent=vlan.10 ipvlan10_NotTested
```

## run as VM

### QEMU NIC

```
use e1000; rtl8139 not detected by default.
eth0 -> LAN, eth1 -> WAN, usually.
```

## Docker

```
service dockerd stop
vi /etc/config/dockerd     # data_root
vi /etc/docker/daemon.json # data-root
reboot
docker pull hello-world
```

## network

### DSA

replace swconfig

Multiple networks (using VLANs): <https://forum.openwrt.org/t/mini-tutorial-for-dsa-network-config/96998>

### /etc/config/

<https://openwrt.org/docs/guide-user/network/network\\_configuration#example\\_configuration>

```
    config interface 'wan'
        option ifname 'eth0'
        option proto 'dhcp'
        option 'defaultroute' '1' # if multi WAN

    config interface 'lan'
        option type 'bridge'
        option ifname 'eth1 eth2'
        option proto 'static'
        option ipaddr '192.168.99.1'
        option netmask '255.255.255.0'
        option ip6assign '60'

/etc/init.d/network restart

# Soft network reload
service network reload

# Hard network restart
service network restart
```

H/W Router: wireless interfaces may be added to lan automatically via LUCI, create new for other bridges.

## DHCP/DNS

/etc/config/dhcp

```
config host
        list mac 'xx:xx:xx:xx:xx:xx'
        option ip '192.168.6.16'

config domain
        option name 'homeassistant.local'
        option ip '192.168.6.16'
```

## QoS

### SQM

<https://openwrt.org/docs/guide-user/network/traffic-shaping/start>

<https://openwrt.org/docs/guide-user/network/traffic-shaping/sqm>

* Interface name to your internet (WAN) link
* Link Layer Adaptation: <https://openwrt.org/docs/guide-user/network/traffic-shaping/sqm-details#sqmlink\\_layer\\_adaptation\\_tab>

### nftables

<https://github.com/openwrt/packages/blob/master/net/nft-qos/files/nft-qos.config>

## Tailscale

* <https://github.com/adyanth/openwrt-tailscale-enabler>
* <https://openwrt.org/docs/guide-user/services/vpn/tailscale/start>

## Mesh

```
iw list | grep -E "phy|mesh" # check if supported hardware
```

### bat-adv

<https://cgomesu.com/blog/Mesh-networking-openwrt-batman/#initial-configuration>

```
opkg remove wpad-basic-
opkg install batctl-full kmod-batman-adv wpad-mesh-wolfssl
```

<https://www.open-mesh.org/doc/batman-adv/Batman-adv-openwrt-config.html>

### Mode 802.11s

<https://openwrt.org/docs/guide-user/network/wifi/mesh/80211s>

### Mode AP - 802.11r

[./wireless.md#80211kvr](https://book.ferro.pro/wireless#80211kvr)

## Switch Chip

<https://openwrt.org/docs/techref/swconfig>

```
swconfig list
swconfig dev switch0 show

VLAN 1:
        vid: 1
        ports: 0 1 6  # 6 = untagged CPU
VLAN 10:  # luci： `/network/vlan`
        vid: 10
        ports: 2 3 6t # tag CPU => create `eth0.X`(eth0=switch0) under `/network/iface_add`
```

<https://openwrt.org/docs/guide-user/network/vlan/switch\\_configuration#vlan\\_explained\\_with\\_default\\_scenario\\_of\\_most\\_openwrt\\_routers>

* Each port `untagged` to exactly one VLAN ID

### Bridged AP Setup

| Web URI                        | Task                                 |
| ------------------------------ | ------------------------------------ |
| /luci/admin/network/vlan       | + vlan : all ports tagged            |
| /luci/admin/network/iface\_add | test new vlan IP ; lan : remove dhcp |

## Controller - OpenWISP

* Install: <https://github.com/openwisp/openwisp-controller#deploy-it-in-production>
* Features: <https://openwisp.org/whatis.html>
* Config: <https://openwisp.io/docs/user/configure-device.html#install-openwisp-config>

## Compile

```
git clone --single-branch --branch main   --depth 1 https://github.com/openwrt/openwrt.git  /data/github/openwrt
git clone --single-branch --branch 22.03  --depth 1 https://github.com/Lienol/openwrt.git   /data/github/openwrt-Lienol-22.03
git clone --single-branch --branch master --depth 1 https://github.com/coolsnowwolf/lede    /data/github/openwrt-lede
```

<https://hub.docker.com/r/p3terx/openwrt-build-env>

```
docker run -itd \
    --name openwrt-build \
    -v /data/github/openwrt:/home/user/openwrt \
    p3terx/openwrt-build-env

n=openwrt-build-lede
docker exec $n sudo chown -hR user:user . && docker exec -it $n bash # tmux
cd ~/openwrt && ls -la
# make clean # rm /bin /build_dir
./scripts/feeds update -a ; ./scripts/feeds install -a
make menuconfig # make targetclean
make download -j8 V=s && make V=s -j$(($(nproc) - 1))


ls /data/github/openwrt*/bin/targets/mediatek/mt7622/*.bin -lh # host
```

<https://openwrt.org/docs/guide-developer/toolchain/use-buildsystem>

LuCI ---> Applications ---> luci-app-mtwifi #闭源Wi-Fi驱动 + kmod-mt76... Extra packages ---> ipv6helper

### Version

```
CONFIG_VERSIONOPT=y
CONFIG_IMAGEOPT=y
CONFIG_VERSION_DIST="##.##-SNAPSHOT"
CONFIG_VERSION_NUMBER="OpenWrt"
```

## Wireshark

* UI: click left icon of text `SSH remote capture` (no password save)
* CMD: <https://openwrt.org/docs/guide-user/firewall/misc/tcpdump\\_wireshark>
