天天看點

wifi子產品rt3070的STA模式和AP模式的添加

在此之前首先要開發闆的USB驅動,驅動添加:https://mp.csdn.net/mdeditor/89789136#

AP模式: Access Point,提供無線接入服務,允許其它無線裝置接入,提供資料通路,一般的無線路由/網橋工作在該模式下。AP和AP之間允許互相連接配接(路由器模式)

STA模式: Station, 類似于無線終端,STA本身并不接受無線的接入,它可以連接配接到AP,一般無線網卡即工作在該模式。

DM9000網卡實作上網和遠端登陸:

首先來看一下FL2440自帶DM9000有線網卡,首先要在Linux核心添加驅動程式,并在make menuconfig中配置并使能有線網卡,在根檔案系統添加dropbear配置,使能遠端SSH登入。燒錄後将開發闆接入路由器,可以看到開發闆列印如下資訊:

檢視路由器ip(網關),我這裡是192.168.16.1。

ping通路由器(網關),這裡要ping通路由器,必須使路由器和開發闆的ip處于同一區域網路,修改開發闆ip為192.168.16.*(101-254)。我這裡的設定為192.168.16.168.

修改開發闆ip:

此時能ping通路由器,此時試試ping在此路由器下的其他主機,如果這裡ping不同,可能是主機防火牆未關閉,關閉防火牆,即可ping通,(這裡通常是開發闆ping不通主機,而主機是可以ping通開發闆的,因為開發闆在這裡是沒有安裝防火牆的)

關閉防火牆,可以ping通:

~ >: ping 192.168.16.110
PING 192.168.16.110 (192.168.16.110): 56 data bytes
64 bytes from 192.168.16.110: seq=0 ttl=128 time=5.000 ms
64 bytes from 192.168.16.110: seq=1 ttl=128 time=0.000 ms
^C
--- 192.168.16.110 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.000/2.500/5.000 ms
``
開發闆在DM9000有線網卡接入路由器時,最終是要實作完整的上網功能,ping百度試試。
可能出現如下結果:

```c
Network is unreachable
           

通常情況下,内網主機都能互相通信,而不能通路外網,可能是網關設定出現問題,設定網關:

route add default gw 192.168.16.1  (可用route指令檢視網關)
           

此時再ping百度的公網IP或者其域名www.baidu.com:

~ >: ping 14.215.177.39
PING 14.215.177.39 (14.215.177.39): 56 data bytes
64 bytes from 14.215.177.39: seq=0 ttl=48 time=95.000 ms
64 bytes from 14.215.177.39: seq=1 ttl=48 time=100.000 ms
           

成功ping通,此時開發闆能夠正常通路網絡了,當然,如果要提供一些更友善的服務,可以将ip的設定和網關的設定寫到相應的配置檔案中,同時提供域名解析服務。

此時如果要實作SSH通路該開發闆,(前提是路由器擁有公網IP)可以在路由器中添加端口映射,外網通路的端口如2222(>1000)映射到内網的192.168.16.168的22号端口。這時外部通路路由器的2222号端口,就可以映射到該開發闆的22号端口(遠端登入SSH預設端口)。

RT3070 STA模式設定

前提條件,使能USB驅動,在這些源碼包所在路徑建立檔案夾install,用來存放編譯wpa_supplicant的庫和頭檔案,以及後面AP模式編譯hostapd的所需庫和頭檔案,。

linux核心對于RT3070的驅動已經比較完善,我們隻需稍微對于核心稍加修改就能使用RT3070實作無線上網。這裡基于linux3.0核心實作。

添加make menuconfig選項:

[*] Networking support  --->  
    -*-   Wireless  --->  
         <*>   cfg80211 - wireless configuration API  
         [*]     enable powersave by default  
         [*]     cfg80211 wireless extensions compatibility
         [*]   Wireless extensions sysfs files  
         {*}   Common routines for IEEE802.11 drivers
         <*>   Generic IEEE 802.11 Networking Stack (mac80211)
     Device Drivers  --->
        Generic Driver Options  --->
            -*- Userspace firmware loading support    
            [*]   Include in-kernel firmware blobs in kernel binary
        [*] Network device support  --->  
            [*]   Wireless LAN  --->  
                <*>   Ralink driver support  --->
                    <*>   Ralink rt27xx/rt28xx/rt30xx (USB) support
                    [*]     rt2800usb - Include support for rt33xx devices  
                    [*]   Ralink debug output
           

現在重新編譯核心燒錄到開發闆,啟動核心。

插入RT3070無線網卡後核心列印資訊,不同配置列印的資訊可能不一樣。

~ >: usb 1-1.2: new full speed USB device number 3 using s3c2410-ohci
phy0 -> rt2x00_set_chip: Info - Chipset detected - rt: 3070, rf: 0005, rev: 0201.
           

此時ifconfig可以看到無線網卡已經添加。

ifconfig -a
eth0      Link encap:Ethernet  HWaddr 08:00:3E:26:0A:5B  
          inet addr:192.168.2.168  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:34 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:3352 (3.2 KiB)  TX bytes:0 (0.0 B)
          Interrupt:51 Base address:0x6300


lo        Link encap:Local Loopback  
          LOOPBACK  MTU:16436  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:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


wlan0     Link encap:Ethernet  HWaddr 00:12:04:23:B0:89  
          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)
           
phy0 -> rt2x00lib_request_firmware: Info - Loading firmware file 'rt2870.bin'.
phy0 -> rt2x00lib_request_firmware: Error - Failed to request Firmware.
           

使能網卡:

現在隻是添加了網卡驅動,還沒有真正的使能網卡,要使能網卡還需要下載下傳RT2870的固件。

在開發闆下建立/lib/firmware檔案夾,将下載下傳下來的linux-firmware/rt2870.bin拷貝到開發闆的firmware檔案夾下。

主機上----wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/rt2870.bin
/lib >: mkdir firmware
/lib >: mv rt2870.bin firmware/
           
~ >: ifconfig wlan0 up
phy0 -> rt2x00lib_request_firmware: Info - Loading firmware file 'rt2870.bin'.
phy0 -> rt2x00lib_request_firmware: Info - Firmware detected - version: 0.29.
phy0 -> rt2x00mac_conf_tx: Info - Configured TX queue 0 - CWmin: 3, CWmax: 4, Aifs: 2, TXop: 102.
phy0 -> rt2x00mac_conf_tx: Info - Configured TX queue 1 - CWmin: 4, CWmax: 5, Aifs: 2, TXop: 188.
phy0 -> rt2x00mac_conf_tx: Info - Configured TX queue 2 - CWmin: 5, CWmax: 10, Aifs: 3, TXop: 0.
phy0 -> rt2x00mac_conf_tx: Info - Configured TX queue 3 - CWmin: 5, CWmax: 10, Aifs: 7, TXop: 0.
           

檢視已使能網卡資訊:

~ >: ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:3E:26:0A:5B  
          inet addr:192.168.2.168  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:54 errors:0 dropped:0 overruns:0 frame:0
          TX packets:21 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:12600 (12.3 KiB)  TX bytes:981 (981.0 B)
          Interrupt:51 Base address:0x6300


wlan0     Link encap:Ethernet  HWaddr 00:12:04:23:B0:89  
          UP 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)
           

現在已經使能無線網卡,但是要連路由器上網還需要移植無線上網工具:openssl和wpa_supplicant

移植openssl

wget http://www.openssl.org/source/openssl-0.9.8i.tar.gztar -xzf openssl-0.9.8i.tar.gzcd openssl-0.9.8i./config no-asm shared --prefix=`pwd`/../install
           

修改Makefile檔案:(這是一個patch檔案,照着修改)注意CC是交叉編譯器路徑

--- openssl-0.9.8i/Makefile    2019-05-25 23:04:09.930783873 +0800
+++ openssl-0.9.8ia/Makefile    2019-05-02 16:57:51.466510704 +0800
@@ -12,9 +12,8 @@
SHLIB_MAJOR=0
SHLIB_MINOR=9.8
SHLIB_EXT=.so.$(SHLIB_MAJOR).$(SHLIB_MINOR)
-PLATFORM=linux-x86_64
-OPTIONS=enable-shared --prefix=/home/zhanghang/gitee/fl2440/driver/openssl-0.9.8i/../install no-asm no-camellia no-capieng no-cms no-gmp no-krb5 no-mdc2 no-montasm no-rc5 no-rfc3779 no-seed no-tlsext no-zlib no-zlib-dynamic
-CONFIGURE_ARGS=linux-x86_64 no-asm shared --prefix=/home/zhanghang/gitee/fl2440/driver/openssl-0.9.8i/../install
+PLATFORM=dist
+OPTIONS=enable-shared no-camellia no-capieng no-cms no-gmp no-krb5 no-mdc2 no-montasm no-rc5 no-rfc3779 no-seed no-tlsext no-zlib no-zlib-dynamic
SHLIB_TARGET=linux-shared
# HERE indicates where this Makefile lives.  This can be used to indicate
@@ -26,10 +25,10 @@
# for, say, /usr/ and yet have everything installed to /tmp/somedir/usr/.
# Normally it is left empty.
INSTALL_PREFIX=
-INSTALLTOP=/home/zhanghang/gitee/fl2440/driver/openssl-0.9.8i/../install
+INSTALLTOP=/home/zhanghang/gitee/fl2440/driver/install
# Do not edit this manually. Use Configure --openssldir=DIR do change this!
-OPENSSLDIR=/home/zhanghang/gitee/fl2440/driver/openssl-0.9.8i/../install/ssl
+OPENSSLDIR=/home/zhanghang/gitee/fl2440/driver/install
# NO_IDEA - Define to build without the IDEA algorithm
# NO_RC4  - Define to build without the RC4 algorithm
@@ -59,19 +58,19 @@
# equal 4.
# PKCS1_CHECK - pkcs1 tests.
-CC= gcc
-CFLAG= -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -m64 -DL_ENDIAN -DTERMIO -O3 -Wall -DMD32_REG_T=int
+CC=/opt/xtools/arm920t/bin/arm-arm920t-linux-gnueabi-gcc
+CFLAG= -O
DEPFLAG= -DOPENSSL_NO_CAMELLIA -DOPENSSL_NO_CAPIENG -DOPENSSL_NO_CMS -DOPENSSL_NO_GMP -DOPENSSL_NO_MDC2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SEED -DOPENSSL_NO_TLSEXT
PEX_LIBS=
-EX_LIBS= -ldl
+EX_LIBS=
EXE_EXT=
ARFLAGS=
-AR=ar $(ARFLAGS) r
-RANLIB= /usr/bin/ranlib
+AR=/opt/xtools/arm920t/bin/arm-arm920t-linux-gnueabi-ar $(ARFLAGS) r
+RANLIB=/opt/xtools/arm920t/bin/arm-arm920t-linux-gnueabi-ranlib
PERL= /usr/bin/perl
TAR= tar
TARFLAGS= --no-recursion
-MAKEDEPPROG= gcc
+MAKEDEPPROG=makedepend
# We let the C compiler driver to take care of .s files. This is done in
# order to be excused from maintaining a separate set of architecture
@@ -140,9 +139,9 @@
LIBS=   libcrypto.a libssl.a
SHARED_CRYPTO=libcrypto$(SHLIB_EXT)
SHARED_SSL=libssl$(SHLIB_EXT)
-SHARED_LIBS=$(SHARED_CRYPTO) $(SHARED_SSL)
-SHARED_LIBS_LINK_EXTS=.so.$(SHLIB_MAJOR) .so
-SHARED_LDFLAGS=-m64
+SHARED_LIBS=
+SHARED_LIBS_LINK_EXTS=
+SHARED_LDFLAGS=
GENERAL=        Makefile
BASENAME=       openssl
@@ -225,7 +224,7 @@
     @[ -n "$(THIS)" ] && $(CLEARENV) && $(MAKE) $(THIS) -e $(BUILDENV)
sub_all: build_all
-build_all: build_libs build_apps build_tests build_tools
+build_all: build_libs build_apps build_tests build_tools  build-shared
build_libs: build_crypto build_ssl build_engines
@@ -241,7 +240,6 @@
     @dir=test; target=all; $(BUILD_ONE_CMD)
build_tools:
     @dir=tools; target=all; $(BUILD_ONE_CMD)
-
all_testapps: build_libs build_testapps
build_testapps:
     @dir=crypto; target=testapps; $(BUILD_ONE_CMD)
           

修改完成後執行make,編譯大約1分半鐘,将生成的 libssl.so 和 libcrypto.so 拷貝到開發闆的/lib目錄下 ,

同時make install安裝wpa_supplicant編譯所需庫到install目錄下

wifi子產品rt3070的STA模式和AP模式的添加

移植wpa_supplicant

wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/wpa_supplicant-0.7.3.tar.gz
tar -xzf wpa_supplicant-0.7.3.tar.gz
cd wpa_supplicant-0.7.3/wpa_supplicant/
cp defconfig .config
           

修改.config

--- wpa_supplicant-0.7.3/wpa_supplicant/.config    2019-05-25 23:22:00.398763905 +0800
+++ .config    2019-05-25 23:23:05.754762686 +0800
@@ -24,7 +24,10 @@
#### sveasoft (e.g., for Linksys WRT54G) ######################################
#CC=mipsel-uclibc-gcc
-#CC=/opt/brcm/hndtools-mipsel-uclibc/bin/mipsel-uclibc-gcc
+CC=/opt/xtools/arm920t/bin/arm-linux-gcc -L/home/zhanghang/gitee/fl2440/driver/install/lib
+CFLAGS +=-I/home/zhanghang/gitee/fl2440/driver/install/include
+CFLAGS +=-I/home/zhanghang/gitee/fl2440/driver/install/libnl/include
+LIBS += -L/home/zhanghang/gitee/fl2440/driver/install/libnl/lib
#CFLAGS += -Os
#CPPFLAGS += -I../src/include -I../../src/router/openssl/include
#LIBS += -L/opt/brcm/hndtools-mipsel-uclibc-0.9.19/lib -lssl
           

這裡會用到libnl庫,必須先下載下傳libnl庫并安裝需要的庫和頭檔案到install目錄下。

移植libnl庫

wget http://www.infradead.org/~tgr/libnl/files/libnl-1.1.tar.gz
tar -xzf libnl-1.1.tar.gz
cd libnl-1.1
./configure -prefix=`pwd`/../install
make CC=/opt/xtools/arm920t/bin/arm-linux-gcc
           

這裡可能報錯:

../include/netlink-local.h: In function '__str2type':
../include/netlink-local.h:218: error: 'ULONG_MAX' undeclared (first use in this function)
../include/netlink-local.h:218: error: (Each undeclared identifier is reported only once
../include/netlink-local.h:218: error: for each function it appears in.)
../include/netlink-local.h: In function '__list_str2type':
../include/netlink-local.h:239: error: 'ULONG_MAX' undeclared (first use in this function)
../include/netlink-local.h: In function '__dp_dump':
../include/netlink-local.h:277: warning: ignoring return value of 'vasprintf', declared with attribute warn_unused_result
           

解決方法:

在對應檔案添加一個頭檔案 #include<limits.h>即可解決問題

最後make intsall安裝到install目錄下

接下來編譯 wpa_supplicant

此時直接make就行,結果如下:

wifi子產品rt3070的STA模式和AP模式的添加

将生成的 wpa_supplicant, wpa_cli, wpa_passphrase三個目标檔案移植到開發闆的usr/bin下

其中wpa_passphrase 生成配置檔案,wpa_supplicant 連接配接wifi指令

wpa_supplicant前置依賴屬于openssl的libcrypto.so和libssl.so

wpa_supplicant.conf配置檔案保留着需要連接配接AP的SSID和key等資訊,可以使用wpa_passphrase一鍵生成

wpa_passphrase theironman 1711318490 >> /etc/wpa_supplicant.conf
其中theironman 是wifi名稱,1711318490為密碼
           

修改etc/wpa_supplicant.conf如下:

ctrl_interface=/var/run/wpa_supplicant


network={
    ssid="theironman"
    #psk="1711318490"
    key_mgmt=WPA-PSK
    proto=WPA2
    group=TKIP CCMP
    pairwise=TKIP CCMP
    psk=8b3b6f8e89d80cbe9b6bd031dfe3ac41a6346b24eacb2c3f29c2369651b73f85
}
           

接下來在etc/init.d中添加一個啟動檔案,在開發闆啟動時,就自動啟動wpa_supplicant相關程式并聯網。

vim S12_STA
!#/bin/bash
  
ifconfig wlan0 up

#start wpa_supplicant
wpa_supplicant -B -d -Dwext -i wlan0 -c /etc/wpa_supplicant.conf
#set ip
ifconfig wlan0 192.168.16.168
#set gateway
route add default gw 192.168.16.1
           

并給其可執行權限。今後開發闆在啟動時就會自動連上該區域網路。同DM9000,STA模式的rt3070也可以遠端登入。

使能DHCP服務

上面隻能手動配置ip,現在開啟DHCP服務,使其自動擷取IP位址busybox已經內建了DHCP的用戶端和服務端,源碼編譯過後的examples/udhcp/simple.script作為DHCP用戶端的示例配置檔案如下,将其重命名為/usr/share/udhcpc/default.script,特别注意必須給default.script可執行權限。 執行udhcpc -i wlan0之後即可自動擷取IP,執行這個指令後udhcp會以守護程序方式在背景運作

rt3070的AP模式設定

實作無線路由器功能。

配置Linux核心:

[*] 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  --->    
           

重新編譯Linux核心。

接下來将開發闆通過網線連接配接到已經連入網際網路的路由器,然後通過SSH遠端登陸軟體軟體登陸到開發闆。注意:必須保證PC和開發闆處于同一區域網路并且在用以網段内,否則登入不上開發闆。

配置無線網絡的工具移植

移植Openssl庫

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

移植libnl庫

libnl核心庫提供了通過netlink socket連接配接所需要的基礎函數,用于處理socket的連結和斷開、資料收發、消息建立分析,提供使用者可定制接收狀态機,并提供一種抽象的資料類型架構,用于簡化netlink 協定對象的實作。 linbl包括四個主要的lib,libnl,libnl-genl,libnl-route,libnl-nf。更多關于libnl和netlink的介紹,大家可以參考這篇部落格https://www.tuicool.com/articles/j6f6z2。

在上面的STA模式中,已經編譯了libnl庫,将install/lib/libnl.so.1拷貝到開發闆的/lib下。

移植Hostapd

hostapd 是一個使用者态用于AP和認證伺服器的守護程序。它實作了IEEE 802.11相關的接入管理,IEEE 802.1X/WPA/WPA2/EAP 認證, RADIUS用戶端,EAP伺服器和RADIUS 認證伺服器。在linux系統中,使用hostapd可以實作WIFI的無線接入熱點(AP)。

wget http://w1.fi/releases/hostapd-1.0.tar.gz
tar -xzf hostapd-1.0.tar.gz
cd hostapd-1.0
cd hostapd/
cp defconfig .config
           

1.修改.config檔案

添加:

23 CONFIG_DRIVER_NL80211=y
           

2.修改Makefile檔案:

編譯該庫需要我們編譯libnl和openssl時安裝在install目錄下的庫和頭檔案。

添加如下:

CFLAGS +=-I/home/zhanghang/gitee/fl2440/driver/install/include/
LIBS +=-L/home/zhanghang/gitee/fl2440/driver/install/lib/
LDFLAGS +=-L/home/zhanghang/gitee/fl2440/driver/install/lib/
           

3.編譯:

make CC=/opt/xtools/arm920t/bin/arm-linux-gcc
make install//可省略
           
wifi子產品rt3070的STA模式和AP模式的添加

将生成的的hostpad檔案拷貝到開發闆的usr/bin下,并給其可執行權限。 hostapd.conf配置檔案是用來設定wifi熱點資訊的檔案,我們可以對hostapd目錄下的hostapd.conf檔案進行拷貝到開發闆然後進行修改。也可以在開發闆/etc目錄下建立hostapd.conf配置檔案

修改或建立hostpad.conf檔案修改如下:

interface=wlan0 //網絡接口
ssid=zhanghang //設定SSID為zhanghang
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

wifi子產品rt3070的STA模式和AP模式的添加

啟動守護程序

hostapd -B etc/hostapd.conf

wifi子產品rt3070的STA模式和AP模式的添加

啟動成功會出現上面的列印資訊,使用ifconfig指令可以看到多了一個如下的網絡接口

現在通過手機或其他終端裝置可以搜尋到SSID為zhanghang的wifi熱點信号,并通過預設的秘鑰12345678可以連上熱點。然而此時并不能上網,因為還沒有配置設定IP位址。

wifi子產品rt3070的STA模式和AP模式的添加

使用ifconfig可以看到多出這個:

wifi子產品rt3070的STA模式和AP模式的添加

可能出錯:

~ >: hostapd -B etc/hostapd.conf
Configuration file: etc/hostapd.conf
Line 5: unknown hw_mode 'a '
1 errors found in configuration file 'etc/hostapd.conf'
           

删除那行。

配置DHCP

為了使連上熱點的中斷裝置能夠自動擷取IP,我們需要啟用DHCP服務。busybox中已經內建了udhcp,我們可以直接對busybox-1.20.2/examples/udhcp/ 下的配置檔案udhcpd.conf進行修改,也可以在開發闆上/etc目錄下建立一個名為udhcp.conf的配置檔案。内容如下:

start 192.168.1.100
end 192.168.1.254 //ip配置設定的起始和結束位址
interface wlan0 
opt dns 8.8.8.8 
option subnet 255.255.255.0 
opt router 192.168.1.1  //網關
           

然後設定并啟動DHCP服務(開發闆上操作以下指令)

mkdir -p /var/lib/misc/
touch /var/lib/misc/udhcpd.leases //建立租賃檔案
ifconfig wlan0 192.168.1.1 netmask 255.255.255.0 //設定接入點
echo "nameserver 8.8.8.8" > /etc/resolv.conf  //已經設定DNS就可以不用再設定
udhcpd -f /etc/udhcpd.conf //啟動DHCP,此時用手機連上zhanghang『路由器』将列印以下資訊
           
wifi子產品rt3070的STA模式和AP模式的添加

成功傳回資料,說明此時手機已經連接配接上『zhanghang路由器』并且成功擷取到IP。然而此時,依然還不能接入網際網路。要使我們的『zhanghang路由器』連上網際網路,我們還需要移植iptables。

移植iptables

iptables是功能十分強大的基于linux核心的防火牆,iptables内部內建了filter,nat,mangle三張表。

filter表負責過濾資料包,包括的規則鍊有,input,output和forward;

Nat表則涉及到網絡位址轉換,包括的規則鍊有,prerouting,postrouting和output;

Mangle表則主要應用在修改資料包内容上,用來做流量整形的。

有關iptables更詳細的介紹,可以參見這篇部落格 https://www.cnblogs.com/metoy/p/4320813.html。

wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/iptables-1.4.12.tar.bz2
tar -xjf iptables-1.4.12.tar.bz2
cd iptables-1.4.12
./configure --host=arm-linux --enable-static --disable-shared --disable-ipv6 --disable-largefile --prefix=`pwd`/../install/lib_s
make CC=/opt/xtools/arm920t/bin/arm-linux-gcc CFALGS=--static LDFLAGS=-static     
           

可能出錯:

解決:在出錯檔案出錯行之前添加

#ifdef __aligned_u64
#undef __aligned_u64
#endif
#define __aligned_u64 unsigned long long __attribute__((__aligned(64)))
           
make install
           

将install/lib_s/lib下的所有檔案拷貝到開發闆的lib下

将install/lib_s/sbin下的xtables-multi拷貝到開發闆的bin下并重命名為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.16.1//該網關是路由器網關
/*啟動 dhcp服務*/
>: udhcpd -f udhcpd.conf
           

最後再來總結一下:

對于DM9000有線網卡,Linux3.0核心已經有其對應的驅動程式,對核心做稍微的修改就可以使能網卡,這裡我就不多說了。隻要保持和接入的路由器處于同一網絡,網關設定正确就可以正常上網。對于同一路由器下的其他主機,如果不能ping通,很大的可能是主機的防火牆沒有關閉,關閉防火牆一般就可以通信了。還有對某個網址ping,如果不通,很大的可能是沒有使能DNS服務,對于工作在應用層的協定,通常在檔案系統中添加相應的配置即可通過域名通路目标網絡,而不需要修改Linux核心。

對于無線網卡rt3070,首先要有USB驅動才能使能網卡。

STA模式,簡單的來說,就是通過wifi無線上網。修改linux核心提供rt3070的驅動和使能某些功能,此時網卡插上後,ifconfig檢視已經擁有的網卡,可以看到wlan0即無線網卡,接下來要是能網卡,需要添加rt2870.bin的固件到開發闆的lib/firmware下,此時使用ifconfig wlan0 up使能無線網卡,現在想要連接配接路由器上網,還需要無線上網的工具wpa_supplicant,主要是wpa_supplicant, wpa_cli, wpa_passphrase這三個工具,該工具的制作和使用會涉及到另外兩個庫openssl和libnl,在wpa_supplicant工具使用時,會使用到openssl庫編譯後的的兩個動态庫libssl.so 和 libcrypto.so,主要用于對無線上網的安全保證,而wpa_suppllicant庫編譯生成三個工具,則需要libnl庫和openssl庫編譯生成的相應庫和頭檔案的支援。最後就是使用wpa_supplicant的工具實作無線上網,此時還需要制作一個配置檔案,對上網的路由器的資訊進行配置,使用wpa_suppllicant指令連接配接路由器,實作上網,當然如不想手動配置無線ip,可以添加啟動檔案和dhcp服務。

相關參考:

wpa_supplicant: http://blog.chinaunix.net/uid-29181887-id-4826168.html

http://velep.com/archives/830.html

AP模式,簡單來說就是路由器功能,此功能要求路由器擁有有線網卡(DM9000)作為WAN口,連接配接上一級路由器擷取網絡,和無線網卡rt3070作為LAN口,實作無線網絡功能。對于無線路由功能,也要配置核心提供相關配置和驅動。然後就是hostapd服務的添加,編譯hostapd庫需要openssl庫和libnl庫編譯後的頭檔案和庫的支援,編譯生成的hostapd工具就是我們的目标工具,改工具還需要libnl庫的libnl.so.1動态庫支援(移植到開發闆的lib下),将hostapd移植到開發闆的usr/bin下或者bin下,同時設定hostapd配置檔案配置wifi的相關資訊,使能無線網卡啟動hostapd服務,我們就可以收到相應的我們在hostapd配置檔案中設定的WIFI資訊。此時沒有ip配置設定和防火牆,是無法上網的。配置DHCP動态配置設定IP,建立udhcp配置檔案,配置無線網卡WAN口ip(wlan0 ip),指令udhcp啟動配置檔案來啟動DHCP服務,此時連接配接rt3070建立的路由器,會有配置設定ip資訊但不能上網。最後移植防火牆,拷貝iptables庫編譯後的lib庫下所有庫檔案和lib/sbin下的xtables-multi工具到開發闆的lib和bin下,修改xtables-multi名為iptables,進行一些配置,實作防火牆的建立,此時再hostapd啟動WIFI服務,可以正常連接配接到WIFI并上網。

對于同一區域網路的SSH的登入,隻需要在路由器配置端口轉發,而對于不同區域網路的SSH登入,需要有公網IP和端口轉發。對于擁有公網ip的每一級路由器下,都需要進行下一級的路由器的端口轉發。