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