[Networking] class101:
Script update

[05/09/2015 : script update] : if looking for a cleaner script it is the one I'm using today, with dhcp options in the header to disable the non required dhcp server of dnsmasq, this the script posted in this ticket: https://code.google.com/p/android/issues/detail?id=99034 or this mirror http://pastebin.com/snS9erAP also included in this post now

The method to reverse tether shown by the original poster only works for Android 4.4.4, if you need one working method for Android 4.4.4, 5.0.1 and higher you need to run you own dnsmasq instance, to do so follow the instructions below

I have explained the method in different previous posts in this thread but in this post I recapitulates all you have to do (close to nothing to do once all is set)

Since my latest posts in this thread, I have updated the script to export the variables into environment variable in case you need to change dns, ip or anything, or download it if you are not familiar in doing UNIX line ending scripts (attached a zip with all the scripts I'm using to this post)

IP=192.168.137.101 # ip of the rndis interface (if using Windows Internet Connection Sharing usually set to an ip in the 192.168.137.x range, or your home network range if using a Network Bridge like 192.168.1.x)
NETMASK=24 # netmask of the rndis interface (if you don't know this setting set it to 24, 255.255.255.255 = 32 | 255.255.255.0 = 24 | 255.255.0.0 = 16 | 255.0.0.0 = 8)
GATEWAY=192.168.137.1 # gateway of the rndis interface (main route, if using Windows Internet Connection Sharing usually set to 192.168.137.1, or your home internet box if using a Network Bridge like 192.168.1.1)
DNS1=8.8.8.8 # domain name resolution (google public dns1 = 8.8.8.8, but should be faster to your home internet box like 192.168.1.1)
DNS2=8.8.4.4 # domain name resolution (google public dns2 = 8.8.4.4)

USE_DHCP=0 # loads the DHCP server option of dnsmasq (not required, defaults to 0)
DHCP_FROM=192.168.137.10 # ignored if USE_DHCP=0
DHCP_TO=192.168.137.90 # ignored if USE_DHCP=0

# ! NO NEED TO EDIT AFTER THIS LINE ! Tested working on Android 4.4.4 and 5.x
echo -- rndis0: setting usb mode to rndis --
setprop sys.usb.config 'rndis'
echo -- rndis0: adding ip rule --
ip rule add from all lookup main
echo -- rndis0: flushing interface --
ip addr flush dev rndis0
echo -- rndis0: setting ip --
ip addr add $IP/$NETMASK dev rndis0
echo -- rndis0: starting the interface --
ip link set rndis0 up
echo -- rndis0: setting route --
#ip route add default via ${GATEWAY} dev rndis0
busybox route add -net 0.0.0.0 netmask 0.0.0.0 gw $GATEWAY dev rndis0
#echo -- rndis0: (optional) setting iptables --
#iptables -t nat -I POSTROUTING 1 -o rndis0 -j MASQUERADE
#echo -- rndis0: (optional) setting ip_forward --
#echo 1 > /proc/sys/net/ipv4/ip_forward
echo -- rndis0: setting properties --
setprop net.dns1 $DNS1
setprop net.dns2 $DNS2
setprop net.rndis0.dns1 $DNS1
setprop net.rndis0.dns2 $DNS2
setprop net.rndis0.gw $GATEWAY
setprop net.rndis0.gateway $GATEWAY
killall dnsmasq &> /dev/null
if [ $USE_DHCP = 1 ]; then
echo -- rndis0: starting dnsmasq with dhcp --
dnsmasq --no-poll --pid-file --interface=rndis0 --interface=wlan0 --interface=rmnet0 --bogus-priv --filterwin2k --no-resolv --server=${DNS1} --server=${DNS2} --cache-size=1000 --dhcp-range=${DHCP_FROM},${DHCP_TO} --dhcp-lease-max=253 --dhcp-authoritative --dhcp-leasefile=/cache/usb_tether_dnsmasq.leases < /dev/null
else
echo -- rndis0: starting dnsmasq without dhcp --
dnsmasq --no-poll --pid-file --interface=rndis0 --interface=wlan0 --interface=rmnet0 --bogus-priv --filterwin2k --no-resolv --server=${DNS1} --server=${DNS2} < /dev/null
fi

How to use these scripts

1) Unpack and copy all the .sh script to the Smartphone shared folder in Windows, for example with a Nexus 5, place files in This PC\Nexus 5 base folder where others folders are showing like DCIM etc.., they will point to /sdcard/ in your phone

2) In a Terminal of your phone, type: su (to get root privilege) and make sure your phone's internet access are disabled, Wifi off, Phone internet off. (the Terminal I'm using in screenshot is Terminal Emulator by Jack Paleviche, very good one)

3) In the Terminal type: su -c 'sh /sdcard/rn.sh' (In the background this will enable the rndis interface in Windows, set the ip of rndis0 in the phone to 192.168.137.101, gateway to 192.168.137.1, set google's public dns to the properties and to dnsmasq, NO ERRORS SHOULD BE SHOWN, ONLY ECHO COMMANDS AS SHOWN IN SCREENSHOT, if you see errors you need to reset the changes with su -c 'sh /sdcard/rnoff.sh' and restarts from step 3 until you fix, or ask here if you are lost how to fix)

4) In Windows uncheck/recheck Internet Connection Sharing on the Internet card (this reenable Internet Connection Sharing sometimes buggy after unpluggin phone and restarting the computer, this should also automatically assign the ip 192.168.137.1 and netmask 255.255.255.0 to the RNDIS interface in Windows, if in your case it is not showing as 137.1 but another IP you will need to update the gateway of the script to point to this IP, or you can also set this ip manually to a new one you will remember 192.168.137.2 and update the gateway in the scripts to point to this new ip 137.2, but the ultimate best approach is to fix why Windows does not automatically attrib 137.1 to it because you will need to disable/enable again the ICS frequently so it is better to have it pointing to its default address by default in case one day it does no more assign your custom IP.

From here you should get Internet Access in Google Chrome and Speedtest.net App

But to get access in 100% of the App you need to run the Wifi interface set to the IP of the RNDIS interface

5) In the Phone enable manually Wifi, connect your home wifi access point in STATIC IP mode, for example here I do IP: 192.168.137.102, GATEWAY: 192.168.137.1 DNS1:8.8.8.8 DNS2:8.8.4.4

6) On Android 4.4.4 you should be all set, on Android 5.0 sometimes the route is not set properly, in this case you can use my wifi scripts

7) su -c 'sh /sdcard/wloff.sh' once to reset the wlan0 interface and shortly after su -c 'sh /sdcard/wl.sh' to set again wifi on again with the same settings (if you get an error after setting the route, just execute wloff.sh/wl.sh again this will pass)

Some useful commands

netcfg (see network cards running state, the ip and netmask of each one)

busybox route -n (see network card routes/gateways)

vi (file edition)

su (alone in the terminal editor to get root privilege directly in the terminal)

su -c 'sh /path/toscript.sh' (run a script as root)

mount -o remount, RW /system (allows writing in system files for example for adding your custom aliases to the /etc/mkshrc file, it reverts to read only after a reboot)

Some custom aliases example (to add in /etc/mkshrc to type things fastly once you are all set, "rn" will start reverse tether, "rnoff" will stop, etc...)

alias rn='sh /sdcard/rn.sh'

alias rnoff='sh /sdcard/rnoff.sh'

alias wl='sh /sdcard/wl.sh'

alias wloff='sh /sdcard/wloff.sh'

alias rm='sh /sdcard/rm.sh'

alias rmoff='sh /sdcard/rmoff.sh'

alias ro='busybox route -n'

alias pi='ping google.com'

alias piip='ping 8.8.8.8'

alias pirn='ping -I rndis0 google.com'

alias wr='mount -o remount, RW /system'

alias n='netcfg'

Screenshot of all the procedure under Android 5.0, RNDIS + WIFI