netstart revision 1.77
1#!/bin/sh -
2#
3#	$OpenBSD: netstart,v 1.77 2001/03/13 21:15:09 deraadt Exp $
4
5# Returns true if $1 contains only alphanumerics
6isalphanumeric() {
7	local _n
8	_n=$1
9	while [ ${#_n} != 0 ]; do
10		case $_n in
11			[A-Za-z0-9]*)	;;
12			*)		return 1;;
13		esac
14		_n=${_n#?}
15	done
16	return 0
17}
18
19# /etc/myname contains my symbolic name
20#
21hostname=`cat /etc/myname`
22hostname $hostname
23if [ -f /etc/defaultdomain ]; then
24	domainname `cat /etc/defaultdomain`
25fi
26
27# pick up option configuration
28. /etc/rc.conf
29
30# Configure the IP filter before configuring network interfaces
31if [ X"${ipfilter}" = X"YES" -a -f "${ipfilter_rules}" ]; then
32	echo 'configuring IP filter'
33	ipf -Fa -f ${ipfilter_rules}
34else
35	ipfilter=NO
36fi
37
38# set the address for the loopback interface
39# it will also initialize IPv6 address for lo0 (::1 and others).
40ifconfig lo0 inet localhost
41
42# use loopback, not the wire
43route -n add -host $hostname localhost > /dev/null
44route -n add -net 127 127.0.0.1 -reject > /dev/null
45
46if ifconfig lo0 inet6 >/dev/null 2>&1; then
47	# IPv6 configurations.
48	ip6kernel=YES
49
50	# disallow link-local unicast dest without outgoing scope identifiers.
51	route add -inet6 fe80:: -prefixlen 10 ::1 -reject > /dev/null
52
53	# disallow site-local unicast dest without outgoing scope identifiers.
54	# If you configure site-locals without scope id (it is permissible
55	# config for routers that are not on scope boundary), you may want
56	# to comment the line out.
57	route add -inet6 fec0:: -prefixlen 10 ::1 -reject > /dev/null
58
59	# disallow "internal" addresses to appear on the wire.
60	route add -inet6 ::ffff:0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
61
62	# disallow packets to malicious IPv4 compatible prefix.
63	route add -inet6 ::224.0.0.0 -prefixlen 100 ::1 -reject > /dev/null
64	route add -inet6 ::127.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
65	route add -inet6 ::0.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
66	route add -inet6 ::255.0.0.0 -prefixlen 104 ::1 -reject > /dev/null
67
68	# disallow packets to malicious 6to4 prefix.
69	route add -inet6 2002:e000:: -prefixlen 20 ::1 -reject > /dev/null
70	route add -inet6 2002:7f00:: -prefixlen 24 ::1 -reject > /dev/null
71	route add -inet6 2002:0000:: -prefixlen 24 ::1 -reject > /dev/null
72	route add -inet6 2002:ff00:: -prefixlen 24 ::1 -reject > /dev/null
73
74	# Completely disallow packets to IPv4 compatible prefix.
75	# This may conflict with RFC1933 under following circumstances:
76	# (1) An IPv6-only KAME node tries to originate packets to IPv4
77	#     compatible destination.  The KAME node has no IPv4 compatible
78	#     support.  Under RFC1933, it should transmit native IPv6
79	#     packets toward IPv4 compatible destination, hoping it would
80	#     reach a router that forwards the packet toward auto-tunnel
81	#     interface.
82	# (2) An IPv6-only node originates a packet to an IPv4 compatible
83	#     destination.  A KAME node is acting as an IPv6 router, and
84	#     asked to forward it.
85	# Due to rare use of IPv4 compatible addresses, and security issues
86	# with it, we disable it by default.
87	route add -inet6 ::0.0.0.0 -prefixlen 96 ::1 -reject > /dev/null
88
89	rtsolif=""
90else
91	ip6kernel=NO
92fi
93
94# configure all of the non-loopback interfaces which we know about.
95# refer to hostname.if(5) and bridgename.if(5)
96for hn in /etc/hostname.*; do
97    # Strip off /etc/hostname. prefix
98    if=${hn#/etc/hostname.}
99
100    # Interface names must be alphanumeric only.  We check to avoid
101    # configuring backup or temp files, and to catch the "*" case.
102    if ! isalphanumeric "$if"; then
103	continue
104    fi
105    ifconfig $if > /dev/null 2>&1
106    if [ "$?" != "0" ]; then
107	continue
108    fi
109
110    # Now parse the hostname.* file
111    while :; do
112	if [ "$cmd2" ]; then
113	    # we are carrying over from the 'read dt dtaddr' last time
114	    set -- $cmd2
115	    af="$1" name="$2" mask="$3" bcaddr="$4" ext1="$5" cmd2=
116	    # make sure and get any remaining args in ext2, like the read below
117	    i=1; while [ i -lt 6 -a -n "$1" ]; do shift; let i=i+1; done
118	    ext2="$@"
119	else
120	    # read the next line or exit the while loop
121	    read af name mask bcaddr ext1 ext2 || break
122	fi
123	# $af can be "dhcp", "up", "rtsol", an address family, commands, or
124	# a comment.
125	case "$af" in
126	"#"*|"") # skip comments and empty lines
127	    continue
128	    ;;
129	"!"*) # parse commands
130	    cmd="${af#*!} ${name} ${mask} ${bcaddr} ${ext1} ${ext2}"
131	    ;;
132	"bridge")
133	    cmd="echo ${hn}: bridges now supported via bridgename.* files"
134	    ;;
135	"dhcp")
136	    [ "$name" = "NONE" ] && name=
137	    [ "$mask" = "NONE" ] && mask=
138	    [ "$bcaddr" = "NONE" ] && bcaddr=
139	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 down
140	    cmd="dhclient $if"
141	    ;;
142	"rtsol")
143	    ifconfig $if $name $mask $bcaddr $ext1 $ext2 up
144	    rtsolif="$rtsolif $if"
145	    cmd=
146	    ;;
147	"up")
148	    # The only one of these guaranteed to be set is $if
149	    # the remaining ones exist so that media controls work
150	    cmd="ifconfig $if $name $mask $bcaddr $ext1 $ext2 up"
151	    ;;
152	*)
153	    read dt dtaddr
154	    if [ "$name"  = "alias" ]; then
155		# perform a 'shift' of sorts
156		alias=$name
157		name=$mask
158		mask=$bcaddr
159		bcaddr=$ext1
160		ext1=$ext2
161		ext2=
162	    else
163		alias=
164	    fi
165	    cmd="ifconfig $if $af $alias $name "
166	    case "$dt" in
167	    dest)
168		cmd="$cmd $dtaddr"
169		;;
170	    [a-z!]*)
171		cmd2="$dt $dtaddr"
172		;;
173	    esac
174	    if [ ! -n "$name" ]; then
175		    echo "/etc/hostname.$if: invalid network configuration file"
176		return
177	    fi
178	    case $af in
179	    inet)
180		[ "$mask" ] && cmd="$cmd netmask $mask"
181		if [ "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
182		    cmd="$cmd broadcast $bcaddr"
183		fi
184		[ "$alias" ] && rtcmd="; route -n add -host $name 127.0.0.1"
185		;;
186	    inet6) [ "$mask" ] && cmd="$cmd prefixlen $mask"
187		cmd="$cmd $bcaddr"
188		;;
189	    *) cmd="$cmd $mask $bcaddr"
190	    esac
191	    cmd="$cmd $ext1 $ext2$rtcmd" rtcmd=
192	    ;;
193	esac
194	eval "$cmd"
195    done < /etc/hostname.$if
196done
197
198if [ "$ip6kernel" = "YES" -a "x$rtsolif" != "x" ]; then
199	fw=`sysctl -n net.inet6.ip6.forwarding`
200	ra=`sysctl -n net.inet6.ip6.accept_rtadv`
201	if [ "x$fw" = "x0" -a "x$ra" = "x1" ]; then
202		echo "IPv6 autoconf:$rtsolif"
203		rtsol $rtsolif
204	else
205		echo "WARNING: inconsistent config - check /etc/sysctl.conf for IPv6 autoconf"
206	fi
207fi
208if [ "$ip6kernel" = "YES" ]; then
209	# this is to make sure DAD is completed before going further.
210	sleep `sysctl -n net.inet6.ip6.dad_count`
211	sleep 1
212fi
213
214for bn in /etc/bridgename.*; do
215    # Strip off /etc/bridgename. prefix
216    if=${bn#/etc/bridgename.}
217
218    # Interface names must be alphanumeric only.  We check to avoid
219    # configuring backup or temp files, and to catch the "*" case.
220    if ! isalphanumeric "$if"; then
221        continue
222    fi
223    brconfig $if > /dev/null 2>&1
224    if [ "$?" != "0" ]; then
225	continue
226    fi
227
228    # Now parse the bridgename.* file
229    {
230	# All lines are run as brconfig(8) commands.
231	while read line ; do
232	    line=${line%%#*}		# strip comments
233	    test -z "$line" && continue
234	    case "$line" in
235	    "!"*)
236		cmd="${line#*!}"
237		;;
238	    *)
239		cmd="brconfig $if $line"
240		;;
241	    esac
242	    eval "$cmd"
243	done
244    } < /etc/bridgename.$if
245done
246
247# /etc/mygate, if it exists, contains the name of my gateway host
248# that name must be in /etc/hosts.
249if [ -f /etc/mygate ]; then
250	route -n add -host default `cat /etc/mygate`
251fi
252
253# Multicast routing.
254#
255# The routing to the 224.0.0.0/4 net is setup according to these rules:
256# multicast_host	multicast_router	route		comment
257# NO			NO			-reject		no multicast
258# NO			YES			none installed	daemon will run
259# YES/interface		NO			-interface	YES=def. iface
260#	   Any other combination		-reject		config error
261case "$multicast_host:$multicast_router" in
262NO:NO)
263	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject> /dev/null
264	;;
265NO:YES)
266	;;
267*:NO)
268	set `if [ $multicast_host = YES ]; then
269		ed -s '!route -n show' <<EOF
270/^default/p
271EOF
272	else
273		ed -s "!ifconfig $multicast_host" <<EOF
274/^	inet /p
275EOF
276	fi`
277	route -n add -net 224.0.0.0/4 -interface $2 > /dev/null
278	;;
279*:*)
280	echo 'config error, multicasting disabled until rc.conf is fixed'
281	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject > /dev/null
282	;;
283esac
284
285# Configure NAT after configuring network interfaces
286if [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
287	echo 'configuring NAT'
288	ipnat -CF -f ${ipnat_rules}
289else
290	ipnat=NO
291fi
292