Home | History | Annotate | Line # | Download | only in rc.d
network revision 1.11.4.2
      1       1.1     lukem #!/bin/sh
      2       1.1     lukem #
      3  1.11.4.2  jdolecek # $NetBSD: network,v 1.11.4.2 2000/07/25 19:12:20 jdolecek Exp $
      4       1.1     lukem #
      5       1.1     lukem 
      6       1.1     lukem # PROVIDE: network
      7       1.5    tsarna # REQUIRE: root mountcritlocal tty sysctl
      8       1.1     lukem 
      9       1.1     lukem . /etc/rc.subr
     10       1.1     lukem 
     11       1.1     lukem name="network"
     12       1.1     lukem start_cmd="network_start"
     13       1.6     lukem stop_cmd=":"
     14       1.1     lukem 
     15       1.1     lukem network_start()
     16       1.1     lukem {
     17       1.1     lukem 	# set hostname, turn on network
     18       1.1     lukem 	#
     19       1.1     lukem 	echo "Starting network."
     20       1.1     lukem 
     21       1.1     lukem 	# If $hostname is set, use it for my Internet name,
     22       1.1     lukem 	# otherwise use /etc/myname
     23       1.1     lukem 	#
     24       1.1     lukem 	if [ -z "$hostname" -a -f /etc/myname ]; then
     25       1.1     lukem 		hostname=`cat /etc/myname`
     26       1.1     lukem 	fi
     27       1.1     lukem 	if [ -n "$hostname" ]; then
     28       1.1     lukem 		echo "Hostname: $hostname"
     29       1.1     lukem 		hostname $hostname
     30       1.1     lukem 	else
     31       1.8   thorpej 		# Don't warn about it if we're going to run
     32       1.8   thorpej 		# DHCP later, as we will probably get the
     33       1.8   thorpej 		# hostname at that time.
     34       1.8   thorpej 		#
     35       1.9     veego 		if ! checkyesno dhclient; then
     36       1.8   thorpej 			warn "\$hostname not set."
     37       1.8   thorpej 		fi
     38       1.1     lukem 	fi
     39       1.1     lukem 
     40       1.1     lukem 	# Check $domainname first, then /etc/defaultdomain,
     41       1.1     lukem 	# for NIS/YP domain name
     42       1.1     lukem 	#
     43       1.1     lukem 	if [ -z "$domainname" -a -f /etc/defaultdomain ]; then
     44       1.1     lukem 		domainname=`cat /etc/defaultdomain`
     45       1.1     lukem 	fi
     46       1.1     lukem 	if [ -n "$domainname" ]; then
     47       1.1     lukem 		echo "NIS domainname: $domainname"
     48       1.1     lukem 		domainname $domainname
     49       1.1     lukem 	fi
     50       1.1     lukem 
     51       1.1     lukem 	# Flush all routes just to make sure it is clean
     52       1.1     lukem 	if checkyesno flushroutes; then
     53       1.1     lukem 		route -n flush
     54       1.1     lukem 	fi
     55       1.1     lukem 
     56       1.1     lukem 	# Set the address for the first loopback interface, so that the
     57       1.1     lukem 	# auto-route from a newly configured interface's address to lo0
     58       1.1     lukem 	# works correctly.
     59       1.1     lukem 	#
     60       1.1     lukem 	# NOTE: obscure networking problems may occur if lo0 isn't configured...
     61       1.1     lukem 	#
     62       1.1     lukem 	ifconfig lo0 inet 127.0.0.1
     63      1.10    itojun 
     64      1.10    itojun 	# According to RFC1122, 127.0.0.0/8 should not leave the node.
     65      1.10    itojun 	#
     66      1.10    itojun 	route add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
     67       1.1     lukem 
     68       1.1     lukem 	# Configure all of the network interfaces listed in $net_interfaces;
     69       1.1     lukem 	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
     70       1.1     lukem 	# In the following, "xxN" stands in for interface names, like "le0".
     71       1.1     lukem 	# For any interfaces that has an $ifconfig_xxN variable associated,
     72       1.1     lukem 	# we do "ifconfig xxN $ifconfig_xxN".
     73       1.1     lukem 	# If there is no such variable, we take the contents of the file
     74       1.1     lukem 	# /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each
     75       1.1     lukem 	# line of the file as the arguments for a seperate "ifconfig"
     76       1.1     lukem 	# invocation.
     77       1.1     lukem 	#
     78       1.1     lukem 	# In order to configure an interface reasonably, you at the very least
     79       1.1     lukem 	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
     80       1.1     lukem 	# and probably a netmask (as in "netmask 0xffffffe0"). You will
     81       1.1     lukem 	# frequently need to specify a media type, as in "media UTP", for
     82       1.1     lukem 	# interface cards with multiple media connections that do not
     83       1.1     lukem 	# autoconfigure. See the ifconfig manual page for details.
     84       1.1     lukem 	#
     85       1.1     lukem 	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
     86       1.1     lukem 	# configuration is possible:
     87       1.1     lukem 	#	inet 10.1.1.1 netmask 0xffffff00
     88       1.1     lukem 	#	inet 10.1.1.2 netmask 0xffffff00 alias
     89       1.1     lukem 	#	inet6 fec0::1 prefixlen 64 alias
     90       1.1     lukem 	#
     91       1.1     lukem 	if [ "$net_interfaces" != NO ]; then
     92       1.1     lukem 		if checkyesno auto_ifconfig; then
     93       1.1     lukem 			tmp="`ifconfig -l`"
     94       1.1     lukem 		else
     95       1.1     lukem 			tmp="$net_interfaces"
     96       1.1     lukem 		fi
     97       1.1     lukem 		echo -n 'Configuring network interfaces:'
     98       1.1     lukem 		for int in $tmp; do
     99       1.1     lukem 			eval `echo 'args=$ifconfig_'$int`
    100       1.1     lukem 			if [ -n "$args" ]; then
    101       1.1     lukem 				echo -n " $int"
    102       1.1     lukem 				ifconfig $int $args
    103       1.1     lukem 			elif [ -f /etc/ifconfig.$int ]; then
    104       1.1     lukem 				echo -n " $int"
    105       1.1     lukem 				(while read args; do
    106       1.1     lukem 					if [ -n "`eval echo '$args'`" ] ; then
    107       1.1     lukem 						ifconfig $int $args
    108       1.1     lukem 					fi
    109       1.1     lukem 				done) < /etc/ifconfig.$int
    110       1.1     lukem 			else
    111       1.1     lukem 				if ! checkyesno auto_ifconfig; then
    112       1.1     lukem 					echo
    113       1.1     lukem 					warn \
    114       1.1     lukem 			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
    115       1.1     lukem 					warn "interface $int not configured."
    116       1.1     lukem 				fi
    117       1.1     lukem 				continue
    118       1.1     lukem 			fi
    119       1.1     lukem 			configured_interfaces="$configured_interfaces $int"
    120       1.1     lukem 		done
    121       1.1     lukem 		echo "."
    122       1.1     lukem 	fi
    123       1.1     lukem 
    124       1.1     lukem 	# Check $defaultroute, then /etc/mygate, for the name of my gateway
    125       1.1     lukem 	# host. That name must be in /etc/hosts.
    126       1.1     lukem 	#
    127       1.1     lukem 	if [ -z "$defaultroute" -a -f /etc/mygate ]; then
    128       1.1     lukem 		defaultroute=`cat /etc/mygate`
    129       1.1     lukem 	fi
    130       1.1     lukem 	if [ -n "$defaultroute" ]; then
    131       1.1     lukem 		route add default $defaultroute
    132       1.1     lukem 	fi
    133       1.1     lukem 
    134       1.1     lukem 	# Check if each configured interface xxN has an $ifaliases_xxN variable
    135       1.1     lukem 	# associated, then configure additional IP addresses for that interface.
    136       1.1     lukem 	# The variable contains a list of "address netmask" pairs, with
    137       1.1     lukem 	# "netmask" set to "-" if the interface default netmask is to be used.
    138       1.1     lukem 	#
    139       1.1     lukem 	# Note that $ifaliases_xxN works only with certain configurations and
    140       1.1     lukem 	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
    141       1.1     lukem 	# 
    142       1.1     lukem 	#
    143       1.1     lukem 	if [ -n "$configured_interfaces" ]; then
    144       1.1     lukem 		echo "Adding interface aliases:"
    145       1.1     lukem 		done_aliases_message=yes
    146       1.1     lukem 	fi
    147       1.1     lukem 	for int in $configured_interfaces; do
    148       1.1     lukem 		eval `echo 'args=$ifaliases_'$int`
    149       1.1     lukem 		if [ -n "$args" ]; then
    150       1.1     lukem 			set -- $args
    151       1.1     lukem 			while [ $# -ge 2 ]; do
    152       1.1     lukem 				addr=$1 ; net=$2 ; shift 2
    153       1.1     lukem 				if [ "$net" = "-" ]; then
    154  1.11.4.2  jdolecek 					# for compatibility only, obsolete
    155       1.1     lukem 					ifconfig $int inet alias $addr
    156       1.1     lukem 				else
    157       1.1     lukem 					ifconfig $int inet alias $addr \
    158       1.1     lukem 					    netmask $net
    159       1.1     lukem 				fi
    160       1.1     lukem 				# Use loopback, not the wire
    161       1.1     lukem 				route add $addr 127.0.0.1
    162       1.1     lukem 			done
    163       1.1     lukem 		fi
    164       1.1     lukem 	done
    165       1.1     lukem 
    166       1.1     lukem 	# /etc/ifaliases, if it exists, contains the names of additional IP
    167       1.1     lukem 	# addresses for each interface. It is formatted as a series of lines
    168       1.1     lukem 	# that contain
    169       1.1     lukem 	#	address interface netmask
    170       1.1     lukem 	#
    171       1.1     lukem 	# Note that /etc/ifaliases works only with certain cases only and its
    172       1.1     lukem 	# use is not recommended.  Use /etc/ifconfig.xxN instead.
    173       1.1     lukem 	#
    174       1.1     lukem 	#
    175       1.1     lukem 	if [ -f /etc/ifaliases ]; then
    176       1.1     lukem 	(
    177       1.1     lukem 		if [ "$done_aliases_message" != yes ]; then
    178       1.1     lukem 			echo "Adding interface aliases:"
    179       1.1     lukem 		fi
    180       1.1     lukem 		while read addr int net; do
    181       1.1     lukem 			if [ -z "$net" ]; then
    182  1.11.4.2  jdolecek 				# for compatibility only, obsolete
    183       1.1     lukem 				ifconfig $int inet alias $addr
    184       1.1     lukem 			else
    185       1.1     lukem 				ifconfig $int inet alias $addr netmask $net
    186       1.1     lukem 			fi
    187       1.1     lukem 			# use loopback, not the wire
    188       1.1     lukem 			route add $addr 127.0.0.1
    189       1.1     lukem 		done
    190       1.1     lukem 	) < /etc/ifaliases
    191       1.1     lukem 	fi
    192       1.1     lukem 
    193       1.1     lukem 	# IPv6
    194       1.1     lukem 	# Note that manual configuration can be done in the above, using
    195       1.1     lukem 	# ifconfig.
    196       1.1     lukem 	#
    197       1.1     lukem 	if ifconfig lo0 inet6 >/dev/null 2>&1; then
    198       1.1     lukem 		# We have IPv6 support in kernel.
    199       1.1     lukem 
    200       1.4    itojun 		# disallow link-local unicast dest without outgoing scope
    201       1.1     lukem 		# identifiers.
    202       1.1     lukem 		#
    203       1.1     lukem 		route add -inet6 fe80:: -prefixlen 10 ::1 -reject
    204       1.4    itojun 
    205       1.4    itojun 		# disallow site-local unicast dest without outgoing scope
    206       1.4    itojun 		# identifiers.
    207       1.4    itojun 		# If you configure site-locals without scope id (it is
    208       1.4    itojun 		# permissible config for routers that are not on scope
    209       1.4    itojun 		# boundary), you may want to comment the following one out.
    210       1.4    itojun 		#
    211       1.4    itojun 		route add -inet6 fec0:: -prefixlen 10 ::1 -reject
    212       1.1     lukem 
    213       1.1     lukem 		# disallow "internal" addresses to appear on the wire.
    214       1.1     lukem 		#
    215       1.1     lukem 		route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
    216       1.4    itojun 
    217       1.4    itojun 		# disallow packets to malicious IPv4 compatible prefix
    218       1.4    itojun 		#
    219       1.4    itojun 		route add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
    220       1.4    itojun 		route add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
    221       1.4    itojun 		route add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
    222       1.4    itojun 		route add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
    223       1.3    itojun 
    224       1.3    itojun 		# disallow packets to malicious 6to4 prefix
    225       1.3    itojun 		#
    226       1.3    itojun 		route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
    227       1.3    itojun 		route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
    228       1.4    itojun 		route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
    229       1.4    itojun 		route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
    230       1.4    itojun 
    231       1.4    itojun 		# Completely disallow packets to IPv4 compatible prefix.
    232       1.4    itojun 		# This may conflict with RFC1933 under following circumstances:
    233       1.4    itojun 		# (1) An IPv6-only KAME node tries to originate packets to IPv4
    234       1.4    itojun 		#     comatible destination.  The KAME node has no IPv4
    235       1.4    itojun 		#     compatible support.  Under RFC1933, it should transmit
    236       1.4    itojun 		#     native IPv6 packets toward IPv4 compatible destination,
    237       1.4    itojun 		#     hoping it would reach a router that forwards the packet
    238       1.4    itojun 		#     toward auto-tunnel interface.
    239       1.4    itojun 		# (2) An IPv6-only node originates a packet to IPv4 compatible
    240       1.4    itojun 		#     destination.  A KAME node is acting as an IPv6 router, and
    241       1.4    itojun 		#     asked to forward it.
    242       1.4    itojun 		# Due to rare use of IPv4 compatible address, and security
    243       1.4    itojun 		# issues with it, we disable it by default.
    244       1.4    itojun 		#
    245       1.4    itojun 		route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
    246       1.1     lukem 
    247       1.1     lukem 		sysctl -w net.inet6.ip6.forwarding=0 >/dev/null
    248       1.1     lukem 		sysctl -w net.inet6.ip6.accept_rtadv=0 >/dev/null
    249       1.1     lukem 
    250       1.1     lukem 		# backward compatibility
    251       1.1     lukem 		#
    252       1.1     lukem 		if [ -z "$ip6mode" -a -n "$ip6forwarding" ]; then
    253       1.1     lukem 			warn 'Please migrate to newer rc.conf' \
    254       1.1     lukem 			    '(use ip6mode, not ip6forwarding)'
    255       1.1     lukem 			if checkyesno ip6forwarding; then
    256       1.1     lukem 				ip6mode=router
    257       1.1     lukem 			else
    258       1.1     lukem 				if checkyesno rtsol; then
    259       1.1     lukem 					ip6mode=autohost
    260       1.1     lukem 				else
    261       1.1     lukem 					ip6mode=host
    262       1.1     lukem 				fi
    263       1.1     lukem 			fi
    264       1.1     lukem 		fi
    265       1.1     lukem 
    266       1.1     lukem 		case $ip6mode in
    267       1.1     lukem 		router)
    268       1.1     lukem 			echo 'IPv6 mode: router'
    269       1.1     lukem 			sysctl -w net.inet6.ip6.forwarding=1 >/dev/null
    270       1.1     lukem 			;;
    271       1.1     lukem 
    272       1.1     lukem 		autohost)
    273       1.1     lukem 			echo 'IPv6 mode: autoconfigured host'
    274       1.1     lukem 			sysctl -w net.inet6.ip6.accept_rtadv=1 >/dev/null
    275       1.1     lukem 			;;
    276       1.1     lukem 
    277       1.1     lukem 		host)	
    278       1.1     lukem 			echo 'IPv6 mode: host'
    279       1.1     lukem 			;;
    280       1.1     lukem 
    281       1.1     lukem 		*)	echo 'WARNING: invalid value in ip6mode'
    282       1.1     lukem 			;;
    283       1.1     lukem 
    284       1.1     lukem 		esac
    285       1.1     lukem 
    286       1.1     lukem 		if checkyesno rtsol; then
    287       1.1     lukem 			if [ "$ip6mode" = "autohost" ]; then
    288       1.1     lukem 				echo 'Sending router solicitation...'
    289       1.1     lukem 				rtsol $rtsol_flags
    290       1.1     lukem 			else
    291       1.1     lukem 				echo
    292       1.1     lukem 				warn \
    293       1.1     lukem 			    "ip6mode must be set to 'autohost' to use rtsol."
    294       1.1     lukem 			fi
    295       1.1     lukem 		fi
    296       1.1     lukem 
    297       1.1     lukem 		# wait till DAD is completed. always invoke it in case if are
    298       1.1     lukem 		# configured manually by ifconfig
    299       1.1     lukem 		#
    300       1.1     lukem 		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
    301       1.1     lukem 		sleep $dadcount
    302       1.1     lukem 		sleep 1
    303       1.1     lukem 	fi
    304       1.1     lukem 
    305       1.1     lukem 	# XXX this must die
    306       1.1     lukem 	if [ -s /etc/netstart.local ]; then
    307       1.1     lukem 		sh /etc/netstart.local start
    308       1.1     lukem 	fi
    309       1.1     lukem }
    310       1.1     lukem 
    311       1.1     lukem network_stop()
    312       1.1     lukem {
    313       1.1     lukem 	echo "Stopping network."
    314       1.1     lukem 
    315       1.1     lukem 	# XXX this must die
    316       1.1     lukem 	if [ -s /etc/netstart.local ]; then
    317       1.1     lukem 		sh /etc/netstart.local stop
    318       1.1     lukem 	fi
    319       1.1     lukem 
    320       1.1     lukem 	echo "Deleting aliases."
    321       1.1     lukem 	if [ -f /etc/ifaliases ]; then
    322       1.1     lukem 	(
    323       1.1     lukem 		while read addr int net; do
    324       1.1     lukem 			ifconfig $int inet delete $addr
    325       1.1     lukem 		done
    326       1.1     lukem 	) < /etc/ifaliases
    327       1.1     lukem 	fi
    328       1.1     lukem 
    329       1.1     lukem 	for int in $configured_interfaces; do
    330       1.1     lukem 		eval `echo 'args=$ifaliases_'$int`
    331       1.1     lukem 		if [ -n "$args" ]; then
    332       1.1     lukem 			set -- $args
    333       1.1     lukem 			while [ $# -ge 2 ]; do
    334       1.1     lukem 				addr=$1 ; net=$2 ; shift 2
    335       1.1     lukem 				ifconfig $int inet delete $addr
    336       1.1     lukem 			done
    337       1.1     lukem 		fi
    338       1.1     lukem 	done
    339       1.1     lukem 
    340       1.1     lukem 	# down interfaces
    341       1.1     lukem 	#
    342       1.1     lukem 	echo -n 'Downing network interfaces:'
    343       1.1     lukem 	if [ "$net_interfaces" != NO ]; then
    344       1.1     lukem 		if checkyesno auto_ifconfig; then
    345       1.1     lukem 			tmp="`ifconfig -l`"
    346       1.1     lukem 		else
    347       1.1     lukem 			tmp="$net_interfaces"
    348       1.1     lukem 		fi
    349       1.1     lukem 		for int in $tmp; do
    350       1.1     lukem 			eval `echo 'args=$ifconfig_'$int`
    351       1.2     veego 			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
    352       1.1     lukem 				echo -n " $int"
    353       1.1     lukem 				ifconfig $int down
    354       1.1     lukem 			fi
    355       1.1     lukem 		done
    356       1.1     lukem 		echo "."
    357       1.1     lukem 	fi
    358       1.1     lukem 
    359       1.1     lukem 	# flush routes
    360       1.1     lukem 	#
    361       1.1     lukem 	route -n flush
    362       1.1     lukem 
    363       1.1     lukem }
    364       1.1     lukem 
    365      1.11     lukem load_rc_config $name
    366       1.1     lukem run_rc_command "$1"
    367