radius.m4: Take over NAT duties.
[firewall] / classify.m4
index 148a72f..6ad069c 100644 (file)
@@ -1,4 +1,4 @@
-### -*-m4-*-
+### -*-sh-*-
 ###
 ### Classify packets according to source and destination networks.
 ###
@@ -67,25 +67,45 @@ clearchain mangle:local-source
 
 ## Packets over the loopback interface are automatically trusted.  All manner
 ## of weird stuff happens on lo, and it's best not to second-guess it.
-run iptables -t mangle -A in-classify -i lo -j ACCEPT
+run ip46tables -t mangle -A in-classify -i lo -j ACCEPT
 
-## Local bootp packets have bizarre addresses.  Don't block them just because
-## of this.
+## Local broadcast and link-local multicast packets sometimes have bizarre
+## addresses.  Don't block them just because of this.
 run iptables -t mangle -A in-classify -j RETURN \
        -s 0.0.0.0 -d 255.255.255.255 \
-       -p udp --source-port $port_bootpc --destination-port $port_bootps
+       -p udp
+run iptables -t mangle -A in-classify -j RETURN \
+       -s 0.0.0.0 -d 224.0.0.0/24 \
+       -p udp
 
 ## Since packets with source and destination addresses both local will go
 ## over the loopback interface, I shouldn't see a packet from me over any
 ## other interface.  Except that I will if I sent a broadcast or multicast.
-## Allow the broadcasts, and remember not to trust them.
+## Allow the broadcasts, and remember not to trust them.  There are no
+## broadcast addresses in IPv6 (only link-local multicast)m so we don't have
+## to worry about that.
 run iptables -t mangle -A local-source -j RETURN \
        -m addrtype --dst-type BROADCAST
 run iptables -t mangle -A local-source -j RETURN \
        -m addrtype --dst-type MULTICAST
-run iptables -t mangle -A local-source -g bad-source-address
+run ip6tables -t mangle -A local-source -j RETURN \
+       -d ff00::/8
+run ip46tables -t mangle -A local-source -g bad-source-address
 run iptables -t mangle -A in-classify -j local-source \
        -m addrtype --src-type LOCAL
+for addr in $host_6addrs; do
+  run ip6tables -t mangle -A in-classify -j local-source \
+         -s $addr
+done
+
+## It's not valid to have a multicast address as a packet source: multicast
+## routing is done away from the source, so a multicast address would make
+## this impossible to do.  So discard these packets.  Also discard class-E
+## IPv4 addresses, since they aren't assigned.
+run iptables -t mangle -A in-classify -g bad-source-address \
+       -s 224.0.0.0/3
+run ip6tables -t mangle -A in-classify -g bad-source-address \
+       -s ff00::/8
 
 m4_divert(41)m4_dnl
 ## Define the important networks.
@@ -97,14 +117,32 @@ done
 m4_divert(46)m4_dnl
 ## Mark addresses reachable on non-default interfaces as not reachable on the
 ## default interface.
-trace "nets = $allnets"
+trace "nets = $allnets $allnets6"
 for net in $allnets; do
-  case $net in
-    "$defaultiface":*)
+  defaultp=nil
+  for iface in $defaultifaces; do
+    case $net in $iface:*) defaultp=t ;; esac
+  done
+  case $defaultp in
+    nil)
+      for iface in $defaultifaces; do
+       run iptables -t mangle -A in-$iface \
+             -s ${net#*:} -g bad-source-address
+      done
       ;;
-    *)
-      run iptables -t mangle -A in-$defaultiface \
+  esac
+done
+for net in $allnets6; do
+  defaultp=nil
+  for iface in $defaultifaces; do
+    case $net in $iface:*) defaultp=t ;; esac
+  done
+  case $defaultp in
+    nil)
+      for iface in $defaultifaces; do
+       run ip6tables -t mangle -A in-$iface \
              -s ${net#*:} -g bad-source-address
+      done
       ;;
   esac
 done
@@ -116,17 +154,26 @@ for addr in \
 do
   run iptables -t mangle -A in-default -s $addr -g bad-source-address
 done
+for addr in \
+       fc00::/7 \
+       2001:0db8::/32
+do
+  run ip6tables -t mangle -A in-default -s $addr -g bad-source-address
+done
 
 m4_divert(92)m4_dnl
 ## Put the final default decision on the in-default chain, and attach the
 ## classification chains to the PREROUTING hook.
-run iptables -t mangle -A in-$defaultiface -g mark-from-$defaultclass
-run iptables -t mangle -A PREROUTING -j in-classify
-run iptables -t mangle -A PREROUTING -j out-classify
+for iface in $defaultifaces; do
+  run ip46tables -t mangle -A in-$iface -g mark-from-$defaultclass
+done
+run ip46tables -t mangle -A out-classify -g mark-to-$defaultclass
+run ip46tables -t mangle -A PREROUTING -j in-classify
+run ip46tables -t mangle -A PREROUTING -j out-classify
 
 ## Now it's safe to let stuff through.
 for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do
-  run iptables -t mangle -P $i ACCEPT
+  run ip46tables -t mangle -P $i ACCEPT
 done
 
 m4_divert(-1)