Home | History | Annotate | Line # | Download | only in rc.d
network revision 1.32
      1 #!/bin/sh
      2 #
      3 # $NetBSD: network,v 1.32 2001/07/08 07:16:00 lukem 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 will 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 			done
    270 		fi
    271 	done
    272 
    273 	# /etc/ifaliases, if it exists, contains the names of additional IP
    274 	# addresses for each interface. It is formatted as a series of lines
    275 	# that contain
    276 	#	address interface netmask
    277 	#
    278 	# Note that /etc/ifaliases works only with certain cases only and its
    279 	# use is not recommended.  Use /etc/ifconfig.xxN instead.
    280 	#
    281 	#
    282 	if [ -f /etc/ifaliases ]; then
    283 		if [ "$done_aliases_message" != yes ]; then
    284 			echo "Adding interface aliases:"
    285 		fi
    286 		while read addr int net; do
    287 			if [ -z "$net" ]; then
    288 				# for compatibility only, obsolete
    289 				ifconfig $int inet alias $addr
    290 			else
    291 				ifconfig $int inet alias $addr netmask $net
    292 			fi
    293 		done < /etc/ifaliases
    294 	fi
    295 
    296 	# IPv6 interface autoconfiguration.
    297 	#
    298 	if ifconfig lo0 inet6 >/dev/null 2>&1; then
    299 		# wait till DAD is completed. always invoke it in case
    300 		# if are configured manually by ifconfig
    301 		#
    302 		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
    303 		sleep $dadcount
    304 		sleep 1
    305 
    306 		if checkyesno rtsol; then
    307 			if [ "$ip6mode" = "autohost" ]; then
    308 				echo 'Sending router solicitation...'
    309 				rtsol $rtsol_flags
    310 			else
    311 				echo
    312 				warn \
    313 			    "ip6mode must be set to 'autohost' to use rtsol."
    314 			fi
    315 
    316 			# wait till DAD is completed, for global addresses
    317 			# configured by router advert message.
    318 			#
    319 			sleep $dadcount
    320 			sleep 1
    321 		fi
    322 	fi
    323 
    324 	# XXX this must die
    325 	if [ -s /etc/netstart.local ]; then
    326 		sh /etc/netstart.local start
    327 	fi
    328 }
    329 
    330 network_stop()
    331 {
    332 	echo "Stopping network."
    333 
    334 	# XXX this must die
    335 	if [ -s /etc/netstart.local ]; then
    336 		sh /etc/netstart.local stop
    337 	fi
    338 
    339 	echo "Deleting aliases."
    340 	if [ -f /etc/ifaliases ]; then
    341 		while read addr int net; do
    342 			ifconfig $int inet delete $addr
    343 		done < /etc/ifaliases
    344 	fi
    345 
    346 	for int in `ifconfig -lu`; do
    347 		eval args=\$ifaliases_$int
    348 		if [ -n "$args" ]; then
    349 			set -- $args
    350 			while [ $# -ge 2 ]; do
    351 				addr=$1 ; net=$2 ; shift 2
    352 				ifconfig $int inet delete $addr
    353 			done
    354 		fi
    355 	done
    356 
    357 	# down interfaces
    358 	#
    359 	echo -n 'Downing network interfaces:'
    360 	if [ "$net_interfaces" != NO ]; then
    361 		if checkyesno auto_ifconfig; then
    362 			tmp=`ifconfig -l`
    363 		else
    364 			tmp="$net_interfaces"
    365 		fi
    366 		for int in $tmp; do
    367 			eval args=\$ifconfig_$int
    368 			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
    369 				echo -n " $int"
    370 				ifconfig $int down
    371 			fi
    372 		done
    373 		echo "."
    374 	fi
    375 
    376 	# flush routes
    377 	#
    378 	route -n flush
    379 
    380 }
    381 
    382 load_rc_config $name
    383 run_rc_command "$1"
    384