xref: /openbsd/etc/netstart (revision 071f497c)
1df930be7Sderaadt#!/bin/sh -
2df930be7Sderaadt#
3*071f497cSmpi#	$OpenBSD: netstart,v 1.139 2013/08/22 07:53:11 mpi Exp $
48fc5e153Smillert
58fc5e153Smillert# Strip comments (and leading/trailing whitespace if IFS is set)
68fc5e153Smillert# from a file and spew to stdout
78fc5e153Smillertstripcom() {
8371a327cStodd	local _l
97c4030c5Stodd	[[ -f $1 ]] || return
10371a327cStodd	while read _l; do
11371a327cStodd		[[ -n ${_l%%#*} ]] && echo $_l
12371a327cStodd	done<$1
138fc5e153Smillert}
1404e0ac27Smillert
15dfc209d0Smiod# Start the $1 interface
16dfc209d0Smiodifstart() {
17f5319bdcSderaadt	if=$1
18dfc209d0Smiod	# Interface names must be alphanumeric only.  We check to avoid
19dfc209d0Smiod	# configuring backup or temp files, and to catch the "*" case.
2052d1b2ceSrpe	[[ $if != +([[:alpha:]])+([[:digit:]]) ]] && return
21dfc209d0Smiod
22bc53e65aSderaadt	file=/etc/hostname.$if
2349352c7bStodd	if ! [ -f $file ]; then
2449352c7bStodd		echo "netstart: $file: No such file or directory"
2549352c7bStodd		return
2649352c7bStodd	fi
2741e7db73Ssthen	# Not using stat(1), we can't rely on having /usr yet
2841e7db73Ssthen	set -A stat -- `ls -nL $file`
2941e7db73Ssthen	if [ "${stat[0]#???????} ${stat[2]} ${stat[3]}" != "--- 0 0" ]; then
30bc53e65aSderaadt		echo "WARNING: $file is insecure, fixing permissions"
3131ecad01Ssthen		chmod -LR o-rwx $file
3231ecad01Ssthen		chown -LR root.wheel $file
33bc53e65aSderaadt	fi
34d7c3aec1Srpe	# Check for ifconfig'able interface.
35d7c3aec1Srpe	(ifconfig $if || ifconfig $if create) >/dev/null 2>&1 || return
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
479e0429c5Ssimon			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		"dhcp")
63dfc209d0Smiod			[ "$name" = "NONE" ] && name=
64dfc209d0Smiod			[ "$mask" = "NONE" ] && mask=
65dfc209d0Smiod			[ "$bcaddr" = "NONE" ] && bcaddr=
6619d144d8Stodd			cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 down"
6719d144d8Stodd			cmd="$cmd;dhclient $if"
687c4030c5Stodd			dhcpif="$dhcpif $if"
69dfc209d0Smiod			;;
70dfc209d0Smiod		"rtsol")
71f5319bdcSderaadt			rtsolif="$rtsolif $if"
7219d144d8Stodd			cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
73dfc209d0Smiod			;;
74dfc209d0Smiod		*)
75dfc209d0Smiod			read dt dtaddr
76dfc209d0Smiod			if [ "$name"  = "alias" ]; then
77dfc209d0Smiod				# perform a 'shift' of sorts
78dfc209d0Smiod				alias=$name
79dfc209d0Smiod				name=$mask
80dfc209d0Smiod				mask=$bcaddr
81dfc209d0Smiod				bcaddr=$ext1
82dfc209d0Smiod				ext1=$ext2
83dfc209d0Smiod				ext2=
84dfc209d0Smiod			else
85dfc209d0Smiod				alias=
86dfc209d0Smiod			fi
87f5319bdcSderaadt			cmd="ifconfig $if $af $alias $name"
88dfc209d0Smiod			case "$dt" in
89dfc209d0Smiod			dest)
90dfc209d0Smiod				cmd="$cmd $dtaddr"
91dfc209d0Smiod				;;
9242a8ea81Stodd			*)
93dfc209d0Smiod				cmd2="$dt $dtaddr"
94dfc209d0Smiod				;;
95dfc209d0Smiod			esac
96dfc209d0Smiod			case $af in
97dfc209d0Smiod			inet)
982817040cStodd				if [ ! -n "$name" ]; then
992817040cStodd					echo "/etc/hostname.$if: inet alone is invalid"
1002817040cStodd					return
1012817040cStodd				fi
102dfc209d0Smiod				[ "$mask" ] && cmd="$cmd netmask $mask"
103dfc209d0Smiod				if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
104dfc209d0Smiod					cmd="$cmd broadcast $bcaddr"
105dfc209d0Smiod				fi
106dfc209d0Smiod				;;
1072817040cStodd			inet6)
1082817040cStodd				if [ ! -n "$name" ]; then
1092817040cStodd					echo "/etc/hostname.$if: inet6 alone is invalid"
1102817040cStodd					return
1112817040cStodd				fi
1122817040cStodd				[ "$mask" ] && cmd="$cmd prefixlen $mask"
113dfc209d0Smiod				cmd="$cmd $bcaddr"
114dfc209d0Smiod				;;
115dfc209d0Smiod			*)
116dfc209d0Smiod				cmd="$cmd $mask $bcaddr"
117dfc209d0Smiod				;;
118dfc209d0Smiod			esac
119*071f497cSmpi			cmd="$cmd $ext1 $ext2"
120dfc209d0Smiod			;;
121dfc209d0Smiod		esac
122dfc209d0Smiod		eval "$cmd"
123f5319bdcSderaadt	done < /etc/hostname.$if
124dfc209d0Smiod}
125dfc209d0Smiod
1269ac6b043Stodd# Start multiple:
1279ac6b043Stodd#   start "$1" interfaces in order or all interfaces if empty
1289ac6b043Stodd#   don't start "$2" interfaces
1299ac6b043Stoddifmstart() {
1309ac6b043Stodd	for sif in ${1:-ALL}; do
1319ac6b043Stodd		for hn in /etc/hostname.*; do
1329ac6b043Stodd			# Strip off /etc/hostname. prefix
1339ac6b043Stodd			if=${hn#/etc/hostname.}
1349ac6b043Stodd			test "$if" = "*" && continue
1359ac6b043Stodd
1369ac6b043Stodd			# Skip unwanted ifs
1379ac6b043Stodd			s=""
1389ac6b043Stodd			for xf in $2; do
1399ac6b043Stodd				test "$xf" = "${if%%[0-9]*}" && s="1" && break
1409ac6b043Stodd			done
1419ac6b043Stodd			test "$s" = "1" && continue
1429ac6b043Stodd
1439ac6b043Stodd			# Start wanted ifs
1449ac6b043Stodd			test "$sif" = "ALL" -o \
1459ac6b043Stodd			     "$sif" = "${if%%[0-9]*}" \
1469ac6b043Stodd				&& ifstart $if
1479ac6b043Stodd		done
1489ac6b043Stodd	done
1499ac6b043Stodd}
1509ac6b043Stodd
1510dc37902Sangelos# Re-read /etc/rc.conf
1520dc37902Sangelos. /etc/rc.conf
1530dc37902Sangelos
154dfc209d0Smiod# If we were invoked with a list of interface names, just reconfigure these
155dfc209d0Smiod# interfaces (or bridges) and return.
1569777ac6bSrpeif [[ $1 == autoboot ]]; then
157dfc209d0Smiod	shift
158dfc209d0Smiodfi
159dfc209d0Smiodif [ $# -gt 0 ]; then
160dfc209d0Smiod	while [ $# -gt 0 ]; do
161dfc209d0Smiod		ifstart $1
162dfc209d0Smiod		shift
163dfc209d0Smiod	done
164dfc209d0Smiod	return
165dfc209d0Smiodfi
166dfc209d0Smiod
167dfc209d0Smiod# Otherwise, process with the complete network initialization.
168dfc209d0Smiod
169df930be7Sderaadt# /etc/myname contains my symbolic name
17037bbdc83Shenningif [ -f /etc/myname ]; then
1713de81825Smillert	hostname=`stripcom /etc/myname`
172df930be7Sderaadt	hostname $hostname
17337bbdc83Shenningelse
17437bbdc83Shenning	hostname=`hostname`
17537bbdc83Shenningfi
17637bbdc83Shenning
1774fbd02fcSsobrado# Set the address for the loopback interface.  Bringing the interface up,
1784fbd02fcSsobrado# automatically invokes the IPv6 address ::1.
179d216f73bShenningifconfig lo0 inet 127.0.0.1/8
18098c28033Skstailey
1813d8fed7cSitojunif ifconfig lo0 inet6 >/dev/null 2>&1; then
1823d8fed7cSitojun	# IPv6 configurations.
1833d8fed7cSitojun	ip6kernel=YES
1843d8fed7cSitojun
185dfc209d0Smiod	# Disallow link-local unicast dest without outgoing scope identifiers.
18603056e2eSderaadt	route -qn add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
18792aceabbSitojun
188dfc209d0Smiod	# Disallow site-local unicast dest without outgoing scope identifiers.
18992aceabbSitojun	# If you configure site-locals without scope id (it is permissible
19092aceabbSitojun	# config for routers that are not on scope boundary), you may want
19192aceabbSitojun	# to comment the line out.
19203056e2eSderaadt	route -qn add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
19392aceabbSitojun
194dfc209d0Smiod	# Disallow "internal" addresses to appear on the wire.
19503056e2eSderaadt	route -qn add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
19692aceabbSitojun
197dfc209d0Smiod	# Disallow packets to malicious IPv4 compatible prefix.
19803056e2eSderaadt	route -qn add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
19903056e2eSderaadt	route -qn add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
20003056e2eSderaadt	route -qn add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
20103056e2eSderaadt	route -qn add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
20292aceabbSitojun
203dfc209d0Smiod	# Disallow packets to malicious 6to4 prefix.
20403056e2eSderaadt	route -qn add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
20503056e2eSderaadt	route -qn add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
20603056e2eSderaadt	route -qn add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
20703056e2eSderaadt	route -qn add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
20892aceabbSitojun
209a4ee3723Sitojun	# Disallow packets without scope identifier.
210a4ee3723Sitojun	route -qn add -inet6 ff01:: -prefixlen 16 ::1 -reject > /dev/null
211a4ee3723Sitojun	route -qn add -inet6 ff02:: -prefixlen 16 ::1 -reject > /dev/null
212a4ee3723Sitojun
21392aceabbSitojun	# Completely disallow packets to IPv4 compatible prefix.
21492aceabbSitojun	# This may conflict with RFC1933 under following circumstances:
21592aceabbSitojun	# (1) An IPv6-only KAME node tries to originate packets to IPv4
2165e268fadSderaadt	#     compatible destination.  The KAME node has no IPv4 compatible
21792aceabbSitojun	#     support.  Under RFC1933, it should transmit native IPv6
21892aceabbSitojun	#     packets toward IPv4 compatible destination, hoping it would
21992aceabbSitojun	#     reach a router that forwards the packet toward auto-tunnel
22092aceabbSitojun	#     interface.
2215e268fadSderaadt	# (2) An IPv6-only node originates a packet to an IPv4 compatible
22292aceabbSitojun	#     destination.  A KAME node is acting as an IPv6 router, and
22392aceabbSitojun	#     asked to forward it.
2245e268fadSderaadt	# Due to rare use of IPv4 compatible addresses, and security issues
22592aceabbSitojun	# with it, we disable it by default.
22603056e2eSderaadt	route -qn add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
22782c17b75Sitojun
22882c17b75Sitojun	rtsolif=""
2293d8fed7cSitojunelse
2303d8fed7cSitojun	ip6kernel=NO
2313d8fed7cSitojunfi
2323d8fed7cSitojun
233df930be7Sderaadt
2349ac6b043Stodd# Configure all the non-loopback interfaces which we know about, but
2354eb97611Sderaadt# do not start interfaces which must be delayed. Refer to hostname.if(5)
236f45bd3bdSmpfifmstart "" "trunk svlan vlan carp gif gre pfsync pppoe tun bridge"
23782c17b75Sitojun
2384bdcd471Sbrad# The trunk interfaces need to come up first in this list.
239f45bd3bdSmpf# The (s)vlan interfaces need to come up after trunk.
2404bdcd471Sbrad# Configure all the carp interfaces which we know about before default route.
241f45bd3bdSmpfifmstart "trunk svlan vlan carp"
2424bdcd471Sbrad
24382c17b75Sitojunif [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
24482c17b75Sitojun	fw=`sysctl -n net.inet6.ip6.forwarding`
24582c17b75Sitojun	ra=`sysctl -n net.inet6.ip6.accept_rtadv`
24682c17b75Sitojun	if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
247d56849f2Sitojun		echo "IPv6 autoconf:$rtsolif"
24882c17b75Sitojun		rtsol $rtsolif
24982c17b75Sitojun	else
25082c17b75Sitojun		echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
25182c17b75Sitojun	fi
25282c17b75Sitojunfi
25382c17b75Sitojun
25480819bdcStodd# Look for default routes in /etc/mygate.
2557c4030c5Stodd[[ -z $dhcpif ]] && stripcom /etc/mygate | while read gw; do
2567c4030c5Stodd		[[ $gw == @(*:*) ]] && continue
2572fcef345Sderaadt		route -qn delete default > /dev/null 2>&1
2587c4030c5Stodd		route -qn add -host default $gw && break
2590408d58aStodddone
2607c4030c5Stodd[[ -z $rtsolif ]] && stripcom /etc/mygate | while read gw; do
2617c4030c5Stodd		[[ $gw == !(*:*) ]] && continue
2627c4030c5Stodd		route -qn delete -inet6 default > /dev/null 2>&1
2637c4030c5Stodd		route -qn add -host -inet6 default $gw && break
2647c4030c5Stodddone
265cf3860a5Sderaadt
266745634aaSniklas# Multicast routing.
267745634aaSniklas#
268745634aaSniklas# The routing to the 224.0.0.0/4 net is setup according to these rules:
269745634aaSniklas# multicast_host	multicast_router	route		comment
270745634aaSniklas# NO			NO			-reject		no multicast
271745634aaSniklas# NO			YES			none installed	daemon will run
272745634aaSniklas# YES/interface		NO			-interface	YES=def. iface
273745634aaSniklas#	   Any other combination		-reject		config error
274705fcffdSreykroute -qn delete 224.0.0.0/4 > /dev/null 2>&1
275745634aaSniklascase "$multicast_host:$multicast_router" in
276745634aaSniklasNO:NO)
277f4b4b73bSderaadt	route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
278fe32e9eaSderaadt	;;
279745634aaSniklasNO:YES)
280745634aaSniklas	;;
281745634aaSniklas*:NO)
282705fcffdSreyk	maddr=`if [ "$multicast_host" = "YES" ]; then
283f4b4b73bSderaadt		ed -s '!route -qn show -inet' <<EOF
284745634aaSniklas/^default/p
285745634aaSniklasEOF
286745634aaSniklas	else
287745634aaSniklas		ed -s "!ifconfig $multicast_host" <<EOF
288745634aaSniklas/^	inet /p
289745634aaSniklasEOF
290705fcffdSreyk	fi 2> /dev/null`
291705fcffdSreyk	if [ "X${maddr}" != "X" ]; then
292705fcffdSreyk		set $maddr
293f4b4b73bSderaadt		route -qn add -net 224.0.0.0/4 -interface $2 > /dev/null
294705fcffdSreyk	else
295705fcffdSreyk		route -qn add -net 224.0.0.0/4 -interface \
296705fcffdSreyk			127.0.0.1 -reject > /dev/null
297705fcffdSreyk	fi
298fe32e9eaSderaadt	;;
299745634aaSniklas*:*)
300745634aaSniklas	echo 'config error, multicasting disabled until rc.conf is fixed'
301f4b4b73bSderaadt	route -qn add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
302fe32e9eaSderaadt	;;
303745634aaSniklasesac
304dfc209d0Smiod
305dfc209d0Smiod
30648e07d19Sjdixon# Configure PPPoE, GIF, GRE and TUN interfaces, delayed because they require
30748e07d19Sjdixon# routes to be set.  TUN might depend on PPPoE, and GIF or GRE may depend on
30848e07d19Sjdixon# either of them.
3094eb97611Sderaadtifmstart "pppoe tun gif gre bridge"
310dfc209d0Smiod
31125d2fb80Sitojun# reject 127/8 other than 127.0.0.1
3128f8fdbefSderaadtroute -qn add -net 127 127.0.0.1 -reject > /dev/null
3138f8fdbefSderaadt
314089287c3Sdavidif [ "$ip6kernel" = "YES" ]; then
315089287c3Sdavid	# this is to make sure DAD is completed before going further.
316c653ce7bSmarkus	count=0
317c653ce7bSmarkus	while [ $((count++)) -lt 10 -a "x"`sysctl -n net.inet6.ip6.dad_pending` != "x0" ]; do
318c653ce7bSmarkus		sleep 1
319c653ce7bSmarkus	done
320089287c3Sdavidfi
321