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