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