xref: /openbsd/etc/netstart (revision 2fcef345)
1df930be7Sderaadt#!/bin/sh -
2df930be7Sderaadt#
3*2fcef345Sderaadt#	$OpenBSD: netstart,v 1.97 2004/05/29 07:01:03 deraadt Exp $
404e0ac27Smillert
504e0ac27Smillert# Returns true if $1 contains only alphanumerics
604e0ac27Smillertisalphanumeric() {
704e0ac27Smillert	local _n
804e0ac27Smillert	_n=$1
904e0ac27Smillert	while [ ${#_n} != 0 ]; do
1004e0ac27Smillert		case $_n in
1104e0ac27Smillert			[A-Za-z0-9]*)	;;
1204e0ac27Smillert			*)		return 1;;
1304e0ac27Smillert		esac
1404e0ac27Smillert		_n=${_n#?}
1504e0ac27Smillert	done
1604e0ac27Smillert	return 0
1704e0ac27Smillert}
18df930be7Sderaadt
19dfc209d0Smiod# Start the $1 interface
20dfc209d0Smiodifstart() {
21f5319bdcSderaadt	if=$1
22dfc209d0Smiod	# Interface names must be alphanumeric only.  We check to avoid
23dfc209d0Smiod	# configuring backup or temp files, and to catch the "*" case.
24f5319bdcSderaadt	if ! isalphanumeric "$if"; then
25dfc209d0Smiod		return
26dfc209d0Smiod	fi
27dfc209d0Smiod
28f5319bdcSderaadt	ifconfig $if > /dev/null 2>&1
29dfc209d0Smiod	if [ "$?" != "0" ]; then
30e087dc57Smarkus		# Try to create interface if it does not exist
31e087dc57Smarkus		ifconfig $if create > /dev/null 2>&1
32e087dc57Smarkus		if [ "$?" != "0" ]; then
33dfc209d0Smiod			return
34dfc209d0Smiod		fi
35e087dc57Smarkus	fi
36dfc209d0Smiod
37dfc209d0Smiod	# Now parse the hostname.* file
38dfc209d0Smiod	while :; do
39dfc209d0Smiod		if [ "$cmd2" ]; then
40dfc209d0Smiod			# We are carrying over from the 'read dt dtaddr'
41dfc209d0Smiod			# last time.
42dfc209d0Smiod			set -- $cmd2
43dfc209d0Smiod			af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
44dfc209d0Smiod			# Make sure and get any remaining args in ext2,
45dfc209d0Smiod			# like the read below
46dfc209d0Smiod			i=1
47dfc209d0Smiod			while [ i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
48dfc209d0Smiod			ext2="$@"
49dfc209d0Smiod		else
50dfc209d0Smiod			# Read the next line or exit the while loop.
51dfc209d0Smiod			read af name mask bcaddr ext1 ext2 || break
52dfc209d0Smiod		fi
53dfc209d0Smiod		# $af can be "dhcp", "up", "rtsol", an address family,
54dfc209d0Smiod		# commands, or a comment.
55dfc209d0Smiod		case "$af" in
56dfc209d0Smiod		"#"*|"") # skip comments and empty lines
57dfc209d0Smiod			continue
58dfc209d0Smiod			;;
59dfc209d0Smiod		"!"*) # parse commands
60dfc209d0Smiod			cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
61dfc209d0Smiod			;;
62dfc209d0Smiod		"bridge")
63f5319bdcSderaadt			cmd="echo /etc/hostname.$if: bridges now supported via bridgename.* files"
64dfc209d0Smiod			;;
65dfc209d0Smiod		"dhcp")
66dfc209d0Smiod			[ "$name" = "NONE" ] && name=
67dfc209d0Smiod			[ "$mask" = "NONE" ] && mask=
68dfc209d0Smiod			[ "$bcaddr" = "NONE" ] && bcaddr=
69f5319bdcSderaadt			ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
70f5319bdcSderaadt			cmd="dhclient $if"
71dfc209d0Smiod			;;
72dfc209d0Smiod		"rtsol")
73f5319bdcSderaadt			ifconfig $if $name $mask $bcaddr $ext1 $ext2 up
74f5319bdcSderaadt			rtsolif="$rtsolif $if"
75dfc209d0Smiod			cmd=
76dfc209d0Smiod			;;
77dfc209d0Smiod		"up")
78f5319bdcSderaadt			# The only one of these guaranteed to be set is $if.
79dfc209d0Smiod			# The remaining ones exist so that media controls work.
80f5319bdcSderaadt			cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
81dfc209d0Smiod			;;
82dfc209d0Smiod		*)
83dfc209d0Smiod			read dt dtaddr
84dfc209d0Smiod			if [ "$name"  = "alias" ]; then
85dfc209d0Smiod				# perform a 'shift' of sorts
86dfc209d0Smiod				alias=$name
87dfc209d0Smiod				name=$mask
88dfc209d0Smiod				mask=$bcaddr
89dfc209d0Smiod				bcaddr=$ext1
90dfc209d0Smiod				ext1=$ext2
91dfc209d0Smiod				ext2=
92dfc209d0Smiod			else
93dfc209d0Smiod				alias=
94dfc209d0Smiod			fi
95f5319bdcSderaadt			cmd="ifconfig $if $af $alias $name "
96dfc209d0Smiod			case "$dt" in
97dfc209d0Smiod			dest)
98dfc209d0Smiod				cmd="$cmd $dtaddr"
99dfc209d0Smiod				;;
100dfc209d0Smiod			[a-z!]*)
101dfc209d0Smiod				cmd2="$dt $dtaddr"
102dfc209d0Smiod				;;
103dfc209d0Smiod			esac
104dfc209d0Smiod			if [ ! -n "$name" ]; then
105f5319bdcSderaadt				echo "/etc/hostname.$if: invalid network configuration file"
106dfc209d0Smiod				return
107dfc209d0Smiod			fi
108dfc209d0Smiod			case $af in
109dfc209d0Smiod			inet)
110dfc209d0Smiod				[ "$mask" ] && cmd="$cmd netmask $mask"
111dfc209d0Smiod				if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
112dfc209d0Smiod					cmd="$cmd broadcast $bcaddr"
113dfc209d0Smiod				fi
114f4b4b73bSderaadt				[ "$alias" ] && rtcmd=";route -qn add -host $name 127.0.0.1"
115dfc209d0Smiod				;;
116dfc209d0Smiod			inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
117dfc209d0Smiod				cmd="$cmd $bcaddr"
118dfc209d0Smiod				;;
119dfc209d0Smiod			*)
120dfc209d0Smiod				cmd="$cmd $mask $bcaddr"
121dfc209d0Smiod				;;
122dfc209d0Smiod			esac
123dfc209d0Smiod			cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
124dfc209d0Smiod			;;
125dfc209d0Smiod		esac
126dfc209d0Smiod		eval "$cmd"
127f5319bdcSderaadt	done < /etc/hostname.$if
128dfc209d0Smiod}
129dfc209d0Smiod
130dfc209d0Smiod# Start the $1 bridge
131dfc209d0Smiodbridgestart() {
132dfc209d0Smiod	# Interface names must be alphanumeric only.  We check to avoid
133dfc209d0Smiod	# configuring backup or temp files, and to catch the "*" case.
134dfc209d0Smiod	if ! isalphanumeric "$1"; then
135dfc209d0Smiod		return
136dfc209d0Smiod	fi
137dfc209d0Smiod	brconfig $1 > /dev/null 2>&1
138dfc209d0Smiod	if [ "$?" != "0" ]; then
139e5eec468Smillert		# Try to create interface if it does not exist
140e5eec468Smillert		ifconfig $if create > /dev/null 2>&1
141e5eec468Smillert		if [ "$?" != "0" ]; then
142dfc209d0Smiod			return
143dfc209d0Smiod		fi
144e5eec468Smillert	fi
145dfc209d0Smiod
146dfc209d0Smiod	# Now parse the bridgename.* file
147dfc209d0Smiod	# All lines are run as brconfig(8) commands.
148dfc209d0Smiod	while read line ; do
149dfc209d0Smiod		line=${line%%#*}		# strip comments
150dfc209d0Smiod		test -z "$line" && continue
151dfc209d0Smiod		case "$line" in
152dfc209d0Smiod		"!"*)
153dfc209d0Smiod			cmd="${line#*!}"
154dfc209d0Smiod			;;
155dfc209d0Smiod		*)
156dfc209d0Smiod			cmd="brconfig $1 $line"
157dfc209d0Smiod			;;
158dfc209d0Smiod		esac
159dfc209d0Smiod		eval "$cmd"
160dfc209d0Smiod	done < /etc/bridgename.$1
161dfc209d0Smiod}
162dfc209d0Smiod
1630dc37902Sangelos# Re-read /etc/rc.conf
1640dc37902Sangelos. /etc/rc.conf
1650dc37902Sangelos
166dfc209d0Smiod# If we were invoked with a list of interface names, just reconfigure these
167dfc209d0Smiod# interfaces (or bridges) and return.
168dfc209d0Smiodif [ $1x = autobootx ]; then
169dfc209d0Smiod	shift
170dfc209d0Smiodfi
171dfc209d0Smiodif [ $# -gt 0 ]; then
172dfc209d0Smiod	while [ $# -gt 0 ]; do
173dfc209d0Smiod		if [ -f /etc/bridgename.$1 ]; then
174dfc209d0Smiod			bridgestart $1
175dfc209d0Smiod		else
176dfc209d0Smiod			ifstart $1
177dfc209d0Smiod		fi
178dfc209d0Smiod		shift
179dfc209d0Smiod	done
180dfc209d0Smiod	return
181dfc209d0Smiodfi
182dfc209d0Smiod
183dfc209d0Smiod# Otherwise, process with the complete network initialization.
184dfc209d0Smiod
185df930be7Sderaadt# /etc/myname contains my symbolic name
18637bbdc83Shenningif [ -f /etc/myname ]; then
187df930be7Sderaadt	hostname=`cat /etc/myname`
188df930be7Sderaadt	hostname $hostname
18937bbdc83Shenningelse
19037bbdc83Shenning	hostname=`hostname`
19137bbdc83Shenningfi
19237bbdc83Shenning
193df930be7Sderaadtif [ -f /etc/defaultdomain ]; then
194df930be7Sderaadt	domainname `cat /etc/defaultdomain`
195df930be7Sderaadtfi
196df930be7Sderaadt
197bb101bd8Sderaadt# Set the address for the loopback interface.  Bringing the
198bb101bd8Sderaadt# interface up, automatically invokes the IPv6 address ::1)
1998f8fdbefSderaadtifconfig lo0 inet 127.0.0.1
20098c28033Skstailey
2013d8fed7cSitojunif ifconfig lo0 inet6 >/dev/null 2>&1; then
2023d8fed7cSitojun	# IPv6 configurations.
2033d8fed7cSitojun	ip6kernel=YES
2043d8fed7cSitojun
205dfc209d0Smiod	# Disallow link-local unicast dest without outgoing scope identifiers.
206f4b4b73bSderaadt	route -q add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
20792aceabbSitojun
208dfc209d0Smiod	# Disallow site-local unicast dest without outgoing scope identifiers.
20992aceabbSitojun	# If you configure site-locals without scope id (it is permissible
21092aceabbSitojun	# config for routers that are not on scope boundary), you may want
21192aceabbSitojun	# to comment the line out.
212f4b4b73bSderaadt	route -q add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
21392aceabbSitojun
214dfc209d0Smiod	# Disallow "internal" addresses to appear on the wire.
215f4b4b73bSderaadt	route -q add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
21692aceabbSitojun
217dfc209d0Smiod	# Disallow packets to malicious IPv4 compatible prefix.
218f4b4b73bSderaadt	route -q add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
219f4b4b73bSderaadt	route -q add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
220f4b4b73bSderaadt	route -q add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
221f4b4b73bSderaadt	route -q add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
22292aceabbSitojun
223dfc209d0Smiod	# Disallow packets to malicious 6to4 prefix.
224f4b4b73bSderaadt	route -q add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
225f4b4b73bSderaadt	route -q add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
226f4b4b73bSderaadt	route -q add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
227f4b4b73bSderaadt	route -q add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
22892aceabbSitojun
22992aceabbSitojun	# Completely disallow packets to IPv4 compatible prefix.
23092aceabbSitojun	# This may conflict with RFC1933 under following circumstances:
23192aceabbSitojun	# (1) An IPv6-only KAME node tries to originate packets to IPv4
2325e268fadSderaadt	#     compatible destination.  The KAME node has no IPv4 compatible
23392aceabbSitojun	#     support.  Under RFC1933, it should transmit native IPv6
23492aceabbSitojun	#     packets toward IPv4 compatible destination, hoping it would
23592aceabbSitojun	#     reach a router that forwards the packet toward auto-tunnel
23692aceabbSitojun	#     interface.
2375e268fadSderaadt	# (2) An IPv6-only node originates a packet to an IPv4 compatible
23892aceabbSitojun	#     destination.  A KAME node is acting as an IPv6 router, and
23992aceabbSitojun	#     asked to forward it.
2405e268fadSderaadt	# Due to rare use of IPv4 compatible addresses, and security issues
24192aceabbSitojun	# with it, we disable it by default.
242f4b4b73bSderaadt	route -q add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
24382c17b75Sitojun
24482c17b75Sitojun	rtsolif=""
2453d8fed7cSitojunelse
2463d8fed7cSitojun	ip6kernel=NO
2473d8fed7cSitojunfi
2483d8fed7cSitojun
249dfc209d0Smiod# Configure all the non-loopback interfaces which we know about.
250dfc209d0Smiod# Refer to hostname.if(5) and bridgename.if(5)
25104e0ac27Smillertfor hn in /etc/hostname.*; do
25204e0ac27Smillert	# Strip off /etc/hostname. prefix
25304e0ac27Smillert	if=${hn#/etc/hostname.}
2547fd32e5eStodd	test "$if" = "*" && continue
255df930be7Sderaadt
256dfc209d0Smiod	case $if in
2572516daceSmcbride	"carp"*|"gif"*|"gre"*|"pfsync"*)
2582516daceSmcbride		# CARP, GIF, GRE and PFSYNC interfaces need the routes to be setup
25939d84bedSdavid		# before they are configured.
26004e0ac27Smillert		continue
261cfa67c92Sniklas		;;
262cfa67c92Sniklas	*)
263dfc209d0Smiod		ifstart $if
264cfa67c92Sniklas		;;
265cfa67c92Sniklas	esac
266df930be7Sderaadtdone
26782c17b75Sitojun
26882c17b75Sitojunif [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
26982c17b75Sitojun	fw=`sysctl -n net.inet6.ip6.forwarding`
27082c17b75Sitojun	ra=`sysctl -n net.inet6.ip6.accept_rtadv`
27182c17b75Sitojun	if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
272d56849f2Sitojun		echo "IPv6 autoconf:$rtsolif"
27382c17b75Sitojun		rtsol $rtsolif
27482c17b75Sitojun	else
27582c17b75Sitojun		echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
27682c17b75Sitojun	fi
27782c17b75Sitojunfi
278d56849f2Sitojunif [ "$ip6kernel" = "YES" ]; then
279d56849f2Sitojun	# this is to make sure DAD is completed before going further.
280d56849f2Sitojun	sleep `sysctl -n net.inet6.ip6.dad_count`
281d56849f2Sitojunfi
28282c17b75Sitojun
283d747464dSderaadt# /etc/mygate, if it exists, contains the name of my gateway host
284d747464dSderaadt# that name must be in /etc/hosts.
285d747464dSderaadtif [ -f /etc/mygate ]; then
286*2fcef345Sderaadt	route -qn delete default > /dev/null 2>&1
287f4b4b73bSderaadt	route -qn add -host default `cat /etc/mygate`
288a4f0e6c1Sdownsjfi
289cf3860a5Sderaadt
290745634aaSniklas# Multicast routing.
291745634aaSniklas#
292745634aaSniklas# The routing to the 224.0.0.0/4 net is setup according to these rules:
293745634aaSniklas# multicast_host	multicast_router	route		comment
294745634aaSniklas# NO			NO			-reject		no multicast
295745634aaSniklas# NO			YES			none installed	daemon will run
296745634aaSniklas# YES/interface		NO			-interface	YES=def. iface
297745634aaSniklas#	   Any other combination		-reject		config error
298745634aaSniklascase "$multicast_host:$multicast_router" in
299745634aaSniklasNO:NO)
300f4b4b73bSderaadt	route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
301fe32e9eaSderaadt	;;
302745634aaSniklasNO:YES)
303745634aaSniklas	;;
304745634aaSniklas*:NO)
305745634aaSniklas	set `if [ $multicast_host = YES ]; then
306f4b4b73bSderaadt		ed -s '!route -qn show -inet' <<EOF
307745634aaSniklas/^default/p
308745634aaSniklasEOF
309745634aaSniklas	else
310745634aaSniklas		ed -s "!ifconfig $multicast_host" <<EOF
311745634aaSniklas/^	inet /p
312745634aaSniklasEOF
313745634aaSniklas	fi`
314f4b4b73bSderaadt	route -qn add -net 224.0.0.0/4 -interface $2 > /dev/null
315fe32e9eaSderaadt	;;
316745634aaSniklas*:*)
317745634aaSniklas	echo 'config error, multicasting disabled until rc.conf is fixed'
318f4b4b73bSderaadt	route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
319fe32e9eaSderaadt	;;
320745634aaSniklasesac
321dfc209d0Smiod
322a3ad2dc3Smcbride# The pfsync interface needs to come up before carp.
323a3ad2dc3Smcbrideif [ -f /etc/hostname.pfsync0 ]; then
324a3ad2dc3Smcbride		ifstart pfsync0
325a3ad2dc3Smcbridefi
326a3ad2dc3Smcbride
327a3ad2dc3Smcbride# Configure all the carp, gif and gre interfaces which we know about.
328dfc209d0Smiod# They were delayed because they require the routes to be set.
329dfc209d0Smiodfor hn in /etc/hostname.*; do
330dfc209d0Smiod	# Strip off /etc/hostname. prefix
331dfc209d0Smiod	if=${hn#/etc/hostname.}
3327fd32e5eStodd	test "$if" = "*" && continue
333dfc209d0Smiod
334dfc209d0Smiod	case $if in
335a3ad2dc3Smcbride	"carp"*|"gif"*|"gre"*)
336dfc209d0Smiod		ifstart $if
337dfc209d0Smiod		;;
338dfc209d0Smiod	*)
339dfc209d0Smiod		# Regular interfaces have already been configured.
340dfc209d0Smiod		continue
341dfc209d0Smiod		;;
342dfc209d0Smiod	esac
343dfc209d0Smioddone
344dfc209d0Smiod
3458f8fdbefSderaadt# Use loopback, not the wire.
3468f8fdbefSderaadtroute -qn add -host $hostname 127.0.0.1 > /dev/null
3478f8fdbefSderaadtroute -qn add -net 127 127.0.0.1 -reject > /dev/null
3488f8fdbefSderaadt
349dfc209d0Smiod# Configure all the bridges.
350dfc209d0Smiodfor bn in /etc/bridgename.*; do
351dfc209d0Smiod	# Strip off /etc/bridgename. prefix
352dfc209d0Smiod	if=${bn#/etc/bridgename.}
3537fd32e5eStodd	test "$if" = "*" && continue
354dfc209d0Smiod
355dfc209d0Smiod	bridgestart $if
356dfc209d0Smioddone
357