]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/network.subr
rc support for vaps
[FreeBSD/FreeBSD.git] / etc / network.subr
1 #
2 # Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # 1. Redistributions of source code must retain the above copyright
8 #    notice, this list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright
10 #    notice, this list of conditions and the following disclaimer in the
11 #    documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 # SUCH DAMAGE.
24 #
25 # $FreeBSD$
26 #
27
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 applied
35 # 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         ipv4_up ${ifn} && cfg=0
48         ipx_up ${ifn} && cfg=0
49         childif_create ${ifn} && cfg=0
50
51         if [ "$cfg" -eq 0 ]; then
52                 ifconfig ${ifn}
53         fi
54
55         return $cfg
56 }
57
58 # ifn_start ifn
59 # Shutdown and de-configure an interface.  If action is taken print the
60 # interface name.
61 #
62 ifn_stop()
63 {
64         local ifn cfg
65         ifn="$1"
66         cfg=1
67
68         [ -z "$ifn" ] && return 1
69
70         ipx_down ${ifn} && cfg=0
71         ipv4_down ${ifn} && cfg=0
72         ifconfig_down ${ifn} && cfg=0
73         ifscript_down ${ifn} && cfg=0
74         childif_destroy ${ifn} && cfg=0
75
76         if [ "$cfg" -eq 0 ]; then
77                 echo -n " ${ifn}"
78         fi
79
80         return $cfg
81 }
82
83 # ifconfig_up if
84 #       Evaluate ifconfig(8) arguments for interface $if and
85 #       run ifconfig(8) with those arguments. It returns 0 if
86 #       arguments were found and executed or 1 if the interface
87 #       had no arguments.  Pseudo arguments DHCP and WPA are handled
88 #       here.
89 #
90 ifconfig_up()
91 {
92         _cfg=1
93
94         ifconfig_args=`ifconfig_getargs $1`
95         if [ -n "${ifconfig_args}" ]; then
96                 ifconfig $1 ${ifconfig_args}
97                 ifconfig $1 up
98                 _cfg=0
99         fi
100
101         if wpaif $1; then
102                 /etc/rc.d/wpa_supplicant start $1
103                 _cfg=0          # XXX: not sure this should count
104         fi
105
106         if dhcpif $1; then
107                 if [ $_cfg -ne 0 ] ; then
108                         ifconfig $1 up
109                 fi
110                 if syncdhcpif $1; then
111                         /etc/rc.d/dhclient start $1
112                 fi
113                 _cfg=0
114         fi
115
116         return $_cfg
117 }
118
119 # ifconfig_down if
120 #       returns 1 if wpa_supplicant or dhclient was stopped or
121 #       the interface exists.
122 #
123 ifconfig_down()
124 {
125         [ -z "$1" ] && return 1
126         _cfg=1
127
128         if wpaif $1; then
129                 /etc/rc.d/wpa_supplicant stop $1
130                 _cfg=0
131         fi
132
133         if dhcpif $1; then
134                 /etc/rc.d/dhclient stop $1
135                 _cfg=0
136         fi
137
138         if ifexists $1; then
139                 ifconfig $1 down
140                 _cfg=0
141         fi
142
143         return $_cfg
144 }
145
146 # get_if_var if var [default]
147 #       Return the value of the pseudo-hash corresponding to $if where
148 #       $var is a string containg the sub-string "IF" which will be
149 #       replaced with $if after the characters defined in _punct are
150 #       replaced with '_'. If the variable is unset, replace it with
151 #       $default if given.
152 get_if_var()
153 {
154         if [ $# -ne 2 -a $# -ne 3 ]; then
155                 err 3 'USAGE: get_if_var name var [default]'
156         fi
157
158         _if=$1
159         _punct=". - / +"
160         for _punct_c in $_punct; do
161                 _if=`ltr ${_if} ${_punct_c} '_'`
162         done
163         _var=$2
164         _default=$3
165
166         prefix=${_var%%IF*}
167         suffix=${_var##*IF}
168         eval echo \${${prefix}${_if}${suffix}-${_default}}
169 }
170
171 # _ifconfig_getargs if
172 #       Echos the arguments for the supplied interface to stdout.
173 #       returns 1 if empty.  In general, ifconfig_getargs should be used
174 #       outside this file.
175 _ifconfig_getargs()
176 {
177         _ifn=$1
178         if [ -z "$_ifn" ]; then
179                 return 1
180         fi
181
182         get_if_var $_ifn ifconfig_IF "$ifconfig_DEFAULT"
183 }
184
185 # ifconfig_getargs if
186 #       Takes the result from _ifconfig_getargs and removes pseudo
187 #       args such as DHCP and WPA.
188 ifconfig_getargs()
189 {
190         _tmpargs=`_ifconfig_getargs $1`
191         if [ $? -eq 1 ]; then
192                 return 1
193         fi
194         _args=
195
196         for _arg in $_tmpargs; do
197                 case $_arg in
198                 [Dd][Hh][Cc][Pp]) ;;
199                 [Nn][Oo][Aa][Uu][Tt][Oo]) ;;
200                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
201                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp]) ;;
202                 [Ww][Pp][Aa]) ;;
203                 *)
204                         _args="$_args $_arg"
205                         ;;
206                 esac
207         done
208
209         echo $_args
210 }
211
212 # autoif
213 #       Returns 0 if the interface should be automaticly configured at
214 #       boot time and 1 otherwise.
215 autoif()
216 {
217         _tmpargs=`_ifconfig_getargs $1`
218         for _arg in $_tmpargs; do
219                 case $_arg in
220                 [Nn][Oo][Aa][Uu][Tt][Oo])
221                         return 1
222                         ;;
223                 esac
224         done
225         return 0
226 }
227
228 # dhcpif if
229 #       Returns 0 if the interface is a DHCP interface and 1 otherwise.
230 dhcpif()
231 {
232         _tmpargs=`_ifconfig_getargs $1`
233         for _arg in $_tmpargs; do
234                 case $_arg in
235                 [Dd][Hh][Cc][Pp])
236                         return 0
237                         ;;
238                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
239                         return 0
240                         ;;
241                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
242                         return 0
243                         ;;
244                 esac
245         done
246         return 1
247 }
248
249 # syncdhcpif
250 #       Returns 0 if the interface should be configured synchronously and
251 #       1 otherwise.
252 syncdhcpif()
253 {
254         _tmpargs=`_ifconfig_getargs $1`
255         for _arg in $_tmpargs; do
256                 case $_arg in
257                 [Nn][Oo][Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
258                         return 1
259                         ;;
260                 [Ss][Yy][Nn][Cc][Dd][Hh][Cc][Pp])
261                         return 0
262                         ;;
263                 esac
264         done
265         if checkyesno synchronous_dhclient; then
266                 return 0
267         else
268                 return 1
269         fi
270 }
271
272 # wpaif if
273 #       Returns 0 if the interface is a WPA interface and 1 otherwise.
274 wpaif()
275 {
276         _tmpargs=`_ifconfig_getargs $1`
277         for _arg in $_tmpargs; do
278                 case $_arg in
279                 [Ww][Pp][Aa])
280                         return 0
281                         ;;
282                 esac
283         done
284         return 1
285 }
286
287 # ipv6if if
288 #       Returns 0 if the interface should be configured for IPv6 and
289 #       1 otherwise.
290 ipv6if()
291 {
292         if ! checkyesno ipv6_enable; then
293                 return 1
294         fi
295         case "${ipv6_network_interfaces}" in
296         [Aa][Uu][Tt][Oo])
297                 return 0
298                 ;;
299         ''|[Nn][Oo][Nn][Ee])
300                 return 1
301                 ;;
302         esac
303         for v6if in ${ipv6_network_interfaces}; do
304                 if [ "${v6if}" = "${1}" ]; then
305                         return 0
306                 fi
307         done
308         return 1
309 }
310
311 # ifexists if
312 #       Returns 0 if the interface exists and 1 otherwise.
313 ifexists()
314 {
315         ifconfig -n $1 > /dev/null 2>&1
316 }
317
318 # ipv4_up if
319 #  add IPv4 addresses to the interface $if 
320 ipv4_up()
321 {
322         _if=$1
323         ifalias_up ${_if}
324         ipv4_addrs_common ${_if} alias
325 }
326
327 # ipv4_down if
328 #  remove IPv4 addresses from the interface $if
329 ipv4_down()
330 {
331         _if=$1
332         _ifs="^"
333         _ret=1
334
335         ifexists ${_if} || return 1
336
337         inetList="`ifconfig ${_if} | grep 'inet ' | tr "\n" "$_ifs"`"
338
339         oldifs="$IFS"
340         IFS="$_ifs"
341         for _inet in $inetList ; do
342                 # get rid of extraneous line
343                 [ -z "$_inet" ] && break
344
345                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
346
347                 IFS="$oldifs"
348                 ifconfig ${_if} ${_inet} delete
349                 IFS="$_ifs"
350                 _ret=0
351         done
352         IFS="$oldifs"
353
354         ifalias_down ${_if} && _ret=0
355         ipv4_addrs_common ${_if} -alias && _ret=0
356
357         return $_ret
358 }
359
360 # ipv4_addrs_common if action
361 #   Evaluate the ifconfig_if_ipv4 arguments for interface $if
362 #   and use $action to add or remove IPv4 addresses from $if.
363 ipv4_addrs_common()
364 {  
365         _ret=1
366         _if=$1
367         _action=$2
368     
369         # get ipv4-addresses
370         cidr_addr=`get_if_var $_if ipv4_addrs_IF`
371     
372         for _cidr in ${cidr_addr}; do
373                 _ipaddr=${_cidr%%/*}
374                 _netmask="/"${_cidr##*/}
375                 _range=${_ipaddr##*.}
376                 _ipnet=${_ipaddr%.*}
377                 _iplow=${_range%-*}
378                 _iphigh=${_range#*-}
379
380                 # clear netmask when removing aliases
381                 if [ "${_action}" = "-alias" ]; then
382                         _netmask=""
383                 fi
384         
385                 _ipcount=${_iplow}
386                 while [ "${_ipcount}" -le "${_iphigh}" ]; do
387                         eval "ifconfig ${_if} ${_action} ${_ipnet}.${_ipcount}${_netmask}"
388                         _ipcount=$((${_ipcount}+1))
389                         _ret=0
390
391                         # only the first ipaddr in a subnet need the real netmask
392                         if [ "${_action}" != "-alias" ]; then
393                                 _netmask="/32"
394                         fi
395                 done
396         done
397         return $_ret
398 }
399
400 # ifalias_up if
401 #       Configure aliases for network interface $if.
402 #       It returns 0 if at least one alias was configured or
403 #       1 if there were none.
404 #
405 ifalias_up()
406 {
407         _ret=1
408         alias=0
409         while : ; do
410                 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
411                 if [ -n "${ifconfig_args}" ]; then
412                         ifconfig $1 ${ifconfig_args} alias
413                         alias=$((${alias} + 1))
414                         _ret=0
415                 else
416                         break
417                 fi
418         done
419         return $_ret
420 }
421
422 #ifalias_down if
423 #       Remove aliases for network interface $if.
424 #       It returns 0 if at least one alias was removed or
425 #       1 if there were none.
426 #
427 ifalias_down()
428 {
429         _ret=1
430         alias=0
431         while : ; do
432                 ifconfig_args=`get_if_var $1 ifconfig_IF_alias${alias}`
433                 if [ -n "${ifconfig_args}" ]; then
434                         ifconfig $1 ${ifconfig_args} -alias
435                         alias=$((${alias} + 1))
436                         _ret=0
437                 else
438                         break
439                 fi
440         done
441         return $_ret
442 }
443
444 # ifscript_up if
445 #       Evaluate a startup script for the $if interface.
446 #       It returns 0 if a script was found and processed or
447 #       1 if no script was found.
448 #
449 ifscript_up()
450 {
451         if [ -r /etc/start_if.$1 ]; then
452                 . /etc/start_if.$1
453                 return 0
454         fi
455         return 1
456 }
457
458 # ifscript_down if
459 #       Evaluate a shutdown script for the $if interface.
460 #       It returns 0 if a script was found and processed or
461 #       1 if no script was found.
462 #
463 ifscript_down()
464 {
465         if [ -r /etc/stop_if.$1 ]; then
466                 . /etc/stop_if.$1
467                 return 0
468         fi
469         return 1
470 }
471
472 # Create cloneable interfaces.
473 #
474 clone_up()
475 {
476         _prefix=
477         _list=
478         for ifn in ${cloned_interfaces}; do
479                 ifconfig ${ifn} create
480                 if [ $? -eq 0 ]; then
481                         _list="${_list}${_prefix}${ifn}"
482                         [ -z "$_prefix" ] && _prefix=' '
483                 fi
484         done
485         debug "Cloned: ${_list}"
486 }
487
488 # Destroy cloned interfaces. Destroyed interfaces are echoed
489 # to standard output.
490 #
491 clone_down()
492 {
493         _prefix=
494         _list=
495         for ifn in ${cloned_interfaces}; do
496                 ifconfig ${ifn} destroy
497                 if [ $? -eq 0 ]; then
498                         _list="${_list}${_prefix}${ifn}"
499                         [ -z "$_prefix" ] && _prefix=' '
500                 fi
501         done
502         debug "Destroyed clones: ${_list}"
503 }
504
505 # Create and configure child interfaces.
506 # Return 0 if child interfaces are created.
507 #
508 childif_create()
509 {
510         local cfg child child_vaps create_args ifn i
511         cfg=1
512
513         ifn=$1
514
515         # Create VAPs
516         child_vaps=`get_if_var $ifn vaps_IF`
517         for child in ${child_vaps}; do
518                 create_args="wlandev $ifn `get_if_var $child vap_create_IF`"
519                 if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
520                         ifconfig $child create ${create_args} && cfg=0
521                 else
522                         i=`ifconfig wlan create ${create_args}`
523                         ifconfig $i name $child && cfg=0
524                 fi
525                 ifn_start $child
526         done
527
528         return 
529 }
530
531 # Destroy child interfaces.
532 #
533 childif_destroy()
534 {
535         local cfg child child_vaps ifn
536
537         child_vaps=`get_if_var $ifn vaps_IF`
538         for child in ${child_vaps}; do
539                 ifconfig $child destroy && cfg=0
540         done
541 }
542
543 # Create netgraph nodes.
544 #
545 ng_mkpeer() {
546         ngctl -f - 2> /dev/null <<EOF
547 mkpeer $*
548 msg dummy nodeinfo
549 EOF
550 }
551
552 ng_create_one() {
553         ng_mkpeer $* | while read line; do
554                 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
555                 if [ -n "${t}" ]; then
556                         echo ${t}
557                         return
558                 fi
559         done
560 }
561
562 gif_up() {
563         for i in ${gif_interfaces}; do
564                 peers=`get_if_var $i gifconfig_IF`
565                 case ${peers} in
566                 '')
567                         continue
568                         ;;
569                 *)
570                         if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
571                                 ifconfig $i create >/dev/null 2>&1
572                         else
573                                 gif=`ifconfig gif create`
574                                 ifconfig $gif name $i
575                         fi
576                         ifconfig $i tunnel ${peers}
577                         ifconfig $i up
578                         ;;
579                 esac
580         done
581 }
582
583 # ng_fec_create ifn
584 # Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
585 # arguments were found and configured; returns !0 otherwise.
586 ng_fec_create() {
587          local req_iface iface bogus
588          req_iface="$1"
589
590          ngctl shutdown ${req_iface}: > /dev/null 2>&1
591
592          bogus=""
593          while true; do
594                  iface=`ng_create_one fec dummy fec`
595                  if [ -z "${iface}" ]; then
596                          exit 2
597                  fi
598                  if [ "${iface}" = "${req_iface}" ]; then
599                          break
600                  fi
601                  bogus="${bogus} ${iface}"
602          done
603
604          for iface in ${bogus}; do
605                  ngctl shutdown ${iface}:
606          done
607 }
608
609 fec_up() {
610         for i in ${fec_interfaces}; do
611                 ng_fec_create $i
612                 for j in `get_if_var $i fecconfig_IF`; do
613                         case ${j} in
614                         '')
615                                 continue
616                                 ;;
617                         *)
618                                 ngctl msg ${i}: add_iface "\"${j}\""
619                                 ;;
620                         esac
621                 done
622         done
623 }
624
625 #
626 # ipx_up ifn
627 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
628 # arguments were found and configured; returns 1 otherwise.
629 #
630 ipx_up()
631 {
632         ifn="$1"
633         ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
634         if [ -n "${ifconfig_args}" ]; then
635                 ifconfig ${ifn} ${ifconfig_args}
636                 return 0
637         fi
638         return 1
639 }
640
641 # ipx_down ifn
642 #       Remove IPX addresses for interface $ifn. Returns 0 if IPX
643 #       addresses were found and unconfigured. It returns 1, otherwise.
644 #
645 ipx_down()
646 {
647         [ -z "$1" ] && return 1
648         _ifs="^"
649         _ret=1
650
651         ifexists $1 || return 1
652
653         ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
654
655         oldifs="$IFS"
656         IFS="$_ifs"
657         for _ipx in $ipxList ; do
658                 # get rid of extraneous line
659                 [ -z "$_ipx" ] && break
660
661                 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
662
663                 IFS="$oldifs"
664                 ifconfig $1 ${_ipx} delete
665                 IFS="$_ifs"
666                 _ret=0
667         done
668         IFS="$oldifs"
669
670         return $_ret
671 }
672
673 # ifnet_rename
674 #       Rename all requested interfaces.
675 #
676 ifnet_rename()
677 {
678
679         _ifn_list="`ifconfig -l`"
680         [ -z "$_ifn_list" ] && return 0
681         for _if in ${_ifn_list} ; do
682                 _ifname=`get_if_var $_if ifconfig_IF_name`
683                 if [ ! -z "$_ifname" ]; then
684                         ifconfig $_if name $_ifname
685                 fi
686         done
687         return 0
688 }
689
690 #
691 # list_net_interfaces type
692 #       List all network interfaces. The type of interface returned
693 #       can be controlled by the type argument. The type
694 #       argument can be any of the following:
695 #               nodhcp - all interfaces, excluding DHCP configured interfaces
696 #               dhcp   - list only DHCP configured interfaces
697 #       If no argument is specified all network interfaces are output.
698 #       Note that the list will include cloned interfaces if applicable.
699 #       Cloned interfaces must already exist to have a chance to appear
700 #       in the list if ${network_interfaces} is set to `auto'.
701 #
702 list_net_interfaces()
703 {
704         type=$1
705
706         # Get a list of ALL the interfaces and make lo0 first if it's there.
707         #
708         case ${network_interfaces} in
709         [Aa][Uu][Tt][Oo])
710                 _prefix=''
711                 _autolist="`ifconfig -l`"
712                 _lo=
713                 for _if in ${_autolist} ; do
714                         if autoif $_if; then
715                                 if [ "$_if" = "lo0" ]; then
716                                         _lo="lo0 "
717                                 else
718                                         _tmplist="${_tmplist}${_prefix}${_if}"
719                                         [ -z "$_prefix" ] && _prefix=' '
720                                 fi
721                         fi
722                 done
723                 _tmplist="${_lo}${_tmplist}"
724                 ;;
725         *)
726                 _tmplist="${network_interfaces} ${cloned_interfaces}"
727                 ;;
728         esac
729
730         if [ -z "$type" ]; then
731                 echo $_tmplist
732                 return 0
733         fi
734
735         # Separate out dhcp and non-dhcp interfaces
736         #
737         _aprefix=
738         _bprefix=
739         for _if in ${_tmplist} ; do
740                 if dhcpif $_if; then
741                         _dhcplist="${_dhcplist}${_aprefix}${_if}"
742                         [ -z "$_aprefix" ] && _aprefix=' '
743                 elif [ -n "`_ifconfig_getargs $_if`" ]; then
744                         _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
745                         [ -z "$_bprefix" ] && _bprefix=' '
746                 fi
747         done
748
749         case "$type" in
750         nodhcp)
751                 echo $_nodhcplist
752                 ;;
753         dhcp)
754                 echo $_dhcplist
755                 ;;
756         esac
757         return 0
758 }
759
760 hexdigit()
761 {
762         if [ $1 -lt 10 ]; then
763                 echo $1
764         else
765                 case $1 in
766                 10)     echo a ;;
767                 11)     echo b ;;
768                 12)     echo c ;;
769                 13)     echo d ;;
770                 14)     echo e ;;
771                 15)     echo f ;;
772                 esac
773         fi
774 }
775
776 hexprint()
777 {
778         val=$1
779         str=''
780
781         dig=`hexdigit $((${val} & 15))`
782         str=${dig}${str}
783         val=$((${val} >> 4))
784         while [ ${val} -gt 0 ]; do
785                 dig=`hexdigit $((${val} & 15))`
786                 str=${dig}${str}
787                 val=$((${val} >> 4))
788         done
789
790         echo ${str}
791 }
792
793 # Setup the interfaces for IPv6
794 network6_interface_setup()
795 {
796         interfaces=$*
797         rtsol_interfaces=''
798         case ${ipv6_gateway_enable} in
799         [Yy][Ee][Ss])
800                 rtsol_available=no
801                 ;;
802         *)
803                 rtsol_available=yes
804                 ;;
805         esac
806         for i in $interfaces; do
807                 rtsol_interface=yes
808                 prefix=`get_if_var $i ipv6_prefix_IF`
809                 if [ -n "${prefix}" ]; then
810                         rtsol_available=no
811                         rtsol_interface=no
812                         laddr=`network6_getladdr $i`
813                         hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
814                         for j in ${prefix}; do
815                                 address=$j\:${hostid}
816                                 ifconfig $i inet6 ${address} prefixlen 64 alias
817
818                                 case ${ipv6_gateway_enable} in
819                                 [Yy][Ee][Ss])
820                                         # subnet-router anycast address
821                                         # (rfc2373)
822                                         ifconfig $i inet6 $j:: prefixlen 64 \
823                                                 alias anycast
824                                         ;;
825                                 esac
826                         done
827                 fi
828                 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
829                 if [ -n "${ipv6_ifconfig}" ]; then
830                         rtsol_available=no
831                         rtsol_interface=no
832                         ifconfig $i inet6 ${ipv6_ifconfig} alias
833                 fi
834
835                 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
836                 then
837                         case ${i} in
838                         lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*|pflog[0-9]*|pfsync[0-9]*)
839                                 ;;
840                         *)
841                                 rtsol_interfaces="${rtsol_interfaces} ${i}"
842                                 ;;
843                         esac
844                 else
845                         ifconfig $i inet6
846                 fi
847         done
848
849         if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
850                 # Act as endhost - automatically configured.
851                 # You can configure only single interface, as
852                 # specification assumes that autoconfigured host has
853                 # single interface only.
854                 sysctl net.inet6.ip6.accept_rtadv=1
855                 set ${rtsol_interfaces}
856                 ifconfig $1 up
857                 rtsol ${rtsol_flags} $1
858         fi
859
860         for i in $interfaces; do
861                 alias=0
862                 while : ; do
863                         ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
864                         if [ -z "${ipv6_ifconfig}" ]; then
865                                 break;
866                         fi
867                         ifconfig $i inet6 ${ipv6_ifconfig} alias
868                         alias=$((${alias} + 1))
869                 done
870         done
871 }
872
873 # Setup IPv6 to IPv4 mapping
874 network6_stf_setup()
875 {
876         case ${stf_interface_ipv4addr} in
877         [Nn][Oo] | '')
878                 ;;
879         *)
880                 # assign IPv6 addr and interface route for 6to4 interface
881                 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
882                 OIFS="$IFS"
883                 IFS=".$IFS"
884                 set ${stf_interface_ipv4addr}
885                 IFS="$OIFS"
886                 hexfrag1=`hexprint $(($1*256 + $2))`
887                 hexfrag2=`hexprint $(($3*256 + $4))`
888                 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
889                 case ${stf_interface_ipv6_ifid} in
890                 [Aa][Uu][Tt][Oo] | '')
891                         for i in ${ipv6_network_interfaces}; do
892                                 laddr=`network6_getladdr ${i}`
893                                 case ${laddr} in
894                                 '')
895                                         ;;
896                                 *)
897                                         break
898                                         ;;
899                                 esac
900                         done
901                         stf_interface_ipv6_ifid=`expr "${laddr}" : \
902                                                       'fe80::\(.*\)%\(.*\)'`
903                         case ${stf_interface_ipv6_ifid} in
904                         '')
905                                 stf_interface_ipv6_ifid=0:0:0:1
906                                 ;;
907                         esac
908                         ;;
909                 esac
910                 ifconfig stf0 create >/dev/null 2>&1
911                 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
912                         prefixlen ${stf_prefixlen}
913                 # disallow packets to malicious 6to4 prefix
914                 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
915                 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
916                 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
917                 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
918                 ;;
919         esac
920 }
921
922 # Setup static routes
923 network6_static_routes_setup()
924 {
925         # Set up any static routes.
926         case ${ipv6_defaultrouter} in
927         [Nn][Oo] | '')
928                 ;;
929         *)
930                 ipv6_static_routes="default ${ipv6_static_routes}"
931                 ipv6_route_default="default ${ipv6_defaultrouter}"
932                 ;;
933         esac
934         case ${ipv6_static_routes} in
935         [Nn][Oo] | '')
936                 ;;
937         *)
938                 for i in ${ipv6_static_routes}; do
939                         ipv6_route_args=`get_if_var $i ipv6_route_IF`
940                         route add -inet6 ${ipv6_route_args}
941                 done
942                 ;;
943         esac
944 }
945
946 # Setup faith
947 network6_faith_setup()
948 {
949         case ${ipv6_faith_prefix} in
950         [Nn][Oo] | '')
951                 ;;
952         *)
953                 sysctl net.inet6.ip6.keepfaith=1
954                 ifconfig faith0 create >/dev/null 2>&1
955                 ifconfig faith0 up
956                 for prefix in ${ipv6_faith_prefix}; do
957                         prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
958                         case ${prefixlen} in
959                         '')
960                                 prefixlen=96
961                                 ;;
962                         *)
963                                 prefix=`expr "${prefix}" : \
964                                              "\(.*\)/${prefixlen}"`
965                                 ;;
966                         esac
967                         route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
968                         route change -inet6 ${prefix} -prefixlen ${prefixlen} \
969                                 -ifp faith0
970                 done
971                 ;;
972         esac
973 }
974
975 # Install the "default interface" to kernel, which will be used
976 # as the default route when there's no router.
977 network6_default_interface_setup()
978 {
979         # Choose IPv6 default interface if it is not clearly specified.
980         case ${ipv6_default_interface} in
981         '')
982                 for i in ${ipv6_network_interfaces}; do
983                         case $i in
984                         lo0|faith[0-9]*)
985                                 continue
986                                 ;;
987                         esac
988                         laddr=`network6_getladdr $i exclude_tentative`
989                         case ${laddr} in
990                         '')
991                                 ;;
992                         *)
993                                 ipv6_default_interface=$i
994                                 break
995                                 ;;
996                         esac
997                 done
998                 ;;
999         esac
1000
1001         # Disallow unicast packets without outgoing scope identifiers,
1002         # or route such packets to a "default" interface, if it is specified.
1003         route add -inet6 fe80:: -prefixlen 10 ::1 -reject
1004         case ${ipv6_default_interface} in
1005         [Nn][Oo] | '')
1006                 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
1007                 ;;
1008         *)
1009                 laddr=`network6_getladdr ${ipv6_default_interface}`
1010                 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
1011                         -cloning
1012
1013                 # Disable installing the default interface with the
1014                 # case net.inet6.ip6.forwarding=0 and
1015                 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
1016                 # between the default router list and the manual
1017                 # configured default route.
1018                 case ${ipv6_gateway_enable} in
1019                 [Yy][Ee][Ss])
1020                         ;;
1021                 *)
1022                         if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
1023                         then
1024                                 ndp -I ${ipv6_default_interface}
1025                         fi
1026                         ;;
1027                 esac
1028                 ;;
1029         esac
1030 }
1031
1032 network6_getladdr()
1033 {
1034         ifconfig $1 2>/dev/null | while read proto addr rest; do
1035                 case ${proto} in
1036                 inet6)
1037                         case ${addr} in
1038                         fe80::*)
1039                                 if [ -z "$2" ]; then
1040                                         echo ${addr}
1041                                         return
1042                                 fi
1043                                 case ${rest} in
1044                                 *tentative*)
1045                                         continue
1046                                         ;;
1047                                 *)
1048                                         echo ${addr}
1049                                         return
1050                                 esac
1051                         esac
1052                 esac
1053         done
1054 }