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