Merge pull request #4121 from wkennington/master.nat
nixos/nat: Don't flush tables, create subchains for autogenerated rules
This commit is contained in:
commit
1ff027b304
@ -13,38 +13,49 @@ let
|
|||||||
dest = if cfg.externalIP == null then "-j MASQUERADE" else "-j SNAT --to-source ${cfg.externalIP}";
|
dest = if cfg.externalIP == null then "-j MASQUERADE" else "-j SNAT --to-source ${cfg.externalIP}";
|
||||||
|
|
||||||
flushNat = ''
|
flushNat = ''
|
||||||
iptables -w -t nat -F PREROUTING
|
iptables -w -t nat -D PREROUTING -j nixos-nat-pre 2>/dev/null|| true
|
||||||
iptables -w -t nat -F POSTROUTING
|
iptables -w -t nat -F nixos-nat-pre 2>/dev/null || true
|
||||||
iptables -w -t nat -X
|
iptables -w -t nat -X nixos-nat-pre 2>/dev/null || true
|
||||||
|
iptables -w -t nat -D POSTROUTING -j nixos-nat-post 2>/dev/null || true
|
||||||
|
iptables -w -t nat -F nixos-nat-post 2>/dev/null || true
|
||||||
|
iptables -w -t nat -X nixos-nat-post 2>/dev/null || true
|
||||||
'';
|
'';
|
||||||
|
|
||||||
setupNat = ''
|
setupNat = ''
|
||||||
|
# Create subchain where we store rules
|
||||||
|
iptables -w -t nat -N nixos-nat-pre
|
||||||
|
iptables -w -t nat -N nixos-nat-post
|
||||||
|
|
||||||
# We can't match on incoming interface in POSTROUTING, so
|
# We can't match on incoming interface in POSTROUTING, so
|
||||||
# mark packets coming from the external interfaces.
|
# mark packets coming from the external interfaces.
|
||||||
${concatMapStrings (iface: ''
|
${concatMapStrings (iface: ''
|
||||||
iptables -w -t nat -A PREROUTING \
|
iptables -w -t nat -A nixos-nat-pre \
|
||||||
-i '${iface}' -j MARK --set-mark 1
|
-i '${iface}' -j MARK --set-mark 1
|
||||||
'') cfg.internalInterfaces}
|
'') cfg.internalInterfaces}
|
||||||
|
|
||||||
# NAT the marked packets.
|
# NAT the marked packets.
|
||||||
${optionalString (cfg.internalInterfaces != []) ''
|
${optionalString (cfg.internalInterfaces != []) ''
|
||||||
iptables -w -t nat -A POSTROUTING -m mark --mark 1 \
|
iptables -w -t nat -A nixos-nat-post -m mark --mark 1 \
|
||||||
-o ${cfg.externalInterface} ${dest}
|
-o ${cfg.externalInterface} ${dest}
|
||||||
''}
|
''}
|
||||||
|
|
||||||
# NAT packets coming from the internal IPs.
|
# NAT packets coming from the internal IPs.
|
||||||
${concatMapStrings (range: ''
|
${concatMapStrings (range: ''
|
||||||
iptables -w -t nat -A POSTROUTING \
|
iptables -w -t nat -A nixos-nat-post \
|
||||||
-s '${range}' -o ${cfg.externalInterface} ${dest}
|
-s '${range}' -o ${cfg.externalInterface} ${dest}
|
||||||
'') cfg.internalIPs}
|
'') cfg.internalIPs}
|
||||||
|
|
||||||
# NAT from external ports to internal ports.
|
# NAT from external ports to internal ports.
|
||||||
${concatMapStrings (fwd: ''
|
${concatMapStrings (fwd: ''
|
||||||
iptables -w -t nat -A PREROUTING \
|
iptables -w -t nat -A nixos-nat-pre \
|
||||||
-i ${cfg.externalInterface} -p tcp \
|
-i ${cfg.externalInterface} -p tcp \
|
||||||
--dport ${builtins.toString fwd.sourcePort} \
|
--dport ${builtins.toString fwd.sourcePort} \
|
||||||
-j DNAT --to-destination ${fwd.destination}
|
-j DNAT --to-destination ${fwd.destination}
|
||||||
'') cfg.forwardPorts}
|
'') cfg.forwardPorts}
|
||||||
|
|
||||||
|
# Append our chains to the nat tables
|
||||||
|
iptables -w -t nat -A PREROUTING -j nixos-nat-pre
|
||||||
|
iptables -w -t nat -A POSTROUTING -j nixos-nat-post
|
||||||
'';
|
'';
|
||||||
|
|
||||||
in
|
in
|
||||||
|
@ -61,7 +61,8 @@ in rec {
|
|||||||
(all nixos.tests.kde4)
|
(all nixos.tests.kde4)
|
||||||
(all nixos.tests.login)
|
(all nixos.tests.login)
|
||||||
(all nixos.tests.misc)
|
(all nixos.tests.misc)
|
||||||
(all nixos.tests.nat)
|
(all nixos.tests.nat.firewall)
|
||||||
|
(all nixos.tests.nat.standalone)
|
||||||
(all nixos.tests.nfs3)
|
(all nixos.tests.nfs3)
|
||||||
(all nixos.tests.openssh)
|
(all nixos.tests.openssh)
|
||||||
(all nixos.tests.printing)
|
(all nixos.tests.printing)
|
||||||
|
@ -244,7 +244,8 @@ in rec {
|
|||||||
tests.munin = callTest tests/munin.nix {};
|
tests.munin = callTest tests/munin.nix {};
|
||||||
tests.mysql = callTest tests/mysql.nix {};
|
tests.mysql = callTest tests/mysql.nix {};
|
||||||
tests.mysqlReplication = callTest tests/mysql-replication.nix {};
|
tests.mysqlReplication = callTest tests/mysql-replication.nix {};
|
||||||
tests.nat = callTest tests/nat.nix {};
|
tests.nat.firewall = callTest tests/nat.nix { withFirewall = true; };
|
||||||
|
tests.nat.standalone = callTest tests/nat.nix { withFirewall = false; };
|
||||||
tests.nfs3 = callTest tests/nfs.nix { version = 3; };
|
tests.nfs3 = callTest tests/nfs.nix { version = 3; };
|
||||||
tests.nsd = callTest tests/nsd.nix {};
|
tests.nsd = callTest tests/nsd.nix {};
|
||||||
tests.openssh = callTest tests/openssh.nix {};
|
tests.openssh = callTest tests/openssh.nix {};
|
||||||
|
@ -3,9 +3,12 @@
|
|||||||
# client on the inside network, a server on the outside network, and a
|
# client on the inside network, a server on the outside network, and a
|
||||||
# router connected to both that performs Network Address Translation
|
# router connected to both that performs Network Address Translation
|
||||||
# for the client.
|
# for the client.
|
||||||
|
import ./make-test.nix ({ withFirewall, ... }:
|
||||||
import ./make-test.nix {
|
let
|
||||||
name = "nat";
|
unit = if withFirewall then "firewall" else "nat";
|
||||||
|
in
|
||||||
|
{
|
||||||
|
name = "nat${if withFirewall then "WithFirewall" else "Standalone"}";
|
||||||
|
|
||||||
nodes =
|
nodes =
|
||||||
{ client =
|
{ client =
|
||||||
@ -19,6 +22,7 @@ import ./make-test.nix {
|
|||||||
router =
|
router =
|
||||||
{ config, pkgs, ... }:
|
{ config, pkgs, ... }:
|
||||||
{ virtualisation.vlans = [ 2 1 ];
|
{ virtualisation.vlans = [ 2 1 ];
|
||||||
|
networking.firewall.enable = withFirewall;
|
||||||
networking.firewall.allowPing = true;
|
networking.firewall.allowPing = true;
|
||||||
networking.nat.enable = true;
|
networking.nat.enable = true;
|
||||||
networking.nat.internalIPs = [ "192.168.1.0/24" ];
|
networking.nat.internalIPs = [ "192.168.1.0/24" ];
|
||||||
@ -48,7 +52,7 @@ import ./make-test.nix {
|
|||||||
$router->succeed("curl --fail http://server/ >&2");
|
$router->succeed("curl --fail http://server/ >&2");
|
||||||
|
|
||||||
# The client should be also able to connect via the NAT router.
|
# The client should be also able to connect via the NAT router.
|
||||||
$router->waitForUnit("nat");
|
$router->waitForUnit("${unit}");
|
||||||
$client->waitForUnit("network.target");
|
$client->waitForUnit("network.target");
|
||||||
$client->succeed("curl --fail http://server/ >&2");
|
$client->succeed("curl --fail http://server/ >&2");
|
||||||
$client->succeed("ping -c 1 server >&2");
|
$client->succeed("ping -c 1 server >&2");
|
||||||
@ -66,14 +70,14 @@ import ./make-test.nix {
|
|||||||
$router->succeed("ping -c 1 client >&2");
|
$router->succeed("ping -c 1 client >&2");
|
||||||
|
|
||||||
# If we turn off NAT, the client shouldn't be able to reach the server.
|
# If we turn off NAT, the client shouldn't be able to reach the server.
|
||||||
$router->stopJob("nat");
|
$router->succeed("iptables -t nat -D PREROUTING -j nixos-nat-pre");
|
||||||
|
$router->succeed("iptables -t nat -D POSTROUTING -j nixos-nat-post");
|
||||||
$client->fail("curl --fail --connect-timeout 5 http://server/ >&2");
|
$client->fail("curl --fail --connect-timeout 5 http://server/ >&2");
|
||||||
$client->fail("ping -c 1 server >&2");
|
$client->fail("ping -c 1 server >&2");
|
||||||
|
|
||||||
# And make sure that restarting the NAT job works.
|
# And make sure that reloading the NAT job works.
|
||||||
$router->succeed("systemctl start nat");
|
$router->succeed("systemctl restart ${unit}");
|
||||||
$client->succeed("curl --fail http://server/ >&2");
|
$client->succeed("curl --fail http://server/ >&2");
|
||||||
$client->succeed("ping -c 1 server >&2");
|
$client->succeed("ping -c 1 server >&2");
|
||||||
'';
|
'';
|
||||||
|
})
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user