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