network revision 1.1
11.1Slukem#!/bin/sh
21.1Slukem#
31.1Slukem# $NetBSD: network,v 1.1 2000/03/10 11:53:24 lukem Exp $
41.1Slukem#
51.1Slukem
61.1Slukem# PROVIDE: network
71.1Slukem# REQUIRE: root mountcritlocal tty ipfilter
81.1Slukem
91.1Slukem. /etc/rc.subr
101.1Slukem. /etc/rc.conf
111.1Slukem
121.1Slukemname="network"
131.1Slukemstart_cmd="network_start"
141.1Slukemstop_cmd="network_stop"
151.1Slukem
161.1Slukemnetwork_start()
171.1Slukem{
181.1Slukem	# set hostname, turn on network
191.1Slukem	#
201.1Slukem	echo "Starting network."
211.1Slukem
221.1Slukem	# If $hostname is set, use it for my Internet name,
231.1Slukem	# otherwise use /etc/myname
241.1Slukem	#
251.1Slukem	if [ -z "$hostname" -a -f /etc/myname ]; then
261.1Slukem		hostname=`cat /etc/myname`
271.1Slukem	fi
281.1Slukem	if [ -n "$hostname" ]; then
291.1Slukem		echo "Hostname: $hostname"
301.1Slukem		hostname $hostname
311.1Slukem	else
321.1Slukem		warn "\$hostname not set."
331.1Slukem	fi
341.1Slukem
351.1Slukem	# Check $domainname first, then /etc/defaultdomain,
361.1Slukem	# for NIS/YP domain name
371.1Slukem	#
381.1Slukem	if [ -z "$domainname" -a -f /etc/defaultdomain ]; then
391.1Slukem		domainname=`cat /etc/defaultdomain`
401.1Slukem	fi
411.1Slukem	if [ -n "$domainname" ]; then
421.1Slukem		echo "NIS domainname: $domainname"
431.1Slukem		domainname $domainname
441.1Slukem	fi
451.1Slukem
461.1Slukem	# Flush all routes just to make sure it is clean
471.1Slukem	if checkyesno flushroutes; then
481.1Slukem		route -n flush
491.1Slukem	fi
501.1Slukem
511.1Slukem	# Set the address for the first loopback interface, so that the
521.1Slukem	# auto-route from a newly configured interface's address to lo0
531.1Slukem	# works correctly.
541.1Slukem	#
551.1Slukem	# NOTE: obscure networking problems may occur if lo0 isn't configured...
561.1Slukem	#
571.1Slukem	ifconfig lo0 inet 127.0.0.1
581.1Slukem
591.1Slukem	# Configure all of the network interfaces listed in $net_interfaces;
601.1Slukem	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
611.1Slukem	# In the following, "xxN" stands in for interface names, like "le0".
621.1Slukem	# For any interfaces that has an $ifconfig_xxN variable associated,
631.1Slukem	# we do "ifconfig xxN $ifconfig_xxN".
641.1Slukem	# If there is no such variable, we take the contents of the file
651.1Slukem	# /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each
661.1Slukem	# line of the file as the arguments for a seperate "ifconfig"
671.1Slukem	# invocation.
681.1Slukem	#
691.1Slukem	# In order to configure an interface reasonably, you at the very least
701.1Slukem	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
711.1Slukem	# and probably a netmask (as in "netmask 0xffffffe0"). You will
721.1Slukem	# frequently need to specify a media type, as in "media UTP", for
731.1Slukem	# interface cards with multiple media connections that do not
741.1Slukem	# autoconfigure. See the ifconfig manual page for details.
751.1Slukem	#
761.1Slukem	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
771.1Slukem	# configuration is possible:
781.1Slukem	#	inet 10.1.1.1 netmask 0xffffff00
791.1Slukem	#	inet 10.1.1.2 netmask 0xffffff00 alias
801.1Slukem	#	inet6 fec0::1 prefixlen 64 alias
811.1Slukem	#
821.1Slukem	if [ "$net_interfaces" != NO ]; then
831.1Slukem		if checkyesno auto_ifconfig; then
841.1Slukem			tmp="`ifconfig -l`"
851.1Slukem		else
861.1Slukem			tmp="$net_interfaces"
871.1Slukem		fi
881.1Slukem		echo -n 'Configuring network interfaces:'
891.1Slukem		for int in $tmp; do
901.1Slukem			eval `echo 'args=$ifconfig_'$int`
911.1Slukem			if [ -n "$args" ]; then
921.1Slukem				echo -n " $int"
931.1Slukem				ifconfig $int $args
941.1Slukem			elif [ -f /etc/ifconfig.$int ]; then
951.1Slukem				echo -n " $int"
961.1Slukem				(while read args; do
971.1Slukem					if [ -n "`eval echo '$args'`" ] ; then
981.1Slukem						ifconfig $int $args
991.1Slukem					fi
1001.1Slukem				done) < /etc/ifconfig.$int
1011.1Slukem			else
1021.1Slukem				if ! checkyesno auto_ifconfig; then
1031.1Slukem					echo
1041.1Slukem					warn \
1051.1Slukem			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
1061.1Slukem					warn "interface $int not configured."
1071.1Slukem				fi
1081.1Slukem				continue
1091.1Slukem			fi
1101.1Slukem			configured_interfaces="$configured_interfaces $int"
1111.1Slukem		done
1121.1Slukem		echo "."
1131.1Slukem	fi
1141.1Slukem
1151.1Slukem	# Check $defaultroute, then /etc/mygate, for the name of my gateway
1161.1Slukem	# host. That name must be in /etc/hosts.
1171.1Slukem	#
1181.1Slukem	if [ -z "$defaultroute" -a -f /etc/mygate ]; then
1191.1Slukem		defaultroute=`cat /etc/mygate`
1201.1Slukem	fi
1211.1Slukem	if [ -n "$defaultroute" ]; then
1221.1Slukem		route add default $defaultroute
1231.1Slukem	fi
1241.1Slukem
1251.1Slukem	# Check if each configured interface xxN has an $ifaliases_xxN variable
1261.1Slukem	# associated, then configure additional IP addresses for that interface.
1271.1Slukem	# The variable contains a list of "address netmask" pairs, with
1281.1Slukem	# "netmask" set to "-" if the interface default netmask is to be used.
1291.1Slukem	#
1301.1Slukem	# Note that $ifaliases_xxN works only with certain configurations and
1311.1Slukem	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
1321.1Slukem	# 
1331.1Slukem	#
1341.1Slukem	if [ -n "$configured_interfaces" ]; then
1351.1Slukem		echo "Adding interface aliases:"
1361.1Slukem		done_aliases_message=yes
1371.1Slukem	fi
1381.1Slukem	for int in $configured_interfaces; do
1391.1Slukem		eval `echo 'args=$ifaliases_'$int`
1401.1Slukem		if [ -n "$args" ]; then
1411.1Slukem			set -- $args
1421.1Slukem			while [ $# -ge 2 ]; do
1431.1Slukem				addr=$1 ; net=$2 ; shift 2
1441.1Slukem				if [ "$net" = "-" ]; then
1451.1Slukem					ifconfig $int inet alias $addr
1461.1Slukem				else
1471.1Slukem					ifconfig $int inet alias $addr \
1481.1Slukem					    netmask $net
1491.1Slukem				fi
1501.1Slukem				# Use loopback, not the wire
1511.1Slukem				route add $addr 127.0.0.1
1521.1Slukem			done
1531.1Slukem		fi
1541.1Slukem	done
1551.1Slukem
1561.1Slukem	# /etc/ifaliases, if it exists, contains the names of additional IP
1571.1Slukem	# addresses for each interface. It is formatted as a series of lines
1581.1Slukem	# that contain
1591.1Slukem	#	address interface netmask
1601.1Slukem	#
1611.1Slukem	# Note that /etc/ifaliases works only with certain cases only and its
1621.1Slukem	# use is not recommended.  Use /etc/ifconfig.xxN instead.
1631.1Slukem	#
1641.1Slukem	#
1651.1Slukem	if [ -f /etc/ifaliases ]; then
1661.1Slukem	(
1671.1Slukem		if [ "$done_aliases_message" != yes ]; then
1681.1Slukem			echo "Adding interface aliases:"
1691.1Slukem		fi
1701.1Slukem		while read addr int net; do
1711.1Slukem			if [ -z "$net" ]; then
1721.1Slukem				ifconfig $int inet alias $addr
1731.1Slukem			else
1741.1Slukem				ifconfig $int inet alias $addr netmask $net
1751.1Slukem			fi
1761.1Slukem			# use loopback, not the wire
1771.1Slukem			route add $addr 127.0.0.1
1781.1Slukem		done
1791.1Slukem	) < /etc/ifaliases
1801.1Slukem	fi
1811.1Slukem
1821.1Slukem	# IPv6
1831.1Slukem	# Note that manual configuration can be done in the above, using
1841.1Slukem	# ifconfig.
1851.1Slukem	#
1861.1Slukem	if ifconfig lo0 inet6 >/dev/null 2>&1; then
1871.1Slukem		# We have IPv6 support in kernel.
1881.1Slukem
1891.1Slukem		# disallow scoped unicast dest without outgoing scope
1901.1Slukem		# identifiers.
1911.1Slukem		#
1921.1Slukem		route add -inet6 fe80:: -prefixlen 10 ::1 -reject
1931.1Slukem		route add -inet6 fc80:: -prefixlen 10 ::1 -reject
1941.1Slukem
1951.1Slukem		# disallow "internal" addresses to appear on the wire.
1961.1Slukem		#
1971.1Slukem		route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
1981.1Slukem		route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
1991.1Slukem
2001.1Slukem		sysctl -w net.inet6.ip6.forwarding=0 >/dev/null
2011.1Slukem		sysctl -w net.inet6.ip6.accept_rtadv=0 >/dev/null
2021.1Slukem
2031.1Slukem		# backward compatibility
2041.1Slukem		#
2051.1Slukem		if [ -z "$ip6mode" -a -n "$ip6forwarding" ]; then
2061.1Slukem			warn 'Please migrate to newer rc.conf' \
2071.1Slukem			    '(use ip6mode, not ip6forwarding)'
2081.1Slukem			if checkyesno ip6forwarding; then
2091.1Slukem				ip6mode=router
2101.1Slukem			else
2111.1Slukem				if checkyesno rtsol; then
2121.1Slukem					ip6mode=autohost
2131.1Slukem				else
2141.1Slukem					ip6mode=host
2151.1Slukem				fi
2161.1Slukem			fi
2171.1Slukem		fi
2181.1Slukem
2191.1Slukem		case $ip6mode in
2201.1Slukem		router)
2211.1Slukem			echo 'IPv6 mode: router'
2221.1Slukem			sysctl -w net.inet6.ip6.forwarding=1 >/dev/null
2231.1Slukem			;;
2241.1Slukem
2251.1Slukem		autohost)
2261.1Slukem			echo 'IPv6 mode: autoconfigured host'
2271.1Slukem			sysctl -w net.inet6.ip6.accept_rtadv=1 >/dev/null
2281.1Slukem			if [ -n "$ip6defaultif" ]; then
2291.1Slukem				ndp -I $ip6defaultif
2301.1Slukem			fi
2311.1Slukem			;;
2321.1Slukem
2331.1Slukem		host)	
2341.1Slukem			echo 'IPv6 mode: host'
2351.1Slukem			if [ -n "$ip6defaultif" ]; then
2361.1Slukem				ndp -I $ip6defaultif
2371.1Slukem			fi
2381.1Slukem			;;
2391.1Slukem
2401.1Slukem		*)	echo 'WARNING: invalid value in ip6mode'
2411.1Slukem			;;
2421.1Slukem
2431.1Slukem		esac
2441.1Slukem
2451.1Slukem		if checkyesno rtsol; then
2461.1Slukem			if [ "$ip6mode" = "autohost" ]; then
2471.1Slukem				echo 'Sending router solicitation...'
2481.1Slukem				rtsol $rtsol_flags
2491.1Slukem			else
2501.1Slukem				echo
2511.1Slukem				warn \
2521.1Slukem			    "ip6mode must be set to 'autohost' to use rtsol."
2531.1Slukem			fi
2541.1Slukem		fi
2551.1Slukem
2561.1Slukem		# wait till DAD is completed. always invoke it in case if are
2571.1Slukem		# configured manually by ifconfig
2581.1Slukem		#
2591.1Slukem		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
2601.1Slukem		sleep $dadcount
2611.1Slukem		sleep 1
2621.1Slukem	fi
2631.1Slukem
2641.1Slukem	# XXX this must die
2651.1Slukem	if [ -s /etc/netstart.local ]; then
2661.1Slukem		sh /etc/netstart.local start
2671.1Slukem	fi
2681.1Slukem}
2691.1Slukem
2701.1Slukemnetwork_stop()
2711.1Slukem{
2721.1Slukem	echo "Stopping network."
2731.1Slukem
2741.1Slukem	# XXX this must die
2751.1Slukem	if [ -s /etc/netstart.local ]; then
2761.1Slukem		sh /etc/netstart.local stop
2771.1Slukem	fi
2781.1Slukem
2791.1Slukem	rtsolpid=`check_process rtsol`
2801.1Slukem	if [ -n "$rtsolpid" ]; then
2811.1Slukem		echo "Stopping rtsol (IPv6 router solicitation daemon)."
2821.1Slukem		kill -TERM $rtsolpid
2831.1Slukem	fi
2841.1Slukem
2851.1Slukem	echo "Deleting aliases."
2861.1Slukem	if [ -f /etc/ifaliases ]; then
2871.1Slukem	(
2881.1Slukem		while read addr int net; do
2891.1Slukem			ifconfig $int inet delete $addr
2901.1Slukem		done
2911.1Slukem	) < /etc/ifaliases
2921.1Slukem	fi
2931.1Slukem
2941.1Slukem	for int in $configured_interfaces; do
2951.1Slukem		eval `echo 'args=$ifaliases_'$int`
2961.1Slukem		if [ -n "$args" ]; then
2971.1Slukem			set -- $args
2981.1Slukem			while [ $# -ge 2 ]; do
2991.1Slukem				addr=$1 ; net=$2 ; shift 2
3001.1Slukem				ifconfig $int inet delete $addr
3011.1Slukem			done
3021.1Slukem		fi
3031.1Slukem	done
3041.1Slukem
3051.1Slukem	# down interfaces
3061.1Slukem	#
3071.1Slukem	echo -n 'Downing network interfaces:'
3081.1Slukem	if [ "$net_interfaces" != NO ]; then
3091.1Slukem		if checkyesno auto_ifconfig; then
3101.1Slukem			tmp="`ifconfig -l`"
3111.1Slukem		else
3121.1Slukem			tmp="$net_interfaces"
3131.1Slukem		fi
3141.1Slukem		for int in $tmp; do
3151.1Slukem			eval `echo 'args=$ifconfig_'$int`
3161.1Slukem			if [ -n "$args" || -f /etc/ifconfig.$int ]; then
3171.1Slukem				echo -n " $int"
3181.1Slukem				ifconfig $int down
3191.1Slukem			fi
3201.1Slukem		done
3211.1Slukem		echo "."
3221.1Slukem	fi
3231.1Slukem
3241.1Slukem	# flush routes
3251.1Slukem	#
3261.1Slukem	route -n flush
3271.1Slukem
3281.1Slukem}
3291.1Slukem
3301.1Slukemrun_rc_command "$1"
331