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