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