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