xref: /openbsd/etc/netstart (revision 4d472c97)
1df930be7Sderaadt#!/bin/sh -
2df930be7Sderaadt#
3*4d472c97Smillert#	$OpenBSD: netstart,v 1.74 2000/11/27 17:14:00 millert 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
19df930be7Sderaadt# /etc/myname contains my symbolic name
20df930be7Sderaadt#
21df930be7Sderaadthostname=`cat /etc/myname`
22df930be7Sderaadthostname $hostname
23df930be7Sderaadtif [ -f /etc/defaultdomain ]; then
24df930be7Sderaadt	domainname `cat /etc/defaultdomain`
25df930be7Sderaadtfi
26df930be7Sderaadt
277fafbaa4Sderaadt# pick up option configuration
287fafbaa4Sderaadt. /etc/rc.conf
297fafbaa4Sderaadt
309a844b63Sdm# Configure the IP filter before configuring network interfaces
319a844b63Sdmif [ X"${ipfilter}" = X"YES" -a -f "${ipfilter_rules}" ]; then
329a844b63Sdm	echo 'configuring IP filter'
33a4c78e88Smillert	ipf -Fa -f ${ipfilter_rules}
349a844b63Sdmelse
359a844b63Sdm	ipfilter=NO
369a844b63Sdmfi
379a844b63Sdm
3898c28033Skstailey# set the address for the loopback interface
3962858998Sitojun# it will also initialize IPv6 address for lo0 (::1 and others).
4098c28033Skstaileyifconfig lo0 inet localhost
41ead8d7f6Skstailey
4298c28033Skstailey# use loopback, not the wire
43fe32e9eaSderaadtroute -n add -host $hostname localhost > /dev/null
44fe32e9eaSderaadtroute -n add -net 127 127.0.0.1 -reject > /dev/null
4598c28033Skstailey
463d8fed7cSitojunif ifconfig lo0 inet6 >/dev/null 2>&1; then
473d8fed7cSitojun	# IPv6 configurations.
483d8fed7cSitojun	ip6kernel=YES
493d8fed7cSitojun
5092aceabbSitojun	# disallow link-local unicast dest without outgoing scope identifiers.
51fe32e9eaSderaadt	route add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
5292aceabbSitojun
5392aceabbSitojun	# disallow site-local unicast dest without outgoing scope identifiers..
5492aceabbSitojun	# If you configure site-locals without scope id (it is permissible
5592aceabbSitojun	# config for routers that are not on scope boundary), you may want
5692aceabbSitojun	# to comment the line out.
57fe32e9eaSderaadt	route add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
5892aceabbSitojun
593d8fed7cSitojun	# disallow "internal" addresses to appear on the wire.
60fe32e9eaSderaadt	route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
6192aceabbSitojun
6292aceabbSitojun	# disallow packets to malicious IPv4 compatible prefix.
63fe32e9eaSderaadt	route add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
64fe32e9eaSderaadt	route add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
65fe32e9eaSderaadt	route add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
66fe32e9eaSderaadt	route add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
6792aceabbSitojun
6892aceabbSitojun	# disallow packets to malicious 6to4 prefix.
69fe32e9eaSderaadt	route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
70fe32e9eaSderaadt	route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
71fe32e9eaSderaadt	route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
72fe32e9eaSderaadt	route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
7392aceabbSitojun
7492aceabbSitojun	# Completely disallow packets to IPv4 compatible prefix.
7592aceabbSitojun	# This may conflict with RFC1933 under following circumstances:
7692aceabbSitojun	# (1) An IPv6-only KAME node tries to originate packets to IPv4
7792aceabbSitojun	#     comatible destination.  The KAME node has no IPv4 compatible
7892aceabbSitojun	#     support.  Under RFC1933, it should transmit native IPv6
7992aceabbSitojun	#     packets toward IPv4 compatible destination, hoping it would
8092aceabbSitojun	#     reach a router that forwards the packet toward auto-tunnel
8192aceabbSitojun	#     interface.
8292aceabbSitojun	# (2) An IPv6-only node originates a packet to IPv4 compatible
8392aceabbSitojun	#     destination.  A KAME node is acting as an IPv6 router, and
8492aceabbSitojun	#     asked to forward it.
8592aceabbSitojun	# Due to rare use of IPv4 compatible address, and security issues
8692aceabbSitojun	# with it, we disable it by default.
87fe32e9eaSderaadt	route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
8882c17b75Sitojun
8982c17b75Sitojun	rtsolif=""
903d8fed7cSitojunelse
913d8fed7cSitojun	ip6kernel=NO
923d8fed7cSitojunfi
933d8fed7cSitojun
9498c28033Skstailey# configure all of the non-loopback interfaces which we know about.
955cbd6651Sderaadt# refer to hostname.if(5) and bridgename.if(5)
9604e0ac27Smillertfor hn in /etc/hostname.*; do
9704e0ac27Smillert    # Strip off /etc/hostname. prefix
9804e0ac27Smillert    if=${hn#/etc/hostname.}
99df930be7Sderaadt
10004e0ac27Smillert    # Interface names must be alphanumeric only.  We check to avoid
10104e0ac27Smillert    # configuring backup or temp files, and to catch the "*" case.
10204e0ac27Smillert    if ! isalphanumeric "$if"; then
10304e0ac27Smillert	continue
10404e0ac27Smillert    fi
1055cbd6651Sderaadt    ifconfig $if > /dev/null 2>&1
106238ba7abSangelos    if [ "$?" != "0" ]; then
107238ba7abSangelos	continue
108238ba7abSangelos    fi
109238ba7abSangelos
11004e0ac27Smillert    # Now parse the hostname.* file
1117e24f925Stodd    while :; do
1127e24f925Stodd	if [ "$cmd2" ]; then
1137e24f925Stodd	    # we are carrying over from the 'read dt dtaddr' last time
1147e24f925Stodd	    set -- $cmd2
1156bbb0011Stodd	    af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
1166bbb0011Stodd	    # make sure and get any remaining args in ext2, like the read below
117*4d472c97Smillert	    i=1; while [ i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
1186bbb0011Stodd	    ext2="$@"
1197e24f925Stodd	else
1207e24f925Stodd	    # read the next line or exit the while loop
1217e24f925Stodd	    read af name mask bcaddr ext1 ext2 || break
1227e24f925Stodd	fi
12352369710Stodd	# $af can be "dhcp", "up", "rtsol", an address family, commands, or
12452369710Stodd	# a comment.
125cfa67c92Sniklas	case "$af" in
12652369710Stodd	"#"*) # skip comments
12752369710Stodd	    continue
12852369710Stodd	    ;;
12952369710Stodd	"!"*) # parse commands
13052369710Stodd	    cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
13152369710Stodd	    ;;
132cfa67c92Sniklas	"bridge")
1335cbd6651Sderaadt	    cmd="echo ${hn}: bridges now supported via bridgename.* files"
134cfa67c92Sniklas	    ;;
135cfa67c92Sniklas	"dhcp")
136e5694912Stodd	    [ "$name" = "NONE" ] && name=
137e5694912Stodd	    [ "$mask" = "NONE" ] && mask=
138e5694912Stodd	    [ "$bcaddr" = "NONE" ] && bcaddr=
1397e24f925Stodd	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
1405cbd6651Sderaadt	    cmd="dhclient $if"
141cfa67c92Sniklas	    ;;
14282c17b75Sitojun	"rtsol")
143fc6da205Sderaadt	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 up
14482c17b75Sitojun	    rtsolif="$rtsolif $if"
145065e4ce0Stodd	    cmd=
14682c17b75Sitojun	    ;;
147cfa67c92Sniklas	"up")
148cfa67c92Sniklas	    # The only one of these guaranteed to be set is $if
1495cbd6651Sderaadt	    # the remaining ones exist so that media controls work
1507e24f925Stodd	    cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
151cfa67c92Sniklas	    ;;
152cfa67c92Sniklas	*)
153cfa67c92Sniklas	    read dt dtaddr
1547e24f925Stodd	    if [ "$name"  = "alias" ]; then
1557e24f925Stodd		# perform a 'shift' of sorts
1567e24f925Stodd		alias=$name
1577e24f925Stodd		name=$mask
1587e24f925Stodd		mask=$bcaddr
1597e24f925Stodd		bcaddr=$ext1
1607e24f925Stodd		ext1=$ext2
1617e24f925Stodd		ext2=
1623b6b8bcbStodd	    else
1633b6b8bcbStodd		alias=
164df930be7Sderaadt	    fi
1657e24f925Stodd	    cmd="ifconfig $if $af $alias $name "
166475b4bf4Stodd	    case "$dt" in
1677e24f925Stodd	    dest)
1687e24f925Stodd		cmd="$cmd $dtaddr"
1697e24f925Stodd		;;
170475b4bf4Stodd	    [a-z!]*)
1717e24f925Stodd		cmd2="$dt $dtaddr"
172cfa67c92Sniklas		;;
173cfa67c92Sniklas	    esac
1747e24f925Stodd	    if [ ! -n "$name" ]; then
1757e24f925Stodd		    echo "/etc/hostname.$if: invalid network configuration file"
1767e24f925Stodd		return
1777e24f925Stodd	    fi
1787e24f925Stodd	    case $af in
1797e24f925Stodd	    inet)
1807e24f925Stodd		[ "$mask" ] && cmd="$cmd netmask $mask"
1817e24f925Stodd		if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
1827e24f925Stodd		    cmd="$cmd broadcast $bcaddr"
1837e24f925Stodd		fi
1847e24f925Stodd		[ "$alias" ] && rtcmd="; route -n add -host $name 127.0.0.1"
1857e24f925Stodd		;;
1867e24f925Stodd	    inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
1877e24f925Stodd		cmd="$cmd $bcaddr"
1887e24f925Stodd		;;
1897e24f925Stodd	    *) cmd="$cmd $mask $bcaddr"
1907e24f925Stodd	    esac
1917e24f925Stodd	    cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
1927e24f925Stodd	    ;;
1937e24f925Stodd	esac
194cfa67c92Sniklas	eval "$cmd"
1957e24f925Stodd    done < /etc/hostname.$if
196df930be7Sderaadtdone
19782c17b75Sitojun
19882c17b75Sitojunif [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
19982c17b75Sitojun	fw=`sysctl -n net.inet6.ip6.forwarding`
20082c17b75Sitojun	ra=`sysctl -n net.inet6.ip6.accept_rtadv`
20182c17b75Sitojun	if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
202d56849f2Sitojun		echo "IPv6 autoconf:$rtsolif"
20382c17b75Sitojun		rtsol $rtsolif
20482c17b75Sitojun	else
20582c17b75Sitojun		echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
20682c17b75Sitojun	fi
20782c17b75Sitojunfi
208d56849f2Sitojunif [ "$ip6kernel" = "YES" ]; then
209d56849f2Sitojun	# this is to make sure DAD is completed before going further.
210d56849f2Sitojun	sleep `sysctl -n net.inet6.ip6.dad_count`
211d56849f2Sitojun	sleep 1
212d56849f2Sitojunfi
21382c17b75Sitojun
2145cbd6651Sderaadtfor bn in /etc/bridgename.*; do
2155cbd6651Sderaadt    # Strip off /etc/bridgename. prefix
2165cbd6651Sderaadt    if=${bn#/etc/bridgename.}
2175cbd6651Sderaadt
2185cbd6651Sderaadt    # Interface names must be alphanumeric only.  We check to avoid
2195cbd6651Sderaadt    # configuring backup or temp files, and to catch the "*" case.
2205cbd6651Sderaadt    if ! isalphanumeric "$if"; then
2215cbd6651Sderaadt        continue
2225cbd6651Sderaadt    fi
2235cbd6651Sderaadt    brconfig $if > /dev/null 2>&1
2245cbd6651Sderaadt    if [ "$?" != "0" ]; then
2255cbd6651Sderaadt	continue
2265cbd6651Sderaadt    fi
2275cbd6651Sderaadt
2285cbd6651Sderaadt    # Now parse the bridgename.* file
2295cbd6651Sderaadt    {
2305cbd6651Sderaadt	# All lines are run as brconfig(8) commands.
2315cbd6651Sderaadt	while read line ; do
232601f2fa9Sderaadt	    line=${line%%#*}		# strip comments
233601f2fa9Sderaadt	    test -z "$line" && continue
2345cbd6651Sderaadt	    brconfig $if $line
2355cbd6651Sderaadt	done
2365cbd6651Sderaadt    } < /etc/bridgename.$if
2375cbd6651Sderaadtdone
238df930be7Sderaadt
239d747464dSderaadt# /etc/mygate, if it exists, contains the name of my gateway host
240d747464dSderaadt# that name must be in /etc/hosts.
241d747464dSderaadtif [ -f /etc/mygate ]; then
242bd498c62Sderaadt	route -n add -host default `cat /etc/mygate`
243a4f0e6c1Sdownsjfi
244cf3860a5Sderaadt
245745634aaSniklas# Multicast routing.
246745634aaSniklas#
247745634aaSniklas# The routing to the 224.0.0.0/4 net is setup according to these rules:
248745634aaSniklas# multicast_host	multicast_router	route		comment
249745634aaSniklas# NO			NO			-reject		no multicast
250745634aaSniklas# NO			YES			none installed	daemon will run
251745634aaSniklas# YES/interface		NO			-interface	YES=def. iface
252745634aaSniklas#	   Any other combination		-reject		config error
253745634aaSniklascase "$multicast_host:$multicast_router" in
254745634aaSniklasNO:NO)
255fe32e9eaSderaadt	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject> /dev/null
256fe32e9eaSderaadt	;;
257745634aaSniklasNO:YES)
258745634aaSniklas	;;
259745634aaSniklas*:NO)
260745634aaSniklas	set `if [ $multicast_host = YES ]; then
261745634aaSniklas		ed -s '!route -n show' <<EOF
262745634aaSniklas/^default/p
263745634aaSniklasEOF
264745634aaSniklas	else
265745634aaSniklas		ed -s "!ifconfig $multicast_host" <<EOF
266745634aaSniklas/^	inet /p
267745634aaSniklasEOF
268745634aaSniklas	fi`
269fe32e9eaSderaadt	route -n add -net 224.0.0.0/4 -interface $2 > /dev/null
270fe32e9eaSderaadt	;;
271745634aaSniklas*:*)
272745634aaSniklas	echo 'config error, multicasting disabled until rc.conf is fixed'
273fe32e9eaSderaadt	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
274fe32e9eaSderaadt	;;
275745634aaSniklasesac
276745634aaSniklas
277cf3860a5Sderaadt# Configure NAT after configuring network interfaces
278cf3860a5Sderaadtif [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
279cf3860a5Sderaadt	echo 'configuring NAT'
280cf3860a5Sderaadt	ipnat -CF -f ${ipnat_rules}
281cf3860a5Sderaadtelse
282cf3860a5Sderaadt	ipnat=NO
283cf3860a5Sderaadtfi
284