blob: 575de7d35b8c6c9702b824c88bcefe7c08e91ab0 (
plain)
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
|
{
config,
inventory,
self_name,
lib,
...
}:
let
inherit (lib)
mkOption
types
mkIf
mkEnableOption
mkDefault
;
inv = inventory.${self_name};
net = inv.interfaces;
cfg = config.wilkuu.firewall;
portRangeType = types.addCheck (types.submodule {
options = {
from = mkOption {
type = types.port;
example = 100;
description = "lowest part of the range (inclusive)";
};
to = mkOption {
type = types.port;
example = 200;
description = "highest part of the range (inclusive)";
};
};
}) (range: range.from < range.to);
mkPortsOption =
protocol:
mkOption {
type = types.listOf types.port;
default = [ ];
example = [ 1234 ];
description = "List of ${protocol} ports to accept";
};
mkPortRangesOption =
protocol:
mkOption {
type = types.listOf portRangeType;
default = [ ];
example = [ 1234 ];
description = "List of ${protocol} port ranges to accept";
};
layerAssertions = lib.mapAttrsToList (
layerName: layerConfig:
let
missing = builtins.filter (importLayer: !(cfg.layers ? ${importLayer})) layerConfig.import-layer;
in
{
assertion = missing == [ ];
message = "Layer ${layerName} imports undefined layer(s): ${toString missing}";
}
) cfg.layers;
in
{
options.wilkuu.firewall = {
enable = mkEnableOption "firewall module";
defaultLayer = lib.mkOption {
type = types.str;
default = "external";
example = "eth";
};
layers = mkOption {
default = { };
description = "Layers and which ports should be open";
type = types.attrsOf (
types.submodule {
options = {
import-layer = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "external" ];
description = "Layer names which ports will also be opened on this layer";
};
allowedTCPPorts = mkPortsOption "TCP";
allowedUDPPorts = mkPortsOption "UDP";
allowedTCPPortRanges = mkPortRangesOption "TCP";
allowedUDPPortRanges = mkPortRangesOption "UDP";
};
}
);
};
};
config = mkIf cfg.enable {
assertions = layerAssertions;
networking.nftables.enable = true;
networking.firewall =
let
# TODO: reconsider doing this as this will only do a flat-import anything more complex would require building an import tree.
resolvedLayers = lib.mapAttrs (
_: l1:
lib.foldl' (acc: set2: {
allowedTCPPorts = acc.allowedTCPPorts ++ set2.allowedTCPPorts;
allowedUDPPorts = acc.allowedUDPPorts ++ set2.allowedUDPPorts;
allowedTCPPortRanges = acc.allowedTCPPortRanges ++ set2.allowedTCPPortRanges;
allowedUDPPortRanges = acc.allowedUDPPortRanges ++ set2.allowedUDPPortRanges;
}) l1 (map (lk: cfg.layers.${lk}) l1.import-layer)
) cfg.layers;
interfaces = lib.mapAttrs (_n: interface: {
inherit (resolvedLayers.${interface.layer})
allowedTCPPorts
allowedUDPPorts
allowedTCPPortRanges
allowedUDPPortRanges
;
}) (lib.filterAttrs (_n: interface: !(builtins.elem interface.type [ "roaming" ])) net);
in
{
enable = true;
checkReversePath = mkDefault false;
inherit interfaces;
inherit (resolvedLayers.${cfg.defaultLayer})
allowedTCPPorts
allowedUDPPorts
allowedTCPPortRanges
allowedUDPPortRanges
;
};
};
}
|