USBNetworking » History » Version 10
Paul Kocialkowski, 02/05/2012 01:23 PM
| 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 | Then, set this file executable: |
||
| 94 | |||
| 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 | 9 | Paul Kocialkowski | <pre> |
| 100 | 1 | Paul Kocialkowski | #!/system/bin/sh |
| 101 | |||
| 102 | # Replicant USB Networking |
||
| 103 | # ======================== |
||
| 104 | # |
||
| 105 | 5 | Paul Kocialkowski | # Copyright (C) 2011 Paul Kocialkowski, GPLv3+ |
| 106 | 1 | Paul Kocialkowski | # |
| 107 | # This program is free software: you can redistribute it and/or modify |
||
| 108 | # it under the terms of the GNU General Public License as published by |
||
| 109 | # the Free Software Foundation, either version 3 of the License, or |
||
| 110 | # (at your option) any later version. |
||
| 111 | # |
||
| 112 | # You should have received a copy of the GNU General Public License |
||
| 113 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
| 114 | |||
| 115 | # Enable USB Networking |
||
| 116 | |||
| 117 | USB_IFACE="usb0" |
||
| 118 | |||
| 119 | USB_IFACE_IP="192.168.4.202" |
||
| 120 | GATEWAY_IP="192.168.4.200" |
||
| 121 | DNS1_IP="8.8.8.8" |
||
| 122 | |||
| 123 | usb_networking_enable () { |
||
| 124 | echo 1 > /sys/class/usb_composite/rndis/enable |
||
| 125 | ifconfig usb0 up |
||
| 126 | } |
||
| 127 | |||
| 128 | usb_networking_configure () { |
||
| 129 | ifconfig $USB_IFACE $USB_IFACE_IP |
||
| 130 | route add default gw $GATEWAY_IP dev $USB_IFACE |
||
| 131 | setprop net.dns1 $DNS1_IP |
||
| 132 | |||
| 133 | # setprop net.dns1 $( cat /system/etc/resolv.conf | sed -e "s|.*#.*||" -e "s|^.*nameserver \(.*\)$|\1|g" | grep -v "^$" | head -n 1 ) |
||
| 134 | } |
||
| 135 | |||
| 136 | usb_networking_disable () { |
||
| 137 | echo 0 > /sys/class/usb_composite/rndis/enable |
||
| 138 | ifconfig usb0 down |
||
| 139 | } |
||
| 140 | |||
| 141 | case $1 in |
||
| 142 | "start") |
||
| 143 | echo "Starting Replicant USB Networking" |
||
| 144 | usb_networking_enable |
||
| 145 | usb_networking_configure |
||
| 146 | ;; |
||
| 147 | "stop") |
||
| 148 | echo "Stopping Replicant USB Networking" |
||
| 149 | usb_networking_disable |
||
| 150 | ;; |
||
| 151 | 2 | Paul Kocialkowski | *) |
| 152 | echo "Usage: sh $0 {start|stop}" |
||
| 153 | 1 | Paul Kocialkowski | ;; |
| 154 | esac |
||
| 155 | 9 | Paul Kocialkowski | </pre> |
| 156 | 1 | Paul Kocialkowski | |
| 157 | |||
| 158 | 9 | Paul Kocialkowski | h3. Using the scripts |
| 159 | |||
| 160 | 1 | Paul Kocialkowski | Now both scripts are in place. You should now: |
| 161 | 9 | Paul Kocialkowski | <pre> |
| 162 | <pre> |
||
| 163 | 1 | Paul Kocialkowski | |
| 164 | 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. |
| 165 | 1 | Paul Kocialkowski | |
| 166 | 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. |
||
| 167 | If It doesn't work, try to run again each script, running the configuration one more time can't do any worse. |
||
| 168 | 7 | Paul Kocialkowski | |
| 169 | 9 | Paul Kocialkowski | h4. Getting back to normal |
| 170 | |||
| 171 | 8 | Paul Kocialkowski | If you want to disable Replicant USB Networking, you have to: |
| 172 | 2 | Paul Kocialkowski | |
| 173 | 9 | Paul Kocialkowski | h3. Customizing the scripts |
| 174 | |||
| 175 | 1 | Paul Kocialkowski | The variables at the beginning of each script can be customized for your usage: |
| 176 | 3 | Paul Kocialkowski | |
| 177 | 9 | Paul Kocialkowski | h4. run_pc.sh |
| 178 | 3 | Paul Kocialkowski | |
| 179 | 2 | Paul Kocialkowski | |
| 180 | 9 | Paul Kocialkowski | h4. run_dev.sh |