1
1
#! /bin/bash
2
2
3
- VERSION=4.1.1
3
+ VERSION=4.2.0
4
+ IPTABLES=$( which iptables)
5
+ IPTABLES_OPTS=()
4
6
5
- # Ingress Routing Daemon v4.1.1
6
- # Copyright © 2020-2021 Struan Bartlett
7
+ # Ingress Routing Daemon v4.2.0
8
+ # Copyright © 2020-2023 Struan Bartlett
7
9
# ----------------------------------------------------------------------
8
10
# Permission is hereby granted, free of charge, to any person
9
11
# obtaining a copy of this software and associated documentation files
@@ -73,7 +75,7 @@ route_ingress() {
73
75
fi
74
76
75
77
local IPTABLE_COMMENT=" docker-ingress-routing-daemon"
76
- if nsenter -n -t $NID iptables -t mangle -C OUTPUT -m comment --comment " $IPTABLE_COMMENT " 2> /dev/null; then
78
+ if nsenter -n -t $NID iptables " ${IPTABLES_OPTS[@]} " -t mangle -C OUTPUT -m comment --comment " $IPTABLE_COMMENT " 2> /dev/null; then
77
79
log " Detected container for service '$SERVICE ', with ID '$ID ' and NID '$NID ': mangle table already configured, so skipping."
78
80
return
79
81
fi
@@ -86,9 +88,9 @@ route_ingress() {
86
88
# TOS byte has been set by the load balancer, then none will be restored and legacy routing rules will apply.
87
89
# - See https://github.com/newsnowlabs/docker-ingress-routing-daemon/issues/11
88
90
log " - Adding container mangle table iptables rules"
89
- nsenter -n -t $NID iptables -t mangle -A OUTPUT -m comment --comment " $IPTABLE_COMMENT "
90
- nsenter -n -t $NID iptables -t mangle -A OUTPUT -p udp -j CONNMARK --restore-mark
91
- nsenter -n -t $NID iptables -t mangle -A OUTPUT -p tcp -j CONNMARK --restore-mark
91
+ nsenter -n -t $NID iptables " ${IPTABLES_OPTS[@]} " -t mangle -A OUTPUT -m comment --comment " $IPTABLE_COMMENT "
92
+ nsenter -n -t $NID iptables " ${IPTABLES_OPTS[@]} " -t mangle -A OUTPUT -p udp -j CONNMARK --restore-mark
93
+ nsenter -n -t $NID iptables " ${IPTABLES_OPTS[@]} " -t mangle -A OUTPUT -p tcp -j CONNMARK --restore-mark
92
94
93
95
# 3.1 Enable 'loose' rp_filter mode on interface $CIF (and 'all' as required by kernel
94
96
# see https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt)
@@ -108,7 +110,7 @@ route_ingress() {
108
110
log " - Adding container policy routing/firewall rules for load-balancer #$NODE_ID with IP $NODE_IP "
109
111
110
112
# 2. Map the TOS value on any incoming packets to a connection mark, using the same value.
111
- nsenter -n -t $NID iptables -t mangle -A PREROUTING -m tos --tos $NODE_ID /0xff -j CONNMARK --set-xmark $NODE_ID /0xffffffff
113
+ nsenter -n -t $NID iptables " ${IPTABLES_OPTS[@]} " -t mangle -A PREROUTING -m tos --tos $NODE_ID /0xff -j CONNMARK --set-xmark $NODE_ID /0xffffffff
112
114
113
115
# 4. Select the correct routing table to use, according to the firewall mark on the outgoing packet.
114
116
nsenter -n -t $NID ip rule add from $INGRESS_SUBNET fwmark $NODE_ID lookup $NODE_ID prio 32700
@@ -134,6 +136,9 @@ usage() {
134
136
echo " --preexisting - optionally install rules where needed" >&2
135
137
echo " on preexisting containers (recommended)" >&2
136
138
echo >&2
139
+ echo " --iptables-wait - pass '--wait' option to iptables" >&2
140
+ echo " --iptables-wait-seconds <n> - pass '--wait <n>' option to iptables" >&2
141
+ echo >&2
137
142
echo " --no-performance - disable performance optimisations" >&2
138
143
echo " --indexed-ids - use sequential ids for load balancers" >&2
139
144
echo " (forced where ingress subnet larger than /24)" >&2
171
176
--no-performance) shift ; PERFORMANCE=0; continue ; ;;
172
177
--indexed-ids) shift ; INDEXED_IDS=1; continue ; ;;
173
178
--preexisting) shift ; PREEXISTING=1; continue ; ;;
179
+ --iptables-wait) shift ; IPTABLES_WAIT=1; continue ; ;;
180
+ --iptables-wait-seconds) shift ; IPTABLES_WAIT=1; IPTABLES_WAIT_SECONDS=" $1 " ; shift ; continue ; ;;
174
181
175
182
-h|--help) usage; ;;
176
183
' ' ) break ; ;;
182
189
# Display usage, unless --install or --uninstall
183
190
[ -z " $INSTALL " ] && usage
184
191
192
+ # Add '--iptables-wait' or '--iptables-wait $IPTABLES_WAIT_SECONDS' to iptables options
193
+ [ -n " $IPTABLES_WAIT " ] && IPTABLES_OPTS+=(--wait $IPTABLES_WAIT_SECONDS )
194
+
185
195
# Convert arrays to comma-separated strings
186
196
TCPServicePortString=$( echo ${TCP_PORTS[@]} | tr ' ' ' ,' )
187
197
UDPServicePortString=$( echo ${UDP_PORTS[@]} | tr ' ' ' ,' )
209
219
# Delete any relevant preexisting rules.
210
220
log " Cleaning up any stale load-balancer rules ..."
211
221
212
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t nat -S | \
222
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t nat -S | \
213
223
grep -- ' -m ipvs --ipvs -j ACCEPT' | \
214
224
sed -r ' s/^-A /-D /' | \
215
225
while read RULE; \
216
226
do
217
- log " - Deleting old rule: iptables -t nat $RULE "
218
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t nat $RULE
227
+ log " - Deleting old rule: iptables " ${IPTABLES_OPTS[@]} " -t nat $RULE "
228
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t nat $RULE
219
229
done
220
230
221
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t mangle -S | \
231
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t mangle -S | \
222
232
grep -- ' -j TOS --set-tos' | \
223
233
sed -r ' s/^-A /-D /' | \
224
234
while read RULE; \
225
235
do
226
- log " - Deleting old rule: iptables -t mangle $RULE "
227
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t mangle $RULE
236
+ log " - Deleting old rule: iptables " ${IPTABLES_OPTS[@]} " -t mangle $RULE "
237
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t mangle $RULE
228
238
done
229
239
230
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t raw -S | \
240
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t raw -S | \
231
241
grep -- ' -j CT --notrack' | \
232
242
sed -r ' s/^-A /-D /' | \
233
243
while read RULE; \
234
244
do
235
- log " - Deleting old rule: iptables -t raw $RULE "
236
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t raw $RULE
245
+ log " - Deleting old rule: iptables " ${IPTABLES_OPTS[@]} " -t raw $RULE "
246
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t raw $RULE
237
247
done
238
248
239
249
if [ " $INSTALL " = " 0" ]; then
@@ -303,39 +313,39 @@ if [ -n "$NODE_ID" ]; then
303
313
304
314
# Add a rule ahead of the ingress network SNAT rule, that will cause the SNAT rule to be skipped.
305
315
if [ -z " $TCPServicePortString " ] && [ -z " $UDPServicePortString " ]; then
306
- log " - Adding ingress_sbox iptables nat rule: iptables -t nat -I POSTROUTING -d $INGRESS_SUBNET -m ipvs --ipvs -j ACCEPT"
307
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t nat -I POSTROUTING -d $INGRESS_SUBNET -m ipvs --ipvs -j ACCEPT
316
+ log " - Adding ingress_sbox iptables nat rule: iptables " ${IPTABLES_OPTS[@]} " -t nat -I POSTROUTING -d $INGRESS_SUBNET -m ipvs --ipvs -j ACCEPT"
317
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t nat -I POSTROUTING -d $INGRESS_SUBNET -m ipvs --ipvs -j ACCEPT
308
318
309
319
# 1. Set TOS to NODE_ID in all outgoing packets to INGRESS_SUBNET
310
- log " - Adding ingress_sbox iptables mangle rule: iptables -t mangle -A POSTROUTING -d $INGRESS_SUBNET -j TOS --set-tos $NODE_ID /0xff"
311
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t mangle -A POSTROUTING -d $INGRESS_SUBNET -j TOS --set-tos $NODE_ID /0xff
320
+ log " - Adding ingress_sbox iptables mangle rule: iptables " ${IPTABLES_OPTS[@]} " -t mangle -A POSTROUTING -d $INGRESS_SUBNET -j TOS --set-tos $NODE_ID /0xff"
321
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t mangle -A POSTROUTING -d $INGRESS_SUBNET -j TOS --set-tos $NODE_ID /0xff
312
322
313
- log " - Adding ingress_sbox connection tracking disable rule: iptables -t raw -I PREROUTING -j CT --notrack"
314
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t raw -I PREROUTING -j CT --notrack
323
+ log " - Adding ingress_sbox connection tracking disable rule: iptables " ${IPTABLES_OPTS[@]} " -t raw -I PREROUTING -j CT --notrack"
324
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t raw -I PREROUTING -j CT --notrack
315
325
else
316
326
317
327
if [ -n " $TCPServicePortString " ]; then
318
- log " - Adding ingress_sbox iptables nat rule: iptables -t nat -I POSTROUTING -d $INGRESS_SUBNET -p tcp -m multiport --dports $TCPServicePortString -m ipvs --ipvs -j ACCEPT"
319
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t nat -I POSTROUTING -d $INGRESS_SUBNET -p tcp -m multiport --dports $TCPServicePortString -m ipvs --ipvs -j ACCEPT
328
+ log " - Adding ingress_sbox iptables nat rule: iptables " ${IPTABLES_OPTS[@]} " -t nat -I POSTROUTING -d $INGRESS_SUBNET -p tcp -m multiport --dports $TCPServicePortString -m ipvs --ipvs -j ACCEPT"
329
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t nat -I POSTROUTING -d $INGRESS_SUBNET -p tcp -m multiport --dports $TCPServicePortString -m ipvs --ipvs -j ACCEPT
320
330
321
331
# 1. Set TOS to NODE_ID in all outgoing packets to INGRESS_SUBNET
322
- log " - Adding ingress_sbox iptables mangle rule: iptables -t mangle -A POSTROUTING -d $INGRESS_SUBNET -p tcp -m multiport --dports $TCPServicePortString -j TOS --set-tos $NODE_ID /0xff"
323
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t mangle -A POSTROUTING -d $INGRESS_SUBNET -p tcp -m multiport --dports $TCPServicePortString -j TOS --set-tos $NODE_ID /0xff
332
+ log " - Adding ingress_sbox iptables mangle rule: iptables " ${IPTABLES_OPTS[@]} " -t mangle -A POSTROUTING -d $INGRESS_SUBNET -p tcp -m multiport --dports $TCPServicePortString -j TOS --set-tos $NODE_ID /0xff"
333
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t mangle -A POSTROUTING -d $INGRESS_SUBNET -p tcp -m multiport --dports $TCPServicePortString -j TOS --set-tos $NODE_ID /0xff
324
334
325
- log " - Adding ingress_sbox connection tracking disable rule: iptables -t raw -I PREROUTING -p tcp -m multiport --dports $TCPServicePortString -j CT --notrack"
326
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t raw -I PREROUTING -p tcp -m multiport --dports $TCPServicePortString -j CT --notrack
335
+ log " - Adding ingress_sbox connection tracking disable rule: iptables " ${IPTABLES_OPTS[@]} " -t raw -I PREROUTING -p tcp -m multiport --dports $TCPServicePortString -j CT --notrack"
336
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t raw -I PREROUTING -p tcp -m multiport --dports $TCPServicePortString -j CT --notrack
327
337
fi
328
338
329
339
if [ -n " $UDPServicePortString " ]; then
330
- log " - Adding ingress_sbox iptables nat rule: iptables -t nat -I POSTROUTING -d $INGRESS_SUBNET -p udp -m multiport --dports $UDPServicePortString -m ipvs --ipvs -j ACCEPT"
331
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t nat -I POSTROUTING -d $INGRESS_SUBNET -p udp -m multiport --dports $UDPServicePortString -m ipvs --ipvs -j ACCEPT
340
+ log " - Adding ingress_sbox iptables nat rule: iptables " ${IPTABLES_OPTS[@]} " -t nat -I POSTROUTING -d $INGRESS_SUBNET -p udp -m multiport --dports $UDPServicePortString -m ipvs --ipvs -j ACCEPT"
341
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t nat -I POSTROUTING -d $INGRESS_SUBNET -p udp -m multiport --dports $UDPServicePortString -m ipvs --ipvs -j ACCEPT
332
342
333
343
# 1. Set TOS to NODE_ID in all outgoing packets to INGRESS_SUBNET
334
- log " - Adding ingress_sbox iptables mangle rule: iptables -t mangle -A POSTROUTING -d $INGRESS_SUBNET -p udp -m multiport --dports $UDPServicePortString -j TOS --set-tos $NODE_ID /0xff"
335
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t mangle -A POSTROUTING -d $INGRESS_SUBNET -p udp -m multiport --dports $UDPServicePortString -j TOS --set-tos $NODE_ID /0xff
344
+ log " - Adding ingress_sbox iptables mangle rule: iptables " ${IPTABLES_OPTS[@]} " -t mangle -A POSTROUTING -d $INGRESS_SUBNET -p udp -m multiport --dports $UDPServicePortString -j TOS --set-tos $NODE_ID /0xff"
345
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t mangle -A POSTROUTING -d $INGRESS_SUBNET -p udp -m multiport --dports $UDPServicePortString -j TOS --set-tos $NODE_ID /0xff
336
346
337
347
log " - Adding ingress_sbox connection tracking disable rule: iptables -p udp -m multiport --dports $UDPServicePortString -j CT --notrack"
338
- nsenter --net=/var/run/docker/netns/ingress_sbox iptables -t raw -I PREROUTING -p udp -m multiport --dports $UDPServicePortString -j CT --notrack
348
+ nsenter --net=/var/run/docker/netns/ingress_sbox iptables " ${IPTABLES_OPTS[@]} " -t raw -I PREROUTING -p udp -m multiport --dports $UDPServicePortString -j CT --notrack
339
349
fi
340
350
341
351
fi
0 commit comments