xref: /openbsd/etc/netstart (revision 7e24f925)
1df930be7Sderaadt#!/bin/sh -
2df930be7Sderaadt#
3*7e24f925Stodd#	$OpenBSD: netstart,v 1.55 2000/01/02 04:38:17 todd 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
563d8fed7cSitojunelse
573d8fed7cSitojun	ip6kernel=NO
583d8fed7cSitojunfi
593d8fed7cSitojun
6098c28033Skstailey# configure all of the non-loopback interfaces which we know about.
615cbd6651Sderaadt# refer to hostname.if(5) and bridgename.if(5)
6204e0ac27Smillertfor hn in /etc/hostname.*; do
6304e0ac27Smillert    # Strip off /etc/hostname. prefix
6404e0ac27Smillert    if=${hn#/etc/hostname.}
65df930be7Sderaadt
6604e0ac27Smillert    # Interface names must be alphanumeric only.  We check to avoid
6704e0ac27Smillert    # configuring backup or temp files, and to catch the "*" case.
6804e0ac27Smillert    if ! isalphanumeric "$if"; then
6904e0ac27Smillert	continue
7004e0ac27Smillert    fi
715cbd6651Sderaadt    ifconfig $if > /dev/null 2>&1
72238ba7abSangelos    if [ "$?" != "0" ]; then
73238ba7abSangelos	continue
74238ba7abSangelos    fi
75238ba7abSangelos
7604e0ac27Smillert    # Now parse the hostname.* file
77*7e24f925Stodd    while :; do
78*7e24f925Stodd	if [ "$cmd2" ]; then
79*7e24f925Stodd	    # we are carrying over from the 'read dt dtaddr' last time
80*7e24f925Stodd	    set -- $cmd2
81*7e24f925Stodd	    af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" ext2="$6"
82*7e24f925Stodd	    cmd2=
83*7e24f925Stodd	else
84*7e24f925Stodd	    # read the next line or exit the while loop
85*7e24f925Stodd	    read af name mask bcaddr ext1 ext2 || break
86*7e24f925Stodd	fi
87*7e24f925Stodd	# skip comments
88*7e24f925Stodd	[ "${af#*#}" = "${af}" ] || continue
895cbd6651Sderaadt	# $af can be either "dhcp", "up" or an address family.
90cfa67c92Sniklas	case "$af" in
91cfa67c92Sniklas	"bridge")
925cbd6651Sderaadt	    cmd="echo ${hn}: bridges now supported via bridgename.* files"
93cfa67c92Sniklas	    ;;
94cfa67c92Sniklas	"dhcp")
95*7e24f925Stodd	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
965cbd6651Sderaadt	    cmd="dhclient $if"
97cfa67c92Sniklas	    ;;
98cfa67c92Sniklas	"up")
99cfa67c92Sniklas	    # The only one of these guaranteed to be set is $if
1005cbd6651Sderaadt	    # the remaining ones exist so that media controls work
101*7e24f925Stodd	    cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
102cfa67c92Sniklas	    ;;
103cfa67c92Sniklas	*)
104cfa67c92Sniklas	    read dt dtaddr
105*7e24f925Stodd	    if [ "$name"  = "alias" ]; then
106*7e24f925Stodd		# perform a 'shift' of sorts
107*7e24f925Stodd		alias=$name
108*7e24f925Stodd		name=$mask
109*7e24f925Stodd		mask=$bcaddr
110*7e24f925Stodd		bcaddr=$ext1
111*7e24f925Stodd		ext1=$ext2
112*7e24f925Stodd		ext2=
113df930be7Sderaadt	    fi
114*7e24f925Stodd	    cmd="ifconfig $if $af $alias $name "
115*7e24f925Stodd	    case $dt in
116*7e24f925Stodd	    dest)
117*7e24f925Stodd		cmd="$cmd $dtaddr"
118*7e24f925Stodd		;;
119*7e24f925Stodd	    [a-z]*)
120*7e24f925Stodd		cmd2="$dt $dtaddr"
121cfa67c92Sniklas		;;
122cfa67c92Sniklas	     esac
123*7e24f925Stodd	     if [ ! -n "$name" ]; then
124*7e24f925Stodd		    echo "/etc/hostname.$if: invalid network configuration file"
125*7e24f925Stodd		return
126*7e24f925Stodd	     fi
127*7e24f925Stodd	     case $af in
128*7e24f925Stodd	     inet)
129*7e24f925Stodd		[ "$mask" ] && cmd="$cmd netmask $mask"
130*7e24f925Stodd		if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
131*7e24f925Stodd		    cmd="$cmd broadcast $bcaddr"
132*7e24f925Stodd		fi
133*7e24f925Stodd		[ "$alias" ] && rtcmd="; route -n add -host $name 127.0.0.1"
134*7e24f925Stodd	     ;;
135*7e24f925Stodd	     inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
136*7e24f925Stodd		cmd="$cmd $bcaddr"
137*7e24f925Stodd		;;
138*7e24f925Stodd	     *) cmd="$cmd $mask $bcaddr"
139*7e24f925Stodd	     esac
140*7e24f925Stodd	     cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
141*7e24f925Stodd	     ;;
142*7e24f925Stodd	esac
143cfa67c92Sniklas	eval "$cmd"
144*7e24f925Stodd    done < /etc/hostname.$if
145df930be7Sderaadtdone
1465cbd6651Sderaadtfor bn in /etc/bridgename.*; do
1475cbd6651Sderaadt    # Strip off /etc/bridgename. prefix
1485cbd6651Sderaadt    if=${bn#/etc/bridgename.}
1495cbd6651Sderaadt
1505cbd6651Sderaadt    # Interface names must be alphanumeric only.  We check to avoid
1515cbd6651Sderaadt    # configuring backup or temp files, and to catch the "*" case.
1525cbd6651Sderaadt    if ! isalphanumeric "$if"; then
1535cbd6651Sderaadt        continue
1545cbd6651Sderaadt    fi
1555cbd6651Sderaadt    brconfig $if > /dev/null 2>&1
1565cbd6651Sderaadt    if [ "$?" != "0" ]; then
1575cbd6651Sderaadt	continue
1585cbd6651Sderaadt    fi
1595cbd6651Sderaadt
1605cbd6651Sderaadt    # Now parse the bridgename.* file
1615cbd6651Sderaadt    {
1625cbd6651Sderaadt	# All lines are run as brconfig(8) commands.
1635cbd6651Sderaadt	while read line ; do
164601f2fa9Sderaadt	    line=${line%%#*}		# strip comments
165601f2fa9Sderaadt	    test -z "$line" && continue
1665cbd6651Sderaadt	    brconfig $if $line
1675cbd6651Sderaadt	done
1685cbd6651Sderaadt    } < /etc/bridgename.$if
1695cbd6651Sderaadtdone
170df930be7Sderaadt
171d747464dSderaadt# /etc/mygate, if it exists, contains the name of my gateway host
172d747464dSderaadt# that name must be in /etc/hosts.
173d747464dSderaadtif [ -f /etc/mygate ]; then
174bd498c62Sderaadt	route -n add -host default `cat /etc/mygate`
175a4f0e6c1Sdownsjfi
176cf3860a5Sderaadt
177745634aaSniklas# Multicast routing.
178745634aaSniklas#
179745634aaSniklas# The routing to the 224.0.0.0/4 net is setup according to these rules:
180745634aaSniklas# multicast_host	multicast_router	route		comment
181745634aaSniklas# NO			NO			-reject		no multicast
182745634aaSniklas# NO			YES			none installed	daemon will run
183745634aaSniklas# YES/interface		NO			-interface	YES=def. iface
184745634aaSniklas#	   Any other combination		-reject		config error
185745634aaSniklascase "$multicast_host:$multicast_router" in
186745634aaSniklasNO:NO)
187745634aaSniklas	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
188745634aaSniklasNO:YES)
189745634aaSniklas	;;
190745634aaSniklas*:NO)
191745634aaSniklas	set `if [ $multicast_host = YES ]; then
192745634aaSniklas		ed -s '!route -n show' <<EOF
193745634aaSniklas/^default/p
194745634aaSniklasEOF
195745634aaSniklas	else
196745634aaSniklas		ed -s "!ifconfig $multicast_host" <<EOF
197745634aaSniklas/^	inet /p
198745634aaSniklasEOF
199745634aaSniklas	fi`
200745634aaSniklas	route -n add -net 224.0.0.0/4 -interface $2;;
201745634aaSniklas*:*)
202745634aaSniklas	echo 'config error, multicasting disabled until rc.conf is fixed'
203745634aaSniklas	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
204745634aaSniklasesac
205745634aaSniklas
206cf3860a5Sderaadt# Configure NAT after configuring network interfaces
207cf3860a5Sderaadtif [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
208cf3860a5Sderaadt	echo 'configuring NAT'
209cf3860a5Sderaadt	ipnat -CF -f ${ipnat_rules}
210cf3860a5Sderaadtelse
211cf3860a5Sderaadt	ipnat=NO
212cf3860a5Sderaadtfi
213