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