2015年9月4日 星期五

透過RaspberryPi來分享IPhone的無線熱點

架構圖如下:
[nb1]---wlan---[RPi2]--usb--[IPhone]--3G/4G--[Internet]
[nb2]--wlan--/

自問自答:為什麼不直接都連IPhone的無線熱點上網就好了呢?

在RPi2 上的前置作業
1. 確認DWA-121 USB無線網卡已插入並且設定完成。
pi@raspberrypi:~$ lsusb
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 2001:3308 D-Link Corp. DWA-121 802.11n Wireless N 150 Pico Adap]
Bus 001 Device 006: ID 413c:1010 Dell Computer Corp.
Bus 001 Device 007: ID 413c:2110 Dell Computer Corp.

pi@raspberrypi:~$ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr 6c:19:8f:b7:72:80
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:107 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:161741 (157.9 KiB)  TX bytes:79141 (77.2 KiB)

2. 安裝ipeth及相關套件
sudo apt-get install gvfs ipheth-utils
sudo apt-get install libimobiledevice-utils gvfs-backends gvfs-bin gvfs-fuse
sudo apt-get install bridge-utils

3. 安裝客製作hostapd
sudo wget http://dl.dropbox.com/u/1663660/hostapd/hostapd -o /usr/sbin/hostpad

並生成相關設定檔,記得改一下ssid及wpa_passphrase設定值。
pi@raspberrypi:~$ cat /etc/hostapd/hostapd.conf
interface=wlan0
bridge=br0
driver=rtl871xdrv
country_code=NZ
ctrl_interface=wlan0
ctrl_interface_group=0
ssid=RPiAP
hw_mode=g
channel=1
wpa=3
wpa_passphrase=PASSWORD
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
beacon_int=100
auth_algs=3
macaddr_acl=0
wmm_enabled=1
eap_reauth_period=360000000

4. 設定網路及橋接。我用兩個script來啟動/停止hostapd及相關設定
pi@raspberrypi:~$ cat hotspot_start.sh
# Refer to http://blog.sip2serve.com/post/48420162196/howto-setup-rtl8188cus-on-rpi-ast
sudo brctl addbr br0
sudo brctl addif br0 eth1
sudo brctl addif br0 wlan0
sudo cp /etc/network/interfaces_hotspot /etc/network/interfaces
sudo service networking restart
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf &
#sudo service hostapd restart
pi@raspberrypi:~$ cat hotspot_stop.sh
# Refer to http://blog.sip2serve.com/post/48420162196/howto-setup-rtl8188cus-on-rpi-ast
sudo cp /etc/network/interfaces_normal /etc/network/interfaces
sudo service networking restart
sudo brctl delif br0 eth1
sudo brctl delif br0 wlan0
sudo ifconfig br0 down
sudo brctl delbr br0
sudo service networking restart
sudo killall hostapd
前置作業完成
5. 啟動iphone上的hotspot功能。必須在RPi2上可以看到eth1並且取得網路位址。
pi@raspberrypi:~$ ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 72:ec:e4:53:20:fe
          inet addr:172.20.10.3  Bcast:172.20.10.15  Mask:255.255.255.240
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10464 errors:0 dropped:6 overruns:0 frame:0
          TX packets:8282 errors:1 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:9967790 (9.5 MiB)  TX bytes:660954 (645.4 KiB)
6. 啟動hostapd後, 應該就可利用在步驟3設定的SSID及密碼連線了。
pi@raspberrypi:~$ ./hotspot_start.sh