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