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