xref: /titanic_51/usr/src/cmd/svc/shell/net_include.sh (revision 9b5bf10ab04b9be5564d70a57980cfb68b6372e7)
17c478bd9Sstevel@tonic-gate#!/bin/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
6032ae3d9Samaguire# Common Development and Distribution License (the "License").
7032ae3d9Samaguire# 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#
23550b6e40SSowmini Varadhan# Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate#
257c478bd9Sstevel@tonic-gate# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
267c478bd9Sstevel@tonic-gate# All rights reserved.
277c478bd9Sstevel@tonic-gate#
287c478bd9Sstevel@tonic-gate
29*9b5bf10aSMark HaywoodNET_INADDR_ANY="0.0.0.0"
30*9b5bf10aSMark HaywoodNET_IN6ADDR_ANY_INIT="::0"
31*9b5bf10aSMark Haywood
32e11c3f44Smeem# Print warnings to console
33e11c3f44Smeemwarn_failed_ifs() {
34e11c3f44Smeem	echo "Failed to $1 interface(s):$2" >/dev/msglog
35e11c3f44Smeem}
36e11c3f44Smeem
377c478bd9Sstevel@tonic-gate#
387c478bd9Sstevel@tonic-gate# shcat file
397c478bd9Sstevel@tonic-gate#   Simulates cat in sh so it doesn't need to be on the root filesystem.
407c478bd9Sstevel@tonic-gate#
417c478bd9Sstevel@tonic-gateshcat() {
427c478bd9Sstevel@tonic-gate        while [ $# -ge 1 ]; do
437c478bd9Sstevel@tonic-gate                while read i; do
447c478bd9Sstevel@tonic-gate                        echo "$i"
457c478bd9Sstevel@tonic-gate                done < $1
467c478bd9Sstevel@tonic-gate                shift
477c478bd9Sstevel@tonic-gate        done
487c478bd9Sstevel@tonic-gate}
497c478bd9Sstevel@tonic-gate
50*9b5bf10aSMark Haywoodnet_record_err()
51*9b5bf10aSMark Haywood{
52*9b5bf10aSMark Haywood	message=$1
53*9b5bf10aSMark Haywood	err=$2
54*9b5bf10aSMark Haywood
55*9b5bf10aSMark Haywood	echo "$message" | smf_console
56*9b5bf10aSMark Haywood	if [ $err -ne 0 ]; then
57*9b5bf10aSMark Haywood		echo "Error code = $err" | smf_console
58*9b5bf10aSMark Haywood	fi
59*9b5bf10aSMark Haywood}
60*9b5bf10aSMark Haywood
617c478bd9Sstevel@tonic-gate#
62e11c3f44Smeem# inet_list	list of IPv4 interfaces.
63e11c3f44Smeem# inet6_list	list of IPv6 interfaces.
64e11c3f44Smeem# ipmp_list	list of IPMP IPv4 interfaces.
65e11c3f44Smeem# ipmp6_list	list of IPMP IPv6 interfaces.
66e11c3f44Smeem# inet_plumbed	list of plumbed IPv4 interfaces.
67e11c3f44Smeem# inet6_plumbed list of plumbed IPv6 interfaces.
68e11c3f44Smeem# ipmp_created 	list of created IPMP IPv4 interfaces.
69e11c3f44Smeem# ipmp6_created	list of created IPMP IPv6 interfaces.
70e11c3f44Smeem# inet_failed	list of IPv4 interfaces that failed to plumb.
71e11c3f44Smeem# inet6_failed	list of IPv6 interfaces that failed to plumb.
72e11c3f44Smeem# ipmp_failed 	list of IPMP IPv4 interfaces that failed to be created.
73e11c3f44Smeem# ipmp6_failed	list of IPMP IPv6 interfaces that failed to be created.
747c478bd9Sstevel@tonic-gate#
757c478bd9Sstevel@tonic-gateunset inet_list inet_plumbed inet_failed \
76e11c3f44Smeem	inet6_list inet6_plumbed inet6_failed \
77e11c3f44Smeem	ipmp_list ipmp_created ipmp_failed \
78e11c3f44Smeem	ipmp6_list ipmp6_created ipmp6_failed
79e11c3f44Smeem
807c478bd9Sstevel@tonic-gate#
817c478bd9Sstevel@tonic-gate# get_physical interface
827c478bd9Sstevel@tonic-gate#
83e11c3f44Smeem# Return physical interface corresponding to the given interface.
847c478bd9Sstevel@tonic-gate#
857c478bd9Sstevel@tonic-gateget_physical()
867c478bd9Sstevel@tonic-gate{
877c478bd9Sstevel@tonic-gate	ORIGIFS="$IFS"
887c478bd9Sstevel@tonic-gate	IFS="${IFS}:"
897c478bd9Sstevel@tonic-gate	set -- $1
907c478bd9Sstevel@tonic-gate	IFS="$ORIGIFS"
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate	echo $1
937c478bd9Sstevel@tonic-gate}
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate#
967c478bd9Sstevel@tonic-gate# get_logical interface
977c478bd9Sstevel@tonic-gate#
987c478bd9Sstevel@tonic-gate# Return logical interface number.  Zero will be returned
99e11c3f44Smeem# if there is no explicit logical number.
1007c478bd9Sstevel@tonic-gate#
1017c478bd9Sstevel@tonic-gateget_logical()
1027c478bd9Sstevel@tonic-gate{
1037c478bd9Sstevel@tonic-gate	ORIGIFS="$IFS"
1047c478bd9Sstevel@tonic-gate	IFS="${IFS}:"
1057c478bd9Sstevel@tonic-gate	set -- $1
1067c478bd9Sstevel@tonic-gate	IFS="$ORIGIFS"
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate	if [ -z "$2" ]; then
1097c478bd9Sstevel@tonic-gate		echo 0
1107c478bd9Sstevel@tonic-gate	else
1117c478bd9Sstevel@tonic-gate		echo $2
1127c478bd9Sstevel@tonic-gate	fi
1137c478bd9Sstevel@tonic-gate}
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate#
1167c478bd9Sstevel@tonic-gate# if_comp if1 if2
1177c478bd9Sstevel@tonic-gate#
118e11c3f44Smeem# Compare interfaces.  Do the physical interface names and logical interface
1197c478bd9Sstevel@tonic-gate# numbers match?
1207c478bd9Sstevel@tonic-gate#
1217c478bd9Sstevel@tonic-gateif_comp()
1227c478bd9Sstevel@tonic-gate{
123e11c3f44Smeem	physical_comp $1 $2 && [ `get_logical $1` -eq `get_logical $2` ]
1247c478bd9Sstevel@tonic-gate}
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate#
1277c478bd9Sstevel@tonic-gate# physical_comp if1 if2
1287c478bd9Sstevel@tonic-gate#
129e11c3f44Smeem# Do the two interfaces share a physical interface?
1307c478bd9Sstevel@tonic-gate#
1317c478bd9Sstevel@tonic-gatephysical_comp()
1327c478bd9Sstevel@tonic-gate{
1337c478bd9Sstevel@tonic-gate	[ "`get_physical $1`" = "`get_physical $2`" ]
1347c478bd9Sstevel@tonic-gate}
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate#
1377c478bd9Sstevel@tonic-gate# in_list op item list
1387c478bd9Sstevel@tonic-gate#
1397c478bd9Sstevel@tonic-gate# Is "item" in the given list?  Use "op" to do the test, applying it to
1407c478bd9Sstevel@tonic-gate# "item" and each member of the list in turn until it returns success.
1417c478bd9Sstevel@tonic-gate#
1427c478bd9Sstevel@tonic-gatein_list()
1437c478bd9Sstevel@tonic-gate{
1447c478bd9Sstevel@tonic-gate	op=$1
1457c478bd9Sstevel@tonic-gate	item=$2
1467c478bd9Sstevel@tonic-gate	shift 2
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate	while [ $# -gt 0 ]; do
1497c478bd9Sstevel@tonic-gate		$op $item $1 && return 0
1507c478bd9Sstevel@tonic-gate		shift
1517c478bd9Sstevel@tonic-gate	done
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate	return 1
1547c478bd9Sstevel@tonic-gate}
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate#
157e11c3f44Smeem# get_groupifname groupname
158e11c3f44Smeem#
159e11c3f44Smeem# Return the IPMP meta-interface name for the group, if it exists.
160e11c3f44Smeem#
161e11c3f44Smeemget_groupifname()
162e11c3f44Smeem{
163e11c3f44Smeem	/sbin/ipmpstat -gP -o groupname,group | while IFS=: read name ifname; do
164e11c3f44Smeem		if [ "$name" = "$1" ]; then
165e11c3f44Smeem			echo "$ifname"
166e11c3f44Smeem			return
167e11c3f44Smeem		fi
168e11c3f44Smeem	done
169e11c3f44Smeem}
170e11c3f44Smeem
171e11c3f44Smeem#
172e11c3f44Smeem# create_ipmp ifname groupname type
173e11c3f44Smeem#
174e11c3f44Smeem# Helper function for create_groupifname() that returns zero if it's able
175e11c3f44Smeem# to create an IPMP interface of the specified type and place it in the
176e11c3f44Smeem# specified group, or non-zero otherwise.
177e11c3f44Smeem#
178e11c3f44Smeemcreate_ipmp()
179e11c3f44Smeem{
180e11c3f44Smeem	/sbin/ifconfig $1 >/dev/null 2>&1 && return 1
181e11c3f44Smeem	/sbin/ifconfig $1 inet6 >/dev/null 2>&1 && return 1
182e11c3f44Smeem	/sbin/ifconfig $1 $3 ipmp group $2 2>/dev/null
183e11c3f44Smeem}
184e11c3f44Smeem
185e11c3f44Smeem#
186e11c3f44Smeem# create_groupifname groupname type
187e11c3f44Smeem#
188e11c3f44Smeem# Create an IPMP meta-interface name for the group.  We only use this
189e11c3f44Smeem# function if all of the interfaces in the group failed at boot and there
190e11c3f44Smeem# were no /etc/hostname[6].<if> files for the IPMP meta-interface.
191e11c3f44Smeem#
192e11c3f44Smeemcreate_groupifname()
193e11c3f44Smeem{
194e11c3f44Smeem	#
195e11c3f44Smeem	# This is a horrible way to count from 0 to 999, but in sh and
196e11c3f44Smeem	# without necessarily having /usr mounted, what else can we do?
197e11c3f44Smeem	#
198e11c3f44Smeem	for a in "" 1 2 3 4 5 6 7 8 9; do
199e11c3f44Smeem		for b in 0 1 2 3 4 5 6 7 8 9; do
200e11c3f44Smeem			for c in 0 1 2 3 4 5 6 7 8 9; do
201e11c3f44Smeem				# strip leading zeroes
202e11c3f44Smeem				[ "$a" = "" ] && [ "$b" = 0 ] && b=""
203e11c3f44Smeem				if create_ipmp ipmp$a$b$c $1 $2; then
204e11c3f44Smeem					echo ipmp$a$b$c
205e11c3f44Smeem					return
206e11c3f44Smeem				fi
207e11c3f44Smeem			done
208e11c3f44Smeem		done
209e11c3f44Smeem	done
210e11c3f44Smeem}
211e11c3f44Smeem
212e11c3f44Smeem#
213e11c3f44Smeem# get_hostname_ipmpinfo interface type
214e11c3f44Smeem#
215e11c3f44Smeem# Return all requested IPMP keywords from hostname file for a given interface.
2167c478bd9Sstevel@tonic-gate#
2177c478bd9Sstevel@tonic-gate# Example:
218e11c3f44Smeem#	get_hostname_ipmpinfo hme0 inet keyword [ keyword ... ]
2197c478bd9Sstevel@tonic-gate#
220e11c3f44Smeemget_hostname_ipmpinfo()
2217c478bd9Sstevel@tonic-gate{
2227c478bd9Sstevel@tonic-gate	case "$2" in
2237c478bd9Sstevel@tonic-gate		inet)	file=/etc/hostname.$1
2247c478bd9Sstevel@tonic-gate			;;
2257c478bd9Sstevel@tonic-gate		inet6)	file=/etc/hostname6.$1
2267c478bd9Sstevel@tonic-gate			;;
2277c478bd9Sstevel@tonic-gate		*)
2287c478bd9Sstevel@tonic-gate			return
2297c478bd9Sstevel@tonic-gate			;;
2307c478bd9Sstevel@tonic-gate	esac
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate	[ -r "$file" ] || return
2337c478bd9Sstevel@tonic-gate
234e11c3f44Smeem	type=$2
235e11c3f44Smeem	shift 2
236e11c3f44Smeem
2377c478bd9Sstevel@tonic-gate	#
238e11c3f44Smeem	# Read through the hostname file looking for the specified
239e11c3f44Smeem	# keywords.  Since there may be several keywords that cancel
240e11c3f44Smeem	# each other out, the caller must post-process as appropriate.
2417c478bd9Sstevel@tonic-gate	#
2427c478bd9Sstevel@tonic-gate	while read line; do
2437c478bd9Sstevel@tonic-gate		[ -z "$line" ] && continue
244e11c3f44Smeem		/sbin/ifparse -s "$type" $line
245e11c3f44Smeem	done < "$file" | while read one two; do
246e11c3f44Smeem		for keyword in "$@"; do
247e11c3f44Smeem			[ "$one" = "$keyword" ] && echo "$one $two"
248e11c3f44Smeem		done
2497c478bd9Sstevel@tonic-gate	done
2507c478bd9Sstevel@tonic-gate}
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate#
2537c478bd9Sstevel@tonic-gate# get_group_for_type interface type list
2547c478bd9Sstevel@tonic-gate#
2557c478bd9Sstevel@tonic-gate# Look through the set of hostname files associated with the same physical
2567c478bd9Sstevel@tonic-gate# interface as "interface", and determine which group they would configure.
2577c478bd9Sstevel@tonic-gate# Only hostname files associated with the physical interface or logical
2587c478bd9Sstevel@tonic-gate# interface zero are allowed to set the group.
2597c478bd9Sstevel@tonic-gate#
2607c478bd9Sstevel@tonic-gateget_group_for_type()
2617c478bd9Sstevel@tonic-gate{
2627c478bd9Sstevel@tonic-gate	physical=`get_physical $1`
2637c478bd9Sstevel@tonic-gate	type=$2
2647c478bd9Sstevel@tonic-gate	group=""
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate	#
2677c478bd9Sstevel@tonic-gate	# The last setting of the group is the one that counts, which is
2687c478bd9Sstevel@tonic-gate	# the reason for the second while loop.
2697c478bd9Sstevel@tonic-gate	#
2707c478bd9Sstevel@tonic-gate	shift 2
271e11c3f44Smeem	for ifname in "$@"; do
272e11c3f44Smeem		if if_comp "$physical" $ifname; then
273e11c3f44Smeem			get_hostname_ipmpinfo $ifname $type group
2747c478bd9Sstevel@tonic-gate		fi
2757c478bd9Sstevel@tonic-gate	done | while :; do
276e11c3f44Smeem		read keyword grname || {
2777c478bd9Sstevel@tonic-gate			echo "$group"
2787c478bd9Sstevel@tonic-gate			break
2797c478bd9Sstevel@tonic-gate		}
280e11c3f44Smeem		group="$grname"
2817c478bd9Sstevel@tonic-gate	done
2827c478bd9Sstevel@tonic-gate}
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate#
285e11c3f44Smeem# get_group interface
286e11c3f44Smeem#
287e11c3f44Smeem# If there is both an inet and inet6 version of an interface, the group
288e11c3f44Smeem# could be set in either set of hostname files.  Since inet6 is configured
289e11c3f44Smeem# after inet, if there's a setting in both files, inet6 wins.
290e11c3f44Smeem#
291e11c3f44Smeemget_group()
292e11c3f44Smeem{
293e11c3f44Smeem	group=`get_group_for_type $1 inet6 $inet6_list`
294e11c3f44Smeem	[ -z "$group" ] && group=`get_group_for_type $1 inet $inet_list`
295e11c3f44Smeem	echo $group
296e11c3f44Smeem}
297e11c3f44Smeem
298e11c3f44Smeem#
2991cb875aeSCathy Zhou# Given the interface name and the address family (inet or inet6), determine
3001cb875aeSCathy Zhou# whether this is a VRRP VNIC.
3011cb875aeSCathy Zhou#
3021cb875aeSCathy Zhou# This is used to determine whether to bring the interface up
3031cb875aeSCathy Zhou#
3041cb875aeSCathy Zhounot_vrrp_interface() {
3051cb875aeSCathy Zhou	macaddrtype=`/sbin/dladm show-vnic $1 -o MACADDRTYPE -p 2>/dev/null`
3061cb875aeSCathy Zhou
3071cb875aeSCathy Zhou	case "$macaddrtype" in
3081cb875aeSCathy Zhou	'vrrp'*''$2'')	vrrp=1
3091cb875aeSCathy Zhou			;;
3101cb875aeSCathy Zhou        *)		vrrp=0
3111cb875aeSCathy Zhou			;;
3121cb875aeSCathy Zhou	esac
3131cb875aeSCathy Zhou	return $vrrp
3141cb875aeSCathy Zhou}
3151cb875aeSCathy Zhou
3167c478bd9Sstevel@tonic-gate# doDHCPhostname interface
3177c478bd9Sstevel@tonic-gate# Pass to this function the name of an interface.  It will return
3187c478bd9Sstevel@tonic-gate# true if one should enable the use of DHCP client-side host name
3197c478bd9Sstevel@tonic-gate# requests on the interface, and false otherwise.
3207c478bd9Sstevel@tonic-gate#
3217c478bd9Sstevel@tonic-gatedoDHCPhostname()
3227c478bd9Sstevel@tonic-gate{
3237c478bd9Sstevel@tonic-gate	if [ -f /etc/dhcp.$1 ] && [ -f /etc/hostname.$1 ]; then
3247c478bd9Sstevel@tonic-gate                set -- `shcat /etc/hostname.$1`
3257c478bd9Sstevel@tonic-gate                [ $# -eq 2 -a "$1" = "inet" ]
3267c478bd9Sstevel@tonic-gate                return $?
3277c478bd9Sstevel@tonic-gate        fi
3287c478bd9Sstevel@tonic-gate        return 1
3297c478bd9Sstevel@tonic-gate}
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate#
3327c478bd9Sstevel@tonic-gate# inet_process_hostname processor [ args ]
3337c478bd9Sstevel@tonic-gate#
3347c478bd9Sstevel@tonic-gate# Process an inet hostname file.  The contents of the file
3357c478bd9Sstevel@tonic-gate# are taken from standard input. Each line is passed
3367c478bd9Sstevel@tonic-gate# on the command line to the "processor" command.
3377c478bd9Sstevel@tonic-gate# Command line arguments can be passed to the processor.
3387c478bd9Sstevel@tonic-gate#
3397c478bd9Sstevel@tonic-gate# Examples:
3407c478bd9Sstevel@tonic-gate#	inet_process_hostname /sbin/ifconfig hme0 < /etc/hostname.hme0
3417c478bd9Sstevel@tonic-gate#
3427c478bd9Sstevel@tonic-gate#	inet_process_hostname /sbin/ifparse -f < /etc/hostname.hme0
3437c478bd9Sstevel@tonic-gate#
3447c478bd9Sstevel@tonic-gate# If there is only line in an hostname file we assume it contains
3457c478bd9Sstevel@tonic-gate# the old style address which results in the interface being brought up
346e11c3f44Smeem# and the netmask and broadcast address being set ($inet_oneline_epilogue).
3477c478bd9Sstevel@tonic-gate#
3481cb875aeSCathy Zhou# Note that if the interface is a VRRP interface, do not bring the address
3491cb875aeSCathy Zhou# up ($inet_oneline_epilogue_no_up).
3501cb875aeSCathy Zhou#
3517c478bd9Sstevel@tonic-gate# If there are multiple lines we assume the file contains a list of
3527c478bd9Sstevel@tonic-gate# commands to the processor with neither the implied bringing up of the
3537c478bd9Sstevel@tonic-gate# interface nor the setting of the default netmask and broadcast address.
3547c478bd9Sstevel@tonic-gate#
3557c478bd9Sstevel@tonic-gate# Return non-zero if any command fails so that the caller may alert
3567c478bd9Sstevel@tonic-gate# users to errors in the configuration.
3577c478bd9Sstevel@tonic-gate#
3581cb875aeSCathy Zhouinet_oneline_epilogue_no_up="netmask + broadcast +"
359e11c3f44Smeeminet_oneline_epilogue="netmask + broadcast + up"
360e11c3f44Smeem
3617c478bd9Sstevel@tonic-gateinet_process_hostname()
3627c478bd9Sstevel@tonic-gate{
3637c478bd9Sstevel@tonic-gate	if doDHCPhostname $2; then
3647c478bd9Sstevel@tonic-gate		:
3657c478bd9Sstevel@tonic-gate	else
3667c478bd9Sstevel@tonic-gate		#
3677c478bd9Sstevel@tonic-gate		# Redirecting input from a file results in a sub-shell being
3687c478bd9Sstevel@tonic-gate		# used, hence this outer loop surrounding the "multiple_lines"
3697c478bd9Sstevel@tonic-gate		# and "ifcmds" variables.
3707c478bd9Sstevel@tonic-gate		#
3717c478bd9Sstevel@tonic-gate		while :; do
3727c478bd9Sstevel@tonic-gate			multiple_lines=false
3737c478bd9Sstevel@tonic-gate			ifcmds=""
3747c478bd9Sstevel@tonic-gate			retval=0
3757c478bd9Sstevel@tonic-gate
376e11c3f44Smeem			while read one rest; do
3777c478bd9Sstevel@tonic-gate				if [ -n "$ifcmds" ]; then
3787c478bd9Sstevel@tonic-gate					#
3797c478bd9Sstevel@tonic-gate					# This handles the first N-1
3807c478bd9Sstevel@tonic-gate					# lines of a N-line hostname file.
3817c478bd9Sstevel@tonic-gate					#
3827c478bd9Sstevel@tonic-gate					$* $ifcmds || retval=$?
3837c478bd9Sstevel@tonic-gate					multiple_lines=true
3847c478bd9Sstevel@tonic-gate				fi
385e11c3f44Smeem
386e11c3f44Smeem				#
387e11c3f44Smeem				# Strip out the "ipmp" keyword if it's the
388e11c3f44Smeem				# first token, since it's used to control
389e11c3f44Smeem				# interface creation, not configuration.
390e11c3f44Smeem				#
391e11c3f44Smeem				[ "$one" = ipmp ] && one=
392e11c3f44Smeem				ifcmds="$one $rest"
3937c478bd9Sstevel@tonic-gate			done
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate			#
3967c478bd9Sstevel@tonic-gate			# If the hostname file is empty or consists of only
3977c478bd9Sstevel@tonic-gate			# blank lines, break out of the outer loop without
3987c478bd9Sstevel@tonic-gate			# configuring the newly plumbed interface.
3997c478bd9Sstevel@tonic-gate			#
4007c478bd9Sstevel@tonic-gate			[ -z "$ifcmds" ] && return $retval
4017c478bd9Sstevel@tonic-gate			if [ $multiple_lines = false ]; then
4021cb875aeSCathy Zhou				#
403e11c3f44Smeem				# The traditional one-line hostname file.
4041cb875aeSCathy Zhou				# Note that we only bring it up if the
4051cb875aeSCathy Zhou				# interface is not a VRRP VNIC.
4061cb875aeSCathy Zhou				#
4071cb875aeSCathy Zhou				if not_vrrp_interface $2 $3; then
4081cb875aeSCathy Zhou					estr="$inet_oneline_epilogue"
4091cb875aeSCathy Zhou				else
4101cb875aeSCathy Zhou					estr="$inet_oneline_epilogue_no_up"
4111cb875aeSCathy Zhou				fi
4121cb875aeSCathy Zhou				ifcmds="$ifcmds $estr"
4137c478bd9Sstevel@tonic-gate			fi
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate			#
4167c478bd9Sstevel@tonic-gate			# This handles either the single-line case or
4177c478bd9Sstevel@tonic-gate			# the last line of the N-line case.
4187c478bd9Sstevel@tonic-gate			#
4197c478bd9Sstevel@tonic-gate			$* $ifcmds || return $?
4207c478bd9Sstevel@tonic-gate			return $retval
4217c478bd9Sstevel@tonic-gate		done
4227c478bd9Sstevel@tonic-gate	fi
4237c478bd9Sstevel@tonic-gate}
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate#
4267c478bd9Sstevel@tonic-gate# inet6_process_hostname processor [ args ]
4277c478bd9Sstevel@tonic-gate#
4287c478bd9Sstevel@tonic-gate# Process an inet6 hostname file.  The contents of the file
4297c478bd9Sstevel@tonic-gate# are taken from standard input. Each line is passed
4307c478bd9Sstevel@tonic-gate# on the command line to the "processor" command.
4317c478bd9Sstevel@tonic-gate# Command line arguments can be passed to the processor.
4327c478bd9Sstevel@tonic-gate#
4337c478bd9Sstevel@tonic-gate# Examples:
4347c478bd9Sstevel@tonic-gate#	inet6_process_hostname /sbin/ifconfig hme0 inet6 < /etc/hostname6.hme0
4357c478bd9Sstevel@tonic-gate#
4367c478bd9Sstevel@tonic-gate#	inet6_process_hostname /sbin/ifparse -f inet6 < /etc/hostname6.hme0
4377c478bd9Sstevel@tonic-gate#
4387c478bd9Sstevel@tonic-gate# Return non-zero if any of the commands fail so that the caller may alert
4397c478bd9Sstevel@tonic-gate# users to errors in the configuration.
4407c478bd9Sstevel@tonic-gate#
4417c478bd9Sstevel@tonic-gateinet6_process_hostname()
4427c478bd9Sstevel@tonic-gate{
4437c478bd9Sstevel@tonic-gate    	retval=0
444e11c3f44Smeem	while read one rest; do
445e11c3f44Smeem		#
446e11c3f44Smeem	    	# See comment in inet_process_hostname for details.
447e11c3f44Smeem	        #
448e11c3f44Smeem		[ "$one" = ipmp ] && one=
449e11c3f44Smeem		ifcmds="$one $rest"
450e11c3f44Smeem
4517c478bd9Sstevel@tonic-gate		if [ -n "$ifcmds" ]; then
4527c478bd9Sstevel@tonic-gate			$* $ifcmds || retval=$?
4537c478bd9Sstevel@tonic-gate		fi
4547c478bd9Sstevel@tonic-gate	done
4557c478bd9Sstevel@tonic-gate	return $retval
4567c478bd9Sstevel@tonic-gate}
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate#
459e11c3f44Smeem# Process interfaces that failed to plumb.  Find the IPMP meta-interface
460e11c3f44Smeem# that should host the addresses.  For IPv6, only static addresses defined
461e11c3f44Smeem# in hostname6 files are moved, autoconfigured addresses are not moved.
4627c478bd9Sstevel@tonic-gate#
4637c478bd9Sstevel@tonic-gate# Example:
4647c478bd9Sstevel@tonic-gate#	move_addresses inet6
4657c478bd9Sstevel@tonic-gate#
4667c478bd9Sstevel@tonic-gatemove_addresses()
4677c478bd9Sstevel@tonic-gate{
4687c478bd9Sstevel@tonic-gate	type="$1"
4697c478bd9Sstevel@tonic-gate	eval "failed=\"\$${type}_failed\""
4707c478bd9Sstevel@tonic-gate	eval "list=\"\$${type}_list\""
471e11c3f44Smeem	process_func="${type}_process_hostname"
4727c478bd9Sstevel@tonic-gate	processed=""
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate	if [ "$type" = inet ]; then
475e11c3f44Smeem	        typedesc="IPv4"
4767c478bd9Sstevel@tonic-gate		zaddr="0.0.0.0"
4777c478bd9Sstevel@tonic-gate		hostpfx="/etc/hostname"
4787c478bd9Sstevel@tonic-gate	else
479e11c3f44Smeem	        typedesc="IPv6"
4807c478bd9Sstevel@tonic-gate		zaddr="::"
4817c478bd9Sstevel@tonic-gate		hostpfx="/etc/hostname6"
4827c478bd9Sstevel@tonic-gate	fi
4837c478bd9Sstevel@tonic-gate
484e11c3f44Smeem	echo "Moving addresses from missing ${typedesc} interface(s):\c" \
485e11c3f44Smeem	    >/dev/msglog
4867c478bd9Sstevel@tonic-gate
487e11c3f44Smeem	for ifname in $failed; do
488e11c3f44Smeem		in_list if_comp $ifname $processed && continue
489e11c3f44Smeem
490e11c3f44Smeem		group=`get_group $ifname`
491e11c3f44Smeem		if [ -z "$group" ]; then
492e11c3f44Smeem			in_list physical_comp $ifname $processed || {
493e11c3f44Smeem				echo " $ifname (not moved -- not" \
494e11c3f44Smeem				    "in an IPMP group)\c" >/dev/msglog
495e11c3f44Smeem				processed="$processed $ifname"
4967c478bd9Sstevel@tonic-gate			}
4977c478bd9Sstevel@tonic-gate			continue
4987c478bd9Sstevel@tonic-gate		fi
499e11c3f44Smeem
500e11c3f44Smeem		#
501e11c3f44Smeem		# Lookup the IPMP meta-interface name.  If one doesn't exist,
502e11c3f44Smeem		# create it.
503e11c3f44Smeem		#
504e11c3f44Smeem		grifname=`get_groupifname $group`
505e11c3f44Smeem		[ -z "$grifname" ] && grifname=`create_groupifname $group $type`
506e11c3f44Smeem
5077c478bd9Sstevel@tonic-gate		#
5087c478bd9Sstevel@tonic-gate		# The hostname files are processed twice.  In the first
5099bea6098Smeem		# pass, we are looking for all commands that apply to the
5109bea6098Smeem		# non-additional interface address.  These may be
5119bea6098Smeem		# scattered over several files.  We won't know whether the
5129bea6098Smeem		# address represents a failover address or not until we've
5139bea6098Smeem		# read all the files associated with the interface.
514e11c3f44Smeem		#
5157c478bd9Sstevel@tonic-gate		# In the first pass through the hostname files, all
5169bea6098Smeem		# additional logical interface commands are removed.  The
5179bea6098Smeem		# remaining commands are concatenated together and passed
5189bea6098Smeem		# to ifparse to determine whether the non-additional
5199bea6098Smeem		# logical interface address is a failover address.  If it
5209bea6098Smeem		# as a failover address, the address may not be the first
5219bea6098Smeem		# item on the line, so we can't just substitute "addif"
5229bea6098Smeem		# for "set".  We prepend an "addif $zaddr" command, and
5239bea6098Smeem		# let the embedded "set" command set the address later.
5247c478bd9Sstevel@tonic-gate		#
5257c478bd9Sstevel@tonic-gate		/sbin/ifparse -f $type `
5267c478bd9Sstevel@tonic-gate			for item in $list; do
527e11c3f44Smeem				if_comp $ifname $item && $process_func \
528e11c3f44Smeem				    /sbin/ifparse $type < $hostpfx.$item
5297c478bd9Sstevel@tonic-gate			done | while read three four; do
530e11c3f44Smeem				[ "$three" != addif ] && echo "$three $four \c"
5317c478bd9Sstevel@tonic-gate			done` | while read one two; do
5327c478bd9Sstevel@tonic-gate				[ -z "$one" ] && continue
533e11c3f44Smeem				[ "$one $two" = "$inet_oneline_epilogue" ] && \
534e11c3f44Smeem				    continue
5357c478bd9Sstevel@tonic-gate				line="addif $zaddr $one $two"
536e11c3f44Smeem				/sbin/ifconfig $grifname $type $line >/dev/null
5377c478bd9Sstevel@tonic-gate			done
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate		#
5407c478bd9Sstevel@tonic-gate		# In the second pass, look for the the "addif" commands
5417c478bd9Sstevel@tonic-gate		# that configure additional failover addresses.  Addif
5427c478bd9Sstevel@tonic-gate		# commands are not valid in logical interface hostname
5437c478bd9Sstevel@tonic-gate		# files.
5447c478bd9Sstevel@tonic-gate		#
545e11c3f44Smeem		if [ "$ifname" = "`get_physical $ifname`" ]; then
546e11c3f44Smeem			$process_func /sbin/ifparse -f $type < $hostpfx.$ifname \
547e11c3f44Smeem			| while read one two; do
5487c478bd9Sstevel@tonic-gate				[ "$one" = addif ] && \
549e11c3f44Smeem					/sbin/ifconfig $grifname $type \
5507c478bd9Sstevel@tonic-gate				    	    addif $two >/dev/null
5517c478bd9Sstevel@tonic-gate			done
5527c478bd9Sstevel@tonic-gate		fi
5537c478bd9Sstevel@tonic-gate
554e11c3f44Smeem		in_list physical_comp $ifname $processed || {
555e11c3f44Smeem			processed="$processed $ifname"
5569bea6098Smeem			echo " $ifname (moved to $grifname)\c" > /dev/msglog
5577c478bd9Sstevel@tonic-gate		}
558e11c3f44Smeem	done
559e11c3f44Smeem	echo "." >/dev/msglog
560e11c3f44Smeem}
561e11c3f44Smeem
562e11c3f44Smeem#
563550b6e40SSowmini Varadhan# ipadm_from_gz_if ifname
564550b6e40SSowmini Varadhan#
565550b6e40SSowmini Varadhan# Return true if we are in a non-global zone and Layer-3 protection of
566550b6e40SSowmini Varadhan# IP addresses is being enforced on the interface by the global zone
567550b6e40SSowmini Varadhan#
568550b6e40SSowmini Varadhanipadm_from_gz_if()
569550b6e40SSowmini Varadhan{
570550b6e40SSowmini Varadhan	pif=`/sbin/ipadm show-if -o persistent -p $1 2>/dev/null | egrep '4|6'`
571550b6e40SSowmini Varadhan	if smf_is_globalzone || ![[ $pif == *4* || $pif == *6* ]]; then
572550b6e40SSowmini Varadhan		return 1
573550b6e40SSowmini Varadhan	else
574550b6e40SSowmini Varadhan		#
575550b6e40SSowmini Varadhan		# In the non-global zone, plumb the interface to show current
576550b6e40SSowmini Varadhan		# flags and check if Layer-3 protection has been enforced by
577550b6e40SSowmini Varadhan		# the global zone. Note that this function may return
578550b6e40SSowmini Varadhan		# with a plumbed interface. Ideally, we would not have to
579550b6e40SSowmini Varadhan		# plumb the interface to check l3protect, but since we
580550b6e40SSowmini Varadhan		# the `allowed-ips' datalink property cannot currently be
581550b6e40SSowmini Varadhan		# examined in any other way from the non-global zone, we
582550b6e40SSowmini Varadhan		# resort to plumbing the interface
583550b6e40SSowmini Varadhan		#
584550b6e40SSowmini Varadhan		/sbin/ifconfig $1 plumb > /dev/null 2>&1
585550b6e40SSowmini Varadhan		l3protect=`/sbin/ipadm show-if -o current -p $1|grep -c 'Z'`
586550b6e40SSowmini Varadhan		if [ $l3protect = 0 ]; then
587550b6e40SSowmini Varadhan			return 1
588550b6e40SSowmini Varadhan		else
589550b6e40SSowmini Varadhan			return 0
590550b6e40SSowmini Varadhan		fi
591550b6e40SSowmini Varadhan	fi
592550b6e40SSowmini Varadhan}
593550b6e40SSowmini Varadhan
594550b6e40SSowmini Varadhan#
595e11c3f44Smeem# if_configure type class interface_list
596e11c3f44Smeem#
597e11c3f44Smeem# Configure all of the interfaces of type `type' (e.g., "inet6") in
598e11c3f44Smeem# `interface_list' according to their /etc/hostname[6].* files.  `class'
599e11c3f44Smeem# describes the class of interface (e.g., "IPMP"), as a diagnostic aid.
600e11c3f44Smeem# For inet6 interfaces, the interface is also brought up.
601e11c3f44Smeem#
602e11c3f44Smeemif_configure()
603e11c3f44Smeem{
604e11c3f44Smeem	fail=
605e11c3f44Smeem	type=$1
606e11c3f44Smeem	class=$2
607e11c3f44Smeem	process_func=${type}_process_hostname
608e11c3f44Smeem	shift 2
609e11c3f44Smeem
610e11c3f44Smeem	if [ "$type" = inet ]; then
611e11c3f44Smeem	        desc="IPv4"
612e11c3f44Smeem		hostpfx="/etc/hostname"
613e11c3f44Smeem	else
614e11c3f44Smeem	        desc="IPv6"
615e11c3f44Smeem		hostpfx="/etc/hostname6"
616e11c3f44Smeem	fi
617e11c3f44Smeem	[ -n "$class" ] && desc="$class $desc"
618e11c3f44Smeem
619e11c3f44Smeem	echo "configuring $desc interfaces:\c"
620e11c3f44Smeem	while [ $# -gt 0 ]; do
621e11c3f44Smeem		$process_func /sbin/ifconfig $1 $type < $hostpfx.$1 >/dev/null
622e11c3f44Smeem		if [ $? != 0 ]; then
623550b6e40SSowmini Varadhan			ipadm_from_gz_if $1
624550b6e40SSowmini Varadhan			if [ $? != 0 ]; then
625e11c3f44Smeem				fail="$fail $1"
626550b6e40SSowmini Varadhan			fi
627e11c3f44Smeem		elif [ "$type" = inet6 ]; then
6281cb875aeSCathy Zhou			#
6291cb875aeSCathy Zhou			# only bring the interface up if it is not a
6301cb875aeSCathy Zhou			# VRRP VNIC
6311cb875aeSCathy Zhou			#
6321cb875aeSCathy Zhou			if not_vrrp_interface $1 $type; then
633e11c3f44Smeem			    	/sbin/ifconfig $1 inet6 up || fail="$fail $1"
634e11c3f44Smeem			fi
6351cb875aeSCathy Zhou		fi
636e11c3f44Smeem		echo " $1\c"
6377c478bd9Sstevel@tonic-gate		shift
6387c478bd9Sstevel@tonic-gate	done
6397c478bd9Sstevel@tonic-gate	echo "."
640e11c3f44Smeem
641e11c3f44Smeem	[ -n "$fail" ] && warn_failed_ifs "configure $desc" "$fail"
6427c478bd9Sstevel@tonic-gate}
643d62bc4baSyz147064
644d62bc4baSyz147064#
645d62bc4baSyz147064# net_reconfigure is called from the network/physical service (by the
646d62bc4baSyz147064# net-physical and net-nwam method scripts) to perform tasks that only
647d62bc4baSyz147064# need to be done during a reconfigure boot.  This needs to be
648d62bc4baSyz147064# isolated in a function since network/physical has two instances
649d62bc4baSyz147064# (default and nwam) that have distinct method scripts that each need
650d62bc4baSyz147064# to do these things.
651d62bc4baSyz147064#
652d62bc4baSyz147064net_reconfigure ()
653d62bc4baSyz147064{
654d62bc4baSyz147064	#
655d62bc4baSyz147064	# Is this a reconfigure boot?  If not, then there's nothing
656d62bc4baSyz147064	# for us to do.
657d62bc4baSyz147064	#
658f1956ffeSCathy Zhou	reconfig=`svcprop -c -p system/reconfigure \
659f1956ffeSCathy Zhou	    system/svc/restarter:default 2>/dev/null`
660b00044a2SJames Carlson	if [ $? -ne 0 -o "$reconfig" = false ]; then
661d62bc4baSyz147064		return 0
662d62bc4baSyz147064	fi
663d62bc4baSyz147064
664d62bc4baSyz147064	#
665d62bc4baSyz147064	# Ensure that the datalink-management service is running since
666d62bc4baSyz147064	# manifest-import has not yet run for a first boot after
667d62bc4baSyz147064	# upgrade.  We wouldn't need to do that if manifest-import ran
668d62bc4baSyz147064	# earlier in boot, since there is an explicit dependency
669d62bc4baSyz147064	# between datalink-management and network/physical.
670d62bc4baSyz147064	#
671d62bc4baSyz147064	svcadm enable -ts network/datalink-management:default
672d62bc4baSyz147064
673d62bc4baSyz147064	#
674d62bc4baSyz147064	# There is a bug in SMF which causes the svcadm command above
675d62bc4baSyz147064	# to exit prematurely (with an error code of 3) before having
676d62bc4baSyz147064	# waited for the service to come online after having enabled
677d62bc4baSyz147064	# it.  Until that bug is fixed, we need to have the following
678d62bc4baSyz147064	# loop to explicitly wait for the service to come online.
679d62bc4baSyz147064	#
680d62bc4baSyz147064	i=0
681d62bc4baSyz147064	while [ $i -lt 30 ]; do
682d62bc4baSyz147064		i=`expr $i + 1`
683d62bc4baSyz147064		sleep 1
684d62bc4baSyz147064		state=`svcprop -p restarter/state \
685d62bc4baSyz147064		    network/datalink-management:default 2>/dev/null`
686d62bc4baSyz147064		if [ $? -ne 0 ]; then
687d62bc4baSyz147064			continue
688d62bc4baSyz147064		elif [ "$state" = "online" ]; then
689d62bc4baSyz147064			break
690d62bc4baSyz147064		fi
691d62bc4baSyz147064	done
692d62bc4baSyz147064	if [ "$state" != "online" ]; then
693d62bc4baSyz147064		echo "The network/datalink-management service \c"
694d62bc4baSyz147064		echo "did not come online."
695d62bc4baSyz147064		return 1
696d62bc4baSyz147064	fi
697d62bc4baSyz147064
698d62bc4baSyz147064	#
699d62bc4baSyz147064	# Initialize the set of physical links, and validate and
700d62bc4baSyz147064	# remove all the physical links which were removed during the
701d62bc4baSyz147064	# system shutdown.
702d62bc4baSyz147064	#
703d62bc4baSyz147064	/sbin/dladm init-phys
704d62bc4baSyz147064	return 0
705d62bc4baSyz147064}
7064eaa4710SRishi Srivatsavai
7074eaa4710SRishi Srivatsavai#
7084eaa4710SRishi Srivatsavai# Check for use of the default "Port VLAN Identifier" (PVID) -- VLAN 1.
7094eaa4710SRishi Srivatsavai# If there is one for a given interface, then warn the user and force the
7104eaa4710SRishi Srivatsavai# PVID to zero (if it's not already set).  We do this by generating a list
7114eaa4710SRishi Srivatsavai# of interfaces with VLAN 1 in use first, and then parsing out the
7124eaa4710SRishi Srivatsavai# corresponding base datalink entries to check for ones without a
7134eaa4710SRishi Srivatsavai# "default_tag" property.
7144eaa4710SRishi Srivatsavai#
7154eaa4710SRishi Srivatsavaiupdate_pvid()
7164eaa4710SRishi Srivatsavai{
7174eaa4710SRishi Srivatsavai	datalink=/etc/dladm/datalink.conf
7184eaa4710SRishi Srivatsavai
7194eaa4710SRishi Srivatsavai	(
7204eaa4710SRishi Srivatsavai		# Find datalinks using VLAN 1 explicitly
7214eaa4710SRishi Srivatsavai		# configured by dladm
7224eaa4710SRishi Srivatsavai		/usr/bin/nawk '
7234eaa4710SRishi Srivatsavai			/^#/ || NF < 2 { next }
7244eaa4710SRishi Srivatsavai			{ linkdata[$1]=$2; }
7254eaa4710SRishi Srivatsavai			/;vid=int,1;/ {
7264eaa4710SRishi Srivatsavai				sub(/.*;linkover=int,/, "", $2);
7274eaa4710SRishi Srivatsavai				sub(/;.*/, "", $2);
7284eaa4710SRishi Srivatsavai				link=linkdata[$2];
7294eaa4710SRishi Srivatsavai				sub(/name=string,/, "", link);
7304eaa4710SRishi Srivatsavai				sub(/;.*/, "", link);
7314eaa4710SRishi Srivatsavai				print link;
7324eaa4710SRishi Srivatsavai			}' $datalink
7334eaa4710SRishi Srivatsavai	) | ( /usr/bin/sort -u; echo END; cat $datalink ) | /usr/bin/nawk '
7344eaa4710SRishi Srivatsavai	    /^END$/ { state=1; }
7354eaa4710SRishi Srivatsavai	    state == 0 { usingpvid[++nusingpvid]=$1; next; }
7364eaa4710SRishi Srivatsavai	    /^#/ || NF < 2 { next; }
7374eaa4710SRishi Srivatsavai	    {
7384eaa4710SRishi Srivatsavai		# If it is already present and has a tag set,
7394eaa4710SRishi Srivatsavai		# then believe it.
7404eaa4710SRishi Srivatsavai		if (!match($2, /;default_tag=/))
7414eaa4710SRishi Srivatsavai			next;
7424eaa4710SRishi Srivatsavai		sub(/name=string,/, "", $2);
7434eaa4710SRishi Srivatsavai		sub(/;.*/, "", $2);
7444eaa4710SRishi Srivatsavai		for (i = 1; i <= nusingpvid; i++) {
7454eaa4710SRishi Srivatsavai			if (usingpvid[i] == $2)
7464eaa4710SRishi Srivatsavai				usingpvid[i]="";
7474eaa4710SRishi Srivatsavai		}
7484eaa4710SRishi Srivatsavai	    }
7494eaa4710SRishi Srivatsavai	    END {
7504eaa4710SRishi Srivatsavai		for (i = 1; i <= nusingpvid; i++) {
7514eaa4710SRishi Srivatsavai			if (usingpvid[i] != "") {
7524eaa4710SRishi Srivatsavai				printf("Warning: default VLAN tag set to 0" \
7534eaa4710SRishi Srivatsavai				    " on %s\n", usingpvid[i]);
7544eaa4710SRishi Srivatsavai				cmd=sprintf("dladm set-linkprop -p " \
7554eaa4710SRishi Srivatsavai				    "default_tag=0 %s\n", usingpvid[i]);
7564eaa4710SRishi Srivatsavai				system(cmd);
7574eaa4710SRishi Srivatsavai			}
7584eaa4710SRishi Srivatsavai		}
7594eaa4710SRishi Srivatsavai	    }'
7604eaa4710SRishi Srivatsavai}
7616ba597c5SAnurag S. Maskey
7626ba597c5SAnurag S. Maskey#
7636ba597c5SAnurag S. Maskey# service_exists fmri
7646ba597c5SAnurag S. Maskey#
7656ba597c5SAnurag S. Maskey# returns success (0) if the service exists, 1 otherwise.
7666ba597c5SAnurag S. Maskey#
7676ba597c5SAnurag S. Maskeyservice_exists()
7686ba597c5SAnurag S. Maskey{
7696ba597c5SAnurag S. Maskey	/usr/sbin/svccfg -s $1 listpg > /dev/null 2>&1
7706ba597c5SAnurag S. Maskey	if [ $? -eq 0 ]; then
7716ba597c5SAnurag S. Maskey		return 0;
7726ba597c5SAnurag S. Maskey	fi
7736ba597c5SAnurag S. Maskey	return 1;
7746ba597c5SAnurag S. Maskey}
7756ba597c5SAnurag S. Maskey
7766ba597c5SAnurag S. Maskey#
7776ba597c5SAnurag S. Maskey# service_is_enabled fmri
7786ba597c5SAnurag S. Maskey#
7796ba597c5SAnurag S. Maskey# returns success (0) if the service is enabled (permanently or
7806ba597c5SAnurag S. Maskey# temporarily), 1 otherwise.
7816ba597c5SAnurag S. Maskey#
7826ba597c5SAnurag S. Maskeyservice_is_enabled()
7836ba597c5SAnurag S. Maskey{
7846ba597c5SAnurag S. Maskey	#
7856ba597c5SAnurag S. Maskey	# The -c option must be specified to use the composed view
7866ba597c5SAnurag S. Maskey	# because the general/enabled property takes immediate effect.
7876ba597c5SAnurag S. Maskey	# See Example 2 in svcprop(1).
7886ba597c5SAnurag S. Maskey	#
7896ba597c5SAnurag S. Maskey	# Look at the general_ovr/enabled (if it is present) first to
7906ba597c5SAnurag S. Maskey	# determine the temporarily enabled state.
7916ba597c5SAnurag S. Maskey	#
7926ba597c5SAnurag S. Maskey	tstate=`/usr/bin/svcprop -c -p general_ovr/enabled $1 2>/dev/null`
7936ba597c5SAnurag S. Maskey	if [ $? -eq 0 ]; then
7946ba597c5SAnurag S. Maskey		[ "$tstate" = "true" ] && return 0
7956ba597c5SAnurag S. Maskey		return 1
7966ba597c5SAnurag S. Maskey	fi
7976ba597c5SAnurag S. Maskey
7986ba597c5SAnurag S. Maskey        state=`/usr/bin/svcprop -c -p general/enabled $1 2>/dev/null`
7996ba597c5SAnurag S. Maskey	[ "$state" = "true" ] && return 0
8006ba597c5SAnurag S. Maskey	return 1
8016ba597c5SAnurag S. Maskey}
8026ba597c5SAnurag S. Maskey
8036ba597c5SAnurag S. Maskey#
8046ba597c5SAnurag S. Maskey# is_valid_v4addr addr
8056ba597c5SAnurag S. Maskey#
8066ba597c5SAnurag S. Maskey# Returns 0 if a valid IPv4 address is given, 1 otherwise.
8076ba597c5SAnurag S. Maskey#
8086ba597c5SAnurag S. Maskeyis_valid_v4addr()
8096ba597c5SAnurag S. Maskey{
8106ba597c5SAnurag S. Maskey	echo $1 | /usr/xpg4/bin/awk 'NF != 1 { exit 1 } \
8116ba597c5SAnurag S. Maskey	$1 !~ /^((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}\
8126ba597c5SAnurag S. Maskey	(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$/ \
8136ba597c5SAnurag S. Maskey	{ exit 1 }'
8146ba597c5SAnurag S. Maskey	return $?
8156ba597c5SAnurag S. Maskey}
8166ba597c5SAnurag S. Maskey
8176ba597c5SAnurag S. Maskey#
8186ba597c5SAnurag S. Maskey# is_valid_v6addr addr
8196ba597c5SAnurag S. Maskey#
8206ba597c5SAnurag S. Maskey# Returns 0 if a valid IPv6 address is given, 1 otherwise.
8216ba597c5SAnurag S. Maskey#
8226ba597c5SAnurag S. Maskeyis_valid_v6addr()
8236ba597c5SAnurag S. Maskey{
8246ba597c5SAnurag S. Maskey	echo $1 | /usr/xpg4/bin/awk 'NF != 1 { exit 1 } \
8256ba597c5SAnurag S. Maskey	# 1:2:3:4:5:6:7:8
8266ba597c5SAnurag S. Maskey	$1 !~ /^([a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/ &&
8276ba597c5SAnurag S. Maskey	# 1:2:3::6:7:8
8286ba597c5SAnurag S. Maskey	$1 !~ /^([a-fA-F0-9]{1,4}:){0,6}:([a-fA-F0-9]{1,4}:){0,6}\
8296ba597c5SAnurag S. Maskey	[a-fA-F0-9]{1,4}$/ &&
8306ba597c5SAnurag S. Maskey	# 1:2:3::
8316ba597c5SAnurag S. Maskey	$1 !~ /^([a-fA-F0-9]{1,4}:){0,7}:$/ &&
8326ba597c5SAnurag S. Maskey	# ::7:8
8336ba597c5SAnurag S. Maskey	$1 !~ /^:(:[a-fA-F0-9]{1,4}){0,6}:[a-fA-F0-9]{1,4}$/ &&
8346ba597c5SAnurag S. Maskey	# ::f:1.2.3.4
8356ba597c5SAnurag S. Maskey	$1 !~ /^:(:[a-fA-F0-9]{1,4}){0,5}:\
8366ba597c5SAnurag S. Maskey	((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}\
8376ba597c5SAnurag S. Maskey	(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$/ &&
8386ba597c5SAnurag S. Maskey	# a:b:c:d:e:f:1.2.3.4
8396ba597c5SAnurag S. Maskey	$1 !~ /^([a-fA-F0-9]{1,4}:){6}\
8406ba597c5SAnurag S. Maskey	((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\.){3}\
8416ba597c5SAnurag S. Maskey	(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$/ \
8426ba597c5SAnurag S. Maskey	{ exit 1 }'
8436ba597c5SAnurag S. Maskey	return $?
8446ba597c5SAnurag S. Maskey}
8456ba597c5SAnurag S. Maskey
8466ba597c5SAnurag S. Maskey#
8476ba597c5SAnurag S. Maskey# is_valid_addr addr
8486ba597c5SAnurag S. Maskey#
8496ba597c5SAnurag S. Maskey# Returns 0 if a valid IPv4 or IPv6 address is given, 1 otherwise.
8506ba597c5SAnurag S. Maskey#
8516ba597c5SAnurag S. Maskeyis_valid_addr()
8526ba597c5SAnurag S. Maskey{
8536ba597c5SAnurag S. Maskey	is_valid_v4addr $1 || is_valid_v6addr $1
8546ba597c5SAnurag S. Maskey}
8556ba597c5SAnurag S. Maskey
8566ba597c5SAnurag S. Maskey#
8576ba597c5SAnurag S. Maskey# nwam_get_loc_prop location property
8586ba597c5SAnurag S. Maskey#
8596ba597c5SAnurag S. Maskey# echoes the value of the property for the given location
8606ba597c5SAnurag S. Maskey# return:
8616ba597c5SAnurag S. Maskey#	0 => property is set
8626ba597c5SAnurag S. Maskey#	1 => property is not set
8636ba597c5SAnurag S. Maskey#
8646ba597c5SAnurag S. Maskeynwam_get_loc_prop()
8656ba597c5SAnurag S. Maskey{
8666ba597c5SAnurag S. Maskey	value=`/usr/sbin/nwamcfg "select loc $1; get -V $2" 2>/dev/null`
8676ba597c5SAnurag S. Maskey	rtn=$?
8686ba597c5SAnurag S. Maskey	echo $value
8696ba597c5SAnurag S. Maskey	return $rtn
8706ba597c5SAnurag S. Maskey}
87103e9aa6dSRenee Danson Sommerfeld
87203e9aa6dSRenee Danson Sommerfeld#
87303e9aa6dSRenee Danson Sommerfeld# nwam_get_loc_list_prop location property
87403e9aa6dSRenee Danson Sommerfeld#
87503e9aa6dSRenee Danson Sommerfeld# echoes a space-separated list of the property values for the given location
87603e9aa6dSRenee Danson Sommerfeld# return:
87703e9aa6dSRenee Danson Sommerfeld#	0 => property is set
87803e9aa6dSRenee Danson Sommerfeld#	1 => property is not set
87903e9aa6dSRenee Danson Sommerfeld#
88003e9aa6dSRenee Danson Sommerfeldnwam_get_loc_list_prop()
88103e9aa6dSRenee Danson Sommerfeld{
88203e9aa6dSRenee Danson Sommerfeld	clist=`/usr/sbin/nwamcfg "select loc $1; get -V $2" 2>/dev/null`
88303e9aa6dSRenee Danson Sommerfeld	rtn=$?
88403e9aa6dSRenee Danson Sommerfeld	#
88503e9aa6dSRenee Danson Sommerfeld	# nwamcfg gives us a comma-separated list;
88603e9aa6dSRenee Danson Sommerfeld	# need to convert commas to spaces.
88703e9aa6dSRenee Danson Sommerfeld	#
88803e9aa6dSRenee Danson Sommerfeld	slist=`echo $clist | sed -e s/","/" "/g`
88903e9aa6dSRenee Danson Sommerfeld	echo $slist
89003e9aa6dSRenee Danson Sommerfeld	return $rtn
89103e9aa6dSRenee Danson Sommerfeld}
892