]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/network.subr
Fix address range specification with ifconfig(8) options such as:
[FreeBSD/FreeBSD.git] / etc / network.subr
1 #
2 # Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # 1. Redistributions of source code must retain the above copyright
8 #    notice, this list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright
10 #    notice, this list of conditions and the following disclaimer in the
11 #    documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 # SUCH DAMAGE.
24 #
25 # $FreeBSD$
26 #
27 IFCONFIG_CMD="/sbin/ifconfig"
28
29 # Maximum number of addresses expanded from a address range specification.
30 _IPEXPANDMAX=31
31
32 #
33 # Subroutines commonly used from network startup scripts.
34 # Requires that rc.conf be loaded first.
35 #
36
37 # ifn_start ifn
38 #       Bring up and configure an interface.  If some configuration is
39 #       applied, print the interface configuration.
40 #
41 ifn_start()
42 {
43         local ifn cfg
44         ifn="$1"
45         cfg=1
46
47         [ -z "$ifn" ] && err 1 "ifn_start called without an interface"
48
49         ifscript_up ${ifn} && cfg=0
50         ifconfig_up ${ifn} && cfg=0
51         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                 # True if $ipv6_prefix_IF is defined.
502                 _tmpargs=`get_if_var $_if ipv6_prefix_IF`
503                 if [ -n "${_tmpargs}" ]; then
504                         return 0
505                 fi
506
507                 # backward compatibility: True if $ipv6_ifconfig_IF is defined.
508                 _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
509                 if [ -n "${_tmpargs}" ]; then
510                         return 0
511                 fi
512                 ;;
513         esac
514
515         return 1
516 }
517
518 # ipv6_autoconfif if
519 #       Returns 0 if the interface should be configured for IPv6 with
520 #       Stateless Address Configuration; 1 otherwise.
521 ipv6_autoconfif()
522 {
523         local _if _tmpargs _arg
524         _if=$1
525
526         case $_if in
527         lo[0-9]*|\
528         stf[0-9]*|\
529         faith[0-9]*|\
530         lp[0-9]*|\
531         sl[0-9]*)
532                 return 1
533                 ;;
534         esac
535         if noafif $_if; then
536                 return 1
537         fi
538         if ! ipv6if $_if; then
539                 return 1
540         fi
541         if checkyesno ipv6_gateway_enable; then
542                 return 1
543         fi
544         _tmpargs=`get_if_var $_if ipv6_prefix_IF`
545         if [ -n "${_tmpargs}" ]; then
546                 return 1
547         fi
548         # backward compatibility: $ipv6_enable
549         case $ipv6_enable in
550         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
551                 if checkyesno ipv6_gateway_enable; then
552                         return 1
553                 else
554                         return 0
555                 fi
556         ;;
557         esac
558
559         _tmpargs=`_ifconfig_getargs $_if ipv6`
560         for _arg in $_tmpargs; do
561                 case $_arg in
562                 accept_rtadv)
563                         return 0
564                         ;;
565                 esac
566         done
567
568         # backward compatibility: $ipv6_ifconfig_IF
569         _tmpargs=`get_if_var $_if ipv6_ifconfig_IF`
570         for _arg in $_tmpargs; do
571                 case $_arg in
572                 accept_rtadv)
573                         return 0
574                         ;;
575                 esac
576         done
577
578         return 1
579 }
580
581 # ifexists if
582 #       Returns 0 if the interface exists and 1 otherwise.
583 ifexists()
584 {
585         [ -z "$1" ] && return 1
586         ${IFCONFIG_CMD} -n $1 > /dev/null 2>&1
587 }
588
589 # ipv4_up if
590 #       add IPv4 addresses to the interface $if
591 ipv4_up()
592 {
593         local _if _ret
594         _if=$1
595         _ret=1
596
597         # Add 127.0.0.1/8 to lo0 unless otherwise specified.
598         if [ "${_if}" = "lo0" ]; then
599                 ifconfig_args=`get_if_var ${_if} ifconfig_IF`
600                 if [ -z "${ifconfig_args}" ]; then
601                         ${IFCONFIG_CMD} ${_if} inet 127.0.0.1/8 alias
602                 fi
603         fi
604         ifalias ${_if} inet alias && _ret=0
605
606         return $_ret
607 }
608
609 # ipv6_up if
610 #       add IPv6 addresses to the interface $if
611 ipv6_up()
612 {
613         local _if _ret
614         _if=$1
615         _ret=1
616
617         if ! ipv6if $_if; then
618                 return 0
619         fi
620
621         ifalias ${_if} inet6 alias && _ret=0
622         ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
623         ipv6_accept_rtadv_up ${_if} && _ret=0
624
625         return $_ret
626 }
627
628 # ipv4_down if
629 #       remove IPv4 addresses from the interface $if
630 ipv4_down()
631 {
632         local _if _ifs _ret inetList oldifs _inet
633         _if=$1
634         _ifs="^"
635         _ret=1
636
637         ifalias ${_if} inet -alias && _ret=0
638
639         inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
640
641         oldifs="$IFS"
642         IFS="$_ifs"
643         for _inet in $inetList ; do
644                 # get rid of extraneous line
645                 [ -z "$_inet" ] && break
646
647                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
648
649                 IFS="$oldifs"
650                 ${IFCONFIG_CMD} ${_if} ${_inet} delete
651                 IFS="$_ifs"
652                 _ret=0
653         done
654         IFS="$oldifs"
655
656         return $_ret
657 }
658
659 # ipv6_down if
660 #       remove IPv6 addresses from the interface $if
661 ipv6_down()
662 {
663         local _if _ifs _ret inetList oldifs _inet6
664         _if=$1
665         _ifs="^"
666         _ret=1
667
668         if ! ipv6if $_if; then
669                 return 0
670         fi
671
672         ipv6_accept_rtadv_down ${_if} && _ret=0
673         ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
674         ifalias ${_if} inet6 -alias && _ret=0
675
676         inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n" "$_ifs"`"
677
678         oldifs="$IFS"
679         IFS="$_ifs"
680         for _inet6 in $inetList ; do
681                 # get rid of extraneous line
682                 [ -z "$_inet6" ] && break
683
684                 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
685
686                 IFS="$oldifs"
687                 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
688                 IFS="$_ifs"
689                 _ret=0
690         done
691         IFS="$oldifs"
692
693         return $_ret
694 }
695
696 # ifalias if af action
697 #       Configure or remove aliases for network interface $if.
698 #       It returns 0 if at least one alias was configured or
699 #       removed, or 1 if there were none.
700 #
701 ifalias()
702 {
703         local _ret
704         _ret=1
705
706         afexists $2 || return $_ret
707
708         case "$2" in
709         inet|inet6|link|ether)
710                 ifalias_af_common $1 $2 $3 && _ret=0
711                 ;;
712         esac
713
714         return $_ret
715 }
716
717 # ifalias_expand_addr af action addr
718 #       Expand address range ("N-M") specification in addr.
719 #       "addr" must not include an address-family keyword.
720 #       The results will include an address-family keyword.
721 #
722 ifalias_expand_addr()
723 {
724         local _af _action
725
726         _af=$1
727         _action=$2
728         shift 2
729
730         afexists $_af || return
731         ifalias_expand_addr_$_af $_action $*
732 }
733
734 # ifalias_expand_addr_inet action addr
735 #       Helper function for ifalias_expand_addr().  Handles IPv4.
736 #
737 ifalias_expand_addr_inet()
738 {
739         local _action _arg _cidr _cidr_addr _exargs
740         local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
741         local _retstr _c
742         _action=$1
743         _arg=$2
744         shift 2
745         _exargs=$*
746         _retstr=
747
748         case $_action:$_arg:$_exargs in
749         *:*--*)         return ;;       # invalid
750         tmp:*[0-9]-[0-9]*:*)            # to be expanded
751                 _action="alias"
752         ;;
753         *:*[0-9]-[0-9]*:*)              # to be expanded
754         ;;
755         tmp:*:*netmask*)                # already expanded w/ netmask option
756                 echo ${_arg%/[0-9]*} $_exargs && return
757         ;;
758         tmp:*:*)                        # already expanded w/o netmask option
759                 echo $_arg $_exargs && return
760         ;;
761         *:*:*netmask*)                  # already expanded w/ netmask option
762                 echo inet ${_arg%/[0-9]*} $_exargs && return
763         ;;
764         *:*:*)                          # already expanded w/o netmask option
765                 echo inet $_arg $_exargs && return
766         ;;
767         esac
768
769         for _cidr in $_arg; do
770                 _ipaddr=${_cidr%%/*}
771                 _plen=${_cidr##*/}
772                 # When subnet prefix length is not specified, use /32.
773                 case $_plen in
774                 $_ipaddr)       _plen=32 ;;     # "/" character not found
775                 esac
776
777                 OIFS=$IFS
778                 IFS=. set -- $_ipaddr
779                 _range=
780                 _iphead=
781                 _iptail=
782                 for _c in $@; do
783                         case $_range:$_c in
784                         :[0-9]*-[0-9]*)
785                                 _range=$_c
786                         ;;
787                         :*)
788                                 _iphead="${_iphead}${_iphead:+.}${_c}"
789                         ;;
790                         *:*)
791                                 _iptail="${_iptail}${_iptail:+.}${_c}"
792                         ;;
793                         esac
794                 done
795                 IFS=$OIFS
796                 _iplow=${_range%-*}
797                 _iphigh=${_range#*-}
798
799                 # clear netmask when removing aliases
800                 if [ "$_action" = "-alias" ]; then
801                         _plen=""
802                 fi
803
804                 _ipcount=$_iplow
805                 while [ "$_ipcount" -le "$_iphigh" ]; do
806                         _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
807                         if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
808                                 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."
809                                 break
810                         else
811                                 _ipcount=$(($_ipcount + 1))
812                         fi
813                         # Forcibly set /32 for remaining aliases.
814                         _plen=32
815                 done
816         done
817
818         for _c in $_retstr; do
819                 ifalias_expand_addr_inet $_action $_c $_exargs
820         done
821 }
822
823 # ifalias_expand_addr_inet6 action addr
824 #       Helper function for ifalias_expand_addr().  Handles IPv6.
825 #
826 ifalias_expand_addr_inet6()
827 {
828         local _action _arg _cidr _cidr_addr _exargs
829         local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
830         local _ipv4part
831         local _retstr _c
832         _action=$1
833         _arg=$2
834         shift 2
835         _exargs=$*
836         _retstr=
837
838         case $_action:$_arg:$_exargs in
839         *:*--*:*)       return ;;       # invalid
840         tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
841                 _action="alias"
842         ;;
843         *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)  # to be expanded
844         ;;
845         tmp:*:*prefixlen*)      # already expanded w/ prefixlen option
846                 echo ${_arg%/[0-9]*} $_exargs && return
847         ;;
848         tmp:*:*)                # already expanded w/o prefixlen option
849                 echo $_arg $_exargs && return
850         ;;
851         *:*:*prefixlen*)        # already expanded w/ prefixlen option
852                 echo inet6 ${_arg%/[0-9]*} $_exargs && return
853         ;;
854         *:*:*)                  # already expanded w/o prefixlen option
855                 echo inet6 $_arg $_exargs && return
856         ;;
857         esac
858
859         for _cidr in $_arg; do
860                 _ipaddr="${_cidr%%/*}"
861                 _plen="${_cidr##*/}"
862
863                 case $_action:$_ipaddr:$_cidr in
864                 -alias:*:*)             unset _plen ;;
865                 *:$_cidr:$_ipaddr)      unset _plen ;;
866                 esac
867
868                 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
869                         # Handle !v4mapped && !v4compat addresses.
870
871                         # The default prefix length is 64.
872                         case $_ipaddr:$_cidr in
873                         $_cidr:$_ipaddr)        _plen="64" ;;
874                         esac
875                         _ipleft=${_ipaddr%-*}
876                         _ipright=${_ipaddr#*-}
877                         _iplow=${_ipleft##*:}
878                         _iphigh=${_ipright%%:*}
879                         _ipleft=${_ipleft%:*}
880                         _ipright=${_ipright#*:}
881
882                         if [ "$_iphigh" = "$_ipright" ]; then
883                                 unset _ipright
884                         else
885                                 _ipright=:$_ipright
886                         fi
887
888                         if [ -n "$_iplow" -a -n "$_iphigh" ]; then
889                                 _iplow=$((0x$_iplow))
890                                 _iphigh=$((0x$_iphigh))
891                                 _ipcount=$_iplow
892                                 while [ $_ipcount -le $_iphigh ]; do
893                                         _r=`printf "%s:%04x%s%s" \
894                                             $_ipleft $_ipcount $_ipright \
895                                             ${_plen:+/}$_plen`
896                                         _retstr="$_retstr $_r"
897                                         if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
898                                         then
899                                                 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."
900                                                 break
901                                         else
902                                                 _ipcount=$(($_ipcount + 1))
903                                         fi
904                                 done
905                         else
906                                 _retstr="${_ipaddr}${_plen:+/}${_plen}"
907                         fi
908
909                         for _c in $_retstr; do
910                                 ifalias_expand_addr_inet6 $_action $_c $_exargs
911                         done
912                 else
913                         # v4mapped/v4compat should handle as an IPv4 alias
914                         _ipv4part=${_ipaddr##*:}
915
916                         # Adjust prefix length if any.  If not, set the
917                         # default prefix length as 32.
918                         case $_ipaddr:$_cidr in
919                         $_cidr:$_ipaddr)        _plen=32 ;;
920                         *)                      _plen=$(($_plen - 96)) ;;
921                         esac
922
923                         _retstr=`ifalias_expand_addr_inet \
924                             tmp ${_ipv4part}${_plen:+/}${_plen}`
925                         for _c in $_retstr; do
926                                 ifalias_expand_addr_inet $_action $_c $_exargs
927                         done
928                 fi
929         done
930 }
931
932 # ifalias_af_common_handler if af action args
933 #       Helper function for ifalias_af_common().
934 #
935 ifalias_af_common_handler()
936 {
937         local _ret _if _af _action _args _c _tmpargs
938
939         _ret=1
940         _if=$1
941         _af=$2
942         _action=$3
943         shift 3
944         _args=$*
945
946         case $_args in
947         ${_af}\ *)      ;;
948         *)      return  ;;
949         esac
950
951         # link(ether) does not support address removal.
952         case $_af:$_action in
953         link:-alias|ether:-alias)       return ;;
954         esac
955
956         _tmpargs=
957         for _c in $_args; do
958                 case $_c in
959                 ${_af})
960                         case $_tmpargs in
961                         ${_af}\ *-*)
962                                 ifalias_af_common_handler $_if $_af $_action \
963                                 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
964                         ;;
965                         ${_af}\ *)
966                                 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
967                         ;;
968                         esac
969                         _tmpargs=$_af
970                 ;;
971                 *)
972                         _tmpargs="$_tmpargs $_c"
973                 ;;
974                 esac
975         done
976         # Process the last component if any.
977         if [ -n "$_tmpargs}" ]; then
978                 case $_tmpargs in
979                 ${_af}\ *-*)
980                         ifalias_af_common_handler $_if $_af $_action \
981                         `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
982                 ;;
983                 ${_af}\ *)
984                         ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
985                 ;;
986                 esac
987         fi
988
989         return $_ret
990 }
991
992 # ifalias_af_common if af action
993 #       Helper function for ifalias().
994 #
995 ifalias_af_common()
996 {
997         local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
998
999         _ret=1
1000         _aliasn=
1001         _if=$1
1002         _af=$2
1003         _action=$3
1004
1005         # ifconfig_IF_aliasN which starts with $_af
1006         alias=0
1007         while : ; do
1008                 ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}`
1009                 _iaf=
1010                 case $ifconfig_args in
1011                 inet\ *)        _iaf=inet ;;
1012                 inet6\ *)       _iaf=inet6 ;;
1013                 ipx\ *)         _iaf=ipx ;;
1014                 link\ *)        _iaf=link ;;
1015                 ether\ *)       _iaf=ether ;;
1016                 esac
1017
1018                 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1019                 ${_af}:*:${_af}:*)
1020                         _aliasn="$_aliasn $ifconfig_args"
1021                         ;;
1022                 ${_af}:*:"":"")
1023                         break
1024                         ;;
1025                 inet:alias:"":*)
1026                         _aliasn="$_aliasn inet $ifconfig_args"
1027                         warn "\$ifconfig_${_if}_alias${alias} needs " \
1028                             "\"inet\" keyword for an IPv4 address."
1029                 esac
1030                 alias=$(($alias + 1))
1031         done
1032
1033         # backward compatibility: ipv6_ifconfig_IF_aliasN.
1034         case $_af in
1035         inet6)
1036                 alias=0
1037                 while : ; do
1038                         ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
1039                         case ${_action}:"${ifconfig_args}" in
1040                         *:"")
1041                                 break
1042                         ;;
1043                         alias:*)
1044                                 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1045                                 warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
1046                                     "is obsolete.  Use ifconfig_$1_aliasN " \
1047                                     "instead."
1048                         ;;
1049                         esac
1050                         alias=$(($alias + 1))
1051                 done
1052         esac
1053
1054         # backward compatibility: ipv4_addrs_IF.
1055         for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1056                 _aliasn="$_aliasn inet $_tmpargs"
1057         done
1058
1059         # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1060         _tmpargs=
1061         for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1062                 case $_c in
1063                 inet|inet6|ipx|link|ether)
1064                         case $_tmpargs in
1065                         ${_af}\ *)
1066                                 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1067                         ;;
1068                         esac
1069                         _tmpargs=$_c
1070                 ;;
1071                 *)
1072                         _tmpargs="$_tmpargs $_c"
1073                 esac
1074         done
1075         # Process the last component
1076         case $_tmpargs in
1077         ${_af}\ *)
1078                 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1079         ;;
1080         esac
1081
1082         return $_ret
1083 }
1084
1085 # ipv6_prefix_hostid_addr_common if action
1086 #       Add or remove IPv6 prefix + hostid addr on the interface $if
1087 #
1088 ipv6_prefix_hostid_addr_common()
1089 {
1090         local _if _action prefix j
1091         _if=$1
1092         _action=$2
1093         prefix=`get_if_var ${_if} ipv6_prefix_IF`
1094
1095         if [ -n "${prefix}" ]; then
1096                 for j in ${prefix}; do
1097                         # The default prefixlen is 64.
1098                         plen=${j#*/}
1099                         case $j:$plen in
1100                         $plen:$j)       plen=64 ;;
1101                         *)              j=${j%/*} ;;
1102                         esac
1103
1104                         # Normalize the last part by removing ":"
1105                         j=${j%::*}
1106                         j=${j%:}
1107                         ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1108                                 prefixlen $plen eui64 ${_action}
1109
1110                         # if I am a router, add subnet router
1111                         # anycast address (RFC 2373).
1112                         if checkyesno ipv6_gateway_enable; then
1113                                 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1114                                         prefixlen $plen ${_action} anycast
1115                         fi
1116                 done
1117         fi
1118 }
1119
1120 # ipv6_accept_rtadv_up if
1121 #       Enable accepting Router Advertisement and send Router
1122 #       Solicitation message
1123 ipv6_accept_rtadv_up()
1124 {
1125         if ipv6_autoconfif $1; then
1126                 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1127                 if ! checkyesno rtsold_enable; then
1128                         rtsol ${rtsol_flags} $1
1129                 fi
1130         fi
1131 }
1132
1133 # ipv6_accept_rtadv_down if
1134 #       Disable accepting Router Advertisement
1135 ipv6_accept_rtadv_down()
1136 {
1137         if ipv6_autoconfif $1; then
1138                 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1139         fi
1140 }
1141
1142 # ifscript_up if
1143 #       Evaluate a startup script for the $if interface.
1144 #       It returns 0 if a script was found and processed or
1145 #       1 if no script was found.
1146 #
1147 ifscript_up()
1148 {
1149         if [ -r /etc/start_if.$1 ]; then
1150                 . /etc/start_if.$1
1151                 return 0
1152         else
1153                 return 1
1154         fi
1155 }
1156
1157 # ifscript_down if
1158 #       Evaluate a shutdown script for the $if interface.
1159 #       It returns 0 if a script was found and processed or
1160 #       1 if no script was found.
1161 #
1162 ifscript_down()
1163 {
1164         if [ -r /etc/stop_if.$1 ]; then
1165                 . /etc/stop_if.$1
1166                 return 0
1167         else
1168                 return 1
1169         fi
1170 }
1171
1172 # clone_up
1173 #       Create cloneable interfaces.
1174 #
1175 clone_up()
1176 {
1177         local _prefix _list ifn
1178         _prefix=
1179         _list=
1180
1181         # create_args_IF
1182         for ifn in ${cloned_interfaces}; do
1183                 ${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF`
1184                 if [ $? -eq 0 ]; then
1185                         _list="${_list}${_prefix}${ifn}"
1186                         [ -z "$_prefix" ] && _prefix=' '
1187                 fi
1188         done
1189         debug "Cloned: ${_list}"
1190 }
1191
1192 # clone_down
1193 #       Destroy cloned interfaces. Destroyed interfaces are echoed to
1194 #       standard output.
1195 #
1196 clone_down()
1197 {
1198         local _prefix _list ifn
1199         _prefix=
1200         _list=
1201
1202         for ifn in ${cloned_interfaces}; do
1203                 ${IFCONFIG_CMD} -n ${ifn} destroy
1204                 if [ $? -eq 0 ]; then
1205                         _list="${_list}${_prefix}${ifn}"
1206                         [ -z "$_prefix" ] && _prefix=' '
1207                 fi
1208         done
1209         debug "Destroyed clones: ${_list}"
1210 }
1211
1212 # childif_create
1213 #       Create and configure child interfaces.  Return 0 if child
1214 #       interfaces are created.
1215 #
1216 childif_create()
1217 {
1218         local cfg child child_vlans child_wlans create_args debug_flags ifn i
1219         cfg=1
1220         ifn=$1
1221
1222         # Create wireless interfaces
1223         child_wlans=`get_if_var $ifn wlans_IF`
1224
1225         for child in ${child_wlans}; do
1226                 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1227                 debug_flags="`get_if_var $child wlandebug_IF`"
1228
1229                 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1230                         ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1231                         if [ -n "${debug_flags}" ]; then
1232                                 wlandebug -i $child ${debug_flags}
1233                         fi
1234                 else
1235                         i=`${IFCONFIG_CMD} wlan create ${create_args}`
1236                         if [ -n "${debug_flags}" ]; then
1237                                 wlandebug -i $i ${debug_flags}
1238                         fi
1239                         ${IFCONFIG_CMD} $i name $child && cfg=0
1240                 fi
1241                 if autoif $child; then
1242                         ifn_start $child
1243                 fi
1244         done
1245
1246         # Create vlan interfaces
1247         child_vlans=`get_if_var $ifn vlans_IF`
1248
1249         if [ -n "${child_vlans}" ]; then
1250                 load_kld if_vlan
1251         fi
1252
1253         for child in ${child_vlans}; do
1254                 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1255                         child="${ifn}.${child}"
1256                         create_args=`get_if_var $child create_args_IF`
1257                         ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1258                 else
1259                         create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1260                         if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1261                                 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1262                         else
1263                                 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1264                                 ${IFCONFIG_CMD} $i name $child && cfg=0
1265                         fi
1266                 fi
1267                 if autoif $child; then
1268                         ifn_start $child
1269                 fi
1270         done
1271
1272         return ${cfg}
1273 }
1274
1275 # childif_destroy
1276 #       Destroy child interfaces.
1277 #
1278 childif_destroy()
1279 {
1280         local cfg child child_vlans child_wlans ifn
1281         cfg=1
1282
1283         child_wlans=`get_if_var $ifn wlans_IF`
1284         for child in ${child_wlans}; do
1285                 if ! ifexists $child; then
1286                         continue
1287                 fi
1288                 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1289         done
1290
1291         child_vlans=`get_if_var $ifn vlans_IF`
1292         for child in ${child_vlans}; do
1293                 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1294                         child="${ifn}.${child}"
1295                 fi
1296                 if ! ifexists $child; then
1297                         continue
1298                 fi
1299                 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1300         done
1301
1302         return ${cfg}
1303 }
1304
1305 # ng_mkpeer
1306 #       Create netgraph nodes.
1307 #
1308 ng_mkpeer()
1309 {
1310         ngctl -f - 2> /dev/null <<EOF
1311 mkpeer $*
1312 msg dummy nodeinfo
1313 EOF
1314 }
1315
1316 # ng_create_one
1317 #       Create netgraph nodes.
1318 #
1319 ng_create_one()
1320 {
1321         local t
1322
1323         ng_mkpeer $* | while read line; do
1324                 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1325                 if [ -n "${t}" ]; then
1326                         echo ${t}
1327                         return
1328                 fi
1329         done
1330 }
1331
1332 # gif_up
1333 #       Create gif(4) tunnel interfaces.
1334 gif_up()
1335 {
1336         local i peers
1337
1338         for i in ${gif_interfaces}; do
1339                 peers=`get_if_var $i gifconfig_IF`
1340                 case ${peers} in
1341                 '')
1342                         continue
1343                         ;;
1344                 *)
1345                         if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
1346                                 ${IFCONFIG_CMD} $i create >/dev/null 2>&1
1347                         else
1348                                 gif=`${IFCONFIG_CMD} gif create`
1349                                 ${IFCONFIG_CMD} $gif name $i
1350                         fi
1351                         ${IFCONFIG_CMD} $i tunnel ${peers}
1352                         ${IFCONFIG_CMD} $i up
1353                         ;;
1354                 esac
1355         done
1356 }
1357
1358 # ng_fec_create ifn
1359 #       Configure Fast EtherChannel for interface $ifn. Returns 0 if
1360 #       FEC arguments were found and configured; returns !0 otherwise.
1361 ng_fec_create()
1362 {
1363          local req_iface iface bogus
1364          req_iface="$1"
1365
1366          ngctl shutdown ${req_iface}: > /dev/null 2>&1
1367
1368          bogus=""
1369          while true; do
1370                  iface=`ng_create_one fec dummy fec`
1371                  if [ -z "${iface}" ]; then
1372                          exit 2
1373                  fi
1374                  if [ "${iface}" = "${req_iface}" ]; then
1375                          break
1376                  fi
1377                  bogus="${bogus} ${iface}"
1378          done
1379
1380          for iface in ${bogus}; do
1381                  ngctl shutdown ${iface}:
1382          done
1383 }
1384
1385 # fec_up
1386 #       Create Fast EtherChannel interfaces.
1387 fec_up()
1388 {
1389         local i j
1390
1391         for i in ${fec_interfaces}; do
1392                 ng_fec_create $i
1393                 for j in `get_if_var $i fecconfig_IF`; do
1394                         case ${j} in
1395                         '')
1396                                 continue
1397                                 ;;
1398                         *)
1399                                 ngctl msg ${i}: add_iface "\"${j}\""
1400                                 ;;
1401                         esac
1402                 done
1403         done
1404 }
1405
1406 # ipx_up ifn
1407 #       Configure any IPX addresses for interface $ifn. Returns 0 if
1408 #       IPX arguments were found and configured; returns 1 otherwise.
1409 #
1410 ipx_up()
1411 {
1412         local ifn
1413         ifn="$1"
1414
1415         # ifconfig_IF_ipx
1416         ifconfig_args=`_ifconfig_getargs $ifn ipx`
1417         if [ -n "${ifconfig_args}" ]; then
1418                 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1419                 return 0
1420         fi
1421
1422         return 1
1423 }
1424
1425 # ipx_down ifn
1426 #       Remove IPX addresses for interface $ifn. Returns 0 if IPX
1427 #       addresses were found and unconfigured. It returns 1, otherwise.
1428 #
1429 ipx_down()
1430 {
1431         local _if _ifs _ret ipxList oldifs _ipx
1432         _if=$1
1433         _ifs="^"
1434         _ret=1
1435         ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1436         oldifs="$IFS"
1437
1438         IFS="$_ifs"
1439         for _ipx in $ipxList ; do
1440                 # get rid of extraneous line
1441                 [ -z "$_ipx" ] && break
1442
1443                 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1444
1445                 IFS="$oldifs"
1446                 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1447                 IFS="$_ifs"
1448                 _ret=0
1449         done
1450         IFS="$oldifs"
1451
1452         return $_ret
1453 }
1454
1455 # ifnet_rename
1456 #       Rename all requested interfaces.
1457 #
1458 ifnet_rename()
1459 {
1460         local _if _ifname
1461
1462         # ifconfig_IF_name
1463         for _if in `${IFCONFIG_CMD} -l`; do
1464                 _ifname=`get_if_var $_if ifconfig_IF_name`
1465                 if [ ! -z "$_ifname" ]; then
1466                         ${IFCONFIG_CMD} $_if name $_ifname
1467                 fi
1468         done
1469
1470         return 0
1471 }
1472
1473 # list_net_interfaces type
1474 #       List all network interfaces. The type of interface returned
1475 #       can be controlled by the type argument. The type
1476 #       argument can be any of the following:
1477 #               nodhcp  - all interfaces, excluding DHCP configured interfaces
1478 #               dhcp    - list only DHCP configured interfaces
1479 #               noautoconf      - all interfaces, excluding IPv6 Stateless
1480 #                                 Address Autoconf configured interfaces
1481 #               autoconf        - list only IPv6 Stateless Address Autoconf
1482 #                                 configured interfaces
1483 #       If no argument is specified all network interfaces are output.
1484 #       Note that the list will include cloned interfaces if applicable.
1485 #       Cloned interfaces must already exist to have a chance to appear
1486 #       in the list if ${network_interfaces} is set to `auto'.
1487 #
1488 list_net_interfaces()
1489 {
1490         local type _tmplist _list _autolist _lo _if
1491         type=$1
1492
1493         # Get a list of ALL the interfaces and make lo0 first if it's there.
1494         #
1495         _tmplist=
1496         case ${network_interfaces} in
1497         [Aa][Uu][Tt][Oo])
1498                 _autolist="`${IFCONFIG_CMD} -l`"
1499                 _lo=
1500                 for _if in ${_autolist} ; do
1501                         if autoif $_if; then
1502                                 if [ "$_if" = "lo0" ]; then
1503                                         _lo="lo0 "
1504                                 else
1505                                         _tmplist="${_tmplist} ${_if}"
1506                                 fi
1507                         fi
1508                 done
1509                 _tmplist="${_lo}${_tmplist# }"
1510                 ;;
1511         *)
1512                 _tmplist="${network_interfaces} ${cloned_interfaces}"
1513
1514                 # lo0 is effectively mandatory, so help prevent foot-shooting
1515                 #
1516                 case "$_tmplist" in
1517                 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1518                 *)      _tmplist="lo0 ${_tmplist}" ;;
1519                 esac
1520                 ;;
1521         esac
1522
1523         _list=
1524         case "$type" in
1525         nodhcp)
1526                 for _if in ${_tmplist} ; do
1527                         if ! dhcpif $_if && \
1528                            [ -n "`_ifconfig_getargs $_if`" ]; then
1529                                 _list="${_list# } ${_if}"
1530                         fi
1531                 done
1532                 ;;
1533         dhcp)
1534                 for _if in ${_tmplist} ; do
1535                         if dhcpif $_if; then
1536                                 _list="${_list# } ${_if}"
1537                         fi
1538                 done
1539                 ;;
1540         noautoconf)
1541                 for _if in ${_tmplist} ; do
1542                         if ! ipv6_autoconfif $_if && \
1543                            [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1544                                 _list="${_list# } ${_if}"
1545                         fi
1546                 done
1547                 ;;
1548         autoconf)
1549                 for _if in ${_tmplist} ; do
1550                         if ipv6_autoconfif $_if; then
1551                                 _list="${_list# } ${_if}"
1552                         fi
1553                 done
1554                 ;;
1555         *)
1556                 _list=${_tmplist}
1557                 ;;
1558         esac
1559
1560         echo $_list
1561
1562         return 0
1563 }
1564
1565 # get_default_if -address_family
1566 #       Get the interface of the default route for the given address family.
1567 #       The -address_family argument must be suitable passing to route(8).
1568 #
1569 get_default_if()
1570 {
1571         local routeget oldifs defif line
1572         defif=
1573         oldifs="$IFS"
1574         IFS="
1575 "
1576         for line in `route -n get $1 default 2>/dev/null`; do
1577                 case $line in
1578                 *interface:*)
1579                         defif=${line##*: }
1580                         ;;
1581                 esac
1582         done
1583         IFS=${oldifs}
1584
1585         echo $defif
1586 }
1587
1588 # hexdigit arg
1589 #       Echo decimal number $arg (single digit) in hexadecimal format.
1590 hexdigit()
1591 {
1592         printf '%x\n' "$1"
1593 }
1594
1595 # hexprint arg
1596 #       Echo decimal number $arg (multiple digits) in hexadecimal format.
1597 hexprint()
1598 {
1599         printf '%x\n' "$1"
1600 }
1601
1602 is_wired_interface()
1603 {
1604         local media
1605
1606         case `${IFCONFIG_CMD} $1 2>/dev/null` in
1607         *media:?Ethernet*) media=Ethernet ;;
1608         esac
1609
1610         test "$media" = "Ethernet"
1611 }
1612
1613 # network6_getladdr if [flag]
1614 #       Echo link-local address from $if if any.
1615 #       If flag is defined, tentative ones will be excluded.
1616 network6_getladdr()
1617 {
1618         local _if _flag proto addr rest
1619         _if=$1
1620         _flag=$2
1621
1622         ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1623                 case "${proto}/${addr}/${_flag}/${rest}" in
1624                 inet6/fe80::*//*)
1625                         echo ${addr}
1626                 ;;
1627                 inet6/fe80:://*tentative*)      # w/o flag
1628                         sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1629                         network6_getladdr $_if $_flags
1630                 ;;
1631                 inet6/fe80::/*/*tentative*)     # w/ flag
1632                         echo ${addr}
1633                 ;;
1634                 *)
1635                         continue
1636                 ;;
1637                 esac
1638
1639                 return
1640         done
1641 }