]> git.openstreetmap.org Git - chef.git/blob - cookbooks/networking/templates/default/nftables.conf.erb
Make nftables block various invalid TCP flag combinations
[chef.git] / cookbooks / networking / templates / default / nftables.conf.erb
1 #!/usr/sbin/nft -f
2
3 define external-interfaces = { <%= @interfaces.sort.uniq.join(", ") %> }
4
5 define ip-private-addresses = { 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 }
6 define ip6-private-addresses = { 2001:db8::/32, fc00::/7 }
7
8 table inet filter {
9   set ip-osm-addresses {
10     type ipv4_addr
11 <%- unless Array(@hosts["inet"]).empty? %>
12     elements = { <%= Array(@hosts["inet"]).sort.join(", ") %> }
13 <%- end %>
14   }
15
16   set ip6-osm-addresses {
17     type ipv6_addr
18 <%- unless Array(@hosts["inet"]).empty? %>
19     elements = { <%= Array(@hosts["inet6"]).sort.join(", ") %> }
20 <%- end %>
21   }
22
23   set ip-blacklist {
24     type ipv4_addr
25     flags dynamic
26   }
27
28   set ip6-blacklist {
29     type ipv6_addr
30     flags dynamic
31   }
32
33 <%- node[:networking][:firewall][:sets].each do |set| %>
34   set <%= set %> {
35 <%- if set.end_with?("-ip") %>
36     type ipv4_addr
37 <%- elsif set.end_with?("-ip6") %>
38     type ipv6_addr
39 <%- end %>
40     flags dynamic
41   }
42
43 <%- end %>
44   chain log-and-drop {
45     limit rate 1/second log
46     drop
47   }
48
49   chain log-and-reject {
50     limit rate 1/second log
51     reject
52   }
53
54   chain incoming {
55     ip saddr { $ip-private-addresses } jump log-and-drop
56     ip6 saddr { $ip6-private-addresses } jump log-and-drop
57
58     ip saddr @ip-blacklist jump log-and-drop
59     ip6 saddr @ip6-blacklist jump log-and-drop
60
61     ct state { established, related } accept
62
63     icmp type { destination-unreachable } accept
64     icmp type { echo-request } limit rate 1/second accept
65     icmp type { echo-request } drop
66
67     icmpv6 type { nd-neighbor-solicit, nd-neighbor-advert, nd-router-solicit, nd-router-advert } accept
68     icmpv6 type { echo-request } limit rate 1/second accept
69     icmpv6 type { echo-request } drop
70
71     meta l4proto { icmp, icmpv6 } jump log-and-drop
72
73     tcp flags fin,psh,urg / fin,syn,rst,psh,ack,urg jump log-and-drop
74     tcp flags ! fin,syn,rst,psh,ack,urg jump log-and-drop
75     tcp flags syn,rst / syn,rst jump log-and-drop
76     tcp flags fin,rst / fin,rst jump log-and-drop
77     tcp flags fin,syn / fin,syn jump log-and-drop
78     tcp flags fin,psh / fin,psh,ack jump log-and-drop
79     tcp sport 0 tcp flags syn / fin,syn,rst,ack jump log-and-drop
80
81 <%- node[:networking][:firewall][:incoming].uniq.each do |rule| %>
82     <%= rule %>
83 <%- end %>
84
85     jump log-and-drop
86   }
87
88   chain outgoing {
89     ip daddr { $ip-private-addresses } jump log-and-drop
90     ip6 daddr { $ip6-private-addresses } jump log-and-drop
91
92 <%- node[:networking][:firewall][:outgoing].each do |rule| %>
93     <%= rule %>
94 <%- end %>
95
96     accept
97   }
98
99   chain input {
100     type filter hook input priority filter;
101
102     iif { $external-interfaces } jump incoming
103
104     accept
105   }
106
107   chain forward {
108     type filter hook forward priority filter;
109
110     iif { $external-interfaces } jump incoming
111     oif { $external-interfaces } jump outgoing
112
113     accept
114   }
115
116   chain output {
117     type filter hook output priority filter;
118
119     oif { $external-interfaces } jump outgoing
120
121     accept
122   }
123 }
124 <%- if node[:roles].include?("gateway") %>
125
126 table ip nat {
127   chain postrouting {
128     type nat hook postrouting priority srcnat;
129
130 <%- node.interfaces(:role => :external).each do |external| %>
131 <%- node.interfaces(:role => :internal).each do |internal| %>
132     oif { < %= external[:interface] %> } ip saddr { <%= internal[:network] %>/<%= internal[:prefix] %> } snat <%= external[:address] %>
133 <%- end %>
134 <%- end %>
135   }
136 }
137 <%- end %>