Sunday, June 10, 2012

Android Reverse USB Tethering


Android usb tethering is a great feature provided in my android but sometime I feel the need to have reverse tethering. Using reverse tethering I can use my system internet on android phone via usb while my phone getting charged. It certainly helps in situations where there is a restricted access available on wifi and you don't have enough data usage or mobile network coverage on your phone like in my office. I did it on my Centos Linux box with Galaxy S but this should work with any other linux os and android phone having firmware with usb tether option.

Note: This can only be done if your phone is rooted, which may also cause to void your manufacturer warranty. Please don't follow if you don't know what you are doing.

Procedure:
 1. First of all enable the tethering on your phone. Settings > Wireless and network > more >Tethering & Portable > Check USB Tethering option. There should be USB0 interface appear on your system as soon as do this. 

root  ~ : ifconfig -a usb0
usb0      Link encap:Ethernet  HWaddr 02:07:30:34:35:3D 
          inet addr:192.168.42.118  Bcast:192.168.42.255  Mask:255.255.255.0
          inet6 addr: fe80::7:30ff:fe34:353d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13 errors:0 dropped:0 overruns:0 frame:0
          TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1200 (1.1 KiB)  TX bytes:7095 (6.9 KiB)

2. Note the IP address (192.168.42.118) of the device interface usb0 from the system using above command.

3. Now get the IP address from the device. Login to device using android adb tool(provided with android sdk).

Note: You may have to enable the root access to adb from the developer option settings. 

root  ~ : adb shell
shell@android:/ $ su
shell@android:/ # netcfg
lo       UP                                   127.0.0.1/8   0x00000049 00:00:00:00:00:00
svnet0   UP                                     0.0.0.0/0   0x000000d1 a0:00:00:00:00:00
ifb0     DOWN                                   0.0.0.0/0   0x00000082 c6:cd:01:c2:8f:40
ifb1     DOWN                                   0.0.0.0/0   0x00000082 aa:37:52:c1:b9:ad
sit0     DOWN                                   0.0.0.0/0   0x00000080 00:00:00:00:00:00
ip6tnl0  DOWN                                   0.0.0.0/0   0x00000080 00:00:00:00:00:00
wlan0    DOWN                                   0.0.0.0/0   0x00001002 b4:07:f9:73:46:d1
rndis0   UP                              192.168.42.129/24  0x00001043 4a:35:0f:bc:01:19

Note down the IP address of the device (192.168.42.129).

4. Check the connectivity of both the interfaces. Ping from device to system and system to device. 

shell@android:/ # ping 192.168.42.118
PING 192.168.42.118 (192.168.42.118) 56(84) bytes of data.
64 bytes from 192.168.42.118: icmp_seq=1 ttl=64 time=1.36 ms
64 bytes from 192.168.42.118: icmp_seq=2 ttl=64 time=0.779 ms
64 bytes from 192.168.42.118: icmp_seq=3 ttl=64 time=0.705 ms

root  ~ : ping 192.168.42.129
PING 192.168.42.129 (192.168.42.129) 56(84) bytes of data.
64 bytes from 192.168.42.129: icmp_seq=1 ttl=64 time=1.16 ms
64 bytes from 192.168.42.129: icmp_seq=2 ttl=64 time=0.812 ms
64 bytes from 192.168.42.129: icmp_seq=3 ttl=64 time=0.791 ms

5. Add system as the default gateway for the android.
shell@android:/ # route add default gw 192.168.42.118 dev rndis0

6. Add the dns server setting to the device.
shell@android:/data # setprop net.dns1 8.8.8.8
shell@android:/data # setprop net.dns1 4.2.2.2

7. Enable the port forwarding on the system. 
root  ~ : echo 1 > /proc/sys/net/ipv4/ip_forward

8. Add the masquerade setting in the system firewall and map this with system's lan interface eth0 or em1( what ever the name that are using in your system. )

root  ~ : iptables --flush -t nat
root  ~ : iptables --table nat --append POSTROUTING --out-interface em1 -j MASQUERADE
root  ~ : iptables --append FORWARD --in-interface usb0 -j ACCEPT 
9. Check the net connectivity from the device using ping and other applications. 
shell@android:/data # ping google.com                                         
PING google.com (74.125.228.37) 56(84) bytes of data.
64 bytes from iad23s06-in-f5.1e100.net (74.125.228.37): icmp_seq=1 ttl=42 time=321 ms
64 bytes from iad23s06-in-f5.1e100.net (74.125.228.37): icmp_seq=2 ttl=42 time=333 ms
64 bytes from iad23s06-in-f5.1e100.net (74.125.228.37): icmp_seq=3 ttl=42 time=315 ms

Note: 
1. It will not show any tethering connection icon in notification bar like 3G or wifi.

2. The routing done on the device is not permanently saved on the device. Once disconnected from the system, you'll have to repeat the steps on the device to enable it again.


5 comments:

kcyhoms said...

thanks,

worked like a charm with my Samsung S 3 and linux Gentoo box.

daniel said...

I have followed all the steps with my redhat 6 and samsung galaxy fit (android 2.3.6). It is not working for me.

I have tried pinging google.com in mobile terminal. It thrown error as unknown host google.com.

Can you help me on this?

Anonymous said...

Every company offering Google seo service in Delhi.
Give them what they want. Even though I didn't work on the farm, I did think it was worth the extra fuel to haul the piles of carpet there for recycling.

my weblog seo company rankings

SUDARSHAN (INDIA) said...

Nothing happens everything works fine but while pining it dosent do anything.

Anonymous said...

You have high ping :) to google 300+ you won't be able to play any online games