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