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