2011-03-10 12:08:39 +00:00
|
|
|
# This module enables Network Address Translation (NAT).
|
2012-10-06 06:11:57 +01:00
|
|
|
# XXX: todo: support multiple upstream links
|
|
|
|
# see http://yesican.chsoft.biz/lartc/MultihomedLinuxNetworking.html
|
2011-03-10 12:08:39 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2011-03-10 12:08:39 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2011-03-10 12:08:39 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.networking.nat;
|
|
|
|
|
2015-02-06 22:55:51 +00:00
|
|
|
mkDest = externalIP: if externalIP == null
|
|
|
|
then "-j MASQUERADE"
|
|
|
|
else "-j SNAT --to-source ${externalIP}";
|
|
|
|
dest = mkDest cfg.externalIP;
|
|
|
|
destIPv6 = mkDest cfg.externalIPv6;
|
|
|
|
|
|
|
|
# Whether given IP (plus optional port) is an IPv6.
|
|
|
|
isIPv6 = ip: builtins.length (lib.splitString ":" ip) > 2;
|
2014-04-10 13:23:38 +01:00
|
|
|
|
2019-12-15 04:10:17 +00:00
|
|
|
helpers = import ./helpers.nix { inherit config lib; };
|
|
|
|
|
2014-09-16 03:03:47 +01:00
|
|
|
flushNat = ''
|
2019-12-15 04:10:17 +00:00
|
|
|
${helpers}
|
2019-09-10 22:58:19 +01:00
|
|
|
ip46tables -w -t nat -D PREROUTING -j nixos-nat-pre 2>/dev/null|| true
|
|
|
|
ip46tables -w -t nat -F nixos-nat-pre 2>/dev/null || true
|
|
|
|
ip46tables -w -t nat -X nixos-nat-pre 2>/dev/null || true
|
|
|
|
ip46tables -w -t nat -D POSTROUTING -j nixos-nat-post 2>/dev/null || true
|
|
|
|
ip46tables -w -t nat -F nixos-nat-post 2>/dev/null || true
|
|
|
|
ip46tables -w -t nat -X nixos-nat-post 2>/dev/null || true
|
|
|
|
ip46tables -w -t nat -D OUTPUT -j nixos-nat-out 2>/dev/null || true
|
|
|
|
ip46tables -w -t nat -F nixos-nat-out 2>/dev/null || true
|
|
|
|
ip46tables -w -t nat -X nixos-nat-out 2>/dev/null || true
|
2017-12-06 16:17:38 +00:00
|
|
|
|
|
|
|
${cfg.extraStopCommands}
|
2014-09-16 03:03:47 +01:00
|
|
|
'';
|
|
|
|
|
2015-02-06 22:55:51 +00:00
|
|
|
mkSetupNat = { iptables, dest, internalIPs, forwardPorts }: ''
|
2014-09-16 03:03:47 +01:00
|
|
|
# We can't match on incoming interface in POSTROUTING, so
|
2019-11-04 03:20:49 +00:00
|
|
|
# mark packets coming from the internal interfaces.
|
2014-09-16 03:03:47 +01:00
|
|
|
${concatMapStrings (iface: ''
|
2015-02-06 22:55:51 +00:00
|
|
|
${iptables} -w -t nat -A nixos-nat-pre \
|
2014-09-16 03:03:47 +01:00
|
|
|
-i '${iface}' -j MARK --set-mark 1
|
|
|
|
'') cfg.internalInterfaces}
|
|
|
|
|
|
|
|
# NAT the marked packets.
|
|
|
|
${optionalString (cfg.internalInterfaces != []) ''
|
2015-02-06 22:55:51 +00:00
|
|
|
${iptables} -w -t nat -A nixos-nat-post -m mark --mark 1 \
|
2018-06-10 17:29:32 +01:00
|
|
|
${optionalString (cfg.externalInterface != null) "-o ${cfg.externalInterface}"} ${dest}
|
2014-09-16 03:03:47 +01:00
|
|
|
''}
|
|
|
|
|
|
|
|
# NAT packets coming from the internal IPs.
|
|
|
|
${concatMapStrings (range: ''
|
2015-02-06 22:55:51 +00:00
|
|
|
${iptables} -w -t nat -A nixos-nat-post \
|
2018-06-10 17:29:32 +01:00
|
|
|
-s '${range}' ${optionalString (cfg.externalInterface != null) "-o ${cfg.externalInterface}"} ${dest}
|
2015-02-06 22:55:51 +00:00
|
|
|
'') internalIPs}
|
2014-09-16 03:03:47 +01:00
|
|
|
|
|
|
|
# NAT from external ports to internal ports.
|
|
|
|
${concatMapStrings (fwd: ''
|
2015-02-06 22:55:51 +00:00
|
|
|
${iptables} -w -t nat -A nixos-nat-pre \
|
2018-06-12 14:14:15 +01:00
|
|
|
-i ${toString cfg.externalInterface} -p ${fwd.proto} \
|
2014-09-16 03:03:47 +01:00
|
|
|
--dport ${builtins.toString fwd.sourcePort} \
|
|
|
|
-j DNAT --to-destination ${fwd.destination}
|
2018-02-19 01:29:51 +00:00
|
|
|
|
|
|
|
${concatMapStrings (loopbackip:
|
|
|
|
let
|
2015-02-06 22:55:51 +00:00
|
|
|
matchIP = if isIPv6 fwd.destination then "[[]([0-9a-fA-F:]+)[]]" else "([0-9.]+)";
|
|
|
|
m = builtins.match "${matchIP}:([0-9-]+)" fwd.destination;
|
|
|
|
destinationIP = if m == null then throw "bad ip:ports `${fwd.destination}'" else elemAt m 0;
|
|
|
|
destinationPorts = if m == null then throw "bad ip:ports `${fwd.destination}'" else builtins.replaceStrings ["-"] [":"] (elemAt m 1);
|
2018-02-19 01:29:51 +00:00
|
|
|
in ''
|
|
|
|
# Allow connections to ${loopbackip}:${toString fwd.sourcePort} from the host itself
|
2015-02-06 22:55:51 +00:00
|
|
|
${iptables} -w -t nat -A nixos-nat-out \
|
2018-02-19 01:29:51 +00:00
|
|
|
-d ${loopbackip} -p ${fwd.proto} \
|
|
|
|
--dport ${builtins.toString fwd.sourcePort} \
|
|
|
|
-j DNAT --to-destination ${fwd.destination}
|
|
|
|
|
|
|
|
# Allow connections to ${loopbackip}:${toString fwd.sourcePort} from other hosts behind NAT
|
2015-02-06 22:55:51 +00:00
|
|
|
${iptables} -w -t nat -A nixos-nat-pre \
|
2018-02-19 01:29:51 +00:00
|
|
|
-d ${loopbackip} -p ${fwd.proto} \
|
|
|
|
--dport ${builtins.toString fwd.sourcePort} \
|
|
|
|
-j DNAT --to-destination ${fwd.destination}
|
|
|
|
|
2015-02-06 22:55:51 +00:00
|
|
|
${iptables} -w -t nat -A nixos-nat-post \
|
2018-02-19 01:29:51 +00:00
|
|
|
-d ${destinationIP} -p ${fwd.proto} \
|
|
|
|
--dport ${destinationPorts} \
|
|
|
|
-j SNAT --to-source ${loopbackip}
|
|
|
|
'') fwd.loopbackIPs}
|
2015-02-06 22:55:51 +00:00
|
|
|
'') forwardPorts}
|
|
|
|
'';
|
|
|
|
|
|
|
|
setupNat = ''
|
|
|
|
${helpers}
|
|
|
|
# Create subchains where we store rules
|
|
|
|
ip46tables -w -t nat -N nixos-nat-pre
|
|
|
|
ip46tables -w -t nat -N nixos-nat-post
|
|
|
|
ip46tables -w -t nat -N nixos-nat-out
|
|
|
|
|
|
|
|
${mkSetupNat {
|
|
|
|
iptables = "iptables";
|
|
|
|
inherit dest;
|
|
|
|
inherit (cfg) internalIPs;
|
|
|
|
forwardPorts = filter (x: !(isIPv6 x.destination)) cfg.forwardPorts;
|
|
|
|
}}
|
|
|
|
|
|
|
|
${optionalString cfg.enableIPv6 (mkSetupNat {
|
|
|
|
iptables = "ip6tables";
|
|
|
|
dest = destIPv6;
|
|
|
|
internalIPs = cfg.internalIPv6s;
|
|
|
|
forwardPorts = filter (x: isIPv6 x.destination) cfg.forwardPorts;
|
|
|
|
})}
|
2014-09-17 00:56:08 +01:00
|
|
|
|
2017-12-04 09:21:58 +00:00
|
|
|
${optionalString (cfg.dmzHost != null) ''
|
|
|
|
iptables -w -t nat -A nixos-nat-pre \
|
2018-06-12 14:14:15 +01:00
|
|
|
-i ${toString cfg.externalInterface} -j DNAT \
|
2018-02-19 01:29:51 +00:00
|
|
|
--to-destination ${cfg.dmzHost}
|
2017-12-04 09:21:58 +00:00
|
|
|
''}
|
|
|
|
|
2017-12-06 16:17:38 +00:00
|
|
|
${cfg.extraCommands}
|
|
|
|
|
2014-09-17 00:56:08 +01:00
|
|
|
# Append our chains to the nat tables
|
2019-09-10 22:58:19 +01:00
|
|
|
ip46tables -w -t nat -A PREROUTING -j nixos-nat-pre
|
|
|
|
ip46tables -w -t nat -A POSTROUTING -j nixos-nat-post
|
|
|
|
ip46tables -w -t nat -A OUTPUT -j nixos-nat-out
|
2014-09-16 03:03:47 +01:00
|
|
|
'';
|
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
networking.nat.enable = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.bool;
|
2011-03-10 12:08:39 +00:00
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Whether to enable Network Address Translation (NAT).
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2015-02-06 22:55:51 +00:00
|
|
|
networking.nat.enableIPv6 = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Whether to enable IPv6 NAT.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-04-10 13:23:38 +01:00
|
|
|
networking.nat.internalInterfaces = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "eth0" ];
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The interfaces for which to perform NAT. Packets coming from
|
|
|
|
these interface and destined for the external interface will
|
|
|
|
be rewritten.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
networking.nat.internalIPs = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.listOf types.str;
|
2014-04-10 13:23:38 +01:00
|
|
|
default = [];
|
|
|
|
example = [ "192.168.1.0/24" ];
|
2011-03-10 12:08:39 +00:00
|
|
|
description =
|
|
|
|
''
|
2012-10-06 06:11:57 +01:00
|
|
|
The IP address ranges for which to perform NAT. Packets
|
2014-04-10 13:23:38 +01:00
|
|
|
coming from these addresses (on any interface) and destined
|
|
|
|
for the external interface will be rewritten.
|
2011-03-10 12:08:39 +00:00
|
|
|
'';
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2015-02-06 22:55:51 +00:00
|
|
|
networking.nat.internalIPv6s = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "fc00::/64" ];
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The IPv6 address ranges for which to perform NAT. Packets
|
|
|
|
coming from these addresses (on any interface) and destined
|
|
|
|
for the external interface will be rewritten.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
networking.nat.externalInterface = mkOption {
|
2018-06-10 17:29:32 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2011-03-10 12:08:39 +00:00
|
|
|
example = "eth1";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The name of the external network interface.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
networking.nat.externalIP = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2011-03-10 12:08:39 +00:00
|
|
|
example = "203.0.113.123";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The public IP address to which packets from the local
|
|
|
|
network are to be rewritten. If this is left empty, the
|
|
|
|
IP address associated with the external interface will be
|
|
|
|
used.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2015-02-06 22:55:51 +00:00
|
|
|
networking.nat.externalIPv6 = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "2001:dc0:2001:11::175";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The public IPv6 address to which packets from the local
|
|
|
|
network are to be rewritten. If this is left empty, the
|
|
|
|
IP address associated with the external interface will be
|
|
|
|
used.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-09-01 21:20:59 +01:00
|
|
|
networking.nat.forwardPorts = mkOption {
|
2016-09-11 10:05:05 +01:00
|
|
|
type = with types; listOf (submodule {
|
|
|
|
options = {
|
|
|
|
sourcePort = mkOption {
|
2017-11-30 18:36:48 +00:00
|
|
|
type = types.either types.int (types.strMatching "[[:digit:]]+:[[:digit:]]+");
|
2016-09-11 10:05:05 +01:00
|
|
|
example = 8080;
|
2017-11-30 18:36:48 +00:00
|
|
|
description = "Source port of the external interface; to specify a port range, use a string with a colon (e.g. \"60000:61000\")";
|
2016-09-11 10:05:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
destination = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "10.0.0.1:80";
|
2015-02-06 22:55:51 +00:00
|
|
|
description = "Forward connection to destination ip:port (or [ipv6]:port); to specify a port range, use ip:start-end";
|
2017-08-04 15:25:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
proto = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "tcp";
|
|
|
|
example = "udp";
|
|
|
|
description = "Protocol of forwarded connection";
|
2016-09-11 10:05:05 +01:00
|
|
|
};
|
2018-02-19 01:29:51 +00:00
|
|
|
|
|
|
|
loopbackIPs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2021-10-03 17:06:03 +01:00
|
|
|
example = literalExpression ''[ "55.1.2.3" ]'';
|
2018-02-19 01:29:51 +00:00
|
|
|
description = "Public IPs for NAT reflection; for connections to `loopbackip:sourcePort' from the host itself and from other hosts behind NAT";
|
|
|
|
};
|
2016-09-11 10:05:05 +01:00
|
|
|
};
|
|
|
|
});
|
2014-09-01 21:20:59 +01:00
|
|
|
default = [];
|
2015-02-06 22:55:51 +00:00
|
|
|
example = [
|
|
|
|
{ sourcePort = 8080; destination = "10.0.0.1:80"; proto = "tcp"; }
|
|
|
|
{ sourcePort = 8080; destination = "[fc00::2]:80"; proto = "tcp"; }
|
|
|
|
];
|
2014-09-01 21:20:59 +01:00
|
|
|
description =
|
|
|
|
''
|
|
|
|
List of forwarded ports from the external interface to
|
2015-02-06 22:55:51 +00:00
|
|
|
internal destinations by using DNAT. Destination can be
|
|
|
|
IPv6 if IPv6 NAT is enabled.
|
2014-09-01 21:20:59 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-12-04 09:21:58 +00:00
|
|
|
networking.nat.dmzHost = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "10.0.0.1";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The local IP address to which all traffic that does not match any
|
|
|
|
forwarding rule is forwarded.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-12-06 16:17:38 +00:00
|
|
|
networking.nat.extraCommands = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = "iptables -A INPUT -p icmp -j ACCEPT";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Additional shell commands executed as part of the nat
|
|
|
|
initialisation script.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.nat.extraStopCommands = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = "iptables -D INPUT -p icmp -j ACCEPT || true";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Additional shell commands executed as part of the nat
|
|
|
|
teardown script.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2017-08-03 18:41:07 +01:00
|
|
|
config = mkMerge [
|
|
|
|
{ networking.firewall.extraCommands = mkBefore flushNat; }
|
|
|
|
(mkIf config.networking.nat.enable {
|
2011-03-10 12:08:39 +00:00
|
|
|
|
2018-06-10 17:29:32 +01:00
|
|
|
assertions = [
|
2015-02-06 22:55:51 +00:00
|
|
|
{ assertion = cfg.enableIPv6 -> config.networking.enableIPv6;
|
|
|
|
message = "networking.nat.enableIPv6 requires networking.enableIPv6";
|
|
|
|
}
|
2018-06-10 17:29:32 +01:00
|
|
|
{ assertion = (cfg.dmzHost != null) -> (cfg.externalInterface != null);
|
|
|
|
message = "networking.nat.dmzHost requires networking.nat.externalInterface";
|
|
|
|
}
|
|
|
|
{ assertion = (cfg.forwardPorts != []) -> (cfg.externalInterface != null);
|
|
|
|
message = "networking.nat.forwardPorts requires networking.nat.externalInterface";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2017-08-03 18:41:07 +01:00
|
|
|
environment.systemPackages = [ pkgs.iptables ];
|
2011-03-10 12:08:39 +00:00
|
|
|
|
2017-08-03 18:41:07 +01:00
|
|
|
boot = {
|
|
|
|
kernelModules = [ "nf_nat_ftp" ];
|
|
|
|
kernel.sysctl = {
|
|
|
|
"net.ipv4.conf.all.forwarding" = mkOverride 99 true;
|
|
|
|
"net.ipv4.conf.default.forwarding" = mkOverride 99 true;
|
2015-02-06 22:55:51 +00:00
|
|
|
} // optionalAttrs cfg.enableIPv6 {
|
|
|
|
# Do not prevent IPv6 autoconfiguration.
|
|
|
|
# See <http://strugglers.net/~andy/blog/2011/09/04/linux-ipv6-router-advertisements-and-forwarding/>.
|
|
|
|
"net.ipv6.conf.all.accept_ra" = mkOverride 99 2;
|
|
|
|
"net.ipv6.conf.default.accept_ra" = mkOverride 99 2;
|
|
|
|
|
|
|
|
# Forward IPv6 packets.
|
|
|
|
"net.ipv6.conf.all.forwarding" = mkOverride 99 true;
|
|
|
|
"net.ipv6.conf.default.forwarding" = mkOverride 99 true;
|
2017-08-03 18:41:07 +01:00
|
|
|
};
|
2011-03-10 12:08:39 +00:00
|
|
|
};
|
2014-09-16 03:03:47 +01:00
|
|
|
|
2017-08-03 18:41:07 +01:00
|
|
|
networking.firewall = mkIf config.networking.firewall.enable {
|
|
|
|
extraCommands = setupNat;
|
|
|
|
extraStopCommands = flushNat;
|
|
|
|
};
|
2014-09-16 03:03:47 +01:00
|
|
|
|
2017-08-03 18:41:07 +01:00
|
|
|
systemd.services = mkIf (!config.networking.firewall.enable) { nat = {
|
|
|
|
description = "Network Address Translation";
|
|
|
|
wantedBy = [ "network.target" ];
|
|
|
|
after = [ "network-pre.target" "systemd-modules-load.service" ];
|
|
|
|
path = [ pkgs.iptables ];
|
|
|
|
unitConfig.ConditionCapability = "CAP_NET_ADMIN";
|
2014-09-16 03:03:47 +01:00
|
|
|
|
2017-08-03 18:41:07 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
};
|
2014-09-16 03:03:47 +01:00
|
|
|
|
2017-08-03 18:41:07 +01:00
|
|
|
script = flushNat + setupNat;
|
2014-09-16 03:03:47 +01:00
|
|
|
|
2017-08-03 18:41:07 +01:00
|
|
|
postStop = flushNat;
|
|
|
|
}; };
|
|
|
|
})
|
|
|
|
];
|
2011-03-10 12:08:39 +00:00
|
|
|
}
|