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