xref: /openbsd/etc/netstart (revision fc6da205)
1df930be7Sderaadt#!/bin/sh -
2df930be7Sderaadt#
3*fc6da205Sderaadt#	$OpenBSD: netstart,v 1.62 2000/01/02 06:50:09 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
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'
339a844b63Sdm	ipf -Fa -f ${ipfilter_rules} -E
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
43bd498c62Sderaadtroute -n add -host $hostname localhost
44bd498c62Sderaadtroute -n add -net 127 127.0.0.1 -reject
4598c28033Skstailey
463d8fed7cSitojunif ifconfig lo0 inet6 >/dev/null 2>&1; then
473d8fed7cSitojun	# IPv6 configurations.
483d8fed7cSitojun	ip6kernel=YES
493d8fed7cSitojun
503d8fed7cSitojun	# disallow scoped unicast dest without outgoing scope identifiers.
513d8fed7cSitojun	route add -inet6 fe80:: -prefixlen 10 ::1 -reject
523d8fed7cSitojun	route add -inet6 fc80:: -prefixlen 10 ::1 -reject
533d8fed7cSitojun	# disallow "internal" addresses to appear on the wire.
543d8fed7cSitojun	route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject
553d8fed7cSitojun	route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject
5682c17b75Sitojun
5782c17b75Sitojun	rtsolif=""
583d8fed7cSitojunelse
593d8fed7cSitojun	ip6kernel=NO
603d8fed7cSitojunfi
613d8fed7cSitojun
6298c28033Skstailey# configure all of the non-loopback interfaces which we know about.
635cbd6651Sderaadt# refer to hostname.if(5) and bridgename.if(5)
6404e0ac27Smillertfor hn in /etc/hostname.*; do
6504e0ac27Smillert    # Strip off /etc/hostname. prefix
6604e0ac27Smillert    if=${hn#/etc/hostname.}
67df930be7Sderaadt
6804e0ac27Smillert    # Interface names must be alphanumeric only.  We check to avoid
6904e0ac27Smillert    # configuring backup or temp files, and to catch the "*" case.
7004e0ac27Smillert    if ! isalphanumeric "$if"; then
7104e0ac27Smillert	continue
7204e0ac27Smillert    fi
735cbd6651Sderaadt    ifconfig $if > /dev/null 2>&1
74238ba7abSangelos    if [ "$?" != "0" ]; then
75238ba7abSangelos	continue
76238ba7abSangelos    fi
77238ba7abSangelos
7804e0ac27Smillert    # Now parse the hostname.* file
797e24f925Stodd    while :; do
807e24f925Stodd	if [ "$cmd2" ]; then
817e24f925Stodd	    # we are carrying over from the 'read dt dtaddr' last time
827e24f925Stodd	    set -- $cmd2
837e24f925Stodd	    af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" ext2="$6"
847e24f925Stodd	    cmd2=
857e24f925Stodd	else
867e24f925Stodd	    # read the next line or exit the while loop
877e24f925Stodd	    read af name mask bcaddr ext1 ext2 || break
887e24f925Stodd	fi
897e24f925Stodd	# skip comments
907e24f925Stodd	[ "${af#*#}" = "${af}" ] || continue
915cbd6651Sderaadt	# $af can be either "dhcp", "up" or an address family.
92cfa67c92Sniklas	case "$af" in
93cfa67c92Sniklas	"bridge")
945cbd6651Sderaadt	    cmd="echo ${hn}: bridges now supported via bridgename.* files"
95cfa67c92Sniklas	    ;;
96cfa67c92Sniklas	"dhcp")
977e24f925Stodd	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
985cbd6651Sderaadt	    cmd="dhclient $if"
99cfa67c92Sniklas	    ;;
10082c17b75Sitojun	"rtsol")
101*fc6da205Sderaadt	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 up
10282c17b75Sitojun	    rtsolif="$rtsolif $if"
103065e4ce0Stodd	    cmd=
10482c17b75Sitojun	    ;;
105cfa67c92Sniklas	"up")
106cfa67c92Sniklas	    # The only one of these guaranteed to be set is $if
1075cbd6651Sderaadt	    # the remaining ones exist so that media controls work
1087e24f925Stodd	    cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
109cfa67c92Sniklas	    ;;
110cfa67c92Sniklas	*)
111cfa67c92Sniklas	    read dt dtaddr
1127e24f925Stodd	    if [ "$name"  = "alias" ]; then
1137e24f925Stodd		# perform a 'shift' of sorts
1147e24f925Stodd		alias=$name
1157e24f925Stodd		name=$mask
1167e24f925Stodd		mask=$bcaddr
1177e24f925Stodd		bcaddr=$ext1
1187e24f925Stodd		ext1=$ext2
1197e24f925Stodd		ext2=
120df930be7Sderaadt	    fi
1217e24f925Stodd	    cmd="ifconfig $if $af $alias $name "
1227e24f925Stodd	    case $dt in
1237e24f925Stodd	    dest)
1247e24f925Stodd		cmd="$cmd $dtaddr"
1257e24f925Stodd		;;
1267e24f925Stodd	    [a-z]*)
1277e24f925Stodd		cmd2="$dt $dtaddr"
128cfa67c92Sniklas		;;
129cfa67c92Sniklas	    esac
1307e24f925Stodd	    if [ ! -n "$name" ]; then
1317e24f925Stodd		    echo "/etc/hostname.$if: invalid network configuration file"
1327e24f925Stodd		return
1337e24f925Stodd	    fi
1347e24f925Stodd	    case $af in
1357e24f925Stodd	    inet)
1367e24f925Stodd		[ "$mask" ] && cmd="$cmd netmask $mask"
1377e24f925Stodd		if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
1387e24f925Stodd		    cmd="$cmd broadcast $bcaddr"
1397e24f925Stodd		fi
1407e24f925Stodd		[ "$alias" ] && rtcmd="; route -n add -host $name 127.0.0.1"
1417e24f925Stodd		;;
1427e24f925Stodd	    inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
1437e24f925Stodd		cmd="$cmd $bcaddr"
1447e24f925Stodd		;;
1457e24f925Stodd	    *) cmd="$cmd $mask $bcaddr"
1467e24f925Stodd	    esac
1477e24f925Stodd	    cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
1487e24f925Stodd	    ;;
1497e24f925Stodd	esac
150cfa67c92Sniklas	eval "$cmd"
1517e24f925Stodd    done < /etc/hostname.$if
152df930be7Sderaadtdone
15382c17b75Sitojun
15482c17b75Sitojunif [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
15582c17b75Sitojun	fw=`sysctl -n net.inet6.ip6.forwarding`
15682c17b75Sitojun	ra=`sysctl -n net.inet6.ip6.accept_rtadv`
15782c17b75Sitojun	if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
158d56849f2Sitojun		echo "IPv6 autoconf:$rtsolif"
15982c17b75Sitojun		rtsol $rtsolif
16082c17b75Sitojun	else
16182c17b75Sitojun		echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
16282c17b75Sitojun	fi
16382c17b75Sitojunfi
164d56849f2Sitojunif [ "$ip6kernel" = "YES" ]; then
165d56849f2Sitojun	# this is to make sure DAD is completed before going further.
166d56849f2Sitojun	sleep `sysctl -n net.inet6.ip6.dad_count`
167d56849f2Sitojun	sleep 1
168d56849f2Sitojunfi
16982c17b75Sitojun
1705cbd6651Sderaadtfor bn in /etc/bridgename.*; do
1715cbd6651Sderaadt    # Strip off /etc/bridgename. prefix
1725cbd6651Sderaadt    if=${bn#/etc/bridgename.}
1735cbd6651Sderaadt
1745cbd6651Sderaadt    # Interface names must be alphanumeric only.  We check to avoid
1755cbd6651Sderaadt    # configuring backup or temp files, and to catch the "*" case.
1765cbd6651Sderaadt    if ! isalphanumeric "$if"; then
1775cbd6651Sderaadt        continue
1785cbd6651Sderaadt    fi
1795cbd6651Sderaadt    brconfig $if > /dev/null 2>&1
1805cbd6651Sderaadt    if [ "$?" != "0" ]; then
1815cbd6651Sderaadt	continue
1825cbd6651Sderaadt    fi
1835cbd6651Sderaadt
1845cbd6651Sderaadt    # Now parse the bridgename.* file
1855cbd6651Sderaadt    {
1865cbd6651Sderaadt	# All lines are run as brconfig(8) commands.
1875cbd6651Sderaadt	while read line ; do
188601f2fa9Sderaadt	    line=${line%%#*}		# strip comments
189601f2fa9Sderaadt	    test -z "$line" && continue
1905cbd6651Sderaadt	    brconfig $if $line
1915cbd6651Sderaadt	done
1925cbd6651Sderaadt    } < /etc/bridgename.$if
1935cbd6651Sderaadtdone
194df930be7Sderaadt
195d747464dSderaadt# /etc/mygate, if it exists, contains the name of my gateway host
196d747464dSderaadt# that name must be in /etc/hosts.
197d747464dSderaadtif [ -f /etc/mygate ]; then
198bd498c62Sderaadt	route -n add -host default `cat /etc/mygate`
199a4f0e6c1Sdownsjfi
200cf3860a5Sderaadt
201745634aaSniklas# Multicast routing.
202745634aaSniklas#
203745634aaSniklas# The routing to the 224.0.0.0/4 net is setup according to these rules:
204745634aaSniklas# multicast_host	multicast_router	route		comment
205745634aaSniklas# NO			NO			-reject		no multicast
206745634aaSniklas# NO			YES			none installed	daemon will run
207745634aaSniklas# YES/interface		NO			-interface	YES=def. iface
208745634aaSniklas#	   Any other combination		-reject		config error
209745634aaSniklascase "$multicast_host:$multicast_router" in
210745634aaSniklasNO:NO)
211745634aaSniklas	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
212745634aaSniklasNO:YES)
213745634aaSniklas	;;
214745634aaSniklas*:NO)
215745634aaSniklas	set `if [ $multicast_host = YES ]; then
216745634aaSniklas		ed -s '!route -n show' <<EOF
217745634aaSniklas/^default/p
218745634aaSniklasEOF
219745634aaSniklas	else
220745634aaSniklas		ed -s "!ifconfig $multicast_host" <<EOF
221745634aaSniklas/^	inet /p
222745634aaSniklasEOF
223745634aaSniklas	fi`
224745634aaSniklas	route -n add -net 224.0.0.0/4 -interface $2;;
225745634aaSniklas*:*)
226745634aaSniklas	echo 'config error, multicasting disabled until rc.conf is fixed'
227745634aaSniklas	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
228745634aaSniklasesac
229745634aaSniklas
230cf3860a5Sderaadt# Configure NAT after configuring network interfaces
231cf3860a5Sderaadtif [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
232cf3860a5Sderaadt	echo 'configuring NAT'
233cf3860a5Sderaadt	ipnat -CF -f ${ipnat_rules}
234cf3860a5Sderaadtelse
235cf3860a5Sderaadt	ipnat=NO
236cf3860a5Sderaadtfi
237