| 1 | ### -*-sh-*- |
| 2 | ### |
| 3 | ### Classify packets according to source and destination networks. |
| 4 | ### |
| 5 | ### (c) 2008 Mark Wooding |
| 6 | ### |
| 7 | |
| 8 | ###----- Licensing notice --------------------------------------------------- |
| 9 | ### |
| 10 | ### This program is free software; you can redistribute it and/or modify |
| 11 | ### it under the terms of the GNU General Public License as published by |
| 12 | ### the Free Software Foundation; either version 2 of the License, or |
| 13 | ### (at your option) any later version. |
| 14 | ### |
| 15 | ### This program is distributed in the hope that it will be useful, |
| 16 | ### but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ### GNU General Public License for more details. |
| 19 | ### |
| 20 | ### You should have received a copy of the GNU General Public License |
| 21 | ### along with this program; if not, write to the Free Software Foundation, |
| 22 | ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 | |
| 24 | m4_divert(40)m4_dnl |
| 25 | ###-------------------------------------------------------------------------- |
| 26 | ### Address classification. |
| 27 | ### |
| 28 | ### The objective of address classification is to work out what kind of |
| 29 | ### networks a packet is travelling between, in order to make filtering |
| 30 | ### decisions easier. |
| 31 | ### |
| 32 | ### Address classification is done in the mangle table, by attaching |
| 33 | ### appropriate marks to the packet. We split the Internet into a number of |
| 34 | ### address classes, and make forwarding decisions based on the classes of |
| 35 | ### the source and destination addresses. |
| 36 | ### |
| 37 | ### The mark word is split into three fields: the FROM and TO fields simply |
| 38 | ### record the source and destination classes numerically; the MASK field is |
| 39 | ### used to determine whether forwarding should occur. There is a mask bit |
| 40 | ### for each address class. Source classification sets mask bits according |
| 41 | ### to the forwarding policy for the source address class. Destination |
| 42 | ### classification clears all of the mask bits except for the one |
| 43 | ### corresponding to the actual destination class. Therefore, forwarding is |
| 44 | ### permitted if and only if the mask bits are not all zero. |
| 45 | ### |
| 46 | ### The mangle chains are arranged as follows. |
| 47 | ### |
| 48 | ### The PREROUTING hook simply invokes in-classify and out-classify chains as |
| 49 | ### subroutines. These will tail-call appropriate classification chains. |
| 50 | ### |
| 51 | ### The in-classify chain is responsible for both source address |
| 52 | ### classification and verifying that the packet arrived from the correct |
| 53 | ### interface. It does an initial dispatch on the source interface, to |
| 54 | ### in-IFACE. The in-IFACE chain dispatches to mark-from-CLASS when it |
| 55 | ### recognizes an address belonging to the CLASS; if no matches succeed, it |
| 56 | ### goes to bad-source-address, which logs a message and drops the packet. |
| 57 | ### The default interface is special. If no explicit matches are found, it |
| 58 | ### dispatches to in-default which forbids a few obviously evil things and |
| 59 | ### finally dispatches to mark-from-untrusted. |
| 60 | ### |
| 61 | ### The out-classify is simpler because it doesn't care about the interface. |
| 62 | ### It simply checks each network range in turn, dispatching to mark-to-CLASS |
| 63 | ### on a match or mark-to-DEFAULT (probably untrusted) if there is no match. |
| 64 | |
| 65 | clearchain mangle:in-classify mangle:in-default mangle:out-classify |
| 66 | clearchain mangle:local-source |
| 67 | |
| 68 | ## Packets over the loopback interface are automatically trusted. All manner |
| 69 | ## of weird stuff happens on lo, and it's best not to second-guess it. |
| 70 | run ip46tables -t mangle -A in-classify -i lo -j ACCEPT |
| 71 | |
| 72 | ## Local broadcast and link-local multicast packets sometimes have bizarre |
| 73 | ## addresses. Don't block them just because of this. |
| 74 | run iptables -t mangle -A in-classify -j RETURN \ |
| 75 | -s 0.0.0.0 -d 255.255.255.255 \ |
| 76 | -p udp |
| 77 | run iptables -t mangle -A in-classify -j RETURN \ |
| 78 | -s 0.0.0.0 -d 224.0.0.0/24 \ |
| 79 | -p udp |
| 80 | |
| 81 | ## Since packets with source and destination addresses both local will go |
| 82 | ## over the loopback interface, I shouldn't see a packet from me over any |
| 83 | ## other interface. Except that I will if I sent a broadcast or multicast. |
| 84 | ## Allow the broadcasts, and remember not to trust them. There are no |
| 85 | ## broadcast addresses in IPv6 (only link-local multicast)m so we don't have |
| 86 | ## to worry about that. |
| 87 | run iptables -t mangle -A local-source -j RETURN \ |
| 88 | -m addrtype --dst-type BROADCAST |
| 89 | run iptables -t mangle -A local-source -j RETURN \ |
| 90 | -m addrtype --dst-type MULTICAST |
| 91 | run ip6tables -t mangle -A local-source -j RETURN \ |
| 92 | -d ff00::/8 |
| 93 | run ip46tables -t mangle -A local-source -g bad-source-address |
| 94 | run iptables -t mangle -A in-classify -j local-source \ |
| 95 | -m addrtype --src-type LOCAL |
| 96 | for addr in $host_6addrs; do |
| 97 | run ip6tables -t mangle -A in-classify -j local-source \ |
| 98 | -s $addr |
| 99 | done |
| 100 | |
| 101 | ## It's not valid to have a multicast address as a packet source: multicast |
| 102 | ## routing is done away from the source, so a multicast address would make |
| 103 | ## this impossible to do. So discard these packets. Also discard class-E |
| 104 | ## IPv4 addresses, since they aren't assigned. |
| 105 | run iptables -t mangle -A in-classify -g bad-source-address \ |
| 106 | -s 224.0.0.0/3 |
| 107 | run ip6tables -t mangle -A in-classify -g bad-source-address \ |
| 108 | -s ff00::/8 |
| 109 | |
| 110 | m4_divert(41)m4_dnl |
| 111 | ## Define the important networks. |
| 112 | for pass in 1 2; do |
| 113 | netclassindex=0 |
| 114 | m4_divert(42)m4_dnl |
| 115 | done |
| 116 | |
| 117 | m4_divert(46)m4_dnl |
| 118 | ## Build the input classification chains. There's one chain `in-IFACE' for |
| 119 | ## each local interface. This chain does a further dispatch on the source |
| 120 | ## address to the appropriate `mark-from-CLASS' chain for the source network |
| 121 | ## class. |
| 122 | seen=: |
| 123 | for iface in $host_ifaces_<::>FWHOST; do |
| 124 | ifname=${iface%=*} |
| 125 | case $seen in *:$ifname:*) continue ;; esac |
| 126 | seen=$seen$ifname: |
| 127 | clearchain mangle:in-$ifname |
| 128 | run ip46tables -t mangle -A in-classify -i $ifname -g in-$ifname |
| 129 | done |
| 130 | |
| 131 | ## Now populate the `in-IFACE' and `out-classify' chains. We iterate over |
| 132 | ## the available networks and add addresses to the appropriate chains. Also, |
| 133 | ## build up a map of which interfaces receive from which address ranged so |
| 134 | ## that we can finish the chains off properly later. This contains entries |
| 135 | ## of the form IFACE=:ADDR:ADDR:...: |
| 136 | ifnets="" |
| 137 | for net in $allnets; do |
| 138 | |
| 139 | ## Determine the addresses and class for this network, and populate the |
| 140 | ## `out-classify' chains. |
| 141 | eval addr=\$net_inet_$net addr6=\$net_inet6_$net class=\$net_class_$net |
| 142 | case $class in virtual) continue ;; esac |
| 143 | trace "$net : $class" |
| 144 | for a in $addr; do |
| 145 | run iptables -t mangle -A out-classify -g mark-to-$class -d $a |
| 146 | done |
| 147 | for a in $addr6; do |
| 148 | run ip6tables -t mangle -A out-classify -g mark-to-$class -d $a |
| 149 | done |
| 150 | |
| 151 | ## Now work through the interfaces. |
| 152 | for iface in $(net_interfaces FWHOST $net); do |
| 153 | nets="" |
| 154 | case $iface in |
| 155 | |
| 156 | -) |
| 157 | ## A special `no interface' marker: we should not receive packets |
| 158 | ## from this network at all. |
| 159 | continue |
| 160 | ;; |
| 161 | |
| 162 | *-+) |
| 163 | ## A special marker indicating a collection of point-to-point |
| 164 | ## interfaces. We should match an address to a particular interface. |
| 165 | ## Later, we'll cap this chain off by rejecting all other traffic. |
| 166 | eval hosts=\$net_hosts_$net |
| 167 | for host in $hosts; do |
| 168 | eval ha=\$host_inet_$host ha6=\$host_inet6_$host |
| 169 | trace "$host : $class -> $iface" |
| 170 | for a in $ha; do |
| 171 | run iptables -t mangle -A in-$iface \ |
| 172 | -i ${iface%+}$host -s $a -g mark-from-$class |
| 173 | nets=$nets$a: |
| 174 | done |
| 175 | for a in $ha6; do |
| 176 | run ip6tables -t mangle -A in-$iface \ |
| 177 | -i ${iface%+}$host -s $a -g mark-from-$class |
| 178 | nets=$nets$a: |
| 179 | done |
| 180 | done |
| 181 | ;; |
| 182 | |
| 183 | *) |
| 184 | ## A normal interface. Classify incoming traffic according to the |
| 185 | ## source address. |
| 186 | trace "$net : $class -> $iface" |
| 187 | for a in $addr; do |
| 188 | run iptables -t mangle -A in-$iface -g mark-from-$class -s $a |
| 189 | nets=$nets$a: |
| 190 | done |
| 191 | for a in $addr6; do |
| 192 | run ip6tables -t mangle -A in-$iface -g mark-from-$class -s $a |
| 193 | nets=$nets$a: |
| 194 | done |
| 195 | case $net in default) nets=${nets}default: ;; esac |
| 196 | ;; |
| 197 | esac |
| 198 | |
| 199 | ## Record that this interface receives traffic from this network. |
| 200 | unset nifnets |
| 201 | foundp=nil |
| 202 | for ifnet in $ifnets; do |
| 203 | case $ifnet in |
| 204 | $iface=*:$net:*) addword nifnets $ifnet; foundp=t ;; |
| 205 | $iface=*) addword nifnets $ifnet$nets; foundp=t ;; |
| 206 | *) addword nifnets $ifnet ;; |
| 207 | esac |
| 208 | done |
| 209 | case $foundp in nil) addword nifnets $iface=:$nets ;; esac |
| 210 | ifnets=$nifnets |
| 211 | |
| 212 | done |
| 213 | done |
| 214 | |
| 215 | ## Wrap up all of the `in-IFACE' chains. A chain which matches the `default' |
| 216 | ## net should have unmatched but known networks blocked off, and then chain |
| 217 | ## onto `in-default'. Other chains should just chain onto |
| 218 | ## `bad-source-address'. |
| 219 | trace "ifnets = $ifnets" |
| 220 | for ifnet in $ifnets; do |
| 221 | iface=${ifnet%%=*} nets=${ifnet#*=} |
| 222 | case $nets in |
| 223 | *:default:*) |
| 224 | for n in $allnets; do |
| 225 | eval addr=\$net_inet_$n addr6=\$net_inet6_$n |
| 226 | for a in $addr; do |
| 227 | case $nets in *:$a:*) continue ;; esac |
| 228 | nets=$nets$a |
| 229 | run iptables -t mangle -A in-$iface -s $a -g bad-source-address |
| 230 | done |
| 231 | for a in $addr6; do |
| 232 | case $nets in *:$a:*) continue ;; esac |
| 233 | nets=$nets$a |
| 234 | run ip6tables -t mangle -A in-$iface -s $a -g bad-source-address |
| 235 | done |
| 236 | done |
| 237 | run ip46tables -t mangle -A in-$iface -g in-default |
| 238 | ;; |
| 239 | *) |
| 240 | run ip46tables -t mangle -A in-$iface -g bad-source-address |
| 241 | ;; |
| 242 | esac |
| 243 | done |
| 244 | |
| 245 | ## Fill in the black holes in the network. |
| 246 | for addr in \ |
| 247 | 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 \ |
| 248 | 127.0.0.0/8 \ |
| 249 | 192.0.2.0/24 198.51.100.0/24 203.0.113.0/24 |
| 250 | do |
| 251 | run iptables -t mangle -A in-default -s $addr -g bad-source-address |
| 252 | done |
| 253 | for addr in \ |
| 254 | fc00::/7 \ |
| 255 | 2001:0db8::/32 |
| 256 | do |
| 257 | run ip6tables -t mangle -A in-default -s $addr -g bad-source-address |
| 258 | done |
| 259 | run ip46tables -t mangle -A in-default -g mark-from-$net_class_default |
| 260 | |
| 261 | m4_divert(92)m4_dnl |
| 262 | ## Put the final default decision on the in-default chain, and attach the |
| 263 | ## classification chains to the PREROUTING hook. |
| 264 | for iface in $defaultifaces; do |
| 265 | run ip46tables -t mangle -A in-$iface -g in-default |
| 266 | done |
| 267 | run ip46tables -t mangle -A out-classify -g mark-to-$net_class_default |
| 268 | run ip46tables -t mangle -A PREROUTING -j in-classify |
| 269 | run ip46tables -t mangle -A PREROUTING -j out-classify |
| 270 | |
| 271 | ## Now it's safe to let stuff through. |
| 272 | for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do |
| 273 | run ip46tables -t mangle -P $i ACCEPT |
| 274 | done |
| 275 | |
| 276 | m4_divert(-1) |
| 277 | ###----- That's all, folks -------------------------------------------------- |