network revision 1.36
11.1Slukem#!/bin/sh
21.1Slukem#
31.36Slukem# $NetBSD: network,v 1.36 2002/03/27 07:37:56 lukem Exp $
41.1Slukem#
51.1Slukem
61.1Slukem# PROVIDE: network
71.19Slukem# REQUIRE: ipfilter ipsec mountcritlocal root tty sysctl
81.34Sthorpej# BEFORE:  NETWORKING
91.1Slukem
101.1Slukem. /etc/rc.subr
111.1Slukem
121.1Slukemname="network"
131.1Slukemstart_cmd="network_start"
141.14Slukemstop_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.20Snisimura	if [ -z "$hostname" ] && [ -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.8Sthorpej		# Don't warn about it if we're going to run
331.8Sthorpej		# DHCP later, as we will probably get the
341.8Sthorpej		# hostname at that time.
351.8Sthorpej		#
361.27Snisimura		if ! checkyesno dhclient && [ -z "`hostname`" ]; then
371.8Sthorpej			warn "\$hostname not set."
381.8Sthorpej		fi
391.1Slukem	fi
401.1Slukem
411.1Slukem	# Check $domainname first, then /etc/defaultdomain,
421.1Slukem	# for NIS/YP domain name
431.1Slukem	#
441.20Snisimura	if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then
451.1Slukem		domainname=`cat /etc/defaultdomain`
461.1Slukem	fi
471.1Slukem	if [ -n "$domainname" ]; then
481.1Slukem		echo "NIS domainname: $domainname"
491.1Slukem		domainname $domainname
501.1Slukem	fi
511.1Slukem
521.1Slukem	# Flush all routes just to make sure it is clean
531.1Slukem	if checkyesno flushroutes; then
541.1Slukem		route -n flush
551.1Slukem	fi
561.1Slukem
571.1Slukem	# Set the address for the first loopback interface, so that the
581.1Slukem	# auto-route from a newly configured interface's address to lo0
591.1Slukem	# works correctly.
601.1Slukem	#
611.32Slukem	# NOTE: obscure networking problems will occur if lo0 isn't configured.
621.1Slukem	#
631.1Slukem	ifconfig lo0 inet 127.0.0.1
641.10Sitojun
651.31Sitojun	# According to RFC1122, 127.0.0.0/8 must not leave the node.
661.10Sitojun	#
671.35Ssommerfe	route -q add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
681.1Slukem
691.30Sitojun	# IPv6 routing setups, and host/router mode selection.
701.30Sitojun	#
711.30Sitojun	if ifconfig lo0 inet6 >/dev/null 2>&1; then
721.30Sitojun		# We have IPv6 support in kernel.
731.30Sitojun
741.30Sitojun		# disallow link-local unicast dest without outgoing scope
751.30Sitojun		# identifiers.
761.30Sitojun		#
771.35Ssommerfe		route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject
781.30Sitojun
791.30Sitojun		# disallow site-local unicast dest without outgoing scope
801.30Sitojun		# identifiers.
811.30Sitojun		# If you configure site-locals without scope id (it is
821.30Sitojun		# permissible config for routers that are not on scope
831.30Sitojun		# boundary), you may want to comment the following one out.
841.30Sitojun		#
851.30Sitojun		if ! checkyesno ip6sitelocal; then
861.35Ssommerfe			route -q add -inet6 fec0:: -prefixlen 10 ::1 -reject
871.30Sitojun		fi
881.30Sitojun
891.30Sitojun		# disallow "internal" addresses to appear on the wire.
901.30Sitojun		#
911.35Ssommerfe		route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
921.30Sitojun
931.30Sitojun		# disallow packets to malicious IPv4 compatible prefix
941.30Sitojun		#
951.35Ssommerfe		route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
961.35Ssommerfe		route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
971.35Ssommerfe		route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
981.35Ssommerfe		route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
991.30Sitojun
1001.30Sitojun		# disallow packets to malicious 6to4 prefix
1011.30Sitojun		#
1021.35Ssommerfe		route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
1031.35Ssommerfe		route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
1041.35Ssommerfe		route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
1051.35Ssommerfe		route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
1061.30Sitojun
1071.30Sitojun		# Completely disallow packets to IPv4 compatible prefix.
1081.30Sitojun		# This may conflict with RFC1933 under following circumstances:
1091.30Sitojun		# (1) An IPv6-only KAME node tries to originate packets to IPv4
1101.30Sitojun		#     comatible destination.  The KAME node has no IPv4
1111.30Sitojun		#     compatible support.  Under RFC1933, it should transmit
1121.30Sitojun		#     native IPv6 packets toward IPv4 compatible destination,
1131.30Sitojun		#     hoping it would reach a router that forwards the packet
1141.30Sitojun		#     toward auto-tunnel interface.
1151.30Sitojun		# (2) An IPv6-only node originates a packet to IPv4 compatible
1161.30Sitojun		#     destination.  A KAME node is acting as an IPv6 router, and
1171.30Sitojun		#     asked to forward it.
1181.30Sitojun		# Due to rare use of IPv4 compatible address, and security
1191.30Sitojun		# issues with it, we disable it by default.
1201.30Sitojun		#
1211.35Ssommerfe		route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
1221.30Sitojun
1231.35Ssommerfe		sysctl -qw net.inet6.ip6.forwarding=0
1241.35Ssommerfe		sysctl -qw net.inet6.ip6.accept_rtadv=0
1251.30Sitojun
1261.30Sitojun		# backward compatibility
1271.30Sitojun		#
1281.30Sitojun		if [ -z "$ip6mode" ] && [ -n "$ip6forwarding" ]; then
1291.30Sitojun			warn 'Please migrate to newer rc.conf' \
1301.30Sitojun			    '(use ip6mode, not ip6forwarding)'
1311.30Sitojun			if checkyesno ip6forwarding; then
1321.30Sitojun				ip6mode=router
1331.30Sitojun			elif checkyesno rtsol; then
1341.30Sitojun				ip6mode=autohost
1351.30Sitojun			else
1361.30Sitojun				ip6mode=host
1371.30Sitojun			fi
1381.30Sitojun		fi
1391.30Sitojun
1401.30Sitojun		case $ip6mode in
1411.30Sitojun		router)
1421.30Sitojun			echo 'IPv6 mode: router'
1431.35Ssommerfe			sysctl -qw net.inet6.ip6.forwarding=1
1441.30Sitojun			;;
1451.30Sitojun
1461.30Sitojun		autohost)
1471.30Sitojun			echo 'IPv6 mode: autoconfigured host'
1481.35Ssommerfe			sysctl -qw net.inet6.ip6.accept_rtadv=1
1491.30Sitojun			;;
1501.30Sitojun
1511.30Sitojun		host)	
1521.30Sitojun			echo 'IPv6 mode: host'
1531.30Sitojun			;;
1541.30Sitojun
1551.36Slukem		*)	warn "invalid \$ip6mode value "\"$ip6mode\"
1561.30Sitojun			;;
1571.30Sitojun
1581.30Sitojun		esac
1591.30Sitojun	fi
1601.30Sitojun
1611.1Slukem	# Configure all of the network interfaces listed in $net_interfaces;
1621.1Slukem	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
1631.1Slukem	# In the following, "xxN" stands in for interface names, like "le0".
1641.1Slukem	# For any interfaces that has an $ifconfig_xxN variable associated,
1651.1Slukem	# we do "ifconfig xxN $ifconfig_xxN".
1661.1Slukem	# If there is no such variable, we take the contents of the file
1671.1Slukem	# /etc/ifconfig.xxN, and run "ifconfig xxN" repeatedly, using each
1681.33Swiz	# line of the file as the arguments for a separate "ifconfig"
1691.1Slukem	# invocation.
1701.1Slukem	#
1711.1Slukem	# In order to configure an interface reasonably, you at the very least
1721.1Slukem	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
1731.1Slukem	# and probably a netmask (as in "netmask 0xffffffe0"). You will
1741.1Slukem	# frequently need to specify a media type, as in "media UTP", for
1751.1Slukem	# interface cards with multiple media connections that do not
1761.1Slukem	# autoconfigure. See the ifconfig manual page for details.
1771.1Slukem	#
1781.1Slukem	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
1791.1Slukem	# configuration is possible:
1801.1Slukem	#	inet 10.1.1.1 netmask 0xffffff00
1811.1Slukem	#	inet 10.1.1.2 netmask 0xffffff00 alias
1821.1Slukem	#	inet6 fec0::1 prefixlen 64 alias
1831.1Slukem	#
1841.29Sitojun	# You can put shell script fragment into /etc/ifconfig.xxN by
1851.29Sitojun	# starting a line with "!".  Refer to ifconfig.if(5) for details.
1861.29Sitojun	#
1871.1Slukem	if [ "$net_interfaces" != NO ]; then
1881.1Slukem		if checkyesno auto_ifconfig; then
1891.22Snisimura			tmp=`ifconfig -l`
1901.15Sthorpej			for cloner in `ifconfig -C 2>/dev/null`; do
1911.25Swiz				for int in /etc/ifconfig.${cloner}[0-9]*; do
1921.23Snisimura					[ ! -f $int ] && break
1931.21Slukem					tmp="$tmp ${int##*.}"
1941.15Sthorpej				done
1951.15Sthorpej			done
1961.1Slukem		else
1971.1Slukem			tmp="$net_interfaces"
1981.1Slukem		fi
1991.1Slukem		echo -n 'Configuring network interfaces:'
2001.1Slukem		for int in $tmp; do
2011.20Snisimura			eval args=\$ifconfig_$int
2021.24Slukem			if [ -n "$args" ]; then
2031.1Slukem				echo -n " $int"
2041.1Slukem				ifconfig $int $args
2051.1Slukem			elif [ -f /etc/ifconfig.$int ]; then
2061.1Slukem				echo -n " $int"
2071.20Snisimura				while read args; do
2081.26Snisimura					[ -z "$args" ] && continue
2091.29Sitojun					case "$args" in
2101.29Sitojun					"#"*)
2111.29Sitojun						;;
2121.29Sitojun					"!"*)
2131.29Sitojun						eval ${args#*!}
2141.29Sitojun						;;
2151.29Sitojun					*)
2161.29Sitojun						ifconfig $int $args
2171.29Sitojun						;;
2181.29Sitojun					esac
2191.20Snisimura				done < /etc/ifconfig.$int
2201.1Slukem			else
2211.1Slukem				if ! checkyesno auto_ifconfig; then
2221.1Slukem					echo
2231.1Slukem					warn \
2241.1Slukem			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
2251.1Slukem					warn "interface $int not configured."
2261.1Slukem				fi
2271.1Slukem				continue
2281.1Slukem			fi
2291.1Slukem			configured_interfaces="$configured_interfaces $int"
2301.1Slukem		done
2311.1Slukem		echo "."
2321.1Slukem	fi
2331.1Slukem
2341.1Slukem	# Check $defaultroute, then /etc/mygate, for the name of my gateway
2351.1Slukem	# host. That name must be in /etc/hosts.
2361.1Slukem	#
2371.20Snisimura	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
2381.1Slukem		defaultroute=`cat /etc/mygate`
2391.1Slukem	fi
2401.1Slukem	if [ -n "$defaultroute" ]; then
2411.1Slukem		route add default $defaultroute
2421.1Slukem	fi
2431.1Slukem
2441.1Slukem	# Check if each configured interface xxN has an $ifaliases_xxN variable
2451.1Slukem	# associated, then configure additional IP addresses for that interface.
2461.1Slukem	# The variable contains a list of "address netmask" pairs, with
2471.1Slukem	# "netmask" set to "-" if the interface default netmask is to be used.
2481.1Slukem	#
2491.1Slukem	# Note that $ifaliases_xxN works only with certain configurations and
2501.1Slukem	# considered not recommended.  Use /etc/ifconfig.xxN if possible.
2511.1Slukem	# 
2521.1Slukem	#
2531.1Slukem	if [ -n "$configured_interfaces" ]; then
2541.1Slukem		echo "Adding interface aliases:"
2551.1Slukem		done_aliases_message=yes
2561.1Slukem	fi
2571.1Slukem	for int in $configured_interfaces; do
2581.20Snisimura		eval args=\$ifaliases_$int
2591.1Slukem		if [ -n "$args" ]; then
2601.1Slukem			set -- $args
2611.1Slukem			while [ $# -ge 2 ]; do
2621.1Slukem				addr=$1 ; net=$2 ; shift 2
2631.1Slukem				if [ "$net" = "-" ]; then
2641.16Sjdolecek					# for compatibility only, obsolete
2651.1Slukem					ifconfig $int inet alias $addr
2661.1Slukem				else
2671.1Slukem					ifconfig $int inet alias $addr \
2681.1Slukem					    netmask $net
2691.1Slukem				fi
2701.1Slukem			done
2711.1Slukem		fi
2721.1Slukem	done
2731.1Slukem
2741.1Slukem	# /etc/ifaliases, if it exists, contains the names of additional IP
2751.1Slukem	# addresses for each interface. It is formatted as a series of lines
2761.1Slukem	# that contain
2771.1Slukem	#	address interface netmask
2781.1Slukem	#
2791.1Slukem	# Note that /etc/ifaliases works only with certain cases only and its
2801.1Slukem	# use is not recommended.  Use /etc/ifconfig.xxN instead.
2811.1Slukem	#
2821.1Slukem	#
2831.1Slukem	if [ -f /etc/ifaliases ]; then
2841.1Slukem		if [ "$done_aliases_message" != yes ]; then
2851.1Slukem			echo "Adding interface aliases:"
2861.1Slukem		fi
2871.1Slukem		while read addr int net; do
2881.1Slukem			if [ -z "$net" ]; then
2891.16Sjdolecek				# for compatibility only, obsolete
2901.1Slukem				ifconfig $int inet alias $addr
2911.1Slukem			else
2921.1Slukem				ifconfig $int inet alias $addr netmask $net
2931.1Slukem			fi
2941.20Snisimura		done < /etc/ifaliases
2951.1Slukem	fi
2961.1Slukem
2971.30Sitojun	# IPv6 interface autoconfiguration.
2981.1Slukem	#
2991.1Slukem	if ifconfig lo0 inet6 >/dev/null 2>&1; then
3001.17Sitojun		# wait till DAD is completed. always invoke it in case
3011.17Sitojun		# if are configured manually by ifconfig
3021.17Sitojun		#
3031.17Sitojun		dadcount=`sysctl -n net.inet6.ip6.dad_count 2>/dev/null`
3041.17Sitojun		sleep $dadcount
3051.17Sitojun		sleep 1
3061.17Sitojun
3071.1Slukem		if checkyesno rtsol; then
3081.1Slukem			if [ "$ip6mode" = "autohost" ]; then
3091.1Slukem				echo 'Sending router solicitation...'
3101.1Slukem				rtsol $rtsol_flags
3111.1Slukem			else
3121.1Slukem				echo
3131.1Slukem				warn \
3141.1Slukem			    "ip6mode must be set to 'autohost' to use rtsol."
3151.1Slukem			fi
3161.17Sitojun
3171.18Sitojun			# wait till DAD is completed, for global addresses
3181.18Sitojun			# configured by router advert message.
3191.17Sitojun			#
3201.17Sitojun			sleep $dadcount
3211.17Sitojun			sleep 1
3221.1Slukem		fi
3231.1Slukem	fi
3241.1Slukem
3251.1Slukem	# XXX this must die
3261.1Slukem	if [ -s /etc/netstart.local ]; then
3271.1Slukem		sh /etc/netstart.local start
3281.1Slukem	fi
3291.1Slukem}
3301.1Slukem
3311.1Slukemnetwork_stop()
3321.1Slukem{
3331.1Slukem	echo "Stopping network."
3341.1Slukem
3351.1Slukem	# XXX this must die
3361.1Slukem	if [ -s /etc/netstart.local ]; then
3371.1Slukem		sh /etc/netstart.local stop
3381.1Slukem	fi
3391.1Slukem
3401.1Slukem	echo "Deleting aliases."
3411.1Slukem	if [ -f /etc/ifaliases ]; then
3421.1Slukem		while read addr int net; do
3431.1Slukem			ifconfig $int inet delete $addr
3441.20Snisimura		done < /etc/ifaliases
3451.1Slukem	fi
3461.1Slukem
3471.21Slukem	for int in `ifconfig -lu`; do
3481.20Snisimura		eval args=\$ifaliases_$int
3491.1Slukem		if [ -n "$args" ]; then
3501.1Slukem			set -- $args
3511.1Slukem			while [ $# -ge 2 ]; do
3521.1Slukem				addr=$1 ; net=$2 ; shift 2
3531.1Slukem				ifconfig $int inet delete $addr
3541.1Slukem			done
3551.1Slukem		fi
3561.1Slukem	done
3571.1Slukem
3581.1Slukem	# down interfaces
3591.1Slukem	#
3601.1Slukem	echo -n 'Downing network interfaces:'
3611.1Slukem	if [ "$net_interfaces" != NO ]; then
3621.1Slukem		if checkyesno auto_ifconfig; then
3631.22Snisimura			tmp=`ifconfig -l`
3641.1Slukem		else
3651.1Slukem			tmp="$net_interfaces"
3661.1Slukem		fi
3671.1Slukem		for int in $tmp; do
3681.20Snisimura			eval args=\$ifconfig_$int
3691.2Sveego			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
3701.1Slukem				echo -n " $int"
3711.1Slukem				ifconfig $int down
3721.1Slukem			fi
3731.1Slukem		done
3741.1Slukem		echo "."
3751.1Slukem	fi
3761.1Slukem
3771.1Slukem	# flush routes
3781.1Slukem	#
3791.1Slukem	route -n flush
3801.1Slukem
3811.1Slukem}
3821.1Slukem
3831.11Slukemload_rc_config $name
3841.1Slukemrun_rc_command "$1"
385