上一篇講解了R2的網口配置,這一篇我們以BananaPi R2為例子來實現一個簡單的路由器;那么一個簡單的路由器應該具備什么樣的功能呢?最簡單的說就是wan+lan+ap這三個功能。
首先wan+lan的功能,R2已經默認有了,接下來我們用網橋(bridge)的方式搭建一個無線熱點(Ap)。
- 打開R2上mt6625(R2上的無線芯片)的ap模式
jack@jack: wmt_loader & jack@jack: stp_uart_launcher -p /etc/firmware & jack@jack: echo A > /dev/wmtWifi &
- 打開后,可以看到生成了一個虛擬網口ap0
root@LEDE:/# ifconfig -a ap0 Link encap:Ethernet HWaddr 02:08:22:4A:BA:50 BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
- 接下來,我們需要創建一個無線熱點出來,這里我使用的是hostapd(soft AP)的方式去創建,在這里我引申一下hostapd的知識:
什么是hostapd?我截取官網一部分解釋:hostapd is a user space daemon for access point and authentication servers. It implements IEEE 802.11 access point management, IEEE 802.1X/WPA/WPA2/EAP Authenticators, RADIUS client, EAP server, and RADIUS authentication server. The current version supports Linux (Host AP, madwifi, mac80211-based drivers) and FreeBSD (net80211). 簡單說就是hostapd是一個用戶空間1的守護進程,AP(access point)遵循IEEE的部分網絡和安全協議。 hostapd官網:https://w1.fi/hostapd/ hostapd相關資料:https://w1.fi/cgit/hostap/plain/hostapd 標注1:用戶空間是相對於內核空間去區別的,我們平時的操作基本都是在用戶空間去進行的,簡單的理解就是我們平時操作的Linux系統Gui,命令行,shell腳本,編譯運行的C程序等等都是在用戶空間的行為。
- 以下是hostapd的簡單配置:
root@LEDE:~# cat hostapd.conf interface=ap0 #網口是ap0 bridge=br-lan #網橋是br-lan ssid=BPI_R2 #SSID即無線信號的名稱是BPI-R2 driver=nl80211 #使用的驅動是nl80211 country_code=CN hw_mode=g #使用的是2.4G channel=1 #信道是1 wpa_key_mgmt=WPA-PSK #加密算法 wpa_passphrase=ledetest #創建的無線AP密碼
關於hostapd.conf文件更多的信息,請查看這里:https://w1.fi/cgit/hostap/plain/hostapd/hostapd.conf
- 配置完成后,執行“hostapd -d hostapd.conf”創建無線熱點:
root@LEDE:~# hostapd -d hostapd.conf Configuration file: hostapd.conf [ 6102.269958] br-lan: port 5(ap0) entered blocking state [ 6102.275130] br-lan: port 5(ap0) entered disabled state [ 6102.280499] device ap0 entered promiscuous mode [ 6102.285076] br-lan: port 5(ap0) entered blocking state [ 6102.290176] br-lan: port 5(ap0) entered forwarding state ap0: interface state UNINITIALIZED->COUNTRY_UPDATE Using interface ap0 with hwaddr 02:08:22:4a:ba:50 and ssid "BPI_R2" ap0: interface state COUNTRY_UPDATE->ENABLED ap0: AP-ENABLED
- 看打印的log,可以看出熱點以及創建成功了,接下來我用windows系統的筆記本去連接:
連接成功,看來Openwrt已經把dhcp服務也配置好了,這里再簡單引申一下dhcp服務:
dhcp(Dynamic Host Configuration Protocol)動態主機配置協議,其實簡單來講,就是設備連入局域網時,可以動態獲取到IP的一種協議。
如果沒有配置dhcp服務,設備連入局域網后,就獲取不到IP,也就上不了Internet。
- 我們來看一下R2上的dhcp服務:
root@LEDE:~# cat /etc/config/dhcp config dnsmasq #dnsmasq是一個輕量級的dhcp,dns服務 option domainneeded '1' option boguspriv '1' option filterwin2k '0' option localise_queries '1' option rebind_protection '1' option rebind_localhost '1' option local '/lan/' option domain 'lan' option expandhosts '1' option nonegcache '0' option authoritative '1' option readethers '1' option leasefile '/tmp/dhcp.leases' option resolvfile '/tmp/resolv.conf.auto' option nonwildcard '1' option localservice '1' config dhcp 'lan' option interface 'lan' option start '100' option limit '150' option leasetime '12h' option dhcpv6 'server' option ra 'server' config dhcp 'wan' option interface 'wan' option ignore '1' config odhcpd 'odhcpd' option maindhcp '0' option leasefile '/tmp/hosts/odhcpd' option leasetrigger '/usr/sbin/odhcpd-update' option loglevel '4'
乍看上去,這里的dhcp配置讓人有點心生畏懼,其實也不用太擔心,我們先來簡單看一下這個dhcp的配置:
我們先暫時略過dnsmasq和odhcpd,后續會添加講解。 這里配置了兩個接口"lan"和"wan"為dhcp的方式,這兩個接口必須也是在/etc/config/network中被定義過的。 “wan”配置很簡單,不用過多說明,我們來簡單分析一下“lan”的配置:
config dhcp 'lan' option interface 'lan' #必須是在/etc/config/network中被定義過的 option start '100' #從x.x.x.100開始分配IP option limit '150' #一共有150個IP,也就是說這里的配置是x.x.x.100 - x.x.x.249 option leasetime '12h' #最長連接時間 option dhcpv6 'server' option ra 'server' #服務定義的模式是IPv6(RA & DHCPv6)
更多細節可以參考這里:https://openwrt.org/docs/guide-user/base-system/dhcp
接下來我用手機連接,然后測試R2無線網絡的速度,當然這個速度是和R2連接的帶寬相關聯的,我這里是;
看起來網速還算是比較快的;現在,我們就已經成功在R2上面部署了一個具有基礎路由功能的Openwrt了。
接下來有人可能會遇到這個問題,我的R2關機或者重啟了,之前配置好的環境沒了,又要重新配置,有沒有什么方法可以開機啟動后去自動配置我要的環境呢?
答案當然是有的:
- 首先創建一個啟動Ap的腳本
vim setup.sh #!/bin/ash wmt_loader & sheep 3 stp_uart_launcher -p /etc/firmware & sleep 5 echo A > /dev/wmtWifi & sleep 5 hostapd -d hostapd.conf
- 然后再把setup.sh加到Openwrt的啟動文件里
root@LEDE:~# vim /etc/rc.local # Put your custom commands here that should be executed once # the system init finished. By default this file does nothing. cd /root/ #我的setup.sh是放在/root目錄下的,所以要先進入/root目錄 sleep 6 #這里sleep 6s是因為在等系統其它服務init完成 ./setup.sh & exit 0
- 然后再套個盒子,接根天線,就有那么點意思了
現在,一個簡單的路由器產品就完成了。最后,有人可能會問,既然是要做一個路由器產品,不可能老是用命令行去操作吧,再怎么樣也要有一個web的頁面去配置吧。
說的沒錯,要實現web頁面對路由器的配置,這里不得不提到Openwrt的好幫手Luci,那么下一篇我們就來看什么是Luci,以及如何配置。
歡迎大家批評指正,轉載請注明出處,多謝。