]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/network.subr
Pull in all the OpenSSH bits that we'd previously left out because we
[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\t" "$_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                 inet\ *)        ;;
665                 *)              continue ;;
666                 esac
667
668                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
669
670                 IFS="$oldifs"
671                 ${IFCONFIG_CMD} ${_if} ${_inet} delete
672                 IFS="$_ifs"
673                 _ret=0
674         done
675         IFS="$oldifs"
676
677         return $_ret
678 }
679
680 # ipv6_down if
681 #       remove IPv6 addresses from the interface $if
682 ipv6_down()
683 {
684         local _if _ifs _ret inetList oldifs _inet6
685         _if=$1
686         _ifs="^"
687         _ret=1
688
689         if ! ipv6if $_if; then
690                 return 0
691         fi
692
693         ipv6_accept_rtadv_down ${_if} && _ret=0
694         ipv6_prefix_hostid_addr_common ${_if} -alias && _ret=0
695         ifalias ${_if} inet6 -alias && _ret=0
696
697         inetList="`${IFCONFIG_CMD} ${_if} | grep 'inet6 ' | tr "\n\t" "$_ifs"`"
698
699         oldifs="$IFS"
700         IFS="$_ifs"
701         for _inet6 in $inetList ; do
702                 # get rid of extraneous line
703                 case $_inet in
704                 inet6\ *)       ;;
705                 *)              continue ;;
706                 esac
707
708                 _inet6=`expr "$_inet6" : '.*\(inet6 \([0-9a-f:]*\)\).*'`
709
710                 IFS="$oldifs"
711                 ${IFCONFIG_CMD} ${_if} ${_inet6} -alias
712                 IFS="$_ifs"
713                 _ret=0
714         done
715         IFS="$oldifs"
716
717         return $_ret
718 }
719
720 # ifalias if af action
721 #       Configure or remove aliases for network interface $if.
722 #       It returns 0 if at least one alias was configured or
723 #       removed, or 1 if there were none.
724 #
725 ifalias()
726 {
727         local _ret
728         _ret=1
729
730         afexists $2 || return $_ret
731
732         case "$2" in
733         inet|inet6|link|ether)
734                 ifalias_af_common $1 $2 $3 && _ret=0
735                 ;;
736         esac
737
738         return $_ret
739 }
740
741 # ifalias_expand_addr af action addr
742 #       Expand address range ("N-M") specification in addr.
743 #       "addr" must not include an address-family keyword.
744 #       The results will include an address-family keyword.
745 #
746 ifalias_expand_addr()
747 {
748         local _af _action
749
750         _af=$1
751         _action=$2
752         shift 2
753
754         afexists $_af || return
755         ifalias_expand_addr_$_af $_action $*
756 }
757
758 # ifalias_expand_addr_inet action addr
759 #       Helper function for ifalias_expand_addr().  Handles IPv4.
760 #
761 ifalias_expand_addr_inet()
762 {
763         local _action _arg _cidr _cidr_addr _exargs
764         local _ipaddr _plen _range _iphead _iptail _iplow _iphigh _ipcount
765         local _retstr _c
766         _action=$1
767         _arg=$2
768         shift 2
769         _exargs=$*
770         _retstr=
771
772         case $_action:$_arg:$_exargs in
773         *:*--*)         return ;;       # invalid
774         tmp:*[0-9]-[0-9]*:*)            # to be expanded
775                 _action="alias"
776         ;;
777         *:*[0-9]-[0-9]*:*)              # to be expanded
778         ;;
779         tmp:*:*netmask*)                # already expanded w/ netmask option
780                 echo ${_arg%/[0-9]*} $_exargs && return
781         ;;
782         tmp:*:*)                        # already expanded w/o netmask option
783                 echo $_arg $_exargs && return
784         ;;
785         *:*:*netmask*)                  # already expanded w/ netmask option
786                 echo inet ${_arg%/[0-9]*} $_exargs && return
787         ;;
788         *:*:*)                          # already expanded w/o netmask option
789                 echo inet $_arg $_exargs && return
790         ;;
791         esac
792
793         for _cidr in $_arg; do
794                 _ipaddr=${_cidr%%/*}
795                 _plen=${_cidr##*/}
796                 # When subnet prefix length is not specified, use /32.
797                 case $_plen in
798                 $_ipaddr)       _plen=32 ;;     # "/" character not found
799                 esac
800
801                 OIFS=$IFS
802                 IFS=. set -- $_ipaddr
803                 _range=
804                 _iphead=
805                 _iptail=
806                 for _c in $@; do
807                         case $_range:$_c in
808                         :[0-9]*-[0-9]*)
809                                 _range=$_c
810                         ;;
811                         :*)
812                                 _iphead="${_iphead}${_iphead:+.}${_c}"
813                         ;;
814                         *:*)
815                                 _iptail="${_iptail}${_iptail:+.}${_c}"
816                         ;;
817                         esac
818                 done
819                 IFS=$OIFS
820                 _iplow=${_range%-*}
821                 _iphigh=${_range#*-}
822
823                 # clear netmask when removing aliases
824                 if [ "$_action" = "-alias" ]; then
825                         _plen=""
826                 fi
827
828                 _ipcount=$_iplow
829                 while [ "$_ipcount" -le "$_iphigh" ]; do
830                         _retstr="${_retstr} ${_iphead}${_iphead:+.}${_ipcount}${_iptail:+.}${_iptail}${_plen:+/}${_plen}"
831                         if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]; then
832                                 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."
833                                 break
834                         else
835                                 _ipcount=$(($_ipcount + 1))
836                         fi
837                         # Forcibly set /32 for remaining aliases.
838                         _plen=32
839                 done
840         done
841
842         for _c in $_retstr; do
843                 ifalias_expand_addr_inet $_action $_c $_exargs
844         done
845 }
846
847 # ifalias_expand_addr_inet6 action addr
848 #       Helper function for ifalias_expand_addr().  Handles IPv6.
849 #
850 ifalias_expand_addr_inet6()
851 {
852         local _action _arg _cidr _cidr_addr _exargs
853         local _ipaddr _plen _ipleft _ipright _iplow _iphigh _ipcount
854         local _ipv4part
855         local _retstr _c
856         _action=$1
857         _arg=$2
858         shift 2
859         _exargs=$*
860         _retstr=
861
862         case $_action:$_arg:$_exargs in
863         *:*--*:*)       return ;;       # invalid
864         tmp:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)# to be expanded
865                 _action="alias"
866         ;;
867         *:*[0-9a-zA-Z]-[0-9a-zA-Z]*:*)  # to be expanded
868         ;;
869         tmp:*:*prefixlen*)      # already expanded w/ prefixlen option
870                 echo ${_arg%/[0-9]*} $_exargs && return
871         ;;
872         tmp:*:*)                # already expanded w/o prefixlen option
873                 echo $_arg $_exargs && return
874         ;;
875         *:*:*prefixlen*)        # already expanded w/ prefixlen option
876                 echo inet6 ${_arg%/[0-9]*} $_exargs && return
877         ;;
878         *:*:*)                  # already expanded w/o prefixlen option
879                 echo inet6 $_arg $_exargs && return
880         ;;
881         esac
882
883         for _cidr in $_arg; do
884                 _ipaddr="${_cidr%%/*}"
885                 _plen="${_cidr##*/}"
886
887                 case $_action:$_ipaddr:$_cidr in
888                 -alias:*:*)             unset _plen ;;
889                 *:$_cidr:$_ipaddr)      unset _plen ;;
890                 esac
891
892                 if [ "${_ipaddr%:*.*.*.*}" = "$_ipaddr" ]; then
893                         # Handle !v4mapped && !v4compat addresses.
894
895                         # The default prefix length is 64.
896                         case $_ipaddr:$_cidr in
897                         $_cidr:$_ipaddr)        _plen="64" ;;
898                         esac
899                         _ipleft=${_ipaddr%-*}
900                         _ipright=${_ipaddr#*-}
901                         _iplow=${_ipleft##*:}
902                         _iphigh=${_ipright%%:*}
903                         _ipleft=${_ipleft%:*}
904                         _ipright=${_ipright#*:}
905
906                         if [ "$_iphigh" = "$_ipright" ]; then
907                                 unset _ipright
908                         else
909                                 _ipright=:$_ipright
910                         fi
911
912                         if [ -n "$_iplow" -a -n "$_iphigh" ]; then
913                                 _iplow=$((0x$_iplow))
914                                 _iphigh=$((0x$_iphigh))
915                                 _ipcount=$_iplow
916                                 while [ $_ipcount -le $_iphigh ]; do
917                                         _r=`printf "%s:%04x%s%s" \
918                                             $_ipleft $_ipcount $_ipright \
919                                             ${_plen:+/}$_plen`
920                                         _retstr="$_retstr $_r"
921                                         if [ $_ipcount -gt $(($_iplow + $_IPEXPANDMAX)) ]
922                                         then
923                                                 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."
924                                                 break
925                                         else
926                                                 _ipcount=$(($_ipcount + 1))
927                                         fi
928                                 done
929                         else
930                                 _retstr="${_ipaddr}${_plen:+/}${_plen}"
931                         fi
932
933                         for _c in $_retstr; do
934                                 ifalias_expand_addr_inet6 $_action $_c $_exargs
935                         done
936                 else
937                         # v4mapped/v4compat should handle as an IPv4 alias
938                         _ipv4part=${_ipaddr##*:}
939
940                         # Adjust prefix length if any.  If not, set the
941                         # default prefix length as 32.
942                         case $_ipaddr:$_cidr in
943                         $_cidr:$_ipaddr)        _plen=32 ;;
944                         *)                      _plen=$(($_plen - 96)) ;;
945                         esac
946
947                         _retstr=`ifalias_expand_addr_inet \
948                             tmp ${_ipv4part}${_plen:+/}${_plen}`
949                         for _c in $_retstr; do
950                                 ifalias_expand_addr_inet $_action $_c $_exargs
951                         done
952                 fi
953         done
954 }
955
956 # ifalias_af_common_handler if af action args
957 #       Helper function for ifalias_af_common().
958 #
959 ifalias_af_common_handler()
960 {
961         local _ret _if _af _action _args _c _tmpargs
962
963         _ret=1
964         _if=$1
965         _af=$2
966         _action=$3
967         shift 3
968         _args=$*
969
970         case $_args in
971         ${_af}\ *)      ;;
972         *)      return  ;;
973         esac
974
975         # link(ether) does not support address removal.
976         case $_af:$_action in
977         link:-alias|ether:-alias)       return ;;
978         esac
979
980         _tmpargs=
981         for _c in $_args; do
982                 case $_c in
983                 ${_af})
984                         case $_tmpargs in
985                         ${_af}\ *-*)
986                                 ifalias_af_common_handler $_if $_af $_action \
987                                 `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
988                         ;;
989                         ${_af}\ *)
990                                 ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
991                         ;;
992                         esac
993                         _tmpargs=$_af
994                 ;;
995                 *)
996                         _tmpargs="$_tmpargs $_c"
997                 ;;
998                 esac
999         done
1000         # Process the last component if any.
1001         if [ -n "$_tmpargs}" ]; then
1002                 case $_tmpargs in
1003                 ${_af}\ *-*)
1004                         ifalias_af_common_handler $_if $_af $_action \
1005                         `ifalias_expand_addr $_af $_action ${_tmpargs#${_af}\ }`
1006                 ;;
1007                 ${_af}\ *)
1008                         ${IFCONFIG_CMD} $_if $_tmpargs $_action && _ret=0
1009                 ;;
1010                 esac
1011         fi
1012
1013         return $_ret
1014 }
1015
1016 # ifalias_af_common if af action
1017 #       Helper function for ifalias().
1018 #
1019 ifalias_af_common()
1020 {
1021         local _ret _if _af _action alias ifconfig_args _aliasn _c _tmpargs _iaf
1022
1023         _ret=1
1024         _aliasn=
1025         _if=$1
1026         _af=$2
1027         _action=$3
1028
1029         # ifconfig_IF_aliasN which starts with $_af
1030         alias=0
1031         while : ; do
1032                 ifconfig_args=`get_if_var $_if ifconfig_IF_alias${alias}`
1033                 _iaf=
1034                 case $ifconfig_args in
1035                 inet\ *)        _iaf=inet ;;
1036                 inet6\ *)       _iaf=inet6 ;;
1037                 ipx\ *)         _iaf=ipx ;;
1038                 link\ *)        _iaf=link ;;
1039                 ether\ *)       _iaf=ether ;;
1040                 esac
1041
1042                 case ${_af}:${_action}:${_iaf}:"${ifconfig_args}" in
1043                 ${_af}:*:${_af}:*)
1044                         _aliasn="$_aliasn $ifconfig_args"
1045                         ;;
1046                 ${_af}:*:"":"")
1047                         break
1048                         ;;
1049                 inet:alias:"":*)
1050                         _aliasn="$_aliasn inet $ifconfig_args"
1051                         warn "\$ifconfig_${_if}_alias${alias} needs " \
1052                             "\"inet\" keyword for an IPv4 address."
1053                 esac
1054                 alias=$(($alias + 1))
1055         done
1056
1057         # backward compatibility: ipv6_ifconfig_IF_aliasN.
1058         case $_af in
1059         inet6)
1060                 alias=0
1061                 while : ; do
1062                         ifconfig_args=`get_if_var $_if ipv6_ifconfig_IF_alias${alias}`
1063                         case ${_action}:"${ifconfig_args}" in
1064                         *:"")
1065                                 break
1066                         ;;
1067                         alias:*)
1068                                 _aliasn="${_aliasn} inet6 ${ifconfig_args}"
1069                                 warn "\$ipv6_ifconfig_${_if}_alias${alias} " \
1070                                     "is obsolete.  Use ifconfig_$1_aliasN " \
1071                                     "instead."
1072                         ;;
1073                         esac
1074                         alias=$(($alias + 1))
1075                 done
1076         esac
1077
1078         # backward compatibility: ipv4_addrs_IF.
1079         for _tmpargs in `get_if_var $_if ipv4_addrs_IF`; do
1080                 _aliasn="$_aliasn inet $_tmpargs"
1081         done
1082
1083         # Handle ifconfig_IF_aliases, ifconfig_IF_aliasN, and the others.
1084         _tmpargs=
1085         for _c in `get_if_var $_if ifconfig_IF_aliases` $_aliasn; do
1086                 case $_c in
1087                 inet|inet6|ipx|link|ether)
1088                         case $_tmpargs in
1089                         ${_af}\ *)
1090                                 eval ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1091                         ;;
1092                         esac
1093                         _tmpargs=$_c
1094                 ;;
1095                 *)
1096                         _tmpargs="$_tmpargs $_c"
1097                 esac
1098         done
1099         # Process the last component
1100         case $_tmpargs in
1101         ${_af}\ *)
1102                 ifalias_af_common_handler $_if $_af $_action $_tmpargs && _ret=0
1103         ;;
1104         esac
1105
1106         return $_ret
1107 }
1108
1109 # ipv6_prefix_hostid_addr_common if action
1110 #       Add or remove IPv6 prefix + hostid addr on the interface $if
1111 #
1112 ipv6_prefix_hostid_addr_common()
1113 {
1114         local _if _action prefix j
1115         _if=$1
1116         _action=$2
1117         prefix=`get_if_var ${_if} ipv6_prefix_IF`
1118
1119         if [ -n "${prefix}" ]; then
1120                 for j in ${prefix}; do
1121                         # The default prefixlen is 64.
1122                         plen=${j#*/}
1123                         case $j:$plen in
1124                         $plen:$j)       plen=64 ;;
1125                         *)              j=${j%/*} ;;
1126                         esac
1127
1128                         # Normalize the last part by removing ":"
1129                         j=${j%::*}
1130                         j=${j%:}
1131                         ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1132                                 prefixlen $plen eui64 ${_action}
1133
1134                         # if I am a router, add subnet router
1135                         # anycast address (RFC 2373).
1136                         if checkyesno ipv6_gateway_enable; then
1137                                 ${IFCONFIG_CMD} ${_if} inet6 $j:: \
1138                                         prefixlen $plen ${_action} anycast
1139                         fi
1140                 done
1141         fi
1142 }
1143
1144 # ipv6_accept_rtadv_up if
1145 #       Enable accepting Router Advertisement and send Router
1146 #       Solicitation message
1147 ipv6_accept_rtadv_up()
1148 {
1149         if ipv6_autoconfif $1; then
1150                 ${IFCONFIG_CMD} $1 inet6 accept_rtadv up
1151                 if ! checkyesno rtsold_enable; then
1152                         rtsol ${rtsol_flags} $1
1153                 fi
1154         fi
1155 }
1156
1157 # ipv6_accept_rtadv_down if
1158 #       Disable accepting Router Advertisement
1159 ipv6_accept_rtadv_down()
1160 {
1161         if ipv6_autoconfif $1; then
1162                 ${IFCONFIG_CMD} $1 inet6 -accept_rtadv
1163         fi
1164 }
1165
1166 # ifscript_up if
1167 #       Evaluate a startup script for the $if interface.
1168 #       It returns 0 if a script was found and processed or
1169 #       1 if no script was found.
1170 #
1171 ifscript_up()
1172 {
1173         if [ -r /etc/start_if.$1 ]; then
1174                 . /etc/start_if.$1
1175                 return 0
1176         else
1177                 return 1
1178         fi
1179 }
1180
1181 # ifscript_down if
1182 #       Evaluate a shutdown script for the $if interface.
1183 #       It returns 0 if a script was found and processed or
1184 #       1 if no script was found.
1185 #
1186 ifscript_down()
1187 {
1188         if [ -r /etc/stop_if.$1 ]; then
1189                 . /etc/stop_if.$1
1190                 return 0
1191         else
1192                 return 1
1193         fi
1194 }
1195
1196 # clone_up
1197 #       Create cloneable interfaces.
1198 #
1199 clone_up()
1200 {
1201         local _prefix _list ifn ifopt _iflist _n tmpargs
1202         _prefix=
1203         _list=
1204         _iflist=$*
1205
1206         # create_args_IF
1207         for ifn in ${cloned_interfaces}; do
1208                 # Parse ifn:ifopt.
1209                 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1210                 case $_iflist in
1211                 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)     ;;
1212                 *)      continue ;;
1213                 esac
1214                 # Skip if ifn already exists.
1215                 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1216                         continue
1217                 fi
1218                 ${IFCONFIG_CMD} ${ifn} create `get_if_var ${ifn} create_args_IF`
1219                 if [ $? -eq 0 ]; then
1220                         _list="${_list}${_prefix}${ifn}"
1221                         [ -z "$_prefix" ] && _prefix=' '
1222                 fi
1223         done
1224         if [ -n "$gif_interfaces" ]; then
1225                 warn "\$gif_interfaces is obsolete.  Use \$cloned_interfaces instead."
1226         fi
1227         for ifn in ${gif_interfaces}; do
1228                 # Parse ifn:ifopt.
1229                 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1230                 case $_iflist in
1231                 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)     ;;
1232                 *)      continue ;;
1233                 esac
1234                 # Skip if ifn already exists.
1235                 if ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1236                         continue
1237                 fi
1238                 case $ifn in
1239                 gif[0-9]*)
1240                         ${IFCONFIG_CMD} $ifn create
1241                 ;;
1242                 *)
1243                         _n=$(${IFCONFIG_CMD} gif create)
1244                         ${IFCONFIG_CMD} $_n name $ifn
1245                 ;;
1246                 esac
1247                 if [ $? -eq 0 ]; then
1248                         _list="${_list}${_prefix}${ifn}"
1249                         [ -z "$_prefix" ] && _prefix=' '
1250                 fi
1251                 tmpargs=$(get_if_var $ifn gifconfig_IF)
1252                 eval ifconfig_${ifn}=\"tunnel \$tmpargs\"
1253         done
1254         if [ -n "${_list}" ]; then
1255                 echo "Created clone interfaces: ${_list}."
1256         fi
1257         debug "Cloned: ${_list}"
1258 }
1259
1260 # clone_down
1261 #       Destroy cloned interfaces. Destroyed interfaces are echoed to
1262 #       standard output.
1263 #
1264 clone_down()
1265 {
1266         local _prefix _list ifn ifopt _iflist _sticky
1267         _prefix=
1268         _list=
1269         _iflist=$*
1270
1271         : ${cloned_interfaces_sticky:=NO}
1272         if checkyesno cloned_interfaces_sticky; then
1273                 _sticky=1
1274         else
1275                 _sticky=0
1276         fi
1277         for ifn in ${cloned_interfaces} ${gif_interfaces}; do
1278                 # Parse ifn:ifopt.
1279                 OIFS=$IFS; IFS=:; set -- $ifn; ifn=$1; ifopt=$2; IFS=$OIFS
1280                 case $ifopt:$_sticky in
1281                 sticky:*)       continue ;;     # :sticky => not destroy
1282                 nosticky:*)     ;;              # :nosticky => destroy
1283                 *:1)            continue ;;     # global sticky knob == 1
1284                 esac
1285                 case $_iflist in
1286                 ""|$ifn|$ifn\ *|*\ $ifn\ *|*\ $ifn)     ;;
1287                 *)      continue ;;
1288                 esac
1289                 # Skip if ifn does not exist.
1290                 if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
1291                         continue
1292                 fi
1293                 ${IFCONFIG_CMD} -n ${ifn} destroy
1294                 if [ $? -eq 0 ]; then
1295                         _list="${_list}${_prefix}${ifn}"
1296                         [ -z "$_prefix" ] && _prefix=' '
1297                 fi
1298         done
1299         if [ -n "${_list}" ]; then
1300                 echo "Destroyed clone interfaces: ${_list}."
1301         fi
1302         debug "Destroyed clones: ${_list}"
1303 }
1304
1305 # childif_create
1306 #       Create and configure child interfaces.  Return 0 if child
1307 #       interfaces are created.
1308 #
1309 childif_create()
1310 {
1311         local cfg child child_vlans child_wlans create_args debug_flags ifn i
1312         cfg=1
1313         ifn=$1
1314
1315         # Create wireless interfaces
1316         child_wlans=`get_if_var $ifn wlans_IF`
1317
1318         for child in ${child_wlans}; do
1319                 create_args="wlandev $ifn `get_if_var $child create_args_IF`"
1320                 debug_flags="`get_if_var $child wlandebug_IF`"
1321
1322                 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
1323                         ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1324                         if [ -n "${debug_flags}" ]; then
1325                                 wlandebug -i $child ${debug_flags}
1326                         fi
1327                 else
1328                         i=`${IFCONFIG_CMD} wlan create ${create_args}`
1329                         if [ -n "${debug_flags}" ]; then
1330                                 wlandebug -i $i ${debug_flags}
1331                         fi
1332                         ${IFCONFIG_CMD} $i name $child && cfg=0
1333                 fi
1334                 if autoif $child; then
1335                         ifn_start $child
1336                 fi
1337         done
1338
1339         # Create vlan interfaces
1340         child_vlans=`get_if_var $ifn vlans_IF`
1341
1342         if [ -n "${child_vlans}" ]; then
1343                 load_kld if_vlan
1344         fi
1345
1346         for child in ${child_vlans}; do
1347                 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1348                         child="${ifn}.${child}"
1349                         create_args=`get_if_var $child create_args_IF`
1350                         ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1351                 else
1352                         create_args="vlandev $ifn `get_if_var $child create_args_IF`"
1353                         if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
1354                                 ${IFCONFIG_CMD} $child create ${create_args} && cfg=0
1355                         else
1356                                 i=`${IFCONFIG_CMD} vlan create ${create_args}`
1357                                 ${IFCONFIG_CMD} $i name $child && cfg=0
1358                         fi
1359                 fi
1360                 if autoif $child; then
1361                         ifn_start $child
1362                 fi
1363         done
1364
1365         return ${cfg}
1366 }
1367
1368 # childif_destroy
1369 #       Destroy child interfaces.
1370 #
1371 childif_destroy()
1372 {
1373         local cfg child child_vlans child_wlans ifn
1374         cfg=1
1375
1376         child_wlans=`get_if_var $ifn wlans_IF`
1377         for child in ${child_wlans}; do
1378                 if ! ifexists $child; then
1379                         continue
1380                 fi
1381                 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1382         done
1383
1384         child_vlans=`get_if_var $ifn vlans_IF`
1385         for child in ${child_vlans}; do
1386                 if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
1387                         child="${ifn}.${child}"
1388                 fi
1389                 if ! ifexists $child; then
1390                         continue
1391                 fi
1392                 ${IFCONFIG_CMD} -n $child destroy && cfg=0
1393         done
1394
1395         return ${cfg}
1396 }
1397
1398 # ng_mkpeer
1399 #       Create netgraph nodes.
1400 #
1401 ng_mkpeer()
1402 {
1403         ngctl -f - 2> /dev/null <<EOF
1404 mkpeer $*
1405 msg dummy nodeinfo
1406 EOF
1407 }
1408
1409 # ng_create_one
1410 #       Create netgraph nodes.
1411 #
1412 ng_create_one()
1413 {
1414         local t
1415
1416         ng_mkpeer $* | while read line; do
1417                 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
1418                 if [ -n "${t}" ]; then
1419                         echo ${t}
1420                         return
1421                 fi
1422         done
1423 }
1424
1425 # ng_fec_create ifn
1426 #       Configure Fast EtherChannel for interface $ifn. Returns 0 if
1427 #       FEC arguments were found and configured; returns !0 otherwise.
1428 ng_fec_create()
1429 {
1430          local req_iface iface bogus
1431          req_iface="$1"
1432
1433          ngctl shutdown ${req_iface}: > /dev/null 2>&1
1434
1435          bogus=""
1436          while true; do
1437                  iface=`ng_create_one fec dummy fec`
1438                  if [ -z "${iface}" ]; then
1439                          exit 2
1440                  fi
1441                  if [ "${iface}" = "${req_iface}" ]; then
1442                          break
1443                  fi
1444                  bogus="${bogus} ${iface}"
1445          done
1446
1447          for iface in ${bogus}; do
1448                  ngctl shutdown ${iface}:
1449          done
1450 }
1451
1452 # fec_up
1453 #       Create Fast EtherChannel interfaces.
1454 fec_up()
1455 {
1456         local i j
1457
1458         for i in ${fec_interfaces}; do
1459                 ng_fec_create $i
1460                 for j in `get_if_var $i fecconfig_IF`; do
1461                         case ${j} in
1462                         '')
1463                                 continue
1464                                 ;;
1465                         *)
1466                                 ngctl msg ${i}: add_iface "\"${j}\""
1467                                 ;;
1468                         esac
1469                 done
1470         done
1471 }
1472
1473 # ipx_up ifn
1474 #       Configure any IPX addresses for interface $ifn. Returns 0 if
1475 #       IPX arguments were found and configured; returns 1 otherwise.
1476 #
1477 ipx_up()
1478 {
1479         local ifn
1480         ifn="$1"
1481
1482         # ifconfig_IF_ipx
1483         ifconfig_args=`_ifconfig_getargs $ifn ipx`
1484         if [ -n "${ifconfig_args}" ]; then
1485                 ${IFCONFIG_CMD} ${ifn} ${ifconfig_args}
1486                 return 0
1487         fi
1488
1489         return 1
1490 }
1491
1492 # ipx_down ifn
1493 #       Remove IPX addresses for interface $ifn. Returns 0 if IPX
1494 #       addresses were found and unconfigured. It returns 1, otherwise.
1495 #
1496 ipx_down()
1497 {
1498         local _if _ifs _ret ipxList oldifs _ipx
1499         _if=$1
1500         _ifs="^"
1501         _ret=1
1502         ipxList="`${IFCONFIG_CMD} ${_if} | grep 'ipx ' | tr "\n" "$_ifs"`"
1503         oldifs="$IFS"
1504
1505         IFS="$_ifs"
1506         for _ipx in $ipxList ; do
1507                 # get rid of extraneous line
1508                 [ -z "$_ipx" ] && break
1509
1510                 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
1511
1512                 IFS="$oldifs"
1513                 ${IFCONFIG_CMD} ${_if} ${_ipx} delete
1514                 IFS="$_ifs"
1515                 _ret=0
1516         done
1517         IFS="$oldifs"
1518
1519         return $_ret
1520 }
1521
1522 # ifnet_rename [ifname]
1523 #       Rename interfaces if ifconfig_IF_name is defined.
1524 #
1525 ifnet_rename()
1526 {
1527         local _if _ifname
1528
1529         # ifconfig_IF_name
1530         for _if in ${*:-$(${IFCONFIG_CMD} -l)}; do
1531                 _ifname=`get_if_var $_if ifconfig_IF_name`
1532                 if [ ! -z "$_ifname" ]; then
1533                         ${IFCONFIG_CMD} $_if name $_ifname
1534                 fi
1535         done
1536
1537         return 0
1538 }
1539
1540 # list_net_interfaces type
1541 #       List all network interfaces. The type of interface returned
1542 #       can be controlled by the type argument. The type
1543 #       argument can be any of the following:
1544 #               nodhcp  - all interfaces, excluding DHCP configured interfaces
1545 #               dhcp    - list only DHCP configured interfaces
1546 #               noautoconf      - all interfaces, excluding IPv6 Stateless
1547 #                                 Address Autoconf configured interfaces
1548 #               autoconf        - list only IPv6 Stateless Address Autoconf
1549 #                                 configured interfaces
1550 #       If no argument is specified all network interfaces are output.
1551 #       Note that the list will include cloned interfaces if applicable.
1552 #       Cloned interfaces must already exist to have a chance to appear
1553 #       in the list if ${network_interfaces} is set to `auto'.
1554 #
1555 list_net_interfaces()
1556 {
1557         local type _tmplist _list _autolist _lo _if
1558         type=$1
1559
1560         # Get a list of ALL the interfaces and make lo0 first if it's there.
1561         #
1562         _tmplist=
1563         case ${network_interfaces} in
1564         [Aa][Uu][Tt][Oo])
1565                 _autolist="`${IFCONFIG_CMD} -l`"
1566                 _lo=
1567                 for _if in ${_autolist} ; do
1568                         if autoif $_if; then
1569                                 if [ "$_if" = "lo0" ]; then
1570                                         _lo="lo0 "
1571                                 else
1572                                         _tmplist="${_tmplist} ${_if}"
1573                                 fi
1574                         fi
1575                 done
1576                 _tmplist="${_lo}${_tmplist# }"
1577                 ;;
1578         *)
1579                 _tmplist="${network_interfaces} ${cloned_interfaces}"
1580
1581                 # lo0 is effectively mandatory, so help prevent foot-shooting
1582                 #
1583                 case "$_tmplist" in
1584                 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
1585                 *)      _tmplist="lo0 ${_tmplist}" ;;
1586                 esac
1587                 ;;
1588         esac
1589
1590         _list=
1591         case "$type" in
1592         nodhcp)
1593                 for _if in ${_tmplist} ; do
1594                         if ! dhcpif $_if && \
1595                            [ -n "`_ifconfig_getargs $_if`" ]; then
1596                                 _list="${_list# } ${_if}"
1597                         fi
1598                 done
1599                 ;;
1600         dhcp)
1601                 for _if in ${_tmplist} ; do
1602                         if dhcpif $_if; then
1603                                 _list="${_list# } ${_if}"
1604                         fi
1605                 done
1606                 ;;
1607         noautoconf)
1608                 for _if in ${_tmplist} ; do
1609                         if ! ipv6_autoconfif $_if && \
1610                            [ -n "`_ifconfig_getargs $_if ipv6`" ]; then
1611                                 _list="${_list# } ${_if}"
1612                         fi
1613                 done
1614                 ;;
1615         autoconf)
1616                 for _if in ${_tmplist} ; do
1617                         if ipv6_autoconfif $_if; then
1618                                 _list="${_list# } ${_if}"
1619                         fi
1620                 done
1621                 ;;
1622         *)
1623                 _list=${_tmplist}
1624                 ;;
1625         esac
1626
1627         echo $_list
1628
1629         return 0
1630 }
1631
1632 # get_default_if -address_family
1633 #       Get the interface of the default route for the given address family.
1634 #       The -address_family argument must be suitable passing to route(8).
1635 #
1636 get_default_if()
1637 {
1638         local routeget oldifs defif line
1639         defif=
1640         oldifs="$IFS"
1641         IFS="
1642 "
1643         for line in `route -n get $1 default 2>/dev/null`; do
1644                 case $line in
1645                 *interface:*)
1646                         defif=${line##*: }
1647                         ;;
1648                 esac
1649         done
1650         IFS=${oldifs}
1651
1652         echo $defif
1653 }
1654
1655 # hexdigit arg
1656 #       Echo decimal number $arg (single digit) in hexadecimal format.
1657 hexdigit()
1658 {
1659         printf '%x\n' "$1"
1660 }
1661
1662 # hexprint arg
1663 #       Echo decimal number $arg (multiple digits) in hexadecimal format.
1664 hexprint()
1665 {
1666         printf '%x\n' "$1"
1667 }
1668
1669 is_wired_interface()
1670 {
1671         local media
1672
1673         case `${IFCONFIG_CMD} $1 2>/dev/null` in
1674         *media:?Ethernet*) media=Ethernet ;;
1675         esac
1676
1677         test "$media" = "Ethernet"
1678 }
1679
1680 # network6_getladdr if [flag]
1681 #       Echo link-local address from $if if any.
1682 #       If flag is defined, tentative ones will be excluded.
1683 network6_getladdr()
1684 {
1685         local _if _flag proto addr rest
1686         _if=$1
1687         _flag=$2
1688
1689         ${IFCONFIG_CMD} $_if 2>/dev/null | while read proto addr rest; do
1690                 case "${proto}/${addr}/${_flag}/${rest}" in
1691                 inet6/fe80::*//*)
1692                         echo ${addr}
1693                 ;;
1694                 inet6/fe80:://*tentative*)      # w/o flag
1695                         sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
1696                         network6_getladdr $_if $_flags
1697                 ;;
1698                 inet6/fe80::/*/*tentative*)     # w/ flag
1699                         echo ${addr}
1700                 ;;
1701                 *)
1702                         continue
1703                 ;;
1704                 esac
1705
1706                 return
1707         done
1708 }