Project

General

Profile

USBNetworking » History » Version 5

Paul Kocialkowski, 04/11/2011 09:54 AM

1 1 Paul Kocialkowski
= Replicant USB Networking =
2
3
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
== What you need ==
6
7 3 Paul Kocialkowski
 * A phone running Replicant
8
 * A computer with USB connectivity and network access running GNU/Linux
9 1 Paul Kocialkowski
10
== Automation scripts ==
11
12
Two scripts are necessary: one to run on the host computer and one to run on the device.
13
14
=== Replicant USB Networking - PC ===
15
16
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
{{{
19
#!/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
	;;
87
	*)
88 3 Paul Kocialkowski
		echo "Usage: sh $0 {start|stop}"
89
	;;
90 1 Paul Kocialkowski
esac
91
}}}
92
93
Then, set this file executable: 
94
{{{ chmod a+x run_pc.sh }}}
95
96
=== Replicant USB Networking - Device ===
97
98
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):
99
100
{{{
101
#!/system/bin/sh
102
103
# Replicant USB Networking
104
# ========================                             
105
# 
106 5 Paul Kocialkowski
# Copyright (C) 2011 Paul Kocialkowski, GPLv3+
107 1 Paul Kocialkowski
# 
108
# This program is free software: you can redistribute it and/or modify
109
# it under the terms of the GNU General Public License as published by
110
# the Free Software Foundation, either version 3 of the License, or
111
# (at your option) any later version.
112
# 
113
# You should have received a copy of the GNU General Public License
114
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
115
116
# Enable USB Networking
117
118
USB_IFACE="usb0"
119
120
USB_IFACE_IP="192.168.4.202"
121
GATEWAY_IP="192.168.4.200"
122
DNS1_IP="8.8.8.8"
123
124
usb_networking_enable () {
125
	echo 1 > /sys/class/usb_composite/rndis/enable
126
	ifconfig usb0 up
127
}
128
129
usb_networking_configure () {
130
	ifconfig $USB_IFACE $USB_IFACE_IP
131
	route add default gw $GATEWAY_IP dev $USB_IFACE
132
	setprop net.dns1 $DNS1_IP
133
134
	# setprop net.dns1 $( cat /system/etc/resolv.conf | sed -e "s|.*#.*||" -e "s|^.*nameserver \(.*\)$|\1|g" | grep -v "^$" | head -n 1 )
135
}
136
137
usb_networking_disable () {
138
	echo 0 > /sys/class/usb_composite/rndis/enable
139
	ifconfig usb0 down
140
}
141
142
case $1 in
143
	"start")
144
		echo "Starting Replicant USB Networking"
145
		usb_networking_enable
146
		usb_networking_configure
147
	;;
148
	"stop")
149
		echo "Stopping Replicant USB Networking"
150
		usb_networking_disable
151
	;;
152
	*)
153
		echo "Usage: sh $0 {start|stop}"
154
	;;
155
esac
156
}}}
157
158 2 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 /sdcard/ }}} (you can change /sdcard/ to any other writable disk place, just remind where you put it).
159
Get a shell on your device ({{{ adb shell }}}) and set this file executable: {{{ # chmod a+x /sdcard/run_dev.sh }}}.
160
161
=== Using the scripts ===
162
163 4 Paul Kocialkowski
Now both scripts are in place. You should now start "run_dev.sh" on the device:
164 2 Paul Kocialkowski
{{{ adb shell "sh /sdcard/run_dev.sh start" }}}
165
and then "run_pc.sh" '''as root''' on the host pc:
166 4 Paul Kocialkowski
{{{ # ./run_pc.sh start }}}
167 2 Paul Kocialkowski
assuming that you are on the directory where you created the script.
168
169
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. 
170 1 Paul Kocialkowski
171
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.
172 4 Paul Kocialkowski
173
==== Getting back to normal ====
174
175
If you want to disable Replicant USB Networking, you have to :
176
 * Stop the script on the device: {{{ adb shell "sh /sdcard/run_dev.sh stop" }}}
177
 * Stop the script on the host computer: {{{ # ./run_pc.sh stop }}}
178 2 Paul Kocialkowski
179
=== Customizing the scripts ===
180
181 1 Paul Kocialkowski
The variables at the beginning of each script can be customized for your usage:
182
183 2 Paul Kocialkowski
==== run_pc.sh ====
184 1 Paul Kocialkowski
185 3 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.
186
 * {{{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. 
187
 * {{{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". 
188
 * {{{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.
189 2 Paul Kocialkowski
190
==== run_dev.sh ====
191
192 3 Paul Kocialkowski
 * {{{USB_IFACE}}} contains the usb network interface name to the computer. This should normally not be changed. 
193
 * {{{USB_IFACE_IP}}} configures the IP address to give to the device on the network between the device and the computer.
194
 * {{{GATEWAY_IP}}} configures the gateway's IP address. Note that this must be the same address in {{{USB_IFACE_IP}}} on the computer script. 
195
 * {{{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.