盈鹏飞嵌入式
直播中

605342

13年用户 111经验值
擅长:EMC/MEI设计,控制/MCU
私信 关注
[作品]

【IMX6UL开发板试用体验】OpenWRT中使用无线网卡做中继器与AP热点

本帖最后由 Tony_Ho 于 2017-2-5 11:33 编辑

中继器与AP的实现这里我们假定中继器完成下面这样的一个功能:
Wired Network ---> Wireless Network ---> 上级无线路由器

即从外部插入一个网线,通过imx6UL上面的无线网卡将数据转发到上级的路由器上面, 当然也可以完成下面这样的传送:

Wireless Network(AP mode) --> Wireless Network(Client) --> 上级无线路由器

即设立一个AP热点,相当于一个路由器,然后再将这个AP的数据通过另外一个无线网卡转发到上一级的路由器或者WLAN上面。
用图片来表示如下:

要完成这些功能, 我们需要:
  • 1. 驱动无线网卡
  • 2. 建立AP热点
  • 3. 其中一个wired或者wireless介入到上一级路由器或者WLAN中
  • 4. 使用IPTables来对数据包进行转发


对于1,我们在前面已经完成,对于Wired Network访问WLAN我们也已经在前面完成。所以这篇我们将完成2与3, 以及4。


AP热点的建立建立AP热点,我们使用经典的Hostapd工具来完成, 这个在OpenWRT中已经存在, 直接选上,然后build即可。
Hostapd的使用
然后我们可以查看对应的使用帮助
  1. root@(none):/# hostapd --help
  2. hostapd: invalid option -- '-'
  3. hostapd v2.5
  4. User space daemon for IEEE 802.11 AP management,
  5. IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator
  6. Copyright (c) 2002-2015, Jouni Malinen and contributors

  7. usage: hostapd [-hdBKtv] [-P ] [-e ]
  8.          [-g ] [-G ]
  9.          

  10. options:
  11.    -h   show this usage
  12.    -d   show more debug messages (-dd for even more)
  13.    -B   run daemon in the background
  14.    -e   entropy file
  15.    -g   global control interface path
  16.    -G   group for control interfaces
  17.    -P   PID file
  18.    -K   include key data in debug messages
  19.    -t   include timestamps in some debug messages
  20.    -v   show hostapd version

因此可以确定需要一个config文件, 这个configuration file我们如下填写:
  1. root@(none):/# cat /etc/hostapd_ori.conf
  2. interface=wlan1
  3. ssid=TonyOpenWRT
  4. channel=9
  5. hw_mode=g
  6. ignore_broadcast_ssid=0
  7. auth_algs=1
  8. wpa=3
  9. wpa_passphrase=TonyHo123456
  10. wpa_key_mgmt=WPA-PSK
  11. wpa_pairwise=TKIP
  12. rsn_pairwise=CCMP

即我们创建一个SSID为TonyOpenWRT的热点, wpa_passphrase为密码, 后面的为加密方式, 然后还选择了channel, 这个channel可以通过前面的iw list来查看, 例如其中一个为:
  1. Frequencies:
  2.                         * 2412 MHz [1] (20.0 dBm)
  3.                         * 2417 MHz [2] (20.0 dBm)
  4.                         * 2422 MHz [3] (20.0 dBm)
  5.                         * 2427 MHz [4] (20.0 dBm)
  6.                         * 2432 MHz [5] (20.0 dBm)
  7.                         * 2437 MHz [6] (20.0 dBm)
  8.                         * 2442 MHz [7] (20.0 dBm)
  9.                         * 2447 MHz [8] (20.0 dBm)
  10.                         * 2452 MHz [9] (20.0 dBm)
  11.                         * 2457 MHz [10] (20.0 dBm)
  12.                         * 2462 MHz [11] (20.0 dBm)
  13.                         * 2467 MHz [12] (disabled)
  14.                         * 2472 MHz [13] (disabled)
  15.                         * 2484 MHz [14] (disabled)
后面的【】中的就是channel了。
建立AP
然后我们使用后台运行的方式启动hostapd,并且打开debug信息,便于我们查看:
  1. root@(none):/# hostapd -B -d /etc/hostapd_ori.conf
  2. random: Trying to read entropy from /dev/random
  3. Configuration file: /etc/hostapd_ori.conf
  4. rfkill: Cannot open RFKILL control device
  5. nl80211: RFKILL status not available
  6. nl80211: TDLS supported
  7. nl80211: TDLS external setup
  8. nl80211: Supported cipher 00-0f-ac:1
  9. nl80211: Supported cipher 00-0f-ac:5
  10. nl80211: Supported cipher 00-0f-ac:2
  11. nl80211: Supported cipher 00-0f-ac:4
  12. nl80211: Supported cipher 00-0f-ac:6
  13. nl80211: Using driver-based off-channel TX
  14. nl80211: Use separate P2P group interface (driver advertised support)
  15. nl80211: interface wlan1 in phy phy1
  16. nl80211: Set mode ifindex 9 iftype 3 (AP)
  17. nl80211: Setup AP(wlan1) - device_ap_sme=0 use_monitor=0
  18. nl80211: Subscribe to mgmt frames with AP handle 0xcfd5d8
  19. nl80211: Register frame type=0xb0 (WLAN_FC_STYPE_AUTH) nl_handle=0xcfd5d8 match=
  20. nl80211: Register frame type=0x0 (WLAN_FC_STYPE_ASSOC_REQ) nl_handle=0xcfd5d8 match=
  21. nl80211: Register frame type=0x20 (WLAN_FC_STYPE_REASSOC_REQ) nl_handle=0xcfd5d8 match=
  22. nl80211: Register frame type=0xa0 (WLAN_FC_STYPE_DISASSOC) nl_handle=0xcfd5d8 match=
  23. nl80211: Register frame type=0xc0 (WLAN_FC_STYPE_DEAUTH) nl_handle=0xcfd5d8 match=
  24. nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) nl_handle=0xcfd5d8 match=
  25. nl80211: Register frame type=0x40 (WLAN_FC_STYPE_PROBE_REQ) nl_handle=0xcfd5d8 match=
  26. [  495.443813] IPv6: ADDRCONF(NETDEV_UP): wlan1: link is not ready
  27. nl80211: Add own interface ifindex 9
  28. nl80211: if_indices[16]: 9
  29. phy: phy1
  30. BSS count 1, BSSID mask 00:00:00:00:00:00 (0 bits)
  31. nl80211: Regulatory information - country=00
  32. nl80211: 2402-2472 @ 40 MHz 20 mBm
  33. nl80211: 2457-2482 @ 40 MHz 20 mBm (no IR)
  34. nl80211: 2474-2494 @ 20 MHz 20 mBm (no OFDM) (no IR)
  35. nl80211: 5170-5250 @ 160 MHz 20 mBm (no IR)
  36. nl80211: 5250-5330 @ 160 MHz 20 mBm (DFS) (no IR)
  37. nl80211: 5490-5730 @ 160 MHz 20 mBm (DFS) (no IR)
  38. nl80211: Added 802.11b mode based on 802.11g information
  39. Completing interface initialization
  40. Mode: IEEE 802.11g  Channel: 9  Frequency: 2452 MHz
  41. DFS 0 channels required radar detection
  42. nl80211: Set freq 2452 (ht_enabled=0, vht_enabled=0, bandwidth=20 MHz, cf1=2452 MHz, cf2=0 MHz)
  43.   * freq=2452
  44.   * vht_enabled=0
  45.   * ht_enabled=0
  46. RATE[0] rate=10 flags=0x1
  47. RATE[1] rate=20 flags=0x1
  48. RATE[2] rate=55 flags=0x1
  49. RATE[3] rate=110 flags=0x1
  50. RATE[4] rate=60 flags=0x0
  51. RATE[5] rate=90 flags=0x0
  52. RATE[6] rate=120 flags=0x0
  53. RATE[7] rate=180 flags=0x0
  54. RATE[8] rate=240 flags=0x0
  55. RATE[9] rate=360 flags=0x0
  56. RATE[10] rate=480 flags=0x0
  57. RATE[11] rate=540 flags=0x0
  58. hostapd_setup_bss(hapd=0xcfcf50 (wlan1), first=1)
  59. wlan1: Flushing old station entries
  60. nl80211: flush -> DEL_STATION wlan1 (all)
  61. wlan1: Deauthenticate all stations
  62. nl80211: send_mlme - da= ff:ff:ff:ff:ff:ff noack=0 freq=0 no_cck=0 offchanok=0 wait_time=0 fc=0xc0 (WLAN_FC_STYPE_DEAUTH) nlmode=3
  63. nl80211: send_mlme -> send_frame
  64. nl80211: send_frame - Use bss->freq=2452
  65. nl80211: send_frame -> send_frame_cmd
  66. nl80211: Frame command failed: ret=-16 (Device or resource busy) (freq=2452 wait=0)
  67. wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=0 addr=(nil) key_idx=0 set_tx=0 seq_len=0 key_len=0
  68. wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=0 addr=(nil) key_idx=1 set_tx=0 seq_len=0 key_len=0
  69. wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=0 addr=(nil) key_idx=2 set_tx=0 seq_len=0 key_len=0
  70. wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=0 addr=(nil) key_idx=3 set_tx=0 seq_len=0 key_len=0
  71. Using interface wlan1 with hwaddr 94:0c:6d:7c:12:f6 and ssid "TonyOpenWRT"
  72. Deriving WPA PSK based on passphrase
  73. SSID - hexdump_ascii(len=11):
  74.      54 6f 6e 79 4f 70 65 6e 57 52 54                  TonyOpenWRT     
  75. PSK (ASCII passphrase) - hexdump_ascii(len=12): [REMOVED]
  76. PSK (from passphrase) - hexdump(len=32): [REMOVED]
  77. random: Got 15/20 bytes from /dev/random
  78. random: Only 15/20 bytes of strong random data available from /dev/random
  79. random: Not enough entropy pool available for secure operations
  80. WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects
  81. GMK - hexdump(len=32): [REMOVED]
  82. Key Counter - hexdump(len=32): [REMOVED]
  83. WPA: Delay group state machine start until Beacon frames have been configured
  84. nl80211: Set beacon (beacon_set=0)
  85. nl80211: Beacon head - hexdump(len=62): 80 00 00 00 ff ff ff ff ff ff 94 0c 6d 7c 12 f6 94 0c 6d 7c 12 f6 00 00 00 00 00 00 00 00 00 00 64 00 11 04 00 0b 54 6f 6e 79 4f 70 65 6e 57 52 54 01 08 82 84 8b 96 0c 12 1y
  86. 01 09
  87. nl80211: Beacon tail - hexdump(len=65): 2a 01 04 32 04 30 48 60 6c 30 14 01 00 00 0f ac 02 01 00 00 0f ac 04 01 00 00 0f ac 02 00 00 dd 16 00 50 f2 01 01 00 00 50 f2 02 01 00 00 50 f2 02 01 00 00 50 f2 02 7f 08 00
  88. nl80211: ifindex=9
  89. nl80211: beacon_int=100
  90. nl80211: dtim_period=2
  91. nl80211: ssid - hexdump_ascii(len=11):
  92.      54 6f 6e 79 4f 70 65 6e 57 52 54                  TonyOpenWRT     
  93.   * beacon_int=100
  94. nl80211: hidden SSID not in use
  95. nl80211: privacy=1
  96. nl80211: auth_algs=0x1
  97. nl80211: wpa_version=0x3
  98. nl80211: key_mgmt_suites=0x2
  99. nl80211: pairwise_ciphers=0x18
  100. nl80211: group_cipher=0x8
  101. nl80211: SMPS mode - off
  102. nl80211: beacon_ies - hexdump(len=10): 7f 08 00 00 00 00 00 00 00 40
  103. nl80211: proberesp_ies - hexdump(len=10): 7f 08 00 00 00 00 00 00 00 40
  104. nl80211: assocresp_ies - hexdump(len=10): 7f 08 00 00 00 00 00 00 00 40
  105. WPA: Start group state machine to set initial keys
  106. WPA: group state machine entering state GTK_INIT (VLAN-ID 0)
  107. GTK - hexdump(len=32): [REMOVED]
  108. WPA: group state machine entering state SETKEYSDONE (VLAN-ID 0)
  109. wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=2 addr=0x8dc74 key_idx=1 set_tx=1 seq_len=0 key_len=32
  110. nl80211: KEY_DATA - hexdump(len=32): [REMOVED]
  111.    broadcast key
  112. nl80211: Set wlan1 operstate 0->1 (UP)
  113. netlink: Operstate: ifindex=9 linkmode=-1 (no change), operstate=6 (IF_OPER_UP)
  114. wlan1: interface state UNINITIALIZED->ENABLED
  115. wlan1: AP-ENABLED
  116. wlan1: Setup of interface done.
  117. ctrl_iface not configured!
其中里面用到了加密时候需要的随机数,这个是使用内核中的random节点来生成的,所以我们在内核这两个还需要添加random驱动。
验证AP
在验证之前,我们需要给我们的这个网卡自己一个IP地址, 因此直接:
  1. root@(none):/# ifconfig wlan1 192.168.2.1  
  2. root@(none):/# ifconfig wlan1
  3. wlan1     Link encap:Ethernet  HWaddr 94:0c:6d:7c:12:f6  
  4.           inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
  5.           inet6 addr: fe80::960c:6dff:fe7c:12f6/64 Scope:Link
  6.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  7.           RX packets:131 errors:0 dropped:0 overruns:0 frame:0
  8.           TX packets:106 errors:0 dropped:0 overruns:0 carrier:0
  9.           collisions:0 txqueuelen:1000
  10.           RX bytes:20162 (19.6 KiB)  TX bytes:20954 (20.4 KiB)

然后我们打开手机查看是否可以看到我们的热点:

然后我们使用手动配置IP方式连接热点


连接完成后, 我们可以看到已经连接的状态:

然后在手机上面可以Ping一把确定是否连通:

然后在iMX6UL上面也来ping一把这个手机:
  1. root@(none):/# ping 192.168.2.111
  2. PING 192.168.2.111 (192.168.2.111): 56 data bytes
  3. 64 bytes from 192.168.2.111: seq=0 ttl=64 time=90.952 ms
  4. 64 bytes from 192.168.2.111: seq=1 ttl=64 time=143.093 ms
  5. 64 bytes from 192.168.2.111: seq=2 ttl=64 time=4.508 ms
  6. 64 bytes from 192.168.2.111: seq=3 ttl=64 time=46.859 ms
  7. 64 bytes from 192.168.2.111: seq=4 ttl=64 time=2.539 ms
  8. 64 bytes from 192.168.2.111: seq=5 ttl=64 time=2.697 ms
  9. ^C
  10. --- 192.168.2.111 ping statistics ---
  11. 6 packets transmitted, 6 packets received, 0% packet loss
  12. round-trip min/avg/max = 2.539/48.441/143.093 ms
至此,基本的连接就好了, 如果想要自动获取IP地址,那么我们还需要配置DHCPD。

wpa_supplicant的配置
前面我们使用其中一个网卡,配置好了AP, 接下来我们使用另外一个Wireless作为上一级router的接入设备。 这个可以参考我以前写的文章:Yocto i.MX6 (TQIMX6) (03) : wifi网卡的相关工具wpa_supplicant与dhcp
但是wpa_supplicant的版本升级后, 和hostapd的使用基本一致起来, 因此以前的使用方法稍微有些不同了。


配置
创建一个配置文件,写明需要接入的SSID,密码等信息:
  1. root@(none):/# cat /etc/wpa.conf
  2. ctrl_interface=/var/run/wpa_supplicant
  3. network={
  4.         ssid="XXX"
  5.         proto=WPA
  6.         key_mgmt=WPA-PSK
  7.         pairwise=TKIP
  8.         group=TKIP
  9.         psk="PASSWORD"
  10. }
其中XXX是路由器的热点名字, psk填写Password, 加密方式TKIP需要根据实际情况填写,不明白可以看我以前的文章,或者直接man。运行先创建一个runtime目录:
root@(none):/# mkdir /var/run/wpa_supplicant
然后直接在后台运行:
  1. root@(none):/# wpa_supplicant -B -iwlan0 -c /etc/wpa.conf
  2. Successfully initialized wpa_supplicant
  3. rfkill: Cannot open RFKILL contro[  172.506937] ieee80211 phy0: rt2x00lib_request_firmware: Info - Loading firmware file 'rt2870.bin'
  4. l device
  5. [  172.527112] ieee80211 phy0: rt2x00lib_request_firmware: Info - Firmware detected - version: 0.29
  6. [  173.083412] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready
  7. root@(none):/# [  174.937567] wlan0: authenticate with bc:d1:77:a0:4f:3c
  8. [  175.073446] wlan0: send auth to bc:d1:77:a0:4f:3c (try 1/3)
  9. [  175.082318] wlan0: authenticated
  10. [  175.087051] rt2800u*** 1-1.2.1:1.0 wlan0: disabling HT/VHT due to WEP/TKIP use
  11. [  175.098735] wlan0: associate with bc:d1:77:a0:4f:3c (try 1/3)
  12. [  175.108018] wlan0: RX AssocResp from bc:d1:77:a0:4f:3c (capab=0x411 status=0 aid=5)
  13. [  175.129960] IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
  14. [  175.136678] wlan0: associated
  15. [  176.069997] IPv6: wlan0: IPv6 duplicate address fe80::7edd:90ff:feb2:73b1 detected!

验证
使用wpa_cli看查看连接的状态
  1. root@(none):/# udhcpc -i wlan0
  2. udhcpc (v1.24.1) started
  3. Sending discover...
  4. Sending select for 192.168.1.112...
  5. Lease of 192.168.1.112 obtained, lease time 86400
  6. /etc/udhcpc.d/50default: Adding DNS 192.168.1.1
  7. /etc/udhcpc.d/50default: Adding DNS 0.0.0.0

  8. root@(none):/# ping meitu.com
  9. PING meitu.com (42.62.69.156): 56 data bytes
  10. 64 bytes from 42.62.69.156: seq=0 ttl=42 time=44.499 ms
  11. 64 bytes from 42.62.69.156: seq=1 ttl=42 time=44.956 ms
  12. 64 bytes from 42.62.69.156: seq=2 ttl=42 time=43.122 ms
  13. 64 bytes from 42.62.69.156: seq=3 ttl=42 time=43.387 ms
  14. 64 bytes from 42.62.69.156: seq=4 ttl=42 time=46.467 ms
  15. 64 bytes from 42.62.69.156: seq=5 ttl=42 time=46.267 ms
  16. 64 bytes from 42.62.69.156: seq=6 ttl=42 time=43.843 ms
  17. ^C
  18. --- meitu.com ping statistics ---
  19. 7 packets transmitted, 7 packets received, 0% packet loss
  20. round-trip min/avg/max = 43.122/44.648/46.467 ms
至此, 我们就完成了基本的配置工作。



回帖(3)

那些年儿ing

2017-2-7 10:30:52
多谢楼主分享
举报

ALTIUM

2017-2-7 20:46:50
  传说中的大神啊,不晓得我这样的门外汉什么时候能够入门呢,膜拜下
举报

可依123

2017-7-7 09:37:59
多谢楼主分享
举报

更多回帖

发帖
×
20
完善资料,
赚取积分