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