fl2440制作无线路由器


作者:潘林 来源:凌云物网智科实验室 时间:2017-05-10

内核需要开启如下配置,在我移植的3.0.56内核已经集成了这些配置

[*] Networking support  --->  
    <*>   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  --->
    [*]   Include in-kernel firmware blobs in kernel binary
    [*] Network device support  --->
        [*]   Wireless LAN  --->
            <*>   Ralink driver support  --->
            <*>   Ralink rt27xx/rt28xx/rt30xx (USB) support
            [*]   Ralink debug output

安装驱动

插入3070网卡后,内核调试信息

> dmesg
usb 1-1.1: new full speed USB device number 5 using s3c2410-ohci
usb 1-1.1: New USB device found, idVendor=148f, idProduct=3070
usb 1-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1.1: Product: 802.11 n WLAN
usb 1-1.1: Manufacturer: Ralink

下载对应的3070固件到/lib/firmware目录下,文件名保持rt2870.bin

wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/rt2870.bin

开启无线网卡

ifconfig wlan0 up

ifconfig打印出wlan0信息

>: ifconfig
wlan0     Link encap:Ethernet  HWaddr 00:A1:B0:40:6F:72  
          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)

连接wifi

上面的步骤只是安装驱动,让无线网卡工作还要”插上网线”

移植openssl库

wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/openssl-1.0.0s.tar.gz
tar zxvf openssl-1.0.0s.tar.gz
cd openssl-1.0.0s
./config no-asm shared --prefix=`pwd`/_install

修改Makefile相应字段

CROSS=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-
CC= $(CROSS)gcc
AR= $(CROSS)ar $(ARFLAGS) r
RANLIB= $(CROSS)ranlib
NM= $(CROSS)nm

继续编译

make
make install

移植wpa_supplicant

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

修改.config相应字段,其中OPENSSL_PATH为上一步openssl的安装路径

CROSS=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-
OPENSSL_PATH=/root/fl2440/3rdapp/openssl/openssl-1.0.0s/_install
CC= $(CROSS)gcc -L$(OPENSSL_PATH)/lib/openssl
CFLAGS += -I$(OPENSSL_PATH)/include
LIBS += -L$(OPENSSL_PATH)lib/openssl

make过后会生成如下几个文件,将其拷贝到开发板/usr/bin

  • wpa_cli (打酱油)
  • wpa_passphrase (一键生成配置文件)
  • wpa_supplicant (连接WiFI)

查看相关依赖库,可见wpa_supplicant前置依赖属于openssllibcrypto.solibssl.so

> arm-linux-readelf -d ./wpa_supplicant
Dynamic section at offset 0x471e8 contains 24 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libssl.so.1.0.0]
 0x00000001 (NEEDED)                     Shared library: [libcrypto.so.1.0.0]
 0x00000001 (NEEDED)                     Shared library: [libdl.so.0]
 0x00000001 (NEEDED)                     Shared library: [libc.so.0]
...
生成配置文件

wpa_supplicant.conf配置文件保留着需要连接APSSIDkey等信息,可以使用wpa_supplicant.conf一键生成

wpa_passphrase dlink-301 cnmbcnmb >> /etc/wpa_supplicant.conf

wpa_supplicant.conf还需要稍微修改一下

ctrl_interface=/var/run/wpa_supplicant
network={
    ssid="dlink-301"
    key_mgmt=WPA-PSK
    proto=WPA2
    group=TKIP CCMP
    pairwise=TKIP CCMP
    psk="cnmbcnmb"
    #psk=56466f08ff364ccbbec3266aeda88cb69619a3914834966cc7e90098985d225c
}

启用wpa_supplicant

wpa_supplicant -B -d -Dwext -i wlan0 -c /etc/wpa_supplicant.conf
网络配置

由于还没有开启DHCP,所以需要手动分配IP等各种配置,这里配置由你的网络环境决定,后面会介绍使用DHCP

#配置IP地址
ifconfig wlan0 192.168.0.168 
#配置默认网关
route add default gw 192.168.0.1
#配置DNS
echo "nameserver 114.114.114.114" > /etc/resolv.conf

接下来进行ping测试

>: ping sfantree.com
PING sfantree.com (123.56.22.253): 56 data bytes
64 bytes from 123.56.22.253: seq=0 ttl=51 time=34.019 ms
64 bytes from 123.56.22.253: seq=1 ttl=51 time=24.689 ms
64 bytes from 123.56.22.253: seq=2 ttl=51 time=25.622 ms
64 bytes from 123.56.22.253: seq=3 ttl=51 time=65.763 ms
64 bytes from 123.56.22.253: seq=4 ttl=51 time=74.489 ms

开启DHCP

busybox已经集成了DHCP的客户端和服务端,源码编译过后的examples/udhcp/simple.script作为DHCP客户端的示例配置文件如下,将其重命名为/usr/share/udhcpc/default.script,特别注意必须给default.script执行权限

#!/bin/sh
# udhcpc script edited by Tim Riker <Tim@Rikers.org>

RESOLV_CONF="/etc/resolv.conf"

[ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }

NETMASK=""
[ -n "$subnet" ] && NETMASK="netmask $subnet"
BROADCAST="broadcast +"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"

case "$1" in
    deconfig)
        echo "Setting IP address 0.0.0.0 on $interface"
        ifconfig $interface 0.0.0.0
        ;;

    renew|bound)
        echo "Setting IP address $ip on $interface"
        ifconfig $interface $ip $NETMASK $BROADCAST

        if [ -n "$router" ] ; then
            echo "Deleting routers"
            while route del default gw 0.0.0.0 dev $interface ; do
                :
            done

            metric=0
            for i in $router ; do
                echo "Adding router $i"
                route add default gw $i dev $interface metric $((metric++))
            done
        fi

        echo "Recreating $RESOLV_CONF"
        echo -n > $RESOLV_CONF-$$
        [ -n "$domain" ] && echo "search $domain" >> $RESOLV_CONF-$$
        for i in $dns ; do
            echo " Adding DNS server $i"
            echo "nameserver $i" >> $RESOLV_CONF-$$
        done
        mv $RESOLV_CONF-$$ $RESOLV_CONF
        ;;
esac

exit 0

执行udhcpc -i wlan0之后即可自动获取IP,执行这个命令后udhcp会以守护进程方式在后台运行

>: udhcpc -i wlan0
udhcpc (v1.20.2) started
Setting IP address 0.0.0.0 on wlan0
Sending discover...
Sending select for 192.168.0.103...
Lease of 192.168.0.103 obtained, lease time 604800
Setting IP address 192.168.0.103 on wlan0
Deleting routers
route: SIOCDELRT: No such process
Adding router 192.168.0.1
Recreating /etc/resolv.conf
 Adding DNS server 192.168.0.1

准备

如果按照上一篇文章成功无线上网之后,在制作路由器前必须断开WiFi连接,好比手机开启流量共享时自动断开已连接的热点,以免发生冲突

关闭WiFi连接

# 关闭DHCP客户端
kill $(ps aux|grep "udhcpc -i wlan0"|grep -v grep|awk '{print $1}')
# 关闭wpa_supplicant
kill $(ps aux|grep wpa_supplicant|grep -v grep|awk '{print $1}')

移植libnl

libnl需要前置依赖openssl库,openssl移植过程在上篇文章有介绍

wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/libnl-1.1.tar.gz
tar zxvf libnl-1.1.tar.gz
cd libnl-1.1
./configure --prefix=`pwd`/_install CC= /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc --host=arm-linux
make CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc
make install

接下来把install里面的libnl.solibnl.so.1libnl.so.1.1移动到开发板的/lib

移植hostapd

编译

tar zxvf hostapd-0.8-snapshot.tar.gz
cd hostapd-0.8-snapshot/cd hostapd/
cp defconfig .config

修改.config,注并添加相应字段

#CONFIG_DRIVER_HOSTAP=y
CONFIG_DRIVER_NL80211=y

修改Makefile,追加以下字段

CROSS=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-
OPENSSL_PATH=/root/fl2440/3rdapp/openssl/openssl-1.0.0s/_install/
LIBNL_PATH=/root/fl2440/3rdapp/wifi/libnl-1.1/_install
CC=$(CROSS)gcc

CFLAGS += -I $(OPENSSL_PATH)/include
CFLAGS += -I $(LIBNL_PATH)/include

LIBS += -L $(LIBNL_PATH)/lib
LIBS += -L $(OPENSSL_PATH)/lib

LDFLAGS += -L $(LIBNL_PATH)/lib
LDFLAGS += -L $(OPENSSL_PATH)/lib

make过后会在hostapd-1.0/hostapd生成可执行文件hostapd,使用readelf查看hostapd前置依赖,前置依赖有openssllibnl,将hostapd复制到开发板/usr/bin

> arm-linux-readelf -d ./hostapd
Dynamic section at offset 0x8700c contains 24 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libnl.so.1]
 0x00000001 (NEEDED)                     Shared library: [libssl.so.1.0.0]
 0x00000001 (NEEDED)                     Shared library: [libcrypto.so.1.0.0]
 0x00000001 (NEEDED)                     Shared library: [libc.so.0]

配置

开发板上新建/etc/hostapd.conf

#开启AP的网卡
interface=wlan0
ssid=FL2440
#所需驱动
driver=nl80211
#频段 中国大陆1-13可选
channel=1
#802.11g 最大速率54Mbps
hw_mode=g
ignore_broadcast_ssid=0
wpa=1
auth_algs=1
# 启用加密 密码12345678
wpa_key_mgmt=WPA-PSK
wpa_passphrase=12345678
wpa_pairwise=TKIP
rsn_pairwise=CCMP

启动

#启动无线网卡
ifconfig wlan0 up
# 解决随机数问题
mv /dev/random /dev/random.bak 
ln -s /dev/urandom /dev/random
# 守护进程启动
/usr/bin/hostapd -B /etc/hostapd.conf

成功开启热点会打印如下信息

> /usr/bin/hostapd -B /etc/hostapd.conf
Configuration file: /etc/hostapd.conf
Using interface wlan0 with hwaddr 00:a1:b0:40:6f:72 and ssid 'FL2440'

并且新增了一个网络接口

> ifconfig
mon.wlan0 Link encap:UNSPEC  HWaddr 00-A1-B0-40-6F-72-00-00-00-00-00-00-00-00-00-00  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:62 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:18156 (17.7 KiB)  TX bytes:0 (0.0 B)

电脑手机都可以接收到AP信号,这时还没有给电脑的无线网卡分配IP,因此提示无网络访问权限

20170509224617.png

DHCP

为了使手机电脑连接上自动获取IP,需要启动DHCP服务器,busybox已经集成了udhcpd

示例配置文件在examples/udhcp/udhcpd.conf,我们在开发板上创建一个/etc/udhcpd.conf

start 10.0.188.100
end 10.0.188.200
interface wlan0
opt dns 114.114.114.114
option subnet 255.255.255.0
opt router 10.0.188.1
option lease 7200

启动DHCP服务器

mkdir -p /var/lib/misc/
touch /var/lib/misc/udhcpd.leases
udhcpd /etc/udhcpd.conf

移植iptables

编译

wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/iptables-1.4.12.tar.bz2
tar xvjf 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
make CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc CFALGS=--static LDFLAGS=-static
make install

_install/sbin下的xtables-multi才是真正的iptables,重命名后复制到开发板的/usr/bin目录下

设置

有线网卡

首先必须保证有线网卡必须可以上网,做好相应的IP配置

采用DHCP

ifconfig eth0 up
udhcpc -i eth0

采用静态IP

ifconfig eth0 up
ifconfig eth0 192.168.0.169
route add default gw 192.168.0.1
echo "nameserver 114.114.114.114" > /etc/resolv.conf

IP各种参数根据你所处的网络环境进行配置

无线网卡

为无线网卡设置一个静态IP

ifconfig wlan0 10.0.188.1
iptables配置
#流量通过eth0接口伪装后转发出去
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
#保持现有连接状态
iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISH -j ACCEPT 
#配置转发端口
iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
内核配置
# 查看内核转发状态
cat /proc/sys/net/ipv4/ip_forward
# 开启内核转发
echo 1 >/proc/sys/net/ipv4/ip_forward
# 自动开启内核转发
echo "# Controls IP packet forwarding" >> etc/sysctl.conf
echo "echo 1 >/proc/sys/net/ipv4/ip_forward" >> /etc/sysctl.conf

测试总结

我寝室一台主路由负责L2TP拨号连接互联网,LAN网段192.168.1.0/24,下面连着我的二级路由LAN``192.168.0.0/24,开发板可以当作三级路由来处理LAN网段10.0.188.0/24,测试的设备连接到三级路由的LAN

附上网络拓扑图

100.png

测试图(延迟还是比较可观)

Screenshot_20170510-082430.jpg

在线咨询
微信号
13554373241
联系方式
135-5437-3241
邮箱
guowenxue@aliyun.com
返回顶部