+## 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. Forbid 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.