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