local.m4: Fix whitespace oddity.
[firewall] / classify.m4
index 27eb376..6254993 100644 (file)
@@ -1,4 +1,4 @@
-### -*-m4-*-
+### -*-sh-*-
 ###
 ### Classify packets according to source and destination networks.
 ###
@@ -45,8 +45,9 @@ m4_divert(40)m4_dnl
 ###
 ### The mangle chains are arranged as follows.
 ###
-### The PREROUTING hook simply invokes in-classify and out-classify chains as
-### subroutines.  These will tail-call appropriate classification chains.
+### The INPUT and FORWARD hooks simply invoke in-classify and out-classify
+### chains as subroutines.  These will tail-call appropriate classification
+### chains.
 ###
 ### The in-classify chain is responsible for both source address
 ### classification and verifying that the packet arrived from the correct
@@ -56,29 +57,47 @@ m4_divert(40)m4_dnl
 ### goes to bad-source-address, which logs a message and drops the packet.
 ### The default interface is special.  If no explicit matches are found, it
 ### dispatches to in-default which forbids a few obviously evil things and
-### finally dispatches to mark-from-untrusted.
+### finally dispatches to mark-from-DEFAULT (usually `untrusted').
 ###
 ### The out-classify is simpler because it doesn't care about the interface.
 ### It simply checks each network range in turn, dispatching to mark-to-CLASS
-### on a match or mark-to-DEFAULT (probably untrusted) if there is no match.
+### on a match or mark-to-DEFAULT (probably `untrusted') if there is no
+### match.
 
 clearchain mangle:in-classify mangle:in-default mangle:out-classify
 clearchain mangle:local-source
 
+## An unpleasant hack.  We can't reject packets from the mangle table, so
+## we mark packets with a bad destination and then detect this in the
+## filter table.
+clearchain mangle:bad-destination-address
+BAD_DEST=0xf6f377d2
+run ip46tables -t mangle -A bad-destination-address \
+  -j MARK --set-mark $BAD_DEST
+run ip46tables -t mangle -A bad-destination-address -j ACCEPT
+for i in $inchains; do
+  run ip46tables -A $i -m mark --mark $BAD_DEST -g bad-destination-address
+done
+
 ## 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 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) 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 \
@@ -101,50 +120,247 @@ m4_divert(42)m4_dnl
 done
 
 m4_divert(46)m4_dnl
-## Mark addresses reachable on non-default interfaces as not reachable on the
-## default interface.
-trace "nets = $allnets $allnets6"
+## Special IPv4 source addresses.  Forbid broadcast and multicast sources.
+## Mark the special zero address and link-local addresses as such.  (This
+## also matches class-E addresses, which are probably permanently invalid.)
+for i in 0.0.0.0 169.254.0.0/16; do
+  run iptables -t mangle -A in-classify -g mark-from-link -s $i
+done
+run iptables -t mangle -A in-classify -g bad-source-address \
+       -s 224.0.0.0/3
+run iptables -t mangle -A in-classify -g bad-source-address \
+       -m addrtype --src-type BROADCAST \
+
+## Special IPv6 addresses.  Format multicast sources, and mark zero and
+## link local addresses.
+for i in :: fe80::/10; do
+  run ip6tables -t mangle -A in-classify -g mark-from-link -s $i
+done
+run ip6tables -t mangle -A in-classify -g bad-source-address \
+       -s ff00::/8
+
+## Special IPv4 destination addresses.  The zero address is invalid; mark
+## link-local and recognized broadcast addresses as link-local.  We leave
+## multicast for later.
+for i in 0.0.0.0 240.0.0.0/4; do
+  run iptables -t mangle -A out-classify -g bad-destination-address -d $i
+done
+run iptables -t mangle -A out-classify -g mark-to-link -d 169.254.0.0/16
+run iptables -t mangle -A out-classify -g mark-to-link \
+       -m addrtype --dst-type BROADCAST
+
+## Special IPv6 destination addressses.  The zero address is again invalid;
+## mark link local addresses.  We do multicast later.
+run ip6tables -t mangle -A out-classify -g bad-destination-address \
+       -d ::
+run ip6tables -t mangle -A out-classify -g mark-to-link -d fe80::/10
+
+## Now deal with multicast.  Link-local multicast is detected as being
+## link-local, so that we can prevent it being forwarded correctly.
+clearchain mangle:out-classify-mcast
+run iptables -t mangle -A out-classify-mcast -g mark-to-link \
+       -d 224.0.0.0/24
+for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
+  run ip6tables -t mangle -A out-classify-mcast -g mark-to-link \
+         -d ff${i}2::/16
+done
+run ip46tables -t mangle -A out-classify-mcast -g mark-to-mcast
+run iptables -t mangle -A out-classify -g out-classify-mcast \
+       -d 224.0.0.0/4
+run ip6tables -t mangle -A out-classify -g out-classify-mcast \
+       -d ff00::/8
+
+## Build the input classification chains.  There's one chain `in-IFACE' for
+## each local interface.  This chain does a further dispatch on the source
+## address to the appropriate `mark-from-CLASS' chain for the source network
+## class.  We also build a table mapping interface names to numbers (since
+## the names are so unhelpful).
+seen=:
+ifq=0
+ifmap=""
+for iface in $host_ifaces_<::>FWHOST; do
+  ifname=${iface%=*}
+  case $seen in *:$ifname:*) continue ;; esac
+  seen=$seen$ifname:
+  addword ifmap $ifname=$ifq
+  ifq=$(( $ifq + 1 ))
+  clearchain mangle:in-$ifname
+  run ip46tables -t mangle -A in-classify -i $ifname -g in-$ifname
+done
+
+## We do a first pass over nets first, and then the interfaces which those
+## networks reach.  During this pass, we populate the `out-classify' chains,
+## and we also build some lists so that we can do later passes over
+## interfaces first and then reaching networks.  This is complicated by
+## interface names being unhelpful.
+##
+## Here are the variables we maintain.
+##
+## ifmap               A list of entries IFACE=N mapping interface names to
+##                     numbers.
+##
+## ifnets_N            A space-separated list of networks reaching interface
+##                     number N.  This is used for building the matching
+##                     chains.
+##
+## ifaddrs_N           A bang-separated list of address ranges reaching
+##                     interface number N.  This is used for filtering out
+##                     known networks if the default network reaches the
+##                     interface.
 for net in $allnets; do
-  case $net in
-    "$defaultiface":*)
-      ;;
-    *)
-      run iptables -t mangle -A in-$defaultiface \
-             -s ${net#*:} -g bad-source-address
-      ;;
-  esac
+
+  ## Work through the interfaces that this network reaches.
+  for iface in $(net_interfaces FWHOST $net); do
+    case $iface in -) break ;; esac
+
+    ## Find a sequence number for this interface.
+    q=nil
+    for i in $ifmap; do
+      case "$i" in "$iface"=*) q=${i##*=}; break ;; esac
+    done
+    case $q in
+      nil)
+       echo >&2 "$0 INTERNAL ERROR: missing interface \`$iface'!"
+       exit 1
+       ;;
+    esac
+
+    ## Remember the reachability information.
+    addword ifnets_$q $net
+  done
 done
-for net in $allnets6; do
-  case $net in
-    "$defaultiface":*)
+
+## Build the `ifaddr_N' map and an `all-addresses' list.
+alladdrs=!
+trace "ifmap = $ifmap"
+for entry in $ifmap; do
+  iface=${entry%=*} q=${entry##*=}
+  eval nets=\$ifnets_$q
+  aa=!
+  for n in $nets; do
+    eval "addrs=\"\$net_inet_$n \$net_inet6_$n\""
+    trace "$iface $n addrs = $addrs"
+    for a in $addrs; do
+      case $aa in *!$a!*) ;; *) aa=$aa$a! ;; esac
+      case $alladdrs in *!$a!*) ;; *) alladdrs=$alladdrs$a! ;; esac
+    done
+  done
+  eval ifaddrs_$q=\$aa
+  trace "iface $q = $iface; nets = $nets; addrs = $aa"
+done
+trace "alladdrs = $alladdrs"
+
+## Populate the `out-classify' chain, matching networks.
+prepare_to () { mode=goto fail=mark-to-$net_class_default; }
+matchnets -d mark-to : prepare_to out-classify "" 0 $allnets
+
+## A `finish' hook for rejecting known address ranges arriving on a
+## default-reachable interface.
+finish_from_default () {
+  q=$1 chain=$2
+  eval addrs=\$ifaddrs_$q
+
+  for n in $allnets; do
+    eval addr=\$net_inet_$n addr6=\$net_inet6_$n
+    for a in $addr; do
+      case $a in !*) continue ;; esac
+      case $addrs in *"!$a!"*) continue ;; esac
+      run iptables -t mangle -A $chain -s $a -g bad-source-address
+    done
+    for a in $addr6; do
+      case $a in !*) continue ;; esac
+      case $addrs in *"!$a!"*) continue ;; esac
+      run ip6tables -t mangle -A $chain -s $a -g bad-source-address
+    done
+  done
+  run ip46tables -t mangle -A $chain -g in-default
+}
+
+## A `prepare' hook for input classification.  If the interface is
+## default-reachable, then we need to reject known address ranges before
+## dispatching to the default chain; otherwise just reject the packet.
+prepare_from () {
+  q=$1 flags=$2
+  case $flags in
+    *:default:*) mode=call finish="finish_from_default $q" ;;
+    *) mode=goto fail=bad-source-address ;;
+  esac
+}
+
+## Populate the `in-IFACE' chains.
+for entry in $ifmap; do
+  iface=${entry%=*} q=${entry##*=}
+  eval nets=\$ifnets_$q
+
+  case $iface in
+    *-+)
+      ## A special marker indicating a collection of point-to-point
+      ## interfaces.  We should match an address to a particular interface.
+      chains=""
+      for net in $nets; do
+       eval hosts=\$net_hosts_$net class=\$net_class_$net
+       for host in $hosts; do
+         eval ha=\$host_inet_$host ha6=\$host_inet6_$host
+         trace "$host : $class -> $iface"
+         for a in $ha; do
+           run iptables -t mangle -A in-$iface \
+                   -i ${iface%+}$host -s $a -g mark-from-$class
+         done
+         for a in $ha6; do
+           run ip6tables -t mangle -A in-$iface \
+                   -i ${iface%+}$host -s $a -g mark-from-$class
+         done
+       done
+      done
+      run ip46tables -t mangle -A in-$iface -g bad-source-address
       ;;
     *)
-      run ip6tables -t mangle -A in-$defaultiface \
-             -s ${net#*:} -g bad-source-address
+      matchnets -s mark-from : "prepare_from $q" in-$iface "" 0 $nets
       ;;
   esac
 done
 
-## Fill in the black holes in the network.
+## Fill in the black holes in the network.  Some of these might actually be
+## known networks, so don't fill those in again.  See RFC5735 and RFC4291,
+## and their successors.
 for addr in \
        10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 \
-       127.0.0.0/8 192.0.2.0/24
+       127.0.0.0/8 \
+       192.0.2.0/24 198.51.100.0/24 203.0.113.0/24
 do
+  case $alladdrs in *!$addr!*) continue ;; esac
   run iptables -t mangle -A in-default -s $addr -g bad-source-address
 done
 for addr in \
        fc00::/7 \
-       2001:0db8::/32
+       ::0:0/96 ::ffff:0:0/96 \
+       2001:db8::/32
 do
+  case $alladdrs in *!$addr!*) continue ;; esac
   run ip6tables -t mangle -A in-default -s $addr -g bad-source-address
 done
+run ip46tables -t mangle -A in-default -g mark-from-$net_class_default
 
 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 ip46tables -t mangle -A in-$defaultiface -g mark-from-$defaultclass
-run ip46tables -t mangle -A PREROUTING -j in-classify
-run ip46tables -t mangle -A PREROUTING -j out-classify
+## classification chains to the INPUT and (maybe) FORWARD hooks.
+for iface in $defaultifaces; do
+  run ip46tables -t mangle -A in-$iface -g in-default
+done
+chains="INPUT"
+case $forward in 1) chains="$chains FORWARD" ;; esac
+for c in $chains; do
+  run ip46tables -t mangle -A $c -j in-classify
+  run ip46tables -t mangle -A $c -j out-classify
+done
+
+## Incoming stuff to or from a link-local address is OK.
+run ip46tables -t mangle -A INPUT \
+       -m mark --mark $to_link/$MASK_TO \
+       -j MARK --or-mark $fwd_link
+run ip46tables -t mangle -A INPUT \
+       -m mark --mark $from_link/$MASK_FROM \
+       -j MARK --or-mark $fwd_link
 
 ## Now it's safe to let stuff through.
 for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do