xref: /titanic_52/usr/src/cmd/svc/milestone/net-svc (revision 27c48ed935c6a5f6015c6534b98e3090b1ddfdb6)
1#!/sbin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22#
23# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26#ident	"%Z%%M%	%I%	%E% SMI"
27
28#
29# This is third phase of TCP/IP startup/configuration.  This script
30# runs after the NIS/NIS+ startup script.  We run things here that may
31# depend on NIS/NIS+ maps.
32#
33
34. /lib/svc/share/smf_include.sh
35. /lib/svc/share/net_include.sh
36
37case "$1" in
38'start')
39	#
40	# In a shared-IP zone we need this service to be up, but all of the
41	# work it tries to do is irrelevant (and will actually lead to the
42	# service failing if we try to do it), so just bail out.
43	# In the global zone and exclusive-IP zones we proceed.
44	#
45	smf_configure_ip || exit 0
46	;; # Fall through -- rest of script is the initialization code
47
48'stop')
49	exit 0
50	;;
51
52*)
53	echo "Usage: $0 { start | stop }"
54	exit 1
55	;;
56esac
57
58interface=$2
59
60# If boot variables are not set, set variables we use
61[ -z "$_INIT_UTS_NODENAME" ] && _INIT_UTS_NODENAME=`/usr/bin/uname -n`
62
63#
64# wait_nis
65# Wait up to 5 seconds for ypbind to obtain a binding.
66#
67wait_nis ()
68{
69	for i in 1 2 3 4 5; do
70		server=`/usr/bin/ypwhich 2>/dev/null`
71		[ $? -eq 0 -a -n "$server" ] && return 0 || sleep 1
72	done
73	return 1
74}
75
76#
77# This function takes two file names and the file mode as input. The two
78# files are compared for differences (using cmp(1)) and if different, the
79# second file is over written with the first. A chmod is done with the file
80# mode passed in. If the files are equal, the first file passed
81# in (the /tmp file) is deleted.
82#
83mv_file ()
84{
85	/usr/bin/cmp -s $1 $2
86	if [ $? -eq 1 ]; then
87		/usr/bin/mv $1 $2
88		#
89		# The umask during boot is configurable, which requires
90		# explicit setting of file permission modes when we
91		# create files.
92		#
93		/usr/bin/chmod $3 $2
94	else
95		/usr/bin/rm $1
96	fi
97}
98
99#
100# update_nss
101# This routine takes as a parameter, the name of the respective policy
102# to change in the nsswitch.conf (hosts or ipnodes) to update with dns.
103#
104update_nss ()
105{
106	policy=$1;
107	# Add dns to the nsswitch file, if it isn't already there.
108	/usr/bin/awk ' $1 ~ /^'${policy}':/ {
109		n = split($0, a);
110		newl = a[1];
111		if ($0 !~ /dns/) {
112			printf("#%s # Commented out by DHCP\n", $0);
113			updated = 0;
114			for (i = 2; i <= n; i++) {
115				if (updated == 0 && index(a[i], "[") == 1) {
116					newl = newl" dns";
117					updated++;
118				}
119				newl = newl" "a[i];
120			}
121			if (updated == 0) {
122				newl = newl" dns";
123				updated++;
124			}
125			if (updated != 0)
126				newl = newl" # Added by DHCP";
127			else
128				newl = $0;
129			printf("%s\n", newl);
130		} else
131			printf("%s\n", $0);
132	} $1 !~ /^'${policy}':/ { printf("%s\n", $0); }' /etc/nsswitch.conf \
133	    >/tmp/nsswitch.conf.$$
134
135	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
136}
137
138#
139# We now need to reset the netmask and broadcast address for our network
140# interfaces.  Since this may result in a name service lookup, we want to
141# now wait for NIS to come up if we previously started it.
142#
143domain=`/usr/bin/domainname 2>/dev/null`
144
145[ -z "$domain" ] || [ ! -d /var/yp/binding/$domain ] || wait_nis || \
146    echo "WARNING: Timed out waiting for NIS to come up" >& 2
147
148#
149# Re-set the netmask and broadcast addr for all IP interfaces.  This ifconfig
150# is run here, after waiting for name services, so that "netmask +" will find
151# the netmask if it lives in a NIS map. The 'D' in -auD tells ifconfig NOT to
152# mess with the interface if it is under DHCP control
153#
154/usr/sbin/ifconfig -auD4 netmask + broadcast +
155
156# Uncomment these lines to print complete network interface configuration
157# echo "network interface configuration:"
158# /usr/sbin/ifconfig -a
159
160smf_netstrategy
161
162if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
163	if [ -n "$interface" ]; then
164		dnsservers=`/sbin/dhcpinfo -i $interface DNSserv`
165	else
166		dnsservers=`/sbin/dhcpinfo DNSserv`
167	fi
168else
169	dnsservers=""
170fi
171
172if [ -n "$dnsservers" ]; then
173	#
174	# Go through /etc/resolv.conf and replace any existing
175	# domain or nameserver entries with new ones derived
176	# from DHCP.  Note that it is important to preserve
177	# order of domain entries vs. search entries; the search
178	# entries are reserved for administrator customization
179	# and if placed after the domain entry will override it.
180	# See resolv.conf(4).
181	#
182	if [ ! -f /etc/resolv.conf ]; then
183		/usr/bin/touch /etc/resolv.conf
184	fi
185	if [ -n "$interface" ]; then
186		dnsdomain=`/sbin/dhcpinfo -i $interface DNSdmain`
187	else
188		dnsdomain=`/sbin/dhcpinfo DNSdmain`
189	fi
190	export dnsservers dnsdomain
191	/usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
192		function writedomain() {
193			if (updated == 0) {
194			    	# Use only first domain, not a search list
195			    	split(ENVIRON["dnsdomain"], d)
196				if(length(d[1]) != 0)
197					printf("domain %s\n", d[1])
198			}
199			++updated
200		}
201		$1 == "domain" { writedomain(); next }
202		$1 != "nameserver" { print $0 }
203		END {
204			writedomain()
205			n = split(ENVIRON["dnsservers"], s)
206			for (i = 1; i <= n; ++i)
207				printf("nameserver %s\n", s[i])
208		}'
209	unset dnsservers dnsdomain
210	mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
211	#
212	# Add dns to the nsswitch file, if it isn't already there.
213	#
214	update_nss hosts
215	update_nss ipnodes
216
217elif /usr/bin/grep '# Added by DHCP$' /etc/nsswitch.conf >/dev/null 2>&1; then
218
219	# If we added DNS to the hosts and ipnodes policy in the nsswitch,
220	# remove it.
221	/usr/bin/sed \
222	    -e '/# Added by DHCP$/d' \
223	    -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
224	    -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
225	    /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
226
227	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
228fi
229
230# Clean up any old DHCP-added entries (except loopback) in the hosts file.
231if /usr/bin/grep '# Added by DHCP$' /etc/inet/hosts >/dev/null 2>&1; then
232	/usr/bin/nawk '{
233		if (index($0, "# Added by DHCP") == 0 ||
234		    $1 == "127.0.0.1" || $1 == "::1") {
235			print $0
236		}
237	}' /etc/inet/hosts > /tmp/hosts.$$
238	mv_file /tmp/hosts.$$ /etc/inet/hosts 444
239fi
240
241#
242# Add a static route for multicast packets out our default interface.
243# The default interface is the interface that corresponds to the node name.
244# Run in background subshell to avoid waiting for name service.
245#
246
247(
248if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
249	mcastif=`/sbin/dhcpinfo Yiaddr` || mcastif=$_INIT_UTS_NODENAME
250else
251	mcastif=$_INIT_UTS_NODENAME
252fi
253
254echo "Setting default IPv4 interface for multicast:" \
255    "add net 224.0/4: gateway $mcastif"
256
257/usr/sbin/route -n add -interface 224.0/4 -gateway "$mcastif" >/dev/null
258) &
259
260#
261# NWAM-triggered interface changes may necessitate updating IPv6 multicast
262# route.
263#
264update_v6_multicast_route
265
266if [ -z "$SMF_FMRI" ] || [ "$SMF_FMRI" = "svc:/network/physical:nwam" ]; then
267        exit 0
268fi
269
270#
271# Load the IPQoS configuration.
272# This is backgrounded so that any remote hostname lookups it performs
273# don't unduely delay startup. Any messages go via syslog.
274#
275
276if [ -f /usr/sbin/ipqosconf -a -f /etc/inet/ipqosinit.conf ]; then
277        /usr/sbin/ipqosconf -s -a /etc/inet/ipqosinit.conf &
278fi
279