FL2440——RT3070 softAP模式 實現無線路由器功能

FL2440實現無線路由器功能

RT3070無線網卡有兩種工作模式STA模式和softAP模式,分別由STA驅動和softAP驅動來實現,STA驅動支持無線網卡工作在STA模式下,可以連接到網絡實現上網功能。而SoftAP的驅動支持無線網卡工作在softAP模式下,可以作爲一個軟的接入點,實現無線路由器功能。

在前面一篇博客總結了STA模式的驅動移植,使得我們的開發板能夠連接到互聯網實現上網功能。這裏對RT3070的第二種工作模式即softAP進行一個回顧總結,使FL2440開發板實現路由器功能,其他終端設備可以連接到「 路由器」實現上網。

內核配置

 [*] Networking support  ---> 
       Networking options  ---> 
            [*] Network packet filtering framework (Netfilter)  ---> 
                 [*]   Advanced netfilter configuration 
                       Core Netfilter Configuration  --->(除了下面選項外其他全選)
                           < >   SCTP protocol connection tracking support (EXPERIMENTAL)
                           < >   FTP protocol support 
                           < >   SIP protocol support 
                           < > Transparent proxying support (EXPERIMENTAL)
                           < >   set target and match support
                           < >   CHECKSUM target support
                           < >   "CT" target support
                           < >   "DSCP" and "TOS" target support
                           < >   "NOTRACK" target support
                           < >   "TRACE" target support
                  <*>   IP set support  --->
                       <*>   bitmap:ip set support
                       <*>   bitmap:ip,mac set support
                       <*>   bitmap:port set support
                       <*>   list:set set support 
                  <*>   IP virtual server support  --->(除了下面選項外其他全選)
                       [ ]   IP virtual server debugging
                       [ ]   SCTP load balancing support
                       < >   FTP protocol helper 
                    IP: Netfilter Configuration  --->(全選)
      <*>   RF switch subsystem support  --->     

按照上面配置完make menuconfig選項後重新編譯內核並燒錄到開發板。

接下來將開發板通過網線連接到已經連入因特網的路由器,然後通過SSH遠程登陸軟件軟件登陸到開發板。注意:必須保證PC和開發板處於同一局域網並且在用以網段內,否則登錄不上開發板。

配置無線網絡的工具移植

移植Openssl庫

Hostapd依賴於openssl與libnl這兩個庫。所以首先要移植openssl與libnl,openssl在前面使能RT3070 STA模式的過程中已經移植了,這裏不再重複。

移植linbl庫

libnl核心庫提供了通過netlink socket連接所需要的基礎函數,用於處理socket的鏈接和斷開、數據收發、消息創建分析,提供用戶可定製接收狀態機,並提供一種抽象的數據類型框架,用於簡化netlink 協議對象的實現。 linbl包括四個主要的lib,libnl,libnl-genl,libnl-route,libnl-nf。
更多關於linbl和netlink的介紹,大家可以參考這篇博客。下面進行libnl的移植操作。

下載並解壓libnl庫
hostapd使用的libnl庫爲1.x版本,2.0版本的API與1.0的可能不兼容,因此我直接移植1.1版本。

[lwn@localhost libnl]$ wget http://www.infradead.org/~tgr/libnl/files/libnl-1.1.tar.gz
[lwn@localhost libnl]$ tar zvxf libnl-1.1.tar.gz
[lwn@localhost libnl]$ ls
libnl-1.1  libnl-1.1.tar.gz

進入libnl目錄配置安裝

[lwn@localhost libnl-1.1]$ mkdir install
[lwn@localhost libnl-1.1]$ ./configure --prefix=`pwd`/install --host=arm-linux
[lwn@localhost libnl-1.1]$ make CC=/opt/dl/buildroot-2012.08/ARM920t/usr/bin/arm-linux-gcc
[lwn@localhost libnl-1.1]$ sudo make install
[lwn@localhost libnl-1.1]$ ls install/lib/
libnl.so  libnl.so.1  libnl.so.1.1  pkgconfig

安裝完成後,將libnl-1.1/install/目錄下的 libnl.so.1 拷貝到開發板/lib目錄下。

移植Hostapd

hostapd 是一個用戶態用於AP和認證服務器的守護進程。它實現了IEEE 802.11相關的接入管理,IEEE 802.1X/WPA/WPA2/EAP 認證, RADIUS客戶端,EAP服務器和RADIUS 認證服務器。在linux系統中,使用hostapd可以實現WIFI的無線接入熱點(AP)。
下載解壓

[lwn@localhost hostpad]$ wget http://w1.fi/releases/hostapd-1.0.tar.gz
[lwn@localhost hostpad]$ tar -xzvf hostapd-1.0.tar.gz
[lwn@localhost hostpad]$ ls
hostapd-1.0  hostapd-1.0.tar.gz

進入hostapd-1.0修改相關配置文件

[lwn@localhost hostapd]$ cd hostapd-1.0
[lwn@localhost hostapd-1.0]$ cd hostapd/
[lwn@localhost hostapd]$ cp defconfig .config

1.修改.config,如下截圖。
[lwn@localhost hostapd]$ vim .config
這裏寫圖片描述

2.修改Makefile,添加相關依賴庫。如下截圖。
[lwn@localhost hostapd]$ vim Makefile
這裏寫圖片描述

3.編譯生成hostapd工具

[lwn@localhost hostapd]$ make CC=/opt/dl/buildroot-2012.08/ARM920t/usr/bin/arm-linux-gcc
[lwn@localhost hostapd]$ sudo make install

這裏寫圖片描述
將生成的hostapd拷貝到開發板的/user/bin目錄下,並給與可執行權限。

創建hostapd.conf配置文件

hostapd.conf配置文件是用來設置wifi熱點信息的文件,我們可以對hostapd目錄下的hostapd.conf文件進行配置然後拷貝到開發板,也可以直接在開發板上新建名爲hostapd.conf的配置文件。由於配置文件內容本身並不多,所以我直接在開發板新建一個配置文件。
在開發板/etc目錄下創建hostapd.conf配置文件,內容如下:

interface=wlan0 //網絡接口
ssid=FL2440 //設置SSID爲FL2440
driver=nl80211  //使用nl80211無線驅動
channel=3   //設置wifi信道爲信道3
hw_mode=g   //使用80211g協議標準 , 有效的值取決於硬件,通常:a, b, g
ignore_broadcast_ssid=0 //禁用廣播ssid
auth_algs=1 /*指定OSA認證算法, auth_algs=1 只支持 WPA2 身份驗證算法。auth_algs=2 表示支持 WEP。永遠不要使用有線等效加密 (wired equivalent privacy, WEP),因爲它非常容易破解,並且多年前就已經被完全破解了。auth_algs=3 表示支持這兩種方式。*/

wpa=3   /*指定WPA/WPA2類型, wpa=2 僅支持 WPA2。wpa=1 表示支持 WPA1,而 wpa=3 表示二者都支持。*/
wpa_key_mgmt=WPA-PSK    //指定支持的加密密鑰算法
wpa_passphrase=12345678 //指定認證密鑰
wpa_pairwise=TKIP   /*啓用了WPA或WPA2則需要指定wpa_pairwise或rsn_pairwise。*/
rsn_pairwise=CCMP   /*wpa_pairwise 和 rsn_pairwise控制支持加密數據的密鑰,可以使用 CCMP、TKIP 或兩者均使用*/

啓動wlan0

>: ifconfig wlan0 up

這裏寫圖片描述

啓動守護進程

>: hostapd -B hostapd.conf 
...
/*啓動hostapd成功的打印信息*/
Using interface wlan0 with hwaddr 00:12:04:24:06:93 and ssid 'FL2440'
...

啓動成功會出現上面的打印信息,使用ifconfig命令可以看到多了一個如下的網絡接口
這裏寫圖片描述

現在通過手機或其他終端設備可以搜索到SSID爲FL2440的wifi熱點信號,並通過預設的祕鑰12345678可以連上熱點。然而此時並不能上網,因爲還沒有分配IP地址。

配置DHCP

爲了使連上熱點的中斷設備能夠自動獲取IP,我們需要啓用DHCP服務。busybox中已經集成了udhcp,我們可以直接對busybox-1.20.2/examples/udhcp/ 下的配置文件udhcpd.conf進行修改,也可以在開發板上/etc目錄下新建一個名爲udhcp.conf的配置文件。內容如下:

start 192.168.3.20
end 192.168.3.254
interface wlan0
opt dns 8.8.8.8
option subnet 255.255.255.0
opt router 192.168.3.1

然後設置並啓動DHCP服務(開發板上操作以下命令)

>mkdir -p /var/lib/misc/ 
>touch /var/lib/misc/udhcpd.leases //創建租賃文件
>ifconfig wlan0 192.168.3.1 netmask 255.255.255.0 //設置接入點 
>echo "nameserver 8.8.8.8" > /etc/resolv.conf //DNS 
>udhcpd -f /etc/udhcpd.conf //啓動DHCP,此時用手機連上FL2440『路由器』將打印以下信息
udhcpd (v1.20.2) started
Sending OFFER of 192.168.3.20/*分配給手機的IP地址爲192.168.3.20*/
Sending ACK to 192.168.3.20

在開發板上用ping命令測試與手機的連通性

>: ping 192.168.3.20
PING 192.168.3.20 (192.168.3.20): 56 data bytes
64 bytes from 192.168.3.20: seq=0 ttl=64 time=72.368 ms
64 bytes from 192.168.3.20: seq=1 ttl=64 time=1280.778 ms
64 bytes from 192.168.3.20: seq=2 ttl=64 time=277.356 ms
64 bytes from 192.168.3.20: seq=3 ttl=64 time=74.611 ms
64 bytes from 192.168.3.20: seq=4 ttl=64 time=23.561 ms

成功返回數據,說明此時手機已經連接上『FL2440路由器』並且成功獲取到IP。然而此時,依然還不能接入互聯網。要使我們的『FL2440路由器』連上因特網,我們還需要移植iptables。

移植iptables

iptables是功能十分強大的基於linux內核的防火牆,iptables內部集成了filter,nat,mangle三張表。

  • filter表負責過濾數據包,包括的規則鏈有,input,output和forward;
  • Nat表則涉及到網絡地址轉換,包括的規則鏈有,prerouting,postrouting和output;
  • Mangle表則主要應用在修改數據包內容上,用來做流量整形的。

有關iptables更詳細的介紹,可以參見這篇博客

下面開始移植iptables

下載並解壓

[lwn@localhost iptables]$ wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/iptables-1.4.12.tar.bz2
[lwn@localhost iptables]$ tar -xzvf iptables-1.4.12.tar.bz2
[lwn@localhost iptables]$ cd iptables-1.4.12
[lwn@localhost iptables-1.4.12]$ mkdir install
[lwn@localhost iptables-1.4.12]$ ./configure  --host=arm-linux --enable-static --disable-shared  --disable-ipv6 --disable-largefile --prefix=`pwd`/install 
[lwn@localhost iptables-1.4.12]$ make CC=/opt/dl/buildroot-2012.08/ARM920t/usr/bin/arm-linux-gcc CFALGS=--static LDFLAGS=-static
[lwn@localhost iptables-1.4.12]$ make install
[lwn@localhost install]$ ls
bin  include  iptab_lib.tar.bz2  lib  sbin  share
[lwn@localhost install]$ cd sbin/
[lwn@localhost sbin]$ ls
iptables  iptables-restore  iptables-save  xtables-multi
[lwn@localhost sbin]$ file *
iptables:         symbolic link to `xtables-multi'
iptables-restore: symbolic link to `xtables-multi'
iptables-save:    symbolic link to `xtables-multi'
xtables-multi:    ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

將/install/lib文件夾的文件和xtables-multi打包放到開發板/lib和/bin目錄下,賦予可執行權限後將xtables-multi重命名爲iptables。

確保開發板連接的路由器能夠上網,即開發板的eth0網卡連入互聯網。
然後在開發板上對iptables如下配置

/*所有去往internet的流量進行地址僞裝,即將源地址改爲eth0*/
>:iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
/*對內轉發,數據包從eth0流向wlan0*/
>: iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISH -j ACCEPT 
/*對外轉發,數據包從wlan0流向eth0*/
>: iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
/*開啓內核轉發功能*/ 
>>:echo "1" >/proc/sys/net/ipv4/ip_forward 
/*設置好默認網關 */
>: route add default gw 192.168.2.1 
 /*啓動 dhcp服務*/
>: udhcpd -f udhcpd.conf

此時我們再次將設備連接到『FL2440路由器』上,然後可以上網了。
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章