]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/network.subr
Fix multiple vulnerabilities in NFS server code. [SA-18:13.nfs]
[FreeBSD/FreeBSD.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 IFCONFIG_CMD="/sbin/ifconfig"
28 : ${netif_ipexpand_max:=2048}
29
30 #
31 # Subroutines commonly used from network startup scripts.
32 # Requires that rc.conf be loaded first.
33 #
34
35 # ifn_start ifn
36 #       Bring up and configure an interface.  If some configuration is
37 #       applied, print the interface configuration.
38 #
39 ifn_start()
40 {
41         local ifn cfg
42         ifn="$1"
43         cfg=1
44
45         [ -z "$ifn" ] && err 1 "ifn_start called without an interface"
46
47         ifscript_up ${ifn} && cfg=0
48         ifconfig_up ${ifn} && cfg=0
49         if ! noafif $ifn; then
50                 afexists inet && ipv4_up ${ifn} && cfg=0
51                 afexists inet6 && ipv6_up ${ifn} && cfg=0
52         fi
53         childif_create ${ifn} && cfg=0
54
55         return $cfg
56 }
57
58 # ifn_stop ifn
59 #       Shutdown and de-configure an interface.  If action is taken,
60 #       print the interface name.
61 #
62 ifn_stop()
63 {
64         local ifn cfg
65         ifn="$1"
66         cfg=1
67
68         [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
69
70         if ! noafif $ifn; then
71                 afexists inet6 && ipv6_down ${ifn} && cfg=0
72                 afexists inet && ipv4_down ${ifn} && cfg=0
73         fi
74         ifconfig_down ${ifn} && cfg=0
75         ifscript_down ${ifn} && cfg=0
76         childif_destroy ${ifn} && cfg=0
77
78         return $cfg
79 }
80
81 # ifn_vnetup ifn
82 #       Move ifn to the specified vnet jail.
83 #
84 ifn_vnetup()
85 {
86
87         ifn_vnet0 $1 vnet
88 }
89
90 # ifn_vnetdown ifn
91 #       Reclaim ifn from the specified vnet jail.
92 #
93 ifn_vnetdown()
94 {
95
96         ifn_vnet0 $1 -vnet
97 }
98
99 # ifn_vnet0 ifn action
100 #       Helper function for ifn_vnetup and ifn_vnetdown.
101 #
102 ifn_vnet0()
103 {
104         local _ifn _cfg _action _vnet
105         _ifn="$1"
106         _action="$2"
107         _cfg=1
108
109         if _vnet=$(vnetif $_ifn); then
110                 ${IFCONFIG_CMD} $_ifn $_action $_vnet && _cfg=0
111         fi
112
113         return $_cfg
114 }
115
116 # ifconfig_up if
117 #       Evaluate ifconfig(8) arguments for interface $if and
118 #       run ifconfig(8) with those arguments. It returns 0 if
119 #       arguments were found and executed or 1 if the interface
120 #       had no arguments.  Pseudo arguments DHCP and WPA are handled
121 #       here.
122 #
123 ifconfig_up()
124 {
125         local _cfg _ifconfig_descr _ipv6_opts ifconfig_args 
126         _cfg=1
127
128         # Make sure lo0 always comes up.
129         if [ "$1" = "lo0" ]; then
130                 _cfg=0
131         fi
132
133         # inet6 specific
134         if ! noafif $1 && afexists inet6; then
135                 if checkyesno ipv6_activate_all_interfaces; then
136                         _ipv6_opts="-ifdisabled"
137                 elif [ "$1" != "lo0" ]; then
138                         _ipv6_opts="ifdisabled"
139                 fi
140
141                 # backward compatibility: $ipv6_enable
142                 case $ipv6_enable in
143                 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
144                         case $1 in
145                         bridge[0-9]*)
146                                 # No accept_rtadv by default on if_bridge(4)
147                                 # to avoid a conflict with the member
148                                 # interfaces.
149                         ;;
150                         *)
151                                 if ! checkyesno ipv6_gateway_enable; then
152                                         _ipv6_opts="${_ipv6_opts} accept_rtadv"
153                                 fi
154                         ;;
155                         esac
156                 ;;
157                 esac
158
159                 case $ipv6_cpe_wanif in
160                 $1)
161                         _ipv6_opts="${_ipv6_opts} -no_radr accept_rtadv"
162                 ;;
163                 esac
164
165                 if [ -n "${_ipv6_opts}" ]; then
166                         ${IFCONFIG_CMD} $1 inet6 ${_ipv6_opts}
167                 fi
168         fi
169
170         # ifconfig_IF
171         ifconfig_args=`ifconfig_getargs $1`
172         if [ -n "${ifconfig_args}" ]; then
173                 eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
174                 _cfg=0
175         fi
176
177         # inet6 specific
178         if ! noafif $1 && afexists inet6; then
179                 # ifconfig_IF_ipv6
180                 ifconfig_args=`ifconfig_getargs $1 ipv6`
181                 if [ -n "${ifconfig_args}" ]; then
182                         # backward compatibility: inet6 keyword
183                         case "${ifconfig_args}" in
184                         :*|[0-9a-fA-F]*:*)
185                                 warn "\$ifconfig_$1_ipv6 needs leading" \
186                                     "\"inet6\" keyword for an IPv6 address."
187                                 ifconfig_args="inet6 ${ifconfig_args}"
188                         ;;
189                         esac
190                         ${IFCONFIG_CMD} $1 inet6 -ifdisabled
191                         eval ${IFCONFIG_CMD} $1 ${ifconfig_args}
192                         _cfg=0
193                 fi
194
195                 # $ipv6_prefix_IF will be handled in
196                 # ipv6_prefix_hostid_addr_common().
197                 ifconfig_args=`get_if_var $1 ipv6_prefix_IF`
198                 if [ -n "${ifconfig_args}" ]; then
199                         ${IFCONFIG_CMD} $1 inet6 -ifdisabled
200                         _cfg=0
201                 fi
202
203                 # backward compatibility: $ipv6_ifconfig_IF
204                 ifconfig_args=`get_if_var $1 ipv6_ifconfig_IF`
205                 if [ -n "${ifconfig_args}" ]; then
206                         warn "\$ipv6_ifconfig_$1 is obsolete." \
207                             "  Use ifconfig_$1_ipv6 instead."
208                         ${IFCONFIG_CMD} $1 inet6 -ifdisabled
209                         eval ${IFCONFIG_CMD} $1 inet6 ${ifconfig_args}
210                         _cfg=0
211                 fi
212         fi
213
214         ifalias $1 link alias
215         ifalias $1 ether alias
216
217         _ifconfig_descr=`get_if_var $1 ifconfig_IF_descr`
218         if [ -n "${_ifconfig_descr}" ]; then
219                 ${IFCONFIG_CMD} $1 description "${_ifconfig_descr}"
220         fi
221
222         if wpaif $1; then
223                 /etc/rc.d/wpa_supplicant start $1
224                 _cfg=0          # XXX: not sure this should count
225         elif hostapif $1; then
226                 /etc/rc.d/hostapd start $1
227                 _cfg=0
228         elif [ ${_cfg} -eq 0 ]; then
229                 ${IFCONFIG_CMD} $1 up
230         fi
231
232         if dhcpif $1; then
233                 if [ $_cfg -ne 0 ] ; then
234                         ${IFCONFIG_CMD} $1 up
235                 fi
236                 if syncdhcpif $1; then
237                         /etc/rc.d/dhclient start $1
238                 fi
239                 _cfg=0
240         fi
241
242         return $_cfg
243 }
244
245 # ifconfig_down if
246 #       returns 1 if wpa_supplicant or dhclient was stopped or
247 #       the interface exists.
248 #
249 ifconfig_down()
250 {
251         local _cfg
252         _cfg=1
253
254         if wpaif $1; then
255                 /etc/rc.d/wpa_supplicant stop $1
256                 _cfg=0
257         elif hostapif $1; then
258                 /etc/rc.d/hostapd stop $1
259                 _cfg=0
260         fi
261
262         if dhcpif $1; then
263                 /etc/rc.d/dhclient stop $1
264                 _cfg=0
265         fi
266
267         if ifexists $1; then
268                 ${IFCONFIG_CMD} $1 down
269                 _cfg=0
270         fi
271
272         return $_cfg
273 }
274
275 # get_if_var if var [default]
276 #       Return the value of the pseudo-hash corresponding to $if where
277 #       $var is a string containg the sub-string "IF" which will be
278 #       replaced with $if after the characters defined in _punct are
279 #       replaced with '_'. If the variable is unset, replace it with
280 #       $default if given.
281 get_if_var()
282 {
283         local _if _punct _punct_c _var _default prefix suffix
284
285         if [ $# -ne 2 -a $# -ne 3 ]; then
286                 err 3 'USAGE: get_if_var name var [default]'
287         fi
288
289         _if=$1
290         _punct=".-/+"
291         ltr ${_if} "${_punct}" '_' _if
292         _var=$2
293         _default=$3
294
295         prefix=${_var%%IF*}
296         suffix=${_var##*IF}
297         eval echo \${${prefix}${_if}${suffix}-${_default}}
298 }
299
300 # _ifconfig_getargs if [af]
301 #       Prints the arguments for the supplied interface to stdout.
302 #       Returns 1 if empty.  In general, ifconfig_getargs should be used
303 #       outside this file.
304 _ifconfig_getargs()
305 {
306         local _ifn _af
307         _ifn=$1
308         _af=${2+_$2}
309
310         if [ -z "$_ifn" ]; then
311                 return 1
312         fi
313
314         get_if_var $_ifn ifconfig_IF$_af "$ifconfig_DEFAULT"
315 }
316
317 # ifconfig_getargs if [af]
318 #       Takes the result from _ifconfig_getargs and removes pseudo
319 #       args such as DHCP and WPA.
320 ifconfig_getargs()
321 {
322         local _tmpargs _arg _args _vnet
323         _tmpargs=`_ifconfig_getargs $1 $2`
324         if [ $? -eq 1 ]; then
325                 return 1
326         fi
327         _args=
328         _vnet=0
329
330         for _arg in $_tmpargs; do
331                 case $_arg:$_vnet in
332                 [Dd][Hh][Cc][Pp]:0) ;;
333                 [Nn][Oo][Aa][Uu][Tt][Oo]:0) ;;
334                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
335                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]:0) ;;
336                 [Ww][Pp][Aa]:0) ;;
337                 [Hh][Oo][Ss][Tt][Aa][Pp]:0) ;;
338                 vnet:0) _vnet=1 ;;
339                 *:1)    _vnet=0 ;;
340                 *:0)
341                         _args="$_args $_arg"
342                 ;;
343                 esac
344         done
345
346         echo $_args
347 }
348
349 # autoif
350 #       Returns 0 if the interface should be automatically configured at
351 #       boot time and 1 otherwise.
352 autoif()
353 {
354         local _tmpargs _arg
355         _tmpargs=`_ifconfig_getargs $1`
356
357         for _arg in $_tmpargs; do
358                 case $_arg in
359                 [Nn][Oo][Aa][Uu][Tt][Oo])
360                         return 1
361                         ;;
362                 esac
363         done
364
365         return 0
366 }
367
368 # dhcpif if
369 #       Returns 0 if the interface is a DHCP interface and 1 otherwise.
370 dhcpif()
371 {
372         local _tmpargs _arg
373         _tmpargs=`_ifconfig_getargs $1`
374
375         case $1 in
376         lo[0-9]*|\
377         stf[0-9]*|\
378         lp[0-9]*|\
379         sl[0-9]*)
380                 return 1
381                 ;;
382         esac
383         if noafif $1; then
384                 return 1
385         fi
386
387         for _arg in $_tmpargs; do
388                 case $_arg in
389                 [Dd][Hh][Cc][Pp])
390                         return 0
391                         ;;
392                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
393                         return 0
394                         ;;
395                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
396                         return 0
397                         ;;
398                 esac
399         done
400
401         return 1
402 }
403
404 # syncdhcpif
405 #       Returns 0 if the interface should be configured synchronously and
406 #       1 otherwise.
407 syncdhcpif()
408 {
409         local _tmpargs _arg
410         _tmpargs=`_ifconfig_getargs $1`
411
412         if noafif $1; then
413                 return 1
414         fi
415
416         for _arg in $_tmpargs; do
417                 case $_arg in
418                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
419                         return 1
420                         ;;
421                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
422                         return 0
423                         ;;
424                 esac
425         done
426
427         checkyesno synchronous_dhclient
428 }
429
430 # wpaif if
431 #       Returns 0 if the interface is a WPA interface and 1 otherwise.
432 wpaif()
433 {
434         local _tmpargs _arg
435         _tmpargs=`_ifconfig_getargs $1`
436
437         for _arg in $_tmpargs; do
438                 case $_arg in
439                 [Ww][Pp][Aa])
440                         return 0
441                         ;;
442                 esac
443         done
444
445         return 1
446 }
447
448 # hostapif if
449 #       Returns 0 if the interface is a HOSTAP interface and 1 otherwise.
450 hostapif()
451 {
452         local _tmpargs _arg
453         _tmpargs=`_ifconfig_getargs $1`
454
455         for _arg in $_tmpargs; do
456                 case $_arg in
457                 [Hh][Oo][Ss][Tt][Aa][Pp])
458                         return 0
459                         ;;
460                 esac
461         done
462
463         return 1
464 }
465
466 # vnetif if
467 #       Returns 0 and echo jail if "vnet" keyword is specified on the
468 #       interface, and 1 otherwise.
469 vnetif()
470 {
471         local _tmpargs _arg _vnet
472         _tmpargs=`_ifconfig_getargs $1`
473
474         _vnet=0
475         for _arg in $_tmpargs; do
476                 case $_arg:$_vnet in
477                 vnet:0) _vnet=1 ;;
478                 *:1)    echo $_arg; return 0 ;;
479                 esac
480         done
481
482         return 1
483 }
484
485 # afexists af
486 #       Returns 0 if the address family is enabled in the kernel
487 #       1 otherwise.
488 afexists()
489 {
490         local _af
491         _af=$1
492
493         case ${_af} in
494         inet|inet6)
495                 check_kern_features ${_af}
496                 ;;
497         atm)
498                 if [ -x /sbin/atmconfig ]; then
499                         /sbin/atmconfig diag list > /dev/null 2>&1
500                 else
501                         return 1
502                 fi
503                 ;;
504         link|ether)
505                 return 0
506                 ;;
507         *)
508                 err 1 "afexists(): Unsupported address family: $_af"
509                 ;;
510         esac
511 }
512
513 # noafif if
514 #       Returns 0 if the interface has no af configuration and 1 otherwise.
515 noafif()
516 {
517         local _if
518         _if=$1
519
520         case $_if in
521         pflog[0-9]*|\
522         pfsync[0-9]*|\
523         usbus[0-9]*|\
524         an[0-9]*|\
525         ath[0-9]*|\
526         ipw[0-9]*|\
527         ipfw[0-9]*|\
528         iwi[0-9]*|\
529         iwn[0-9]*|\
530         ral[0-9]*|\
531         wi[0-9]*|\
532         wl[0-9]*|\
533         wpi[0-9]*)
534                 return 0
535                 ;;
536         esac
537
538         return 1
539 }
540
541 # ipv6if if
542 #       Returns 0 if the interface should be configured for IPv6 and
543 #       1 otherwise.
544 ipv6if()
545 {
546         local _if _tmpargs i
547         _if=$1
548
549         if ! afexists inet6; then
550                 return 1
551         fi
552
553         # lo0 is always IPv6-enabled
554         case $_if in
555         lo0)
556                 return 0
557                 ;;
558         esac
559
560         case "${ipv6_network_interfaces}" in
561         $_if|"$_if "*|*" $_if"|*" $_if "*|[Aa][Uu][Tt][Oo])
562                 # True if $ifconfig_IF_ipv6 is defined.
563                 _tmpargs=`_ifconfig_getargs $_if ipv6`
564                 if [ -n "${_tmpargs}" ]; then
565                         return 0
566                 fi
567
568                 # True if $ipv6_prefix_IF is defined.
569                 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
570                 if [ -n "${_tmpargs}" ]; then
571                         return 0
572                 fi
573
574                 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
575                 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
576                 if [ -n "${_tmpargs}" ]; then
577                         return 0
578                 fi
579                 ;;
580         esac
581
582         return 1
583 }
584
585 # ipv6_autoconfif if
586 #       Returns 0 if the interface should be configured for IPv6 with
587 #       Stateless Address Configuration; 1 otherwise.
588 ipv6_autoconfif()
589 {
590         local _if _tmpargs _arg
591         _if=$1
592
593         case $_if in
594         lo[0-9]*|\
595         stf[0-9]*|\
596         lp[0-9]*|\
597         sl[0-9]*)
598                 return 1
599                 ;;
600         esac
601         if noafif $_if; then
602                 return 1
603         fi
604         if ! ipv6if $_if; then
605                 return 1
606         fi
607         if checkyesno ipv6_gateway_enable; then
608                 return 1
609         fi
610         _tmpargs=`get_if_var $_if ipv6_prefix_IF`
611         if [ -n "${_tmpargs}" ]; then
612                 return 1
613         fi
614         # backward compatibility: $ipv6_enable
615         case $ipv6_enable in
616         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
617                 if checkyesno ipv6_gateway_enable; then
618                         return 1
619                 fi
620                 case $1 in
621                 bridge[0-9]*)
622                         # No accept_rtadv by default on if_bridge(4)
623                         # to avoid a conflict with the member
624                         # interfaces.
625                         return 1
626                 ;;
627                 *)
628                         return 0
629                 ;;
630                 esac
631         ;;
632         esac
633
634         _tmpargs=`_ifconfig_getargs $_if ipv6`
635         for _arg in $_tmpargs; do
636                 case $_arg in
637                 accept_rtadv)
638                         return 0
639                         ;;
640                 esac
641         done
642
643         # backward compatibility: $ipv6_ifconfig_IF
644         _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
645         for _arg in $_tmpargs; do
646                 case $_arg in
647                 accept_rtadv)
648                         return 0
649                         ;;
650                 esac
651         done
652
653         return 1
654 }
655
656 # ifexists if
657 #       Returns 0 if the interface exists and 1 otherwise.
658 ifexists()
659 {
660         [ -z "$1" ] && return 1
661         ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
662 }
663
664 # ipv4_up if
665 #       add IPv4 addresses to the interface $if
666 ipv4_up()
667 {
668         local _if _ret
669         _if=$1
670         _ret=1
671
672         # Add 127.0.0.1/8 to lo0 unless otherwise specified.
673         if [ "${_if}" = "lo0" ]; then
674                 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
675                 if [ -z "${ifconfig_args}" ]; then
676                         ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
677                 fi
678         fi
679         ifalias ${_if} inet alias && _ret=0
680
681         return $_ret
682 }
683
684 # ipv6_up if
685 #       add IPv6 addresses to the interface $if
686 ipv6_up()
687 {
688         local _if _ret
689         _if=$1
690         _ret=1
691
692         if ! ipv6if $_if; then
693                 return 0
694         fi
695
696         ifalias ${_if} inet6 alias && _ret=0
697         ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
698         ipv6_accept_rtadv_up ${_if} && _ret=0
699
700         return $_ret
701 }
702
703 # ipv4_down if
704 #       remove IPv4 addresses from the interface $if
705 ipv4_down()
706 {
707         local _if _ifs _ret inetList oldifs _inet
708         _if=$1
709         _ifs="^"
710         _ret=1
711
712         ifalias ${_if} inet -alias && _ret=0
713
714         inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n\t" "$_ifs"`"
715
716         oldifs="$IFS"
717         IFS="$_ifs"
718         for _inet in $inetList ; do
719                 # get rid of extraneous line
720                 case $_inet in
721                 inet\ *)        ;;
722                 *)              continue ;;
723                 esac
724
725                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
726
727                 IFS="$oldifs"
728                 ${IFCONFIG_CMD} ${_if} ${_inet} delete
729                 IFS="$_ifs"
730                 _ret=0
731         done
732         IFS="$oldifs"
733
734         return $_ret
735 }
736
737 # ipv6_down if
738 #       remove IPv6 addresses from the interface $if
739 ipv6_down()
740 {
741         local _if _ifs _ret inetList oldifs _inet6
742         _if=$1
743         _ifs="^"
744         _ret=1
745
746         if ! ipv6if $_if; then
747                 return 0
748         fi
749
750         ipv6_accept_rtadv_down ${_if} && _ret=0
751         ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
752         ifalias ${_if} inet6 -alias && _ret=0
753
754         inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
755
756         oldifs="$IFS"
757         IFS="$_ifs"
758         for _inet6 in $inetList ; do
759                 # get rid of extraneous line
760                 case $_inet6 in
761                 inet6\ *)       ;;
762                 *)              continue ;;
763                 esac
764
765                 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
766
767                 IFS="$oldifs"
768                 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
769                 IFS="$_ifs"
770                 _ret=0
771         done
772         IFS="$oldifs"
773
774         return $_ret
775 }
776
777 # ifalias if af action
778 #       Configure or remove aliases for network interface $if.
779 #       It returns 0 if at least one alias was configured or
780 #       removed, or 1 if there were none.
781 #
782 ifalias()
783 {
784         local _ret
785         _ret=1
786
787         afexists $2 || return $_ret
788
789         case "$2" in
790         inet|inet6|link|ether)
791                 ifalias_af_common $1 $2 $3 && _ret=0
792                 ;;
793         esac
794
795         return $_ret
796 }
797
798 # ifalias_expand_addr af action addr
799 #       Expand address range ("N-M") specification in addr.
800 #       "addr" must not include an address-family keyword.
801 #       The results will include an address-family keyword.
802 #
803 ifalias_expand_addr()
804 {
805         local _af _action
806
807         _af=$1
808         _action=$2
809         shift 2
810
811         afexists $_af || return
812         ifalias_expand_addr_$_af $_action $*
813 }
814
815 # ifalias_expand_addr_inet action addr
816 #       Helper function for ifalias_expand_addr().  Handles IPv4.
817 #
818 ifalias_expand_addr_inet()
819 {
820         local _action _arg _cidr _cidr_addr _exargs
821         local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
822         local _retstr _c
823         _action=$1
824         _arg=$2
825         shift 2
826         _exargs=$*
827         _retstr=
828
829         case $_action:$_arg:$_exargs in
830         *:*--*)         return ;;       # invalid
831         tmp:*[0-9]-[0-9]*:*)            # to be expanded
832                 _action="alias"
833         ;;
834         *:*[0-9]-[0-9]*:*)              # to be expanded
835         ;;
836         tmp:*:*netmask*)                # already expanded w/ netmask option
837                 echo ${_arg%/[0-9]*} $_exargs && return
838         ;;
839         tmp:*:*)                        # already expanded w/o netmask option
840                 echo $_arg $_exargs && return
841         ;;
842         *:*:*netmask*)                  # already expanded w/ netmask option
843                 echo inet ${_arg%/[0-9]*} $_exargs && return
844         ;;
845         *:*:*)                          # already expanded w/o netmask option
846                 echo inet $_arg $_exargs && return
847         ;;
848         esac
849
850         for _cidr in $_arg; do
851                 _ipaddr=${_cidr%%/*}
852                 _plen=${_cidr##*/}
853                 # When subnet prefix length is not specified, use /32.
854                 case $_plen in
855                 $_ipaddr)       _plen=32 ;;     # "/" character not found
856                 esac
857
858                 OIFS=$IFS
859                 IFS=. set -- $_ipaddr
860                 _range=
861                 _iphead=
862                 _iptail=
863                 for _c in $@; do
864                         case $_range:$_c in
865                         :[0-9]*-[0-9]*)
866                                 _range=$_c
867                         ;;
868                         :*)
869                                 _iphead="${_iphead}${_iphead:+.}${_c}"
870                         ;;
871                         *:*)
872                                 _iptail="${_iptail}${_iptail:+.}${_c}"
873                         ;;
874                         esac
875                 done
876                 IFS=$OIFS
877                 _iplow=${_range%-*}
878                 _iphigh=${_range#*-}
879
880                 # clear netmask when removing aliases
881                 if [ "$_action" = "-alias" ]; then
882                         _plen=""
883                 fi
884
885                 _ipcount=$_iplow
886                 while [ "$_ipcount" -le "$_iphigh" ]; do
887                         _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
888                         if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]; then
889                                 warn "Range specification is too large (${_iphead}${_iphead:+.}${_iplow}${_iptail:+.}${_iptail}-${_iphead}${_iphead:+.}${_iphigh}${_iptail:+.}${_iptail}).  ${_iphead}${_iphead:+.}${_iplow}${_iptail:+.}${_iptail}-${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail} was processed.  Increase \$netif_ipexpand_max in rc.conf."
890                                 break
891                         else
892                                 _ipcount=$(($_ipcount + 1))
893                         fi
894                         # Forcibly set /32 for remaining aliases.
895                         _plen=32
896                 done
897         done
898
899         for _c in $_retstr; do
900                 ifalias_expand_addr_inet $_action $_c $_exargs
901         done
902 }
903
904 # ifalias_expand_addr_inet6 action addr
905 #       Helper function for ifalias_expand_addr().  Handles IPv6.
906 #
907 ifalias_expand_addr_inet6()
908 {
909         local _action _arg _cidr _cidr_addr _exargs
910         local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
911         local _ipv4part
912         local _retstr _c
913         _action=$1
914         _arg=$2
915         shift 2
916         _exargs=$*
917         _retstr=
918
919         case $_action:$_arg:$_exargs in
920         *:*--*:*)       return ;;       # invalid
921         tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
922                 _action="alias"
923         ;;
924         *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)  # to be expanded
925         ;;
926         tmp:*:*prefixlen*)      # already expanded w/ prefixlen option
927                 echo ${_arg%/[0-9]*} $_exargs && return
928         ;;
929         tmp:*:*)                # already expanded w/o prefixlen option
930                 echo $_arg $_exargs && return
931         ;;
932         *:*:*prefixlen*)        # already expanded w/ prefixlen option
933                 echo inet6 ${_arg%/[0-9]*} $_exargs && return
934         ;;
935         *:*:*)                  # already expanded w/o prefixlen option
936                 echo inet6 $_arg $_exargs && return
937         ;;
938         esac
939
940         for _cidr in $_arg; do
941                 _ipaddr="${_cidr%%/*}"
942                 _plen="${_cidr##*/}"
943
944                 case $_action:$_ipaddr:$_cidr in
945                 -alias:*:*)             unset _plen ;;
946                 *:$_cidr:$_ipaddr)      unset _plen ;;
947                 esac
948
949                 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
950                         # Handle !v4mapped && !v4compat addresses.
951
952                         # The default prefix length is 64.
953                         case $_ipaddr:$_cidr in
954                         $_cidr:$_ipaddr)        _plen="64" ;;
955                         esac
956                         _ipleft=${_ipaddr%-*}
957                         _ipright=${_ipaddr#*-}
958                         _iplow=${_ipleft##*:}
959                         _iphigh=${_ipright%%:*}
960                         _ipleft=${_ipleft%:*}
961                         _ipright=${_ipright#*:}
962
963                         if [ "$_iphigh" = "$_ipright" ]; then
964                                 unset _ipright
965                         else
966                                 _ipright=:$_ipright
967                         fi
968
969                         if [ -n "$_iplow" -a -n "$_iphigh" ]; then
970                                 _iplow=$((0x$_iplow))
971                                 _iphigh=$((0x$_iphigh))
972                                 _ipcount=$_iplow
973                                 while [ $_ipcount -le $_iphigh ]; do
974                                         _r=`printf "%s:%04x%s%s" \
975                                             $_ipleft $_ipcount $_ipright \
976                                             ${_plen:+/}$_plen`
977                                         _retstr="$_retstr $_r"
978                                         if [ $_ipcount -gt $(($_iplow + $netif_ipexpand_max)) ]
979                                         then
980                                                 warn "Range specification is too large $(printf '(%s:%x%s-%s:%x%s)' "$_ipleft" "$_iplow" "$_ipright" "$_ipleft" "$_iphigh" "$_ipright"). $(printf '%s:%x%s-%s:%x%s' "$_ipleft" "$_iplow" "$_ipright" "$_ipleft" "$_ipcount" "$_ipright") was processed.  Increase \$netif_ipexpand_max in rc.conf."
981                                                 break
982                                         else
983                                                 _ipcount=$(($_ipcount + 1))
984                                         fi
985                                 done
986                         else
987                                 _retstr="${_ipaddr}${_plen:+/}${_plen}"
988                         fi
989
990                         for _c in $_retstr; do
991                                 ifalias_expand_addr_inet6 $_action $_c $_exargs
992                         done
993                 else
994                         # v4mapped/v4compat should handle as an IPv4 alias
995                         _ipv4part=${_ipaddr##*:}
996
997                         # Adjust prefix length if any.  If not, set the
998                         # default prefix length as 32.
999                         case $_ipaddr:$_cidr in
1000                         $_cidr:$_ipaddr)        _plen=32 ;;
1001                         *)                      _plen=$(($_plen - 96)) ;;
1002                         esac
1003
1004                         _retstr=`ifalias_expand_addr_inet \
1005                             tmp ${_ipv4part}${_plen:+/}${_plen}`
1006                         for _c in $_retstr; do
1007                                 ifalias_expand_addr_inet $_action $_c $_exargs
1008                         done
1009                 fi
1010         done
1011 }
1012
1013 # ifalias_af_common_handler if af action args
1014 #       Helper function for ifalias_af_common().
1015 #
1016 ifalias_af_common_handler()
1017 {
1018         local _ret _if _af _action _args _c _tmpargs
1019
1020         _ret=1
1021         _if=$1
1022         _af=$2
1023         _action=$3
1024         shift 3
1025         _args=$*
1026
1027         case $_args in
1028         ${_af}\ *)      ;;
1029         *)      return  ;;
1030         esac
1031
1032         # link(ether) does not support address removal.
1033         case $_af:$_action in
1034         link:-alias|ether:-alias)       return ;;
1035         esac
1036
1037         _tmpargs=
1038         for _c in $_args; do
1039                 case $_c in
1040                 ${_af})
1041                         case $_tmpargs in
1042                         ${_af}\ *[0-9a-fA-F]-*)
1043                                 ifalias_af_common_handler $_if $_af $_action \
1044                                 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1045                         ;;
1046                         ${_af}\ *)
1047                                 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1048                         ;;
1049                         esac
1050                         _tmpargs=$_af
1051                 ;;
1052                 *)
1053                         _tmpargs="$_tmpargs $_c"
1054                 ;;
1055                 esac
1056         done
1057         # Process the last component if any.
1058         if [ -n "$_tmpargs}" ]; then
1059                 case $_tmpargs in
1060                 ${_af}\ *[0-9a-fA-F]-*)
1061                         ifalias_af_common_handler $_if $_af $_action \
1062                         `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1063                 ;;
1064                 ${_af}\ *)
1065                         ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1066                 ;;
1067                 esac
1068         fi
1069
1070         return $_ret
1071 }
1072
1073 # ifalias_af_common if af action
1074 #       Helper function for ifalias().
1075 #
1076 ifalias_af_common()
1077 {
1078         local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1079         local _vif _punct=".-/+"
1080
1081         _ret=1
1082         _aliasn=
1083         _if=$1
1084         _af=$2
1085         _action=$3
1086
1087         # Normalize $_if before using it in a pattern to list_vars()
1088         ltr "$_if" "$_punct" "_" _vif
1089
1090         # ifconfig_IF_aliasN which starts with $_af
1091         for alias in `list_vars ifconfig_${_vif}_alias[0-9]\* |
1092                 sort_lite -nk1.$((9+${#_vif}+7))`
1093         do
1094                 eval ifconfig_args=\"\$$alias\"
1095                 _iaf=
1096                 case $ifconfig_args in
1097                 inet\ *)        _iaf=inet ;;
1098                 inet6\ *)       _iaf=inet6 ;;
1099                 link\ *)        _iaf=link ;;
1100                 ether\ *)       _iaf=ether ;;
1101                 esac
1102
1103                 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1104                 ${_af}:*:${_af}:*)
1105                         _aliasn="$_aliasn $ifconfig_args"
1106                         ;;
1107                 ${_af}:*:"":"")
1108                         break
1109                         ;;
1110                 inet:alias:"":*)
1111                         _aliasn="$_aliasn inet $ifconfig_args"
1112                         warn "\$${alias} needs leading" \
1113                             "\"inet\" keyword for an IPv4 address."
1114                 esac
1115         done
1116
1117         # backward compatibility: ipv6_ifconfig_IF_aliasN.
1118         case $_af in
1119         inet6)
1120                 for alias in `list_vars ipv6_ifconfig_${_vif}_alias[0-9]\* |
1121                         sort_lite -nk1.$((14+${#_vif}+7))`
1122                 do
1123                         eval ifconfig_args=\"\$$alias\"
1124                         case ${_action}:"${ifconfig_args}" in
1125                         *:"")
1126                                 break
1127                         ;;
1128                         alias:*)
1129                                 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1130                                 warn "\$${alias} is obsolete. " \
1131                                     "Use ifconfig_${_vif}_aliasN instead."
1132                         ;;
1133                         esac
1134                 done
1135         esac
1136
1137         # backward compatibility: ipv4_addrs_IF.
1138         for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1139                 _aliasn="$_aliasn inet $_tmpargs"
1140         done
1141
1142         # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1143         _tmpargs=
1144         for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1145                 case $_c in
1146                 inet|inet6|link|ether)
1147                         case $_tmpargs in
1148                         ${_af}\ *)
1149                                 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1150                         ;;
1151                         esac
1152                         _tmpargs=$_c
1153                 ;;
1154                 *)
1155                         _tmpargs="$_tmpargs $_c"
1156                 esac
1157         done
1158         # Process the last component
1159         case $_tmpargs in
1160         ${_af}\ *)
1161                 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1162         ;;
1163         esac
1164
1165         return $_ret
1166 }
1167
1168 # ipv6_prefix_hostid_addr_common if action
1169 #       Add or remove IPv6 prefix + hostid addr on the interface $if
1170 #
1171 ipv6_prefix_hostid_addr_common()
1172 {
1173         local _if _action prefix j
1174         _if=$1
1175         _action=$2
1176         prefix=`get_if_var ${_if} ipv6_prefix_IF`
1177
1178         if [ -n "${prefix}" ]; then
1179                 for j in ${prefix}; do
1180                         # The default prefixlen is 64.
1181                         plen=${j#*/}
1182                         case $j:$plen in
1183                         $plen:$j)       plen=64 ;;
1184                         *)              j=${j%/*} ;;
1185                         esac
1186
1187                         # Normalize the last part by removing ":"
1188                         j=${j%::*}
1189                         j=${j%:}
1190                         ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1191                                 prefixlen $plen eui64 ${_action}
1192
1193                         # if I am a router, add subnet router
1194                         # anycast address (RFC 2373).
1195                         if checkyesno ipv6_gateway_enable; then
1196                                 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1197                                         prefixlen $plen ${_action} anycast
1198                         fi
1199                 done
1200         fi
1201 }
1202
1203 # ipv6_accept_rtadv_up if
1204 #       Enable accepting Router Advertisement and send Router
1205 #       Solicitation message
1206 ipv6_accept_rtadv_up()
1207 {
1208         if ipv6_autoconfif $1; then
1209                 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1210                 if ! checkyesno rtsold_enable; then
1211                         rtsol ${rtsol_flags} $1
1212                 fi
1213         fi
1214 }
1215
1216 # ipv6_accept_rtadv_down if
1217 #       Disable accepting Router Advertisement
1218 ipv6_accept_rtadv_down()
1219 {
1220         if ipv6_autoconfif $1; then
1221                 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1222         fi
1223 }
1224
1225 # ifscript_up if
1226 #       Evaluate a startup script for the $if interface.
1227 #       It returns 0 if a script was found and processed or
1228 #       1 if no script was found.
1229 #
1230 ifscript_up()
1231 {
1232         if [ -r /etc/start_if.$1 ]; then
1233                 . /etc/start_if.$1
1234                 return 0
1235         else
1236                 return 1
1237         fi
1238 }
1239
1240 # ifscript_down if
1241 #       Evaluate a shutdown script for the $if interface.
1242 #       It returns 0 if a script was found and processed or
1243 #       1 if no script was found.
1244 #
1245 ifscript_down()
1246 {
1247         if [ -r /etc/stop_if.$1 ]; then
1248                 . /etc/stop_if.$1
1249                 return 0
1250         else
1251                 return 1
1252         fi
1253 }
1254
1255 # wlan_up
1256 #       Create IEEE802.11 interfaces.
1257 #
1258 wlan_up()
1259 {
1260         local _list _iflist parent child_wlans child create_args debug_flags
1261         _list=
1262         _iflist=$*
1263
1264         # Parse wlans_$parent="$child ..."
1265         for parent in `set | sed -nE 's/wlans_([a-z]+[0-9]+)=.*/\1/p'`; do
1266                 child_wlans=`get_if_var $parent wlans_IF`
1267                 for child in ${child_wlans}; do
1268                         create_args="wlandev $parent `get_if_var $child create_args_IF`"
1269                         debug_flags="`get_if_var $child wlandebug_IF`"
1270                         case $_iflist in
1271                         ""|$child|$child\ *|*\ $child\ *|*\ $child)     ;;
1272                         *)      continue ;;
1273                         esac
1274                         # Skip if ${child} already exists.
1275                         if ${IFCONFIG_CMD} $child > /dev/null 2>&1; then
1276                                 continue
1277                         fi
1278                         if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1279                                 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1280                                 if [ $? -eq 0 ]; then
1281                                         _list="$_list $child"
1282                                 fi
1283                                 if [ -n "${debug_flags}" ]; then
1284                                         wlandebug -i $child ${debug_flags}
1285                                 fi
1286                         else
1287                                 i=`${IFCONFIG_CMD} wlan create ${create_args}`
1288                                 # XXXGL: wlandebug should accept any name
1289                                 if [ -n "${debug_flags}" ]; then
1290                                         wlandebug -i $i ${debug_flags}
1291                                 fi
1292                                 ${IFCONFIG_CMD} $i name $child && cfg=0
1293                                 if [ $? -eq 0 ]; then
1294                                         _list="$_list $child"
1295                                 fi
1296                         fi
1297                 done
1298         done
1299         if [ -n "${_list# }" ]; then
1300                 echo "Created wlan(4) interfaces: ${_list# }."
1301         fi
1302         debug "Created wlan(4)s: ${_list# }"
1303 }
1304
1305 # wlan_down
1306 #       Destroy IEEE802.11 interfaces.
1307 #
1308 wlan_down()
1309 {
1310         local _list _iflist parent child_wlans child
1311         _list=
1312         _iflist=$*
1313
1314         # Parse wlans_$parent="$child ..."
1315         for parent in `set | sed -nE 's/wlans_([a-z]+[0-9]+)=.*/\1/p'`; do
1316                 child_wlans=`get_if_var $parent wlans_IF`
1317                 for child in ${child_wlans}; do
1318                         case $_iflist in
1319                         ""|$child|$child\ *|*\ $child\ *|*\ $child)     ;;
1320                         *)      continue ;;
1321                         esac
1322                         # Skip if ${child} doesn't exists.
1323                         if ! ${IFCONFIG_CMD} $child > /dev/null 2>&1; then
1324                                 continue
1325                         fi
1326                         ${IFCONFIG_CMD} -n ${child} destroy
1327                         if [ $? -eq 0 ]; then
1328                                 _list="$_list $child"
1329                         fi
1330                 done
1331         done
1332         if [ -n "${_list# }" ]; then
1333                 echo "Destroyed wlan(4) interfaces: ${_list# }."
1334         fi
1335         debug "Destroyed wlan(4)s: ${_list# }"
1336 }
1337
1338 # clone_up
1339 #       Create cloneable interfaces.
1340 #
1341 clone_up()
1342 {
1343         local _list ifn ifopt _iflist _n tmpargs
1344         _list=
1345         _iflist=$*
1346
1347         # create_args_IF
1348         for ifn in ${cloned_interfaces}; do
1349                 # Parse ifn:ifopt.
1350                 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1351                 case $_iflist in
1352                 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)     ;;
1353                 *)      continue ;;
1354                 esac
1355                 case $ifn in
1356                 epair[0-9]*)
1357                         # epair(4) uses epair[0-9] for creation and
1358                         # epair[0-9][ab] for configuration.
1359                         #
1360                         # Skip if ${ifn}a or ${ifn}b already exist.
1361                         if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1362                                 continue
1363                         elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1364                                 continue
1365                         fi
1366                         ${IFCONFIG_CMD} ${ifn} create \
1367                             `get_if_var ${ifn} create_args_IF`
1368                         if [ $? -eq 0 ]; then
1369                                 _list="$_list ${ifn}a ${ifn}b"
1370                         fi
1371                 ;;
1372                 *)
1373                         # Skip if ${ifn} already exists.
1374                         if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1375                                 continue
1376                         fi
1377                         ${IFCONFIG_CMD} ${ifn} create \
1378                             `get_if_var ${ifn} create_args_IF`
1379                         if [ $? -eq 0 ]; then
1380                                 _list="$_list $ifn"
1381                         fi
1382                 esac
1383         done
1384         if [ -n "$gif_interfaces" ]; then
1385                 warn "\$gif_interfaces is obsolete.  Use \$cloned_interfaces instead."
1386         fi
1387         for ifn in ${gif_interfaces}; do
1388                 # Parse ifn:ifopt.
1389                 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1390                 case $_iflist in
1391                 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)     ;;
1392                 *)      continue ;;
1393                 esac
1394                 # Skip if ifn already exists.
1395                 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1396                         continue
1397                 fi
1398                 case $ifn in
1399                 gif[0-9]*)
1400                         ${IFCONFIG_CMD} $ifn create
1401                 ;;
1402                 *)
1403                         _n=$(${IFCONFIG_CMD} gif create)
1404                         ${IFCONFIG_CMD} $_n name $ifn
1405                 ;;
1406                 esac
1407                 if [ $? -eq 0 ]; then
1408                         _list="$_list $ifn"
1409                 fi
1410                 tmpargs=$(get_if_var $ifn gifconfig_IF)
1411                 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1412         done
1413         if [ -n "${_list# }" ]; then
1414                 echo "Created clone interfaces: ${_list# }."
1415         fi
1416         debug "Cloned: ${_list# }"
1417 }
1418
1419 # clone_down
1420 #       Destroy cloned interfaces. Destroyed interfaces are echoed to
1421 #       standard output.
1422 #
1423 clone_down()
1424 {
1425         local _list ifn _difn ifopt _iflist _sticky
1426         _list=
1427         _iflist=$*
1428
1429         : ${cloned_interfaces_sticky:=NO}
1430         if checkyesno cloned_interfaces_sticky; then
1431                 _sticky=1
1432         else
1433                 _sticky=0
1434         fi
1435         for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1436                 # Parse ifn:ifopt.
1437                 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1438                 case $ifopt:$_sticky in
1439                 sticky:*)       continue ;;     # :sticky => not destroy
1440                 nosticky:*)     ;;              # :nosticky => destroy
1441                 *:1)            continue ;;     # global sticky knob == 1
1442                 esac
1443                 case $_iflist in
1444                 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)     ;;
1445                 *)      continue ;;
1446                 esac
1447                 case $ifn in
1448                 epair[0-9]*)
1449                         # Note: epair(4) uses epair[0-9] for removal and
1450                         # epair[0-9][ab] for configuration.
1451                         #
1452                         # Skip if both of ${ifn}a and ${ifn}b do not exist.
1453                         if ${IFCONFIG_CMD} ${ifn}a > /dev/null 2>&1; then
1454                                 _difn=${ifn}a
1455                         elif ${IFCONFIG_CMD} ${ifn}b > /dev/null 2>&1; then
1456                                 _difn=${ifn}b
1457                         else
1458                                 continue
1459                         fi
1460                         ${IFCONFIG_CMD} -n $_difn destroy
1461                         if [ $? -eq 0 ]; then
1462                                 _list="$_list ${ifn}a ${ifn}b"
1463                         fi
1464                 ;;
1465                 *)
1466                         # Skip if ifn does not exist.
1467                         if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1468                                 continue
1469                         fi
1470                         ${IFCONFIG_CMD} -n ${ifn} destroy
1471                         if [ $? -eq 0 ]; then
1472                                 _list="$_list $ifn"
1473                         fi
1474                 ;;
1475                 esac
1476         done
1477         if [ -n "${_list# }" ]; then
1478                 echo "Destroyed clone interfaces: ${_list# }."
1479         fi
1480         debug "Destroyed clones: ${_list# }"
1481 }
1482
1483 # childif_create
1484 #       Create and configure child interfaces.  Return 0 if child
1485 #       interfaces are created.
1486 #
1487 #       XXXGL: the wlan code in this functions is superseded by wlan_up(),
1488 #       and will go away soon.
1489 #
1490 childif_create()
1491 {
1492         local cfg child child_vlans child_wlans create_args debug_flags ifn i
1493         cfg=1
1494         ifn=$1
1495
1496         # Create wireless interfaces
1497         child_wlans=`get_if_var $ifn wlans_IF`
1498
1499         for child in ${child_wlans}; do
1500                 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1501                 debug_flags="`get_if_var $child wlandebug_IF`"
1502
1503                 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1504                         ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1505                         if [ -n "${debug_flags}" ]; then
1506                                 wlandebug -i $child ${debug_flags}
1507                         fi
1508                 else
1509                         i=`${IFCONFIG_CMD} wlan create ${create_args}`
1510                         if [ -n "${debug_flags}" ]; then
1511                                 wlandebug -i $i ${debug_flags}
1512                         fi
1513                         ${IFCONFIG_CMD} $i name $child && cfg=0
1514                 fi
1515                 if autoif $child; then
1516                         ifn_start $child
1517                 fi
1518         done
1519
1520         # Create vlan interfaces
1521         child_vlans=`get_if_var $ifn vlans_IF`
1522
1523         if [ -n "${child_vlans}" ]; then
1524                 load_kld if_vlan
1525         fi
1526
1527         for child in ${child_vlans}; do
1528                 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1529                         child="${ifn}.${child}"
1530                         create_args=`get_if_var $child create_args_IF`
1531                         ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1532                 else
1533                         create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1534                         if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1535                                 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1536                         else
1537                                 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1538                                 ${IFCONFIG_CMD} $i name $child && cfg=0
1539                         fi
1540                 fi
1541                 if autoif $child; then
1542                         ifn_start $child
1543                 fi
1544         done
1545
1546         return ${cfg}
1547 }
1548
1549 # childif_destroy
1550 #       Destroy child interfaces.
1551 #
1552 childif_destroy()
1553 {
1554         local cfg child child_vlans child_wlans ifn
1555         cfg=1
1556
1557         child_wlans=`get_if_var $ifn wlans_IF`
1558         for child in ${child_wlans}; do
1559                 if ! ifexists $child; then
1560                         continue
1561                 fi
1562                 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1563         done
1564
1565         child_vlans=`get_if_var $ifn vlans_IF`
1566         for child in ${child_vlans}; do
1567                 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1568                         child="${ifn}.${child}"
1569                 fi
1570                 if ! ifexists $child; then
1571                         continue
1572                 fi
1573                 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1574         done
1575
1576         return ${cfg}
1577 }
1578
1579 # ng_mkpeer
1580 #       Create netgraph nodes.
1581 #
1582 ng_mkpeer()
1583 {
1584         ngctl -f - 2> /dev/null <<EOF
1585 mkpeer $*
1586 msg dummy nodeinfo
1587 EOF
1588 }
1589
1590 # ng_create_one
1591 #       Create netgraph nodes.
1592 #
1593 ng_create_one()
1594 {
1595         local t
1596
1597         ng_mkpeer $* | while read line; do
1598                 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1599                 if [ -n "${t}" ]; then
1600                         echo ${t}
1601                         return
1602                 fi
1603         done
1604 }
1605
1606 # ifnet_rename [ifname]
1607 #       Rename interfaces if ifconfig_IF_name is defined.
1608 #
1609 ifnet_rename()
1610 {
1611         local _if _ifname
1612
1613         # ifconfig_IF_name
1614         for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1615                 _ifname=`get_if_var $_if ifconfig_IF_name`
1616                 if [ ! -z "$_ifname" ]; then
1617                         ${IFCONFIG_CMD} $_if name $_ifname
1618                 fi
1619         done
1620
1621         return 0
1622 }
1623
1624 # list_net_interfaces type
1625 #       List all network interfaces. The type of interface returned
1626 #       can be controlled by the type argument. The type
1627 #       argument can be any of the following:
1628 #               nodhcp  - all interfaces, excluding DHCP configured interfaces
1629 #               dhcp    - list only DHCP configured interfaces
1630 #               noautoconf      - all interfaces, excluding IPv6 Stateless
1631 #                                 Address Autoconf configured interfaces
1632 #               autoconf        - list only IPv6 Stateless Address Autoconf
1633 #                                 configured interfaces
1634 #       If no argument is specified all network interfaces are output.
1635 #       Note that the list will include cloned interfaces if applicable.
1636 #       Cloned interfaces must already exist to have a chance to appear
1637 #       in the list if ${network_interfaces} is set to `auto'.
1638 #
1639 list_net_interfaces()
1640 {
1641         local type _tmplist _list _autolist _lo _if
1642         type=$1
1643
1644         # Get a list of ALL the interfaces and make lo0 first if it's there.
1645         #
1646         _tmplist=
1647         case ${network_interfaces} in
1648         [Aa][Uu][Tt][Oo])
1649                 _autolist="`${IFCONFIG_CMD} -l`"
1650                 _lo=
1651                 for _if in ${_autolist} ; do
1652                         if autoif $_if; then
1653                                 if [ "$_if" = "lo0" ]; then
1654                                         _lo="lo0 "
1655                                 else
1656                                         _tmplist="${_tmplist} ${_if}"
1657                                 fi
1658                         fi
1659                 done
1660                 _tmplist="${_lo}${_tmplist# }"
1661         ;;
1662         *)
1663                 for _if in ${network_interfaces} ${cloned_interfaces}; do
1664                         # epair(4) uses epair[0-9] for creation and
1665                         # epair[0-9][ab] for configuration.
1666                         case $_if in
1667                         epair[0-9]*)
1668                                 _tmplist="$_tmplist ${_if}a ${_if}b"
1669                         ;;
1670                         *)
1671                                 _tmplist="$_tmplist $_if"
1672                         ;;
1673                         esac
1674                 done
1675                 #
1676                 # lo0 is effectively mandatory, so help prevent foot-shooting
1677                 #
1678                 case "$_tmplist" in
1679                 lo0|'lo0 '*|*' lo0'|*' lo0 '*)
1680                         # This is fine, do nothing
1681                         _tmplist="${_tmplist# }"
1682                 ;;
1683                 *)
1684                         _tmplist="lo0 ${_tmplist# }"
1685                 ;;
1686                 esac
1687         ;;
1688         esac
1689
1690         _list=
1691         case "$type" in
1692         nodhcp)
1693                 for _if in ${_tmplist} ; do
1694                         if ! dhcpif $_if && \
1695                            [ -n "`_ifconfig_getargs $_if`" ]; then
1696                                 _list="${_list# } ${_if}"
1697                         fi
1698                 done
1699         ;;
1700         dhcp)
1701                 for _if in ${_tmplist} ; do
1702                         if dhcpif $_if; then
1703                                 _list="${_list# } ${_if}"
1704                         fi
1705                 done
1706         ;;
1707         noautoconf)
1708                 for _if in ${_tmplist} ; do
1709                         if ! ipv6_autoconfif $_if && \
1710                            [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1711                                 _list="${_list# } ${_if}"
1712                         fi
1713                 done
1714         ;;
1715         autoconf)
1716                 for _if in ${_tmplist} ; do
1717                         if ipv6_autoconfif $_if; then
1718                                 _list="${_list# } ${_if}"
1719                         fi
1720                 done
1721         ;;
1722         *)
1723                 _list=${_tmplist}
1724         ;;
1725         esac
1726
1727         echo $_list
1728
1729         return 0
1730 }
1731
1732 # get_default_if -address_family
1733 #       Get the interface of the default route for the given address family.
1734 #       The -address_family argument must be suitable passing to route(8).
1735 #
1736 get_default_if()
1737 {
1738         local routeget oldifs defif line
1739         defif=
1740         oldifs="$IFS"
1741         IFS="
1742 "
1743         for line in `route -n get $1 default 2>/dev/null`; do
1744                 case $line in
1745                 *interface:*)
1746                         defif=${line##*: }
1747                         ;;
1748                 esac
1749         done
1750         IFS=${oldifs}
1751
1752         echo $defif
1753 }
1754
1755 # hexdigit arg
1756 #       Echo decimal number $arg (single digit) in hexadecimal format.
1757 hexdigit()
1758 {
1759         printf '%x\n' "$1"
1760 }
1761
1762 # hexprint arg
1763 #       Echo decimal number $arg (multiple digits) in hexadecimal format.
1764 hexprint()
1765 {
1766         printf '%x\n' "$1"
1767 }
1768
1769 is_wired_interface()
1770 {
1771         local media
1772
1773         case `${IFCONFIG_CMD} $1 2>/dev/null` in
1774         *media:?Ethernet*) media=Ethernet ;;
1775         esac
1776
1777         test "$media" = "Ethernet"
1778 }
1779
1780 # network6_getladdr if [flag]
1781 #       Echo link-local address from $if if any.
1782 #       If flag is defined, tentative ones will be excluded.
1783 network6_getladdr()
1784 {
1785         local _if _flag proto addr rest
1786         _if=$1
1787         _flag=$2
1788
1789         ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1790                 case "${proto}/${addr}/${_flag}/${rest}" in
1791                 inet6/fe80::*//*)
1792                         echo ${addr}
1793                 ;;
1794                 inet6/fe80:://*tentative*)      # w/o flag
1795                         sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1796                         network6_getladdr $_if $_flags
1797                 ;;
1798                 inet6/fe80::/*/*tentative*)     # w/ flag
1799                         echo ${addr}
1800                 ;;
1801                 *)
1802                         continue
1803                 ;;
1804                 esac
1805
1806                 return
1807         done
1808 }