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