X-Git-Url: https://git.openstreetmap.org./chef.git/blobdiff_plain/124c8d627e1313782d38b20ac1d384459beb0dae..50aea11082eaa90bd37593b9c987d38a082f4e1b:/cookbooks/networking/templates/default/nftables.conf.erb diff --git a/cookbooks/networking/templates/default/nftables.conf.erb b/cookbooks/networking/templates/default/nftables.conf.erb index cb9891624..1ef38da7f 100644 --- a/cookbooks/networking/templates/default/nftables.conf.erb +++ b/cookbooks/networking/templates/default/nftables.conf.erb @@ -1,89 +1,128 @@ #!/usr/sbin/nft -f +<% unless @interfaces.empty? -%> define external-interfaces = { <%= @interfaces.sort.uniq.join(", ") %> } - -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 } +<% end -%> +# Exclude 169.254.169.0/24 from ip-private-addresses as is widely by cloud providers. +define ip-private-addresses = { 0.0.0.0, 10.0.0.0/8, 127.0.0.0/8, 169.254.0.0-169.254.168.255, 169.254.170.0-169.254.255.255, 172.16.0.0/12, 192.0.2.0/24, 192.168.0.0/16 } +define ip-multicast-addresses = { 224.0.0.0/4 } define ip6-private-addresses = { 2001:db8::/32, fc00::/7 } +define ip6-multicast-addresses = { ff00::/8 } -table inet filter { +table inet chef-filter { set ip-osm-addresses { type ipv4_addr -<%- unless Array(@hosts["inet"]).empty? %> - elements = { <%= Array(@hosts["inet"]).sort.join(", ") %> } -<%- end %> +<% unless Array(@hosts[:inet]).empty? -%> + elements = { <%= Array(@hosts[:inet]).sort.join(", ") %> } +<% end -%> } set ip6-osm-addresses { type ipv6_addr -<%- unless Array(@hosts["inet"]).empty? %> - elements = { <%= Array(@hosts["inet6"]).sort.join(", ") %> } -<%- end %> +<% unless Array(@hosts[:inet6]).empty? -%> + elements = { <%= Array(@hosts[:inet6]).sort.join(", ") %> } +<% end -%> } - set ip-blacklist { + set ip-blocklist { type ipv4_addr - flags dynamic + flags interval } - set ip6-blacklist { + set ip6-blocklist { type ipv6_addr - flags dynamic + flags interval } -<%- node[:networking][:firewall][:sets].each do |set| %> - set <%= set %> { -<%- if set.end_with?("-ip") %> + set ratelimit-icmp-echo-ip { type ipv4_addr -<%- elsif set.end_with?("-ip6") %> + flags dynamic + timeout 120s + } + + set ratelimit-icmp-echo-ip6 { type ipv6_addr -<%- end %> flags dynamic + timeout 120s + } + +<% node[:networking][:firewall][:sets].each do |set| -%> + set <%= set[:name] %> { + type <%= set[:type] %> +<% if set[:flags] -%> + flags <%= set[:flags].join(", ") %> +<% end -%> +<% if set[:timeout] -%> + timeout <%= set[:timeout] %>s +<% end -%> } -<%- end %> +<% end -%> + +<% node[:networking][:firewall][:helpers].each do |helper| -%> + ct helper <%= helper[:name] %> { + type "<%= helper[:helper] %>" protocol <%= helper[:protocol] %> + } + +<% end -%> chain log-and-drop { - limit rate 1/second log + limit rate 1/second log level notice drop } chain log-and-reject { - limit rate 1/second log + limit rate 1/second log level notice reject } chain incoming { - ip saddr { $ip-private-addresses } jump log-and-drop - ip6 saddr { $ip6-private-addresses } jump log-and-drop - - ip saddr @ip-blacklist jump log-and-drop - ip6 saddr @ip6-blacklist jump log-and-drop +<% if node[:networking][:firewall][:allowlist].empty? -%> + ip saddr { $ip-private-addresses, $ip-multicast-addresses } jump log-and-drop +<% else -%> + ip saddr { $ip-private-addresses, $ip-multicast-addresses } ip saddr != { <%= node[:networking][:firewall][:allowlist].sort.join(", ") %> } jump log-and-drop +<% end -%> + ip6 saddr { $ip6-private-addresses, $ip6-multicast-addresses } jump log-and-drop - ct state { established, related } accept + ip saddr @ip-blocklist jump log-and-drop + ip6 saddr @ip6-blocklist jump log-and-drop - icmp type { destination-unreachable } accept - icmp type { echo-request } limit rate 1/second accept + icmp type { echo-request } update @ratelimit-icmp-echo-ip { ip saddr limit rate 1/second } accept icmp type { echo-request } drop icmpv6 type { nd-neighbor-solicit, nd-neighbor-advert, nd-router-solicit, nd-router-advert } accept - icmpv6 type { echo-request } limit rate 1/second accept + icmpv6 type { echo-request } update @ratelimit-icmp-echo-ip6 { ip6 saddr limit rate 1/second } accept icmpv6 type { echo-request } drop + ct state { established, related } accept + meta l4proto { icmp, icmpv6 } jump log-and-drop -<%- node[:networking][:firewall][:incoming].uniq.each do |rule| %> + tcp flags & (fin|syn|rst|psh|ack|urg) == fin|psh|urg jump log-and-drop + tcp flags & (fin|syn|rst|psh|ack|urg) == 0x0 jump log-and-drop + tcp flags & (syn|rst) == syn|rst jump log-and-drop + tcp flags & (fin|rst) == fin|rst jump log-and-drop + tcp flags & (fin|syn) == fin|syn jump log-and-drop + tcp flags & (fin|psh|ack) == fin|psh jump log-and-drop + tcp sport 0 tcp flags & (fin|syn|rst|ack) == syn jump log-and-drop + +<% node[:networking][:firewall][:incoming].uniq.each do |rule| -%> <%= rule %> -<%- end %> +<% end -%> jump log-and-drop } chain outgoing { +<% if node[:networking][:firewall][:allowlist].empty? -%> ip daddr { $ip-private-addresses } jump log-and-drop +<% else -%> + ip daddr { $ip-private-addresses } ip daddr != { <%= node[:networking][:firewall][:allowlist].sort.join(", ") %> } jump log-and-drop +<% end -%> ip6 daddr { $ip6-private-addresses } jump log-and-drop -<%- node[:networking][:firewall][:outgoing].each do |rule| %> +<% node[:networking][:firewall][:outgoing].each do |rule| -%> <%= rule %> -<%- end %> +<% end -%> accept } @@ -91,7 +130,9 @@ table inet filter { chain input { type filter hook input priority filter; - iif { $external-interfaces } jump incoming +<% unless @interfaces.empty? -%> + iifname { $external-interfaces } jump incoming +<% end -%> accept } @@ -99,8 +140,10 @@ table inet filter { chain forward { type filter hook forward priority filter; - iif { $external-interfaces } jump incoming - oif { $external-interfaces } jump outgoing +<% unless @interfaces.empty? -%> + iifname { $external-interfaces } jump incoming + oifname { $external-interfaces } jump outgoing +<% end -%> accept } @@ -108,22 +151,24 @@ table inet filter { chain output { type filter hook output priority filter; - oif { $external-interfaces } jump outgoing +<% unless @interfaces.empty? -%> + oifname { $external-interfaces } jump outgoing +<% end -%> accept } } -<%- if node[:roles].include?("gateway") %> +<% if node[:roles].include?("gateway") -%> -table ip nat { +table ip chef-nat { chain postrouting { type nat hook postrouting priority srcnat; -<%- node.interfaces(:role => :external).each do |external| %> -<%- node.interfaces(:role => :internal).each do |internal| %> - oif { < %= external[:interface] %> } ip saddr { <%= internal[:network] %>/<%= internal[:prefix] %> } snat <%= external[:address] %> -<%- end %> -<%- end %> +<% node.interfaces(:role => :external).each do |external| -%> +<% node.ipaddresses(:role => :internal, :family => :inet).each do |internal| -%> + oifname { <%= external[:interface] %> } ip saddr { <%= internal.subnet %> } snat <%= external[:inet][:address] %> +<% end -%> +<% end -%> } } -<%- end %> +<% end -%>