USBNetworking » History » Version 1
Paul Kocialkowski, 04/11/2011 09:30 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 | *)#!/bin/sh |
||
88 | |||
89 | # Replicant USB Networking |
||
90 | # ======================== |
||
91 | # |
||
92 | # Copyright (C) 2010 Paul Kocialkowski, GPLv3+ |
||
93 | # |
||
94 | # This program is free software: you can redistribute it and/or modify |
||
95 | # it under the terms of the GNU General Public License as published by |
||
96 | # the Free Software Foundation, either version 3 of the License, or |
||
97 | # (at your option) any later version. |
||
98 | # |
||
99 | # You should have received a copy of the GNU General Public License |
||
100 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
101 | |||
102 | IPTABLES_CLEAN_RULES=true |
||
103 | |||
104 | USB_IFACE="usb0" |
||
105 | INTERNET_IFACE="eth0" |
||
106 | |||
107 | USB_IFACE_IP="192.168.4.200" |
||
108 | |||
109 | # Clean iptables rules |
||
110 | |||
111 | iptables_rules_clean () { |
||
112 | if [ $IPTABLES_CLEAN_RULES = false ] |
||
113 | then |
||
114 | return |
||
115 | fi |
||
116 | |||
117 | iptables --flush |
||
118 | iptables --table nat --flush |
||
119 | iptables --delete-chain |
||
120 | iptables --table nat --delete-chain |
||
121 | } |
||
122 | |||
123 | # Inject iptables forwarding rules |
||
124 | |||
125 | iptables_forward_rules_apply () { |
||
126 | iptables --table nat --append POSTROUTING --out-interface $INTERNET_IFACE -j MASQUERADE |
||
127 | iptables --append FORWARD --in-interface $USB_IFACE -j ACCEPT |
||
128 | echo 1 > /proc/sys/net/ipv4/ip_forward |
||
129 | } |
||
130 | |||
131 | # Configure network link |
||
132 | |||
133 | usb_networking_configure () { |
||
134 | ifconfig $USB_IFACE up |
||
135 | ifconfig $USB_IFACE $USB_IFACE_IP |
||
136 | } |
||
137 | |||
138 | usb_networking_disable () { |
||
139 | ifconfig $USB_IFACE down |
||
140 | echo 0 > /proc/sys/net/ipv4/ip_forward |
||
141 | } |
||
142 | |||
143 | case $1 in |
||
144 | "start") |
||
145 | echo "Starting Replicant USB Networking" |
||
146 | iptables_rules_clean |
||
147 | usb_networking_configure |
||
148 | iptables_forward_rules_apply |
||
149 | ;; |
||
150 | "stop") |
||
151 | echo "Stopping Replicant USB Networking" |
||
152 | usb_networking_disable |
||
153 | iptables_rules_clean |
||
154 | ;; |
||
155 | *) |
||
156 | echo "Usage: sh $0 {start|stop}" |
||
157 | ;; |
||
158 | esac |
||
159 | echo "Usage: sh $0 {start|stop}" |
||
160 | ;; |
||
161 | esac |
||
162 | }}} |
||
163 | |||
164 | Then, set this file executable: |
||
165 | {{{ chmod a+x run_pc.sh }}} |
||
166 | |||
167 | === Replicant USB Networking - Device === |
||
168 | |||
169 | 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): |
||
170 | |||
171 | {{{ |
||
172 | #!/system/bin/sh |
||
173 | |||
174 | # Replicant USB Networking |
||
175 | # ======================== |
||
176 | # |
||
177 | # Copyright (C) 2010 Paul Kocialkowski, GPLv3+ |
||
178 | # |
||
179 | # This program is free software: you can redistribute it and/or modify |
||
180 | # it under the terms of the GNU General Public License as published by |
||
181 | # the Free Software Foundation, either version 3 of the License, or |
||
182 | # (at your option) any later version. |
||
183 | # |
||
184 | # You should have received a copy of the GNU General Public License |
||
185 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
186 | |||
187 | # Enable USB Networking |
||
188 | |||
189 | USB_IFACE="usb0" |
||
190 | |||
191 | USB_IFACE_IP="192.168.4.202" |
||
192 | GATEWAY_IP="192.168.4.200" |
||
193 | DNS1_IP="8.8.8.8" |
||
194 | |||
195 | usb_networking_enable () { |
||
196 | echo 1 > /sys/class/usb_composite/rndis/enable |
||
197 | ifconfig usb0 up |
||
198 | } |
||
199 | |||
200 | usb_networking_configure () { |
||
201 | ifconfig $USB_IFACE $USB_IFACE_IP |
||
202 | route add default gw $GATEWAY_IP dev $USB_IFACE |
||
203 | setprop net.dns1 $DNS1_IP |
||
204 | |||
205 | # setprop net.dns1 $( cat /system/etc/resolv.conf | sed -e "s|.*#.*||" -e "s|^.*nameserver \(.*\)$|\1|g" | grep -v "^$" | head -n 1 ) |
||
206 | } |
||
207 | |||
208 | usb_networking_disable () { |
||
209 | echo 0 > /sys/class/usb_composite/rndis/enable |
||
210 | ifconfig usb0 down |
||
211 | } |
||
212 | |||
213 | case $1 in |
||
214 | "start") |
||
215 | echo "Starting Replicant USB Networking" |
||
216 | usb_networking_enable |
||
217 | usb_networking_configure |
||
218 | ;; |
||
219 | "stop") |
||
220 | echo "Stopping Replicant USB Networking" |
||
221 | usb_networking_disable |
||
222 | ;; |
||
223 | *) |
||
224 | echo "Usage: sh $0 {start|stop}" |
||
225 | ;; |
||
226 | esac |
||
227 | }}} |
||
228 | |||
229 | 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). |