]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - etc/network.subr
MFC:
[FreeBSD/stable/8.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}
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}
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 ${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_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         return ${cfg}
563 }
564
565 # Destroy child interfaces.
566 #
567 childif_destroy()
568 {
569         local cfg child child_wlans ifn
570
571         child_wlans="`get_if_var $ifn wlans_IF` `get_if_var $ifn vaps_IF`"
572         for child in ${child_wlans}; do
573                 ifconfig $child destroy && cfg=0
574         done
575 }
576
577 # Create netgraph nodes.
578 #
579 ng_mkpeer() {
580         ngctl -f - 2> /dev/null <<EOF
581 mkpeer $*
582 msg dummy nodeinfo
583 EOF
584 }
585
586 ng_create_one() {
587         ng_mkpeer $* | while read line; do
588                 t=`expr "${line}" : '.* name="\([a-z]*[0-9]*\)" .*'`
589                 if [ -n "${t}" ]; then
590                         echo ${t}
591                         return
592                 fi
593         done
594 }
595
596 gif_up() {
597         for i in ${gif_interfaces}; do
598                 peers=`get_if_var $i gifconfig_IF`
599                 case ${peers} in
600                 '')
601                         continue
602                         ;;
603                 *)
604                         if expr $i : 'gif[0-9][0-9]*$' >/dev/null 2>&1; then
605                                 ifconfig $i create >/dev/null 2>&1
606                         else
607                                 gif=`ifconfig gif create`
608                                 ifconfig $gif name $i
609                         fi
610                         ifconfig $i tunnel ${peers}
611                         ifconfig $i up
612                         ;;
613                 esac
614         done
615 }
616
617 # ng_fec_create ifn
618 # Configure Fast EtherChannel for interface $ifn. Returns 0 if FEC
619 # arguments were found and configured; returns !0 otherwise.
620 ng_fec_create() {
621          local req_iface iface bogus
622          req_iface="$1"
623
624          ngctl shutdown ${req_iface}: > /dev/null 2>&1
625
626          bogus=""
627          while true; do
628                  iface=`ng_create_one fec dummy fec`
629                  if [ -z "${iface}" ]; then
630                          exit 2
631                  fi
632                  if [ "${iface}" = "${req_iface}" ]; then
633                          break
634                  fi
635                  bogus="${bogus} ${iface}"
636          done
637
638          for iface in ${bogus}; do
639                  ngctl shutdown ${iface}:
640          done
641 }
642
643 fec_up() {
644         for i in ${fec_interfaces}; do
645                 ng_fec_create $i
646                 for j in `get_if_var $i fecconfig_IF`; do
647                         case ${j} in
648                         '')
649                                 continue
650                                 ;;
651                         *)
652                                 ngctl msg ${i}: add_iface "\"${j}\""
653                                 ;;
654                         esac
655                 done
656         done
657 }
658
659 #
660 # ipx_up ifn
661 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
662 # arguments were found and configured; returns 1 otherwise.
663 #
664 ipx_up()
665 {
666         ifn="$1"
667         ifconfig_args=`get_if_var $ifn ifconfig_IF_ipx`
668         if [ -n "${ifconfig_args}" ]; then
669                 ifconfig ${ifn} ${ifconfig_args}
670                 return 0
671         fi
672         return 1
673 }
674
675 # ipx_down ifn
676 #       Remove IPX addresses for interface $ifn. Returns 0 if IPX
677 #       addresses were found and unconfigured. It returns 1, otherwise.
678 #
679 ipx_down()
680 {
681         [ -z "$1" ] && return 1
682         _ifs="^"
683         _ret=1
684
685         ifexists $1 || return 1
686
687         ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
688
689         oldifs="$IFS"
690         IFS="$_ifs"
691         for _ipx in $ipxList ; do
692                 # get rid of extraneous line
693                 [ -z "$_ipx" ] && break
694
695                 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
696
697                 IFS="$oldifs"
698                 ifconfig $1 ${_ipx} delete
699                 IFS="$_ifs"
700                 _ret=0
701         done
702         IFS="$oldifs"
703
704         return $_ret
705 }
706
707 # ifnet_rename
708 #       Rename all requested interfaces.
709 #
710 ifnet_rename()
711 {
712
713         _ifn_list="`ifconfig -l`"
714         [ -z "$_ifn_list" ] && return 0
715         for _if in ${_ifn_list} ; do
716                 _ifname=`get_if_var $_if ifconfig_IF_name`
717                 if [ ! -z "$_ifname" ]; then
718                         ifconfig $_if name $_ifname
719                 fi
720         done
721         return 0
722 }
723
724 #
725 # list_net_interfaces type
726 #       List all network interfaces. The type of interface returned
727 #       can be controlled by the type argument. The type
728 #       argument can be any of the following:
729 #               nodhcp - all interfaces, excluding DHCP configured interfaces
730 #               dhcp   - list only DHCP configured interfaces
731 #       If no argument is specified all network interfaces are output.
732 #       Note that the list will include cloned interfaces if applicable.
733 #       Cloned interfaces must already exist to have a chance to appear
734 #       in the list if ${network_interfaces} is set to `auto'.
735 #
736 list_net_interfaces()
737 {
738         type=$1
739
740         # Get a list of ALL the interfaces and make lo0 first if it's there.
741         #
742         case ${network_interfaces} in
743         [Aa][Uu][Tt][Oo])
744                 _prefix=''
745                 _autolist="`ifconfig -l`"
746                 _lo=
747                 for _if in ${_autolist} ; do
748                         if autoif $_if; then
749                                 if [ "$_if" = "lo0" ]; then
750                                         _lo="lo0 "
751                                 else
752                                         _tmplist="${_tmplist}${_prefix}${_if}"
753                                         [ -z "$_prefix" ] && _prefix=' '
754                                 fi
755                         fi
756                 done
757                 _tmplist="${_lo}${_tmplist}"
758                 ;;
759         *)
760                 _tmplist="${network_interfaces} ${cloned_interfaces}"
761
762                 # lo0 is effectively mandatory, so help prevent foot-shooting
763                 #
764                 case "$_tmplist" in
765                 lo0|'lo0 '*|*' lo0'|*' lo0 '*) ;; # This is fine, do nothing
766                 *)      _tmplist="lo0 ${_tmplist}" ;;
767                 esac
768                 ;;
769         esac
770
771         if [ -z "$type" ]; then
772                 echo $_tmplist
773                 return 0
774         fi
775
776         # Separate out dhcp and non-dhcp interfaces
777         #
778         _aprefix=
779         _bprefix=
780         for _if in ${_tmplist} ; do
781                 if dhcpif $_if; then
782                         _dhcplist="${_dhcplist}${_aprefix}${_if}"
783                         [ -z "$_aprefix" ] && _aprefix=' '
784                 elif [ -n "`_ifconfig_getargs $_if`" ]; then
785                         _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
786                         [ -z "$_bprefix" ] && _bprefix=' '
787                 fi
788         done
789
790         case "$type" in
791         nodhcp)
792                 echo $_nodhcplist
793                 ;;
794         dhcp)
795                 echo $_dhcplist
796                 ;;
797         esac
798         return 0
799 }
800
801 # get_default_if -address_family
802 #       Get the interface of the default route for the given address family.
803 #       The -address_family argument must be suitable passing to route(8).
804 #
805 get_default_if()
806 {
807         routeget="`route -n get $1 default 2>/dev/null`"
808         oldifs="$IFS"
809         IFS="
810 "
811         defif=
812         for line in $routeget ; do
813                 case $line in
814                 *interface:*)
815                         defif=${line##*: }
816                         ;;
817                 esac
818         done
819         IFS=${oldifs}
820
821         echo $defif
822 }
823
824 hexdigit()
825 {
826         if [ $1 -lt 10 ]; then
827                 echo $1
828         else
829                 case $1 in
830                 10)     echo a ;;
831                 11)     echo b ;;
832                 12)     echo c ;;
833                 13)     echo d ;;
834                 14)     echo e ;;
835                 15)     echo f ;;
836                 esac
837         fi
838 }
839
840 hexprint()
841 {
842         val=$1
843         str=''
844
845         dig=`hexdigit $((${val} & 15))`
846         str=${dig}${str}
847         val=$((${val} >> 4))
848         while [ ${val} -gt 0 ]; do
849                 dig=`hexdigit $((${val} & 15))`
850                 str=${dig}${str}
851                 val=$((${val} >> 4))
852         done
853
854         echo ${str}
855 }
856
857 is_wired_interface()
858 {
859         local media
860
861         case `ifconfig $1 2>/dev/null` in
862         *media:?Ethernet*) media=Ethernet ;;
863         esac
864
865         test "$media" = "Ethernet"
866 }
867
868 # Setup the interfaces for IPv6
869 network6_interface_setup()
870 {
871         interfaces=$*
872         rtsol_interfaces=''
873         case ${ipv6_gateway_enable} in
874         [Yy][Ee][Ss])
875                 rtsol_available=no
876                 ;;
877         *)
878                 rtsol_available=yes
879                 ;;
880         esac
881         for i in $interfaces; do
882                 rtsol_interface=yes
883                 prefix=`get_if_var $i ipv6_prefix_IF`
884                 if [ -n "${prefix}" ]; then
885                         rtsol_available=no
886                         rtsol_interface=no
887                         laddr=`network6_getladdr $i`
888                         hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
889                         for j in ${prefix}; do
890                                 address=$j\:${hostid}
891                                 ifconfig $i inet6 ${address} prefixlen 64 alias
892
893                                 case ${ipv6_gateway_enable} in
894                                 [Yy][Ee][Ss])
895                                         # subnet-router anycast address
896                                         # (rfc2373)
897                                         ifconfig $i inet6 $j:: prefixlen 64 \
898                                                 alias anycast
899                                         ;;
900                                 esac
901                         done
902                 fi
903                 ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF`
904                 if [ -n "${ipv6_ifconfig}" ]; then
905                         rtsol_available=no
906                         rtsol_interface=no
907                         ifconfig $i inet6 ${ipv6_ifconfig} alias
908                 fi
909
910                 # Wireless NIC cards are virtualized through the wlan interface
911                 if ! is_wired_interface ${i}; then
912                         case "${i}" in
913                         wlan*)  rtsol_interface=yes ;;
914                         *)      rtsol_interface=no ;;
915                         esac
916                 fi
917
918                 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
919                 then
920                         case ${i} in
921                         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]*)
922                                 ;;
923                         *)
924                                 rtsol_interfaces="${rtsol_interfaces} ${i}"
925                                 ;;
926                         esac
927                 else
928                         ifconfig $i inet6
929                 fi
930         done
931
932         if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
933                 # Act as endhost - automatically configured.
934                 # You can configure only single interface, as
935                 # specification assumes that autoconfigured host has
936                 # single interface only.
937                 sysctl net.inet6.ip6.accept_rtadv=1
938                 set ${rtsol_interfaces}
939                 ifconfig $1 up
940                 rtsol ${rtsol_flags} $1
941         fi
942
943         for i in $interfaces; do
944                 alias=0
945                 while : ; do
946                         ipv6_ifconfig=`get_if_var $i ipv6_ifconfig_IF_alias${alias}`
947                         if [ -z "${ipv6_ifconfig}" ]; then
948                                 break;
949                         fi
950                         ifconfig $i inet6 ${ipv6_ifconfig} alias
951                         alias=$((${alias} + 1))
952                 done
953         done
954 }
955
956 # Setup IPv6 to IPv4 mapping
957 network6_stf_setup()
958 {
959         case ${stf_interface_ipv4addr} in
960         [Nn][Oo] | '')
961                 ;;
962         *)
963                 # assign IPv6 addr and interface route for 6to4 interface
964                 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
965                 OIFS="$IFS"
966                 IFS=".$IFS"
967                 set ${stf_interface_ipv4addr}
968                 IFS="$OIFS"
969                 hexfrag1=`hexprint $(($1*256 + $2))`
970                 hexfrag2=`hexprint $(($3*256 + $4))`
971                 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
972                 case ${stf_interface_ipv6_ifid} in
973                 [Aa][Uu][Tt][Oo] | '')
974                         for i in ${ipv6_network_interfaces}; do
975                                 laddr=`network6_getladdr ${i}`
976                                 case ${laddr} in
977                                 '')
978                                         ;;
979                                 *)
980                                         break
981                                         ;;
982                                 esac
983                         done
984                         stf_interface_ipv6_ifid=`expr "${laddr}" : \
985                                                       'fe80::\(.*\)%\(.*\)'`
986                         case ${stf_interface_ipv6_ifid} in
987                         '')
988                                 stf_interface_ipv6_ifid=0:0:0:1
989                                 ;;
990                         esac
991                         ;;
992                 esac
993                 ifconfig stf0 create >/dev/null 2>&1
994                 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
995                         prefixlen ${stf_prefixlen}
996                 # disallow packets to malicious 6to4 prefix
997                 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
998                 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
999                 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
1000                 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
1001                 ;;
1002         esac
1003 }
1004
1005 # Setup static routes
1006 network6_static_routes_setup()
1007 {
1008         # Set up any static routes.
1009         case ${ipv6_defaultrouter} in
1010         [Nn][Oo] | '')
1011                 ;;
1012         *)
1013                 ipv6_static_routes="default ${ipv6_static_routes}"
1014                 ipv6_route_default="default ${ipv6_defaultrouter}"
1015                 ;;
1016         esac
1017         case ${ipv6_static_routes} in
1018         [Nn][Oo] | '')
1019                 ;;
1020         *)
1021                 for i in ${ipv6_static_routes}; do
1022                         ipv6_route_args=`get_if_var $i ipv6_route_IF`
1023                         route add -inet6 ${ipv6_route_args}
1024                 done
1025                 ;;
1026         esac
1027 }
1028
1029 # Setup faith
1030 network6_faith_setup()
1031 {
1032         case ${ipv6_faith_prefix} in
1033         [Nn][Oo] | '')
1034                 ;;
1035         *)
1036                 sysctl net.inet6.ip6.keepfaith=1
1037                 ifconfig faith0 create >/dev/null 2>&1
1038                 ifconfig faith0 up
1039                 for prefix in ${ipv6_faith_prefix}; do
1040                         prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
1041                         case ${prefixlen} in
1042                         '')
1043                                 prefixlen=96
1044                                 ;;
1045                         *)
1046                                 prefix=`expr "${prefix}" : \
1047                                              "\(.*\)/${prefixlen}"`
1048                                 ;;
1049                         esac
1050                         route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
1051                         route change -inet6 ${prefix} -prefixlen ${prefixlen} \
1052                                 -ifp faith0
1053                 done
1054                 ;;
1055         esac
1056 }
1057
1058 # Install the "default interface" to kernel, which will be used
1059 # as the default route when there's no router.
1060 network6_default_interface_setup()
1061 {
1062         # Choose IPv6 default interface if it is not clearly specified.
1063         case ${ipv6_default_interface} in
1064         '')
1065                 for i in ${ipv6_network_interfaces}; do
1066                         case $i in
1067                         lo0|faith[0-9]*)
1068                                 continue
1069                                 ;;
1070                         esac
1071                         laddr=`network6_getladdr $i exclude_tentative`
1072                         case ${laddr} in
1073                         '')
1074                                 ;;
1075                         *)
1076                                 ipv6_default_interface=$i
1077                                 break
1078                                 ;;
1079                         esac
1080                 done
1081                 ;;
1082         esac
1083
1084         # Disallow unicast packets without outgoing scope identifiers,
1085         # or route such packets to a "default" interface, if it is specified.
1086         route add -inet6 fe80:: -prefixlen 10 ::1 -reject
1087         case ${ipv6_default_interface} in
1088         [Nn][Oo] | '')
1089                 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
1090                 ;;
1091         *)
1092                 laddr=`network6_getladdr ${ipv6_default_interface}`
1093                 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
1094                         -cloning
1095
1096                 # Disable installing the default interface with the
1097                 # case net.inet6.ip6.forwarding=0 and
1098                 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
1099                 # between the default router list and the manual
1100                 # configured default route.
1101                 case ${ipv6_gateway_enable} in
1102                 [Yy][Ee][Ss])
1103                         ;;
1104                 *)
1105                         if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
1106                         then
1107                                 ndp -I ${ipv6_default_interface}
1108                         fi
1109                         ;;
1110                 esac
1111                 ;;
1112         esac
1113 }
1114
1115 network6_getladdr()
1116 {
1117         ifconfig $1 2>/dev/null | while read proto addr rest; do
1118                 case ${proto} in
1119                 inet6)
1120                         case ${addr} in
1121                         fe80::*)
1122                                 if [ -z "$2" ]; then
1123                                         echo ${addr}
1124                                         return
1125                                 fi
1126                                 case ${rest} in
1127                                 *tentative*)
1128                                         continue
1129                                         ;;
1130                                 *)
1131                                         echo ${addr}
1132                                         return
1133                                 esac
1134                         esac
1135                 esac
1136         done
1137 }