基于Zynq的RT3070 WIFI + hostapd 实现Wifi和WifiAP

之前的博客实现了编译RT3070的驱动程序实现STA模式和SoftAP模式的wifi,这里实现另一种方式,貌似是现在比较新的,那两种也可实现就是略旧。


主机开发环境:ubuntu14.04
交叉编译器:arm-xilinx-linux-gnueabi-gcc
Linux内核版本:Linux-3.6.0
开发平台:zynq数字板
作者:zhu


感谢http://blog.csdn.net/zhengnice/article/details/51694474
http://www.07net01.com/2015/07/888012.html两篇博客为本次开发提供的宝贵经验。

1:内核编译

内核是使用德致伦官方的xilinx内核linux-digilent-master,
github地址:德致伦linux-xilinx-master
我的CSDN下载地址:

内核配置用的之前的配置文件:digilent_zed_1_defconfig,
配置文件
然后参考网上的配置:

[*] Networking support  --->  
        -*-   Wireless  --->
     <*>   cfg80211 - wireless configuration API                        
     [ ]     nl80211 testmode command                                    
     [ ]     enable developer warnings                               
     [ ]     cfg80211 regulatory debugging                             
     [*]     enable powersave by default                             
     [ ]     cfg80211 DebugFS entries                                 
     [*]     cfg80211 wireless extensions compatibility                
     [*]   Wireless extensions sysfs files                               
     {*}   Common routines for IEEE802.11 drivers                          
     [ ]   lib80211 debugging messages                               
     <*>   Generic IEEE 802.11 Networking Stack (mac80211)                 
                  Default rate control algorithm (Minstrel)  --->                 
     [ ]   Enable mac80211 mesh networking (pre-802.11s) support            
     [ ]   Export mac80211 internals in DebugFS                             
     [ ]   Select mac80211 debugging features  ---> 
Device Drivers  --->
       Generic Driver Options  --->
      (/sbin/hotplug) path to uevent helper                                  
      [ ] Maintain a devtmpfs filesystem to mount at /dev                    
      [ ] Select only drivers that don't need compile-time external firmware
      [ ] Prevent firmware from being built  
      -*- Userspace firmware loading support                                
      [*]   Include in-kernel firmware blobs in kernel binary              
      ()    External firmware blobs to build into the kernel binary         
      [ ] Driver Core verbose debug messages                                 
      [ ] Managed device resources verbose debug messages
    [*] Network device support  --->
           [*]   Wireless LAN  --->
               <*>   Ralink driver support  --->
               < >   Ralink rt2500 (USB) support                                    
               < >   Ralink rt2501/rt73 (USB) support                                 
               <*>   Ralink rt27xx/rt28xx/rt30xx (USB) support                      
                 [ ]     rt2800usb - Include support for rt33xx devices                 
                 [ ]     rt2800usb - Include support for rt35xx devices (EXPERIMENTAL)  
                 [ ]     rt2800usb - Include support for rt53xx devices (EXPERIMENTAL)  
                 [ ]     rt2800usb - Include support for unknown (USB) devices          
                 [*]   Ralink debug output

这样内核配置第一步就实现了STA模式,这里主要是加载了linux内核源码里面的驱动。

2:wifi工具移植

目前可以使用wireless-tools或wpa_supplicant工具来配置无线网络。但要注意对无线网络的配置是全局性的,而非针对具体的接口。wpa_supplicant是一个较好的选择,但缺点是它不支持所有的驱动。另外,wpa_supplicant目前只能连接到那些你已经配置好ESSID的无线网络。而wireless-tools支持几乎所有的无线网卡和驱动,但它不能连接到那些只支持WPA的AP。

我两个都移植了,但调试时wpa_supplicant不怎么好使,也没费劲调,就用wireless-tools连接了没有密码的路由器。
Wireless tools for Linux是一个Linux命令行工具包,用来设置支持Linux Wireless Extension的无线设备。

下载:wireless_tools.29.tar.gz
wireless-tools
解压:

#tar zxvf wireless_tools.29.tar.gz
#cd  wireless_tools.29

修改Makefile

ifndef PREFIX
  PREFIX = /home/zhu/my_project/arm/wifi_sta/wifi_lib/wirelesstool/
endif

## Compiler to use (modify this for cross compile).
CC = /home/zhu/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-gcc
## Other tools you need to modify for cross compile (static lib only).
AR = /home/zhu/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-ar
RANLIB = /home/zhu/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-ranlib
#CC = /usr/local/arm/2.95.3/bin/arm-linux-gcc
#AR = /usr/local/arm/2.95.3/bin/arm-linux-ar
#RANLIB = /usr/local/arm/2.95.3/bin/arm-linux-ranlib

编译:make
将生成的命令工具:iwlist、iwconfig、iwpriv,libiw.so.29 等拷贝到目标板上,路径为:分别把他们放到开发板的/bin和/lib目录下,并赋予权限chmod 777 iw* libiw.so.29

我这里是在arm上写了个脚本.sh

ln -s /mnt/wifi_lib/libiw.so.29 /lib/libiw.so.29
ln -s /mnt/wifi_lib/iwconfig /bin/iwconfig
ln -s /mnt/wifi_lib/iwspy /bin/iwspy
ln -s /mnt/wifi_lib/iwevent /bin/iwevent
ln -s /mnt/wifi_lib/iwpriv /bin/iwpriv
ln -s /mnt/wifi_lib/iwlist /bin/iwlist
ln -s /mnt/wifi_lib/iwgetid /bin/iwgetid

因为我的ramdisk镜像设置的比较小,copy过去的话空间不够,就做了个链接。效果一样。

接下来移植openssl
下载:openssl-0.9.8e.tar.gz
解压后修改Makefile:

INSTALLTOP=/home/zhu/my_project/7Z030/software/wifi_sta/wifi_lib/ssl

# Do not edit this manually. Use Configure --openssldir=DIR do change this!
OPENSSLDIR=/home/zhu/my_project/7Z030/software/wifi_sta/wifi_lib/ssl

CC= /home/zhu/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-gcc
CFLAG= -O
DEPFLAG= -DOPENSSL_NO_CAMELLIA -DOPENSSL_NO_GMP -DOPENSSL_NO_MDC2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 
PEX_LIBS= 
EX_LIBS= 
EXE_EXT= 
ARFLAGS= 
AR=/home/zhu/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-ar $(ARFLAGS) r
RANLIB= /home/zhu/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-ranlib
PERL= /usr/bin/perl
TAR= tar
TARFLAGS= --no-recursion
MAKEDEPPROG=makedepend

编译:
sudo make & make install 并拷贝libcrypto.a ; libssl.a到开发板的/lib下
同样做了软链接

ln -s /mnt/wifi_lib/libssl.a /lib/libssl.a
ln -s /mnt/wifi_lib/libcrypto.a /lib/libcrypto.a

因为没用wpa_supplicant工具设置wifi,这里就不表述了,网上编译wpa_supplicant的文章很多,百度参考下就好了。

3:启动wifi 连接路由器

这里先启动wifi连接路由器,这个版本不使用驱动源码编译出的RT3070.STA和.ko了,用linux内核源码中勾选加rt2870.bin加载完成。

rt2870.bin下载地址:rt2870.bin
也可以到内核网站
git clone git://git.kernel.org/pub/scm/linux/kernel/git/dwmw2/linux-firmware.git
把各种固件都下了。

这个文件要复制到板子上的/lib/firmware/中,不然会报错
这里写图片描述
将下载好的固件 RT2870.bin并放入/lib文件下新创建的firmware目录中就可以。脚本里:文件系统/lib中没有firmware目录,新建一个。

mkdir /lib/firmware
cp /mnt/wifi_lib/rt2870.bin /lib/firmware

这样开机启动时运行脚本.sh,就可以ifconfig -a 看到wlan0了,表示wifi的驱动移植完毕。

之后在板子上运行以下命令,或者在启动脚本里直接运行。

ifconfig wlan0 up
ifconfig wlan0 192.168.1.13
iwconfig wlan0 ESSID TP-LINK_9D9608
#udhcpc -i wlan0 #下面再说这个命令

我的路由器名字时TP-LINK_9D9608,没有密码,设置192.168.1.13是wlan0的IP地址,这样就可以在PC上运行telnet 192.168.1.13登陆arm上的操作系统了。

这里解决了之前的方式在使用无线时必须将有线网络禁止
ifconfig eth0 down。这种方式可以同时登陆有线和无线wifi,IP地址不同而已,我的有线和无线连在了一个路由器上,路由器地址192.168.1.1 。

也可以不给wifi设置IP地址,用udhcpc动态随机获取IP,这种可以连接外网。
制作文件系统用到了busybox,这里就有udhcp工具。
从busybox的examples/udhcp/下copy好simple.script文件到开发板/usr/share/udhcpc/下(busybox里面默认的目录文件是/usr/share/udhcpc/default.script),并重命名为default.script.

没有/share/udhcpc/目录,新建一个。

mkdir -p /usr/share/udhcpc/
cp /mnt/wifi_lib/default.script /usr/share/udhcpc/

这样在板子上运行或在脚本里书写下面命令,就给wifi随机一个IP。

udhcpc -i wlan0

4:配置wifi+hostapd作为热点

首先要对内核配置支持SoftAP参考了下网上的教程对内核做了配置改动,但我的内核编译好之后,不能启动,找了好久原因也没找到。因为我的内核是支持SoftAP模式的,在之前的博客中编译的SoftAP源码之后是可以启动的,网上的教程改动的太多了,我自己改动了点,最后也成功了。

[*] Networking support  --->
Networking option  ---> 
<*> Packet socket
<*> Unix domain sockets                                         
            < > PF_KEY sockets                                                    
            [*] TCP/IP networking                                        
            [ ]   IP: advanced router                                           
            [*]   IP: kernel level autoconfiguration                                    
            [*]     IP: DHCP support                                               
            [*]     IP: BOOTP support                                               
            [*]     IP: RARP support                                               
            < >   IP: tunneling                                                    
            < >   IP: GRE demultiplexer                                            
            [*]   IP: multicast routing 
[*] Networking support  --->
   <*>   RF switch subsystem support  ---> //注意要选不然在使用hostapd命令时会出错

配置如果没有build in RF选项,在使用hostapd命令时会出错
这里写图片描述

接下来就是工具移植:
I:这里用了上面移植的openssl.0.9.8e,这里就不继续介绍了,参考上面的编译。

II:移植libnl
下载地址:libnl-1.1.tar.gz

# mkdir install

./configure --prefix=/home/zhuzhu/downloads/libnl-1.1/install(指定安装路径)

#  make CC= /home/zhu/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-gcc

#sudo make install 

将install下的lib文件夹中的libnl.so.1拷贝到开发板/lib目录下。
在脚本里添加:

ln -s /mnt/wifi_lib/libnl.so.1.1 /lib/libnl.so.1

III:移植Hostapd
下载:hostapd-1.0.tar.gz
解压后:

# cp defconfig .config
# vim .config

找到这句代码:去掉注释。
#CONFIG_DRIVER_NL80211=y

同样根据你的要求配置其他选项.基本上,只要配置这行就足够让hostapd运行WPA/WPA2 验证和加密。

修改Makefile,分别添加openssl和libnl的头文件和库路径,openssl的路径和上面不太一样,我复制到下面目录了。

CFLAGS += -I/home/zhu/downloads/libnl-1.1/install/include
CFLAGS += -I/home/zhu/coding/openssl-0.9.8e/install/include
LIBS += -L/home/zhu/downloads/libnl-1.1/install/lib
LDFLAGS += -L/home/zhu/downloads/libnl-1.1/install/lib
LIBS += -L/home/zhu/coding/openssl-0.9.8e/install/lib
LDFLAGS += -L/home/zhu/coding/openssl-0.9.8e/install/lib

编译

# make CC=/home/zhu/CodeSourcery/Sourcery_CodeBench_Lite_for_Xilinx_GNU_Linux/bin/arm-xilinx-linux-gnueabi-gcc
# sudo make install

将在/usr/local/bin/生成可执行命令hostapd和配置文件hostapd.conf。
这里不make install 也可以,就在本目录下找hostapd文件和.conf 。

修改hostapd.conf文件

interface=wlan0
driver=nl80211
ssid=chuhang-1
channel=1
hw_mode=g
ignore_broadcast_ssid=0
auth_algs=1
#wpa=3
#wpa_passphrase=11111111
#wpa_key_mgmt=WPA-PSK   
#wpa_pairwise=TKIP
#rsn_pairwise=CCMP
上面各项解释如下: 
(1) ssid:无线路由器发射的wifi名称; 
(2) hw_mode:指定802.11协议,包括 a = IEEE 802.11a, b = IEEE 802.11b, g = IEEE 802.11g;
========================================================
无线局域网标准 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(理论值) 
========================================================
(3)channel:设定无线频道;
(4)interface:接入点设备名称,注意不要包含ap后缀,即如果该设备称为wlan0ap,填写wlan0即可;
(5)driver:设定无线驱动,我这里是nl80211;
(6)auth_algs=1 
其中auth_algs指定采用哪种认证算法,采用位域(bit fields)方式来制定,其中第一位表示开放系统认证(Open System Authentication, OSA),第二位表示共享密钥认证(Shared Key Authentication, SKA)。我这里设置alth_algs的值为1,表示只采用OSA;
(7)wpa:指定WPA类型,这是一个位域值(bit fields),第一位表示启用WPA,第二位表示启用WPA2。在我的配置中,无论设置成1、2或3,都可以正常连接
(8)wpa_passphrase:WPA/WPA2加密需要指定密钥,这个选项就是配置WPA/WPA2的密钥。注意wpa_passphrase要求8~63个字符。另外还可以通过配置wpa_psk来制定密钥,不过要设置一个256位的16进制密钥,不适合我们的需求;
(9)wpa_pairwise/rsn_pairwise:如果启用了WPA,需要指定wpa_pairwise;如果启用了WPA2,需要指定 rsn_pairwise,或者采用wpa_pairwise的设定。

启动板子,执行

ln -s /mnt/wifi_lib/hostapd /bin/hostapd
ifconfig wlan0 up 
ifconfig wlan0 192.168.0.5
hostapd -B hostapd.conf
#udhcpd /etc/udhcpd.conf #这个下面解释

在板子上直接执行hostapd -B hostapd.conf 或在脚本里添加即可;如果编译过程出现错误,请自行检查你的libnl库和openssl库是否是使用GCC交叉编译后可以到ARM板上跑的库。

这样就可以作为热点了,用电脑连接登陆完全可以,只不过要把PC的IP设成192.168.0.x,才可以。

使用udhcpd就不用设置PC的IP地址了。和udhcpc相似。

修改busybox/examples/udhcp/udhcpd.conf然后拷贝到开发板/etc目录下
因为是实现最基础的功能,所以我们只要简单的修改地址池、默认网关以及dns即可,以下是我的配置:

# The start and end of the IP lease block
start       192.168.0.20
end     192.168.0.254

# The interface that udhcpd will use
interface   wlan0

opt dns 192.168.0.1 192.168.0.10
option  subnet  255.255.255.0
opt router  192.168.0.1
#opt    wins    192.168.10.10
#option dns 129.219.13.81   # appended to above DNS servers for a total of 3
option  domain  local
option  lease   864000      # default: 10 days
option  msstaticroutes  10.0.0.0/8 10.127.0.1       # single static route
option  staticroutes    10.0.0.0/8 10.127.0.1, 10.11.12.0/24 10.11.12.1
# Arbitrary option in hex form:
option  0x08    01020304    # option 8: "cookie server IP addr: 1.2.3.4"

运行 就可以启动dhcp服务器了

cp /mnt/wifi_lib/udhcpd.conf /etc
udhcpd  /etc/udhcpd.conf

这样就可以使用wifi作为热点直接登陆了,并且不用设置IP。至此使用hostapd+udhcpd等工具基于内核mac80211驱动框架就实现了RT3070无线网卡的softAP!!!

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