Project

General

Profile

USBNetworking » History » Version 14

Paul Kocialkowski, 02/28/2014 02:28 PM

1 9 Paul Kocialkowski
h1. Replicant USB Networking
2
3 14 Paul Kocialkowski
This page explains how to connect your Replicant phone to the Internet via an USB connection to a computer connected to the Internet. 
4
5
h2. Using reverse_tether.sh
6
7
h2. Using the Replicant USB Networking scripts
8
9
10
11 1 Paul Kocialkowski
12 9 Paul Kocialkowski
h2. What you need
13 1 Paul Kocialkowski
14 9 Paul Kocialkowski
* A phone running Replicant (but it should also work on [[CyanogenMod]] or Android)
15
* A computer with USB connectivity and network access running GNU/Linux
16
17
h2. Automation scripts
18
19 1 Paul Kocialkowski
Two scripts are necessary: one to run on the host computer and one to run on the device.
20
21 9 Paul Kocialkowski
h3. Replicant USB Networking - PC
22
23 1 Paul Kocialkowski
Here's the script to run on the computer. Copy the following text to a file named "run_pc.sh" (or any other name, you just need to keep the same name along the process):
24
25 9 Paul Kocialkowski
<pre>
26 1 Paul Kocialkowski
#!/bin/sh
27
28
# Replicant USB Networking
29
# ========================                             
30
# 
31 5 Paul Kocialkowski
# Copyright (C) 2011 Paul Kocialkowski, GPLv3+
32 1 Paul Kocialkowski
# 
33
# This program is free software: you can redistribute it and/or modify
34
# it under the terms of the GNU General Public License as published by
35
# the Free Software Foundation, either version 3 of the License, or
36
# (at your option) any later version.
37
# 
38
# You should have received a copy of the GNU General Public License
39
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
40
41
IPTABLES_CLEAN_RULES=true
42
43
USB_IFACE="usb0"
44
INTERNET_IFACE="eth0"
45
46
USB_IFACE_IP="192.168.4.200"
47
48
# Clean iptables rules
49
50
iptables_rules_clean () {
51
	if [ $IPTABLES_CLEAN_RULES = false ]
52
	then
53
		return
54
	fi
55
56
	iptables --flush
57
	iptables --table nat --flush
58
	iptables --delete-chain
59
	iptables --table nat --delete-chain
60
}
61
62
# Inject iptables forwarding rules
63
64
iptables_forward_rules_apply () {
65
	iptables --table nat --append POSTROUTING --out-interface $INTERNET_IFACE -j MASQUERADE
66
	iptables --append FORWARD --in-interface $USB_IFACE -j ACCEPT
67
	echo 1 > /proc/sys/net/ipv4/ip_forward 
68
}
69
70
# Configure network link
71
72
usb_networking_configure () {
73
	ifconfig $USB_IFACE up
74
	ifconfig $USB_IFACE $USB_IFACE_IP
75
}
76
77
usb_networking_disable () {
78
	ifconfig $USB_IFACE down
79
	echo 0 > /proc/sys/net/ipv4/ip_forward 
80
}
81
82
case $1 in
83
	"start")
84
		echo "Starting Replicant USB Networking"
85
		iptables_rules_clean
86
		usb_networking_configure
87
		iptables_forward_rules_apply
88
	;;
89
	"stop")
90
		echo "Stopping Replicant USB Networking"
91
		usb_networking_disable
92
		iptables_rules_clean
93 3 Paul Kocialkowski
	;;
94 1 Paul Kocialkowski
	*)
95
		echo "Usage: sh $0 {start|stop}"
96
	;;
97 3 Paul Kocialkowski
esac
98 9 Paul Kocialkowski
</pre>
99 1 Paul Kocialkowski
100 11 Paul Kocialkowski
Then, set this file executable: @chmod a+x run_pc.sh@ 
101 1 Paul Kocialkowski
102 9 Paul Kocialkowski
h3. Replicant USB Networking - Device
103
104 1 Paul Kocialkowski
Here's the script to run on the device. Copy the following text to a file named "run_dev.sh" (or any other name, you just need to keep the same name along the process):
105
106 13 Denis 'GNUtoo' Carikli
h4. For Replicant 4.0 (tested on the Nexus S).
107
108
<pre>
109
#!/system/bin/sh
110
111
# Replicant USB Networking
112
# ========================                             
113
# 
114
# Copyright (C) 2011 Paul Kocialkowski, GPLv3+
115
# 
116
# This program is free software: you can redistribute it and/or modify
117
# it under the terms of the GNU General Public License as published by
118
# the Free Software Foundation, either version 3 of the License, or
119
# (at your option) any later version.
120
# 
121
# You should have received a copy of the GNU General Public License
122
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
123
124
# Enable USB Networking
125
126
USB_IFACE="rndis0" 
127
128
USB_IFACE_IP="192.168.4.202" 
129
GATEWAY_IP="192.168.4.200" 
130
DNS1_IP="8.8.8.8" 
131
132
usb_networking_enable () {
133
    echo 1 > /sys/class/android_usb/android0/enable
134
    ifconfig rndis0 up
135
}
136
137
usb_networking_configure () {
138
    ifconfig $USB_IFACE $USB_IFACE_IP
139
    route add default gw $GATEWAY_IP dev $USB_IFACE
140
    setprop net.dns1 $DNS1_IP
141
142
    # setprop net.dns1 $( cat /system/etc/resolv.conf | sed -e "s|.*#.*||" -e "s|^.*nameserver \(.*\)$|\1|g" | grep -v "^$" | head -n 1 )
143
}
144
145
usb_networking_disable () {
146
    echo 0 > /sys/class/android_usb/android0/enable
147
    ifconfig rndis0 down
148
}
149
150
case $1 in
151
    "start")
152
        echo "Starting Replicant USB Networking" 
153
        usb_networking_enable
154
        usb_networking_configure
155
    ;;
156
    "stop")
157
        echo "Stopping Replicant USB Networking" 
158
        usb_networking_disable
159
    ;;
160
    *)
161
        echo "Usage: sh $0 {start|stop}" 
162
    ;;
163
esac
164
</pre>
165
166
h4. For Replicant 2.3 
167
168 9 Paul Kocialkowski
<pre>
169 1 Paul Kocialkowski
#!/system/bin/sh
170
171
# Replicant USB Networking
172
# ========================                             
173
# 
174 5 Paul Kocialkowski
# Copyright (C) 2011 Paul Kocialkowski, GPLv3+
175 1 Paul Kocialkowski
# 
176
# This program is free software: you can redistribute it and/or modify
177
# it under the terms of the GNU General Public License as published by
178
# the Free Software Foundation, either version 3 of the License, or
179
# (at your option) any later version.
180
# 
181
# You should have received a copy of the GNU General Public License
182
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
183
184
# Enable USB Networking
185
186
USB_IFACE="usb0"
187
188
USB_IFACE_IP="192.168.4.202"
189
GATEWAY_IP="192.168.4.200"
190
DNS1_IP="8.8.8.8"
191
192
usb_networking_enable () {
193
	echo 1 > /sys/class/usb_composite/rndis/enable
194
	ifconfig usb0 up
195
}
196
197
usb_networking_configure () {
198
	ifconfig $USB_IFACE $USB_IFACE_IP
199
	route add default gw $GATEWAY_IP dev $USB_IFACE
200
	setprop net.dns1 $DNS1_IP
201
202
	# setprop net.dns1 $( cat /system/etc/resolv.conf | sed -e "s|.*#.*||" -e "s|^.*nameserver \(.*\)$|\1|g" | grep -v "^$" | head -n 1 )
203
}
204
205
usb_networking_disable () {
206
	echo 0 > /sys/class/usb_composite/rndis/enable
207
	ifconfig usb0 down
208
}
209
210
case $1 in
211
	"start")
212
		echo "Starting Replicant USB Networking"
213
		usb_networking_enable
214
		usb_networking_configure
215
	;;
216
	"stop")
217
		echo "Stopping Replicant USB Networking"
218
		usb_networking_disable
219
	;;
220 2 Paul Kocialkowski
	*)
221
		echo "Usage: sh $0 {start|stop}"
222 1 Paul Kocialkowski
	;;
223
esac
224 9 Paul Kocialkowski
</pre>
225 1 Paul Kocialkowski
226 11 Paul Kocialkowski
Now you need to copy it to your phone. Start adb server as root: @# adb start-server@ and copy the file (as regular user): @adb push run_dev.sh /data/@ Get a shell on your device (@adb shell@) and set this file executable: @# chmod a+x /data/run_dev.sh@ 
227 1 Paul Kocialkowski
228 9 Paul Kocialkowski
h3. Using the scripts
229 1 Paul Kocialkowski
230
Now both scripts are in place. You should now: 
231 12 Paul Kocialkowski
* Start "run_dev.sh" on the device: @adb shell "sh /data/run_dev.sh start"@
232
* Start "run_pc.sh" as root on the host pc (you must be in the directory where you created the script): @# ./run_pc.sh start@
233 1 Paul Kocialkowski
234 9 Paul Kocialkowski
Note that if you use [[NetworkManager]], it could notice a new network interface and try to configure it: you should disconnect this interface on [[NetworkManager]] before you run the second script. 
235 1 Paul Kocialkowski
236
Now everything is in place. If no error is shown, it should now be working. You can try to ping some website on the device.
237
If It doesn't work, try to run again each script, running the configuration one more time can't do any worse. 
238 7 Paul Kocialkowski
239 9 Paul Kocialkowski
h4. Getting back to normal
240 1 Paul Kocialkowski
241
If you want to disable Replicant USB Networking, you have to:
242 12 Paul Kocialkowski
* Stop the script on the device: @adb shell "sh /data/run_dev.sh stop"@
243
* Stop the script on the host computer: @# ./run_pc.sh stop@
244 2 Paul Kocialkowski
245 9 Paul Kocialkowski
h3. Customizing the scripts
246
247 1 Paul Kocialkowski
The variables at the beginning of each script can be customized for your usage:
248 3 Paul Kocialkowski
249 1 Paul Kocialkowski
h4. run_pc.sh
250
251 12 Paul Kocialkowski
* @IPTABLES_CLEAN_RULES@ can be set to true or false, depending if you want the iptables rules to be cleaned before the configuration.
252
* @USB_IFACE@ contains the usb network interface name to the device. You can find out this name by launching "ifconfig" before and after you run the first script: the usb interface will appear after the first script completed.
253
* @INTERNET_IFACE@ defines the name of the network interface connected to the internet. This should be the one configured with an IP address on ifconfig (and which is not called lo). If you use an Ethernet connection, it's probably "eth0" and if you use a WiFi? connection, it's probably "wlan0".
254
* @USB_IFACE_IP@ configures the IP address to give to the computer on the network between the device and the computer. Note that this must be the same address in GATEWAY_IP on the device script. 
255 1 Paul Kocialkowski
256
h4. run_dev.sh
257 12 Paul Kocialkowski
258
* @USB_IFACE@ contains the usb network interface name to the computer. This should normally not be changed.
259
* @USB_IFACE_IP@ configures the IP address to give to the device on the network between the device and the computer.
260
* @GATEWAY_IP@ configures the gateway's IP address. Note that this must be the same address in USB_IFACE_IP on the computer script.
261
* @DNS1_IP@ defines which DNS server to use for name resolution. This is set by default to the Google DNS server but you can change it as you want to any other DNS server.