chiark / gitweb /
80caf1db05a92502ea3fb49c4d79590ab73d0a97
[firewall] / functions.m4
1 ### -*-m4-*-
2 ###
3 ### Utility functions for firewall scripts
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(20)m4_dnl
25 ###--------------------------------------------------------------------------
26 ### Utility functions.
27
28 ## doit COMMAND ARGS...
29 ##
30 ## If debugging, print the COMMAND and ARGS.  If serious, execute them.
31 run () {
32   set -e
33   if [ "$FW_DEBUG" ]; then echo "* $*"; fi
34   if ! [ "$FW_NOACT" ]; then "$@"; fi
35 }
36
37 ## trace MESSAGE...
38 ##
39 ## If debugging, print the MESSAGE.
40 trace () {
41   set -e
42   if [ "$FW_DEBUG" ]; then echo "$*"; fi
43 }
44
45 ## defport NAME NUMBER
46 ##
47 ## Define $port_NAME to be NUMBER.
48 defport () {
49   name=$1 number=$2
50   eval port_$name=$number
51 }
52
53 m4_divert(22)m4_dnl
54 ###--------------------------------------------------------------------------
55 ### Basic chain constructions.
56
57 ## clearchain CHAIN CHAIN ...
58 ##
59 ## Ensure that the named chains exist and are empty.
60 clearchain () {
61   set -e
62   for chain; do
63     case $chain in
64       *:*) table=${chain%:*} chain=${chain#*:} ;;
65       *) table=filter ;;
66     esac
67     run iptables -t $table -N $chain
68   done
69 }
70
71 ## errorchain CHAIN ACTION ARGS ...
72 ##
73 ## Make a chain which logs a message and then invokes some other action,
74 ## typically REJECT.  Log messages are prefixed by `fw: CHAIN'.
75 errorchain () {
76   set -e
77   chain=$1; shift
78   case $chain in
79     *:*) table=${chain%:*} chain=${chain#*:} ;;
80     *) table=filter ;;
81   esac
82   clearchain $table:$chain
83   run iptables -t $table -A $chain -j LOG \
84           -m limit --limit 3/minute --limit-burst 10 \
85           --log-prefix "fw: $chain " --log-level notice
86   run iptables -t $table -A $chain -j "$@"
87 }
88
89 m4_divert(24)m4_dnl
90 ###--------------------------------------------------------------------------
91 ### Basic option setting.
92
93 ## setopt OPTION VALUE
94 ##
95 ## Set an IP sysctl.
96 setopt () {
97   set -e
98   opt=$1; shift; val=$*
99   run sysctl -q net/ipv4/$opt="$val"
100 }
101
102 ## setdevopt OPTION VALUE
103 ##
104 ## Set an IP interface-level sysctl.
105 setdevopt () {
106   set -e
107   opt=$1; shift; val=$*
108   for i in /proc/sys/net/ipv4/conf/*; do
109     [ -f $i/$opt ] &&
110       run sysctl -q net/ipv4/conf/${i#/proc/sys/net/ipv4/conf/}/$opt="$val"
111   done
112 }
113
114 m4_divert(26)m4_dnl
115 ###--------------------------------------------------------------------------
116 ### Packet filter construction.
117
118 ## conntrack CHAIN
119 ##
120 ## Add connection tracking to CHAIN, and allow obvious stuff.
121 conntrack () {
122   set -e
123   chain=$1
124   run iptables -A $chain -p tcp -m state \
125           --state ESTABLISHED,RELATED -j ACCEPT
126   run iptables -A $chain -p tcp ! --syn -g bad-tcp
127 }
128
129 ## allowservices CHAIN PROTO SERVICE ...
130 ##
131 ## Add rules to allow the SERVICES on the CHAIN.
132 allowservices () {
133   set -e
134   chain=$1 proto=$2; shift 2
135   count=0
136   list=
137   for svc; do
138     case $svc in
139       *:*)
140         n=2
141         left=${svc%:*} right=${svc#*:}
142         case $left in *[!0-9]*) eval left=\$port_$left ;; esac
143         case $right in *[!0-9]*) eval right=\$port_$right ;; esac
144         svc=$left:$right
145         ;;
146       *)
147         n=1
148         case $svc in *[!0-9]*) eval svc=\$port_$svc ;; esac
149         ;;
150     esac
151     case $svc in
152       *: | :* | "" | *[!0-9:]*)
153         echo >&2 "Bad service name"
154         exit 1
155         ;;
156     esac
157     count=$(( $count + $n ))
158     if [ $count -gt 15 ]; then
159       run iptables -A $chain -p $proto -m multiport -j ACCEPT \
160              --destination-ports ${list#,}
161       list= count=$n
162     fi
163     list=$list,$svc
164   done
165   case $list in
166     "")
167       ;;
168     ,*,*)
169       run iptables -A $chain -p $proto -m multiport -j ACCEPT \
170               --destination-ports ${list#,}
171       ;;
172     *) 
173       run iptables -A $chain -p $proto -j ACCEPT \
174               --destination-port ${list#,}
175       ;;
176   esac
177 }
178
179 ## ntpclient CHAIN NTPSERVER ...
180 ##
181 ## Add rules to CHAIN to allow NTP with NTPSERVERs.
182 ntpclient () {
183   set -e
184   chain=$1; shift
185   for ntp; do
186     run iptables -A $chain -s $ntp -j ACCEPT \
187             -p udp --source-port 123 --destination-port 123
188   done
189 }
190
191 ## dnsresolver CHAIN
192 ##
193 ## Add rules to allow CHAIN to be a DNS resolver.
194 dnsresolver () {
195   set -e
196   chain=$1
197   for p in tcp udp; do
198     run iptables -A $chain -j ACCEPT \
199              -m state --state ESTABLISHED \
200              -p $p --source-port 53
201   done
202 }
203
204 ## openports CHAIN [MIN MAX]
205 ##
206 ## Add rules to CHAIN to allow the open ports.
207 openports () {
208   set -e
209   chain=$1; shift
210   [ $# -eq 0 ] && set -- $open_port_min $open_port_max
211   run iptables -A $chain -p tcp -g interesting --destination-port $1:$2
212   run iptables -A $chain -p udp -g interesting --destination-port $1:$2
213 }
214
215 m4_divert(28)m4_dnl
216 ###--------------------------------------------------------------------------
217 ### Packet classification.
218
219 ## defbitfield NAME WIDTH
220 ##
221 ## Defines MASK_NAME and BIT_NAME symbolic constants for dealing with
222 ## bitfields: x << BIT_NAME yields the value x in the correct position, and
223 ## ff & MASK_NAME extracts the corresponding value.
224 defbitfield () {
225   set -e
226   name=$1 width=$2
227   eval MASK_$name=$(( (1 << $width) - 1 << $bitindex ))
228   eval BIT_$name=$bitindex
229   bitindex=$(( $bitindex + $width ))
230 }
231
232 ## Define the layout of the bitfield.
233 bitindex=0
234 defbitfield MASK 16
235 defbitfield FROM 4
236 defbitfield TO 4
237
238 ## defnetclass NAME FORWARD-TO...
239 ##
240 ## Defines a netclass called NAME, which is allowed to forward to the
241 ## FORWARD-TO netclasses.
242 ##
243 ## For each netclass, constants from_NAME and to_NAME are defined as the
244 ## appropriate values in the FROM and TO fields (i.e., not including any mask
245 ## bits).
246 ##
247 ## This function also establishes mangle chains mark-from-NAME and
248 ## mark-to-NAME for applying the appropriate mark bits to the packet.
249 ##
250 ## Because it needs to resolve forward references, netclasses must be defined
251 ## in a two-pass manner, using a loop of the form
252 ##
253 ##   for pass in 1 2; do netclassindex=0; ...; done
254 netclassess=
255 defnetclass () {
256   set -e
257   name=$1; shift
258   case $pass in
259     1)
260
261       ## Pass 1.  Establish the from_NAME and to_NAME constants, and the
262       ## netclass's mask bit.
263       eval from_$name=$(( $netclassindex << $BIT_FROM ))
264       eval to_$name=$(( $netclassindex << $BIT_TO ))
265       eval _mask_$name=$(( 1 << ($netclassindex + $BIT_MASK) ))
266       nets="$nets $name"
267       ;;
268     2)
269
270       ## Pass 2.  Compute the actual from and to values.  We're a little bit
271       ## clever during source classification, and set the TO field to
272       ## all-bits-one, so that destination classification needs only a single
273       ## AND operation.
274       from=$(( ($netclassindex << $BIT_FROM) + (0xf << $BIT_TO) ))
275       for net; do
276         eval bit=\$_mask_$net
277         from=$(( $from + $bit ))
278       done
279       to=$(( ($netclassindex << $BIT_TO) + \
280              (0xf << $BIT_FROM) + \
281              (1 << ($netclassindex + $BIT_MASK)) ))
282       trace "from $name --> set $(printf %x $from)"
283       trace "  to $name --> and $(printf %x $from)"
284
285       ## Now establish the mark-from-NAME and mark-to-NAME chains.
286       clearchain mangle:mark-from-$name mangle:mark-to-$name
287       run iptables -t mangle -A mark-from-$name -j MARK --set-mark $from
288       run iptables -t mangle -A mark-to-$name -j MARK --and-mark $to
289       ;;
290   esac
291   netclassindex=$(( $netclassindex + 1 ))
292 }
293
294 ## defiface NAME NETCLASS:NETWORK/MASK...
295 ##
296 ## Declares a network interface NAME and associates with it a number of
297 ## reachable networks.  During source classification, a packet arriving on
298 ## interface NAME from an address in NETWORK/MASK is classified as coming
299 ## from to NETCLASS.  During destination classification, all packets going to
300 ## NETWORK/MASK are classified as going to NETCLASS, regardless of interface
301 ## (which is good, because the outgoing interface hasn't been determined
302 ## yet).
303 ##
304 ## As a special case, the NETWORK/MASK can be the string `default', which
305 ## indicates that all addresses not matched elsewhere should be considered.
306 ifaces=:
307 defaultiface=none
308 allnets=
309 defiface () {
310   set -e
311   name=$1; shift
312   case $ifaces in
313     *:"$name":*) ;;
314     *)
315       clearchain mangle:in-$name
316       run iptables -t mangle -A in-classify -i $name -g in-$name
317       ;;
318   esac
319   ifaces=$ifaces$name:
320   for item; do
321     netclass=${item%:*} addr=${item#*:}
322     case $addr in
323       default)
324         defaultiface=$name
325         defaultclass=$netclass
326         run iptables -t mangle -A out-classify -g mark-to-$netclass
327         ;;
328       *)
329         run iptables -t mangle -A in-$name -s $addr -g mark-from-$netclass
330         run iptables -t mangle -A out-classify -d $addr -g mark-to-$netclass
331         allnets="$allnets $name:$addr"
332         ;;
333     esac
334   done
335 }
336
337 ## defvpn IFACE CLASS NET HOST:ADDR ...
338 ##
339 ## Defines a VPN interface.  If the interface has the form `ROOT+' (i.e., a
340 ## netfilter wildcard) then define a separate interface ROOTHOST routing to
341 ## ADDR; otherwise just write a blanket rule allowing the whole NET.  All
342 ## addresses concerned are put in the named CLASS.
343 defvpn () {
344   set -e
345   iface=$1 class=$2 net=$3; shift 3
346   case $iface in
347     *-+)
348       root=${iface%+}
349       for host; do
350         name=${host%:*} addr=${host#*:}
351         defiface $root$name $class:$addr
352       done
353       ;;
354     *)
355       defiface $iface $class:$net
356       ;;
357   esac
358 }
359
360 m4_divert(-1)
361 ###----- That's all, folks --------------------------------------------------