numbers.m4, vampire.m4: Expose print server to local untrusted hosts.
[firewall] / functions.m4
CommitLineData
775bd287 1### -*-sh-*-
bfdc045d
MW
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
24m4_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.
31run () {
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.
40trace () {
41 set -e
42 if [ "$FW_DEBUG" ]; then echo "$*"; fi
43}
44
45## defport NAME NUMBER
46##
47## Define $port_NAME to be NUMBER.
48defport () {
49 name=$1 number=$2
50 eval port_$name=$number
51}
52
ce79e94a
MW
53## defproto NAME NUMBER
54##
55## Define $proto_NAME to be NUMBER.
56defproto () {
57 name=$1 number=$2
58 eval proto_$name=$number
59}
60
beb4f0ee
MW
61## addword VAR WORD
62##
63## Adds WORD to the value of the shell variable VAR, if it's not there
64## already. Words are separated by a single space; no leading or trailing
65## spaces are introduced.
66addword () {
67 var=$1 word=$2
68 eval val=\$$var
69 case " $val " in
70 *" $word "*) ;;
71 *) eval "$var=\${$var:+\$val }\$word" ;;
72 esac
73}
74
c70bfbbb
MW
75m4_divert(38)m4_dnl
76###--------------------------------------------------------------------------
77### Utility chains (used by function definitions).
78
a4d8cae3 79m4_divert(20)m4_dnl
bfdc045d
MW
80###--------------------------------------------------------------------------
81### Basic chain constructions.
82
0291d6d5
MW
83## ip46tables ARGS ...
84##
85## Do the same thing for `iptables' and `ip6tables'.
86ip46tables () {
87 set -e
88 iptables "$@"
89 ip6tables "$@"
90}
91
bfdc045d
MW
92## clearchain CHAIN CHAIN ...
93##
94## Ensure that the named chains exist and are empty.
95clearchain () {
96 set -e
7dde20fa
MW
97 for _chain; do
98 case $_chain in
99 *:*) table=${_chain%:*} _chain=${_chain#*:} ;;
bfdc045d
MW
100 *) table=filter ;;
101 esac
7dde20fa 102 run ip46tables -t $table -N $_chain 2>/dev/null || :
bfdc045d
MW
103 done
104}
105
9f3cffaa
MW
106## makeset SET TYPE [PARAMS]
107##
108## Ensure that the named ipset exists. Don't clear it.
109makeset () {
110 set -e
111 name=$1; shift
112 if ipset -nL | grep -q "^Name: $name$"; then
113 :
114 else
115 ipset -N "$name" "$@"
116 fi
117}
118
bfdc045d
MW
119## errorchain CHAIN ACTION ARGS ...
120##
121## Make a chain which logs a message and then invokes some other action,
122## typically REJECT. Log messages are prefixed by `fw: CHAIN'.
123errorchain () {
124 set -e
125 chain=$1; shift
126 case $chain in
127 *:*) table=${chain%:*} chain=${chain#*:} ;;
128 *) table=filter ;;
129 esac
130 clearchain $table:$chain
0291d6d5 131 run ip46tables -t $table -A $chain -j LOG \
bfdc045d 132 -m limit --limit 3/minute --limit-burst 10 \
fc10e52b 133 --log-prefix "fw: $chain " --log-level notice
a188f549
MW
134 run ip46tables -t $table -A $chain -j "$@" \
135 -m limit --limit 20/second --limit-burst 100
136 run ip46tables -t $table -A $chain -j DROP
bfdc045d
MW
137}
138
a4d8cae3 139m4_divert(20)m4_dnl
bfdc045d
MW
140###--------------------------------------------------------------------------
141### Basic option setting.
142
143## setopt OPTION VALUE
144##
145## Set an IP sysctl.
146setopt () {
147 set -e
0f6364ac
MW
148 opt=$1 val=$2
149 any=nil
150 for ver in ipv4 ipv6; do
151 if [ -f /proc/sys/net/$ver/$opt ]; then
152 run sysctl -q net/$ver/$opt="$val"
153 any=t
154 fi
155 done
156 case $any in
157 nil) echo >&2 "$0: unknown IP option $opt"; exit 1 ;;
158 esac
bfdc045d
MW
159}
160
0f6364ac 161## setdevopt OPTION VALUE [INTERFACES ...]
bfdc045d
MW
162##
163## Set an IP interface-level sysctl.
164setdevopt () {
165 set -e
0f6364ac
MW
166 opt=$1 val=$2; shift 2
167 case "$#,$1" in
168 0, | 1,all)
169 set -- $(
170 seen=:
171 for ver in ipv4 ipv6; do
172 cd /proc/sys/net/$ver/conf
173 for i in *; do
174 [ -f $i/$opt ] || continue
175 case "$seen" in (*:$i:*) continue ;; esac
176 echo $i
177 done
178 done)
179 ;;
180 esac
181 for i in "$@"; do
182 any=nil
183 for ver in ipv4 ipv6; do
184 if [ -f /proc/sys/net/$ver/conf/$i/$opt ]; then
185 any=t
4224a17b 186 run sysctl -q net/$ver/conf/$i/$opt="$val"
0f6364ac
MW
187 fi
188 done
189 case $any in
190 nil) echo >&2 "$0: unknown device option $opt"; exit 1 ;;
191 esac
bfdc045d
MW
192 done
193}
194
a4d8cae3 195m4_divert(20)m4_dnl
bfdc045d
MW
196###--------------------------------------------------------------------------
197### Packet filter construction.
198
199## conntrack CHAIN
200##
201## Add connection tracking to CHAIN, and allow obvious stuff.
202conntrack () {
203 set -e
204 chain=$1
0291d6d5 205 run ip46tables -A $chain -p tcp -m state \
bfdc045d 206 --state ESTABLISHED,RELATED -j ACCEPT
0291d6d5 207 run ip46tables -A $chain -p tcp ! --syn -g bad-tcp
bfdc045d
MW
208}
209
ecdca131
MW
210## commonrules CHAIN
211##
212## Add standard IP filtering rules to the CHAIN.
213commonrules () {
214 set -e
215 chain=$1
216
217 ## Pass fragments through, assuming that the eventual destination will sort
218 ## things out properly. Except for TCP, that is, which should never be
c70bfbbb
MW
219 ## fragmented. This is an extra pain for ip6tables, which doesn't provide
220 ## a pleasant way to detect non-initial fragments.
ecdca131
MW
221 run iptables -A $chain -p tcp -f -g tcp-fragment
222 run iptables -A $chain -f -j ACCEPT
0291d6d5
MW
223 run ip6tables -A $chain -p tcp -g tcp-fragment \
224 -m ipv6header --soft --header frag
c70bfbbb 225 run ip6tables -A $chain -j accept-non-init-frag
ecdca131
MW
226}
227
c70bfbbb
MW
228m4_divert(38)m4_dnl
229## Accept a non-initial fragment. This is only needed by IPv6, to work
230## around a deficiency in the option parser.
231run ip6tables -N accept-non-init-frag
232run ip6tables -A accept-non-init-frag -j RETURN \
233 -m frag --fragfirst
234run ip6tables -A accept-non-init-frag -j ACCEPT
235
a4d8cae3 236m4_divert(20)m4_dnl
bfdc045d
MW
237## allowservices CHAIN PROTO SERVICE ...
238##
239## Add rules to allow the SERVICES on the CHAIN.
240allowservices () {
241 set -e
242 chain=$1 proto=$2; shift 2
243 count=0
244 list=
245 for svc; do
246 case $svc in
247 *:*)
12ac65a1 248 n=2
bfdc045d
MW
249 left=${svc%:*} right=${svc#*:}
250 case $left in *[!0-9]*) eval left=\$port_$left ;; esac
251 case $right in *[!0-9]*) eval right=\$port_$right ;; esac
252 svc=$left:$right
253 ;;
254 *)
12ac65a1 255 n=1
bfdc045d
MW
256 case $svc in *[!0-9]*) eval svc=\$port_$svc ;; esac
257 ;;
258 esac
259 case $svc in
260 *: | :* | "" | *[!0-9:]*)
12ac65a1 261 echo >&2 "Bad service name"
bfdc045d
MW
262 exit 1
263 ;;
264 esac
265 count=$(( $count + $n ))
266 if [ $count -gt 15 ]; then
0291d6d5 267 run ip46tables -A $chain -p $proto -m multiport -j ACCEPT \
bfdc045d
MW
268 --destination-ports ${list#,}
269 list= count=$n
270 fi
271 list=$list,$svc
272 done
273 case $list in
274 "")
275 ;;
276 ,*,*)
0291d6d5 277 run ip46tables -A $chain -p $proto -m multiport -j ACCEPT \
bfdc045d
MW
278 --destination-ports ${list#,}
279 ;;
12ac65a1 280 *)
0291d6d5 281 run ip46tables -A $chain -p $proto -j ACCEPT \
bfdc045d
MW
282 --destination-port ${list#,}
283 ;;
284 esac
285}
286
287## ntpclient CHAIN NTPSERVER ...
288##
289## Add rules to CHAIN to allow NTP with NTPSERVERs.
290ntpclient () {
291 set -e
ace5a2fb
MW
292 ntpchain=$1; shift
293
294 clearchain ntp-servers
295 for ntp; do run iptables -A ntp-servers -j ACCEPT -s $ntp; done
296 run iptables -A $ntpchain -j ntp-servers \
297 -p udp --source-port 123 --destination-port 123
bfdc045d
MW
298}
299
300## dnsresolver CHAIN
301##
302## Add rules to allow CHAIN to be a DNS resolver.
303dnsresolver () {
304 set -e
305 chain=$1
306 for p in tcp udp; do
0291d6d5 307 run ip46tables -A $chain -j ACCEPT \
bfdc045d
MW
308 -m state --state ESTABLISHED \
309 -p $p --source-port 53
310 done
311}
312
7dde20fa
MW
313## dnsserver CHAIN
314##
315## Add rules to allow CHAIN to be a DNS server.
316dnsserver () {
317 set -e
318 chain=$1
319
320 ## Allow TCP access. Hitting us with SYNs will make us deploy SYN cookies,
321 ## but that's tolerable.
322 run ip46tables -A $chain -j ACCEPT -p tcp --destination-port 53
323
324 ## Avoid being a DDoS amplifier by rate-limiting incoming DNS queries.
325 clearchain $chain-udp-dns
326 run ip46tables -A $chain-udp-dns -j ACCEPT \
327 -m limit --limit 20/second --limit-burst 300
328 run ip46tables -A $chain-udp-dns -g dns-rate-limit
329 run ip46tables -A $chain -j $chain-udp-dns \
330 -p udp --destination-port 53
331}
332
bfdc045d
MW
333## openports CHAIN [MIN MAX]
334##
335## Add rules to CHAIN to allow the open ports.
336openports () {
337 set -e
338 chain=$1; shift
339 [ $# -eq 0 ] && set -- $open_port_min $open_port_max
0291d6d5
MW
340 run ip46tables -A $chain -p tcp -g interesting --destination-port $1:$2
341 run ip46tables -A $chain -p udp -g interesting --destination-port $1:$2
bfdc045d
MW
342}
343
a4d8cae3 344m4_divert(20)m4_dnl
bfdc045d
MW
345###--------------------------------------------------------------------------
346### Packet classification.
beb4f0ee
MW
347###
348### See `classify.m4' for an explanation of how the firewall machinery for
349### packet classification works.
350###
351### A list of all network names is kept in `allnets'. For each network NET,
352### shell variables are defined describing their properties.
353###
354### net_class_NET The class of the network, as defined by
355### `defnetclass'.
356### net_inet_NET List of IPv4 address ranges in the network.
357### net_inet6_NET List of IPv6 address ranges in the network.
358### net_fwd_NET List of other networks that this one forwards to.
359### net_hosts_NET List of hosts known to be in the network.
360### host_inet_HOST IPv4 address of the named HOST.
361### host_inet6_HOST IPv6 address of the named HOST.
362###
363### Similarly, a list of hosts is kept in `allhosts', and for each host HOST,
364### a shell variables are defined:
365###
366### host_ifaces_HOST List of interfaces for this host and the networks
367### they attach to, in the form IFACE=NET.
bfdc045d
MW
368
369## defbitfield NAME WIDTH
370##
371## Defines MASK_NAME and BIT_NAME symbolic constants for dealing with
372## bitfields: x << BIT_NAME yields the value x in the correct position, and
373## ff & MASK_NAME extracts the corresponding value.
374defbitfield () {
375 set -e
376 name=$1 width=$2
377 eval MASK_$name=$(( (1 << $width) - 1 << $bitindex ))
378 eval BIT_$name=$bitindex
379 bitindex=$(( $bitindex + $width ))
380}
381
382## Define the layout of the bitfield.
383bitindex=0
384defbitfield MASK 16
385defbitfield FROM 4
386defbitfield TO 4
387
388## defnetclass NAME FORWARD-TO...
389##
390## Defines a netclass called NAME, which is allowed to forward to the
391## FORWARD-TO netclasses.
392##
393## For each netclass, constants from_NAME and to_NAME are defined as the
394## appropriate values in the FROM and TO fields (i.e., not including any mask
395## bits).
396##
397## This function also establishes mangle chains mark-from-NAME and
398## mark-to-NAME for applying the appropriate mark bits to the packet.
399##
400## Because it needs to resolve forward references, netclasses must be defined
401## in a two-pass manner, using a loop of the form
402##
403## for pass in 1 2; do netclassindex=0; ...; done
404netclassess=
405defnetclass () {
406 set -e
407 name=$1; shift
408 case $pass in
409 1)
410
411 ## Pass 1. Establish the from_NAME and to_NAME constants, and the
412 ## netclass's mask bit.
16838f59 413 trace "netclass $name = $netclassindex"
bfdc045d
MW
414 eval from_$name=$(( $netclassindex << $BIT_FROM ))
415 eval to_$name=$(( $netclassindex << $BIT_TO ))
3b250fe6 416 eval fwd_$name=$(( 1 << ($netclassindex + $BIT_MASK) ))
bfdc045d
MW
417 nets="$nets $name"
418 ;;
419 2)
420
1850991d
MW
421 ## Pass 2. Compute the actual from and to values. This is fiddly:
422 ## we want to preserve the other flags.
423 from=$(( ($netclassindex << $BIT_FROM) ))
424 frommask=$(( $MASK_FROM | $MASK_MASK ))
bfdc045d 425 for net; do
3b250fe6 426 eval bit=\$fwd_$net
bfdc045d
MW
427 from=$(( $from + $bit ))
428 done
1850991d
MW
429 to=$(( ($netclassindex << $BIT_TO) ))
430 tomask=$(( $MASK_MASK ^ (1 << ($netclassindex + $BIT_MASK)) ))
431 trace "from $name --> set $(printf %08x/%08x $from $frommask)"
432 trace " to $name --> and $(printf %08x/%08x $to $tomask)"
bfdc045d
MW
433
434 ## Now establish the mark-from-NAME and mark-to-NAME chains.
435 clearchain mangle:mark-from-$name mangle:mark-to-$name
1850991d
MW
436 run ip46tables -t mangle -A mark-from-$name -j MARK \
437 --set-xmark $from/$frommask
438 run ip46tables -t mangle -A mark-to-$name -j MARK \
439 --set-xmark $to/$tomask
bfdc045d
MW
440 ;;
441 esac
442 netclassindex=$(( $netclassindex + 1 ))
443}
444
beb4f0ee
MW
445## defnet NET CLASS
446##
447## Define a network. Follow by calls to `addr', `forwards', etc. to define
448## properties of the network. Networks are processed in order, so if their
449## addresses overlap then the more specific addresses should be defined
450## earlier.
451defnet () {
452 net=$1 class=$2
453 addword allnets $net
454 eval net_class_$1=\$class
455}
456
457## addr ADDRESS/LEN ...
458##
459## Define addresses for the network being defined. ADDRESSes are in
460## colon-separated IPv6 or dotted-quad IPv4 form.
461addr () {
462 for i in "$@"; do
463 case "$i" in
464 *:*) addword net_inet6_$net $i ;;
465 *) addword net_inet_$net $i ;;
bfdc045d
MW
466 esac
467 done
468}
469
beb4f0ee 470## forwards NET ...
bfdc045d 471##
beb4f0ee
MW
472## Declare that packets from this network are forwarded to the other NETs.
473forwards () {
474 eval "net_fwd_$net=\"$*\""
475}
476
477## noxit NET ...
478##
479## Declare that packets from this network must not be forwarded to the other
480## NETs.
481noxit () {
482 eval "net_noxit_$net=\"$*\""
483}
484
485## host HOST ADDR ...
486##
487## Define the address of an individual host on the current network. The
488## ADDRs may be full IPv4 or IPv6 addresses, or offsets from the containing
489## network address, which is a simple number for IPv4, or a suffix beginning
490## with `::' for IPv6. If an IPv6 base address is provided for the network
491## but not for the host then the host's IPv4 address is used as a suffix.
492host () {
493 name=$1; shift
494
495 ## Work out which addresses we've actually been given.
496 unset a6
497 for i in "$@"; do
498 case "$i" in ::*) a6=$i ;; *) a=$i ;; esac
499 done
500 case "${a+t}" in
501 t) ;;
502 *) echo >&2 "$0: no address for $name"; exit 1 ;;
bfdc045d 503 esac
beb4f0ee
MW
504 case "${a6+t}" in t) ;; *) a6=::$a ;; esac
505
506 ## Work out the IPv4 address.
507 eval nn=\$net_inet_$net
508 for n in $nn; do
509 addr=${n%/*}
510 base=${addr%.*}
511 offset=${addr##*.}
512 case $a in *.*) aa=$a ;; *) aa=$base.$(( $offset + $a )) ;; esac
513 eval host_inet_$name=$aa
514 done
515
516 ## Work out the IPv6 address.
517 eval nn=\$net_inet6_$net
518 for n in $nn; do
519 addr=${n%/*}
520 base=${addr%::*}
1710944b 521 case $a6 in ::*) aa=$base$a6 ;; *) aa=$a6 ;; esac
beb4f0ee
MW
522 eval host_inet6_$name=$aa
523 done
524
525 ## Remember the host in the list.
526 addword net_hosts_$net $name
527}
528
529## defhost NAME
530##
531## Define a new host. Follow by calls to `iface' to define the host's
532## interfaces.
533defhost () {
534 host=$1
535 addword allhosts $host
4eb9f4df 536 eval host_type_$host=server
beb4f0ee
MW
537}
538
4eb9f4df 539## hosttype TYPE
beb4f0ee 540##
4eb9f4df
MW
541## Declare the host to have the given type.
542hosttype () {
543 type=$1
544 case $type in
545 router | server | client) ;;
546 *) echo >&2 "$0: bad host type \`$type'"; exit 1 ;;
547 esac
548 eval host_type_$host=$type
beb4f0ee
MW
549}
550
551## iface IFACE NET ...
552##
553## Define a host's interfaces. Specifically, declares that the host has an
554## interface IFACE attached to the listed NETs.
555iface () {
556 name=$1; shift
557 for net in "$@"; do
558 addword host_ifaces_$host $name=$net
559 done
560}
561
1264e917
MW
562## Build rules which match a particular collection of networks.
563## Specifically, use the address-comparison operator OPT (typically `-s' or
564## `-d') to match the addresses of NOT, writing the rules to the chain
565## BASESUFFIX. If we find a match, dispatch to WIN-CLASS, where CLASS is
566## the class of the matching network. In order to deal with networks
567## containing negative address ranges, more chains may need to be
568## constructed; they will be named BASE#Q for sequence numbers Q starting
569## with NEXT. All of this happens on the `mangle' table, and there isn't
570## (currently) a way to tweak this.
571##
572## The FLAGS gather additional interesting information about the job,
573## separated by colons. The only flag currently is :default: which means
574## that the default network was listed.
575##
576## Finally, there is a hook PREPARE which is called just in advance of
577## processing the final network, passing it the argument FLAGS. (The PREPARE
578## string will be subjected to shell word-splitting, so it can provide some
579## arguments of its own if it wants.) It should set `mode' to indicate how
580## the chain should be finished.
581##
582## goto If no networks matched, then issue a final `goto' to the
583## chain named by the variable `fail'.
584##
585## call Run `$finish CHAIN' to write final rules to the named CHAIN
586## (which may be suffixed from the original BASE argument if
587## this was necessary). This function will arrange to call
588## these rules if no networks match.
589##
590## ret If no network matches then return (maybe by falling off the
591## end of the chain).
592matchnets () {
593 local opt win flags prepare base suffix next net lose splitp
594 opt=$1 win=$2 flags=$3 prepare=$4 base=$5 suffix=$6 next=$7 net=$8
595 shift 8
596
597 ## If this is the default network, then set the flag.
598 case "$net" in default) flags=${flags}default: ;; esac
599
600 ## Do an initial pass over the addresses to see whether there are any
601 ## negative ranges. If so, we'll need to split. See also the standard
602 ## joke about soup.
603 splitp=nil
604 eval "addrs=\"\$net_inet_$net \$net_inet6_$net\""
605 for a in $addrs; do case $a in !*) splitp=t; break ;; esac; done
606
607 trace "MATCHNETS [splitp $splitp] $opt $win $flags [$prepare] $base $suffix $next : $net $*"
608
609 ## Work out how to handle matches against negative address ranges. If this
610 ## is the last network, invoke the PREPARE hook to find out. Otherwise, if
611 ## we have to split the chain, recursively build the target here.
612 case $splitp,$# in
613 t,0 | nil,0)
614 $prepare $flags
615 case $splitp,$mode in
616 *,goto)
617 lose="-g $fail"
618 ;;
619 *,ret)
620 lose="-j RETURN"
621 ;;
622 t,call)
623 clearchain mangle:$base#$next
624 lose="-g $base#$next"
625 ;;
626 nil,call)
627 ;;
628 esac
629 ;;
630 t,*)
631 clearchain mangle:$base#$next
632 matchnets $opt $win $flags "$prepare" \
633 $base \#$next $(( $next + 1 )) "$@"
634 lose="-g $base#$next" mode=goto
635 ;;
636 *)
637 mode=continue
638 ;;
639 esac
640
641 ## Populate the chain with rules to match the necessary networks.
642 eval addr=\$net_inet_$net addr6=\$net_inet6_$net class=\$net_class_$net
643 for a in $addr; do
644 case $a in
645 !*) run iptables -t mangle -A $base$suffix $lose $opt ${a#!} ;;
646 *) run iptables -t mangle -A $base$suffix -g $win-$class $opt $a ;;
647 esac
648 done
649 for a in $addr6; do
650 case $a in
651 !*) run ip6tables -t mangle -A $base$suffix $lose $opt ${a#!} ;;
652 *) run ip6tables -t mangle -A $base$suffix -g $win-$class $opt $a ;;
653 esac
654 done
655
656 ## Wrap up the chain appropriately. If we didn't split and there are more
657 ## networks to handle then append the necessary rules now. (If we did
658 ## split, then we already wrote the rules for them above.) If there are no
659 ## more networks then consult the `mode' setting to find out what to do.
660 case $splitp,$#,$mode in
661 *,0,ret) ;;
662 *,*,goto) run ip46tables -t mangle -A $base$suffix $lose ;;
663 t,0,call) $finish $base#$next ;;
664 nil,0,call) $finish $base$suffix ;;
665 nil,*,*)
666 matchnets $opt $win $flags "$prepare" $base "$suffix" $next "$@"
667 ;;
668 esac
669}
670
beb4f0ee
MW
671## net_interfaces HOST NET
672##
673## Determine the interfaces on which packets may plausibly arrive from the
674## named NET. Returns `-' if no such interface exists.
675##
676## This algorithm is not very clever. It's just about barely good enough to
677## deduce transitivity through a simple routed network; with complicated
678## networks, it will undoubtedly give wrong answers. Check the results
679## carefully, and, if necessary, list the connectivity explicitly; use the
680## special interface `-' for networks you know shouldn't send packets to a
681## host.
682net_interfaces () {
683 host=$1 startnet=$2
684
685 ## Determine the locally attached networks.
686 targets=:
687 eval ii=\$host_ifaces_$host
688 for i in $ii; do targets=$targets$i:; done
689
690 ## Determine the transitivity.
691 seen=:
692 nets=$startnet
693 while :; do
694
695 ## First pass. Determine whether any of the networks we're considering
696 ## are in the target set. If they are, then return the corresponding
697 ## interfaces.
698 found=""
699 for net in $nets; do
700 tg=$targets
701 while :; do
702 any=nil
703 case $tg in
704 *"=$net:"*)
705 n=${tg%=$net:*}; tg=${n%:*}:; n=${n##*:}
706 addword found $n
707 any=t
708 ;;
709 esac
710 case $any in nil) break ;; esac
711 done
712 done
713 case "$found" in ?*) echo $found; return ;; esac
714
715 ## No joy. Determine the set of networks which (a) these ones can
716 ## forward to, and (b) that we've not considered already. These are the
717 ## nets we'll consider next time around.
718 nextnets=""
719 any=nil
720 for net in $nets; do
721 eval fwd=\$net_fwd_$net
722 for n in $fwd; do
723 case $seen in *":$n:"*) continue ;; esac
724 seen=$seen$n:
725 eval noxit=\$net_noxit_$n
726 case " $noxit " in *" $startnet "*) continue ;; esac
727 case " $nextnets " in
728 *" $n "*) ;;
729 *) addword nextnets $n; any=t ;;
730 esac
731 done
732 done
733
734 ## If we've run out of networks then there's no reachability. Return a
735 ## failure.
736 case $any in nil) echo -; return ;; esac
737 nets=$nextnets
738 done
bfdc045d
MW
739}
740
741m4_divert(-1)
742###----- That's all, folks --------------------------------------------------