summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/milestone/net-install
blob: c2e6d1506b0de12866c7f675c9047f5995be95ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/sbin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
#

. /lib/svc/share/smf_include.sh
. /lib/svc/share/net_include.sh

IPADM=/sbin/ipadm
SVCCFG=/usr/sbin/svccfg
SVCPROP=/usr/bin/svcprop
SVCADM=/usr/sbin/svcadm
ROUTE=/sbin/route

NET_NWAM_FMRI="svc:/network/physical:nwam"
NET_INSTALL_FMRI=$SMF_FMRI

NET_V4IF=install_ipv4_interface
NET_V6IF=install_ipv6_interface

NET_UNDEFINED_STRING_PROP="\"\""

net_install_debug=0

unset net_install_name net_install_addrtype net_install_addr \
    net_install_dhcpwait net_install_interface_id \
    net_install_stateless net_install_stateful net_install_route

net_configure_install_if()
{
	ipv6_interface=$1

	case $net_install_addrtype in
	static)
		cmd="$IPADM create-addr -T static "
		cmd=$cmd"-a local=$net_install_addr $net_install_name"
		;;

	dhcp)
		dhcpwait=""
		if [ "$net_install_dhcpwait" != "" ]; then
			dhcpwait="-w $net_install_dhcpwait"
		fi

		cmd="$IPADM create-addr -T dhcp $dhcpwait $net_install_name"
		;;
	addrconf)
		interface_id=""
		if [ "$net_install_interface_id" != "" ]; then
			interface_id="-i $net_install_interface_id"
		fi

		state=""
		if [ "$net_install_stateless" != "" ]; then
			state="-p stateless=$net_install_stateless"
		fi

		if [ "$net_install_stateful" != "" ]; then
			if [ "$state" = "" ]; then
				state="-p stateful=$net_install_stateful"
			else
				state=$state",stateful=$net_install_stateful"
			fi
		fi

		cmd="$IPADM create-addr -T addrconf "
		cmd=$cmd"$interface_id $state $net_install_name"
		;;
	esac

	$cmd
	if [ $? -ne 0 ]; then
		net_record_err "Error configuring interface:\n\"$cmd\"" $?
		return $SMF_EXIT_ERR_FATAL
	fi

	if [ "$net_install_route" != "" ]; then
		if [ $ipv6_interface == 1 ]; then
			details="-inet6 default"
		else
			details="default"
		fi
		ifp=`echo $net_install_name | /usr/bin/cut -f1 -d'/'`
		details="$details $net_install_route -ifp $ifp"
		cmd="$ROUTE add $details"
		$cmd
		cmd="$ROUTE get $details"
		$cmd
		if [ $? -ne 0 ]; then
			err=$?
			msg="Error creating default route:\n\"$cmd\""
			net_record_err "$msg" $err
			return $SMF_EXIT_ERR_FATAL
		fi
		rootdir=/etc/svc/volatile
		/usr/bin/mkdir -p $rootdir/etc/inet
		if [ $? -ne 0 ]; then
			err=$?
			msg="Error creating \"$rootdir/etc/inet\" directory"
			net_record_err "$msg" $err
			return $SMF_EXIT_ERR_FATAL
		fi
		cmd="$ROUTE -R $rootdir -p add $details"
		$cmd
		if [ $? -ne 0 ]; then
			err=$?
			msg="Error adding persistent default route:\n\"$cmd\""
			net_record_err "$msg" $err
			return $SMF_EXIT_ERR_FATAL
		fi
	fi

	return $SMF_EXIT_OK
}

net_process_v4_pg()
{
	net_install_name=""
	net_install_addrtype=""
	net_install_addr=""
	net_install_dhcpwait=""
	net_install_route=""

	#
	# Retrieve the mandatory interface name property value. If
	# the value is empty, then no interface is configured.
	#
	prop=`$SVCPROP -p $NET_V4IF/name $NET_INSTALL_FMRI`
	if [ $? -ne 0 -o "$prop" = "$NET_UNDEFINED_STRING_PROP" ]; then
		return $SMF_EXIT_OK
	fi
	net_install_name=$prop

	#
	# Retrieve the mandatory address type property value. The two
	# valid values are "static" and "dhcp".
	#
	prop=`$SVCPROP -p $NET_V4IF/address_type $NET_INSTALL_FMRI`
	if [ $? -ne 0 -o "$prop" = "$NET_UNDEFINED_STRING_PROP" ]; then
		msg="No \"address_type\" property defined in the "
		msg=$msg"\"$NET_V4IF\" property group"
		net_record_err "$msg" 0
		return $SMF_EXIT_ERR_CONFIG
	fi
	case $prop in
	static | dhcp)
		net_install_addrtype=$prop
		;;
	*)
		msg="Bad value, \"$prop\", defined for the \"address_type\" "
		msg=$msg"property in the \"$NET_V4IF\" property group"
		net_record_err "$msg" 0
		return $SMF_EXIT_ERR_CONFIG
		;;
	esac

	#
	# Retrieve the static address property value. The address property
	# only applies to static address type configurations. If not
	# configuring a static address, then the property should still have
	# its default value of 0.0.0.0/0.
	#
	prop=`$SVCPROP -p $NET_V4IF/static_address $NET_INSTALL_FMRI`
	if [ $? -eq 0 -a "$prop" != "$NET_UNDEFINED_STRING_PROP" ]; then
		if [ "$net_install_addrtype" = "dhcp" ]; then
			if [ "$prop" != "${NET_INADDR_ANY}/0" ]; then
				msg="Warning: static address ignored "
				msg=$msg"in the \"$NET_V4IF\ property group"
				net_record_err "$msg" 0
			fi
		else
			if [ "$prop" = "${NET_INADDR_ANY}/0" ]; then
				msg="Error: static address required in the "
				msg=$msg"\"$NET_V4IF\" property group"
				net_record_err "$msg" 0
				return $SMF_EXIT_ERR_CONFIG
			fi
			net_install_addr=$prop
		fi
	else
		if [ "$net_install_addrtype" = "static" ]; then
			msg="Error: static address required in the "
			msg=$msg"\"$NET_V4IF\" property group"
			net_record_err "$msg" 0
			return $SMF_EXIT_ERR_CONFIG
		fi
	fi

	#
	# Retrieve the optional DHCP wait property value.
	#
	prop=`$SVCPROP -p $NET_V4IF/dhcp_wait $NET_INSTALL_FMRI`
	if [ $? -eq 0 -a "$prop" != "$NET_UNDEFINED_STRING_PROP" ]; then
		if [ "$net_install_addrtype" != "dhcp" ]; then
			if [ "$prop" != "120" ]; then
				msg="Warning: DHCP wait value ignored in the "
				msg=$msg"\"$NET_V4IF\" property group"
				net_record_err "$msg" 0
			fi
		else
			net_install_dhcpwait=$prop
		fi
	fi

	#
	# Retrieve the optional default route property value.
	#
	prop=`$SVCPROP -p $NET_V4IF/default_route $NET_INSTALL_FMRI`
	if [ $? -eq 0 -a "$prop" != "$NET_UNDEFINED_STRING_PROP" ]; then
		if [ "$prop" != "$NET_INADDR_ANY" ]; then
			net_install_route=$prop
		fi
	fi

	net_configure_install_if 0

	return $?
}

net_process_v6_pg()
{
	net_install_name=""
	net_install_addrtype=""
	net_install_addr=""
	net_install_stateless=""
	net_install_stateful=""
	net_install_interface_id=""
	net_install_route=""

	#
	# Retrieve the mandatory interface name property value. If
	# the value is empty, then no interface is configured.
	#
	prop=`$SVCPROP -p $NET_V6IF/name $NET_INSTALL_FMRI`
	if [ $? -ne 0 -o "$prop" = "$NET_UNDEFINED_STRING_PROP" ]; then
		return $SMF_EXIT_OK
	fi
	net_install_name=$prop

	#
	# Retrieve the mandatory address type property value. The two
	# valid values are "static" and "addrconf".
	#
	prop=`$SVCPROP -p $NET_V6IF/address_type $NET_INSTALL_FMRI`
	if [ $? -ne 0 -o "$prop" = "$NET_UNDEFINED_STRING_PROP" ]; then
		msg="No \"address_type\" property defined in the "
		msg=$msg"\"$NET_V6IF\" property group"
		net_record_err "$msg" 0
		return $SMF_EXIT_ERR_CONFIG
	fi
	case $prop in
	static | addrconf)
		net_install_addrtype=$prop
		;;
	*)
		msg="Bad value \"$prop\" defined for \"address_type\""
		net_record_err "$msg" 0
		return $SMF_EXIT_ERR_CONFIG
		;;
	esac

	#
	# Retrieve the static address property value. The address property
	# only applies to static address type configurations. If not
	# configuring a static address, then the property should still have
	# its default value of ::0/0.
	#
	prop=`$SVCPROP -p $NET_V6IF/static_address $NET_INSTALL_FMRI`
	if [ $? -eq 0 -a "$prop" != "$NET_UNDEFINED_STRING_PROP" ]; then
		if [ "$net_install_addrtype" = "addrconf" ]; then
			if [ "$prop" != "${NET_IN6ADDR_ANY_INIT}/0" ]; then
				msg="Warning: static address ignored in the "
				msg=$msg"\"$NET_V6IF\" property group"
				net_record_err "$msg" 0
			fi
		else
			if [ "$prop" = "${NET_IN6ADDR_ANY_INIT}/0" ]; then
				msg="Error: static address required in the "
				msg=$msg"\"$NET_V6IF\" property group"
				net_record_err "$msg" 0
				return $SMF_EXIT_ERR_CONFIG
			fi
			net_install_addr=$prop
		fi
	else
		if [ "$net_install_addrtype" = "static" ]; then
			msg="Error: static address required in the "
			msg=$msg"\"$NET_V6IF\" property group"
			net_record_err "$msg" 0
			return $SMF_EXIT_ERR_CONFIG
		fi

	fi

	#
	# Retrieve the optional interface id property value. The
	# property only applies to addrconf address type configurations.
	# If configuring a static address, then the property should still
	# have its default value of ::0/0.
	#
	prop=`$SVCPROP -p $NET_V6IF/interface_id $NET_INSTALL_FMRI`
	if [ $? -eq 0 -a "$prop" != "$NET_UNDEFINED_STRING_PROP" ]; then
		if [ "$prop" != "${NET_IN6ADDR_ANY_INIT}/0" ]; then
			if [ "$net_install_addrtype" != "addrconf" ]; then
				msg="Warning: interface id value ignored in "
				msg=$msg"the \"$NET_V6IF\" property group"
				net_record_err "$msg" 0
			else
				net_install_interface_id=$prop
			fi
		fi
	fi

	#
	# Retrieve the optional stateless property value. The property
	# only applies to addrconf address type configurations. If
	# configuring a static address, then the property should still
	# have its default value of "yes".
	#
	prop=`$SVCPROP -p $NET_V6IF/stateless $NET_INSTALL_FMRI`
	if [ $? -eq 0 -a "$prop" != "$NET_UNDEFINED_STRING_PROP" ]; then
		case $prop in
		yes)
			net_install_stateless=$prop
			;;
		no)
			if [ "$net_install_addrtype" != "addrconf" ]; then
				msg="Warning: stateless value ignored in the "
				msg=$msg"\"$NET_V6IF\" property group"
				net_record_err "$msg" 0
			else
				net_install_stateless=$prop
			fi
			;;
		*)
			msg="Bad value \"$prop\" defined for the \"stateless\""
			msg=$msg" property in the \"$NET_V6IF\" property group"
			net_record_err "$msg" 0
			return $SMF_EXIT_ERR_CONFIG
		;;
		esac
	fi

	#
	# Retrieve the optional stateful property value. The property
	# only applies to addrconf address type configurations. If
	# configuring a static address, then the property should still
	# have its default value of "yes".
	#
	prop=`$SVCPROP -p $NET_V6IF/stateful $NET_INSTALL_FMRI`
	if [ $? -eq 0 -a "$prop" != "$NET_UNDEFINED_STRING_PROP" ]; then
		case $prop in
		yes)
			net_install_stateful=$prop
			;;
		no)
			if [ "$net_install_addrtype" != "addrconf" ]; then
				msg="Warning: stateless value ignored in the "
				msg=$msg"\"$NET_V6IF\" property group"
				net_record_err "$msg" 0
			else
				net_install_stateful=$prop
			fi
			;;
		*)
			msg="Bad value \"$prop\" defined for the \"stateless\""
			msg=$msg" property in the \"$NET_V6IF\" property group"
			net_record_err "$msg" 0
			return $SMF_EXIT_ERR_CONFIG
		;;
		esac
	fi

	#
	# Retrieve the optional default route property value.
	#
	prop=`$SVCPROP -p $NET_V6IF/default_route $NET_INSTALL_FMRI`
	if [ $? -eq 0 -a "$prop" != "$NET_UNDEFINED_STRING_PROP" ]; then
		if [ "$prop" != "$NET_IN6ADDR_ANY_INIT" ]; then
			net_install_route=$prop
		fi
	fi

	net_configure_install_if 1

	return $?
}

net_process_install()
{
	vout=`$SVCCFG -s $NET_INSTALL_FMRI validate 2>&1`
	if [ "$vout" != "" ]; then
		msg="Validation errors in $NET_INSTALL_FMRI:\n$vout"
		net_record_err "$msg" 0
		return $SMF_EXIT_ERR_CONFIG
	fi

	ecode=$SMF_EXIT_OK
	errs=0
	ifcnt=0
	for intf in $NET_V4IF $NET_V6IF
	do
		pg=`$SVCPROP -p $intf $NET_INSTALL_FMRI`
		if [ $? -eq 0 ]; then
			if service_is_enabled $NET_NWAM_FMRI; then
				msg="NWAM enabled. Install static "
				msg=$msg"configuration ignored."
				net_record_err "$msg" 0
				errs=`expr $errs + 1`
				ecode=$SMF_EXIT_ERR_CONFIG
			else
				if [ "$intf" == "$NET_V4IF" ]; then
					net_process_v4_pg
				else
					net_process_v6_pg
				fi
				if [ $? -ne $SMF_EXIT_OK ]; then
					#
				    	# Last error wins.
					#
					ecode=$?
					errs=`expr $errs + 1`
				else
					ifcnt=`expr $ifcnt + 1`
				fi
			fi
			$SVCCFG -s $NET_INSTALL_FMRI delpg $intf
			$SVCCFG -s $NET_INSTALL_FMRI refresh
		fi
	done

 	if [ $net_install_debug -eq 1 ]; then
		if [ $errs -ne 0 ]; then
			echo "$errs errors encountered" \
			    "configuring interfaces on behalf of install"
		fi

		if [ $ifcnt -ne 0 ]; then
			echo "$ifcnt interfaces configured on" \
			    "behalf of install"
		fi
	fi

	return $ecode
}

#
# The network/install service will be enabled by the install derived profile
# after the intial install. The service will disable itself after processing
# the install defined property values.
#
# When the non-global shared-IP stack zone boots, it tries to bring up this
# service as well. We just want to exit successfully.
#
if smf_is_nonglobalzone; then
	if [ `/sbin/zonename -t` = shared ]; then
		$SVCADM disable $NET_INSTALL_FMRI
		exit $SMF_EXIT_OK
	fi
fi

net_process_install || exit $?

$SVCADM disable $NET_INSTALL_FMRI
exit $SMF_EXIT_OK