Overhaul address classification for link-local and non-unicast addresses.
[firewall] / classify.m4
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 ## An unpleasant hack. We can't reject packets from the mangle table, so
69 ## we mark packets with a bad destination and then detect this in the
70 ## filter table.
71 clearchain mangle:bad-destination-address
72 BAD_DEST=0xf6f377d2
73 ip46tables -t mangle -A bad-destination-address -j MARK --set-mark $BAD_DEST
74 ip46tables -t mangle -A bad-destination-address -j ACCEPT
75 for i in $inchains; do
76 ip46tables -A $i -m mark --mark $BAD_DEST -g bad-destination-address
77 done
78
79 ## Packets over the loopback interface are automatically trusted. All manner
80 ## of weird stuff happens on lo, and it's best not to second-guess it.
81 run ip46tables -t mangle -A in-classify -i lo -j ACCEPT
82
83 ## Local broadcast and link-local multicast packets sometimes have bizarre
84 ## addresses. Don't block them just because of this.
85 run iptables -t mangle -A in-classify -j RETURN \
86 -s 0.0.0.0 -d 255.255.255.255 \
87 -p udp
88 run iptables -t mangle -A in-classify -j RETURN \
89 -s 0.0.0.0 -d 224.0.0.0/24 \
90 -p udp
91
92 ## Since packets with source and destination addresses both local will go
93 ## over the loopback interface, I shouldn't see a packet from me over any
94 ## other interface. Except that I will if I sent a broadcast or multicast.
95 ## Allow the broadcasts, and remember not to trust them. There are no
96 ## broadcast addresses in IPv6 (only link-local multicast)m so we don't have
97 ## to worry about that.
98 run iptables -t mangle -A local-source -j RETURN \
99 -m addrtype --dst-type BROADCAST
100 run iptables -t mangle -A local-source -j RETURN \
101 -m addrtype --dst-type MULTICAST
102 run ip6tables -t mangle -A local-source -j RETURN \
103 -d ff00::/8
104 run ip46tables -t mangle -A local-source -g bad-source-address
105 run iptables -t mangle -A in-classify -j local-source \
106 -m addrtype --src-type LOCAL
107 for addr in $host_6addrs; do
108 run ip6tables -t mangle -A in-classify -j local-source \
109 -s $addr
110 done
111
112 m4_divert(41)m4_dnl
113 ## Define the important networks.
114 for pass in 1 2; do
115 netclassindex=0
116 m4_divert(42)m4_dnl
117 done
118
119 m4_divert(46)m4_dnl
120 ## Special IPv4 source addresses. Forbid broadcast and multicast sources.
121 ## Mark the special zero address and link-local addresses as such. (This
122 ## also matches class-E addresses, which are probably permanently invalid.)
123 for i in 0.0.0.0 169.254.0.0/16; do
124 run iptables -t mangle -A in-classify -g mark-from-link -s $i
125 done
126 run iptables -t mangle -A in-classify -g bad-source-address \
127 -s 224.0.0.0/3
128 run iptables -t mangle -A in-classify -g bad-source-address \
129 -m addrtype --src-type BROADCAST \
130
131 ## Special IPv6 addresses. Format multicast sources, and mark zero and
132 ## link local addresses.
133 for i in :: fe80::/10; do
134 run ip6tables -t mangle -A in-classify -g mark-from-link -s $i
135 done
136 run ip6tables -t mangle -A in-classify -g bad-source-address \
137 -s ff00::/8
138
139 ## Special IPv4 destination addresses. The zero address is invalid; mark
140 ## link-local and recognized broadcast addresses as link-local. We leave
141 ## multicast for later.
142 for i in 0.0.0.0 240.0.0.0/4; do
143 run iptables -t mangle -A out-classify -g bad-destination-address -d $i
144 done
145 run iptables -t mangle -A out-classify -g mark-to-link -d 169.254.0.0/16
146 run iptables -t mangle -A out-classify -g mark-to-link \
147 -m addrtype --dst-type BROADCAST
148
149 ## Special IPv6 destination addressses. The zero address is again invalid;
150 ## mark link local addresses. We do multicast later.
151 run ip6tables -t mangle -A out-classify -g bad-destination-address \
152 -d ::
153 run ip6tables -t mangle -A out-classify -g mark-to-link -d fe80::/10
154
155 ## Now deal with multicast. Link-local multicast is detected as being
156 ## link-local, so that we can prevent it being forwarded correctly.
157 clearchain mangle:out-classify-mcast
158 run iptables -t mangle -A out-classify-mcast -g mark-to-link \
159 -d 224.0.0.0/24
160 for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
161 run ip6tables -t mangle -A out-classify-mcast -g mark-to-link \
162 -d ff${i}2::/16
163 done
164 run ip46tables -t mangle -A out-classify-mcast -g mark-to-mcast
165 run iptables -t mangle -A out-classify -g out-classify-mcast \
166 -d 224.0.0.0/4
167 run ip6tables -t mangle -A out-classify -g out-classify-mcast \
168 -d ff00::/8
169
170 ## Build the input classification chains. There's one chain `in-IFACE' for
171 ## each local interface. This chain does a further dispatch on the source
172 ## address to the appropriate `mark-from-CLASS' chain for the source network
173 ## class.
174 seen=:
175 for iface in $host_ifaces_<::>FWHOST; do
176 ifname=${iface%=*}
177 case $seen in *:$ifname:*) continue ;; esac
178 seen=$seen$ifname:
179 clearchain mangle:in-$ifname
180 run ip46tables -t mangle -A in-classify -i $ifname -g in-$ifname
181 done
182
183 ## Now populate the `in-IFACE' and `out-classify' chains. We iterate over
184 ## the available networks and add addresses to the appropriate chains. Also,
185 ## build up a map of which interfaces receive from which address ranged so
186 ## that we can finish the chains off properly later. This contains entries
187 ## of the form IFACE=:ADDR:ADDR:...:
188 ifnets=""
189 for net in $allnets; do
190
191 ## Determine the addresses and class for this network, and populate the
192 ## `out-classify' chains.
193 eval addr=\$net_inet_$net addr6=\$net_inet6_$net class=\$net_class_$net
194 case $class in virtual) continue ;; esac
195 trace "$net : $class"
196 for a in $addr; do
197 run iptables -t mangle -A out-classify -g mark-to-$class -d $a
198 done
199 for a in $addr6; do
200 run ip6tables -t mangle -A out-classify -g mark-to-$class -d $a
201 done
202
203 ## Now work through the interfaces.
204 for iface in $(net_interfaces FWHOST $net); do
205 nets=""
206 case $iface in
207
208 -)
209 ## A special `no interface' marker: we should not receive packets
210 ## from this network at all.
211 continue
212 ;;
213
214 *-+)
215 ## A special marker indicating a collection of point-to-point
216 ## interfaces. We should match an address to a particular interface.
217 ## Later, we'll cap this chain off by rejecting all other traffic.
218 eval hosts=\$net_hosts_$net
219 for host in $hosts; do
220 eval ha=\$host_inet_$host ha6=\$host_inet6_$host
221 trace "$host : $class -> $iface"
222 for a in $ha; do
223 run iptables -t mangle -A in-$iface \
224 -i ${iface%+}$host -s $a -g mark-from-$class
225 nets=$nets$a:
226 done
227 for a in $ha6; do
228 run ip6tables -t mangle -A in-$iface \
229 -i ${iface%+}$host -s $a -g mark-from-$class
230 nets=$nets$a:
231 done
232 done
233 ;;
234
235 *)
236 ## A normal interface. Classify incoming traffic according to the
237 ## source address.
238 trace "$net : $class -> $iface"
239 for a in $addr; do
240 run iptables -t mangle -A in-$iface -g mark-from-$class -s $a
241 nets=$nets$a:
242 done
243 for a in $addr6; do
244 run ip6tables -t mangle -A in-$iface -g mark-from-$class -s $a
245 nets=$nets$a:
246 done
247 case $net in default) nets=${nets}default: ;; esac
248 ;;
249 esac
250
251 ## Record that this interface receives traffic from this network.
252 unset nifnets
253 foundp=nil
254 for ifnet in $ifnets; do
255 case $ifnet in
256 $iface=*:$net:*) addword nifnets $ifnet; foundp=t ;;
257 $iface=*) addword nifnets $ifnet$nets; foundp=t ;;
258 *) addword nifnets $ifnet ;;
259 esac
260 done
261 case $foundp in nil) addword nifnets $iface=:$nets ;; esac
262 ifnets=$nifnets
263
264 done
265 done
266
267 ## Wrap up all of the `in-IFACE' chains. A chain which matches the `default'
268 ## net should have unmatched but known networks blocked off, and then chain
269 ## onto `in-default'. Other chains should just chain onto
270 ## `bad-source-address'.
271 trace "ifnets = $ifnets"
272 for ifnet in $ifnets; do
273 iface=${ifnet%%=*} nets=${ifnet#*=}
274 case $nets in
275 *:default:*)
276 for n in $allnets; do
277 eval addr=\$net_inet_$n addr6=\$net_inet6_$n
278 for a in $addr; do
279 case $nets in *:$a:*) continue ;; esac
280 nets=$nets$a
281 run iptables -t mangle -A in-$iface -s $a -g bad-source-address
282 done
283 for a in $addr6; do
284 case $nets in *:$a:*) continue ;; esac
285 nets=$nets$a
286 run ip6tables -t mangle -A in-$iface -s $a -g bad-source-address
287 done
288 done
289 run ip46tables -t mangle -A in-$iface -g in-default
290 ;;
291 *)
292 run ip46tables -t mangle -A in-$iface -g bad-source-address
293 ;;
294 esac
295 done
296
297 ## Fill in the black holes in the network.
298 for addr in \
299 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 \
300 127.0.0.0/8 \
301 192.0.2.0/24 198.51.100.0/24 203.0.113.0/24
302 do
303 run iptables -t mangle -A in-default -s $addr -g bad-source-address
304 done
305 for addr in \
306 fc00::/7 \
307 2001:db8::/32
308 do
309 run ip6tables -t mangle -A in-default -s $addr -g bad-source-address
310 done
311 run ip46tables -t mangle -A in-default -g mark-from-$net_class_default
312
313 m4_divert(92)m4_dnl
314 ## Put the final default decision on the in-default chain, and attach the
315 ## classification chains to the PREROUTING hook.
316 for iface in $defaultifaces; do
317 run ip46tables -t mangle -A in-$iface -g in-default
318 done
319 run ip46tables -t mangle -A out-classify -g mark-to-$net_class_default
320 run ip46tables -t mangle -A PREROUTING -j in-classify
321 run ip46tables -t mangle -A PREROUTING -j out-classify
322
323 ## Incoming stuff to or from a link-local address is OK.
324 run ip46tables -t mangle -A INPUT \
325 -m mark --mark $to_link/$MASK_TO \
326 -j MARK --or-mark $fwd_link
327 run ip46tables -t mangle -A INPUT \
328 -m mark --mark $from_link/$MASK_FROM \
329 -j MARK --or-mark $fwd_link
330
331 ## Now it's safe to let stuff through.
332 for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do
333 run ip46tables -t mangle -P $i ACCEPT
334 done
335
336 m4_divert(-1)
337 ###----- That's all, folks --------------------------------------------------