network revision 1.56
11.1Slukem#!/bin/sh
21.1Slukem#
31.56Sapb# $NetBSD: network,v 1.56 2008/09/26 10:31:46 apb 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.54Sapb					# "ssid 'foo;*>bar'".
2761.54Sapb					(
2771.54Sapb						set -o noglob
2781.55Sapb						set -- $args
2791.54Sapb						#echo >&2 "[$#] [$1] [$2] [$3]"
2801.54Sapb						/sbin/ifconfig $int "$@"
2811.54Sapb					)
2821.54Sapb					;;
2831.54Sapb				esac
2841.54Sapb			done
2851.1Slukem			configured_interfaces="$configured_interfaces $int"
2861.1Slukem		done
2871.1Slukem		echo "."
2881.1Slukem	fi
2891.1Slukem
2901.48Scjs	echo -n "Adding interface aliases:"
2911.48Scjs
2921.1Slukem	# Check if each configured interface xxN has an $ifaliases_xxN variable
2931.1Slukem	# associated, then configure additional IP addresses for that interface.
2941.1Slukem	# The variable contains a list of "address netmask" pairs, with
2951.1Slukem	# "netmask" set to "-" if the interface default netmask is to be used.
2961.1Slukem	#
2971.54Sapb	# Note that $ifaliases_xxN works only in certain cases and its
2981.54Sapb	# use is not recommended.  Use /etc/ifconfig.xxN or multiple
2991.54Sapb	# commands in $ifconfig_xxN instead.
3001.1Slukem	#
3011.48Scjs	for int in lo0 $configured_interfaces; do
3021.20Snisimura		eval args=\$ifaliases_$int
3031.1Slukem		if [ -n "$args" ]; then
3041.1Slukem			set -- $args
3051.1Slukem			while [ $# -ge 2 ]; do
3061.1Slukem				addr=$1 ; net=$2 ; shift 2
3071.1Slukem				if [ "$net" = "-" ]; then
3081.16Sjdolecek					# for compatibility only, obsolete
3091.53Sreed					/sbin/ifconfig $int inet alias $addr
3101.1Slukem				else
3111.53Sreed					/sbin/ifconfig $int inet alias $addr \
3121.1Slukem					    netmask $net
3131.1Slukem				fi
3141.48Scjs				echo -n " $int:$addr"
3151.1Slukem			done
3161.1Slukem		fi
3171.1Slukem	done
3181.1Slukem
3191.1Slukem	# /etc/ifaliases, if it exists, contains the names of additional IP
3201.1Slukem	# addresses for each interface. It is formatted as a series of lines
3211.1Slukem	# that contain
3221.1Slukem	#	address interface netmask
3231.1Slukem	#
3241.54Sapb	# Note that /etc/ifaliases works only in certain cases and its
3251.54Sapb	# use is not recommended.  Use /etc/ifconfig.xxN or multiple
3261.54Sapb	# commands in $ifconfig_xxN instead.
3271.1Slukem	#
3281.1Slukem	if [ -f /etc/ifaliases ]; then
3291.1Slukem		while read addr int net; do
3301.1Slukem			if [ -z "$net" ]; then
3311.16Sjdolecek				# for compatibility only, obsolete
3321.53Sreed				/sbin/ifconfig $int inet alias $addr
3331.1Slukem			else
3341.53Sreed				/sbin/ifconfig $int inet alias $addr netmask $net
3351.1Slukem			fi
3361.20Snisimura		done < /etc/ifaliases
3371.1Slukem	fi
3381.1Slukem
3391.56Sapb	echo "." # for "Adding interface aliases:"
3401.56Sapb
3411.56Sapb	# Check $defaultroute, then /etc/mygate, for the name or address
3421.56Sapb	# of my IPv4 gateway host. If using a name, that name must be in
3431.56Sapb	# /etc/hosts.
3441.56Sapb	#
3451.56Sapb	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
3461.56Sapb		defaultroute=$(cat /etc/mygate)
3471.56Sapb	fi
3481.56Sapb	if [ -n "$defaultroute" ]; then
3491.56Sapb		/sbin/route add default $defaultroute
3501.56Sapb	fi
3511.56Sapb
3521.56Sapb	# Check $defaultroute6, then /etc/mygate6, for the name or address
3531.56Sapb	# of my IPv6 gateway host. If using a name, that name must be in
3541.56Sapb	# /etc/hosts.  Note that the gateway host address must be a link-local
3551.56Sapb	# address if it is not using an stf* interface.
3561.56Sapb	#
3571.56Sapb	if [ -z "$defaultroute6" ] && [ -f /etc/mygate6 ]; then
3581.56Sapb		defaultroute6=$(cat /etc/mygate6)
3591.56Sapb	fi
3601.56Sapb	if [ -n "$defaultroute6" ]; then
3611.56Sapb		if [ "$ip6mode" = "autohost" ]; then
3621.56Sapb			echo
3631.56Sapb			warn \
3641.56Sapb	    "ip6mode is set to 'autohost' and a v6 default route is also set."
3651.56Sapb		fi
3661.56Sapb		/sbin/route add -inet6 default $defaultroute6
3671.56Sapb	fi
3681.48Scjs
3691.30Sitojun	# IPv6 interface autoconfiguration.
3701.1Slukem	#
3711.53Sreed	if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then
3721.17Sitojun		# wait till DAD is completed. always invoke it in case
3731.17Sitojun		# if are configured manually by ifconfig
3741.17Sitojun		#
3751.53Sreed		dadcount=$(/sbin/sysctl -n net.inet6.ip6.dad_count 2>/dev/null)
3761.17Sitojun		sleep $dadcount
3771.17Sitojun		sleep 1
3781.17Sitojun
3791.1Slukem		if checkyesno rtsol; then
3801.1Slukem			if [ "$ip6mode" = "autohost" ]; then
3811.1Slukem				echo 'Sending router solicitation...'
3821.53Sreed				/sbin/rtsol $rtsol_flags
3831.1Slukem			else
3841.1Slukem				echo
3851.1Slukem				warn \
3861.1Slukem			    "ip6mode must be set to 'autohost' to use rtsol."
3871.1Slukem			fi
3881.17Sitojun
3891.18Sitojun			# wait till DAD is completed, for global addresses
3901.18Sitojun			# configured by router advert message.
3911.17Sitojun			#
3921.17Sitojun			sleep $dadcount
3931.17Sitojun			sleep 1
3941.1Slukem		fi
3951.42Schristos	fi
3961.42Schristos
3971.1Slukem	# XXX this must die
3981.1Slukem	if [ -s /etc/netstart.local ]; then
3991.1Slukem		sh /etc/netstart.local start
4001.1Slukem	fi
4011.1Slukem}
4021.1Slukem
4031.1Slukemnetwork_stop()
4041.1Slukem{
4051.1Slukem	echo "Stopping network."
4061.1Slukem
4071.1Slukem	# XXX this must die
4081.1Slukem	if [ -s /etc/netstart.local ]; then
4091.1Slukem		sh /etc/netstart.local stop
4101.1Slukem	fi
4111.1Slukem
4121.1Slukem	echo "Deleting aliases."
4131.1Slukem	if [ -f /etc/ifaliases ]; then
4141.1Slukem		while read addr int net; do
4151.53Sreed			/sbin/ifconfig $int inet delete $addr
4161.20Snisimura		done < /etc/ifaliases
4171.1Slukem	fi
4181.1Slukem
4191.53Sreed	for int in $(/sbin/ifconfig -lu); do
4201.20Snisimura		eval args=\$ifaliases_$int
4211.1Slukem		if [ -n "$args" ]; then
4221.1Slukem			set -- $args
4231.1Slukem			while [ $# -ge 2 ]; do
4241.1Slukem				addr=$1 ; net=$2 ; shift 2
4251.53Sreed				/sbin/ifconfig $int inet delete $addr
4261.1Slukem			done
4271.1Slukem		fi
4281.1Slukem	done
4291.1Slukem
4301.1Slukem	# down interfaces
4311.1Slukem	#
4321.1Slukem	echo -n 'Downing network interfaces:'
4331.1Slukem	if [ "$net_interfaces" != NO ]; then
4341.1Slukem		if checkyesno auto_ifconfig; then
4351.53Sreed			tmp=$(/sbin/ifconfig -l)
4361.1Slukem		else
4371.1Slukem			tmp="$net_interfaces"
4381.1Slukem		fi
4391.1Slukem		for int in $tmp; do
4401.20Snisimura			eval args=\$ifconfig_$int
4411.2Sveego			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
4421.1Slukem				echo -n " $int"
4431.53Sreed				/sbin/dhcpcd -k $int 2> /dev/null
4441.53Sreed				/sbin/ifconfig $int down
4451.53Sreed				if /sbin/ifconfig $int destroy 2>/dev/null && \
4461.39Stron				   checkyesno ipfilter; then
4471.39Stron					# resync ipf(4)
4481.53Sreed					/sbin/ipf -y >/dev/null
4491.39Stron				fi
4501.1Slukem			fi
4511.1Slukem		done
4521.1Slukem		echo "."
4531.1Slukem	fi
4541.1Slukem
4551.1Slukem	# flush routes
4561.1Slukem	#
4571.53Sreed	/sbin/route -qn flush
4581.38Stron
4591.1Slukem}
4601.1Slukem
4611.47Slukemload_rc_config $name
4621.47Slukemload_rc_config_var dhclient dhclient
4631.47Slukemload_rc_config_var ipfilter ipfilter
4641.1Slukemrun_rc_command "$1"
465