1 # This module enables Network Address Translation (NAT).
2 # XXX: todo: support multiple upstream links
3 # see http://yesican.chsoft.biz/lartc/MultihomedLinuxNetworking.html
16 cfg = config.networking.nat;
24 networking.nat.enable = mkOption {
28 Whether to enable Network Address Translation (NAT). A
29 properly configured firewall or a trusted L2 on all network
30 interfaces is required to prevent unauthorized access to
35 networking.nat.enableIPv6 = mkOption {
39 Whether to enable IPv6 NAT.
43 networking.nat.internalInterfaces = mkOption {
44 type = types.listOf types.str;
48 The interfaces for which to perform NAT. Packets coming from
49 these interface and destined for the external interface will
54 networking.nat.internalIPs = mkOption {
55 type = types.listOf types.str;
57 example = [ "192.168.1.0/24" ];
59 The IP address ranges for which to perform NAT. Packets
60 coming from these addresses (on any interface) and destined
61 for the external interface will be rewritten.
65 networking.nat.internalIPv6s = mkOption {
66 type = types.listOf types.str;
68 example = [ "fc00::/64" ];
70 The IPv6 address ranges for which to perform NAT. Packets
71 coming from these addresses (on any interface) and destined
72 for the external interface will be rewritten.
76 networking.nat.externalInterface = mkOption {
77 type = types.nullOr types.str;
81 The name of the external network interface.
85 networking.nat.externalIP = mkOption {
86 type = types.nullOr types.str;
88 example = "203.0.113.123";
90 The public IP address to which packets from the local
91 network are to be rewritten. If this is left empty, the
92 IP address associated with the external interface will be
93 used. Only connections made to this IP address will be
94 forwarded to the internal network when using forwardPorts.
98 networking.nat.externalIPv6 = mkOption {
99 type = types.nullOr types.str;
101 example = "2001:dc0:2001:11::175";
103 The public IPv6 address to which packets from the local
104 network are to be rewritten. If this is left empty, the
105 IP address associated with the external interface will be
106 used. Only connections made to this IP address will be
107 forwarded to the internal network when using forwardPorts.
111 networking.nat.forwardPorts = mkOption {
116 sourcePort = mkOption {
117 type = types.either types.int (types.strMatching "[[:digit:]]+:[[:digit:]]+");
119 description = "Source port of the external interface; to specify a port range, use a string with a colon (e.g. \"60000:61000\")";
122 destination = mkOption {
124 example = "10.0.0.1:80";
125 description = "Forward connection to destination ip:port (or [ipv6]:port); to specify a port range, use ip:start-end";
132 description = "Protocol of forwarded connection";
135 loopbackIPs = mkOption {
136 type = types.listOf types.str;
138 example = literalExpression ''[ "55.1.2.3" ]'';
139 description = "Public IPs for NAT reflection; for connections to `loopbackip:sourcePort` from the host itself and from other hosts behind NAT";
147 destination = "10.0.0.1:80";
152 destination = "[fc00::2]:80";
157 List of forwarded ports from the external interface to
158 internal destinations by using DNAT. Destination can be
159 IPv6 if IPv6 NAT is enabled.
163 networking.nat.dmzHost = mkOption {
164 type = types.nullOr types.str;
166 example = "10.0.0.1";
168 The local IP address to which all traffic that does not match any
169 forwarding rule is forwarded.
175 config = mkIf config.networking.nat.enable {
179 assertion = cfg.enableIPv6 -> config.networking.enableIPv6;
180 message = "networking.nat.enableIPv6 requires networking.enableIPv6";
183 assertion = (cfg.dmzHost != null) -> (cfg.externalInterface != null);
184 message = "networking.nat.dmzHost requires networking.nat.externalInterface";
187 assertion = (cfg.forwardPorts != [ ]) -> (cfg.externalInterface != null);
188 message = "networking.nat.forwardPorts requires networking.nat.externalInterface";
192 # Use the same iptables package as in config.networking.firewall.
193 # When the firewall is enabled, this should be deduplicated without any
195 environment.systemPackages = [ config.networking.firewall.package ];
198 kernelModules = [ "nf_nat_ftp" ];
201 "net.ipv4.conf.all.forwarding" = mkOverride 99 true;
202 "net.ipv4.conf.default.forwarding" = mkOverride 99 true;
204 // optionalAttrs cfg.enableIPv6 {
205 # Do not prevent IPv6 autoconfiguration.
206 # See <http://strugglers.net/~andy/blog/2011/09/04/linux-ipv6-router-advertisements-and-forwarding/>.
207 "net.ipv6.conf.all.accept_ra" = mkOverride 99 2;
208 "net.ipv6.conf.default.accept_ra" = mkOverride 99 2;
210 # Forward IPv6 packets.
211 "net.ipv6.conf.all.forwarding" = mkOverride 99 true;
212 "net.ipv6.conf.default.forwarding" = mkOverride 99 true;