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