中繼器與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的使用
然後我們可以查看對應的使用協助
root@(none):/# hostapd --helphostapd: invalid option -- '-'hostapd v2.5User space daemon for IEEE 802.11 AP management,IEEE 802.1X/WPA/WPA2/EAP/RADIUS AuthenticatorCopyright (c) 2002-2015, Jouni Malinen <j@w1.fi> and contributorsusage: hostapd [-hdBKtv] [-P <PID file>] [-e <entropy file>] \ [-g <global ctrl_iface>] [-G <group>] \ <configuration file(s)>options: -h show this usage -d show more debug messages (-dd for even more) -B run daemon in the background -e entropy file -g global control interface path -G group for control interfaces -P PID file -K include key data in debug messages -t include timestamps in some debug messages -v show hostapd version
因此可以確定需要一個config檔案, 這個configuration file我們如下填寫:
root@(none):/# cat /etc/hostapd_ori.conf interface=wlan1ssid=TonyOpenWRTchannel=9hw_mode=gignore_broadcast_ssid=0auth_algs=1wpa=3wpa_passphrase=TonyHo123456wpa_key_mgmt=WPA-PSKwpa_pairwise=TKIPrsn_pairwise=CCMP
即我們建立一個SSID為TonyOpenWRT的熱點, wpa_passphrase為密碼, 後面的為加密方式, 然後還選擇了channel, 這個channel可以通過前面的iw list來查看, 例如其中一個為:
Frequencies: * 2412 MHz [1] (20.0 dBm) * 2417 MHz [2] (20.0 dBm) * 2422 MHz [3] (20.0 dBm) * 2427 MHz [4] (20.0 dBm) * 2432 MHz [5] (20.0 dBm) * 2437 MHz [6] (20.0 dBm) * 2442 MHz [7] (20.0 dBm) * 2447 MHz [8] (20.0 dBm) * 2452 MHz [9] (20.0 dBm) * 2457 MHz [10] (20.0 dBm) * 2462 MHz [11] (20.0 dBm) * 2467 MHz [12] (disabled) * 2472 MHz [13] (disabled) * 2484 MHz [14] (disabled)
後面的【】中的就是channel了。
建立AP
然後我們使用後台啟動並執行方式啟動hostapd,並且開啟debug資訊,便於我們查看:
root@(none):/# hostapd -B -d /etc/hostapd_ori.conf random: Trying to read entropy from /dev/randomConfiguration file: /etc/hostapd_ori.confrfkill: Cannot open RFKILL control devicenl80211: RFKILL status not availablenl80211: TDLS supportednl80211: TDLS external setupnl80211: Supported cipher 00-0f-ac:1nl80211: Supported cipher 00-0f-ac:5nl80211: Supported cipher 00-0f-ac:2nl80211: Supported cipher 00-0f-ac:4nl80211: Supported cipher 00-0f-ac:6nl80211: Using driver-based off-channel TXnl80211: Use separate P2P group interface (driver advertised support)nl80211: interface wlan1 in phy phy1nl80211: Set mode ifindex 9 iftype 3 (AP)nl80211: Setup AP(wlan1) - device_ap_sme=0 use_monitor=0nl80211: Subscribe to mgmt frames with AP handle 0xcfd5d8nl80211: Register frame type=0xb0 (WLAN_FC_STYPE_AUTH) nl_handle=0xcfd5d8 match=nl80211: Register frame type=0x0 (WLAN_FC_STYPE_ASSOC_REQ) nl_handle=0xcfd5d8 match=nl80211: Register frame type=0x20 (WLAN_FC_STYPE_REASSOC_REQ) nl_handle=0xcfd5d8 match=nl80211: Register frame type=0xa0 (WLAN_FC_STYPE_DISASSOC) nl_handle=0xcfd5d8 match=nl80211: Register frame type=0xc0 (WLAN_FC_STYPE_DEAUTH) nl_handle=0xcfd5d8 match=nl80211: Register frame type=0xd0 (WLAN_FC_STYPE_ACTION) nl_handle=0xcfd5d8 match=nl80211: Register frame type=0x40 (WLAN_FC_STYPE_PROBE_REQ) nl_handle=0xcfd5d8 match=[ 495.443813] IPv6: ADDRCONF(NETDEV_UP): wlan1: link is not readynl80211: Add own interface ifindex 9nl80211: if_indices[16]: 9phy: phy1BSS count 1, BSSID mask 00:00:00:00:00:00 (0 bits)nl80211: Regulatory information - country=00nl80211: 2402-2472 @ 40 MHz 20 mBmnl80211: 2457-2482 @ 40 MHz 20 mBm (no IR)nl80211: 2474-2494 @ 20 MHz 20 mBm (no OFDM) (no IR)nl80211: 5170-5250 @ 160 MHz 20 mBm (no IR)nl80211: 5250-5330 @ 160 MHz 20 mBm (DFS) (no IR)nl80211: 5490-5730 @ 160 MHz 20 mBm (DFS) (no IR)nl80211: Added 802.11b mode based on 802.11g informationCompleting interface initializationMode: IEEE 802.11g Channel: 9 Frequency: 2452 MHzDFS 0 channels required radar detectionnl80211: Set freq 2452 (ht_enabled=0, vht_enabled=0, bandwidth=20 MHz, cf1=2452 MHz, cf2=0 MHz) * freq=2452 * vht_enabled=0 * ht_enabled=0RATE[0] rate=10 flags=0x1RATE[1] rate=20 flags=0x1RATE[2] rate=55 flags=0x1RATE[3] rate=110 flags=0x1RATE[4] rate=60 flags=0x0RATE[5] rate=90 flags=0x0RATE[6] rate=120 flags=0x0RATE[7] rate=180 flags=0x0RATE[8] rate=240 flags=0x0RATE[9] rate=360 flags=0x0RATE[10] rate=480 flags=0x0RATE[11] rate=540 flags=0x0hostapd_setup_bss(hapd=0xcfcf50 (wlan1), first=1)wlan1: Flushing old station entriesnl80211: flush -> DEL_STATION wlan1 (all)wlan1: Deauthenticate all stationsnl80211: 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=3nl80211: send_mlme -> send_framenl80211: send_frame - Use bss->freq=2452nl80211: send_frame -> send_frame_cmdnl80211: Frame command failed: ret=-16 (Device or resource busy) (freq=2452 wait=0)wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=0 addr=(nil) key_idx=0 set_tx=0 seq_len=0 key_len=0wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=0 addr=(nil) key_idx=1 set_tx=0 seq_len=0 key_len=0wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=0 addr=(nil) key_idx=2 set_tx=0 seq_len=0 key_len=0wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=0 addr=(nil) key_idx=3 set_tx=0 seq_len=0 key_len=0Using interface wlan1 with hwaddr 94:0c:6d:7c:12:f6 and ssid "TonyOpenWRT"Deriving WPA PSK based on passphraseSSID - hexdump_ascii(len=11): 54 6f 6e 79 4f 70 65 6e 57 52 54 TonyOpenWRT PSK (ASCII passphrase) - hexdump_ascii(len=12): [REMOVED]PSK (from passphrase) - hexdump(len=32): [REMOVED]random: Got 15/20 bytes from /dev/randomrandom: Only 15/20 bytes of strong random data available from /dev/randomrandom: Not enough entropy pool available for secure operationsWPA: Not enough entropy in random pool for secure operations - update keys later when the first station connectsGMK - hexdump(len=32): [REMOVED]Key Counter - hexdump(len=32): [REMOVED]WPA: Delay group state machine start until Beacon frames have been configurednl80211: Set beacon (beacon_set=0)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 01 09nl80211: 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 00nl80211: ifindex=9nl80211: beacon_int=100nl80211: dtim_period=2nl80211: ssid - hexdump_ascii(len=11): 54 6f 6e 79 4f 70 65 6e 57 52 54 TonyOpenWRT * beacon_int=100nl80211: hidden SSID not in usenl80211: privacy=1nl80211: auth_algs=0x1nl80211: wpa_version=0x3nl80211: key_mgmt_suites=0x2nl80211: pairwise_ciphers=0x18nl80211: group_cipher=0x8nl80211: SMPS mode - offnl80211: beacon_ies - hexdump(len=10): 7f 08 00 00 00 00 00 00 00 40nl80211: proberesp_ies - hexdump(len=10): 7f 08 00 00 00 00 00 00 00 40nl80211: assocresp_ies - hexdump(len=10): 7f 08 00 00 00 00 00 00 00 40WPA: Start group state machine to set initial keysWPA: group state machine entering state GTK_INIT (VLAN-ID 0)GTK - hexdump(len=32): [REMOVED]WPA: group state machine entering state SETKEYSDONE (VLAN-ID 0)wpa_driver_nl80211_set_key: ifindex=9 (wlan1) alg=2 addr=0x8dc74 key_idx=1 set_tx=1 seq_len=0 key_len=32nl80211: KEY_DATA - hexdump(len=32): [REMOVED] broadcast keynl80211: Set wlan1 operstate 0->1 (UP)netlink: Operstate: ifindex=9 linkmode=-1 (no change), operstate=6 (IF_OPER_UP)wlan1: interface state UNINITIALIZED->ENABLEDwlan1: AP-ENABLED wlan1: Setup of interface done.ctrl_iface not configured!
其中裡面用到了加密時候需要的隨機數,這個是使用核心中的random節點來產生的,所以我們在核心這兩個還需要添加random驅動。
驗證AP
在驗證之前,我們需要給我們的這個網卡自己一個IP地址, 因此直接:
root@(none):/# ifconfig wlan1 192.168.2.1 root@(none):/# ifconfig wlan1wlan1 Link encap:Ethernet HWaddr 94:0c:6d:7c:12:f6 inet addr:192.168.2.1 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::960c:6dff:fe7c:12f6/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:131 errors:0 dropped:0 overruns:0 frame:0 TX packets:106 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:20162 (19.6 KiB) TX bytes:20954 (20.4 KiB)
然後我們開啟手機查看是否可以看到我們的熱點:
然後我們使用手動設定IP方式串連熱點
串連完成後, 我們可以看到已經串連的狀態:
然後在手機上面可以Ping一把確定是否連通:
然後在iMX6UL上面也來ping一把這個手機:
root@(none):/# ping 192.168.2.111PING 192.168.2.111 (192.168.2.111): 56 data bytes64 bytes from 192.168.2.111: seq=0 ttl=64 time=90.952 ms64 bytes from 192.168.2.111: seq=1 ttl=64 time=143.093 ms64 bytes from 192.168.2.111: seq=2 ttl=64 time=4.508 ms64 bytes from 192.168.2.111: seq=3 ttl=64 time=46.859 ms64 bytes from 192.168.2.111: seq=4 ttl=64 time=2.539 ms64 bytes from 192.168.2.111: seq=5 ttl=64 time=2.697 ms^C--- 192.168.2.111 ping statistics ---6 packets transmitted, 6 packets received, 0% packet lossround-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,密碼等資訊:
root@(none):/# cat /etc/wpa.conf ctrl_interface=/var/run/wpa_supplicantnetwork={ ssid="XXX" proto=WPA key_mgmt=WPA-PSK pairwise=TKIP group=TKIP psk="PASSWORD"}
其中XXX是路由器的熱點名字, psk填寫Password, 加密方式TKIP需要根據實際情況填寫,不明白可以看我以前的文章,或者直接man。
運行
先建立一個runtime目錄:
root@(none):/# mkdir /var/run/wpa_supplicant
然後直接在後台運行:
root@(none):/# wpa_supplicant -B -iwlan0 -c /etc/wpa.conf Successfully initialized wpa_supplicantrfkill: Cannot open RFKILL contro[ 172.506937] ieee80211 phy0: rt2x00lib_request_firmware: Info - Loading firmware file 'rt2870.bin'l device[ 172.527112] ieee80211 phy0: rt2x00lib_request_firmware: Info - Firmware detected - version: 0.29[ 173.083412] IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not readyroot@(none):/# [ 174.937567] wlan0: authenticate with bc:d1:77:a0:4f:3c[ 175.073446] wlan0: send auth to bc:d1:77:a0:4f:3c (try 1/3)[ 175.082318] wlan0: authenticated[ 175.087051] rt2800usb 1-1.2.1:1.0 wlan0: disabling HT/VHT due to WEP/TKIP use[ 175.098735] wlan0: associate with bc:d1:77:a0:4f:3c (try 1/3)[ 175.108018] wlan0: RX AssocResp from bc:d1:77:a0:4f:3c (capab=0x411 status=0 aid=5)[ 175.129960] IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready[ 175.136678] wlan0: associated[ 176.069997] IPv6: wlan0: IPv6 duplicate address fe80::7edd:90ff:feb2:73b1 detected!
驗證
使用wpa_cli看查看串連的狀態
root@(none):/# wpa_cli statusSelected interface 'wlan0'bssid=bc:d1:77:a0:4f:3cfreq=2412ssid=XXXid=0mode=stationpairwise_cipher=TKIPgroup_cipher=TKIPkey_mgmt=WPA-PSKwpa_state=COMPLETEDaddress=7c:dd:90:b2:73:b1uuid=1e82e362-f927-544e-aa4d-1a229533944c
status為COMPLETED那麼表示串連上了, 然後我們擷取IP地址, 並嘗試ping國內的某個網站:
root@(none):/# udhcpc -i wlan0udhcpc (v1.24.1) startedSending discover...Sending select for 192.168.1.112...Lease of 192.168.1.112 obtained, lease time 86400/etc/udhcpc.d/50default: Adding DNS 192.168.1.1/etc/udhcpc.d/50default: Adding DNS 0.0.0.0root@(none):/# ping meitu.comPING meitu.com (42.62.69.156): 56 data bytes64 bytes from 42.62.69.156: seq=0 ttl=42 time=44.499 ms64 bytes from 42.62.69.156: seq=1 ttl=42 time=44.956 ms64 bytes from 42.62.69.156: seq=2 ttl=42 time=43.122 ms64 bytes from 42.62.69.156: seq=3 ttl=42 time=43.387 ms64 bytes from 42.62.69.156: seq=4 ttl=42 time=46.467 ms64 bytes from 42.62.69.156: seq=5 ttl=42 time=46.267 ms64 bytes from 42.62.69.156: seq=6 ttl=42 time=43.843 ms^C--- meitu.com ping statistics ---7 packets transmitted, 7 packets received, 0% packet lossround-trip min/avg/max = 43.122/44.648/46.467 ms
至此, 我們就完成了基本的配置工作。