Overhaul address classification for link-local and non-unicast addresses.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 11 Mar 2012 23:58:09 +0000 (23:58 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 12 Mar 2012 00:13:28 +0000 (00:13 +0000)
The previous attempts just weren't working.  Intead, assign them their
own classes, and work things using the forwarding masks.  There's a
minor wrinkle, that we must handle forwarded packets differently from
inbound ones if they involve link-local addresses, but this is handled
with a fixup in the mangle INPUT chain.

The other significant change here is that the mangle table is now
responsible for selecting packets with bogus destination addresses for
rejection -- though it can't do the rejection itself because of a
kernel restriction.

classify.m4
icmp.m4
local.m4

index 606d90d..59ac3d9 100644 (file)
@@ -65,6 +65,17 @@ m4_divert(40)m4_dnl
 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
+ip46tables -t mangle -A bad-destination-address -j MARK --set-mark $BAD_DEST
+ip46tables -t mangle -A bad-destination-address -j ACCEPT
+for i in $inchains; do
+  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
@@ -98,15 +109,6 @@ for addr in $host_6addrs; do
          -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.
 for pass in 1 2; do
@@ -115,6 +117,56 @@ m4_divert(42)m4_dnl
 done
 
 m4_divert(46)m4_dnl
+## 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
@@ -268,6 +320,14 @@ run ip46tables -t mangle -A out-classify -g mark-to-$net_class_default
 run ip46tables -t mangle -A PREROUTING -j in-classify
 run ip46tables -t mangle -A PREROUTING -j out-classify
 
+## 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
   run ip46tables -t mangle -P $i ACCEPT
diff --git a/icmp.m4 b/icmp.m4
index 460c838..93c2973 100644 (file)
--- a/icmp.m4
+++ b/icmp.m4
@@ -33,21 +33,6 @@ for type in echo-request echo-reply; do
   run ip6tables -A check-icmp -p icmpv6 --icmpv6-type $type -j RETURN
 done
 
-## Certainly don't allow ping to broadcast or multicast addresses.
-case $forward in
-  1)
-    run iptables -A FORWARD -g forbidden \
-           -p icmp --icmp-type echo-request \
-           -m addrtype --dst-type BROADCAST
-    run iptables -A FORWARD -g forbidden \
-           -p icmp --icmp-type echo-request \
-           -d 224.0.0.0/8
-    run ip6tables -A FORWARD -g forbidden \
-           -p icmpv6 --icmpv6-type echo-request \
-           -d ff00::/16
-    ;;
-esac
-
 m4_divert(58)m4_dnl
 ## Other ICMP is basically benign, we claim.
 run ip46tables -A check-icmp -j ACCEPT
index 251cf59..d084db4 100644 (file)
--- a/local.m4
+++ b/local.m4
@@ -35,10 +35,12 @@ m4_divert(-1)
 
 ## Define the available network classes.
 m4_divert(42)m4_dnl
-defnetclass untrusted untrusted trusted
-defnetclass trusted untrusted trusted safe noloop
-defnetclass safe trusted safe noloop
-defnetclass noloop trusted safe
+defnetclass untrusted untrusted trusted mcast
+defnetclass trusted untrusted trusted safe noloop mcast
+defnetclass safe trusted safe noloop mcast
+defnetclass noloop trusted safe mcast
+defnetclass link
+defnetclass mcast
 m4_divert(-1)
 
 m4_divert(26)m4_dnl
@@ -236,22 +238,6 @@ run iptables -A inbound -j ACCEPT \
        -s 172.29.198.0/23 \
        -p udp --source-port $port_bootpc --destination-port $port_bootps
 
-## Incoming multicast on a network interface associated with a trusted
-## network is OK, since it must have originated there (or been forwarded, but
-## we don't do that yet).
-seen=:-:
-for net in $allnets; do
-  eval class=\$net_class_$net
-  case $class in trusted) ;; *) continue ;; esac
-  for iface in $(net_interfaces FWHOST $net); do
-    case "$seen" in *:$iface:*) continue ;; esac
-    seen=$seen$iface:
-    run iptables -A inbound -j ACCEPT \
-       -s 0.0.0.0 -d 224.0.0.0/24 \
-       -i $iface
-  done
-done
-
 ## Allow incoming ping.  This is the only ICMP left.
 run ip46tables -A inbound -j ACCEPT -p icmp