Commit | Line | Data |
---|---|---|
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 | ||
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. | |
0291d6d5 | 70 | run ip46tables -t mangle -A in-classify -i lo -j ACCEPT |
bfdc045d MW |
71 | |
72 | ## Local bootp packets have bizarre addresses. Don't block them just because | |
73 | ## 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 --source-port $port_bootpc --destination-port $port_bootps | |
77 | ||
78 | ## Since packets with source and destination addresses both local will go | |
79 | ## over the loopback interface, I shouldn't see a packet from me over any | |
80 | ## other interface. Except that I will if I sent a broadcast or multicast. | |
d5214471 MW |
81 | ## Allow the broadcasts, and remember not to trust them. There are no |
82 | ## broadcast addresses in IPv6 (only link-local multicast)m so we don't have | |
83 | ## to worry about that. | |
bfdc045d MW |
84 | run iptables -t mangle -A local-source -j RETURN \ |
85 | -m addrtype --dst-type BROADCAST | |
86 | run iptables -t mangle -A local-source -j RETURN \ | |
87 | -m addrtype --dst-type MULTICAST | |
0291d6d5 MW |
88 | run ip6tables -t mangle -A local-source -j RETURN \ |
89 | -d ff00::/8 | |
90 | run ip46tables -t mangle -A local-source -g bad-source-address | |
bfdc045d MW |
91 | run iptables -t mangle -A in-classify -j local-source \ |
92 | -m addrtype --src-type LOCAL | |
0291d6d5 MW |
93 | for addr in $host_6addrs; do |
94 | run ip6tables -t mangle -A in-classify -j local-source \ | |
95 | -s $addr | |
96 | done | |
bfdc045d | 97 | |
d5214471 MW |
98 | ## It's not valid to have a multicast address as a packet source: multicast |
99 | ## routing is done away from the source, so a multicast address would make | |
100 | ## this impossible to do. So discard these packets. Also discard class-E | |
101 | ## IPv4 addresses, since they aren't assigned. | |
102 | run iptables -t mangle -A in-classify -g bad-source-address \ | |
103 | -s 224.0.0.0/3 | |
104 | run ip6tables -t mangle -A in-classify -g bad-source-address \ | |
105 | -s ff00::/8 | |
106 | ||
bfdc045d MW |
107 | m4_divert(41)m4_dnl |
108 | ## Define the important networks. | |
109 | for pass in 1 2; do | |
110 | netclassindex=0 | |
111 | m4_divert(42)m4_dnl | |
112 | done | |
113 | ||
114 | m4_divert(46)m4_dnl | |
115 | ## Mark addresses reachable on non-default interfaces as not reachable on the | |
116 | ## default interface. | |
0291d6d5 | 117 | trace "nets = $allnets $allnets6" |
bfdc045d MW |
118 | for net in $allnets; do |
119 | case $net in | |
120 | "$defaultiface":*) | |
121 | ;; | |
122 | *) | |
123 | run iptables -t mangle -A in-$defaultiface \ | |
124 | -s ${net#*:} -g bad-source-address | |
125 | ;; | |
126 | esac | |
127 | done | |
0291d6d5 MW |
128 | for net in $allnets6; do |
129 | case $net in | |
130 | "$defaultiface":*) | |
131 | ;; | |
132 | *) | |
133 | run ip6tables -t mangle -A in-$defaultiface \ | |
134 | -s ${net#*:} -g bad-source-address | |
135 | ;; | |
136 | esac | |
137 | done | |
bfdc045d MW |
138 | |
139 | ## Fill in the black holes in the network. | |
140 | for addr in \ | |
141 | 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 \ | |
142 | 127.0.0.0/8 192.0.2.0/24 | |
143 | do | |
144 | run iptables -t mangle -A in-default -s $addr -g bad-source-address | |
145 | done | |
0291d6d5 MW |
146 | for addr in \ |
147 | fc00::/7 \ | |
148 | 2001:0db8::/32 | |
149 | do | |
150 | run ip6tables -t mangle -A in-default -s $addr -g bad-source-address | |
151 | done | |
bfdc045d MW |
152 | |
153 | m4_divert(92)m4_dnl | |
154 | ## Put the final default decision on the in-default chain, and attach the | |
155 | ## classification chains to the PREROUTING hook. | |
0291d6d5 MW |
156 | run ip46tables -t mangle -A in-$defaultiface -g mark-from-$defaultclass |
157 | run ip46tables -t mangle -A PREROUTING -j in-classify | |
158 | run ip46tables -t mangle -A PREROUTING -j out-classify | |
bfdc045d MW |
159 | |
160 | ## Now it's safe to let stuff through. | |
161 | for i in PREROUTING INPUT FORWARD OUTPUT POSTROUTING; do | |
0291d6d5 | 162 | run ip46tables -t mangle -P $i ACCEPT |
bfdc045d MW |
163 | done |
164 | ||
165 | m4_divert(-1) | |
166 | ###----- That's all, folks -------------------------------------------------- |