]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - etc/network.subr
Merge r228472. For the sake of POLA for the whole 9.x timeline add
[FreeBSD/releng/9.0.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$
26 #
27
28 #
29 # Subroutines commonly used from network startup scripts.
30 # Requires that rc.conf be loaded first.
31 #
32
33 # ifn_start ifn
34 #       Bring up and configure an interface.  If some configuration is
35 #       applied, print the interface configuration.
36 #
37 ifn_start()
38 {
39         local ifn cfg
40         ifn="$1"
41         cfg=1
42
43         [ -z "$ifn" ] && err 1 "ifn_start called without an interface"
44
45         ifscript_up ${ifn} && cfg=0
46         ifconfig_up ${ifn} && cfg=0
47         afexists inet && ipv4_up ${ifn} && cfg=0
48         afexists inet6 && ipv6_up ${ifn} && cfg=0
49         afexists ipx && ipx_up ${ifn} && cfg=0
50         childif_create ${ifn} && cfg=0
51
52         return $cfg
53 }
54
55 # ifn_stop ifn
56 #       Shutdown and de-configure an interface.  If action is taken,
57 #       print the interface name.
58 #
59 ifn_stop()
60 {
61         local ifn cfg
62         ifn="$1"
63         cfg=1
64
65         [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
66
67         afexists ipx && ipx_down ${ifn} && cfg=0
68         afexists inet6 && ipv6_down ${ifn} && cfg=0
69         afexists inet && ipv4_down ${ifn} && cfg=0
70         ifconfig_down ${ifn} && cfg=0
71         ifscript_down ${ifn} && cfg=0
72         childif_destroy ${ifn} && cfg=0
73
74         return $cfg
75 }
76
77 # ifconfig_up if
78 #       Evaluate ifconfig(8) arguments for interface $if and
79 #       run ifconfig(8) with those arguments. It returns 0 if
80 #       arguments were found and executed or 1 if the interface
81 #       had no arguments.  Pseudo arguments DHCP and WPA are handled
82 #       here.
83 #
84 ifconfig_up()
85 {
86         local _cfg _ipv6_opts ifconfig_args
87         _cfg=1
88
89         # Make sure lo0 always comes up.
90         if [ "$1" = "lo0" ]; then
91                 _cfg=0
92         fi
93
94         # ifconfig_IF
95         ifconfig_args=`ifconfig_getargs $1`
96         if [ -n "${ifconfig_args}" ]; then
97                 eval ifconfig $1 ${ifconfig_args}
98                 _cfg=0
99         fi
100
101         # inet6 specific
102         if afexists inet6; then
103                 if checkyesno ipv6_activate_all_interfaces; then
104                         _ipv6_opts="-ifdisabled"
105                 elif [ "$1" != "lo0" ]; then
106                         _ipv6_opts="ifdisabled"
107                 fi
108
109                 # backward compatibility: $ipv6_enable
110                 case $ipv6_enable in
111                 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
112                         _ipv6_opts="${_ipv6_opts} accept_rtadv"
113                         ;;
114                 esac
115
116                 case $ipv6_cpe_wanif in
117                 $1)
118                         _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
119                 ;;
120                 esac
121
122                 if [ -n "${_ipv6_opts}" ]; then
123                         ifconfig $1 inet6 ${_ipv6_opts}
124                 fi
125
126                 # ifconfig_IF_ipv6
127                 ifconfig_args=`ifconfig_getargs $1 ipv6`
128                 if [ -n "${ifconfig_args}" ]; then
129                         # backward compatibility: inet6 keyword
130                         case "${ifconfig_args}" in
131                         :*|[0-9a-fA-F]*:*)
132                                 warn "\$ifconfig_$1_ipv6 needs " \
133                                     "\"inet6\" keyword for an IPv6 address."
134                                 ifconfig_args="inet6 ${ifconfig_args}"
135                         ;;
136                         esac
137                         ifconfig $1 inet6 -ifdisabled
138                         eval ifconfig $1 ${ifconfig_args}
139                         _cfg=0
140                 fi
141
142                 # backward compatiblity: $ipv6_ifconfig_IF
143                 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
144                 if [ -n "${ifconfig_args}" ]; then
145                         warn "\$ipv6_ifconfig_$1 is obsolete." \
146                             "  Use ifconfig_$1_ipv6 instead."
147                         ifconfig $1 inet6 -ifdisabled
148                         eval ifconfig $1 inet6 ${ifconfig_args}
149                         _cfg=0
150                 fi
151         fi
152
153         if [ ${_cfg} -eq 0 ]; then
154                 ifconfig $1 up
155         fi
156
157         if wpaif $1; then
158                 /etc/rc.d/wpa_supplicant start $1
159                 _cfg=0          # XXX: not sure this should count
160         fi
161
162         if dhcpif $1; then
163                 if [ $_cfg -ne 0 ] ; then
164                         ifconfig $1 up
165                 fi
166                 if syncdhcpif $1; then
167                         /etc/rc.d/dhclient start $1
168                 fi
169                 _cfg=0
170         fi
171
172         return $_cfg
173 }
174
175 # ifconfig_down if
176 #       returns 1 if wpa_supplicant or dhclient was stopped or
177 #       the interface exists.
178 #
179 ifconfig_down()
180 {
181         local _cfg
182         _cfg=1
183
184         if wpaif $1; then
185                 /etc/rc.d/wpa_supplicant stop $1
186                 _cfg=0
187         fi
188
189         if dhcpif $1; then
190                 /etc/rc.d/dhclient stop $1
191                 _cfg=0
192         fi
193
194         if ifexists $1; then
195                 ifconfig $1 down
196                 _cfg=0
197         fi
198
199         return $_cfg
200 }
201
202 # get_if_var if var [default]
203 #       Return the value of the pseudo-hash corresponding to $if where
204 #       $var is a string containg the sub-string "IF" which will be
205 #       replaced with $if after the characters defined in _punct are
206 #       replaced with '_'. If the variable is unset, replace it with
207 #       $default if given.
208 get_if_var()
209 {
210         local _if _punct _punct_c _var _default prefix suffix
211
212         if [ $# -ne 2 -a $# -ne 3 ]; then
213                 err 3 'USAGE: get_if_var name var [default]'
214         fi
215
216         _if=$1
217         _punct=". - / +"
218         for _punct_c in $_punct; do
219                 _if=`ltr ${_if} ${_punct_c} '_'`
220         done
221         _var=$2
222         _default=$3
223
224         prefix=${_var%%IF*}
225         suffix=${_var##*IF}
226         eval echo \${${prefix}${_if}${suffix}-${_default}}
227 }
228
229 # _ifconfig_getargs if [af]
230 #       Prints the arguments for the supplied interface to stdout.
231 #       Returns 1 if empty.  In general, ifconfig_getargs should be used
232 #       outside this file.
233 _ifconfig_getargs()
234 {
235         local _ifn _af
236         _ifn=$1
237         _af=${2+_$2}
238
239         if [ -z "$_ifn" ]; then
240                 return 1
241         fi
242
243         get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
244 }
245
246 # ifconfig_getargs if [af]
247 #       Takes the result from _ifconfig_getargs and removes pseudo
248 #       args such as DHCP and WPA.
249 ifconfig_getargs()
250 {
251         local _tmpargs _arg _args
252         _tmpargs=`_ifconfig_getargs $1 $2`
253         if [ $? -eq 1 ]; then
254                 return 1
255         fi
256         _args=
257
258         for _arg in $_tmpargs; do
259                 case $_arg in
260                 [Dd][Hh][Cc][Pp]) ;;
261                 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
262                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
263                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
264                 [Ww][Pp][Aa]) ;;
265                 *)
266                         _args="$_args $_arg"
267                         ;;
268                 esac
269         done
270
271         echo $_args
272 }
273
274 # autoif
275 #       Returns 0 if the interface should be automatically configured at
276 #       boot time and 1 otherwise.
277 autoif()
278 {
279         local _tmpargs _arg
280         _tmpargs=`_ifconfig_getargs $1`
281
282         for _arg in $_tmpargs; do
283                 case $_arg in
284                 [Nn][Oo][Aa][Uu][Tt][Oo])
285                         return 1
286                         ;;
287                 esac
288         done
289
290         return 0
291 }
292
293 # dhcpif if
294 #       Returns 0 if the interface is a DHCP interface and 1 otherwise.
295 dhcpif()
296 {
297         local _tmpargs _arg
298         _tmpargs=`_ifconfig_getargs $1`
299
300         if noafif $1; then
301                 return 1
302         fi
303
304         for _arg in $_tmpargs; do
305                 case $_arg in
306                 [Dd][Hh][Cc][Pp])
307                         return 0
308                         ;;
309                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
310                         return 0
311                         ;;
312                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
313                         return 0
314                         ;;
315                 esac
316         done
317
318         return 1
319 }
320
321 # syncdhcpif
322 #       Returns 0 if the interface should be configured synchronously and
323 #       1 otherwise.
324 syncdhcpif()
325 {
326         local _tmpargs _arg
327         _tmpargs=`_ifconfig_getargs $1`
328
329         if noafif $1; then
330                 return 1
331         fi
332
333         for _arg in $_tmpargs; do
334                 case $_arg in
335                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
336                         return 1
337                         ;;
338                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
339                         return 0
340                         ;;
341                 esac
342         done
343
344         checkyesno synchronous_dhclient
345 }
346
347 # wpaif if
348 #       Returns 0 if the interface is a WPA interface and 1 otherwise.
349 wpaif()
350 {
351         local _tmpargs _arg
352         _tmpargs=`_ifconfig_getargs $1`
353
354         for _arg in $_tmpargs; do
355                 case $_arg in
356                 [Ww][Pp][Aa])
357                         return 0
358                         ;;
359                 esac
360         done
361
362         return 1
363 }
364
365 # afexists af
366 #       Returns 0 if the address family is enabled in the kernel
367 #       1 otherwise.
368 afexists()
369 {
370         local _af
371         _af=$1
372
373         case ${_af} in
374         inet|inet6)
375                 check_kern_features ${_af}
376                 ;;
377         ipx)
378                 ${SYSCTL_N} net.ipx > /dev/null 2>&1
379                 ;;
380         atm)
381                 if [ -x /sbin/atmconfig ]; then
382                         /sbin/atmconfig diag list > /dev/null 2>&1
383                 else
384                         return 1
385                 fi
386                 ;;
387         *)
388                 err 1 "afexists(): Unsupported address family: $_af"
389                 ;;
390         esac
391 }
392
393 # noafif if
394 #       Returns 0 if the interface has no af configuration and 1 otherwise.
395 noafif()
396 {
397         local _if
398         _if=$1
399
400         case $_if in
401         pflog[0-9]*|\
402         pfsync[0-9]*|\
403         an[0-9]*|\
404         ath[0-9]*|\
405         ipw[0-9]*|\
406         ipfw[0-9]*|\
407         iwi[0-9]*|\
408         iwn[0-9]*|\
409         ral[0-9]*|\
410         wi[0-9]*|\
411         wl[0-9]*|\
412         wpi[0-9]*)
413                 return 0
414                 ;;
415         esac
416
417         return 1
418 }
419
420 # ipv6if if
421 #       Returns 0 if the interface should be configured for IPv6 and
422 #       1 otherwise.
423 ipv6if()
424 {
425         local _if _tmpargs i
426         _if=$1
427
428         if ! afexists inet6; then
429                 return 1
430         fi
431
432         # lo0 is always IPv6-enabled
433         case $_if in
434         lo0)
435                 return 0
436                 ;;
437         esac
438
439         case "${ipv6_network_interfaces}" in
440         $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
441                 # True if $ifconfig_IF_ipv6 is defined.
442                 _tmpargs=`_ifconfig_getargs $_if ipv6`
443                 if [ -n "${_tmpargs}" ]; then
444                         return 0
445                 fi
446
447                 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
448                 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
449                 if [ -n "${_tmpargs}" ]; then
450                         return 0
451                 fi
452                 ;;
453         esac
454
455         return 1
456 }
457
458 # ipv6_autoconfif if
459 #       Returns 0 if the interface should be configured for IPv6 with
460 #       Stateless Address Configuration; 1 otherwise.
461 ipv6_autoconfif()
462 {
463         local _if _tmpargs _arg
464         _if=$1
465
466         case $_if in
467         lo0|\
468         stf[0-9]*|\
469         faith[0-9]*|\
470         lp[0-9]*|\
471         sl[0-9]*)
472                 return 1
473                 ;;
474         esac
475         if noafif $_if; then
476                 return 1
477         fi
478         if ! ipv6if $_if; then
479                 return 1
480         fi
481         if checkyesno ipv6_gateway_enable; then
482                 return 1
483         fi
484         _tmpargs=`get_if_var $_if ipv6_prefix_IF`
485         if [ -n "${_tmpargs}" ]; then
486                 return 1
487         fi
488         # backward compatibility: $ipv6_enable
489         case $ipv6_enable in
490         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
491                 return 0
492         ;;
493         esac
494
495         _tmpargs=`_ifconfig_getargs $_if ipv6`
496         for _arg in $_tmpargs; do
497                 case $_arg in
498                 accept_rtadv)
499                         return 0
500                         ;;
501                 esac
502         done
503
504         # backward compatibility: $ipv6_ifconfig_IF
505         _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
506         for _arg in $_tmpargs; do
507                 case $_arg in
508                 accept_rtadv)
509                         return 0
510                         ;;
511                 esac
512         done
513
514         return 1
515 }
516
517 # ifexists if
518 #       Returns 0 if the interface exists and 1 otherwise.
519 ifexists()
520 {
521         [ -z "$1" ] && return 1
522         ifconfig -n $1 > /dev/null 2>&1
523 }
524
525 # ipv4_up if
526 #       add IPv4 addresses to the interface $if
527 ipv4_up()
528 {
529         local _if _ret
530         _if=$1
531         _ret=1
532
533         # Add 127.0.0.1/8 to lo0 unless otherwise specified.
534         if [ "${_if}" = "lo0" ]; then
535                 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
536                 if [ -z "${ifconfig_args}" ]; then
537                         ifconfig ${_if} inet 127.0.0.1/8 alias
538                 fi
539         fi
540         ifalias_up ${_if} inet && _ret=0
541         ipv4_addrs_common ${_if} alias && _ret=0
542
543         return $_ret
544 }
545
546 # ipv6_up if
547 #       add IPv6 addresses to the interface $if
548 ipv6_up()
549 {
550         local _if _ret
551         _if=$1
552         _ret=1
553
554         if ! ipv6if $_if; then
555                 return 0
556         fi
557
558         ifalias_up ${_if} inet6 && _ret=0
559         ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
560         ipv6_accept_rtadv_up ${_if} && _ret=0
561
562         # wait for DAD
563         sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
564         sleep 1
565
566         return $_ret
567 }
568
569 # ipv4_down if
570 #       remove IPv4 addresses from the interface $if
571 ipv4_down()
572 {
573         local _if _ifs _ret inetList oldifs _inet
574         _if=$1
575         _ifs="^"
576         _ret=1
577
578         inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
579
580         oldifs="$IFS"
581         IFS="$_ifs"
582         for _inet in $inetList ; do
583                 # get rid of extraneous line
584                 [ -z "$_inet" ] && break
585
586                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
587
588                 IFS="$oldifs"
589                 ifconfig ${_if} ${_inet} delete
590                 IFS="$_ifs"
591                 _ret=0
592         done
593         IFS="$oldifs"
594
595         ifalias_down ${_if} inet && _ret=0
596         ipv4_addrs_common ${_if} -alias && _ret=0
597
598         return $_ret
599 }
600
601 # ipv6_down if
602 #       remove IPv6 addresses from the interface $if
603 ipv6_down()
604 {
605         local _if _ifs _ret inetList oldifs _inet6
606         _if=$1
607         _ifs="^"
608         _ret=1
609
610         if ! ipv6if $_if; then
611                 return 0
612         fi
613
614         ipv6_accept_rtadv_down ${_if} && _ret=0
615         ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
616         ifalias_down ${_if} inet6 && _ret=0
617
618         inetList="`ifconfig ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
619
620         oldifs="$IFS"
621         IFS="$_ifs"
622         for _inet6 in $inetList ; do
623                 # get rid of extraneous line
624                 [ -z "$_inet6" ] && break
625
626                 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
627
628                 IFS="$oldifs"
629                 ifconfig ${_if} ${_inet6} -alias
630                 IFS="$_ifs"
631                 _ret=0
632         done
633         IFS="$oldifs"
634
635         return $_ret
636 }
637
638 # ipv4_addrs_common if action
639 #       Evaluate the ifconfig_if_ipv4 arguments for interface $if and
640 #       use $action to add or remove IPv4 addresses from $if.
641 ipv4_addrs_common()
642 {
643         local _ret _if _action _cidr _cidr_addr
644         local _ipaddr _netmask _range _ipnet _iplow _iphigh _ipcount
645         _ret=1
646         _if=$1
647         _action=$2
648
649         # get ipv4-addresses
650         cidr_addr=`get_if_var $_if ipv4_addrs_IF`
651
652         for _cidr in ${cidr_addr}; do
653                 _ipaddr=${_cidr%%/*}
654                 _netmask="/"${_cidr##*/}
655                 _range=${_ipaddr##*.}
656                 _ipnet=${_ipaddr%.*}
657                 _iplow=${_range%-*}
658                 _iphigh=${_range#*-}
659
660                 # clear netmask when removing aliases
661                 if [ "${_action}" = "-alias" ]; then
662                         _netmask=""
663                 fi
664
665                 _ipcount=${_iplow}
666                 while [ "${_ipcount}" -le "${_iphigh}" ]; do
667                         eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
668                         _ipcount=$((${_ipcount}+1))
669                         _ret=0
670
671                         # only the first ipaddr in a subnet need the real netmask
672                         if [ "${_action}" != "-alias" ]; then
673                                 _netmask="/32"
674                         fi
675                 done
676         done
677
678         return $_ret
679 }
680
681 # ifalias_up if af
682 #       Configure aliases for network interface $if.
683 #       It returns 0 if at least one alias was configured or
684 #       1 if there were none.
685 #
686 ifalias_up()
687 {
688         local _ret
689         _ret=1
690
691         case "$2" in
692         inet)
693                 _ret=`ifalias_ipv4_up "$1"`
694                 ;;
695         inet6)
696                 _ret=`ifalias_ipv6_up "$1"`
697                 ;;
698         esac
699
700         return $_ret
701 }
702
703 # ifalias_ipv4_up if
704 #       Helper function for ifalias_up().  Handles IPv4.
705 #
706 ifalias_ipv4_up()
707 {
708         local _ret alias ifconfig_args
709         _ret=1
710
711         # ifconfig_IF_aliasN which starts with "inet"
712         alias=0
713         while : ; do
714                 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
715                 case "${ifconfig_args}" in
716                 inet\ *)
717                         ifconfig $1 ${ifconfig_args} alias && _ret=0
718                         ;;
719                 inet6\ *)
720                         ;;
721                 "")
722                         break
723                         ;;
724                 *)
725                         warn "\$ifconfig_$1_alias${alias} needs " \
726                             "\"inet\" keyword for an IPv4 address."
727                         ifconfig $1 ${ifconfig_args} alias && _ret=0
728                         ;;
729                 esac
730                 alias=$((${alias} + 1))
731         done
732
733         return $_ret
734 }
735
736 # ifalias_ipv6_up if
737 #       Helper function for ifalias_up().  Handles IPv6.
738 #
739 ifalias_ipv6_up()
740 {
741         local _ret alias ifconfig_args
742         _ret=1
743
744         # ifconfig_IF_aliasN which starts with "inet6"
745         alias=0
746         while : ; do
747                 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
748                 case "${ifconfig_args}" in
749                 inet6\ *)
750                         ifconfig $1 ${ifconfig_args} alias && _ret=0
751                         ;;
752                 "")
753                         break
754                         ;;
755                 esac
756                 alias=$((${alias} + 1))
757         done
758
759         # backward compatibility: ipv6_ifconfig_IF_aliasN.
760         alias=0
761         while : ; do
762                 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
763                 case "${ifconfig_args}" in
764                 "")
765                         break
766                         ;;
767                 *)
768                         ifconfig $1 inet6 ${ifconfig_args} alias && _ret=0
769                         warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
770                             "  Use ifconfig_$1_aliasN instead."
771                         ;;
772                 esac
773                 alias=$((${alias} + 1))
774         done
775
776         return $_ret
777 }
778
779 # ifalias_down if af
780 #       Remove aliases for network interface $if.
781 #       It returns 0 if at least one alias was removed or
782 #       1 if there were none.
783 #
784 ifalias_down()
785 {
786         local _ret
787         _ret=1
788
789         case "$2" in
790         inet)
791                 _ret=`ifalias_ipv4_down "$1"`
792                 ;;
793         inet6)
794                 _ret=`ifalias_ipv6_down "$1"`
795                 ;;
796         esac
797
798         return $_ret
799 }
800
801 # ifalias_ipv4_down if
802 #       Helper function for ifalias_down().  Handles IPv4.
803 #
804 ifalias_ipv4_down()
805 {
806         local _ret alias ifconfig_args
807         _ret=1
808
809         # ifconfig_IF_aliasN which starts with "inet"
810         alias=0
811         while : ; do
812                 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
813                 case "${ifconfig_args}" in
814                 inet\ *)
815                         ifconfig $1 ${ifconfig_args} -alias && _ret=0
816                         ;;
817                 "")
818                         break
819                         ;;
820                 esac
821                 alias=$((${alias} + 1))
822         done
823
824         return $_ret
825 }
826
827 # ifalias_ipv6_down if
828 #       Helper function for ifalias_down().  Handles IPv6.
829 #
830 ifalias_ipv6_down()
831 {
832         local _ret alias ifconfig_args
833         _ret=1
834
835         # ifconfig_IF_aliasN which starts with "inet6"
836         alias=0
837         while : ; do
838                 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
839                 case "${ifconfig_args}" in
840                 inet6\ *)
841                         ifconfig $1 ${ifconfig_args} -alias && _ret=0
842                         ;;
843                 "")
844                         break
845                         ;;
846                 esac
847                 alias=$((${alias} + 1))
848         done
849
850         # backward compatibility: ipv6_ifconfig_IF_aliasN.
851         alias=0
852         while : ; do
853                 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF_alias${alias}`
854                 case "${ifconfig_args}" in
855                 "")
856                         break
857                         ;;
858                 *)
859                         ifconfig $1 inet6 ${ifconfig_args} -alias && _ret=0
860                         warn "\$ipv6_ifconfig_$1_alias${alias} is obsolete." \
861                             "  Use ifconfig_$1_aliasN instead."
862                         ;;
863                 esac
864                 alias=$((${alias} + 1))
865         done
866
867         return $_ret
868 }
869
870 # ipv6_prefix_hostid_addr_common if action
871 #       Add or remove IPv6 prefix + hostid addr on the interface $if
872 #
873 ipv6_prefix_hostid_addr_common()
874 {
875         local _if _action prefix laddr hostid j address
876         _if=$1
877         _action=$2
878         prefix=`get_if_var ${_if} ipv6_prefix_IF`
879
880         if [ -n "${prefix}" ]; then
881                 laddr=`network6_getladdr ${_if}`
882                 hostid=${laddr#fe80::}
883                 hostid=${hostid%\%*}
884
885                 for j in ${prefix}; do
886                         address=$j\:${hostid}
887                         ifconfig ${_if} inet6 ${address} prefixlen 64 ${_action}
888
889                         # if I am a router, add subnet router
890                         # anycast address (RFC 2373).
891                         if checkyesno ipv6_gateway_enable; then
892                                 ifconfig ${_if} inet6 $j:: prefixlen 64 \
893                                         ${_action} anycast
894                         fi
895                 done
896         fi
897 }
898
899 # ipv6_accept_rtadv_up if
900 #       Enable accepting Router Advertisement and send Router
901 #       Solicitation message
902 ipv6_accept_rtadv_up()
903 {
904         if ipv6_autoconfif $1; then
905                 ifconfig $1 inet6 accept_rtadv up
906                 if ! checkyesno rtsold_enable; then
907                         rtsol ${rtsol_flags} $1
908                 fi
909         fi
910 }
911
912 # ipv6_accept_rtadv_down if
913 #       Disable accepting Router Advertisement
914 ipv6_accept_rtadv_down()
915 {
916         if ipv6_autoconfif $1; then
917                 ifconfig $1 inet6 -accept_rtadv
918         fi
919 }
920
921 # ifscript_up if
922 #       Evaluate a startup script for the $if interface.
923 #       It returns 0 if a script was found and processed or
924 #       1 if no script was found.
925 #
926 ifscript_up()
927 {
928         if [ -r /etc/start_if.$1 ]; then
929                 . /etc/start_if.$1
930                 return 0
931         else
932                 return 1
933         fi
934 }
935
936 # ifscript_down if
937 #       Evaluate a shutdown script for the $if interface.
938 #       It returns 0 if a script was found and processed or
939 #       1 if no script was found.
940 #
941 ifscript_down()
942 {
943         if [ -r /etc/stop_if.$1 ]; then
944                 . /etc/stop_if.$1
945                 return 0
946         else
947                 return 1
948         fi
949 }
950
951 # clone_up
952 #       Create cloneable interfaces.
953 #
954 clone_up()
955 {
956         local _prefix _list ifn
957         _prefix=
958         _list=
959
960         # create_args_IF
961         for ifn in ${cloned_interfaces}; do
962                 ifconfig ${ifn} create `get_if_var ${ifn} create_args_IF`
963                 if [ $? -eq 0 ]; then
964                         _list="${_list}${_prefix}${ifn}"
965                         [ -z "$_prefix" ] && _prefix=' '
966                 fi
967         done
968         debug "Cloned: ${_list}"
969 }
970
971 # clone_down
972 #       Destroy cloned interfaces. Destroyed interfaces are echoed to
973 #       standard output.
974 #
975 clone_down()
976 {
977         local _prefix _list ifn
978         _prefix=
979         _list=
980
981         for ifn in ${cloned_interfaces}; do
982                 ifconfig -n ${ifn} destroy
983                 if [ $? -eq 0 ]; then
984                         _list="${_list}${_prefix}${ifn}"
985                         [ -z "$_prefix" ] && _prefix=' '
986                 fi
987         done
988         debug "Destroyed clones: ${_list}"
989 }
990
991 # childif_create
992 #       Create and configure child interfaces.  Return 0 if child
993 #       interfaces are created.
994 #
995 childif_create()
996 {
997         local cfg child child_vlans child_wlans create_args debug_flags ifn i
998         cfg=1
999         ifn=$1
1000
1001         # Create wireless interfaces
1002         child_wlans=`get_if_var $ifn wlans_IF`
1003
1004         for child in ${child_wlans}; do
1005                 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1006                 debug_flags="`get_if_var $child wlandebug_IF`"
1007
1008                 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1009                         ifconfig $child create ${create_args} && cfg=0
1010                         if [ -n "${debug_flags}" ]; then
1011                                 wlandebug -i $child ${debug_flags}
1012                         fi
1013                 else
1014                         i=`ifconfig wlan create ${create_args}`
1015                         if [ -n "${debug_flags}" ]; then
1016                                 wlandebug -i $i ${debug_flags}
1017                         fi
1018                         ifconfig $i name $child && cfg=0
1019                 fi
1020                 if autoif $child; then
1021                         ifn_start $child
1022                 fi
1023         done
1024
1025         # Create vlan interfaces
1026         child_vlans=`get_if_var $ifn vlans_IF`
1027
1028         if [ -n "${child_vlans}" ]; then
1029                 load_kld if_vlan
1030         fi
1031
1032         for child in ${child_vlans}; do
1033                 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1034                         child="${ifn}.${child}"
1035                         create_args=`get_if_var $child create_args_IF`
1036                         ifconfig $child create ${create_args} && cfg=0
1037                 else
1038                         create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1039                         if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1040                                 ifconfig $child create ${create_args} && cfg=0
1041                         else
1042                                 i=`ifconfig vlan create ${create_args}`
1043                                 ifconfig $i name $child && cfg=0
1044                         fi
1045                 fi
1046                 if autoif $child; then
1047                         ifn_start $child
1048                 fi
1049         done
1050
1051         return ${cfg}
1052 }
1053
1054 # childif_destroy
1055 #       Destroy child interfaces.
1056 #
1057 childif_destroy()
1058 {
1059         local cfg child child_vlans child_wlans ifn
1060         cfg=1
1061
1062         child_wlans=`get_if_var $ifn wlans_IF`
1063         for child in ${child_wlans}; do
1064                 if ! ifexists $child; then
1065                         continue
1066                 fi
1067                 ifconfig -n $child destroy && cfg=0
1068         done
1069
1070         child_vlans=`get_if_var $ifn vlans_IF`
1071         for child in ${child_vlans}; do
1072                 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1073                         child="${ifn}.${child}"
1074                 fi
1075                 if ! ifexists $child; then
1076                         continue
1077                 fi
1078                 ifconfig -n $child destroy && cfg=0
1079         done
1080
1081         return ${cfg}
1082 }
1083
1084 # ng_mkpeer
1085 #       Create netgraph nodes.
1086 #
1087 ng_mkpeer()
1088 {
1089         ngctl -f - 2> /dev/null <<EOF
1090 mkpeer $*
1091 msg dummy nodeinfo
1092 EOF
1093 }
1094
1095 # ng_create_one
1096 #       Create netgraph nodes.
1097 #
1098 ng_create_one()
1099 {
1100         local t
1101
1102         ng_mkpeer $* | while read line; do
1103                 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1104                 if [ -n "${t}" ]; then
1105                         echo ${t}
1106                         return
1107                 fi
1108         done
1109 }
1110
1111 # gif_up
1112 #       Create gif(4) tunnel interfaces.
1113 gif_up()
1114 {
1115         local i peers
1116
1117         for i in ${gif_interfaces}; do
1118                 peers=`get_if_var $i gifconfig_IF`
1119                 case ${peers} in
1120                 '')
1121                         continue
1122                         ;;
1123                 *)
1124                         if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1125                                 ifconfig $i create >/dev/null 2>&1
1126                         else
1127                                 gif=`ifconfig gif create`
1128                                 ifconfig $gif name $i
1129                         fi
1130                         ifconfig $i tunnel ${peers}
1131                         ifconfig $i up
1132                         ;;
1133                 esac
1134         done
1135 }
1136
1137 # ng_fec_create ifn
1138 #       Configure Fast EtherChannel for interface $ifn. Returns 0 if
1139 #       FEC arguments were found and configured; returns !0 otherwise.
1140 ng_fec_create()
1141 {
1142          local req_iface iface bogus
1143          req_iface="$1"
1144
1145          ngctl shutdown ${req_iface}: > /dev/null 2>&1
1146
1147          bogus=""
1148          while true; do
1149                  iface=`ng_create_one fec dummy fec`
1150                  if [ -z "${iface}" ]; then
1151                          exit 2
1152                  fi
1153                  if [ "${iface}" = "${req_iface}" ]; then
1154                          break
1155                  fi
1156                  bogus="${bogus} ${iface}"
1157          done
1158
1159          for iface in ${bogus}; do
1160                  ngctl shutdown ${iface}:
1161          done
1162 }
1163
1164 # fec_up
1165 #       Create Fast EtherChannel interfaces.
1166 fec_up()
1167 {
1168         local i j
1169
1170         for i in ${fec_interfaces}; do
1171                 ng_fec_create $i
1172                 for j in `get_if_var $i fecconfig_IF`; do
1173                         case ${j} in
1174                         '')
1175                                 continue
1176                                 ;;
1177                         *)
1178                                 ngctl msg ${i}: add_iface "\"${j}\""
1179                                 ;;
1180                         esac
1181                 done
1182         done
1183 }
1184
1185 # ipx_up ifn
1186 #       Configure any IPX addresses for interface $ifn. Returns 0 if
1187 #       IPX arguments were found and configured; returns 1 otherwise.
1188 #
1189 ipx_up()
1190 {
1191         local ifn
1192         ifn="$1"
1193
1194         # ifconfig_IF_ipx
1195         ifconfig_args=`_ifconfig_getargs $ifn ipx`
1196         if [ -n "${ifconfig_args}" ]; then
1197                 ifconfig ${ifn} ${ifconfig_args}
1198                 return 0
1199         fi
1200
1201         return 1
1202 }
1203
1204 # ipx_down ifn
1205 #       Remove IPX addresses for interface $ifn. Returns 0 if IPX
1206 #       addresses were found and unconfigured. It returns 1, otherwise.
1207 #
1208 ipx_down()
1209 {
1210         local _if _ifs _ret ipxList oldifs _ipx
1211         _if=$1
1212         _ifs="^"
1213         _ret=1
1214         ipxList="`ifconfig ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1215         oldifs="$IFS"
1216
1217         IFS="$_ifs"
1218         for _ipx in $ipxList ; do
1219                 # get rid of extraneous line
1220                 [ -z "$_ipx" ] && break
1221
1222                 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1223
1224                 IFS="$oldifs"
1225                 ifconfig ${_if} ${_ipx} delete
1226                 IFS="$_ifs"
1227                 _ret=0
1228         done
1229         IFS="$oldifs"
1230
1231         return $_ret
1232 }
1233
1234 # ifnet_rename
1235 #       Rename all requested interfaces.
1236 #
1237 ifnet_rename()
1238 {
1239         local _if _ifname
1240
1241         # ifconfig_IF_name
1242         for _if in `ifconfig -l`; do
1243                 _ifname=`get_if_var $_if ifconfig_IF_name`
1244                 if [ ! -z "$_ifname" ]; then
1245                         ifconfig $_if name $_ifname
1246                 fi
1247         done
1248
1249         return 0
1250 }
1251
1252 # list_net_interfaces type
1253 #       List all network interfaces. The type of interface returned
1254 #       can be controlled by the type argument. The type
1255 #       argument can be any of the following:
1256 #               nodhcp  - all interfaces, excluding DHCP configured interfaces
1257 #               dhcp    - list only DHCP configured interfaces
1258 #               noautoconf      - all interfaces, excluding IPv6 Stateless
1259 #                                 Address Autoconf configured interfaces
1260 #               autoconf        - list only IPv6 Stateless Address Autoconf
1261 #                                 configured interfaces
1262 #       If no argument is specified all network interfaces are output.
1263 #       Note that the list will include cloned interfaces if applicable.
1264 #       Cloned interfaces must already exist to have a chance to appear
1265 #       in the list if ${network_interfaces} is set to `auto'.
1266 #
1267 list_net_interfaces()
1268 {
1269         local type _tmplist _list _autolist _lo _if
1270         type=$1
1271
1272         # Get a list of ALL the interfaces and make lo0 first if it's there.
1273         #
1274         _tmplist=
1275         case ${network_interfaces} in
1276         [Aa][Uu][Tt][Oo])
1277                 _autolist="`ifconfig -l`"
1278                 _lo=
1279                 for _if in ${_autolist} ; do
1280                         if autoif $_if; then
1281                                 if [ "$_if" = "lo0" ]; then
1282                                         _lo="lo0 "
1283                                 else
1284                                         _tmplist="${_tmplist} ${_if}"
1285                                 fi
1286                         fi
1287                 done
1288                 _tmplist="${_lo}${_tmplist# }"
1289                 ;;
1290         *)
1291                 _tmplist="${network_interfaces} ${cloned_interfaces}"
1292
1293                 # lo0 is effectively mandatory, so help prevent foot-shooting
1294                 #
1295                 case "$_tmplist" in
1296                 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1297                 *)      _tmplist="lo0 ${_tmplist}" ;;
1298                 esac
1299                 ;;
1300         esac
1301
1302         _list=
1303         case "$type" in
1304         nodhcp)
1305                 for _if in ${_tmplist} ; do
1306                         if ! dhcpif $_if && \
1307                            [ -n "`_ifconfig_getargs $_if`" ]; then
1308                                 _list="${_list# } ${_if}"
1309                         fi
1310                 done
1311                 ;;
1312         dhcp)
1313                 for _if in ${_tmplist} ; do
1314                         if dhcpif $_if; then
1315                                 _list="${_list# } ${_if}"
1316                         fi
1317                 done
1318                 ;;
1319         noautoconf)
1320                 for _if in ${_tmplist} ; do
1321                         if ! ipv6_autoconfif $_if && \
1322                            [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1323                                 _list="${_list# } ${_if}"
1324                         fi
1325                 done
1326                 ;;
1327         autoconf)
1328                 for _if in ${_tmplist} ; do
1329                         if ipv6_autoconfif $_if; then
1330                                 _list="${_list# } ${_if}"
1331                         fi
1332                 done
1333                 ;;
1334         *)
1335                 _list=${_tmplist}
1336                 ;;
1337         esac
1338
1339         echo $_list
1340
1341         return 0
1342 }
1343
1344 # get_default_if -address_family
1345 #       Get the interface of the default route for the given address family.
1346 #       The -address_family argument must be suitable passing to route(8).
1347 #
1348 get_default_if()
1349 {
1350         local routeget oldifs defif line
1351         defif=
1352         oldifs="$IFS"
1353         IFS="
1354 "
1355         for line in `route -n get $1 default 2>/dev/null`; do
1356                 case $line in
1357                 *interface:*)
1358                         defif=${line##*: }
1359                         ;;
1360                 esac
1361         done
1362         IFS=${oldifs}
1363
1364         echo $defif
1365 }
1366
1367 # hexdigit arg
1368 #       Echo decimal number $arg (single digit) in hexadecimal format.
1369 hexdigit()
1370 {
1371         printf '%x\n' "$1"
1372 }
1373
1374 # hexprint arg
1375 #       Echo decimal number $arg (multiple digits) in hexadecimal format.
1376 hexprint()
1377 {
1378         printf '%x\n' "$1"
1379 }
1380
1381 is_wired_interface()
1382 {
1383         local media
1384
1385         case `ifconfig $1 2>/dev/null` in
1386         *media:?Ethernet*) media=Ethernet ;;
1387         esac
1388
1389         test "$media" = "Ethernet"
1390 }
1391
1392 # network6_getladdr if [flag]
1393 #       Echo link-local address from $if if any.
1394 #       If flag is defined, tentative ones will be excluded.
1395 network6_getladdr()
1396 {
1397         local proto addr rest
1398         ifconfig $1 2>/dev/null | while read proto addr rest; do
1399                 case ${proto} in
1400                 inet6)
1401                         case ${addr} in
1402                         fe80::*)
1403                                 if [ -z "$2" ]; then
1404                                         echo ${addr}
1405                                         return
1406                                 fi
1407                                 case ${rest} in
1408                                 *tentative*)
1409                                         continue
1410                                         ;;
1411                                 *)
1412                                         echo ${addr}
1413                                         return
1414                                 esac
1415                         esac
1416                 esac
1417         done
1418 }