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