Merge from vendor branch TNFTP:
[dragonfly.git] / etc / network.subr
1 #
2 # Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # 1. Redistributions of source code must retain the above copyright
8 #    notice, this list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright
10 #    notice, this list of conditions and the following disclaimer in the
11 #    documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 # SUCH DAMAGE.
24 #
25 # $FreeBSD: src/etc/network.subr,v 1.163 2005/06/30 04:52:47 brooks Exp $
26 # $DragonFly: src/etc/network.subr,v 1.6 2008/02/04 19:03:06 swildner Exp $
27 #
28
29 #
30 # Subroutines commonly used from network startup scripts.
31 # Requires that rc.conf be loaded first.
32 #
33
34 # ifconfig_up if
35 #       Evaluate ifconfig(8) arguments for interface $if and
36 #       run ifconfig(8) with those arguments. It returns 0 if
37 #       arguments were found and executed or 1 if the interface
38 #       had no arguments.  Pseudo arguments DHCP and WPA are handled
39 #       here.
40 #
41 ifconfig_up()
42 {
43         _cfg=1
44
45         ifconfig_args=`ifconfig_getargs $1`
46         if [ -n "${ifconfig_args}" ]; then
47                 ifconfig $1 ${ifconfig_args}
48                 _cfg=0
49         fi
50
51         if wpaif $1; then
52                 /etc/rc.d/wpa_supplicant start $1
53                 _cfg=0          # XXX: not sure this should count
54         fi
55
56         if dhcpif $1; then
57                 /etc/rc.d/dhclient start $1
58                 _cfg=0
59         fi
60
61         return $_cfg
62 }
63
64 # ifconfig_down if
65 #       Remove all inet entries from the $if interface. It returns
66 #       0 if inet entries were found and removed. It returns 1 if
67 #       no entries were found or they could not be removed.
68 #
69 ifconfig_down()
70 {
71         [ -z "$1" ] && return 1
72         _ifs="^"
73         _cfg=1
74
75         inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
76
77         oldifs="$IFS"
78         IFS="$_ifs"
79         for _inet in $inetList ; do
80                 # get rid of extraneous line
81                 [ -z "$_inet" ] && break
82
83                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
84
85                 IFS="$oldifs"
86                 ifconfig $1 ${_inet} delete
87                 IFS="$_ifs"
88                 _cfg=0
89         done
90         IFS="$oldifs"
91
92         if wpaif $1; then
93                 /etc/rc.d/wpa_supplicant stop $1
94         fi
95
96         if dhcpif $1; then
97                 /etc/rc.d/dhclient stop $1
98                 _cfg=0
99         fi
100
101         return $_cfg
102 }
103
104 # _ifconfig_getargs if
105 #       Echos the arguments for the supplied interface to stdout.
106 #       returns 1 if empty.  In general, ifconfig_getargs should be used
107 #       outside this file.
108 _ifconfig_getargs()
109 {
110         _ifn=$1
111         if [ -z "$_ifn" ]; then
112                 return 1
113         fi
114
115         eval _args=\$ifconfig_$1
116         if [ -z "$_args" -a -n "${pccard_ifconfig}" ]; then
117                 for _if in ${removable_interfaces} ; do
118                         if [ "$_if" = "$_ifn" ] ; then
119                                 _args=${pccard_ifconfig}
120                                 break
121                         fi
122                 done
123         fi
124
125         echo $_args
126 }
127
128 # ifconfig_getargs if
129 #       Takes the result from _ifconfig_getargs and removes pseudo
130 #       args such as DHCP and WPA.
131 ifconfig_getargs()
132 {
133         _tmpargs=`_ifconfig_getargs $1`
134         if [ $? -eq 1 ]; then
135                 return 1
136         fi
137         _args=
138
139         is_optarg=no
140         for _arg in $_tmpargs; do
141                 if [ "$is_optarg" = "no" ]; then
142                         case $_arg in
143                         [Dd][Hh][Cc][Pp])
144                                 ;;
145                         [Ww][Pp][Aa])
146                                 ;;
147                         *)
148                                 _args="$_args $_arg"
149                                 case $_arg in
150                                 authmode)
151                                         is_optarg=yes
152                                         ;;
153                                 esac
154                                 ;;
155                         esac
156                 else
157                         _args="$_args $_arg"
158                         is_optarg=no
159                 fi
160         done
161
162         echo $_args
163 }
164
165 # dhcpif if
166 #       Returns 0 if the interface is a DHCP interface and 1 otherwise.
167 dhcpif()
168 {
169         _tmpargs=`_ifconfig_getargs $1`
170         for _arg in $_tmpargs; do
171                 case $_arg in
172                 [Dd][Hh][Cc][Pp])
173                         return 0
174                         ;;
175                 esac
176         done
177         return 1
178 }
179
180 # wpaif if
181 #       Returns 0 if the interface is a WPA interface and 1 otherwise.
182 wpaif()
183 {
184         _tmpargs=`_ifconfig_getargs $1`
185         is_optarg=no
186         for _arg in $_tmpargs; do
187                 if [ "$is_optarg" = "no" ]; then
188                         case $_arg in
189                         [Ww][Pp][Aa])
190                                 return 0
191                                 ;;
192                         authmode)
193                                 is_optarg=yes
194                                 ;;
195                         esac
196                 else
197                         is_optarg=no
198                 fi
199         done
200         return 1
201 }
202
203 # ifalias_up if
204 #       Configure aliases for network interface $if.
205 #       It returns 0 if at least one alias was configured or
206 #       1 if there were none.
207 #
208 ifalias_up()
209 {
210         _ret=1
211         alias=0
212         while : ; do
213                 eval ifconfig_args=\$ifconfig_$1_alias${alias}
214                 if [ -n "${ifconfig_args}" ]; then
215                         ifconfig $1 ${ifconfig_args} alias
216                         alias=$((${alias} + 1))
217                         _ret=0
218                 else
219                         break
220                 fi
221         done
222         return $_ret
223 }
224
225 #ifalias_down if
226 #       Remove aliases for network interface $if.
227 #       It returns 0 if at least one alias was removed or
228 #       1 if there were none.
229 #
230 ifalias_down()
231 {
232         _ret=1
233         alias=0
234         while : ; do
235                 eval ifconfig_args=\$ifconfig_$1_alias${alias}
236                 if [ -n "${ifconfig_args}" ]; then
237                         ifconfig $1 ${ifconfig_args} -alias
238                         alias=$((${alias} + 1))
239                         _ret=0
240                 else
241                         break
242                 fi
243         done
244         return $_ret
245 }
246
247 # ifscript_up if
248 #       Evaluate a startup script for the $if interface.
249 #       It returns 0 if a script was found and processed or
250 #       1 if no script was found.
251 #
252 ifscript_up()
253 {
254         if [ -r /etc/start_if.$1 ]; then
255                 . /etc/start_if.$1
256                 return 0
257         fi
258         return 1
259 }
260
261 # ifscript_down if
262 #       Evaluate a shutdown script for the $if interface.
263 #       It returns 0 if a script was found and processed or
264 #       1 if no script was found.
265 #
266 ifscript_down()
267 {
268         if [ -r /etc/stop_if.$1 ]; then
269                 . /etc/stop_if.$1
270                 return 0
271         fi
272         return 1
273 }
274
275 # Create cloneable interfaces.
276 #
277 clone_up()
278 {
279         _prefix=
280         _list=
281         for ifn in ${cloned_interfaces}; do
282                 ifconfig ${ifn} create
283                 if [ $? -eq 0 ]; then
284                         _list="${_list}${_prefix}${ifn}"
285                         [ -z "$_prefix" ] && _prefix=' '
286                 fi
287         done
288         debug "Cloned: ${_list}"
289 }
290
291 # Destroy cloned interfaces. Destroyed interfaces are echoed
292 # to standard output.
293 #
294 clone_down()
295 {
296         _prefix=
297         _list=
298         for ifn in ${cloned_interfaces}; do
299                 ifconfig ${ifn} destroy
300                 if [ $? -eq 0 ]; then
301                         _list="${_list}${_prefix}${ifn}"
302                         [ -z "$_prefix" ] && _prefix=' '
303                 fi
304         done
305         debug "Destroyed clones: ${_list}"
306 }
307
308 gif_up() {
309         case ${gif_interfaces} in
310         [Nn][Oo] | '')
311                 ;;
312         *)
313                 for i in ${gif_interfaces}; do
314                         eval peers=\$gifconfig_$i
315                         case ${peers} in
316                         '')
317                                 continue
318                                 ;;
319                         *)
320                                 ifconfig $i create >/dev/null 2>&1
321                                 ifconfig $i tunnel ${peers}
322                                 ifconfig $i up
323                                 ;;
324                         esac
325                 done
326                 ;;
327         esac
328 }
329
330 #
331 # ipx_up ifn
332 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
333 # arguments were found and configured; returns 1 otherwise.
334 #
335 ipx_up()
336 {
337         ifn="$1"
338         eval ifconfig_args=\$ifconfig_${ifn}_ipx
339         if [ -n "${ifconfig_args}" ]; then
340                 ifconfig ${ifn} ${ifconfig_args}
341                 return 0
342         fi
343         return 1
344 }
345
346 # ipx_down ifn
347 #       Remove IPX addresses for interface $ifn. Returns 0 if IPX
348 #       addresses were found and unconfigured. It returns 1, otherwise.
349 #
350 ipx_down()
351 {
352         [ -z "$1" ] && return 1
353         _ifs="^"
354         _ret=1
355
356         ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
357
358         oldifs="$IFS"
359         IFS="$_ifs"
360         for _ipx in $ipxList ; do
361                 # get rid of extraneous line
362                 [ -z "$_ipx" ] && break
363
364                 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
365
366                 IFS="$oldifs"
367                 ifconfig $1 ${_ipx} delete
368                 IFS="$_ifs"
369                 _ret=0
370         done
371         IFS="$oldifs"
372
373         return $_ret
374 }
375
376 # ifnet_rename
377 #       Rename all requested interfaces.
378 #
379 ifnet_rename()
380 {
381
382         _ifn_list="`ifconfig -l`"
383         [ -z "$_ifn_list" ] && return 0
384         for _if in ${_ifn_list} ; do
385                 eval _ifname=\$ifconfig_${_if}_name
386                 if [ ! -z "$_ifname" ]; then
387                         ifconfig $_if name $_ifname
388                 fi
389         done
390         return 0
391 }
392
393 #
394 # list_net_interfaces type
395 #       List all network interfaces. The type of interface returned
396 #       can be controlled by the type argument. The type
397 #       argument can be any of the following:
398 #               nodhcp - all interfaces, excluding DHCP configured interfaces
399 #               dhcp   - list only DHCP configured interfaces
400 #       If no argument is specified all network interfaces are output.
401 #       Note that the list will include cloned interfaces if applicable.
402 #       Cloned interfaces must already exist to have a chance to appear
403 #       in the list if ${network_interfaces} is set to `auto'.
404 #
405 list_net_interfaces()
406 {
407         type=$1
408
409         # Get a list of ALL the interfaces.  NOTE: cloned interfaces
410         # have already been configured so they should show up in the
411         # ifconfig -l output.
412         #
413         case ${network_interfaces} in
414         [Aa][Uu][Tt][Oo])
415                 _tmplist="`ifconfig -l`"
416                 ;;
417         *)
418                 _tmplist="${network_interfaces} ${cloned_interfaces}"
419                 ;;
420         esac
421
422         if [ -z "$type" ]; then
423                 echo $_tmplist
424                 return 0
425         fi
426
427         # Separate out dhcp and non-dhcp interfaces
428         #
429         _aprefix=
430         _bprefix=
431         for _if in ${_tmplist} ; do
432                 eval _ifarg="\$ifconfig_${_if}"
433                 case "$_ifarg" in
434                 [Dd][Hh][Cc][Pp])
435                         _dhcplist="${_dhcplist}${_aprefix}${_if}"
436                         [ -z "$_aprefix" ] && _aprefix=' '
437                         ;;
438                 ''|*)
439                         _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
440                         [ -z "$_bprefix" ] && _bprefix=' '
441                         ;;
442                 esac
443         done
444
445         case "$type" in
446         nodhcp)
447                 echo $_nodhcplist
448                 ;;
449         dhcp)
450                 echo $_dhcplist
451                 ;;
452         esac
453         return 0
454 }
455
456 hexdigit()
457 {
458         if [ $1 -lt 10 ]; then
459                 echo $1
460         else
461                 case $1 in
462                 10)     echo a ;;
463                 11)     echo b ;;
464                 12)     echo c ;;
465                 13)     echo d ;;
466                 14)     echo e ;;
467                 15)     echo f ;;
468                 esac
469         fi
470 }
471
472 hexprint()
473 {
474         val=$1
475         str=''
476
477         dig=`hexdigit $((${val} & 15))`
478         str=${dig}${str}
479         val=$((${val} >> 4))
480         while [ ${val} -gt 0 ]; do
481                 dig=`hexdigit $((${val} & 15))`
482                 str=${dig}${str}
483                 val=$((${val} >> 4))
484         done
485
486         echo ${str}
487 }
488
489 # Setup the interfaces for IPv6
490 network6_interface_setup()
491 {
492         interfaces=$*
493         rtsol_interfaces=''
494         case ${ipv6_gateway_enable} in
495         [Yy][Ee][Ss])
496                 rtsol_available=no
497                 ;;
498         *)
499                 rtsol_available=yes
500                 ;;
501         esac
502         for i in $interfaces; do
503                 rtsol_interface=yes
504                 eval prefix=\$ipv6_prefix_$i
505                 if [ -n "${prefix}" ]; then
506                         rtsol_available=no
507                         rtsol_interface=no
508                         laddr=`network6_getladdr $i`
509                         hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
510                         for j in ${prefix}; do
511                                 address=$j\:${hostid}
512                                 ifconfig $i inet6 ${address} prefixlen 64 alias
513
514                                 case ${ipv6_gateway_enable} in
515                                 [Yy][Ee][Ss])
516                                         # subnet-router anycast address
517                                         # (rfc2373)
518                                         ifconfig $i inet6 $j:: prefixlen 64 \
519                                                 alias anycast
520                                         ;;
521                                 esac
522                         done
523                 fi
524                 eval ipv6_ifconfig=\$ipv6_ifconfig_$i
525                 if [ -n "${ipv6_ifconfig}" ]; then
526                         rtsol_available=no
527                         rtsol_interface=no
528                         ifconfig $i inet6 ${ipv6_ifconfig} alias
529                 fi
530
531                 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
532                 then
533                         case ${i} in
534                         lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
535                                 ;;
536                         *)
537                                 rtsol_interfaces="${rtsol_interfaces} ${i}"
538                                 ;;
539                         esac
540                 else
541                         ifconfig $i inet6
542                 fi
543         done
544
545         if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
546                 # Act as endhost - automatically configured.
547                 # You can configure only single interface, as
548                 # specification assumes that autoconfigured host has
549                 # single interface only.
550                 sysctl net.inet6.ip6.accept_rtadv=1
551                 set ${rtsol_interfaces}
552                 ifconfig $1 up
553                 rtsol $1
554         fi
555
556         for i in $interfaces; do
557                 alias=0
558                 while : ; do
559                         eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias}
560                         if [ -z "${ipv6_ifconfig}" ]; then
561                                 break;
562                         fi
563                         ifconfig $i inet6 ${ipv6_ifconfig} alias
564                         alias=$((${alias} + 1))
565                 done
566         done
567 }
568
569 # Setup IPv6 to IPv4 mapping
570 network6_stf_setup()
571 {
572         case ${stf_interface_ipv4addr} in
573         [Nn][Oo] | '')
574                 ;;
575         *)
576                 # assign IPv6 addr and interface route for 6to4 interface
577                 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
578                 OIFS="$IFS"
579                 IFS=".$IFS"
580                 set ${stf_interface_ipv4addr}
581                 IFS="$OIFS"
582                 hexfrag1=`hexprint $(($1*256 + $2))`
583                 hexfrag2=`hexprint $(($3*256 + $4))`
584                 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
585                 case ${stf_interface_ipv6_ifid} in
586                 [Aa][Uu][Tt][Oo] | '')
587                         for i in ${ipv6_network_interfaces}; do
588                                 laddr=`network6_getladdr ${i}`
589                                 case ${laddr} in
590                                 '')
591                                         ;;
592                                 *)
593                                         break
594                                         ;;
595                                 esac
596                         done
597                         stf_interface_ipv6_ifid=`expr "${laddr}" : \
598                                                       'fe80::\(.*\)%\(.*\)'`
599                         case ${stf_interface_ipv6_ifid} in
600                         '')
601                                 stf_interface_ipv6_ifid=0:0:0:1
602                                 ;;
603                         esac
604                         ;;
605                 esac
606                 ifconfig stf0 create >/dev/null 2>&1
607                 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
608                         prefixlen ${stf_prefixlen}
609                 # disallow packets to malicious 6to4 prefix
610                 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
611                 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
612                 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
613                 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
614                 ;;
615         esac
616 }
617
618 # Setup static routes
619 network6_static_routes_setup()
620 {
621         # Set up any static routes.
622         case ${ipv6_defaultrouter} in
623         [Nn][Oo] | '')
624                 ;;
625         *)
626                 ipv6_static_routes="default ${ipv6_static_routes}"
627                 ipv6_route_default="default ${ipv6_defaultrouter}"
628                 ;;
629         esac
630         case ${ipv6_static_routes} in
631         [Nn][Oo] | '')
632                 ;;
633         *)
634                 for i in ${ipv6_static_routes}; do
635                         eval ipv6_route_args=\$ipv6_route_${i}
636                         route add -inet6 ${ipv6_route_args}
637                 done
638                 ;;
639         esac
640 }
641
642 # Setup faith
643 network6_faith_setup()
644 {
645         case ${ipv6_faith_prefix} in
646         [Nn][Oo] | '')
647                 ;;
648         *)
649                 sysctl net.inet6.ip6.keepfaith=1
650                 ifconfig faith0 create >/dev/null 2>&1
651                 ifconfig faith0 up
652                 for prefix in ${ipv6_faith_prefix}; do
653                         prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
654                         case ${prefixlen} in
655                         '')
656                                 prefixlen=96
657                                 ;;
658                         *)
659                                 prefix=`expr "${prefix}" : \
660                                              "\(.*\)/${prefixlen}"`
661                                 ;;
662                         esac
663                         route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
664                         route change -inet6 ${prefix} -prefixlen ${prefixlen} \
665                                 -ifp faith0
666                 done
667                 ;;
668         esac
669 }
670
671 # Install the "default interface" to kernel, which will be used
672 # as the default route when there's no router.
673 network6_default_interface_setup()
674 {
675         # Choose IPv6 default interface if it is not clearly specified.
676         case ${ipv6_default_interface} in
677         '')
678                 for i in ${ipv6_network_interfaces}; do
679                         case $i in
680                         lo0|faith[0-9]*)
681                                 continue
682                                 ;;
683                         esac
684                         laddr=`network6_getladdr $i exclude_tentative`
685                         case ${laddr} in
686                         '')
687                                 ;;
688                         *)
689                                 ipv6_default_interface=$i
690                                 break
691                                 ;;
692                         esac
693                 done
694                 ;;
695         esac
696
697         # Disallow unicast packets without outgoing scope identifiers,
698         # or route such packets to a "default" interface, if it is specified.
699         route add -inet6 fe80:: -prefixlen 10 ::1 -reject
700         case ${ipv6_default_interface} in
701         [Nn][Oo] | '')
702                 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
703                 ;;
704         *)
705                 laddr=`network6_getladdr ${ipv6_default_interface}`
706                 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
707                         -cloning
708
709                 # Disable installing the default interface with the
710                 # case net.inet6.ip6.forwarding=0 and
711                 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
712                 # between the default router list and the manual
713                 # configured default route.
714                 case ${ipv6_gateway_enable} in
715                 [Yy][Ee][Ss])
716                         ;;
717                 *)
718                         if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
719                         then
720                                 ndp -I ${ipv6_default_interface}
721                         fi
722                         ;;
723                 esac
724                 ;;
725         esac
726 }
727
728 network6_getladdr()
729 {
730         ifconfig $1 2>/dev/null | while read proto addr rest; do
731                 case ${proto} in
732                 inet6)
733                         case ${addr} in
734                         fe80::*)
735                                 if [ -z "$2" ]; then
736                                         echo ${addr}
737                                         return
738                                 fi
739                                 case ${rest} in
740                                 *tentative*)
741                                         continue
742                                         ;;
743                                 *)
744                                         echo ${addr}
745                                         return
746                                 esac
747                         esac
748                 esac
749         done
750 }