Project

General

Profile

USBNetworking » History » Version 2

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