xref: /titanic_50/usr/src/cmd/zoneadmd/vplat.c (revision 63b1987d5d7387bda49397c0f3f063b49651a5bc)
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 2009 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 /*
2196  * Configures a single interface: a new virtual interface is added, based on
2197  * the physical interface nwiftabptr->zone_nwif_physical, with the address
2198  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
2199  * the "address" can be an IPv6 address (with a /prefixlength required), an
2200  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
2201  * an IPv4 name-to-address resolution will be attempted.
2202  *
2203  * If anything goes wrong, we log an detailed error message, attempt to tear
2204  * down whatever we set up and return an error.
2205  */
2206 static int
2207 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2208     struct zone_nwiftab *nwiftabptr)
2209 {
2210 	struct lifreq lifr;
2211 	struct sockaddr_in netmask4;
2212 	struct sockaddr_in6 netmask6;
2213 	struct in_addr in4;
2214 	sa_family_t af;
2215 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
2216 	int s;
2217 	boolean_t got_netmask = B_FALSE;
2218 	char addrstr4[INET_ADDRSTRLEN];
2219 	int res;
2220 
2221 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
2222 	if (res != Z_OK) {
2223 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
2224 		    nwiftabptr->zone_nwif_address);
2225 		return (-1);
2226 	}
2227 	af = lifr.lifr_addr.ss_family;
2228 	if (af == AF_INET)
2229 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
2230 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
2231 		zerror(zlogp, B_TRUE, "could not get socket");
2232 		return (-1);
2233 	}
2234 
2235 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
2236 	    sizeof (lifr.lifr_name));
2237 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
2238 		/*
2239 		 * Here, we know that the interface can't be brought up.
2240 		 * A similar warning message was already printed out to
2241 		 * the console by zoneadm(1M) so instead we log the
2242 		 * message to syslog and continue.
2243 		 */
2244 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
2245 		    "'%s' which may not be present/plumbed in the "
2246 		    "global zone.", lifr.lifr_name);
2247 		(void) close(s);
2248 		return (Z_OK);
2249 	}
2250 
2251 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2252 		zerror(zlogp, B_TRUE,
2253 		    "%s: could not set IP address to %s",
2254 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
2255 		goto bad;
2256 	}
2257 
2258 	/* Preserve literal IPv4 address for later potential printing. */
2259 	if (af == AF_INET)
2260 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
2261 
2262 	lifr.lifr_zoneid = zone_id;
2263 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2264 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2265 		    "into zone", lifr.lifr_name);
2266 		goto bad;
2267 	}
2268 
2269 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2270 		got_netmask = B_TRUE;	/* default setting will be correct */
2271 	} else {
2272 		if (af == AF_INET) {
2273 			/*
2274 			 * The IPv4 netmask can be determined either
2275 			 * directly if a prefix length was supplied with
2276 			 * the address or via the netmasks database.  Not
2277 			 * being able to determine it is a common failure,
2278 			 * but it often is not fatal to operation of the
2279 			 * interface.  In that case, a warning will be
2280 			 * printed after the rest of the interface's
2281 			 * parameters have been configured.
2282 			 */
2283 			(void) memset(&netmask4, 0, sizeof (netmask4));
2284 			if (slashp != NULL) {
2285 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
2286 				    (uchar_t *)&netmask4.sin_addr) != 0) {
2287 					*slashp = '/';
2288 					zerror(zlogp, B_FALSE,
2289 					    "%s: invalid prefix length in %s",
2290 					    lifr.lifr_name,
2291 					    nwiftabptr->zone_nwif_address);
2292 					goto bad;
2293 				}
2294 				got_netmask = B_TRUE;
2295 			} else if (getnetmaskbyaddr(in4,
2296 			    &netmask4.sin_addr) == 0) {
2297 				got_netmask = B_TRUE;
2298 			}
2299 			if (got_netmask) {
2300 				netmask4.sin_family = af;
2301 				(void) memcpy(&lifr.lifr_addr, &netmask4,
2302 				    sizeof (netmask4));
2303 			}
2304 		} else {
2305 			(void) memset(&netmask6, 0, sizeof (netmask6));
2306 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
2307 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
2308 				*slashp = '/';
2309 				zerror(zlogp, B_FALSE,
2310 				    "%s: invalid prefix length in %s",
2311 				    lifr.lifr_name,
2312 				    nwiftabptr->zone_nwif_address);
2313 				goto bad;
2314 			}
2315 			got_netmask = B_TRUE;
2316 			netmask6.sin6_family = af;
2317 			(void) memcpy(&lifr.lifr_addr, &netmask6,
2318 			    sizeof (netmask6));
2319 		}
2320 		if (got_netmask &&
2321 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
2322 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
2323 			    lifr.lifr_name);
2324 			goto bad;
2325 		}
2326 
2327 		/*
2328 		 * This doesn't set the broadcast address at all. Rather, it
2329 		 * gets, then sets the interface's address, relying on the fact
2330 		 * that resetting the address will reset the broadcast address.
2331 		 */
2332 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
2333 			zerror(zlogp, B_TRUE, "%s: could not get address",
2334 			    lifr.lifr_name);
2335 			goto bad;
2336 		}
2337 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2338 			zerror(zlogp, B_TRUE,
2339 			    "%s: could not reset broadcast address",
2340 			    lifr.lifr_name);
2341 			goto bad;
2342 		}
2343 	}
2344 
2345 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2346 		zerror(zlogp, B_TRUE, "%s: could not get flags",
2347 		    lifr.lifr_name);
2348 		goto bad;
2349 	}
2350 	lifr.lifr_flags |= IFF_UP;
2351 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
2352 		int save_errno = errno;
2353 		char *zone_using;
2354 
2355 		/*
2356 		 * If we failed with something other than EADDRNOTAVAIL,
2357 		 * then skip to the end.  Otherwise, look up our address,
2358 		 * then call a function to determine which zone is already
2359 		 * using that address.
2360 		 */
2361 		if (errno != EADDRNOTAVAIL) {
2362 			zerror(zlogp, B_TRUE,
2363 			    "%s: could not bring network interface up",
2364 			    lifr.lifr_name);
2365 			goto bad;
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 		zone_using = who_is_using(zlogp, &lifr);
2373 		errno = save_errno;
2374 		if (zone_using == NULL)
2375 			zerror(zlogp, B_TRUE,
2376 			    "%s: could not bring network interface up",
2377 			    lifr.lifr_name);
2378 		else
2379 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2380 			    "interface up: address in use by zone '%s'",
2381 			    lifr.lifr_name, zone_using);
2382 		goto bad;
2383 	}
2384 
2385 	if (!got_netmask) {
2386 		/*
2387 		 * A common, but often non-fatal problem, is that the system
2388 		 * cannot find the netmask for an interface address. This is
2389 		 * often caused by it being only in /etc/inet/netmasks, but
2390 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
2391 		 * in that. This doesn't show up at boot because the netmask
2392 		 * is obtained from /etc/inet/netmasks when no network
2393 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
2394 		 * available. We warn the user here that something like this
2395 		 * has happened and we're just running with a default and
2396 		 * possible incorrect netmask.
2397 		 */
2398 		char buffer[INET6_ADDRSTRLEN];
2399 		void  *addr;
2400 		const char *nomatch = "no matching subnet found in netmasks(4)";
2401 
2402 		if (af == AF_INET)
2403 			addr = &((struct sockaddr_in *)
2404 			    (&lifr.lifr_addr))->sin_addr;
2405 		else
2406 			addr = &((struct sockaddr_in6 *)
2407 			    (&lifr.lifr_addr))->sin6_addr;
2408 
2409 		/*
2410 		 * Find out what netmask the interface is going to be using.
2411 		 * If we just brought up an IPMP data address on an underlying
2412 		 * interface above, the address will have already migrated, so
2413 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
2414 		 * to bring the address up to get the actual netmask).  Just
2415 		 * omit printing the actual netmask in this corner-case.
2416 		 */
2417 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2418 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2419 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2420 			    nomatch);
2421 		} else {
2422 			zerror(zlogp, B_FALSE,
2423 			    "WARNING: %s: %s: %s; using default of %s.",
2424 			    lifr.lifr_name, nomatch, addrstr4, buffer);
2425 		}
2426 	}
2427 
2428 	/*
2429 	 * If a default router was specified for this interface
2430 	 * set the route now. Ignore if already set.
2431 	 */
2432 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2433 		int status;
2434 		char *argv[7];
2435 
2436 		argv[0] = "route";
2437 		argv[1] = "add";
2438 		argv[2] = "-ifp";
2439 		argv[3] = nwiftabptr->zone_nwif_physical;
2440 		argv[4] = "default";
2441 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2442 		argv[6] = NULL;
2443 
2444 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2445 		if (status != 0 && status != EEXIST)
2446 			zerror(zlogp, B_FALSE, "Unable to set route for "
2447 			    "interface %s to %s\n",
2448 			    nwiftabptr->zone_nwif_physical,
2449 			    nwiftabptr->zone_nwif_defrouter);
2450 	}
2451 
2452 	(void) close(s);
2453 	return (Z_OK);
2454 bad:
2455 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
2456 	(void) close(s);
2457 	return (-1);
2458 }
2459 
2460 /*
2461  * Sets up network interfaces based on information from the zone configuration.
2462  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2463  * system.
2464  *
2465  * If anything goes wrong, we log a general error message, attempt to tear down
2466  * whatever we set up, and return an error.
2467  */
2468 static int
2469 configure_shared_network_interfaces(zlog_t *zlogp)
2470 {
2471 	zone_dochandle_t handle;
2472 	struct zone_nwiftab nwiftab, loopback_iftab;
2473 	zoneid_t zoneid;
2474 
2475 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
2476 		zerror(zlogp, B_TRUE, "unable to get zoneid");
2477 		return (-1);
2478 	}
2479 
2480 	if ((handle = zonecfg_init_handle()) == NULL) {
2481 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2482 		return (-1);
2483 	}
2484 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2485 		zerror(zlogp, B_FALSE, "invalid configuration");
2486 		zonecfg_fini_handle(handle);
2487 		return (-1);
2488 	}
2489 	if (zonecfg_setnwifent(handle) == Z_OK) {
2490 		for (;;) {
2491 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2492 				break;
2493 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
2494 			    Z_OK) {
2495 				(void) zonecfg_endnwifent(handle);
2496 				zonecfg_fini_handle(handle);
2497 				return (-1);
2498 			}
2499 		}
2500 		(void) zonecfg_endnwifent(handle);
2501 	}
2502 	zonecfg_fini_handle(handle);
2503 	if (is_system_labeled()) {
2504 		/*
2505 		 * Labeled zones share the loopback interface
2506 		 * so it is not plumbed for shared stack instances.
2507 		 */
2508 		return (0);
2509 	}
2510 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
2511 	    sizeof (loopback_iftab.zone_nwif_physical));
2512 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
2513 	    sizeof (loopback_iftab.zone_nwif_address));
2514 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2515 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2516 		return (-1);
2517 
2518 	/* Always plumb up the IPv6 loopback interface. */
2519 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
2520 	    sizeof (loopback_iftab.zone_nwif_address));
2521 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2522 		return (-1);
2523 	return (0);
2524 }
2525 
2526 static int
2527 add_datalink(zlog_t *zlogp, char *zone_name, char *dlname)
2528 {
2529 	/* First check if it's in use by global zone. */
2530 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2531 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
2532 		errno = EPERM;
2533 		zerror(zlogp, B_TRUE, "WARNING: skipping network interface "
2534 		    "'%s' which is used in the global zone.", dlname);
2535 		return (-1);
2536 	}
2537 
2538 	/* Set zoneid of this link. */
2539 	if (dladm_setzid(dld_handle, dlname, zone_name) != DLADM_STATUS_OK) {
2540 		zerror(zlogp, B_TRUE, "WARNING: unable to add network "
2541 		    "interface '%s'.", dlname);
2542 		return (-1);
2543 	}
2544 
2545 	return (0);
2546 }
2547 
2548 static int
2549 remove_datalink(zlog_t *zlogp, char *dlname)
2550 {
2551 	if (dladm_setzid(dld_handle, dlname, GLOBAL_ZONENAME)
2552 	    != DLADM_STATUS_OK) {
2553 		zerror(zlogp, B_TRUE, "unable to release network "
2554 		    "interface '%s'", dlname);
2555 		return (-1);
2556 	}
2557 	return (0);
2558 }
2559 
2560 /*
2561  * Add the kernel access control information for the interface names.
2562  * If anything goes wrong, we log a general error message, attempt to tear down
2563  * whatever we set up, and return an error.
2564  */
2565 static int
2566 configure_exclusive_network_interfaces(zlog_t *zlogp)
2567 {
2568 	zone_dochandle_t handle;
2569 	struct zone_nwiftab nwiftab;
2570 	char rootpath[MAXPATHLEN];
2571 	char path[MAXPATHLEN];
2572 	di_prof_t prof = NULL;
2573 	boolean_t added = B_FALSE;
2574 
2575 	if ((handle = zonecfg_init_handle()) == NULL) {
2576 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2577 		return (-1);
2578 	}
2579 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2580 		zerror(zlogp, B_FALSE, "invalid configuration");
2581 		zonecfg_fini_handle(handle);
2582 		return (-1);
2583 	}
2584 
2585 	if (zonecfg_setnwifent(handle) != Z_OK) {
2586 		zonecfg_fini_handle(handle);
2587 		return (0);
2588 	}
2589 
2590 	for (;;) {
2591 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2592 			break;
2593 
2594 		if (prof == NULL) {
2595 			if (zone_get_devroot(zone_name, rootpath,
2596 			    sizeof (rootpath)) != Z_OK) {
2597 				(void) zonecfg_endnwifent(handle);
2598 				zonecfg_fini_handle(handle);
2599 				zerror(zlogp, B_TRUE,
2600 				    "unable to determine dev root");
2601 				return (-1);
2602 			}
2603 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2604 			    "/dev");
2605 			if (di_prof_init(path, &prof) != 0) {
2606 				(void) zonecfg_endnwifent(handle);
2607 				zonecfg_fini_handle(handle);
2608 				zerror(zlogp, B_TRUE,
2609 				    "failed to initialize profile");
2610 				return (-1);
2611 			}
2612 		}
2613 
2614 		/*
2615 		 * Create the /dev entry for backward compatibility.
2616 		 * Only create the /dev entry if it's not in use.
2617 		 * Note that the zone still boots when the assigned
2618 		 * interface is inaccessible, used by others, etc.
2619 		 * Also, when vanity naming is used, some interface do
2620 		 * do not have corresponding /dev node names (for example,
2621 		 * vanity named aggregations).  The /dev entry is not
2622 		 * created in that case.  The /dev/net entry is always
2623 		 * accessible.
2624 		 */
2625 		if (add_datalink(zlogp, zone_name, nwiftab.zone_nwif_physical)
2626 		    == 0) {
2627 			added = B_TRUE;
2628 		} else {
2629 			(void) zonecfg_endnwifent(handle);
2630 			zonecfg_fini_handle(handle);
2631 			zerror(zlogp, B_TRUE, "failed to add network device");
2632 			return (-1);
2633 		}
2634 	}
2635 	(void) zonecfg_endnwifent(handle);
2636 	zonecfg_fini_handle(handle);
2637 
2638 	if (prof != NULL && added) {
2639 		if (di_prof_commit(prof) != 0) {
2640 			zerror(zlogp, B_TRUE, "failed to commit profile");
2641 			return (-1);
2642 		}
2643 	}
2644 	if (prof != NULL)
2645 		di_prof_fini(prof);
2646 
2647 	return (0);
2648 }
2649 
2650 /*
2651  * Get the list of the data-links from kernel, and try to remove it
2652  */
2653 static int
2654 unconfigure_exclusive_network_interfaces_run(zlog_t *zlogp, zoneid_t zoneid)
2655 {
2656 	char *dlnames, *ptr;
2657 	int dlnum, dlnum_saved, i;
2658 
2659 	dlnum = 0;
2660 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2661 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2662 		return (-1);
2663 	}
2664 again:
2665 	/* this zone doesn't have any data-links */
2666 	if (dlnum == 0)
2667 		return (0);
2668 
2669 	dlnames = malloc(dlnum * LIFNAMSIZ);
2670 	if (dlnames == NULL) {
2671 		zerror(zlogp, B_TRUE, "memory allocation failed");
2672 		return (-1);
2673 	}
2674 	dlnum_saved = dlnum;
2675 
2676 	if (zone_list_datalink(zoneid, &dlnum, dlnames) != 0) {
2677 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2678 		free(dlnames);
2679 		return (-1);
2680 	}
2681 	if (dlnum_saved < dlnum) {
2682 		/* list increased, try again */
2683 		free(dlnames);
2684 		goto again;
2685 	}
2686 	ptr = dlnames;
2687 	for (i = 0; i < dlnum; i++) {
2688 		/* Remove access control information */
2689 		if (remove_datalink(zlogp, ptr) != 0) {
2690 			free(dlnames);
2691 			return (-1);
2692 		}
2693 		ptr += LIFNAMSIZ;
2694 	}
2695 	free(dlnames);
2696 	return (0);
2697 }
2698 
2699 /*
2700  * Get the list of the data-links from configuration, and try to remove it
2701  */
2702 static int
2703 unconfigure_exclusive_network_interfaces_static(zlog_t *zlogp)
2704 {
2705 	zone_dochandle_t handle;
2706 	struct zone_nwiftab nwiftab;
2707 
2708 	if ((handle = zonecfg_init_handle()) == NULL) {
2709 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2710 		return (-1);
2711 	}
2712 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2713 		zerror(zlogp, B_FALSE, "invalid configuration");
2714 		zonecfg_fini_handle(handle);
2715 		return (-1);
2716 	}
2717 	if (zonecfg_setnwifent(handle) != Z_OK) {
2718 		zonecfg_fini_handle(handle);
2719 		return (0);
2720 	}
2721 	for (;;) {
2722 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2723 			break;
2724 		/* Remove access control information */
2725 		if (remove_datalink(zlogp, nwiftab.zone_nwif_physical)
2726 		    != 0) {
2727 			(void) zonecfg_endnwifent(handle);
2728 			zonecfg_fini_handle(handle);
2729 			return (-1);
2730 		}
2731 	}
2732 	(void) zonecfg_endnwifent(handle);
2733 	zonecfg_fini_handle(handle);
2734 	return (0);
2735 }
2736 
2737 /*
2738  * Remove the access control information from the kernel for the exclusive
2739  * network interfaces.
2740  */
2741 static int
2742 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2743 {
2744 	if (unconfigure_exclusive_network_interfaces_run(zlogp, zoneid) != 0) {
2745 		return (unconfigure_exclusive_network_interfaces_static(zlogp));
2746 	}
2747 
2748 	return (0);
2749 }
2750 
2751 static int
2752 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
2753     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
2754 {
2755 	int fd;
2756 	struct strioctl ioc;
2757 	tcp_ioc_abort_conn_t conn;
2758 	int error;
2759 
2760 	conn.ac_local = *local;
2761 	conn.ac_remote = *remote;
2762 	conn.ac_start = TCPS_SYN_SENT;
2763 	conn.ac_end = TCPS_TIME_WAIT;
2764 	conn.ac_zoneid = zoneid;
2765 
2766 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
2767 	ioc.ic_timout = -1; /* infinite timeout */
2768 	ioc.ic_len = sizeof (conn);
2769 	ioc.ic_dp = (char *)&conn;
2770 
2771 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
2772 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
2773 		return (-1);
2774 	}
2775 
2776 	error = ioctl(fd, I_STR, &ioc);
2777 	(void) close(fd);
2778 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
2779 		return (0);
2780 	return (-1);
2781 }
2782 
2783 static int
2784 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
2785 {
2786 	struct sockaddr_storage l, r;
2787 	struct sockaddr_in *local, *remote;
2788 	struct sockaddr_in6 *local6, *remote6;
2789 	int error;
2790 
2791 	/*
2792 	 * Abort IPv4 connections.
2793 	 */
2794 	bzero(&l, sizeof (*local));
2795 	local = (struct sockaddr_in *)&l;
2796 	local->sin_family = AF_INET;
2797 	local->sin_addr.s_addr = INADDR_ANY;
2798 	local->sin_port = 0;
2799 
2800 	bzero(&r, sizeof (*remote));
2801 	remote = (struct sockaddr_in *)&r;
2802 	remote->sin_family = AF_INET;
2803 	remote->sin_addr.s_addr = INADDR_ANY;
2804 	remote->sin_port = 0;
2805 
2806 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
2807 		return (error);
2808 
2809 	/*
2810 	 * Abort IPv6 connections.
2811 	 */
2812 	bzero(&l, sizeof (*local6));
2813 	local6 = (struct sockaddr_in6 *)&l;
2814 	local6->sin6_family = AF_INET6;
2815 	local6->sin6_port = 0;
2816 	local6->sin6_addr = in6addr_any;
2817 
2818 	bzero(&r, sizeof (*remote6));
2819 	remote6 = (struct sockaddr_in6 *)&r;
2820 	remote6->sin6_family = AF_INET6;
2821 	remote6->sin6_port = 0;
2822 	remote6->sin6_addr = in6addr_any;
2823 
2824 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
2825 		return (error);
2826 	return (0);
2827 }
2828 
2829 static int
2830 get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
2831 {
2832 	int error = -1;
2833 	zone_dochandle_t handle;
2834 	char *privname = NULL;
2835 
2836 	if ((handle = zonecfg_init_handle()) == NULL) {
2837 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2838 		return (-1);
2839 	}
2840 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2841 		zerror(zlogp, B_FALSE, "invalid configuration");
2842 		zonecfg_fini_handle(handle);
2843 		return (-1);
2844 	}
2845 
2846 	if (ALT_MOUNT(mount_cmd)) {
2847 		zone_iptype_t	iptype;
2848 		const char	*curr_iptype;
2849 
2850 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
2851 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
2852 			zonecfg_fini_handle(handle);
2853 			return (-1);
2854 		}
2855 
2856 		switch (iptype) {
2857 		case ZS_SHARED:
2858 			curr_iptype = "shared";
2859 			break;
2860 		case ZS_EXCLUSIVE:
2861 			curr_iptype = "exclusive";
2862 			break;
2863 		}
2864 
2865 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
2866 			zonecfg_fini_handle(handle);
2867 			return (0);
2868 		}
2869 		zerror(zlogp, B_FALSE,
2870 		    "failed to determine the zone's default privilege set");
2871 		zonecfg_fini_handle(handle);
2872 		return (-1);
2873 	}
2874 
2875 	switch (zonecfg_get_privset(handle, privs, &privname)) {
2876 	case Z_OK:
2877 		error = 0;
2878 		break;
2879 	case Z_PRIV_PROHIBITED:
2880 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
2881 		    "within the zone's privilege set", privname);
2882 		break;
2883 	case Z_PRIV_REQUIRED:
2884 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
2885 		    "from the zone's privilege set", privname);
2886 		break;
2887 	case Z_PRIV_UNKNOWN:
2888 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
2889 		    "in the zone's privilege set", privname);
2890 		break;
2891 	default:
2892 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
2893 		    "privilege set");
2894 		break;
2895 	}
2896 
2897 	free(privname);
2898 	zonecfg_fini_handle(handle);
2899 	return (error);
2900 }
2901 
2902 static int
2903 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
2904 {
2905 	nvlist_t *nvl = NULL;
2906 	char *nvl_packed = NULL;
2907 	size_t nvl_size = 0;
2908 	nvlist_t **nvlv = NULL;
2909 	int rctlcount = 0;
2910 	int error = -1;
2911 	zone_dochandle_t handle;
2912 	struct zone_rctltab rctltab;
2913 	rctlblk_t *rctlblk = NULL;
2914 
2915 	*bufp = NULL;
2916 	*bufsizep = 0;
2917 
2918 	if ((handle = zonecfg_init_handle()) == NULL) {
2919 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2920 		return (-1);
2921 	}
2922 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2923 		zerror(zlogp, B_FALSE, "invalid configuration");
2924 		zonecfg_fini_handle(handle);
2925 		return (-1);
2926 	}
2927 
2928 	rctltab.zone_rctl_valptr = NULL;
2929 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
2930 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
2931 		goto out;
2932 	}
2933 
2934 	if (zonecfg_setrctlent(handle) != Z_OK) {
2935 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
2936 		goto out;
2937 	}
2938 
2939 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
2940 		zerror(zlogp, B_TRUE, "memory allocation failed");
2941 		goto out;
2942 	}
2943 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
2944 		struct zone_rctlvaltab *rctlval;
2945 		uint_t i, count;
2946 		const char *name = rctltab.zone_rctl_name;
2947 
2948 		/* zoneadm should have already warned about unknown rctls. */
2949 		if (!zonecfg_is_rctl(name)) {
2950 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
2951 			rctltab.zone_rctl_valptr = NULL;
2952 			continue;
2953 		}
2954 		count = 0;
2955 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
2956 		    rctlval = rctlval->zone_rctlval_next) {
2957 			count++;
2958 		}
2959 		if (count == 0) {	/* ignore */
2960 			continue;	/* Nothing to free */
2961 		}
2962 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
2963 			goto out;
2964 		i = 0;
2965 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
2966 		    rctlval = rctlval->zone_rctlval_next, i++) {
2967 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
2968 				zerror(zlogp, B_TRUE, "%s failed",
2969 				    "nvlist_alloc");
2970 				goto out;
2971 			}
2972 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
2973 			    != Z_OK) {
2974 				zerror(zlogp, B_FALSE, "invalid rctl value: "
2975 				    "(priv=%s,limit=%s,action=%s)",
2976 				    rctlval->zone_rctlval_priv,
2977 				    rctlval->zone_rctlval_limit,
2978 				    rctlval->zone_rctlval_action);
2979 				goto out;
2980 			}
2981 			if (!zonecfg_valid_rctl(name, rctlblk)) {
2982 				zerror(zlogp, B_FALSE,
2983 				    "(priv=%s,limit=%s,action=%s) is not a "
2984 				    "valid value for rctl '%s'",
2985 				    rctlval->zone_rctlval_priv,
2986 				    rctlval->zone_rctlval_limit,
2987 				    rctlval->zone_rctlval_action,
2988 				    name);
2989 				goto out;
2990 			}
2991 			if (nvlist_add_uint64(nvlv[i], "privilege",
2992 			    rctlblk_get_privilege(rctlblk)) != 0) {
2993 				zerror(zlogp, B_FALSE, "%s failed",
2994 				    "nvlist_add_uint64");
2995 				goto out;
2996 			}
2997 			if (nvlist_add_uint64(nvlv[i], "limit",
2998 			    rctlblk_get_value(rctlblk)) != 0) {
2999 				zerror(zlogp, B_FALSE, "%s failed",
3000 				    "nvlist_add_uint64");
3001 				goto out;
3002 			}
3003 			if (nvlist_add_uint64(nvlv[i], "action",
3004 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
3005 			    != 0) {
3006 				zerror(zlogp, B_FALSE, "%s failed",
3007 				    "nvlist_add_uint64");
3008 				goto out;
3009 			}
3010 		}
3011 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3012 		rctltab.zone_rctl_valptr = NULL;
3013 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
3014 		    != 0) {
3015 			zerror(zlogp, B_FALSE, "%s failed",
3016 			    "nvlist_add_nvlist_array");
3017 			goto out;
3018 		}
3019 		for (i = 0; i < count; i++)
3020 			nvlist_free(nvlv[i]);
3021 		free(nvlv);
3022 		nvlv = NULL;
3023 		rctlcount++;
3024 	}
3025 	(void) zonecfg_endrctlent(handle);
3026 
3027 	if (rctlcount == 0) {
3028 		error = 0;
3029 		goto out;
3030 	}
3031 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
3032 	    != 0) {
3033 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
3034 		goto out;
3035 	}
3036 
3037 	error = 0;
3038 	*bufp = nvl_packed;
3039 	*bufsizep = nvl_size;
3040 
3041 out:
3042 	free(rctlblk);
3043 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3044 	if (error && nvl_packed != NULL)
3045 		free(nvl_packed);
3046 	if (nvl != NULL)
3047 		nvlist_free(nvl);
3048 	if (nvlv != NULL)
3049 		free(nvlv);
3050 	if (handle != NULL)
3051 		zonecfg_fini_handle(handle);
3052 	return (error);
3053 }
3054 
3055 static int
3056 get_implicit_datasets(zlog_t *zlogp, char **retstr)
3057 {
3058 	char cmdbuf[2 * MAXPATHLEN];
3059 
3060 	if (query_hook[0] == '\0')
3061 		return (0);
3062 
3063 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3064 	    > sizeof (cmdbuf))
3065 		return (-1);
3066 
3067 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3068 		return (-1);
3069 
3070 	return (0);
3071 }
3072 
3073 static int
3074 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3075 {
3076 	zone_dochandle_t handle;
3077 	struct zone_dstab dstab;
3078 	size_t total, offset, len;
3079 	int error = -1;
3080 	char *str = NULL;
3081 	char *implicit_datasets = NULL;
3082 	int implicit_len = 0;
3083 
3084 	*bufp = NULL;
3085 	*bufsizep = 0;
3086 
3087 	if ((handle = zonecfg_init_handle()) == NULL) {
3088 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3089 		return (-1);
3090 	}
3091 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3092 		zerror(zlogp, B_FALSE, "invalid configuration");
3093 		zonecfg_fini_handle(handle);
3094 		return (-1);
3095 	}
3096 
3097 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3098 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3099 		goto out;
3100 	}
3101 
3102 	if (zonecfg_setdsent(handle) != Z_OK) {
3103 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3104 		goto out;
3105 	}
3106 
3107 	total = 0;
3108 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3109 		total += strlen(dstab.zone_dataset_name) + 1;
3110 	(void) zonecfg_enddsent(handle);
3111 
3112 	if (implicit_datasets != NULL)
3113 		implicit_len = strlen(implicit_datasets);
3114 	if (implicit_len > 0)
3115 		total += implicit_len + 1;
3116 
3117 	if (total == 0) {
3118 		error = 0;
3119 		goto out;
3120 	}
3121 
3122 	if ((str = malloc(total)) == NULL) {
3123 		zerror(zlogp, B_TRUE, "memory allocation failed");
3124 		goto out;
3125 	}
3126 
3127 	if (zonecfg_setdsent(handle) != Z_OK) {
3128 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3129 		goto out;
3130 	}
3131 	offset = 0;
3132 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3133 		len = strlen(dstab.zone_dataset_name);
3134 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3135 		    total - offset);
3136 		offset += len;
3137 		if (offset < total - 1)
3138 			str[offset++] = ',';
3139 	}
3140 	(void) zonecfg_enddsent(handle);
3141 
3142 	if (implicit_len > 0)
3143 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3144 
3145 	error = 0;
3146 	*bufp = str;
3147 	*bufsizep = total;
3148 
3149 out:
3150 	if (error != 0 && str != NULL)
3151 		free(str);
3152 	if (handle != NULL)
3153 		zonecfg_fini_handle(handle);
3154 	if (implicit_datasets != NULL)
3155 		free(implicit_datasets);
3156 
3157 	return (error);
3158 }
3159 
3160 static int
3161 validate_datasets(zlog_t *zlogp)
3162 {
3163 	zone_dochandle_t handle;
3164 	struct zone_dstab dstab;
3165 	zfs_handle_t *zhp;
3166 	libzfs_handle_t *hdl;
3167 
3168 	if ((handle = zonecfg_init_handle()) == NULL) {
3169 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3170 		return (-1);
3171 	}
3172 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3173 		zerror(zlogp, B_FALSE, "invalid configuration");
3174 		zonecfg_fini_handle(handle);
3175 		return (-1);
3176 	}
3177 
3178 	if (zonecfg_setdsent(handle) != Z_OK) {
3179 		zerror(zlogp, B_FALSE, "invalid configuration");
3180 		zonecfg_fini_handle(handle);
3181 		return (-1);
3182 	}
3183 
3184 	if ((hdl = libzfs_init()) == NULL) {
3185 		zerror(zlogp, B_FALSE, "opening ZFS library");
3186 		zonecfg_fini_handle(handle);
3187 		return (-1);
3188 	}
3189 
3190 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3191 
3192 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3193 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3194 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3195 			    dstab.zone_dataset_name);
3196 			zonecfg_fini_handle(handle);
3197 			libzfs_fini(hdl);
3198 			return (-1);
3199 		}
3200 
3201 		/*
3202 		 * Automatically set the 'zoned' property.  We check the value
3203 		 * first because we'll get EPERM if it is already set.
3204 		 */
3205 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3206 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3207 		    "on") != 0) {
3208 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3209 			    "property for ZFS dataset '%s'\n",
3210 			    dstab.zone_dataset_name);
3211 			zonecfg_fini_handle(handle);
3212 			zfs_close(zhp);
3213 			libzfs_fini(hdl);
3214 			return (-1);
3215 		}
3216 
3217 		zfs_close(zhp);
3218 	}
3219 	(void) zonecfg_enddsent(handle);
3220 
3221 	zonecfg_fini_handle(handle);
3222 	libzfs_fini(hdl);
3223 
3224 	return (0);
3225 }
3226 
3227 /*
3228  * Mount lower level home directories into/from current zone
3229  * Share exported directories specified in dfstab for zone
3230  */
3231 static int
3232 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
3233 {
3234 	zoneid_t *zids = NULL;
3235 	priv_set_t *zid_privs;
3236 	const priv_impl_info_t *ip = NULL;
3237 	uint_t nzents_saved;
3238 	uint_t nzents;
3239 	int i;
3240 	char readonly[] = "ro";
3241 	struct zone_fstab lower_fstab;
3242 	char *argv[4];
3243 
3244 	if (!is_system_labeled())
3245 		return (0);
3246 
3247 	if (zid_label == NULL) {
3248 		zid_label = m_label_alloc(MAC_LABEL);
3249 		if (zid_label == NULL)
3250 			return (-1);
3251 	}
3252 
3253 	/* Make sure our zone has an /export/home dir */
3254 	(void) make_one_dir(zlogp, rootpath, "/export/home",
3255 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3256 
3257 	lower_fstab.zone_fs_raw[0] = '\0';
3258 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
3259 	    sizeof (lower_fstab.zone_fs_type));
3260 	lower_fstab.zone_fs_options = NULL;
3261 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
3262 
3263 	/*
3264 	 * Get the list of zones from the kernel
3265 	 */
3266 	if (zone_list(NULL, &nzents) != 0) {
3267 		zerror(zlogp, B_TRUE, "unable to list zones");
3268 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3269 		return (-1);
3270 	}
3271 again:
3272 	if (nzents == 0) {
3273 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3274 		return (-1);
3275 	}
3276 
3277 	zids = malloc(nzents * sizeof (zoneid_t));
3278 	if (zids == NULL) {
3279 		zerror(zlogp, B_TRUE, "memory allocation failed");
3280 		return (-1);
3281 	}
3282 	nzents_saved = nzents;
3283 
3284 	if (zone_list(zids, &nzents) != 0) {
3285 		zerror(zlogp, B_TRUE, "unable to list zones");
3286 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3287 		free(zids);
3288 		return (-1);
3289 	}
3290 	if (nzents != nzents_saved) {
3291 		/* list changed, try again */
3292 		free(zids);
3293 		goto again;
3294 	}
3295 
3296 	ip = getprivimplinfo();
3297 	if ((zid_privs = priv_allocset()) == NULL) {
3298 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3299 		zonecfg_free_fs_option_list(
3300 		    lower_fstab.zone_fs_options);
3301 		free(zids);
3302 		return (-1);
3303 	}
3304 
3305 	for (i = 0; i < nzents; i++) {
3306 		char zid_name[ZONENAME_MAX];
3307 		zone_state_t zid_state;
3308 		char zid_rpath[MAXPATHLEN];
3309 		struct stat stat_buf;
3310 
3311 		if (zids[i] == GLOBAL_ZONEID)
3312 			continue;
3313 
3314 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3315 			continue;
3316 
3317 		/*
3318 		 * Do special setup for the zone we are booting
3319 		 */
3320 		if (strcmp(zid_name, zone_name) == 0) {
3321 			struct zone_fstab autofs_fstab;
3322 			char map_path[MAXPATHLEN];
3323 			int fd;
3324 
3325 			/*
3326 			 * Create auto_home_<zone> map for this zone
3327 			 * in the global zone. The non-global zone entry
3328 			 * will be created by automount when the zone
3329 			 * is booted.
3330 			 */
3331 
3332 			(void) snprintf(autofs_fstab.zone_fs_special,
3333 			    MAXPATHLEN, "auto_home_%s", zid_name);
3334 
3335 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
3336 			    "/zone/%s/home", zid_name);
3337 
3338 			(void) snprintf(map_path, sizeof (map_path),
3339 			    "/etc/%s", autofs_fstab.zone_fs_special);
3340 			/*
3341 			 * If the map file doesn't exist create a template
3342 			 */
3343 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
3344 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
3345 				int len;
3346 				char map_rec[MAXPATHLEN];
3347 
3348 				len = snprintf(map_rec, sizeof (map_rec),
3349 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
3350 				    autofs_fstab.zone_fs_special, rootpath);
3351 				(void) write(fd, map_rec, len);
3352 				(void) close(fd);
3353 			}
3354 
3355 			/*
3356 			 * Mount auto_home_<zone> in the global zone if absent.
3357 			 * If it's already of type autofs, then
3358 			 * don't mount it again.
3359 			 */
3360 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
3361 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
3362 				char optstr[] = "indirect,ignore,nobrowse";
3363 
3364 				(void) make_one_dir(zlogp, "",
3365 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
3366 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3367 
3368 				/*
3369 				 * Mount will fail if automounter has already
3370 				 * processed the auto_home_<zonename> map
3371 				 */
3372 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
3373 				    autofs_fstab.zone_fs_special,
3374 				    autofs_fstab.zone_fs_dir);
3375 			}
3376 			continue;
3377 		}
3378 
3379 
3380 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
3381 		    (zid_state != ZONE_STATE_READY &&
3382 		    zid_state != ZONE_STATE_RUNNING))
3383 			/* Skip over zones without mounted filesystems */
3384 			continue;
3385 
3386 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3387 		    sizeof (m_label_t)) < 0)
3388 			/* Skip over zones with unspecified label */
3389 			continue;
3390 
3391 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3392 		    sizeof (zid_rpath)) == -1)
3393 			/* Skip over zones with bad path */
3394 			continue;
3395 
3396 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
3397 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
3398 			/* Skip over zones with bad privs */
3399 			continue;
3400 
3401 		/*
3402 		 * Reading down is valid according to our label model
3403 		 * but some customers want to disable it because it
3404 		 * allows execute down and other possible attacks.
3405 		 * Therefore, we restrict this feature to zones that
3406 		 * have the NET_MAC_AWARE privilege which is required
3407 		 * for NFS read-down semantics.
3408 		 */
3409 		if ((bldominates(zlabel, zid_label)) &&
3410 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
3411 			/*
3412 			 * Our zone dominates this one.
3413 			 * Create a lofs mount from lower zone's /export/home
3414 			 */
3415 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3416 			    "%s/zone/%s/export/home", rootpath, zid_name);
3417 
3418 			/*
3419 			 * If the target is already an LOFS mount
3420 			 * then don't do it again.
3421 			 */
3422 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3423 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3424 
3425 				if (snprintf(lower_fstab.zone_fs_special,
3426 				    MAXPATHLEN, "%s/export",
3427 				    zid_rpath) > MAXPATHLEN)
3428 					continue;
3429 
3430 				/*
3431 				 * Make sure the lower-level home exists
3432 				 */
3433 				if (make_one_dir(zlogp,
3434 				    lower_fstab.zone_fs_special, "/home",
3435 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
3436 				    DEFAULT_DIR_GROUP) != 0)
3437 					continue;
3438 
3439 				(void) strlcat(lower_fstab.zone_fs_special,
3440 				    "/home", MAXPATHLEN);
3441 
3442 				/*
3443 				 * Mount can fail because the lower-level
3444 				 * zone may have already done a mount up.
3445 				 */
3446 				(void) mount_one(zlogp, &lower_fstab, "",
3447 				    Z_MNT_BOOT);
3448 			}
3449 		} else if ((bldominates(zid_label, zlabel)) &&
3450 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
3451 			/*
3452 			 * This zone dominates our zone.
3453 			 * Create a lofs mount from our zone's /export/home
3454 			 */
3455 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3456 			    "%s/zone/%s/export/home", zid_rpath,
3457 			    zone_name) > MAXPATHLEN)
3458 				continue;
3459 
3460 			/*
3461 			 * If the target is already an LOFS mount
3462 			 * then don't do it again.
3463 			 */
3464 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3465 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3466 
3467 				(void) snprintf(lower_fstab.zone_fs_special,
3468 				    MAXPATHLEN, "%s/export/home", rootpath);
3469 
3470 				/*
3471 				 * Mount can fail because the higher-level
3472 				 * zone may have already done a mount down.
3473 				 */
3474 				(void) mount_one(zlogp, &lower_fstab, "",
3475 				    Z_MNT_BOOT);
3476 			}
3477 		}
3478 	}
3479 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3480 	priv_freeset(zid_privs);
3481 	free(zids);
3482 
3483 	/*
3484 	 * Now share any exported directories from this zone.
3485 	 * Each zone can have its own dfstab.
3486 	 */
3487 
3488 	argv[0] = "zoneshare";
3489 	argv[1] = "-z";
3490 	argv[2] = zone_name;
3491 	argv[3] = NULL;
3492 
3493 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
3494 	/* Don't check for errors since they don't affect the zone */
3495 
3496 	return (0);
3497 }
3498 
3499 /*
3500  * Unmount lofs mounts from higher level zones
3501  * Unshare nfs exported directories
3502  */
3503 static void
3504 tsol_unmounts(zlog_t *zlogp, char *zone_name)
3505 {
3506 	zoneid_t *zids = NULL;
3507 	uint_t nzents_saved;
3508 	uint_t nzents;
3509 	int i;
3510 	char *argv[4];
3511 	char path[MAXPATHLEN];
3512 
3513 	if (!is_system_labeled())
3514 		return;
3515 
3516 	/*
3517 	 * Get the list of zones from the kernel
3518 	 */
3519 	if (zone_list(NULL, &nzents) != 0) {
3520 		return;
3521 	}
3522 
3523 	if (zid_label == NULL) {
3524 		zid_label = m_label_alloc(MAC_LABEL);
3525 		if (zid_label == NULL)
3526 			return;
3527 	}
3528 
3529 again:
3530 	if (nzents == 0)
3531 		return;
3532 
3533 	zids = malloc(nzents * sizeof (zoneid_t));
3534 	if (zids == NULL) {
3535 		zerror(zlogp, B_TRUE, "memory allocation failed");
3536 		return;
3537 	}
3538 	nzents_saved = nzents;
3539 
3540 	if (zone_list(zids, &nzents) != 0) {
3541 		free(zids);
3542 		return;
3543 	}
3544 	if (nzents != nzents_saved) {
3545 		/* list changed, try again */
3546 		free(zids);
3547 		goto again;
3548 	}
3549 
3550 	for (i = 0; i < nzents; i++) {
3551 		char zid_name[ZONENAME_MAX];
3552 		zone_state_t zid_state;
3553 		char zid_rpath[MAXPATHLEN];
3554 
3555 		if (zids[i] == GLOBAL_ZONEID)
3556 			continue;
3557 
3558 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3559 			continue;
3560 
3561 		/*
3562 		 * Skip the zone we are halting
3563 		 */
3564 		if (strcmp(zid_name, zone_name) == 0)
3565 			continue;
3566 
3567 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
3568 		    sizeof (zid_state)) < 0) ||
3569 		    (zid_state < ZONE_IS_READY))
3570 			/* Skip over zones without mounted filesystems */
3571 			continue;
3572 
3573 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3574 		    sizeof (m_label_t)) < 0)
3575 			/* Skip over zones with unspecified label */
3576 			continue;
3577 
3578 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3579 		    sizeof (zid_rpath)) == -1)
3580 			/* Skip over zones with bad path */
3581 			continue;
3582 
3583 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
3584 			/*
3585 			 * This zone dominates our zone.
3586 			 * Unmount the lofs mount of our zone's /export/home
3587 			 */
3588 
3589 			if (snprintf(path, MAXPATHLEN,
3590 			    "%s/zone/%s/export/home", zid_rpath,
3591 			    zone_name) > MAXPATHLEN)
3592 				continue;
3593 
3594 			/* Skip over mount failures */
3595 			(void) umount(path);
3596 		}
3597 	}
3598 	free(zids);
3599 
3600 	/*
3601 	 * Unmount global zone autofs trigger for this zone
3602 	 */
3603 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
3604 	/* Skip over mount failures */
3605 	(void) umount(path);
3606 
3607 	/*
3608 	 * Next unshare any exported directories from this zone.
3609 	 */
3610 
3611 	argv[0] = "zoneunshare";
3612 	argv[1] = "-z";
3613 	argv[2] = zone_name;
3614 	argv[3] = NULL;
3615 
3616 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
3617 	/* Don't check for errors since they don't affect the zone */
3618 
3619 	/*
3620 	 * Finally, deallocate any devices in the zone.
3621 	 */
3622 
3623 	argv[0] = "deallocate";
3624 	argv[1] = "-Isz";
3625 	argv[2] = zone_name;
3626 	argv[3] = NULL;
3627 
3628 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
3629 	/* Don't check for errors since they don't affect the zone */
3630 }
3631 
3632 /*
3633  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
3634  * this zone.
3635  */
3636 static tsol_zcent_t *
3637 get_zone_label(zlog_t *zlogp, priv_set_t *privs)
3638 {
3639 	FILE *fp;
3640 	tsol_zcent_t *zcent = NULL;
3641 	char line[MAXTNZLEN];
3642 
3643 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
3644 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
3645 		return (NULL);
3646 	}
3647 
3648 	while (fgets(line, sizeof (line), fp) != NULL) {
3649 		/*
3650 		 * Check for malformed database
3651 		 */
3652 		if (strlen(line) == MAXTNZLEN - 1)
3653 			break;
3654 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
3655 			continue;
3656 		if (strcmp(zcent->zc_name, zone_name) == 0)
3657 			break;
3658 		tsol_freezcent(zcent);
3659 		zcent = NULL;
3660 	}
3661 	(void) fclose(fp);
3662 
3663 	if (zcent == NULL) {
3664 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
3665 		    "See tnzonecfg(4)");
3666 	} else {
3667 		if (zlabel == NULL)
3668 			zlabel = m_label_alloc(MAC_LABEL);
3669 		/*
3670 		 * Save this zone's privileges for later read-down processing
3671 		 */
3672 		if ((zprivs = priv_allocset()) == NULL) {
3673 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3674 			return (NULL);
3675 		} else {
3676 			priv_copyset(privs, zprivs);
3677 		}
3678 	}
3679 	return (zcent);
3680 }
3681 
3682 /*
3683  * Add the Trusted Extensions multi-level ports for this zone.
3684  */
3685 static void
3686 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
3687 {
3688 	tsol_mlp_t *mlp;
3689 	tsol_mlpent_t tsme;
3690 
3691 	if (!is_system_labeled())
3692 		return;
3693 
3694 	tsme.tsme_zoneid = zoneid;
3695 	tsme.tsme_flags = 0;
3696 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
3697 		tsme.tsme_mlp = *mlp;
3698 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
3699 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
3700 			    "on %d-%d/%d", mlp->mlp_port,
3701 			    mlp->mlp_port_upper, mlp->mlp_ipp);
3702 		}
3703 	}
3704 
3705 	tsme.tsme_flags = TSOL_MEF_SHARED;
3706 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
3707 		tsme.tsme_mlp = *mlp;
3708 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
3709 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
3710 			    "on %d-%d/%d", mlp->mlp_port,
3711 			    mlp->mlp_port_upper, mlp->mlp_ipp);
3712 		}
3713 	}
3714 }
3715 
3716 static void
3717 remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
3718 {
3719 	tsol_mlpent_t tsme;
3720 
3721 	if (!is_system_labeled())
3722 		return;
3723 
3724 	(void) memset(&tsme, 0, sizeof (tsme));
3725 	tsme.tsme_zoneid = zoneid;
3726 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
3727 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
3728 }
3729 
3730 int
3731 prtmount(const char *fs, void *x) {
3732 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
3733 	return (0);
3734 }
3735 
3736 /*
3737  * Look for zones running on the main system that are using this root (or any
3738  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3739  * is found or if we can't tell.
3740  */
3741 static boolean_t
3742 duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
3743 {
3744 	zoneid_t *zids = NULL;
3745 	uint_t nzids = 0;
3746 	boolean_t retv;
3747 	int rlen, zlen;
3748 	char zroot[MAXPATHLEN];
3749 	char zonename[ZONENAME_MAX];
3750 
3751 	for (;;) {
3752 		nzids += 10;
3753 		zids = malloc(nzids * sizeof (*zids));
3754 		if (zids == NULL) {
3755 			zerror(zlogp, B_TRUE, "memory allocation failed");
3756 			return (B_TRUE);
3757 		}
3758 		if (zone_list(zids, &nzids) == 0)
3759 			break;
3760 		free(zids);
3761 	}
3762 	retv = B_FALSE;
3763 	rlen = strlen(rootpath);
3764 	while (nzids > 0) {
3765 		/*
3766 		 * Ignore errors; they just mean that the zone has disappeared
3767 		 * while we were busy.
3768 		 */
3769 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3770 		    sizeof (zroot)) == -1)
3771 			continue;
3772 		zlen = strlen(zroot);
3773 		if (zlen > rlen)
3774 			zlen = rlen;
3775 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3776 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3777 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3778 			if (getzonenamebyid(zids[nzids], zonename,
3779 			    sizeof (zonename)) == -1)
3780 				(void) snprintf(zonename, sizeof (zonename),
3781 				    "id %d", (int)zids[nzids]);
3782 			zerror(zlogp, B_FALSE,
3783 			    "zone root %s already in use by zone %s",
3784 			    rootpath, zonename);
3785 			retv = B_TRUE;
3786 			break;
3787 		}
3788 	}
3789 	free(zids);
3790 	return (retv);
3791 }
3792 
3793 /*
3794  * Search for loopback mounts that use this same source node (same device and
3795  * inode).  Return B_TRUE if there is one or if we can't tell.
3796  */
3797 static boolean_t
3798 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3799 {
3800 	struct stat64 rst, zst;
3801 	struct mnttab *mnp;
3802 
3803 	if (stat64(rootpath, &rst) == -1) {
3804 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3805 		return (B_TRUE);
3806 	}
3807 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3808 		return (B_TRUE);
3809 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3810 		if (mnp->mnt_fstype == NULL ||
3811 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3812 			continue;
3813 		/* We're looking at a loopback mount.  Stat it. */
3814 		if (mnp->mnt_special != NULL &&
3815 		    stat64(mnp->mnt_special, &zst) != -1 &&
3816 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3817 			zerror(zlogp, B_FALSE,
3818 			    "zone root %s is reachable through %s",
3819 			    rootpath, mnp->mnt_mountp);
3820 			return (B_TRUE);
3821 		}
3822 	}
3823 	return (B_FALSE);
3824 }
3825 
3826 /*
3827  * Set memory cap and pool info for the zone's resource management
3828  * configuration.
3829  */
3830 static int
3831 setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
3832 {
3833 	int res;
3834 	uint64_t tmp;
3835 	struct zone_mcaptab mcap;
3836 	char sched[MAXNAMELEN];
3837 	zone_dochandle_t handle = NULL;
3838 	char pool_err[128];
3839 
3840 	if ((handle = zonecfg_init_handle()) == NULL) {
3841 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3842 		return (Z_BAD_HANDLE);
3843 	}
3844 
3845 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
3846 		zerror(zlogp, B_FALSE, "invalid configuration");
3847 		zonecfg_fini_handle(handle);
3848 		return (res);
3849 	}
3850 
3851 	/*
3852 	 * If a memory cap is configured, set the cap in the kernel using
3853 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
3854 	 */
3855 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
3856 		uint64_t num;
3857 		char smf_err[128];
3858 
3859 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
3860 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
3861 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
3862 			zonecfg_fini_handle(handle);
3863 			return (Z_INVAL);
3864 		}
3865 
3866 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
3867 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
3868 			    "failed: %s", smf_err);
3869 			zonecfg_fini_handle(handle);
3870 			return (Z_INVAL);
3871 		}
3872 	}
3873 
3874 	/* Get the scheduling class set in the zone configuration. */
3875 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
3876 	    strlen(sched) > 0) {
3877 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
3878 		    strlen(sched)) == -1)
3879 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
3880 			    "default scheduling class");
3881 
3882 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
3883 	    == Z_OK) {
3884 		/*
3885 		 * If the zone has the zone.cpu-shares rctl set then we want to
3886 		 * use the Fair Share Scheduler (FSS) for processes in the
3887 		 * zone.  Check what scheduling class the zone would be running
3888 		 * in by default so we can print a warning and modify the class
3889 		 * if we wouldn't be using FSS.
3890 		 */
3891 		char class_name[PC_CLNMSZ];
3892 
3893 		if (zonecfg_get_dflt_sched_class(handle, class_name,
3894 		    sizeof (class_name)) != Z_OK) {
3895 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
3896 			    "the zone's scheduling class");
3897 
3898 		} else if (strcmp("FSS", class_name) != 0) {
3899 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
3900 			    "rctl is set but\nFSS is not the default "
3901 			    "scheduling class for\nthis zone.  FSS will be "
3902 			    "used for processes\nin the zone but to get the "
3903 			    "full benefit of FSS,\nit should be the default "
3904 			    "scheduling class.\nSee dispadmin(1M) for more "
3905 			    "details.");
3906 
3907 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
3908 			    strlen("FSS")) == -1)
3909 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
3910 				    "zone scheduling class to FSS");
3911 		}
3912 	}
3913 
3914 	/*
3915 	 * The next few blocks of code attempt to set up temporary pools as
3916 	 * well as persistent pools.  In all cases we call the functions
3917 	 * unconditionally.  Within each funtion the code will check if the
3918 	 * zone is actually configured for a temporary pool or persistent pool
3919 	 * and just return if there is nothing to do.
3920 	 *
3921 	 * If we are rebooting we want to attempt to reuse any temporary pool
3922 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
3923 	 * right thing in all cases (reuse or create) based on the current
3924 	 * zonecfg.
3925 	 */
3926 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
3927 	    sizeof (pool_err))) != Z_OK) {
3928 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
3929 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
3930 			    "cannot be instantiated", zonecfg_strerror(res),
3931 			    pool_err);
3932 		else
3933 			zerror(zlogp, B_FALSE, "could not bind zone to "
3934 			    "temporary pool: %s", zonecfg_strerror(res));
3935 		zonecfg_fini_handle(handle);
3936 		return (Z_POOL_BIND);
3937 	}
3938 
3939 	/*
3940 	 * Check if we need to warn about poold not being enabled.
3941 	 */
3942 	if (zonecfg_warn_poold(handle)) {
3943 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
3944 		    "been specified\nbut the dynamic pool service is not "
3945 		    "enabled.\nThe system will not dynamically adjust the\n"
3946 		    "processor allocation within the specified range\n"
3947 		    "until svc:/system/pools/dynamic is enabled.\n"
3948 		    "See poold(1M).");
3949 	}
3950 
3951 	/* The following is a warning, not an error. */
3952 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
3953 	    sizeof (pool_err))) != Z_OK) {
3954 		if (res == Z_POOL_BIND)
3955 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
3956 			    "pool '%s'; using default pool.", pool_err);
3957 		else if (res == Z_POOL)
3958 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
3959 			    zonecfg_strerror(res), pool_err);
3960 		else
3961 			zerror(zlogp, B_FALSE, "WARNING: %s",
3962 			    zonecfg_strerror(res));
3963 	}
3964 
3965 	zonecfg_fini_handle(handle);
3966 	return (Z_OK);
3967 }
3968 
3969 zoneid_t
3970 vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
3971 {
3972 	zoneid_t rval = -1;
3973 	priv_set_t *privs;
3974 	char rootpath[MAXPATHLEN];
3975 	char *rctlbuf = NULL;
3976 	size_t rctlbufsz = 0;
3977 	char *zfsbuf = NULL;
3978 	size_t zfsbufsz = 0;
3979 	zoneid_t zoneid = -1;
3980 	int xerr;
3981 	char *kzone;
3982 	FILE *fp = NULL;
3983 	tsol_zcent_t *zcent = NULL;
3984 	int match = 0;
3985 	int doi = 0;
3986 	int flags;
3987 	zone_iptype_t iptype;
3988 
3989 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
3990 		zerror(zlogp, B_TRUE, "unable to determine zone root");
3991 		return (-1);
3992 	}
3993 	if (zonecfg_in_alt_root())
3994 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
3995 
3996 	if (get_iptype(zlogp, &iptype) < 0) {
3997 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
3998 		return (-1);
3999 	}
4000 	switch (iptype) {
4001 	case ZS_SHARED:
4002 		flags = 0;
4003 		break;
4004 	case ZS_EXCLUSIVE:
4005 		flags = ZCF_NET_EXCL;
4006 		break;
4007 	}
4008 
4009 	if ((privs = priv_allocset()) == NULL) {
4010 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
4011 		return (-1);
4012 	}
4013 	priv_emptyset(privs);
4014 	if (get_privset(zlogp, privs, mount_cmd) != 0)
4015 		goto error;
4016 
4017 	if (mount_cmd == Z_MNT_BOOT &&
4018 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
4019 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
4020 		goto error;
4021 	}
4022 
4023 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4024 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4025 		goto error;
4026 	}
4027 
4028 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
4029 		zcent = get_zone_label(zlogp, privs);
4030 		if (zcent != NULL) {
4031 			match = zcent->zc_match;
4032 			doi = zcent->zc_doi;
4033 			*zlabel = zcent->zc_label;
4034 		} else {
4035 			goto error;
4036 		}
4037 	}
4038 
4039 	kzone = zone_name;
4040 
4041 	/*
4042 	 * We must do this scan twice.  First, we look for zones running on the
4043 	 * main system that are using this root (or any subdirectory of it).
4044 	 * Next, we reduce to the shortest path and search for loopback mounts
4045 	 * that use this same source node (same device and inode).
4046 	 */
4047 	if (duplicate_zone_root(zlogp, rootpath))
4048 		goto error;
4049 	if (duplicate_reachable_path(zlogp, rootpath))
4050 		goto error;
4051 
4052 	if (ALT_MOUNT(mount_cmd)) {
4053 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4054 
4055 		/*
4056 		 * Forge up a special root for this zone.  When a zone is
4057 		 * mounted, we can't let the zone have its own root because the
4058 		 * tools that will be used in this "scratch zone" need access
4059 		 * to both the zone's resources and the running machine's
4060 		 * executables.
4061 		 *
4062 		 * Note that the mkdir here also catches read-only filesystems.
4063 		 */
4064 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4065 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4066 			goto error;
4067 		}
4068 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4069 			goto error;
4070 	}
4071 
4072 	if (zonecfg_in_alt_root()) {
4073 		/*
4074 		 * If we are mounting up a zone in an alternate root partition,
4075 		 * then we have some additional work to do before starting the
4076 		 * zone.  First, resolve the root path down so that we're not
4077 		 * fooled by duplicates.  Then forge up an internal name for
4078 		 * the zone.
4079 		 */
4080 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4081 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4082 			goto error;
4083 		}
4084 		if (zonecfg_lock_scratch(fp) != 0) {
4085 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4086 			goto error;
4087 		}
4088 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4089 		    NULL, 0) == 0) {
4090 			zerror(zlogp, B_FALSE, "scratch zone already running");
4091 			goto error;
4092 		}
4093 		/* This is the preferred name */
4094 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4095 		    zone_name);
4096 		srandom(getpid());
4097 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4098 		    0) == 0) {
4099 			/* This is just an arbitrary name; note "." usage */
4100 			(void) snprintf(kernzone, sizeof (kernzone),
4101 			    "SUNWlu.%08lX%08lX", random(), random());
4102 		}
4103 		kzone = kernzone;
4104 	}
4105 
4106 	xerr = 0;
4107 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4108 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4109 	    flags)) == -1) {
4110 		if (xerr == ZE_AREMOUNTS) {
4111 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
4112 				zerror(zlogp, B_FALSE,
4113 				    "An unknown file-system is mounted on "
4114 				    "a subdirectory of %s", rootpath);
4115 			} else {
4116 
4117 				zerror(zlogp, B_FALSE,
4118 				    "These file-systems are mounted on "
4119 				    "subdirectories of %s:", rootpath);
4120 				(void) zonecfg_find_mounts(rootpath,
4121 				    prtmount, zlogp);
4122 			}
4123 		} else if (xerr == ZE_CHROOTED) {
4124 			zerror(zlogp, B_FALSE, "%s: "
4125 			    "cannot create a zone from a chrooted "
4126 			    "environment", "zone_create");
4127 		} else if (xerr == ZE_LABELINUSE) {
4128 			char zonename[ZONENAME_MAX];
4129 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
4130 			    zonename, ZONENAME_MAX);
4131 			zerror(zlogp, B_FALSE, "The zone label is already "
4132 			    "used by the zone '%s'.", zonename);
4133 		} else {
4134 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
4135 		}
4136 		goto error;
4137 	}
4138 
4139 	if (zonecfg_in_alt_root() &&
4140 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4141 	    zonecfg_get_root()) == -1) {
4142 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4143 		goto error;
4144 	}
4145 
4146 	/*
4147 	 * The following actions are not performed when merely mounting a zone
4148 	 * for administrative use.
4149 	 */
4150 	if (mount_cmd == Z_MNT_BOOT) {
4151 		brand_handle_t bh;
4152 		struct brand_attr attr;
4153 		char modname[MAXPATHLEN];
4154 
4155 		if ((zone_get_brand(zone_name, attr.ba_brandname,
4156 		    MAXNAMELEN) != Z_OK) ||
4157 		    (bh = brand_open(attr.ba_brandname)) == NULL) {
4158 			zerror(zlogp, B_FALSE,
4159 			    "unable to determine brand name");
4160 			return (-1);
4161 		}
4162 
4163 		/*
4164 		 * If this brand requires any kernel support, now is the time to
4165 		 * get it loaded and initialized.
4166 		 */
4167 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4168 			brand_close(bh);
4169 			zerror(zlogp, B_FALSE,
4170 			    "unable to determine brand kernel module");
4171 			return (-1);
4172 		}
4173 		brand_close(bh);
4174 
4175 		if (strlen(modname) > 0) {
4176 			(void) strlcpy(attr.ba_modname, modname, MAXPATHLEN);
4177 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
4178 			    sizeof (attr) != 0)) {
4179 				zerror(zlogp, B_TRUE,
4180 				    "could not set zone brand attribute.");
4181 				goto error;
4182 			}
4183 		}
4184 
4185 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) {
4186 			(void) zone_shutdown(zoneid);
4187 			goto error;
4188 		}
4189 
4190 		set_mlps(zlogp, zoneid, zcent);
4191 	}
4192 
4193 	rval = zoneid;
4194 	zoneid = -1;
4195 
4196 error:
4197 	if (zoneid != -1)
4198 		(void) zone_destroy(zoneid);
4199 	if (rctlbuf != NULL)
4200 		free(rctlbuf);
4201 	priv_freeset(privs);
4202 	if (fp != NULL)
4203 		zonecfg_close_scratch(fp);
4204 	lofs_discard_mnttab();
4205 	if (zcent != NULL)
4206 		tsol_freezcent(zcent);
4207 	return (rval);
4208 }
4209 
4210 /*
4211  * Enter the zone and write a /etc/zones/index file there.  This allows
4212  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4213  * details from inside the zone.
4214  */
4215 static void
4216 write_index_file(zoneid_t zoneid)
4217 {
4218 	FILE *zef;
4219 	FILE *zet;
4220 	struct zoneent *zep;
4221 	pid_t child;
4222 	int tmpl_fd;
4223 	ctid_t ct;
4224 	int fd;
4225 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4226 
4227 	/* Locate the zone entry in the global zone's index file */
4228 	if ((zef = setzoneent()) == NULL)
4229 		return;
4230 	while ((zep = getzoneent_private(zef)) != NULL) {
4231 		if (strcmp(zep->zone_name, zone_name) == 0)
4232 			break;
4233 		free(zep);
4234 	}
4235 	endzoneent(zef);
4236 	if (zep == NULL)
4237 		return;
4238 
4239 	if ((tmpl_fd = init_template()) == -1) {
4240 		free(zep);
4241 		return;
4242 	}
4243 
4244 	if ((child = fork()) == -1) {
4245 		(void) ct_tmpl_clear(tmpl_fd);
4246 		(void) close(tmpl_fd);
4247 		free(zep);
4248 		return;
4249 	}
4250 
4251 	/* parent waits for child to finish */
4252 	if (child != 0) {
4253 		free(zep);
4254 		if (contract_latest(&ct) == -1)
4255 			ct = -1;
4256 		(void) ct_tmpl_clear(tmpl_fd);
4257 		(void) close(tmpl_fd);
4258 		(void) waitpid(child, NULL, 0);
4259 		(void) contract_abandon_id(ct);
4260 		return;
4261 	}
4262 
4263 	/* child enters zone and sets up index file */
4264 	(void) ct_tmpl_clear(tmpl_fd);
4265 	if (zone_enter(zoneid) != -1) {
4266 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4267 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4268 		    ZONE_CONFIG_GID);
4269 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4270 		    ZONE_INDEX_MODE);
4271 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4272 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4273 			if (uuid_is_null(zep->zone_uuid))
4274 				uuidstr[0] = '\0';
4275 			else
4276 				uuid_unparse(zep->zone_uuid, uuidstr);
4277 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4278 			    zone_state_str(zep->zone_state),
4279 			    uuidstr);
4280 			(void) fclose(zet);
4281 		}
4282 	}
4283 	_exit(0);
4284 }
4285 
4286 int
4287 vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
4288 {
4289 	char zonepath[MAXPATHLEN];
4290 
4291 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4292 		lofs_discard_mnttab();
4293 		return (-1);
4294 	}
4295 
4296 	/*
4297 	 * Before we try to mount filesystems we need to create the
4298 	 * attribute backing store for /dev
4299 	 */
4300 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
4301 		lofs_discard_mnttab();
4302 		return (-1);
4303 	}
4304 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
4305 
4306 	/* Make /dev directory owned by root, grouped sys */
4307 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
4308 	    0, 3) != 0) {
4309 		lofs_discard_mnttab();
4310 		return (-1);
4311 	}
4312 
4313 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4314 		lofs_discard_mnttab();
4315 		return (-1);
4316 	}
4317 
4318 	if (mount_cmd == Z_MNT_BOOT) {
4319 		zone_iptype_t iptype;
4320 
4321 		if (get_iptype(zlogp, &iptype) < 0) {
4322 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4323 			lofs_discard_mnttab();
4324 			return (-1);
4325 		}
4326 
4327 		switch (iptype) {
4328 		case ZS_SHARED:
4329 			/* Always do this to make lo0 get configured */
4330 			if (configure_shared_network_interfaces(zlogp) != 0) {
4331 				lofs_discard_mnttab();
4332 				return (-1);
4333 			}
4334 			break;
4335 		case ZS_EXCLUSIVE:
4336 			if (configure_exclusive_network_interfaces(zlogp) !=
4337 			    0) {
4338 				lofs_discard_mnttab();
4339 				return (-1);
4340 			}
4341 			break;
4342 		}
4343 	}
4344 
4345 	write_index_file(zoneid);
4346 
4347 	lofs_discard_mnttab();
4348 	return (0);
4349 }
4350 
4351 static int
4352 lu_root_teardown(zlog_t *zlogp)
4353 {
4354 	char zroot[MAXPATHLEN];
4355 
4356 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4357 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4358 		return (-1);
4359 	}
4360 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4361 
4362 	/*
4363 	 * At this point, the processes are gone, the filesystems (save the
4364 	 * root) are unmounted, and the zone is on death row.  But there may
4365 	 * still be creds floating about in the system that reference the
4366 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4367 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4368 	 * (How I wish this were not true, and umount2 just did the right
4369 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4370 	 */
4371 	if (umount2(zroot, MS_FORCE) != 0) {
4372 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4373 			goto unmounted;
4374 		if (errno == EBUSY) {
4375 			int tries = 10;
4376 
4377 			while (--tries >= 0) {
4378 				(void) sleep(1);
4379 				if (umount2(zroot, 0) == 0)
4380 					goto unmounted;
4381 				if (errno != EBUSY)
4382 					break;
4383 			}
4384 		}
4385 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4386 		return (-1);
4387 	}
4388 unmounted:
4389 
4390 	/*
4391 	 * Only zones in an alternate root environment have scratch zone
4392 	 * entries.
4393 	 */
4394 	if (zonecfg_in_alt_root()) {
4395 		FILE *fp;
4396 		int retv;
4397 
4398 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4399 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4400 			return (-1);
4401 		}
4402 		retv = -1;
4403 		if (zonecfg_lock_scratch(fp) != 0)
4404 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4405 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4406 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4407 		else
4408 			retv = 0;
4409 		zonecfg_close_scratch(fp);
4410 		return (retv);
4411 	} else {
4412 		return (0);
4413 	}
4414 }
4415 
4416 int
4417 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4418 {
4419 	char *kzone;
4420 	zoneid_t zoneid;
4421 	int res;
4422 	char pool_err[128];
4423 	char zpath[MAXPATHLEN];
4424 	char cmdbuf[MAXPATHLEN];
4425 	char brand[MAXNAMELEN];
4426 	brand_handle_t bh = NULL;
4427 	ushort_t flags;
4428 
4429 	kzone = zone_name;
4430 	if (zonecfg_in_alt_root()) {
4431 		FILE *fp;
4432 
4433 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4434 			zerror(zlogp, B_TRUE, "unable to open map file");
4435 			goto error;
4436 		}
4437 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4438 		    kernzone, sizeof (kernzone)) != 0) {
4439 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4440 			zonecfg_close_scratch(fp);
4441 			goto error;
4442 		}
4443 		zonecfg_close_scratch(fp);
4444 		kzone = kernzone;
4445 	}
4446 
4447 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
4448 		if (!bringup_failure_recovery)
4449 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4450 		if (unmount_cmd)
4451 			(void) lu_root_teardown(zlogp);
4452 		goto error;
4453 	}
4454 
4455 	if (zone_shutdown(zoneid) != 0) {
4456 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
4457 		goto error;
4458 	}
4459 
4460 	/* Get the zonepath of this zone */
4461 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
4462 		zerror(zlogp, B_FALSE, "unable to determine zone path");
4463 		goto error;
4464 	}
4465 
4466 	/* Get a handle to the brand info for this zone */
4467 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
4468 	    (bh = brand_open(brand)) == NULL) {
4469 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
4470 		return (-1);
4471 	}
4472 	/*
4473 	 * If there is a brand 'halt' callback, execute it now to give the
4474 	 * brand a chance to cleanup any custom configuration.
4475 	 */
4476 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4477 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
4478 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
4479 		brand_close(bh);
4480 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
4481 		    "halt callback.");
4482 		goto error;
4483 	}
4484 	brand_close(bh);
4485 
4486 	if ((strlen(cmdbuf) > EXEC_LEN) &&
4487 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
4488 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
4489 		goto error;
4490 	}
4491 
4492 	if (!unmount_cmd) {
4493 		zone_iptype_t iptype;
4494 
4495 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4496 		    sizeof (flags)) < 0) {
4497 			if (get_iptype(zlogp, &iptype) < 0) {
4498 				zerror(zlogp, B_TRUE, "unable to determine "
4499 				    "ip-type");
4500 				goto error;
4501 			}
4502 		} else {
4503 			if (flags & ZF_NET_EXCL)
4504 				iptype = ZS_EXCLUSIVE;
4505 			else
4506 				iptype = ZS_SHARED;
4507 		}
4508 
4509 		switch (iptype) {
4510 		case ZS_SHARED:
4511 			if (unconfigure_shared_network_interfaces(zlogp,
4512 			    zoneid) != 0) {
4513 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4514 				    "network interfaces in zone");
4515 				goto error;
4516 			}
4517 			break;
4518 		case ZS_EXCLUSIVE:
4519 			if (unconfigure_exclusive_network_interfaces(zlogp,
4520 			    zoneid) != 0) {
4521 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4522 				    "network interfaces in zone");
4523 				goto error;
4524 			}
4525 			break;
4526 		}
4527 	}
4528 
4529 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
4530 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
4531 		goto error;
4532 	}
4533 
4534 	/* destroy zconsole before umount /dev */
4535 	if (!unmount_cmd)
4536 		destroy_console_slave();
4537 
4538 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
4539 		zerror(zlogp, B_FALSE,
4540 		    "unable to unmount file systems in zone");
4541 		goto error;
4542 	}
4543 
4544 	/*
4545 	 * If we are rebooting then we normally don't want to destroy an
4546 	 * existing temporary pool at this point so that we can just reuse it
4547 	 * when the zone boots back up.  However, it is also possible we were
4548 	 * running with a temporary pool and the zone configuration has been
4549 	 * modified to no longer use a temporary pool.  In that case we need
4550 	 * to destroy the temporary pool now.  This case looks like the case
4551 	 * where we never had a temporary pool configured but
4552 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
4553 	 */
4554 	if (!unmount_cmd) {
4555 		boolean_t destroy_tmp_pool = B_TRUE;
4556 
4557 		if (rebooting) {
4558 			struct zone_psettab pset_tab;
4559 			zone_dochandle_t handle;
4560 
4561 			if ((handle = zonecfg_init_handle()) != NULL &&
4562 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4563 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4564 				destroy_tmp_pool = B_FALSE;
4565 
4566 			zonecfg_fini_handle(handle);
4567 		}
4568 
4569 		if (destroy_tmp_pool) {
4570 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
4571 			    sizeof (pool_err))) != Z_OK) {
4572 				if (res == Z_POOL)
4573 					zerror(zlogp, B_FALSE, pool_err);
4574 			}
4575 		}
4576 	}
4577 
4578 	remove_mlps(zlogp, zoneid);
4579 
4580 	if (zone_destroy(zoneid) != 0) {
4581 		zerror(zlogp, B_TRUE, "unable to destroy zone");
4582 		goto error;
4583 	}
4584 
4585 	/*
4586 	 * Special teardown for alternate boot environments: remove the tmpfs
4587 	 * root for the zone and then remove it from the map file.
4588 	 */
4589 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4590 		goto error;
4591 
4592 	lofs_discard_mnttab();
4593 	return (0);
4594 
4595 error:
4596 	lofs_discard_mnttab();
4597 	return (-1);
4598 }
4599