Reverse tethering for android

Since I’m on a road-trip and don’t have a SIM-card with data for every country I’m visiting (I’m fine in Germany and Belgium, but did not get a SIM-card for the Czech republic where i’m just for 24 hours) and never connect with my phone to an untrusted wifi network, I thought i’d do the reverse of tethering: connect my phone to my laptop (which has a tunnel to my server-rack in the datacenter) and surf through that. So mostly as a note for myself, here’s what I did:

Connected the USB between phone and laptop, then enabled tethering (this is Cyanogen 2.2 on a G1, rooted). This brings up the usb0 nic on both ends with some default ip’s. Then went into the terminal on the phone (probably can do this with adb shell as well) and checked I could ping:

# ifconfig usb0
usb0: ip 192.168.42.129 mask 255.255.255.0 flags [up broadcast running multicast]
# ping 192.168.42.137
PING 192.168.42.137 (192.168.42.137) 56(84) bytes of data.
64 bytes from 192.168.42.137: icmp_seq=1 ttl=64 time=0.519ms

With that running, it is time to enable forwarding on my linux laptop:

sysctl -w net.ipv4.ip_forward=1

And enable some firewall rules to have traffic from the phone get NATTED on the outgoing tunnel endpoint on my laptop (tap0, remember usb0 is the usb nic that connects to my phone):

iptables -A FORWARD -i tap0 -o usb0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A POSTROUTING -t nat -o tap0 -j MASQUERADE
iptables -A INPUT -i usb0 -j ACCEPT
iptables -A FORWARD -i usb0 -o tap0 -j ACCEPT

Then I set the default route:

route add default gw 192.168.42.137

And finally, I need to tell the android that my nameservers are 194.109.6.66 and 194.109.9.99:

# setprop net.dns1 194.109.6.66
# setprop net.dns2 194.109.9.99

And we’re done!

Flattr this

Leave a Reply