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