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
-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
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.
+seen=:
+for iface in $host_ifaces_<::>FWHOST; do
+ ifname=${iface%=*}
+ case $seen in *:$ifname:*) continue ;; esac
+ seen=$seen$ifname:
+ clearchain mangle:in-$ifname
+ run ip46tables -t mangle -A in-classify -i $ifname -g in-$ifname
+done
+
+## Now populate the `in-IFACE' and `out-classify' chains. We iterate over
+## the available networks and add addresses to the appropriate chains. Also,
+## build up a map of which interfaces receive from which address ranged so
+## that we can finish the chains off properly later. This contains entries
+## of the form IFACE=:ADDR:ADDR:...:
+ifnets=""
for net in $allnets; do
- case $net in
- "$defaultiface":*)
- ;;
- *)
- run iptables -t mangle -A in-$defaultiface \
- -s ${net#*:} -g bad-source-address
- ;;
- esac
+
+ ## Determine the addresses and class for this network, and populate the
+ ## `out-classify' chains.
+ eval addr=\$net_inet_$net addr6=\$net_inet6_$net class=\$net_class_$net
+ case $class in virtual) continue ;; esac
+ trace "$net : $class"
+ for a in $addr; do
+ run iptables -t mangle -A out-classify -g mark-to-$class -d $a
+ done
+ for a in $addr6; do
+ run ip6tables -t mangle -A out-classify -g mark-to-$class -d $a
+ done
+
+ ## Now work through the interfaces.
+ for iface in $(net_interfaces FWHOST $net); do
+ nets=""
+ case $iface in
+
+ -)
+ ## A special `no interface' marker: we should not receive packets
+ ## from this network at all.
+ continue
+ ;;
+
+ *-+)
+ ## A special marker indicating a collection of point-to-point
+ ## interfaces. We should match an address to a particular interface.
+ ## Later, we'll cap this chain off by rejecting all other traffic.
+ eval hosts=\$net_hosts_$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
+ nets=$nets$a:
+ done
+ for a in $ha6; do
+ run ip6tables -t mangle -A in-$iface \
+ -i ${iface%+}$host -s $a -g mark-from-$class
+ nets=$nets$a:
+ done
+ done
+ ;;
+
+ *)
+ ## A normal interface. Classify incoming traffic according to the
+ ## source address.
+ trace "$net : $class -> $iface"
+ for a in $addr; do
+ run iptables -t mangle -A in-$iface -g mark-from-$class -s $a
+ nets=$nets$a:
+ done
+ for a in $addr6; do
+ run ip6tables -t mangle -A in-$iface -g mark-from-$class -s $a
+ nets=$nets$a:
+ done
+ case $net in default) nets=${nets}default: ;; esac
+ ;;
+ esac
+
+ ## Record that this interface receives traffic from this network.
+ unset nifnets
+ foundp=nil
+ for ifnet in $ifnets; do
+ case $ifnet in
+ $iface=*:$net:*) addword nifnets $ifnet; foundp=t ;;
+ $iface=*) addword nifnets $ifnet$nets; foundp=t ;;
+ *) addword nifnets $ifnet ;;
+ esac
+ done
+ case $foundp in nil) addword nifnets $iface=:$nets ;; esac
+ ifnets=$nifnets
+
+ done
done
-for net in $allnets6; do
- case $net in
- "$defaultiface":*)
+
+## Wrap up all of the `in-IFACE' chains. A chain which matches the `default'
+## net should have unmatched but known networks blocked off, and then chain
+## onto `in-default'. Other chains should just chain onto
+## `bad-source-address'.
+trace "ifnets = $ifnets"
+for ifnet in $ifnets; do
+ iface=${ifnet%%=*} nets=${ifnet#*=}
+ case $nets in
+ *:default:*)
+ for n in $allnets; do
+ eval addr=\$net_inet_$n addr6=\$net_inet6_$n
+ for a in $addr; do
+ case $nets in *:$a:*) continue ;; esac
+ nets=$nets$a
+ run iptables -t mangle -A in-$iface -s $a -g bad-source-address
+ done
+ for a in $addr6; do
+ case $nets in *:$a:*) continue ;; esac
+ nets=$nets$a
+ run ip6tables -t mangle -A in-$iface -s $a -g bad-source-address
+ done
+ done
+ run ip46tables -t mangle -A in-$iface -g in-default
;;
*)
- run ip6tables -t mangle -A in-$defaultiface \
- -s ${net#*:} -g bad-source-address
+ run ip46tables -t mangle -A in-$iface -g bad-source-address
;;
esac
done
## Fill in the black holes in the network.
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
run iptables -t mangle -A in-default -s $addr -g bad-source-address
done
for addr in \
fc00::/7 \
- 2001:0db8::/32
+ 2001:db8::/32
do
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
+for iface in $defaultifaces; do
+ run ip46tables -t mangle -A in-$iface -g in-default
+done
+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