]> git.openstreetmap.org Git - chef.git/blob - cookbooks/networking/templates/default/nftables.conf.erb
Add support for using an nftables based firewall
[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 define ip-osm-addresses = { <%= Array(@hosts[:inet]).sort.join(", ") %> }
9 define ip6-osm-addresses = { <%= Array(@hosts[:inet6]).sort.join(", ") %> }
10
11 flush ruleset
12
13 table inet filter {
14   set ip-blacklist {
15     type ipv4_addr
16     flags dynamic
17   }
18
19   set ip6-blacklist {
20     type ipv6_addr
21     flags dynamic
22   }
23
24 <%- node[:networking][:firewall][:sets].each do |set| %>
25   set <%= set %> {
26 <%- if set.end_with?("-ip") %>
27     type ipv4_addr
28 <%- elsif set.end_with?("-ip6") %>
29     type ipv6_addr
30 <%- end %>
31     flags dynamic
32   }
33
34 <%- end %>
35   chain log-and-drop {
36     limit rate 1/second log
37     drop
38   }
39
40   chain log-and-reject {
41     limit rate 1/second log
42     reject
43   }
44
45   chain incoming {
46     ip saddr { $ip-private-addresses } jump log-and-drop
47     ip6 saddr { $ip6-private-addresses } jump log-and-drop
48
49     ip saddr @ip-blacklist jump log-and-drop
50     ip6 saddr @ip6-blacklist jump log-and-drop
51
52     ct state { established, related } accept
53
54     icmp type { destination-unreachable } accept
55     icmpv6 type { nd-neighbor-solicit, nd-neighbor-advert, nd-router-solicit, nd-router-advert } accept
56
57     icmp type { echo-request } limit rate 1/second accept
58     icmpv6 type { echo-request } limit rate 1/second accept
59
60     meta l4proto { icmp, icmpv6 } jump log-and-drop
61
62 <%- node[:networking][:firewall][:incoming].uniq.each do |rule| %>
63     <%= rule %>
64 <%- end %>
65
66     jump log-and-drop
67   }
68
69   chain outgoing {
70     ip daddr { $ip-private-addresses } jump log-and-drop
71     ip6 daddr { $ip6-private-addresses } jump log-and-drop
72
73 <%- node[:networking][:firewall][:outgoing].each do |rule| %>
74     <%= rule %>
75 <%- end %>
76
77     accept
78   }
79
80   chain input {
81     type filter hook input priority filter;
82
83     iif { $external-interfaces } jump incoming
84
85     accept
86   }
87
88   chain forward {
89     type filter hook forward priority filter;
90
91     iif { $external-interfaces } jump incoming
92     oif { $external-interfaces } jump outgoing
93
94     accept
95   }
96
97   chain output {
98     type filter hook output priority filter;
99
100     oif { $external-interfaces } jump outgoing
101
102     accept
103   }
104 }
105 <%- if node[:roles].include?("gateway") %>
106
107 table ip nat {
108   chain postrouting {
109     type nat hook postrouting priority srcnat;
110
111 <%- node.interfaces(:role => :external).each do |external| %>
112 <%- node.interfaces(:role => :internal).each do |internal| %>
113     oif { < %= external[:interface] %> } ip saddr { <%= internal[:network] %>/<%= internal[:prefix] %> } snat <%= external[:address] %>
114 <%- end %>
115 <%- end %>
116   }
117 }
118 <%- end %>