Topic: tech myref prev next
tech myref > Raspberry Pi iPhone USB Tethering
It is possible, and sometimes desirable, to build a wireless network with a Raspberry Pi 3 as the WiFi access point and a USB-connected iPhone as the backhaul. This approach has a few benefits over simply using the ‘Personal Hotspot’ feature on the iPhone. With USB tethering, the Raspberry Pi will always stay connected to the phone and keep the session active; it won’t disable the access point due to inactivity. The Raspberry Pi, acting as a hotspot, will have device isolation disabled, so wireless devices on the local subnet will be able to talk to each other as well as the Internet. Finally, the Raspberry Pi hotspot will allow more devices to be connected simultaneously (the iPhone appears to limit connected devices to five, though the /28 DHCP subnet it uses would limit the number of devices to fourteen).
There are better ways to build a WiFi network with cellular backhaul, such as by using a dedicated WiFi to cellular router, however my requirements were to use a particular mobile phone and to use the equipment already available to me. Note that the speed of the wireless network is limited to around 20Mbit/s by the WiFi adapter on the Raspberry Pi, which will typically be slower than the Internet backhaul. Also, transfer speeds between hosts on the WiFi network will be limited to half of this (10Mbit/s), as data is received and retransmitted by the Raspberry Pi.
The steps to configure a WiFi network are as follows:
Additionally, the Ethernet port on the Raspberry Pi can be configured to route packets between a wired subnet, the WiFi and the Internet.
usbmuxd will allow the iPhone to be tethered by USB.
sudo apt install usbmuxd
sudo systemctl enable usbmuxd
sudo systemctl start usbmuxd
The iPhone will now appear as network interface eth1 and will assign the Raspberry Pi an IPv4 address by DHCP.
hostapd is a simple way to start a wireless access point.
sudo apt install hostapd
Enter the following into /etc/hostapd/hostapd.conf
interface=wlan0
ssid=YourChosenSSID
wpa_passphrase=YourChosenPassphrase
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
hw_mode=g
channel=11
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
rsn_pairwise=CCMP
dnsmasq will be used as the DHCP and DNS server.
sudo apt install dnsmasq
Edit /etc/dnsmasq.conf and add the following lines:
interface=wlan0
dhcp-range=192.168.8.11,192.168.8.100,12h
Enable the wireless interface and dnsmasq:
sudo ip link set wlan0 down
sudo ip addr flush dev wlan0
sudo ip link set wlan0 up
sudo ip addr add 192.168.8.1/24 dev wlan0
sudo systemctl restart dnsmasq
sudo /usr/sbin/hostapd -P /run/hostapd.pid /etc/hostapd/hostapd.conf -B
Note the -B flag on hostapd, which tells it run in the background.
Enter the following iptables commands. These commands assume that the interface with Internet access is eth1 and the local network interface is wlan0 (check these with ‘ip addr’).
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth1 -j ACCEPT
sudo sysctl -w net.ipv4.ip_forward=1
The steps to add a wired subnet are similar to those for adding the wireless subnet above. Here, eth0 is the wired Ethernet interface, and the subnet is 192.168.9.0/24.
sudo ip link set eth0 down
sudo ip addr flush dev eth0
sudo ip link set eth0 up
sudo ip addr add 192.168.9.1/24 dev eth0
sudo iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
The dnsmasq configuration hasn’t been changed to run DHCP or a DNS forwarder on eth0, so hosts will have to be configured with a static IP address, or another machine will have to act as the DHCP and DNS server.
The following script can be saved in a sensible location (such as /root/start_wifi.sh) and executed on startup from /etc/rc.local.
#!/bin/sh
ip link set wlan0 down
ip addr flush dev wlan0
ip link set wlan0 up
ip addr add 192.168.8.1/24 dev wlan0
ip link set eth0 down
ip addr flush dev eth0
ip link set eth0 up
ip addr add 192.168.9.1/24 dev eth0
hostapd -P /run/hostapd.pid /etc/hostapd/hostapd.conf -B
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wlan0 -o eth1 -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
sysctl -w net.ipv4.ip_forward=1
dnsmasq -a 192.168.8.1 -i wlan0 -I lo -z -F 192.168.8.11,192.168.8.100,12h
exit 0