### -*-sh-*- ### ### Classify packets according to source and destination networks. ### ### (c) 2008 Mark Wooding ### ###----- Licensing notice --------------------------------------------------- ### ### This program is free software; you can redistribute it and/or modify ### it under the terms of the GNU General Public License as published by ### the Free Software Foundation; either version 2 of the License, or ### (at your option) any later version. ### ### This program is distributed in the hope that it will be useful, ### but WITHOUT ANY WARRANTY; without even the implied warranty of ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ### GNU General Public License for more details. ### ### You should have received a copy of the GNU General Public License ### along with this program; if not, write to the Free Software Foundation, ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. m4_divert(40)m4_dnl ###-------------------------------------------------------------------------- ### Address classification. ### ### The objective of address classification is to work out what kind of ### networks a packet is travelling between, in order to make filtering ### decisions easier. ### ### Address classification is done in the mangle table, by attaching ### appropriate marks to the packet. We split the Internet into a number of ### address classes, and make forwarding decisions based on the classes of ### the source and destination addresses. ### ### The mark word is split into three fields: the FROM and TO fields simply ### record the source and destination classes numerically; the MASK field is ### used to determine whether forwarding should occur. There is a mask bit ### for each address class. Source classification sets mask bits according ### to the forwarding policy for the source address class. Destination ### classification clears all of the mask bits except for the one ### corresponding to the actual destination class. Therefore, forwarding is ### permitted if and only if the mask bits are not all zero. ### ### 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 in-classify chain is responsible for both source address ### classification and verifying that the packet arrived from the correct ### interface. It does an initial dispatch on the source interface, to ### in-IFACE. The in-IFACE chain dispatches to mark-from-CLASS when it ### recognizes an address belonging to the CLASS; if no matches succeed, it ### 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. ### ### 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. clearchain mangle:in-classify mangle:in-default mangle:out-classify 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 ip46tables -t mangle -A in-classify -i lo -j ACCEPT ## 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 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. 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 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. for pass in 1 2; do netclassindex=0 m4_divert(42)m4_dnl done m4_divert(46)m4_dnl ## 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 ## 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 ## 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 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 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 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. 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 ## Now it's safe to let stuff through. for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do run ip46tables -t mangle -P $i ACCEPT done m4_divert(-1) ###----- That's all, folks --------------------------------------------------