1
|
#!/bin/sh
|
2
|
|
3
|
# Replicant USB Networking
|
4
|
# ========================
|
5
|
#
|
6
|
# Copyright (C) 2011 Paul Kocialkowski, GPLv3+
|
7
|
#
|
8
|
# This program is free software: you can redistribute it and/or modify
|
9
|
# it under the terms of the GNU General Public License as published by
|
10
|
# the Free Software Foundation, either version 3 of the License, or
|
11
|
# (at your option) any later version.
|
12
|
#
|
13
|
# You should have received a copy of the GNU General Public License
|
14
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
15
|
|
16
|
IPTABLES_CLEAN_RULES=true
|
17
|
|
18
|
USB_IFACE="usb0"
|
19
|
INTERNET_IFACE="eth0"
|
20
|
|
21
|
USB_IFACE_IP="192.168.4.200"
|
22
|
|
23
|
# Clean iptables rules
|
24
|
|
25
|
iptables_rules_clean () {
|
26
|
if [ $IPTABLES_CLEAN_RULES = false ]
|
27
|
then
|
28
|
return
|
29
|
fi
|
30
|
|
31
|
iptables --flush
|
32
|
iptables --table nat --flush
|
33
|
iptables --delete-chain
|
34
|
iptables --table nat --delete-chain
|
35
|
}
|
36
|
|
37
|
# Inject iptables forwarding rules
|
38
|
|
39
|
iptables_forward_rules_apply () {
|
40
|
iptables --table nat --append POSTROUTING --out-interface $INTERNET_IFACE -j MASQUERADE
|
41
|
iptables --append FORWARD --in-interface $USB_IFACE -j ACCEPT
|
42
|
echo 1 > /proc/sys/net/ipv4/ip_forward
|
43
|
}
|
44
|
|
45
|
# Configure network link
|
46
|
|
47
|
usb_networking_configure () {
|
48
|
ifconfig $USB_IFACE up
|
49
|
ifconfig $USB_IFACE $USB_IFACE_IP
|
50
|
}
|
51
|
|
52
|
usb_networking_disable () {
|
53
|
ifconfig $USB_IFACE down
|
54
|
echo 0 > /proc/sys/net/ipv4/ip_forward
|
55
|
}
|
56
|
|
57
|
case $1 in
|
58
|
"start")
|
59
|
echo "Starting Replicant USB Networking"
|
60
|
iptables_rules_clean
|
61
|
usb_networking_configure
|
62
|
iptables_forward_rules_apply
|
63
|
;;
|
64
|
"stop")
|
65
|
echo "Stopping Replicant USB Networking"
|
66
|
usb_networking_disable
|
67
|
iptables_rules_clean
|
68
|
;;
|
69
|
*)
|
70
|
echo "Usage: sh $0 {start|stop}"
|
71
|
;;
|
72
|
esac
|