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