classify.m4: Clean up interface map tracing.
[firewall] / classify.m4
CommitLineData
775bd287 1### -*-sh-*-
bfdc045d 2###
20106bbb 3### Classify packets according to source and destination networks.
bfdc045d
MW
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
24m4_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
65clearchain mangle:in-classify mangle:in-default mangle:out-classify
66clearchain mangle:local-source
67
44f95827
MW
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.
71clearchain mangle:bad-destination-address
72BAD_DEST=0xf6f377d2
fb7845a8
MW
73run ip46tables -t mangle -A bad-destination-address \
74 -j MARK --set-mark $BAD_DEST
75run ip46tables -t mangle -A bad-destination-address -j ACCEPT
44f95827 76for i in $inchains; do
fb7845a8 77 run ip46tables -A $i -m mark --mark $BAD_DEST -g bad-destination-address
44f95827
MW
78done
79
bfdc045d
MW
80## Packets over the loopback interface are automatically trusted. All manner
81## of weird stuff happens on lo, and it's best not to second-guess it.
0291d6d5 82run ip46tables -t mangle -A in-classify -i lo -j ACCEPT
bfdc045d 83
429f4314
MW
84## Local broadcast and link-local multicast packets sometimes have bizarre
85## addresses. Don't block them just because of this.
bfdc045d
MW
86run iptables -t mangle -A in-classify -j RETURN \
87 -s 0.0.0.0 -d 255.255.255.255 \
429f4314
MW
88 -p udp
89run iptables -t mangle -A in-classify -j RETURN \
90 -s 0.0.0.0 -d 224.0.0.0/24 \
91 -p udp
bfdc045d
MW
92
93## Since packets with source and destination addresses both local will go
94## over the loopback interface, I shouldn't see a packet from me over any
95## other interface. Except that I will if I sent a broadcast or multicast.
d5214471
MW
96## Allow the broadcasts, and remember not to trust them. There are no
97## broadcast addresses in IPv6 (only link-local multicast)m so we don't have
98## to worry about that.
bfdc045d
MW
99run iptables -t mangle -A local-source -j RETURN \
100 -m addrtype --dst-type BROADCAST
101run iptables -t mangle -A local-source -j RETURN \
102 -m addrtype --dst-type MULTICAST
0291d6d5
MW
103run ip6tables -t mangle -A local-source -j RETURN \
104 -d ff00::/8
105run ip46tables -t mangle -A local-source -g bad-source-address
bfdc045d
MW
106run iptables -t mangle -A in-classify -j local-source \
107 -m addrtype --src-type LOCAL
0291d6d5
MW
108for addr in $host_6addrs; do
109 run ip6tables -t mangle -A in-classify -j local-source \
110 -s $addr
111done
bfdc045d
MW
112
113m4_divert(41)m4_dnl
114## Define the important networks.
115for pass in 1 2; do
116 netclassindex=0
117m4_divert(42)m4_dnl
118done
119
120m4_divert(46)m4_dnl
44f95827
MW
121## Special IPv4 source addresses. Forbid broadcast and multicast sources.
122## Mark the special zero address and link-local addresses as such. (This
123## also matches class-E addresses, which are probably permanently invalid.)
124for i in 0.0.0.0 169.254.0.0/16; do
125 run iptables -t mangle -A in-classify -g mark-from-link -s $i
126done
127run iptables -t mangle -A in-classify -g bad-source-address \
128 -s 224.0.0.0/3
129run iptables -t mangle -A in-classify -g bad-source-address \
130 -m addrtype --src-type BROADCAST \
131
132## Special IPv6 addresses. Format multicast sources, and mark zero and
133## link local addresses.
134for i in :: fe80::/10; do
135 run ip6tables -t mangle -A in-classify -g mark-from-link -s $i
136done
137run ip6tables -t mangle -A in-classify -g bad-source-address \
138 -s ff00::/8
139
140## Special IPv4 destination addresses. The zero address is invalid; mark
141## link-local and recognized broadcast addresses as link-local. We leave
142## multicast for later.
143for i in 0.0.0.0 240.0.0.0/4; do
144 run iptables -t mangle -A out-classify -g bad-destination-address -d $i
145done
146run iptables -t mangle -A out-classify -g mark-to-link -d 169.254.0.0/16
147run iptables -t mangle -A out-classify -g mark-to-link \
148 -m addrtype --dst-type BROADCAST
149
150## Special IPv6 destination addressses. The zero address is again invalid;
151## mark link local addresses. We do multicast later.
152run ip6tables -t mangle -A out-classify -g bad-destination-address \
153 -d ::
154run ip6tables -t mangle -A out-classify -g mark-to-link -d fe80::/10
155
156## Now deal with multicast. Link-local multicast is detected as being
157## link-local, so that we can prevent it being forwarded correctly.
158clearchain mangle:out-classify-mcast
159run iptables -t mangle -A out-classify-mcast -g mark-to-link \
160 -d 224.0.0.0/24
161for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f; do
162 run ip6tables -t mangle -A out-classify-mcast -g mark-to-link \
163 -d ff${i}2::/16
164done
165run ip46tables -t mangle -A out-classify-mcast -g mark-to-mcast
166run iptables -t mangle -A out-classify -g out-classify-mcast \
167 -d 224.0.0.0/4
168run ip6tables -t mangle -A out-classify -g out-classify-mcast \
169 -d ff00::/8
170
beb4f0ee
MW
171## Build the input classification chains. There's one chain `in-IFACE' for
172## each local interface. This chain does a further dispatch on the source
173## address to the appropriate `mark-from-CLASS' chain for the source network
1264e917
MW
174## class. We also build a table mapping interface names to numbers (since
175## the names are so unhelpful).
beb4f0ee 176seen=:
1264e917
MW
177ifq=0
178ifmap=""
beb4f0ee
MW
179for iface in $host_ifaces_<::>FWHOST; do
180 ifname=${iface%=*}
181 case $seen in *:$ifname:*) continue ;; esac
182 seen=$seen$ifname:
1264e917
MW
183 addword ifmap $ifname=$ifq
184 ifq=$(( $ifq + 1 ))
beb4f0ee
MW
185 clearchain mangle:in-$ifname
186 run ip46tables -t mangle -A in-classify -i $ifname -g in-$ifname
187done
188
1264e917
MW
189## We do a first pass over nets first, and then the interfaces which those
190## networks reach. During this pass, we populate the `out-classify' chains,
191## and we also build some lists so that we can do later passes over
192## interfaces first and then reaching networks. This is complicated by
193## interface names being unhelpful.
194##
195## Here are the variables we maintain.
196##
197## ifmap A list of entries IFACE=N mapping interface names to
198## numbers.
199##
200## ifnets_N A space-separated list of networks reaching interface
201## number N. This is used for building the matching
202## chains.
203##
204## ifaddrs_N A bang-separated list of address ranges reaching
205## interface number N. This is used for filtering out
206## known networks if the default network reaches the
207## interface.
bfdc045d 208for net in $allnets; do
beb4f0ee 209
1264e917
MW
210 ## Work through the interfaces that this network reaches.
211 for iface in $(net_interfaces FWHOST $net); do
212 case $iface in -) break ;; esac
213
214 ## Find a sequence number for this interface.
215 q=nil
216 for i in $ifmap; do
217 case "$i" in "$iface"=*) q=${i##*=}; break ;; esac
218 done
219 case $q in
220 nil)
221 echo >&2 "$0 INTERNAL ERROR: missing interface \`$iface'!"
222 exit 1
223 ;;
224 esac
225
226 ## Remember the reachability information.
227 addword ifnets_$q $net
3a68f688 228 done
1264e917
MW
229done
230
231## Build the `ifaddr_N' map and an `all-addresses' list.
232alladdrs=!
233trace "ifmap = $ifmap"
234for entry in $ifmap; do
235 iface=${entry%=*} q=${entry##*=}
236 eval nets=\$ifnets_$q
1264e917
MW
237 aa=!
238 for n in $nets; do
239 eval "addrs=\"\$net_inet_$n \$net_inet6_$n\""
240 trace "$iface $n addrs = $addrs"
241 for a in $addrs; do
242 case $aa in *!$a!*) ;; *) aa=$aa$a! ;; esac
243 case $alladdrs in *!$a!*) ;; *) alladdrs=$alladdrs$a! ;; esac
244 done
3a68f688 245 done
1264e917
MW
246 eval ifaddrs_$q=\$aa
247 trace "iface $q = $iface; nets = $nets; addrs = $aa"
1264e917 248done
78aaac07 249trace "alladdrs = $alladdrs"
beb4f0ee 250
1264e917
MW
251## Populate the `out-classify' chain, matching networks.
252prepare_to () { mode=goto fail=mark-to-$net_class_default; }
253matchnets -d mark-from : prepare_to out-classify "" 0 $allnets
beb4f0ee 254
1264e917
MW
255## A `finish' hook for rejecting known address ranges arriving on a
256## default-reachable interface.
257finish_from_default () {
258 q=$1 chain=$2
259 eval addrs=\$ifaddrs_$q
260
261 for n in $allnets; do
262 eval addr=\$net_inet_$n addr6=\$net_inet6_$n
263 for a in $addr; do
264 case $a in !*) continue ;; esac
265 case $addrs in *"!$a!"*) continue ;; esac
266 run iptables -t mangle -A $chain -s $a -g bad-source-address
267 done
268 for a in $addr6; do
269 case $a in !*) continue ;; esac
270 case $addrs in *"!$a!"*) continue ;; esac
271 run ip6tables -t mangle -A $chain -s $a -g bad-source-address
272 done
273 done
274 run ip46tables -t mangle -A $chain -g in-default
275}
276
277## A `prepare' hook for input classification. If the interface is
278## default-reachable, then we need to reject known address ranges before
279## dispatching to the default chain; otherwise just reject the packet.
280prepare_from () {
281 q=$1 flags=$2
282 case $flags in
283 *:default:*) mode=call finish="finish_from_default $q" ;;
284 *) mode=goto fail=bad-source-address ;;
285 esac
286}
beb4f0ee 287
1264e917
MW
288## Populate the `in-IFACE' chains.
289for entry in $ifmap; do
290 iface=${entry%=*} q=${entry##*=}
291 eval nets=\$ifnets_$q
292
293 case $iface in
294 *-+)
295 ## A special marker indicating a collection of point-to-point
296 ## interfaces. We should match an address to a particular interface.
297 chains=""
298 for net in $nets; do
beb4f0ee
MW
299 eval hosts=\$net_hosts_$net
300 for host in $hosts; do
301 eval ha=\$host_inet_$host ha6=\$host_inet6_$host
302 trace "$host : $class -> $iface"
303 for a in $ha; do
304 run iptables -t mangle -A in-$iface \
305 -i ${iface%+}$host -s $a -g mark-from-$class
beb4f0ee
MW
306 done
307 for a in $ha6; do
308 run ip6tables -t mangle -A in-$iface \
309 -i ${iface%+}$host -s $a -g mark-from-$class
beb4f0ee
MW
310 done
311 done
3a68f688 312 done
1264e917 313 run ip46tables -t mangle -A in-$iface -g bad-source-address
beb4f0ee
MW
314 ;;
315 *)
1264e917 316 matchnets -s mark-from : "prepare_from $q" in-$iface "" 0 $nets
0291d6d5
MW
317 ;;
318 esac
319done
bfdc045d 320
1264e917
MW
321## Fill in the black holes in the network. Some of these might actually be
322## known networks, so don't fill those in again.
bfdc045d
MW
323for addr in \
324 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 \
677ef44e
MW
325 127.0.0.0/8 \
326 192.0.2.0/24 198.51.100.0/24 203.0.113.0/24
bfdc045d 327do
1264e917 328 case $alladdrs in *!$addr!*) continue ;; esac
bfdc045d
MW
329 run iptables -t mangle -A in-default -s $addr -g bad-source-address
330done
0291d6d5
MW
331for addr in \
332 fc00::/7 \
2f863436 333 2001:db8::/32
0291d6d5 334do
1264e917 335 case $alladdrs in *!$addr!*) continue ;; esac
0291d6d5
MW
336 run ip6tables -t mangle -A in-default -s $addr -g bad-source-address
337done
beb4f0ee 338run ip46tables -t mangle -A in-default -g mark-from-$net_class_default
bfdc045d
MW
339
340m4_divert(92)m4_dnl
341## Put the final default decision on the in-default chain, and attach the
342## classification chains to the PREROUTING hook.
3a68f688 343for iface in $defaultifaces; do
beb4f0ee 344 run ip46tables -t mangle -A in-$iface -g in-default
3a68f688 345done
0291d6d5
MW
346run ip46tables -t mangle -A PREROUTING -j in-classify
347run ip46tables -t mangle -A PREROUTING -j out-classify
bfdc045d 348
44f95827
MW
349## Incoming stuff to or from a link-local address is OK.
350run ip46tables -t mangle -A INPUT \
351 -m mark --mark $to_link/$MASK_TO \
352 -j MARK --or-mark $fwd_link
353run ip46tables -t mangle -A INPUT \
354 -m mark --mark $from_link/$MASK_FROM \
355 -j MARK --or-mark $fwd_link
356
bfdc045d
MW
357## Now it's safe to let stuff through.
358for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do
0291d6d5 359 run ip46tables -t mangle -P $i ACCEPT
bfdc045d
MW
360done
361
362m4_divert(-1)
363###----- That's all, folks --------------------------------------------------