Skip to content

Commit 69af801

Browse files
authored
firewall-iptables: drop old OUTPUT rules after reload (#268)
1 parent 3cf540e commit 69af801

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

nixos/modules/services/networking/firewall-iptables.nix

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,31 @@ let
273273
274274
${cfg.extraStopCommands}
275275
276+
N_OUTPUT_RULES="$(iptables --list OUTPUT --line-number | tail -1 | cut -d' ' -f1)"
277+
${lib.optionalString config.networking.enableIPv6 ''
278+
N_OUTPUT_RULES_IPv6="$(ip6tables --list OUTPUT --line-number | tail -1 | cut -d' ' -f1)"
279+
''}
280+
276281
if ${startScript}; then
277282
ip46tables -D INPUT -j nixos-drop 2>/dev/null || true
283+
284+
# Do not clear OUTPUT rules to avoid dropping connections,
285+
# instead drop the old rules after reload. Rules are dropped
286+
# in reverse order in order to preserve fallthrough behaviour
287+
# while rules are being dropped.
288+
289+
if [[ "$N_OUTPUT_RULES" != "num" ]]; then # Catch case when chain is empty
290+
for i in $(seq $N_OUTPUT_RULES -1 1); do
291+
iptables -D OUTPUT $i
292+
done
293+
fi
294+
${lib.optionalString config.networking.enableIPv6 ''
295+
if [[ "$N_OUTPUT_RULES_IPv6" != "num" ]]; then # Catch case when chain is empty
296+
for i in $(seq $N_OUTPUT_RULES_IPv6 -1 1); do
297+
ip6tables -D OUTPUT $i
298+
done
299+
fi
300+
''}
278301
else
279302
echo "Failed to reload firewall... Stopping"
280303
${stopScript}
@@ -347,7 +370,7 @@ in
347370
];
348371
conflicts = [ "shutdown.target" ];
349372

350-
path = [ cfg.package ] ++ cfg.extraPackages;
373+
path = [ pkgs.coreutils cfg.package ] ++ cfg.extraPackages;
351374

352375
# FIXME: this module may also try to load kernel modules, but
353376
# containers don't have CAP_SYS_MODULE. So the host system had

0 commit comments

Comments
 (0)