Project

General

Profile

USBNetworking » History » Version 13

Denis 'GNUtoo' Carikli, 12/24/2012 04:56 AM

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