xref: /titanic_50/usr/src/cmd/zoneadmd/vplat.c (revision 530f2c280d739b194cfbb75f25352b75bb99b4b2)
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 native
1089 	 * brand device mounts.
1090 	 */
1091 	if (ALT_MOUNT(mount_cmd)) {
1092 		(void) strlcpy(brand, NATIVE_BRAND_NAME, 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 native
1763 	 * brand global mounts.
1764 	 */
1765 	if (ALT_MOUNT(mount_cmd)) {
1766 		(void) strlcpy(brand, NATIVE_BRAND_NAME, 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  * Mount lower level home directories into/from current zone
3145  * Share exported directories specified in dfstab for zone
3146  */
3147 static int
3148 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
3149 {
3150 	zoneid_t *zids = NULL;
3151 	priv_set_t *zid_privs;
3152 	const priv_impl_info_t *ip = NULL;
3153 	uint_t nzents_saved;
3154 	uint_t nzents;
3155 	int i;
3156 	char readonly[] = "ro";
3157 	struct zone_fstab lower_fstab;
3158 	char *argv[4];
3159 
3160 	if (!is_system_labeled())
3161 		return (0);
3162 
3163 	if (zid_label == NULL) {
3164 		zid_label = m_label_alloc(MAC_LABEL);
3165 		if (zid_label == NULL)
3166 			return (-1);
3167 	}
3168 
3169 	/* Make sure our zone has an /export/home dir */
3170 	(void) make_one_dir(zlogp, rootpath, "/export/home",
3171 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3172 
3173 	lower_fstab.zone_fs_raw[0] = '\0';
3174 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
3175 	    sizeof (lower_fstab.zone_fs_type));
3176 	lower_fstab.zone_fs_options = NULL;
3177 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
3178 
3179 	/*
3180 	 * Get the list of zones from the kernel
3181 	 */
3182 	if (zone_list(NULL, &nzents) != 0) {
3183 		zerror(zlogp, B_TRUE, "unable to list zones");
3184 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3185 		return (-1);
3186 	}
3187 again:
3188 	if (nzents == 0) {
3189 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3190 		return (-1);
3191 	}
3192 
3193 	zids = malloc(nzents * sizeof (zoneid_t));
3194 	if (zids == NULL) {
3195 		zerror(zlogp, B_TRUE, "memory allocation failed");
3196 		return (-1);
3197 	}
3198 	nzents_saved = nzents;
3199 
3200 	if (zone_list(zids, &nzents) != 0) {
3201 		zerror(zlogp, B_TRUE, "unable to list zones");
3202 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3203 		free(zids);
3204 		return (-1);
3205 	}
3206 	if (nzents != nzents_saved) {
3207 		/* list changed, try again */
3208 		free(zids);
3209 		goto again;
3210 	}
3211 
3212 	ip = getprivimplinfo();
3213 	if ((zid_privs = priv_allocset()) == NULL) {
3214 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3215 		zonecfg_free_fs_option_list(
3216 		    lower_fstab.zone_fs_options);
3217 		free(zids);
3218 		return (-1);
3219 	}
3220 
3221 	for (i = 0; i < nzents; i++) {
3222 		char zid_name[ZONENAME_MAX];
3223 		zone_state_t zid_state;
3224 		char zid_rpath[MAXPATHLEN];
3225 		struct stat stat_buf;
3226 
3227 		if (zids[i] == GLOBAL_ZONEID)
3228 			continue;
3229 
3230 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3231 			continue;
3232 
3233 		/*
3234 		 * Do special setup for the zone we are booting
3235 		 */
3236 		if (strcmp(zid_name, zone_name) == 0) {
3237 			struct zone_fstab autofs_fstab;
3238 			char map_path[MAXPATHLEN];
3239 			int fd;
3240 
3241 			/*
3242 			 * Create auto_home_<zone> map for this zone
3243 			 * in the global zone. The non-global zone entry
3244 			 * will be created by automount when the zone
3245 			 * is booted.
3246 			 */
3247 
3248 			(void) snprintf(autofs_fstab.zone_fs_special,
3249 			    MAXPATHLEN, "auto_home_%s", zid_name);
3250 
3251 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
3252 			    "/zone/%s/home", zid_name);
3253 
3254 			(void) snprintf(map_path, sizeof (map_path),
3255 			    "/etc/%s", autofs_fstab.zone_fs_special);
3256 			/*
3257 			 * If the map file doesn't exist create a template
3258 			 */
3259 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
3260 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
3261 				int len;
3262 				char map_rec[MAXPATHLEN];
3263 
3264 				len = snprintf(map_rec, sizeof (map_rec),
3265 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
3266 				    autofs_fstab.zone_fs_special, rootpath);
3267 				(void) write(fd, map_rec, len);
3268 				(void) close(fd);
3269 			}
3270 
3271 			/*
3272 			 * Mount auto_home_<zone> in the global zone if absent.
3273 			 * If it's already of type autofs, then
3274 			 * don't mount it again.
3275 			 */
3276 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
3277 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
3278 				char optstr[] = "indirect,ignore,nobrowse";
3279 
3280 				(void) make_one_dir(zlogp, "",
3281 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
3282 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3283 
3284 				/*
3285 				 * Mount will fail if automounter has already
3286 				 * processed the auto_home_<zonename> map
3287 				 */
3288 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
3289 				    autofs_fstab.zone_fs_special,
3290 				    autofs_fstab.zone_fs_dir);
3291 			}
3292 			continue;
3293 		}
3294 
3295 
3296 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
3297 		    (zid_state != ZONE_STATE_READY &&
3298 		    zid_state != ZONE_STATE_RUNNING))
3299 			/* Skip over zones without mounted filesystems */
3300 			continue;
3301 
3302 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3303 		    sizeof (m_label_t)) < 0)
3304 			/* Skip over zones with unspecified label */
3305 			continue;
3306 
3307 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3308 		    sizeof (zid_rpath)) == -1)
3309 			/* Skip over zones with bad path */
3310 			continue;
3311 
3312 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
3313 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
3314 			/* Skip over zones with bad privs */
3315 			continue;
3316 
3317 		/*
3318 		 * Reading down is valid according to our label model
3319 		 * but some customers want to disable it because it
3320 		 * allows execute down and other possible attacks.
3321 		 * Therefore, we restrict this feature to zones that
3322 		 * have the NET_MAC_AWARE privilege which is required
3323 		 * for NFS read-down semantics.
3324 		 */
3325 		if ((bldominates(zlabel, zid_label)) &&
3326 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
3327 			/*
3328 			 * Our zone dominates this one.
3329 			 * Create a lofs mount from lower zone's /export/home
3330 			 */
3331 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3332 			    "%s/zone/%s/export/home", rootpath, zid_name);
3333 
3334 			/*
3335 			 * If the target is already an LOFS mount
3336 			 * then don't do it again.
3337 			 */
3338 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3339 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3340 
3341 				if (snprintf(lower_fstab.zone_fs_special,
3342 				    MAXPATHLEN, "%s/export",
3343 				    zid_rpath) > MAXPATHLEN)
3344 					continue;
3345 
3346 				/*
3347 				 * Make sure the lower-level home exists
3348 				 */
3349 				if (make_one_dir(zlogp,
3350 				    lower_fstab.zone_fs_special, "/home",
3351 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
3352 				    DEFAULT_DIR_GROUP) != 0)
3353 					continue;
3354 
3355 				(void) strlcat(lower_fstab.zone_fs_special,
3356 				    "/home", MAXPATHLEN);
3357 
3358 				/*
3359 				 * Mount can fail because the lower-level
3360 				 * zone may have already done a mount up.
3361 				 */
3362 				(void) mount_one(zlogp, &lower_fstab, "",
3363 				    Z_MNT_BOOT);
3364 			}
3365 		} else if ((bldominates(zid_label, zlabel)) &&
3366 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
3367 			/*
3368 			 * This zone dominates our zone.
3369 			 * Create a lofs mount from our zone's /export/home
3370 			 */
3371 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3372 			    "%s/zone/%s/export/home", zid_rpath,
3373 			    zone_name) > MAXPATHLEN)
3374 				continue;
3375 
3376 			/*
3377 			 * If the target is already an LOFS mount
3378 			 * then don't do it again.
3379 			 */
3380 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3381 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3382 
3383 				(void) snprintf(lower_fstab.zone_fs_special,
3384 				    MAXPATHLEN, "%s/export/home", rootpath);
3385 
3386 				/*
3387 				 * Mount can fail because the higher-level
3388 				 * zone may have already done a mount down.
3389 				 */
3390 				(void) mount_one(zlogp, &lower_fstab, "",
3391 				    Z_MNT_BOOT);
3392 			}
3393 		}
3394 	}
3395 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3396 	priv_freeset(zid_privs);
3397 	free(zids);
3398 
3399 	/*
3400 	 * Now share any exported directories from this zone.
3401 	 * Each zone can have its own dfstab.
3402 	 */
3403 
3404 	argv[0] = "zoneshare";
3405 	argv[1] = "-z";
3406 	argv[2] = zone_name;
3407 	argv[3] = NULL;
3408 
3409 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
3410 	/* Don't check for errors since they don't affect the zone */
3411 
3412 	return (0);
3413 }
3414 
3415 /*
3416  * Unmount lofs mounts from higher level zones
3417  * Unshare nfs exported directories
3418  */
3419 static void
3420 tsol_unmounts(zlog_t *zlogp, char *zone_name)
3421 {
3422 	zoneid_t *zids = NULL;
3423 	uint_t nzents_saved;
3424 	uint_t nzents;
3425 	int i;
3426 	char *argv[4];
3427 	char path[MAXPATHLEN];
3428 
3429 	if (!is_system_labeled())
3430 		return;
3431 
3432 	/*
3433 	 * Get the list of zones from the kernel
3434 	 */
3435 	if (zone_list(NULL, &nzents) != 0) {
3436 		return;
3437 	}
3438 
3439 	if (zid_label == NULL) {
3440 		zid_label = m_label_alloc(MAC_LABEL);
3441 		if (zid_label == NULL)
3442 			return;
3443 	}
3444 
3445 again:
3446 	if (nzents == 0)
3447 		return;
3448 
3449 	zids = malloc(nzents * sizeof (zoneid_t));
3450 	if (zids == NULL) {
3451 		zerror(zlogp, B_TRUE, "memory allocation failed");
3452 		return;
3453 	}
3454 	nzents_saved = nzents;
3455 
3456 	if (zone_list(zids, &nzents) != 0) {
3457 		free(zids);
3458 		return;
3459 	}
3460 	if (nzents != nzents_saved) {
3461 		/* list changed, try again */
3462 		free(zids);
3463 		goto again;
3464 	}
3465 
3466 	for (i = 0; i < nzents; i++) {
3467 		char zid_name[ZONENAME_MAX];
3468 		zone_state_t zid_state;
3469 		char zid_rpath[MAXPATHLEN];
3470 
3471 		if (zids[i] == GLOBAL_ZONEID)
3472 			continue;
3473 
3474 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3475 			continue;
3476 
3477 		/*
3478 		 * Skip the zone we are halting
3479 		 */
3480 		if (strcmp(zid_name, zone_name) == 0)
3481 			continue;
3482 
3483 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
3484 		    sizeof (zid_state)) < 0) ||
3485 		    (zid_state < ZONE_IS_READY))
3486 			/* Skip over zones without mounted filesystems */
3487 			continue;
3488 
3489 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3490 		    sizeof (m_label_t)) < 0)
3491 			/* Skip over zones with unspecified label */
3492 			continue;
3493 
3494 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3495 		    sizeof (zid_rpath)) == -1)
3496 			/* Skip over zones with bad path */
3497 			continue;
3498 
3499 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
3500 			/*
3501 			 * This zone dominates our zone.
3502 			 * Unmount the lofs mount of our zone's /export/home
3503 			 */
3504 
3505 			if (snprintf(path, MAXPATHLEN,
3506 			    "%s/zone/%s/export/home", zid_rpath,
3507 			    zone_name) > MAXPATHLEN)
3508 				continue;
3509 
3510 			/* Skip over mount failures */
3511 			(void) umount(path);
3512 		}
3513 	}
3514 	free(zids);
3515 
3516 	/*
3517 	 * Unmount global zone autofs trigger for this zone
3518 	 */
3519 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
3520 	/* Skip over mount failures */
3521 	(void) umount(path);
3522 
3523 	/*
3524 	 * Next unshare any exported directories from this zone.
3525 	 */
3526 
3527 	argv[0] = "zoneunshare";
3528 	argv[1] = "-z";
3529 	argv[2] = zone_name;
3530 	argv[3] = NULL;
3531 
3532 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
3533 	/* Don't check for errors since they don't affect the zone */
3534 
3535 	/*
3536 	 * Finally, deallocate any devices in the zone.
3537 	 */
3538 
3539 	argv[0] = "deallocate";
3540 	argv[1] = "-Isz";
3541 	argv[2] = zone_name;
3542 	argv[3] = NULL;
3543 
3544 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
3545 	/* Don't check for errors since they don't affect the zone */
3546 }
3547 
3548 /*
3549  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
3550  * this zone.
3551  */
3552 static tsol_zcent_t *
3553 get_zone_label(zlog_t *zlogp, priv_set_t *privs)
3554 {
3555 	FILE *fp;
3556 	tsol_zcent_t *zcent = NULL;
3557 	char line[MAXTNZLEN];
3558 
3559 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
3560 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
3561 		return (NULL);
3562 	}
3563 
3564 	while (fgets(line, sizeof (line), fp) != NULL) {
3565 		/*
3566 		 * Check for malformed database
3567 		 */
3568 		if (strlen(line) == MAXTNZLEN - 1)
3569 			break;
3570 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
3571 			continue;
3572 		if (strcmp(zcent->zc_name, zone_name) == 0)
3573 			break;
3574 		tsol_freezcent(zcent);
3575 		zcent = NULL;
3576 	}
3577 	(void) fclose(fp);
3578 
3579 	if (zcent == NULL) {
3580 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
3581 		    "See tnzonecfg(4)");
3582 	} else {
3583 		if (zlabel == NULL)
3584 			zlabel = m_label_alloc(MAC_LABEL);
3585 		/*
3586 		 * Save this zone's privileges for later read-down processing
3587 		 */
3588 		if ((zprivs = priv_allocset()) == NULL) {
3589 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3590 			return (NULL);
3591 		} else {
3592 			priv_copyset(privs, zprivs);
3593 		}
3594 	}
3595 	return (zcent);
3596 }
3597 
3598 /*
3599  * Add the Trusted Extensions multi-level ports for this zone.
3600  */
3601 static void
3602 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
3603 {
3604 	tsol_mlp_t *mlp;
3605 	tsol_mlpent_t tsme;
3606 
3607 	if (!is_system_labeled())
3608 		return;
3609 
3610 	tsme.tsme_zoneid = zoneid;
3611 	tsme.tsme_flags = 0;
3612 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
3613 		tsme.tsme_mlp = *mlp;
3614 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
3615 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
3616 			    "on %d-%d/%d", mlp->mlp_port,
3617 			    mlp->mlp_port_upper, mlp->mlp_ipp);
3618 		}
3619 	}
3620 
3621 	tsme.tsme_flags = TSOL_MEF_SHARED;
3622 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
3623 		tsme.tsme_mlp = *mlp;
3624 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
3625 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
3626 			    "on %d-%d/%d", mlp->mlp_port,
3627 			    mlp->mlp_port_upper, mlp->mlp_ipp);
3628 		}
3629 	}
3630 }
3631 
3632 static void
3633 remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
3634 {
3635 	tsol_mlpent_t tsme;
3636 
3637 	if (!is_system_labeled())
3638 		return;
3639 
3640 	(void) memset(&tsme, 0, sizeof (tsme));
3641 	tsme.tsme_zoneid = zoneid;
3642 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
3643 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
3644 }
3645 
3646 int
3647 prtmount(const char *fs, void *x) {
3648 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
3649 	return (0);
3650 }
3651 
3652 /*
3653  * Look for zones running on the main system that are using this root (or any
3654  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3655  * is found or if we can't tell.
3656  */
3657 static boolean_t
3658 duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
3659 {
3660 	zoneid_t *zids = NULL;
3661 	uint_t nzids = 0;
3662 	boolean_t retv;
3663 	int rlen, zlen;
3664 	char zroot[MAXPATHLEN];
3665 	char zonename[ZONENAME_MAX];
3666 
3667 	for (;;) {
3668 		nzids += 10;
3669 		zids = malloc(nzids * sizeof (*zids));
3670 		if (zids == NULL) {
3671 			zerror(zlogp, B_TRUE, "memory allocation failed");
3672 			return (B_TRUE);
3673 		}
3674 		if (zone_list(zids, &nzids) == 0)
3675 			break;
3676 		free(zids);
3677 	}
3678 	retv = B_FALSE;
3679 	rlen = strlen(rootpath);
3680 	while (nzids > 0) {
3681 		/*
3682 		 * Ignore errors; they just mean that the zone has disappeared
3683 		 * while we were busy.
3684 		 */
3685 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3686 		    sizeof (zroot)) == -1)
3687 			continue;
3688 		zlen = strlen(zroot);
3689 		if (zlen > rlen)
3690 			zlen = rlen;
3691 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3692 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3693 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3694 			if (getzonenamebyid(zids[nzids], zonename,
3695 			    sizeof (zonename)) == -1)
3696 				(void) snprintf(zonename, sizeof (zonename),
3697 				    "id %d", (int)zids[nzids]);
3698 			zerror(zlogp, B_FALSE,
3699 			    "zone root %s already in use by zone %s",
3700 			    rootpath, zonename);
3701 			retv = B_TRUE;
3702 			break;
3703 		}
3704 	}
3705 	free(zids);
3706 	return (retv);
3707 }
3708 
3709 /*
3710  * Search for loopback mounts that use this same source node (same device and
3711  * inode).  Return B_TRUE if there is one or if we can't tell.
3712  */
3713 static boolean_t
3714 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3715 {
3716 	struct stat64 rst, zst;
3717 	struct mnttab *mnp;
3718 
3719 	if (stat64(rootpath, &rst) == -1) {
3720 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3721 		return (B_TRUE);
3722 	}
3723 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3724 		return (B_TRUE);
3725 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3726 		if (mnp->mnt_fstype == NULL ||
3727 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3728 			continue;
3729 		/* We're looking at a loopback mount.  Stat it. */
3730 		if (mnp->mnt_special != NULL &&
3731 		    stat64(mnp->mnt_special, &zst) != -1 &&
3732 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3733 			zerror(zlogp, B_FALSE,
3734 			    "zone root %s is reachable through %s",
3735 			    rootpath, mnp->mnt_mountp);
3736 			return (B_TRUE);
3737 		}
3738 	}
3739 	return (B_FALSE);
3740 }
3741 
3742 /*
3743  * Set memory cap and pool info for the zone's resource management
3744  * configuration.
3745  */
3746 static int
3747 setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
3748 {
3749 	int res;
3750 	uint64_t tmp;
3751 	struct zone_mcaptab mcap;
3752 	char sched[MAXNAMELEN];
3753 	zone_dochandle_t handle = NULL;
3754 	char pool_err[128];
3755 
3756 	if ((handle = zonecfg_init_handle()) == NULL) {
3757 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3758 		return (Z_BAD_HANDLE);
3759 	}
3760 
3761 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
3762 		zerror(zlogp, B_FALSE, "invalid configuration");
3763 		zonecfg_fini_handle(handle);
3764 		return (res);
3765 	}
3766 
3767 	/*
3768 	 * If a memory cap is configured, set the cap in the kernel using
3769 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
3770 	 */
3771 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
3772 		uint64_t num;
3773 		char smf_err[128];
3774 
3775 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
3776 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
3777 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
3778 			zonecfg_fini_handle(handle);
3779 			return (Z_INVAL);
3780 		}
3781 
3782 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
3783 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
3784 			    "failed: %s", smf_err);
3785 			zonecfg_fini_handle(handle);
3786 			return (Z_INVAL);
3787 		}
3788 	}
3789 
3790 	/* Get the scheduling class set in the zone configuration. */
3791 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
3792 	    strlen(sched) > 0) {
3793 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
3794 		    strlen(sched)) == -1)
3795 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
3796 			    "default scheduling class");
3797 
3798 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
3799 	    == Z_OK) {
3800 		/*
3801 		 * If the zone has the zone.cpu-shares rctl set then we want to
3802 		 * use the Fair Share Scheduler (FSS) for processes in the
3803 		 * zone.  Check what scheduling class the zone would be running
3804 		 * in by default so we can print a warning and modify the class
3805 		 * if we wouldn't be using FSS.
3806 		 */
3807 		char class_name[PC_CLNMSZ];
3808 
3809 		if (zonecfg_get_dflt_sched_class(handle, class_name,
3810 		    sizeof (class_name)) != Z_OK) {
3811 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
3812 			    "the zone's scheduling class");
3813 
3814 		} else if (strcmp("FSS", class_name) != 0) {
3815 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
3816 			    "rctl is set but\nFSS is not the default "
3817 			    "scheduling class for\nthis zone.  FSS will be "
3818 			    "used for processes\nin the zone but to get the "
3819 			    "full benefit of FSS,\nit should be the default "
3820 			    "scheduling class.\nSee dispadmin(1M) for more "
3821 			    "details.");
3822 
3823 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
3824 			    strlen("FSS")) == -1)
3825 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
3826 				    "zone scheduling class to FSS");
3827 		}
3828 	}
3829 
3830 	/*
3831 	 * The next few blocks of code attempt to set up temporary pools as
3832 	 * well as persistent pools.  In all cases we call the functions
3833 	 * unconditionally.  Within each funtion the code will check if the
3834 	 * zone is actually configured for a temporary pool or persistent pool
3835 	 * and just return if there is nothing to do.
3836 	 *
3837 	 * If we are rebooting we want to attempt to reuse any temporary pool
3838 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
3839 	 * right thing in all cases (reuse or create) based on the current
3840 	 * zonecfg.
3841 	 */
3842 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
3843 	    sizeof (pool_err))) != Z_OK) {
3844 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
3845 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
3846 			    "cannot be instantiated", zonecfg_strerror(res),
3847 			    pool_err);
3848 		else
3849 			zerror(zlogp, B_FALSE, "could not bind zone to "
3850 			    "temporary pool: %s", zonecfg_strerror(res));
3851 		zonecfg_fini_handle(handle);
3852 		return (Z_POOL_BIND);
3853 	}
3854 
3855 	/*
3856 	 * Check if we need to warn about poold not being enabled.
3857 	 */
3858 	if (zonecfg_warn_poold(handle)) {
3859 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
3860 		    "been specified\nbut the dynamic pool service is not "
3861 		    "enabled.\nThe system will not dynamically adjust the\n"
3862 		    "processor allocation within the specified range\n"
3863 		    "until svc:/system/pools/dynamic is enabled.\n"
3864 		    "See poold(1M).");
3865 	}
3866 
3867 	/* The following is a warning, not an error. */
3868 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
3869 	    sizeof (pool_err))) != Z_OK) {
3870 		if (res == Z_POOL_BIND)
3871 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
3872 			    "pool '%s'; using default pool.", pool_err);
3873 		else if (res == Z_POOL)
3874 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
3875 			    zonecfg_strerror(res), pool_err);
3876 		else
3877 			zerror(zlogp, B_FALSE, "WARNING: %s",
3878 			    zonecfg_strerror(res));
3879 	}
3880 
3881 	zonecfg_fini_handle(handle);
3882 	return (Z_OK);
3883 }
3884 
3885 /*
3886  * Sets the hostid of the new zone based on its configured value.  The zone's
3887  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
3888  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
3889  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
3890  * ID of the new zone.
3891  *
3892  * This function returns zero on success and a nonzero error code on failure.
3893  */
3894 static int
3895 setup_zone_hostid(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
3896 {
3897 	int res;
3898 	zone_dochandle_t handle;
3899 	char hostidp[HW_HOSTID_LEN];
3900 	unsigned int hostid;
3901 
3902 	if ((handle = zonecfg_init_handle()) == NULL) {
3903 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3904 		return (Z_BAD_HANDLE);
3905 	}
3906 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
3907 		zerror(zlogp, B_FALSE, "invalid configuration");
3908 		zonecfg_fini_handle(handle);
3909 		return (res);
3910 	}
3911 
3912 	if ((res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp))) ==
3913 	    Z_OK) {
3914 		if (zonecfg_valid_hostid(hostidp) != Z_OK) {
3915 			zerror(zlogp, B_FALSE,
3916 			    "zone hostid is not valid: %s", hostidp);
3917 			zonecfg_fini_handle(handle);
3918 			return (Z_HOSTID_FUBAR);
3919 		}
3920 		hostid = (unsigned int)strtoul(hostidp, NULL, 16);
3921 		if (zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
3922 		    sizeof (hostid)) != 0) {
3923 			zerror(zlogp, B_TRUE,
3924 			    "zone hostid is not valid: %s", hostidp);
3925 			zonecfg_fini_handle(handle);
3926 			return (Z_SYSTEM);
3927 		}
3928 	} else if (res != Z_BAD_PROPERTY) {
3929 		/*
3930 		 * Z_BAD_PROPERTY is an acceptable error value (from
3931 		 * zonecfg_get_hostid()) because it indicates that the zone
3932 		 * doesn't have a hostid.
3933 		 */
3934 		if (res == Z_TOO_BIG)
3935 			zerror(zlogp, B_FALSE, "hostid string in zone "
3936 			    "configuration is too large.");
3937 		else
3938 			zerror(zlogp, B_TRUE, "fetching zone hostid from "
3939 			    "configuration");
3940 		zonecfg_fini_handle(handle);
3941 		return (res);
3942 	}
3943 
3944 	zonecfg_fini_handle(handle);
3945 	return (Z_OK);
3946 }
3947 
3948 zoneid_t
3949 vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
3950 {
3951 	zoneid_t rval = -1;
3952 	priv_set_t *privs;
3953 	char rootpath[MAXPATHLEN];
3954 	char *rctlbuf = NULL;
3955 	size_t rctlbufsz = 0;
3956 	char *zfsbuf = NULL;
3957 	size_t zfsbufsz = 0;
3958 	zoneid_t zoneid = -1;
3959 	int xerr;
3960 	char *kzone;
3961 	FILE *fp = NULL;
3962 	tsol_zcent_t *zcent = NULL;
3963 	int match = 0;
3964 	int doi = 0;
3965 	int flags;
3966 	zone_iptype_t iptype;
3967 
3968 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
3969 		zerror(zlogp, B_TRUE, "unable to determine zone root");
3970 		return (-1);
3971 	}
3972 	if (zonecfg_in_alt_root())
3973 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
3974 
3975 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
3976 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
3977 		return (-1);
3978 	}
3979 	switch (iptype) {
3980 	case ZS_SHARED:
3981 		flags = 0;
3982 		break;
3983 	case ZS_EXCLUSIVE:
3984 		flags = ZCF_NET_EXCL;
3985 		break;
3986 	}
3987 
3988 	if ((privs = priv_allocset()) == NULL) {
3989 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3990 		return (-1);
3991 	}
3992 	priv_emptyset(privs);
3993 	if (get_privset(zlogp, privs, mount_cmd) != 0)
3994 		goto error;
3995 
3996 	if (mount_cmd == Z_MNT_BOOT &&
3997 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
3998 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
3999 		goto error;
4000 	}
4001 
4002 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4003 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4004 		goto error;
4005 	}
4006 
4007 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
4008 		zcent = get_zone_label(zlogp, privs);
4009 		if (zcent != NULL) {
4010 			match = zcent->zc_match;
4011 			doi = zcent->zc_doi;
4012 			*zlabel = zcent->zc_label;
4013 		} else {
4014 			goto error;
4015 		}
4016 	}
4017 
4018 	kzone = zone_name;
4019 
4020 	/*
4021 	 * We must do this scan twice.  First, we look for zones running on the
4022 	 * main system that are using this root (or any subdirectory of it).
4023 	 * Next, we reduce to the shortest path and search for loopback mounts
4024 	 * that use this same source node (same device and inode).
4025 	 */
4026 	if (duplicate_zone_root(zlogp, rootpath))
4027 		goto error;
4028 	if (duplicate_reachable_path(zlogp, rootpath))
4029 		goto error;
4030 
4031 	if (ALT_MOUNT(mount_cmd)) {
4032 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4033 
4034 		/*
4035 		 * Forge up a special root for this zone.  When a zone is
4036 		 * mounted, we can't let the zone have its own root because the
4037 		 * tools that will be used in this "scratch zone" need access
4038 		 * to both the zone's resources and the running machine's
4039 		 * executables.
4040 		 *
4041 		 * Note that the mkdir here also catches read-only filesystems.
4042 		 */
4043 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4044 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4045 			goto error;
4046 		}
4047 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4048 			goto error;
4049 	}
4050 
4051 	if (zonecfg_in_alt_root()) {
4052 		/*
4053 		 * If we are mounting up a zone in an alternate root partition,
4054 		 * then we have some additional work to do before starting the
4055 		 * zone.  First, resolve the root path down so that we're not
4056 		 * fooled by duplicates.  Then forge up an internal name for
4057 		 * the zone.
4058 		 */
4059 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4060 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4061 			goto error;
4062 		}
4063 		if (zonecfg_lock_scratch(fp) != 0) {
4064 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4065 			goto error;
4066 		}
4067 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4068 		    NULL, 0) == 0) {
4069 			zerror(zlogp, B_FALSE, "scratch zone already running");
4070 			goto error;
4071 		}
4072 		/* This is the preferred name */
4073 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4074 		    zone_name);
4075 		srandom(getpid());
4076 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4077 		    0) == 0) {
4078 			/* This is just an arbitrary name; note "." usage */
4079 			(void) snprintf(kernzone, sizeof (kernzone),
4080 			    "SUNWlu.%08lX%08lX", random(), random());
4081 		}
4082 		kzone = kernzone;
4083 	}
4084 
4085 	xerr = 0;
4086 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4087 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4088 	    flags)) == -1) {
4089 		if (xerr == ZE_AREMOUNTS) {
4090 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
4091 				zerror(zlogp, B_FALSE,
4092 				    "An unknown file-system is mounted on "
4093 				    "a subdirectory of %s", rootpath);
4094 			} else {
4095 
4096 				zerror(zlogp, B_FALSE,
4097 				    "These file-systems are mounted on "
4098 				    "subdirectories of %s:", rootpath);
4099 				(void) zonecfg_find_mounts(rootpath,
4100 				    prtmount, zlogp);
4101 			}
4102 		} else if (xerr == ZE_CHROOTED) {
4103 			zerror(zlogp, B_FALSE, "%s: "
4104 			    "cannot create a zone from a chrooted "
4105 			    "environment", "zone_create");
4106 		} else if (xerr == ZE_LABELINUSE) {
4107 			char zonename[ZONENAME_MAX];
4108 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
4109 			    zonename, ZONENAME_MAX);
4110 			zerror(zlogp, B_FALSE, "The zone label is already "
4111 			    "used by the zone '%s'.", zonename);
4112 		} else {
4113 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
4114 		}
4115 		goto error;
4116 	}
4117 
4118 	if (zonecfg_in_alt_root() &&
4119 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4120 	    zonecfg_get_root()) == -1) {
4121 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4122 		goto error;
4123 	}
4124 
4125 	/*
4126 	 * The following actions are not performed when merely mounting a zone
4127 	 * for administrative use.
4128 	 */
4129 	if (mount_cmd == Z_MNT_BOOT) {
4130 		brand_handle_t bh;
4131 		struct brand_attr attr;
4132 		char modname[MAXPATHLEN];
4133 
4134 		if (setup_zone_hostid(zlogp, zone_name, zoneid) != Z_OK)
4135 			goto error;
4136 
4137 		if ((bh = brand_open(brand_name)) == NULL) {
4138 			zerror(zlogp, B_FALSE,
4139 			    "unable to determine brand name");
4140 			goto error;
4141 		}
4142 
4143 		if (!is_system_labeled() &&
4144 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
4145 			brand_close(bh);
4146 			zerror(zlogp, B_FALSE,
4147 			    "cannot boot labeled zone on unlabeled system");
4148 			goto error;
4149 		}
4150 
4151 		/*
4152 		 * If this brand requires any kernel support, now is the time to
4153 		 * get it loaded and initialized.
4154 		 */
4155 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4156 			brand_close(bh);
4157 			zerror(zlogp, B_FALSE,
4158 			    "unable to determine brand kernel module");
4159 			goto error;
4160 		}
4161 		brand_close(bh);
4162 
4163 		if (strlen(modname) > 0) {
4164 			(void) strlcpy(attr.ba_brandname, brand_name,
4165 			    sizeof (attr.ba_brandname));
4166 			(void) strlcpy(attr.ba_modname, modname,
4167 			    sizeof (attr.ba_modname));
4168 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
4169 			    sizeof (attr) != 0)) {
4170 				zerror(zlogp, B_TRUE,
4171 				    "could not set zone brand attribute.");
4172 				goto error;
4173 			}
4174 		}
4175 
4176 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
4177 			goto error;
4178 
4179 		set_mlps(zlogp, zoneid, zcent);
4180 	}
4181 
4182 	rval = zoneid;
4183 	zoneid = -1;
4184 
4185 error:
4186 	if (zoneid != -1) {
4187 		(void) zone_shutdown(zoneid);
4188 		(void) zone_destroy(zoneid);
4189 	}
4190 	if (rctlbuf != NULL)
4191 		free(rctlbuf);
4192 	priv_freeset(privs);
4193 	if (fp != NULL)
4194 		zonecfg_close_scratch(fp);
4195 	lofs_discard_mnttab();
4196 	if (zcent != NULL)
4197 		tsol_freezcent(zcent);
4198 	return (rval);
4199 }
4200 
4201 /*
4202  * Enter the zone and write a /etc/zones/index file there.  This allows
4203  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4204  * details from inside the zone.
4205  */
4206 static void
4207 write_index_file(zoneid_t zoneid)
4208 {
4209 	FILE *zef;
4210 	FILE *zet;
4211 	struct zoneent *zep;
4212 	pid_t child;
4213 	int tmpl_fd;
4214 	ctid_t ct;
4215 	int fd;
4216 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4217 
4218 	/* Locate the zone entry in the global zone's index file */
4219 	if ((zef = setzoneent()) == NULL)
4220 		return;
4221 	while ((zep = getzoneent_private(zef)) != NULL) {
4222 		if (strcmp(zep->zone_name, zone_name) == 0)
4223 			break;
4224 		free(zep);
4225 	}
4226 	endzoneent(zef);
4227 	if (zep == NULL)
4228 		return;
4229 
4230 	if ((tmpl_fd = init_template()) == -1) {
4231 		free(zep);
4232 		return;
4233 	}
4234 
4235 	if ((child = fork()) == -1) {
4236 		(void) ct_tmpl_clear(tmpl_fd);
4237 		(void) close(tmpl_fd);
4238 		free(zep);
4239 		return;
4240 	}
4241 
4242 	/* parent waits for child to finish */
4243 	if (child != 0) {
4244 		free(zep);
4245 		if (contract_latest(&ct) == -1)
4246 			ct = -1;
4247 		(void) ct_tmpl_clear(tmpl_fd);
4248 		(void) close(tmpl_fd);
4249 		(void) waitpid(child, NULL, 0);
4250 		(void) contract_abandon_id(ct);
4251 		return;
4252 	}
4253 
4254 	/* child enters zone and sets up index file */
4255 	(void) ct_tmpl_clear(tmpl_fd);
4256 	if (zone_enter(zoneid) != -1) {
4257 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4258 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4259 		    ZONE_CONFIG_GID);
4260 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4261 		    ZONE_INDEX_MODE);
4262 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4263 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4264 			if (uuid_is_null(zep->zone_uuid))
4265 				uuidstr[0] = '\0';
4266 			else
4267 				uuid_unparse(zep->zone_uuid, uuidstr);
4268 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4269 			    zone_state_str(zep->zone_state),
4270 			    uuidstr);
4271 			(void) fclose(zet);
4272 		}
4273 	}
4274 	_exit(0);
4275 }
4276 
4277 int
4278 vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
4279 {
4280 	char zonepath[MAXPATHLEN];
4281 
4282 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4283 		lofs_discard_mnttab();
4284 		return (-1);
4285 	}
4286 
4287 	/*
4288 	 * Before we try to mount filesystems we need to create the
4289 	 * attribute backing store for /dev
4290 	 */
4291 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
4292 		lofs_discard_mnttab();
4293 		return (-1);
4294 	}
4295 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
4296 
4297 	/* Make /dev directory owned by root, grouped sys */
4298 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
4299 	    0, 3) != 0) {
4300 		lofs_discard_mnttab();
4301 		return (-1);
4302 	}
4303 
4304 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4305 		lofs_discard_mnttab();
4306 		return (-1);
4307 	}
4308 
4309 	if (mount_cmd == Z_MNT_BOOT) {
4310 		zone_iptype_t iptype;
4311 
4312 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
4313 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4314 			lofs_discard_mnttab();
4315 			return (-1);
4316 		}
4317 
4318 		switch (iptype) {
4319 		case ZS_SHARED:
4320 			/* Always do this to make lo0 get configured */
4321 			if (configure_shared_network_interfaces(zlogp) != 0) {
4322 				lofs_discard_mnttab();
4323 				return (-1);
4324 			}
4325 			break;
4326 		case ZS_EXCLUSIVE:
4327 			if (configure_exclusive_network_interfaces(zlogp) !=
4328 			    0) {
4329 				lofs_discard_mnttab();
4330 				return (-1);
4331 			}
4332 			break;
4333 		}
4334 	}
4335 
4336 	write_index_file(zoneid);
4337 
4338 	lofs_discard_mnttab();
4339 	return (0);
4340 }
4341 
4342 static int
4343 lu_root_teardown(zlog_t *zlogp)
4344 {
4345 	char zroot[MAXPATHLEN];
4346 
4347 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4348 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4349 		return (-1);
4350 	}
4351 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4352 
4353 	/*
4354 	 * At this point, the processes are gone, the filesystems (save the
4355 	 * root) are unmounted, and the zone is on death row.  But there may
4356 	 * still be creds floating about in the system that reference the
4357 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4358 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4359 	 * (How I wish this were not true, and umount2 just did the right
4360 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4361 	 */
4362 	if (umount2(zroot, MS_FORCE) != 0) {
4363 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4364 			goto unmounted;
4365 		if (errno == EBUSY) {
4366 			int tries = 10;
4367 
4368 			while (--tries >= 0) {
4369 				(void) sleep(1);
4370 				if (umount2(zroot, 0) == 0)
4371 					goto unmounted;
4372 				if (errno != EBUSY)
4373 					break;
4374 			}
4375 		}
4376 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4377 		return (-1);
4378 	}
4379 unmounted:
4380 
4381 	/*
4382 	 * Only zones in an alternate root environment have scratch zone
4383 	 * entries.
4384 	 */
4385 	if (zonecfg_in_alt_root()) {
4386 		FILE *fp;
4387 		int retv;
4388 
4389 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4390 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4391 			return (-1);
4392 		}
4393 		retv = -1;
4394 		if (zonecfg_lock_scratch(fp) != 0)
4395 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4396 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4397 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4398 		else
4399 			retv = 0;
4400 		zonecfg_close_scratch(fp);
4401 		return (retv);
4402 	} else {
4403 		return (0);
4404 	}
4405 }
4406 
4407 int
4408 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4409 {
4410 	char *kzone;
4411 	zoneid_t zoneid;
4412 	int res;
4413 	char pool_err[128];
4414 	char zpath[MAXPATHLEN];
4415 	char cmdbuf[MAXPATHLEN];
4416 	brand_handle_t bh = NULL;
4417 	dladm_status_t status;
4418 	char errmsg[DLADM_STRSIZE];
4419 	ushort_t flags;
4420 
4421 	kzone = zone_name;
4422 	if (zonecfg_in_alt_root()) {
4423 		FILE *fp;
4424 
4425 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4426 			zerror(zlogp, B_TRUE, "unable to open map file");
4427 			goto error;
4428 		}
4429 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4430 		    kernzone, sizeof (kernzone)) != 0) {
4431 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4432 			zonecfg_close_scratch(fp);
4433 			goto error;
4434 		}
4435 		zonecfg_close_scratch(fp);
4436 		kzone = kernzone;
4437 	}
4438 
4439 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
4440 		if (!bringup_failure_recovery)
4441 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4442 		if (unmount_cmd)
4443 			(void) lu_root_teardown(zlogp);
4444 		goto error;
4445 	}
4446 
4447 	if (zone_shutdown(zoneid) != 0) {
4448 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
4449 		goto error;
4450 	}
4451 
4452 	/* Get the zonepath of this zone */
4453 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
4454 		zerror(zlogp, B_FALSE, "unable to determine zone path");
4455 		goto error;
4456 	}
4457 
4458 	/* Get a handle to the brand info for this zone */
4459 	if ((bh = brand_open(brand_name)) == NULL) {
4460 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
4461 		return (-1);
4462 	}
4463 	/*
4464 	 * If there is a brand 'halt' callback, execute it now to give the
4465 	 * brand a chance to cleanup any custom configuration.
4466 	 */
4467 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4468 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
4469 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
4470 		brand_close(bh);
4471 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
4472 		    "halt callback.");
4473 		goto error;
4474 	}
4475 	brand_close(bh);
4476 
4477 	if ((strlen(cmdbuf) > EXEC_LEN) &&
4478 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
4479 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
4480 		goto error;
4481 	}
4482 
4483 	if (!unmount_cmd) {
4484 		zone_iptype_t iptype;
4485 
4486 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4487 		    sizeof (flags)) < 0) {
4488 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
4489 				zerror(zlogp, B_TRUE, "unable to determine "
4490 				    "ip-type");
4491 				goto error;
4492 			}
4493 		} else {
4494 			if (flags & ZF_NET_EXCL)
4495 				iptype = ZS_EXCLUSIVE;
4496 			else
4497 				iptype = ZS_SHARED;
4498 		}
4499 
4500 		switch (iptype) {
4501 		case ZS_SHARED:
4502 			if (unconfigure_shared_network_interfaces(zlogp,
4503 			    zoneid) != 0) {
4504 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4505 				    "network interfaces in zone");
4506 				goto error;
4507 			}
4508 			break;
4509 		case ZS_EXCLUSIVE:
4510 			if (unconfigure_exclusive_network_interfaces(zlogp,
4511 			    zoneid) != 0) {
4512 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4513 				    "network interfaces in zone");
4514 				goto error;
4515 			}
4516 			status = dladm_zone_halt(dld_handle, zoneid);
4517 			if (status != DLADM_STATUS_OK) {
4518 				zerror(zlogp, B_FALSE, "unable to notify "
4519 				    "dlmgmtd of zone halt: %s",
4520 				    dladm_status2str(status, errmsg));
4521 			}
4522 			break;
4523 		}
4524 	}
4525 
4526 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
4527 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
4528 		goto error;
4529 	}
4530 
4531 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
4532 		zerror(zlogp, B_FALSE,
4533 		    "unable to unmount file systems in zone");
4534 		goto error;
4535 	}
4536 
4537 	/*
4538 	 * If we are rebooting then we normally don't want to destroy an
4539 	 * existing temporary pool at this point so that we can just reuse it
4540 	 * when the zone boots back up.  However, it is also possible we were
4541 	 * running with a temporary pool and the zone configuration has been
4542 	 * modified to no longer use a temporary pool.  In that case we need
4543 	 * to destroy the temporary pool now.  This case looks like the case
4544 	 * where we never had a temporary pool configured but
4545 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
4546 	 */
4547 	if (!unmount_cmd) {
4548 		boolean_t destroy_tmp_pool = B_TRUE;
4549 
4550 		if (rebooting) {
4551 			struct zone_psettab pset_tab;
4552 			zone_dochandle_t handle;
4553 
4554 			if ((handle = zonecfg_init_handle()) != NULL &&
4555 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4556 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4557 				destroy_tmp_pool = B_FALSE;
4558 
4559 			zonecfg_fini_handle(handle);
4560 		}
4561 
4562 		if (destroy_tmp_pool) {
4563 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
4564 			    sizeof (pool_err))) != Z_OK) {
4565 				if (res == Z_POOL)
4566 					zerror(zlogp, B_FALSE, pool_err);
4567 			}
4568 		}
4569 	}
4570 
4571 	remove_mlps(zlogp, zoneid);
4572 
4573 	if (zone_destroy(zoneid) != 0) {
4574 		zerror(zlogp, B_TRUE, "unable to destroy zone");
4575 		goto error;
4576 	}
4577 
4578 	/*
4579 	 * Special teardown for alternate boot environments: remove the tmpfs
4580 	 * root for the zone and then remove it from the map file.
4581 	 */
4582 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4583 		goto error;
4584 
4585 	lofs_discard_mnttab();
4586 	return (0);
4587 
4588 error:
4589 	lofs_discard_mnttab();
4590 	return (-1);
4591 }
4592