]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/network.subr
This commit was generated by cvs2svn to compensate for changes in r147001,
[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 # ifconfig_up if
34 #       Evaluate ifconfig(8) arguments for interface $if and
35 #       run ifconfig(8) with those arguments. It returns 0 if
36 #       arguments were found and executed or 1 if the interface
37 #       had no arguments.
38 #
39 ifconfig_up()
40 {
41         eval ifconfig_args=\$ifconfig_$1
42         if [ -n "${ifconfig_args}" ]; then
43                 ifconfig $1 ${ifconfig_args}
44                 return 0
45         fi
46         return 1
47 }
48
49 # ifconfig_down if
50 #       Remove all inet entries from the $if interface. It returns
51 #       0 if inet entries were found and removed. It returns 1 if
52 #       no entries were found or they could not be removed.
53 #
54 ifconfig_down()
55 {
56         [ -z "$1" ] && return 1
57         _ifs="^"
58         _ret=1
59
60         inetList="`ifconfig $1 | grep 'inet ' | tr "\n" "$_ifs"`"
61
62         oldifs="$IFS"
63         IFS="$_ifs"
64         for _inet in $inetList ; do
65                 # get rid of extraneous line
66                 [ -z "$_inet" ] && break
67
68                 _inet=`expr "$_inet" : '.*\(inet \([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*'`
69
70                 IFS="$oldifs"
71                 ifconfig $1 ${_inet} delete
72                 IFS="$_ifs"
73                 _ret=0
74         done
75         IFS="$oldifs"
76
77         return $_ret
78 }
79
80 # ifalias_up if
81 #       Configure aliases for network interface $if.
82 #       It returns 0 if at least one alias was configured or
83 #       1 if there were none.
84 #
85 ifalias_up()
86 {
87         _ret=1
88         alias=0
89         while : ; do
90                 eval ifconfig_args=\$ifconfig_$1_alias${alias}
91                 if [ -n "${ifconfig_args}" ]; then
92                         ifconfig $1 ${ifconfig_args} alias
93                         alias=$((${alias} + 1))
94                         _ret=0
95                 else
96                         break
97                 fi
98         done
99         return $_ret
100 }
101
102 #ifalias_down if
103 #       Remove aliases for network interface $if.
104 #       It returns 0 if at least one alias was removed or
105 #       1 if there were none.
106 #
107 ifalias_down()
108 {
109         _ret=1
110         alias=0
111         while : ; do
112                 eval ifconfig_args=\$ifconfig_$1_alias${alias}
113                 if [ -n "${ifconfig_args}" ]; then
114                         ifconfig $1 ${ifconfig_args} -alias
115                         alias=$((${alias} + 1))
116                         _ret=0
117                 else
118                         break
119                 fi
120         done
121         return $_ret
122 }
123
124 # ifscript_up if
125 #       Evaluate a startup script for the $if interface.
126 #       It returns 0 if a script was found and processed or
127 #       1 if no script was found.
128 #
129 ifscript_up()
130 {
131         if [ -r /etc/start_if.$1 ]; then
132                 . /etc/start_if.$1
133                 return 0
134         fi
135         return 1
136 }
137
138 # ifscript_down if
139 #       Evaluate a shutdown script for the $if interface.
140 #       It returns 0 if a script was found and processed or
141 #       1 if no script was found.
142 #
143 ifscript_down()
144 {
145         if [ -r /etc/stop_if.$1 ]; then
146                 . /etc/stop_if.$1
147                 return 0
148         fi
149         return 1
150 }
151
152 # Create cloneable interfaces.
153 #
154 clone_up()
155 {
156         _prefix=
157         _list=
158         for ifn in ${cloned_interfaces}; do
159                 ifconfig ${ifn} create
160                 if [ $? -eq 0 ]; then
161                         _list="${_list}${_prefix}${ifn}"
162                         [ -z "$_prefix" ] && _prefix=' '
163                 fi
164         done
165         debug "Cloned: ${_list}"
166 }
167
168 # Destroy cloned interfaces. Destroyed interfaces are echoed
169 # to standard output.
170 #
171 clone_down()
172 {
173         _prefix=
174         _list=
175         for ifn in ${cloned_interfaces}; do
176                 ifconfig ${ifn} destroy
177                 if [ $? -eq 0 ]; then
178                         _list="${_list}${_prefix}${ifn}"
179                         [ -z "$_prefix" ] && _prefix=' '
180                 fi
181         done
182         debug "Destroyed clones: ${_list}"
183 }
184
185 gif_up() {
186         case ${gif_interfaces} in
187         [Nn][Oo] | '')
188                 ;;
189         *)
190                 for i in ${gif_interfaces}; do
191                         eval peers=\$gifconfig_$i
192                         case ${peers} in
193                         '')
194                                 continue
195                                 ;;
196                         *)
197                                 ifconfig $i create >/dev/null 2>&1
198                                 ifconfig $i tunnel ${peers}
199                                 ifconfig $i up
200                                 ;;
201                         esac
202                 done
203                 ;;
204         esac
205 }
206
207 #
208 # ipx_up ifn
209 # Configure any IPX addresses for interface $ifn. Returns 0 if IPX
210 # arguments were found and configured; returns 1 otherwise.
211 #
212 ipx_up()
213 {
214         ifn="$1"
215         eval ifconfig_args=\$ifconfig_${ifn}_ipx
216         if [ -n "${ifconfig_args}" ]; then
217                 ifconfig ${ifn} ${ifconfig_args}
218                 return 0
219         fi
220         return 1
221 }
222
223 # ipx_down ifn
224 #       Remove IPX addresses for interface $ifn. Returns 0 if IPX
225 #       addresses were found and unconfigured. It returns 1, otherwise.
226 #
227 ipx_down()
228 {
229         [ -z "$1" ] && return 1
230         _ifs="^"
231         _ret=1
232
233         ipxList="`ifconfig $1 | grep 'ipx ' | tr "\n" "$_ifs"`"
234
235         oldifs="$IFS"
236         IFS="$_ifs"
237         for _ipx in $ipxList ; do
238                 # get rid of extraneous line
239                 [ -z "$_ipx" ] && break
240
241                 _ipx=`expr "$_ipx" : '.*\(ipx [0-9a-h]\{1,8\}H*\.[0-9a-h]\{1,12\}\).*'`
242
243                 IFS="$oldifs"
244                 ifconfig $1 ${_ipx} delete
245                 IFS="$_ifs"
246                 _ret=0
247         done
248         IFS="$oldifs"
249
250         return $_ret
251 }
252
253 # ifnet_rename
254 #       Rename all requested interfaces.
255 #
256 ifnet_rename()
257 {
258
259         _ifn_list="`ifconfig -l`"
260         [ -z "$_ifn_list" ] && return 0
261         for _if in ${_ifn_list} ; do
262                 eval _ifname=\$ifconfig_${_if}_name
263                 if [ ! -z "$_ifname" ]; then
264                         ifconfig $_if name $_ifname
265                 fi
266         done
267         return 0
268 }
269
270 #
271 # list_net_interfaces type
272 #       List all network interfaces. The type of interface returned
273 #       can be controlled by the type argument. The type
274 #       argument can be any of the following:
275 #               nodhcp - all interfaces, excluding DHCP configured interfaces
276 #               dhcp   - list only DHCP configured interfaces
277 #       If no argument is specified all network interfaces are output.
278 #       Note that the list will include cloned interfaces if applicable.
279 #       Cloned interfaces must already exist to have a chance to appear
280 #       in the list if ${network_interfaces} is set to `auto'.
281 #
282 list_net_interfaces()
283 {
284         type=$1
285
286         # Get a list of ALL the interfaces
287         #
288         case ${network_interfaces} in
289         [Aa][Uu][Tt][Oo])
290                 _tmplist="`ifconfig -l`"
291                 ;;
292         *)
293                 _tmplist="${network_interfaces} ${cloned_interfaces}"
294                 ;;
295         esac
296
297         if [ -z "$type" ]; then
298                 echo $_tmplist
299                 return 0
300         fi
301
302         # Separate out dhcp and non-dhcp interfaces
303         #
304         _aprefix=
305         _bprefix=
306         for _if in ${_tmplist} ; do
307                 eval _ifarg="\$ifconfig_${_if}"
308                 case "$_ifarg" in
309                 [Dd][Hh][Cc][Pp])
310                         _dhcplist="${_dhcplist}${_aprefix}${_if}"
311                         [ -z "$_aprefix" ] && _aprefix=' '
312                         ;;
313                 ''|*)
314                         _nodhcplist="${_nodhcplist}${_bprefix}${_if}"
315                         [ -z "$_bprefix" ] && _bprefix=' '
316                         ;;
317                 esac
318         done
319
320         case ${pccard_ifconfig} in
321         [Dd][Hh][Cc][Pp])
322                 for _if in ${removable_interfaces} ; do
323                         _test_if=`ifconfig ${_if} 2>&1`
324                         case "$_test_if" in
325                         "ifconfig: interface $_if does not exist")
326                                 ;;
327                         *)
328                                 _dhcplist="${_dhcplist}${_aprefix}${_if}"
329                                 [ -z "$_aprefix" ] && _aprefix=' '
330                                 ;;
331                         esac
332                 done
333                 ;;
334         *)
335                 ;;
336         esac
337
338         case "$type" in
339         nodhcp)
340                 echo $_nodhcplist
341                 ;;
342         dhcp)
343                 echo $_dhcplist
344                 ;;
345         esac
346         return 0
347 }
348
349 hexdigit()
350 {
351         if [ $1 -lt 10 ]; then
352                 echo $1
353         else
354                 case $1 in
355                 10)     echo a ;;
356                 11)     echo b ;;
357                 12)     echo c ;;
358                 13)     echo d ;;
359                 14)     echo e ;;
360                 15)     echo f ;;
361                 esac
362         fi
363 }
364
365 hexprint()
366 {
367         val=$1
368         str=''
369
370         dig=`hexdigit $((${val} & 15))`
371         str=${dig}${str}
372         val=$((${val} >> 4))
373         while [ ${val} -gt 0 ]; do
374                 dig=`hexdigit $((${val} & 15))`
375                 str=${dig}${str}
376                 val=$((${val} >> 4))
377         done
378
379         echo ${str}
380 }
381
382 # Setup the interfaces for IPv6
383 network6_interface_setup()
384 {
385         interfaces=$*
386         rtsol_interfaces=''
387         case ${ipv6_gateway_enable} in
388         [Yy][Ee][Ss])
389                 rtsol_available=no
390                 ;;
391         *)
392                 rtsol_available=yes
393                 ;;
394         esac
395         for i in $interfaces; do
396                 rtsol_interface=yes
397                 eval prefix=\$ipv6_prefix_$i
398                 if [ -n "${prefix}" ]; then
399                         rtsol_available=no
400                         rtsol_interface=no
401                         laddr=`network6_getladdr $i`
402                         hostid=`expr "${laddr}" : 'fe80::\(.*\)%\(.*\)'`
403                         for j in ${prefix}; do
404                                 address=$j\:${hostid}
405                                 ifconfig $i inet6 ${address} prefixlen 64 alias
406
407                                 case ${ipv6_gateway_enable} in
408                                 [Yy][Ee][Ss])
409                                         # subnet-router anycast address
410                                         # (rfc2373)
411                                         ifconfig $i inet6 $j:: prefixlen 64 \
412                                                 alias anycast
413                                         ;;
414                                 esac
415                         done
416                 fi
417                 eval ipv6_ifconfig=\$ipv6_ifconfig_$i
418                 if [ -n "${ipv6_ifconfig}" ]; then
419                         rtsol_available=no
420                         rtsol_interface=no
421                         ifconfig $i inet6 ${ipv6_ifconfig} alias
422                 fi
423
424                 if [ ${rtsol_available} = yes -a ${rtsol_interface} = yes ]
425                 then
426                         case ${i} in
427                         lo0|gif[0-9]*|stf[0-9]*|faith[0-9]*|lp[0-9]*|sl[0-9]*|tun[0-9]*)
428                                 ;;
429                         *)
430                                 rtsol_interfaces="${rtsol_interfaces} ${i}"
431                                 ;;
432                         esac
433                 else
434                         ifconfig $i inet6
435                 fi
436         done
437
438         if [ ${rtsol_available} = yes -a -n "${rtsol_interfaces}" ]; then
439                 # Act as endhost - automatically configured.
440                 # You can configure only single interface, as
441                 # specification assumes that autoconfigured host has
442                 # single interface only.
443                 sysctl net.inet6.ip6.accept_rtadv=1
444                 set ${rtsol_interfaces}
445                 ifconfig $1 up
446                 rtsol ${rtsol_flags} $1
447         fi
448
449         for i in $interfaces; do
450                 alias=0
451                 while : ; do
452                         eval ipv6_ifconfig=\$ipv6_ifconfig_${i}_alias${alias}
453                         if [ -z "${ipv6_ifconfig}" ]; then
454                                 break;
455                         fi
456                         ifconfig $i inet6 ${ipv6_ifconfig} alias
457                         alias=$((${alias} + 1))
458                 done
459         done
460 }
461
462 # Setup IPv6 to IPv4 mapping
463 network6_stf_setup()
464 {
465         case ${stf_interface_ipv4addr} in
466         [Nn][Oo] | '')
467                 ;;
468         *)
469                 # assign IPv6 addr and interface route for 6to4 interface
470                 stf_prefixlen=$((16+${stf_interface_ipv4plen:-0}))
471                 OIFS="$IFS"
472                 IFS=".$IFS"
473                 set ${stf_interface_ipv4addr}
474                 IFS="$OIFS"
475                 hexfrag1=`hexprint $(($1*256 + $2))`
476                 hexfrag2=`hexprint $(($3*256 + $4))`
477                 ipv4_in_hexformat="${hexfrag1}:${hexfrag2}"
478                 case ${stf_interface_ipv6_ifid} in
479                 [Aa][Uu][Tt][Oo] | '')
480                         for i in ${ipv6_network_interfaces}; do
481                                 laddr=`network6_getladdr ${i}`
482                                 case ${laddr} in
483                                 '')
484                                         ;;
485                                 *)
486                                         break
487                                         ;;
488                                 esac
489                         done
490                         stf_interface_ipv6_ifid=`expr "${laddr}" : \
491                                                       'fe80::\(.*\)%\(.*\)'`
492                         case ${stf_interface_ipv6_ifid} in
493                         '')
494                                 stf_interface_ipv6_ifid=0:0:0:1
495                                 ;;
496                         esac
497                         ;;
498                 esac
499                 ifconfig stf0 create >/dev/null 2>&1
500                 ifconfig stf0 inet6 2002:${ipv4_in_hexformat}:${stf_interface_ipv6_slaid:-0}:${stf_interface_ipv6_ifid} \
501                         prefixlen ${stf_prefixlen}
502                 # disallow packets to malicious 6to4 prefix
503                 route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
504                 route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
505                 route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
506                 route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
507                 ;;
508         esac
509 }
510
511 # Setup static routes
512 network6_static_routes_setup()
513 {
514         # Set up any static routes.
515         case ${ipv6_defaultrouter} in
516         [Nn][Oo] | '')
517                 ;;
518         *)
519                 ipv6_static_routes="default ${ipv6_static_routes}"
520                 ipv6_route_default="default ${ipv6_defaultrouter}"
521                 ;;
522         esac
523         case ${ipv6_static_routes} in
524         [Nn][Oo] | '')
525                 ;;
526         *)
527                 for i in ${ipv6_static_routes}; do
528                         eval ipv6_route_args=\$ipv6_route_${i}
529                         route add -inet6 ${ipv6_route_args}
530                 done
531                 ;;
532         esac
533 }
534
535 # Setup faith
536 network6_faith_setup()
537 {
538         case ${ipv6_faith_prefix} in
539         [Nn][Oo] | '')
540                 ;;
541         *)
542                 sysctl net.inet6.ip6.keepfaith=1
543                 ifconfig faith0 create >/dev/null 2>&1
544                 ifconfig faith0 up
545                 for prefix in ${ipv6_faith_prefix}; do
546                         prefixlen=`expr "${prefix}" : ".*/\(.*\)"`
547                         case ${prefixlen} in
548                         '')
549                                 prefixlen=96
550                                 ;;
551                         *)
552                                 prefix=`expr "${prefix}" : \
553                                              "\(.*\)/${prefixlen}"`
554                                 ;;
555                         esac
556                         route add -inet6 ${prefix} -prefixlen ${prefixlen} ::1
557                         route change -inet6 ${prefix} -prefixlen ${prefixlen} \
558                                 -ifp faith0
559                 done
560                 ;;
561         esac
562 }
563
564 # Install the "default interface" to kernel, which will be used
565 # as the default route when there's no router.
566 network6_default_interface_setup()
567 {
568         # Choose IPv6 default interface if it is not clearly specified.
569         case ${ipv6_default_interface} in
570         '')
571                 for i in ${ipv6_network_interfaces}; do
572                         case $i in
573                         lo0|faith[0-9]*)
574                                 continue
575                                 ;;
576                         esac
577                         laddr=`network6_getladdr $i exclude_tentative`
578                         case ${laddr} in
579                         '')
580                                 ;;
581                         *)
582                                 ipv6_default_interface=$i
583                                 break
584                                 ;;
585                         esac
586                 done
587                 ;;
588         esac
589
590         # Disallow unicast packets without outgoing scope identifiers,
591         # or route such packets to a "default" interface, if it is specified.
592         route add -inet6 fe80:: -prefixlen 10 ::1 -reject
593         case ${ipv6_default_interface} in
594         [Nn][Oo] | '')
595                 route add -inet6 ff02:: -prefixlen 16 ::1 -reject
596                 ;;
597         *)
598                 laddr=`network6_getladdr ${ipv6_default_interface}`
599                 route add -inet6 ff02:: ${laddr} -prefixlen 16 -interface \
600                         -cloning
601
602                 # Disable installing the default interface with the
603                 # case net.inet6.ip6.forwarding=0 and
604                 # net.inet6.ip6.accept_rtadv=0, due to avoid conflict
605                 # between the default router list and the manual
606                 # configured default route.
607                 case ${ipv6_gateway_enable} in
608                 [Yy][Ee][Ss])
609                         ;;
610                 *)
611                         if [ `sysctl -n net.inet6.ip6.accept_rtadv` -eq 1 ]
612                         then
613                                 ndp -I ${ipv6_default_interface}
614                         fi
615                         ;;
616                 esac
617                 ;;
618         esac
619 }
620
621 network6_getladdr()
622 {
623         ifconfig $1 2>/dev/null | while read proto addr rest; do
624                 case ${proto} in
625                 inet6)
626                         case ${addr} in
627                         fe80::*)
628                                 if [ -z "$2" ]; then
629                                         echo ${addr}
630                                         return
631                                 fi
632                                 case ${rest} in
633                                 *tentative*)
634                                         continue
635                                         ;;
636                                 *)
637                                         echo ${addr}
638                                         return
639                                 esac
640                         esac
641                 esac
642         done
643 }