Try   HackMD

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)

我會挑ASUS USB-AC51,原因是OpenWRT有內建mt7602u的driver。
OpenWRT支援的USB WIFI不多,要確定有支援或是有第三方提供(From github),才有辦法支援。

會用到外接USB WIFI還有一個原因,就是因為Raspberry pi本身無線網路卡不支援AP+STA的混合模式,也就是不能一邊連上網,一邊分享。

參考:

Build OpenWRT於Raspberry Pi4


編譯OpenWRT

  1. 編譯環境安裝:
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
  1. 從官方下載OpenWRT Source code
git clone https://github.com/openwrt/openwrt -depth=1 sudo chown -R user:user openwrt cd openwrt
  1. 下載需要的package
./scripts/feeds update -a ./scripts/feeds install -a
  1. 設定
make menuconfig

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

可以使用如參考頁面的基礎設定(如下),不過因為我會用到其他功能(包含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
  8. Exit
  9. save

如果是使用我的設定檔案,直接拷貝至openwrt目錄底下,並命名為.config就可以了
下載

  1. 編譯:
make -j6 V=s

第一次編譯,會需要編譯Toolchain,所以需要幾個小時的時間


拷貝至SDCard

  1. 解壓縮編譯完成的檔案(bin/targets/bcm27xx/bcm2711/openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img.gz)
cd bin/targets/bcm27xx/bcm2711 gunzip -d openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img.gz
  1. 燒錄至SDCard
sudo dd if=openwrt-bcm27xx-bcm2711-rpi-4-squashfs-factory.img of={你的記憶卡位置} bs=1M sync

連接Raspberry pi和PC

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

插上Raspberry pi開機,即可在console操作Raspberry pi

這裡透過screen操作console

sudo screen /dev/ttyUSB0 115200
  • 如果上述步驟成功,應該可已看到初始化過程

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

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

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


設定網路

  1. 連接 ASUS USB-AC51(mt7602u)
    將ASUS USB-AC51(mt7602u) network card插在Raspberry pi 4上USB 3的Port。

USB 2將無法驅動該網卡。

  1. 輸入ip address
    看到wlan2, wlan3表示有抓到Raspberry pi內建網路卡和ASUS USB-AC51

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 初始化網路設定
rm /etc/config/wireless; wifi config; sed -i '/disabled/ s/1/0/g' /etc/config/wireless; wifi up
  1. 設定無線網路

OpenWRT無線網路設定分成三個檔案:

/etc/config/network (網路設定)
/etc/config/wireless (無線網路設定)
/etc/config/firewall (防火牆設定)

在/etc/config/network新增DHCP設定

config interface 'wan'
	option proto 'dhcp'

在/etc/config/wireless設定無線網路

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'

簡易設定檔介紹:

​​​​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' => 設定密碼
  1. 設定防火牆
    將wan設定中的option input 'REJECT' 改成 option input 'ACCEPT'。

該步驟是為了等等可以透過網路連入OpenWRT的GUI (Luci)

  1. 開啟wlan2, wlan3的radio
uci set wireless.radio2.disabled=0 uci set wireless.radio3.disabled=0 uci commit
  1. 將網路設定應用
/etc/init.d/network restart

確定設定成功

  1. 輸入以下指令查看各個網卡狀態
ifconfig

確認wlan3有從其他路由器拿到IP

  1. 利用其他裝置,確認是否有收到Raspberry pi發出來的ssid

進入OpenWRT GUI (Luci)

  • 在同網域下的隨便一台裝置,開啟瀏覽器輸入剛剛看到wlan3的IP

會看到以下畫面,帳號密碼預設都是root

進入後可看到基本畫面


OpenWRT GUI裡的網路設定,雖然可以設定,但網卡不一定支援,錯誤的設定會導致網卡開不起來。例如 Raspberry pi 4 內建網路卡沒有支援802.11AC, 80mhz,但GUI可以設定。所以,要注意依照自己的網卡能力選擇適合的設定。

基本的OpenWRT建置先寫到這裡,下一篇會補上Docker和NordVPN (OpenVPN)的設定