-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirewall.nix
76 lines (65 loc) · 2.29 KB
/
firewall.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
{ config, lib, ... }:
let
cfg = config.router;
heCfg = cfg.heTunnelBroker;
wan6IsHurricaneElectric = heCfg.enable;
devWAN = config.systemd.network.networks."10-wan".name;
devWAN6 = if wan6IsHurricaneElectric then heCfg.name else devWAN;
bogonNetworks = lib.mapAttrs (_: routes: map (route: route.Destination) routes) (
builtins.partition (
route: (builtins.match "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(/[0-9]+)?" route.Destination) != null
) config.systemd.network.networks."10-wan".routes
);
v4BogonNetworks = lib.concatStringsSep ", " bogonNetworks.right;
v6BogonNetworks = lib.concatStringsSep ", " bogonNetworks.wrong;
bogonInputRules = ''
iifname { ${devWAN} } ip saddr { ${v4BogonNetworks} } drop
iifname { ${devWAN6} } ip6 saddr { ${v6BogonNetworks} } drop
'';
bogonOutputRules = ''
oifname { ${devWAN} } ip daddr { ${v4BogonNetworks} } drop
oifname { ${devWAN6} } ip6 daddr { ${v6BogonNetworks} } drop
'';
in
{
config = lib.mkIf cfg.enable {
boot.kernel.sysctl = {
"net.ipv4.conf.all.forwarding" = 1;
"net.ipv6.conf.all.forwarding" = 1;
};
networking.jool = lib.mkIf cfg.ipv6Only {
enable = true;
nat64.default.global.pool6 = "64:ff9b::/96";
};
networking.nftables.enable = true;
networking.firewall = {
enable = true;
filterForward = true;
interfaces.${config.systemd.network.networks."10-lan".name} = {
allowedUDPPorts = [
53 # dns
67 # dhcpv4
5353 # mdns
];
allowedTCPPorts = [
53 # dns
];
};
extraInputRules = lib.mkIf config.router.blockBogonNetworks bogonInputRules;
extraForwardRules =
lib.optionalString config.router.blockBogonNetworks ''
${bogonInputRules}
${bogonOutputRules}
''
+ ''
iifname { ${config.systemd.network.networks."10-lan".name} } accept
${lib.optionalString wan6IsHurricaneElectric ''
# The nixpkgs NAT module sets up forward rules for one external
# interface. Make sure it is setup here for a hurricane electric
# tunnel interface.
iifname { ${lib.concatStringsSep ", " config.networking.nat.internalInterfaces} } oifname ${devWAN6} accept
''}
'';
};
};
}