]> git.openstreetmap.org Git - chef.git/blob - cookbooks/networking/templates/default/nftables.conf.erb
Allow AWS DNS queries through the firewall
[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 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-blacklist {
26     type ipv4_addr
27     flags dynamic
28   }
29
30   set ip6-blacklist {
31     type ipv6_addr
32     flags dynamic
33   }
34
35   set limit-icmp-echo-ip {
36     type ipv4_addr
37     flags dynamic
38   }
39
40   set limit-icmp-echo-ip6 {
41     type ipv6_addr
42     flags dynamic
43   }
44
45 <%- node[:networking][:firewall][:sets].each do |set| %>
46   set <%= set %> {
47 <%- if set.end_with?("-ip") %>
48     type ipv4_addr
49 <%- elsif set.end_with?("-ip6") %>
50     type ipv6_addr
51 <%- end %>
52     flags dynamic
53   }
54
55 <%- end %>
56   chain log-and-drop {
57     limit rate 1/second log
58     drop
59   }
60
61   chain log-and-reject {
62     limit rate 1/second log
63     reject
64   }
65
66   chain incoming {
67 <%- if node[:networking][:firewall][:whitelist].empty? %>
68     ip saddr { $ip-private-addresses } jump log-and-drop
69 <%- else %>
70     ip saddr { $ip-private-addresses } ip saddr != { <%= node[:networking][:firewall][:whitelist].sort.join(", ") %> } jump log-and-drop
71 <%- end %>
72     ip6 saddr { $ip6-private-addresses } jump log-and-drop
73
74     ip saddr @ip-blacklist jump log-and-drop
75     ip6 saddr @ip6-blacklist jump log-and-drop
76
77     ct state { established, related } accept
78
79     icmp type { destination-unreachable } accept
80     icmp type { echo-request } add @limit-icmp-echo-ip { ip saddr limit rate 1/second } accept
81     icmp type { echo-request } drop
82
83     icmpv6 type { nd-neighbor-solicit, nd-neighbor-advert, nd-router-solicit, nd-router-advert } accept
84     icmpv6 type { echo-request } add @limit-icmp-echo-ip6 { ip6 saddr limit rate 1/second } accept
85     icmpv6 type { echo-request } drop
86
87     meta l4proto { icmp, icmpv6 } jump log-and-drop
88
89     tcp flags & (fin|syn|rst|psh|ack|urg) == fin|psh|urg jump log-and-drop
90     tcp flags & (fin|syn|rst|psh|ack|urg) == 0x0 jump log-and-drop
91     tcp flags & (syn|rst) == syn|rst jump log-and-drop
92     tcp flags & (fin|rst) == fin|rst jump log-and-drop
93     tcp flags & (fin|syn) == fin|syn jump log-and-drop
94     tcp flags & (fin|psh|ack) == fin|psh jump log-and-drop
95     tcp sport 0 tcp flags & (fin|syn|rst|ack) == syn jump log-and-drop
96
97 <%- node[:networking][:firewall][:incoming].uniq.each do |rule| %>
98     <%= rule %>
99 <%- end %>
100
101     jump log-and-drop
102   }
103
104   chain outgoing {
105 <%- if node[:networking][:firewall][:whitelist].empty? %>
106     ip daddr { $ip-private-addresses } jump log-and-drop
107 <%- else %>
108     ip daddr { $ip-private-addresses } ip daddr != { <%= node[:networking][:firewall][:whitelist].sort.join(", ") %> } jump log-and-drop
109 <%- end %>
110     ip6 daddr { $ip6-private-addresses } jump log-and-drop
111
112 <%- node[:networking][:firewall][:outgoing].each do |rule| %>
113     <%= rule %>
114 <%- end %>
115
116     accept
117   }
118
119   chain input {
120     type filter hook input priority filter;
121
122 <%- unless @interfaces.empty? %>
123     iifname { $external-interfaces } jump incoming
124 <%- end %>
125
126     accept
127   }
128
129   chain forward {
130     type filter hook forward priority filter;
131
132 <%- unless @interfaces.empty? %>
133     iifname { $external-interfaces } jump incoming
134     oifname { $external-interfaces } jump outgoing
135 <%- end %>
136
137     accept
138   }
139
140   chain output {
141     type filter hook output priority filter;
142
143 <%- unless @interfaces.empty? %>
144     oifname { $external-interfaces } jump outgoing
145 <%- end %>
146
147     accept
148   }
149 }
150 <%- if node[:roles].include?("gateway") %>
151
152 table ip nat {
153   chain postrouting {
154     type nat hook postrouting priority srcnat;
155
156 <%- node.interfaces(:role => :external, :family => :inet).each do |external| %>
157 <%- node.interfaces(:role => :internal, :family => :inet).each do |internal| %>
158     oifname { <%= external[:interface] %> } ip saddr { <%= internal[:network] %>/<%= internal[:prefix] %> } snat <%= external[:address] %>
159 <%- end %>
160 <%- end %>
161   }
162 }
163 <%- end %>