1#!/vendor/bin/sh 2 3# Do all the setup required for WiFi. 4# The kernel driver mac80211_hwsim has already created two virtual wifi devices 5# us. These devices are connected so that everything that's sent on one device 6# is recieved on the other and vice versa. This allows us to create a fake 7# WiFi network with an access point running inside the guest. Here is the setup 8# for that and the basics of how it works. 9# 10# Create a namespace named router and move eth0 to it. Create a virtual ethernet 11# pair of devices and move both one virtual ethernet interface and one virtual 12# wifi interface into the router namespace. Then set up NAT networking for those 13# interfaces so that traffic flowing through them reach eth0 and eventually the 14# host and the internet. The main network namespace will now only see the other 15# ends of those pipes and send traffic on them depending on if WiFi or radio is 16# used. Finally run hostapd in the network namespace to create an access point 17# for the guest to connect to and dnsmasq to serve as a DHCP server for the WiFi 18# connection. 19# 20# main namespace router namespace 21# ------- ---------- | --------------- 22# | ril |<----->| radio0 |<--+--->| radio0-peer |<-------+ 23# ------- ---------- | --------------- | 24# | ^ | 25# | | | 26# | v v 27# | ************* -------- 28# | * ipv6proxy *<--->| eth0 |<--+ 29# | ************* -------- | 30# | ^ ^ | 31# | | | | 32# | v | | 33# ------------------ --------- | --------- | | 34# | wpa_supplicant |<->| wlan0 |<--+------->| wlan1 |<---------+ | 35# ------------------ --------- | --------- | 36# | ^ ^ | 37# | | | v 38# | v v -------- 39# | *********** *********** | host | 40# | * hostapd * * dnsmasq * -------- 41# | *********** *********** 42# 43 44wifi_mac_prefix=`getprop net.wifi_mac_prefix` 45if [ -n "$wifi_mac_prefix" ]; then 46 /vendor/bin/mac80211_create_radios 2 $wifi_mac_prefix || exit 1 47fi 48 49NAMESPACE="router" 50createns ${NAMESPACE} 51 52# createns will have created a file that contains the process id (pid) of a 53# process running in the network namespace. This pid is needed for some commands 54# to access the namespace. 55PID=$(</data/vendor/var/run/netns/${NAMESPACE}.pid) 56 57/vendor/bin/ip link set eth0 netns ${PID} 58 59/vendor/bin/ip link add radio0 type veth peer name radio0-peer netns ${PID} 60 61# Enable privacy addresses for radio0, this is done by the framework for wlan0 62sysctl -wq net.ipv6.conf.radio0.use_tempaddr=2 63 64execns ${NAMESPACE} /vendor/bin/ip link set radio0-peer up 65 66execns ${NAMESPACE} /vendor/bin/ip link set eth0 up 67 68/vendor/bin/ip link set radio0 up 69 70execns ${NAMESPACE} /vendor/bin/ip link set wlan1 up 71 72/vendor/bin/iw phy phy1 set netns $PID 73 74setprop ctl.start netmgr 75 76setprop ctl.start wifi_forwarder 77 78# If this is a clean boot we need to copy the hostapd configuration file to the 79# data partition where netmgr can change it if needed. If it already exists we 80# need to preserve the existing settings. 81if [ ! -f /data/vendor/wifi/hostapd/hostapd.conf ]; then 82 cp /vendor/etc/simulated_hostapd.conf /data/vendor/wifi/hostapd/hostapd.conf 83 chown wifi:wifi /data/vendor/wifi/hostapd/hostapd.conf 84 chmod 660 /data/vendor/wifi/hostapd/hostapd.conf 85fi 86 87# Start hostapd, the access point software 88setprop ctl.start emu_hostapd 89