blob: 82f7c59a4e832db99d1d24d26ff8128b8d673199 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
#!/bin/bash
# Copyright 2010 Google Inc.
# All right reserved.
# Author: Szymon Jakubczak <szym@google.com>
#
# Configure the host and the Android phone for "reverse tethering".
# (Route all network traffic via the host.)
# default values
: ${BRIDGE:=usbeth}
: ${LAN_DEV:=eth0} # LAN uplink on the host
: ${HOST_DEV:=usb0} # name of the RNDIS interface on the host
: ${PHONE_DEV:=rndis0} # name of the RNDIS interface on the phone
: ${PHONE_IP:=192.168.77.2} # for NAT and tests
: ${HOST_IP:=192.168.77.1}
: ${NETMASK:=255.255.255.0}
# location of the hwaddr utility
: ${HWADDR:=/home/build/nonconf/google3/experimental/users/szym/moblat/hwaddr/hwaddr-armeabi}
: ${PHONE_HW:=""} # hardware (Ethernet) address for the interface (bridge only)
# for NAT configuration
: ${DNS1:=8.8.8.8}
: ${DNS2:=8.8.4.4}
# export ADB=/path/to/sdk/adb for custom adb
ADB="${ADB:-adb} ${SERIAL:+-s $SERIAL}"
set -e
trap error ERR
error() {
echo >&2 "Error occured: $?"
}
usage() {
echo "Usage: $0 <command>"
echo " rndis -- start RNDIS and test ping the phone"
echo " nat -- use host as NAT"
echo " nat+secure -- nat + extra security"
echo " bridge -- use host as bridge"
echo " stop -- switch back to 3G"
echo " stop-all -- clean up everything"
echo
echo "Advanced Commands"
echo " Host:"
echo " nat_start "
echo " nat_secure "
echo " nat_stop "
echo " bridge_start "
echo " bridge_add "
echo " bridge_stop "
echo " Phone:"
echo " rndis_start "
echo " rndis_stop "
echo " rndis_test "
echo " route_nat "
echo " route_bridge "
echo " route_reset "
echo
echo "Options and Environment Variables:"
echo " -h|--help"
echo " -b bridge_name BRIDGE=$BRIDGE"
echo " -s serial_number SERIAL=$SERIAL"
echo " -u host_usb_device HOST_DEV=$HOST_DEV"
echo " -l host_lan_device LAN_DEV=$LAN_DEV"
echo " -d dns1 dns2 DNS1=$DNS1"
echo " DNS2=$DNS2"
echo " -p phone_ip PHONE_IP=$PHONE_IP"
echo " -a host_ip HOST_IP=$HOST_IP"
echo " -m netmask NETMASK=$NETMASK"
echo " -e hardware_addr PHONE_HW=$PHONE_HW"
echo
echo " HWADDR=$HWADDR"
echo " ADB=$ADB"
}
##################################
### PHONE configuration routines
##################################
rndis_start() {
echo "Starting RNDIS..."
$ADB wait-for-device
$ADB shell "svc usb setFunction rndis"
$ADB wait-for-device
$ADB shell "ifconfig $PHONE_DEV down"
if [[ -n "$PHONE_HW" ]]; then
$ADB push $HWADDR /data/local/hwaddr # TODO(szym) handle failures?
$ADB shell "/data/local/hwaddr $PHONE_DEV $PHONE_HW"
$ADB shell "/data/local/hwaddr $PHONE_DEV"
fi
}
rndis_stop() {
$ADB shell "svc usb setFunction" #empty to clear
}
rndis_test() {
# configure some IPs, so that we can ping
$ADB shell "ifconfig $PHONE_DEV $PHONE_IP netmask $NETMASK up"
sudo ifconfig $HOST_DEV $HOST_IP netmask $NETMASK up
echo "Pinging the phone..."
ping -q -c 1 -W 1 $PHONE_IP
echo "Success!"
}
update_dns() {
$ADB shell 'setprop net.dnschange $((`getprop net.dnschange`+1))'
}
default_routes() {
$ADB shell 'cat /proc/net/route' | awk '{ if ($2==00000000) print $1 }'
}
route_none() {
$ADB shell "svc data disable"
$ADB shell "svc wifi disable"
# kill all default route interfaces (just in case something remains)
for dev in `default_routes`; do
$ADB shell "ifconfig $dev down"
done
}
route_nat() {
echo "Setting up phone routes and DNS..."
route_none
$ADB shell "ifconfig $PHONE_DEV $PHONE_IP netmask $NETMASK up"
$ADB shell "route add default gw $HOST_IP dev $PHONE_DEV"
$ADB shell "setprop net.dns1 $DNS1"
$ADB shell "setprop net.dns2 $DNS2"
update_dns
}
route_bridge() {
echo "Running DHCP on the phone..."
route_none
$ADB shell "ifconfig $PHONE_DEV up"
$ADB shell "netcfg $PHONE_DEV dhcp"
$ADB shell "ifconfig $PHONE_DEV" # for diagnostics
DNS1=`$ADB shell getprop net.${PHONE_DEV}.dns1`
$ADB shell "setprop net.dns1 $DNS1"
DNS2=`$ADB shell getprop net.${PHONE_DEV}.dns2`
$ADB shell "setprop net.dns2 $DNS2"
update_dns
}
route_reset() {
route_none
$ADB shell "svc data enable"
}
#################################
### HOST configuration routines
#################################
nat_start() {
echo "Configuring NAT..."
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -F
sudo iptables -t nat -F
sudo iptables -t nat -A POSTROUTING -o $LAN_DEV -j MASQUERADE
sudo iptables -P FORWARD ACCEPT
sudo ifconfig $HOST_DEV $HOST_IP netmask $NETMASK up
}
nat_secure() {
echo "Making your NAT secure..."
sudo iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -m state --state NEW -i $HOST_DEV -j ACCEPT
sudo iptables -P FORWARD DROP
sudo ifconfig usb0 $HOST_IP netmask $NETMASK up
}
nat_stop() {
sudo sysctl -w net.ipv4.ip_forward=0
sudo iptables -F
sudo iptables -t nat -F
}
bridge_start() {
echo "Configuring bridge..."
sudo brctl addbr $BRIDGE || return 0 # all good
sudo brctl setfd $BRIDGE 0
sudo ifconfig $LAN_DEV 0.0.0.0
sudo brctl addif $BRIDGE $LAN_DEV
sudo dhclient $BRIDGE || {
echo "DHCP failed. Recovering..."
bridge_stop
false
}
}
bridge_add() {
echo "Adding usb0 to the bridge"
sudo brctl delif $BRIDGE $HOST_DEV 2>/dev/null || true # ignore
sudo ifconfig $HOST_DEV 0.0.0.0
sudo brctl addif $BRIDGE $HOST_DEV
}
bridge_stop() {
sudo ifconfig $BRIDGE down || true # ignore errors
sudo brctl delbr $BRIDGE || true
sudo dhclient $LAN_DEV
}
### command-line interpreter
if [ $# == "0" ]; then
usage
fi
while (( $# )); do
case $1 in
--help|-h)
usage
exit
;;
-b) shift; BRIDGE=$1 ;;
-s) shift; SERIAL=$1 ;;
-u) shift; HOST_DEV=$1 ;;
-l) shift; LAN_DEV=$1 ;;
-d) shift; DNS1=$1; shift; DNS2=$1 ;;
-p) shift; PHONE_IP=$1 ;;
-a) shift; HOST_IP=$1 ;;
-m) shift; NETMASK=$1 ;;
-e) shift; PHONE_HW=$1 ;;
rndis)
rndis_start
rndis_test
;;
bridge)
ifconfig $HOST_DEV >/dev/null || $0 rndis
bridge_start
bridge_add
route_bridge
;;
nat)
ifconfig $HOST_DEV >/dev/null || $0 rndis
nat_start
route_nat
;;
nat+secure)
$0 nat
nat_secure
;;
stop)
route_reset
;;
stop-all)
bridge_stop
nat_stop
route_reset
rndis_stop
;;
*) # execute 'advanced command' by function name
$1
;;
esac
shift
done
|