network revision 1.32
11.1Slukem#!/bin/sh
21.1Slukem#
31.32Slukem# $NetBSD: network,v 1.32 2001/07/08 07:16:00 lukem Exp $
41.1Slukem#
51.1Slukem
61.1Slukem# PROVIDE: network
71.19Slukem# REQUIRE: ipfilter ipsec mountcritlocal root tty sysctl
81.1Slukem
91.1Slukem. /etc/rc.subr
101.1Slukem
111.1Slukemname="network"
121.1Slukemstart_cmd="network_start"
131.14Slukemstop_cmd="network_stop"
141.1Slukem
151.1Slukemnetwork_start()
161.1Slukem{
171.1Slukem	# set hostname, turn on network
181.1Slukem	#
191.1Slukem	echo "Starting network."
201.1Slukem
211.1Slukem	# If $hostname is set, use it for my Internet name,
221.1Slukem	# otherwise use /etc/myname
231.1Slukem	#
241.20Snisimura	if [ -z "$hostname" ] && [ -f /etc/myname ]; then
251.1Slukem		hostname=`cat /etc/myname`
261.1Slukem	fi
271.1Slukem	if [ -n "$hostname" ]; then
281.1Slukem		echo "Hostname: $hostname"
291.1Slukem		hostname $hostname
301.1Slukem	else
311.8Sthorpej		# Don't warn about it if we're going to run
321.8Sthorpej		# DHCP later, as we will probably get the
331.8Sthorpej		# hostname at that time.
341.8Sthorpej		#
351.27Snisimura		if ! checkyesno dhclient && [ -z "`hostname`" ]; then
361.8Sthorpej			warn "\$hostname not set."
371.8Sthorpej		fi
381.1Slukem	fi
391.1Slukem
401.1Slukem	# Check $domainname first, then /etc/defaultdomain,
411.1Slukem	# for NIS/YP domain name
421.1Slukem	#
431.20Snisimura	if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then
441.1Slukem		domainname=`cat /etc/defaultdomain`
451.1Slukem	fi
461.1Slukem	if [ -n "$domainname" ]; then
471.1Slukem		echo "NIS domainname: $domainname"
481.1Slukem		domainname $domainname
491.1Slukem	fi
501.1Slukem
511.1Slukem	# Flush all routes just to make sure it is clean
521.1Slukem	if checkyesno flushroutes; then
531.1Slukem		route -n flush
541.1Slukem	fi
551.1Slukem
561.1Slukem	# Set the address for the first loopback interface, so that the
571.1Slukem	# auto-route from a newly configured interface's address to lo0
581.1Slukem	# works correctly.
591.1Slukem	#
601.32Slukem	# NOTE: obscure networking problems will occur if lo0 isn't configured.
611.1Slukem	#
621.1Slukem	ifconfig lo0 inet 127.0.0.1
631.10Sitojun
641.31Sitojun	# According to RFC1122, 127.0.0.0/8 must not leave the node.
651.10Sitojun	#
661.10Sitojun	route add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
671.1Slukem
681.30Sitojun	# IPv6 routing setups, and host/router mode selection.
691.30Sitojun	#
701.30Sitojun	if ifconfig lo0 inet6 >/dev/null 2>&1; then
711.30Sitojun		# We have IPv6 support in kernel.
721.30Sitojun
731.30Sitojun		# disallow link-local unicast dest without outgoing scope
741.30Sitojun		# identifiers.
751.30Sitojun		#
761.30Sitojun		route add -inet6 fe80:: -prefixlen 10 ::1 -reject
771.30Sitojun
781.30Sitojun		# disallow site-local unicast dest without outgoing scope
791.30Sitojun		# identifiers.
801.30Sitojun		# If you configure site-locals without scope id (it is
811.30Sitojun		# permissible config for routers that are not on scope
821.30Sitojun		# boundary), you may want to comment the following one out.
831.30Sitojun		#
841.30Sitojun		if ! checkyesno ip6sitelocal; then
851.30Sitojun			route add -inet6 fec0:: -prefixlen 10 ::1 -reject
861.30Sitojun		fi
871.30Sitojun
881.30Sitojun		# disallow "internal" addresses to appear on the wire.
891.30Sitojun		#
901.30Sitojun		route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
911.30Sitojun
921.30Sitojun		# disallow packets to malicious IPv4 compatible prefix
931.30Sitojun		#
941.30Sitojun		route add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
951.30Sitojun		route add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
961.30Sitojun		route add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
971.30Sitojun		route add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
981.30Sitojun
991.30Sitojun		# disallow packets to malicious 6to4 prefix
1001.30Sitojun		#
1011.30Sitojun		route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
1021.30Sitojun		route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
1031.30Sitojun		route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
1041.30Sitojun		route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
1051.30Sitojun
1061.30Sitojun		# Completely disallow packets to IPv4 compatible prefix.
1071.30Sitojun		# This may conflict with RFC1933 under following circumstances:
1081.30Sitojun		# (1) An IPv6-only KAME node tries to originate packets to IPv4
1091.30Sitojun		#     comatible destination.  The KAME node has no IPv4
1101.30Sitojun		#     compatible support.  Under RFC1933, it should transmit
1111.30Sitojun		#     native IPv6 packets toward IPv4 compatible destination,
1121.30Sitojun		#     hoping it would reach a router that forwards the packet
1131.30Sitojun		#     toward auto-tunnel interface.
1141.30Sitojun		# (2) An IPv6-only node originates a packet to IPv4 compatible
1151.30Sitojun		#     destination.  A KAME node is acting as an IPv6 router, and
1161.30Sitojun		#     asked to forward it.
1171.30Sitojun		# Due to rare use of IPv4 compatible address, and security
1181.30Sitojun		# issues with it, we disable it by default.
1191.30Sitojun		#
1201.30Sitojun		route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
1211.30Sitojun
1221.30Sitojun		sysctl -w net.inet6.ip6.forwarding=0 >/dev/null
1231.30Sitojun		sysctl -w net.inet6.ip6.accept_rtadv=0 >/dev/null
1241.30Sitojun
1251.30Sitojun		# backward compatibility
1261.30Sitojun		#
1271.30Sitojun		if [ -z "$ip6mode" ] && [ -n "$ip6forwarding" ]; then
1281.30Sitojun			warn 'Please migrate to newer rc.conf' \
1291.30Sitojun			    '(use ip6mode, not ip6forwarding)'
1301.30Sitojun			if checkyesno ip6forwarding; then
1311.30Sitojun				ip6mode=router
1321.30Sitojun			elif checkyesno rtsol; then
1331.30Sitojun				ip6mode=autohost
1341.30Sitojun			else
1351.30Sitojun				ip6mode=host
1361.30Sitojun			fi
1371.30Sitojun		fi
1381.30Sitojun
1391.30Sitojun		case $ip6mode in
1401.30Sitojun		router)
1411.30Sitojun			echo 'IPv6 mode: router'
1421.30Sitojun			sysctl -w net.inet6.ip6.forwarding=1 >/dev/null
1431.30Sitojun			;;
1441.30Sitojun
1451.30Sitojun		autohost)
1461.30Sitojun			echo 'IPv6 mode: autoconfigured host'
1471.30Sitojun			sysctl -w net.inet6.ip6.accept_rtadv=1 >/dev/null
1481.30Sitojun			;;
1491.30Sitojun
1501.30Sitojun		host)	
1511.30Sitojun			echo 'IPv6 mode: host'
1521.30Sitojun			;;
1531.30Sitojun
1541.30Sitojun		*)	echo 'WARNING: invalid value in ip6mode'
1551.30Sitojun			;;
1561.30Sitojun
1571.30Sitojun		esac
1581.30Sitojun	fi
1591.30Sitojun
1601.1Slukem	# Configure all of the network interfaces listed in $net_interfaces;
1611.1Slukem	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
1621.1Slukem	# In the following, "xxN" stands in for interface names, like "le0".
1631.1Slukem	# For any interfaces that has an $ifconfig_xxN variable associated,
1641.1Slukem	# we do "ifconfig xxN $ifconfig_xxN".
1651.1Slukem	# If there is no such variable, we take the contents of the file
1661.1Slukem	# /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each
1671.1Slukem	# line of the file as the arguments for a seperate "ifconfig"
1681.1Slukem	# invocation.
1691.1Slukem	#
1701.1Slukem	# In order to configure an interface reasonably, you at the very least
1711.1Slukem	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
1721.1Slukem	# and probably a netmask (as in "netmask 0xffffffe0"). You will
1731.1Slukem	# frequently need to specify a media type, as in "media UTP", for
1741.1Slukem	# interface cards with multiple media connections that do not
1751.1Slukem	# autoconfigure. See the ifconfig manual page for details.
1761.1Slukem	#
1771.1Slukem	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
1781.1Slukem	# configuration is possible:
1791.1Slukem	#	inet 10.1.1.1 netmask 0xffffff00
1801.1Slukem	#	inet 10.1.1.2 netmask 0xffffff00 alias
1811.1Slukem	#	inet6 fec0::1 prefixlen 64 alias
1821.1Slukem	#
1831.29Sitojun	# You can put shell script fragment into /etc/ifconfig.xxN by
1841.29Sitojun	# starting a line with "!".  Refer to ifconfig.if(5) for details.
1851.29Sitojun	#
1861.1Slukem	if [ "$net_interfaces" != NO ]; then
1871.1Slukem		if checkyesno auto_ifconfig; then
1881.22Snisimura			tmp=`ifconfig -l`
1891.15Sthorpej			for cloner in `ifconfig -C 2>/dev/null`; do
1901.25Swiz				for int in /etc/ifconfig.${cloner}[0-9]*; do
1911.23Snisimura					[ ! -f $int ] && break
1921.21Slukem					tmp="$tmp ${int##*.}"
1931.15Sthorpej				done
1941.15Sthorpej			done
1951.1Slukem		else
1961.1Slukem			tmp="$net_interfaces"
1971.1Slukem		fi
1981.1Slukem		echo -n 'Configuring network interfaces:'
1991.1Slukem		for int in $tmp; do
2001.20Snisimura			eval args=\$ifconfig_$int
2011.24Slukem			if [ -n "$args" ]; then
2021.1Slukem				echo -n " $int"
2031.1Slukem				ifconfig $int $args
2041.1Slukem			elif [ -f /etc/ifconfig.$int ]; then
2051.1Slukem				echo -n " $int"
2061.20Snisimura				while read args; do
2071.26Snisimura					[ -z "$args" ] && continue
2081.29Sitojun					case "$args" in
2091.29Sitojun					"#"*)
2101.29Sitojun						;;
2111.29Sitojun					"!"*)
2121.29Sitojun						eval ${args#*!}
2131.29Sitojun						;;
2141.29Sitojun					*)
2151.29Sitojun						ifconfig $int $args
2161.29Sitojun						;;
2171.29Sitojun					esac
2181.20Snisimura				done < /etc/ifconfig.$int
2191.1Slukem			else
2201.1Slukem				if ! checkyesno auto_ifconfig; then
2211.1Slukem					echo
2221.1Slukem					warn \
2231.1Slukem			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
2241.1Slukem					warn "interface $int not configured."
2251.1Slukem				fi
2261.1Slukem				continue
2271.1Slukem			fi
2281.1Slukem			configured_interfaces="$configured_interfaces $int"
2291.1Slukem		done
2301.1Slukem		echo "."
2311.1Slukem	fi
2321.1Slukem
2331.1Slukem	# Check $defaultroute, then /etc/mygate, for the name of my gateway
2341.1Slukem	# host. That name must be in /etc/hosts.
2351.1Slukem	#
2361.20Snisimura	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
2371.1Slukem		defaultroute=`cat /etc/mygate`
2381.1Slukem	fi
2391.1Slukem	if [ -n "$defaultroute" ]; then
2401.1Slukem		route add default $defaultroute
2411.1Slukem	fi
2421.1Slukem
2431.1Slukem	# Check if each configured interface xxN has an $ifaliases_xxN variable
2441.1Slukem	# associated, then configure additional IP addresses for that interface.
2451.1Slukem	# The variable contains a list of "address netmask" pairs, with
2461.1Slukem	# "netmask" set to "-" if the interface default netmask is to be used.
2471.1Slukem	#
2481.1Slukem	# Note that $ifaliases_xxN works only with certain configurations and
2491.1Slukem	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
2501.1Slukem	# 
2511.1Slukem	#
2521.1Slukem	if [ -n "$configured_interfaces" ]; then
2531.1Slukem		echo "Adding interface aliases:"
2541.1Slukem		done_aliases_message=yes
2551.1Slukem	fi
2561.1Slukem	for int in $configured_interfaces; do
2571.20Snisimura		eval args=\$ifaliases_$int
2581.1Slukem		if [ -n "$args" ]; then
2591.1Slukem			set -- $args
2601.1Slukem			while [ $# -ge 2 ]; do
2611.1Slukem				addr=$1 ; net=$2 ; shift 2
2621.1Slukem				if [ "$net" = "-" ]; then
2631.16Sjdolecek					# for compatibility only, obsolete
2641.1Slukem					ifconfig $int inet alias $addr
2651.1Slukem				else
2661.1Slukem					ifconfig $int inet alias $addr \
2671.1Slukem					    netmask $net
2681.1Slukem				fi
2691.1Slukem			done
2701.1Slukem		fi
2711.1Slukem	done
2721.1Slukem
2731.1Slukem	# /etc/ifaliases, if it exists, contains the names of additional IP
2741.1Slukem	# addresses for each interface. It is formatted as a series of lines
2751.1Slukem	# that contain
2761.1Slukem	#	address interface netmask
2771.1Slukem	#
2781.1Slukem	# Note that /etc/ifaliases works only with certain cases only and its
2791.1Slukem	# use is not recommended.  Use /etc/ifconfig.xxN instead.
2801.1Slukem	#
2811.1Slukem	#
2821.1Slukem	if [ -f /etc/ifaliases ]; then
2831.1Slukem		if [ "$done_aliases_message" != yes ]; then
2841.1Slukem			echo "Adding interface aliases:"
2851.1Slukem		fi
2861.1Slukem		while read addr int net; do
2871.1Slukem			if [ -z "$net" ]; then
2881.16Sjdolecek				# for compatibility only, obsolete
2891.1Slukem				ifconfig $int inet alias $addr
2901.1Slukem			else
2911.1Slukem				ifconfig $int inet alias $addr netmask $net
2921.1Slukem			fi
2931.20Snisimura		done < /etc/ifaliases
2941.1Slukem	fi
2951.1Slukem
2961.30Sitojun	# IPv6 interface autoconfiguration.
2971.1Slukem	#
2981.1Slukem	if ifconfig lo0 inet6 >/dev/null 2>&1; then
2991.17Sitojun		# wait till DAD is completed. always invoke it in case
3001.17Sitojun		# if are configured manually by ifconfig
3011.17Sitojun		#
3021.17Sitojun		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
3031.17Sitojun		sleep $dadcount
3041.17Sitojun		sleep 1
3051.17Sitojun
3061.1Slukem		if checkyesno rtsol; then
3071.1Slukem			if [ "$ip6mode" = "autohost" ]; then
3081.1Slukem				echo 'Sending router solicitation...'
3091.1Slukem				rtsol $rtsol_flags
3101.1Slukem			else
3111.1Slukem				echo
3121.1Slukem				warn \
3131.1Slukem			    "ip6mode must be set to 'autohost' to use rtsol."
3141.1Slukem			fi
3151.17Sitojun
3161.18Sitojun			# wait till DAD is completed, for global addresses
3171.18Sitojun			# configured by router advert message.
3181.17Sitojun			#
3191.17Sitojun			sleep $dadcount
3201.17Sitojun			sleep 1
3211.1Slukem		fi
3221.1Slukem	fi
3231.1Slukem
3241.1Slukem	# XXX this must die
3251.1Slukem	if [ -s /etc/netstart.local ]; then
3261.1Slukem		sh /etc/netstart.local start
3271.1Slukem	fi
3281.1Slukem}
3291.1Slukem
3301.1Slukemnetwork_stop()
3311.1Slukem{
3321.1Slukem	echo "Stopping network."
3331.1Slukem
3341.1Slukem	# XXX this must die
3351.1Slukem	if [ -s /etc/netstart.local ]; then
3361.1Slukem		sh /etc/netstart.local stop
3371.1Slukem	fi
3381.1Slukem
3391.1Slukem	echo "Deleting aliases."
3401.1Slukem	if [ -f /etc/ifaliases ]; then
3411.1Slukem		while read addr int net; do
3421.1Slukem			ifconfig $int inet delete $addr
3431.20Snisimura		done < /etc/ifaliases
3441.1Slukem	fi
3451.1Slukem
3461.21Slukem	for int in `ifconfig -lu`; do
3471.20Snisimura		eval args=\$ifaliases_$int
3481.1Slukem		if [ -n "$args" ]; then
3491.1Slukem			set -- $args
3501.1Slukem			while [ $# -ge 2 ]; do
3511.1Slukem				addr=$1 ; net=$2 ; shift 2
3521.1Slukem				ifconfig $int inet delete $addr
3531.1Slukem			done
3541.1Slukem		fi
3551.1Slukem	done
3561.1Slukem
3571.1Slukem	# down interfaces
3581.1Slukem	#
3591.1Slukem	echo -n 'Downing network interfaces:'
3601.1Slukem	if [ "$net_interfaces" != NO ]; then
3611.1Slukem		if checkyesno auto_ifconfig; then
3621.22Snisimura			tmp=`ifconfig -l`
3631.1Slukem		else
3641.1Slukem			tmp="$net_interfaces"
3651.1Slukem		fi
3661.1Slukem		for int in $tmp; do
3671.20Snisimura			eval args=\$ifconfig_$int
3681.2Sveego			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
3691.1Slukem				echo -n " $int"
3701.1Slukem				ifconfig $int down
3711.1Slukem			fi
3721.1Slukem		done
3731.1Slukem		echo "."
3741.1Slukem	fi
3751.1Slukem
3761.1Slukem	# flush routes
3771.1Slukem	#
3781.1Slukem	route -n flush
3791.1Slukem
3801.1Slukem}
3811.1Slukem
3821.11Slukemload_rc_config $name
3831.1Slukemrun_rc_command "$1"
384