]> git.openstreetmap.org Git - chef.git/blobdiff - cookbooks/networking/templates/default/nftables.conf.erb
Allow AWS DNS queries through the firewall
[chef.git] / cookbooks / networking / templates / default / nftables.conf.erb
index 13e070569bb0681ccabb8d133511259b79cdb30f..2545c97c868c733d8b2052e9aa11be0d53a3d0da 100644 (file)
@@ -1,9 +1,11 @@
 #!/usr/sbin/nft -f
 
+<%- unless @interfaces.empty? %>
 define external-interfaces = { <%= @interfaces.sort.uniq.join(", ") %> }
+<%- end %>
 
-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 }
-define ip6-private-addresses = { 2001:db8::/32, fc00::/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 }
+define ip6-private-addresses = { 2001:db8::/32, fc00::/7, ff00::/8 }
 
 table inet filter {
   set ip-osm-addresses {
@@ -62,7 +64,11 @@ table inet filter {
   }
 
   chain incoming {
+<%- if node[:networking][:firewall][:whitelist].empty? %>
     ip saddr { $ip-private-addresses } jump log-and-drop
+<%- else %>
+    ip saddr { $ip-private-addresses } ip saddr != { <%= node[:networking][:firewall][:whitelist].sort.join(", ") %> } jump log-and-drop
+<%- end %>
     ip6 saddr { $ip6-private-addresses } jump log-and-drop
 
     ip saddr @ip-blacklist jump log-and-drop
@@ -80,13 +86,13 @@ table inet filter {
 
     meta l4proto { icmp, icmpv6 } jump log-and-drop
 
-    tcp flags fin,psh,urg / fin,syn,rst,psh,ack,urg jump log-and-drop
-    tcp flags ! fin,syn,rst,psh,ack,urg 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 / fin,psh,ack jump log-and-drop
-    tcp sport 0 tcp flags syn / fin,syn,rst,ack jump log-and-drop
+    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 %>
@@ -96,7 +102,11 @@ table inet filter {
   }
 
   chain outgoing {
+<%- if node[:networking][:firewall][:whitelist].empty? %>
     ip daddr { $ip-private-addresses } jump log-and-drop
+<%- else %>
+    ip daddr { $ip-private-addresses } ip daddr != { <%= node[:networking][:firewall][:whitelist].sort.join(", ") %> } jump log-and-drop
+<%- end %>
     ip6 daddr { $ip6-private-addresses } jump log-and-drop
 
 <%- node[:networking][:firewall][:outgoing].each do |rule| %>
@@ -109,7 +119,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
   }
@@ -117,8 +129,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
   }
@@ -126,7 +140,9 @@ 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
   }
@@ -137,9 +153,9 @@ table ip 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] %>
+<%- node.interfaces(:role => :external, :family => :inet).each do |external| %>
+<%- node.interfaces(:role => :internal, :family => :inet).each do |internal| %>
+    oifname { <%= external[:interface] %> } ip saddr { <%= internal[:network] %>/<%= internal[:prefix] %> } snat <%= external[:address] %>
 <%- end %>
 <%- end %>
   }