3f1f4466a0f1ee740480ff1e366f9ce24b4146b2
[config/nftables.git] / nftables-workstation.nft
1 #!/usr/sbin/nft -f
2 #
3 # nftables ruleset for an End Node acting as a workstation in a LAN.
4 #
5 # Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
6 # SPDX-License-Identifier: MIT
7 #
8 # Based on:
9 # https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes#Simple_IP.2FIPv6_Firewall
10 # https://wiki.archlinux.org/index.php/Nftables#Workstation
11 # https://stosb.com/blog/explaining-my-configs-nftables/
12
13 flush ruleset
14
15 table inet filter {
16
17     define common_open_ports = {
18             xmpp-client,
19             xmpp-server,
20             5298, # Link-local XMPP, for backward compatibility, XEP-0174
21     }
22
23     set tcp_open_ports {
24         type inet_service
25         flags interval
26
27         elements = {
28             $common_open_ports,
29         }
30     }
31
32     set udp_open_ports {
33         type inet_service
34         flags interval
35
36         elements = {
37             $common_open_ports,
38         }
39     }
40
41     chain input {
42         type filter hook input priority 0;
43
44         ct state established,related accept
45         ct state invalid drop
46         ct state new jump in-new
47
48         iif lo accept
49
50         ip protocol icmp icmp type {
51             echo-reply,
52             echo-request,
53             time-exceeded,
54             parameter-problem,
55             destination-unreachable
56         } accept
57
58         # ICMPv6 configuration based on:
59         # https://github.com/intel/intel-iot-refkit/blob/master/meta-refkit-core/recipes-security/nftables-settings-default/files/firewall.template
60         ip6 nexthdr ipv6-icmp icmpv6 type {
61             echo-reply,
62             echo-request,
63             time-exceeded,
64             parameter-problem,
65             destination-unreachable,
66             packet-too-big
67         } accept
68
69         # Allow auto configuration support.
70         ip6 nexthdr ipv6-icmp icmpv6 type {
71             nd-router-advert,
72             nd-router-solicit,
73             nd-neighbor-advert,
74             nd-neighbor-solicit
75         } ip6 hoplimit 255 accept
76
77         # Allow multicast listener discovery on link-local addresses.
78         ip6 nexthdr ipv6-icmp icmpv6 type {
79             mld-listener-query,
80             mld-listener-report,
81             mld-listener-reduction
82         } ip6 saddr fe80::/10 accept
83
84         # Allow multicast router discovery messages on link-local addresses (hop limit 1).
85         ip6 nexthdr ipv6-icmp icmpv6 type {
86             nd-router-advert,
87             nd-router-solicit
88         } ip6 hoplimit 1 ip6 saddr fe80::/10 accept
89
90         # Allow IGMPv3 queries.
91         ip protocol igmp ip daddr 224.0.0.1 accept
92
93         # Silently drop other incoming broadcast and multicast traffic.
94         meta pkttype {broadcast, multicast} drop
95
96         limit rate 3/minute burst 10 packets log prefix "[INPUT]: "
97         counter reject
98     }
99
100     chain in-new {
101         # Silently drop DHCPv4 Discover and Request packets from other clients.
102         ip saddr 0.0.0.0 ip daddr 255.255.255.255 udp sport bootpc udp dport bootps drop
103
104         # Silently drop DHCPv6 from other clients.
105         ip6 saddr fe80::/64 ip6 daddr fe80::/64 udp sport dhcpv6-client udp dport dhcpv6-server drop
106
107         # mDNS (ZeroConf/Bonjour)
108         ip  daddr 224.0.0.251 udp dport mdns accept
109         ip6 daddr ff02::fb    udp dport mdns accept
110
111         # SSDP: https://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol
112         ip daddr 239.255.255.250 udp dport 1900 accept
113         ip6 daddr {
114             ff02::c,
115             ff05::c,
116             ff08::c,
117             ff0e::c
118         } udp dport 1900 accept
119
120         tcp dport @tcp_open_ports tcp flags & (fin | syn | rst | ack) == syn accept
121         udp dport @udp_open_ports accept
122     }
123
124     chain forward {
125         type filter hook forward priority 0;
126         limit rate 3/minute burst 10 packets log prefix "[FORWARD]: "
127         counter reject
128     }
129
130     chain output {
131         type filter hook output priority 0;
132         counter accept
133     }
134 }