network revision 1.61
11.1Slukem#!/bin/sh
21.1Slukem#
31.61Sapb# $NetBSD: network,v 1.61 2010/09/26 18:52:04 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.58Sroy		if ! checkyesno dhclient && ! checkyesno dhcpcd && \
401.58Sroy			[ -z "$(hostname)" ]
411.58Sroy		then
421.8Sthorpej			warn "\$hostname not set."
431.8Sthorpej		fi
441.1Slukem	fi
451.1Slukem
461.1Slukem	# Check $domainname first, then /etc/defaultdomain,
471.1Slukem	# for NIS/YP domain name
481.1Slukem	#
491.20Snisimura	if [ -z "$domainname" ] && [ -f /etc/defaultdomain ]; then
501.46Schristos		domainname=$(cat /etc/defaultdomain)
511.1Slukem	fi
521.1Slukem	if [ -n "$domainname" ]; then
531.1Slukem		echo "NIS domainname: $domainname"
541.1Slukem		domainname $domainname
551.1Slukem	fi
561.1Slukem
571.1Slukem	# Flush all routes just to make sure it is clean
581.1Slukem	if checkyesno flushroutes; then
591.53Sreed		/sbin/route -qn flush
601.1Slukem	fi
611.1Slukem
621.1Slukem	# Set the address for the first loopback interface, so that the
631.1Slukem	# auto-route from a newly configured interface's address to lo0
641.1Slukem	# works correctly.
651.1Slukem	#
661.32Slukem	# NOTE: obscure networking problems will occur if lo0 isn't configured.
671.1Slukem	#
681.53Sreed	/sbin/ifconfig lo0 inet 127.0.0.1
691.10Sitojun
701.31Sitojun	# According to RFC1122, 127.0.0.0/8 must not leave the node.
711.10Sitojun	#
721.53Sreed	/sbin/route -q add -inet 127.0.0.0 -netmask 0xff000000 127.0.0.1 -reject
731.1Slukem
741.30Sitojun	# IPv6 routing setups, and host/router mode selection.
751.30Sitojun	#
761.53Sreed	if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then
771.30Sitojun		# We have IPv6 support in kernel.
781.30Sitojun
791.30Sitojun		# disallow link-local unicast dest without outgoing scope
801.30Sitojun		# identifiers.
811.30Sitojun		#
821.53Sreed		/sbin/route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject
831.30Sitojun
841.50Srpaulo		# disallow the use of the RFC3849 documentation address
851.50Srpaulo		#
861.53Sreed		/sbin/route -q add -inet6 2001:db8:: -prefixlen 32 ::1 -reject
871.50Srpaulo
881.50Srpaulo		# IPv6 site-local scoped address prefix (fec0::/10)
891.50Srpaulo		# has been deprecated by RFC3879.
901.30Sitojun		#
911.50Srpaulo		if [ -n "$ip6sitelocal" ]; then
921.50Srpaulo			warn "\$ip6sitelocal is no longer valid"
931.30Sitojun		fi
941.30Sitojun
951.30Sitojun		# disallow "internal" addresses to appear on the wire.
961.30Sitojun		#
971.53Sreed		/sbin/route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
981.30Sitojun
991.30Sitojun		# disallow packets to malicious IPv4 compatible prefix
1001.30Sitojun		#
1011.53Sreed		/sbin/route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject
1021.53Sreed		/sbin/route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject
1031.53Sreed		/sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject
1041.53Sreed		/sbin/route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject
1051.30Sitojun
1061.30Sitojun		# disallow packets to malicious 6to4 prefix
1071.30Sitojun		#
1081.53Sreed		/sbin/route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject
1091.53Sreed		/sbin/route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject
1101.53Sreed		/sbin/route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject
1111.53Sreed		/sbin/route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject
1121.30Sitojun
1131.30Sitojun		# Completely disallow packets to IPv4 compatible prefix.
1141.30Sitojun		# This may conflict with RFC1933 under following circumstances:
1151.30Sitojun		# (1) An IPv6-only KAME node tries to originate packets to IPv4
1161.51Sreed		#     compatible destination.  The KAME node has no IPv4
1171.30Sitojun		#     compatible support.  Under RFC1933, it should transmit
1181.30Sitojun		#     native IPv6 packets toward IPv4 compatible destination,
1191.30Sitojun		#     hoping it would reach a router that forwards the packet
1201.30Sitojun		#     toward auto-tunnel interface.
1211.30Sitojun		# (2) An IPv6-only node originates a packet to IPv4 compatible
1221.30Sitojun		#     destination.  A KAME node is acting as an IPv6 router, and
1231.30Sitojun		#     asked to forward it.
1241.30Sitojun		# Due to rare use of IPv4 compatible address, and security
1251.30Sitojun		# issues with it, we disable it by default.
1261.30Sitojun		#
1271.53Sreed		/sbin/route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
1281.30Sitojun
1291.53Sreed		/sbin/sysctl -qw net.inet6.ip6.forwarding=0
1301.53Sreed		/sbin/sysctl -qw net.inet6.ip6.accept_rtadv=0
1311.30Sitojun
1321.30Sitojun		case $ip6mode in
1331.30Sitojun		router)
1341.30Sitojun			echo 'IPv6 mode: router'
1351.53Sreed			/sbin/sysctl -qw net.inet6.ip6.forwarding=1
1361.50Srpaulo
1371.50Srpaulo			# disallow unique-local unicast forwarding without
1381.50Srpaulo			# explicit configuration.
1391.50Srpaulo			if ! checkyesno ip6uniquelocal; then
1401.53Sreed				/sbin/route -q add -inet6 fc00:: -prefixlen 7 \
1411.50Srpaulo				    ::1 -reject
1421.50Srpaulo			fi
1431.30Sitojun			;;
1441.30Sitojun
1451.30Sitojun		autohost)
1461.30Sitojun			echo 'IPv6 mode: autoconfigured host'
1471.53Sreed			/sbin/sysctl -qw net.inet6.ip6.accept_rtadv=1
1481.30Sitojun			;;
1491.30Sitojun
1501.30Sitojun		host)	
1511.30Sitojun			echo 'IPv6 mode: host'
1521.30Sitojun			;;
1531.30Sitojun
1541.36Slukem		*)	warn "invalid \$ip6mode value "\"$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.54Sapb	#
1641.54Sapb	# For any interfaces that has an $ifconfig_xxN variable
1651.54Sapb	# associated, we break it into lines using ';' as a separator,
1661.54Sapb	# then process it just like the contents of an /etc/ifconfig.xxN
1671.54Sapb	# file.
1681.54Sapb	#
1691.54Sapb	# For each line from the $ifconfig_xxN variable or the
1701.54Sapb	# /etc/ifconfig.xxN file, we ignore comments and blank lines,
1711.54Sapb	# treat lines beginning with "!" as commands to execute, treat
1721.54Sapb	# "dhcp" as a special case to invoke dhcpcd, and for any other
1731.54Sapb	# line we run "ifconfig xxN", using each line of the file as the
1741.54Sapb	# arguments for a separate "ifconfig" invocation.
1751.1Slukem	#
1761.1Slukem	# In order to configure an interface reasonably, you at the very least
1771.1Slukem	# need to specify "[addr_family] [hostname]" (e.g "inet my.domain.org"),
1781.1Slukem	# and probably a netmask (as in "netmask 0xffffffe0"). You will
1791.1Slukem	# frequently need to specify a media type, as in "media UTP", for
1801.1Slukem	# interface cards with multiple media connections that do not
1811.1Slukem	# autoconfigure. See the ifconfig manual page for details.
1821.1Slukem	#
1831.1Slukem	# Note that /etc/ifconfig.xxN takes multiple lines.  The following
1841.1Slukem	# configuration is possible:
1851.1Slukem	#	inet 10.1.1.1 netmask 0xffffff00
1861.1Slukem	#	inet 10.1.1.2 netmask 0xffffff00 alias
1871.50Srpaulo	#	inet6 2001:db8::1 prefixlen 64 alias
1881.1Slukem	#
1891.29Sitojun	# You can put shell script fragment into /etc/ifconfig.xxN by
1901.29Sitojun	# starting a line with "!".  Refer to ifconfig.if(5) for details.
1911.29Sitojun	#
1921.1Slukem	if [ "$net_interfaces" != NO ]; then
1931.1Slukem		if checkyesno auto_ifconfig; then
1941.53Sreed			tmp=$(/sbin/ifconfig -l)
1951.53Sreed			for cloner in $(/sbin/ifconfig -C 2>/dev/null); do
1961.25Swiz				for int in /etc/ifconfig.${cloner}[0-9]*; do
1971.23Snisimura					[ ! -f $int ] && break
1981.21Slukem					tmp="$tmp ${int##*.}"
1991.15Sthorpej				done
2001.15Sthorpej			done
2011.1Slukem		else
2021.1Slukem			tmp="$net_interfaces"
2031.1Slukem		fi
2041.1Slukem		echo -n 'Configuring network interfaces:'
2051.1Slukem		for int in $tmp; do
2061.54Sapb			eval argslist=\$ifconfig_$int
2071.54Sapb
2081.54Sapb			# Skip interfaces that do not have explicit
2091.54Sapb			# configuration information.  If auto_ifconfig is
2101.54Sapb			# false then also warn about such interfaces.
2111.54Sapb			#
2121.54Sapb			if [ -z "$argslist" ] && ! [ -f /etc/ifconfig.$int ]
2131.54Sapb			then
2141.1Slukem				if ! checkyesno auto_ifconfig; then
2151.1Slukem					echo
2161.1Slukem					warn \
2171.1Slukem			"/etc/ifconfig.$int missing and ifconfig_$int not set;"
2181.1Slukem					warn "interface $int not configured."
2191.1Slukem				fi
2201.1Slukem				continue
2211.1Slukem			fi
2221.54Sapb
2231.54Sapb			echo -n " $int"
2241.54Sapb
2251.54Sapb			# Create the interface if necessary.
2261.54Sapb			# If the interface did not exist before,
2271.54Sapb			# then also resync ipf(4).
2281.54Sapb			#
2291.54Sapb			if /sbin/ifconfig $int create 2>/dev/null && \
2301.54Sapb			   checkyesno ipfilter; then
2311.54Sapb				/sbin/ipf -y >/dev/null
2321.54Sapb			fi
2331.54Sapb
2341.54Sapb			# If $ifconfig_xxN is empty, then use
2351.54Sapb			# /etc/ifconfig.xxN, which we know exists due to
2361.54Sapb			# an earlier test.
2371.54Sapb			#
2381.54Sapb			# If $ifconfig_xxN is non-empty and contains a
2391.54Sapb			# newline, then just use it as is.  (This allows
2401.54Sapb			# semicolons through unmolested.)
2411.54Sapb			#
2421.54Sapb			# If $ifconfig_xxN is non-empty and does not
2431.54Sapb			# contain a newline, then convert all semicolons
2441.54Sapb			# to newlines.
2451.54Sapb			#
2461.54Sapb			case "$argslist" in
2471.54Sapb			'')
2481.54Sapb				cat /etc/ifconfig.$int
2491.54Sapb				;;
2501.54Sapb			*"${nl}"*)
2511.54Sapb				echo "$argslist"
2521.54Sapb				;;
2531.54Sapb			*)
2541.54Sapb				(
2551.54Sapb					set -o noglob
2561.54Sapb					IFS=';'; set -- $argslist
2571.54Sapb					#echo >&2 "[$#] [$1] [$2] [$3] [$4]"
2581.54Sapb					IFS="$nl"; echo "$*"
2591.54Sapb				)
2601.54Sapb				;;
2611.54Sapb			esac |
2621.61Sapb			collapse_backslash_newline |
2631.54Sapb			while read -r args; do
2641.54Sapb				case "$args" in
2651.54Sapb				''|"#"*|create)
2661.54Sapb					;;
2671.54Sapb				"!"*)
2681.54Sapb					# Run arbitrary command in a subshell.
2691.54Sapb					( eval "${args#*!}" )
2701.54Sapb					;;
2711.54Sapb				dhcp)
2721.58Sroy					if ! checkyesno dhcpcd; then
2731.58Sroy						/sbin/dhcpcd -n \
2741.58Sroy							${dhcpcd_flags} $int
2751.58Sroy					fi
2761.54Sapb					;;
2771.54Sapb				*)
2781.54Sapb					# Pass args to ifconfig.  Note
2791.54Sapb					# that args may contain embedded
2801.54Sapb					# shell metacharacters, such as
2811.57Schristos					# "ssid 'foo;*>bar'". We eval
2821.57Schristos					# one more time so that things
2831.57Schristos					# like ssid "Columbia University" work.
2841.54Sapb					(
2851.54Sapb						set -o noglob
2861.57Schristos						eval set -- $args
2871.54Sapb						#echo >&2 "[$#] [$1] [$2] [$3]"
2881.54Sapb						/sbin/ifconfig $int "$@"
2891.54Sapb					)
2901.54Sapb					;;
2911.54Sapb				esac
2921.54Sapb			done
2931.1Slukem			configured_interfaces="$configured_interfaces $int"
2941.1Slukem		done
2951.1Slukem		echo "."
2961.1Slukem	fi
2971.1Slukem
2981.48Scjs	echo -n "Adding interface aliases:"
2991.48Scjs
3001.1Slukem	# Check if each configured interface xxN has an $ifaliases_xxN variable
3011.1Slukem	# associated, then configure additional IP addresses for that interface.
3021.1Slukem	# The variable contains a list of "address netmask" pairs, with
3031.1Slukem	# "netmask" set to "-" if the interface default netmask is to be used.
3041.1Slukem	#
3051.54Sapb	# Note that $ifaliases_xxN works only in certain cases and its
3061.54Sapb	# use is not recommended.  Use /etc/ifconfig.xxN or multiple
3071.54Sapb	# commands in $ifconfig_xxN instead.
3081.1Slukem	#
3091.48Scjs	for int in lo0 $configured_interfaces; do
3101.20Snisimura		eval args=\$ifaliases_$int
3111.1Slukem		if [ -n "$args" ]; then
3121.1Slukem			set -- $args
3131.1Slukem			while [ $# -ge 2 ]; do
3141.1Slukem				addr=$1 ; net=$2 ; shift 2
3151.1Slukem				if [ "$net" = "-" ]; then
3161.16Sjdolecek					# for compatibility only, obsolete
3171.53Sreed					/sbin/ifconfig $int inet alias $addr
3181.1Slukem				else
3191.53Sreed					/sbin/ifconfig $int inet alias $addr \
3201.1Slukem					    netmask $net
3211.1Slukem				fi
3221.48Scjs				echo -n " $int:$addr"
3231.1Slukem			done
3241.1Slukem		fi
3251.1Slukem	done
3261.1Slukem
3271.1Slukem	# /etc/ifaliases, if it exists, contains the names of additional IP
3281.1Slukem	# addresses for each interface. It is formatted as a series of lines
3291.1Slukem	# that contain
3301.1Slukem	#	address interface netmask
3311.1Slukem	#
3321.54Sapb	# Note that /etc/ifaliases works only in certain cases and its
3331.54Sapb	# use is not recommended.  Use /etc/ifconfig.xxN or multiple
3341.54Sapb	# commands in $ifconfig_xxN instead.
3351.1Slukem	#
3361.1Slukem	if [ -f /etc/ifaliases ]; then
3371.1Slukem		while read addr int net; do
3381.1Slukem			if [ -z "$net" ]; then
3391.16Sjdolecek				# for compatibility only, obsolete
3401.53Sreed				/sbin/ifconfig $int inet alias $addr
3411.1Slukem			else
3421.53Sreed				/sbin/ifconfig $int inet alias $addr netmask $net
3431.1Slukem			fi
3441.20Snisimura		done < /etc/ifaliases
3451.1Slukem	fi
3461.1Slukem
3471.56Sapb	echo "." # for "Adding interface aliases:"
3481.56Sapb
3491.56Sapb	# Check $defaultroute, then /etc/mygate, for the name or address
3501.56Sapb	# of my IPv4 gateway host. If using a name, that name must be in
3511.56Sapb	# /etc/hosts.
3521.56Sapb	#
3531.56Sapb	if [ -z "$defaultroute" ] && [ -f /etc/mygate ]; then
3541.56Sapb		defaultroute=$(cat /etc/mygate)
3551.56Sapb	fi
3561.56Sapb	if [ -n "$defaultroute" ]; then
3571.56Sapb		/sbin/route add default $defaultroute
3581.56Sapb	fi
3591.56Sapb
3601.56Sapb	# Check $defaultroute6, then /etc/mygate6, for the name or address
3611.56Sapb	# of my IPv6 gateway host. If using a name, that name must be in
3621.56Sapb	# /etc/hosts.  Note that the gateway host address must be a link-local
3631.56Sapb	# address if it is not using an stf* interface.
3641.56Sapb	#
3651.56Sapb	if [ -z "$defaultroute6" ] && [ -f /etc/mygate6 ]; then
3661.56Sapb		defaultroute6=$(cat /etc/mygate6)
3671.56Sapb	fi
3681.56Sapb	if [ -n "$defaultroute6" ]; then
3691.56Sapb		if [ "$ip6mode" = "autohost" ]; then
3701.56Sapb			echo
3711.56Sapb			warn \
3721.56Sapb	    "ip6mode is set to 'autohost' and a v6 default route is also set."
3731.56Sapb		fi
3741.56Sapb		/sbin/route add -inet6 default $defaultroute6
3751.56Sapb	fi
3761.48Scjs
3771.30Sitojun	# IPv6 interface autoconfiguration.
3781.1Slukem	#
3791.53Sreed	if /sbin/ifconfig lo0 inet6 >/dev/null 2>&1; then
3801.17Sitojun		# wait till DAD is completed. always invoke it in case
3811.17Sitojun		# if are configured manually by ifconfig
3821.17Sitojun		#
3831.53Sreed		dadcount=$(/sbin/sysctl -n net.inet6.ip6.dad_count 2>/dev/null)
3841.17Sitojun		sleep $dadcount
3851.17Sitojun		sleep 1
3861.17Sitojun
3871.1Slukem		if checkyesno rtsol; then
3881.1Slukem			if [ "$ip6mode" = "autohost" ]; then
3891.1Slukem				echo 'Sending router solicitation...'
3901.53Sreed				/sbin/rtsol $rtsol_flags
3911.1Slukem			else
3921.1Slukem				echo
3931.1Slukem				warn \
3941.1Slukem			    "ip6mode must be set to 'autohost' to use rtsol."
3951.1Slukem			fi
3961.17Sitojun
3971.18Sitojun			# wait till DAD is completed, for global addresses
3981.18Sitojun			# configured by router advert message.
3991.17Sitojun			#
4001.17Sitojun			sleep $dadcount
4011.17Sitojun			sleep 1
4021.1Slukem		fi
4031.42Schristos	fi
4041.42Schristos
4051.1Slukem	# XXX this must die
4061.1Slukem	if [ -s /etc/netstart.local ]; then
4071.1Slukem		sh /etc/netstart.local start
4081.1Slukem	fi
4091.1Slukem}
4101.1Slukem
4111.1Slukemnetwork_stop()
4121.1Slukem{
4131.1Slukem	echo "Stopping network."
4141.1Slukem
4151.1Slukem	# XXX this must die
4161.1Slukem	if [ -s /etc/netstart.local ]; then
4171.1Slukem		sh /etc/netstart.local stop
4181.1Slukem	fi
4191.1Slukem
4201.1Slukem	echo "Deleting aliases."
4211.1Slukem	if [ -f /etc/ifaliases ]; then
4221.1Slukem		while read addr int net; do
4231.53Sreed			/sbin/ifconfig $int inet delete $addr
4241.20Snisimura		done < /etc/ifaliases
4251.1Slukem	fi
4261.1Slukem
4271.53Sreed	for int in $(/sbin/ifconfig -lu); do
4281.20Snisimura		eval args=\$ifaliases_$int
4291.1Slukem		if [ -n "$args" ]; then
4301.1Slukem			set -- $args
4311.1Slukem			while [ $# -ge 2 ]; do
4321.1Slukem				addr=$1 ; net=$2 ; shift 2
4331.53Sreed				/sbin/ifconfig $int inet delete $addr
4341.1Slukem			done
4351.1Slukem		fi
4361.1Slukem	done
4371.1Slukem
4381.1Slukem	# down interfaces
4391.1Slukem	#
4401.1Slukem	echo -n 'Downing network interfaces:'
4411.1Slukem	if [ "$net_interfaces" != NO ]; then
4421.1Slukem		if checkyesno auto_ifconfig; then
4431.53Sreed			tmp=$(/sbin/ifconfig -l)
4441.1Slukem		else
4451.1Slukem			tmp="$net_interfaces"
4461.1Slukem		fi
4471.1Slukem		for int in $tmp; do
4481.20Snisimura			eval args=\$ifconfig_$int
4491.2Sveego			if [ -n "$args" ] || [ -f /etc/ifconfig.$int ]; then
4501.1Slukem				echo -n " $int"
4511.58Sroy				if [ -f /var/run/dhcpcd-$int.pid ]; then
4521.58Sroy					/sbin/dhcpcd -k $int 2> /dev/null
4531.58Sroy				fi
4541.53Sreed				/sbin/ifconfig $int down
4551.53Sreed				if /sbin/ifconfig $int destroy 2>/dev/null && \
4561.39Stron				   checkyesno ipfilter; then
4571.39Stron					# resync ipf(4)
4581.53Sreed					/sbin/ipf -y >/dev/null
4591.39Stron				fi
4601.1Slukem			fi
4611.1Slukem		done
4621.1Slukem		echo "."
4631.1Slukem	fi
4641.1Slukem
4651.1Slukem	# flush routes
4661.1Slukem	#
4671.53Sreed	/sbin/route -qn flush
4681.38Stron
4691.1Slukem}
4701.1Slukem
4711.47Slukemload_rc_config $name
4721.47Slukemload_rc_config_var dhclient dhclient
4731.58Sroyload_rc_config_var dhcpcd dhcpcd
4741.47Slukemload_rc_config_var ipfilter ipfilter
4751.1Slukemrun_rc_command "$1"
476