xref: /titanic_50/usr/src/cmd/zoneadmd/vplat.c (revision bf1d7e28fd966a3f7e92b40aa301efdedc81ef7b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
23cf6b13d2Sgjelinek  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
317c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
327c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
337c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
347c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
357c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
367c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
37108322fbScarlsonj  *
38108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
39108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
40108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
41108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
42108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
43108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
44108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
45108322fbScarlsonj  * without relying on any binaries in the zone itself.
46108322fbScarlsonj  *
47108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
48108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
49108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
50108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
51108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
52108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
53108322fbScarlsonj  * process.
54108322fbScarlsonj  *
55108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
56108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
57108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
58108322fbScarlsonj  * $ZONEPATH/lu as the root.
59108322fbScarlsonj  *
60108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
61108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
62108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
63108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/param.h>
677c478bd9Sstevel@tonic-gate #include <sys/mount.h>
687c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
697c478bd9Sstevel@tonic-gate #include <sys/socket.h>
707c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
717c478bd9Sstevel@tonic-gate #include <sys/types.h>
727c478bd9Sstevel@tonic-gate #include <sys/stat.h>
737c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
747c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
757c478bd9Sstevel@tonic-gate #include <sys/conf.h>
767c478bd9Sstevel@tonic-gate 
77f4b3ec61Sdh155122 #include <sys/dlpi.h>
78f4b3ec61Sdh155122 #include <libdlpi.h>
79f4b3ec61Sdh155122 #include <libdladm.h>
80f4b3ec61Sdh155122 
817c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
827c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
837c478bd9Sstevel@tonic-gate #include <netinet/in.h>
847c478bd9Sstevel@tonic-gate #include <net/route.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <stdio.h>
877c478bd9Sstevel@tonic-gate #include <errno.h>
887c478bd9Sstevel@tonic-gate #include <fcntl.h>
897c478bd9Sstevel@tonic-gate #include <unistd.h>
907c478bd9Sstevel@tonic-gate #include <rctl.h>
917c478bd9Sstevel@tonic-gate #include <stdlib.h>
927c478bd9Sstevel@tonic-gate #include <string.h>
937c478bd9Sstevel@tonic-gate #include <strings.h>
947c478bd9Sstevel@tonic-gate #include <wait.h>
957c478bd9Sstevel@tonic-gate #include <limits.h>
967c478bd9Sstevel@tonic-gate #include <libgen.h>
97fa9e4066Sahrens #include <libzfs.h>
98facf4a8dSllai1 #include <libdevinfo.h>
997c478bd9Sstevel@tonic-gate #include <zone.h>
1007c478bd9Sstevel@tonic-gate #include <assert.h>
101555afedfScarlsonj #include <libcontract.h>
102555afedfScarlsonj #include <libcontract_priv.h>
103555afedfScarlsonj #include <uuid/uuid.h>
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1067c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1077c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1087c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
109fa9e4066Sahrens #include <sys/fs/zfs.h>
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #include <pool.h>
1127c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1130209230bSgjelinek #include <sys/priocntl.h>
1147c478bd9Sstevel@tonic-gate 
1159acbbeafSnn35248 #include <libbrand.h>
1169acbbeafSnn35248 #include <sys/brand.h>
1177c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
11839d3e169Sevanl #include <synch.h>
11922321485Svp157776 
1207c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
12145916cd2Sjpk #include <tsol/label.h>
12245916cd2Sjpk #include <libtsnet.h>
12345916cd2Sjpk #include <sys/priv.h>
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1267c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1297c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1307c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1337c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13645916cd2Sjpk #define	MAXTNZLEN	2048
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /* for routing socket */
1397c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1407c478bd9Sstevel@tonic-gate 
141108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
142108322fbScarlsonj static char kernzone[ZONENAME_MAX];
143108322fbScarlsonj 
144108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
145108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
146108322fbScarlsonj 
14745916cd2Sjpk /* for Trusted Extensions */
14845916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
14945916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
15045916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
15145916cd2Sjpk static m_label_t *zlabel = NULL;
15245916cd2Sjpk static m_label_t *zid_label = NULL;
15345916cd2Sjpk static priv_set_t *zprivs = NULL;
15445916cd2Sjpk 
1557c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1567c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
159108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
160108322fbScarlsonj  * data) only once every N times through the loop.
161108322fbScarlsonj  */
162108322fbScarlsonj #define	MNTTAB_HUNK	32
163108322fbScarlsonj 
164108322fbScarlsonj /*
1657c478bd9Sstevel@tonic-gate  * Private autofs system call
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static int
1707c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
178108322fbScarlsonj static void
179108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
180108322fbScarlsonj {
181108322fbScarlsonj 	uint_t i;
182108322fbScarlsonj 
183108322fbScarlsonj 	if (mnt_array == NULL)
184108322fbScarlsonj 		return;
185108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
186108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
187108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
188108322fbScarlsonj 		free(mnt_array[i].mnt_special);
189108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
190108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
191108322fbScarlsonj 	}
192108322fbScarlsonj 	free(mnt_array);
193108322fbScarlsonj }
194108322fbScarlsonj 
195108322fbScarlsonj /*
196108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
197108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
198108322fbScarlsonj  * array in "nelemp".
199108322fbScarlsonj  */
200108322fbScarlsonj static int
201108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
202108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
203108322fbScarlsonj {
204108322fbScarlsonj 	struct mnttab mnt;
205108322fbScarlsonj 	struct mnttab *mnts;
206108322fbScarlsonj 	struct mnttab *mnp;
207108322fbScarlsonj 	uint_t nmnt;
208108322fbScarlsonj 
209108322fbScarlsonj 	rewind(mnttab);
210108322fbScarlsonj 	resetmnttab(mnttab);
211108322fbScarlsonj 	nmnt = 0;
212108322fbScarlsonj 	mnts = NULL;
213108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
214108322fbScarlsonj 		struct mnttab *tmp_array;
215108322fbScarlsonj 
216108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
217108322fbScarlsonj 			continue;
218108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
219108322fbScarlsonj 			tmp_array = realloc(mnts,
220108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
221108322fbScarlsonj 			if (tmp_array == NULL) {
222108322fbScarlsonj 				free_mnttable(mnts, nmnt);
223108322fbScarlsonj 				return (-1);
224108322fbScarlsonj 			}
225108322fbScarlsonj 			mnts = tmp_array;
226108322fbScarlsonj 		}
227108322fbScarlsonj 		mnp = &mnts[nmnt++];
228108322fbScarlsonj 
229108322fbScarlsonj 		/*
230108322fbScarlsonj 		 * Zero out any fields we're not using.
231108322fbScarlsonj 		 */
232108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
233108322fbScarlsonj 
234108322fbScarlsonj 		if (mnt.mnt_special != NULL)
235108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
236108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
237108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
238108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
239108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
240108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
241108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
242108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
243108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
244108322fbScarlsonj 			free_mnttable(mnts, nmnt);
245108322fbScarlsonj 			return (-1);
246108322fbScarlsonj 		}
247108322fbScarlsonj 	}
248108322fbScarlsonj 	*mnt_arrayp = mnts;
249108322fbScarlsonj 	*nelemp = nmnt;
250108322fbScarlsonj 	return (0);
251108322fbScarlsonj }
252108322fbScarlsonj 
253108322fbScarlsonj /*
254108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
255108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
256108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
257108322fbScarlsonj  * reread of the list of mount points
258108322fbScarlsonj  */
259108322fbScarlsonj static void
260108322fbScarlsonj lofs_discard_mnttab(void)
261108322fbScarlsonj {
262108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
263108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
264108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
265108322fbScarlsonj }
266108322fbScarlsonj 
267108322fbScarlsonj static int
268108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
269108322fbScarlsonj {
270108322fbScarlsonj 	FILE *mnttab;
271108322fbScarlsonj 	uint_t nmnts;
272108322fbScarlsonj 
273108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
274108322fbScarlsonj 		return (-1);
275108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
276108322fbScarlsonj 	    &nmnts) == -1) {
277108322fbScarlsonj 		(void) fclose(mnttab);
278108322fbScarlsonj 		return (-1);
279108322fbScarlsonj 	}
280108322fbScarlsonj 	(void) fclose(mnttab);
281108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
282108322fbScarlsonj 	return (0);
283108322fbScarlsonj }
284108322fbScarlsonj 
285108322fbScarlsonj /*
286108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
287108322fbScarlsonj  * path and resolves them all down to an absolute path.
288108322fbScarlsonj  */
289108322fbScarlsonj static void
290108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
291108322fbScarlsonj {
292108322fbScarlsonj 	int len, arlen;
293108322fbScarlsonj 	const char *altroot;
294108322fbScarlsonj 	char tmppath[MAXPATHLEN];
295108322fbScarlsonj 	boolean_t outside_altroot;
296108322fbScarlsonj 
297108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
298108322fbScarlsonj 		return;
299108322fbScarlsonj 	tmppath[len] = '\0';
300108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
301108322fbScarlsonj 
302108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
303108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
304108322fbScarlsonj 		return;
305108322fbScarlsonj 
306108322fbScarlsonj 	altroot = zonecfg_get_root();
307108322fbScarlsonj 	arlen = strlen(altroot);
308108322fbScarlsonj 	outside_altroot = B_FALSE;
309108322fbScarlsonj 	for (;;) {
310108322fbScarlsonj 		struct mnttab *mnp;
311108322fbScarlsonj 
3121e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3131e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3141e9d8f9fSdminer 		    mnp--) {
315108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
316108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3171e9d8f9fSdminer 			    mnp->mnt_special == NULL)
318108322fbScarlsonj 				continue;
319108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
320108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
321108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
322108322fbScarlsonj 				break;
323108322fbScarlsonj 		}
3241e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3251e9d8f9fSdminer 			break;
3261e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3271e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
328108322fbScarlsonj 			break;
329108322fbScarlsonj 		if (outside_altroot) {
330108322fbScarlsonj 			char *cp;
331108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
332108322fbScarlsonj 
333108322fbScarlsonj 			/*
334108322fbScarlsonj 			 * If we run into a read-only mount outside of the
335108322fbScarlsonj 			 * alternate root environment, then the user doesn't
336108322fbScarlsonj 			 * want this path to be made read-write.
337108322fbScarlsonj 			 */
338108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
339108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
340108322fbScarlsonj 			    NULL &&
341108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
342108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
343108322fbScarlsonj 				break;
344108322fbScarlsonj 			}
345108322fbScarlsonj 		} else if (arlen > 0 &&
346108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
347108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
348108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
349108322fbScarlsonj 			outside_altroot = B_TRUE;
350108322fbScarlsonj 		}
351108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
352108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
353108322fbScarlsonj 		    mnp->mnt_special, path + len);
354108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
355108322fbScarlsonj 			break;
356108322fbScarlsonj 		path[len] = '\0';
357108322fbScarlsonj 	}
358108322fbScarlsonj }
359108322fbScarlsonj 
360108322fbScarlsonj /*
361108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
362108322fbScarlsonj  * referenced device is already mounted somewhere.
363108322fbScarlsonj  */
364108322fbScarlsonj static int
365108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
366108322fbScarlsonj {
367108322fbScarlsonj 	struct mnttab *mnp;
368108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
369108322fbScarlsonj 
370108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
371108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
372108322fbScarlsonj 		return (-1);
373108322fbScarlsonj 
374108322fbScarlsonj 	/*
375108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
376108322fbScarlsonj 	 * no need to worry about conflicting mounts.
377108322fbScarlsonj 	 */
378108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
379108322fbScarlsonj 	    mnp++) {
380108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
381108322fbScarlsonj 			break;
382108322fbScarlsonj 	}
383108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
384108322fbScarlsonj 		return (0);
385108322fbScarlsonj 
386108322fbScarlsonj 	/*
387108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
388108322fbScarlsonj 	 */
389108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
390108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
391108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
392108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
393108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
394108322fbScarlsonj 
395108322fbScarlsonj 	/*
396108322fbScarlsonj 	 * Discard all but one of the original options and set that to be the
397108322fbScarlsonj 	 * same set of options used for inherit package directory resources.
398108322fbScarlsonj 	 */
399108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
400108322fbScarlsonj 	if (optptr == NULL) {
401108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
402108322fbScarlsonj 		if (optptr == NULL) {
403108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
404108322fbScarlsonj 			    fsptr->zone_fs_dir);
405108322fbScarlsonj 			return (-1);
406108322fbScarlsonj 		}
407108322fbScarlsonj 	} else {
408108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
409108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
410108322fbScarlsonj 			free(onext);
411108322fbScarlsonj 		}
412108322fbScarlsonj 	}
413108322fbScarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
414108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
415108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
416108322fbScarlsonj 	return (0);
417108322fbScarlsonj }
418108322fbScarlsonj 
4197c478bd9Sstevel@tonic-gate static int
4207c478bd9Sstevel@tonic-gate make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4237c478bd9Sstevel@tonic-gate 	struct stat st;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4267c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4287c478bd9Sstevel@tonic-gate 		    subdir);
4297c478bd9Sstevel@tonic-gate 		return (-1);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4337c478bd9Sstevel@tonic-gate 		/*
4347c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4357c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4367c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4377c478bd9Sstevel@tonic-gate 		 */
4387c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
43945916cd2Sjpk 			if (is_system_labeled() &&
44045916cd2Sjpk 			    S_ISREG(st.st_mode)) {
44145916cd2Sjpk 				/*
44245916cd2Sjpk 				 * The need to mount readonly copies of
44345916cd2Sjpk 				 * global zone /etc/ files is unique to
44445916cd2Sjpk 				 * Trusted Extensions.
44545916cd2Sjpk 				 */
44645916cd2Sjpk 				if (strncmp(subdir, "/etc/",
44745916cd2Sjpk 				    strlen("/etc/")) != 0) {
44845916cd2Sjpk 					zerror(zlogp, B_FALSE,
44945916cd2Sjpk 					    "%s is not in /etc", path);
4507c478bd9Sstevel@tonic-gate 					return (-1);
4517c478bd9Sstevel@tonic-gate 				}
45245916cd2Sjpk 			} else {
45345916cd2Sjpk 				zerror(zlogp, B_FALSE,
45445916cd2Sjpk 				    "%s is not a directory", path);
45545916cd2Sjpk 				return (-1);
45645916cd2Sjpk 			}
45745916cd2Sjpk 		}
4587c478bd9Sstevel@tonic-gate 	} else if (mkdirp(path, mode) != 0) {
4597c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4607c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4617c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4627c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4637c478bd9Sstevel@tonic-gate 		else
4647c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4657c478bd9Sstevel@tonic-gate 		return (-1);
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	return (0);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate static void
4717c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	uint_t i;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (types == NULL)
4767c478bd9Sstevel@tonic-gate 		return;
4777c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
4787c478bd9Sstevel@tonic-gate 		free(types[i]);
4797c478bd9Sstevel@tonic-gate 	free(types);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate static char **
4837c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	char **types = NULL;
4867c478bd9Sstevel@tonic-gate 	FILE *fp;
4877c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
4887c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
4897c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
4907c478bd9Sstevel@tonic-gate 	uint_t i;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
4937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
4947c478bd9Sstevel@tonic-gate 		return (NULL);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 	/*
4977c478bd9Sstevel@tonic-gate 	 * Count the number of lines
4987c478bd9Sstevel@tonic-gate 	 */
4997c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5007c478bd9Sstevel@tonic-gate 		lines++;
5017c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5027c478bd9Sstevel@tonic-gate 		goto out;
5037c478bd9Sstevel@tonic-gate 	rewind(fp);
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5067c478bd9Sstevel@tonic-gate 	 */
5077c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5087c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5107c478bd9Sstevel@tonic-gate 		goto out;
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 	i = 0;
5137c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5147c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5157c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5167c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5177c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5187c478bd9Sstevel@tonic-gate 			types = NULL;
5197c478bd9Sstevel@tonic-gate 			goto out;
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5227c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5237c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5247c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5257c478bd9Sstevel@tonic-gate 			types = NULL;
5267c478bd9Sstevel@tonic-gate 			goto out;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		i++;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate out:
5317c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5327c478bd9Sstevel@tonic-gate 	return (types);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate static boolean_t
5367c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	uint_t i;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5417c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5427c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5437c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5447c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
549108322fbScarlsonj /*
550108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
551108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
552108322fbScarlsonj  */
5537c478bd9Sstevel@tonic-gate static void
554108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5557c478bd9Sstevel@tonic-gate {
5569acbbeafSnn35248 	assert(zone_isnative);
5579acbbeafSnn35248 
558108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
559108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
560108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5657c478bd9Sstevel@tonic-gate  *
5667c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5677c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5687c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5697c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5707c478bd9Sstevel@tonic-gate  *
5717c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5727c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5737c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5747c478bd9Sstevel@tonic-gate  *
5757c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5767c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5777c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5787c478bd9Sstevel@tonic-gate  *
5797c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5807c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5817c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5827c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5837c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5847c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5857c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5867c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5877c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5887c478bd9Sstevel@tonic-gate  * graceful mounts first.
5897c478bd9Sstevel@tonic-gate  *
5907c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate static int
593108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	int error = 0;
5967c478bd9Sstevel@tonic-gate 	FILE *mnttab;
5977c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
5987c478bd9Sstevel@tonic-gate 	uint_t nmnt;
5997c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6007c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6017c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6027c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6037c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6067c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6077c478bd9Sstevel@tonic-gate 		return (-1);
6087c478bd9Sstevel@tonic-gate 	}
609108322fbScarlsonj 	if (unmount_cmd)
610108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6137c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6147c478bd9Sstevel@tonic-gate 
61545916cd2Sjpk 	/*
61645916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
61745916cd2Sjpk 	 * of our zone's /export/home
61845916cd2Sjpk 	 */
61948451833Scarlsonj 	if (!unmount_cmd)
62045916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
62145916cd2Sjpk 
6227c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6247c478bd9Sstevel@tonic-gate 		return (-1);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 	/*
6277c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6287c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6297c478bd9Sstevel@tonic-gate 	 */
6307c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6317c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6327c478bd9Sstevel@tonic-gate 		error++;
6337c478bd9Sstevel@tonic-gate 		goto out;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/*
6377c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6387c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6397c478bd9Sstevel@tonic-gate 	 */
6407c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6417c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6427c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6437c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6447c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6457c478bd9Sstevel@tonic-gate 		char *path;
6467c478bd9Sstevel@tonic-gate 		uint_t i;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		mnts = NULL;
6497c478bd9Sstevel@tonic-gate 		nmnt = 0;
6507c478bd9Sstevel@tonic-gate 		/*
6517c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6527c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6537c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6547c478bd9Sstevel@tonic-gate 		 * filesystems.
6557c478bd9Sstevel@tonic-gate 		 */
6567c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6577c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6587c478bd9Sstevel@tonic-gate 			error++;
6597c478bd9Sstevel@tonic-gate 			goto out;
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6627c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6637c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6647c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6657c478bd9Sstevel@tonic-gate 			/*
6667c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6677c478bd9Sstevel@tonic-gate 			 *
6687c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6697c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6707c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
6717c478bd9Sstevel@tonic-gate 			 */
6727c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6737c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6747c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 			/*
6777c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6787c478bd9Sstevel@tonic-gate 			 */
6797c478bd9Sstevel@tonic-gate 			if (stuck) {
6807c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6817c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6827c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
6837c478bd9Sstevel@tonic-gate 				} else {
6847c478bd9Sstevel@tonic-gate 					/*
6857c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
6867c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
6877c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
6887c478bd9Sstevel@tonic-gate 					 * bail.
6897c478bd9Sstevel@tonic-gate 					 */
6907c478bd9Sstevel@tonic-gate 					error++;
6917c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6927c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
6937c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
6947c478bd9Sstevel@tonic-gate 					goto out;
6957c478bd9Sstevel@tonic-gate 				}
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 			/*
6987c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
6997c478bd9Sstevel@tonic-gate 			 */
7007c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7017c478bd9Sstevel@tonic-gate 				newcount++;
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7067c478bd9Sstevel@tonic-gate 			break;
7077c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7087c478bd9Sstevel@tonic-gate 			/*
7097c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7107c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7117c478bd9Sstevel@tonic-gate 			 */
7127c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		/*
7177c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7187c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7197c478bd9Sstevel@tonic-gate 		 */
7207c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7217c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7227c478bd9Sstevel@tonic-gate 			error++;
7237c478bd9Sstevel@tonic-gate 			goto out;
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate out:
7287c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7297c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7307c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate static int
7347c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7377c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate /*
7437c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7447c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7457c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7467c478bd9Sstevel@tonic-gate  *
7477c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7487c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7497c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7507c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7517c478bd9Sstevel@tonic-gate  * remains the case.
7527c478bd9Sstevel@tonic-gate  */
7537c478bd9Sstevel@tonic-gate static int
7547c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7577c478bd9Sstevel@tonic-gate 	int child_status = 0;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7617c478bd9Sstevel@tonic-gate 	 */
7627c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7637c478bd9Sstevel@tonic-gate 	child_pid = fork();
7647c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7657c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
7667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7677c478bd9Sstevel@tonic-gate 		return (-1);
7687c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
7697c478bd9Sstevel@tonic-gate 		closefrom(0);
7701390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7711390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7721390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7731390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7747c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
7757c478bd9Sstevel@tonic-gate 		/*
7767c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7777c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7787c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7797c478bd9Sstevel@tonic-gate 		 * accordingly.
7807c478bd9Sstevel@tonic-gate 		 */
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7837c478bd9Sstevel@tonic-gate 	} else {
7847c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7887c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7897c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7907c478bd9Sstevel@tonic-gate 		return (-1);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
7937c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
7947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
7957c478bd9Sstevel@tonic-gate 		return (-1);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate static int
8017c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8047c478bd9Sstevel@tonic-gate 	char *argv[4];
8057c478bd9Sstevel@tonic-gate 	int status;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	/*
8087c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8097c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8107c478bd9Sstevel@tonic-gate 	 */
8117c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8129acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8147c478bd9Sstevel@tonic-gate 		return (-1);
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
8187c478bd9Sstevel@tonic-gate 	argv[1] = "-m";
8197c478bd9Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
8207c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8237c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8247c478bd9Sstevel@tonic-gate 		return (status);
8257c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8267c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8277c478bd9Sstevel@tonic-gate 	return (-1);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate static int
8317c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8327c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8357c478bd9Sstevel@tonic-gate 	char *argv[6];
8367c478bd9Sstevel@tonic-gate 	int status;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/*
8397c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8407c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8417c478bd9Sstevel@tonic-gate 	 */
8427c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8439acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8447c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8457c478bd9Sstevel@tonic-gate 		return (-1);
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
8487c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
8497c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
8507c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
8517c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
8527c478bd9Sstevel@tonic-gate 	} else {
8537c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
8547c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
8557c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
8567c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
8577c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8617c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8627c478bd9Sstevel@tonic-gate 		return (status);
8637c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
8647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8657c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8667c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8677c478bd9Sstevel@tonic-gate 	else
8687c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8697c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8707c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8717c478bd9Sstevel@tonic-gate 	return (-1);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate  * Make sure if a given path exists, it is not a sym-link, and is a directory.
8767c478bd9Sstevel@tonic-gate  */
8777c478bd9Sstevel@tonic-gate static int
8787c478bd9Sstevel@tonic-gate check_path(zlog_t *zlogp, const char *path)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	struct stat statbuf;
8817c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
8827c478bd9Sstevel@tonic-gate 	int res;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
8857c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
8867c478bd9Sstevel@tonic-gate 			return (0);
8877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
8887c478bd9Sstevel@tonic-gate 		return (-1);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
8917c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
8927c478bd9Sstevel@tonic-gate 		return (-1);
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(statbuf.st_mode)) {
89545916cd2Sjpk 		if (is_system_labeled() && S_ISREG(statbuf.st_mode)) {
89645916cd2Sjpk 			/*
89745916cd2Sjpk 			 * The need to mount readonly copies of
89845916cd2Sjpk 			 * global zone /etc/ files is unique to
89945916cd2Sjpk 			 * Trusted Extensions.
90045916cd2Sjpk 			 * The check for /etc/ via strstr() is to
90145916cd2Sjpk 			 * allow paths like $ZONEROOT/etc/passwd
90245916cd2Sjpk 			 */
90345916cd2Sjpk 			if (strstr(path, "/etc/") == NULL) {
90445916cd2Sjpk 				zerror(zlogp, B_FALSE,
90545916cd2Sjpk 				    "%s is not in /etc", path);
90645916cd2Sjpk 				return (-1);
90745916cd2Sjpk 			}
90845916cd2Sjpk 		} else {
9097c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s is not a directory", path);
9107c478bd9Sstevel@tonic-gate 			return (-1);
9117c478bd9Sstevel@tonic-gate 		}
91245916cd2Sjpk 	}
9137c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9157c478bd9Sstevel@tonic-gate 		return (-1);
9167c478bd9Sstevel@tonic-gate 	}
9177c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9187c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9197c478bd9Sstevel@tonic-gate 		/*
9207c478bd9Sstevel@tonic-gate 		 * We don't like ".."s and "."s throwing us off
9217c478bd9Sstevel@tonic-gate 		 */
9227c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9237c478bd9Sstevel@tonic-gate 		return (-1);
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 	return (0);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate /*
9297c478bd9Sstevel@tonic-gate  * Check every component of rootpath/relpath.  If any component fails (ie,
9307c478bd9Sstevel@tonic-gate  * exists but isn't the canonical path to a directory), it is returned in
9317c478bd9Sstevel@tonic-gate  * badpath, which is assumed to be at least of size MAXPATHLEN.
9327c478bd9Sstevel@tonic-gate  *
9337c478bd9Sstevel@tonic-gate  * Relpath must begin with '/'.
9347c478bd9Sstevel@tonic-gate  */
9357c478bd9Sstevel@tonic-gate static boolean_t
9367c478bd9Sstevel@tonic-gate valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath)
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	char abspath[MAXPATHLEN], *slashp;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	/*
9417c478bd9Sstevel@tonic-gate 	 * Make sure abspath has at least one '/' after its rootpath
9427c478bd9Sstevel@tonic-gate 	 * component, and ends with '/'.
9437c478bd9Sstevel@tonic-gate 	 */
9449acbbeafSnn35248 	if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) >=
9457c478bd9Sstevel@tonic-gate 	    sizeof (abspath)) {
9467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath,
9477c478bd9Sstevel@tonic-gate 		    relpath);
9487c478bd9Sstevel@tonic-gate 		return (B_FALSE);
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
9527c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
9537c478bd9Sstevel@tonic-gate 	do {
9547c478bd9Sstevel@tonic-gate 		*slashp = '\0';
9557c478bd9Sstevel@tonic-gate 		if (check_path(zlogp, abspath) != 0)
9567c478bd9Sstevel@tonic-gate 			return (B_FALSE);
9577c478bd9Sstevel@tonic-gate 		*slashp = '/';
9587c478bd9Sstevel@tonic-gate 		slashp++;
9597c478bd9Sstevel@tonic-gate 	} while ((slashp = strchr(slashp, '/')) != NULL);
9607c478bd9Sstevel@tonic-gate 	return (B_TRUE);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate static int
9649acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
9659acbbeafSnn35248 {
9669acbbeafSnn35248 	di_prof_t prof = arg;
9679acbbeafSnn35248 
9689acbbeafSnn35248 	if (name == NULL)
9699acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
9709acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
9719acbbeafSnn35248 }
9729acbbeafSnn35248 
9739acbbeafSnn35248 static int
9749acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
9759acbbeafSnn35248 {
9769acbbeafSnn35248 	di_prof_t prof = arg;
9779acbbeafSnn35248 
9789acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
9799acbbeafSnn35248 }
9809acbbeafSnn35248 
981f4b3ec61Sdh155122 static int
982f4b3ec61Sdh155122 get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
983f4b3ec61Sdh155122 {
984f4b3ec61Sdh155122 	zone_dochandle_t handle;
985f4b3ec61Sdh155122 
986f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
987f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
988f4b3ec61Sdh155122 		return (-1);
989f4b3ec61Sdh155122 	}
990f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
991f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
992f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
993f4b3ec61Sdh155122 		return (-1);
994f4b3ec61Sdh155122 	}
995f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
996f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
997f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
998f4b3ec61Sdh155122 		return (-1);
999f4b3ec61Sdh155122 	}
1000f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1001f4b3ec61Sdh155122 	return (0);
1002f4b3ec61Sdh155122 }
1003f4b3ec61Sdh155122 
10049acbbeafSnn35248 /*
10059acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10069acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10079acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10089acbbeafSnn35248  */
10099acbbeafSnn35248 static int
10109acbbeafSnn35248 mount_one_dev(zlog_t *zlogp, char *devpath)
10119acbbeafSnn35248 {
10129acbbeafSnn35248 	char			brand[MAXNAMELEN];
10139acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1014123807fbSedp 	brand_handle_t		bh = NULL;
10159acbbeafSnn35248 	struct zone_devtab	ztab;
10169acbbeafSnn35248 	di_prof_t		prof = NULL;
10179acbbeafSnn35248 	int			err;
10189acbbeafSnn35248 	int			retval = -1;
1019f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1020f4b3ec61Sdh155122 	const char 		*curr_iptype;
10219acbbeafSnn35248 
10229acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
10239acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10249acbbeafSnn35248 		goto cleanup;
10259acbbeafSnn35248 	}
10269acbbeafSnn35248 
10279acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
10289acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
1029123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
10309acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
10319acbbeafSnn35248 		goto cleanup;
10329acbbeafSnn35248 	}
10339acbbeafSnn35248 
1034f4b3ec61Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
1035f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1036f4b3ec61Sdh155122 		goto cleanup;
1037f4b3ec61Sdh155122 	}
1038f4b3ec61Sdh155122 	switch (iptype) {
1039f4b3ec61Sdh155122 	case ZS_SHARED:
1040f4b3ec61Sdh155122 		curr_iptype = "shared";
1041f4b3ec61Sdh155122 		break;
1042f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1043f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1044f4b3ec61Sdh155122 		break;
1045f4b3ec61Sdh155122 	}
1046f4b3ec61Sdh155122 
1047123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1048f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
10499acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
10509acbbeafSnn35248 		goto cleanup;
10519acbbeafSnn35248 	}
10529acbbeafSnn35248 
1053123807fbSedp 	if (brand_platform_iter_link(bh,
10549acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
10559acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
10569acbbeafSnn35248 		goto cleanup;
10579acbbeafSnn35248 	}
10589acbbeafSnn35248 
10599acbbeafSnn35248 	/* Add user-specified devices and directories */
10609acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
10619acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
10629acbbeafSnn35248 		goto cleanup;
10639acbbeafSnn35248 	}
10649acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
10659acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
10669acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
10679acbbeafSnn35248 		goto cleanup;
10689acbbeafSnn35248 	}
10699acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
10709acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
10719acbbeafSnn35248 		    zonecfg_strerror(err));
10729acbbeafSnn35248 		goto cleanup;
10739acbbeafSnn35248 	}
10749acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
10759acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
10769acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
10779acbbeafSnn35248 			    "user-specified device");
10789acbbeafSnn35248 			goto cleanup;
10799acbbeafSnn35248 		}
10809acbbeafSnn35248 	}
10819acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
10829acbbeafSnn35248 
10839acbbeafSnn35248 	/* Send profile to kernel */
10849acbbeafSnn35248 	if (di_prof_commit(prof)) {
10859acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
10869acbbeafSnn35248 		goto cleanup;
10879acbbeafSnn35248 	}
10889acbbeafSnn35248 
10899acbbeafSnn35248 	retval = 0;
10909acbbeafSnn35248 
10919acbbeafSnn35248 cleanup:
1092123807fbSedp 	if (bh != NULL)
1093123807fbSedp 		brand_close(bh);
10949acbbeafSnn35248 	if (handle)
10959acbbeafSnn35248 		zonecfg_fini_handle(handle);
10969acbbeafSnn35248 	if (prof)
10979acbbeafSnn35248 		di_prof_fini(prof);
10989acbbeafSnn35248 	return (retval);
10999acbbeafSnn35248 }
11009acbbeafSnn35248 
11019acbbeafSnn35248 static int
11027c478bd9Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
1105108322fbScarlsonj 	char specpath[MAXPATHLEN];
11067c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11077c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11089acbbeafSnn35248 	int rv;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) {
11117c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11127c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11137c478bd9Sstevel@tonic-gate 		return (-1);
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
11177c478bd9Sstevel@tonic-gate 	    DEFAULT_DIR_MODE) != 0)
11187c478bd9Sstevel@tonic-gate 		return (-1);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
11217c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
11247c478bd9Sstevel@tonic-gate 		/*
11257c478bd9Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1126108322fbScarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1127108322fbScarlsonj 		 * can be seen via the alternate zone's root.
11287c478bd9Sstevel@tonic-gate 		 */
1129108322fbScarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1130108322fbScarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1131108322fbScarlsonj 		    sizeof (specpath)) {
1132108322fbScarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1133108322fbScarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1134108322fbScarlsonj 			return (-1);
1135108322fbScarlsonj 		}
1136108322fbScarlsonj 		if (zonecfg_in_alt_root())
1137108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
11387c478bd9Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1139108322fbScarlsonj 		    specpath, path) != 0) {
11407c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1141108322fbScarlsonj 			    specpath);
11427c478bd9Sstevel@tonic-gate 			return (-1);
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 		return (0);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/*
11487c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
11497c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
11507c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
11517c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
11527c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
11537c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
11547c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
11557c478bd9Sstevel@tonic-gate 	 */
11567c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
11577c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
11587c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
11597c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
11607c478bd9Sstevel@tonic-gate 		return (-1);
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
1164108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1165108322fbScarlsonj 	 * read-only loopback mounts as necessary.  For all lofs mounts, make
1166108322fbScarlsonj 	 * sure that the 'special' entry points inside the alternate root.  (We
1167108322fbScarlsonj 	 * don't do this with other mounts, as devfs isn't in the alternate
1168108322fbScarlsonj 	 * root, and we need to assume the device environment is roughly the
1169108322fbScarlsonj 	 * same.)
1170108322fbScarlsonj 	 */
1171108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1172108322fbScarlsonj 		struct stat64 st;
1173108322fbScarlsonj 
1174108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
117568eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
117668eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1177108322fbScarlsonj 				return (-1);
117868eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1179108322fbScarlsonj 			if (snprintf(specpath, sizeof (specpath), "%s%s",
1180108322fbScarlsonj 			    zonecfg_get_root(), fsptr->zone_fs_special) >=
1181108322fbScarlsonj 			    sizeof (specpath)) {
1182108322fbScarlsonj 				zerror(zlogp, B_FALSE, "cannot mount %s: path "
1183108322fbScarlsonj 				    "too long in alternate root",
1184108322fbScarlsonj 				    fsptr->zone_fs_special);
1185108322fbScarlsonj 				return (-1);
1186108322fbScarlsonj 			}
1187108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
1188108322fbScarlsonj 			(void) strlcpy(fsptr->zone_fs_special, specpath,
1189108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1190108322fbScarlsonj 		}
1191108322fbScarlsonj 	}
1192108322fbScarlsonj 
1193108322fbScarlsonj 	/*
11947c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
11957c478bd9Sstevel@tonic-gate 	 */
11967c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
11977c478bd9Sstevel@tonic-gate 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0)
11987c478bd9Sstevel@tonic-gate 		return (-1);
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	/*
12017c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
12027c478bd9Sstevel@tonic-gate 	 */
12037c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
12047c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
12057c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12067c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
12077c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
12087c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
12097c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
12107c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
12117c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
12127c478bd9Sstevel@tonic-gate 		}
12137c478bd9Sstevel@tonic-gate 	}
12149acbbeafSnn35248 
12159acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
12169acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
12179acbbeafSnn35248 		return (rv);
12189acbbeafSnn35248 
12199acbbeafSnn35248 	/*
12209acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
12219acbbeafSnn35248 	 * we're done.
12229acbbeafSnn35248 	 */
12239acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
12249acbbeafSnn35248 		return (0);
12259acbbeafSnn35248 
12269acbbeafSnn35248 	/*
12279acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
12289acbbeafSnn35248 	 * need to configure it.
12299acbbeafSnn35248 	 */
12309acbbeafSnn35248 	return (mount_one_dev(zlogp, path));
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate static void
12347c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate 	uint_t i;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
12397c478bd9Sstevel@tonic-gate 		return;
12407c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
12417c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
12427c478bd9Sstevel@tonic-gate 	free(fsarray);
12437c478bd9Sstevel@tonic-gate }
12447c478bd9Sstevel@tonic-gate 
1245108322fbScarlsonj /*
1246f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1247f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1248f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1249f4368d3dSvp157776  * is done this way because:
1250f4368d3dSvp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
1251f4368d3dSvp157776  * 	We loopback mount zone's own /etc and /var into the root of the
1252f4368d3dSvp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1253f4368d3dSvp157776  * 	need to delay the mount of /var till the zone's root gets populated.
1254f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1255f4368d3dSvp157776  * 	build_mounted_post_var() which gets called only after the zone
1256f4368d3dSvp157776  * 	specific filesystems are mounted.
1257108322fbScarlsonj  */
1258108322fbScarlsonj static boolean_t
1259f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
126016bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1261108322fbScarlsonj {
1262108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1263108322fbScarlsonj 	const char **cpp;
1264108322fbScarlsonj 	static const char *mkdirs[] = {
12653f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
12663f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1267108322fbScarlsonj 	};
1268108322fbScarlsonj 	char *altstr;
1269f4368d3dSvp157776 	FILE *fp;
1270108322fbScarlsonj 	uuid_t uuid;
1271108322fbScarlsonj 
12729acbbeafSnn35248 	assert(zone_isnative);
12739acbbeafSnn35248 
1274108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
127516bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
127616bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1277108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1278108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1279108322fbScarlsonj 
1280108322fbScarlsonj 	/*
1281108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1282108322fbScarlsonj 	 * zone_mount_early.)
1283108322fbScarlsonj 	 */
1284108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1285108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1286108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1287108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1288108322fbScarlsonj 			return (B_FALSE);
1289108322fbScarlsonj 		}
1290108322fbScarlsonj 	}
1291f4368d3dSvp157776 	/*
1292f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1293f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1294f4368d3dSvp157776 	 * read-only inside the scratch zone.
1295f4368d3dSvp157776 	 */
1296f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1297f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1298f4368d3dSvp157776 	if (altstr == NULL) {
1299f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1300f4368d3dSvp157776 		return (B_FALSE);
1301f4368d3dSvp157776 	}
1302f4368d3dSvp157776 	zonecfg_set_root("");
1303f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1304f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1305f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1306f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1307f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1308f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1309f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1310f4368d3dSvp157776 			return (B_FALSE);
1311f4368d3dSvp157776 		}
1312f4368d3dSvp157776 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1313f4368d3dSvp157776 		    tmp) != 0) {
1314f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1315f4368d3dSvp157776 			    fromdir);
1316f4368d3dSvp157776 			return (B_FALSE);
1317f4368d3dSvp157776 		}
1318f4368d3dSvp157776 	}
1319f4368d3dSvp157776 	zonecfg_set_root(altstr);
1320f4368d3dSvp157776 	free(altstr);
1321f4368d3dSvp157776 
1322f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1323f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1324f4368d3dSvp157776 		return (B_FALSE);
1325f4368d3dSvp157776 	}
1326f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1327f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1328f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1329f4368d3dSvp157776 	}
1330f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1331f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1332f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1333f4368d3dSvp157776 		return (B_FALSE);
1334f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1335f4368d3dSvp157776 	return (B_TRUE);
1336f4368d3dSvp157776 }
1337f4368d3dSvp157776 
1338f4368d3dSvp157776 
1339f4368d3dSvp157776 static boolean_t
134016bca938Svp157776 build_mounted_post_var(zlog_t *zlogp, char *rootpath, const char *luroot)
1341f4368d3dSvp157776 {
1342f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1343f4368d3dSvp157776 	const char **cpp;
1344f4368d3dSvp157776 	static const char *localdirs[] = {
1345f4368d3dSvp157776 		"/etc", "/var", NULL
1346f4368d3dSvp157776 	};
1347f4368d3dSvp157776 	static const char *loopdirs[] = {
1348f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1349f4368d3dSvp157776 		"/usr", NULL
1350f4368d3dSvp157776 	};
1351f4368d3dSvp157776 	static const char *tmpdirs[] = {
1352f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1353f4368d3dSvp157776 	};
1354f4368d3dSvp157776 	struct stat st;
1355f4368d3dSvp157776 
1356108322fbScarlsonj 	/*
1357108322fbScarlsonj 	 * These are mounted read-write from the zone undergoing upgrade.  We
1358108322fbScarlsonj 	 * must be careful not to 'leak' things from the main system into the
1359108322fbScarlsonj 	 * zone, and this accomplishes that goal.
1360108322fbScarlsonj 	 */
1361108322fbScarlsonj 	for (cpp = localdirs; *cpp != NULL; cpp++) {
1362108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1363108322fbScarlsonj 		(void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath,
1364108322fbScarlsonj 		    *cpp);
1365108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1366108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1367108322fbScarlsonj 			return (B_FALSE);
1368108322fbScarlsonj 		}
1369108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) {
1370108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1371108322fbScarlsonj 			    *cpp);
1372108322fbScarlsonj 			return (B_FALSE);
1373108322fbScarlsonj 		}
1374108322fbScarlsonj 	}
1375108322fbScarlsonj 
1376108322fbScarlsonj 	/*
1377108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1378108322fbScarlsonj 	 * they contain binaries that must match system.
1379108322fbScarlsonj 	 */
1380108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1381108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1382108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1383108322fbScarlsonj 			if (errno != EEXIST) {
1384108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1385108322fbScarlsonj 				return (B_FALSE);
1386108322fbScarlsonj 			}
1387108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1388108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1389108322fbScarlsonj 				return (B_FALSE);
1390108322fbScarlsonj 			}
1391108322fbScarlsonj 			/*
1392108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1393108322fbScarlsonj 			 * things that have been converted into symlinks
1394108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1395108322fbScarlsonj 			 * fixup.
1396108322fbScarlsonj 			 */
1397108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1398108322fbScarlsonj 				continue;
1399108322fbScarlsonj 		}
1400108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1401108322fbScarlsonj 		    tmp) != 0) {
1402108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1403108322fbScarlsonj 			    *cpp);
1404108322fbScarlsonj 			return (B_FALSE);
1405108322fbScarlsonj 		}
1406108322fbScarlsonj 	}
1407108322fbScarlsonj 
1408108322fbScarlsonj 	/*
1409108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1410108322fbScarlsonj 	 */
1411108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1412108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1413108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
1414108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1415108322fbScarlsonj 			return (B_FALSE);
1416108322fbScarlsonj 		}
1417d30e996aSgjelinek 
1418d30e996aSgjelinek 		/*
1419d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1420d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1421d30e996aSgjelinek 		 * the correct mode for /tmp now.
1422d30e996aSgjelinek 		 */
1423d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1424d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1425d30e996aSgjelinek 			return (B_FALSE);
1426d30e996aSgjelinek 		}
1427d30e996aSgjelinek 
1428108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1429108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1430108322fbScarlsonj 			return (B_FALSE);
1431108322fbScarlsonj 		}
1432108322fbScarlsonj 	}
1433108322fbScarlsonj 	return (B_TRUE);
1434108322fbScarlsonj }
1435108322fbScarlsonj 
14369acbbeafSnn35248 typedef struct plat_gmount_cb_data {
14379acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
14389acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
14399acbbeafSnn35248 	int			*pgcd_num_fs;
14409acbbeafSnn35248 } plat_gmount_cb_data_t;
14419acbbeafSnn35248 
14429acbbeafSnn35248 /*
14439acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
14449acbbeafSnn35248  * through all global brand platform mounts.
14459acbbeafSnn35248  */
14469acbbeafSnn35248 int
14479acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
14489acbbeafSnn35248     const char *fstype, const char *opt)
14499acbbeafSnn35248 {
14509acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
14519acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
14529acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
14539acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
14549acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
14559acbbeafSnn35248 
14569acbbeafSnn35248 	num_fs++;
14579acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
14589acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
14599acbbeafSnn35248 		return (-1);
14609acbbeafSnn35248 	}
14619acbbeafSnn35248 
14629acbbeafSnn35248 	fs_ptr = tmp_ptr;
14639acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
14649acbbeafSnn35248 
14659acbbeafSnn35248 	/* update the callback struct passed in */
14669acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
14679acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
14689acbbeafSnn35248 
14699acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
14709acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
14719acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
14729acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
14739acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
14749acbbeafSnn35248 	fsp->zone_fs_options = NULL;
14759acbbeafSnn35248 	if (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK) {
14769acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
14779acbbeafSnn35248 		return (-1);
14789acbbeafSnn35248 	}
14799acbbeafSnn35248 
14809acbbeafSnn35248 	return (0);
14819acbbeafSnn35248 }
14829acbbeafSnn35248 
14839acbbeafSnn35248 static int
14849acbbeafSnn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
14859acbbeafSnn35248     struct zone_fstab **fs_tabp, int *num_fsp)
14869acbbeafSnn35248 {
14879acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
14889acbbeafSnn35248 	int num_fs;
14899acbbeafSnn35248 
14909acbbeafSnn35248 	num_fs = *num_fsp;
14919acbbeafSnn35248 	fs_ptr = *fs_tabp;
14929acbbeafSnn35248 
14939acbbeafSnn35248 	if (zonecfg_setipdent(handle) != Z_OK) {
14949acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
14959acbbeafSnn35248 		return (-1);
14969acbbeafSnn35248 	}
14979acbbeafSnn35248 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
14989acbbeafSnn35248 		num_fs++;
14999acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
15009acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
15019acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
15029acbbeafSnn35248 			(void) zonecfg_endipdent(handle);
15039acbbeafSnn35248 			return (-1);
15049acbbeafSnn35248 		}
15059acbbeafSnn35248 
15069acbbeafSnn35248 		/* update the pointers passed in */
15079acbbeafSnn35248 		*fs_tabp = tmp_ptr;
15089acbbeafSnn35248 		*num_fsp = num_fs;
15099acbbeafSnn35248 
15109acbbeafSnn35248 		/*
15119acbbeafSnn35248 		 * IPDs logically only have a mount point; all other properties
15129acbbeafSnn35248 		 * are implied.
15139acbbeafSnn35248 		 */
15149acbbeafSnn35248 		fs_ptr = tmp_ptr;
15159acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
15169acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
15179acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
15189acbbeafSnn35248 		fsp->zone_fs_special[0] = '\0';
15199acbbeafSnn35248 		fsp->zone_fs_raw[0] = '\0';
15209acbbeafSnn35248 		fsp->zone_fs_type[0] = '\0';
15219acbbeafSnn35248 		fsp->zone_fs_options = NULL;
15229acbbeafSnn35248 	}
15239acbbeafSnn35248 	(void) zonecfg_endipdent(handle);
15249acbbeafSnn35248 	return (0);
15259acbbeafSnn35248 }
15269acbbeafSnn35248 
15279acbbeafSnn35248 static int
15289acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
15299acbbeafSnn35248     struct zone_fstab **fs_tabp, int *num_fsp, int mount_cmd)
15309acbbeafSnn35248 {
15319acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
15329acbbeafSnn35248 	int num_fs;
15339acbbeafSnn35248 
15349acbbeafSnn35248 	num_fs = *num_fsp;
15359acbbeafSnn35248 	fs_ptr = *fs_tabp;
15369acbbeafSnn35248 
15379acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
15389acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
15399acbbeafSnn35248 		return (-1);
15409acbbeafSnn35248 	}
15419acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
15429acbbeafSnn35248 		/*
15439acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
15449acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
15459acbbeafSnn35248 		 * case.
15469acbbeafSnn35248 		 */
15479acbbeafSnn35248 		if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
15489acbbeafSnn35248 			continue;
15499acbbeafSnn35248 
15509acbbeafSnn35248 		num_fs++;
15519acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
15529acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
15539acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
15549acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
15559acbbeafSnn35248 			return (-1);
15569acbbeafSnn35248 		}
15579acbbeafSnn35248 		/* update the pointers passed in */
15589acbbeafSnn35248 		*fs_tabp = tmp_ptr;
15599acbbeafSnn35248 		*num_fsp = num_fs;
15609acbbeafSnn35248 
15619acbbeafSnn35248 		fs_ptr = tmp_ptr;
15629acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
15639acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
15649acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
15659acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special,
15669acbbeafSnn35248 		    sizeof (fsp->zone_fs_special));
15679acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
15689acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
15699acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
15709acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
15719acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
15729acbbeafSnn35248 	}
15739acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
15749acbbeafSnn35248 	return (0);
15759acbbeafSnn35248 }
15769acbbeafSnn35248 
15777c478bd9Sstevel@tonic-gate static int
1578108322fbScarlsonj mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd)
15797c478bd9Sstevel@tonic-gate {
15807c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
15817c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
15829acbbeafSnn35248 	char brand[MAXNAMELEN];
158316bca938Svp157776 	char luroot[MAXPATHLEN];
15849acbbeafSnn35248 	int i, num_fs = 0;
15859acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
15867c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
15877c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1588123807fbSedp 	brand_handle_t bh;
15899acbbeafSnn35248 	plat_gmount_cb_data_t cb;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1592108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
15937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1594108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1595108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1596108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
15977c478bd9Sstevel@tonic-gate 		goto bad;
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
16017c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
16027c478bd9Sstevel@tonic-gate 		goto bad;
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
16067c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
16077c478bd9Sstevel@tonic-gate 		goto bad;
16087c478bd9Sstevel@tonic-gate 	}
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1611ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
16127c478bd9Sstevel@tonic-gate 		goto bad;
16137c478bd9Sstevel@tonic-gate 	}
16147c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
16157c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
16167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
16177c478bd9Sstevel@tonic-gate 		goto bad;
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 
16209acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
16219acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
1622123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
16239acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
16249acbbeafSnn35248 		return (-1);
16259acbbeafSnn35248 	}
16269acbbeafSnn35248 
16279acbbeafSnn35248 	/*
16289acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
16299acbbeafSnn35248 	 * configuration.
16309acbbeafSnn35248 	 */
16319acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
16329acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
16339acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1634123807fbSedp 	if (brand_platform_iter_gmounts(bh, zonepath,
16359acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
16369acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1637123807fbSedp 		brand_close(bh);
16389acbbeafSnn35248 		return (-1);
16399acbbeafSnn35248 	}
1640123807fbSedp 	brand_close(bh);
16419acbbeafSnn35248 
16427c478bd9Sstevel@tonic-gate 	/*
16437c478bd9Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
16447c478bd9Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
16457c478bd9Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
16467c478bd9Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
16477c478bd9Sstevel@tonic-gate 	 */
16489acbbeafSnn35248 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
16497c478bd9Sstevel@tonic-gate 		goto bad;
16507c478bd9Sstevel@tonic-gate 
16519acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
16529acbbeafSnn35248 	    mount_cmd) != 0)
16537c478bd9Sstevel@tonic-gate 		goto bad;
1654fa9e4066Sahrens 
16557c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
16567c478bd9Sstevel@tonic-gate 	handle = NULL;
16577c478bd9Sstevel@tonic-gate 
1658108322fbScarlsonj 	/*
16599acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
16609acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
16619acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
16629acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
16639acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
16649acbbeafSnn35248 	 * the zones filesystems there instead.
16659acbbeafSnn35248 	 *
16669acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
16679acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
16689acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
16699acbbeafSnn35248 	 *
16709acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
16719acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
16729acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
16739acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
16749acbbeafSnn35248 	 * upgrade environment.)
16759acbbeafSnn35248 	 *
16769acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
16779acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
16789acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
16799acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
16809acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
16819acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
16829acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
16839acbbeafSnn35248 	 *
16849acbbeafSnn35248 	 * All this work is done in three phases:
1685f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1686f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1687f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1688f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1689108322fbScarlsonj 	 */
1690108322fbScarlsonj 	if (mount_cmd &&
1691f4368d3dSvp157776 	    !build_mounted_pre_var(zlogp,
169216bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1693108322fbScarlsonj 		goto bad;
1694108322fbScarlsonj 
16957c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
16969acbbeafSnn35248 
16977c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
16989acbbeafSnn35248 		if (mount_cmd &&
16999acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
17009acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
17019acbbeafSnn35248 
17029acbbeafSnn35248 			/*
17039acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
17049acbbeafSnn35248 			 * but /dev is special and always goes at the top
17059acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
17069acbbeafSnn35248 			 */
17079acbbeafSnn35248 			assert(zone_isnative);
17089acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
17099acbbeafSnn35248 			rootpath[slen] = '\0';
17109acbbeafSnn35248 			if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
17119acbbeafSnn35248 				goto bad;
17129acbbeafSnn35248 			rootpath[slen] = '/';
17139acbbeafSnn35248 			continue;
17149acbbeafSnn35248 		}
17157c478bd9Sstevel@tonic-gate 		if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
17167c478bd9Sstevel@tonic-gate 			goto bad;
17177c478bd9Sstevel@tonic-gate 	}
1718f4368d3dSvp157776 	if (mount_cmd &&
171916bca938Svp157776 	    !build_mounted_post_var(zlogp, rootpath, luroot))
1720f4368d3dSvp157776 		goto bad;
172145916cd2Sjpk 
172245916cd2Sjpk 	/*
172345916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
172445916cd2Sjpk 	 */
172548451833Scarlsonj 	if (!mount_cmd && tsol_mounts(zlogp, zone_name, rootpath) != 0)
172645916cd2Sjpk 		goto bad;
172745916cd2Sjpk 
17287c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
17327c478bd9Sstevel@tonic-gate 	 */
17337c478bd9Sstevel@tonic-gate 	return (0);
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate bad:
17367c478bd9Sstevel@tonic-gate 	if (handle != NULL)
17377c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
17387c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
17397c478bd9Sstevel@tonic-gate 	return (-1);
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
17437c478bd9Sstevel@tonic-gate static int
17447c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate 	int prefixlen;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
17497c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
17507c478bd9Sstevel@tonic-gate 		return (1);
17517c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
17527c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
17537c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
17547c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
17557c478bd9Sstevel@tonic-gate 			continue;
17567c478bd9Sstevel@tonic-gate 		}
17577c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
17587c478bd9Sstevel@tonic-gate 		prefixlen--;
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 	return (0);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate /*
17647c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
17657c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
17667c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
17677c478bd9Sstevel@tonic-gate  *
17687c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
17697c478bd9Sstevel@tonic-gate  */
17707c478bd9Sstevel@tonic-gate static int
1771f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
17727c478bd9Sstevel@tonic-gate {
17737c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
17747c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
17757c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
17767c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
17777c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
17787c478bd9Sstevel@tonic-gate 	uint_t bufsize;
17797c478bd9Sstevel@tonic-gate 	char *buf = NULL;
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
17827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
17837c478bd9Sstevel@tonic-gate 		ret_code = -1;
17847c478bd9Sstevel@tonic-gate 		goto bad;
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
17877c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
17887c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
17897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1790f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
17917c478bd9Sstevel@tonic-gate 		ret_code = -1;
17927c478bd9Sstevel@tonic-gate 		goto bad;
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
17957c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
17967c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
17977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
17987c478bd9Sstevel@tonic-gate 		ret_code = -1;
17997c478bd9Sstevel@tonic-gate 		goto bad;
18007c478bd9Sstevel@tonic-gate 	}
18017c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
18027c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
18037c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
18047c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
18057c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1806f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1807f4b3ec61Sdh155122 		    "interfaces");
18087c478bd9Sstevel@tonic-gate 		ret_code = -1;
18097c478bd9Sstevel@tonic-gate 		goto bad;
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
18127c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
18137c478bd9Sstevel@tonic-gate 		(void) close(s);
18147c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
18157c478bd9Sstevel@tonic-gate 		    0) {
18167c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
18177c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
18187c478bd9Sstevel@tonic-gate 			ret_code = -1;
18197c478bd9Sstevel@tonic-gate 			continue;
18207c478bd9Sstevel@tonic-gate 		}
18217c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
18227c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
18237c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
18247c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1825c1c0ebd5Ssl108498 			if (errno == ENXIO)
1826c1c0ebd5Ssl108498 				/*
1827c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1828c1c0ebd5Ssl108498 				 * another zone halting.
1829c1c0ebd5Ssl108498 				 */
1830c1c0ebd5Ssl108498 				continue;
18317c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1832c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1833f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
18347c478bd9Sstevel@tonic-gate 			ret_code = -1;
18357c478bd9Sstevel@tonic-gate 			continue;
18367c478bd9Sstevel@tonic-gate 		}
18377c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
18387c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
18397c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1840f4b3ec61Sdh155122 				    "%s: could not remove network interface",
18417c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
18427c478bd9Sstevel@tonic-gate 				ret_code = -1;
18437c478bd9Sstevel@tonic-gate 				continue;
18447c478bd9Sstevel@tonic-gate 			}
18457c478bd9Sstevel@tonic-gate 		}
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate bad:
18487c478bd9Sstevel@tonic-gate 	if (s > 0)
18497c478bd9Sstevel@tonic-gate 		(void) close(s);
18507c478bd9Sstevel@tonic-gate 	if (buf)
18517c478bd9Sstevel@tonic-gate 		free(buf);
18527c478bd9Sstevel@tonic-gate 	return (ret_code);
18537c478bd9Sstevel@tonic-gate }
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate static union	sockunion {
18567c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
18577c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
18587c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
18597c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
18607c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate static struct {
18637c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
18647c478bd9Sstevel@tonic-gate 	char	space[512];
18657c478bd9Sstevel@tonic-gate } rtmsg;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate static int
18687c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
18697c478bd9Sstevel@tonic-gate {
18707c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
18717c478bd9Sstevel@tonic-gate 	case AF_INET:
18727c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
18737c478bd9Sstevel@tonic-gate 	case AF_LINK:
18747c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
18757c478bd9Sstevel@tonic-gate 	case AF_INET6:
18767c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
18777c478bd9Sstevel@tonic-gate 	default:
18787c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate }
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
18837c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate /*
18867c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
18877c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
18887c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
18897c478bd9Sstevel@tonic-gate  *
18907c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
18917c478bd9Sstevel@tonic-gate  *
18927c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
18937c478bd9Sstevel@tonic-gate  *
18947c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
18957c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
18967c478bd9Sstevel@tonic-gate  */
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate static char *
18997c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
19007c478bd9Sstevel@tonic-gate {
19017c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
19027c478bd9Sstevel@tonic-gate 	pid_t pid;
19037c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
19047c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
19057c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
19067c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
19077c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	pid = getpid();
19127c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
19137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
19147c478bd9Sstevel@tonic-gate 		return (NULL);
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
19187c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
19217c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
19227c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
19237c478bd9Sstevel@tonic-gate 	} else {
19247c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
19277c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
19287c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
19297c478bd9Sstevel@tonic-gate 	}
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
19347c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
19357c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
19367c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
19377c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
19387c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
19417c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
19427c478bd9Sstevel@tonic-gate 	cp += l;
19437c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
19447c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
19457c478bd9Sstevel@tonic-gate 	cp += l;
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
19507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
19517c478bd9Sstevel@tonic-gate 		return (NULL);
19527c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
19537c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
19547c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
19557c478bd9Sstevel@tonic-gate 		return (NULL);
19567c478bd9Sstevel@tonic-gate 	}
19577c478bd9Sstevel@tonic-gate 	do {
19587c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
19597c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
19607c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
19617c478bd9Sstevel@tonic-gate 	if (l < 0) {
19627c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
19637c478bd9Sstevel@tonic-gate 		return (NULL);
19647c478bd9Sstevel@tonic-gate 	}
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
19677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
19687c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
19697c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
19707c478bd9Sstevel@tonic-gate 		return (NULL);
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
19737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
19747c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
19757c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
19767c478bd9Sstevel@tonic-gate 		return (NULL);
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
19797c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
19807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
19817c478bd9Sstevel@tonic-gate 		return (NULL);
19827c478bd9Sstevel@tonic-gate 	}
19837c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
1984f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
19857c478bd9Sstevel@tonic-gate 		return (NULL);
19867c478bd9Sstevel@tonic-gate 	}
19877c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
19887c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
19897c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
19907c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
19917c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
19927c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
19937c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
19947c478bd9Sstevel@tonic-gate 			continue;
19957c478bd9Sstevel@tonic-gate 		}
19967c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
19977c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
19987c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
19997c478bd9Sstevel@tonic-gate 		break;
20007c478bd9Sstevel@tonic-gate 	}
20017c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2002f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2003f4b3ec61Sdh155122 		    "determined");
20047c478bd9Sstevel@tonic-gate 		return (NULL);
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	/*
20087c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
20097c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
20107c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
20117c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
20127c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
20137c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
20147c478bd9Sstevel@tonic-gate 	 */
20157c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
20167c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
20177c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
20187c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
20197c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
20207c478bd9Sstevel@tonic-gate 	if (i < 0) {
20217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2022f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2023f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
20247c478bd9Sstevel@tonic-gate 		return (NULL);
20257c478bd9Sstevel@tonic-gate 	}
20267c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
20277c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
20287c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
20317c478bd9Sstevel@tonic-gate 		return (answer);
20327c478bd9Sstevel@tonic-gate 	return (NULL);
20337c478bd9Sstevel@tonic-gate }
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate typedef struct mcast_rtmsg_s {
20367c478bd9Sstevel@tonic-gate 	struct rt_msghdr	m_rtm;
20377c478bd9Sstevel@tonic-gate 	union {
20387c478bd9Sstevel@tonic-gate 		struct {
20397c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_dst;
20407c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_gw;
20417c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_netmask;
20427c478bd9Sstevel@tonic-gate 		} m_v4;
20437c478bd9Sstevel@tonic-gate 		struct {
20447c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_dst;
20457c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_gw;
20467c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_netmask;
20477c478bd9Sstevel@tonic-gate 		} m_v6;
20487c478bd9Sstevel@tonic-gate 	} m_u;
20497c478bd9Sstevel@tonic-gate } mcast_rtmsg_t;
20507c478bd9Sstevel@tonic-gate #define	m_dst4		m_u.m_v4.m_dst
20517c478bd9Sstevel@tonic-gate #define	m_dst6		m_u.m_v6.m_dst
20527c478bd9Sstevel@tonic-gate #define	m_gw4		m_u.m_v4.m_gw
20537c478bd9Sstevel@tonic-gate #define	m_gw6		m_u.m_v6.m_gw
20547c478bd9Sstevel@tonic-gate #define	m_netmask4	m_u.m_v4.m_netmask
20557c478bd9Sstevel@tonic-gate #define	m_netmask6	m_u.m_v6.m_netmask
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate /*
20587c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
20597c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
20607c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
20617c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
20627c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
20637c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
20647c478bd9Sstevel@tonic-gate  *
20657c478bd9Sstevel@tonic-gate  * A default interface route for multicast is created on the first IPv4 and
20667c478bd9Sstevel@tonic-gate  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
20677c478bd9Sstevel@tonic-gate  * This should really be done in the init scripts if we ever allow zones to
20687c478bd9Sstevel@tonic-gate  * modify the routing tables.
20697c478bd9Sstevel@tonic-gate  *
20707c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
20717c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
20727c478bd9Sstevel@tonic-gate  */
20737c478bd9Sstevel@tonic-gate static int
20747c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
20757c478bd9Sstevel@tonic-gate     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
20767c478bd9Sstevel@tonic-gate     boolean_t *mcast_rt_v6_setp)
20777c478bd9Sstevel@tonic-gate {
20787c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
20797c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
20807c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
20817c478bd9Sstevel@tonic-gate 	struct in_addr in4;
20827c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
20837c478bd9Sstevel@tonic-gate 	sa_family_t af;
20847c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
20857c478bd9Sstevel@tonic-gate 	mcast_rtmsg_t mcast_rtmsg;
20867c478bd9Sstevel@tonic-gate 	int s;
20877c478bd9Sstevel@tonic-gate 	int rs;
20887c478bd9Sstevel@tonic-gate 	int rlen;
20897c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
20907c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
20917c478bd9Sstevel@tonic-gate 	int res;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
20947c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
20957c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
20967c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
20977c478bd9Sstevel@tonic-gate 		return (-1);
20987c478bd9Sstevel@tonic-gate 	}
20997c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
21007c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21017c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21027c478bd9Sstevel@tonic-gate 	else
21037c478bd9Sstevel@tonic-gate 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21067c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
21077c478bd9Sstevel@tonic-gate 		return (-1);
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
21117c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
21127c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
211322321485Svp157776 		/*
211422321485Svp157776 		 * Here, we know that the interface can't be brought up.
211522321485Svp157776 		 * A similar warning message was already printed out to
211622321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
211722321485Svp157776 		 * message to syslog and continue.
211822321485Svp157776 		 */
2119f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
212022321485Svp157776 		    "'%s' which may not be present/plumbed in the "
212122321485Svp157776 		    "global zone.", lifr.lifr_name);
21227c478bd9Sstevel@tonic-gate 		(void) close(s);
212322321485Svp157776 		return (Z_OK);
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
21277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
21287c478bd9Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
21297c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
21307c478bd9Sstevel@tonic-gate 		goto bad;
21317c478bd9Sstevel@tonic-gate 	}
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
21347c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21357c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
21387c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2139f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2140f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
21417c478bd9Sstevel@tonic-gate 		goto bad;
21427c478bd9Sstevel@tonic-gate 	}
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
21457c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
21467c478bd9Sstevel@tonic-gate 	} else {
21477c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
21487c478bd9Sstevel@tonic-gate 			/*
21497c478bd9Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
21507c478bd9Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
21517c478bd9Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
21527c478bd9Sstevel@tonic-gate 			 * being able to determine it is a common failure,
21537c478bd9Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
21547c478bd9Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
21557c478bd9Sstevel@tonic-gate 			 * printed after the rest of the interface's
21567c478bd9Sstevel@tonic-gate 			 * parameters have been configured.
21577c478bd9Sstevel@tonic-gate 			 */
21587c478bd9Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
21597c478bd9Sstevel@tonic-gate 			if (slashp != NULL) {
21607c478bd9Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
21617c478bd9Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
21627c478bd9Sstevel@tonic-gate 					*slashp = '/';
21637c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
21647c478bd9Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
21657c478bd9Sstevel@tonic-gate 					    lifr.lifr_name,
21667c478bd9Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
21677c478bd9Sstevel@tonic-gate 					goto bad;
21687c478bd9Sstevel@tonic-gate 				}
21697c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
21707c478bd9Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
21717c478bd9Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
21727c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
21737c478bd9Sstevel@tonic-gate 			}
21747c478bd9Sstevel@tonic-gate 			if (got_netmask) {
21757c478bd9Sstevel@tonic-gate 				netmask4.sin_family = af;
21767c478bd9Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
21777c478bd9Sstevel@tonic-gate 				    sizeof (netmask4));
21787c478bd9Sstevel@tonic-gate 			}
21797c478bd9Sstevel@tonic-gate 		} else {
21807c478bd9Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
21817c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
21827c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
21837c478bd9Sstevel@tonic-gate 				*slashp = '/';
21847c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
21857c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
21867c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
21877c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
21887c478bd9Sstevel@tonic-gate 				goto bad;
21897c478bd9Sstevel@tonic-gate 			}
21907c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
21917c478bd9Sstevel@tonic-gate 			netmask6.sin6_family = af;
21927c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
21937c478bd9Sstevel@tonic-gate 			    sizeof (netmask6));
21947c478bd9Sstevel@tonic-gate 		}
21957c478bd9Sstevel@tonic-gate 		if (got_netmask &&
21967c478bd9Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
21977c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
21987c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
21997c478bd9Sstevel@tonic-gate 			goto bad;
22007c478bd9Sstevel@tonic-gate 		}
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 		/*
22037c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
22047c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
22057c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
22067c478bd9Sstevel@tonic-gate 		 */
22077c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
22087c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
22097c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
22107c478bd9Sstevel@tonic-gate 			goto bad;
22117c478bd9Sstevel@tonic-gate 		}
22127c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22137c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
22147c478bd9Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
22157c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
22167c478bd9Sstevel@tonic-gate 			goto bad;
22177c478bd9Sstevel@tonic-gate 		}
22187c478bd9Sstevel@tonic-gate 	}
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
22227c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22237c478bd9Sstevel@tonic-gate 		goto bad;
22247c478bd9Sstevel@tonic-gate 	}
22257c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
22267c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
22277c478bd9Sstevel@tonic-gate 		int save_errno = errno;
22287c478bd9Sstevel@tonic-gate 		char *zone_using;
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 		/*
22317c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
22327c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
22337c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
22347c478bd9Sstevel@tonic-gate 		 * using that address.
22357c478bd9Sstevel@tonic-gate 		 */
22367c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
22377c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2238f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2239f4b3ec61Sdh155122 			    lifr.lifr_name);
22407c478bd9Sstevel@tonic-gate 			goto bad;
22417c478bd9Sstevel@tonic-gate 		}
22427c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
22437c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
22447c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
22457c478bd9Sstevel@tonic-gate 			goto bad;
22467c478bd9Sstevel@tonic-gate 		}
22477c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
22487c478bd9Sstevel@tonic-gate 		errno = save_errno;
22497c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
22507c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2251f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2252f4b3ec61Sdh155122 			    lifr.lifr_name);
22537c478bd9Sstevel@tonic-gate 		else
2254f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2255f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2256f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
22577c478bd9Sstevel@tonic-gate 		goto bad;
22587c478bd9Sstevel@tonic-gate 	}
22597c478bd9Sstevel@tonic-gate 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
22607c478bd9Sstevel@tonic-gate 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
22617c478bd9Sstevel@tonic-gate 	    (af == AF_INET6 &&
22627c478bd9Sstevel@tonic-gate 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
22637c478bd9Sstevel@tonic-gate 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
22647c478bd9Sstevel@tonic-gate 		if (rs < 0) {
22657c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not create "
22667c478bd9Sstevel@tonic-gate 			    "routing socket", lifr.lifr_name);
22677c478bd9Sstevel@tonic-gate 			goto bad;
22687c478bd9Sstevel@tonic-gate 		}
22697c478bd9Sstevel@tonic-gate 		(void) shutdown(rs, 0);
22707c478bd9Sstevel@tonic-gate 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
22717c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
22727c478bd9Sstevel@tonic-gate 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
22737c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
22747c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
22757c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
22767c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
22777c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_addrs =
22787c478bd9Sstevel@tonic-gate 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
22797c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
22807c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
22817c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
22827c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
22837c478bd9Sstevel@tonic-gate 			    htonl(INADDR_UNSPEC_GROUP);
22847c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
22857c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_addr = in4;
22867c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
22877c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
22887c478bd9Sstevel@tonic-gate 			    htonl(IN_CLASSD_NET);
22897c478bd9Sstevel@tonic-gate 		} else {
22907c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
22917c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
22927c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
22937c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_addr = in6;
22947c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
22957c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
22967c478bd9Sstevel@tonic-gate 		}
22977c478bd9Sstevel@tonic-gate 		rlen = write(rs, (char *)&mcast_rtmsg,
22987c478bd9Sstevel@tonic-gate 		    mcast_rtmsg.m_rtm.rtm_msglen);
229922321485Svp157776 		/*
230022321485Svp157776 		 * The write to the multicast socket will fail if the
230122321485Svp157776 		 * interface belongs to a failed IPMP group. This is a
230222321485Svp157776 		 * non-fatal error and the zone will continue booting.
230322321485Svp157776 		 * While the zone is running, if any interface in the
230422321485Svp157776 		 * failed IPMP group recovers, the zone will fallback to
230522321485Svp157776 		 * using that interface.
230622321485Svp157776 		 */
23077c478bd9Sstevel@tonic-gate 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
23087c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
2309f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "WARNING: network "
2310f4b3ec61Sdh155122 				    "interface '%s' not available as default "
2311f4b3ec61Sdh155122 				    "for multicast.", lifr.lifr_name);
23127c478bd9Sstevel@tonic-gate 			} else {
2313f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "WARNING: network "
2314f4b3ec61Sdh155122 				    "interface '%s' not available as default "
2315f4b3ec61Sdh155122 				    "for multicast; routing socket returned "
231622321485Svp157776 				    "unexpected %d bytes.",
231722321485Svp157776 				    lifr.lifr_name, rlen);
23187c478bd9Sstevel@tonic-gate 			}
231922321485Svp157776 		} else {
232022321485Svp157776 
23217c478bd9Sstevel@tonic-gate 			if (af == AF_INET) {
23227c478bd9Sstevel@tonic-gate 				*mcast_rt_v4_setp = B_TRUE;
23237c478bd9Sstevel@tonic-gate 			} else {
23247c478bd9Sstevel@tonic-gate 				*mcast_rt_v6_setp = B_TRUE;
23257c478bd9Sstevel@tonic-gate 			}
232622321485Svp157776 		}
23277c478bd9Sstevel@tonic-gate 		(void) close(rs);
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	if (!got_netmask) {
23317c478bd9Sstevel@tonic-gate 		/*
23327c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23337c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23347c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23357c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23367c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23377c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23387c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23397c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23407c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23417c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
23427c478bd9Sstevel@tonic-gate 		 */
23437c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23447c478bd9Sstevel@tonic-gate 		void  *addr;
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
23477c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23487c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23497c478bd9Sstevel@tonic-gate 		else
23507c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
23517c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
23547c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
23557c478bd9Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
23567c478bd9Sstevel@tonic-gate 			goto bad;
23577c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
23587c478bd9Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
23597c478bd9Sstevel@tonic-gate 		    "%s; using default of %s.",
23607c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
23617c478bd9Sstevel@tonic-gate 	}
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	(void) close(s);
23647c478bd9Sstevel@tonic-gate 	return (Z_OK);
23657c478bd9Sstevel@tonic-gate bad:
23667c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
23677c478bd9Sstevel@tonic-gate 	(void) close(s);
23687c478bd9Sstevel@tonic-gate 	return (-1);
23697c478bd9Sstevel@tonic-gate }
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate /*
23727c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
23737c478bd9Sstevel@tonic-gate  * An IPv4 loopback interface is set up "for free", modeling the global system.
23747c478bd9Sstevel@tonic-gate  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
23757c478bd9Sstevel@tonic-gate  * address is set up as well.
23767c478bd9Sstevel@tonic-gate  *
23777c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
23787c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
23797c478bd9Sstevel@tonic-gate  */
23807c478bd9Sstevel@tonic-gate static int
2381f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
23847c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
23857c478bd9Sstevel@tonic-gate 	boolean_t saw_v6 = B_FALSE;
23867c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v4_set = B_FALSE;
23877c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v6_set = B_FALSE;
23887c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
23917c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
23927c478bd9Sstevel@tonic-gate 		return (-1);
23937c478bd9Sstevel@tonic-gate 	}
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
23967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
23977c478bd9Sstevel@tonic-gate 		return (-1);
23987c478bd9Sstevel@tonic-gate 	}
23997c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24017c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24027c478bd9Sstevel@tonic-gate 		return (-1);
24037c478bd9Sstevel@tonic-gate 	}
24047c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24057c478bd9Sstevel@tonic-gate 		for (;;) {
24067c478bd9Sstevel@tonic-gate 			struct in6_addr in6;
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24097c478bd9Sstevel@tonic-gate 				break;
24107c478bd9Sstevel@tonic-gate 			if (configure_one_interface(zlogp, zoneid,
24117c478bd9Sstevel@tonic-gate 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
24127c478bd9Sstevel@tonic-gate 			    Z_OK) {
24137c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24147c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24157c478bd9Sstevel@tonic-gate 				return (-1);
24167c478bd9Sstevel@tonic-gate 			}
24177c478bd9Sstevel@tonic-gate 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
24187c478bd9Sstevel@tonic-gate 			    &in6) == 1)
24197c478bd9Sstevel@tonic-gate 				saw_v6 = B_TRUE;
24207c478bd9Sstevel@tonic-gate 		}
24217c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24227c478bd9Sstevel@tonic-gate 	}
24237c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
24247c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
24257c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
24267c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
24277c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
24287c478bd9Sstevel@tonic-gate 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
24297c478bd9Sstevel@tonic-gate 	    != Z_OK) {
24307c478bd9Sstevel@tonic-gate 		return (-1);
24317c478bd9Sstevel@tonic-gate 	}
24327c478bd9Sstevel@tonic-gate 	if (saw_v6) {
24337c478bd9Sstevel@tonic-gate 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
24347c478bd9Sstevel@tonic-gate 		    sizeof (loopback_iftab.zone_nwif_address));
24357c478bd9Sstevel@tonic-gate 		if (configure_one_interface(zlogp, zoneid,
24367c478bd9Sstevel@tonic-gate 		    &loopback_iftab, NULL, NULL) != Z_OK) {
24377c478bd9Sstevel@tonic-gate 			return (-1);
24387c478bd9Sstevel@tonic-gate 		}
24397c478bd9Sstevel@tonic-gate 	}
24407c478bd9Sstevel@tonic-gate 	return (0);
24417c478bd9Sstevel@tonic-gate }
24427c478bd9Sstevel@tonic-gate 
2443f4b3ec61Sdh155122 static void
2444f4b3ec61Sdh155122 show_owner(zlog_t *zlogp, char *dlname)
2445f4b3ec61Sdh155122 {
2446f4b3ec61Sdh155122 	zoneid_t dl_owner_zid;
2447f4b3ec61Sdh155122 	char dl_owner_zname[ZONENAME_MAX];
2448f4b3ec61Sdh155122 
2449f4b3ec61Sdh155122 	dl_owner_zid = ALL_ZONES;
2450f4b3ec61Sdh155122 	if (zone_check_datalink(&dl_owner_zid, dlname) != 0)
2451f4b3ec61Sdh155122 		(void) snprintf(dl_owner_zname, ZONENAME_MAX, "<unknown>");
2452f4b3ec61Sdh155122 	else if (getzonenamebyid(dl_owner_zid, dl_owner_zname, ZONENAME_MAX)
2453f4b3ec61Sdh155122 	    < 0)
2454f4b3ec61Sdh155122 		(void) snprintf(dl_owner_zname, ZONENAME_MAX, "<%d>",
2455f4b3ec61Sdh155122 		    dl_owner_zid);
2456f4b3ec61Sdh155122 
2457f4b3ec61Sdh155122 	errno = EPERM;
2458f4b3ec61Sdh155122 	zerror(zlogp, B_TRUE, "WARNING: skipping network interface '%s' "
2459f4b3ec61Sdh155122 	    "which is used by the non-global zone '%s'.\n",
2460f4b3ec61Sdh155122 	    dlname, dl_owner_zname);
2461f4b3ec61Sdh155122 }
2462f4b3ec61Sdh155122 
2463f4b3ec61Sdh155122 static int
2464f4b3ec61Sdh155122 add_datalink(zlog_t *zlogp, zoneid_t zoneid, char *dlname)
2465f4b3ec61Sdh155122 {
2466f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2467f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2468f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
2469f4b3ec61Sdh155122 		errno = EPERM;
2470f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "WARNING: skipping network interface "
2471f4b3ec61Sdh155122 		    "'%s' which is used in the global zone.", dlname);
2472f4b3ec61Sdh155122 		return (-1);
2473f4b3ec61Sdh155122 	}
2474f4b3ec61Sdh155122 
2475f4b3ec61Sdh155122 	/* Add access control information */
2476f4b3ec61Sdh155122 	if (zone_add_datalink(zoneid, dlname) != 0) {
2477f4b3ec61Sdh155122 		/* If someone got this link before us, show its name */
2478f4b3ec61Sdh155122 		if (errno == EPERM)
2479f4b3ec61Sdh155122 			show_owner(zlogp, dlname);
2480f4b3ec61Sdh155122 		else
2481f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "WARNING: unable to add network "
2482f4b3ec61Sdh155122 			    "interface '%s'.", dlname);
2483f4b3ec61Sdh155122 		return (-1);
2484f4b3ec61Sdh155122 	}
2485f4b3ec61Sdh155122 
2486f4b3ec61Sdh155122 	/* Hold the link for this zone */
2487f4b3ec61Sdh155122 	if (dladm_hold_link(dlname, zoneid, B_FALSE) < 0) {
2488f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "WARNING: unable to hold network "
2489f4b3ec61Sdh155122 		    "interface '%s'.", dlname);
2490f4b3ec61Sdh155122 		(void) zone_remove_datalink(zoneid, dlname);
2491f4b3ec61Sdh155122 		return (-1);
2492f4b3ec61Sdh155122 	}
2493f4b3ec61Sdh155122 
2494f4b3ec61Sdh155122 	return (0);
2495f4b3ec61Sdh155122 }
2496f4b3ec61Sdh155122 
2497f4b3ec61Sdh155122 static int
2498f4b3ec61Sdh155122 remove_datalink(zlog_t *zlogp, zoneid_t zoneid, char *dlname)
2499f4b3ec61Sdh155122 {
2500f4b3ec61Sdh155122 	/*
2501f4b3ec61Sdh155122 	 * Remove access control information.
2502f4b3ec61Sdh155122 	 * If the errno is ENXIO, the interface is not added yet,
2503f4b3ec61Sdh155122 	 * nothing to report then.
2504f4b3ec61Sdh155122 	 */
2505f4b3ec61Sdh155122 	if (zone_remove_datalink(zoneid, dlname) != 0) {
2506f4b3ec61Sdh155122 		if (errno == ENXIO)
2507f4b3ec61Sdh155122 			return (0);
2508f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to remove network interface '%s'",
2509f4b3ec61Sdh155122 		    dlname);
2510f4b3ec61Sdh155122 		return (-1);
2511f4b3ec61Sdh155122 	}
2512f4b3ec61Sdh155122 
2513f4b3ec61Sdh155122 	if (dladm_rele_link(dlname, 0, B_FALSE) < 0) {
2514f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to release network "
2515f4b3ec61Sdh155122 		    "interface '%s'", dlname);
2516f4b3ec61Sdh155122 		return (-1);
2517f4b3ec61Sdh155122 	}
2518f4b3ec61Sdh155122 	return (0);
2519f4b3ec61Sdh155122 }
2520f4b3ec61Sdh155122 
2521f4b3ec61Sdh155122 /*
2522f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2523f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2524f4b3ec61Sdh155122  * whatever we set up, and return an error.
2525f4b3ec61Sdh155122  */
2526f4b3ec61Sdh155122 static int
2527f4b3ec61Sdh155122 configure_exclusive_network_interfaces(zlog_t *zlogp)
2528f4b3ec61Sdh155122 {
2529f4b3ec61Sdh155122 	zone_dochandle_t handle;
2530f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2531f4b3ec61Sdh155122 	zoneid_t zoneid;
2532f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2533f4b3ec61Sdh155122 	char path[MAXPATHLEN];
2534f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2535f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2536f4b3ec61Sdh155122 
2537f4b3ec61Sdh155122 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
2538f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to get zoneid");
2539f4b3ec61Sdh155122 		return (-1);
2540f4b3ec61Sdh155122 	}
2541f4b3ec61Sdh155122 
2542f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2543f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2544f4b3ec61Sdh155122 		return (-1);
2545f4b3ec61Sdh155122 	}
2546f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2547f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2548f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2549f4b3ec61Sdh155122 		return (-1);
2550f4b3ec61Sdh155122 	}
2551f4b3ec61Sdh155122 
2552f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2553f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2554f4b3ec61Sdh155122 		return (0);
2555f4b3ec61Sdh155122 	}
2556f4b3ec61Sdh155122 
2557f4b3ec61Sdh155122 	for (;;) {
2558f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2559f4b3ec61Sdh155122 			break;
2560f4b3ec61Sdh155122 
2561f4b3ec61Sdh155122 		if (prof == NULL) {
2562f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2563f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2564f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2565f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2566f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2567f4b3ec61Sdh155122 				    "unable to determine dev root");
2568f4b3ec61Sdh155122 				return (-1);
2569f4b3ec61Sdh155122 			}
2570f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2571f4b3ec61Sdh155122 			    "/dev");
2572f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2573f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2574f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2575f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2576f4b3ec61Sdh155122 				    "failed to initialize profile");
2577f4b3ec61Sdh155122 				return (-1);
2578f4b3ec61Sdh155122 			}
2579f4b3ec61Sdh155122 		}
2580f4b3ec61Sdh155122 
2581f4b3ec61Sdh155122 		/*
2582f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2583f4b3ec61Sdh155122 		 * Note here the zone still boots when the interfaces
2584f4b3ec61Sdh155122 		 * assigned is inaccessible, used by others, etc.
2585f4b3ec61Sdh155122 		 */
2586f4b3ec61Sdh155122 		if (add_datalink(zlogp, zoneid, nwiftab.zone_nwif_physical)
2587f4b3ec61Sdh155122 		    == 0) {
2588f4b3ec61Sdh155122 			if (di_prof_add_dev(prof, nwiftab.zone_nwif_physical)
2589f4b3ec61Sdh155122 			    != 0) {
2590f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2591f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2592f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2593f4b3ec61Sdh155122 				    "failed to add network device");
2594f4b3ec61Sdh155122 				return (-1);
2595f4b3ec61Sdh155122 			}
2596f4b3ec61Sdh155122 			added = B_TRUE;
2597f4b3ec61Sdh155122 		}
2598f4b3ec61Sdh155122 	}
2599f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2600f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2601f4b3ec61Sdh155122 
2602f4b3ec61Sdh155122 	if (prof != NULL && added) {
2603f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2604f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
2605f4b3ec61Sdh155122 			return (-1);
2606f4b3ec61Sdh155122 		}
2607f4b3ec61Sdh155122 	}
2608f4b3ec61Sdh155122 	if (prof != NULL)
2609f4b3ec61Sdh155122 		di_prof_fini(prof);
2610f4b3ec61Sdh155122 
2611f4b3ec61Sdh155122 	return (0);
2612f4b3ec61Sdh155122 }
2613f4b3ec61Sdh155122 
2614f4b3ec61Sdh155122 /*
2615f4b3ec61Sdh155122  * Get the list of the data-links from kernel, and try to remove it
2616f4b3ec61Sdh155122  */
2617f4b3ec61Sdh155122 static int
2618f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces_run(zlog_t *zlogp, zoneid_t zoneid)
2619f4b3ec61Sdh155122 {
2620f4b3ec61Sdh155122 	char *dlnames, *ptr;
2621f4b3ec61Sdh155122 	int dlnum, dlnum_saved, i;
2622f4b3ec61Sdh155122 
2623f4b3ec61Sdh155122 	dlnum = 0;
2624f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2625f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2626f4b3ec61Sdh155122 		return (-1);
2627f4b3ec61Sdh155122 	}
2628f4b3ec61Sdh155122 again:
2629f4b3ec61Sdh155122 	/* this zone doesn't have any data-links */
2630f4b3ec61Sdh155122 	if (dlnum == 0)
2631f4b3ec61Sdh155122 		return (0);
2632f4b3ec61Sdh155122 
2633f4b3ec61Sdh155122 	dlnames = malloc(dlnum * LIFNAMSIZ);
2634f4b3ec61Sdh155122 	if (dlnames == NULL) {
2635f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "memory allocation failed");
2636f4b3ec61Sdh155122 		return (-1);
2637f4b3ec61Sdh155122 	}
2638f4b3ec61Sdh155122 	dlnum_saved = dlnum;
2639f4b3ec61Sdh155122 
2640f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, dlnames) != 0) {
2641f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2642f4b3ec61Sdh155122 		free(dlnames);
2643f4b3ec61Sdh155122 		return (-1);
2644f4b3ec61Sdh155122 	}
2645f4b3ec61Sdh155122 	if (dlnum_saved < dlnum) {
2646f4b3ec61Sdh155122 		/* list increased, try again */
2647f4b3ec61Sdh155122 		free(dlnames);
2648f4b3ec61Sdh155122 		goto again;
2649f4b3ec61Sdh155122 	}
2650f4b3ec61Sdh155122 	ptr = dlnames;
2651f4b3ec61Sdh155122 	for (i = 0; i < dlnum; i++) {
2652f4b3ec61Sdh155122 		/* Remove access control information */
2653f4b3ec61Sdh155122 		if (remove_datalink(zlogp, zoneid, ptr) != 0) {
2654f4b3ec61Sdh155122 			free(dlnames);
2655f4b3ec61Sdh155122 			return (-1);
2656f4b3ec61Sdh155122 		}
2657f4b3ec61Sdh155122 		ptr += LIFNAMSIZ;
2658f4b3ec61Sdh155122 	}
2659f4b3ec61Sdh155122 	free(dlnames);
2660f4b3ec61Sdh155122 	return (0);
2661f4b3ec61Sdh155122 }
2662f4b3ec61Sdh155122 
2663f4b3ec61Sdh155122 /*
2664f4b3ec61Sdh155122  * Get the list of the data-links from configuration, and try to remove it
2665f4b3ec61Sdh155122  */
2666f4b3ec61Sdh155122 static int
2667f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces_static(zlog_t *zlogp, zoneid_t zoneid)
2668f4b3ec61Sdh155122 {
2669f4b3ec61Sdh155122 	zone_dochandle_t handle;
2670f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2671f4b3ec61Sdh155122 
2672f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2673f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2674f4b3ec61Sdh155122 		return (-1);
2675f4b3ec61Sdh155122 	}
2676f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2677f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2678f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2679f4b3ec61Sdh155122 		return (-1);
2680f4b3ec61Sdh155122 	}
2681f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2682f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2683f4b3ec61Sdh155122 		return (0);
2684f4b3ec61Sdh155122 	}
2685f4b3ec61Sdh155122 	for (;;) {
2686f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2687f4b3ec61Sdh155122 			break;
2688f4b3ec61Sdh155122 		/* Remove access control information */
2689f4b3ec61Sdh155122 		if (remove_datalink(zlogp, zoneid, nwiftab.zone_nwif_physical)
2690f4b3ec61Sdh155122 		    != 0) {
2691f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2692f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
2693f4b3ec61Sdh155122 			return (-1);
2694f4b3ec61Sdh155122 		}
2695f4b3ec61Sdh155122 	}
2696f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2697f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2698f4b3ec61Sdh155122 	return (0);
2699f4b3ec61Sdh155122 }
2700f4b3ec61Sdh155122 
2701f4b3ec61Sdh155122 /*
2702f4b3ec61Sdh155122  * Remove the access control information from the kernel for the exclusive
2703f4b3ec61Sdh155122  * network interfaces.
2704f4b3ec61Sdh155122  */
2705f4b3ec61Sdh155122 static int
2706f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2707f4b3ec61Sdh155122 {
2708f4b3ec61Sdh155122 	if (unconfigure_exclusive_network_interfaces_run(zlogp, zoneid) != 0) {
2709f4b3ec61Sdh155122 		return (unconfigure_exclusive_network_interfaces_static(zlogp,
2710f4b3ec61Sdh155122 		    zoneid));
2711f4b3ec61Sdh155122 	}
2712f4b3ec61Sdh155122 
2713f4b3ec61Sdh155122 	return (0);
2714f4b3ec61Sdh155122 }
2715f4b3ec61Sdh155122 
27167c478bd9Sstevel@tonic-gate static int
27177c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
27187c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
27197c478bd9Sstevel@tonic-gate {
27207c478bd9Sstevel@tonic-gate 	int fd;
27217c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
27227c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
27237c478bd9Sstevel@tonic-gate 	int error;
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
27267c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
27277c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
27287c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
27297c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
27327c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
27337c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
27347c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
27377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
27387c478bd9Sstevel@tonic-gate 		return (-1);
27397c478bd9Sstevel@tonic-gate 	}
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
27427c478bd9Sstevel@tonic-gate 	(void) close(fd);
27437c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
27447c478bd9Sstevel@tonic-gate 		return (0);
27457c478bd9Sstevel@tonic-gate 	return (-1);
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate static int
27497c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
27507c478bd9Sstevel@tonic-gate {
27517c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
27527c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
27537c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
27547c478bd9Sstevel@tonic-gate 	int error;
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	/*
27577c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
27587c478bd9Sstevel@tonic-gate 	 */
27597c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
27607c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
27617c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
27627c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
27637c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
27667c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
27677c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
27687c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
27697c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
27727c478bd9Sstevel@tonic-gate 		return (error);
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	/*
27757c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
27767c478bd9Sstevel@tonic-gate 	 */
27777c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
27787c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
27797c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
27807c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
27817c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
27847c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
27857c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
27867c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
27877c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
27907c478bd9Sstevel@tonic-gate 		return (error);
27917c478bd9Sstevel@tonic-gate 	return (0);
27927c478bd9Sstevel@tonic-gate }
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate static int
2795ffbafc53Scomay get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd)
2796ffbafc53Scomay {
2797ffbafc53Scomay 	int error = -1;
2798ffbafc53Scomay 	zone_dochandle_t handle;
2799ffbafc53Scomay 	char *privname = NULL;
2800ffbafc53Scomay 
2801ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
2802ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2803ffbafc53Scomay 		return (-1);
2804ffbafc53Scomay 	}
2805ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2806ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
2807ffbafc53Scomay 		zonecfg_fini_handle(handle);
2808ffbafc53Scomay 		return (-1);
2809ffbafc53Scomay 	}
2810ffbafc53Scomay 
28119acbbeafSnn35248 	if (mount_cmd) {
2812*bf1d7e28Sdh155122 		zone_iptype_t	iptype;
2813*bf1d7e28Sdh155122 		const char	*curr_iptype;
2814*bf1d7e28Sdh155122 
2815*bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
2816*bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
2817*bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
2818*bf1d7e28Sdh155122 			return (-1);
2819*bf1d7e28Sdh155122 		}
2820*bf1d7e28Sdh155122 
2821*bf1d7e28Sdh155122 		switch (iptype) {
2822*bf1d7e28Sdh155122 		case ZS_SHARED:
2823*bf1d7e28Sdh155122 			curr_iptype = "shared";
2824*bf1d7e28Sdh155122 			break;
2825*bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
2826*bf1d7e28Sdh155122 			curr_iptype = "exclusive";
2827*bf1d7e28Sdh155122 			break;
2828*bf1d7e28Sdh155122 		}
2829*bf1d7e28Sdh155122 
2830*bf1d7e28Sdh155122 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK)
28319acbbeafSnn35248 			return (0);
28329acbbeafSnn35248 		zerror(zlogp, B_FALSE,
28339acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
28349acbbeafSnn35248 		zonecfg_fini_handle(handle);
28359acbbeafSnn35248 		return (-1);
28369acbbeafSnn35248 	}
28379acbbeafSnn35248 
2838ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
2839ffbafc53Scomay 	case Z_OK:
2840ffbafc53Scomay 		error = 0;
2841ffbafc53Scomay 		break;
2842ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2843ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
2844ffbafc53Scomay 		    "within the zone's privilege set", privname);
2845ffbafc53Scomay 		break;
2846ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2847ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
2848ffbafc53Scomay 		    "from the zone's privilege set", privname);
2849ffbafc53Scomay 		break;
2850ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2851ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
2852ffbafc53Scomay 		    "in the zone's privilege set", privname);
2853ffbafc53Scomay 		break;
2854ffbafc53Scomay 	default:
2855ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
2856ffbafc53Scomay 		    "privilege set");
2857ffbafc53Scomay 		break;
2858ffbafc53Scomay 	}
2859ffbafc53Scomay 
2860ffbafc53Scomay 	free(privname);
2861ffbafc53Scomay 	zonecfg_fini_handle(handle);
2862ffbafc53Scomay 	return (error);
2863ffbafc53Scomay }
2864ffbafc53Scomay 
2865ffbafc53Scomay static int
28667c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
28677c478bd9Sstevel@tonic-gate {
28687c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
28697c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
28707c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
28717c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
28727c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
28737c478bd9Sstevel@tonic-gate 	int error = -1;
28747c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
28757c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
28767c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	*bufp = NULL;
28797c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
28827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
28837c478bd9Sstevel@tonic-gate 		return (-1);
28847c478bd9Sstevel@tonic-gate 	}
28857c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
28867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
28877c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
28887c478bd9Sstevel@tonic-gate 		return (-1);
28897c478bd9Sstevel@tonic-gate 	}
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
28927c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
28937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
28947c478bd9Sstevel@tonic-gate 		goto out;
28957c478bd9Sstevel@tonic-gate 	}
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
28987c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
28997c478bd9Sstevel@tonic-gate 		goto out;
29007c478bd9Sstevel@tonic-gate 	}
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
29037c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
29047c478bd9Sstevel@tonic-gate 		goto out;
29057c478bd9Sstevel@tonic-gate 	}
29067c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
29077c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
29087c478bd9Sstevel@tonic-gate 		uint_t i, count;
29097c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
29127c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
29137c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29147c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
29157c478bd9Sstevel@tonic-gate 			continue;
29167c478bd9Sstevel@tonic-gate 		}
29177c478bd9Sstevel@tonic-gate 		count = 0;
29187c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
29197c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
29207c478bd9Sstevel@tonic-gate 			count++;
29217c478bd9Sstevel@tonic-gate 		}
29227c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
29237c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
29247c478bd9Sstevel@tonic-gate 		}
29257c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
29267c478bd9Sstevel@tonic-gate 			goto out;
29277c478bd9Sstevel@tonic-gate 		i = 0;
29287c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
29297c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
29307c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
29317c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
29327c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
29337c478bd9Sstevel@tonic-gate 				goto out;
29347c478bd9Sstevel@tonic-gate 			}
29357c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
29367c478bd9Sstevel@tonic-gate 			    != Z_OK) {
29377c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
29387c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
29397c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
29407c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
29417c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
29427c478bd9Sstevel@tonic-gate 				goto out;
29437c478bd9Sstevel@tonic-gate 			}
29447c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
29457c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
29467c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
29477c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
29487c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
29497c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
29507c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
29517c478bd9Sstevel@tonic-gate 				    name);
29527c478bd9Sstevel@tonic-gate 				goto out;
29537c478bd9Sstevel@tonic-gate 			}
29547c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
29557c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
29567c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29577c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29587c478bd9Sstevel@tonic-gate 				goto out;
29597c478bd9Sstevel@tonic-gate 			}
29607c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
29617c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
29627c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29637c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29647c478bd9Sstevel@tonic-gate 				goto out;
29657c478bd9Sstevel@tonic-gate 			}
29667c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
29677c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
29687c478bd9Sstevel@tonic-gate 			    != 0) {
29697c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29707c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29717c478bd9Sstevel@tonic-gate 				goto out;
29727c478bd9Sstevel@tonic-gate 			}
29737c478bd9Sstevel@tonic-gate 		}
29747c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29757c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
29767c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
29777c478bd9Sstevel@tonic-gate 		    != 0) {
29787c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
29797c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
29807c478bd9Sstevel@tonic-gate 			goto out;
29817c478bd9Sstevel@tonic-gate 		}
29827c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
29837c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
29847c478bd9Sstevel@tonic-gate 		free(nvlv);
29857c478bd9Sstevel@tonic-gate 		nvlv = NULL;
29867c478bd9Sstevel@tonic-gate 		rctlcount++;
29877c478bd9Sstevel@tonic-gate 	}
29887c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
29917c478bd9Sstevel@tonic-gate 		error = 0;
29927c478bd9Sstevel@tonic-gate 		goto out;
29937c478bd9Sstevel@tonic-gate 	}
29947c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
29957c478bd9Sstevel@tonic-gate 	    != 0) {
29967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
29977c478bd9Sstevel@tonic-gate 		goto out;
29987c478bd9Sstevel@tonic-gate 	}
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	error = 0;
30017c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
30027c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate out:
30057c478bd9Sstevel@tonic-gate 	free(rctlblk);
30067c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30077c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
30087c478bd9Sstevel@tonic-gate 		free(nvl_packed);
30097c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
30107c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
30117c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
30127c478bd9Sstevel@tonic-gate 		free(nvlv);
30137c478bd9Sstevel@tonic-gate 	if (handle != NULL)
30147c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
30157c478bd9Sstevel@tonic-gate 	return (error);
30167c478bd9Sstevel@tonic-gate }
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate static int
3019fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3020fa9e4066Sahrens {
3021fa9e4066Sahrens 	zone_dochandle_t handle;
3022fa9e4066Sahrens 	struct zone_dstab dstab;
3023fa9e4066Sahrens 	size_t total, offset, len;
3024fa9e4066Sahrens 	int error = -1;
3025fa9e4066Sahrens 	char *str;
3026fa9e4066Sahrens 
3027fa9e4066Sahrens 	*bufp = NULL;
3028fa9e4066Sahrens 	*bufsizep = 0;
3029fa9e4066Sahrens 
3030fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3031fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3032fa9e4066Sahrens 		return (-1);
3033fa9e4066Sahrens 	}
3034fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3035fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3036fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3037fa9e4066Sahrens 		return (-1);
3038fa9e4066Sahrens 	}
3039fa9e4066Sahrens 
3040fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3041fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3042fa9e4066Sahrens 		goto out;
3043fa9e4066Sahrens 	}
3044fa9e4066Sahrens 
3045fa9e4066Sahrens 	total = 0;
3046fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3047fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3048fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3049fa9e4066Sahrens 
3050fa9e4066Sahrens 	if (total == 0) {
3051fa9e4066Sahrens 		error = 0;
3052fa9e4066Sahrens 		goto out;
3053fa9e4066Sahrens 	}
3054fa9e4066Sahrens 
3055fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3056fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3057fa9e4066Sahrens 		goto out;
3058fa9e4066Sahrens 	}
3059fa9e4066Sahrens 
3060fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3061fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3062fa9e4066Sahrens 		goto out;
3063fa9e4066Sahrens 	}
3064fa9e4066Sahrens 	offset = 0;
3065fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3066fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3067fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3068fa9e4066Sahrens 		    sizeof (dstab.zone_dataset_name) - offset);
3069fa9e4066Sahrens 		offset += len;
3070fa9e4066Sahrens 		if (offset != total - 1)
3071fa9e4066Sahrens 			str[offset++] = ',';
3072fa9e4066Sahrens 	}
3073fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3074fa9e4066Sahrens 
3075fa9e4066Sahrens 	error = 0;
3076fa9e4066Sahrens 	*bufp = str;
3077fa9e4066Sahrens 	*bufsizep = total;
3078fa9e4066Sahrens 
3079fa9e4066Sahrens out:
3080fa9e4066Sahrens 	if (error != 0 && str != NULL)
3081fa9e4066Sahrens 		free(str);
3082fa9e4066Sahrens 	if (handle != NULL)
3083fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3084fa9e4066Sahrens 
3085fa9e4066Sahrens 	return (error);
3086fa9e4066Sahrens }
3087fa9e4066Sahrens 
3088fa9e4066Sahrens static int
3089fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3090fa9e4066Sahrens {
3091fa9e4066Sahrens 	zone_dochandle_t handle;
3092fa9e4066Sahrens 	struct zone_dstab dstab;
3093fa9e4066Sahrens 	zfs_handle_t *zhp;
309499653d4eSeschrock 	libzfs_handle_t *hdl;
3095fa9e4066Sahrens 
3096fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3097fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3098fa9e4066Sahrens 		return (-1);
3099fa9e4066Sahrens 	}
3100fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3101fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3102fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3103fa9e4066Sahrens 		return (-1);
3104fa9e4066Sahrens 	}
3105fa9e4066Sahrens 
3106fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3107fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3108fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3109fa9e4066Sahrens 		return (-1);
3110fa9e4066Sahrens 	}
3111fa9e4066Sahrens 
311299653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
311399653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
311499653d4eSeschrock 		zonecfg_fini_handle(handle);
311599653d4eSeschrock 		return (-1);
311699653d4eSeschrock 	}
3117fa9e4066Sahrens 
3118fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3119fa9e4066Sahrens 
312099653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3121fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3122fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3123fa9e4066Sahrens 			    dstab.zone_dataset_name);
3124fa9e4066Sahrens 			zonecfg_fini_handle(handle);
312599653d4eSeschrock 			libzfs_fini(hdl);
3126fa9e4066Sahrens 			return (-1);
3127fa9e4066Sahrens 		}
3128fa9e4066Sahrens 
3129fa9e4066Sahrens 		/*
3130fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3131fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3132fa9e4066Sahrens 		 */
3133fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3134e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3135e9dbad6fSeschrock 		    "on") != 0) {
3136fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3137fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3138fa9e4066Sahrens 			    dstab.zone_dataset_name);
3139fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3140fa9e4066Sahrens 			zfs_close(zhp);
314199653d4eSeschrock 			libzfs_fini(hdl);
3142fa9e4066Sahrens 			return (-1);
3143fa9e4066Sahrens 		}
3144fa9e4066Sahrens 
3145fa9e4066Sahrens 		zfs_close(zhp);
3146fa9e4066Sahrens 	}
3147fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3148fa9e4066Sahrens 
3149fa9e4066Sahrens 	zonecfg_fini_handle(handle);
315099653d4eSeschrock 	libzfs_fini(hdl);
3151fa9e4066Sahrens 
3152fa9e4066Sahrens 	return (0);
3153fa9e4066Sahrens }
3154fa9e4066Sahrens 
315545916cd2Sjpk /*
315645916cd2Sjpk  * Mount lower level home directories into/from current zone
315745916cd2Sjpk  * Share exported directories specified in dfstab for zone
315845916cd2Sjpk  */
315945916cd2Sjpk static int
316045916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
316145916cd2Sjpk {
316245916cd2Sjpk 	zoneid_t *zids = NULL;
316345916cd2Sjpk 	priv_set_t *zid_privs;
316445916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
316545916cd2Sjpk 	uint_t nzents_saved;
316645916cd2Sjpk 	uint_t nzents;
316745916cd2Sjpk 	int i;
316845916cd2Sjpk 	char readonly[] = "ro";
316945916cd2Sjpk 	struct zone_fstab lower_fstab;
317045916cd2Sjpk 	char *argv[4];
317145916cd2Sjpk 
317245916cd2Sjpk 	if (!is_system_labeled())
317345916cd2Sjpk 		return (0);
317445916cd2Sjpk 
317545916cd2Sjpk 	if (zid_label == NULL) {
317645916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
317745916cd2Sjpk 		if (zid_label == NULL)
317845916cd2Sjpk 			return (-1);
317945916cd2Sjpk 	}
318045916cd2Sjpk 
318145916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
318245916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
318345916cd2Sjpk 	    DEFAULT_DIR_MODE);
318445916cd2Sjpk 
318545916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
318645916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
318745916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
318845916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
318945916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
319045916cd2Sjpk 
319145916cd2Sjpk 	/*
319245916cd2Sjpk 	 * Get the list of zones from the kernel
319345916cd2Sjpk 	 */
319445916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
319545916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
319645916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
319745916cd2Sjpk 		return (-1);
319845916cd2Sjpk 	}
319945916cd2Sjpk again:
320045916cd2Sjpk 	if (nzents == 0) {
320145916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
320245916cd2Sjpk 		return (-1);
320345916cd2Sjpk 	}
320445916cd2Sjpk 
320545916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
320645916cd2Sjpk 	if (zids == NULL) {
32073f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
320845916cd2Sjpk 		return (-1);
320945916cd2Sjpk 	}
321045916cd2Sjpk 	nzents_saved = nzents;
321145916cd2Sjpk 
321245916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
321345916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
321445916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
321545916cd2Sjpk 		free(zids);
321645916cd2Sjpk 		return (-1);
321745916cd2Sjpk 	}
321845916cd2Sjpk 	if (nzents != nzents_saved) {
321945916cd2Sjpk 		/* list changed, try again */
322045916cd2Sjpk 		free(zids);
322145916cd2Sjpk 		goto again;
322245916cd2Sjpk 	}
322345916cd2Sjpk 
322445916cd2Sjpk 	ip = getprivimplinfo();
322545916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
322645916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
322745916cd2Sjpk 		zonecfg_free_fs_option_list(
322845916cd2Sjpk 		    lower_fstab.zone_fs_options);
322945916cd2Sjpk 		free(zids);
323045916cd2Sjpk 		return (-1);
323145916cd2Sjpk 	}
323245916cd2Sjpk 
323345916cd2Sjpk 	for (i = 0; i < nzents; i++) {
323445916cd2Sjpk 		char zid_name[ZONENAME_MAX];
323545916cd2Sjpk 		zone_state_t zid_state;
323645916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
323745916cd2Sjpk 		struct stat stat_buf;
323845916cd2Sjpk 
323945916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
324045916cd2Sjpk 			continue;
324145916cd2Sjpk 
324245916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
324345916cd2Sjpk 			continue;
324445916cd2Sjpk 
324545916cd2Sjpk 		/*
324645916cd2Sjpk 		 * Do special setup for the zone we are booting
324745916cd2Sjpk 		 */
324845916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
324945916cd2Sjpk 			struct zone_fstab autofs_fstab;
325045916cd2Sjpk 			char map_path[MAXPATHLEN];
325145916cd2Sjpk 			int fd;
325245916cd2Sjpk 
325345916cd2Sjpk 			/*
325445916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
32559acbbeafSnn35248 			 * in the global zone. The non-global zone entry
325645916cd2Sjpk 			 * will be created by automount when the zone
325745916cd2Sjpk 			 * is booted.
325845916cd2Sjpk 			 */
325945916cd2Sjpk 
326045916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
326145916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
326245916cd2Sjpk 
326345916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
326445916cd2Sjpk 			    "/zone/%s/home", zid_name);
326545916cd2Sjpk 
326645916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
326745916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
326845916cd2Sjpk 			/*
326945916cd2Sjpk 			 * If the map file doesn't exist create a template
327045916cd2Sjpk 			 */
327145916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
327245916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
327345916cd2Sjpk 				int len;
327445916cd2Sjpk 				char map_rec[MAXPATHLEN];
327545916cd2Sjpk 
327645916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
327745916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
327845916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
327945916cd2Sjpk 				(void) write(fd, map_rec, len);
328045916cd2Sjpk 				(void) close(fd);
328145916cd2Sjpk 			}
328245916cd2Sjpk 
328345916cd2Sjpk 			/*
328445916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
328545916cd2Sjpk 			 * If it's already of type autofs, then
328645916cd2Sjpk 			 * don't mount it again.
328745916cd2Sjpk 			 */
328845916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
328945916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
329045916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
329145916cd2Sjpk 
329245916cd2Sjpk 				(void) make_one_dir(zlogp, "",
329345916cd2Sjpk 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE);
329445916cd2Sjpk 
329545916cd2Sjpk 				/*
329645916cd2Sjpk 				 * Mount will fail if automounter has already
329745916cd2Sjpk 				 * processed the auto_home_<zonename> map
329845916cd2Sjpk 				 */
329945916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
330045916cd2Sjpk 				    autofs_fstab.zone_fs_special,
330145916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
330245916cd2Sjpk 			}
330345916cd2Sjpk 			continue;
330445916cd2Sjpk 		}
330545916cd2Sjpk 
330645916cd2Sjpk 
330745916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
330848451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
330948451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
331045916cd2Sjpk 			/* Skip over zones without mounted filesystems */
331145916cd2Sjpk 			continue;
331245916cd2Sjpk 
331345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
331445916cd2Sjpk 		    sizeof (m_label_t)) < 0)
331545916cd2Sjpk 			/* Skip over zones with unspecified label */
331645916cd2Sjpk 			continue;
331745916cd2Sjpk 
331845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
331945916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
332045916cd2Sjpk 			/* Skip over zones with bad path */
332145916cd2Sjpk 			continue;
332245916cd2Sjpk 
332345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
332445916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
332545916cd2Sjpk 			/* Skip over zones with bad privs */
332645916cd2Sjpk 			continue;
332745916cd2Sjpk 
332845916cd2Sjpk 		/*
332945916cd2Sjpk 		 * Reading down is valid according to our label model
333045916cd2Sjpk 		 * but some customers want to disable it because it
333145916cd2Sjpk 		 * allows execute down and other possible attacks.
333245916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
333345916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
333445916cd2Sjpk 		 * for NFS read-down semantics.
333545916cd2Sjpk 		 */
333645916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
333745916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
333845916cd2Sjpk 			/*
333945916cd2Sjpk 			 * Our zone dominates this one.
334045916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
334145916cd2Sjpk 			 */
334245916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
334345916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
334445916cd2Sjpk 
334545916cd2Sjpk 			/*
334645916cd2Sjpk 			 * If the target is already an LOFS mount
334745916cd2Sjpk 			 * then don't do it again.
334845916cd2Sjpk 			 */
334945916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
335045916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
335145916cd2Sjpk 
335245916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
335345916cd2Sjpk 				    MAXPATHLEN, "%s/export",
335445916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
335545916cd2Sjpk 					continue;
335645916cd2Sjpk 
335745916cd2Sjpk 				/*
335845916cd2Sjpk 				 * Make sure the lower-level home exists
335945916cd2Sjpk 				 */
336045916cd2Sjpk 				if (make_one_dir(zlogp,
336145916cd2Sjpk 				    lower_fstab.zone_fs_special,
336245916cd2Sjpk 				    "/home", DEFAULT_DIR_MODE) != 0)
336345916cd2Sjpk 					continue;
336445916cd2Sjpk 
336545916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
336645916cd2Sjpk 				    "/home", MAXPATHLEN);
336745916cd2Sjpk 
336845916cd2Sjpk 				/*
336945916cd2Sjpk 				 * Mount can fail because the lower-level
337045916cd2Sjpk 				 * zone may have already done a mount up.
337145916cd2Sjpk 				 */
337245916cd2Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
337345916cd2Sjpk 			}
337445916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
337545916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
337645916cd2Sjpk 			/*
337745916cd2Sjpk 			 * This zone dominates our zone.
337845916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
337945916cd2Sjpk 			 */
338045916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
338145916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
338245916cd2Sjpk 			    zone_name) > MAXPATHLEN)
338345916cd2Sjpk 				continue;
338445916cd2Sjpk 
338545916cd2Sjpk 			/*
338645916cd2Sjpk 			 * If the target is already an LOFS mount
338745916cd2Sjpk 			 * then don't do it again.
338845916cd2Sjpk 			 */
338945916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
339045916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
339145916cd2Sjpk 
339245916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
339345916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
339445916cd2Sjpk 
339545916cd2Sjpk 				/*
339645916cd2Sjpk 				 * Mount can fail because the higher-level
339745916cd2Sjpk 				 * zone may have already done a mount down.
339845916cd2Sjpk 				 */
339945916cd2Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
340045916cd2Sjpk 			}
340145916cd2Sjpk 		}
340245916cd2Sjpk 	}
340345916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
340445916cd2Sjpk 	priv_freeset(zid_privs);
340545916cd2Sjpk 	free(zids);
340645916cd2Sjpk 
340745916cd2Sjpk 	/*
340845916cd2Sjpk 	 * Now share any exported directories from this zone.
340945916cd2Sjpk 	 * Each zone can have its own dfstab.
341045916cd2Sjpk 	 */
341145916cd2Sjpk 
341245916cd2Sjpk 	argv[0] = "zoneshare";
341345916cd2Sjpk 	argv[1] = "-z";
341445916cd2Sjpk 	argv[2] = zone_name;
341545916cd2Sjpk 	argv[3] = NULL;
341645916cd2Sjpk 
341745916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
341845916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
341945916cd2Sjpk 
342045916cd2Sjpk 	return (0);
342145916cd2Sjpk }
342245916cd2Sjpk 
342345916cd2Sjpk /*
342445916cd2Sjpk  * Unmount lofs mounts from higher level zones
342545916cd2Sjpk  * Unshare nfs exported directories
342645916cd2Sjpk  */
342745916cd2Sjpk static void
342845916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
342945916cd2Sjpk {
343045916cd2Sjpk 	zoneid_t *zids = NULL;
343145916cd2Sjpk 	uint_t nzents_saved;
343245916cd2Sjpk 	uint_t nzents;
343345916cd2Sjpk 	int i;
343445916cd2Sjpk 	char *argv[4];
343545916cd2Sjpk 	char path[MAXPATHLEN];
343645916cd2Sjpk 
343745916cd2Sjpk 	if (!is_system_labeled())
343845916cd2Sjpk 		return;
343945916cd2Sjpk 
344045916cd2Sjpk 	/*
344145916cd2Sjpk 	 * Get the list of zones from the kernel
344245916cd2Sjpk 	 */
344345916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
344445916cd2Sjpk 		return;
344545916cd2Sjpk 	}
344645916cd2Sjpk 
344745916cd2Sjpk 	if (zid_label == NULL) {
344845916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
344945916cd2Sjpk 		if (zid_label == NULL)
345045916cd2Sjpk 			return;
345145916cd2Sjpk 	}
345245916cd2Sjpk 
345345916cd2Sjpk again:
345445916cd2Sjpk 	if (nzents == 0)
345545916cd2Sjpk 		return;
345645916cd2Sjpk 
345745916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
345845916cd2Sjpk 	if (zids == NULL) {
34593f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
346045916cd2Sjpk 		return;
346145916cd2Sjpk 	}
346245916cd2Sjpk 	nzents_saved = nzents;
346345916cd2Sjpk 
346445916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
346545916cd2Sjpk 		free(zids);
346645916cd2Sjpk 		return;
346745916cd2Sjpk 	}
346845916cd2Sjpk 	if (nzents != nzents_saved) {
346945916cd2Sjpk 		/* list changed, try again */
347045916cd2Sjpk 		free(zids);
347145916cd2Sjpk 		goto again;
347245916cd2Sjpk 	}
347345916cd2Sjpk 
347445916cd2Sjpk 	for (i = 0; i < nzents; i++) {
347545916cd2Sjpk 		char zid_name[ZONENAME_MAX];
347645916cd2Sjpk 		zone_state_t zid_state;
347745916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
347845916cd2Sjpk 
347945916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
348045916cd2Sjpk 			continue;
348145916cd2Sjpk 
348245916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
348345916cd2Sjpk 			continue;
348445916cd2Sjpk 
348545916cd2Sjpk 		/*
348645916cd2Sjpk 		 * Skip the zone we are halting
348745916cd2Sjpk 		 */
348845916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
348945916cd2Sjpk 			continue;
349045916cd2Sjpk 
349145916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
349245916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
349345916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
349445916cd2Sjpk 			/* Skip over zones without mounted filesystems */
349545916cd2Sjpk 			continue;
349645916cd2Sjpk 
349745916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
349845916cd2Sjpk 		    sizeof (m_label_t)) < 0)
349945916cd2Sjpk 			/* Skip over zones with unspecified label */
350045916cd2Sjpk 			continue;
350145916cd2Sjpk 
350245916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
350345916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
350445916cd2Sjpk 			/* Skip over zones with bad path */
350545916cd2Sjpk 			continue;
350645916cd2Sjpk 
350745916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
350845916cd2Sjpk 			/*
350945916cd2Sjpk 			 * This zone dominates our zone.
351045916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
351145916cd2Sjpk 			 */
351245916cd2Sjpk 
351345916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
351445916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
351545916cd2Sjpk 			    zone_name) > MAXPATHLEN)
351645916cd2Sjpk 				continue;
351745916cd2Sjpk 
351845916cd2Sjpk 			/* Skip over mount failures */
351945916cd2Sjpk 			(void) umount(path);
352045916cd2Sjpk 		}
352145916cd2Sjpk 	}
352245916cd2Sjpk 	free(zids);
352345916cd2Sjpk 
352445916cd2Sjpk 	/*
352545916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
352645916cd2Sjpk 	 */
352745916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
352845916cd2Sjpk 	/* Skip over mount failures */
352945916cd2Sjpk 	(void) umount(path);
353045916cd2Sjpk 
353145916cd2Sjpk 	/*
353245916cd2Sjpk 	 * Next unshare any exported directories from this zone.
353345916cd2Sjpk 	 */
353445916cd2Sjpk 
353545916cd2Sjpk 	argv[0] = "zoneunshare";
353645916cd2Sjpk 	argv[1] = "-z";
353745916cd2Sjpk 	argv[2] = zone_name;
353845916cd2Sjpk 	argv[3] = NULL;
353945916cd2Sjpk 
354045916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
354145916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
354245916cd2Sjpk 
354345916cd2Sjpk 	/*
354445916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
354545916cd2Sjpk 	 */
354645916cd2Sjpk 
354745916cd2Sjpk 	argv[0] = "deallocate";
354845916cd2Sjpk 	argv[1] = "-Isz";
354945916cd2Sjpk 	argv[2] = zone_name;
355045916cd2Sjpk 	argv[3] = NULL;
355145916cd2Sjpk 
355245916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
355345916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
355445916cd2Sjpk }
355545916cd2Sjpk 
355645916cd2Sjpk /*
355745916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
355845916cd2Sjpk  * this zone.
355945916cd2Sjpk  */
356045916cd2Sjpk static tsol_zcent_t *
356145916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
356245916cd2Sjpk {
356345916cd2Sjpk 	FILE *fp;
356445916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
356545916cd2Sjpk 	char line[MAXTNZLEN];
356645916cd2Sjpk 
356745916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
356845916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
356945916cd2Sjpk 		return (NULL);
357045916cd2Sjpk 	}
357145916cd2Sjpk 
357245916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
357345916cd2Sjpk 		/*
357445916cd2Sjpk 		 * Check for malformed database
357545916cd2Sjpk 		 */
357645916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
357745916cd2Sjpk 			break;
357845916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
357945916cd2Sjpk 			continue;
358045916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
358145916cd2Sjpk 			break;
358245916cd2Sjpk 		tsol_freezcent(zcent);
358345916cd2Sjpk 		zcent = NULL;
358445916cd2Sjpk 	}
358545916cd2Sjpk 	(void) fclose(fp);
358645916cd2Sjpk 
358745916cd2Sjpk 	if (zcent == NULL) {
358845916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
358945916cd2Sjpk 		    "See tnzonecfg(4)");
359045916cd2Sjpk 	} else {
359145916cd2Sjpk 		if (zlabel == NULL)
359245916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
359345916cd2Sjpk 		/*
359445916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
359545916cd2Sjpk 		 */
359645916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
359745916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
359845916cd2Sjpk 			return (NULL);
359945916cd2Sjpk 		} else {
360045916cd2Sjpk 			priv_copyset(privs, zprivs);
360145916cd2Sjpk 		}
360245916cd2Sjpk 	}
360345916cd2Sjpk 	return (zcent);
360445916cd2Sjpk }
360545916cd2Sjpk 
360645916cd2Sjpk /*
360745916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
360845916cd2Sjpk  */
360945916cd2Sjpk static void
361045916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
361145916cd2Sjpk {
361245916cd2Sjpk 	tsol_mlp_t *mlp;
361345916cd2Sjpk 	tsol_mlpent_t tsme;
361445916cd2Sjpk 
361545916cd2Sjpk 	if (!is_system_labeled())
361645916cd2Sjpk 		return;
361745916cd2Sjpk 
361845916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
361945916cd2Sjpk 	tsme.tsme_flags = 0;
362045916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
362145916cd2Sjpk 		tsme.tsme_mlp = *mlp;
362245916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
362345916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
362445916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
362545916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
362645916cd2Sjpk 		}
362745916cd2Sjpk 	}
362845916cd2Sjpk 
362945916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
363045916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
363145916cd2Sjpk 		tsme.tsme_mlp = *mlp;
363245916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
363345916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
363445916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
363545916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
363645916cd2Sjpk 		}
363745916cd2Sjpk 	}
363845916cd2Sjpk }
363945916cd2Sjpk 
364045916cd2Sjpk static void
364145916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
364245916cd2Sjpk {
364345916cd2Sjpk 	tsol_mlpent_t tsme;
364445916cd2Sjpk 
364545916cd2Sjpk 	if (!is_system_labeled())
364645916cd2Sjpk 		return;
364745916cd2Sjpk 
364845916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
364945916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
365045916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
365145916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
365245916cd2Sjpk }
365345916cd2Sjpk 
36547c478bd9Sstevel@tonic-gate int
36557c478bd9Sstevel@tonic-gate prtmount(const char *fs, void *x) {
36567c478bd9Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
36577c478bd9Sstevel@tonic-gate 	return (0);
36587c478bd9Sstevel@tonic-gate }
36597c478bd9Sstevel@tonic-gate 
3660108322fbScarlsonj /*
3661108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
3662108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3663108322fbScarlsonj  * is found or if we can't tell.
3664108322fbScarlsonj  */
3665108322fbScarlsonj static boolean_t
3666108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
36677c478bd9Sstevel@tonic-gate {
3668108322fbScarlsonj 	zoneid_t *zids = NULL;
3669108322fbScarlsonj 	uint_t nzids = 0;
3670108322fbScarlsonj 	boolean_t retv;
3671108322fbScarlsonj 	int rlen, zlen;
3672108322fbScarlsonj 	char zroot[MAXPATHLEN];
3673108322fbScarlsonj 	char zonename[ZONENAME_MAX];
3674108322fbScarlsonj 
3675108322fbScarlsonj 	for (;;) {
3676108322fbScarlsonj 		nzids += 10;
3677108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
3678108322fbScarlsonj 		if (zids == NULL) {
36793f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3680108322fbScarlsonj 			return (B_TRUE);
3681108322fbScarlsonj 		}
3682108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
3683108322fbScarlsonj 			break;
3684108322fbScarlsonj 		free(zids);
3685108322fbScarlsonj 	}
3686108322fbScarlsonj 	retv = B_FALSE;
3687108322fbScarlsonj 	rlen = strlen(rootpath);
3688108322fbScarlsonj 	while (nzids > 0) {
3689108322fbScarlsonj 		/*
3690108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3691108322fbScarlsonj 		 * while we were busy.
3692108322fbScarlsonj 		 */
3693108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3694108322fbScarlsonj 		    sizeof (zroot)) == -1)
3695108322fbScarlsonj 			continue;
3696108322fbScarlsonj 		zlen = strlen(zroot);
3697108322fbScarlsonj 		if (zlen > rlen)
3698108322fbScarlsonj 			zlen = rlen;
3699108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3700108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3701108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3702108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3703108322fbScarlsonj 			    sizeof (zonename)) == -1)
3704108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3705108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
3706108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3707108322fbScarlsonj 			    "zone root %s already in use by zone %s",
3708108322fbScarlsonj 			    rootpath, zonename);
3709108322fbScarlsonj 			retv = B_TRUE;
3710108322fbScarlsonj 			break;
3711108322fbScarlsonj 		}
3712108322fbScarlsonj 	}
3713108322fbScarlsonj 	free(zids);
3714108322fbScarlsonj 	return (retv);
3715108322fbScarlsonj }
3716108322fbScarlsonj 
3717108322fbScarlsonj /*
3718108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
3719108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3720108322fbScarlsonj  */
3721108322fbScarlsonj static boolean_t
3722108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3723108322fbScarlsonj {
3724108322fbScarlsonj 	struct stat64 rst, zst;
3725108322fbScarlsonj 	struct mnttab *mnp;
3726108322fbScarlsonj 
3727108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
3728108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3729108322fbScarlsonj 		return (B_TRUE);
3730108322fbScarlsonj 	}
3731108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3732108322fbScarlsonj 		return (B_TRUE);
3733108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3734108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
3735108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3736108322fbScarlsonj 			continue;
3737108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3738108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
3739108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3740108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3741108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3742108322fbScarlsonj 			    "zone root %s is reachable through %s",
3743108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
3744108322fbScarlsonj 			return (B_TRUE);
3745108322fbScarlsonj 		}
3746108322fbScarlsonj 	}
3747108322fbScarlsonj 	return (B_FALSE);
3748108322fbScarlsonj }
3749108322fbScarlsonj 
37500209230bSgjelinek /*
37510209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
37520209230bSgjelinek  * configuration.
37530209230bSgjelinek  */
37540209230bSgjelinek static int
37550209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
37560209230bSgjelinek {
37570209230bSgjelinek 	int res;
37580209230bSgjelinek 	uint64_t tmp;
37590209230bSgjelinek 	struct zone_mcaptab mcap;
37600209230bSgjelinek 	char sched[MAXNAMELEN];
37610209230bSgjelinek 	zone_dochandle_t handle = NULL;
37620209230bSgjelinek 	char pool_err[128];
37630209230bSgjelinek 
37640209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
37650209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
37660209230bSgjelinek 		return (Z_BAD_HANDLE);
37670209230bSgjelinek 	}
37680209230bSgjelinek 
37690209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
37700209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
37710209230bSgjelinek 		zonecfg_fini_handle(handle);
37720209230bSgjelinek 		return (res);
37730209230bSgjelinek 	}
37740209230bSgjelinek 
37750209230bSgjelinek 	/*
37760209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
37770209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
37780209230bSgjelinek 	 */
37790209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
37800209230bSgjelinek 		uint64_t num;
37810209230bSgjelinek 		char smf_err[128];
37820209230bSgjelinek 
37830209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
37840209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
37850209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
37860209230bSgjelinek 			zonecfg_fini_handle(handle);
37870209230bSgjelinek 			return (Z_INVAL);
37880209230bSgjelinek 		}
37890209230bSgjelinek 
37900209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
37910209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
37920209230bSgjelinek 			    "failed: %s", smf_err);
37930209230bSgjelinek 			zonecfg_fini_handle(handle);
37940209230bSgjelinek 			return (Z_INVAL);
37950209230bSgjelinek 		}
37960209230bSgjelinek 	}
37970209230bSgjelinek 
37980209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
37990209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
38000209230bSgjelinek 	    strlen(sched) > 0) {
38010209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
38020209230bSgjelinek 		    strlen(sched)) == -1)
38030209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
38040209230bSgjelinek 			    "default scheduling class");
38050209230bSgjelinek 
38060209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
38070209230bSgjelinek 	    == Z_OK) {
38080209230bSgjelinek 		/*
38090209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
38100209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
38110209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
38120209230bSgjelinek 		 * in by default so we can print a warning and modify the class
38130209230bSgjelinek 		 * if we wouldn't be using FSS.
38140209230bSgjelinek 		 */
38150209230bSgjelinek 		char class_name[PC_CLNMSZ];
38160209230bSgjelinek 
38170209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
38180209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
38190209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
38200209230bSgjelinek 			    "the zone's scheduling class");
38210209230bSgjelinek 
38220209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
38230209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
38240209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
38250209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
38260209230bSgjelinek 			    "used for processes\nin the zone but to get the "
38270209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
38280209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
38290209230bSgjelinek 			    "details.");
38300209230bSgjelinek 
38310209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
38320209230bSgjelinek 			    strlen("FSS")) == -1)
38330209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
38340209230bSgjelinek 				    "zone scheduling class to FSS");
38350209230bSgjelinek 		}
38360209230bSgjelinek 	}
38370209230bSgjelinek 
38380209230bSgjelinek 	/*
38390209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
38400209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
38410209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
38420209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
38430209230bSgjelinek 	 * and just return if there is nothing to do.
38440209230bSgjelinek 	 *
38450209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
38460209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
38470209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
38480209230bSgjelinek 	 * zonecfg.
38490209230bSgjelinek 	 */
38500209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
38510209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
38520209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
38530209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
38540209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
38550209230bSgjelinek 			    pool_err);
38560209230bSgjelinek 		else
38570209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
38580209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
38590209230bSgjelinek 		zonecfg_fini_handle(handle);
38600209230bSgjelinek 		return (Z_POOL_BIND);
38610209230bSgjelinek 	}
38620209230bSgjelinek 
38630209230bSgjelinek 	/*
38640209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
38650209230bSgjelinek 	 */
38660209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
38670209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
38680209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
38690209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
38700209230bSgjelinek 		    "processor allocation within the specified range\n"
38710209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
38720209230bSgjelinek 		    "See poold(1M).");
38730209230bSgjelinek 	}
38740209230bSgjelinek 
38750209230bSgjelinek 	/* The following is a warning, not an error. */
38760209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
38770209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
38780209230bSgjelinek 		if (res == Z_POOL_BIND)
38790209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
38800209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
38810209230bSgjelinek 		else if (res == Z_POOL)
38820209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
38830209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
38840209230bSgjelinek 		else
38850209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
38860209230bSgjelinek 			    zonecfg_strerror(res));
38870209230bSgjelinek 	}
38880209230bSgjelinek 
38890209230bSgjelinek 	zonecfg_fini_handle(handle);
38900209230bSgjelinek 	return (Z_OK);
38910209230bSgjelinek }
38920209230bSgjelinek 
3893108322fbScarlsonj zoneid_t
3894108322fbScarlsonj vplat_create(zlog_t *zlogp, boolean_t mount_cmd)
3895108322fbScarlsonj {
3896108322fbScarlsonj 	zoneid_t rval = -1;
38977c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
38987c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
38999acbbeafSnn35248 	char modname[MAXPATHLEN];
39009acbbeafSnn35248 	struct brand_attr attr;
3901123807fbSedp 	brand_handle_t bh;
39027c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
3903108322fbScarlsonj 	size_t rctlbufsz = 0;
3904fa9e4066Sahrens 	char *zfsbuf = NULL;
3905fa9e4066Sahrens 	size_t zfsbufsz = 0;
3906108322fbScarlsonj 	zoneid_t zoneid = -1;
39077c478bd9Sstevel@tonic-gate 	int xerr;
3908108322fbScarlsonj 	char *kzone;
3909108322fbScarlsonj 	FILE *fp = NULL;
391045916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
391145916cd2Sjpk 	int match = 0;
391245916cd2Sjpk 	int doi = 0;
3913f4b3ec61Sdh155122 	int flags;
3914f4b3ec61Sdh155122 	zone_iptype_t iptype;
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
39177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
39187c478bd9Sstevel@tonic-gate 		return (-1);
39197c478bd9Sstevel@tonic-gate 	}
3920108322fbScarlsonj 	if (zonecfg_in_alt_root())
3921108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
39227c478bd9Sstevel@tonic-gate 
3923f4b3ec61Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
3924f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
3925f4b3ec61Sdh155122 		return (-1);
3926f4b3ec61Sdh155122 	}
3927f4b3ec61Sdh155122 	switch (iptype) {
3928f4b3ec61Sdh155122 	case ZS_SHARED:
3929f4b3ec61Sdh155122 		flags = 0;
3930f4b3ec61Sdh155122 		break;
3931f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
3932f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
3933f4b3ec61Sdh155122 		break;
3934f4b3ec61Sdh155122 	}
3935f4b3ec61Sdh155122 
39367c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
39377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
39387c478bd9Sstevel@tonic-gate 		return (-1);
39397c478bd9Sstevel@tonic-gate 	}
39407c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
3941ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
39427c478bd9Sstevel@tonic-gate 		goto error;
3943ffbafc53Scomay 
3944108322fbScarlsonj 	if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
39457c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
39467c478bd9Sstevel@tonic-gate 		goto error;
39477c478bd9Sstevel@tonic-gate 	}
3948ffbafc53Scomay 
3949fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
3950fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
3951fa9e4066Sahrens 		goto error;
3952fa9e4066Sahrens 	}
39537c478bd9Sstevel@tonic-gate 
395448451833Scarlsonj 	if (!mount_cmd && is_system_labeled()) {
395545916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
395648451833Scarlsonj 		if (zcent != NULL) {
395745916cd2Sjpk 			match = zcent->zc_match;
395845916cd2Sjpk 			doi = zcent->zc_doi;
395945916cd2Sjpk 			*zlabel = zcent->zc_label;
396045916cd2Sjpk 		} else {
396145916cd2Sjpk 			goto error;
396245916cd2Sjpk 		}
396345916cd2Sjpk 	}
396445916cd2Sjpk 
3965108322fbScarlsonj 	kzone = zone_name;
3966108322fbScarlsonj 
3967108322fbScarlsonj 	/*
3968108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
3969108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
3970108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
3971108322fbScarlsonj 	 * that use this same source node (same device and inode).
3972108322fbScarlsonj 	 */
3973108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
3974108322fbScarlsonj 		goto error;
3975108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
3976108322fbScarlsonj 		goto error;
3977108322fbScarlsonj 
3978108322fbScarlsonj 	if (mount_cmd) {
39799acbbeafSnn35248 		assert(zone_isnative);
3980108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
3981108322fbScarlsonj 
3982108322fbScarlsonj 		/*
3983108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
3984108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
3985108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
3986108322fbScarlsonj 		 * to both the zone's resources and the running machine's
3987108322fbScarlsonj 		 * executables.
3988108322fbScarlsonj 		 *
3989108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
3990108322fbScarlsonj 		 */
3991108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
3992108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
3993108322fbScarlsonj 			goto error;
3994108322fbScarlsonj 		}
3995108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
3996108322fbScarlsonj 			goto error;
3997108322fbScarlsonj 	}
3998108322fbScarlsonj 
3999108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4000108322fbScarlsonj 		/*
4001108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4002108322fbScarlsonj 		 * then we have some additional work to do before starting the
4003108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4004108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4005108322fbScarlsonj 		 * the zone.
4006108322fbScarlsonj 		 */
4007108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4008108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4009108322fbScarlsonj 			goto error;
4010108322fbScarlsonj 		}
4011108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4012108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4013108322fbScarlsonj 			goto error;
4014108322fbScarlsonj 		}
4015108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4016108322fbScarlsonj 		    NULL, 0) == 0) {
4017108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4018108322fbScarlsonj 			goto error;
4019108322fbScarlsonj 		}
4020108322fbScarlsonj 		/* This is the preferred name */
4021108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4022108322fbScarlsonj 		    zone_name);
4023108322fbScarlsonj 		srandom(getpid());
4024108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4025108322fbScarlsonj 		    0) == 0) {
4026108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4027108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4028108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4029108322fbScarlsonj 		}
4030108322fbScarlsonj 		kzone = kernzone;
4031108322fbScarlsonj 	}
4032108322fbScarlsonj 
40337c478bd9Sstevel@tonic-gate 	xerr = 0;
4034108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4035f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4036f4b3ec61Sdh155122 	    flags)) == -1) {
40377c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
40387c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
40397c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
40407c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
40417c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
40427c478bd9Sstevel@tonic-gate 			} else {
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
40457c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
40467c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
40477c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
40487c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
40497c478bd9Sstevel@tonic-gate 			}
40507c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
40517c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
40527c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
40537c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
40547c478bd9Sstevel@tonic-gate 		} else {
40557c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
40567c478bd9Sstevel@tonic-gate 		}
40577c478bd9Sstevel@tonic-gate 		goto error;
40587c478bd9Sstevel@tonic-gate 	}
4059108322fbScarlsonj 
4060108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4061108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4062108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4063108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4064108322fbScarlsonj 		goto error;
4065108322fbScarlsonj 	}
4066108322fbScarlsonj 
40679acbbeafSnn35248 	if ((zone_get_brand(zone_name, attr.ba_brandname,
40689acbbeafSnn35248 	    MAXNAMELEN) != Z_OK) ||
4069123807fbSedp 	    (bh = brand_open(attr.ba_brandname)) == NULL) {
40709acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine brand name");
40719acbbeafSnn35248 		return (-1);
40729acbbeafSnn35248 	}
40739acbbeafSnn35248 
40749acbbeafSnn35248 	/*
40759acbbeafSnn35248 	 * If this brand requires any kernel support, now is the time to
40769acbbeafSnn35248 	 * get it loaded and initialized.
40779acbbeafSnn35248 	 */
4078123807fbSedp 	if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
40799acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine brand kernel "
40809acbbeafSnn35248 		    "module");
40819acbbeafSnn35248 		return (-1);
40829acbbeafSnn35248 	}
40839acbbeafSnn35248 
40849acbbeafSnn35248 	if (strlen(modname) > 0) {
40859acbbeafSnn35248 		(void) strlcpy(attr.ba_modname, modname, MAXPATHLEN);
40869acbbeafSnn35248 		if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
40879acbbeafSnn35248 		    sizeof (attr) != 0)) {
40889acbbeafSnn35248 			zerror(zlogp, B_TRUE, "could not set zone brand "
40899acbbeafSnn35248 			    "attribute.");
40909acbbeafSnn35248 			goto error;
40919acbbeafSnn35248 		}
40929acbbeafSnn35248 	}
40939acbbeafSnn35248 
40947c478bd9Sstevel@tonic-gate 	/*
40950209230bSgjelinek 	 * The following actions are not performed when merely mounting a zone
40960209230bSgjelinek 	 * for administrative use.
40977c478bd9Sstevel@tonic-gate 	 */
40980209230bSgjelinek 	if (!mount_cmd) {
40990209230bSgjelinek 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) {
41000209230bSgjelinek 			(void) zone_shutdown(zoneid);
41010209230bSgjelinek 			goto error;
41020209230bSgjelinek 		}
41030209230bSgjelinek 
410445916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
41050209230bSgjelinek 	}
41060209230bSgjelinek 
4107108322fbScarlsonj 	rval = zoneid;
4108108322fbScarlsonj 	zoneid = -1;
4109108322fbScarlsonj 
41107c478bd9Sstevel@tonic-gate error:
4111108322fbScarlsonj 	if (zoneid != -1)
4112108322fbScarlsonj 		(void) zone_destroy(zoneid);
41137c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
41147c478bd9Sstevel@tonic-gate 		free(rctlbuf);
41157c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
4116108322fbScarlsonj 	if (fp != NULL)
4117108322fbScarlsonj 		zonecfg_close_scratch(fp);
4118108322fbScarlsonj 	lofs_discard_mnttab();
411945916cd2Sjpk 	if (zcent != NULL)
412045916cd2Sjpk 		tsol_freezcent(zcent);
41217c478bd9Sstevel@tonic-gate 	return (rval);
41227c478bd9Sstevel@tonic-gate }
41237c478bd9Sstevel@tonic-gate 
4124555afedfScarlsonj /*
4125555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
4126555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4127555afedfScarlsonj  * details from inside the zone.
4128555afedfScarlsonj  */
4129555afedfScarlsonj static void
4130555afedfScarlsonj write_index_file(zoneid_t zoneid)
4131555afedfScarlsonj {
4132555afedfScarlsonj 	FILE *zef;
4133555afedfScarlsonj 	FILE *zet;
4134555afedfScarlsonj 	struct zoneent *zep;
4135555afedfScarlsonj 	pid_t child;
4136555afedfScarlsonj 	int tmpl_fd;
4137555afedfScarlsonj 	ctid_t ct;
4138555afedfScarlsonj 	int fd;
4139555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4140555afedfScarlsonj 
4141555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
4142555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
4143555afedfScarlsonj 		return;
4144555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
4145555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
4146555afedfScarlsonj 			break;
4147555afedfScarlsonj 		free(zep);
4148555afedfScarlsonj 	}
4149555afedfScarlsonj 	endzoneent(zef);
4150555afedfScarlsonj 	if (zep == NULL)
4151555afedfScarlsonj 		return;
4152555afedfScarlsonj 
4153555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
4154555afedfScarlsonj 		free(zep);
4155555afedfScarlsonj 		return;
4156555afedfScarlsonj 	}
4157555afedfScarlsonj 
4158555afedfScarlsonj 	if ((child = fork()) == -1) {
4159555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4160555afedfScarlsonj 		(void) close(tmpl_fd);
4161555afedfScarlsonj 		free(zep);
4162555afedfScarlsonj 		return;
4163555afedfScarlsonj 	}
4164555afedfScarlsonj 
4165555afedfScarlsonj 	/* parent waits for child to finish */
4166555afedfScarlsonj 	if (child != 0) {
4167555afedfScarlsonj 		free(zep);
4168555afedfScarlsonj 		if (contract_latest(&ct) == -1)
4169555afedfScarlsonj 			ct = -1;
4170555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4171555afedfScarlsonj 		(void) close(tmpl_fd);
4172555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
4173555afedfScarlsonj 		(void) contract_abandon_id(ct);
4174555afedfScarlsonj 		return;
4175555afedfScarlsonj 	}
4176555afedfScarlsonj 
4177555afedfScarlsonj 	/* child enters zone and sets up index file */
4178555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
4179555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
4180555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4181555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4182555afedfScarlsonj 		    ZONE_CONFIG_GID);
4183555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4184555afedfScarlsonj 		    ZONE_INDEX_MODE);
4185555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4186555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4187555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
4188555afedfScarlsonj 				uuidstr[0] = '\0';
4189555afedfScarlsonj 			else
4190555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
4191555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4192555afedfScarlsonj 			    zone_state_str(zep->zone_state),
4193555afedfScarlsonj 			    uuidstr);
4194555afedfScarlsonj 			(void) fclose(zet);
4195555afedfScarlsonj 		}
4196555afedfScarlsonj 	}
4197555afedfScarlsonj 	_exit(0);
4198555afedfScarlsonj }
4199555afedfScarlsonj 
42007c478bd9Sstevel@tonic-gate int
4201555afedfScarlsonj vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd, zoneid_t zoneid)
42027c478bd9Sstevel@tonic-gate {
42039acbbeafSnn35248 	char zonepath[MAXPATHLEN];
42045749802bSdp 
4205fa9e4066Sahrens 	if (!mount_cmd && validate_datasets(zlogp) != 0) {
4206fa9e4066Sahrens 		lofs_discard_mnttab();
4207fa9e4066Sahrens 		return (-1);
4208fa9e4066Sahrens 	}
4209fa9e4066Sahrens 
42109acbbeafSnn35248 	/*
42119acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
42129acbbeafSnn35248 	 * attribute backing store for /dev
42139acbbeafSnn35248 	 */
42149acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
42159acbbeafSnn35248 		lofs_discard_mnttab();
42169acbbeafSnn35248 		return (-1);
42179acbbeafSnn35248 	}
421816bca938Svp157776 
421916bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
42209acbbeafSnn35248 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE) != 0) {
4221108322fbScarlsonj 		lofs_discard_mnttab();
42227c478bd9Sstevel@tonic-gate 		return (-1);
4223108322fbScarlsonj 	}
4224facf4a8dSllai1 
42259acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4226facf4a8dSllai1 		lofs_discard_mnttab();
4227facf4a8dSllai1 		return (-1);
4228facf4a8dSllai1 	}
4229facf4a8dSllai1 
4230f4b3ec61Sdh155122 	if (!mount_cmd) {
4231f4b3ec61Sdh155122 		zone_iptype_t iptype;
4232f4b3ec61Sdh155122 
4233f4b3ec61Sdh155122 		if (get_iptype(zlogp, &iptype) < 0) {
4234f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4235108322fbScarlsonj 			lofs_discard_mnttab();
42367c478bd9Sstevel@tonic-gate 			return (-1);
4237108322fbScarlsonj 		}
4238555afedfScarlsonj 
4239f4b3ec61Sdh155122 		switch (iptype) {
4240f4b3ec61Sdh155122 		case ZS_SHARED:
4241f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
4242f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
4243f4b3ec61Sdh155122 				lofs_discard_mnttab();
4244f4b3ec61Sdh155122 				return (-1);
4245f4b3ec61Sdh155122 			}
4246f4b3ec61Sdh155122 			break;
4247f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4248f4b3ec61Sdh155122 			if (configure_exclusive_network_interfaces(zlogp) !=
4249f4b3ec61Sdh155122 			    0) {
4250f4b3ec61Sdh155122 				lofs_discard_mnttab();
4251f4b3ec61Sdh155122 				return (-1);
4252f4b3ec61Sdh155122 			}
4253f4b3ec61Sdh155122 			break;
4254f4b3ec61Sdh155122 		}
4255f4b3ec61Sdh155122 	}
4256f4b3ec61Sdh155122 
4257555afedfScarlsonj 	write_index_file(zoneid);
4258555afedfScarlsonj 
4259108322fbScarlsonj 	lofs_discard_mnttab();
42607c478bd9Sstevel@tonic-gate 	return (0);
42617c478bd9Sstevel@tonic-gate }
42627c478bd9Sstevel@tonic-gate 
4263108322fbScarlsonj static int
4264108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
42657c478bd9Sstevel@tonic-gate {
4266108322fbScarlsonj 	char zroot[MAXPATHLEN];
4267108322fbScarlsonj 
42689acbbeafSnn35248 	assert(zone_isnative);
42699acbbeafSnn35248 
4270108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4271108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4272108322fbScarlsonj 		return (-1);
4273108322fbScarlsonj 	}
4274108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4275108322fbScarlsonj 
4276108322fbScarlsonj 	/*
4277108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
4278108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
4279108322fbScarlsonj 	 * still be creds floating about in the system that reference the
4280108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4281108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4282108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
4283108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4284108322fbScarlsonj 	 */
4285108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
4286108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4287108322fbScarlsonj 			goto unmounted;
4288108322fbScarlsonj 		if (errno == EBUSY) {
4289108322fbScarlsonj 			int tries = 10;
4290108322fbScarlsonj 
4291108322fbScarlsonj 			while (--tries >= 0) {
4292108322fbScarlsonj 				(void) sleep(1);
4293108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
4294108322fbScarlsonj 					goto unmounted;
4295108322fbScarlsonj 				if (errno != EBUSY)
4296108322fbScarlsonj 					break;
4297108322fbScarlsonj 			}
4298108322fbScarlsonj 		}
4299108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4300108322fbScarlsonj 		return (-1);
4301108322fbScarlsonj 	}
4302108322fbScarlsonj unmounted:
4303108322fbScarlsonj 
4304108322fbScarlsonj 	/*
4305108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
4306108322fbScarlsonj 	 * entries.
4307108322fbScarlsonj 	 */
4308108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4309108322fbScarlsonj 		FILE *fp;
4310108322fbScarlsonj 		int retv;
4311108322fbScarlsonj 
4312108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4313108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4314108322fbScarlsonj 			return (-1);
4315108322fbScarlsonj 		}
4316108322fbScarlsonj 		retv = -1;
4317108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
4318108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4319108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4320108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4321108322fbScarlsonj 		else
4322108322fbScarlsonj 			retv = 0;
4323108322fbScarlsonj 		zonecfg_close_scratch(fp);
4324108322fbScarlsonj 		return (retv);
4325108322fbScarlsonj 	} else {
4326108322fbScarlsonj 		return (0);
4327108322fbScarlsonj 	}
4328108322fbScarlsonj }
4329108322fbScarlsonj 
4330108322fbScarlsonj int
43310209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4332108322fbScarlsonj {
4333108322fbScarlsonj 	char *kzone;
43347c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
43350209230bSgjelinek 	int res;
43360209230bSgjelinek 	char pool_err[128];
43379acbbeafSnn35248 	char zroot[MAXPATHLEN];
43389acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
43399acbbeafSnn35248 	char brand[MAXNAMELEN];
4340123807fbSedp 	brand_handle_t bh = NULL;
4341f4b3ec61Sdh155122 	ushort_t flags;
43427c478bd9Sstevel@tonic-gate 
4343108322fbScarlsonj 	kzone = zone_name;
4344108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4345108322fbScarlsonj 		FILE *fp;
4346108322fbScarlsonj 
4347108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4348108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
4349108322fbScarlsonj 			goto error;
4350108322fbScarlsonj 		}
4351108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4352108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
4353108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4354108322fbScarlsonj 			zonecfg_close_scratch(fp);
4355108322fbScarlsonj 			goto error;
4356108322fbScarlsonj 		}
4357108322fbScarlsonj 		zonecfg_close_scratch(fp);
4358108322fbScarlsonj 		kzone = kernzone;
4359108322fbScarlsonj 	}
4360108322fbScarlsonj 
4361108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
43627c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
43637c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4364108322fbScarlsonj 		if (unmount_cmd)
4365108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
43667c478bd9Sstevel@tonic-gate 		goto error;
43677c478bd9Sstevel@tonic-gate 	}
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
43707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
43717c478bd9Sstevel@tonic-gate 		goto error;
43727c478bd9Sstevel@tonic-gate 	}
43737c478bd9Sstevel@tonic-gate 
43749acbbeafSnn35248 	/* Get the path to the root of this zone */
43759acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
43769acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone root");
43779acbbeafSnn35248 		goto error;
43789acbbeafSnn35248 	}
43799acbbeafSnn35248 
43809acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
43819acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
4382123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
43839acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
43849acbbeafSnn35248 		return (-1);
43859acbbeafSnn35248 	}
43869acbbeafSnn35248 	/*
43879acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
43889acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
43899acbbeafSnn35248 	 */
43909acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4391123807fbSedp 	if (brand_get_halt(bh, zone_name, zroot, cmdbuf + EXEC_LEN,
43929acbbeafSnn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) < 0) {
4393123807fbSedp 		brand_close(bh);
43949acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
43959acbbeafSnn35248 		    "halt callback.");
43969acbbeafSnn35248 		goto error;
43979acbbeafSnn35248 	}
4398123807fbSedp 	brand_close(bh);
43999acbbeafSnn35248 
44009acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
44019acbbeafSnn35248 	    (do_subproc(zlogp, cmdbuf) != Z_OK)) {
44029acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
44039acbbeafSnn35248 		goto error;
44049acbbeafSnn35248 	}
44059acbbeafSnn35248 
4406f4b3ec61Sdh155122 	if (!unmount_cmd) {
4407f4b3ec61Sdh155122 		zone_iptype_t iptype;
4408f4b3ec61Sdh155122 
4409f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4410f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
4411f4b3ec61Sdh155122 			if (get_iptype(zlogp, &iptype) < 0) {
4412f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
4413f4b3ec61Sdh155122 				    "ip-type");
44147c478bd9Sstevel@tonic-gate 				goto error;
44157c478bd9Sstevel@tonic-gate 			}
4416f4b3ec61Sdh155122 		} else {
4417f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
4418f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
4419f4b3ec61Sdh155122 			else
4420f4b3ec61Sdh155122 				iptype = ZS_SHARED;
4421f4b3ec61Sdh155122 		}
4422f4b3ec61Sdh155122 
4423f4b3ec61Sdh155122 		switch (iptype) {
4424f4b3ec61Sdh155122 		case ZS_SHARED:
4425f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
4426f4b3ec61Sdh155122 			    zoneid) != 0) {
4427f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4428f4b3ec61Sdh155122 				    "network interfaces in zone");
4429f4b3ec61Sdh155122 				goto error;
4430f4b3ec61Sdh155122 			}
4431f4b3ec61Sdh155122 			break;
4432f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4433f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
4434f4b3ec61Sdh155122 			    zoneid) != 0) {
4435f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4436f4b3ec61Sdh155122 				    "network interfaces in zone");
4437f4b3ec61Sdh155122 				goto error;
4438f4b3ec61Sdh155122 			}
4439f4b3ec61Sdh155122 			break;
4440f4b3ec61Sdh155122 		}
4441f4b3ec61Sdh155122 	}
44427c478bd9Sstevel@tonic-gate 
4443108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
44447c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
44457c478bd9Sstevel@tonic-gate 		goto error;
44467c478bd9Sstevel@tonic-gate 	}
44477c478bd9Sstevel@tonic-gate 
4448facf4a8dSllai1 	/* destroy zconsole before umount /dev */
4449facf4a8dSllai1 	if (!unmount_cmd)
4450facf4a8dSllai1 		destroy_console_slave();
4451facf4a8dSllai1 
4452108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
44537c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
44547c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
44557c478bd9Sstevel@tonic-gate 		goto error;
44567c478bd9Sstevel@tonic-gate 	}
44577c478bd9Sstevel@tonic-gate 
44580209230bSgjelinek 	/*
4459cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
4460cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
4461cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
4462cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
4463cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
4464cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
4465cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
4466cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
44670209230bSgjelinek 	 */
4468cf6b13d2Sgjelinek 	if (!unmount_cmd) {
4469cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
4470cf6b13d2Sgjelinek 
4471cf6b13d2Sgjelinek 		if (rebooting) {
4472cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
4473cf6b13d2Sgjelinek 			zone_dochandle_t handle;
4474cf6b13d2Sgjelinek 
4475cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
4476cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4477cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4478cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
4479cf6b13d2Sgjelinek 		}
4480cf6b13d2Sgjelinek 
4481cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
44820209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
44830209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
44840209230bSgjelinek 				if (res == Z_POOL)
44850209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
44860209230bSgjelinek 			}
44870209230bSgjelinek 		}
4488cf6b13d2Sgjelinek 	}
44890209230bSgjelinek 
449045916cd2Sjpk 	remove_mlps(zlogp, zoneid);
449145916cd2Sjpk 
44927c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
44937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
44947c478bd9Sstevel@tonic-gate 		goto error;
44957c478bd9Sstevel@tonic-gate 	}
4496108322fbScarlsonj 
4497108322fbScarlsonj 	/*
4498108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
4499108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
4500108322fbScarlsonj 	 */
4501108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4502108322fbScarlsonj 		goto error;
4503108322fbScarlsonj 
4504108322fbScarlsonj 	lofs_discard_mnttab();
45057c478bd9Sstevel@tonic-gate 	return (0);
45067c478bd9Sstevel@tonic-gate 
45077c478bd9Sstevel@tonic-gate error:
4508108322fbScarlsonj 	lofs_discard_mnttab();
45097c478bd9Sstevel@tonic-gate 	return (-1);
45107c478bd9Sstevel@tonic-gate }
4511