OpenWRT on Raspberry pi 4
===
因為手邊缺一台router,看著手邊的Raspberry pi,不如自己搞一台看看。
### 目標功能:
1. 其他Router(AP) => WIFI => Raspberry PI(Station)
2. Raspberry PI(Station) => (Ethernet, WIFI) => Device
3. Openvpn (連接NordVPN)
4. Docker (架測Nextcloud當作雲端儲存)
### Environment:
1. ASUS USB-AC51(mt7602u) network card
2. Raspberry pi 4b+
3. Ubuntu 21.04
4. 64GB up記憶卡
5. TTL to USB (連接Raspberry pi和PC)
:::info
我會挑ASUS USB-AC51,原因是OpenWRT有內建mt7602u的driver。
OpenWRT支援的USB WIFI不多,要確定有支援或是有第三方提供(From github),才有辦法支援。
會用到外接USB WIFI還有一個原因,就是因為Raspberry pi本身無線網路卡不支援AP+STA的混合模式,也就是不能一邊連上網,一邊分享。
:::
### 參考:
[Build OpenWRT於Raspberry Pi4 ](https://ithelp.ithome.com.tw/articles/10255724?sc=rss.qu)
---
# 編譯OpenWRT
1. 編譯環境安裝:
```bash=
sudo apt update
sudo apt install build-essential ccache ecj fastjar file g++ gawk \
gettext git java-propose-classpath libelf-dev libncurses5-dev \
libncursesw5-dev libssl-dev python python2.7-dev python3 unzip wget \
python3-distutils python3-setuptools rsync subversion swig time \
xsltproc zlib1g-dev cmake
```
2. 從官方下載OpenWRT Source code
```bash=
git clone https://github.com/openwrt/openwrt -depth=1
sudo chown -R user:user openwrt
cd openwrt
```
3. 下載需要的package
```bash=
./scripts/feeds update -a
./scripts/feeds install -a
```
4. 設定
```bash=
make menuconfig
```

:::info
可以使用如參考頁面的基礎設定(如下),不過因為我會用到其他功能(包含mt7602u驅動),所以可以直接引用我的設定檔。
1. Target System = Broadcom BCM27xx
2. Subtarget = BCM2711 boards 64bit
3. Target Profile = Raspberry Pi 4b
4. Target Images = squashfs
5. Kernel Modules -
-- USB Support = kmod-usb-hid
= kmod-usb-net
= kmod-usb-net-asix
= kmod-usb-net-asix-ax88179
= kmod-usb2
= kmod-usb3
6. Libraries
-- libssh
-- libssh2
-- (N) libustream-mbedtls
-- (Y) libustream-openssl
-- (N) libustream-wolfssl
7. LUCI -
-- Collections -
= luci
= luci-ssl-openssl
9. Exit
10. save
:::
:::info
如果是使用我的設定檔案,直接拷貝至openwrt目錄底下,並命名為.config就可以了
[下載](https://drive.google.com/file/d/1MhggINztSXAy_tBWKSqnnfLxzQpsRJ1p/view?usp=sharing)
:::
5. 編譯:
```bash=
make -j6 V=s
```
:::info
第一次編譯,會需要編譯Toolchain,所以需要幾個小時的時間
:::
---
# 拷貝至SDCard
1. 解壓縮編譯完成的檔案(bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img.gz)
```bash=
cd bin/targets/bcm27xx/bcm2711
gunzip -d openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img.gz
```
2. 燒錄至SDCard
```bash=
sudo dd if=openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img of={你的記憶卡位置} bs=1M
sync
```
---
# 連接Raspberry pi和PC
* Raspberry pi console 接法[參考 [基礎] 從序列埠登入到 Raspberry Pi](https://www.raspberrypi.com.tw/1999/connect-to-raspberry-pi-via-serial/)

插上Raspberry pi開機,即可在console操作Raspberry pi
這裡透過screen操作console
```bash=
sudo screen /dev/ttyUSB0 115200
```
* 如果上述步驟成功,應該可已看到初始化過程

* 等到初始化結束(Console畫面停下來),按下Enter鍵,進入互動介面。

---
# 設定網路
1. 連接 ASUS USB-AC51(mt7602u)
將ASUS USB-AC51(mt7602u) network card插在Raspberry pi 4上USB 3的Port。
:::info
USB 2將無法驅動該網卡。
:::
2. 輸入ip address
看到wlan2, wlan3表示有抓到Raspberry pi內建網路卡和ASUS USB-AC51

3. 初始化網路設定
```bash=
rm /etc/config/wireless; wifi config; sed -i '/disabled/ s/1/0/g' /etc/config/wireless; wifi up
```
4. 設定無線網路
:::info
OpenWRT無線網路設定分成三個檔案:
/etc/config/network (網路設定)
/etc/config/wireless (無線網路設定)
/etc/config/firewall (防火牆設定)
:::
在/etc/config/network新增DHCP設定
```text=
config interface 'wan'
option proto 'dhcp'
```
在/etc/config/wireless設定無線網路
```text=
config wifi-device 'radio2'
option type 'mac80211'
option path 'platform/soc/fe300000.mmcnr/mmc_host/mmc1/mmc1:0001/mmc1:0001:1'
option legacy_rates '0'
option country 'US'
option short_gi_40 '0'
option htmode 'VHT40'
option hwmode '11a'
option channel 'auto'
config wifi-iface 'default_radio2'
option device 'radio2'
option network 'lan'
option mode 'ap'
option ssid 'ssid_ap'
option encryption 'encryption'
option key 'password'
config wifi-device 'radio3'
option type 'mac80211'
option path 'scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0'
option band '5g'
option htmode 'VHT80'
option disabled '0'
option channel 'auto'
option cell_density '0'
option txpower '19'
config wifi-iface 'default_radio3'
option device 'radio3'
option network 'wan'
option mode 'sta'
option ssid 'ssid'
option encryption 'encryption'
option key 'passwrd'
```
:::info
簡易設定檔介紹:
option path 'platform/soc/fe300000.mmcnr/mmc_host/mmc1/mmc1:0001/mmc1:0001:1' => 表示為Raspberry pi內建無線網路卡
option path 'scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.0' => 表示為ASUS USB-AC51(mt7602u)
option network 'lan' => 網路設定檔。(lan預設設定是靜態網路)
option network 'wan' => 網路設定檔。(剛剛設定的DHCP網路)
option mode 'ap' => AP mode (給別人連)
option mode 'sta' => Station mode (連接別人)
option ssid 'ssid' => 設定SSID
option encryption 'encryption' => 設定加密方式(EX: psk2, sae)
option key 'passwrd' => 設定密碼
:::
5. 設定防火牆
將wan設定中的option input 'REJECT' 改成 option input 'ACCEPT'。

:::info
該步驟是為了等等可以透過網路連入OpenWRT的GUI (Luci)
:::
:::info
詳細設定可參考
https://openwrt.org/docs/guide-user/network/wifi/basic
https://openwrt.org/docs/guide-user/base-system/basic-networking
https://openwrt.org/docs/guide-user/firewall/firewall_configuration
:::
6. 開啟wlan2, wlan3的radio
```bash=
uci set wireless.radio2.disabled=0
uci set wireless.radio3.disabled=0
uci commit
```
7. 將網路設定應用
```bash=
/etc/init.d/network restart
```
---
# 確定設定成功
1. 輸入以下指令查看各個網卡狀態
```bash=
ifconfig
```

:::info
確認wlan3有從其他路由器拿到IP
:::
2. 利用其他裝置,確認是否有收到Raspberry pi發出來的ssid
---
# 進入OpenWRT GUI (Luci)
* 在同網域下的隨便一台裝置,開啟瀏覽器輸入剛剛看到wlan3的IP
會看到以下畫面,帳號密碼預設都是root

進入後可看到基本畫面

---
:::info
OpenWRT GUI裡的網路設定,雖然可以設定,但網卡不一定支援,錯誤的設定會導致網卡開不起來。例如 Raspberry pi 4 內建網路卡沒有支援802.11AC, 80mhz,但GUI可以設定。所以,要注意依照自己的網卡能力選擇適合的設定。
:::
### 基本的OpenWRT建置先寫到這裡,下一篇會補上Docker和NordVPN (OpenVPN)的設定