This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
/
net.sh
executable file
·186 lines (149 loc) · 5.27 KB
/
net.sh
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
#!/bin/bash
[ -n "$DEBUG" ] && set -o xtrace
set -o nounset
set -o errexit
shopt -s nullglob
filter_forward_chain="warden-forward"
filter_default_chain="warden-default"
filter_instance_prefix="warden-instance-"
nat_prerouting_chain="warden-prerouting"
nat_postrouting_chain="warden-postrouting"
nat_instance_prefix="warden-instance-"
# Default ALLOW_NETWORKS/DENY_NETWORKS to empty
ALLOW_NETWORKS=${ALLOW_NETWORKS:-}
DENY_NETWORKS=${DENY_NETWORKS:-}
function external_ip() {
# The ';tx;d;:x' trick deletes non-matching lines
ip route get 8.8.8.8 | sed 's/.*src\s\(.*\)\s/\1/;tx;d;:x'
}
function teardown_deprecated_rules() {
# Remove jump to warden-dispatch from INPUT
iptables -S INPUT 2> /dev/null |
grep " -j warden-dispatch" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables
# Remove jump to warden-dispatch from FORWARD
iptables -S FORWARD 2> /dev/null |
grep " -j warden-dispatch" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables
# Prune warden-dispatch
iptables -F warden-dispatch 2> /dev/null || true
# Delete warden-dispatch
iptables -X warden-dispatch 2> /dev/null || true
}
function teardown_filter() {
teardown_deprecated_rules
# Prune warden-forward chain
iptables -S ${filter_forward_chain} 2> /dev/null |
grep "\-g ${filter_instance_prefix}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables
# Prune per-instance chains
iptables -S 2> /dev/null |
grep "^-A ${filter_instance_prefix}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables
# Delete per-instance chains
iptables -S 2> /dev/null |
grep "^-N ${filter_instance_prefix}" |
sed -e "s/-N/-X/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables
# Remove jump to warden-forward from FORWARD
iptables -S FORWARD 2> /dev/null |
grep " -j ${filter_forward_chain}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables
iptables -F ${filter_forward_chain} 2> /dev/null || true
iptables -F ${filter_default_chain} 2> /dev/null || true
}
function setup_filter() {
teardown_filter
# Create or flush forward chain
iptables -N ${filter_forward_chain} 2> /dev/null || iptables -F ${filter_forward_chain}
iptables -A ${filter_forward_chain} -j DROP
# Create or flush default chain
iptables -N ${filter_default_chain} 2> /dev/null || iptables -F ${filter_default_chain}
for n in ${ALLOW_NETWORKS}; do
if [ "$n" == "" ]
then
break
fi
iptables -A ${filter_default_chain} --destination "$n" --jump RETURN
done
for n in ${DENY_NETWORKS}; do
if [ "$n" == "" ]
then
break
fi
iptables -A ${filter_default_chain} --destination "$n" --jump DROP
done
# Forward outbound traffic via ${filter_forward_chain}
iptables -A FORWARD -i w-+ --jump ${filter_forward_chain}
# Forward inbound traffic immediately
default_interface=$(ip route show | grep default | cut -d' ' -f5 | head -1)
iptables -I ${filter_forward_chain} -i $default_interface --jump ACCEPT
}
function teardown_nat() {
# Prune prerouting chain
iptables -t nat -S ${nat_prerouting_chain} 2> /dev/null |
grep "\-j ${nat_instance_prefix}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables -t nat
# Prune per-instance chains
iptables -t nat -S 2> /dev/null |
grep "^-A ${nat_instance_prefix}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables -t nat
# Delete per-instance chains
iptables -t nat -S 2> /dev/null |
grep "^-N ${nat_instance_prefix}" |
sed -e "s/-N/-X/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 iptables -t nat
# Flush prerouting chain
iptables -t nat -F ${nat_prerouting_chain} 2> /dev/null || true
# Flush postrouting chain
iptables -t nat -F ${nat_postrouting_chain} 2> /dev/null || true
}
function setup_nat() {
teardown_nat
# Create prerouting chain
iptables -t nat -N ${nat_prerouting_chain} 2> /dev/null || true
# Bind chain to PREROUTING
(iptables -t nat -S PREROUTING | grep -q "\-j ${nat_prerouting_chain}\b") ||
iptables -t nat -A PREROUTING \
--jump ${nat_prerouting_chain}
# Bind chain to OUTPUT (for traffic originating from same host)
(iptables -t nat -S OUTPUT | grep -q "\-j ${nat_prerouting_chain}\b") ||
iptables -t nat -A OUTPUT \
--out-interface "lo" \
--jump ${nat_prerouting_chain}
# Create postrouting chain
iptables -t nat -N ${nat_postrouting_chain} 2> /dev/null || true
# Bind chain to POSTROUTING
(iptables -t nat -S POSTROUTING | grep -q "\-j ${nat_postrouting_chain}\b") ||
iptables -t nat -A POSTROUTING \
--jump ${nat_postrouting_chain}
# Enable NAT for traffic coming from containers
(iptables -t nat -S ${nat_postrouting_chain} | grep -q "\-j SNAT\b") ||
iptables -t nat -A ${nat_postrouting_chain} \
--source ${POOL_NETWORK} \
--jump SNAT \
--to $(external_ip)
}
case "${1}" in
setup)
setup_filter
setup_nat
# Enable forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
;;
teardown)
teardown_filter
teardown_nat
;;
*)
echo "Unknown command: ${1}" 1>&2
exit 1
;;
esac