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 vendor.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" 50 51# createns will have created a file that contains the process id (pid) of a 52# process running in the network namespace. This pid is needed for some commands 53# to access the namespace. 54PID=$(</data/vendor/var/run/netns/${NAMESPACE}.pid) 55 56/vendor/bin/ip link set eth0 netns ${PID} 57 58/vendor/bin/ip link add radio0 type veth peer name radio0-peer netns ${PID} 59 60# Enable privacy addresses for radio0, this is done by the framework for wlan0 61sysctl -wq net.ipv6.conf.radio0.use_tempaddr=2 62 63execns ${NAMESPACE} /vendor/bin/ip link set radio0-peer up 64 65execns ${NAMESPACE} /vendor/bin/ip link set eth0 up 66 67/vendor/bin/ip link set radio0 up 68 69execns ${NAMESPACE} /vendor/bin/ip link set wlan1 up 70 71/vendor/bin/iw phy phy1 set netns $PID 72 73setprop ctl.start netmgr 74 75setprop ctl.start wifi_forwarder 76 77# If this is a clean boot we need to copy the hostapd configuration file to the 78# data partition where netmgr can change it if needed. If it already exists we 79# need to preserve the existing settings. 80if [ ! -f /data/vendor/wifi/hostapd/hostapd.conf ]; then 81 cp /vendor/etc/simulated_hostapd.conf /data/vendor/wifi/hostapd/hostapd.conf 82 chown wifi:wifi /data/vendor/wifi/hostapd/hostapd.conf 83 chmod 660 /data/vendor/wifi/hostapd/hostapd.conf 84fi 85 86# Start hostapd, the access point software 87setprop ctl.start emu_hostapd 88 89ifconfig radio0 -multicast 90