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