netstart revision 1.52
1#!/bin/sh -
2#
3#	$OpenBSD: netstart,v 1.52 1999/12/09 13:59:57 itojun 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} -E
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
44route -n add -net 127 127.0.0.1 -reject
45
46# configure all of the non-loopback interfaces which we know about.
47# refer to hostname.if(5) and bridgename.if(5)
48for hn in /etc/hostname.*; do
49    # Strip off /etc/hostname. prefix
50    if=${hn#/etc/hostname.}
51
52    # Interface names must be alphanumeric only.  We check to avoid
53    # configuring backup or temp files, and to catch the "*" case.
54    if ! isalphanumeric "$if"; then
55        continue
56    fi
57    ifconfig $if > /dev/null 2>&1
58    if [ "$?" != "0" ]; then
59	continue
60    fi
61
62    # Now parse the hostname.* file
63    {
64        read af name mask bcaddr extras
65
66	# $af can be either "dhcp", "up" or an address family.
67	case "$af" in
68	"bridge")
69	    cmd="echo ${hn}: bridges now supported via bridgename.* files"
70	    ;;
71	"dhcp")
72	    ifconfig $if $name $mask $bcaddr $extras down
73	    cmd="dhclient $if"
74	    ;;
75	"up")
76	    # The only one of these guaranteed to be set is $if
77	    # the remaining ones exist so that media controls work
78	    cmd="ifconfig $if $name $mask $bcaddr $extras up"
79 	    ;;
80 	*)
81            read dt dtaddr
82	    if [ ! -n "$name" ]; then
83		echo "/etc/hostname.$if: invalid network configuration file"
84		continue
85	    fi
86
87	    cmd="ifconfig $if $af $name "
88	    if [ "${dt}" = "dest" ]; then cmd="$cmd $dtaddr"; fi
89	    if [ -n "$mask" ]; then cmd="$cmd netmask $mask"; fi
90	    if [ -n "$bcaddr" -a "X$bcaddr" != "XNONE" ]; then
91		cmd="$cmd broadcast $bcaddr";
92	    fi
93	    cmd="$cmd $extras";
94	    ;;
95	esac
96
97	eval "$cmd"
98    } < /etc/hostname.$if
99done
100for bn in /etc/bridgename.*; do
101    # Strip off /etc/bridgename. prefix
102    if=${bn#/etc/bridgename.}
103
104    # Interface names must be alphanumeric only.  We check to avoid
105    # configuring backup or temp files, and to catch the "*" case.
106    if ! isalphanumeric "$if"; then
107        continue
108    fi
109    brconfig $if > /dev/null 2>&1
110    if [ "$?" != "0" ]; then
111	continue
112    fi
113
114    # Now parse the bridgename.* file
115    {
116	# All lines are run as brconfig(8) commands.
117	while read line ; do
118	    line=${line%%#*}		# strip comments
119	    test -z "$line" && continue
120	    brconfig $if $line
121	done
122    } < /etc/bridgename.$if
123done
124
125# /etc/mygate, if it exists, contains the name of my gateway host
126# that name must be in /etc/hosts.
127if [ -f /etc/mygate ]; then
128	route -n add -host default `cat /etc/mygate`
129fi
130
131# Multicast routing.
132#
133# The routing to the 224.0.0.0/4 net is setup according to these rules:
134# multicast_host	multicast_router	route		comment
135# NO			NO			-reject		no multicast
136# NO			YES			none installed	daemon will run
137# YES/interface		NO			-interface	YES=def. iface
138#	   Any other combination		-reject		config error
139case "$multicast_host:$multicast_router" in
140NO:NO)
141	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
142NO:YES)
143	;;
144*:NO)
145	set `if [ $multicast_host = YES ]; then
146		ed -s '!route -n show' <<EOF
147/^default/p
148EOF
149	else
150		ed -s "!ifconfig $multicast_host" <<EOF
151/^	inet /p
152EOF
153	fi`
154	route -n add -net 224.0.0.0/4 -interface $2;;
155*:*)
156	echo 'config error, multicasting disabled until rc.conf is fixed'
157	route -n add -net 224.0.0.0/4 -interface 127.0.0.1 -reject;;
158esac
159	
160# Configure NAT after configuring network interfaces
161if [ "${ipnat}" = "YES" -a "${ipfilter}" = "YES" -a -f "${ipnat_rules}" ]; then
162	echo 'configuring NAT'
163	ipnat -CF -f ${ipnat_rules}
164else
165	ipnat=NO
166fi
167