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