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