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