xref: /illumos-gate/usr/src/cmd/svc/milestone/net-physical (revision e23347b1b88ce2c0847fad6e9467a1f953597aa7)
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
248if smf_is_globalzone; then
249	#
250	# Wifi drivers use special property interfaces that are not supported
251	# by the existing property persistence mechanism, so if a wifi driver
252	# unloads and then is subsequently reloaded, associated links' properties
253	# will not be restored.  For now, wait until after interfaces have been
254	# plumbed (above) to initialize link properties.
255	#
256	/sbin/dladm init-linkprop -w
257fi
258
259#
260# Process the /etc/hostname[6].* files for IPMP interfaces.  Processing these
261# before non-IPMP interfaces avoids accidental implicit IPMP group creation.
262#
263[ -n "$ipmp_created" ] && if_configure inet "IPMP" $ipmp_created
264[ -n "$ipmp6_created" ] && if_configure inet6 "IPMP" $ipmp6_created
265
266#
267# Process the /etc/hostname[6].* files for non-IPMP interfaces.
268#
269[ -n "$inet_plumbed" ] && if_configure inet "" $inet_plumbed
270[ -n "$inet6_plumbed" ] && if_configure inet6 "" $inet6_plumbed
271
272#
273# For the IPv4 and IPv6 interfaces that failed to plumb, find (or create)
274# IPMP meta-interfaces to host their data addresses.
275#
276[ -n "$inet_failed" ] && move_addresses inet
277[ -n "$inet6_failed" ] && move_addresses inet6
278
279# Run DHCP if requested. Skip boot-configured interface.
280interface_names="`echo /etc/dhcp.*[0-9] 2>/dev/null`"
281if [ "$interface_names" != '/etc/dhcp.*[0-9]' ]; then
282	#
283	# First find the primary interface. Default to the first
284	# interface if not specified. First primary interface found
285	# "wins". Use care not to "reconfigure" a net-booted interface
286	# configured using DHCP. Run through the list of interfaces
287	# again, this time trying DHCP.
288	#
289	i4d_fail=
290	firstif=
291	primary=
292	ORIGIFS="$IFS"
293	IFS="${IFS}."
294	set -- $interface_names
295
296	while [ $# -ge 2 ]; do
297		shift
298		[ -z "$firstif" ] && firstif=$1
299
300		for i in `shcat /etc/dhcp\.$1`; do
301			if [ "$i" = primary ]; then
302				primary=$1
303				break
304			fi
305		done
306
307		[ -n "$primary" ] && break
308		shift
309	done
310
311	[ -z "$primary" ] && primary="$firstif"
312	cmdline=`shcat /etc/dhcp\.${primary}`
313
314	if [ "$_INIT_NET_IF" != "$primary" ]; then
315		echo "starting DHCP on primary interface $primary"
316		/sbin/ifconfig $primary auto-dhcp primary $cmdline
317		# Exit code 4 means ifconfig timed out waiting for dhcpagent
318		[ $? != 0 ] && [ $? != 4 ] && i4d_fail="$i4d_fail $primary"
319	fi
320
321	set -- $interface_names
322
323	while [ $# -ge 2 ]; do
324		shift
325		cmdline=`shcat /etc/dhcp\.$1`
326		if [ "$1" != "$primary" -a \
327			"$1" != "$_INIT_NET_IF"  ]; then
328			echo "starting DHCP on interface $1"
329			/sbin/ifconfig $1 dhcp start wait 0 $cmdline
330			# Exit code can't be timeout when wait is 0
331			[ $? != 0 ] && i4d_fail="$i4d_fail $1"
332		fi
333		shift
334	done
335	IFS="$ORIGIFS"
336	unset ORIGIFS
337	[ -n "$i4d_fail" ] && warn_failed_ifs "configure IPv4 DHCP" "$i4d_fail"
338fi
339
340# In order to avoid bringing up the interfaces that have
341# intentionally been left down, perform RARP only if the system
342# has no configured hostname in /etc/nodename
343hostname="`shcat /etc/nodename 2>/dev/null`"
344if [ "$_INIT_NET_STRATEGY" = "rarp" -o -z "$hostname" ]; then
345	/sbin/ifconfig -adD4 auto-revarp netmask + broadcast + up
346fi
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