]> git.openstreetmap.org Git - chef.git/blob - cookbooks/networking/templates/default/nftables.conf.erb
Rename firewall tables to avoid any clash with iptables
[chef.git] / cookbooks / networking / templates / default / nftables.conf.erb
1 #!/usr/sbin/nft -f
2
3 <%- unless @interfaces.empty? %>
4 define external-interfaces = { <%= @interfaces.sort.uniq.join(", ") %> }
5 <%- end %>
6
7 define ip-private-addresses = { 0.0.0.0, 10.0.0.0/8, 127.0.0.0/8, 169.254.0.0/16, 172.16.0.0/12, 192.0.2.0/24, 192.168.0.0/16, 224.0.0.0/4 }
8 define ip6-private-addresses = { 2001:db8::/32, fc00::/7, ff00::/8 }
9
10 table inet chef-filter {
11   set ip-osm-addresses {
12     type ipv4_addr
13 <%- unless Array(@hosts["inet"]).empty? %>
14     elements = { <%= Array(@hosts["inet"]).sort.join(", ") %> }
15 <%- end %>
16   }
17
18   set ip6-osm-addresses {
19     type ipv6_addr
20 <%- unless Array(@hosts["inet"]).empty? %>
21     elements = { <%= Array(@hosts["inet6"]).sort.join(", ") %> }
22 <%- end %>
23   }
24
25   set ip-blocklist {
26     type ipv4_addr
27     flags dynamic
28   }
29
30   set ip6-blocklist {
31     type ipv6_addr
32     flags dynamic
33   }
34
35   set ratelimit-icmp-echo-ip {
36     type ipv4_addr
37     flags dynamic
38     timeout 120s
39   }
40
41   set ratelimit-icmp-echo-ip6 {
42     type ipv6_addr
43     flags dynamic
44     timeout 120s
45   }
46
47 <%- node[:networking][:firewall][:sets].each do |set| %>
48   set <%= set %> {
49 <%- if set.end_with?("-ip") %>
50     type ipv4_addr
51 <%- elsif set.end_with?("-ip6") %>
52     type ipv6_addr
53 <%- end %>
54     flags dynamic
55 <%- unless set.start_with?("connlimit-") %>
56     timeout 120s
57 <%- end %>
58   }
59
60 <%- end %>
61   chain log-and-drop {
62     limit rate 1/second log
63     drop
64   }
65
66   chain log-and-reject {
67     limit rate 1/second log
68     reject
69   }
70
71   chain incoming {
72 <%- if node[:networking][:firewall][:allowlist].empty? %>
73     ip saddr { $ip-private-addresses } jump log-and-drop
74 <%- else %>
75     ip saddr { $ip-private-addresses } ip saddr != { <%= node[:networking][:firewall][:allowlist].sort.join(", ") %> } jump log-and-drop
76 <%- end %>
77     ip6 saddr { $ip6-private-addresses } jump log-and-drop
78
79     ip saddr @ip-blocklist jump log-and-drop
80     ip6 saddr @ip6-blocklist jump log-and-drop
81
82     ct state { established, related } accept
83
84     icmp type { destination-unreachable } accept
85     icmp type { echo-request } update @ratelimit-icmp-echo-ip { ip saddr limit rate 1/second } accept
86     icmp type { echo-request } drop
87
88     icmpv6 type { nd-neighbor-solicit, nd-neighbor-advert, nd-router-solicit, nd-router-advert } accept
89     icmpv6 type { echo-request } update @ratelimit-icmp-echo-ip6 { ip6 saddr limit rate 1/second } accept
90     icmpv6 type { echo-request } drop
91
92     meta l4proto { icmp, icmpv6 } jump log-and-drop
93
94     tcp flags & (fin|syn|rst|psh|ack|urg) == fin|psh|urg jump log-and-drop
95     tcp flags & (fin|syn|rst|psh|ack|urg) == 0x0 jump log-and-drop
96     tcp flags & (syn|rst) == syn|rst jump log-and-drop
97     tcp flags & (fin|rst) == fin|rst jump log-and-drop
98     tcp flags & (fin|syn) == fin|syn jump log-and-drop
99     tcp flags & (fin|psh|ack) == fin|psh jump log-and-drop
100     tcp sport 0 tcp flags & (fin|syn|rst|ack) == syn jump log-and-drop
101
102 <%- node[:networking][:firewall][:incoming].uniq.each do |rule| %>
103     <%= rule %>
104 <%- end %>
105
106     jump log-and-drop
107   }
108
109   chain outgoing {
110 <%- if node[:networking][:firewall][:allowlist].empty? %>
111     ip daddr { $ip-private-addresses } jump log-and-drop
112 <%- else %>
113     ip daddr { $ip-private-addresses } ip daddr != { <%= node[:networking][:firewall][:allowlist].sort.join(", ") %> } jump log-and-drop
114 <%- end %>
115     ip6 daddr { $ip6-private-addresses } jump log-and-drop
116
117 <%- node[:networking][:firewall][:outgoing].each do |rule| %>
118     <%= rule %>
119 <%- end %>
120
121     accept
122   }
123
124   chain input {
125     type filter hook input priority filter;
126
127 <%- unless @interfaces.empty? %>
128     iifname { $external-interfaces } jump incoming
129 <%- end %>
130
131     accept
132   }
133
134   chain forward {
135     type filter hook forward priority filter;
136
137 <%- unless @interfaces.empty? %>
138     iifname { $external-interfaces } jump incoming
139     oifname { $external-interfaces } jump outgoing
140 <%- end %>
141
142     accept
143   }
144
145   chain output {
146     type filter hook output priority filter;
147
148 <%- unless @interfaces.empty? %>
149     oifname { $external-interfaces } jump outgoing
150 <%- end %>
151
152     accept
153   }
154 }
155 <%- if node[:roles].include?("gateway") %>
156
157 table ip chef-nat {
158   chain postrouting {
159     type nat hook postrouting priority srcnat;
160
161 <%- node.interfaces(:role => :external, :family => :inet).each do |external| %>
162 <%- node.interfaces(:role => :internal, :family => :inet).each do |internal| %>
163     oifname { <%= external[:interface] %> } ip saddr { <%= internal[:network] %>/<%= internal[:prefix] %> } snat <%= external[:address] %>
164 <%- end %>
165 <%- end %>
166   }
167 }
168 <%- end %>