blob: e2fdcbc31284e089c9c574ca806fdbeb7c2037e9 (
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
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
|
{ config, lib, pkgs, ... }: let {
cfg = config.tv.iptables;
body = {
options.tv.iptables = api;
config = lib.mkIf cfg.enable imp;
};
extraTypes = {
rules = lib.types.submodule {
options = {
nat.OUTPUT = lib.mkOption {
type = with lib.types; listOf str;
default = [];
};
nat.PREROUTING = lib.mkOption {
type = with lib.types; listOf str;
default = [];
};
nat.POSTROUTING = lib.mkOption {
type = with lib.types; listOf str;
default = [];
};
filter.FORWARD = lib.mkOption {
type = with lib.types; listOf str;
default = [];
};
filter.INPUT = lib.mkOption {
type = with lib.types; listOf str;
default = [];
};
filter.Retiolum = lib.mkOption {
type = with lib.types; listOf str;
default = [];
};
filter.Wiregrill = lib.mkOption {
type = with lib.types; listOf str;
default = [];
};
};
};
};
api = {
enable = lib.mkEnableOption "tv.iptables";
accept-echo-request = lib.mkOption {
type = with lib.types; nullOr (enum ["internet" "retiolum"]);
default = "retiolum";
};
input-internet-accept-tcp = lib.mkOption {
type = with lib.types; listOf (either int str);
default = [];
};
input-internet-accept-udp = lib.mkOption {
type = with lib.types; listOf (either int str);
default = [];
};
input-retiolum-accept-tcp = lib.mkOption {
type = with lib.types; listOf (either int str);
default = [];
};
input-retiolum-accept-udp = lib.mkOption {
type = with lib.types; listOf (either int str);
default = [];
};
input-wiregrill-accept-tcp = lib.mkOption {
type = with lib.types; listOf (either int str);
default = [];
};
input-wiregrill-accept-udp = lib.mkOption {
type = with lib.types; listOf (either int str);
default = [];
};
extra = lib.mkOption {
default = {};
type = extraTypes.rules;
};
extra4 = lib.mkOption {
default = {};
type = extraTypes.rules;
};
extra6 = lib.mkOption {
default = {};
type = extraTypes.rules;
};
};
imp = {
networking.firewall.enable = false;
systemd.services.tv-iptables = {
wantedBy = [ "sysinit.target" ];
wants = [ "network-pre.target" ];
before = [ "network-pre.target" ];
after = [ "systemd-modules-load.service" ];
path = with pkgs; [
iptables
];
restartIfChanged = true;
serviceConfig = {
Type = "simple";
RemainAfterExit = true;
Restart = "always";
SyslogIdentifier = "tv-iptables_start";
ExecStart = pkgs.writeDash "tv-iptables_start" ''
set -euf
iptables-restore < ${rules 4}
ip6tables-restore < ${rules 6}
'';
};
unitConfig.DefaultDependencies = false;
};
};
formatTable = table:
(lib.concatStringsSep "\n"
(lib.mapAttrsToList
(chain: lib.concatMapStringsSep "\n" (rule: "-A ${chain} ${rule}"))
table));
rules = iptables-version: let
accept-echo-request = {
ip4tables = "-p icmp -m icmp --icmp-type echo-request -j ACCEPT";
ip6tables = "-p ipv6-icmp -m icmp6 --icmpv6-type echo-request -j ACCEPT";
}."ip${toString iptables-version}tables";
accept-tcp = port: "-p tcp -m tcp --dport ${port} -j ACCEPT";
accept-udp = port: "-p udp -m udp --dport ${port} -j ACCEPT";
in
pkgs.writeText "tv-iptables-rules${toString iptables-version}" ''
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
${formatTable cfg."extra${toString iptables-version}".nat}
${formatTable cfg.extra.nat}
COMMIT
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
:Retiolum - [0:0]
:Wiregrill - [0:0]
${lib.concatMapStringsSep "\n" (rule: "-A INPUT ${rule}") ([]
++ [
"-m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT"
"-i lo -j ACCEPT"
]
++ lib.optional (cfg.accept-echo-request == "internet") accept-echo-request
++ map accept-tcp (lib.unique (map toString cfg.input-internet-accept-tcp))
++ map accept-udp (lib.unique (map toString cfg.input-internet-accept-udp))
++ ["-i retiolum -j Retiolum"]
++ ["-i wiregrill -j Wiregrill"]
)}
${formatTable cfg.extra.filter}
${formatTable cfg."extra${toString iptables-version}".filter}
${lib.concatMapStringsSep "\n" (rule: "-A Retiolum ${rule}") ([]
++ lib.optional (cfg.accept-echo-request == "retiolum") accept-echo-request
++ map accept-tcp (lib.unique (map toString cfg.input-retiolum-accept-tcp))
++ map accept-udp (lib.unique (map toString cfg.input-retiolum-accept-udp))
++ {
ip4tables = [
"-p tcp -j REJECT --reject-with tcp-reset"
"-p udp -j REJECT --reject-with icmp-port-unreachable"
"-j REJECT --reject-with icmp-proto-unreachable"
];
ip6tables = [
"-p tcp -j REJECT --reject-with tcp-reset"
"-p udp -j REJECT --reject-with icmp6-port-unreachable"
"-j REJECT"
];
}."ip${toString iptables-version}tables"
)}
${lib.concatMapStringsSep "\n" (rule: "-A Wiregrill ${rule}") ([]
++ lib.optional (cfg.accept-echo-request == "wiregrill") accept-echo-request
++ map accept-tcp (lib.unique (map toString cfg.input-wiregrill-accept-tcp))
++ map accept-udp (lib.unique (map toString cfg.input-wiregrill-accept-udp))
++ {
ip4tables = [
"-p tcp -j REJECT --reject-with tcp-reset"
"-p udp -j REJECT --reject-with icmp-port-unreachable"
"-j REJECT --reject-with icmp-proto-unreachable"
];
ip6tables = [
"-p tcp -j REJECT --reject-with tcp-reset"
"-p udp -j REJECT --reject-with icmp6-port-unreachable"
"-j REJECT"
];
}."ip${toString iptables-version}tables"
)}
COMMIT
'';
}
|