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
|
{ config, lib, ... }: let
cfg = config.hrm.nftables;
# A port is a number, a service name from /etc/services, or a range like
# "60000-61000".
port = lib.types.either lib.types.port lib.types.str;
ruleType = lib.types.submodule ({ name, ... }: {
options = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
};
order = lib.mkOption {
type = lib.types.int;
default = 1000;
description = "Rules are sorted by order, then by name.";
};
text = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "A rule in nft syntax, instead of the fields below.";
};
iifname = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
oifname = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
saddr = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "IPv4 and IPv6 addresses; each family gets its own nft rule.";
};
daddr = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = "IPv4 and IPv6 addresses; each family gets its own nft rule.";
};
ct.state = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
example = [ "established" "related" ];
};
icmp.type = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
};
icmpv6.type = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
};
tcp.dport = lib.mkOption {
type = lib.types.listOf port;
default = [];
};
udp.dport = lib.mkOption {
type = lib.types.listOf port;
default = [];
};
verdict = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "jump reject-all";
description = ''
The statement at the end of the rule, like accept, drop, reject,
masquerade, "jump reject-all" or "redirect to :22".
'';
};
};
});
chainType = lib.types.submodule {
options = {
type = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [ "filter" "nat" "route" ]);
default = null;
description = "Type of a base chain; null for a regular chain.";
};
hook = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "input";
};
priority = lib.mkOption {
type = lib.types.nullOr (lib.types.either lib.types.int lib.types.str);
default = null;
example = "filter";
};
policy = lib.mkOption {
type = lib.types.nullOr (lib.types.enum [ "accept" "drop" ]);
default = null;
};
rules = lib.mkOption {
type = lib.types.attrsOf ruleType;
default = {};
};
};
};
tableType = lib.types.submodule {
options = {
family = lib.mkOption {
type = lib.types.enum [ "ip" "ip6" "inet" "arp" "bridge" "netdev" ];
default = "inet";
};
chains = lib.mkOption {
type = lib.types.attrsOf chainType;
default = {};
};
};
};
zoneType = lib.types.submodule {
options = {
tcp = lib.mkOption {
type = lib.types.listOf port;
default = [];
description = "TCP ports to accept.";
};
udp = lib.mkOption {
type = lib.types.listOf port;
default = [];
description = "UDP ports to accept.";
};
echo-request = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to accept ICMP and ICMPv6 echo requests.";
};
};
};
renderSet = xs: let
xs' = lib.unique (map toString xs);
in
if lib.length xs' == 1
then lib.head xs'
else "{ ${lib.concatStringsSep ", " xs'} }";
isIPv6 = lib.hasInfix ":";
# A rule becomes one nft rule per address family and protocol it needs:
# addresses and ICMP types are family-specific, ports protocol-specific.
renderRule = rule: let
familySpecific =
rule.saddr != [] || rule.daddr != [] ||
rule.icmp.type != [] || rule.icmpv6.type != [];
families =
if familySpecific then [
{ name = "ip"; isMember = x: !isIPv6 x; icmp = [ "icmp" rule.icmp.type ]; }
{ name = "ip6"; isMember = isIPv6; icmp = [ "icmpv6" rule.icmpv6.type ]; }
] else [
null
];
protocols =
lib.optional (rule.tcp.dport != []) [ "tcp" rule.tcp.dport ]
++ lib.optional (rule.udp.dport != []) [ "udp" rule.udp.dport ];
render = family: protocol: let
saddr = lib.filter family.isMember rule.saddr;
daddr = lib.filter family.isMember rule.daddr;
icmp = lib.elemAt family.icmp 1;
hasICMP = rule.icmp.type != [] || rule.icmpv6.type != [];
applies =
family == null || (
(rule.saddr == [] || saddr != []) &&
(rule.daddr == [] || daddr != []) &&
(!hasICMP || icmp != [])
);
in
lib.optional applies (lib.concatStringsSep " " (
lib.optional (rule.iifname != null) "iifname \"${rule.iifname}\""
++ lib.optional (rule.oifname != null) "oifname \"${rule.oifname}\""
++ lib.optional (family != null && saddr != []) "${family.name} saddr ${renderSet saddr}"
++ lib.optional (family != null && daddr != []) "${family.name} daddr ${renderSet daddr}"
++ lib.optional (rule.ct.state != []) "ct state ${renderSet rule.ct.state}"
++ lib.optional (family != null && icmp != []) "${lib.head family.icmp} type ${renderSet icmp}"
++ lib.optional (protocol != null) "${lib.head protocol} dport ${renderSet (lib.elemAt protocol 1)}"
++ lib.optional (rule.verdict != null) rule.verdict
));
in
if rule.text != null
then [ rule.text ]
else lib.concatMap (family:
lib.concatMap (render family) (if protocols == [] then [ null ] else protocols)
) families;
renderChain = name: chain: let
rules =
lib.sort
(a: b: if a.order != b.order then a.order < b.order else a.name < b.name)
(lib.filter (rule: rule.enable)
(lib.mapAttrsToList (name: rule: rule // { inherit name; }) chain.rules));
in
lib.concatStringsSep "\n" (
[ "chain ${name} {" ]
++ lib.optional (chain.hook != null) " ${lib.concatStringsSep " " (
[ "type ${chain.type} hook ${chain.hook} priority ${toString chain.priority};" ]
++ lib.optional (chain.policy != null) "policy ${chain.policy};"
)}"
++ map (line: " ${line}") (lib.concatMap renderRule rules)
++ [ "}" ]
);
# Rules accepting what a zone accepts.
zoneRules = prefix: zone: {
"${prefix}echo-request" = {
enable = zone.echo-request;
icmp.type = [ "echo-request" ];
icmpv6.type = [ "echo-request" ];
verdict = "accept";
};
"${prefix}tcp" = {
enable = zone.tcp != [];
tcp.dport = zone.tcp;
verdict = "accept";
};
"${prefix}udp" = {
enable = zone.udp != [];
udp.dport = zone.udp;
verdict = "accept";
};
};
# Interface zones, i.e. all zones but "internet", get their own chain.
interfaceZones = lib.removeAttrs cfg.input [ "internet" ];
in {
options.hrm.nftables = {
enable = lib.mkEnableOption "hrm.nftables";
tables = lib.mkOption {
type = lib.types.attrsOf tableType;
default = {};
description = ''
nftables tables, rendered to networking.nftables.tables.
'';
};
input = lib.mkOption {
type = lib.types.attrsOf zoneType;
default = {};
example = {
internet.tcp = [ "ssh" ];
retiolum.tcp = [ "http" ];
};
description = ''
What to accept on input. "internet" applies to every interface;
any other name is an interface, whose traffic goes to a chain of the
same name, which rejects what it doesn't accept.
'';
};
};
config = lib.mkIf cfg.enable {
networking.firewall.enable = false;
networking.nftables.enable = true;
networking.nftables.tables =
lib.mapAttrs (_: table: {
inherit (table) family;
content = lib.concatStringsSep "\n" (lib.mapAttrsToList renderChain table.chains);
}) cfg.tables;
hrm.nftables.tables.filter.chains = {
input = {
type = "filter";
hook = "input";
priority = "filter";
policy = "drop";
rules = {
established = {
order = 100;
ct.state = [ "established" "related" ];
verdict = "accept";
};
loopback = {
order = 100;
iifname = "lo";
verdict = "accept";
};
# IPv6 needs neighbor discovery.
neighbor-discovery = {
order = 100;
icmpv6.type = [ "nd-neighbor-solicit" "nd-neighbor-advert" "nd-router-advert" ];
verdict = "accept";
};
}
// zoneRules "internet-" (cfg.input.internet or { tcp = []; udp = []; echo-request = false; })
// lib.mapAttrs' (name: _: lib.nameValuePair "jump-${name}" {
order = 1400;
iifname = name;
verdict = "jump ${name}";
}) interfaceZones;
};
forward = {
type = "filter";
hook = "forward";
priority = "filter";
policy = "drop";
};
reject-all.rules = {
tcp = { order = 100; text = "meta l4proto tcp reject with tcp reset"; };
udp = { order = 200; text = "meta l4proto udp reject with icmpx type port-unreachable"; };
ipv4 = { order = 300; text = "meta nfproto ipv4 reject with icmp type prot-unreachable"; };
other = { order = 400; verdict = "reject"; };
};
} // lib.mapAttrs (_: zone: {
rules = zoneRules "" zone // {
reject = {
order = 2000;
verdict = "jump reject-all";
};
};
}) interfaceZones;
hrm.nftables.tables.nat.chains = {
prerouting = {
type = "nat";
hook = "prerouting";
priority = "dstnat";
};
output = {
type = "nat";
hook = "output";
priority = "dstnat";
};
postrouting = {
type = "nat";
hook = "postrouting";
priority = "srcnat";
};
};
};
}
|