xref: /illumos-gate/usr/src/cmd/svc/milestone/net-physical (revision 4e9cfc9a015e8ca7d41f7d018c74dc8a692305b3)
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# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
27# All rights reserved.
28#
29#
30# ident	"%Z%%M%	%I%	%E% SMI"
31
32. /lib/svc/share/smf_include.sh
33. /lib/svc/share/net_include.sh
34
35#
36# In a shared-IP zone we need this service to be up, but all of the work
37# it tries to do is irrelevant (and will actually lead to the service
38# failing if we try to do it), so just bail out.
39# In the global zone and exclusive-IP zones we proceed.
40#
41smf_configure_ip || exit $SMF_EXIT_OK
42
43# Print warnings to console
44warn_failed_ifs() {
45	echo "Failed to $1 interface(s): $2" >/dev/msglog
46}
47
48# Make sure that the libraries essential to this stage of booting can be found.
49LD_LIBRARY_PATH=/lib; export LD_LIBRARY_PATH
50
51#
52# Cause ifconfig to not automatically start in.mpathd when IPMP groups are
53# configured.  This is not strictly necessary but makes it so that in.mpathd
54# will always be started explicitly from /etc/init.d/inetinit, when we're
55# sure that /usr is mounted.
56#
57SUNW_NO_MPATHD=; export SUNW_NO_MPATHD
58
59smf_netstrategy
60
61if smf_is_globalzone; then
62	#
63	# Bring up link aggregations and initialize security objects.
64	# Note that link property initialization is deferred until after
65	# IP interfaces are plumbed to ensure that the links will not
66	# be unloaded (and the property settings lost).
67	#
68	/sbin/dladm up-aggr
69	/sbin/dladm init-secobj
70fi
71
72#
73# If the system was net booted by DHCP, hand DHCP management off to the
74# DHCP agent (ifconfig communicates to the DHCP agent through the
75# loopback interface).
76#
77if [ -n "$_INIT_NET_IF" -a "$_INIT_NET_STRATEGY" = "dhcp" ]; then
78	/sbin/dhcpagent -a
79fi
80
81#
82# The network initialization is done early to support diskless and
83# dataless configurations.  For IPv4 interfaces that were configured by
84# the kernel (e.g.  those on diskless machines) and not configured by
85# DHCP, reset the netmask using the local "/etc/netmasks" file if one
86# exists, and then reset the broadcast address based on the netmask.
87#
88/sbin/ifconfig -auD4 netmask + broadcast +
89
90#
91# All the IPv4 and IPv6 interfaces are plumbed before doing any
92# interface configuration.  This prevents errors from plumb failures
93# getting mixed in with the configured interface lists that the script
94# outputs.
95#
96
97#
98# Get the list of IPv4 interfaces to configure by breaking
99# /etc/hostname.* into separate args by using "." as a shell separator
100# character.
101#
102interface_names="`echo /etc/hostname.*[0-9] 2>/dev/null`"
103if [ "$interface_names" != "/etc/hostname.*[0-9]" ]; then
104	ORIGIFS="$IFS"
105	IFS="$IFS."
106	set -- $interface_names
107	IFS="$ORIGIFS"
108	while [ $# -ge 2 ]; do
109		shift
110		if [ "$1" = "xx0" ]; then
111			#
112			# For some unknown historical reason the xx0
113			# ifname is ignored.
114			#
115			shift
116			continue
117		fi
118		if [ $# -gt 1 -a "$2" != "/etc/hostname" ]; then
119			while [ $# -gt 1 -a "$1" != "/etc/hostname" ]; do
120				shift
121			done
122		else
123			inet_list="$inet_list $1"
124			shift
125		fi
126	done
127fi
128
129#
130# Get the list of IPv6 interfaces to configure by breaking
131# /etc/hostname6.* into separate args by using "." as a shell separator
132# character.
133#
134interface_names="`echo /etc/hostname6.*[0-9] 2>/dev/null`"
135if [ "$interface_names" != "/etc/hostname6.*[0-9]" ]; then
136	ORIGIFS="$IFS"
137	IFS="$IFS."
138	set -- $interface_names
139	IFS="$ORIGIFS"
140	while [ $# -ge 2 ]; do
141		shift
142		if [ $# -gt 1 -a "$2" != "/etc/hostname6" ]; then
143			while [ $# -gt 1 -a "$1" != "/etc/hostname6" ]; do
144				shift
145			done
146		else
147			inet6_list="$inet6_list $1"
148			shift
149		fi
150	done
151fi
152
153
154#
155# Step through the IPv4 interface list and try to plumb every interface.
156# Generate list of plumbed and failed IPv4 interfaces.
157#
158if [ -n "$inet_list" ]; then
159	set -- $inet_list
160	while [ $# -gt 0 ]; do
161		/sbin/ifconfig $1 plumb
162		if /sbin/ifconfig $1 inet >/dev/null 2>&1; then
163			inet_plumbed="$inet_plumbed $1"
164		else
165			inet_failed="$inet_failed $1"
166		fi
167		shift
168	done
169	[ -n "$inet_failed" ] && warn_failed_ifs "plumb IPv4" $inet_failed
170fi
171
172# Run autoconf to connect to a WLAN if the interface is a wireless one
173if [ -x /sbin/wificonfig -a -n "$inet_plumbed" ]; then
174	set -- $inet_plumbed
175	while [ $# -gt 0 ]; do
176			if [ -r /dev/wifi/$1 ]; then
177				/sbin/wificonfig -i $1 startconf >/dev/null
178			fi
179		shift
180	done
181fi
182
183#
184# Step through the IPv6 interface list and plumb every interface.
185# Generate list of plumbed and failed IPv6 interfaces.  Each plumbed
186# interface will be brought up later, after processing any contents of
187# the /etc/hostname6.* file.
188#
189if [ -n "$inet6_list" ]; then
190	set -- $inet6_list
191	while [ $# -gt 0 ]; do
192		/sbin/ifconfig $1 inet6 plumb
193		if /sbin/ifconfig $1 inet6 >/dev/null 2>&1; then
194			inet6_plumbed="$inet6_plumbed $1"
195		else
196			inet6_failed="$inet6_failed $1"
197		fi
198		shift
199	done
200	[ -n "$inet6_failed" ] && warn_failed_ifs "plumb IPv6" $inet6_failed
201fi
202
203if smf_is_globalzone; then
204	#
205	# Unfortunately, if a driver unloads and then is subsequently reloaded,
206	# no mechanism currently exists to restore the properties of its
207	# associated links.  Hence, we wait until after interfaces have been
208	# plumbed (above) to initialize link properties.
209	#
210	/sbin/dladm init-linkprop
211fi
212
213#
214# Process the /etc/hostname.* files of plumbed IPv4 interfaces.  If an
215# /etc/hostname file is not present or is empty, the ifconfig auto-dhcp
216# / auto-revarp command will attempt to set the address, later.
217#
218# If /etc/hostname.lo0 exists the loop below will do additional
219# configuration of lo0.
220#
221if [ -n "$inet_plumbed" ]; then
222	i4s_fail=
223	echo "configuring IPv4 interfaces:\c"
224	set -- $inet_plumbed
225	while [ $# -gt 0 ]; do
226		inet_process_hostname /sbin/ifconfig $1 inet \
227		    </etc/hostname.$1 >/dev/null
228		[ $? != 0 ] && i4s_fail="$i4s_fail $1"
229		echo " $1\c"
230		shift
231	done
232	echo "."
233	[ -n "$i4s_fail" ] && warn_failed_ifs "configure IPv4" $i4s_fail
234fi
235
236#
237# Process the /etc/hostname6.* files of plumbed IPv6 interfaces.  After
238# processing the hostname6 file, bring the interface up.  If
239# /etc/hostname6.lo0 exists the loop below will do additional
240# configuration of lo0.
241#
242if [ -n "$inet6_plumbed" ]; then
243	i6_fail=
244	echo "configuring IPv6 interfaces:\c"
245	set -- $inet6_plumbed
246	while [ $# -gt 0 ]; do
247		inet6_process_hostname /sbin/ifconfig $1 inet6 \
248		    </etc/hostname6.$1 >/dev/null &&
249		    /sbin/ifconfig $1 inet6 up
250		[ $? != 0 ] && i6_fail="$i6_fail $1"
251		echo " $1\c"
252		shift
253	done
254	echo "."
255	[ -n "$i6_fail" ] && warn_failed_ifs "configure IPv6" $i6_fail
256fi
257
258# Run DHCP if requested. Skip boot-configured interface.
259interface_names="`echo /etc/dhcp.*[0-9] 2>/dev/null`"
260if [ "$interface_names" != '/etc/dhcp.*[0-9]' ]; then
261	#
262	# First find the primary interface. Default to the first
263	# interface if not specified. First primary interface found
264	# "wins". Use care not to "reconfigure" a net-booted interface
265	# configured using DHCP. Run through the list of interfaces
266	# again, this time trying DHCP.
267	#
268	i4d_fail=
269	firstif=
270	primary=
271	ORIGIFS="$IFS"
272	IFS="${IFS}."
273	set -- $interface_names
274
275	while [ $# -ge 2 ]; do
276		shift
277		[ -z "$firstif" ] && firstif=$1
278
279		for i in `shcat /etc/dhcp\.$1`; do
280			if [ "$i" = primary ]; then
281				primary=$1
282				break
283			fi
284		done
285
286		[ -n "$primary" ] && break
287		shift
288	done
289
290	[ -z "$primary" ] && primary="$firstif"
291	cmdline=`shcat /etc/dhcp\.${primary}`
292
293	if [ "$_INIT_NET_IF" != "$primary" ]; then
294		echo "starting DHCP on primary interface $primary"
295		/sbin/ifconfig $primary auto-dhcp primary $cmdline
296		# Exit code 4 means ifconfig timed out waiting for dhcpagent
297		[ $? != 0 ] && [ $? != 4 ] && i4d_fail="$i4d_fail $primary"
298	fi
299
300	set -- $interface_names
301
302	while [ $# -ge 2 ]; do
303		shift
304		cmdline=`shcat /etc/dhcp\.$1`
305		if [ "$1" != "$primary" -a \
306			"$1" != "$_INIT_NET_IF"  ]; then
307			echo "starting DHCP on interface $1"
308			/sbin/ifconfig $1 dhcp start wait 0 $cmdline
309			# Exit code can't be timeout when wait is 0
310			[ $? != 0 ] && i4d_fail="$i4d_fail $1"
311		fi
312		shift
313	done
314	IFS="$ORIGIFS"
315	unset ORIGIFS
316	[ -n "$i4d_fail" ] && warn_failed_ifs "configure IPv4 DHCP" $i4d_fail
317fi
318
319# In order to avoid bringing up the interfaces that have
320# intentionally been left down, perform RARP only if the system
321# has no configured hostname in /etc/nodename
322hostname="`shcat /etc/nodename 2>/dev/null`"
323if [ "$_INIT_NET_STRATEGY" = "rarp" -o -z "$hostname" ]; then
324	/sbin/ifconfig -adD4 auto-revarp netmask + broadcast + up
325fi
326
327#
328# Process IPv4 and IPv6 interfaces that failed to plumb.  Find an
329# alternative interface to host the addresses.
330#
331[ -n "$inet_failed" ] && move_addresses inet
332
333[ -n "$inet6_failed" ] && move_addresses inet6
334
335#
336# If the /etc/defaultrouter file exists, process it now so that the next
337# stage of booting will have access to NFS.
338#
339if [ -f /etc/defaultrouter ]; then
340	while read router rubbish; do
341		case "$router" in
342			'#'* | '') ;;	#  Ignore comments, empty lines
343			*)	/sbin/route -n add default -gateway $router ;;
344		esac
345	done </etc/defaultrouter
346fi
347
348#
349# If we get here and were not asked to plumb any IPv4 interfaces, look
350# for boot properties that direct us.
351#
352# - The "network-interface" property is required and indicates the
353#   interface name.
354# - The "xpv-hcp" property, if present, is used by the hypervisor
355#   tools to indicate how the specified interface should be configured.
356#   Permitted values are "dhcp" and "off", where "off" indicates static
357#   IP configuration.
358#
359# In the case where "xpv-hcp" is set to "dhcp", no further properties
360# are required or examined.
361#
362# In the case where "xpv-hcp" is not present or set to "off", the
363# "host-ip" and "subnet-mask" properties are used to configure
364# the specified interface.  The "router-ip" property, if present,
365# is used to add a default route.
366#
367nic="`/sbin/devprop network-interface`"
368if smf_is_globalzone && [ -z "$inet_list" ] && [ -n "$nic" ]; then
369	hcp="`/sbin/devprop xpv-hcp`"
370	case "$hcp" in
371	"dhcp")
372		/sbin/ifconfig $nic plumb 2>/dev/null
373		[ -n "`/sbin/ifconfig $nic 2>/dev/null`" ] && (
374			# The interface is successfully plumbed, so
375			# modify "inet_list" to force the exit code
376			# checks to work.
377			inet_list=$nic;
378			# Given that this is the only IPv4 interface,
379			# we assert that it is primary.
380			echo "starting DHCP on primary interface $primary";
381			/sbin/ifconfig $nic auto-dhcp primary;
382			# Exit code 4 means ifconfig timed out waiting
383			# for dhcpagent
384			[ $? != 0 ] && [ $? != 4 ] && \
385			    i4d_fail="$i4d_fail $nic";
386		)
387		;;
388
389	"off"|"")
390		/sbin/devprop host-ip subnet-mask router-ip | (
391			read ip;
392			read mask;
393			read router;
394			[ -n "$ip" ] && [ -n "$mask" ] && \
395				/sbin/ifconfig $nic plumb 2>/dev/null
396			[ -n "`/sbin/ifconfig $nic 2>/dev/null`" ] && (
397				# The interface is successfully
398				# plumbed, so modify "inet_list" to
399				# force the exit code checks to work.
400				inet_list=$nic;
401				/sbin/ifconfig $nic  inet $ip \
402				    netmask $mask broadcast + up 2>/dev/null;
403				[ -n "$router" ] && route add \
404				    default $router 2>/dev/null;
405			)
406		)
407		;;
408	esac
409fi
410
411#
412# We tell smf this service is online if any of the following is true:
413# - no interfaces were configured for plumbing and no DHCP failures
414# - any non-loopback IPv4 interfaces are up and have a non-zero address
415# - there are any DHCP interfaces started
416# - any non-loopback IPv6 interfaces are up
417#
418# If we weren't asked to configure any interfaces, exit
419if [ -z "$inet_list" ] && [ -z "$inet6_list" ]; then
420	# Config error if DHCP was attempted without plumbed interfaces
421	[ -n "$i4d_fail" ] && exit $SMF_EXIT_ERR_CONFIG
422	exit $SMF_EXIT_OK
423fi
424
425# Any non-loopback IPv4 interfaces with usable addresses up?
426if [ -n "`/sbin/ifconfig -a4u`" ]; then
427    	/sbin/ifconfig -a4u | while read intf addr rest; do
428		[ $intf = inet ] && [ $addr != 127.0.0.1 ] &&
429		[ $addr != 0.0.0.0 ] && exit $SMF_EXIT_OK
430	done && exit $SMF_EXIT_OK
431fi
432
433# Any DHCP interfaces started?
434[ -n "`/sbin/ifconfig -a4 dhcp status 2>/dev/null`" ] && exit $SMF_EXIT_OK
435
436# Any non-loopback IPv6 interfaces up?
437if [ -n "`/sbin/ifconfig -au6`" ]; then
438	/sbin/ifconfig -au6 | while read intf addr rest; do
439		[ $intf = inet6 ] && [ $addr != ::1/128 ] && exit $SMF_EXIT_OK
440	done && exit $SMF_EXIT_OK
441fi
442
443# This service was supposed to configure something yet didn't.  Exit
444# with config error.
445exit $SMF_EXIT_ERR_CONFIG
446