network revision 1.57
11.1Slukem#!/bin/sh
21.1Slukem#
31.57Schristos# $NetBSD: network,v 1.57 2008/10/11 17:28:03 christos 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.45Smycroft$_rc_subr_loaded . /etc/rc.subr
111.1Slukem
121.1Slukemname="network"
131.1Slukemstart_cmd="network_start"
141.14Slukemstop_cmd="network_stop"
151.1Slukem
161.54Sapbnl='
171.54Sapb' # a newline
181.54Sapb
191.1Slukemnetwork_start()
201.1Slukem{
211.1Slukem	# set hostname, turn on network
221.1Slukem	#
231.1Slukem	echo "Starting network."
241.1Slukem
251.1Slukem	# If $hostname is set, use it for my Internet name,
261.1Slukem	# otherwise use /etc/myname
271.1Slukem	#
281.20Snisimura	if [ -z "$hostname" ] && [ -f /etc/myname ]; then
291.46Schristos		hostname=$(cat /etc/myname)
301.1Slukem	fi
311.1Slukem	if [ -n "$hostname" ]; then
321.1Slukem		echo "Hostname: $hostname"
331.1Slukem		hostname $hostname
341.1Slukem	else
351.8Sthorpej		# Don't warn about it if we're going to run
361.8Sthorpej		# DHCP later, as we will probably get the
371.8Sthorpej		# hostname at that time.
381.8Sthorpej		#
391.46Schristos		if ! checkyesno dhclient && [ -z "$(hostname)" ]; then
401.8Sthorpej			warn "\$hostname not set."
411.8Sthorpej		fi
421.1Slukem	fi
431.1Slukem
441.1Slukem	# Check $domainname first, then /etc/defaultdomain,
451.1Slukem	# for NIS/YP domain name
461.1Slukem	#
471.20Snisimura	if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then
481.46Schristos		domainname=$(cat /etc/defaultdomain)
491.1Slukem	fi
501.1Slukem	if [ -n "$domainname" ]; then
511.1Slukem		echo "NIS domainname: $domainname"
521.1Slukem		domainname $domainname
531.1Slukem	fi
541.1Slukem
551.1Slukem	# Flush all routes just to make sure it is clean
561.1Slukem	if checkyesno flushroutes; then
571.53Sreed		/sbin/route -qn flush
581.1Slukem	fi
591.1Slukem
601.1Slukem	# Set the address for the first loopback interface, so that the
611.1Slukem	# auto-route from a newly configured interface's address to lo0
621.1Slukem	# works correctly.
631.1Slukem	#
641.32Slukem	# NOTE: obscure networking problems will occur if lo0 isn't configured.
651.1Slukem	#
661.53Sreed	/sbin/ifconfig lo0 inet 127.0.0.1
671.10Sitojun
681.31Sitojun	# According to RFC1122, 127.0.0.0/8 must not leave the node.
691.10Sitojun	#
701.53Sreed	/sbin/route -q add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
711.1Slukem
721.30Sitojun	# IPv6 routing setups, and host/router mode selection.
731.30Sitojun	#
741.53Sreed	if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then
751.30Sitojun		# We have IPv6 support in kernel.
761.30Sitojun
771.30Sitojun		# disallow link-local unicast dest without outgoing scope
781.30Sitojun		# identifiers.
791.30Sitojun		#
801.53Sreed		/sbin/route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject
811.30Sitojun
821.50Srpaulo		# disallow the use of the RFC3849 documentation address
831.50Srpaulo		#
841.53Sreed		/sbin/route -q add -inet6 2001:db8:: -prefixlen 32 ::1 -reject
851.50Srpaulo
861.50Srpaulo		# IPv6 site-local scoped address prefix (fec0::/10)
871.50Srpaulo		# has been deprecated by RFC3879.
881.30Sitojun		#
891.50Srpaulo		if [ -n "$ip6sitelocal" ]; then
901.50Srpaulo			warn "\$ip6sitelocal is no longer valid"
911.30Sitojun		fi
921.30Sitojun
931.30Sitojun		# disallow "internal" addresses to appear on the wire.
941.30Sitojun		#
951.53Sreed		/sbin/route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
961.30Sitojun
971.30Sitojun		# disallow packets to malicious IPv4 compatible prefix
981.30Sitojun		#
991.53Sreed		/sbin/route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
1001.53Sreed		/sbin/route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
1011.53Sreed		/sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
1021.53Sreed		/sbin/route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
1031.30Sitojun
1041.30Sitojun		# disallow packets to malicious 6to4 prefix
1051.30Sitojun		#
1061.53Sreed		/sbin/route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
1071.53Sreed		/sbin/route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
1081.53Sreed		/sbin/route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
1091.53Sreed		/sbin/route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
1101.30Sitojun
1111.30Sitojun		# Completely disallow packets to IPv4 compatible prefix.
1121.30Sitojun		# This may conflict with RFC1933 under following circumstances:
1131.30Sitojun		# (1) An IPv6-only KAME node tries to originate packets to IPv4
1141.51Sreed		#     compatible destination.  The KAME node has no IPv4
1151.30Sitojun		#     compatible support.  Under RFC1933, it should transmit
1161.30Sitojun		#     native IPv6 packets toward IPv4 compatible destination,
1171.30Sitojun		#     hoping it would reach a router that forwards the packet
1181.30Sitojun		#     toward auto-tunnel interface.
1191.30Sitojun		# (2) An IPv6-only node originates a packet to IPv4 compatible
1201.30Sitojun		#     destination.  A KAME node is acting as an IPv6 router, and
1211.30Sitojun		#     asked to forward it.
1221.30Sitojun		# Due to rare use of IPv4 compatible address, and security
1231.30Sitojun		# issues with it, we disable it by default.
1241.30Sitojun		#
1251.53Sreed		/sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
1261.30Sitojun
1271.53Sreed		/sbin/sysctl -qw net.inet6.ip6.forwarding=0
1281.53Sreed		/sbin/sysctl -qw net.inet6.ip6.accept_rtadv=0
1291.30Sitojun
1301.30Sitojun		case $ip6mode in
1311.30Sitojun		router)
1321.30Sitojun			echo 'IPv6 mode: router'
1331.53Sreed			/sbin/sysctl -qw net.inet6.ip6.forwarding=1
1341.50Srpaulo
1351.50Srpaulo			# disallow unique-local unicast forwarding without
1361.50Srpaulo			# explicit configuration.
1371.50Srpaulo			if ! checkyesno ip6uniquelocal; then
1381.53Sreed				/sbin/route -q add -inet6 fc00:: -prefixlen 7 \
1391.50Srpaulo				    ::1 -reject
1401.50Srpaulo			fi
1411.30Sitojun			;;
1421.30Sitojun
1431.30Sitojun		autohost)
1441.30Sitojun			echo 'IPv6 mode: autoconfigured host'
1451.53Sreed			/sbin/sysctl -qw net.inet6.ip6.accept_rtadv=1
1461.30Sitojun			;;
1471.30Sitojun
1481.30Sitojun		host)	
1491.30Sitojun			echo 'IPv6 mode: host'
1501.30Sitojun			;;
1511.30Sitojun
1521.36Slukem		*)	warn "invalid \$ip6mode value "\"$ip6mode\"
1531.30Sitojun			;;
1541.30Sitojun
1551.30Sitojun		esac
1561.30Sitojun	fi
1571.30Sitojun
1581.1Slukem	# Configure all of the network interfaces listed in $net_interfaces;
1591.1Slukem	# if $auto_ifconfig is YES, grab all interfaces from ifconfig.
1601.1Slukem	# In the following, "xxN" stands in for interface names, like "le0".
1611.54Sapb	#
1621.54Sapb	# For any interfaces that has an $ifconfig_xxN variable
1631.54Sapb	# associated, we break it into lines using ';' as a separator,
1641.54Sapb	# then process it just like the contents of an /etc/ifconfig.xxN
1651.54Sapb	# file.
1661.54Sapb	#
1671.54Sapb	# For each line from the $ifconfig_xxN variable or the
1681.54Sapb	# /etc/ifconfig.xxN file, we ignore comments and blank lines,
1691.54Sapb	# treat lines beginning with "!" as commands to execute, treat
1701.54Sapb	# "dhcp" as a special case to invoke dhcpcd, and for any other
1711.54Sapb	# line we run "ifconfig xxN", using each line of the file as the
1721.54Sapb	# arguments for a separate "ifconfig" invocation.
1731.1Slukem	#
1741.1Slukem	# In order to configure an interface reasonably, you at the very least
1751.1Slukem	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
1761.1Slukem	# and probably a netmask (as in "netmask 0xffffffe0"). You will
1771.1Slukem	# frequently need to specify a media type, as in "media UTP", for
1781.1Slukem	# interface cards with multiple media connections that do not
1791.1Slukem	# autoconfigure. See the ifconfig manual page for details.
1801.1Slukem	#
1811.1Slukem	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
1821.1Slukem	# configuration is possible:
1831.1Slukem	#	inet 10.1.1.1 netmask 0xffffff00
1841.1Slukem	#	inet 10.1.1.2 netmask 0xffffff00 alias
1851.50Srpaulo	#	inet6 2001:db8::1 prefixlen 64 alias
1861.1Slukem	#
1871.29Sitojun	# You can put shell script fragment into /etc/ifconfig.xxN by
1881.29Sitojun	# starting a line with "!".  Refer to ifconfig.if(5) for details.
1891.29Sitojun	#
1901.1Slukem	if [ "$net_interfaces" != NO ]; then
1911.1Slukem		if checkyesno auto_ifconfig; then
1921.53Sreed			tmp=$(/sbin/ifconfig -l)
1931.53Sreed			for cloner in $(/sbin/ifconfig -C 2>/dev/null); do
1941.25Swiz				for int in /etc/ifconfig.${cloner}[0-9]*; do
1951.23Snisimura					[ ! -f $int ] && break
1961.21Slukem					tmp="$tmp ${int##*.}"
1971.15Sthorpej				done
1981.15Sthorpej			done
1991.1Slukem		else
2001.1Slukem			tmp="$net_interfaces"
2011.1Slukem		fi
2021.1Slukem		echo -n 'Configuring network interfaces:'
2031.1Slukem		for int in $tmp; do
2041.54Sapb			eval argslist=\$ifconfig_$int
2051.54Sapb
2061.54Sapb			# Skip interfaces that do not have explicit
2071.54Sapb			# configuration information.  If auto_ifconfig is
2081.54Sapb			# false then also warn about such interfaces.
2091.54Sapb			#
2101.54Sapb			if [ -z "$argslist" ] && ! [ -f /etc/ifconfig.$int ]
2111.54Sapb			then
2121.1Slukem				if ! checkyesno auto_ifconfig; then
2131.1Slukem					echo
2141.1Slukem					warn \
2151.1Slukem			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
2161.1Slukem					warn "interface $int not configured."
2171.1Slukem				fi
2181.1Slukem				continue
2191.1Slukem			fi
2201.54Sapb
2211.54Sapb			echo -n " $int"
2221.54Sapb
2231.54Sapb			# Create the interface if necessary.
2241.54Sapb			# If the interface did not exist before,
2251.54Sapb			# then also resync ipf(4).
2261.54Sapb			#
2271.54Sapb			if /sbin/ifconfig $int create 2>/dev/null && \
2281.54Sapb			   checkyesno ipfilter; then
2291.54Sapb				/sbin/ipf -y >/dev/null
2301.54Sapb			fi
2311.54Sapb
2321.54Sapb			# If $ifconfig_xxN is empty, then use
2331.54Sapb			# /etc/ifconfig.xxN, which we know exists due to
2341.54Sapb			# an earlier test.
2351.54Sapb			#
2361.54Sapb			# If $ifconfig_xxN is non-empty and contains a
2371.54Sapb			# newline, then just use it as is.  (This allows
2381.54Sapb			# semicolons through unmolested.)
2391.54Sapb			#
2401.54Sapb			# If $ifconfig_xxN is non-empty and does not
2411.54Sapb			# contain a newline, then convert all semicolons
2421.54Sapb			# to newlines.
2431.54Sapb			#
2441.54Sapb			case "$argslist" in
2451.54Sapb			'')
2461.54Sapb				cat /etc/ifconfig.$int
2471.54Sapb				;;
2481.54Sapb			*"${nl}"*)
2491.54Sapb				echo "$argslist"
2501.54Sapb				;;
2511.54Sapb			*)
2521.54Sapb				(
2531.54Sapb					set -o noglob
2541.54Sapb					IFS=';'; set -- $argslist
2551.54Sapb					#echo >&2 "[$#] [$1] [$2] [$3] [$4]"
2561.54Sapb					IFS="$nl"; echo "$*"
2571.54Sapb				)
2581.54Sapb				;;
2591.54Sapb			esac |
2601.54Sapb			while read -r args; do
2611.54Sapb				case "$args" in
2621.54Sapb				''|"#"*|create)
2631.54Sapb					;;
2641.54Sapb				"!"*)
2651.54Sapb					# Run arbitrary command in a subshell.
2661.54Sapb					( eval "${args#*!}" )
2671.54Sapb					;;
2681.54Sapb				dhcp)
2691.54Sapb					/sbin/dhcpcd -n ${dhcpcd_flags} $int
2701.54Sapb					;;
2711.54Sapb				*)
2721.54Sapb					# Pass args to ifconfig.  Note
2731.54Sapb					# that args may contain embedded
2741.54Sapb					# shell metacharacters, such as
2751.57Schristos					# "ssid 'foo;*>bar'". We eval
2761.57Schristos					# one more time so that things
2771.57Schristos					# like ssid "Columbia University" work.
2781.54Sapb					(
2791.54Sapb						set -o noglob
2801.57Schristos						eval set -- $args
2811.54Sapb						#echo >&2 "[$#] [$1] [$2] [$3]"
2821.54Sapb						/sbin/ifconfig $int "$@"
2831.54Sapb					)
2841.54Sapb					;;
2851.54Sapb				esac
2861.54Sapb			done
2871.1Slukem			configured_interfaces="$configured_interfaces $int"
2881.1Slukem		done
2891.1Slukem		echo "."
2901.1Slukem	fi
2911.1Slukem
2921.48Scjs	echo -n "Adding interface aliases:"
2931.48Scjs
2941.1Slukem	# Check if each configured interface xxN has an $ifaliases_xxN variable
2951.1Slukem	# associated, then configure additional IP addresses for that interface.
2961.1Slukem	# The variable contains a list of "address netmask" pairs, with
2971.1Slukem	# "netmask" set to "-" if the interface default netmask is to be used.
2981.1Slukem	#
2991.54Sapb	# Note that $ifaliases_xxN works only in certain cases and its
3001.54Sapb	# use is not recommended.  Use /etc/ifconfig.xxN or multiple
3011.54Sapb	# commands in $ifconfig_xxN instead.
3021.1Slukem	#
3031.48Scjs	for int in lo0 $configured_interfaces; do
3041.20Snisimura		eval args=\$ifaliases_$int
3051.1Slukem		if [ -n "$args" ]; then
3061.1Slukem			set -- $args
3071.1Slukem			while [ $# -ge 2 ]; do
3081.1Slukem				addr=$1 ; net=$2 ; shift 2
3091.1Slukem				if [ "$net" = "-" ]; then
3101.16Sjdolecek					# for compatibility only, obsolete
3111.53Sreed					/sbin/ifconfig $int inet alias $addr
3121.1Slukem				else
3131.53Sreed					/sbin/ifconfig $int inet alias $addr \
3141.1Slukem					    netmask $net
3151.1Slukem				fi
3161.48Scjs				echo -n " $int:$addr"
3171.1Slukem			done
3181.1Slukem		fi
3191.1Slukem	done
3201.1Slukem
3211.1Slukem	# /etc/ifaliases, if it exists, contains the names of additional IP
3221.1Slukem	# addresses for each interface. It is formatted as a series of lines
3231.1Slukem	# that contain
3241.1Slukem	#	address interface netmask
3251.1Slukem	#
3261.54Sapb	# Note that /etc/ifaliases works only in certain cases and its
3271.54Sapb	# use is not recommended.  Use /etc/ifconfig.xxN or multiple
3281.54Sapb	# commands in $ifconfig_xxN instead.
3291.1Slukem	#
3301.1Slukem	if [ -f /etc/ifaliases ]; then
3311.1Slukem		while read addr int net; do
3321.1Slukem			if [ -z "$net" ]; then
3331.16Sjdolecek				# for compatibility only, obsolete
3341.53Sreed				/sbin/ifconfig $int inet alias $addr
3351.1Slukem			else
3361.53Sreed				/sbin/ifconfig $int inet alias $addr netmask $net
3371.1Slukem			fi
3381.20Snisimura		done < /etc/ifaliases
3391.1Slukem	fi
3401.1Slukem
3411.56Sapb	echo "." # for "Adding interface aliases:"
3421.56Sapb
3431.56Sapb	# Check $defaultroute, then /etc/mygate, for the name or address
3441.56Sapb	# of my IPv4 gateway host. If using a name, that name must be in
3451.56Sapb	# /etc/hosts.
3461.56Sapb	#
3471.56Sapb	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
3481.56Sapb		defaultroute=$(cat /etc/mygate)
3491.56Sapb	fi
3501.56Sapb	if [ -n "$defaultroute" ]; then
3511.56Sapb		/sbin/route add default $defaultroute
3521.56Sapb	fi
3531.56Sapb
3541.56Sapb	# Check $defaultroute6, then /etc/mygate6, for the name or address
3551.56Sapb	# of my IPv6 gateway host. If using a name, that name must be in
3561.56Sapb	# /etc/hosts.  Note that the gateway host address must be a link-local
3571.56Sapb	# address if it is not using an stf* interface.
3581.56Sapb	#
3591.56Sapb	if [ -z "$defaultroute6" ] && [ -f /etc/mygate6 ]; then
3601.56Sapb		defaultroute6=$(cat /etc/mygate6)
3611.56Sapb	fi
3621.56Sapb	if [ -n "$defaultroute6" ]; then
3631.56Sapb		if [ "$ip6mode" = "autohost" ]; then
3641.56Sapb			echo
3651.56Sapb			warn \
3661.56Sapb	    "ip6mode is set to 'autohost' and a v6 default route is also set."
3671.56Sapb		fi
3681.56Sapb		/sbin/route add -inet6 default $defaultroute6
3691.56Sapb	fi
3701.48Scjs
3711.30Sitojun	# IPv6 interface autoconfiguration.
3721.1Slukem	#
3731.53Sreed	if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then
3741.17Sitojun		# wait till DAD is completed. always invoke it in case
3751.17Sitojun		# if are configured manually by ifconfig
3761.17Sitojun		#
3771.53Sreed		dadcount=$(/sbin/sysctl -n net.inet6.ip6.dad_count 2>/dev/null)
3781.17Sitojun		sleep $dadcount
3791.17Sitojun		sleep 1
3801.17Sitojun
3811.1Slukem		if checkyesno rtsol; then
3821.1Slukem			if [ "$ip6mode" = "autohost" ]; then
3831.1Slukem				echo 'Sending router solicitation...'
3841.53Sreed				/sbin/rtsol $rtsol_flags
3851.1Slukem			else
3861.1Slukem				echo
3871.1Slukem				warn \
3881.1Slukem			    "ip6mode must be set to 'autohost' to use rtsol."
3891.1Slukem			fi
3901.17Sitojun
3911.18Sitojun			# wait till DAD is completed, for global addresses
3921.18Sitojun			# configured by router advert message.
3931.17Sitojun			#
3941.17Sitojun			sleep $dadcount
3951.17Sitojun			sleep 1
3961.1Slukem		fi
3971.42Schristos	fi
3981.42Schristos
3991.1Slukem	# XXX this must die
4001.1Slukem	if [ -s /etc/netstart.local ]; then
4011.1Slukem		sh /etc/netstart.local start
4021.1Slukem	fi
4031.1Slukem}
4041.1Slukem
4051.1Slukemnetwork_stop()
4061.1Slukem{
4071.1Slukem	echo "Stopping network."
4081.1Slukem
4091.1Slukem	# XXX this must die
4101.1Slukem	if [ -s /etc/netstart.local ]; then
4111.1Slukem		sh /etc/netstart.local stop
4121.1Slukem	fi
4131.1Slukem
4141.1Slukem	echo "Deleting aliases."
4151.1Slukem	if [ -f /etc/ifaliases ]; then
4161.1Slukem		while read addr int net; do
4171.53Sreed			/sbin/ifconfig $int inet delete $addr
4181.20Snisimura		done < /etc/ifaliases
4191.1Slukem	fi
4201.1Slukem
4211.53Sreed	for int in $(/sbin/ifconfig -lu); do
4221.20Snisimura		eval args=\$ifaliases_$int
4231.1Slukem		if [ -n "$args" ]; then
4241.1Slukem			set -- $args
4251.1Slukem			while [ $# -ge 2 ]; do
4261.1Slukem				addr=$1 ; net=$2 ; shift 2
4271.53Sreed				/sbin/ifconfig $int inet delete $addr
4281.1Slukem			done
4291.1Slukem		fi
4301.1Slukem	done
4311.1Slukem
4321.1Slukem	# down interfaces
4331.1Slukem	#
4341.1Slukem	echo -n 'Downing network interfaces:'
4351.1Slukem	if [ "$net_interfaces" != NO ]; then
4361.1Slukem		if checkyesno auto_ifconfig; then
4371.53Sreed			tmp=$(/sbin/ifconfig -l)
4381.1Slukem		else
4391.1Slukem			tmp="$net_interfaces"
4401.1Slukem		fi
4411.1Slukem		for int in $tmp; do
4421.20Snisimura			eval args=\$ifconfig_$int
4431.2Sveego			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
4441.1Slukem				echo -n " $int"
4451.53Sreed				/sbin/dhcpcd -k $int 2> /dev/null
4461.53Sreed				/sbin/ifconfig $int down
4471.53Sreed				if /sbin/ifconfig $int destroy 2>/dev/null && \
4481.39Stron				   checkyesno ipfilter; then
4491.39Stron					# resync ipf(4)
4501.53Sreed					/sbin/ipf -y >/dev/null
4511.39Stron				fi
4521.1Slukem			fi
4531.1Slukem		done
4541.1Slukem		echo "."
4551.1Slukem	fi
4561.1Slukem
4571.1Slukem	# flush routes
4581.1Slukem	#
4591.53Sreed	/sbin/route -qn flush
4601.38Stron
4611.1Slukem}
4621.1Slukem
4631.47Slukemload_rc_config $name
4641.47Slukemload_rc_config_var dhclient dhclient
4651.47Slukemload_rc_config_var ipfilter ipfilter
4661.1Slukemrun_rc_command "$1"
467