Make FW_NOACT work properly.
[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 run ip46tables -t mangle -A bad-destination-address \
74 -j MARK --set-mark $BAD_DEST
75 run ip46tables -t mangle -A bad-destination-address -j ACCEPT
76 for i in $inchains; do
77 run ip46tables -A $i -m mark --mark $BAD_DEST -g bad-destination-address
78 done
79
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.
82 run ip46tables -t mangle -A in-classify -i lo -j ACCEPT
83
84 ## Local broadcast and link-local multicast packets sometimes have bizarre
85 ## addresses. Don't block them just because of this.
86 run iptables -t mangle -A in-classify -j RETURN \
87 -s 0.0.0.0 -d 255.255.255.255 \
88 -p udp
89 run iptables -t mangle -A in-classify -j RETURN \
90 -s 0.0.0.0 -d 224.0.0.0/24 \
91 -p udp
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.
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.
99 run iptables -t mangle -A local-source -j RETURN \
100 -m addrtype --dst-type BROADCAST
101 run iptables -t mangle -A local-source -j RETURN \
102 -m addrtype --dst-type MULTICAST
103 run ip6tables -t mangle -A local-source -j RETURN \
104 -d ff00::/8
105 run ip46tables -t mangle -A local-source -g bad-source-address
106 run iptables -t mangle -A in-classify -j local-source \
107 -m addrtype --src-type LOCAL
108 for addr in $host_6addrs; do
109 run ip6tables -t mangle -A in-classify -j local-source \
110 -s $addr
111 done
112
113 m4_divert(41)m4_dnl
114 ## Define the important networks.
115 for pass in 1 2; do
116 netclassindex=0
117 m4_divert(42)m4_dnl
118 done
119
120 m4_divert(46)m4_dnl
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.)
124 for 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
126 done
127 run iptables -t mangle -A in-classify -g bad-source-address \
128 -s 224.0.0.0/3
129 run 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.
134 for i in :: fe80::/10; do
135 run ip6tables -t mangle -A in-classify -g mark-from-link -s $i
136 done
137 run 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.
143 for 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
145 done
146 run iptables -t mangle -A out-classify -g mark-to-link -d 169.254.0.0/16
147 run 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.
152 run ip6tables -t mangle -A out-classify -g bad-destination-address \
153 -d ::
154 run 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.
158 clearchain mangle:out-classify-mcast
159 run iptables -t mangle -A out-classify-mcast -g mark-to-link \
160 -d 224.0.0.0/24
161 for 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
164 done
165 run ip46tables -t mangle -A out-classify-mcast -g mark-to-mcast
166 run iptables -t mangle -A out-classify -g out-classify-mcast \
167 -d 224.0.0.0/4
168 run ip6tables -t mangle -A out-classify -g out-classify-mcast \
169 -d ff00::/8
170
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
174 ## class.
175 seen=:
176 for iface in $host_ifaces_<::>FWHOST; do
177 ifname=${iface%=*}
178 case $seen in *:$ifname:*) continue ;; esac
179 seen=$seen$ifname:
180 clearchain mangle:in-$ifname
181 run ip46tables -t mangle -A in-classify -i $ifname -g in-$ifname
182 done
183
184 ## Now populate the `in-IFACE' and `out-classify' chains. We iterate over
185 ## the available networks and add addresses to the appropriate chains. Also,
186 ## build up a map of which interfaces receive from which address ranged so
187 ## that we can finish the chains off properly later. This contains entries
188 ## of the form IFACE=:ADDR:ADDR:...:
189 ifnets=""
190 for net in $allnets; do
191
192 ## Determine the addresses and class for this network, and populate the
193 ## `out-classify' chains.
194 eval addr=\$net_inet_$net addr6=\$net_inet6_$net class=\$net_class_$net
195 case $class in virtual) continue ;; esac
196 trace "$net : $class"
197 for a in $addr; do
198 run iptables -t mangle -A out-classify -g mark-to-$class -d $a
199 done
200 for a in $addr6; do
201 run ip6tables -t mangle -A out-classify -g mark-to-$class -d $a
202 done
203
204 ## Now work through the interfaces.
205 for iface in $(net_interfaces FWHOST $net); do
206 nets=""
207 case $iface in
208
209 -)
210 ## A special `no interface' marker: we should not receive packets
211 ## from this network at all.
212 continue
213 ;;
214
215 *-+)
216 ## A special marker indicating a collection of point-to-point
217 ## interfaces. We should match an address to a particular interface.
218 ## Later, we'll cap this chain off by rejecting all other traffic.
219 eval hosts=\$net_hosts_$net
220 for host in $hosts; do
221 eval ha=\$host_inet_$host ha6=\$host_inet6_$host
222 trace "$host : $class -> $iface"
223 for a in $ha; do
224 run iptables -t mangle -A in-$iface \
225 -i ${iface%+}$host -s $a -g mark-from-$class
226 nets=$nets$a:
227 done
228 for a in $ha6; do
229 run ip6tables -t mangle -A in-$iface \
230 -i ${iface%+}$host -s $a -g mark-from-$class
231 nets=$nets$a:
232 done
233 done
234 ;;
235
236 *)
237 ## A normal interface. Classify incoming traffic according to the
238 ## source address.
239 trace "$net : $class -> $iface"
240 for a in $addr; do
241 run iptables -t mangle -A in-$iface -g mark-from-$class -s $a
242 nets=$nets$a:
243 done
244 for a in $addr6; do
245 run ip6tables -t mangle -A in-$iface -g mark-from-$class -s $a
246 nets=$nets$a:
247 done
248 case $net in default) nets=${nets}default: ;; esac
249 ;;
250 esac
251
252 ## Record that this interface receives traffic from this network.
253 unset nifnets
254 foundp=nil
255 for ifnet in $ifnets; do
256 case $ifnet in
257 $iface=*:$net:*) addword nifnets $ifnet; foundp=t ;;
258 $iface=*) addword nifnets $ifnet$nets; foundp=t ;;
259 *) addword nifnets $ifnet ;;
260 esac
261 done
262 case $foundp in nil) addword nifnets $iface=:$nets ;; esac
263 ifnets=$nifnets
264
265 done
266 done
267
268 ## Wrap up all of the `in-IFACE' chains. A chain which matches the `default'
269 ## net should have unmatched but known networks blocked off, and then chain
270 ## onto `in-default'. Other chains should just chain onto
271 ## `bad-source-address'.
272 trace "ifnets = $ifnets"
273 for ifnet in $ifnets; do
274 iface=${ifnet%%=*} nets=${ifnet#*=}
275 case $nets in
276 *:default:*)
277 for n in $allnets; do
278 eval addr=\$net_inet_$n addr6=\$net_inet6_$n
279 for a in $addr; do
280 case $nets in *:$a:*) continue ;; esac
281 nets=$nets$a
282 run iptables -t mangle -A in-$iface -s $a -g bad-source-address
283 done
284 for a in $addr6; do
285 case $nets in *:$a:*) continue ;; esac
286 nets=$nets$a
287 run ip6tables -t mangle -A in-$iface -s $a -g bad-source-address
288 done
289 done
290 run ip46tables -t mangle -A in-$iface -g in-default
291 ;;
292 *)
293 run ip46tables -t mangle -A in-$iface -g bad-source-address
294 ;;
295 esac
296 done
297
298 ## Fill in the black holes in the network.
299 for addr in \
300 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 \
301 127.0.0.0/8 \
302 192.0.2.0/24 198.51.100.0/24 203.0.113.0/24
303 do
304 run iptables -t mangle -A in-default -s $addr -g bad-source-address
305 done
306 for addr in \
307 fc00::/7 \
308 2001:db8::/32
309 do
310 run ip6tables -t mangle -A in-default -s $addr -g bad-source-address
311 done
312 run ip46tables -t mangle -A in-default -g mark-from-$net_class_default
313
314 m4_divert(92)m4_dnl
315 ## Put the final default decision on the in-default chain, and attach the
316 ## classification chains to the PREROUTING hook.
317 for iface in $defaultifaces; do
318 run ip46tables -t mangle -A in-$iface -g in-default
319 done
320 run ip46tables -t mangle -A out-classify -g mark-to-$net_class_default
321 run ip46tables -t mangle -A PREROUTING -j in-classify
322 run ip46tables -t mangle -A PREROUTING -j out-classify
323
324 ## Incoming stuff to or from a link-local address is OK.
325 run ip46tables -t mangle -A INPUT \
326 -m mark --mark $to_link/$MASK_TO \
327 -j MARK --or-mark $fwd_link
328 run ip46tables -t mangle -A INPUT \
329 -m mark --mark $from_link/$MASK_FROM \
330 -j MARK --or-mark $fwd_link
331
332 ## Now it's safe to let stuff through.
333 for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do
334 run ip46tables -t mangle -P $i ACCEPT
335 done
336
337 m4_divert(-1)
338 ###----- That's all, folks --------------------------------------------------