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