forked from thefloweringash/nixos-nftables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nft-nat.nix
400 lines (345 loc) · 13.1 KB
/
nft-nat.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
{ config, lib, pkgs, ... }:
with lib;
let
inherit (lib) mkIf concatMapStrings optionalString elemAt isInt;
cfg = config.networking.nat;
dest =
if cfg.externalIP == null then
"masquerade"
else
"snat ip to ${cfg.externalIP}";
oifExternal = optionalString (cfg.externalInterface != null)
''oifname "${cfg.externalInterface}"'';
iptablesPortsToNftables = range:
if isInt range then
toString range
else
let m = builtins.match "([0-9]+):([0-9]+)" range;
in if m == null then
range # assume a single port, rely in input validation.
else
"${elemAt m 0}-${elemAt m 1}";
in
{
disabledModules = [
"services/networking/nat.nix"
"services/networking/nat-nftables.nix"
"services/networking/nat-iptables.nix"
];
###### interface
options = {
networking.nat.enable = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc ''
Whether to enable Network Address Translation (NAT).
'';
};
networking.nat.enableIPv6 = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc ''
Whether to enable IPv6 NAT.
'';
};
networking.nat.internalInterfaces = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "eth0" ];
description = lib.mdDoc ''
The interfaces for which to perform NAT. Packets coming from
these interface and destined for the external interface will
be rewritten.
'';
};
networking.nat.internalIPs = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "192.168.1.0/24" ];
description = lib.mdDoc ''
The IP address ranges for which to perform NAT. Packets
coming from these addresses (on any interface) and destined
for the external interface will be rewritten.
'';
};
networking.nat.internalIPv6s = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "fc00::/64" ];
description = lib.mdDoc ''
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.
'';
};
networking.nat.externalInterface = mkOption {
type = types.nullOr types.str;
default = null;
example = "eth1";
description = lib.mdDoc ''
The name of the external network interface.
'';
};
networking.nat.externalIP = mkOption {
type = types.nullOr types.str;
default = null;
example = "203.0.113.123";
description = lib.mdDoc ''
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.
'';
};
networking.nat.externalIPv6 = mkOption {
type = types.nullOr types.str;
default = null;
example = "2001:dc0:2001:11::175";
description = lib.mdDoc ''
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.
'';
};
networking.nat.forwardPorts = mkOption {
type = with types;
listOf (submodule {
options = {
sourcePort = mkOption {
type = types.either types.int
(types.strMatching "[[:digit:]]+:[[:digit:]]+");
example = 8080;
description = lib.mdDoc ''
Source port of the external interface; to specify a port range, use a string with a colon (e.g. "60000:61000")'';
};
destination = mkOption {
type = types.str;
example = "10.0.0.1:80";
description = lib.mdDoc
"Forward connection to destination ip:port (or [ipv6]:port); to specify a port range, use ip:start-end";
};
proto = mkOption {
type = types.str;
default = "tcp";
example = "udp";
description = lib.mdDoc "Protocol of forwarded connection";
};
loopbackIPs = mkOption {
type = types.listOf types.str;
default = [ ];
example = literalExpression ''[ "55.1.2.3" ]'';
description = lib.mdDoc
"Public IPs for NAT reflection; for connections to `loopbackip:sourcePort' from the host itself and from other hosts behind NAT";
};
};
});
default = [ ];
example = [
{
sourcePort = 8080;
destination = "10.0.0.1:80";
proto = "tcp";
}
{
sourcePort = 8080;
destination = "[fc00::2]:80";
proto = "tcp";
}
];
description = lib.mdDoc ''
List of forwarded ports from the external interface to
internal destinations by using DNAT. Destination can be
IPv6 if IPv6 NAT is enabled.
'';
};
networking.nat.dmzHost = mkOption {
type = types.nullOr types.str;
default = null;
example = "10.0.0.1";
description = lib.mdDoc ''
The local IP address to which all traffic that does not match any
forwarding rule is forwarded.
'';
};
networking.nat.mark = mkOption {
type = types.ints.u32;
default = 1;
example = "1";
description = lib.mdDoc ''
The mark used for natting
'';
};
# actually a 32bit but it can overrun into other hooks so
networking.nat.priorityOffset = mkOption {
type = types.ints.s8;
default = 0;
description = ''
An nft priority expression which will be used for the rpfilter base chain
You may use any nft priority expression that's valid in the `input` hook.
https://wiki.nftables.org/wiki-nftables/index.php/Netfilter_hooks#Priority_within_hook
'';
};
networking.nat.extraCommands = mkOption {
type = types.lines;
default = "";
example = "iptables -A INPUT -p icmp -j ACCEPT";
description = lib.mdDoc ''
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 = lib.mdDoc ''
Additional shell commands executed as part of the nat
teardown script.
'';
};
};
config = mkIf config.networking.nat.enable {
assertions = [
{
assertion = cfg.enableIPv6 -> config.networking.enableIPv6;
message = "networking.nat.enableIPv6 requires networking.enableIPv6";
}
{
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";
}
];
boot = {
kernelModules = [ "nf_nat_ftp" ];
kernel.sysctl = {
"net.ipv4.conf.all.forwarding" = mkOverride 99 true;
"net.ipv4.conf.default.forwarding" = mkOverride 99 true;
} // 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;
};
};
# TODO: priority offsets
systemd.services.nat =
let
mark = (toString config.networking.nat.mark);
# startingPriority can be a string or a number
reducePriority = startingPriority: offset:
let
signedStringify = int:
if int > 0 then "+${toString int}"
else toString int;
in
if offset == 0 then toString startingPriority else
if builtins.typeOf startingPriority == "string" then
"${startingPriority}${signedStringify offset}"
else
"${ toString (startingPriority + offset)}";
natStartRules = pkgs.writeText "nixos-nat-start.nft" ''
add table inet nixos-nat
flush table inet nixos-nat
delete table inet nixos-nat
table inet nixos-nat {
chain prerouting { type nat hook prerouting priority ${reducePriority "dstnat" cfg.priorityOffset}; }
chain input { type nat hook input priority ${reducePriority 100 cfg.priorityOffset}; }
chain output { type nat hook output priority ${reducePriority (-100) cfg.priorityOffset}; }
chain postrouting { type nat hook postrouting priority ${reducePriority "srcnat" cfg.priorityOffset}; }
chain nixos-nat-pre {
# We can't match on incoming interface in POSTROUTING, so
# mark packets coming from the internal interfaces.
${
concatMapStrings (iface: ''
iifname "${iface}" counter meta mark set ${mark}
'') cfg.internalInterfaces
}
}
chain nixos-nat-post {
# NAT the marked packets.
${
optionalString (cfg.internalInterfaces != [ ]) ''
${oifExternal} meta mark ${mark} counter ${dest}
''
}
# NAT packets coming from the internal IPs.
${
concatMapStrings (range: ''
${oifExternal} ip saddr ${range} counter ${dest}
'') cfg.internalIPs
}
}
}
# NAT from external ports to internal ports.
${concatMapStrings (fwd:
let nftSourcePort = iptablesPortsToNftables fwd.sourcePort;
in ''
add rule inet nixos-nat nixos-nat-pre iifname "${cfg.externalInterface}" ${fwd.proto} dport ${nftSourcePort} counter dnat ip to ${fwd.destination}
${concatMapStrings (loopbackip:
let
m = builtins.match "([0-9.]+):([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
elemAt m 1;
#FIXME: DNAT to ipv6 is broken
in ''
# Allow connections to ${loopbackip}:${nftSourcePort} from the host itself
add rule inet nixos-nat output ip daddr ${loopbackip} ${fwd.proto} dport ${nftSourcePort} counter dnat ip to ${fwd.destination}
# Allow connections to ${loopbackip}:${nftSourcePort} from other hosts behind NAT
add rule inet nixos-nat nixos-nat-pre ip daddr ${loopbackip} ${fwd.proto} dport ${nftSourcePort} counter dnat ip to ${fwd.destination}
add rule inet nixos-nat nixos-nat-post ip daddr ${destinationIP} ${fwd.proto} dport ${
iptablesPortsToNftables destinationPorts
} counter snat ip to ${loopbackip}
'') fwd.loopbackIPs}
'') cfg.forwardPorts}
${optionalString (cfg.dmzHost != null) ''
add rule inet nixos-nat nixos-nat-pre iifname "${cfg.externalInterface}" counter dnat ip to ${cfg.dmzHost}
''}
# Append our chains to the nat tables
add rule inet nixos-nat prerouting counter jump nixos-nat-pre
add rule inet nixos-nat postrouting counter jump nixos-nat-post
'';
natStopRules = pkgs.writeText "nixos-nat-start.nft" ''
add table inet nixos-nat
flush table inet nixos-nat
delete table inet nixos-nat
'';
in
{
description = "Network Address Translation";
wantedBy = [ "sysinit.target" ];
wants = [ "network-pre.target" ];
before = [ "network-pre.target" ];
after = [ "systemd-modules-load.service" ];
# FIXME: this module may also try to load kernel modules, but
# containers don't have CAP_SYS_MODULE. So the host system had
# better have all necessary modules already loaded.
unitConfig.ConditionCapability = "CAP_NET_ADMIN";
unitConfig.DefaultDependencies = false;
# TODO: make packages configurable
path = [ pkgs.nftables ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script = "nft -f ${natStartRules}";
preStop = "nft -f ${natStopRules}";
};
};
}