FORUMS
Remove All Ads from XDA

[HOWTO] USB/RNDIS Tethering (root required)

196 posts
Thanks Meter: 17
 
By whitslack, Senior Member on 1st February 2013, 03:09 AM
Post Reply Email Thread
First, let me apologize if solutions to this problem have already been posted on this forum and elsewhere. I developed this solution completely independently, and I am sharing it here in hopes that it may prove useful to someone else.

This pair of scripts will allow you to share your phone's Internet connection with a computer via a USB cable. It definitely works when the computer is running Linux (assuming you have RNDIS support in your kernel), and it should work as well when the computer is running Windows. However, it will not work with Mac, as OS X does not have an RNDIS driver.

First, upload these two scripts to your /sdcard:

/sdcard/usb_tether_start.sh:
Code:
#!/system/bin/sh

prevconfig=$(getprop sys.usb.config)
if [ "${prevconfig}" != "${prevconfig#rndis}" ] ; then
	echo 'Is tethering already active?' >&2
	exit 1
fi

echo "${prevconfig}" > /cache/usb_tether_prevconfig
setprop sys.usb.config 'rndis,adb'
until [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done

ip rule add from all lookup main
ip addr flush dev rndis0
ip addr add 192.168.2.1/24 dev rndis0
ip link set rndis0 up
iptables -t nat -I POSTROUTING 1 -o rmnet0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
dnsmasq --pid-file=/cache/usb_tether_dnsmasq.pid --interface=rndis0 --bind-interfaces --bogus-priv --filterwin2k --no-resolv --domain-needed --server=8.8.8.8 --server=8.8.4.4 --cache-size=1000 --dhcp-range=192.168.2.2,192.168.2.254,255.255.255.0,192.168.2.255 --dhcp-lease-max=253 --dhcp-authoritative --dhcp-leasefile=/cache/usb_tether_dnsmasq.leases < /dev/null
/sdcard/usb_tether_stop.sh:
Code:
#!/system/bin/sh

if [ ! -f /cache/usb_tether_prevconfig ] ; then
	echo '/cache/usb_tether_prevconfig not found. Is tethering really active?' >&2
	exit 1
fi

if [ -f /cache/usb_tether_dnsmasq.pid ] ; then
	kill "$(cat /cache/usb_tether_dnsmasq.pid)"
	rm /cache/usb_tether_dnsmasq.pid
fi

echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -D POSTROUTING 1
ip link set rndis0 down
ip addr flush dev rndis0
ip rule del from all lookup main

setprop sys.usb.config "$(cat /cache/usb_tether_prevconfig)"
rm /cache/usb_tether_prevconfig
while [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done

To start USB tethering:
Code:
adb shell "su -c 'sh /sdcard/usb_tether_start.sh'"
Grant the superuser request on the phone if one appears.

If you're on Linux, you'll see a new network interface appear (probably called "usb0"). Bring the link up on that interface (ip link set usb0 up), run a DHCP client, and you're all set!

If you're on Windows, it will probably Just Work™.


To stop USB tethering:
Code:
adb shell "su -c 'sh /sdcard/usb_tether_stop.sh'"
Again, grant the superuser request on the phone if one appears.


That's it!
The Following 5 Users Say Thank You to whitslack For This Useful Post: [ View ] Gift whitslack Ad-Free
 
 
9th April 2013, 01:07 PM |#2  
Junior Member
Thanks Meter: 1
 
More
OMG !!!!

You rock ! This solution is the FIRST one which works on my international i9300 device (not a 'Sprint' version) with cm10.1.

I can't understand why ... but i'll keep your script on my ext SDCard.

I'll prupose this script to french cm10.1 users.

Thank you!
8th May 2013, 07:22 AM |#3  
Junior Member
Thanks Meter: 0
 
More
OS X does not have an RNDIS driver? really?
Quote:

...OS X does not have an RNDIS driver...

Please have a look at HoRNDIS: USB tethering driver for Mac OSX (it even supports modern/recent OSX versions!) @ hxxp://joshuawise.com/horndis (unable to embed inline URL link due to new user restriction)

Quote:
Originally Posted by whitslack

First, let me apologize if solutions to this problem have already been posted on this forum and elsewhere. I developed this solution completely independently, and I am sharing it here in hopes that it may prove useful to someone else.

This pair of scripts will allow you to share your phone's Internet connection with a computer via a USB cable. It definitely works when the computer is running Linux (assuming you have RNDIS support in your kernel), and it should work as well when the computer is running Windows. However, it will not work with Mac, as OS X does not have an RNDIS driver.

First, upload these two scripts to your /sdcard:

/sdcard/usb_tether_start.sh:

Code:
#!/system/bin/sh

prevconfig=$(getprop sys.usb.config)
if [ "${prevconfig}" != "${prevconfig#rndis}" ] ; then
	echo 'Is tethering already active?' >&2
	exit 1
fi

echo "${prevconfig}" > /cache/usb_tether_prevconfig
setprop sys.usb.config 'rndis,adb'
until [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done

ip rule add from all lookup main
ip addr flush dev rndis0
ip addr add 192.168.2.1/24 dev rndis0
ip link set rndis0 up
iptables -t nat -I POSTROUTING 1 -o rmnet0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
dnsmasq --pid-file=/cache/usb_tether_dnsmasq.pid --interface=rndis0 --bind-interfaces --bogus-priv --filterwin2k --no-resolv --domain-needed --server=8.8.8.8 --server=8.8.4.4 --cache-size=1000 --dhcp-range=192.168.2.2,192.168.2.254,255.255.255.0,192.168.2.255 --dhcp-lease-max=253 --dhcp-authoritative --dhcp-leasefile=/cache/usb_tether_dnsmasq.leases < /dev/null
/sdcard/usb_tether_stop.sh:
Code:
#!/system/bin/sh

if [ ! -f /cache/usb_tether_prevconfig ] ; then
	echo '/cache/usb_tether_prevconfig not found. Is tethering really active?' >&2
	exit 1
fi

if [ -f /cache/usb_tether_dnsmasq.pid ] ; then
	kill "$(cat /cache/usb_tether_dnsmasq.pid)"
	rm /cache/usb_tether_dnsmasq.pid
fi

echo 0 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -D POSTROUTING 1
ip link set rndis0 down
ip addr flush dev rndis0
ip rule del from all lookup main

setprop sys.usb.config "$(cat /cache/usb_tether_prevconfig)"
rm /cache/usb_tether_prevconfig
while [ "$(getprop sys.usb.state)" = 'rndis,adb' ] ; do sleep 1 ; done

To start USB tethering:
Code:
adb shell "su -c 'sh /sdcard/usb_tether_start.sh'"
Grant the superuser request on the phone if one appears.

If you're on Linux, you'll see a new network interface appear (probably called "usb0"). Bring the link up on that interface (ip link set usb0 up), run a DHCP client, and you're all set!

If you're on Windows, it will probably Just Work™.


To stop USB tethering:
Code:
adb shell "su -c 'sh /sdcard/usb_tether_stop.sh'"
Again, grant the superuser request on the phone if one appears.


That's it!

8th May 2013, 03:27 PM |#4  
Logix's Avatar
Forum Moderator
Flag St.Petersburg
Thanks Meter: 580
 
More
what is the difference between this and native tethering?
8th May 2013, 06:03 PM |#5  
Member
Flag Seattle
Thanks Meter: 1
 
More
for rooted stock roms

http://forum.xda-developers.com/show....php?t=2224083

this is the by far the easiest..
Post Reply Subscribe to Thread

Tags
rndis, root, tether, tethering, usb

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes