xref: /titanic_41/usr/src/cmd/zoneadmd/vplat.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This module contains functions used to bring up and tear down the
29  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
30  * interfaces, [un]configuring devices, establishing resource controls,
31  * and creating/destroying the zone in the kernel.  These actions, on
32  * the way up, ready the zone; on the way down, they halt the zone.
33  * See the much longer block comment at the beginning of zoneadmd.c
34  * for a bigger picture of how the whole program functions.
35  *
36  * This module also has primary responsibility for the layout of "scratch
37  * zones."  These are mounted, but inactive, zones that are used during
38  * operating system upgrade and potentially other administrative action.  The
39  * scratch zone environment is similar to the miniroot environment.  The zone's
40  * actual root is mounted read-write on /a, and the standard paths (/usr,
41  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
42  * This allows the administrative tools to manipulate the zone using "-R /a"
43  * without relying on any binaries in the zone itself.
44  *
45  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
46  * environment), then we must resolve the lofs mounts used there to uncover
47  * writable (unshared) resources.  Shared resources, though, are always
48  * read-only.  In addition, if the "same" zone with a different root path is
49  * currently running, then "/b" inside the zone points to the running zone's
50  * root.  This allows LU to synchronize configuration files during the upgrade
51  * process.
52  *
53  * To construct this environment, this module creates a tmpfs mount on
54  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
55  * described above is constructed on the fly.  The zone is then created using
56  * $ZONEPATH/lu as the root.
57  *
58  * Note that scratch zones are inactive.  The zone's bits are not running and
59  * likely cannot be run correctly until upgrade is done.  Init is not running
60  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
61  * is not a part of the usual halt/ready/boot state machine.
62  */
63 
64 #include <sys/param.h>
65 #include <sys/mount.h>
66 #include <sys/mntent.h>
67 #include <sys/socket.h>
68 #include <sys/utsname.h>
69 #include <sys/types.h>
70 #include <sys/stat.h>
71 #include <sys/sockio.h>
72 #include <sys/stropts.h>
73 #include <sys/conf.h>
74 
75 #include <libdlpi.h>
76 #include <libdllink.h>
77 #include <libdlvlan.h>
78 
79 #include <inet/tcp.h>
80 #include <arpa/inet.h>
81 #include <netinet/in.h>
82 #include <net/route.h>
83 
84 #include <stdio.h>
85 #include <errno.h>
86 #include <fcntl.h>
87 #include <unistd.h>
88 #include <rctl.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <strings.h>
92 #include <wait.h>
93 #include <limits.h>
94 #include <libgen.h>
95 #include <libzfs.h>
96 #include <libdevinfo.h>
97 #include <zone.h>
98 #include <assert.h>
99 #include <libcontract.h>
100 #include <libcontract_priv.h>
101 #include <uuid/uuid.h>
102 
103 #include <sys/mntio.h>
104 #include <sys/mnttab.h>
105 #include <sys/fs/autofs.h>	/* for _autofssys() */
106 #include <sys/fs/lofs_info.h>
107 #include <sys/fs/zfs.h>
108 
109 #include <pool.h>
110 #include <sys/pool.h>
111 #include <sys/priocntl.h>
112 
113 #include <libbrand.h>
114 #include <sys/brand.h>
115 #include <libzonecfg.h>
116 #include <synch.h>
117 
118 #include "zoneadmd.h"
119 #include <tsol/label.h>
120 #include <libtsnet.h>
121 #include <sys/priv.h>
122 
123 #define	V4_ADDR_LEN	32
124 #define	V6_ADDR_LEN	128
125 
126 #define	IPD_DEFAULT_OPTS \
127 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
128 
129 #define	DFSTYPES	"/etc/dfs/fstypes"
130 #define	MAXTNZLEN	2048
131 
132 #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
133 
134 /* for routing socket */
135 static int rts_seqno = 0;
136 
137 /* mangled zone name when mounting in an alternate root environment */
138 static char kernzone[ZONENAME_MAX];
139 
140 /* array of cached mount entries for resolve_lofs */
141 static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
142 
143 /* for Trusted Extensions */
144 static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
145 static int tsol_mounts(zlog_t *, char *, char *);
146 static void tsol_unmounts(zlog_t *, char *);
147 
148 static m_label_t *zlabel = NULL;
149 static m_label_t *zid_label = NULL;
150 static priv_set_t *zprivs = NULL;
151 
152 /* from libsocket, not in any header file */
153 extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
154 
155 /* from zoneadmd */
156 extern char query_hook[];
157 
158 /*
159  * An optimization for build_mnttable: reallocate (and potentially copy the
160  * data) only once every N times through the loop.
161  */
162 #define	MNTTAB_HUNK	32
163 
164 /*
165  * Private autofs system call
166  */
167 extern int _autofssys(int, void *);
168 
169 static int
170 autofs_cleanup(zoneid_t zoneid)
171 {
172 	/*
173 	 * Ask autofs to unmount all trigger nodes in the given zone.
174 	 */
175 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
176 }
177 
178 static void
179 free_mnttable(struct mnttab *mnt_array, uint_t nelem)
180 {
181 	uint_t i;
182 
183 	if (mnt_array == NULL)
184 		return;
185 	for (i = 0; i < nelem; i++) {
186 		free(mnt_array[i].mnt_mountp);
187 		free(mnt_array[i].mnt_fstype);
188 		free(mnt_array[i].mnt_special);
189 		free(mnt_array[i].mnt_mntopts);
190 		assert(mnt_array[i].mnt_time == NULL);
191 	}
192 	free(mnt_array);
193 }
194 
195 /*
196  * Build the mount table for the zone rooted at "zroot", storing the resulting
197  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
198  * array in "nelemp".
199  */
200 static int
201 build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
202     struct mnttab **mnt_arrayp, uint_t *nelemp)
203 {
204 	struct mnttab mnt;
205 	struct mnttab *mnts;
206 	struct mnttab *mnp;
207 	uint_t nmnt;
208 
209 	rewind(mnttab);
210 	resetmnttab(mnttab);
211 	nmnt = 0;
212 	mnts = NULL;
213 	while (getmntent(mnttab, &mnt) == 0) {
214 		struct mnttab *tmp_array;
215 
216 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
217 			continue;
218 		if (nmnt % MNTTAB_HUNK == 0) {
219 			tmp_array = realloc(mnts,
220 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
221 			if (tmp_array == NULL) {
222 				free_mnttable(mnts, nmnt);
223 				return (-1);
224 			}
225 			mnts = tmp_array;
226 		}
227 		mnp = &mnts[nmnt++];
228 
229 		/*
230 		 * Zero out any fields we're not using.
231 		 */
232 		(void) memset(mnp, 0, sizeof (*mnp));
233 
234 		if (mnt.mnt_special != NULL)
235 			mnp->mnt_special = strdup(mnt.mnt_special);
236 		if (mnt.mnt_mntopts != NULL)
237 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
238 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
239 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
240 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
241 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
242 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
243 			zerror(zlogp, B_TRUE, "memory allocation failed");
244 			free_mnttable(mnts, nmnt);
245 			return (-1);
246 		}
247 	}
248 	*mnt_arrayp = mnts;
249 	*nelemp = nmnt;
250 	return (0);
251 }
252 
253 /*
254  * This is an optimization.  The resolve_lofs function is used quite frequently
255  * to manipulate file paths, and on a machine with a large number of zones,
256  * there will be a huge number of mounted file systems.  Thus, we trigger a
257  * reread of the list of mount points
258  */
259 static void
260 lofs_discard_mnttab(void)
261 {
262 	free_mnttable(resolve_lofs_mnts,
263 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
264 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
265 }
266 
267 static int
268 lofs_read_mnttab(zlog_t *zlogp)
269 {
270 	FILE *mnttab;
271 	uint_t nmnts;
272 
273 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
274 		return (-1);
275 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
276 	    &nmnts) == -1) {
277 		(void) fclose(mnttab);
278 		return (-1);
279 	}
280 	(void) fclose(mnttab);
281 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
282 	return (0);
283 }
284 
285 /*
286  * This function loops over potential loopback mounts and symlinks in a given
287  * path and resolves them all down to an absolute path.
288  */
289 void
290 resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
291 {
292 	int len, arlen;
293 	const char *altroot;
294 	char tmppath[MAXPATHLEN];
295 	boolean_t outside_altroot;
296 
297 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
298 		return;
299 	tmppath[len] = '\0';
300 	(void) strlcpy(path, tmppath, sizeof (tmppath));
301 
302 	/* This happens once per zoneadmd operation. */
303 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
304 		return;
305 
306 	altroot = zonecfg_get_root();
307 	arlen = strlen(altroot);
308 	outside_altroot = B_FALSE;
309 	for (;;) {
310 		struct mnttab *mnp;
311 
312 		/* Search in reverse order to find longest match */
313 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
314 		    mnp--) {
315 			if (mnp->mnt_fstype == NULL ||
316 			    mnp->mnt_mountp == NULL ||
317 			    mnp->mnt_special == NULL)
318 				continue;
319 			len = strlen(mnp->mnt_mountp);
320 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
321 			    (path[len] == '/' || path[len] == '\0'))
322 				break;
323 		}
324 		if (mnp < resolve_lofs_mnts)
325 			break;
326 		/* If it's not a lofs then we're done */
327 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
328 			break;
329 		if (outside_altroot) {
330 			char *cp;
331 			int olen = sizeof (MNTOPT_RO) - 1;
332 
333 			/*
334 			 * If we run into a read-only mount outside of the
335 			 * alternate root environment, then the user doesn't
336 			 * want this path to be made read-write.
337 			 */
338 			if (mnp->mnt_mntopts != NULL &&
339 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
340 			    NULL &&
341 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
342 			    (cp[olen] == '\0' || cp[olen] == ',')) {
343 				break;
344 			}
345 		} else if (arlen > 0 &&
346 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
347 		    (mnp->mnt_special[arlen] != '\0' &&
348 		    mnp->mnt_special[arlen] != '/'))) {
349 			outside_altroot = B_TRUE;
350 		}
351 		/* use temporary buffer because new path might be longer */
352 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
353 		    mnp->mnt_special, path + len);
354 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
355 			break;
356 		path[len] = '\0';
357 	}
358 }
359 
360 /*
361  * For a regular mount, check if a replacement lofs mount is needed because the
362  * referenced device is already mounted somewhere.
363  */
364 static int
365 check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
366 {
367 	struct mnttab *mnp;
368 	zone_fsopt_t *optptr, *onext;
369 
370 	/* This happens once per zoneadmd operation. */
371 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
372 		return (-1);
373 
374 	/*
375 	 * If this special node isn't already in use, then it's ours alone;
376 	 * no need to worry about conflicting mounts.
377 	 */
378 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
379 	    mnp++) {
380 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
381 			break;
382 	}
383 	if (mnp >= resolve_lofs_mnt_max)
384 		return (0);
385 
386 	/*
387 	 * Convert this duplicate mount into a lofs mount.
388 	 */
389 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
390 	    sizeof (fsptr->zone_fs_special));
391 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
392 	    sizeof (fsptr->zone_fs_type));
393 	fsptr->zone_fs_raw[0] = '\0';
394 
395 	/*
396 	 * Discard all but one of the original options and set that to be the
397 	 * same set of options used for inherit package directory resources.
398 	 */
399 	optptr = fsptr->zone_fs_options;
400 	if (optptr == NULL) {
401 		optptr = malloc(sizeof (*optptr));
402 		if (optptr == NULL) {
403 			zerror(zlogp, B_TRUE, "cannot mount %s",
404 			    fsptr->zone_fs_dir);
405 			return (-1);
406 		}
407 	} else {
408 		while ((onext = optptr->zone_fsopt_next) != NULL) {
409 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
410 			free(onext);
411 		}
412 	}
413 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
414 	optptr->zone_fsopt_next = NULL;
415 	fsptr->zone_fs_options = optptr;
416 	return (0);
417 }
418 
419 int
420 make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
421     uid_t userid, gid_t groupid)
422 {
423 	char path[MAXPATHLEN];
424 	struct stat st;
425 
426 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
427 	    sizeof (path)) {
428 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
429 		    subdir);
430 		return (-1);
431 	}
432 
433 	if (lstat(path, &st) == 0) {
434 		/*
435 		 * We don't check the file mode since presumably the zone
436 		 * administrator may have had good reason to change the mode,
437 		 * and we don't need to second guess him.
438 		 */
439 		if (!S_ISDIR(st.st_mode)) {
440 			if (S_ISREG(st.st_mode)) {
441 				/*
442 				 * Allow readonly mounts of /etc/ files; this
443 				 * is needed most by Trusted Extensions.
444 				 */
445 				if (strncmp(subdir, "/etc/",
446 				    strlen("/etc/")) != 0) {
447 					zerror(zlogp, B_FALSE,
448 					    "%s is not in /etc", path);
449 					return (-1);
450 				}
451 			} else {
452 				zerror(zlogp, B_FALSE,
453 				    "%s is not a directory", path);
454 				return (-1);
455 			}
456 		}
457 		return (0);
458 	}
459 
460 	if (mkdirp(path, mode) != 0) {
461 		if (errno == EROFS)
462 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
463 			    "a read-only file system in this local zone.\nMake "
464 			    "sure %s exists in the global zone.", path, subdir);
465 		else
466 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
467 		return (-1);
468 	}
469 
470 	(void) chown(path, userid, groupid);
471 	return (0);
472 }
473 
474 static void
475 free_remote_fstypes(char **types)
476 {
477 	uint_t i;
478 
479 	if (types == NULL)
480 		return;
481 	for (i = 0; types[i] != NULL; i++)
482 		free(types[i]);
483 	free(types);
484 }
485 
486 static char **
487 get_remote_fstypes(zlog_t *zlogp)
488 {
489 	char **types = NULL;
490 	FILE *fp;
491 	char buf[MAXPATHLEN];
492 	char fstype[MAXPATHLEN];
493 	uint_t lines = 0;
494 	uint_t i;
495 
496 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
497 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
498 		return (NULL);
499 	}
500 	/*
501 	 * Count the number of lines
502 	 */
503 	while (fgets(buf, sizeof (buf), fp) != NULL)
504 		lines++;
505 	if (lines == 0)	/* didn't read anything; empty file */
506 		goto out;
507 	rewind(fp);
508 	/*
509 	 * Allocate enough space for a NULL-terminated array.
510 	 */
511 	types = calloc(lines + 1, sizeof (char *));
512 	if (types == NULL) {
513 		zerror(zlogp, B_TRUE, "memory allocation failed");
514 		goto out;
515 	}
516 	i = 0;
517 	while (fgets(buf, sizeof (buf), fp) != NULL) {
518 		/* LINTED - fstype is big enough to hold buf */
519 		if (sscanf(buf, "%s", fstype) == 0) {
520 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
521 			free_remote_fstypes(types);
522 			types = NULL;
523 			goto out;
524 		}
525 		types[i] = strdup(fstype);
526 		if (types[i] == NULL) {
527 			zerror(zlogp, B_TRUE, "memory allocation failed");
528 			free_remote_fstypes(types);
529 			types = NULL;
530 			goto out;
531 		}
532 		i++;
533 	}
534 out:
535 	(void) fclose(fp);
536 	return (types);
537 }
538 
539 static boolean_t
540 is_remote_fstype(const char *fstype, char *const *remote_fstypes)
541 {
542 	uint_t i;
543 
544 	if (remote_fstypes == NULL)
545 		return (B_FALSE);
546 	for (i = 0; remote_fstypes[i] != NULL; i++) {
547 		if (strcmp(remote_fstypes[i], fstype) == 0)
548 			return (B_TRUE);
549 	}
550 	return (B_FALSE);
551 }
552 
553 /*
554  * This converts a zone root path (normally of the form .../root) to a Live
555  * Upgrade scratch zone root (of the form .../lu).
556  */
557 static void
558 root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
559 {
560 	if (!isresolved && zonecfg_in_alt_root())
561 		resolve_lofs(zlogp, zroot, zrootlen);
562 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
563 }
564 
565 /*
566  * The general strategy for unmounting filesystems is as follows:
567  *
568  * - Remote filesystems may be dead, and attempting to contact them as
569  * part of a regular unmount may hang forever; we want to always try to
570  * forcibly unmount such filesystems and only fall back to regular
571  * unmounts if the filesystem doesn't support forced unmounts.
572  *
573  * - We don't want to unnecessarily corrupt metadata on local
574  * filesystems (ie UFS), so we want to start off with graceful unmounts,
575  * and only escalate to doing forced unmounts if we get stuck.
576  *
577  * We start off walking backwards through the mount table.  This doesn't
578  * give us strict ordering but ensures that we try to unmount submounts
579  * first.  We thus limit the number of failed umount2(2) calls.
580  *
581  * The mechanism for determining if we're stuck is to count the number
582  * of failed unmounts each iteration through the mount table.  This
583  * gives us an upper bound on the number of filesystems which remain
584  * mounted (autofs trigger nodes are dealt with separately).  If at the
585  * end of one unmount+autofs_cleanup cycle we still have the same number
586  * of mounts that we started out with, we're stuck and try a forced
587  * unmount.  If that fails (filesystem doesn't support forced unmounts)
588  * then we bail and are unable to teardown the zone.  If it succeeds,
589  * we're no longer stuck so we continue with our policy of trying
590  * graceful mounts first.
591  *
592  * Zone must be down (ie, no processes or threads active).
593  */
594 static int
595 unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
596 {
597 	int error = 0;
598 	FILE *mnttab;
599 	struct mnttab *mnts;
600 	uint_t nmnt;
601 	char zroot[MAXPATHLEN + 1];
602 	size_t zrootlen;
603 	uint_t oldcount = UINT_MAX;
604 	boolean_t stuck = B_FALSE;
605 	char **remote_fstypes = NULL;
606 
607 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
608 		zerror(zlogp, B_FALSE, "unable to determine zone root");
609 		return (-1);
610 	}
611 	if (unmount_cmd)
612 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
613 
614 	(void) strcat(zroot, "/");
615 	zrootlen = strlen(zroot);
616 
617 	/*
618 	 * For Trusted Extensions unmount each higher level zone's mount
619 	 * of our zone's /export/home
620 	 */
621 	if (!unmount_cmd)
622 		tsol_unmounts(zlogp, zone_name);
623 
624 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
625 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
626 		return (-1);
627 	}
628 	/*
629 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
630 	 * MS_NOMNTTAB.
631 	 */
632 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
633 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
634 		error++;
635 		goto out;
636 	}
637 
638 	/*
639 	 * Build the list of remote fstypes so we know which ones we
640 	 * should forcibly unmount.
641 	 */
642 	remote_fstypes = get_remote_fstypes(zlogp);
643 	for (; /* ever */; ) {
644 		uint_t newcount = 0;
645 		boolean_t unmounted;
646 		struct mnttab *mnp;
647 		char *path;
648 		uint_t i;
649 
650 		mnts = NULL;
651 		nmnt = 0;
652 		/*
653 		 * MNTTAB gives us a way to walk through mounted
654 		 * filesystems; we need to be able to walk them in
655 		 * reverse order, so we build a list of all mounted
656 		 * filesystems.
657 		 */
658 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
659 		    &nmnt) != 0) {
660 			error++;
661 			goto out;
662 		}
663 		for (i = 0; i < nmnt; i++) {
664 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
665 			path = mnp->mnt_mountp;
666 			unmounted = B_FALSE;
667 			/*
668 			 * Try forced unmount first for remote filesystems.
669 			 *
670 			 * Not all remote filesystems support forced unmounts,
671 			 * so if this fails (ENOTSUP) we'll continue on
672 			 * and try a regular unmount.
673 			 */
674 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
675 				if (umount2(path, MS_FORCE) == 0)
676 					unmounted = B_TRUE;
677 			}
678 			/*
679 			 * Try forced unmount if we're stuck.
680 			 */
681 			if (stuck) {
682 				if (umount2(path, MS_FORCE) == 0) {
683 					unmounted = B_TRUE;
684 					stuck = B_FALSE;
685 				} else {
686 					/*
687 					 * The first failure indicates a
688 					 * mount we won't be able to get
689 					 * rid of automatically, so we
690 					 * bail.
691 					 */
692 					error++;
693 					zerror(zlogp, B_FALSE,
694 					    "unable to unmount '%s'", path);
695 					free_mnttable(mnts, nmnt);
696 					goto out;
697 				}
698 			}
699 			/*
700 			 * Try regular unmounts for everything else.
701 			 */
702 			if (!unmounted && umount2(path, 0) != 0)
703 				newcount++;
704 		}
705 		free_mnttable(mnts, nmnt);
706 
707 		if (newcount == 0)
708 			break;
709 		if (newcount >= oldcount) {
710 			/*
711 			 * Last round didn't unmount anything; we're stuck and
712 			 * should start trying forced unmounts.
713 			 */
714 			stuck = B_TRUE;
715 		}
716 		oldcount = newcount;
717 
718 		/*
719 		 * Autofs doesn't let you unmount its trigger nodes from
720 		 * userland so we have to tell the kernel to cleanup for us.
721 		 */
722 		if (autofs_cleanup(zoneid) != 0) {
723 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
724 			error++;
725 			goto out;
726 		}
727 	}
728 
729 out:
730 	free_remote_fstypes(remote_fstypes);
731 	(void) fclose(mnttab);
732 	return (error ? -1 : 0);
733 }
734 
735 static int
736 fs_compare(const void *m1, const void *m2)
737 {
738 	struct zone_fstab *i = (struct zone_fstab *)m1;
739 	struct zone_fstab *j = (struct zone_fstab *)m2;
740 
741 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
742 }
743 
744 /*
745  * Fork and exec (and wait for) the mentioned binary with the provided
746  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
747  * returns the exit status otherwise.
748  *
749  * If we were unable to exec the provided pathname (for whatever
750  * reason), we return the special token ZEXIT_EXEC.  The current value
751  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
752  * consumers of this function; any future consumers must make sure this
753  * remains the case.
754  */
755 static int
756 forkexec(zlog_t *zlogp, const char *path, char *const argv[])
757 {
758 	pid_t child_pid;
759 	int child_status = 0;
760 
761 	/*
762 	 * Do not let another thread localize a message while we are forking.
763 	 */
764 	(void) mutex_lock(&msglock);
765 	child_pid = fork();
766 	(void) mutex_unlock(&msglock);
767 	if (child_pid == -1) {
768 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
769 		return (-1);
770 	} else if (child_pid == 0) {
771 		closefrom(0);
772 		/* redirect stdin, stdout & stderr to /dev/null */
773 		(void) open("/dev/null", O_RDONLY);	/* stdin */
774 		(void) open("/dev/null", O_WRONLY);	/* stdout */
775 		(void) open("/dev/null", O_WRONLY);	/* stderr */
776 		(void) execv(path, argv);
777 		/*
778 		 * Since we are in the child, there is no point calling zerror()
779 		 * since there is nobody waiting to consume it.  So exit with a
780 		 * special code that the parent will recognize and call zerror()
781 		 * accordingly.
782 		 */
783 
784 		_exit(ZEXIT_EXEC);
785 	} else {
786 		(void) waitpid(child_pid, &child_status, 0);
787 	}
788 
789 	if (WIFSIGNALED(child_status)) {
790 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
791 		    "signal %d", path, WTERMSIG(child_status));
792 		return (-1);
793 	}
794 	assert(WIFEXITED(child_status));
795 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
796 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
797 		return (-1);
798 	}
799 	return (WEXITSTATUS(child_status));
800 }
801 
802 static int
803 isregfile(const char *path)
804 {
805 	struct stat64 st;
806 
807 	if (stat64(path, &st) == -1)
808 		return (-1);
809 
810 	return (S_ISREG(st.st_mode));
811 }
812 
813 static int
814 dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
815 {
816 	char cmdbuf[MAXPATHLEN];
817 	char *argv[4];
818 	int status;
819 
820 	/*
821 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
822 	 * that would cost us an extra fork/exec without buying us anything.
823 	 */
824 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
825 	    >= sizeof (cmdbuf)) {
826 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
827 		return (-1);
828 	}
829 
830 	/*
831 	 * If it doesn't exist, that's OK: we verified this previously
832 	 * in zoneadm.
833 	 */
834 	if (isregfile(cmdbuf) == -1)
835 		return (0);
836 
837 	argv[0] = "fsck";
838 	argv[1] = "-m";
839 	argv[2] = (char *)rawdev;
840 	argv[3] = NULL;
841 
842 	status = forkexec(zlogp, cmdbuf, argv);
843 	if (status == 0 || status == -1)
844 		return (status);
845 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
846 	    "run fsck manually", rawdev, status);
847 	return (-1);
848 }
849 
850 static int
851 domount(zlog_t *zlogp, const char *fstype, const char *opts,
852     const char *special, const char *directory)
853 {
854 	char cmdbuf[MAXPATHLEN];
855 	char *argv[6];
856 	int status;
857 
858 	/*
859 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
860 	 * that would cost us an extra fork/exec without buying us anything.
861 	 */
862 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
863 	    >= sizeof (cmdbuf)) {
864 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
865 		return (-1);
866 	}
867 	argv[0] = "mount";
868 	if (opts[0] == '\0') {
869 		argv[1] = (char *)special;
870 		argv[2] = (char *)directory;
871 		argv[3] = NULL;
872 	} else {
873 		argv[1] = "-o";
874 		argv[2] = (char *)opts;
875 		argv[3] = (char *)special;
876 		argv[4] = (char *)directory;
877 		argv[5] = NULL;
878 	}
879 
880 	status = forkexec(zlogp, cmdbuf, argv);
881 	if (status == 0 || status == -1)
882 		return (status);
883 	if (opts[0] == '\0')
884 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
885 		    "failed with exit code %d",
886 		    cmdbuf, special, directory, status);
887 	else
888 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
889 		    "failed with exit code %d",
890 		    cmdbuf, opts, special, directory, status);
891 	return (-1);
892 }
893 
894 /*
895  * Check if a given mount point path exists.
896  * If it does, make sure it doesn't contain any symlinks.
897  * Note that if "leaf" is false we're checking an intermediate
898  * component of the mount point path, so it must be a directory.
899  * If "leaf" is true, then we're checking the entire mount point
900  * path, so the mount point itself can be anything aside from a
901  * symbolic link.
902  *
903  * If the path is invalid then a negative value is returned.  If the
904  * path exists and is a valid mount point path then 0 is returned.
905  * If the path doesn't exist return a positive value.
906  */
907 static int
908 valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
909 {
910 	struct stat statbuf;
911 	char respath[MAXPATHLEN];
912 	int res;
913 
914 	if (lstat(path, &statbuf) != 0) {
915 		if (errno == ENOENT)
916 			return (1);
917 		zerror(zlogp, B_TRUE, "can't stat %s", path);
918 		return (-1);
919 	}
920 	if (S_ISLNK(statbuf.st_mode)) {
921 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
922 		return (-1);
923 	}
924 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
925 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
926 		return (-1);
927 	}
928 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
929 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
930 		return (-1);
931 	}
932 	respath[res] = '\0';
933 	if (strcmp(path, respath) != 0) {
934 		/*
935 		 * We don't like ".."s, "."s, or "//"s throwing us off
936 		 */
937 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
938 		return (-1);
939 	}
940 	return (0);
941 }
942 
943 /*
944  * Validate a mount point path.  A valid mount point path is an
945  * absolute path that either doesn't exist, or, if it does exists it
946  * must be an absolute canonical path that doesn't have any symbolic
947  * links in it.  The target of a mount point path can be any filesystem
948  * object.  (Different filesystems can support different mount points,
949  * for example "lofs" and "mntfs" both support files and directories
950  * while "ufs" just supports directories.)
951  *
952  * If the path is invalid then a negative value is returned.  If the
953  * path exists and is a valid mount point path then 0 is returned.
954  * If the path doesn't exist return a positive value.
955  */
956 int
957 valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
958     const char *dir, const char *fstype)
959 {
960 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
961 	int rv;
962 
963 	/*
964 	 * Sanity check the target mount point path.
965 	 * It must be a non-null string that starts with a '/'.
966 	 */
967 	if (dir[0] != '/') {
968 		if (spec[0] == '\0') {
969 			/*
970 			 * This must be an invalid ipd entry (see comments
971 			 * in mount_filesystems_ipdent()).
972 			 */
973 			zerror(zlogp, B_FALSE,
974 			    "invalid inherit-pkg-dir entry: \"%s\"", dir);
975 		} else {
976 			/* Something went wrong. */
977 			zerror(zlogp, B_FALSE, "invalid mount directory, "
978 			    "type: \"%s\", special: \"%s\", dir: \"%s\"",
979 			    fstype, spec, dir);
980 		}
981 		return (-1);
982 	}
983 
984 	/*
985 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
986 	 * is added to all paths (even non-directory paths) to allow us
987 	 * to detect the end of paths below.  If the path already ends
988 	 * in a '/', then that's ok too (although we'll fail the
989 	 * cannonical path check in valid_mount_point()).
990 	 */
991 	if (snprintf(abspath, sizeof (abspath),
992 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
993 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
994 		    rootpath, dir);
995 		return (-1);
996 	}
997 
998 	/*
999 	 * Starting with rootpath, verify the mount path one component
1000 	 * at a time.  Continue until we've evaluated all of abspath.
1001 	 */
1002 	slashp = &abspath[strlen(rootpath)];
1003 	assert(*slashp == '/');
1004 	do {
1005 		slashp_next = strchr(slashp + 1, '/');
1006 		*slashp = '\0';
1007 		if (slashp_next != NULL) {
1008 			/* This is an intermediary mount path component. */
1009 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1010 		} else {
1011 			/* This is the last component of the mount path. */
1012 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1013 		}
1014 		if (rv < 0)
1015 			return (rv);
1016 		*slashp = '/';
1017 	} while ((slashp = slashp_next) != NULL);
1018 	return (rv);
1019 }
1020 
1021 static int
1022 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
1023 {
1024 	di_prof_t prof = arg;
1025 
1026 	if (name == NULL)
1027 		return (di_prof_add_dev(prof, match));
1028 	return (di_prof_add_map(prof, match, name));
1029 }
1030 
1031 static int
1032 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
1033 {
1034 	di_prof_t prof = arg;
1035 
1036 	return (di_prof_add_symlink(prof, source, target));
1037 }
1038 
1039 static int
1040 get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1041 {
1042 	zone_dochandle_t handle;
1043 
1044 	if ((handle = zonecfg_init_handle()) == NULL) {
1045 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1046 		return (-1);
1047 	}
1048 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1049 		zerror(zlogp, B_FALSE, "invalid configuration");
1050 		zonecfg_fini_handle(handle);
1051 		return (-1);
1052 	}
1053 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1054 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1055 		zonecfg_fini_handle(handle);
1056 		return (-1);
1057 	}
1058 	zonecfg_fini_handle(handle);
1059 	return (0);
1060 }
1061 
1062 /*
1063  * Apply the standard lists of devices/symlinks/mappings and the user-specified
1064  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
1065  * use these as a profile/filter to determine what exists in /dev.
1066  */
1067 static int
1068 mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
1069 {
1070 	char			brand[MAXNAMELEN];
1071 	zone_dochandle_t	handle = NULL;
1072 	brand_handle_t		bh = NULL;
1073 	struct zone_devtab	ztab;
1074 	di_prof_t		prof = NULL;
1075 	int			err;
1076 	int			retval = -1;
1077 	zone_iptype_t		iptype;
1078 	const char 		*curr_iptype;
1079 
1080 	if (di_prof_init(devpath, &prof)) {
1081 		zerror(zlogp, B_TRUE, "failed to initialize profile");
1082 		goto cleanup;
1083 	}
1084 
1085 	/*
1086 	 * Get a handle to the brand info for this zone.
1087 	 * If we are mounting the zone, then we must always use the native
1088 	 * brand device mounts.
1089 	 */
1090 	if (ALT_MOUNT(mount_cmd)) {
1091 		(void) strlcpy(brand, NATIVE_BRAND_NAME, sizeof (brand));
1092 	} else {
1093 		if (zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) {
1094 			zerror(zlogp, B_FALSE,
1095 			    "unable to determine zone brand");
1096 			goto cleanup;
1097 		}
1098 	}
1099 
1100 	if ((bh = brand_open(brand)) == NULL) {
1101 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1102 		goto cleanup;
1103 	}
1104 
1105 	if (get_iptype(zlogp, &iptype) < 0) {
1106 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1107 		goto cleanup;
1108 	}
1109 	switch (iptype) {
1110 	case ZS_SHARED:
1111 		curr_iptype = "shared";
1112 		break;
1113 	case ZS_EXCLUSIVE:
1114 		curr_iptype = "exclusive";
1115 		break;
1116 	}
1117 
1118 	if (brand_platform_iter_devices(bh, zone_name,
1119 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
1120 		zerror(zlogp, B_TRUE, "failed to add standard device");
1121 		goto cleanup;
1122 	}
1123 
1124 	if (brand_platform_iter_link(bh,
1125 	    mount_one_dev_symlink_cb, prof) != 0) {
1126 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
1127 		goto cleanup;
1128 	}
1129 
1130 	/* Add user-specified devices and directories */
1131 	if ((handle = zonecfg_init_handle()) == NULL) {
1132 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
1133 		goto cleanup;
1134 	}
1135 	if (err = zonecfg_get_handle(zone_name, handle)) {
1136 		zerror(zlogp, B_FALSE, "can't get handle for zone "
1137 		    "%s: %s", zone_name, zonecfg_strerror(err));
1138 		goto cleanup;
1139 	}
1140 	if (err = zonecfg_setdevent(handle)) {
1141 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
1142 		    zonecfg_strerror(err));
1143 		goto cleanup;
1144 	}
1145 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
1146 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
1147 			zerror(zlogp, B_TRUE, "failed to add "
1148 			    "user-specified device");
1149 			goto cleanup;
1150 		}
1151 	}
1152 	(void) zonecfg_enddevent(handle);
1153 
1154 	/* Send profile to kernel */
1155 	if (di_prof_commit(prof)) {
1156 		zerror(zlogp, B_TRUE, "failed to commit profile");
1157 		goto cleanup;
1158 	}
1159 
1160 	retval = 0;
1161 
1162 cleanup:
1163 	if (bh != NULL)
1164 		brand_close(bh);
1165 	if (handle != NULL)
1166 		zonecfg_fini_handle(handle);
1167 	if (prof)
1168 		di_prof_fini(prof);
1169 	return (retval);
1170 }
1171 
1172 static int
1173 mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
1174     zone_mnt_t mount_cmd)
1175 {
1176 	char path[MAXPATHLEN];
1177 	char specpath[MAXPATHLEN];
1178 	char optstr[MAX_MNTOPT_STR];
1179 	zone_fsopt_t *optptr;
1180 	int rv;
1181 
1182 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1183 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
1184 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
1185 		    rootpath, fsptr->zone_fs_dir);
1186 		return (-1);
1187 	} else if (rv > 0) {
1188 		/* The mount point path doesn't exist, create it now. */
1189 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1190 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1191 		    DEFAULT_DIR_GROUP) != 0) {
1192 			zerror(zlogp, B_FALSE, "failed to create mount point");
1193 			return (-1);
1194 		}
1195 
1196 		/*
1197 		 * Now this might seem weird, but we need to invoke
1198 		 * valid_mount_path() again.  Why?  Because it checks
1199 		 * to make sure that the mount point path is canonical,
1200 		 * which it can only do if the path exists, so now that
1201 		 * we've created the path we have to verify it again.
1202 		 */
1203 		if ((rv = valid_mount_path(zlogp, rootpath,
1204 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1205 		    fsptr->zone_fs_type)) < 0) {
1206 			zerror(zlogp, B_FALSE,
1207 			    "%s%s is not a valid mount point",
1208 			    rootpath, fsptr->zone_fs_dir);
1209 			return (-1);
1210 		}
1211 	}
1212 
1213 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
1214 	    fsptr->zone_fs_dir);
1215 
1216 	if (strlen(fsptr->zone_fs_special) == 0) {
1217 		/*
1218 		 * A zero-length special is how we distinguish IPDs from
1219 		 * general-purpose FSs.  Make sure it mounts from a place that
1220 		 * can be seen via the alternate zone's root.
1221 		 */
1222 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1223 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1224 		    sizeof (specpath)) {
1225 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1226 			    "long in alternate root", fsptr->zone_fs_dir);
1227 			return (-1);
1228 		}
1229 		if (zonecfg_in_alt_root())
1230 			resolve_lofs(zlogp, specpath, sizeof (specpath));
1231 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1232 		    specpath, path) != 0) {
1233 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1234 			    specpath);
1235 			return (-1);
1236 		}
1237 		return (0);
1238 	}
1239 
1240 	/*
1241 	 * In general the strategy here is to do just as much verification as
1242 	 * necessary to avoid crashing or otherwise doing something bad; if the
1243 	 * administrator initiated the operation via zoneadm(1m), he'll get
1244 	 * auto-verification which will let him know what's wrong.  If he
1245 	 * modifies the zone configuration of a running zone and doesn't attempt
1246 	 * to verify that it's OK we won't crash but won't bother trying to be
1247 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
1248 	 */
1249 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
1250 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
1251 		    "invalid file-system type %s", fsptr->zone_fs_special,
1252 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
1253 		return (-1);
1254 	}
1255 
1256 	/*
1257 	 * If we're looking at an alternate root environment, then construct
1258 	 * read-only loopback mounts as necessary.  Note that any special
1259 	 * paths for lofs zone mounts in an alternate root must have
1260 	 * already been pre-pended with any alternate root path by the
1261 	 * time we get here.
1262 	 */
1263 	if (zonecfg_in_alt_root()) {
1264 		struct stat64 st;
1265 
1266 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
1267 		    S_ISBLK(st.st_mode)) {
1268 			/*
1269 			 * If we're going to mount a block device we need
1270 			 * to check if that device is already mounted
1271 			 * somewhere else, and if so, do a lofs mount
1272 			 * of the device instead of a direct mount
1273 			 */
1274 			if (check_lofs_needed(zlogp, fsptr) == -1)
1275 				return (-1);
1276 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1277 			/*
1278 			 * For lofs mounts, the special node is inside the
1279 			 * alternate root.  We need lofs resolution for
1280 			 * this case in order to get at the underlying
1281 			 * read-write path.
1282 			 */
1283 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1284 			    sizeof (fsptr->zone_fs_special));
1285 		}
1286 	}
1287 
1288 	/*
1289 	 * Run 'fsck -m' if there's a device to fsck.
1290 	 */
1291 	if (fsptr->zone_fs_raw[0] != '\0' &&
1292 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
1293 		return (-1);
1294 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
1295 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
1296 		return (-1);
1297 	}
1298 
1299 	/*
1300 	 * Build up mount option string.
1301 	 */
1302 	optstr[0] = '\0';
1303 	if (fsptr->zone_fs_options != NULL) {
1304 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
1305 		    sizeof (optstr));
1306 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
1307 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
1308 			(void) strlcat(optstr, ",", sizeof (optstr));
1309 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
1310 			    sizeof (optstr));
1311 		}
1312 	}
1313 
1314 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
1315 	    fsptr->zone_fs_special, path)) != 0)
1316 		return (rv);
1317 
1318 	/*
1319 	 * The mount succeeded.  If this was not a mount of /dev then
1320 	 * we're done.
1321 	 */
1322 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
1323 		return (0);
1324 
1325 	/*
1326 	 * We just mounted an instance of a /dev filesystem, so now we
1327 	 * need to configure it.
1328 	 */
1329 	return (mount_one_dev(zlogp, path, mount_cmd));
1330 }
1331 
1332 static void
1333 free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
1334 {
1335 	uint_t i;
1336 
1337 	if (fsarray == NULL)
1338 		return;
1339 	for (i = 0; i < nelem; i++)
1340 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
1341 	free(fsarray);
1342 }
1343 
1344 /*
1345  * This function initiates the creation of a small Solaris Environment for
1346  * scratch zone. The Environment creation process is split up into two
1347  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1348  * is done this way because:
1349  * 	We need to have both /etc and /var in the root of the scratchzone.
1350  * 	We loopback mount zone's own /etc and /var into the root of the
1351  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1352  * 	need to delay the mount of /var till the zone's root gets populated.
1353  *	So mounting of localdirs[](/etc and /var) have been moved to the
1354  * 	build_mounted_post_var() which gets called only after the zone
1355  * 	specific filesystems are mounted.
1356  *
1357  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
1358  * does not loopback mount the zone's own /etc and /var into the root of the
1359  * scratch zone.
1360  */
1361 static boolean_t
1362 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
1363     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1364 {
1365 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1366 	const char **cpp;
1367 	static const char *mkdirs[] = {
1368 		"/system", "/system/contract", "/system/object", "/proc",
1369 		"/dev", "/tmp", "/a", NULL
1370 	};
1371 	char *altstr;
1372 	FILE *fp;
1373 	uuid_t uuid;
1374 
1375 	resolve_lofs(zlogp, rootpath, rootlen);
1376 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
1377 	resolve_lofs(zlogp, luroot, lurootlen);
1378 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1379 	(void) symlink("./usr/bin", tmp);
1380 
1381 	/*
1382 	 * These are mostly special mount points; not handled here.  (See
1383 	 * zone_mount_early.)
1384 	 */
1385 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1386 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1387 		if (mkdir(tmp, 0755) != 0) {
1388 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1389 			return (B_FALSE);
1390 		}
1391 	}
1392 	/*
1393 	 * This is here to support lucopy.  If there's an instance of this same
1394 	 * zone on the current running system, then we mount its root up as
1395 	 * read-only inside the scratch zone.
1396 	 */
1397 	(void) zonecfg_get_uuid(zone_name, uuid);
1398 	altstr = strdup(zonecfg_get_root());
1399 	if (altstr == NULL) {
1400 		zerror(zlogp, B_TRUE, "memory allocation failed");
1401 		return (B_FALSE);
1402 	}
1403 	zonecfg_set_root("");
1404 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1405 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1406 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1407 	    strcmp(fromdir, rootpath) != 0) {
1408 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1409 		if (mkdir(tmp, 0755) != 0) {
1410 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1411 			return (B_FALSE);
1412 		}
1413 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1414 		    tmp) != 0) {
1415 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1416 			    fromdir);
1417 			return (B_FALSE);
1418 		}
1419 	}
1420 	zonecfg_set_root(altstr);
1421 	free(altstr);
1422 
1423 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1424 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1425 		return (B_FALSE);
1426 	}
1427 	(void) ftruncate(fileno(fp), 0);
1428 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1429 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1430 	}
1431 	zonecfg_close_scratch(fp);
1432 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1433 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1434 		return (B_FALSE);
1435 	(void) strlcpy(rootpath, tmp, rootlen);
1436 	return (B_TRUE);
1437 }
1438 
1439 
1440 static boolean_t
1441 build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
1442     const char *luroot)
1443 {
1444 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1445 	const char **cpp;
1446 	const char **loopdirs;
1447 	const char **tmpdirs;
1448 	static const char *localdirs[] = {
1449 		"/etc", "/var", NULL
1450 	};
1451 	static const char *scr_loopdirs[] = {
1452 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1453 		"/usr", NULL
1454 	};
1455 	static const char *upd_loopdirs[] = {
1456 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
1457 		"/usr", "/var", NULL
1458 	};
1459 	static const char *scr_tmpdirs[] = {
1460 		"/tmp", "/var/run", NULL
1461 	};
1462 	static const char *upd_tmpdirs[] = {
1463 		"/tmp", "/var/run", "/var/tmp", NULL
1464 	};
1465 	struct stat st;
1466 
1467 	if (mount_cmd == Z_MNT_SCRATCH) {
1468 		/*
1469 		 * These are mounted read-write from the zone undergoing
1470 		 * upgrade.  We must be careful not to 'leak' things from the
1471 		 * main system into the zone, and this accomplishes that goal.
1472 		 */
1473 		for (cpp = localdirs; *cpp != NULL; cpp++) {
1474 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1475 			    *cpp);
1476 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
1477 			    rootpath, *cpp);
1478 			if (mkdir(tmp, 0755) != 0) {
1479 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1480 				return (B_FALSE);
1481 			}
1482 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
1483 			    != 0) {
1484 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
1485 				    tmp, *cpp);
1486 				return (B_FALSE);
1487 			}
1488 		}
1489 	}
1490 
1491 	if (mount_cmd == Z_MNT_UPDATE)
1492 		loopdirs = upd_loopdirs;
1493 	else
1494 		loopdirs = scr_loopdirs;
1495 
1496 	/*
1497 	 * These are things mounted read-only from the running system because
1498 	 * they contain binaries that must match system.
1499 	 */
1500 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1501 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1502 		if (mkdir(tmp, 0755) != 0) {
1503 			if (errno != EEXIST) {
1504 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1505 				return (B_FALSE);
1506 			}
1507 			if (lstat(tmp, &st) != 0) {
1508 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1509 				return (B_FALSE);
1510 			}
1511 			/*
1512 			 * Ignore any non-directories encountered.  These are
1513 			 * things that have been converted into symlinks
1514 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1515 			 * fixup.
1516 			 */
1517 			if (!S_ISDIR(st.st_mode))
1518 				continue;
1519 		}
1520 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1521 		    tmp) != 0) {
1522 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1523 			    *cpp);
1524 			return (B_FALSE);
1525 		}
1526 	}
1527 
1528 	if (mount_cmd == Z_MNT_UPDATE)
1529 		tmpdirs = upd_tmpdirs;
1530 	else
1531 		tmpdirs = scr_tmpdirs;
1532 
1533 	/*
1534 	 * These are things with tmpfs mounted inside.
1535 	 */
1536 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1537 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1538 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
1539 		    errno != EEXIST) {
1540 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1541 			return (B_FALSE);
1542 		}
1543 
1544 		/*
1545 		 * We could set the mode for /tmp when we do the mkdir but
1546 		 * since that can be modified by the umask we will just set
1547 		 * the correct mode for /tmp now.
1548 		 */
1549 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1550 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1551 			return (B_FALSE);
1552 		}
1553 
1554 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1555 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1556 			return (B_FALSE);
1557 		}
1558 	}
1559 	return (B_TRUE);
1560 }
1561 
1562 typedef struct plat_gmount_cb_data {
1563 	zlog_t			*pgcd_zlogp;
1564 	struct zone_fstab	**pgcd_fs_tab;
1565 	int			*pgcd_num_fs;
1566 } plat_gmount_cb_data_t;
1567 
1568 /*
1569  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
1570  * through all global brand platform mounts.
1571  */
1572 int
1573 plat_gmount_cb(void *data, const char *spec, const char *dir,
1574     const char *fstype, const char *opt)
1575 {
1576 	plat_gmount_cb_data_t	*cp = data;
1577 	zlog_t			*zlogp = cp->pgcd_zlogp;
1578 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
1579 	int			num_fs = *cp->pgcd_num_fs;
1580 	struct zone_fstab	*fsp, *tmp_ptr;
1581 
1582 	num_fs++;
1583 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
1584 		zerror(zlogp, B_TRUE, "memory allocation failed");
1585 		return (-1);
1586 	}
1587 
1588 	fs_ptr = tmp_ptr;
1589 	fsp = &fs_ptr[num_fs - 1];
1590 
1591 	/* update the callback struct passed in */
1592 	*cp->pgcd_fs_tab = fs_ptr;
1593 	*cp->pgcd_num_fs = num_fs;
1594 
1595 	fsp->zone_fs_raw[0] = '\0';
1596 	(void) strlcpy(fsp->zone_fs_special, spec,
1597 	    sizeof (fsp->zone_fs_special));
1598 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
1599 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
1600 	fsp->zone_fs_options = NULL;
1601 	if ((opt != NULL) &&
1602 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
1603 		zerror(zlogp, B_FALSE, "error adding property");
1604 		return (-1);
1605 	}
1606 
1607 	return (0);
1608 }
1609 
1610 static int
1611 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
1612     struct zone_fstab **fs_tabp, int *num_fsp)
1613 {
1614 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
1615 	int num_fs;
1616 
1617 	num_fs = *num_fsp;
1618 	fs_ptr = *fs_tabp;
1619 
1620 	if (zonecfg_setipdent(handle) != Z_OK) {
1621 		zerror(zlogp, B_FALSE, "invalid configuration");
1622 		return (-1);
1623 	}
1624 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
1625 		num_fs++;
1626 		if ((tmp_ptr = realloc(fs_ptr,
1627 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
1628 			zerror(zlogp, B_TRUE, "memory allocation failed");
1629 			(void) zonecfg_endipdent(handle);
1630 			return (-1);
1631 		}
1632 
1633 		/* update the pointers passed in */
1634 		*fs_tabp = tmp_ptr;
1635 		*num_fsp = num_fs;
1636 
1637 		/*
1638 		 * IPDs logically only have a mount point; all other properties
1639 		 * are implied.
1640 		 */
1641 		fs_ptr = tmp_ptr;
1642 		fsp = &fs_ptr[num_fs - 1];
1643 		(void) strlcpy(fsp->zone_fs_dir,
1644 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
1645 		fsp->zone_fs_special[0] = '\0';
1646 		fsp->zone_fs_raw[0] = '\0';
1647 		fsp->zone_fs_type[0] = '\0';
1648 		fsp->zone_fs_options = NULL;
1649 	}
1650 	(void) zonecfg_endipdent(handle);
1651 	return (0);
1652 }
1653 
1654 static int
1655 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
1656     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
1657 {
1658 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
1659 	int num_fs;
1660 
1661 	num_fs = *num_fsp;
1662 	fs_ptr = *fs_tabp;
1663 
1664 	if (zonecfg_setfsent(handle) != Z_OK) {
1665 		zerror(zlogp, B_FALSE, "invalid configuration");
1666 		return (-1);
1667 	}
1668 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
1669 		/*
1670 		 * ZFS filesystems will not be accessible under an alternate
1671 		 * root, since the pool will not be known.  Ignore them in this
1672 		 * case.
1673 		 */
1674 		if (ALT_MOUNT(mount_cmd) &&
1675 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
1676 			continue;
1677 
1678 		num_fs++;
1679 		if ((tmp_ptr = realloc(fs_ptr,
1680 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
1681 			zerror(zlogp, B_TRUE, "memory allocation failed");
1682 			(void) zonecfg_endfsent(handle);
1683 			return (-1);
1684 		}
1685 		/* update the pointers passed in */
1686 		*fs_tabp = tmp_ptr;
1687 		*num_fsp = num_fs;
1688 
1689 		fs_ptr = tmp_ptr;
1690 		fsp = &fs_ptr[num_fs - 1];
1691 		(void) strlcpy(fsp->zone_fs_dir,
1692 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
1693 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
1694 		    sizeof (fsp->zone_fs_raw));
1695 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
1696 		    sizeof (fsp->zone_fs_type));
1697 		fsp->zone_fs_options = fstab.zone_fs_options;
1698 
1699 		/*
1700 		 * For all lofs mounts, make sure that the 'special'
1701 		 * entry points inside the alternate root.  The
1702 		 * source path for a lofs mount in a given zone needs
1703 		 * to be relative to the root of the boot environment
1704 		 * that contains the zone.  Note that we don't do this
1705 		 * for non-lofs mounts since they will have a device
1706 		 * as a backing store and device paths must always be
1707 		 * specified relative to the current boot environment.
1708 		 */
1709 		fsp->zone_fs_special[0] = '\0';
1710 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1711 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1712 			    sizeof (fsp->zone_fs_special));
1713 		}
1714 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1715 		    sizeof (fsp->zone_fs_special));
1716 	}
1717 	(void) zonecfg_endfsent(handle);
1718 	return (0);
1719 }
1720 
1721 static int
1722 mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
1723 {
1724 	char rootpath[MAXPATHLEN];
1725 	char zonepath[MAXPATHLEN];
1726 	char brand[MAXNAMELEN];
1727 	char luroot[MAXPATHLEN];
1728 	int i, num_fs = 0;
1729 	struct zone_fstab *fs_ptr = NULL;
1730 	zone_dochandle_t handle = NULL;
1731 	zone_state_t zstate;
1732 	brand_handle_t bh;
1733 	plat_gmount_cb_data_t cb;
1734 
1735 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1736 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
1737 		zerror(zlogp, B_FALSE,
1738 		    "zone must be in '%s' or '%s' state to mount file-systems",
1739 		    zone_state_str(ZONE_STATE_READY),
1740 		    zone_state_str(ZONE_STATE_MOUNTED));
1741 		goto bad;
1742 	}
1743 
1744 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
1745 		zerror(zlogp, B_TRUE, "unable to determine zone path");
1746 		goto bad;
1747 	}
1748 
1749 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
1750 		zerror(zlogp, B_TRUE, "unable to determine zone root");
1751 		goto bad;
1752 	}
1753 
1754 	if ((handle = zonecfg_init_handle()) == NULL) {
1755 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1756 		goto bad;
1757 	}
1758 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
1759 	    zonecfg_setfsent(handle) != Z_OK) {
1760 		zerror(zlogp, B_FALSE, "invalid configuration");
1761 		goto bad;
1762 	}
1763 
1764 	/*
1765 	 * If we are mounting the zone, then we must always use the native
1766 	 * brand global mounts.
1767 	 */
1768 	if (ALT_MOUNT(mount_cmd)) {
1769 		(void) strlcpy(brand, NATIVE_BRAND_NAME, sizeof (brand));
1770 	} else {
1771 		if (zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) {
1772 			zerror(zlogp, B_FALSE,
1773 			    "unable to determine zone brand");
1774 			zonecfg_fini_handle(handle);
1775 			return (-1);
1776 		}
1777 	}
1778 
1779 	/* Get a handle to the brand info for this zone */
1780 	if ((bh = brand_open(brand)) == NULL) {
1781 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1782 		zonecfg_fini_handle(handle);
1783 		return (-1);
1784 	}
1785 
1786 	/*
1787 	 * Get the list of global filesystems to mount from the brand
1788 	 * configuration.
1789 	 */
1790 	cb.pgcd_zlogp = zlogp;
1791 	cb.pgcd_fs_tab = &fs_ptr;
1792 	cb.pgcd_num_fs = &num_fs;
1793 	if (brand_platform_iter_gmounts(bh, zonepath,
1794 	    plat_gmount_cb, &cb) != 0) {
1795 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1796 		brand_close(bh);
1797 		zonecfg_fini_handle(handle);
1798 		return (-1);
1799 	}
1800 	brand_close(bh);
1801 
1802 	/*
1803 	 * Iterate through the rest of the filesystems, first the IPDs, then
1804 	 * the general FSs.  Sort them all, then mount them in sorted order.
1805 	 * This is to make sure the higher level directories (e.g., /usr)
1806 	 * get mounted before any beneath them (e.g., /usr/local).
1807 	 */
1808 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
1809 		goto bad;
1810 
1811 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
1812 	    mount_cmd) != 0)
1813 		goto bad;
1814 
1815 	zonecfg_fini_handle(handle);
1816 	handle = NULL;
1817 
1818 	/*
1819 	 * Normally when we mount a zone all the zone filesystems
1820 	 * get mounted relative to rootpath, which is usually
1821 	 * <zonepath>/root.  But when mounting a zone for administration
1822 	 * purposes via the zone "mount" state, build_mounted_pre_var()
1823 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
1824 	 * the zones filesystems there instead.
1825 	 *
1826 	 * build_mounted_pre_var() and build_mounted_post_var() will
1827 	 * also do some extra work to create directories and lofs mount
1828 	 * a bunch of global zone file system paths into <zonepath>/lu.
1829 	 *
1830 	 * This allows us to be able to enter the zone (now rooted at
1831 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
1832 	 * global zone and have them upgrade the to-be-modified zone's
1833 	 * files mounted on /a.  (Which mirrors the existing standard
1834 	 * upgrade environment.)
1835 	 *
1836 	 * There is of course one catch.  When doing the upgrade
1837 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
1838 	 * for the zone and we don't want to have any /dev filesystem
1839 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
1840 	 * as a normal zone filesystem by default we'll try to mount
1841 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
1842 	 * case and instead mount it at <zoneroot>/lu/dev.
1843 	 *
1844 	 * All this work is done in three phases:
1845 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1846 	 *   2) Mount the required filesystems as per the zone configuration.
1847 	 *   3) Set up the rest of the scratch zone environment
1848 	 *	(build_mounted_post_var()).
1849 	 */
1850 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
1851 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1852 		goto bad;
1853 
1854 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
1855 
1856 	for (i = 0; i < num_fs; i++) {
1857 		if (ALT_MOUNT(mount_cmd) &&
1858 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
1859 			size_t slen = strlen(rootpath) - 2;
1860 
1861 			/*
1862 			 * By default we'll try to mount /dev as /a/dev
1863 			 * but /dev is special and always goes at the top
1864 			 * so strip the trailing '/a' from the rootpath.
1865 			 */
1866 			assert(strcmp(&rootpath[slen], "/a") == 0);
1867 			rootpath[slen] = '\0';
1868 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
1869 			    != 0)
1870 				goto bad;
1871 			rootpath[slen] = '/';
1872 			continue;
1873 		}
1874 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
1875 			goto bad;
1876 	}
1877 	if (ALT_MOUNT(mount_cmd) &&
1878 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1879 		goto bad;
1880 
1881 	/*
1882 	 * For Trusted Extensions cross-mount each lower level /export/home
1883 	 */
1884 	if (mount_cmd == Z_MNT_BOOT &&
1885 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
1886 		goto bad;
1887 
1888 	free_fs_data(fs_ptr, num_fs);
1889 
1890 	/*
1891 	 * Everything looks fine.
1892 	 */
1893 	return (0);
1894 
1895 bad:
1896 	if (handle != NULL)
1897 		zonecfg_fini_handle(handle);
1898 	free_fs_data(fs_ptr, num_fs);
1899 	return (-1);
1900 }
1901 
1902 /* caller makes sure neither parameter is NULL */
1903 static int
1904 addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
1905 {
1906 	int prefixlen;
1907 
1908 	prefixlen = atoi(prefixstr);
1909 	if (prefixlen < 0 || prefixlen > maxprefixlen)
1910 		return (1);
1911 	while (prefixlen > 0) {
1912 		if (prefixlen >= 8) {
1913 			*maskstr++ = 0xFF;
1914 			prefixlen -= 8;
1915 			continue;
1916 		}
1917 		*maskstr |= 1 << (8 - prefixlen);
1918 		prefixlen--;
1919 	}
1920 	return (0);
1921 }
1922 
1923 /*
1924  * Tear down all interfaces belonging to the given zone.  This should
1925  * be called with the zone in a state other than "running", so that
1926  * interfaces can't be assigned to the zone after this returns.
1927  *
1928  * If anything goes wrong, log an error message and return an error.
1929  */
1930 static int
1931 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
1932 {
1933 	struct lifnum lifn;
1934 	struct lifconf lifc;
1935 	struct lifreq *lifrp, lifrl;
1936 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
1937 	int num_ifs, s, i, ret_code = 0;
1938 	uint_t bufsize;
1939 	char *buf = NULL;
1940 
1941 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1942 		zerror(zlogp, B_TRUE, "could not get socket");
1943 		ret_code = -1;
1944 		goto bad;
1945 	}
1946 	lifn.lifn_family = AF_UNSPEC;
1947 	lifn.lifn_flags = (int)lifc_flags;
1948 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
1949 		zerror(zlogp, B_TRUE,
1950 		    "could not determine number of network interfaces");
1951 		ret_code = -1;
1952 		goto bad;
1953 	}
1954 	num_ifs = lifn.lifn_count;
1955 	bufsize = num_ifs * sizeof (struct lifreq);
1956 	if ((buf = malloc(bufsize)) == NULL) {
1957 		zerror(zlogp, B_TRUE, "memory allocation failed");
1958 		ret_code = -1;
1959 		goto bad;
1960 	}
1961 	lifc.lifc_family = AF_UNSPEC;
1962 	lifc.lifc_flags = (int)lifc_flags;
1963 	lifc.lifc_len = bufsize;
1964 	lifc.lifc_buf = buf;
1965 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1966 		zerror(zlogp, B_TRUE, "could not get configured network "
1967 		    "interfaces");
1968 		ret_code = -1;
1969 		goto bad;
1970 	}
1971 	lifrp = lifc.lifc_req;
1972 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
1973 		(void) close(s);
1974 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
1975 		    0) {
1976 			zerror(zlogp, B_TRUE, "%s: could not get socket",
1977 			    lifrl.lifr_name);
1978 			ret_code = -1;
1979 			continue;
1980 		}
1981 		(void) memset(&lifrl, 0, sizeof (lifrl));
1982 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
1983 		    sizeof (lifrl.lifr_name));
1984 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1985 			if (errno == ENXIO)
1986 				/*
1987 				 * Interface may have been removed by admin or
1988 				 * another zone halting.
1989 				 */
1990 				continue;
1991 			zerror(zlogp, B_TRUE,
1992 			    "%s: could not determine the zone to which this "
1993 			    "network interface is bound", lifrl.lifr_name);
1994 			ret_code = -1;
1995 			continue;
1996 		}
1997 		if (lifrl.lifr_zoneid == zone_id) {
1998 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
1999 				zerror(zlogp, B_TRUE,
2000 				    "%s: could not remove network interface",
2001 				    lifrl.lifr_name);
2002 				ret_code = -1;
2003 				continue;
2004 			}
2005 		}
2006 	}
2007 bad:
2008 	if (s > 0)
2009 		(void) close(s);
2010 	if (buf)
2011 		free(buf);
2012 	return (ret_code);
2013 }
2014 
2015 static union	sockunion {
2016 	struct	sockaddr sa;
2017 	struct	sockaddr_in sin;
2018 	struct	sockaddr_dl sdl;
2019 	struct	sockaddr_in6 sin6;
2020 } so_dst, so_ifp;
2021 
2022 static struct {
2023 	struct	rt_msghdr hdr;
2024 	char	space[512];
2025 } rtmsg;
2026 
2027 static int
2028 salen(struct sockaddr *sa)
2029 {
2030 	switch (sa->sa_family) {
2031 	case AF_INET:
2032 		return (sizeof (struct sockaddr_in));
2033 	case AF_LINK:
2034 		return (sizeof (struct sockaddr_dl));
2035 	case AF_INET6:
2036 		return (sizeof (struct sockaddr_in6));
2037 	default:
2038 		return (sizeof (struct sockaddr));
2039 	}
2040 }
2041 
2042 #define	ROUNDUP_LONG(a) \
2043 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
2044 
2045 /*
2046  * Look up which zone is using a given IP address.  The address in question
2047  * is expected to have been stuffed into the structure to which lifr points
2048  * via a previous SIOCGLIFADDR ioctl().
2049  *
2050  * This is done using black router socket magic.
2051  *
2052  * Return the name of the zone on success or NULL on failure.
2053  *
2054  * This is a lot of code for a simple task; a new ioctl request to take care
2055  * of this might be a useful RFE.
2056  */
2057 
2058 static char *
2059 who_is_using(zlog_t *zlogp, struct lifreq *lifr)
2060 {
2061 	static char answer[ZONENAME_MAX];
2062 	pid_t pid;
2063 	int s, rlen, l, i;
2064 	char *cp = rtmsg.space;
2065 	struct sockaddr_dl *ifp = NULL;
2066 	struct sockaddr *sa;
2067 	char save_if_name[LIFNAMSIZ];
2068 
2069 	answer[0] = '\0';
2070 
2071 	pid = getpid();
2072 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
2073 		zerror(zlogp, B_TRUE, "could not get routing socket");
2074 		return (NULL);
2075 	}
2076 
2077 	if (lifr->lifr_addr.ss_family == AF_INET) {
2078 		struct sockaddr_in *sin4;
2079 
2080 		so_dst.sa.sa_family = AF_INET;
2081 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
2082 		so_dst.sin.sin_addr = sin4->sin_addr;
2083 	} else {
2084 		struct sockaddr_in6 *sin6;
2085 
2086 		so_dst.sa.sa_family = AF_INET6;
2087 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
2088 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
2089 	}
2090 
2091 	so_ifp.sa.sa_family = AF_LINK;
2092 
2093 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
2094 	rtmsg.hdr.rtm_type = RTM_GET;
2095 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
2096 	rtmsg.hdr.rtm_version = RTM_VERSION;
2097 	rtmsg.hdr.rtm_seq = ++rts_seqno;
2098 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
2099 
2100 	l = ROUNDUP_LONG(salen(&so_dst.sa));
2101 	(void) memmove(cp, &(so_dst), l);
2102 	cp += l;
2103 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
2104 	(void) memmove(cp, &(so_ifp), l);
2105 	cp += l;
2106 
2107 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
2108 
2109 	if ((rlen = write(s, &rtmsg, l)) < 0) {
2110 		zerror(zlogp, B_TRUE, "writing to routing socket");
2111 		return (NULL);
2112 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
2113 		zerror(zlogp, B_TRUE,
2114 		    "write to routing socket got only %d for len\n", rlen);
2115 		return (NULL);
2116 	}
2117 	do {
2118 		l = read(s, &rtmsg, sizeof (rtmsg));
2119 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
2120 	    rtmsg.hdr.rtm_pid != pid));
2121 	if (l < 0) {
2122 		zerror(zlogp, B_TRUE, "reading from routing socket");
2123 		return (NULL);
2124 	}
2125 
2126 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
2127 		zerror(zlogp, B_FALSE,
2128 		    "routing message version %d not understood",
2129 		    rtmsg.hdr.rtm_version);
2130 		return (NULL);
2131 	}
2132 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
2133 		zerror(zlogp, B_FALSE, "message length mismatch, "
2134 		    "expected %d bytes, returned %d bytes",
2135 		    rtmsg.hdr.rtm_msglen, l);
2136 		return (NULL);
2137 	}
2138 	if (rtmsg.hdr.rtm_errno != 0)  {
2139 		errno = rtmsg.hdr.rtm_errno;
2140 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
2141 		return (NULL);
2142 	}
2143 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2144 		zerror(zlogp, B_FALSE, "network interface not found");
2145 		return (NULL);
2146 	}
2147 	cp = ((char *)(&rtmsg.hdr + 1));
2148 	for (i = 1; i != 0; i <<= 1) {
2149 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2150 		sa = (struct sockaddr *)cp;
2151 		if (i != RTA_IFP) {
2152 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
2153 				cp += ROUNDUP_LONG(salen(sa));
2154 			continue;
2155 		}
2156 		if (sa->sa_family == AF_LINK &&
2157 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
2158 			ifp = (struct sockaddr_dl *)sa;
2159 		break;
2160 	}
2161 	if (ifp == NULL) {
2162 		zerror(zlogp, B_FALSE, "network interface could not be "
2163 		    "determined");
2164 		return (NULL);
2165 	}
2166 
2167 	/*
2168 	 * We need to set the I/F name to what we got above, then do the
2169 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
2170 	 * used by the calling function to do a REMOVEIF, so if we leave the
2171 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
2172 	 * of the bad one.  So we save the old (bad) I/F name before over-
2173 	 * writing it and doing the ioctl, then restore it after the ioctl.
2174 	 */
2175 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
2176 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
2177 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
2178 	i = ioctl(s, SIOCGLIFZONE, lifr);
2179 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
2180 	if (i < 0) {
2181 		zerror(zlogp, B_TRUE,
2182 		    "%s: could not determine the zone network interface "
2183 		    "belongs to", lifr->lifr_name);
2184 		return (NULL);
2185 	}
2186 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
2187 		(void) snprintf(answer, sizeof (answer), "%d",
2188 		    lifr->lifr_zoneid);
2189 
2190 	if (strlen(answer) > 0)
2191 		return (answer);
2192 	return (NULL);
2193 }
2194 
2195 typedef struct mcast_rtmsg_s {
2196 	struct rt_msghdr	m_rtm;
2197 	union {
2198 		struct {
2199 			struct sockaddr_in	m_dst;
2200 			struct sockaddr_in	m_gw;
2201 			struct sockaddr_in	m_netmask;
2202 		} m_v4;
2203 		struct {
2204 			struct sockaddr_in6	m_dst;
2205 			struct sockaddr_in6	m_gw;
2206 			struct sockaddr_in6	m_netmask;
2207 		} m_v6;
2208 	} m_u;
2209 } mcast_rtmsg_t;
2210 #define	m_dst4		m_u.m_v4.m_dst
2211 #define	m_dst6		m_u.m_v6.m_dst
2212 #define	m_gw4		m_u.m_v4.m_gw
2213 #define	m_gw6		m_u.m_v6.m_gw
2214 #define	m_netmask4	m_u.m_v4.m_netmask
2215 #define	m_netmask6	m_u.m_v6.m_netmask
2216 
2217 /*
2218  * Configures a single interface: a new virtual interface is added, based on
2219  * the physical interface nwiftabptr->zone_nwif_physical, with the address
2220  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
2221  * the "address" can be an IPv6 address (with a /prefixlength required), an
2222  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
2223  * an IPv4 name-to-address resolution will be attempted.
2224  *
2225  * A default interface route for multicast is created on the first IPv4 and
2226  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
2227  * This should really be done in the init scripts if we ever allow zones to
2228  * modify the routing tables.
2229  *
2230  * If anything goes wrong, we log an detailed error message, attempt to tear
2231  * down whatever we set up and return an error.
2232  */
2233 static int
2234 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2235     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
2236     boolean_t *mcast_rt_v6_setp)
2237 {
2238 	struct lifreq lifr;
2239 	struct sockaddr_in netmask4;
2240 	struct sockaddr_in6 netmask6;
2241 	struct in_addr in4;
2242 	struct in6_addr in6;
2243 	sa_family_t af;
2244 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
2245 	mcast_rtmsg_t mcast_rtmsg;
2246 	int s;
2247 	int rs;
2248 	int rlen;
2249 	boolean_t got_netmask = B_FALSE;
2250 	char addrstr4[INET_ADDRSTRLEN];
2251 	int res;
2252 
2253 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
2254 	if (res != Z_OK) {
2255 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
2256 		    nwiftabptr->zone_nwif_address);
2257 		return (-1);
2258 	}
2259 	af = lifr.lifr_addr.ss_family;
2260 	if (af == AF_INET)
2261 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
2262 	else
2263 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
2264 
2265 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
2266 		zerror(zlogp, B_TRUE, "could not get socket");
2267 		return (-1);
2268 	}
2269 
2270 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
2271 	    sizeof (lifr.lifr_name));
2272 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
2273 		/*
2274 		 * Here, we know that the interface can't be brought up.
2275 		 * A similar warning message was already printed out to
2276 		 * the console by zoneadm(1M) so instead we log the
2277 		 * message to syslog and continue.
2278 		 */
2279 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
2280 		    "'%s' which may not be present/plumbed in the "
2281 		    "global zone.", lifr.lifr_name);
2282 		(void) close(s);
2283 		return (Z_OK);
2284 	}
2285 
2286 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2287 		zerror(zlogp, B_TRUE,
2288 		    "%s: could not set IP address to %s",
2289 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
2290 		goto bad;
2291 	}
2292 
2293 	/* Preserve literal IPv4 address for later potential printing. */
2294 	if (af == AF_INET)
2295 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
2296 
2297 	lifr.lifr_zoneid = zone_id;
2298 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2299 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2300 		    "into zone", lifr.lifr_name);
2301 		goto bad;
2302 	}
2303 
2304 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2305 		got_netmask = B_TRUE;	/* default setting will be correct */
2306 	} else {
2307 		if (af == AF_INET) {
2308 			/*
2309 			 * The IPv4 netmask can be determined either
2310 			 * directly if a prefix length was supplied with
2311 			 * the address or via the netmasks database.  Not
2312 			 * being able to determine it is a common failure,
2313 			 * but it often is not fatal to operation of the
2314 			 * interface.  In that case, a warning will be
2315 			 * printed after the rest of the interface's
2316 			 * parameters have been configured.
2317 			 */
2318 			(void) memset(&netmask4, 0, sizeof (netmask4));
2319 			if (slashp != NULL) {
2320 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
2321 				    (uchar_t *)&netmask4.sin_addr) != 0) {
2322 					*slashp = '/';
2323 					zerror(zlogp, B_FALSE,
2324 					    "%s: invalid prefix length in %s",
2325 					    lifr.lifr_name,
2326 					    nwiftabptr->zone_nwif_address);
2327 					goto bad;
2328 				}
2329 				got_netmask = B_TRUE;
2330 			} else if (getnetmaskbyaddr(in4,
2331 			    &netmask4.sin_addr) == 0) {
2332 				got_netmask = B_TRUE;
2333 			}
2334 			if (got_netmask) {
2335 				netmask4.sin_family = af;
2336 				(void) memcpy(&lifr.lifr_addr, &netmask4,
2337 				    sizeof (netmask4));
2338 			}
2339 		} else {
2340 			(void) memset(&netmask6, 0, sizeof (netmask6));
2341 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
2342 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
2343 				*slashp = '/';
2344 				zerror(zlogp, B_FALSE,
2345 				    "%s: invalid prefix length in %s",
2346 				    lifr.lifr_name,
2347 				    nwiftabptr->zone_nwif_address);
2348 				goto bad;
2349 			}
2350 			got_netmask = B_TRUE;
2351 			netmask6.sin6_family = af;
2352 			(void) memcpy(&lifr.lifr_addr, &netmask6,
2353 			    sizeof (netmask6));
2354 		}
2355 		if (got_netmask &&
2356 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
2357 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
2358 			    lifr.lifr_name);
2359 			goto bad;
2360 		}
2361 
2362 		/*
2363 		 * This doesn't set the broadcast address at all. Rather, it
2364 		 * gets, then sets the interface's address, relying on the fact
2365 		 * that resetting the address will reset the broadcast address.
2366 		 */
2367 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
2368 			zerror(zlogp, B_TRUE, "%s: could not get address",
2369 			    lifr.lifr_name);
2370 			goto bad;
2371 		}
2372 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2373 			zerror(zlogp, B_TRUE,
2374 			    "%s: could not reset broadcast address",
2375 			    lifr.lifr_name);
2376 			goto bad;
2377 		}
2378 	}
2379 
2380 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2381 		zerror(zlogp, B_TRUE, "%s: could not get flags",
2382 		    lifr.lifr_name);
2383 		goto bad;
2384 	}
2385 	lifr.lifr_flags |= IFF_UP;
2386 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
2387 		int save_errno = errno;
2388 		char *zone_using;
2389 
2390 		/*
2391 		 * If we failed with something other than EADDRNOTAVAIL,
2392 		 * then skip to the end.  Otherwise, look up our address,
2393 		 * then call a function to determine which zone is already
2394 		 * using that address.
2395 		 */
2396 		if (errno != EADDRNOTAVAIL) {
2397 			zerror(zlogp, B_TRUE,
2398 			    "%s: could not bring network interface up",
2399 			    lifr.lifr_name);
2400 			goto bad;
2401 		}
2402 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
2403 			zerror(zlogp, B_TRUE, "%s: could not get address",
2404 			    lifr.lifr_name);
2405 			goto bad;
2406 		}
2407 		zone_using = who_is_using(zlogp, &lifr);
2408 		errno = save_errno;
2409 		if (zone_using == NULL)
2410 			zerror(zlogp, B_TRUE,
2411 			    "%s: could not bring network interface up",
2412 			    lifr.lifr_name);
2413 		else
2414 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2415 			    "interface up: address in use by zone '%s'",
2416 			    lifr.lifr_name, zone_using);
2417 		goto bad;
2418 	}
2419 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
2420 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
2421 	    (af == AF_INET6 &&
2422 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
2423 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
2424 		if (rs < 0) {
2425 			zerror(zlogp, B_TRUE, "%s: could not create "
2426 			    "routing socket", lifr.lifr_name);
2427 			goto bad;
2428 		}
2429 		(void) shutdown(rs, 0);
2430 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
2431 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
2432 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
2433 		    sizeof (struct sockaddr_in6));
2434 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
2435 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
2436 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
2437 		mcast_rtmsg.m_rtm.rtm_addrs =
2438 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
2439 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
2440 		if (af == AF_INET) {
2441 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
2442 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
2443 			    htonl(INADDR_UNSPEC_GROUP);
2444 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
2445 			mcast_rtmsg.m_gw4.sin_addr = in4;
2446 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
2447 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
2448 			    htonl(IN_CLASSD_NET);
2449 		} else {
2450 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
2451 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
2452 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
2453 			mcast_rtmsg.m_gw6.sin6_addr = in6;
2454 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
2455 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
2456 		}
2457 		rlen = write(rs, (char *)&mcast_rtmsg,
2458 		    mcast_rtmsg.m_rtm.rtm_msglen);
2459 		/*
2460 		 * The write to the multicast socket will fail if the
2461 		 * interface belongs to a failed IPMP group. This is a
2462 		 * non-fatal error and the zone will continue booting.
2463 		 * While the zone is running, if any interface in the
2464 		 * failed IPMP group recovers, the zone will fallback to
2465 		 * using that interface.
2466 		 */
2467 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
2468 			if (rlen < 0) {
2469 				zerror(zlogp, B_TRUE, "WARNING: network "
2470 				    "interface '%s' not available as default "
2471 				    "for multicast.", lifr.lifr_name);
2472 			} else {
2473 				zerror(zlogp, B_FALSE, "WARNING: network "
2474 				    "interface '%s' not available as default "
2475 				    "for multicast; routing socket returned "
2476 				    "unexpected %d bytes.",
2477 				    lifr.lifr_name, rlen);
2478 			}
2479 		} else {
2480 
2481 			if (af == AF_INET) {
2482 				*mcast_rt_v4_setp = B_TRUE;
2483 			} else {
2484 				*mcast_rt_v6_setp = B_TRUE;
2485 			}
2486 		}
2487 		(void) close(rs);
2488 	}
2489 
2490 	if (!got_netmask) {
2491 		/*
2492 		 * A common, but often non-fatal problem, is that the system
2493 		 * cannot find the netmask for an interface address. This is
2494 		 * often caused by it being only in /etc/inet/netmasks, but
2495 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
2496 		 * in that. This doesn't show up at boot because the netmask
2497 		 * is obtained from /etc/inet/netmasks when no network
2498 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
2499 		 * available. We warn the user here that something like this
2500 		 * has happened and we're just running with a default and
2501 		 * possible incorrect netmask.
2502 		 */
2503 		char buffer[INET6_ADDRSTRLEN];
2504 		void  *addr;
2505 
2506 		if (af == AF_INET)
2507 			addr = &((struct sockaddr_in *)
2508 			    (&lifr.lifr_addr))->sin_addr;
2509 		else
2510 			addr = &((struct sockaddr_in6 *)
2511 			    (&lifr.lifr_addr))->sin6_addr;
2512 
2513 		/* Find out what netmask interface is going to be using */
2514 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2515 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
2516 			goto bad;
2517 		zerror(zlogp, B_FALSE,
2518 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
2519 		    "%s; using default of %s.",
2520 		    lifr.lifr_name, addrstr4, buffer);
2521 	}
2522 
2523 	/*
2524 	 * If a default router was specified for this interface
2525 	 * set the route now. Ignore if already set.
2526 	 */
2527 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2528 		int status;
2529 		char *argv[7];
2530 
2531 		argv[0] = "route";
2532 		argv[1] = "add";
2533 		argv[2] = "-ifp";
2534 		argv[3] = nwiftabptr->zone_nwif_physical;
2535 		argv[4] = "default";
2536 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2537 		argv[6] = NULL;
2538 
2539 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2540 		if (status != 0 && status != EEXIST)
2541 			zerror(zlogp, B_FALSE, "Unable to set route for "
2542 			    "interface %s to %s\n",
2543 			    nwiftabptr->zone_nwif_physical,
2544 			    nwiftabptr->zone_nwif_defrouter);
2545 	}
2546 
2547 	(void) close(s);
2548 	return (Z_OK);
2549 bad:
2550 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
2551 	(void) close(s);
2552 	return (-1);
2553 }
2554 
2555 /*
2556  * Sets up network interfaces based on information from the zone configuration.
2557  * An IPv4 loopback interface is set up "for free", modeling the global system.
2558  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
2559  * address is set up as well.
2560  *
2561  * If anything goes wrong, we log a general error message, attempt to tear down
2562  * whatever we set up, and return an error.
2563  */
2564 static int
2565 configure_shared_network_interfaces(zlog_t *zlogp)
2566 {
2567 	zone_dochandle_t handle;
2568 	struct zone_nwiftab nwiftab, loopback_iftab;
2569 	boolean_t saw_v6 = B_FALSE;
2570 	boolean_t mcast_rt_v4_set = B_FALSE;
2571 	boolean_t mcast_rt_v6_set = B_FALSE;
2572 	zoneid_t zoneid;
2573 
2574 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
2575 		zerror(zlogp, B_TRUE, "unable to get zoneid");
2576 		return (-1);
2577 	}
2578 
2579 	if ((handle = zonecfg_init_handle()) == NULL) {
2580 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2581 		return (-1);
2582 	}
2583 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2584 		zerror(zlogp, B_FALSE, "invalid configuration");
2585 		zonecfg_fini_handle(handle);
2586 		return (-1);
2587 	}
2588 	if (zonecfg_setnwifent(handle) == Z_OK) {
2589 		for (;;) {
2590 			struct in6_addr in6;
2591 
2592 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2593 				break;
2594 			if (configure_one_interface(zlogp, zoneid,
2595 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
2596 			    Z_OK) {
2597 				(void) zonecfg_endnwifent(handle);
2598 				zonecfg_fini_handle(handle);
2599 				return (-1);
2600 			}
2601 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
2602 			    &in6) == 1)
2603 				saw_v6 = B_TRUE;
2604 		}
2605 		(void) zonecfg_endnwifent(handle);
2606 	}
2607 	zonecfg_fini_handle(handle);
2608 	if (is_system_labeled()) {
2609 		/*
2610 		 * Labeled zones share the loopback interface
2611 		 * so it is not plumbed for shared stack instances.
2612 		 */
2613 		return (0);
2614 	}
2615 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
2616 	    sizeof (loopback_iftab.zone_nwif_physical));
2617 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
2618 	    sizeof (loopback_iftab.zone_nwif_address));
2619 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2620 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
2621 	    != Z_OK) {
2622 		return (-1);
2623 	}
2624 	if (saw_v6) {
2625 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
2626 		    sizeof (loopback_iftab.zone_nwif_address));
2627 		if (configure_one_interface(zlogp, zoneid,
2628 		    &loopback_iftab, NULL, NULL) != Z_OK) {
2629 			return (-1);
2630 		}
2631 	}
2632 	return (0);
2633 }
2634 
2635 static int
2636 add_datalink(zlog_t *zlogp, char *zone_name, char *dlname)
2637 {
2638 	/* First check if it's in use by global zone. */
2639 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2640 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
2641 		errno = EPERM;
2642 		zerror(zlogp, B_TRUE, "WARNING: skipping network interface "
2643 		    "'%s' which is used in the global zone.", dlname);
2644 		return (-1);
2645 	}
2646 
2647 	/* Set zoneid of this link. */
2648 	if (dladm_setzid(dlname, zone_name) != DLADM_STATUS_OK) {
2649 		zerror(zlogp, B_TRUE, "WARNING: unable to add network "
2650 		    "interface '%s'.", dlname);
2651 		return (-1);
2652 	}
2653 
2654 	return (0);
2655 }
2656 
2657 static int
2658 remove_datalink(zlog_t *zlogp, char *dlname)
2659 {
2660 	if (dladm_setzid(dlname, GLOBAL_ZONENAME)
2661 	    != DLADM_STATUS_OK) {
2662 		zerror(zlogp, B_TRUE, "unable to release network "
2663 		    "interface '%s'", dlname);
2664 		return (-1);
2665 	}
2666 	return (0);
2667 }
2668 
2669 /*
2670  * Add the kernel access control information for the interface names.
2671  * If anything goes wrong, we log a general error message, attempt to tear down
2672  * whatever we set up, and return an error.
2673  */
2674 static int
2675 configure_exclusive_network_interfaces(zlog_t *zlogp)
2676 {
2677 	zone_dochandle_t handle;
2678 	struct zone_nwiftab nwiftab;
2679 	char rootpath[MAXPATHLEN];
2680 	char path[MAXPATHLEN];
2681 	di_prof_t prof = NULL;
2682 	boolean_t added = B_FALSE;
2683 
2684 	if ((handle = zonecfg_init_handle()) == NULL) {
2685 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2686 		return (-1);
2687 	}
2688 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2689 		zerror(zlogp, B_FALSE, "invalid configuration");
2690 		zonecfg_fini_handle(handle);
2691 		return (-1);
2692 	}
2693 
2694 	if (zonecfg_setnwifent(handle) != Z_OK) {
2695 		zonecfg_fini_handle(handle);
2696 		return (0);
2697 	}
2698 
2699 	for (;;) {
2700 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2701 			break;
2702 
2703 		if (prof == NULL) {
2704 			if (zone_get_devroot(zone_name, rootpath,
2705 			    sizeof (rootpath)) != Z_OK) {
2706 				(void) zonecfg_endnwifent(handle);
2707 				zonecfg_fini_handle(handle);
2708 				zerror(zlogp, B_TRUE,
2709 				    "unable to determine dev root");
2710 				return (-1);
2711 			}
2712 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2713 			    "/dev");
2714 			if (di_prof_init(path, &prof) != 0) {
2715 				(void) zonecfg_endnwifent(handle);
2716 				zonecfg_fini_handle(handle);
2717 				zerror(zlogp, B_TRUE,
2718 				    "failed to initialize profile");
2719 				return (-1);
2720 			}
2721 		}
2722 
2723 		/*
2724 		 * Create the /dev entry for backward compatibility.
2725 		 * Only create the /dev entry if it's not in use.
2726 		 * Note that the zone still boots when the assigned
2727 		 * interface is inaccessible, used by others, etc.
2728 		 * Also, when vanity naming is used, some interface do
2729 		 * do not have corresponding /dev node names (for example,
2730 		 * vanity named aggregations).  The /dev entry is not
2731 		 * created in that case.  The /dev/net entry is always
2732 		 * accessible.
2733 		 */
2734 		if (add_datalink(zlogp, zone_name, nwiftab.zone_nwif_physical)
2735 		    == 0) {
2736 			added = B_TRUE;
2737 		} else {
2738 			(void) zonecfg_endnwifent(handle);
2739 			zonecfg_fini_handle(handle);
2740 			zerror(zlogp, B_TRUE, "failed to add network device");
2741 			return (-1);
2742 		}
2743 	}
2744 	(void) zonecfg_endnwifent(handle);
2745 	zonecfg_fini_handle(handle);
2746 
2747 	if (prof != NULL && added) {
2748 		if (di_prof_commit(prof) != 0) {
2749 			zerror(zlogp, B_TRUE, "failed to commit profile");
2750 			return (-1);
2751 		}
2752 	}
2753 	if (prof != NULL)
2754 		di_prof_fini(prof);
2755 
2756 	return (0);
2757 }
2758 
2759 /*
2760  * Get the list of the data-links from kernel, and try to remove it
2761  */
2762 static int
2763 unconfigure_exclusive_network_interfaces_run(zlog_t *zlogp, zoneid_t zoneid)
2764 {
2765 	char *dlnames, *ptr;
2766 	int dlnum, dlnum_saved, i;
2767 
2768 	dlnum = 0;
2769 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2770 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2771 		return (-1);
2772 	}
2773 again:
2774 	/* this zone doesn't have any data-links */
2775 	if (dlnum == 0)
2776 		return (0);
2777 
2778 	dlnames = malloc(dlnum * LIFNAMSIZ);
2779 	if (dlnames == NULL) {
2780 		zerror(zlogp, B_TRUE, "memory allocation failed");
2781 		return (-1);
2782 	}
2783 	dlnum_saved = dlnum;
2784 
2785 	if (zone_list_datalink(zoneid, &dlnum, dlnames) != 0) {
2786 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2787 		free(dlnames);
2788 		return (-1);
2789 	}
2790 	if (dlnum_saved < dlnum) {
2791 		/* list increased, try again */
2792 		free(dlnames);
2793 		goto again;
2794 	}
2795 	ptr = dlnames;
2796 	for (i = 0; i < dlnum; i++) {
2797 		/* Remove access control information */
2798 		if (remove_datalink(zlogp, ptr) != 0) {
2799 			free(dlnames);
2800 			return (-1);
2801 		}
2802 		ptr += LIFNAMSIZ;
2803 	}
2804 	free(dlnames);
2805 	return (0);
2806 }
2807 
2808 /*
2809  * Get the list of the data-links from configuration, and try to remove it
2810  */
2811 static int
2812 unconfigure_exclusive_network_interfaces_static(zlog_t *zlogp)
2813 {
2814 	zone_dochandle_t handle;
2815 	struct zone_nwiftab nwiftab;
2816 
2817 	if ((handle = zonecfg_init_handle()) == NULL) {
2818 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2819 		return (-1);
2820 	}
2821 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2822 		zerror(zlogp, B_FALSE, "invalid configuration");
2823 		zonecfg_fini_handle(handle);
2824 		return (-1);
2825 	}
2826 	if (zonecfg_setnwifent(handle) != Z_OK) {
2827 		zonecfg_fini_handle(handle);
2828 		return (0);
2829 	}
2830 	for (;;) {
2831 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2832 			break;
2833 		/* Remove access control information */
2834 		if (remove_datalink(zlogp, nwiftab.zone_nwif_physical)
2835 		    != 0) {
2836 			(void) zonecfg_endnwifent(handle);
2837 			zonecfg_fini_handle(handle);
2838 			return (-1);
2839 		}
2840 	}
2841 	(void) zonecfg_endnwifent(handle);
2842 	zonecfg_fini_handle(handle);
2843 	return (0);
2844 }
2845 
2846 /*
2847  * Remove the access control information from the kernel for the exclusive
2848  * network interfaces.
2849  */
2850 static int
2851 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2852 {
2853 	if (unconfigure_exclusive_network_interfaces_run(zlogp, zoneid) != 0) {
2854 		return (unconfigure_exclusive_network_interfaces_static(zlogp));
2855 	}
2856 
2857 	return (0);
2858 }
2859 
2860 static int
2861 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
2862     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
2863 {
2864 	int fd;
2865 	struct strioctl ioc;
2866 	tcp_ioc_abort_conn_t conn;
2867 	int error;
2868 
2869 	conn.ac_local = *local;
2870 	conn.ac_remote = *remote;
2871 	conn.ac_start = TCPS_SYN_SENT;
2872 	conn.ac_end = TCPS_TIME_WAIT;
2873 	conn.ac_zoneid = zoneid;
2874 
2875 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
2876 	ioc.ic_timout = -1; /* infinite timeout */
2877 	ioc.ic_len = sizeof (conn);
2878 	ioc.ic_dp = (char *)&conn;
2879 
2880 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
2881 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
2882 		return (-1);
2883 	}
2884 
2885 	error = ioctl(fd, I_STR, &ioc);
2886 	(void) close(fd);
2887 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
2888 		return (0);
2889 	return (-1);
2890 }
2891 
2892 static int
2893 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
2894 {
2895 	struct sockaddr_storage l, r;
2896 	struct sockaddr_in *local, *remote;
2897 	struct sockaddr_in6 *local6, *remote6;
2898 	int error;
2899 
2900 	/*
2901 	 * Abort IPv4 connections.
2902 	 */
2903 	bzero(&l, sizeof (*local));
2904 	local = (struct sockaddr_in *)&l;
2905 	local->sin_family = AF_INET;
2906 	local->sin_addr.s_addr = INADDR_ANY;
2907 	local->sin_port = 0;
2908 
2909 	bzero(&r, sizeof (*remote));
2910 	remote = (struct sockaddr_in *)&r;
2911 	remote->sin_family = AF_INET;
2912 	remote->sin_addr.s_addr = INADDR_ANY;
2913 	remote->sin_port = 0;
2914 
2915 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
2916 		return (error);
2917 
2918 	/*
2919 	 * Abort IPv6 connections.
2920 	 */
2921 	bzero(&l, sizeof (*local6));
2922 	local6 = (struct sockaddr_in6 *)&l;
2923 	local6->sin6_family = AF_INET6;
2924 	local6->sin6_port = 0;
2925 	local6->sin6_addr = in6addr_any;
2926 
2927 	bzero(&r, sizeof (*remote6));
2928 	remote6 = (struct sockaddr_in6 *)&r;
2929 	remote6->sin6_family = AF_INET6;
2930 	remote6->sin6_port = 0;
2931 	remote6->sin6_addr = in6addr_any;
2932 
2933 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
2934 		return (error);
2935 	return (0);
2936 }
2937 
2938 static int
2939 get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
2940 {
2941 	int error = -1;
2942 	zone_dochandle_t handle;
2943 	char *privname = NULL;
2944 
2945 	if ((handle = zonecfg_init_handle()) == NULL) {
2946 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2947 		return (-1);
2948 	}
2949 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2950 		zerror(zlogp, B_FALSE, "invalid configuration");
2951 		zonecfg_fini_handle(handle);
2952 		return (-1);
2953 	}
2954 
2955 	if (ALT_MOUNT(mount_cmd)) {
2956 		zone_iptype_t	iptype;
2957 		const char	*curr_iptype;
2958 
2959 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
2960 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
2961 			zonecfg_fini_handle(handle);
2962 			return (-1);
2963 		}
2964 
2965 		switch (iptype) {
2966 		case ZS_SHARED:
2967 			curr_iptype = "shared";
2968 			break;
2969 		case ZS_EXCLUSIVE:
2970 			curr_iptype = "exclusive";
2971 			break;
2972 		}
2973 
2974 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
2975 			zonecfg_fini_handle(handle);
2976 			return (0);
2977 		}
2978 		zerror(zlogp, B_FALSE,
2979 		    "failed to determine the zone's default privilege set");
2980 		zonecfg_fini_handle(handle);
2981 		return (-1);
2982 	}
2983 
2984 	switch (zonecfg_get_privset(handle, privs, &privname)) {
2985 	case Z_OK:
2986 		error = 0;
2987 		break;
2988 	case Z_PRIV_PROHIBITED:
2989 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
2990 		    "within the zone's privilege set", privname);
2991 		break;
2992 	case Z_PRIV_REQUIRED:
2993 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
2994 		    "from the zone's privilege set", privname);
2995 		break;
2996 	case Z_PRIV_UNKNOWN:
2997 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
2998 		    "in the zone's privilege set", privname);
2999 		break;
3000 	default:
3001 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
3002 		    "privilege set");
3003 		break;
3004 	}
3005 
3006 	free(privname);
3007 	zonecfg_fini_handle(handle);
3008 	return (error);
3009 }
3010 
3011 static int
3012 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3013 {
3014 	nvlist_t *nvl = NULL;
3015 	char *nvl_packed = NULL;
3016 	size_t nvl_size = 0;
3017 	nvlist_t **nvlv = NULL;
3018 	int rctlcount = 0;
3019 	int error = -1;
3020 	zone_dochandle_t handle;
3021 	struct zone_rctltab rctltab;
3022 	rctlblk_t *rctlblk = NULL;
3023 
3024 	*bufp = NULL;
3025 	*bufsizep = 0;
3026 
3027 	if ((handle = zonecfg_init_handle()) == NULL) {
3028 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3029 		return (-1);
3030 	}
3031 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3032 		zerror(zlogp, B_FALSE, "invalid configuration");
3033 		zonecfg_fini_handle(handle);
3034 		return (-1);
3035 	}
3036 
3037 	rctltab.zone_rctl_valptr = NULL;
3038 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
3039 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
3040 		goto out;
3041 	}
3042 
3043 	if (zonecfg_setrctlent(handle) != Z_OK) {
3044 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
3045 		goto out;
3046 	}
3047 
3048 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
3049 		zerror(zlogp, B_TRUE, "memory allocation failed");
3050 		goto out;
3051 	}
3052 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
3053 		struct zone_rctlvaltab *rctlval;
3054 		uint_t i, count;
3055 		const char *name = rctltab.zone_rctl_name;
3056 
3057 		/* zoneadm should have already warned about unknown rctls. */
3058 		if (!zonecfg_is_rctl(name)) {
3059 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3060 			rctltab.zone_rctl_valptr = NULL;
3061 			continue;
3062 		}
3063 		count = 0;
3064 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
3065 		    rctlval = rctlval->zone_rctlval_next) {
3066 			count++;
3067 		}
3068 		if (count == 0) {	/* ignore */
3069 			continue;	/* Nothing to free */
3070 		}
3071 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
3072 			goto out;
3073 		i = 0;
3074 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
3075 		    rctlval = rctlval->zone_rctlval_next, i++) {
3076 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
3077 				zerror(zlogp, B_TRUE, "%s failed",
3078 				    "nvlist_alloc");
3079 				goto out;
3080 			}
3081 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
3082 			    != Z_OK) {
3083 				zerror(zlogp, B_FALSE, "invalid rctl value: "
3084 				    "(priv=%s,limit=%s,action=%s)",
3085 				    rctlval->zone_rctlval_priv,
3086 				    rctlval->zone_rctlval_limit,
3087 				    rctlval->zone_rctlval_action);
3088 				goto out;
3089 			}
3090 			if (!zonecfg_valid_rctl(name, rctlblk)) {
3091 				zerror(zlogp, B_FALSE,
3092 				    "(priv=%s,limit=%s,action=%s) is not a "
3093 				    "valid value for rctl '%s'",
3094 				    rctlval->zone_rctlval_priv,
3095 				    rctlval->zone_rctlval_limit,
3096 				    rctlval->zone_rctlval_action,
3097 				    name);
3098 				goto out;
3099 			}
3100 			if (nvlist_add_uint64(nvlv[i], "privilege",
3101 			    rctlblk_get_privilege(rctlblk)) != 0) {
3102 				zerror(zlogp, B_FALSE, "%s failed",
3103 				    "nvlist_add_uint64");
3104 				goto out;
3105 			}
3106 			if (nvlist_add_uint64(nvlv[i], "limit",
3107 			    rctlblk_get_value(rctlblk)) != 0) {
3108 				zerror(zlogp, B_FALSE, "%s failed",
3109 				    "nvlist_add_uint64");
3110 				goto out;
3111 			}
3112 			if (nvlist_add_uint64(nvlv[i], "action",
3113 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
3114 			    != 0) {
3115 				zerror(zlogp, B_FALSE, "%s failed",
3116 				    "nvlist_add_uint64");
3117 				goto out;
3118 			}
3119 		}
3120 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3121 		rctltab.zone_rctl_valptr = NULL;
3122 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
3123 		    != 0) {
3124 			zerror(zlogp, B_FALSE, "%s failed",
3125 			    "nvlist_add_nvlist_array");
3126 			goto out;
3127 		}
3128 		for (i = 0; i < count; i++)
3129 			nvlist_free(nvlv[i]);
3130 		free(nvlv);
3131 		nvlv = NULL;
3132 		rctlcount++;
3133 	}
3134 	(void) zonecfg_endrctlent(handle);
3135 
3136 	if (rctlcount == 0) {
3137 		error = 0;
3138 		goto out;
3139 	}
3140 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
3141 	    != 0) {
3142 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
3143 		goto out;
3144 	}
3145 
3146 	error = 0;
3147 	*bufp = nvl_packed;
3148 	*bufsizep = nvl_size;
3149 
3150 out:
3151 	free(rctlblk);
3152 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3153 	if (error && nvl_packed != NULL)
3154 		free(nvl_packed);
3155 	if (nvl != NULL)
3156 		nvlist_free(nvl);
3157 	if (nvlv != NULL)
3158 		free(nvlv);
3159 	if (handle != NULL)
3160 		zonecfg_fini_handle(handle);
3161 	return (error);
3162 }
3163 
3164 static int
3165 get_implicit_datasets(zlog_t *zlogp, char **retstr)
3166 {
3167 	char cmdbuf[2 * MAXPATHLEN];
3168 
3169 	if (query_hook[0] == '\0')
3170 		return (0);
3171 
3172 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3173 	    > sizeof (cmdbuf))
3174 		return (-1);
3175 
3176 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3177 		return (-1);
3178 
3179 	return (0);
3180 }
3181 
3182 static int
3183 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3184 {
3185 	zone_dochandle_t handle;
3186 	struct zone_dstab dstab;
3187 	size_t total, offset, len;
3188 	int error = -1;
3189 	char *str = NULL;
3190 	char *implicit_datasets = NULL;
3191 	int implicit_len = 0;
3192 
3193 	*bufp = NULL;
3194 	*bufsizep = 0;
3195 
3196 	if ((handle = zonecfg_init_handle()) == NULL) {
3197 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3198 		return (-1);
3199 	}
3200 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3201 		zerror(zlogp, B_FALSE, "invalid configuration");
3202 		zonecfg_fini_handle(handle);
3203 		return (-1);
3204 	}
3205 
3206 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3207 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3208 		goto out;
3209 	}
3210 
3211 	if (zonecfg_setdsent(handle) != Z_OK) {
3212 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3213 		goto out;
3214 	}
3215 
3216 	total = 0;
3217 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3218 		total += strlen(dstab.zone_dataset_name) + 1;
3219 	(void) zonecfg_enddsent(handle);
3220 
3221 	if (implicit_datasets != NULL)
3222 		implicit_len = strlen(implicit_datasets);
3223 	if (implicit_len > 0)
3224 		total += implicit_len + 1;
3225 
3226 	if (total == 0) {
3227 		error = 0;
3228 		goto out;
3229 	}
3230 
3231 	if ((str = malloc(total)) == NULL) {
3232 		zerror(zlogp, B_TRUE, "memory allocation failed");
3233 		goto out;
3234 	}
3235 
3236 	if (zonecfg_setdsent(handle) != Z_OK) {
3237 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3238 		goto out;
3239 	}
3240 	offset = 0;
3241 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3242 		len = strlen(dstab.zone_dataset_name);
3243 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3244 		    total - offset);
3245 		offset += len;
3246 		if (offset < total - 1)
3247 			str[offset++] = ',';
3248 	}
3249 	(void) zonecfg_enddsent(handle);
3250 
3251 	if (implicit_len > 0)
3252 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3253 
3254 	error = 0;
3255 	*bufp = str;
3256 	*bufsizep = total;
3257 
3258 out:
3259 	if (error != 0 && str != NULL)
3260 		free(str);
3261 	if (handle != NULL)
3262 		zonecfg_fini_handle(handle);
3263 	if (implicit_datasets != NULL)
3264 		free(implicit_datasets);
3265 
3266 	return (error);
3267 }
3268 
3269 static int
3270 validate_datasets(zlog_t *zlogp)
3271 {
3272 	zone_dochandle_t handle;
3273 	struct zone_dstab dstab;
3274 	zfs_handle_t *zhp;
3275 	libzfs_handle_t *hdl;
3276 
3277 	if ((handle = zonecfg_init_handle()) == NULL) {
3278 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3279 		return (-1);
3280 	}
3281 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3282 		zerror(zlogp, B_FALSE, "invalid configuration");
3283 		zonecfg_fini_handle(handle);
3284 		return (-1);
3285 	}
3286 
3287 	if (zonecfg_setdsent(handle) != Z_OK) {
3288 		zerror(zlogp, B_FALSE, "invalid configuration");
3289 		zonecfg_fini_handle(handle);
3290 		return (-1);
3291 	}
3292 
3293 	if ((hdl = libzfs_init()) == NULL) {
3294 		zerror(zlogp, B_FALSE, "opening ZFS library");
3295 		zonecfg_fini_handle(handle);
3296 		return (-1);
3297 	}
3298 
3299 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3300 
3301 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3302 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3303 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3304 			    dstab.zone_dataset_name);
3305 			zonecfg_fini_handle(handle);
3306 			libzfs_fini(hdl);
3307 			return (-1);
3308 		}
3309 
3310 		/*
3311 		 * Automatically set the 'zoned' property.  We check the value
3312 		 * first because we'll get EPERM if it is already set.
3313 		 */
3314 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3315 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3316 		    "on") != 0) {
3317 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3318 			    "property for ZFS dataset '%s'\n",
3319 			    dstab.zone_dataset_name);
3320 			zonecfg_fini_handle(handle);
3321 			zfs_close(zhp);
3322 			libzfs_fini(hdl);
3323 			return (-1);
3324 		}
3325 
3326 		zfs_close(zhp);
3327 	}
3328 	(void) zonecfg_enddsent(handle);
3329 
3330 	zonecfg_fini_handle(handle);
3331 	libzfs_fini(hdl);
3332 
3333 	return (0);
3334 }
3335 
3336 /*
3337  * Mount lower level home directories into/from current zone
3338  * Share exported directories specified in dfstab for zone
3339  */
3340 static int
3341 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
3342 {
3343 	zoneid_t *zids = NULL;
3344 	priv_set_t *zid_privs;
3345 	const priv_impl_info_t *ip = NULL;
3346 	uint_t nzents_saved;
3347 	uint_t nzents;
3348 	int i;
3349 	char readonly[] = "ro";
3350 	struct zone_fstab lower_fstab;
3351 	char *argv[4];
3352 
3353 	if (!is_system_labeled())
3354 		return (0);
3355 
3356 	if (zid_label == NULL) {
3357 		zid_label = m_label_alloc(MAC_LABEL);
3358 		if (zid_label == NULL)
3359 			return (-1);
3360 	}
3361 
3362 	/* Make sure our zone has an /export/home dir */
3363 	(void) make_one_dir(zlogp, rootpath, "/export/home",
3364 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3365 
3366 	lower_fstab.zone_fs_raw[0] = '\0';
3367 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
3368 	    sizeof (lower_fstab.zone_fs_type));
3369 	lower_fstab.zone_fs_options = NULL;
3370 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
3371 
3372 	/*
3373 	 * Get the list of zones from the kernel
3374 	 */
3375 	if (zone_list(NULL, &nzents) != 0) {
3376 		zerror(zlogp, B_TRUE, "unable to list zones");
3377 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3378 		return (-1);
3379 	}
3380 again:
3381 	if (nzents == 0) {
3382 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3383 		return (-1);
3384 	}
3385 
3386 	zids = malloc(nzents * sizeof (zoneid_t));
3387 	if (zids == NULL) {
3388 		zerror(zlogp, B_TRUE, "memory allocation failed");
3389 		return (-1);
3390 	}
3391 	nzents_saved = nzents;
3392 
3393 	if (zone_list(zids, &nzents) != 0) {
3394 		zerror(zlogp, B_TRUE, "unable to list zones");
3395 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3396 		free(zids);
3397 		return (-1);
3398 	}
3399 	if (nzents != nzents_saved) {
3400 		/* list changed, try again */
3401 		free(zids);
3402 		goto again;
3403 	}
3404 
3405 	ip = getprivimplinfo();
3406 	if ((zid_privs = priv_allocset()) == NULL) {
3407 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3408 		zonecfg_free_fs_option_list(
3409 		    lower_fstab.zone_fs_options);
3410 		free(zids);
3411 		return (-1);
3412 	}
3413 
3414 	for (i = 0; i < nzents; i++) {
3415 		char zid_name[ZONENAME_MAX];
3416 		zone_state_t zid_state;
3417 		char zid_rpath[MAXPATHLEN];
3418 		struct stat stat_buf;
3419 
3420 		if (zids[i] == GLOBAL_ZONEID)
3421 			continue;
3422 
3423 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3424 			continue;
3425 
3426 		/*
3427 		 * Do special setup for the zone we are booting
3428 		 */
3429 		if (strcmp(zid_name, zone_name) == 0) {
3430 			struct zone_fstab autofs_fstab;
3431 			char map_path[MAXPATHLEN];
3432 			int fd;
3433 
3434 			/*
3435 			 * Create auto_home_<zone> map for this zone
3436 			 * in the global zone. The non-global zone entry
3437 			 * will be created by automount when the zone
3438 			 * is booted.
3439 			 */
3440 
3441 			(void) snprintf(autofs_fstab.zone_fs_special,
3442 			    MAXPATHLEN, "auto_home_%s", zid_name);
3443 
3444 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
3445 			    "/zone/%s/home", zid_name);
3446 
3447 			(void) snprintf(map_path, sizeof (map_path),
3448 			    "/etc/%s", autofs_fstab.zone_fs_special);
3449 			/*
3450 			 * If the map file doesn't exist create a template
3451 			 */
3452 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
3453 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
3454 				int len;
3455 				char map_rec[MAXPATHLEN];
3456 
3457 				len = snprintf(map_rec, sizeof (map_rec),
3458 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
3459 				    autofs_fstab.zone_fs_special, rootpath);
3460 				(void) write(fd, map_rec, len);
3461 				(void) close(fd);
3462 			}
3463 
3464 			/*
3465 			 * Mount auto_home_<zone> in the global zone if absent.
3466 			 * If it's already of type autofs, then
3467 			 * don't mount it again.
3468 			 */
3469 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
3470 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
3471 				char optstr[] = "indirect,ignore,nobrowse";
3472 
3473 				(void) make_one_dir(zlogp, "",
3474 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
3475 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3476 
3477 				/*
3478 				 * Mount will fail if automounter has already
3479 				 * processed the auto_home_<zonename> map
3480 				 */
3481 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
3482 				    autofs_fstab.zone_fs_special,
3483 				    autofs_fstab.zone_fs_dir);
3484 			}
3485 			continue;
3486 		}
3487 
3488 
3489 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
3490 		    (zid_state != ZONE_STATE_READY &&
3491 		    zid_state != ZONE_STATE_RUNNING))
3492 			/* Skip over zones without mounted filesystems */
3493 			continue;
3494 
3495 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3496 		    sizeof (m_label_t)) < 0)
3497 			/* Skip over zones with unspecified label */
3498 			continue;
3499 
3500 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3501 		    sizeof (zid_rpath)) == -1)
3502 			/* Skip over zones with bad path */
3503 			continue;
3504 
3505 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
3506 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
3507 			/* Skip over zones with bad privs */
3508 			continue;
3509 
3510 		/*
3511 		 * Reading down is valid according to our label model
3512 		 * but some customers want to disable it because it
3513 		 * allows execute down and other possible attacks.
3514 		 * Therefore, we restrict this feature to zones that
3515 		 * have the NET_MAC_AWARE privilege which is required
3516 		 * for NFS read-down semantics.
3517 		 */
3518 		if ((bldominates(zlabel, zid_label)) &&
3519 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
3520 			/*
3521 			 * Our zone dominates this one.
3522 			 * Create a lofs mount from lower zone's /export/home
3523 			 */
3524 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3525 			    "%s/zone/%s/export/home", rootpath, zid_name);
3526 
3527 			/*
3528 			 * If the target is already an LOFS mount
3529 			 * then don't do it again.
3530 			 */
3531 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3532 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3533 
3534 				if (snprintf(lower_fstab.zone_fs_special,
3535 				    MAXPATHLEN, "%s/export",
3536 				    zid_rpath) > MAXPATHLEN)
3537 					continue;
3538 
3539 				/*
3540 				 * Make sure the lower-level home exists
3541 				 */
3542 				if (make_one_dir(zlogp,
3543 				    lower_fstab.zone_fs_special, "/home",
3544 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
3545 				    DEFAULT_DIR_GROUP) != 0)
3546 					continue;
3547 
3548 				(void) strlcat(lower_fstab.zone_fs_special,
3549 				    "/home", MAXPATHLEN);
3550 
3551 				/*
3552 				 * Mount can fail because the lower-level
3553 				 * zone may have already done a mount up.
3554 				 */
3555 				(void) mount_one(zlogp, &lower_fstab, "",
3556 				    Z_MNT_BOOT);
3557 			}
3558 		} else if ((bldominates(zid_label, zlabel)) &&
3559 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
3560 			/*
3561 			 * This zone dominates our zone.
3562 			 * Create a lofs mount from our zone's /export/home
3563 			 */
3564 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3565 			    "%s/zone/%s/export/home", zid_rpath,
3566 			    zone_name) > MAXPATHLEN)
3567 				continue;
3568 
3569 			/*
3570 			 * If the target is already an LOFS mount
3571 			 * then don't do it again.
3572 			 */
3573 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3574 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3575 
3576 				(void) snprintf(lower_fstab.zone_fs_special,
3577 				    MAXPATHLEN, "%s/export/home", rootpath);
3578 
3579 				/*
3580 				 * Mount can fail because the higher-level
3581 				 * zone may have already done a mount down.
3582 				 */
3583 				(void) mount_one(zlogp, &lower_fstab, "",
3584 				    Z_MNT_BOOT);
3585 			}
3586 		}
3587 	}
3588 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3589 	priv_freeset(zid_privs);
3590 	free(zids);
3591 
3592 	/*
3593 	 * Now share any exported directories from this zone.
3594 	 * Each zone can have its own dfstab.
3595 	 */
3596 
3597 	argv[0] = "zoneshare";
3598 	argv[1] = "-z";
3599 	argv[2] = zone_name;
3600 	argv[3] = NULL;
3601 
3602 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
3603 	/* Don't check for errors since they don't affect the zone */
3604 
3605 	return (0);
3606 }
3607 
3608 /*
3609  * Unmount lofs mounts from higher level zones
3610  * Unshare nfs exported directories
3611  */
3612 static void
3613 tsol_unmounts(zlog_t *zlogp, char *zone_name)
3614 {
3615 	zoneid_t *zids = NULL;
3616 	uint_t nzents_saved;
3617 	uint_t nzents;
3618 	int i;
3619 	char *argv[4];
3620 	char path[MAXPATHLEN];
3621 
3622 	if (!is_system_labeled())
3623 		return;
3624 
3625 	/*
3626 	 * Get the list of zones from the kernel
3627 	 */
3628 	if (zone_list(NULL, &nzents) != 0) {
3629 		return;
3630 	}
3631 
3632 	if (zid_label == NULL) {
3633 		zid_label = m_label_alloc(MAC_LABEL);
3634 		if (zid_label == NULL)
3635 			return;
3636 	}
3637 
3638 again:
3639 	if (nzents == 0)
3640 		return;
3641 
3642 	zids = malloc(nzents * sizeof (zoneid_t));
3643 	if (zids == NULL) {
3644 		zerror(zlogp, B_TRUE, "memory allocation failed");
3645 		return;
3646 	}
3647 	nzents_saved = nzents;
3648 
3649 	if (zone_list(zids, &nzents) != 0) {
3650 		free(zids);
3651 		return;
3652 	}
3653 	if (nzents != nzents_saved) {
3654 		/* list changed, try again */
3655 		free(zids);
3656 		goto again;
3657 	}
3658 
3659 	for (i = 0; i < nzents; i++) {
3660 		char zid_name[ZONENAME_MAX];
3661 		zone_state_t zid_state;
3662 		char zid_rpath[MAXPATHLEN];
3663 
3664 		if (zids[i] == GLOBAL_ZONEID)
3665 			continue;
3666 
3667 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3668 			continue;
3669 
3670 		/*
3671 		 * Skip the zone we are halting
3672 		 */
3673 		if (strcmp(zid_name, zone_name) == 0)
3674 			continue;
3675 
3676 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
3677 		    sizeof (zid_state)) < 0) ||
3678 		    (zid_state < ZONE_IS_READY))
3679 			/* Skip over zones without mounted filesystems */
3680 			continue;
3681 
3682 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3683 		    sizeof (m_label_t)) < 0)
3684 			/* Skip over zones with unspecified label */
3685 			continue;
3686 
3687 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3688 		    sizeof (zid_rpath)) == -1)
3689 			/* Skip over zones with bad path */
3690 			continue;
3691 
3692 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
3693 			/*
3694 			 * This zone dominates our zone.
3695 			 * Unmount the lofs mount of our zone's /export/home
3696 			 */
3697 
3698 			if (snprintf(path, MAXPATHLEN,
3699 			    "%s/zone/%s/export/home", zid_rpath,
3700 			    zone_name) > MAXPATHLEN)
3701 				continue;
3702 
3703 			/* Skip over mount failures */
3704 			(void) umount(path);
3705 		}
3706 	}
3707 	free(zids);
3708 
3709 	/*
3710 	 * Unmount global zone autofs trigger for this zone
3711 	 */
3712 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
3713 	/* Skip over mount failures */
3714 	(void) umount(path);
3715 
3716 	/*
3717 	 * Next unshare any exported directories from this zone.
3718 	 */
3719 
3720 	argv[0] = "zoneunshare";
3721 	argv[1] = "-z";
3722 	argv[2] = zone_name;
3723 	argv[3] = NULL;
3724 
3725 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
3726 	/* Don't check for errors since they don't affect the zone */
3727 
3728 	/*
3729 	 * Finally, deallocate any devices in the zone.
3730 	 */
3731 
3732 	argv[0] = "deallocate";
3733 	argv[1] = "-Isz";
3734 	argv[2] = zone_name;
3735 	argv[3] = NULL;
3736 
3737 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
3738 	/* Don't check for errors since they don't affect the zone */
3739 }
3740 
3741 /*
3742  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
3743  * this zone.
3744  */
3745 static tsol_zcent_t *
3746 get_zone_label(zlog_t *zlogp, priv_set_t *privs)
3747 {
3748 	FILE *fp;
3749 	tsol_zcent_t *zcent = NULL;
3750 	char line[MAXTNZLEN];
3751 
3752 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
3753 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
3754 		return (NULL);
3755 	}
3756 
3757 	while (fgets(line, sizeof (line), fp) != NULL) {
3758 		/*
3759 		 * Check for malformed database
3760 		 */
3761 		if (strlen(line) == MAXTNZLEN - 1)
3762 			break;
3763 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
3764 			continue;
3765 		if (strcmp(zcent->zc_name, zone_name) == 0)
3766 			break;
3767 		tsol_freezcent(zcent);
3768 		zcent = NULL;
3769 	}
3770 	(void) fclose(fp);
3771 
3772 	if (zcent == NULL) {
3773 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
3774 		    "See tnzonecfg(4)");
3775 	} else {
3776 		if (zlabel == NULL)
3777 			zlabel = m_label_alloc(MAC_LABEL);
3778 		/*
3779 		 * Save this zone's privileges for later read-down processing
3780 		 */
3781 		if ((zprivs = priv_allocset()) == NULL) {
3782 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3783 			return (NULL);
3784 		} else {
3785 			priv_copyset(privs, zprivs);
3786 		}
3787 	}
3788 	return (zcent);
3789 }
3790 
3791 /*
3792  * Add the Trusted Extensions multi-level ports for this zone.
3793  */
3794 static void
3795 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
3796 {
3797 	tsol_mlp_t *mlp;
3798 	tsol_mlpent_t tsme;
3799 
3800 	if (!is_system_labeled())
3801 		return;
3802 
3803 	tsme.tsme_zoneid = zoneid;
3804 	tsme.tsme_flags = 0;
3805 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
3806 		tsme.tsme_mlp = *mlp;
3807 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
3808 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
3809 			    "on %d-%d/%d", mlp->mlp_port,
3810 			    mlp->mlp_port_upper, mlp->mlp_ipp);
3811 		}
3812 	}
3813 
3814 	tsme.tsme_flags = TSOL_MEF_SHARED;
3815 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
3816 		tsme.tsme_mlp = *mlp;
3817 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
3818 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
3819 			    "on %d-%d/%d", mlp->mlp_port,
3820 			    mlp->mlp_port_upper, mlp->mlp_ipp);
3821 		}
3822 	}
3823 }
3824 
3825 static void
3826 remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
3827 {
3828 	tsol_mlpent_t tsme;
3829 
3830 	if (!is_system_labeled())
3831 		return;
3832 
3833 	(void) memset(&tsme, 0, sizeof (tsme));
3834 	tsme.tsme_zoneid = zoneid;
3835 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
3836 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
3837 }
3838 
3839 int
3840 prtmount(const char *fs, void *x) {
3841 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
3842 	return (0);
3843 }
3844 
3845 /*
3846  * Look for zones running on the main system that are using this root (or any
3847  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3848  * is found or if we can't tell.
3849  */
3850 static boolean_t
3851 duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
3852 {
3853 	zoneid_t *zids = NULL;
3854 	uint_t nzids = 0;
3855 	boolean_t retv;
3856 	int rlen, zlen;
3857 	char zroot[MAXPATHLEN];
3858 	char zonename[ZONENAME_MAX];
3859 
3860 	for (;;) {
3861 		nzids += 10;
3862 		zids = malloc(nzids * sizeof (*zids));
3863 		if (zids == NULL) {
3864 			zerror(zlogp, B_TRUE, "memory allocation failed");
3865 			return (B_TRUE);
3866 		}
3867 		if (zone_list(zids, &nzids) == 0)
3868 			break;
3869 		free(zids);
3870 	}
3871 	retv = B_FALSE;
3872 	rlen = strlen(rootpath);
3873 	while (nzids > 0) {
3874 		/*
3875 		 * Ignore errors; they just mean that the zone has disappeared
3876 		 * while we were busy.
3877 		 */
3878 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3879 		    sizeof (zroot)) == -1)
3880 			continue;
3881 		zlen = strlen(zroot);
3882 		if (zlen > rlen)
3883 			zlen = rlen;
3884 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3885 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3886 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3887 			if (getzonenamebyid(zids[nzids], zonename,
3888 			    sizeof (zonename)) == -1)
3889 				(void) snprintf(zonename, sizeof (zonename),
3890 				    "id %d", (int)zids[nzids]);
3891 			zerror(zlogp, B_FALSE,
3892 			    "zone root %s already in use by zone %s",
3893 			    rootpath, zonename);
3894 			retv = B_TRUE;
3895 			break;
3896 		}
3897 	}
3898 	free(zids);
3899 	return (retv);
3900 }
3901 
3902 /*
3903  * Search for loopback mounts that use this same source node (same device and
3904  * inode).  Return B_TRUE if there is one or if we can't tell.
3905  */
3906 static boolean_t
3907 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3908 {
3909 	struct stat64 rst, zst;
3910 	struct mnttab *mnp;
3911 
3912 	if (stat64(rootpath, &rst) == -1) {
3913 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3914 		return (B_TRUE);
3915 	}
3916 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3917 		return (B_TRUE);
3918 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3919 		if (mnp->mnt_fstype == NULL ||
3920 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3921 			continue;
3922 		/* We're looking at a loopback mount.  Stat it. */
3923 		if (mnp->mnt_special != NULL &&
3924 		    stat64(mnp->mnt_special, &zst) != -1 &&
3925 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3926 			zerror(zlogp, B_FALSE,
3927 			    "zone root %s is reachable through %s",
3928 			    rootpath, mnp->mnt_mountp);
3929 			return (B_TRUE);
3930 		}
3931 	}
3932 	return (B_FALSE);
3933 }
3934 
3935 /*
3936  * Set memory cap and pool info for the zone's resource management
3937  * configuration.
3938  */
3939 static int
3940 setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
3941 {
3942 	int res;
3943 	uint64_t tmp;
3944 	struct zone_mcaptab mcap;
3945 	char sched[MAXNAMELEN];
3946 	zone_dochandle_t handle = NULL;
3947 	char pool_err[128];
3948 
3949 	if ((handle = zonecfg_init_handle()) == NULL) {
3950 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3951 		return (Z_BAD_HANDLE);
3952 	}
3953 
3954 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
3955 		zerror(zlogp, B_FALSE, "invalid configuration");
3956 		zonecfg_fini_handle(handle);
3957 		return (res);
3958 	}
3959 
3960 	/*
3961 	 * If a memory cap is configured, set the cap in the kernel using
3962 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
3963 	 */
3964 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
3965 		uint64_t num;
3966 		char smf_err[128];
3967 
3968 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
3969 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
3970 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
3971 			zonecfg_fini_handle(handle);
3972 			return (Z_INVAL);
3973 		}
3974 
3975 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
3976 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
3977 			    "failed: %s", smf_err);
3978 			zonecfg_fini_handle(handle);
3979 			return (Z_INVAL);
3980 		}
3981 	}
3982 
3983 	/* Get the scheduling class set in the zone configuration. */
3984 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
3985 	    strlen(sched) > 0) {
3986 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
3987 		    strlen(sched)) == -1)
3988 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
3989 			    "default scheduling class");
3990 
3991 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
3992 	    == Z_OK) {
3993 		/*
3994 		 * If the zone has the zone.cpu-shares rctl set then we want to
3995 		 * use the Fair Share Scheduler (FSS) for processes in the
3996 		 * zone.  Check what scheduling class the zone would be running
3997 		 * in by default so we can print a warning and modify the class
3998 		 * if we wouldn't be using FSS.
3999 		 */
4000 		char class_name[PC_CLNMSZ];
4001 
4002 		if (zonecfg_get_dflt_sched_class(handle, class_name,
4003 		    sizeof (class_name)) != Z_OK) {
4004 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
4005 			    "the zone's scheduling class");
4006 
4007 		} else if (strcmp("FSS", class_name) != 0) {
4008 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
4009 			    "rctl is set but\nFSS is not the default "
4010 			    "scheduling class for\nthis zone.  FSS will be "
4011 			    "used for processes\nin the zone but to get the "
4012 			    "full benefit of FSS,\nit should be the default "
4013 			    "scheduling class.\nSee dispadmin(1M) for more "
4014 			    "details.");
4015 
4016 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
4017 			    strlen("FSS")) == -1)
4018 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
4019 				    "zone scheduling class to FSS");
4020 		}
4021 	}
4022 
4023 	/*
4024 	 * The next few blocks of code attempt to set up temporary pools as
4025 	 * well as persistent pools.  In all cases we call the functions
4026 	 * unconditionally.  Within each funtion the code will check if the
4027 	 * zone is actually configured for a temporary pool or persistent pool
4028 	 * and just return if there is nothing to do.
4029 	 *
4030 	 * If we are rebooting we want to attempt to reuse any temporary pool
4031 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
4032 	 * right thing in all cases (reuse or create) based on the current
4033 	 * zonecfg.
4034 	 */
4035 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
4036 	    sizeof (pool_err))) != Z_OK) {
4037 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
4038 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
4039 			    "cannot be instantiated", zonecfg_strerror(res),
4040 			    pool_err);
4041 		else
4042 			zerror(zlogp, B_FALSE, "could not bind zone to "
4043 			    "temporary pool: %s", zonecfg_strerror(res));
4044 		zonecfg_fini_handle(handle);
4045 		return (Z_POOL_BIND);
4046 	}
4047 
4048 	/*
4049 	 * Check if we need to warn about poold not being enabled.
4050 	 */
4051 	if (zonecfg_warn_poold(handle)) {
4052 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
4053 		    "been specified\nbut the dynamic pool service is not "
4054 		    "enabled.\nThe system will not dynamically adjust the\n"
4055 		    "processor allocation within the specified range\n"
4056 		    "until svc:/system/pools/dynamic is enabled.\n"
4057 		    "See poold(1M).");
4058 	}
4059 
4060 	/* The following is a warning, not an error. */
4061 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
4062 	    sizeof (pool_err))) != Z_OK) {
4063 		if (res == Z_POOL_BIND)
4064 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
4065 			    "pool '%s'; using default pool.", pool_err);
4066 		else if (res == Z_POOL)
4067 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
4068 			    zonecfg_strerror(res), pool_err);
4069 		else
4070 			zerror(zlogp, B_FALSE, "WARNING: %s",
4071 			    zonecfg_strerror(res));
4072 	}
4073 
4074 	zonecfg_fini_handle(handle);
4075 	return (Z_OK);
4076 }
4077 
4078 zoneid_t
4079 vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4080 {
4081 	zoneid_t rval = -1;
4082 	priv_set_t *privs;
4083 	char rootpath[MAXPATHLEN];
4084 	char *rctlbuf = NULL;
4085 	size_t rctlbufsz = 0;
4086 	char *zfsbuf = NULL;
4087 	size_t zfsbufsz = 0;
4088 	zoneid_t zoneid = -1;
4089 	int xerr;
4090 	char *kzone;
4091 	FILE *fp = NULL;
4092 	tsol_zcent_t *zcent = NULL;
4093 	int match = 0;
4094 	int doi = 0;
4095 	int flags;
4096 	zone_iptype_t iptype;
4097 
4098 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
4099 		zerror(zlogp, B_TRUE, "unable to determine zone root");
4100 		return (-1);
4101 	}
4102 	if (zonecfg_in_alt_root())
4103 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
4104 
4105 	if (get_iptype(zlogp, &iptype) < 0) {
4106 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4107 		return (-1);
4108 	}
4109 	switch (iptype) {
4110 	case ZS_SHARED:
4111 		flags = 0;
4112 		break;
4113 	case ZS_EXCLUSIVE:
4114 		flags = ZCF_NET_EXCL;
4115 		break;
4116 	}
4117 
4118 	if ((privs = priv_allocset()) == NULL) {
4119 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
4120 		return (-1);
4121 	}
4122 	priv_emptyset(privs);
4123 	if (get_privset(zlogp, privs, mount_cmd) != 0)
4124 		goto error;
4125 
4126 	if (mount_cmd == Z_MNT_BOOT &&
4127 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
4128 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
4129 		goto error;
4130 	}
4131 
4132 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4133 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4134 		goto error;
4135 	}
4136 
4137 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
4138 		zcent = get_zone_label(zlogp, privs);
4139 		if (zcent != NULL) {
4140 			match = zcent->zc_match;
4141 			doi = zcent->zc_doi;
4142 			*zlabel = zcent->zc_label;
4143 		} else {
4144 			goto error;
4145 		}
4146 	}
4147 
4148 	kzone = zone_name;
4149 
4150 	/*
4151 	 * We must do this scan twice.  First, we look for zones running on the
4152 	 * main system that are using this root (or any subdirectory of it).
4153 	 * Next, we reduce to the shortest path and search for loopback mounts
4154 	 * that use this same source node (same device and inode).
4155 	 */
4156 	if (duplicate_zone_root(zlogp, rootpath))
4157 		goto error;
4158 	if (duplicate_reachable_path(zlogp, rootpath))
4159 		goto error;
4160 
4161 	if (ALT_MOUNT(mount_cmd)) {
4162 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4163 
4164 		/*
4165 		 * Forge up a special root for this zone.  When a zone is
4166 		 * mounted, we can't let the zone have its own root because the
4167 		 * tools that will be used in this "scratch zone" need access
4168 		 * to both the zone's resources and the running machine's
4169 		 * executables.
4170 		 *
4171 		 * Note that the mkdir here also catches read-only filesystems.
4172 		 */
4173 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4174 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4175 			goto error;
4176 		}
4177 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4178 			goto error;
4179 	}
4180 
4181 	if (zonecfg_in_alt_root()) {
4182 		/*
4183 		 * If we are mounting up a zone in an alternate root partition,
4184 		 * then we have some additional work to do before starting the
4185 		 * zone.  First, resolve the root path down so that we're not
4186 		 * fooled by duplicates.  Then forge up an internal name for
4187 		 * the zone.
4188 		 */
4189 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4190 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4191 			goto error;
4192 		}
4193 		if (zonecfg_lock_scratch(fp) != 0) {
4194 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4195 			goto error;
4196 		}
4197 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4198 		    NULL, 0) == 0) {
4199 			zerror(zlogp, B_FALSE, "scratch zone already running");
4200 			goto error;
4201 		}
4202 		/* This is the preferred name */
4203 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4204 		    zone_name);
4205 		srandom(getpid());
4206 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4207 		    0) == 0) {
4208 			/* This is just an arbitrary name; note "." usage */
4209 			(void) snprintf(kernzone, sizeof (kernzone),
4210 			    "SUNWlu.%08lX%08lX", random(), random());
4211 		}
4212 		kzone = kernzone;
4213 	}
4214 
4215 	xerr = 0;
4216 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4217 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4218 	    flags)) == -1) {
4219 		if (xerr == ZE_AREMOUNTS) {
4220 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
4221 				zerror(zlogp, B_FALSE,
4222 				    "An unknown file-system is mounted on "
4223 				    "a subdirectory of %s", rootpath);
4224 			} else {
4225 
4226 				zerror(zlogp, B_FALSE,
4227 				    "These file-systems are mounted on "
4228 				    "subdirectories of %s:", rootpath);
4229 				(void) zonecfg_find_mounts(rootpath,
4230 				    prtmount, zlogp);
4231 			}
4232 		} else if (xerr == ZE_CHROOTED) {
4233 			zerror(zlogp, B_FALSE, "%s: "
4234 			    "cannot create a zone from a chrooted "
4235 			    "environment", "zone_create");
4236 		} else if (xerr == ZE_LABELINUSE) {
4237 			char zonename[ZONENAME_MAX];
4238 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
4239 			    zonename, ZONENAME_MAX);
4240 			zerror(zlogp, B_FALSE, "The zone label is already "
4241 			    "used by the zone '%s'.", zonename);
4242 		} else {
4243 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
4244 		}
4245 		goto error;
4246 	}
4247 
4248 	if (zonecfg_in_alt_root() &&
4249 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4250 	    zonecfg_get_root()) == -1) {
4251 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4252 		goto error;
4253 	}
4254 
4255 	/*
4256 	 * The following actions are not performed when merely mounting a zone
4257 	 * for administrative use.
4258 	 */
4259 	if (mount_cmd == Z_MNT_BOOT) {
4260 		brand_handle_t bh;
4261 		struct brand_attr attr;
4262 		char modname[MAXPATHLEN];
4263 
4264 		if ((zone_get_brand(zone_name, attr.ba_brandname,
4265 		    MAXNAMELEN) != Z_OK) ||
4266 		    (bh = brand_open(attr.ba_brandname)) == NULL) {
4267 			zerror(zlogp, B_FALSE,
4268 			    "unable to determine brand name");
4269 			return (-1);
4270 		}
4271 
4272 		/*
4273 		 * If this brand requires any kernel support, now is the time to
4274 		 * get it loaded and initialized.
4275 		 */
4276 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4277 			brand_close(bh);
4278 			zerror(zlogp, B_FALSE,
4279 			    "unable to determine brand kernel module");
4280 			return (-1);
4281 		}
4282 		brand_close(bh);
4283 
4284 		if (strlen(modname) > 0) {
4285 			(void) strlcpy(attr.ba_modname, modname, MAXPATHLEN);
4286 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
4287 			    sizeof (attr) != 0)) {
4288 				zerror(zlogp, B_TRUE,
4289 				    "could not set zone brand attribute.");
4290 				goto error;
4291 			}
4292 		}
4293 
4294 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) {
4295 			(void) zone_shutdown(zoneid);
4296 			goto error;
4297 		}
4298 
4299 		set_mlps(zlogp, zoneid, zcent);
4300 	}
4301 
4302 	rval = zoneid;
4303 	zoneid = -1;
4304 
4305 error:
4306 	if (zoneid != -1)
4307 		(void) zone_destroy(zoneid);
4308 	if (rctlbuf != NULL)
4309 		free(rctlbuf);
4310 	priv_freeset(privs);
4311 	if (fp != NULL)
4312 		zonecfg_close_scratch(fp);
4313 	lofs_discard_mnttab();
4314 	if (zcent != NULL)
4315 		tsol_freezcent(zcent);
4316 	return (rval);
4317 }
4318 
4319 /*
4320  * Enter the zone and write a /etc/zones/index file there.  This allows
4321  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4322  * details from inside the zone.
4323  */
4324 static void
4325 write_index_file(zoneid_t zoneid)
4326 {
4327 	FILE *zef;
4328 	FILE *zet;
4329 	struct zoneent *zep;
4330 	pid_t child;
4331 	int tmpl_fd;
4332 	ctid_t ct;
4333 	int fd;
4334 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4335 
4336 	/* Locate the zone entry in the global zone's index file */
4337 	if ((zef = setzoneent()) == NULL)
4338 		return;
4339 	while ((zep = getzoneent_private(zef)) != NULL) {
4340 		if (strcmp(zep->zone_name, zone_name) == 0)
4341 			break;
4342 		free(zep);
4343 	}
4344 	endzoneent(zef);
4345 	if (zep == NULL)
4346 		return;
4347 
4348 	if ((tmpl_fd = init_template()) == -1) {
4349 		free(zep);
4350 		return;
4351 	}
4352 
4353 	if ((child = fork()) == -1) {
4354 		(void) ct_tmpl_clear(tmpl_fd);
4355 		(void) close(tmpl_fd);
4356 		free(zep);
4357 		return;
4358 	}
4359 
4360 	/* parent waits for child to finish */
4361 	if (child != 0) {
4362 		free(zep);
4363 		if (contract_latest(&ct) == -1)
4364 			ct = -1;
4365 		(void) ct_tmpl_clear(tmpl_fd);
4366 		(void) close(tmpl_fd);
4367 		(void) waitpid(child, NULL, 0);
4368 		(void) contract_abandon_id(ct);
4369 		return;
4370 	}
4371 
4372 	/* child enters zone and sets up index file */
4373 	(void) ct_tmpl_clear(tmpl_fd);
4374 	if (zone_enter(zoneid) != -1) {
4375 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4376 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4377 		    ZONE_CONFIG_GID);
4378 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4379 		    ZONE_INDEX_MODE);
4380 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4381 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4382 			if (uuid_is_null(zep->zone_uuid))
4383 				uuidstr[0] = '\0';
4384 			else
4385 				uuid_unparse(zep->zone_uuid, uuidstr);
4386 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4387 			    zone_state_str(zep->zone_state),
4388 			    uuidstr);
4389 			(void) fclose(zet);
4390 		}
4391 	}
4392 	_exit(0);
4393 }
4394 
4395 int
4396 vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
4397 {
4398 	char zonepath[MAXPATHLEN];
4399 
4400 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4401 		lofs_discard_mnttab();
4402 		return (-1);
4403 	}
4404 
4405 	/*
4406 	 * Before we try to mount filesystems we need to create the
4407 	 * attribute backing store for /dev
4408 	 */
4409 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
4410 		lofs_discard_mnttab();
4411 		return (-1);
4412 	}
4413 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
4414 
4415 	/* Make /dev directory owned by root, grouped sys */
4416 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
4417 	    0, 3) != 0) {
4418 		lofs_discard_mnttab();
4419 		return (-1);
4420 	}
4421 
4422 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4423 		lofs_discard_mnttab();
4424 		return (-1);
4425 	}
4426 
4427 	if (mount_cmd == Z_MNT_BOOT) {
4428 		zone_iptype_t iptype;
4429 
4430 		if (get_iptype(zlogp, &iptype) < 0) {
4431 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4432 			lofs_discard_mnttab();
4433 			return (-1);
4434 		}
4435 
4436 		switch (iptype) {
4437 		case ZS_SHARED:
4438 			/* Always do this to make lo0 get configured */
4439 			if (configure_shared_network_interfaces(zlogp) != 0) {
4440 				lofs_discard_mnttab();
4441 				return (-1);
4442 			}
4443 			break;
4444 		case ZS_EXCLUSIVE:
4445 			if (configure_exclusive_network_interfaces(zlogp) !=
4446 			    0) {
4447 				lofs_discard_mnttab();
4448 				return (-1);
4449 			}
4450 			break;
4451 		}
4452 	}
4453 
4454 	write_index_file(zoneid);
4455 
4456 	lofs_discard_mnttab();
4457 	return (0);
4458 }
4459 
4460 static int
4461 lu_root_teardown(zlog_t *zlogp)
4462 {
4463 	char zroot[MAXPATHLEN];
4464 
4465 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4466 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4467 		return (-1);
4468 	}
4469 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4470 
4471 	/*
4472 	 * At this point, the processes are gone, the filesystems (save the
4473 	 * root) are unmounted, and the zone is on death row.  But there may
4474 	 * still be creds floating about in the system that reference the
4475 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4476 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4477 	 * (How I wish this were not true, and umount2 just did the right
4478 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4479 	 */
4480 	if (umount2(zroot, MS_FORCE) != 0) {
4481 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4482 			goto unmounted;
4483 		if (errno == EBUSY) {
4484 			int tries = 10;
4485 
4486 			while (--tries >= 0) {
4487 				(void) sleep(1);
4488 				if (umount2(zroot, 0) == 0)
4489 					goto unmounted;
4490 				if (errno != EBUSY)
4491 					break;
4492 			}
4493 		}
4494 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4495 		return (-1);
4496 	}
4497 unmounted:
4498 
4499 	/*
4500 	 * Only zones in an alternate root environment have scratch zone
4501 	 * entries.
4502 	 */
4503 	if (zonecfg_in_alt_root()) {
4504 		FILE *fp;
4505 		int retv;
4506 
4507 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4508 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4509 			return (-1);
4510 		}
4511 		retv = -1;
4512 		if (zonecfg_lock_scratch(fp) != 0)
4513 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4514 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4515 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4516 		else
4517 			retv = 0;
4518 		zonecfg_close_scratch(fp);
4519 		return (retv);
4520 	} else {
4521 		return (0);
4522 	}
4523 }
4524 
4525 int
4526 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4527 {
4528 	char *kzone;
4529 	zoneid_t zoneid;
4530 	int res;
4531 	char pool_err[128];
4532 	char zpath[MAXPATHLEN];
4533 	char cmdbuf[MAXPATHLEN];
4534 	char brand[MAXNAMELEN];
4535 	brand_handle_t bh = NULL;
4536 	ushort_t flags;
4537 
4538 	kzone = zone_name;
4539 	if (zonecfg_in_alt_root()) {
4540 		FILE *fp;
4541 
4542 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4543 			zerror(zlogp, B_TRUE, "unable to open map file");
4544 			goto error;
4545 		}
4546 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4547 		    kernzone, sizeof (kernzone)) != 0) {
4548 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4549 			zonecfg_close_scratch(fp);
4550 			goto error;
4551 		}
4552 		zonecfg_close_scratch(fp);
4553 		kzone = kernzone;
4554 	}
4555 
4556 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
4557 		if (!bringup_failure_recovery)
4558 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4559 		if (unmount_cmd)
4560 			(void) lu_root_teardown(zlogp);
4561 		goto error;
4562 	}
4563 
4564 	if (zone_shutdown(zoneid) != 0) {
4565 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
4566 		goto error;
4567 	}
4568 
4569 	/* Get the zonepath of this zone */
4570 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
4571 		zerror(zlogp, B_FALSE, "unable to determine zone path");
4572 		goto error;
4573 	}
4574 
4575 	/* Get a handle to the brand info for this zone */
4576 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
4577 	    (bh = brand_open(brand)) == NULL) {
4578 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
4579 		return (-1);
4580 	}
4581 	/*
4582 	 * If there is a brand 'halt' callback, execute it now to give the
4583 	 * brand a chance to cleanup any custom configuration.
4584 	 */
4585 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4586 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
4587 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
4588 		brand_close(bh);
4589 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
4590 		    "halt callback.");
4591 		goto error;
4592 	}
4593 	brand_close(bh);
4594 
4595 	if ((strlen(cmdbuf) > EXEC_LEN) &&
4596 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
4597 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
4598 		goto error;
4599 	}
4600 
4601 	if (!unmount_cmd) {
4602 		zone_iptype_t iptype;
4603 
4604 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4605 		    sizeof (flags)) < 0) {
4606 			if (get_iptype(zlogp, &iptype) < 0) {
4607 				zerror(zlogp, B_TRUE, "unable to determine "
4608 				    "ip-type");
4609 				goto error;
4610 			}
4611 		} else {
4612 			if (flags & ZF_NET_EXCL)
4613 				iptype = ZS_EXCLUSIVE;
4614 			else
4615 				iptype = ZS_SHARED;
4616 		}
4617 
4618 		switch (iptype) {
4619 		case ZS_SHARED:
4620 			if (unconfigure_shared_network_interfaces(zlogp,
4621 			    zoneid) != 0) {
4622 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4623 				    "network interfaces in zone");
4624 				goto error;
4625 			}
4626 			break;
4627 		case ZS_EXCLUSIVE:
4628 			if (unconfigure_exclusive_network_interfaces(zlogp,
4629 			    zoneid) != 0) {
4630 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4631 				    "network interfaces in zone");
4632 				goto error;
4633 			}
4634 			break;
4635 		}
4636 	}
4637 
4638 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
4639 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
4640 		goto error;
4641 	}
4642 
4643 	/* destroy zconsole before umount /dev */
4644 	if (!unmount_cmd)
4645 		destroy_console_slave();
4646 
4647 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
4648 		zerror(zlogp, B_FALSE,
4649 		    "unable to unmount file systems in zone");
4650 		goto error;
4651 	}
4652 
4653 	/*
4654 	 * If we are rebooting then we normally don't want to destroy an
4655 	 * existing temporary pool at this point so that we can just reuse it
4656 	 * when the zone boots back up.  However, it is also possible we were
4657 	 * running with a temporary pool and the zone configuration has been
4658 	 * modified to no longer use a temporary pool.  In that case we need
4659 	 * to destroy the temporary pool now.  This case looks like the case
4660 	 * where we never had a temporary pool configured but
4661 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
4662 	 */
4663 	if (!unmount_cmd) {
4664 		boolean_t destroy_tmp_pool = B_TRUE;
4665 
4666 		if (rebooting) {
4667 			struct zone_psettab pset_tab;
4668 			zone_dochandle_t handle;
4669 
4670 			if ((handle = zonecfg_init_handle()) != NULL &&
4671 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4672 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4673 				destroy_tmp_pool = B_FALSE;
4674 
4675 			zonecfg_fini_handle(handle);
4676 		}
4677 
4678 		if (destroy_tmp_pool) {
4679 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
4680 			    sizeof (pool_err))) != Z_OK) {
4681 				if (res == Z_POOL)
4682 					zerror(zlogp, B_FALSE, pool_err);
4683 			}
4684 		}
4685 	}
4686 
4687 	remove_mlps(zlogp, zoneid);
4688 
4689 	if (zone_destroy(zoneid) != 0) {
4690 		zerror(zlogp, B_TRUE, "unable to destroy zone");
4691 		goto error;
4692 	}
4693 
4694 	/*
4695 	 * Special teardown for alternate boot environments: remove the tmpfs
4696 	 * root for the zone and then remove it from the map file.
4697 	 */
4698 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4699 		goto error;
4700 
4701 	lofs_discard_mnttab();
4702 	return (0);
4703 
4704 error:
4705 	lofs_discard_mnttab();
4706 	return (-1);
4707 }
4708