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