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