xref: /titanic_50/usr/src/cmd/svc/milestone/net-svc (revision 36e852a172cba914383d7341c988128b2c667fbd)
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#
23*36e852a1SRaja Andra# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate#
287c478bd9Sstevel@tonic-gate# This is third phase of TCP/IP startup/configuration.  This script
29*36e852a1SRaja Andra# runs after the NIS startup script.  We run things here that may
30*36e852a1SRaja Andra# depend on NIS maps.
317c478bd9Sstevel@tonic-gate#
327c478bd9Sstevel@tonic-gate
336927f468Sdp. /lib/svc/share/smf_include.sh
346927f468Sdp
357c478bd9Sstevel@tonic-gatecase "$1" in
367c478bd9Sstevel@tonic-gate'start')
377c478bd9Sstevel@tonic-gate	#
38f4b3ec61Sdh155122	# In a shared-IP zone we need this service to be up, but all of the
39f4b3ec61Sdh155122	# work it tries to do is irrelevant (and will actually lead to the
40f4b3ec61Sdh155122	# service failing if we try to do it), so just bail out.
41f4b3ec61Sdh155122	# In the global zone and exclusive-IP zones we proceed.
427c478bd9Sstevel@tonic-gate	#
43f4b3ec61Sdh155122	smf_configure_ip || exit 0
447c478bd9Sstevel@tonic-gate	;; # Fall through -- rest of script is the initialization code
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate'stop')
477c478bd9Sstevel@tonic-gate	exit 0
487c478bd9Sstevel@tonic-gate	;;
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate*)
517c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
527c478bd9Sstevel@tonic-gate	exit 1
537c478bd9Sstevel@tonic-gate	;;
547c478bd9Sstevel@tonic-gateesac
557c478bd9Sstevel@tonic-gate
563cf1b3e0SokieNWAM_FMRI="svc:/network/physical:nwam"
573cf1b3e0SokieNETSVC_FMRI="svc:/network/service:default"
583cf1b3e0Sokie
59d71dbb73Sjbeckinterface=$2
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate# If boot variables are not set, set variables we use
627c478bd9Sstevel@tonic-gate[ -z "$_INIT_UTS_NODENAME" ] && _INIT_UTS_NODENAME=`/usr/bin/uname -n`
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate#
653cf1b3e0Sokie# This function removes the instance portion of the passed-in FMRI; for
663cf1b3e0Sokie# example, 'svc:/network/service:default' becomes 'svc:/network/service'.
673cf1b3e0Sokie#
683cf1b3e0Sokieremove_fmri_inst () {
693cf1b3e0Sokie	echo $1 | awk -F: ' { printf "%s:%s", $1, $2 } '
703cf1b3e0Sokie}
713cf1b3e0Sokie
723cf1b3e0Sokie#
733cf1b3e0Sokie# This function returns true if this script was *not* invoked
743cf1b3e0Sokie# by an instance of svc:/network/service.
753cf1b3e0Sokie#
763cf1b3e0Sokiefmri_is_not_netsvc () {
773cf1b3e0Sokie	FMRI_1=`remove_fmri_inst $SMF_FMRI`
783cf1b3e0Sokie	FMRI_2=`remove_fmri_inst $NETSVC_FMRI`
793cf1b3e0Sokie	[ "$FMRI_1" = "$FMRI_2" ] && return 1
803cf1b3e0Sokie	return 0
813cf1b3e0Sokie}
823cf1b3e0Sokie
833cf1b3e0Sokie#
843cf1b3e0Sokie# This function returns true if this script was *not* invoked
853cf1b3e0Sokie# by the nwam instance of the network/physical service.
863cf1b3e0Sokie#
873cf1b3e0Sokiefmri_is_not_nwam () {
883cf1b3e0Sokie	[ "&SMF_FMRI" = "$NWAM_FMRI" ] && return 1
893cf1b3e0Sokie	return 0
903cf1b3e0Sokie}
913cf1b3e0Sokie
923cf1b3e0Sokie#
933cf1b3e0Sokie# This function returns true if the nwam service is not running, false
943cf1b3e0Sokie# if it is.  "running" is defined as "current state is online or next
953cf1b3e0Sokie# state is online".
963cf1b3e0Sokie#
973cf1b3e0Sokienwam_is_not_running() {
983cf1b3e0Sokie	state=`/usr/bin/svcprop -p restarter/state $NWAM_FMRI`
993cf1b3e0Sokie	nstate=`/usr/bin/svcprop -p restarter/next_state $NWAM_FMRI`
1003cf1b3e0Sokie
1013cf1b3e0Sokie	[ "$state" = "online" -o "$nextstate" = "online" ] && return 1
1023cf1b3e0Sokie	return 0
1033cf1b3e0Sokie}
1043cf1b3e0Sokie
1053cf1b3e0Sokie#
1067c478bd9Sstevel@tonic-gate# wait_nis
1077c478bd9Sstevel@tonic-gate# Wait up to 5 seconds for ypbind to obtain a binding.
1087c478bd9Sstevel@tonic-gate#
1097c478bd9Sstevel@tonic-gatewait_nis ()
1107c478bd9Sstevel@tonic-gate{
1117c478bd9Sstevel@tonic-gate	for i in 1 2 3 4 5; do
1127c478bd9Sstevel@tonic-gate		server=`/usr/bin/ypwhich 2>/dev/null`
1137c478bd9Sstevel@tonic-gate		[ $? -eq 0 -a -n "$server" ] && return 0 || sleep 1
1147c478bd9Sstevel@tonic-gate	done
1157c478bd9Sstevel@tonic-gate	return 1
1167c478bd9Sstevel@tonic-gate}
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate#
1197c478bd9Sstevel@tonic-gate# This function takes two file names and the file mode as input. The two
1207c478bd9Sstevel@tonic-gate# files are compared for differences (using cmp(1)) and if different, the
1217c478bd9Sstevel@tonic-gate# second file is over written with the first. A chmod is done with the file
1227c478bd9Sstevel@tonic-gate# mode passed in. If the files are equal, the first file passed
1237c478bd9Sstevel@tonic-gate# in (the /tmp file) is deleted.
1247c478bd9Sstevel@tonic-gate#
1257c478bd9Sstevel@tonic-gatemv_file ()
1267c478bd9Sstevel@tonic-gate{
1277c478bd9Sstevel@tonic-gate	/usr/bin/cmp -s $1 $2
1287c478bd9Sstevel@tonic-gate	if [ $? -eq 1 ]; then
1297c478bd9Sstevel@tonic-gate		/usr/bin/mv $1 $2
1307c478bd9Sstevel@tonic-gate		#
1317c478bd9Sstevel@tonic-gate		# The umask during boot is configurable, which requires
1327c478bd9Sstevel@tonic-gate		# explicit setting of file permission modes when we
1337c478bd9Sstevel@tonic-gate		# create files.
1347c478bd9Sstevel@tonic-gate		#
1357c478bd9Sstevel@tonic-gate		/usr/bin/chmod $3 $2
1367c478bd9Sstevel@tonic-gate	else
1377c478bd9Sstevel@tonic-gate		/usr/bin/rm $1
1387c478bd9Sstevel@tonic-gate	fi
1397c478bd9Sstevel@tonic-gate}
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate#
1423cf1b3e0Sokie# This function takes a DHCP parameter (as defined in /etc/dhcp/inittab)
1433cf1b3e0Sokie# and returns the value for that parameter returned by the DHCP server.
1443cf1b3e0Sokie# If the global 'interface' is defined, it will request the value learned
1453cf1b3e0Sokie# on that interface, else it will request the value learned on the primary
1463cf1b3e0Sokie# interface.
1473cf1b3e0Sokie#
1483cf1b3e0Sokieget_dhcp_var ()
1493cf1b3e0Sokie{
1503cf1b3e0Sokie	if [ -n "$interface" ]; then
1513cf1b3e0Sokie		/sbin/dhcpinfo -i $interface $1
1523cf1b3e0Sokie	else
1533cf1b3e0Sokie		/sbin/dhcpinfo $1
1543cf1b3e0Sokie	fi
1553cf1b3e0Sokie}
1563cf1b3e0Sokie
1573cf1b3e0Sokie#
1583cf1b3e0Sokie# This function returns true if the string "# Added by DHCP$" occurs in
1593cf1b3e0Sokie# the passed-in file, false otherwise.
1603cf1b3e0Sokie#
1613cf1b3e0Sokiedhcp_edits ()
1623cf1b3e0Sokie{
1633cf1b3e0Sokie	/usr/bin/grep '# Added by DHCP$' $1 >/dev/null 2>&1
1643cf1b3e0Sokie	return $?
1653cf1b3e0Sokie}
1663cf1b3e0Sokie
1673cf1b3e0Sokie#
1683cf1b3e0Sokie# update_resolv()
1693cf1b3e0Sokie# Go through /etc/resolv.conf and replace any existing domain or
1703cf1b3e0Sokie# nameserver entries with new ones derived from DHCP.  Note that
1713cf1b3e0Sokie# it is important to preserve order of domain entries vs. search
1723cf1b3e0Sokie# entries; the search entries are reserved for administrator
1733cf1b3e0Sokie# customization and if placed after the domain entry will override
1743cf1b3e0Sokie# it.  See resolv.conf(4).
1753cf1b3e0Sokie#
1763cf1b3e0Sokie# The first arg should be the dns servers string, the second
1773cf1b3e0Sokie# should be the dns domain.
1783cf1b3e0Sokie#
1793cf1b3e0Sokieupdate_resolv ()
1803cf1b3e0Sokie{
1813cf1b3e0Sokie	dnsservers=$1
1823cf1b3e0Sokie	dnsdomain=$2
1833cf1b3e0Sokie
1843cf1b3e0Sokie	if [ ! -f /etc/resolv.conf ]; then
1853cf1b3e0Sokie		/usr/bin/touch /etc/resolv.conf
1863cf1b3e0Sokie	fi
1873cf1b3e0Sokie	export dnsservers dnsdomain
1883cf1b3e0Sokie	/usr/bin/nawk </etc/resolv.conf >/tmp/resolv.conf.$$ '
1893cf1b3e0Sokie		function writedomain() {
1903cf1b3e0Sokie			if (updated == 0) {
1913cf1b3e0Sokie			    	# Use only first domain, not a search list
1923cf1b3e0Sokie			    	split(ENVIRON["dnsdomain"], d)
1933cf1b3e0Sokie				if(length(d[1]) != 0)
1943cf1b3e0Sokie					printf("domain %s\n", d[1])
1953cf1b3e0Sokie			}
1963cf1b3e0Sokie			++updated
1973cf1b3e0Sokie		}
1983cf1b3e0Sokie		$1 == "domain" { writedomain(); next }
1993cf1b3e0Sokie		$1 != "nameserver" { print $0 }
2003cf1b3e0Sokie		END {
2013cf1b3e0Sokie			writedomain()
2023cf1b3e0Sokie			n = split(ENVIRON["dnsservers"], s)
2033cf1b3e0Sokie			for (i = 1; i <= n; ++i)
2043cf1b3e0Sokie				printf("nameserver %s\n", s[i])
2053cf1b3e0Sokie		}'
2063cf1b3e0Sokie	unset dnsservers dnsdomain
2073cf1b3e0Sokie	mv_file /tmp/resolv.conf.$$ /etc/resolv.conf 644
2083cf1b3e0Sokie}
2093cf1b3e0Sokie
2103cf1b3e0Sokie#
2117c478bd9Sstevel@tonic-gate# update_nss
2127c478bd9Sstevel@tonic-gate# This routine takes as a parameter, the name of the respective policy
2137c478bd9Sstevel@tonic-gate# to change in the nsswitch.conf (hosts or ipnodes) to update with dns.
2147c478bd9Sstevel@tonic-gate#
2157c478bd9Sstevel@tonic-gateupdate_nss ()
2167c478bd9Sstevel@tonic-gate{
2177c478bd9Sstevel@tonic-gate	policy=$1;
2187c478bd9Sstevel@tonic-gate	# Add dns to the nsswitch file, if it isn't already there.
2197c478bd9Sstevel@tonic-gate	/usr/bin/awk ' $1 ~ /^'${policy}':/ {
2207c478bd9Sstevel@tonic-gate		n = split($0, a);
2217c478bd9Sstevel@tonic-gate		newl = a[1];
2227c478bd9Sstevel@tonic-gate		if ($0 !~ /dns/) {
2237c478bd9Sstevel@tonic-gate			printf("#%s # Commented out by DHCP\n", $0);
2247c478bd9Sstevel@tonic-gate			updated = 0;
2257c478bd9Sstevel@tonic-gate			for (i = 2; i <= n; i++) {
2267c478bd9Sstevel@tonic-gate				if (updated == 0 && index(a[i], "[") == 1) {
2277c478bd9Sstevel@tonic-gate					newl = newl" dns";
2287c478bd9Sstevel@tonic-gate					updated++;
2297c478bd9Sstevel@tonic-gate				}
2307c478bd9Sstevel@tonic-gate				newl = newl" "a[i];
2317c478bd9Sstevel@tonic-gate			}
2327c478bd9Sstevel@tonic-gate			if (updated == 0) {
2337c478bd9Sstevel@tonic-gate				newl = newl" dns";
2347c478bd9Sstevel@tonic-gate				updated++;
2357c478bd9Sstevel@tonic-gate			}
2367c478bd9Sstevel@tonic-gate			if (updated != 0)
2377c478bd9Sstevel@tonic-gate				newl = newl" # Added by DHCP";
2387c478bd9Sstevel@tonic-gate			else
2397c478bd9Sstevel@tonic-gate				newl = $0;
2407c478bd9Sstevel@tonic-gate			printf("%s\n", newl);
2417c478bd9Sstevel@tonic-gate		} else
2427c478bd9Sstevel@tonic-gate			printf("%s\n", $0);
2437c478bd9Sstevel@tonic-gate	} $1 !~ /^'${policy}':/ { printf("%s\n", $0); }' /etc/nsswitch.conf \
2447c478bd9Sstevel@tonic-gate	    >/tmp/nsswitch.conf.$$
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
2477c478bd9Sstevel@tonic-gate}
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate#
2503cf1b3e0Sokie# Remove any lines with the "# Added by DHCP" tag from /etc/nsswitch.conf;
2513cf1b3e0Sokie# also uncomment hosts and ipnodes entries which were previously commented
2523cf1b3e0Sokie# out by this script.
2537c478bd9Sstevel@tonic-gate#
2543cf1b3e0Sokiecleanup_nss ()
2553cf1b3e0Sokie{
2567c478bd9Sstevel@tonic-gate	/usr/bin/sed \
2577c478bd9Sstevel@tonic-gate	    -e '/# Added by DHCP$/d' \
2587c478bd9Sstevel@tonic-gate	    -e 's/^\(#hosts:\)\(.*[^#]\)\(#.*\)$/hosts: \2/' \
2597c478bd9Sstevel@tonic-gate	    -e 's/^\(#ipnodes:\)\(.*[^#]\)\(#.*\)$/ipnodes: \2/' \
2607c478bd9Sstevel@tonic-gate	    /etc/nsswitch.conf >/tmp/nsswitch.conf.$$
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate	mv_file /tmp/nsswitch.conf.$$ /etc/nsswitch.conf 644
2633cf1b3e0Sokie}
2647c478bd9Sstevel@tonic-gate
2653cf1b3e0Sokie#
2663cf1b3e0Sokie# Remove any lines with the "# Added by DHCP" tag from /etc/inet/hosts.
2673cf1b3e0Sokie#
2683cf1b3e0Sokiecleanup_hosts ()
2693cf1b3e0Sokie{
270d71dbb73Sjbeck	/usr/bin/nawk '{
271d71dbb73Sjbeck		if (index($0, "# Added by DHCP") == 0 ||
272d71dbb73Sjbeck		    $1 == "127.0.0.1" || $1 == "::1") {
273d71dbb73Sjbeck			print $0
274d71dbb73Sjbeck		}
275d71dbb73Sjbeck	}' /etc/inet/hosts > /tmp/hosts.$$
2767c478bd9Sstevel@tonic-gate	mv_file /tmp/hosts.$$ /etc/inet/hosts 444
2773cf1b3e0Sokie}
2783cf1b3e0Sokie
2793cf1b3e0Sokie#
2803cf1b3e0Sokie# We now need to reset the netmask and broadcast address for our network
2813cf1b3e0Sokie# interfaces.  Since this may result in a name service lookup, we want to
2823cf1b3e0Sokie# now wait for NIS to come up if we previously started it.
2833cf1b3e0Sokie#
2843cf1b3e0Sokie# Only do this in the non-nwam case.
2853cf1b3e0Sokie#
2863cf1b3e0Sokieif fmri_is_not_nwam; then
2873cf1b3e0Sokie	domain=`/usr/bin/domainname 2>/dev/null`
2883cf1b3e0Sokie
2893cf1b3e0Sokie	[ -z "$domain" ] || [ ! -d /var/yp/binding/$domain ] || wait_nis || \
2903cf1b3e0Sokie    		echo "WARNING: Timed out waiting for NIS to come up" >& 2
2913cf1b3e0Sokie
2923cf1b3e0Sokie	#
2933cf1b3e0Sokie	# Re-set the netmask and broadcast addr for all IP interfaces.  This
2943cf1b3e0Sokie	# ifconfig is run here, after waiting for name services, so that
2953cf1b3e0Sokie	# "netmask +" will find the netmask if it lives in a NIS map. The 'D'
2963cf1b3e0Sokie	# in -auD tells ifconfig NOT to mess with the interface if it is
2973cf1b3e0Sokie	# under DHCP control
2983cf1b3e0Sokie	#
2993cf1b3e0Sokie	/usr/sbin/ifconfig -auD4 netmask + broadcast +
3007c478bd9Sstevel@tonic-gatefi
301d71dbb73Sjbeck
3023cf1b3e0Sokie# Uncomment these lines to print complete network interface configuration
3033cf1b3e0Sokie# echo "network interface configuration:"
3043cf1b3e0Sokie# /usr/sbin/ifconfig -a
3053cf1b3e0Sokie
3063cf1b3e0Sokie#
3073cf1b3e0Sokie# If our network configuration strategy is DHCP, check for DNS
3083cf1b3e0Sokie# configuration parameters obtained from the DHCP server.
3093cf1b3e0Sokie#
3103cf1b3e0Sokie# If NWAM is enabled, it will invoke this script to do this configuration
3113cf1b3e0Sokie# whenever a DHCP lease is obtained; in that case, this configuration
3123cf1b3e0Sokie# should *not* happen when svc:network/service is starting, as it will
3133cf1b3e0Sokie# interfere with the configuration performed by NWAM.
3143cf1b3e0Sokie#
3153cf1b3e0Sokieif nwam_is_not_running || fmri_is_not_netsvc; then
3163cf1b3e0Sokie
3173cf1b3e0Sokie	smf_netstrategy
3183cf1b3e0Sokie
3193cf1b3e0Sokie	if [ "$_INIT_NET_STRATEGY" = "dhcp" ]; then
3203cf1b3e0Sokie		dnsservers=`get_dhcp_var DNSserv`
3213cf1b3e0Sokie		dnsdomain=`get_dhcp_var DNSdmain`
3223cf1b3e0Sokie	else
3233cf1b3e0Sokie		dnsservers=""
3243cf1b3e0Sokie		dnsdomain=""
3253cf1b3e0Sokie	fi
3263cf1b3e0Sokie
3273cf1b3e0Sokie	if [ -n "$dnsservers" ]; then
3283cf1b3e0Sokie		#
3293cf1b3e0Sokie		# add settings retrieved from dhcp server to /etc/resolv.conf
3303cf1b3e0Sokie		#
3313cf1b3e0Sokie		update_resolv "$dnsservers" "$dnsdomain"
3323cf1b3e0Sokie
3333cf1b3e0Sokie		#
3343cf1b3e0Sokie		# Add dns to the nsswitch file, if it isn't already there.
3353cf1b3e0Sokie		#
3363cf1b3e0Sokie		update_nss hosts
3373cf1b3e0Sokie		update_nss ipnodes
3383cf1b3e0Sokie
3393cf1b3e0Sokie	elif dhcp_edits /etc/nsswitch.conf; then
3403cf1b3e0Sokie		# If we added DNS to the hosts and ipnodes
3413cf1b3e0Sokie		# policy in the nsswitch, remove it.
3423cf1b3e0Sokie		cleanup_nss
3433cf1b3e0Sokie	fi
3443cf1b3e0Sokiefi
3453cf1b3e0Sokie
3463cf1b3e0Sokieif dhcp_edits /etc/inet/hosts; then
3473cf1b3e0Sokie	# Clean up any old DHCP-added entries
3483cf1b3e0Sokie	# (except loopback) in the hosts file.
3493cf1b3e0Sokie	cleanup_hosts
3503cf1b3e0Sokiefi
3513cf1b3e0Sokie
3523cf1b3e0Sokie#
3533cf1b3e0Sokie# If we were invoked by NWAM, can exit now (skipping the ipqos config)
3543cf1b3e0Sokie#
3553cf1b3e0Sokieif [ -z "$SMF_FMRI" ] || [ "$SMF_FMRI" = "$NWAM_FMRI" ]; then
356032ae3d9Samaguire        exit 0
357032ae3d9Samaguirefi
358032ae3d9Samaguire
359032ae3d9Samaguire#
360032ae3d9Samaguire# Load the IPQoS configuration.
361032ae3d9Samaguire# This is backgrounded so that any remote hostname lookups it performs
362032ae3d9Samaguire# don't unduely delay startup. Any messages go via syslog.
363032ae3d9Samaguire#
364032ae3d9Samaguire
365032ae3d9Samaguireif [ -f /usr/sbin/ipqosconf -a -f /etc/inet/ipqosinit.conf ]; then
366032ae3d9Samaguire        /usr/sbin/ipqosconf -s -a /etc/inet/ipqosinit.conf &
367032ae3d9Samaguirefi
368