xref: /titanic_41/usr/src/cmd/svc/milestone/net-svc (revision d71dbb732372504daff1f1783bc0d8864ce9bd50)
17c478bd9Sstevel@tonic-gate#!/sbin/sh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
66927f468Sdp# Common Development and Distribution License (the "License").
76927f468Sdp# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
23f4b3ec61Sdh155122# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate#
297c478bd9Sstevel@tonic-gate# This is third phase of TCP/IP startup/configuration.  This script
307c478bd9Sstevel@tonic-gate# runs after the NIS/NIS+ startup script.  We run things here that may
317c478bd9Sstevel@tonic-gate# depend on NIS/NIS+ maps.
327c478bd9Sstevel@tonic-gate#
337c478bd9Sstevel@tonic-gate
346927f468Sdp. /lib/svc/share/smf_include.sh
356927f468Sdp
367c478bd9Sstevel@tonic-gatecase "$1" in
377c478bd9Sstevel@tonic-gate'start')
387c478bd9Sstevel@tonic-gate	#
39f4b3ec61Sdh155122	# In a shared-IP zone we need this service to be up, but all of the
40f4b3ec61Sdh155122	# work it tries to do is irrelevant (and will actually lead to the
41f4b3ec61Sdh155122	# service failing if we try to do it), so just bail out.
42f4b3ec61Sdh155122	# In the global zone and exclusive-IP zones we proceed.
437c478bd9Sstevel@tonic-gate	#
44f4b3ec61Sdh155122	smf_configure_ip || exit 0
457c478bd9Sstevel@tonic-gate	;; # Fall through -- rest of script is the initialization code
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate'stop')
487c478bd9Sstevel@tonic-gate	exit 0
497c478bd9Sstevel@tonic-gate	;;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate*)
527c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
537c478bd9Sstevel@tonic-gate	exit 1
547c478bd9Sstevel@tonic-gate	;;
557c478bd9Sstevel@tonic-gateesac
567c478bd9Sstevel@tonic-gate
57*d71dbb73Sjbeckinterface=$2
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate# If boot variables are not set, set variables we use
607c478bd9Sstevel@tonic-gate[ -z "$_INIT_UTS_NODENAME" ] && _INIT_UTS_NODENAME=`/usr/bin/uname -n`
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate#
637c478bd9Sstevel@tonic-gate# wait_nis
647c478bd9Sstevel@tonic-gate# Wait up to 5 seconds for ypbind to obtain a binding.
657c478bd9Sstevel@tonic-gate#
667c478bd9Sstevel@tonic-gatewait_nis ()
677c478bd9Sstevel@tonic-gate{
687c478bd9Sstevel@tonic-gate	for i in 1 2 3 4 5; do
697c478bd9Sstevel@tonic-gate		server=`/usr/bin/ypwhich 2>/dev/null`
707c478bd9Sstevel@tonic-gate		[ $? -eq 0 -a -n "$server" ] && return 0 || sleep 1
717c478bd9Sstevel@tonic-gate	done
727c478bd9Sstevel@tonic-gate	return 1
737c478bd9Sstevel@tonic-gate}
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate#
767c478bd9Sstevel@tonic-gate# This function takes two file names and the file mode as input. The two
777c478bd9Sstevel@tonic-gate# files are compared for differences (using cmp(1)) and if different, the
787c478bd9Sstevel@tonic-gate# second file is over written with the first. A chmod is done with the file
797c478bd9Sstevel@tonic-gate# mode passed in. If the files are equal, the first file passed
807c478bd9Sstevel@tonic-gate# in (the /tmp file) is deleted.
817c478bd9Sstevel@tonic-gate#
827c478bd9Sstevel@tonic-gatemv_file ()
837c478bd9Sstevel@tonic-gate{
847c478bd9Sstevel@tonic-gate	/usr/bin/cmp -s $1 $2
857c478bd9Sstevel@tonic-gate	if [ $? -eq 1 ]; then
867c478bd9Sstevel@tonic-gate		/usr/bin/mv $1 $2
877c478bd9Sstevel@tonic-gate		#
887c478bd9Sstevel@tonic-gate		# The umask during boot is configurable, which requires
897c478bd9Sstevel@tonic-gate		# explicit setting of file permission modes when we
907c478bd9Sstevel@tonic-gate		# create files.
917c478bd9Sstevel@tonic-gate		#
927c478bd9Sstevel@tonic-gate		/usr/bin/chmod $3 $2
937c478bd9Sstevel@tonic-gate	else
947c478bd9Sstevel@tonic-gate		/usr/bin/rm $1
957c478bd9Sstevel@tonic-gate	fi
967c478bd9Sstevel@tonic-gate}
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate#
997c478bd9Sstevel@tonic-gate# update_nss
1007c478bd9Sstevel@tonic-gate# This routine takes as a parameter, the name of the respective policy
1017c478bd9Sstevel@tonic-gate# to change in the nsswitch.conf (hosts or ipnodes) to update with dns.
1027c478bd9Sstevel@tonic-gate#
1037c478bd9Sstevel@tonic-gateupdate_nss ()
1047c478bd9Sstevel@tonic-gate{
1057c478bd9Sstevel@tonic-gate	policy=$1;
1067c478bd9Sstevel@tonic-gate	# Add dns to the nsswitch file, if it isn't already there.
1077c478bd9Sstevel@tonic-gate	/usr/bin/awk ' $1 ~ /^'${policy}':/ {
1087c478bd9Sstevel@tonic-gate		n = split($0, a);
1097c478bd9Sstevel@tonic-gate		newl = a[1];
1107c478bd9Sstevel@tonic-gate		if ($0 !~ /dns/) {
1117c478bd9Sstevel@tonic-gate			printf("#%s # Commented out by DHCP\n", $0);
1127c478bd9Sstevel@tonic-gate			updated = 0;
1137c478bd9Sstevel@tonic-gate			for (i = 2; i <= n; i++) {
1147c478bd9Sstevel@tonic-gate				if (updated == 0 && index(a[i], "[") == 1) {
1157c478bd9Sstevel@tonic-gate					newl = newl" dns";
1167c478bd9Sstevel@tonic-gate					updated++;
1177c478bd9Sstevel@tonic-gate				}
1187c478bd9Sstevel@tonic-gate				newl = newl" "a[i];
1197c478bd9Sstevel@tonic-gate			}
1207c478bd9Sstevel@tonic-gate			if (updated == 0) {
1217c478bd9Sstevel@tonic-gate				newl = newl" dns";
1227c478bd9Sstevel@tonic-gate				updated++;
1237c478bd9Sstevel@tonic-gate			}
1247c478bd9Sstevel@tonic-gate			if (updated != 0)
1257c478bd9Sstevel@tonic-gate				newl = newl" # Added by DHCP";
1267c478bd9Sstevel@tonic-gate			else
1277c478bd9Sstevel@tonic-gate				newl = $0;
1287c478bd9Sstevel@tonic-gate			printf("%s\n", newl);
1297c478bd9Sstevel@tonic-gate		} else
1307c478bd9Sstevel@tonic-gate			printf("%s\n", $0);
1317c478bd9Sstevel@tonic-gate	} $1 !~ /^'${policy}':/ { printf("%s\n", $0); }' /etc/nsswitch.conf \
1327c478bd9Sstevel@tonic-gate	    >/tmp/nsswitch.conf.$$
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
1357c478bd9Sstevel@tonic-gate}
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate#
1387c478bd9Sstevel@tonic-gate# We now need to reset the netmask and broadcast address for our network
1397c478bd9Sstevel@tonic-gate# interfaces.  Since this may result in a name service lookup, we want to
1407c478bd9Sstevel@tonic-gate# now wait for NIS to come up if we previously started it.
1417c478bd9Sstevel@tonic-gate#
1427c478bd9Sstevel@tonic-gatedomain=`/usr/bin/domainname 2>/dev/null`
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate[ -z "$domain" ] || [ ! -d /var/yp/binding/$domain ] || wait_nis || \
1457c478bd9Sstevel@tonic-gate    echo "WARNING: Timed out waiting for NIS to come up" >& 2
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate#
1487c478bd9Sstevel@tonic-gate# Re-set the netmask and broadcast addr for all IP interfaces.  This ifconfig
1497c478bd9Sstevel@tonic-gate# is run here, after waiting for name services, so that "netmask +" will find
1507c478bd9Sstevel@tonic-gate# the netmask if it lives in a NIS map. The 'D' in -auD tells ifconfig NOT to
1517c478bd9Sstevel@tonic-gate# mess with the interface if it is under DHCP control
1527c478bd9Sstevel@tonic-gate#
1537c478bd9Sstevel@tonic-gate/usr/sbin/ifconfig -auD4 netmask + broadcast +
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate# Uncomment these lines to print complete network interface configuration
1567c478bd9Sstevel@tonic-gate# echo "network interface configuration:"
1577c478bd9Sstevel@tonic-gate# /usr/sbin/ifconfig -a
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gatesmf_netstrategy
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gateif [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
162*d71dbb73Sjbeck	if [ -n "$interface" ]; then
163*d71dbb73Sjbeck		dnsservers=`/sbin/dhcpinfo -i $interface DNSserv`
164*d71dbb73Sjbeck	else
1657c478bd9Sstevel@tonic-gate		dnsservers=`/sbin/dhcpinfo DNSserv`
166*d71dbb73Sjbeck	fi
1677c478bd9Sstevel@tonic-gateelse
1687c478bd9Sstevel@tonic-gate	dnsservers=""
1697c478bd9Sstevel@tonic-gatefi
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gateif [ -n "$dnsservers" ]; then
1727c478bd9Sstevel@tonic-gate	#
1737c478bd9Sstevel@tonic-gate	# Go through /etc/resolv.conf and replace any existing
1747c478bd9Sstevel@tonic-gate	# domain or nameserver entries with new ones derived
1757c478bd9Sstevel@tonic-gate	# from DHCP.  Note that it is important to preserve
1767c478bd9Sstevel@tonic-gate	# order of domain entries vs. search entries; the search
1777c478bd9Sstevel@tonic-gate	# entries are reserved for administrator customization
1787c478bd9Sstevel@tonic-gate	# and if placed after the domain entry will override it.
1797c478bd9Sstevel@tonic-gate	# See resolv.conf(4).
1807c478bd9Sstevel@tonic-gate	#
1817c478bd9Sstevel@tonic-gate	if [ ! -f /etc/resolv.conf ]; then
1827c478bd9Sstevel@tonic-gate		/usr/bin/touch /etc/resolv.conf
1837c478bd9Sstevel@tonic-gate	fi
184*d71dbb73Sjbeck	if [ -n "$interface" ]; then
185*d71dbb73Sjbeck		dnsdomain=`/sbin/dhcpinfo -i $interface DNSdmain`
186*d71dbb73Sjbeck	else
1877c478bd9Sstevel@tonic-gate		dnsdomain=`/sbin/dhcpinfo DNSdmain`
188*d71dbb73Sjbeck	fi
1897c478bd9Sstevel@tonic-gate	export dnsservers dnsdomain
1907c478bd9Sstevel@tonic-gate	/usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
1917c478bd9Sstevel@tonic-gate		function writedomain() {
1927c478bd9Sstevel@tonic-gate			if (updated == 0) {
1937c478bd9Sstevel@tonic-gate			    	# Use only first domain, not a search list
1947c478bd9Sstevel@tonic-gate			    	split(ENVIRON["dnsdomain"], d)
1957c478bd9Sstevel@tonic-gate				if(length(d[1]) != 0)
1967c478bd9Sstevel@tonic-gate					printf("domain %s\n", d[1])
1977c478bd9Sstevel@tonic-gate			}
1987c478bd9Sstevel@tonic-gate			++updated
1997c478bd9Sstevel@tonic-gate		}
2007c478bd9Sstevel@tonic-gate		$1 == "domain" { writedomain(); next }
2017c478bd9Sstevel@tonic-gate		$1 != "nameserver" { print $0 }
2027c478bd9Sstevel@tonic-gate		END {
2037c478bd9Sstevel@tonic-gate			writedomain()
2047c478bd9Sstevel@tonic-gate			n = split(ENVIRON["dnsservers"], s)
2057c478bd9Sstevel@tonic-gate			for (i = 1; i <= n; ++i)
2067c478bd9Sstevel@tonic-gate				printf("nameserver %s\n", s[i])
2077c478bd9Sstevel@tonic-gate		}'
2087c478bd9Sstevel@tonic-gate	unset dnsservers dnsdomain
2097c478bd9Sstevel@tonic-gate	mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
2107c478bd9Sstevel@tonic-gate	#
2117c478bd9Sstevel@tonic-gate	# Add dns to the nsswitch file, if it isn't already there.
2127c478bd9Sstevel@tonic-gate	#
2137c478bd9Sstevel@tonic-gate	update_nss hosts
2147c478bd9Sstevel@tonic-gate	update_nss ipnodes
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gateelif /usr/bin/grep '# Added by DHCP$' /etc/nsswitch.conf >/dev/null 2>&1; then
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate	# If we added DNS to the hosts and ipnodes policy in the nsswitch,
2197c478bd9Sstevel@tonic-gate	# remove it.
2207c478bd9Sstevel@tonic-gate	/usr/bin/sed \
2217c478bd9Sstevel@tonic-gate	    -e '/# Added by DHCP$/d' \
2227c478bd9Sstevel@tonic-gate	    -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
2237c478bd9Sstevel@tonic-gate	    -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
2247c478bd9Sstevel@tonic-gate	    /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
2277c478bd9Sstevel@tonic-gatefi
2287c478bd9Sstevel@tonic-gate
229*d71dbb73Sjbeck# Clean up any old DHCP-added entries (except loopback) in the hosts file.
230*d71dbb73Sjbeckif /usr/bin/grep '# Added by DHCP$' /etc/inet/hosts >/dev/null 2>&1; then
231*d71dbb73Sjbeck	/usr/bin/nawk '{
232*d71dbb73Sjbeck		if (index($0, "# Added by DHCP") == 0 ||
233*d71dbb73Sjbeck		    $1 == "127.0.0.1" || $1 == "::1") {
234*d71dbb73Sjbeck			print $0
235*d71dbb73Sjbeck		}
236*d71dbb73Sjbeck	}' /etc/inet/hosts > /tmp/hosts.$$
2377c478bd9Sstevel@tonic-gate	mv_file /tmp/hosts.$$ /etc/inet/hosts 444
2387c478bd9Sstevel@tonic-gatefi
239*d71dbb73Sjbeck
240*d71dbb73Sjbeckif [ -z "$SMF_FMRI" ] || [ "$SMF_FMRI" = "svc:/network/physical:nwam" ]; then
241*d71dbb73Sjbeck	exit 0
2427c478bd9Sstevel@tonic-gatefi
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate#
2457c478bd9Sstevel@tonic-gate# Load the IPQoS configuration.
2467c478bd9Sstevel@tonic-gate# This is backgrounded so that any remote hostname lookups it performs
2477c478bd9Sstevel@tonic-gate# don't unduely delay startup. Any messages go via syslog.
2487c478bd9Sstevel@tonic-gate#
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gateif [ -f /usr/sbin/ipqosconf -a -f /etc/inet/ipqosinit.conf ]; then
2517c478bd9Sstevel@tonic-gate	/usr/sbin/ipqosconf -s -a /etc/inet/ipqosinit.conf &
2527c478bd9Sstevel@tonic-gatefi
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate#
2557c478bd9Sstevel@tonic-gate# Add a static route for multicast packets out our default interface.
2567c478bd9Sstevel@tonic-gate# The default interface is the interface that corresponds to the node name.
2577c478bd9Sstevel@tonic-gate# Run in background subshell to avoid waiting for name service.
2587c478bd9Sstevel@tonic-gate#
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate(
2617c478bd9Sstevel@tonic-gateif [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
2627c478bd9Sstevel@tonic-gate	mcastif=`/sbin/dhcpinfo Yiaddr` || mcastif=$_INIT_UTS_NODENAME
2637c478bd9Sstevel@tonic-gateelse
2647c478bd9Sstevel@tonic-gate	mcastif=$_INIT_UTS_NODENAME
2657c478bd9Sstevel@tonic-gatefi
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gateecho "Setting default IPv4 interface for multicast:" \
2687c478bd9Sstevel@tonic-gate    "add net 224.0/4: gateway $mcastif"
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate/usr/sbin/route -n add -interface 224.0/4 -gateway "$mcastif" >/dev/null
2717c478bd9Sstevel@tonic-gate) &
272