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
{
povSelf,
lib,
config,
pkgs,
...
}:
let
inherit (lib) types;
cfg = lib.getAttrFromPath povSelf config;
in
{
options = {
enable = {
type = types.bool;
default = false;
};
asn = {
type = types.int;
};
routerId = {
type = types.int;
};
address = {
type = types.str;
};
range = {
type = types.str;
};
peerings = {
default = { };
type =
with types;
attrsOf (submodule {
options = {
asn = lib.mkOption { type = types.int; };
remoteLinkLocalAddress = lib.mkOption { type = types.str; };
localLinkLocalAddress = lib.mkOption {
type = types.str;
default = "fe80::6b61/64";
};
endpoint = lib.mkOption {
type = with types; nullOr str;
default = null;
};
listenPort = lib.mkOption { type = types.int; };
publicKey = lib.mkOption { type = types.str; };
hasPresharedKey = lib.mkOption {
type = types.bool;
default = false;
};
};
});
};
};
config = lib.mkIf cfg.enable {
networking.firewall.allowedUDPPorts = lib.mapAttrsToList (
name: peerConfig: peerConfig.listenPort
) cfg.peerings;
systemd.services.systemd-networkd.after = [ "sops-install-secrets.service" ];
sops.secrets = (
cfg.peerings
|> lib.mapAttrsToList (
name: peerConfig:
[
(lib.nameValuePair "dn42/peerings/${name}/wgPrivateKey" {
owner = "systemd-network";
group = "systemd-network";
})
]
++ lib.optionals peerConfig.hasPresharedKey [
(lib.nameValuePair "dn42/peerings/${name}/wgPresharedKey" {
owner = "systemd-network";
group = "systemd-network";
})
]
)
|> lib.lists.flatten
|> lib.listToAttrs
);
systemd.network = {
netdevs = lib.mapAttrs' (
name: peerConfig:
lib.nameValuePair "20-dn42${name}" {
netdevConfig = {
Kind = "wireguard";
Name = "dn42${name}";
};
wireguardConfig =
{
ListenPort = peerConfig.listenPort;
PrivateKeyFile = config.sops.secrets."dn42/peerings/${name}/wgPrivateKey".path;
}
// (lib.optionalAttrs peerConfig.hasPresharedKey {
PresharedKeyFile = config.sops.secrets."dn42/peerings/${name}/wgPresharedKey".path;
});
wireguardPeers = [
{
PersistentKeepalive = 30;
Endpoint = lib.mkIf (!builtins.isNull peerConfig.endpoint) peerConfig.endpoint;
PublicKey = peerConfig.publicKey;
AllowedIPs = [
"fd00::/8"
peerConfig.remoteLinkLocalAddress
];
}
];
}
) cfg.peerings;
networks = lib.mapAttrs' (
name: peerConfig:
lib.nameValuePair "20-dn42${name}" {
matchConfig.Name = "dn42${name}";
linkConfig.RequiredForOnline = "no";
address = [ peerConfig.localLinkLocalAddress ];
routes = [ { Destination = "${peerConfig.remoteLinkLocalAddress}/128"; } ];
networkConfig = {
IPv6Forwarding = true;
IPv6AcceptRA = false;
DHCP = false;
};
}
) cfg.peerings;
};
systemd.services.stayrtr = {
wantedBy = [
"multi-user.target"
"bird.service"
];
serviceConfig.DynamicUser = true;
serviceConfig.ExecStart = ''
${lib.getExe pkgs.stayrtr} \
-bind [::1]:8282 \
-cache=https://dn42.burble.com/roa/dn42_roa_46.json \
-checktime=false
'';
};
services.bird = {
enable = true;
package = pkgs.bird3;
preCheckConfig = ''
# Remove roa files for checking, because they are only available at runtime
sed -i 's|include "/etc/bird/roa_dn42.conf";||' bird.conf
cat -n bird.conf # here for debugging purposes
'';
config =
''
log syslog { debug, trace, info, remote, warning, error, auth, fatal, bug };
log stderr all;
define OWNAS = ${toString cfg.asn};
define OWNNET = ${cfg.range};
define OWNNETSET = [ ${cfg.range} ];
define OWNIP = ${cfg.address};
router id ${toString cfg.routerId};
hostname "${config.networking.hostName}";
roa6 table dn42_roa;
function is_self_net() -> bool {
return net ~ OWNNETSET;
}
function is_valid_network() -> bool {
return net ~ [
fd00::/8{44,64}
];
}
function import_filter() {
if (net.type != NET_IP6 || ! is_valid_network() || is_self_net()) then
reject;
if (roa_check(dn42_roa, net, bgp_path.last) != ROA_VALID) then {
# Reject when unknown or invalid according to ROA
print "[dn42] ROA check failed for ", net, " ASN ", bgp_path.last;
reject;
}
accept;
}
function export_filter() {
if ( ! is_valid_network() ) then
reject;
if (source !~ [RTS_STATIC, RTS_BGP]) then
reject;
accept;
}
protocol rpki {
roa6 { table dn42_roa; };
remote ::1;
port 8282;
refresh 600;
retry 300;
expire 7200;
}
protocol device {
scan time 10;
}
protocol static {
route OWNNET unreachable;
ipv6 {
import all;
export none;
};
}
protocol kernel {
scan time 20;
ipv6 {
import none;
export filter {
# dont export static routes
if source = RTS_STATIC then reject;
# preferred outgoing source address
krt_prefsrc = OWNIP;
accept;
};
};
}
template bgp dn42_peers {
local as OWNAS;
path metric 1;
advertise hostname on;
enforce first as on;
med metric on;
ipv6 {
import keep filtered;
import limit 9000 action block;
import where import_filter();
next hop self; # advertise this router as next hop
export where export_filter();
};
}
''
+ (lib.concatStringsSep "\n" (
lib.mapAttrsToList (name: peerConfig: ''
protocol bgp ${name} from dn42_peers {
neighbor ${peerConfig.remoteLinkLocalAddress}%dn42${name} as ${toString peerConfig.asn};
enable extended messages;
}
'') cfg.peerings
));
};
};
}