1 | #!/bin/sh |
---|
2 | |
---|
3 | # set IP MASQUERADE for listed devices |
---|
4 | # this script must be set on the LAN gateway device |
---|
5 | |
---|
6 | |
---|
7 | set -x |
---|
8 | |
---|
9 | ## INTERFACES(5) |
---|
10 | # IFACE physical name of the interface being processed |
---|
11 | # METHOD method of the interface (e.g., static) |
---|
12 | # MODE start if run from ifup, stop if run from ifdown |
---|
13 | # PHASE as per MODE, but with finer granularity, distinguishing the pre- |
---|
14 | # up, post-up, pre-down and post-down phases. |
---|
15 | |
---|
16 | # optional argument to set default gateway devices connected to WAN manually |
---|
17 | # (try each of them in order) |
---|
18 | GATEWAYS="$@" |
---|
19 | |
---|
20 | if [ "${PHASE}" = "post-down" ]; then |
---|
21 | # flush POSTROUTING chain and exit |
---|
22 | /sbin/iptables -t nat -F POSTROUTING |
---|
23 | exit |
---|
24 | fi |
---|
25 | |
---|
26 | |
---|
27 | ( |
---|
28 | |
---|
29 | # wait for initialization of WAN device with DHCPd |
---|
30 | sleep 60 |
---|
31 | |
---|
32 | if [ -z "${GATEWAYS}" ]; then |
---|
33 | GATEWAYS=`/sbin/ip route show table main | grep -w default | tr -s ' ' | cut -d ' ' -f 5` |
---|
34 | fi |
---|
35 | |
---|
36 | for GW in ${GATEWAYS}; do |
---|
37 | if [ ${GW} = ${IFACE} ]; then |
---|
38 | # skip if device is the LAN gateway |
---|
39 | continue |
---|
40 | fi |
---|
41 | |
---|
42 | IPADDR=`/sbin/ip addr show dev ${GW} | grep -w inet | tr -s ' ' | cut -d ' ' -f 3 | cut -d / -f 1` |
---|
43 | if [ -z "${IPADDR}" ]; then |
---|
44 | # device doesn't seem to have an IP address |
---|
45 | continue |
---|
46 | fi |
---|
47 | |
---|
48 | ## set masquerade rule to all devices |
---|
49 | # because there is no way to know which one is default gateway |
---|
50 | # without manual specification |
---|
51 | /sbin/iptables -t nat -A POSTROUTING -o ${GW} -j MASQUERADE |
---|
52 | done |
---|
53 | |
---|
54 | ) & |
---|