FL2440添加RT3070实现无线上网功能---AP模式
>>>想要让网卡设备成为AP,就要移植Hostapd
rtl3070的中文名为雷凌3070,是台湾雷凌科技有限公司生产的一款专用于usb无线网卡的处理芯片,是一种低功耗高度集成的MAC / BBP和2.4G RF单一芯片,支持300Mbps的吞吐量。
RT3070无线网卡有两种工作模式STA模式和软AP模式,分别由STA驱动和软AP驱动来实现。
AP分为硬AP(硬件完成AP功能)和软AP(软件完成AP功能)
无线路由器是硬AP; Hostapd是软AP。
基于FL2440上面已经使能的DM9000有限网卡和USB驱动,可以使用DM9000网卡实现介入网线实现上网功能。
STA驱动支持无线网卡工作在STA模式下,可以连接到网络实现上网功能。而软AP的驱动支持无线网卡工作在软AP模式下,可以作为一个软的接入点,实现无线路由器功能。
一、添加makemeunconfig选项支持wireless无线上网
现在开始在Linux-3.0内核中添加支持:
[*] 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 --->
编译生成内核烧录到开发板。
二、配置无线网络的工具移植
Openssl移植
Hostapd依赖于openssl与libnl这两个库。所以首先要移植openssl与libnl,openssl在前面使能RT3070 STA模式的过程中已经移植了,可以查看所述。
#下载源码,解压源码
[yan@localhost wireless]$ wget https://www.openssl.org/source/old/0.9.x/openssl-0.9.8i.tar.gz
[yan@localhost openssl-0.9.8i]$ mkdir install
#修改Makefile
29 INSTALLTOP=/home/yan/FL2440_openfile/3rdparty/wireless/openssl-0.9.8i/install
32 OPENSSLDIR=/home/yan/FL2440_openfile/3rdparty/wireless/openssl-0.9.8i/install
62 CC=/opt/xtools/arm920t/bin/arm-linux-gcc
69 AR=/opt/xtools/arm920t/bin/arm-linux-ar $(ARFLAGS) r
70 RANLIB=/opt/xtools/arm920t/bin/arm-linux-ranlib
#编译、安装
[yan@localhost openssl-0.9.8i]$ make #make 和 make install 一起会错误不知道是为什么
[yan@localhost openssl-0.9.8i]$ make install
#复制
复制libcrypto.a和libssl.a到开发板上的/lib/,并赋予权限
linbl移植
libnl核心库提供了通过netlink socket连接所需要的基础函数,用于处理socket的链接和断开、数据收发、消息创建分析,提供用户可定制接收状态机,并提供一种抽象的数据类型框架,用于简化netlink 协议对象的实现。 linbl包括四个主要的lib,libnl,libnl-genl,libnl-route,libnl-nf。
更多关于linbl和netlink的介绍,大家可以参考https://www.tuicool.com/articles/j6f6z2
下面进行libnl的移植操作:
hostapd使用的libnl库为1.x版本,2.0版本的API与1.0的可能不兼容,因此我直接移植1.1版本。
[yan@localhost libnl]$ wget http://www.infradead.org/~tgr/libnl/files/libnl-1.1.tar.gz
[yan@localhost libnl]$ tar zvxf libnl-1.1.tar.gz
[yan@localhost libnl]$ ls
libnl-1.1 libnl-1.1.tar.gz
#进入libnl目录配置安装
[yan@localhost libnl-1.1]$ mkdir install
[yan@localhost libnl-1.1]$ ./configure --prefix=`pwd`/install --host=arm-linux
[yan@localhost libnl-1.1]$ make CC=/opt/dl/buildroot-2012.08/ARM920t/usr/bin/arm-linux-gcc
[yan@localhost libnl-1.1]$ sudo make install
[yan@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目录下。
In file included from addr.c:28:
../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
make[2]: *** [addr.o] Error 1
make[1]: *** [all] Error 2
make: *** [all] Error 2
解决方法是:在vi include/netlink-local.h 里面增加头文件#include <limits.h>然后继续编译.
Hostapd移植
hostapd 是一个用户态用于AP和认证服务器的守护进程。它实现了IEEE 802.11相关的接入管理,IEEE 802.1X/WPA/WPA2/EAP 认证, RADIUS客户端,EAP服务器和RADIUS 认证服务器。
在linux系统中,使用hostapd可以实现WIFI的无线接入热点(AP)。
#下载源码,解压源码
[yan@localhost wireless]$ wget http://w1.fi/releases/hostapd-1.0.tar.gz
[yan@localhost wireless]$ tar -xzvf hostapd-1.0.tar.gz
#修改配置文件
[yan@localhost wireless]$ cd hostapd-1.0
[yan@localhost hostapd-1.0]$ cd hostapd/
[yan@localhost hostapd]$ cp defconfig .config
#修改一:.config
[yan@localhost hostapd]$ vim .config
22 # Driver interface for drivers using the nl80211 kernel interface
23 CONFIG_DRIVER_NL80211=y
#修改二:修改Makefile
12 CFLAGS += -I/home/yan/FL2440_openfile/3rdparty/wireless/libnl-1.1/install/include
13 CFLAGS += -I/home/yan/FL2440_openfile/3rdparty/wireless/openssl-0.9.8i/install/include
14
15 LIBS += -L/home/yan/FL2440_openfile/3rdparty/wireless/libnl-1.1/install/include
16 LIBS += -L/home/yan/FL2440_openfile/3rdparty/wireless/openssl-0.9.8i/install/lib
17
18 LDFLAGS += -L/home/yan/FL2440_openfile/3rdparty/wireless/libnl-1.1/install/lib
19 LDFLAGS += -L/home/yan/FL2440_openfile/3rdparty/wireless/openssl-0.9.8i/install/lib
#编译生成Hostapd工具
[yan@localhost hostapd]$ make CC=/opt/xtools/arm920t/bin/arm-linux-gcc
[yan@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
hostapd.conf解释:
ssid:别人所看到的我们这个无线接入点的名称;
hw_mode:指定802.11协议,包括 a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g;
channel:设定无线频道;
interface:接入点设备名称,注意不要包含ap后缀,即如果该设备称为wlan0ap,填写wlan0即可;
bridge:指定所处网桥,对于一个同时接入公网、提供内部网和无线接入的路由器来说,设定网桥很有必要;
driver:设定无线驱动,我这里是nl80211;
macaddr_acl:可选,指定MAC地址过滤规则,0表示除非在禁止列表否则允许,1表示除非在允许列表否则禁止,2表示使用外部RADIUS服务器;
accept_mac_file:指定允许MAC列表文件所在;
deny_mac_file:指定禁止MAC列表文件所在;
查看路由器设置:
启动wlan0
>: ifconfig wlan0 up
启动守护进程
/etc >: hostapd -B hostapd.conf
Configuration file: hostapd.conf
phy1 -> rt2x00mac_conf_tx: Info - Configured TX queue 0 - CWmin: 3, CWmax: 4, Aifs: 2, TXop: 102.
phy1 -> rt2x00mac_conf_tx: Info - Configured TX queue 1 - CWmin: 4, CWmax: 5, Aifs: 2, TXop: 188.
phy1 -> rt2x00mac_conf_tx: Info - Configured TX queue 2 - CWmin: 5, CWmax: 10, Aifs: 3, TXop: 0.
phy1 -> rt2x00mac_conf_tx: Info - Configured TX queue 3 - CWmin: 5, CWmax: 10, Aifs: 7, TXop: 0.
Using interface wlan0 with hwaddr 00:a1:b0:40:6f:1e and ssid 'FL2440'
random: Cannot read from /dev/random: Resource temporarily unavailable
random: Only 0/20 bytes of strong random data available from /dev/random
random: Not enough entropy pool available for secure operations
WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects
phy1 -> rt2x00mac_conf_tx: Info - Configured TX queue 0 - CWmin: 2, CWmax: 3, Aifs: 1, TXop: 47.
phy1 -> rt2x00mac_conf_tx: Info - Configured TX queue 1 - CWmin: 3, CWmax: 4, Aifs: 1, TXop: 94.
phy1 -> rt2x00mac_conf_tx: Info - Configured TX queue 2 - CWmin: 4, CWmax: 6, Aifs: 3, TXop: 0.
phy1 -> rt2x00mac_conf_tx: Info - Configured TX queue 3 - CWmin: 4, CWmax: 10, Aifs: 7, TXop: 0.
无线局域网标准 IEEE 802.11协议:
*IEEE 802.11, 1997年,原始标准(2Mbit/s,工作在2.4GHz)。
*IEEE 802.11a,1999年,物理层补充(54Mbit/s,工作在5GHz)。
*IEEE 802.11b,1999年,物理层补充(11Mbit/s工作在2.4GHz)。
*IEEE 802.11g,2003年,物理层补充(54Mbit/s,工作在2.4GHz)。
使用最多的应该是802.11n标准,工作在2.4GHz频段,可达600Mbps(理论值)
启动成功会出现上面的打印信息,使用ifconfig命令可以看到多了一个如下的网络接口:
>: ifconfig
现在通过手机或其他终端设备可以搜索到SSID为FL2440的wifi热点信号,并通过预设的秘钥12345678可以连上热点。然而此时并不能上网,因为还没有分配IP地址。
配置DHCP
为了使连上热点的中断设备能够自动获取IP,我们需要启用DHCP服务。busybox中已经集成了udhcpd,我们可以直接对busybox-1.20.2/examples/udhcpd/ 下的配置文件udhcpd.conf进行修改,也可以在开发板上/etc目录下新建一个名为udhcpd.conf的配置文件。内容如下:
start 192.168.5.20 #自动分配的IP范围
end 192.168.5.254
interface wlan0 #网络接口为wlan0
opt dns 114.114.114.114
option subnet 255.255.255.0
option lease 18000 #自动更新IP的时间
opt router 192.168.5.1 #这里是RT3070作为Router的IP,要和上一个接入的AP(我这里是192.168.3.1)不在一个网段内
设置并启动DHCP服务
>mkdir -p /var/lib/misc/
>touch /var/lib/misc/udhcpd.leases //创建租赁文件
>ifconfig wlan0 192.168.5.1 netmask 255.255.255.0 //设置接入点
>echo "nameserver 114.114.114.114" > /etc/resolv.conf //DNS
/etc >: udhcpd -f /etc/udhcpd.conf //启动DHCP,此时用手机连上FL2440『路由器』将打印以下信息
udhcpd: started, v1.27.1
udhcpd: sending OFFER of 192.168.5.20 /*分配给手机的IP地址为192.168.3.20*/
udhcpd: sending OFFER of 192.168.5.20
udhcpd: sending OFFER of 192.168.5.20
udhcpd: sending OFFER of 192.168.5.20
在开发板上用ping命令测试与手机的连通性
~ >: ping 192.168.5.20
PING 192.168.5.20 (192.168.5.20): 56 data bytes
64 bytes from 192.168.5.20: seq=0 ttl=64 time=75.000 ms
64 bytes from 192.168.5.20: seq=1 ttl=64 time=90.000 ms
64 bytes from 192.168.5.20: seq=2 ttl=64 time=5.000 ms
64 bytes from 192.168.5.20: seq=3 ttl=64 time=100.000 ms
64 bytes from 192.168.5.20: seq=4 ttl=64 time=35.000 ms
成功返回数据,说明此时手机已经连接上『FL2440路由器』并且成功获取到IP。然而此时,依然还不能接入互联网。要使我们的『FL2440路由器』连上因特网,我们还需要移植iptables。
移植iptables
iptables是功能十分强大的基于linux内核的防火墙,iptables内部集成了filter,nat,mangle三张表。
filter表负责过滤数据包,包括的规则链有,input,output和forward;
Nat表则涉及到网络地址转换,包括的规则链有,prerouting,postrouting和output;
Mangle表则主要应用在修改数据包内容上,用来做流量整形的。
iptables更详细的介绍:http://www.cnblogs.com/metoy/p/4320813.html
#下载源码,解压源码
[yan@localhost iptables]$ wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/iptables-1.4.12.tar.bz2
#iptables-1.4.12版本课当前使用的交叉编译器不适应,经过多次试验,所以换成iptables-1.4.21
[yan@localhost wireless]$ wget http://www.netfilter.org/projects/iptables/files/iptables-1.4.21.tar.bz2
[yan@localhost wireless]$ tar -xjf iptables-1.4.21.tar.bz2
#安装iptables
[yan@localhost wireless]$ cd iptables-1.4.21
[yan@localhost iptables-1.4.21]$ mkdir install
[yan@localhost iptables-1.4.21]$ ./configure --host=arm-linux --enable-static --disable-shared --disable-ipv6 --disable-largefile --prefix=`pwd`/install
[yan@localhost iptables-1.4.21]$ make CC=/opt/xtools/arm920t/bin/arm-linux-gcc CFALGS=--static LDFLAGS=-static
[yan@localhost iptables-1.4.21]$ make install
[yan@localhost install]$ ls
bin include iptab_lib.tar.bz2 lib sbin share
[yan@localhost install]$ cd sbin/
[yan@localhost sbin]$ ls
iptables iptables-restore iptables-save xtables-multi
[yan@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。
开发板iptables配置
#我的目的是让wlan0提供AP,那么首先要开发板有网络
#我的做法是让eth0有网络,再通过eth0转发wlan0的流量
#第一步:让有线网络eth0联网
#网线直插上级路由器,通过DHCP赋予ip地址
>: ifconfig eth0 up
>: udhcpc -i eth0 #上行路由器不会自行给ARM分配一个ip,只能自己申请,并修改到同一个网段内
>: route add default gw 192.168.2.1 #给eth0增加上行网关
>: ping 192.168.2.1
>: ping 4.2.2.2
#第二步:分配wlan0一个不同网段的ip地址,作为AP
>: ifconfig wlan0 192.168.5.1
#所有去往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服务
/etc >: udhcpd -f udhcpd.conf
现在网络已经连上,并且可以提供AP热点