1 { pkgs, lib, config, machines, ... }:
3 inherit (builtins) hasAttr readFile;
4 inherit (pkgs.lib) unlinesAttrs;
5 inherit (config.users) users groups;
8 networking.firewall.enable = false;
9 security.lockKernelModules = false;
10 systemd.services.disable-kernel-module-loading.after = [ "nftables.service" ];
11 systemd.services.nftables.serviceConfig.TimeoutStartSec = "20";
12 networking.nftables = {
14 ruleset = lib.mkBefore ''
22 add @lograte { ip saddr limit rate 1/minute } log level warn prefix "ping-flood: "
26 ip protocol icmp icmp type echo-request limit rate over 10/second burst 20 packets goto ping-flood
27 ip6 nexthdr icmpv6 icmpv6 type echo-request limit rate over 10/second burst 20 packets goto ping-flood
30 add @lograte { ip saddr limit rate 1/minute } log level warn prefix "smurf: "
33 chain check-broadcast {
34 ip saddr 0.0.0.0/32 counter accept comment "DHCP broadcast"
35 fib saddr type broadcast counter goto smurf
36 ip saddr 224.0.0.0/4 counter goto smurf
39 add @lograte { ip saddr limit rate 1/minute } log level warn prefix "bogus-tcp: "
43 add @lograte { ip saddr limit rate 1/minute } log level warn prefix "syn-flood: "
47 tcp flags syn tcp option maxseg size != 536-65535 counter goto bogus-tcp
48 tcp flags & (ack|fin) == fin counter goto bogus-tcp
49 tcp flags & (ack|psh) == psh counter goto bogus-tcp
50 tcp flags & (ack|urg) == urg counter goto bogus-tcp
51 tcp flags & (fin|ack) == fin counter goto bogus-tcp
52 tcp flags & (fin|rst) == (fin|rst) counter goto bogus-tcp
53 tcp flags & (fin|psh|ack) == (fin|psh) counter goto bogus-tcp
54 tcp flags & (syn|fin) == (syn|fin) counter goto bogus-tcp comment "SYN-FIN scan"
55 tcp flags & (syn|rst) == (syn|rst) counter goto bogus-tcp comment "SYN-RST scan"
56 tcp flags == (fin|syn|rst|psh|ack|urg) counter goto bogus-tcp comment "XMAS scan"
57 tcp flags == 0x0 counter goto bogus-tcp comment "NULL scan"
58 tcp flags == (fin|urg|psh) counter goto bogus-tcp
59 tcp flags == (fin|urg|psh|syn) counter goto bogus-tcp comment "NMAP-ID"
60 tcp flags == (fin|urg|syn|rst|ack) counter goto bogus-tcp
62 ct state new tcp flags != syn counter goto bogus-tcp
63 tcp sport 0 tcp flags & (fin|syn|rst|ack) == syn counter goto bogus-tcp
64 tcp flags & (fin|syn|rst|ack) == syn counter limit rate over 30/second burst 60 packets goto syn-flood
67 #ip saddr 224.0.0.0/3 counter drop
68 #ip saddr 169.254.0.0/16 counter drop
69 #ip saddr 172.16.0.0/12 counter drop
70 #ip saddr 192.0.2.0/24 counter drop
71 #ip saddr 192.168.0.0/16 counter drop
72 #ip saddr 10.0.0.0/8 counter drop
73 #ip saddr 0.0.0.0/8 counter drop
74 #ip saddr 240.0.0.0/5 counter drop
75 #ip saddr 127.0.0.0/8 counter drop
78 #udp dport mdns ip6 daddr ff02::fb counter accept comment "Accept mDNS"
79 #udp dport mdns ip daddr 224.0.0.251 counter accept comment "Accept mDNS"
82 #ct state new add @connlimit { ip saddr ct count over 20 } counter tcp reject with tcp reset
84 # Some .nix append rules here with: add rule inet filter net2fw ...
87 ip daddr 224.0.0.0/4 udp dport 1900 counter accept comment "UPnP"
88 tcp dport { 80, 443 } counter accept comment "HTTP"
89 udp dport 123 skuid ${users.systemd-timesync.name} counter accept comment "NTP"
90 tcp dport 9418 counter accept comment "Git"
92 # Some .nix append rules here with: add rule inet filter fw2net ...
95 # Some .nix append rules here with: add rule inet filter intra2fw ...
98 # Some .nix append rules here with: add rule inet filter fw2intra ...
101 # Some .nix append rules here with: add rule inet filter fwd-intra ...
105 type filter hook input priority 0
114 ct state { established, related } accept
115 ct state invalid counter drop
118 tcp dport 22 counter accept comment "SSH"
119 udp dport 60000-61000 counter accept comment "Mosh"
122 ip protocol icmp icmp type echo-request counter accept
123 ip protocol icmp icmp type destination-unreachable counter accept
124 ip protocol icmp icmp type router-solicitation counter accept
125 ip protocol icmp icmp type router-advertisement counter accept
126 ip protocol icmp icmp type time-exceeded counter accept
127 ip protocol icmp icmp type parameter-problem counter accept
128 #ip protocol icmp icmp type { echo-request, destination-unreachable, router-solicitation, router-advertisement, time-exceeded, parameter-problem } counter accept
129 ip protocol icmp log level warn prefix "net2fw: icmpv: " counter accept
131 ip6 nexthdr icmpv6 icmpv6 type echo-request counter accept
132 ip6 nexthdr icmpv6 icmpv6 type nd-neighbor-solicit counter accept
133 ip6 nexthdr icmpv6 icmpv6 type nd-neighbor-advert counter accept
134 ip6 nexthdr icmpv6 icmpv6 type nd-router-solicit counter accept
135 ip6 nexthdr icmpv6 icmpv6 type nd-router-advert counter accept
136 ip6 nexthdr icmpv6 icmpv6 type mld-listener-query counter accept
137 ip6 nexthdr icmpv6 icmpv6 type mld-listener-report counter accept
138 ip6 nexthdr icmpv6 icmpv6 type mld-listener-reduction counter accept
139 ip6 nexthdr icmpv6 icmpv6 type destination-unreachable counter accept
140 ip6 nexthdr icmpv6 icmpv6 type packet-too-big counter accept
141 ip6 nexthdr icmpv6 icmpv6 type time-exceeded counter accept
142 ip6 nexthdr icmpv6 icmpv6 type parameter-problem counter accept
143 ip6 nexthdr icmpv6 icmpv6 type ind-neighbor-solicit counter accept
144 ip6 nexthdr icmpv6 icmpv6 type ind-neighbor-advert counter accept
145 ip6 nexthdr icmpv6 icmpv6 type mld2-listener-report counter accept
146 ip6 nexthdr icmpv6 log level warn prefix "net2fw: icmpv6: " counter accept
147 #ip6 nexthdr icmpv6 icmpv6 type { echo-request, nd-neighbor-solicit, nd-neighbor-advert, nd-router-solicit, nd-router-advert, mld-listener-query, mld-listener-report, mld-listener-reduction, destination-unreachable, packet-too-big, time-exceeded, parameter-problem, ind-neighbor-solicit, ind-neighbor-advert, mld2-listener-report } counter accept
149 # Some .nix append gotos here with: add rule inet filter input iffname ... goto ...
152 type filter hook output priority 0
157 ct state { related, established } accept
158 ct state invalid counter drop
161 ip protocol icmp counter accept
162 ip6 nexthdr icmpv6 counter accept
164 tcp dport 22 counter accept comment "SSH"
166 # Some .nix append gotos here with: add rule inet filter output oifname ... goto ...
169 type filter hook forward priority 0