xref: /titanic_54/usr/src/cmd/zoneadmd/vplat.c (revision 99653d4ee642c6528e88224f12409a5f23060994)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
23ea8dc4b6Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
317c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
327c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
337c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
347c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
357c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
367c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
37108322fbScarlsonj  *
38108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
39108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
40108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
41108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
42108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
43108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
44108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
45108322fbScarlsonj  * without relying on any binaries in the zone itself.
46108322fbScarlsonj  *
47108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
48108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
49108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
50108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
51108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
52108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
53108322fbScarlsonj  * process.
54108322fbScarlsonj  *
55108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
56108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
57108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
58108322fbScarlsonj  * $ZONEPATH/lu as the root.
59108322fbScarlsonj  *
60108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
61108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
62108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
63108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/param.h>
677c478bd9Sstevel@tonic-gate #include <sys/mount.h>
687c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
697c478bd9Sstevel@tonic-gate #include <sys/socket.h>
707c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
717c478bd9Sstevel@tonic-gate #include <sys/types.h>
727c478bd9Sstevel@tonic-gate #include <sys/stat.h>
737c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
747c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
757c478bd9Sstevel@tonic-gate #include <sys/conf.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
787c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
797c478bd9Sstevel@tonic-gate #include <netinet/in.h>
807c478bd9Sstevel@tonic-gate #include <net/route.h>
817c478bd9Sstevel@tonic-gate #include <netdb.h>
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #include <stdio.h>
847c478bd9Sstevel@tonic-gate #include <errno.h>
857c478bd9Sstevel@tonic-gate #include <fcntl.h>
867c478bd9Sstevel@tonic-gate #include <unistd.h>
877c478bd9Sstevel@tonic-gate #include <rctl.h>
887c478bd9Sstevel@tonic-gate #include <stdlib.h>
897c478bd9Sstevel@tonic-gate #include <string.h>
907c478bd9Sstevel@tonic-gate #include <strings.h>
917c478bd9Sstevel@tonic-gate #include <wait.h>
927c478bd9Sstevel@tonic-gate #include <limits.h>
937c478bd9Sstevel@tonic-gate #include <libgen.h>
94fa9e4066Sahrens #include <libzfs.h>
957c478bd9Sstevel@tonic-gate #include <zone.h>
967c478bd9Sstevel@tonic-gate #include <assert.h>
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
997c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1007c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1017c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
102fa9e4066Sahrens #include <sys/fs/zfs.h>
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #include <pool.h>
1057c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
1087c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
10945916cd2Sjpk #include <tsol/label.h>
11045916cd2Sjpk #include <libtsnet.h>
11145916cd2Sjpk #include <sys/priv.h>
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1147c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1177c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1187c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1217c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
12445916cd2Sjpk #define	MAXTNZLEN	2048
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * A list of directories which should be created.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate struct dir_info {
1317c478bd9Sstevel@tonic-gate 	char *dir_name;
1327c478bd9Sstevel@tonic-gate 	mode_t dir_mode;
1337c478bd9Sstevel@tonic-gate };
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * The pathnames below are relative to the zonepath
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static struct dir_info dev_dirs[] = {
1397c478bd9Sstevel@tonic-gate 	{ "/dev",	0755 },
1407c478bd9Sstevel@tonic-gate 	{ "/dev/dsk",	0755 },
1417c478bd9Sstevel@tonic-gate 	{ "/dev/fd",	0555 },
1427c478bd9Sstevel@tonic-gate 	{ "/dev/pts",	0755 },
1437c478bd9Sstevel@tonic-gate 	{ "/dev/rdsk",	0755 },
1447c478bd9Sstevel@tonic-gate 	{ "/dev/rmt",	0755 },
1457c478bd9Sstevel@tonic-gate 	{ "/dev/sad",	0755 },
1467c478bd9Sstevel@tonic-gate 	{ "/dev/swap",	0755 },
1477c478bd9Sstevel@tonic-gate 	{ "/dev/term",	0755 },
1487c478bd9Sstevel@tonic-gate };
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * A list of devices which should be symlinked to /dev/zconsole.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate struct symlink_info {
1557c478bd9Sstevel@tonic-gate 	char *sl_source;
1567c478bd9Sstevel@tonic-gate 	char *sl_target;
1577c478bd9Sstevel@tonic-gate };
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * The "source" paths are relative to the zonepath
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate static struct symlink_info dev_symlinks[] = {
1637c478bd9Sstevel@tonic-gate 	{ "/dev/stderr",	"./fd/2" },
1647c478bd9Sstevel@tonic-gate 	{ "/dev/stdin",		"./fd/0" },
1657c478bd9Sstevel@tonic-gate 	{ "/dev/stdout",	"./fd/1" },
1667c478bd9Sstevel@tonic-gate 	{ "/dev/dtremote",	"/dev/null" },
1677c478bd9Sstevel@tonic-gate 	{ "/dev/console",	"zconsole" },
1687c478bd9Sstevel@tonic-gate 	{ "/dev/syscon",	"zconsole" },
1697c478bd9Sstevel@tonic-gate 	{ "/dev/sysmsg",	"zconsole" },
1707c478bd9Sstevel@tonic-gate 	{ "/dev/systty",	"zconsole" },
1717c478bd9Sstevel@tonic-gate 	{ "/dev/msglog",	"zconsole" },
1727c478bd9Sstevel@tonic-gate };
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /* for routing socket */
1757c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1767c478bd9Sstevel@tonic-gate 
177108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
178108322fbScarlsonj static char kernzone[ZONENAME_MAX];
179108322fbScarlsonj 
180108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
181108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
182108322fbScarlsonj 
18345916cd2Sjpk /* for Trusted Extensions */
18445916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
18545916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
18645916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
18745916cd2Sjpk static m_label_t *zlabel = NULL;
18845916cd2Sjpk static m_label_t *zid_label = NULL;
18945916cd2Sjpk static priv_set_t *zprivs = NULL;
19045916cd2Sjpk 
1917c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1927c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
195108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
196108322fbScarlsonj  * data) only once every N times through the loop.
197108322fbScarlsonj  */
198108322fbScarlsonj #define	MNTTAB_HUNK	32
199108322fbScarlsonj 
200108322fbScarlsonj /*
2017c478bd9Sstevel@tonic-gate  * Private autofs system call
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static int
2067c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
214108322fbScarlsonj static void
215108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
216108322fbScarlsonj {
217108322fbScarlsonj 	uint_t i;
218108322fbScarlsonj 
219108322fbScarlsonj 	if (mnt_array == NULL)
220108322fbScarlsonj 		return;
221108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
222108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
223108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
224108322fbScarlsonj 		free(mnt_array[i].mnt_special);
225108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
226108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
227108322fbScarlsonj 	}
228108322fbScarlsonj 	free(mnt_array);
229108322fbScarlsonj }
230108322fbScarlsonj 
231108322fbScarlsonj /*
232108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
233108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
234108322fbScarlsonj  * array in "nelemp".
235108322fbScarlsonj  */
236108322fbScarlsonj static int
237108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
238108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
239108322fbScarlsonj {
240108322fbScarlsonj 	struct mnttab mnt;
241108322fbScarlsonj 	struct mnttab *mnts;
242108322fbScarlsonj 	struct mnttab *mnp;
243108322fbScarlsonj 	uint_t nmnt;
244108322fbScarlsonj 
245108322fbScarlsonj 	rewind(mnttab);
246108322fbScarlsonj 	resetmnttab(mnttab);
247108322fbScarlsonj 	nmnt = 0;
248108322fbScarlsonj 	mnts = NULL;
249108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
250108322fbScarlsonj 		struct mnttab *tmp_array;
251108322fbScarlsonj 
252108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
253108322fbScarlsonj 			continue;
254108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
255108322fbScarlsonj 			tmp_array = realloc(mnts,
256108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
257108322fbScarlsonj 			if (tmp_array == NULL) {
258108322fbScarlsonj 				free_mnttable(mnts, nmnt);
259108322fbScarlsonj 				return (-1);
260108322fbScarlsonj 			}
261108322fbScarlsonj 			mnts = tmp_array;
262108322fbScarlsonj 		}
263108322fbScarlsonj 		mnp = &mnts[nmnt++];
264108322fbScarlsonj 
265108322fbScarlsonj 		/*
266108322fbScarlsonj 		 * Zero out any fields we're not using.
267108322fbScarlsonj 		 */
268108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
269108322fbScarlsonj 
270108322fbScarlsonj 		if (mnt.mnt_special != NULL)
271108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
272108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
273108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
274108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
275108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
276108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
277108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
278108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
279108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
280108322fbScarlsonj 			free_mnttable(mnts, nmnt);
281108322fbScarlsonj 			return (-1);
282108322fbScarlsonj 		}
283108322fbScarlsonj 	}
284108322fbScarlsonj 	*mnt_arrayp = mnts;
285108322fbScarlsonj 	*nelemp = nmnt;
286108322fbScarlsonj 	return (0);
287108322fbScarlsonj }
288108322fbScarlsonj 
289108322fbScarlsonj /*
290108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
291108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
292108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
293108322fbScarlsonj  * reread of the list of mount points
294108322fbScarlsonj  */
295108322fbScarlsonj static void
296108322fbScarlsonj lofs_discard_mnttab(void)
297108322fbScarlsonj {
298108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
299108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
300108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
301108322fbScarlsonj }
302108322fbScarlsonj 
303108322fbScarlsonj static int
304108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
305108322fbScarlsonj {
306108322fbScarlsonj 	FILE *mnttab;
307108322fbScarlsonj 	uint_t nmnts;
308108322fbScarlsonj 
309108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
310108322fbScarlsonj 		return (-1);
311108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
312108322fbScarlsonj 	    &nmnts) == -1) {
313108322fbScarlsonj 		(void) fclose(mnttab);
314108322fbScarlsonj 		return (-1);
315108322fbScarlsonj 	}
316108322fbScarlsonj 	(void) fclose(mnttab);
317108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
318108322fbScarlsonj 	return (0);
319108322fbScarlsonj }
320108322fbScarlsonj 
321108322fbScarlsonj /*
322108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
323108322fbScarlsonj  * path and resolves them all down to an absolute path.
324108322fbScarlsonj  */
325108322fbScarlsonj static void
326108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
327108322fbScarlsonj {
328108322fbScarlsonj 	int len, arlen;
329108322fbScarlsonj 	const char *altroot;
330108322fbScarlsonj 	char tmppath[MAXPATHLEN];
331108322fbScarlsonj 	boolean_t outside_altroot;
332108322fbScarlsonj 
333108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
334108322fbScarlsonj 		return;
335108322fbScarlsonj 	tmppath[len] = '\0';
336108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
337108322fbScarlsonj 
338108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
339108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
340108322fbScarlsonj 		return;
341108322fbScarlsonj 
342108322fbScarlsonj 	altroot = zonecfg_get_root();
343108322fbScarlsonj 	arlen = strlen(altroot);
344108322fbScarlsonj 	outside_altroot = B_FALSE;
345108322fbScarlsonj 	for (;;) {
346108322fbScarlsonj 		struct mnttab *mnp;
347108322fbScarlsonj 
348108322fbScarlsonj 		for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
349108322fbScarlsonj 		    mnp++) {
350108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
351108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
352108322fbScarlsonj 			    mnp->mnt_special == NULL ||
353108322fbScarlsonj 			    strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
354108322fbScarlsonj 				continue;
355108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
356108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
357108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
358108322fbScarlsonj 				break;
359108322fbScarlsonj 		}
360108322fbScarlsonj 		if (mnp >= resolve_lofs_mnt_max)
361108322fbScarlsonj 			break;
362108322fbScarlsonj 		if (outside_altroot) {
363108322fbScarlsonj 			char *cp;
364108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
365108322fbScarlsonj 
366108322fbScarlsonj 			/*
367108322fbScarlsonj 			 * If we run into a read-only mount outside of the
368108322fbScarlsonj 			 * alternate root environment, then the user doesn't
369108322fbScarlsonj 			 * want this path to be made read-write.
370108322fbScarlsonj 			 */
371108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
372108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
373108322fbScarlsonj 			    NULL &&
374108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
375108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
376108322fbScarlsonj 				break;
377108322fbScarlsonj 			}
378108322fbScarlsonj 		} else if (arlen > 0 &&
379108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
380108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
381108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
382108322fbScarlsonj 			outside_altroot = B_TRUE;
383108322fbScarlsonj 		}
384108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
385108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
386108322fbScarlsonj 		    mnp->mnt_special, path + len);
387108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
388108322fbScarlsonj 			break;
389108322fbScarlsonj 		path[len] = '\0';
390108322fbScarlsonj 	}
391108322fbScarlsonj }
392108322fbScarlsonj 
393108322fbScarlsonj /*
394108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
395108322fbScarlsonj  * referenced device is already mounted somewhere.
396108322fbScarlsonj  */
397108322fbScarlsonj static int
398108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
399108322fbScarlsonj {
400108322fbScarlsonj 	struct mnttab *mnp;
401108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
402108322fbScarlsonj 
403108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
404108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
405108322fbScarlsonj 		return (-1);
406108322fbScarlsonj 
407108322fbScarlsonj 	/*
408108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
409108322fbScarlsonj 	 * no need to worry about conflicting mounts.
410108322fbScarlsonj 	 */
411108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
412108322fbScarlsonj 	    mnp++) {
413108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
414108322fbScarlsonj 			break;
415108322fbScarlsonj 	}
416108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
417108322fbScarlsonj 		return (0);
418108322fbScarlsonj 
419108322fbScarlsonj 	/*
420108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
421108322fbScarlsonj 	 */
422108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
423108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
424108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
425108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
426108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
427108322fbScarlsonj 
428108322fbScarlsonj 	/*
429108322fbScarlsonj 	 * Discard all but one of the original options and set that to be the
430108322fbScarlsonj 	 * same set of options used for inherit package directory resources.
431108322fbScarlsonj 	 */
432108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
433108322fbScarlsonj 	if (optptr == NULL) {
434108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
435108322fbScarlsonj 		if (optptr == NULL) {
436108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
437108322fbScarlsonj 			    fsptr->zone_fs_dir);
438108322fbScarlsonj 			return (-1);
439108322fbScarlsonj 		}
440108322fbScarlsonj 	} else {
441108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
442108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
443108322fbScarlsonj 			free(onext);
444108322fbScarlsonj 		}
445108322fbScarlsonj 	}
446108322fbScarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
447108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
448108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
449108322fbScarlsonj 	return (0);
450108322fbScarlsonj }
451108322fbScarlsonj 
4527c478bd9Sstevel@tonic-gate static int
4537c478bd9Sstevel@tonic-gate make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4567c478bd9Sstevel@tonic-gate 	struct stat st;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4597c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4607c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4617c478bd9Sstevel@tonic-gate 		    subdir);
4627c478bd9Sstevel@tonic-gate 		return (-1);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4687c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4697c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4707c478bd9Sstevel@tonic-gate 		 */
4717c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
47245916cd2Sjpk 			if (is_system_labeled() &&
47345916cd2Sjpk 			    S_ISREG(st.st_mode)) {
47445916cd2Sjpk 				/*
47545916cd2Sjpk 				 * The need to mount readonly copies of
47645916cd2Sjpk 				 * global zone /etc/ files is unique to
47745916cd2Sjpk 				 * Trusted Extensions.
47845916cd2Sjpk 				 */
47945916cd2Sjpk 				if (strncmp(subdir, "/etc/",
48045916cd2Sjpk 				    strlen("/etc/")) != 0) {
48145916cd2Sjpk 					zerror(zlogp, B_FALSE,
48245916cd2Sjpk 					    "%s is not in /etc", path);
4837c478bd9Sstevel@tonic-gate 					return (-1);
4847c478bd9Sstevel@tonic-gate 				}
48545916cd2Sjpk 			} else {
48645916cd2Sjpk 				zerror(zlogp, B_FALSE,
48745916cd2Sjpk 				    "%s is not a directory", path);
48845916cd2Sjpk 				return (-1);
48945916cd2Sjpk 			}
49045916cd2Sjpk 		}
4917c478bd9Sstevel@tonic-gate 	} else if (mkdirp(path, mode) != 0) {
4927c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4937c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4947c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4957c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4967c478bd9Sstevel@tonic-gate 		else
4977c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4987c478bd9Sstevel@tonic-gate 		return (-1);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 	return (0);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * Make /dev and various directories underneath it.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate static int
5077c478bd9Sstevel@tonic-gate make_dev_dirs(zlog_t *zlogp, const char *zonepath)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	int i;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (dev_dirs) / sizeof (struct dir_info); i++) {
5127c478bd9Sstevel@tonic-gate 		if (make_one_dir(zlogp, zonepath, dev_dirs[i].dir_name,
5137c478bd9Sstevel@tonic-gate 		    dev_dirs[i].dir_mode) != 0)
5147c478bd9Sstevel@tonic-gate 			return (-1);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	return (0);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /*
5207c478bd9Sstevel@tonic-gate  * Make various sym-links underneath /dev.
5217c478bd9Sstevel@tonic-gate  */
5227c478bd9Sstevel@tonic-gate static int
5237c478bd9Sstevel@tonic-gate make_dev_links(zlog_t *zlogp, char *zonepath)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	int i;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (dev_symlinks) / sizeof (struct symlink_info);
5287c478bd9Sstevel@tonic-gate 	    i++) {
5297c478bd9Sstevel@tonic-gate 		char dev[MAXPATHLEN];
5307c478bd9Sstevel@tonic-gate 		struct stat st;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		(void) snprintf(dev, sizeof (dev), "%s%s", zonepath,
5337c478bd9Sstevel@tonic-gate 		    dev_symlinks[i].sl_source);
5347c478bd9Sstevel@tonic-gate 		if (lstat(dev, &st) == 0) {
5357c478bd9Sstevel@tonic-gate 			/*
5367c478bd9Sstevel@tonic-gate 			 * Try not to call unlink(2) on directories, since that
5377c478bd9Sstevel@tonic-gate 			 * makes UFS unhappy.
5387c478bd9Sstevel@tonic-gate 			 */
5397c478bd9Sstevel@tonic-gate 			if (S_ISDIR(st.st_mode)) {
5407c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "symlink path %s is a "
5417c478bd9Sstevel@tonic-gate 				    "directory", dev_symlinks[i].sl_source);
5427c478bd9Sstevel@tonic-gate 				return (-1);
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 			(void) unlink(dev);
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 		if (symlink(dev_symlinks[i].sl_target, dev) != 0) {
547108322fbScarlsonj 			zerror(zlogp, B_TRUE, "could not setup %s->%s symlink",
548108322fbScarlsonj 			    dev_symlinks[i].sl_source,
549108322fbScarlsonj 			    dev_symlinks[i].sl_target);
5507c478bd9Sstevel@tonic-gate 			return (-1);
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	return (0);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Create various directories and sym-links under /dev.
5587c478bd9Sstevel@tonic-gate  */
5597c478bd9Sstevel@tonic-gate static int
5607c478bd9Sstevel@tonic-gate create_dev_files(zlog_t *zlogp)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
5657c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
5667c478bd9Sstevel@tonic-gate 		return (-1);
5677c478bd9Sstevel@tonic-gate 	}
568108322fbScarlsonj 	if (zonecfg_in_alt_root())
569108322fbScarlsonj 		resolve_lofs(zlogp, zonepath, sizeof (zonepath));
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if (make_dev_dirs(zlogp, zonepath) != 0)
5727c478bd9Sstevel@tonic-gate 		return (-1);
5737c478bd9Sstevel@tonic-gate 	if (make_dev_links(zlogp, zonepath) != 0)
5747c478bd9Sstevel@tonic-gate 		return (-1);
5757c478bd9Sstevel@tonic-gate 	return (0);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate static void
5797c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	uint_t i;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (types == NULL)
5847c478bd9Sstevel@tonic-gate 		return;
5857c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
5867c478bd9Sstevel@tonic-gate 		free(types[i]);
5877c478bd9Sstevel@tonic-gate 	free(types);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate static char **
5917c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	char **types = NULL;
5947c478bd9Sstevel@tonic-gate 	FILE *fp;
5957c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
5967c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
5977c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
5987c478bd9Sstevel@tonic-gate 	uint_t i;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
6017c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
6027c478bd9Sstevel@tonic-gate 		return (NULL);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	/*
6057c478bd9Sstevel@tonic-gate 	 * Count the number of lines
6067c478bd9Sstevel@tonic-gate 	 */
6077c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
6087c478bd9Sstevel@tonic-gate 		lines++;
6097c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
6107c478bd9Sstevel@tonic-gate 		goto out;
6117c478bd9Sstevel@tonic-gate 	rewind(fp);
6127c478bd9Sstevel@tonic-gate 	/*
6137c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
6167c478bd9Sstevel@tonic-gate 	if (types == NULL) {
6177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
6187c478bd9Sstevel@tonic-gate 		goto out;
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	i = 0;
6217c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
6227c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
6237c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
6247c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
6257c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
6267c478bd9Sstevel@tonic-gate 			types = NULL;
6277c478bd9Sstevel@tonic-gate 			goto out;
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
6307c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
6317c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
6327c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
6337c478bd9Sstevel@tonic-gate 			types = NULL;
6347c478bd9Sstevel@tonic-gate 			goto out;
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 		i++;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate out:
6397c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
6407c478bd9Sstevel@tonic-gate 	return (types);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate static boolean_t
6447c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	uint_t i;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
6497c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6507c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
6517c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
6527c478bd9Sstevel@tonic-gate 			return (B_TRUE);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	return (B_FALSE);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
657108322fbScarlsonj /*
658108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
659108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
660108322fbScarlsonj  */
6617c478bd9Sstevel@tonic-gate static void
662108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
6637c478bd9Sstevel@tonic-gate {
664108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
665108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
666108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate /*
6707c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
6717c478bd9Sstevel@tonic-gate  *
6727c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
6737c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
6747c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
6757c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
6767c478bd9Sstevel@tonic-gate  *
6777c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
6787c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
6797c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
6807c478bd9Sstevel@tonic-gate  *
6817c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
6827c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
6837c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
6847c478bd9Sstevel@tonic-gate  *
6857c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
6867c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
6877c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
6887c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
6897c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
6907c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
6917c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
6927c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
6937c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
6947c478bd9Sstevel@tonic-gate  * graceful mounts first.
6957c478bd9Sstevel@tonic-gate  *
6967c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
6977c478bd9Sstevel@tonic-gate  */
6987c478bd9Sstevel@tonic-gate static int
699108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	int error = 0;
7027c478bd9Sstevel@tonic-gate 	FILE *mnttab;
7037c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
7047c478bd9Sstevel@tonic-gate 	uint_t nmnt;
7057c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
7067c478bd9Sstevel@tonic-gate 	size_t zrootlen;
7077c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
7087c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
7097c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
7127c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
7137c478bd9Sstevel@tonic-gate 		return (-1);
7147c478bd9Sstevel@tonic-gate 	}
715108322fbScarlsonj 	if (unmount_cmd)
716108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
7197c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
7207c478bd9Sstevel@tonic-gate 
72145916cd2Sjpk 	/*
72245916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
72345916cd2Sjpk 	 * of our zone's /export/home
72445916cd2Sjpk 	 */
72548451833Scarlsonj 	if (!unmount_cmd)
72645916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
72745916cd2Sjpk 
7287c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
7297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
7307c478bd9Sstevel@tonic-gate 		return (-1);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
7347c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
7357c478bd9Sstevel@tonic-gate 	 */
7367c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
7377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
7387c478bd9Sstevel@tonic-gate 		error++;
7397c478bd9Sstevel@tonic-gate 		goto out;
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/*
7437c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
7447c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
7457c478bd9Sstevel@tonic-gate 	 */
7467c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
7477c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
7487c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
7497c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
7507c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
7517c478bd9Sstevel@tonic-gate 		char *path;
7527c478bd9Sstevel@tonic-gate 		uint_t i;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		mnts = NULL;
7557c478bd9Sstevel@tonic-gate 		nmnt = 0;
7567c478bd9Sstevel@tonic-gate 		/*
7577c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
7587c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
7597c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
7607c478bd9Sstevel@tonic-gate 		 * filesystems.
7617c478bd9Sstevel@tonic-gate 		 */
7627c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
7637c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
7647c478bd9Sstevel@tonic-gate 			error++;
7657c478bd9Sstevel@tonic-gate 			goto out;
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
7687c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
7697c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
7707c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
7717c478bd9Sstevel@tonic-gate 			/*
7727c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
7737c478bd9Sstevel@tonic-gate 			 *
7747c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
7757c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
7767c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
7777c478bd9Sstevel@tonic-gate 			 */
7787c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
7797c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
7807c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7817c478bd9Sstevel@tonic-gate 			}
7827c478bd9Sstevel@tonic-gate 			/*
7837c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
7847c478bd9Sstevel@tonic-gate 			 */
7857c478bd9Sstevel@tonic-gate 			if (stuck) {
7867c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
7877c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7887c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
7897c478bd9Sstevel@tonic-gate 				} else {
7907c478bd9Sstevel@tonic-gate 					/*
7917c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
7927c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
7937c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
7947c478bd9Sstevel@tonic-gate 					 * bail.
7957c478bd9Sstevel@tonic-gate 					 */
7967c478bd9Sstevel@tonic-gate 					error++;
7977c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
7987c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
7997c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
8007c478bd9Sstevel@tonic-gate 					goto out;
8017c478bd9Sstevel@tonic-gate 				}
8027c478bd9Sstevel@tonic-gate 			}
8037c478bd9Sstevel@tonic-gate 			/*
8047c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
8057c478bd9Sstevel@tonic-gate 			 */
8067c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
8077c478bd9Sstevel@tonic-gate 				newcount++;
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		if (newcount == 0)
8127c478bd9Sstevel@tonic-gate 			break;
8137c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
8147c478bd9Sstevel@tonic-gate 			/*
8157c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
8167c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
8177c478bd9Sstevel@tonic-gate 			 */
8187c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 		oldcount = newcount;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 		/*
8237c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
8247c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
8257c478bd9Sstevel@tonic-gate 		 */
8267c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
8277c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
8287c478bd9Sstevel@tonic-gate 			error++;
8297c478bd9Sstevel@tonic-gate 			goto out;
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate out:
8347c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
8357c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
8367c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate static int
8407c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
8437c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate /*
8497c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
8507c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
8517c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
8527c478bd9Sstevel@tonic-gate  *
8537c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
8547c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
8557c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
8567c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
8577c478bd9Sstevel@tonic-gate  * remains the case.
8587c478bd9Sstevel@tonic-gate  */
8597c478bd9Sstevel@tonic-gate static int
8607c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate 	pid_t child_pid;
8637c478bd9Sstevel@tonic-gate 	int child_status = 0;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/*
8667c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
8677c478bd9Sstevel@tonic-gate 	 */
8687c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
8697c478bd9Sstevel@tonic-gate 	child_pid = fork();
8707c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
8717c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
8727c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
8737c478bd9Sstevel@tonic-gate 		return (-1);
8747c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
8757c478bd9Sstevel@tonic-gate 		closefrom(0);
8761390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
8771390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
8781390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
8791390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
8807c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
8817c478bd9Sstevel@tonic-gate 		/*
8827c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
8837c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
8847c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
8857c478bd9Sstevel@tonic-gate 		 * accordingly.
8867c478bd9Sstevel@tonic-gate 		 */
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
8897c478bd9Sstevel@tonic-gate 	} else {
8907c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
8947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8957c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
8967c478bd9Sstevel@tonic-gate 		return (-1);
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
8997c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
9007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
9017c478bd9Sstevel@tonic-gate 		return (-1);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate static int
9077c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
9087c478bd9Sstevel@tonic-gate {
9097c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
9107c478bd9Sstevel@tonic-gate 	char *argv[4];
9117c478bd9Sstevel@tonic-gate 	int status;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/*
9147c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
9157c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
9167c478bd9Sstevel@tonic-gate 	 */
9177c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
9187c478bd9Sstevel@tonic-gate 	    > sizeof (cmdbuf)) {
9197c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
9207c478bd9Sstevel@tonic-gate 		return (-1);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
9247c478bd9Sstevel@tonic-gate 	argv[1] = "-m";
9257c478bd9Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
9267c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
9297c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
9307c478bd9Sstevel@tonic-gate 		return (status);
9317c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
9327c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
9337c478bd9Sstevel@tonic-gate 	return (-1);
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate static int
9377c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
9387c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
9417c478bd9Sstevel@tonic-gate 	char *argv[6];
9427c478bd9Sstevel@tonic-gate 	int status;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	/*
9457c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
9467c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
9477c478bd9Sstevel@tonic-gate 	 */
9487c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
9497c478bd9Sstevel@tonic-gate 	    > sizeof (cmdbuf)) {
9507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
9517c478bd9Sstevel@tonic-gate 		return (-1);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
9547c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
9557c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
9567c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
9577c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
9587c478bd9Sstevel@tonic-gate 	} else {
9597c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
9607c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
9617c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
9627c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
9637c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
9677c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
9687c478bd9Sstevel@tonic-gate 		return (status);
9697c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
9707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
9717c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9727c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
9737c478bd9Sstevel@tonic-gate 	else
9747c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
9757c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9767c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
9777c478bd9Sstevel@tonic-gate 	return (-1);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * Make sure if a given path exists, it is not a sym-link, and is a directory.
9827c478bd9Sstevel@tonic-gate  */
9837c478bd9Sstevel@tonic-gate static int
9847c478bd9Sstevel@tonic-gate check_path(zlog_t *zlogp, const char *path)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9877c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9887c478bd9Sstevel@tonic-gate 	int res;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9917c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
9927c478bd9Sstevel@tonic-gate 			return (0);
9937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9947c478bd9Sstevel@tonic-gate 		return (-1);
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9987c478bd9Sstevel@tonic-gate 		return (-1);
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(statbuf.st_mode)) {
100145916cd2Sjpk 		if (is_system_labeled() && S_ISREG(statbuf.st_mode)) {
100245916cd2Sjpk 			/*
100345916cd2Sjpk 			 * The need to mount readonly copies of
100445916cd2Sjpk 			 * global zone /etc/ files is unique to
100545916cd2Sjpk 			 * Trusted Extensions.
100645916cd2Sjpk 			 * The check for /etc/ via strstr() is to
100745916cd2Sjpk 			 * allow paths like $ZONEROOT/etc/passwd
100845916cd2Sjpk 			 */
100945916cd2Sjpk 			if (strstr(path, "/etc/") == NULL) {
101045916cd2Sjpk 				zerror(zlogp, B_FALSE,
101145916cd2Sjpk 				    "%s is not in /etc", path);
101245916cd2Sjpk 				return (-1);
101345916cd2Sjpk 			}
101445916cd2Sjpk 		} else {
10157c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s is not a directory", path);
10167c478bd9Sstevel@tonic-gate 			return (-1);
10177c478bd9Sstevel@tonic-gate 		}
101845916cd2Sjpk 	}
10197c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
10207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
10217c478bd9Sstevel@tonic-gate 		return (-1);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
10247c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
10257c478bd9Sstevel@tonic-gate 		/*
10267c478bd9Sstevel@tonic-gate 		 * We don't like ".."s and "."s throwing us off
10277c478bd9Sstevel@tonic-gate 		 */
10287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
10297c478bd9Sstevel@tonic-gate 		return (-1);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 	return (0);
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate /*
10357c478bd9Sstevel@tonic-gate  * Check every component of rootpath/relpath.  If any component fails (ie,
10367c478bd9Sstevel@tonic-gate  * exists but isn't the canonical path to a directory), it is returned in
10377c478bd9Sstevel@tonic-gate  * badpath, which is assumed to be at least of size MAXPATHLEN.
10387c478bd9Sstevel@tonic-gate  *
10397c478bd9Sstevel@tonic-gate  * Relpath must begin with '/'.
10407c478bd9Sstevel@tonic-gate  */
10417c478bd9Sstevel@tonic-gate static boolean_t
10427c478bd9Sstevel@tonic-gate valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	char abspath[MAXPATHLEN], *slashp;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	/*
10477c478bd9Sstevel@tonic-gate 	 * Make sure abspath has at least one '/' after its rootpath
10487c478bd9Sstevel@tonic-gate 	 * component, and ends with '/'.
10497c478bd9Sstevel@tonic-gate 	 */
10507c478bd9Sstevel@tonic-gate 	if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) >
10517c478bd9Sstevel@tonic-gate 	    sizeof (abspath)) {
10527c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath,
10537c478bd9Sstevel@tonic-gate 		    relpath);
10547c478bd9Sstevel@tonic-gate 		return (B_FALSE);
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10587c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10597c478bd9Sstevel@tonic-gate 	do {
10607c478bd9Sstevel@tonic-gate 		*slashp = '\0';
10617c478bd9Sstevel@tonic-gate 		if (check_path(zlogp, abspath) != 0)
10627c478bd9Sstevel@tonic-gate 			return (B_FALSE);
10637c478bd9Sstevel@tonic-gate 		*slashp = '/';
10647c478bd9Sstevel@tonic-gate 		slashp++;
10657c478bd9Sstevel@tonic-gate 	} while ((slashp = strchr(slashp, '/')) != NULL);
10667c478bd9Sstevel@tonic-gate 	return (B_TRUE);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate static int
10707c478bd9Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	char    path[MAXPATHLEN];
1073108322fbScarlsonj 	char	specpath[MAXPATHLEN];
10747c478bd9Sstevel@tonic-gate 	char    optstr[MAX_MNTOPT_STR];
10757c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) {
10787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
10797c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
10807c478bd9Sstevel@tonic-gate 		return (-1);
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
10847c478bd9Sstevel@tonic-gate 	    DEFAULT_DIR_MODE) != 0)
10857c478bd9Sstevel@tonic-gate 		return (-1);
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
10887c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
10917c478bd9Sstevel@tonic-gate 		/*
10927c478bd9Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1093108322fbScarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1094108322fbScarlsonj 		 * can be seen via the alternate zone's root.
10957c478bd9Sstevel@tonic-gate 		 */
1096108322fbScarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1097108322fbScarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1098108322fbScarlsonj 		    sizeof (specpath)) {
1099108322fbScarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1100108322fbScarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1101108322fbScarlsonj 			return (-1);
1102108322fbScarlsonj 		}
1103108322fbScarlsonj 		if (zonecfg_in_alt_root())
1104108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
11057c478bd9Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1106108322fbScarlsonj 		    specpath, path) != 0) {
11077c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1108108322fbScarlsonj 			    specpath);
11097c478bd9Sstevel@tonic-gate 			return (-1);
11107c478bd9Sstevel@tonic-gate 		}
11117c478bd9Sstevel@tonic-gate 		return (0);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	/*
11157c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
11167c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
11177c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
11187c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
11197c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
11207c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
11217c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
11227c478bd9Sstevel@tonic-gate 	 */
11237c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
11247c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
11257c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
11267c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
11277c478bd9Sstevel@tonic-gate 		return (-1);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	/*
1131108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1132108322fbScarlsonj 	 * read-only loopback mounts as necessary.  For all lofs mounts, make
1133108322fbScarlsonj 	 * sure that the 'special' entry points inside the alternate root.  (We
1134108322fbScarlsonj 	 * don't do this with other mounts, as devfs isn't in the alternate
1135108322fbScarlsonj 	 * root, and we need to assume the device environment is roughly the
1136108322fbScarlsonj 	 * same.)
1137108322fbScarlsonj 	 */
1138108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1139108322fbScarlsonj 		struct stat64 st;
1140108322fbScarlsonj 
1141108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
1142108322fbScarlsonj 		    S_ISBLK(st.st_mode) &&
1143108322fbScarlsonj 		    check_lofs_needed(zlogp, fsptr) == -1)
1144108322fbScarlsonj 			return (-1);
1145108322fbScarlsonj 		if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1146108322fbScarlsonj 			if (snprintf(specpath, sizeof (specpath), "%s%s",
1147108322fbScarlsonj 			    zonecfg_get_root(), fsptr->zone_fs_special) >=
1148108322fbScarlsonj 			    sizeof (specpath)) {
1149108322fbScarlsonj 				zerror(zlogp, B_FALSE, "cannot mount %s: path "
1150108322fbScarlsonj 				    "too long in alternate root",
1151108322fbScarlsonj 				    fsptr->zone_fs_special);
1152108322fbScarlsonj 				return (-1);
1153108322fbScarlsonj 			}
1154108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
1155108322fbScarlsonj 			(void) strlcpy(fsptr->zone_fs_special, specpath,
1156108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1157108322fbScarlsonj 		}
1158108322fbScarlsonj 	}
1159108322fbScarlsonj 
1160108322fbScarlsonj 	/*
11617c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
11627c478bd9Sstevel@tonic-gate 	 */
11637c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
11647c478bd9Sstevel@tonic-gate 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0)
11657c478bd9Sstevel@tonic-gate 		return (-1);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	/*
11687c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
11697c478bd9Sstevel@tonic-gate 	 */
11707c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
11717c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
11727c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
11737c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
11747c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
11757c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
11767c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
11777c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
11787c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
11797c478bd9Sstevel@tonic-gate 		}
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 	return (domount(zlogp, fsptr->zone_fs_type, optstr,
11827c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_special, path));
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate static void
11867c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	uint_t i;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
11917c478bd9Sstevel@tonic-gate 		return;
11927c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
11937c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
11947c478bd9Sstevel@tonic-gate 	free(fsarray);
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate 
1197108322fbScarlsonj /*
1198108322fbScarlsonj  * This function constructs the miniroot-like "scratch zone" environment.  If
1199108322fbScarlsonj  * it returns B_FALSE, then the error has already been logged.
1200108322fbScarlsonj  */
1201108322fbScarlsonj static boolean_t
1202108322fbScarlsonj build_mounted(zlog_t *zlogp, char *rootpath, size_t rootlen,
1203108322fbScarlsonj     const char *zonepath)
1204108322fbScarlsonj {
1205108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1206108322fbScarlsonj 	char luroot[MAXPATHLEN];
1207108322fbScarlsonj 	const char **cpp;
1208108322fbScarlsonj 	static const char *mkdirs[] = {
1209108322fbScarlsonj 		"/system", "/system/contract", "/proc", "/dev", "/tmp",
1210108322fbScarlsonj 		"/a", NULL
1211108322fbScarlsonj 	};
1212108322fbScarlsonj 	static const char *localdirs[] = {
1213108322fbScarlsonj 		"/etc", "/var", NULL
1214108322fbScarlsonj 	};
1215108322fbScarlsonj 	static const char *loopdirs[] = {
1216108322fbScarlsonj 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1217108322fbScarlsonj 		"/usr", NULL
1218108322fbScarlsonj 	};
1219108322fbScarlsonj 	static const char *tmpdirs[] = {
1220108322fbScarlsonj 		"/tmp", "/var/run", NULL
1221108322fbScarlsonj 	};
1222108322fbScarlsonj 	FILE *fp;
1223108322fbScarlsonj 	struct stat st;
1224108322fbScarlsonj 	char *altstr;
1225108322fbScarlsonj 	uuid_t uuid;
1226108322fbScarlsonj 
1227108322fbScarlsonj 	/*
1228108322fbScarlsonj 	 * Construct a small Solaris environment, including the zone root
1229108322fbScarlsonj 	 * mounted on '/a' inside that environment.
1230108322fbScarlsonj 	 */
1231108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
1232108322fbScarlsonj 	(void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
1233108322fbScarlsonj 	resolve_lofs(zlogp, luroot, sizeof (luroot));
1234108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1235108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1236108322fbScarlsonj 
1237108322fbScarlsonj 	/*
1238108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1239108322fbScarlsonj 	 * zone_mount_early.)
1240108322fbScarlsonj 	 */
1241108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1242108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1243108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1244108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1245108322fbScarlsonj 			return (B_FALSE);
1246108322fbScarlsonj 		}
1247108322fbScarlsonj 	}
1248108322fbScarlsonj 
1249108322fbScarlsonj 	/*
1250108322fbScarlsonj 	 * These are mounted read-write from the zone undergoing upgrade.  We
1251108322fbScarlsonj 	 * must be careful not to 'leak' things from the main system into the
1252108322fbScarlsonj 	 * zone, and this accomplishes that goal.
1253108322fbScarlsonj 	 */
1254108322fbScarlsonj 	for (cpp = localdirs; *cpp != NULL; cpp++) {
1255108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1256108322fbScarlsonj 		(void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath,
1257108322fbScarlsonj 		    *cpp);
1258108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1259108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1260108322fbScarlsonj 			return (B_FALSE);
1261108322fbScarlsonj 		}
1262108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) {
1263108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1264108322fbScarlsonj 			    *cpp);
1265108322fbScarlsonj 			return (B_FALSE);
1266108322fbScarlsonj 		}
1267108322fbScarlsonj 	}
1268108322fbScarlsonj 
1269108322fbScarlsonj 	/*
1270108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1271108322fbScarlsonj 	 * they contain binaries that must match system.
1272108322fbScarlsonj 	 */
1273108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1274108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1275108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1276108322fbScarlsonj 			if (errno != EEXIST) {
1277108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1278108322fbScarlsonj 				return (B_FALSE);
1279108322fbScarlsonj 			}
1280108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1281108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1282108322fbScarlsonj 				return (B_FALSE);
1283108322fbScarlsonj 			}
1284108322fbScarlsonj 			/*
1285108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1286108322fbScarlsonj 			 * things that have been converted into symlinks
1287108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1288108322fbScarlsonj 			 * fixup.
1289108322fbScarlsonj 			 */
1290108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1291108322fbScarlsonj 				continue;
1292108322fbScarlsonj 		}
1293108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1294108322fbScarlsonj 		    tmp) != 0) {
1295108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1296108322fbScarlsonj 			    *cpp);
1297108322fbScarlsonj 			return (B_FALSE);
1298108322fbScarlsonj 		}
1299108322fbScarlsonj 	}
1300108322fbScarlsonj 
1301108322fbScarlsonj 	/*
1302108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1303108322fbScarlsonj 	 */
1304108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1305108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1306108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
1307108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1308108322fbScarlsonj 			return (B_FALSE);
1309108322fbScarlsonj 		}
1310108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1311108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1312108322fbScarlsonj 			return (B_FALSE);
1313108322fbScarlsonj 		}
1314108322fbScarlsonj 	}
1315108322fbScarlsonj 
1316108322fbScarlsonj 	/*
1317108322fbScarlsonj 	 * This is here to support lucopy.  If there's an instance of this same
1318108322fbScarlsonj 	 * zone on the current running system, then we mount its root up as
1319108322fbScarlsonj 	 * read-only inside the scratch zone.
1320108322fbScarlsonj 	 */
1321108322fbScarlsonj 	(void) zonecfg_get_uuid(zone_name, uuid);
1322108322fbScarlsonj 	altstr = strdup(zonecfg_get_root());
1323108322fbScarlsonj 	if (altstr == NULL) {
1324108322fbScarlsonj 		zerror(zlogp, B_TRUE, "out of memory");
1325108322fbScarlsonj 		return (B_FALSE);
1326108322fbScarlsonj 	}
1327108322fbScarlsonj 	zonecfg_set_root("");
1328108322fbScarlsonj 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1329108322fbScarlsonj 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1330108322fbScarlsonj 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1331108322fbScarlsonj 	    strcmp(fromdir, rootpath) != 0) {
1332108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1333108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1334108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1335108322fbScarlsonj 			return (B_FALSE);
1336108322fbScarlsonj 		}
1337108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1338108322fbScarlsonj 		    tmp) != 0) {
1339108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1340108322fbScarlsonj 			    fromdir);
1341108322fbScarlsonj 			return (B_FALSE);
1342108322fbScarlsonj 		}
1343108322fbScarlsonj 	}
1344108322fbScarlsonj 	zonecfg_set_root(altstr);
1345108322fbScarlsonj 	free(altstr);
1346108322fbScarlsonj 
1347108322fbScarlsonj 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1348108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1349108322fbScarlsonj 		return (B_FALSE);
1350108322fbScarlsonj 	}
1351108322fbScarlsonj 	(void) ftruncate(fileno(fp), 0);
1352108322fbScarlsonj 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1353108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1354108322fbScarlsonj 	}
1355108322fbScarlsonj 	zonecfg_close_scratch(fp);
1356108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1357108322fbScarlsonj 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1358108322fbScarlsonj 		return (B_FALSE);
1359108322fbScarlsonj 	(void) strlcpy(rootpath, tmp, rootlen);
1360108322fbScarlsonj 	return (B_TRUE);
1361108322fbScarlsonj }
1362108322fbScarlsonj 
13637c478bd9Sstevel@tonic-gate static int
1364108322fbScarlsonj mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd)
13657c478bd9Sstevel@tonic-gate {
13667c478bd9Sstevel@tonic-gate 	char	rootpath[MAXPATHLEN];
13677c478bd9Sstevel@tonic-gate 	char	zonepath[MAXPATHLEN];
13687c478bd9Sstevel@tonic-gate 	int	num_fs = 0, i;
13697c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab, *fs_ptr = NULL, *tmp_ptr;
13707c478bd9Sstevel@tonic-gate 	struct zone_fstab *fsp;
13717c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
13727c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1375108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
13767c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1377108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1378108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1379108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
13807c478bd9Sstevel@tonic-gate 		goto bad;
13817c478bd9Sstevel@tonic-gate 	}
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
13847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
13857c478bd9Sstevel@tonic-gate 		goto bad;
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
13897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
13907c478bd9Sstevel@tonic-gate 		goto bad;
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1394ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
13957c478bd9Sstevel@tonic-gate 		goto bad;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
13987c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
13997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
14007c478bd9Sstevel@tonic-gate 		goto bad;
14017c478bd9Sstevel@tonic-gate 	}
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	/*
14047c478bd9Sstevel@tonic-gate 	 * /dev in the zone is loopback'd from the external /dev repository,
14057c478bd9Sstevel@tonic-gate 	 * in order to provide a largely read-only semantic.  But because
14067c478bd9Sstevel@tonic-gate 	 * processes in the zone need to be able to chown, chmod, etc. zone
14077c478bd9Sstevel@tonic-gate 	 * /dev files, we can't use a 'ro' lofs mount.  Instead we use a
14087c478bd9Sstevel@tonic-gate 	 * special mode just for zones, "zonedevfs".
14097c478bd9Sstevel@tonic-gate 	 *
14107c478bd9Sstevel@tonic-gate 	 * In the future we should front /dev with a full-fledged filesystem.
14117c478bd9Sstevel@tonic-gate 	 */
14127c478bd9Sstevel@tonic-gate 	num_fs++;
14137c478bd9Sstevel@tonic-gate 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
14147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
14157c478bd9Sstevel@tonic-gate 		num_fs--;
14167c478bd9Sstevel@tonic-gate 		goto bad;
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 	fs_ptr = tmp_ptr;
14197c478bd9Sstevel@tonic-gate 	fsp = &fs_ptr[num_fs - 1];
1420108322fbScarlsonj 	/*
1421108322fbScarlsonj 	 * Note that mount_one will prepend the alternate root to
1422108322fbScarlsonj 	 * zone_fs_special and do the necessary resolution, so all that is
1423108322fbScarlsonj 	 * needed here is to strip the root added by zone_get_zonepath.
1424108322fbScarlsonj 	 */
14257c478bd9Sstevel@tonic-gate 	(void) strlcpy(fsp->zone_fs_dir, "/dev", sizeof (fsp->zone_fs_dir));
14267c478bd9Sstevel@tonic-gate 	(void) snprintf(fsp->zone_fs_special, sizeof (fsp->zone_fs_special),
1427108322fbScarlsonj 	    "%s/dev", zonepath + strlen(zonecfg_get_root()));
14287c478bd9Sstevel@tonic-gate 	fsp->zone_fs_raw[0] = '\0';
14297c478bd9Sstevel@tonic-gate 	(void) strlcpy(fsp->zone_fs_type, MNTTYPE_LOFS,
14307c478bd9Sstevel@tonic-gate 	    sizeof (fsp->zone_fs_type));
14317c478bd9Sstevel@tonic-gate 	fsp->zone_fs_options = NULL;
14327c478bd9Sstevel@tonic-gate 	if (zonecfg_add_fs_option(fsp, MNTOPT_LOFS_ZONEDEVFS) != Z_OK) {
14337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "error adding property");
14347c478bd9Sstevel@tonic-gate 		goto bad;
14357c478bd9Sstevel@tonic-gate 	}
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	/*
14387c478bd9Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
14397c478bd9Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
14407c478bd9Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
14417c478bd9Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
14427c478bd9Sstevel@tonic-gate 	 */
14437c478bd9Sstevel@tonic-gate 	if (zonecfg_setipdent(handle) != Z_OK) {
14447c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
14457c478bd9Sstevel@tonic-gate 		goto bad;
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
14487c478bd9Sstevel@tonic-gate 		num_fs++;
14497c478bd9Sstevel@tonic-gate 		if ((tmp_ptr = realloc(fs_ptr,
14507c478bd9Sstevel@tonic-gate 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
14517c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
14527c478bd9Sstevel@tonic-gate 			num_fs--;
14537c478bd9Sstevel@tonic-gate 			(void) zonecfg_endipdent(handle);
14547c478bd9Sstevel@tonic-gate 			goto bad;
14557c478bd9Sstevel@tonic-gate 		}
14567c478bd9Sstevel@tonic-gate 		fs_ptr = tmp_ptr;
14577c478bd9Sstevel@tonic-gate 		fsp = &fs_ptr[num_fs - 1];
14587c478bd9Sstevel@tonic-gate 		/*
14597c478bd9Sstevel@tonic-gate 		 * IPDs logically only have a mount point; all other properties
14607c478bd9Sstevel@tonic-gate 		 * are implied.
14617c478bd9Sstevel@tonic-gate 		 */
14627c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_dir,
14637c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
14647c478bd9Sstevel@tonic-gate 		fsp->zone_fs_special[0] = '\0';
14657c478bd9Sstevel@tonic-gate 		fsp->zone_fs_raw[0] = '\0';
14667c478bd9Sstevel@tonic-gate 		fsp->zone_fs_type[0] = '\0';
14677c478bd9Sstevel@tonic-gate 		fsp->zone_fs_options = NULL;
14687c478bd9Sstevel@tonic-gate 	}
14697c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
14727c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
14737c478bd9Sstevel@tonic-gate 		goto bad;
14747c478bd9Sstevel@tonic-gate 	}
14757c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
1476fa9e4066Sahrens 		/*
1477fa9e4066Sahrens 		 * ZFS filesystems will not be accessible under an alternate
1478fa9e4066Sahrens 		 * root, since the pool will not be known.  Ignore them in this
1479fa9e4066Sahrens 		 * case.
1480fa9e4066Sahrens 		 */
1481fa9e4066Sahrens 		if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
1482fa9e4066Sahrens 			continue;
1483fa9e4066Sahrens 
14847c478bd9Sstevel@tonic-gate 		num_fs++;
14857c478bd9Sstevel@tonic-gate 		if ((tmp_ptr = realloc(fs_ptr,
14867c478bd9Sstevel@tonic-gate 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
14877c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
14887c478bd9Sstevel@tonic-gate 			num_fs--;
14897c478bd9Sstevel@tonic-gate 			(void) zonecfg_endfsent(handle);
14907c478bd9Sstevel@tonic-gate 			goto bad;
14917c478bd9Sstevel@tonic-gate 		}
14927c478bd9Sstevel@tonic-gate 		fs_ptr = tmp_ptr;
14937c478bd9Sstevel@tonic-gate 		fsp = &fs_ptr[num_fs - 1];
14947c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_dir,
14957c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
14967c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special,
14977c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_special));
14987c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
14997c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_raw));
15007c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
15017c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_type));
15027c478bd9Sstevel@tonic-gate 		fsp->zone_fs_options = fstab.zone_fs_options;
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
15057c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
15067c478bd9Sstevel@tonic-gate 	handle = NULL;
15077c478bd9Sstevel@tonic-gate 
1508108322fbScarlsonj 	/*
1509108322fbScarlsonj 	 * If we're mounting a zone for administration, then we need to set up
1510108322fbScarlsonj 	 * the "/a" environment inside the zone so that the commands that run
1511108322fbScarlsonj 	 * in there have access to both the running system's utilities and the
1512108322fbScarlsonj 	 * to-be-modified zone's files.
1513108322fbScarlsonj 	 */
1514108322fbScarlsonj 	if (mount_cmd &&
1515108322fbScarlsonj 	    !build_mounted(zlogp, rootpath, sizeof (rootpath), zonepath))
1516108322fbScarlsonj 		goto bad;
1517108322fbScarlsonj 
15187c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
15197c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
1520108322fbScarlsonj 		if (mount_cmd && strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
1521108322fbScarlsonj 			size_t slen = strlen(rootpath) - 2;
1522108322fbScarlsonj 
1523108322fbScarlsonj 			/* /dev is special and always goes at the top */
1524108322fbScarlsonj 			rootpath[slen] = '\0';
1525108322fbScarlsonj 			if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
1526108322fbScarlsonj 				goto bad;
1527108322fbScarlsonj 			rootpath[slen] = '/';
1528108322fbScarlsonj 			continue;
1529108322fbScarlsonj 		}
15307c478bd9Sstevel@tonic-gate 		if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
15317c478bd9Sstevel@tonic-gate 			goto bad;
15327c478bd9Sstevel@tonic-gate 	}
153345916cd2Sjpk 
153445916cd2Sjpk 	/*
153545916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
153645916cd2Sjpk 	 */
153748451833Scarlsonj 	if (!mount_cmd && tsol_mounts(zlogp, zone_name, rootpath) != 0)
153845916cd2Sjpk 		goto bad;
153945916cd2Sjpk 
15407c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	/*
15437c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
15447c478bd9Sstevel@tonic-gate 	 */
15457c478bd9Sstevel@tonic-gate 	return (0);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate bad:
15487c478bd9Sstevel@tonic-gate 	if (handle != NULL)
15497c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
15507c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
15517c478bd9Sstevel@tonic-gate 	return (-1);
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
15557c478bd9Sstevel@tonic-gate static int
15567c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
15577c478bd9Sstevel@tonic-gate {
15587c478bd9Sstevel@tonic-gate 	int prefixlen;
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
15617c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
15627c478bd9Sstevel@tonic-gate 		return (1);
15637c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
15647c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
15657c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
15667c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
15677c478bd9Sstevel@tonic-gate 			continue;
15687c478bd9Sstevel@tonic-gate 		}
15697c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
15707c478bd9Sstevel@tonic-gate 		prefixlen--;
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 	return (0);
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate /*
15767c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
15777c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
15787c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
15797c478bd9Sstevel@tonic-gate  *
15807c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
15817c478bd9Sstevel@tonic-gate  */
15827c478bd9Sstevel@tonic-gate static int
15837c478bd9Sstevel@tonic-gate unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
15847c478bd9Sstevel@tonic-gate {
15857c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
15867c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
15877c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
15887c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
15897c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
15907c478bd9Sstevel@tonic-gate 	uint_t bufsize;
15917c478bd9Sstevel@tonic-gate 	char *buf = NULL;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
15947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
15957c478bd9Sstevel@tonic-gate 		ret_code = -1;
15967c478bd9Sstevel@tonic-gate 		goto bad;
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
15997c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
16007c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
16017c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
16027c478bd9Sstevel@tonic-gate 		    "could not determine number of interfaces");
16037c478bd9Sstevel@tonic-gate 		ret_code = -1;
16047c478bd9Sstevel@tonic-gate 		goto bad;
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
16077c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
16087c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
16097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
16107c478bd9Sstevel@tonic-gate 		ret_code = -1;
16117c478bd9Sstevel@tonic-gate 		goto bad;
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
16147c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
16157c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
16167c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
16177c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
16187c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get configured interfaces");
16197c478bd9Sstevel@tonic-gate 		ret_code = -1;
16207c478bd9Sstevel@tonic-gate 		goto bad;
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
16237c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
16247c478bd9Sstevel@tonic-gate 		(void) close(s);
16257c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
16267c478bd9Sstevel@tonic-gate 		    0) {
16277c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
16287c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
16297c478bd9Sstevel@tonic-gate 			ret_code = -1;
16307c478bd9Sstevel@tonic-gate 			continue;
16317c478bd9Sstevel@tonic-gate 		}
16327c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
16337c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
16347c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
16357c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
16367c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
16377c478bd9Sstevel@tonic-gate 			    "%s: could not determine zone interface belongs to",
16387c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
16397c478bd9Sstevel@tonic-gate 			ret_code = -1;
16407c478bd9Sstevel@tonic-gate 			continue;
16417c478bd9Sstevel@tonic-gate 		}
16427c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
16437c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
16447c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
16457c478bd9Sstevel@tonic-gate 				    "%s: could not remove interface",
16467c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
16477c478bd9Sstevel@tonic-gate 				ret_code = -1;
16487c478bd9Sstevel@tonic-gate 				continue;
16497c478bd9Sstevel@tonic-gate 			}
16507c478bd9Sstevel@tonic-gate 		}
16517c478bd9Sstevel@tonic-gate 	}
16527c478bd9Sstevel@tonic-gate bad:
16537c478bd9Sstevel@tonic-gate 	if (s > 0)
16547c478bd9Sstevel@tonic-gate 		(void) close(s);
16557c478bd9Sstevel@tonic-gate 	if (buf)
16567c478bd9Sstevel@tonic-gate 		free(buf);
16577c478bd9Sstevel@tonic-gate 	return (ret_code);
16587c478bd9Sstevel@tonic-gate }
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate static union	sockunion {
16617c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
16627c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
16637c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
16647c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
16657c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate static struct {
16687c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
16697c478bd9Sstevel@tonic-gate 	char	space[512];
16707c478bd9Sstevel@tonic-gate } rtmsg;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate static int
16737c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
16747c478bd9Sstevel@tonic-gate {
16757c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
16767c478bd9Sstevel@tonic-gate 	case AF_INET:
16777c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
16787c478bd9Sstevel@tonic-gate 	case AF_LINK:
16797c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
16807c478bd9Sstevel@tonic-gate 	case AF_INET6:
16817c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
16827c478bd9Sstevel@tonic-gate 	default:
16837c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate }
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
16887c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate /*
16917c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
16927c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
16937c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
16947c478bd9Sstevel@tonic-gate  *
16957c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
16967c478bd9Sstevel@tonic-gate  *
16977c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
16987c478bd9Sstevel@tonic-gate  *
16997c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
17007c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
17017c478bd9Sstevel@tonic-gate  */
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate static char *
17047c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
17057c478bd9Sstevel@tonic-gate {
17067c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
17077c478bd9Sstevel@tonic-gate 	pid_t pid;
17087c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
17097c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
17107c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
17117c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
17127c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	pid = getpid();
17177c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
17187c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
17197c478bd9Sstevel@tonic-gate 		return (NULL);
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
17237c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
17267c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
17277c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
17287c478bd9Sstevel@tonic-gate 	} else {
17297c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
17327c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
17337c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
17347c478bd9Sstevel@tonic-gate 	}
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
17397c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
17407c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
17417c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
17427c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
17437c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
17467c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
17477c478bd9Sstevel@tonic-gate 	cp += l;
17487c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
17497c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
17507c478bd9Sstevel@tonic-gate 	cp += l;
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
17557c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
17567c478bd9Sstevel@tonic-gate 		return (NULL);
17577c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
17587c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
17597c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
17607c478bd9Sstevel@tonic-gate 		return (NULL);
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 	do {
17637c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
17647c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
17657c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
17667c478bd9Sstevel@tonic-gate 	if (l < 0) {
17677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
17687c478bd9Sstevel@tonic-gate 		return (NULL);
17697c478bd9Sstevel@tonic-gate 	}
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
17727c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
17737c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
17747c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
17757c478bd9Sstevel@tonic-gate 		return (NULL);
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
17787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
17797c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
17807c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
17817c478bd9Sstevel@tonic-gate 		return (NULL);
17827c478bd9Sstevel@tonic-gate 	}
17837c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
17847c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
17857c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
17867c478bd9Sstevel@tonic-gate 		return (NULL);
17877c478bd9Sstevel@tonic-gate 	}
17887c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
17897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "interface not found");
17907c478bd9Sstevel@tonic-gate 		return (NULL);
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
17937c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
17947c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
17957c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
17967c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
17977c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
17987c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
17997c478bd9Sstevel@tonic-gate 			continue;
18007c478bd9Sstevel@tonic-gate 		}
18017c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
18027c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
18037c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
18047c478bd9Sstevel@tonic-gate 		break;
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
18077c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "interface could not be determined");
18087c478bd9Sstevel@tonic-gate 		return (NULL);
18097c478bd9Sstevel@tonic-gate 	}
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	/*
18127c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
18137c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
18147c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
18157c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
18167c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
18177c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
18187c478bd9Sstevel@tonic-gate 	 */
18197c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
18207c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
18217c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
18227c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
18237c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
18247c478bd9Sstevel@tonic-gate 	if (i < 0) {
18257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
18267c478bd9Sstevel@tonic-gate 		    "%s: could not determine the zone interface belongs to",
18277c478bd9Sstevel@tonic-gate 		    lifr->lifr_name);
18287c478bd9Sstevel@tonic-gate 		return (NULL);
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
18317c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
18327c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
18357c478bd9Sstevel@tonic-gate 		return (answer);
18367c478bd9Sstevel@tonic-gate 	return (NULL);
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate typedef struct mcast_rtmsg_s {
18407c478bd9Sstevel@tonic-gate 	struct rt_msghdr	m_rtm;
18417c478bd9Sstevel@tonic-gate 	union {
18427c478bd9Sstevel@tonic-gate 		struct {
18437c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_dst;
18447c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_gw;
18457c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_netmask;
18467c478bd9Sstevel@tonic-gate 		} m_v4;
18477c478bd9Sstevel@tonic-gate 		struct {
18487c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_dst;
18497c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_gw;
18507c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_netmask;
18517c478bd9Sstevel@tonic-gate 		} m_v6;
18527c478bd9Sstevel@tonic-gate 	} m_u;
18537c478bd9Sstevel@tonic-gate } mcast_rtmsg_t;
18547c478bd9Sstevel@tonic-gate #define	m_dst4		m_u.m_v4.m_dst
18557c478bd9Sstevel@tonic-gate #define	m_dst6		m_u.m_v6.m_dst
18567c478bd9Sstevel@tonic-gate #define	m_gw4		m_u.m_v4.m_gw
18577c478bd9Sstevel@tonic-gate #define	m_gw6		m_u.m_v6.m_gw
18587c478bd9Sstevel@tonic-gate #define	m_netmask4	m_u.m_v4.m_netmask
18597c478bd9Sstevel@tonic-gate #define	m_netmask6	m_u.m_v6.m_netmask
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate /*
18627c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
18637c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
18647c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
18657c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
18667c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
18677c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
18687c478bd9Sstevel@tonic-gate  *
18697c478bd9Sstevel@tonic-gate  * A default interface route for multicast is created on the first IPv4 and
18707c478bd9Sstevel@tonic-gate  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
18717c478bd9Sstevel@tonic-gate  * This should really be done in the init scripts if we ever allow zones to
18727c478bd9Sstevel@tonic-gate  * modify the routing tables.
18737c478bd9Sstevel@tonic-gate  *
18747c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
18757c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
18767c478bd9Sstevel@tonic-gate  */
18777c478bd9Sstevel@tonic-gate static int
18787c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
18797c478bd9Sstevel@tonic-gate     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
18807c478bd9Sstevel@tonic-gate     boolean_t *mcast_rt_v6_setp)
18817c478bd9Sstevel@tonic-gate {
18827c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
18837c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
18847c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
18857c478bd9Sstevel@tonic-gate 	struct in_addr in4;
18867c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
18877c478bd9Sstevel@tonic-gate 	sa_family_t af;
18887c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
18897c478bd9Sstevel@tonic-gate 	mcast_rtmsg_t mcast_rtmsg;
18907c478bd9Sstevel@tonic-gate 	int s;
18917c478bd9Sstevel@tonic-gate 	int rs;
18927c478bd9Sstevel@tonic-gate 	int rlen;
18937c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
18947c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
18957c478bd9Sstevel@tonic-gate 	int res;
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
18987c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
18997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
19007c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
19017c478bd9Sstevel@tonic-gate 		return (-1);
19027c478bd9Sstevel@tonic-gate 	}
19037c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
19047c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
19057c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
19067c478bd9Sstevel@tonic-gate 	else
19077c478bd9Sstevel@tonic-gate 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
19107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
19117c478bd9Sstevel@tonic-gate 		return (-1);
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
19157c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
19167c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
19177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not add interface",
19187c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
19197c478bd9Sstevel@tonic-gate 		(void) close(s);
19207c478bd9Sstevel@tonic-gate 		return (-1);
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
19247c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
19257c478bd9Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
19267c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
19277c478bd9Sstevel@tonic-gate 		goto bad;
19287c478bd9Sstevel@tonic-gate 	}
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
19317c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
19327c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
19357c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
19367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not place interface into zone",
19377c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
19387c478bd9Sstevel@tonic-gate 		goto bad;
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
19427c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
19437c478bd9Sstevel@tonic-gate 	} else {
19447c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
19457c478bd9Sstevel@tonic-gate 			/*
19467c478bd9Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
19477c478bd9Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
19487c478bd9Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
19497c478bd9Sstevel@tonic-gate 			 * being able to determine it is a common failure,
19507c478bd9Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
19517c478bd9Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
19527c478bd9Sstevel@tonic-gate 			 * printed after the rest of the interface's
19537c478bd9Sstevel@tonic-gate 			 * parameters have been configured.
19547c478bd9Sstevel@tonic-gate 			 */
19557c478bd9Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
19567c478bd9Sstevel@tonic-gate 			if (slashp != NULL) {
19577c478bd9Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
19587c478bd9Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
19597c478bd9Sstevel@tonic-gate 					*slashp = '/';
19607c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
19617c478bd9Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
19627c478bd9Sstevel@tonic-gate 					    lifr.lifr_name,
19637c478bd9Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
19647c478bd9Sstevel@tonic-gate 					goto bad;
19657c478bd9Sstevel@tonic-gate 				}
19667c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
19677c478bd9Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
19687c478bd9Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
19697c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
19707c478bd9Sstevel@tonic-gate 			}
19717c478bd9Sstevel@tonic-gate 			if (got_netmask) {
19727c478bd9Sstevel@tonic-gate 				netmask4.sin_family = af;
19737c478bd9Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
19747c478bd9Sstevel@tonic-gate 				    sizeof (netmask4));
19757c478bd9Sstevel@tonic-gate 			}
19767c478bd9Sstevel@tonic-gate 		} else {
19777c478bd9Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
19787c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
19797c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
19807c478bd9Sstevel@tonic-gate 				*slashp = '/';
19817c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
19827c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
19837c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
19847c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
19857c478bd9Sstevel@tonic-gate 				goto bad;
19867c478bd9Sstevel@tonic-gate 			}
19877c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
19887c478bd9Sstevel@tonic-gate 			netmask6.sin6_family = af;
19897c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
19907c478bd9Sstevel@tonic-gate 			    sizeof (netmask6));
19917c478bd9Sstevel@tonic-gate 		}
19927c478bd9Sstevel@tonic-gate 		if (got_netmask &&
19937c478bd9Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
19947c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
19957c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
19967c478bd9Sstevel@tonic-gate 			goto bad;
19977c478bd9Sstevel@tonic-gate 		}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 		/*
20007c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
20017c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
20027c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
20037c478bd9Sstevel@tonic-gate 		 */
20047c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
20057c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
20067c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
20077c478bd9Sstevel@tonic-gate 			goto bad;
20087c478bd9Sstevel@tonic-gate 		}
20097c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
20107c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
20117c478bd9Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
20127c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
20137c478bd9Sstevel@tonic-gate 			goto bad;
20147c478bd9Sstevel@tonic-gate 		}
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
20187c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
20197c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
20207c478bd9Sstevel@tonic-gate 		goto bad;
20217c478bd9Sstevel@tonic-gate 	}
20227c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
20237c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
20247c478bd9Sstevel@tonic-gate 		int save_errno = errno;
20257c478bd9Sstevel@tonic-gate 		char *zone_using;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 		/*
20287c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
20297c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
20307c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
20317c478bd9Sstevel@tonic-gate 		 * using that address.
20327c478bd9Sstevel@tonic-gate 		 */
20337c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
20347c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
20357c478bd9Sstevel@tonic-gate 			    "%s: could not bring interface up", lifr.lifr_name);
20367c478bd9Sstevel@tonic-gate 			goto bad;
20377c478bd9Sstevel@tonic-gate 		}
20387c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
20397c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
20407c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
20417c478bd9Sstevel@tonic-gate 			goto bad;
20427c478bd9Sstevel@tonic-gate 		}
20437c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
20447c478bd9Sstevel@tonic-gate 		errno = save_errno;
20457c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
20467c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
20477c478bd9Sstevel@tonic-gate 			    "%s: could not bring interface up", lifr.lifr_name);
20487c478bd9Sstevel@tonic-gate 		else
20497c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not bring interface "
20507c478bd9Sstevel@tonic-gate 			    "up: address in use by zone '%s'", lifr.lifr_name,
20517c478bd9Sstevel@tonic-gate 			    zone_using);
20527c478bd9Sstevel@tonic-gate 		goto bad;
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
20557c478bd9Sstevel@tonic-gate 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
20567c478bd9Sstevel@tonic-gate 	    (af == AF_INET6 &&
20577c478bd9Sstevel@tonic-gate 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
20587c478bd9Sstevel@tonic-gate 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
20597c478bd9Sstevel@tonic-gate 		if (rs < 0) {
20607c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not create "
20617c478bd9Sstevel@tonic-gate 			    "routing socket", lifr.lifr_name);
20627c478bd9Sstevel@tonic-gate 			goto bad;
20637c478bd9Sstevel@tonic-gate 		}
20647c478bd9Sstevel@tonic-gate 		(void) shutdown(rs, 0);
20657c478bd9Sstevel@tonic-gate 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
20667c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
20677c478bd9Sstevel@tonic-gate 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
20687c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
20697c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
20707c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
20717c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
20727c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_addrs =
20737c478bd9Sstevel@tonic-gate 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
20747c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
20757c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
20767c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
20777c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
20787c478bd9Sstevel@tonic-gate 			    htonl(INADDR_UNSPEC_GROUP);
20797c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
20807c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_addr = in4;
20817c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
20827c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
20837c478bd9Sstevel@tonic-gate 			    htonl(IN_CLASSD_NET);
20847c478bd9Sstevel@tonic-gate 		} else {
20857c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
20867c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
20877c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
20887c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_addr = in6;
20897c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
20907c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
20917c478bd9Sstevel@tonic-gate 		}
20927c478bd9Sstevel@tonic-gate 		rlen = write(rs, (char *)&mcast_rtmsg,
20937c478bd9Sstevel@tonic-gate 		    mcast_rtmsg.m_rtm.rtm_msglen);
20947c478bd9Sstevel@tonic-gate 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
20957c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
20967c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s: could not set "
20977c478bd9Sstevel@tonic-gate 				    "default interface for multicast",
20987c478bd9Sstevel@tonic-gate 				    lifr.lifr_name);
20997c478bd9Sstevel@tonic-gate 			} else {
21007c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s: write to routing "
21017c478bd9Sstevel@tonic-gate 				    "socket returned %d", lifr.lifr_name, rlen);
21027c478bd9Sstevel@tonic-gate 			}
21037c478bd9Sstevel@tonic-gate 			(void) close(rs);
21047c478bd9Sstevel@tonic-gate 			goto bad;
21057c478bd9Sstevel@tonic-gate 		}
21067c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
21077c478bd9Sstevel@tonic-gate 			*mcast_rt_v4_setp = B_TRUE;
21087c478bd9Sstevel@tonic-gate 		} else {
21097c478bd9Sstevel@tonic-gate 			*mcast_rt_v6_setp = B_TRUE;
21107c478bd9Sstevel@tonic-gate 		}
21117c478bd9Sstevel@tonic-gate 		(void) close(rs);
21127c478bd9Sstevel@tonic-gate 	}
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 	if (!got_netmask) {
21157c478bd9Sstevel@tonic-gate 		/*
21167c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
21177c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
21187c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
21197c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
21207c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
21217c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
21227c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
21237c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
21247c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
21257c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
21267c478bd9Sstevel@tonic-gate 		 */
21277c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
21287c478bd9Sstevel@tonic-gate 		void  *addr;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
21317c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
21327c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
21337c478bd9Sstevel@tonic-gate 		else
21347c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
21357c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
21387c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
21397c478bd9Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
21407c478bd9Sstevel@tonic-gate 			goto bad;
21417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
21427c478bd9Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
21437c478bd9Sstevel@tonic-gate 		    "%s; using default of %s.",
21447c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
21457c478bd9Sstevel@tonic-gate 	}
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 	(void) close(s);
21487c478bd9Sstevel@tonic-gate 	return (Z_OK);
21497c478bd9Sstevel@tonic-gate bad:
21507c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
21517c478bd9Sstevel@tonic-gate 	(void) close(s);
21527c478bd9Sstevel@tonic-gate 	return (-1);
21537c478bd9Sstevel@tonic-gate }
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate /*
21567c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
21577c478bd9Sstevel@tonic-gate  * An IPv4 loopback interface is set up "for free", modeling the global system.
21587c478bd9Sstevel@tonic-gate  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
21597c478bd9Sstevel@tonic-gate  * address is set up as well.
21607c478bd9Sstevel@tonic-gate  *
21617c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
21627c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
21637c478bd9Sstevel@tonic-gate  */
21647c478bd9Sstevel@tonic-gate static int
21657c478bd9Sstevel@tonic-gate configure_network_interfaces(zlog_t *zlogp)
21667c478bd9Sstevel@tonic-gate {
21677c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
21687c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
21697c478bd9Sstevel@tonic-gate 	boolean_t saw_v6 = B_FALSE;
21707c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v4_set = B_FALSE;
21717c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v6_set = B_FALSE;
21727c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
21757c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
21767c478bd9Sstevel@tonic-gate 		return (-1);
21777c478bd9Sstevel@tonic-gate 	}
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
21807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
21817c478bd9Sstevel@tonic-gate 		return (-1);
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
21847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
21857c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
21867c478bd9Sstevel@tonic-gate 		return (-1);
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
21897c478bd9Sstevel@tonic-gate 		for (;;) {
21907c478bd9Sstevel@tonic-gate 			struct in6_addr in6;
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
21937c478bd9Sstevel@tonic-gate 				break;
21947c478bd9Sstevel@tonic-gate 			if (configure_one_interface(zlogp, zoneid,
21957c478bd9Sstevel@tonic-gate 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
21967c478bd9Sstevel@tonic-gate 			    Z_OK) {
21977c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
21987c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
21997c478bd9Sstevel@tonic-gate 				return (-1);
22007c478bd9Sstevel@tonic-gate 			}
22017c478bd9Sstevel@tonic-gate 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
22027c478bd9Sstevel@tonic-gate 			    &in6) == 1)
22037c478bd9Sstevel@tonic-gate 				saw_v6 = B_TRUE;
22047c478bd9Sstevel@tonic-gate 		}
22057c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
22087c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
22097c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
22107c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
22117c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
22127c478bd9Sstevel@tonic-gate 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
22137c478bd9Sstevel@tonic-gate 	    != Z_OK) {
22147c478bd9Sstevel@tonic-gate 		return (-1);
22157c478bd9Sstevel@tonic-gate 	}
22167c478bd9Sstevel@tonic-gate 	if (saw_v6) {
22177c478bd9Sstevel@tonic-gate 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
22187c478bd9Sstevel@tonic-gate 		    sizeof (loopback_iftab.zone_nwif_address));
22197c478bd9Sstevel@tonic-gate 		if (configure_one_interface(zlogp, zoneid,
22207c478bd9Sstevel@tonic-gate 		    &loopback_iftab, NULL, NULL) != Z_OK) {
22217c478bd9Sstevel@tonic-gate 			return (-1);
22227c478bd9Sstevel@tonic-gate 		}
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate 	return (0);
22257c478bd9Sstevel@tonic-gate }
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate static int
22287c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
22297c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
22307c478bd9Sstevel@tonic-gate {
22317c478bd9Sstevel@tonic-gate 	int fd;
22327c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
22337c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
22347c478bd9Sstevel@tonic-gate 	int error;
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
22377c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
22387c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
22397c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
22407c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
22437c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
22447c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
22457c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
22487c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
22497c478bd9Sstevel@tonic-gate 		return (-1);
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
22537c478bd9Sstevel@tonic-gate 	(void) close(fd);
22547c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
22557c478bd9Sstevel@tonic-gate 		return (0);
22567c478bd9Sstevel@tonic-gate 	return (-1);
22577c478bd9Sstevel@tonic-gate }
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate static int
22607c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
22617c478bd9Sstevel@tonic-gate {
22627c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
22637c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
22647c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
22657c478bd9Sstevel@tonic-gate 	int error;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	/*
22687c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
22697c478bd9Sstevel@tonic-gate 	 */
22707c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
22717c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
22727c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
22737c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
22747c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
22777c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
22787c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
22797c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
22807c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
22837c478bd9Sstevel@tonic-gate 		return (error);
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	/*
22867c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
22877c478bd9Sstevel@tonic-gate 	 */
22887c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
22897c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
22907c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
22917c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
22927c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
22957c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
22967c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
22977c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
22987c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
23017c478bd9Sstevel@tonic-gate 		return (error);
23027c478bd9Sstevel@tonic-gate 	return (0);
23037c478bd9Sstevel@tonic-gate }
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate static int
23067c478bd9Sstevel@tonic-gate devfsadm_call(zlog_t *zlogp, const char *arg)
23077c478bd9Sstevel@tonic-gate {
23087c478bd9Sstevel@tonic-gate 	char *argv[4];
23097c478bd9Sstevel@tonic-gate 	int status;
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	argv[0] = DEVFSADM;
23127c478bd9Sstevel@tonic-gate 	argv[1] = (char *)arg;
23137c478bd9Sstevel@tonic-gate 	argv[2] = zone_name;
23147c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
23157c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, DEVFSADM_PATH, argv);
23167c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
23177c478bd9Sstevel@tonic-gate 		return (status);
23187c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "%s call (%s %s %s) unexpectedly returned %d",
23197c478bd9Sstevel@tonic-gate 	    DEVFSADM, DEVFSADM_PATH, arg, zone_name, status);
23207c478bd9Sstevel@tonic-gate 	return (-1);
23217c478bd9Sstevel@tonic-gate }
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate static int
23247c478bd9Sstevel@tonic-gate devfsadm_register(zlog_t *zlogp)
23257c478bd9Sstevel@tonic-gate {
23267c478bd9Sstevel@tonic-gate 	/*
23277c478bd9Sstevel@tonic-gate 	 * Ready the zone's devices.
23287c478bd9Sstevel@tonic-gate 	 */
23297c478bd9Sstevel@tonic-gate 	return (devfsadm_call(zlogp, "-z"));
23307c478bd9Sstevel@tonic-gate }
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate static int
23337c478bd9Sstevel@tonic-gate devfsadm_unregister(zlog_t *zlogp)
23347c478bd9Sstevel@tonic-gate {
23357c478bd9Sstevel@tonic-gate 	return (devfsadm_call(zlogp, "-Z"));
23367c478bd9Sstevel@tonic-gate }
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate static int
2339ffbafc53Scomay get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd)
2340ffbafc53Scomay {
2341ffbafc53Scomay 	int error = -1;
2342ffbafc53Scomay 	zone_dochandle_t handle;
2343ffbafc53Scomay 	char *privname = NULL;
2344ffbafc53Scomay 
2345ffbafc53Scomay 	if (mount_cmd) {
2346ffbafc53Scomay 		if (zonecfg_default_privset(privs) == Z_OK)
2347ffbafc53Scomay 			return (0);
2348ffbafc53Scomay 		zerror(zlogp, B_FALSE,
2349ffbafc53Scomay 		    "failed to determine the zone's default privilege set");
2350ffbafc53Scomay 		return (-1);
2351ffbafc53Scomay 	}
2352ffbafc53Scomay 
2353ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
2354ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2355ffbafc53Scomay 		return (-1);
2356ffbafc53Scomay 	}
2357ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2358ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
2359ffbafc53Scomay 		zonecfg_fini_handle(handle);
2360ffbafc53Scomay 		return (-1);
2361ffbafc53Scomay 	}
2362ffbafc53Scomay 
2363ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
2364ffbafc53Scomay 	case Z_OK:
2365ffbafc53Scomay 		error = 0;
2366ffbafc53Scomay 		break;
2367ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2368ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
2369ffbafc53Scomay 		    "within the zone's privilege set", privname);
2370ffbafc53Scomay 		break;
2371ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2372ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
2373ffbafc53Scomay 		    "from the zone's privilege set", privname);
2374ffbafc53Scomay 		break;
2375ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2376ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
2377ffbafc53Scomay 		    "in the zone's privilege set", privname);
2378ffbafc53Scomay 		break;
2379ffbafc53Scomay 	default:
2380ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
2381ffbafc53Scomay 		    "privilege set");
2382ffbafc53Scomay 		break;
2383ffbafc53Scomay 	}
2384ffbafc53Scomay 
2385ffbafc53Scomay 	free(privname);
2386ffbafc53Scomay 	zonecfg_fini_handle(handle);
2387ffbafc53Scomay 	return (error);
2388ffbafc53Scomay }
2389ffbafc53Scomay 
2390ffbafc53Scomay static int
23917c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
23927c478bd9Sstevel@tonic-gate {
23937c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
23947c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
23957c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
23967c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
23977c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
23987c478bd9Sstevel@tonic-gate 	int error = -1;
23997c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24007c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
24017c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	*bufp = NULL;
24047c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24077c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24087c478bd9Sstevel@tonic-gate 		return (-1);
24097c478bd9Sstevel@tonic-gate 	}
24107c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24117c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24127c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24137c478bd9Sstevel@tonic-gate 		return (-1);
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
24177c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
24187c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
24197c478bd9Sstevel@tonic-gate 		goto out;
24207c478bd9Sstevel@tonic-gate 	}
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
24237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
24247c478bd9Sstevel@tonic-gate 		goto out;
24257c478bd9Sstevel@tonic-gate 	}
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
24287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
24297c478bd9Sstevel@tonic-gate 		goto out;
24307c478bd9Sstevel@tonic-gate 	}
24317c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
24327c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
24337c478bd9Sstevel@tonic-gate 		uint_t i, count;
24347c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
24377c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
24387c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
24397c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
24407c478bd9Sstevel@tonic-gate 			continue;
24417c478bd9Sstevel@tonic-gate 		}
24427c478bd9Sstevel@tonic-gate 		count = 0;
24437c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
24447c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
24457c478bd9Sstevel@tonic-gate 			count++;
24467c478bd9Sstevel@tonic-gate 		}
24477c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
24487c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
24497c478bd9Sstevel@tonic-gate 		}
24507c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
24517c478bd9Sstevel@tonic-gate 			goto out;
24527c478bd9Sstevel@tonic-gate 		i = 0;
24537c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
24547c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
24557c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
24567c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
24577c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
24587c478bd9Sstevel@tonic-gate 				goto out;
24597c478bd9Sstevel@tonic-gate 			}
24607c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
24617c478bd9Sstevel@tonic-gate 			    != Z_OK) {
24627c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
24637c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
24647c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
24657c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
24667c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
24677c478bd9Sstevel@tonic-gate 				goto out;
24687c478bd9Sstevel@tonic-gate 			}
24697c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
24707c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
24717c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
24727c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
24737c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
24747c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
24757c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
24767c478bd9Sstevel@tonic-gate 				    name);
24777c478bd9Sstevel@tonic-gate 				goto out;
24787c478bd9Sstevel@tonic-gate 			}
24797c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
24807c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
24817c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
24827c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
24837c478bd9Sstevel@tonic-gate 				goto out;
24847c478bd9Sstevel@tonic-gate 			}
24857c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
24867c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
24877c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
24887c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
24897c478bd9Sstevel@tonic-gate 				goto out;
24907c478bd9Sstevel@tonic-gate 			}
24917c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
24927c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
24937c478bd9Sstevel@tonic-gate 			    != 0) {
24947c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
24957c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
24967c478bd9Sstevel@tonic-gate 				goto out;
24977c478bd9Sstevel@tonic-gate 			}
24987c478bd9Sstevel@tonic-gate 		}
24997c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
25007c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
25017c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
25027c478bd9Sstevel@tonic-gate 		    != 0) {
25037c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
25047c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
25057c478bd9Sstevel@tonic-gate 			goto out;
25067c478bd9Sstevel@tonic-gate 		}
25077c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
25087c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
25097c478bd9Sstevel@tonic-gate 		free(nvlv);
25107c478bd9Sstevel@tonic-gate 		nvlv = NULL;
25117c478bd9Sstevel@tonic-gate 		rctlcount++;
25127c478bd9Sstevel@tonic-gate 	}
25137c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
25167c478bd9Sstevel@tonic-gate 		error = 0;
25177c478bd9Sstevel@tonic-gate 		goto out;
25187c478bd9Sstevel@tonic-gate 	}
25197c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
25207c478bd9Sstevel@tonic-gate 	    != 0) {
25217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
25227c478bd9Sstevel@tonic-gate 		goto out;
25237c478bd9Sstevel@tonic-gate 	}
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	error = 0;
25267c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
25277c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate out:
25307c478bd9Sstevel@tonic-gate 	free(rctlblk);
25317c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
25327c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
25337c478bd9Sstevel@tonic-gate 		free(nvl_packed);
25347c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
25357c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
25367c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
25377c478bd9Sstevel@tonic-gate 		free(nvlv);
25387c478bd9Sstevel@tonic-gate 	if (handle != NULL)
25397c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
25407c478bd9Sstevel@tonic-gate 	return (error);
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate static int
25447c478bd9Sstevel@tonic-gate get_zone_pool(zlog_t *zlogp, char *poolbuf, size_t bufsz)
25457c478bd9Sstevel@tonic-gate {
25467c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
25477c478bd9Sstevel@tonic-gate 	int error;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
25507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2551ffbafc53Scomay 		return (Z_NOMEM);
25527c478bd9Sstevel@tonic-gate 	}
2553ffbafc53Scomay 	error = zonecfg_get_snapshot_handle(zone_name, handle);
2554ffbafc53Scomay 	if (error != Z_OK) {
25557c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
25567c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
2557ffbafc53Scomay 		return (error);
25587c478bd9Sstevel@tonic-gate 	}
25597c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolbuf, bufsz);
25607c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
25617c478bd9Sstevel@tonic-gate 	return (error);
25627c478bd9Sstevel@tonic-gate }
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate static int
2565fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
2566fa9e4066Sahrens {
2567fa9e4066Sahrens 	zone_dochandle_t handle;
2568fa9e4066Sahrens 	struct zone_dstab dstab;
2569fa9e4066Sahrens 	size_t total, offset, len;
2570fa9e4066Sahrens 	int error = -1;
2571fa9e4066Sahrens 	char *str;
2572fa9e4066Sahrens 
2573fa9e4066Sahrens 	*bufp = NULL;
2574fa9e4066Sahrens 	*bufsizep = 0;
2575fa9e4066Sahrens 
2576fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
2577fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2578fa9e4066Sahrens 		return (-1);
2579fa9e4066Sahrens 	}
2580fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2581fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
2582fa9e4066Sahrens 		zonecfg_fini_handle(handle);
2583fa9e4066Sahrens 		return (-1);
2584fa9e4066Sahrens 	}
2585fa9e4066Sahrens 
2586fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2587fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
2588fa9e4066Sahrens 		goto out;
2589fa9e4066Sahrens 	}
2590fa9e4066Sahrens 
2591fa9e4066Sahrens 	total = 0;
2592fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
2593fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
2594fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
2595fa9e4066Sahrens 
2596fa9e4066Sahrens 	if (total == 0) {
2597fa9e4066Sahrens 		error = 0;
2598fa9e4066Sahrens 		goto out;
2599fa9e4066Sahrens 	}
2600fa9e4066Sahrens 
2601fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
2602fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
2603fa9e4066Sahrens 		goto out;
2604fa9e4066Sahrens 	}
2605fa9e4066Sahrens 
2606fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2607fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
2608fa9e4066Sahrens 		goto out;
2609fa9e4066Sahrens 	}
2610fa9e4066Sahrens 	offset = 0;
2611fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
2612fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
2613fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
2614fa9e4066Sahrens 		    sizeof (dstab.zone_dataset_name) - offset);
2615fa9e4066Sahrens 		offset += len;
2616fa9e4066Sahrens 		if (offset != total - 1)
2617fa9e4066Sahrens 			str[offset++] = ',';
2618fa9e4066Sahrens 	}
2619fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
2620fa9e4066Sahrens 
2621fa9e4066Sahrens 	error = 0;
2622fa9e4066Sahrens 	*bufp = str;
2623fa9e4066Sahrens 	*bufsizep = total;
2624fa9e4066Sahrens 
2625fa9e4066Sahrens out:
2626fa9e4066Sahrens 	if (error != 0 && str != NULL)
2627fa9e4066Sahrens 		free(str);
2628fa9e4066Sahrens 	if (handle != NULL)
2629fa9e4066Sahrens 		zonecfg_fini_handle(handle);
2630fa9e4066Sahrens 
2631fa9e4066Sahrens 	return (error);
2632fa9e4066Sahrens }
2633fa9e4066Sahrens 
2634fa9e4066Sahrens static int
2635fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
2636fa9e4066Sahrens {
2637fa9e4066Sahrens 	zone_dochandle_t handle;
2638fa9e4066Sahrens 	struct zone_dstab dstab;
2639fa9e4066Sahrens 	zfs_handle_t *zhp;
2640*99653d4eSeschrock 	libzfs_handle_t *hdl;
2641fa9e4066Sahrens 
2642fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
2643fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2644fa9e4066Sahrens 		return (-1);
2645fa9e4066Sahrens 	}
2646fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2647fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
2648fa9e4066Sahrens 		zonecfg_fini_handle(handle);
2649fa9e4066Sahrens 		return (-1);
2650fa9e4066Sahrens 	}
2651fa9e4066Sahrens 
2652fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2653fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
2654fa9e4066Sahrens 		zonecfg_fini_handle(handle);
2655fa9e4066Sahrens 		return (-1);
2656fa9e4066Sahrens 	}
2657fa9e4066Sahrens 
2658*99653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
2659*99653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
2660*99653d4eSeschrock 		zonecfg_fini_handle(handle);
2661*99653d4eSeschrock 		return (-1);
2662*99653d4eSeschrock 	}
2663fa9e4066Sahrens 
2664fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
2665fa9e4066Sahrens 
2666*99653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
2667fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
2668fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
2669fa9e4066Sahrens 			    dstab.zone_dataset_name);
2670fa9e4066Sahrens 			zonecfg_fini_handle(handle);
2671*99653d4eSeschrock 			libzfs_fini(hdl);
2672fa9e4066Sahrens 			return (-1);
2673fa9e4066Sahrens 		}
2674fa9e4066Sahrens 
2675fa9e4066Sahrens 		/*
2676fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
2677fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
2678fa9e4066Sahrens 		 */
2679fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
2680fa9e4066Sahrens 		    zfs_prop_set(zhp, ZFS_PROP_ZONED, "on") != 0) {
2681fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
2682fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
2683fa9e4066Sahrens 			    dstab.zone_dataset_name);
2684fa9e4066Sahrens 			zonecfg_fini_handle(handle);
2685fa9e4066Sahrens 			zfs_close(zhp);
2686*99653d4eSeschrock 			libzfs_fini(hdl);
2687fa9e4066Sahrens 			return (-1);
2688fa9e4066Sahrens 		}
2689fa9e4066Sahrens 
2690fa9e4066Sahrens 		zfs_close(zhp);
2691fa9e4066Sahrens 	}
2692fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
2693fa9e4066Sahrens 
2694fa9e4066Sahrens 	zonecfg_fini_handle(handle);
2695*99653d4eSeschrock 	libzfs_fini(hdl);
2696fa9e4066Sahrens 
2697fa9e4066Sahrens 	return (0);
2698fa9e4066Sahrens }
2699fa9e4066Sahrens 
2700fa9e4066Sahrens static int
27017c478bd9Sstevel@tonic-gate bind_to_pool(zlog_t *zlogp, zoneid_t zoneid)
27027c478bd9Sstevel@tonic-gate {
27037c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
27047c478bd9Sstevel@tonic-gate 	pool_t *pool;
27057c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
27067c478bd9Sstevel@tonic-gate 	int status;
27077c478bd9Sstevel@tonic-gate 	int error;
27087c478bd9Sstevel@tonic-gate 
27097c478bd9Sstevel@tonic-gate 	/*
27107c478bd9Sstevel@tonic-gate 	 * Find the pool mentioned in the zone configuration, and bind to it.
27117c478bd9Sstevel@tonic-gate 	 */
27127c478bd9Sstevel@tonic-gate 	error = get_zone_pool(zlogp, poolname, sizeof (poolname));
27137c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
27147c478bd9Sstevel@tonic-gate 		/*
27157c478bd9Sstevel@tonic-gate 		 * The property is not set on the zone, so the pool
27167c478bd9Sstevel@tonic-gate 		 * should be bound to the default pool.  But that's
27177c478bd9Sstevel@tonic-gate 		 * already done by the kernel, so we can just return.
27187c478bd9Sstevel@tonic-gate 		 */
27197c478bd9Sstevel@tonic-gate 		return (0);
27207c478bd9Sstevel@tonic-gate 	}
27217c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
27227c478bd9Sstevel@tonic-gate 		/*
27237c478bd9Sstevel@tonic-gate 		 * Not an error, even though it shouldn't be happening.
27247c478bd9Sstevel@tonic-gate 		 */
27257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
27267c478bd9Sstevel@tonic-gate 		    "WARNING: unable to retrieve default pool.");
27277c478bd9Sstevel@tonic-gate 		return (0);
27287c478bd9Sstevel@tonic-gate 	}
27297c478bd9Sstevel@tonic-gate 	/*
27307c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
27317c478bd9Sstevel@tonic-gate 	 */
27327c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
27337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: pools facility not active; "
27347c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'.", poolname);
27357c478bd9Sstevel@tonic-gate 		return (0);
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 	/*
27387c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
27397c478bd9Sstevel@tonic-gate 	 * exist.
27407c478bd9Sstevel@tonic-gate 	 */
27417c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
27427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "pool_conf_alloc");
27437c478bd9Sstevel@tonic-gate 		return (-1);
27447c478bd9Sstevel@tonic-gate 	}
27457c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
27467c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
27477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "pool_conf_open");
27487c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
27497c478bd9Sstevel@tonic-gate 		return (-1);
27507c478bd9Sstevel@tonic-gate 	}
27517c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
27527c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
27537c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
27547c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
27557c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: pool '%s' not found; "
27567c478bd9Sstevel@tonic-gate 		    "using default pool.", poolname);
27577c478bd9Sstevel@tonic-gate 		return (0);
27587c478bd9Sstevel@tonic-gate 	}
27597c478bd9Sstevel@tonic-gate 	/*
27607c478bd9Sstevel@tonic-gate 	 * Bind the zone to the pool.
27617c478bd9Sstevel@tonic-gate 	 */
27627c478bd9Sstevel@tonic-gate 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
27637c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: unable to bind to pool '%s'; "
27647c478bd9Sstevel@tonic-gate 		    "using default pool.", poolname);
27657c478bd9Sstevel@tonic-gate 	}
27667c478bd9Sstevel@tonic-gate 	return (0);
27677c478bd9Sstevel@tonic-gate }
27687c478bd9Sstevel@tonic-gate 
276945916cd2Sjpk /*
277045916cd2Sjpk  * Mount lower level home directories into/from current zone
277145916cd2Sjpk  * Share exported directories specified in dfstab for zone
277245916cd2Sjpk  */
277345916cd2Sjpk static int
277445916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
277545916cd2Sjpk {
277645916cd2Sjpk 	zoneid_t *zids = NULL;
277745916cd2Sjpk 	priv_set_t *zid_privs;
277845916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
277945916cd2Sjpk 	uint_t nzents_saved;
278045916cd2Sjpk 	uint_t nzents;
278145916cd2Sjpk 	int i;
278245916cd2Sjpk 	char readonly[] = "ro";
278345916cd2Sjpk 	struct zone_fstab lower_fstab;
278445916cd2Sjpk 	char *argv[4];
278545916cd2Sjpk 
278645916cd2Sjpk 	if (!is_system_labeled())
278745916cd2Sjpk 		return (0);
278845916cd2Sjpk 
278945916cd2Sjpk 	if (zid_label == NULL) {
279045916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
279145916cd2Sjpk 		if (zid_label == NULL)
279245916cd2Sjpk 			return (-1);
279345916cd2Sjpk 	}
279445916cd2Sjpk 
279545916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
279645916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
279745916cd2Sjpk 	    DEFAULT_DIR_MODE);
279845916cd2Sjpk 
279945916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
280045916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
280145916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
280245916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
280345916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
280445916cd2Sjpk 
280545916cd2Sjpk 	/*
280645916cd2Sjpk 	 * Get the list of zones from the kernel
280745916cd2Sjpk 	 */
280845916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
280945916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
281045916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
281145916cd2Sjpk 		return (-1);
281245916cd2Sjpk 	}
281345916cd2Sjpk again:
281445916cd2Sjpk 	if (nzents == 0) {
281545916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
281645916cd2Sjpk 		return (-1);
281745916cd2Sjpk 	}
281845916cd2Sjpk 
281945916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
282045916cd2Sjpk 	if (zids == NULL) {
282145916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to allocate memory");
282245916cd2Sjpk 		return (-1);
282345916cd2Sjpk 	}
282445916cd2Sjpk 	nzents_saved = nzents;
282545916cd2Sjpk 
282645916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
282745916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
282845916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
282945916cd2Sjpk 		free(zids);
283045916cd2Sjpk 		return (-1);
283145916cd2Sjpk 	}
283245916cd2Sjpk 	if (nzents != nzents_saved) {
283345916cd2Sjpk 		/* list changed, try again */
283445916cd2Sjpk 		free(zids);
283545916cd2Sjpk 		goto again;
283645916cd2Sjpk 	}
283745916cd2Sjpk 
283845916cd2Sjpk 	ip = getprivimplinfo();
283945916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
284045916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
284145916cd2Sjpk 		zonecfg_free_fs_option_list(
284245916cd2Sjpk 		    lower_fstab.zone_fs_options);
284345916cd2Sjpk 		free(zids);
284445916cd2Sjpk 		return (-1);
284545916cd2Sjpk 	}
284645916cd2Sjpk 
284745916cd2Sjpk 	for (i = 0; i < nzents; i++) {
284845916cd2Sjpk 		char zid_name[ZONENAME_MAX];
284945916cd2Sjpk 		zone_state_t zid_state;
285045916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
285145916cd2Sjpk 		struct stat stat_buf;
285245916cd2Sjpk 
285345916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
285445916cd2Sjpk 			continue;
285545916cd2Sjpk 
285645916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
285745916cd2Sjpk 			continue;
285845916cd2Sjpk 
285945916cd2Sjpk 		/*
286045916cd2Sjpk 		 * Do special setup for the zone we are booting
286145916cd2Sjpk 		 */
286245916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
286345916cd2Sjpk 			struct zone_fstab autofs_fstab;
286445916cd2Sjpk 			char map_path[MAXPATHLEN];
286545916cd2Sjpk 			int fd;
286645916cd2Sjpk 
286745916cd2Sjpk 			/*
286845916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
286945916cd2Sjpk 			 * in the global zone. The local zone entry
287045916cd2Sjpk 			 * will be created by automount when the zone
287145916cd2Sjpk 			 * is booted.
287245916cd2Sjpk 			 */
287345916cd2Sjpk 
287445916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
287545916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
287645916cd2Sjpk 
287745916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
287845916cd2Sjpk 			    "/zone/%s/home", zid_name);
287945916cd2Sjpk 
288045916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
288145916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
288245916cd2Sjpk 			/*
288345916cd2Sjpk 			 * If the map file doesn't exist create a template
288445916cd2Sjpk 			 */
288545916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
288645916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
288745916cd2Sjpk 				int len;
288845916cd2Sjpk 				char map_rec[MAXPATHLEN];
288945916cd2Sjpk 
289045916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
289145916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
289245916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
289345916cd2Sjpk 				(void) write(fd, map_rec, len);
289445916cd2Sjpk 				(void) close(fd);
289545916cd2Sjpk 			}
289645916cd2Sjpk 
289745916cd2Sjpk 			/*
289845916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
289945916cd2Sjpk 			 * If it's already of type autofs, then
290045916cd2Sjpk 			 * don't mount it again.
290145916cd2Sjpk 			 */
290245916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
290345916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
290445916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
290545916cd2Sjpk 
290645916cd2Sjpk 				(void) make_one_dir(zlogp, "",
290745916cd2Sjpk 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE);
290845916cd2Sjpk 
290945916cd2Sjpk 				/*
291045916cd2Sjpk 				 * Mount will fail if automounter has already
291145916cd2Sjpk 				 * processed the auto_home_<zonename> map
291245916cd2Sjpk 				 */
291345916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
291445916cd2Sjpk 				    autofs_fstab.zone_fs_special,
291545916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
291645916cd2Sjpk 			}
291745916cd2Sjpk 			continue;
291845916cd2Sjpk 		}
291945916cd2Sjpk 
292045916cd2Sjpk 
292145916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
292248451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
292348451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
292445916cd2Sjpk 			/* Skip over zones without mounted filesystems */
292545916cd2Sjpk 			continue;
292645916cd2Sjpk 
292745916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
292845916cd2Sjpk 		    sizeof (m_label_t)) < 0)
292945916cd2Sjpk 			/* Skip over zones with unspecified label */
293045916cd2Sjpk 			continue;
293145916cd2Sjpk 
293245916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
293345916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
293445916cd2Sjpk 			/* Skip over zones with bad path */
293545916cd2Sjpk 			continue;
293645916cd2Sjpk 
293745916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
293845916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
293945916cd2Sjpk 			/* Skip over zones with bad privs */
294045916cd2Sjpk 			continue;
294145916cd2Sjpk 
294245916cd2Sjpk 		/*
294345916cd2Sjpk 		 * Reading down is valid according to our label model
294445916cd2Sjpk 		 * but some customers want to disable it because it
294545916cd2Sjpk 		 * allows execute down and other possible attacks.
294645916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
294745916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
294845916cd2Sjpk 		 * for NFS read-down semantics.
294945916cd2Sjpk 		 */
295045916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
295145916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
295245916cd2Sjpk 			/*
295345916cd2Sjpk 			 * Our zone dominates this one.
295445916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
295545916cd2Sjpk 			 */
295645916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
295745916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
295845916cd2Sjpk 
295945916cd2Sjpk 			/*
296045916cd2Sjpk 			 * If the target is already an LOFS mount
296145916cd2Sjpk 			 * then don't do it again.
296245916cd2Sjpk 			 */
296345916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
296445916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
296545916cd2Sjpk 
296645916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
296745916cd2Sjpk 				    MAXPATHLEN, "%s/export",
296845916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
296945916cd2Sjpk 					continue;
297045916cd2Sjpk 
297145916cd2Sjpk 				/*
297245916cd2Sjpk 				 * Make sure the lower-level home exists
297345916cd2Sjpk 				 */
297445916cd2Sjpk 				if (make_one_dir(zlogp,
297545916cd2Sjpk 				    lower_fstab.zone_fs_special,
297645916cd2Sjpk 				    "/home", DEFAULT_DIR_MODE) != 0)
297745916cd2Sjpk 					continue;
297845916cd2Sjpk 
297945916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
298045916cd2Sjpk 				    "/home", MAXPATHLEN);
298145916cd2Sjpk 
298245916cd2Sjpk 				/*
298345916cd2Sjpk 				 * Mount can fail because the lower-level
298445916cd2Sjpk 				 * zone may have already done a mount up.
298545916cd2Sjpk 				 */
298645916cd2Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
298745916cd2Sjpk 			}
298845916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
298945916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
299045916cd2Sjpk 			/*
299145916cd2Sjpk 			 * This zone dominates our zone.
299245916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
299345916cd2Sjpk 			 */
299445916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
299545916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
299645916cd2Sjpk 			    zone_name) > MAXPATHLEN)
299745916cd2Sjpk 				continue;
299845916cd2Sjpk 
299945916cd2Sjpk 			/*
300045916cd2Sjpk 			 * If the target is already an LOFS mount
300145916cd2Sjpk 			 * then don't do it again.
300245916cd2Sjpk 			 */
300345916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
300445916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
300545916cd2Sjpk 
300645916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
300745916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
300845916cd2Sjpk 
300945916cd2Sjpk 				/*
301045916cd2Sjpk 				 * Mount can fail because the higher-level
301145916cd2Sjpk 				 * zone may have already done a mount down.
301245916cd2Sjpk 				 */
301345916cd2Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
301445916cd2Sjpk 			}
301545916cd2Sjpk 		}
301645916cd2Sjpk 	}
301745916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
301845916cd2Sjpk 	priv_freeset(zid_privs);
301945916cd2Sjpk 	free(zids);
302045916cd2Sjpk 
302145916cd2Sjpk 	/*
302245916cd2Sjpk 	 * Now share any exported directories from this zone.
302345916cd2Sjpk 	 * Each zone can have its own dfstab.
302445916cd2Sjpk 	 */
302545916cd2Sjpk 
302645916cd2Sjpk 	argv[0] = "zoneshare";
302745916cd2Sjpk 	argv[1] = "-z";
302845916cd2Sjpk 	argv[2] = zone_name;
302945916cd2Sjpk 	argv[3] = NULL;
303045916cd2Sjpk 
303145916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
303245916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
303345916cd2Sjpk 
303445916cd2Sjpk 	return (0);
303545916cd2Sjpk }
303645916cd2Sjpk 
303745916cd2Sjpk /*
303845916cd2Sjpk  * Unmount lofs mounts from higher level zones
303945916cd2Sjpk  * Unshare nfs exported directories
304045916cd2Sjpk  */
304145916cd2Sjpk static void
304245916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
304345916cd2Sjpk {
304445916cd2Sjpk 	zoneid_t *zids = NULL;
304545916cd2Sjpk 	uint_t nzents_saved;
304645916cd2Sjpk 	uint_t nzents;
304745916cd2Sjpk 	int i;
304845916cd2Sjpk 	char *argv[4];
304945916cd2Sjpk 	char path[MAXPATHLEN];
305045916cd2Sjpk 
305145916cd2Sjpk 	if (!is_system_labeled())
305245916cd2Sjpk 		return;
305345916cd2Sjpk 
305445916cd2Sjpk 	/*
305545916cd2Sjpk 	 * Get the list of zones from the kernel
305645916cd2Sjpk 	 */
305745916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
305845916cd2Sjpk 		return;
305945916cd2Sjpk 	}
306045916cd2Sjpk 
306145916cd2Sjpk 	if (zid_label == NULL) {
306245916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
306345916cd2Sjpk 		if (zid_label == NULL)
306445916cd2Sjpk 			return;
306545916cd2Sjpk 	}
306645916cd2Sjpk 
306745916cd2Sjpk again:
306845916cd2Sjpk 	if (nzents == 0)
306945916cd2Sjpk 		return;
307045916cd2Sjpk 
307145916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
307245916cd2Sjpk 	if (zids == NULL) {
307345916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to allocate memory");
307445916cd2Sjpk 		return;
307545916cd2Sjpk 	}
307645916cd2Sjpk 	nzents_saved = nzents;
307745916cd2Sjpk 
307845916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
307945916cd2Sjpk 		free(zids);
308045916cd2Sjpk 		return;
308145916cd2Sjpk 	}
308245916cd2Sjpk 	if (nzents != nzents_saved) {
308345916cd2Sjpk 		/* list changed, try again */
308445916cd2Sjpk 		free(zids);
308545916cd2Sjpk 		goto again;
308645916cd2Sjpk 	}
308745916cd2Sjpk 
308845916cd2Sjpk 	for (i = 0; i < nzents; i++) {
308945916cd2Sjpk 		char zid_name[ZONENAME_MAX];
309045916cd2Sjpk 		zone_state_t zid_state;
309145916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
309245916cd2Sjpk 
309345916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
309445916cd2Sjpk 			continue;
309545916cd2Sjpk 
309645916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
309745916cd2Sjpk 			continue;
309845916cd2Sjpk 
309945916cd2Sjpk 		/*
310045916cd2Sjpk 		 * Skip the zone we are halting
310145916cd2Sjpk 		 */
310245916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
310345916cd2Sjpk 			continue;
310445916cd2Sjpk 
310545916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
310645916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
310745916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
310845916cd2Sjpk 			/* Skip over zones without mounted filesystems */
310945916cd2Sjpk 			continue;
311045916cd2Sjpk 
311145916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
311245916cd2Sjpk 		    sizeof (m_label_t)) < 0)
311345916cd2Sjpk 			/* Skip over zones with unspecified label */
311445916cd2Sjpk 			continue;
311545916cd2Sjpk 
311645916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
311745916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
311845916cd2Sjpk 			/* Skip over zones with bad path */
311945916cd2Sjpk 			continue;
312045916cd2Sjpk 
312145916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
312245916cd2Sjpk 			/*
312345916cd2Sjpk 			 * This zone dominates our zone.
312445916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
312545916cd2Sjpk 			 */
312645916cd2Sjpk 
312745916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
312845916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
312945916cd2Sjpk 			    zone_name) > MAXPATHLEN)
313045916cd2Sjpk 				continue;
313145916cd2Sjpk 
313245916cd2Sjpk 			/* Skip over mount failures */
313345916cd2Sjpk 			(void) umount(path);
313445916cd2Sjpk 		}
313545916cd2Sjpk 	}
313645916cd2Sjpk 	free(zids);
313745916cd2Sjpk 
313845916cd2Sjpk 	/*
313945916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
314045916cd2Sjpk 	 */
314145916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
314245916cd2Sjpk 	/* Skip over mount failures */
314345916cd2Sjpk 	(void) umount(path);
314445916cd2Sjpk 
314545916cd2Sjpk 	/*
314645916cd2Sjpk 	 * Next unshare any exported directories from this zone.
314745916cd2Sjpk 	 */
314845916cd2Sjpk 
314945916cd2Sjpk 	argv[0] = "zoneunshare";
315045916cd2Sjpk 	argv[1] = "-z";
315145916cd2Sjpk 	argv[2] = zone_name;
315245916cd2Sjpk 	argv[3] = NULL;
315345916cd2Sjpk 
315445916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
315545916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
315645916cd2Sjpk 
315745916cd2Sjpk 	/*
315845916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
315945916cd2Sjpk 	 */
316045916cd2Sjpk 
316145916cd2Sjpk 	argv[0] = "deallocate";
316245916cd2Sjpk 	argv[1] = "-Isz";
316345916cd2Sjpk 	argv[2] = zone_name;
316445916cd2Sjpk 	argv[3] = NULL;
316545916cd2Sjpk 
316645916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
316745916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
316845916cd2Sjpk }
316945916cd2Sjpk 
317045916cd2Sjpk /*
317145916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
317245916cd2Sjpk  * this zone.
317345916cd2Sjpk  */
317445916cd2Sjpk static tsol_zcent_t *
317545916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
317645916cd2Sjpk {
317745916cd2Sjpk 	FILE *fp;
317845916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
317945916cd2Sjpk 	char line[MAXTNZLEN];
318045916cd2Sjpk 
318145916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
318245916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
318345916cd2Sjpk 		return (NULL);
318445916cd2Sjpk 	}
318545916cd2Sjpk 
318645916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
318745916cd2Sjpk 		/*
318845916cd2Sjpk 		 * Check for malformed database
318945916cd2Sjpk 		 */
319045916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
319145916cd2Sjpk 			break;
319245916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
319345916cd2Sjpk 			continue;
319445916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
319545916cd2Sjpk 			break;
319645916cd2Sjpk 		tsol_freezcent(zcent);
319745916cd2Sjpk 		zcent = NULL;
319845916cd2Sjpk 	}
319945916cd2Sjpk 	(void) fclose(fp);
320045916cd2Sjpk 
320145916cd2Sjpk 	if (zcent == NULL) {
320245916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
320345916cd2Sjpk 		    "See tnzonecfg(4)");
320445916cd2Sjpk 	} else {
320545916cd2Sjpk 		if (zlabel == NULL)
320645916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
320745916cd2Sjpk 		/*
320845916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
320945916cd2Sjpk 		 */
321045916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
321145916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
321245916cd2Sjpk 			return (NULL);
321345916cd2Sjpk 		} else {
321445916cd2Sjpk 			priv_copyset(privs, zprivs);
321545916cd2Sjpk 		}
321645916cd2Sjpk 	}
321745916cd2Sjpk 	return (zcent);
321845916cd2Sjpk }
321945916cd2Sjpk 
322045916cd2Sjpk /*
322145916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
322245916cd2Sjpk  */
322345916cd2Sjpk static void
322445916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
322545916cd2Sjpk {
322645916cd2Sjpk 	tsol_mlp_t *mlp;
322745916cd2Sjpk 	tsol_mlpent_t tsme;
322845916cd2Sjpk 
322945916cd2Sjpk 	if (!is_system_labeled())
323045916cd2Sjpk 		return;
323145916cd2Sjpk 
323245916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
323345916cd2Sjpk 	tsme.tsme_flags = 0;
323445916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
323545916cd2Sjpk 		tsme.tsme_mlp = *mlp;
323645916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
323745916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
323845916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
323945916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
324045916cd2Sjpk 		}
324145916cd2Sjpk 	}
324245916cd2Sjpk 
324345916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
324445916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
324545916cd2Sjpk 		tsme.tsme_mlp = *mlp;
324645916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
324745916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
324845916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
324945916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
325045916cd2Sjpk 		}
325145916cd2Sjpk 	}
325245916cd2Sjpk }
325345916cd2Sjpk 
325445916cd2Sjpk static void
325545916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
325645916cd2Sjpk {
325745916cd2Sjpk 	tsol_mlpent_t tsme;
325845916cd2Sjpk 
325945916cd2Sjpk 	if (!is_system_labeled())
326045916cd2Sjpk 		return;
326145916cd2Sjpk 
326245916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
326345916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
326445916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
326545916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
326645916cd2Sjpk }
326745916cd2Sjpk 
32687c478bd9Sstevel@tonic-gate int
32697c478bd9Sstevel@tonic-gate prtmount(const char *fs, void *x) {
32707c478bd9Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
32717c478bd9Sstevel@tonic-gate 	return (0);
32727c478bd9Sstevel@tonic-gate }
32737c478bd9Sstevel@tonic-gate 
3274108322fbScarlsonj /*
3275108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
3276108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3277108322fbScarlsonj  * is found or if we can't tell.
3278108322fbScarlsonj  */
3279108322fbScarlsonj static boolean_t
3280108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
32817c478bd9Sstevel@tonic-gate {
3282108322fbScarlsonj 	zoneid_t *zids = NULL;
3283108322fbScarlsonj 	uint_t nzids = 0;
3284108322fbScarlsonj 	boolean_t retv;
3285108322fbScarlsonj 	int rlen, zlen;
3286108322fbScarlsonj 	char zroot[MAXPATHLEN];
3287108322fbScarlsonj 	char zonename[ZONENAME_MAX];
3288108322fbScarlsonj 
3289108322fbScarlsonj 	for (;;) {
3290108322fbScarlsonj 		nzids += 10;
3291108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
3292108322fbScarlsonj 		if (zids == NULL) {
3293108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to allocate memory");
3294108322fbScarlsonj 			return (B_TRUE);
3295108322fbScarlsonj 		}
3296108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
3297108322fbScarlsonj 			break;
3298108322fbScarlsonj 		free(zids);
3299108322fbScarlsonj 	}
3300108322fbScarlsonj 	retv = B_FALSE;
3301108322fbScarlsonj 	rlen = strlen(rootpath);
3302108322fbScarlsonj 	while (nzids > 0) {
3303108322fbScarlsonj 		/*
3304108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3305108322fbScarlsonj 		 * while we were busy.
3306108322fbScarlsonj 		 */
3307108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3308108322fbScarlsonj 		    sizeof (zroot)) == -1)
3309108322fbScarlsonj 			continue;
3310108322fbScarlsonj 		zlen = strlen(zroot);
3311108322fbScarlsonj 		if (zlen > rlen)
3312108322fbScarlsonj 			zlen = rlen;
3313108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3314108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3315108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3316108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3317108322fbScarlsonj 			    sizeof (zonename)) == -1)
3318108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3319108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
3320108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3321108322fbScarlsonj 			    "zone root %s already in use by zone %s",
3322108322fbScarlsonj 			    rootpath, zonename);
3323108322fbScarlsonj 			retv = B_TRUE;
3324108322fbScarlsonj 			break;
3325108322fbScarlsonj 		}
3326108322fbScarlsonj 	}
3327108322fbScarlsonj 	free(zids);
3328108322fbScarlsonj 	return (retv);
3329108322fbScarlsonj }
3330108322fbScarlsonj 
3331108322fbScarlsonj /*
3332108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
3333108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3334108322fbScarlsonj  */
3335108322fbScarlsonj static boolean_t
3336108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3337108322fbScarlsonj {
3338108322fbScarlsonj 	struct stat64 rst, zst;
3339108322fbScarlsonj 	struct mnttab *mnp;
3340108322fbScarlsonj 
3341108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
3342108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3343108322fbScarlsonj 		return (B_TRUE);
3344108322fbScarlsonj 	}
3345108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3346108322fbScarlsonj 		return (B_TRUE);
3347108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3348108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
3349108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3350108322fbScarlsonj 			continue;
3351108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3352108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
3353108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3354108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3355108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3356108322fbScarlsonj 			    "zone root %s is reachable through %s",
3357108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
3358108322fbScarlsonj 			return (B_TRUE);
3359108322fbScarlsonj 		}
3360108322fbScarlsonj 	}
3361108322fbScarlsonj 	return (B_FALSE);
3362108322fbScarlsonj }
3363108322fbScarlsonj 
3364108322fbScarlsonj zoneid_t
3365108322fbScarlsonj vplat_create(zlog_t *zlogp, boolean_t mount_cmd)
3366108322fbScarlsonj {
3367108322fbScarlsonj 	zoneid_t rval = -1;
33687c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
33697c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
33707c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
3371108322fbScarlsonj 	size_t rctlbufsz = 0;
3372fa9e4066Sahrens 	char *zfsbuf = NULL;
3373fa9e4066Sahrens 	size_t zfsbufsz = 0;
3374108322fbScarlsonj 	zoneid_t zoneid = -1;
33757c478bd9Sstevel@tonic-gate 	int xerr;
3376108322fbScarlsonj 	char *kzone;
3377108322fbScarlsonj 	FILE *fp = NULL;
337845916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
337945916cd2Sjpk 	int match = 0;
338045916cd2Sjpk 	int doi = 0;
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
33837c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
33847c478bd9Sstevel@tonic-gate 		return (-1);
33857c478bd9Sstevel@tonic-gate 	}
3386108322fbScarlsonj 	if (zonecfg_in_alt_root())
3387108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
33907c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
33917c478bd9Sstevel@tonic-gate 		return (-1);
33927c478bd9Sstevel@tonic-gate 	}
33937c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
3394ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
33957c478bd9Sstevel@tonic-gate 		goto error;
3396ffbafc53Scomay 
3397108322fbScarlsonj 	if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
33987c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
33997c478bd9Sstevel@tonic-gate 		goto error;
34007c478bd9Sstevel@tonic-gate 	}
3401ffbafc53Scomay 
3402fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
3403fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
3404fa9e4066Sahrens 		goto error;
3405fa9e4066Sahrens 	}
34067c478bd9Sstevel@tonic-gate 
340748451833Scarlsonj 	if (!mount_cmd && is_system_labeled()) {
340845916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
340948451833Scarlsonj 		if (zcent != NULL) {
341045916cd2Sjpk 			match = zcent->zc_match;
341145916cd2Sjpk 			doi = zcent->zc_doi;
341245916cd2Sjpk 			*zlabel = zcent->zc_label;
341345916cd2Sjpk 		} else {
341445916cd2Sjpk 			goto error;
341545916cd2Sjpk 		}
341645916cd2Sjpk 	}
341745916cd2Sjpk 
3418108322fbScarlsonj 	kzone = zone_name;
3419108322fbScarlsonj 
3420108322fbScarlsonj 	/*
3421108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
3422108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
3423108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
3424108322fbScarlsonj 	 * that use this same source node (same device and inode).
3425108322fbScarlsonj 	 */
3426108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
3427108322fbScarlsonj 		goto error;
3428108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
3429108322fbScarlsonj 		goto error;
3430108322fbScarlsonj 
3431108322fbScarlsonj 	if (mount_cmd) {
3432108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
3433108322fbScarlsonj 
3434108322fbScarlsonj 		/*
3435108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
3436108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
3437108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
3438108322fbScarlsonj 		 * to both the zone's resources and the running machine's
3439108322fbScarlsonj 		 * executables.
3440108322fbScarlsonj 		 *
3441108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
3442108322fbScarlsonj 		 */
3443108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
3444108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
3445108322fbScarlsonj 			goto error;
3446108322fbScarlsonj 		}
3447108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
3448108322fbScarlsonj 			goto error;
3449108322fbScarlsonj 	}
3450108322fbScarlsonj 
3451108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
3452108322fbScarlsonj 		/*
3453108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
3454108322fbScarlsonj 		 * then we have some additional work to do before starting the
3455108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
3456108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
3457108322fbScarlsonj 		 * the zone.
3458108322fbScarlsonj 		 */
3459108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
3460108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
3461108322fbScarlsonj 			goto error;
3462108322fbScarlsonj 		}
3463108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
3464108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
3465108322fbScarlsonj 			goto error;
3466108322fbScarlsonj 		}
3467108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
3468108322fbScarlsonj 		    NULL, 0) == 0) {
3469108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
3470108322fbScarlsonj 			goto error;
3471108322fbScarlsonj 		}
3472108322fbScarlsonj 		/* This is the preferred name */
3473108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
3474108322fbScarlsonj 		    zone_name);
3475108322fbScarlsonj 		srandom(getpid());
3476108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
3477108322fbScarlsonj 		    0) == 0) {
3478108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
3479108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
3480108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
3481108322fbScarlsonj 		}
3482108322fbScarlsonj 		kzone = kernzone;
3483108322fbScarlsonj 	}
3484108322fbScarlsonj 
34857c478bd9Sstevel@tonic-gate 	xerr = 0;
3486108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
348745916cd2Sjpk 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel)) == -1) {
34887c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
34897c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
34907c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
34917c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
34927c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
34937c478bd9Sstevel@tonic-gate 			} else {
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
34967c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
34977c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
34987c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
34997c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
35007c478bd9Sstevel@tonic-gate 			}
35017c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
35027c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
35037c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
35047c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
35057c478bd9Sstevel@tonic-gate 		} else {
35067c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
35077c478bd9Sstevel@tonic-gate 		}
35087c478bd9Sstevel@tonic-gate 		goto error;
35097c478bd9Sstevel@tonic-gate 	}
3510108322fbScarlsonj 
3511108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
3512108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
3513108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
3514108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
3515108322fbScarlsonj 		goto error;
3516108322fbScarlsonj 	}
3517108322fbScarlsonj 
35187c478bd9Sstevel@tonic-gate 	/*
3519108322fbScarlsonj 	 * The following is a warning, not an error, and is not performed when
3520108322fbScarlsonj 	 * merely mounting a zone for administrative use.
35217c478bd9Sstevel@tonic-gate 	 */
3522108322fbScarlsonj 	if (!mount_cmd && bind_to_pool(zlogp, zoneid) != 0)
35237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: unable to bind zone to "
35247c478bd9Sstevel@tonic-gate 		    "requested pool; using default pool.");
352548451833Scarlsonj 	if (!mount_cmd)
352645916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
3527108322fbScarlsonj 	rval = zoneid;
3528108322fbScarlsonj 	zoneid = -1;
3529108322fbScarlsonj 
35307c478bd9Sstevel@tonic-gate error:
3531108322fbScarlsonj 	if (zoneid != -1)
3532108322fbScarlsonj 		(void) zone_destroy(zoneid);
35337c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
35347c478bd9Sstevel@tonic-gate 		free(rctlbuf);
35357c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
3536108322fbScarlsonj 	if (fp != NULL)
3537108322fbScarlsonj 		zonecfg_close_scratch(fp);
3538108322fbScarlsonj 	lofs_discard_mnttab();
353945916cd2Sjpk 	if (zcent != NULL)
354045916cd2Sjpk 		tsol_freezcent(zcent);
35417c478bd9Sstevel@tonic-gate 	return (rval);
35427c478bd9Sstevel@tonic-gate }
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate int
3545108322fbScarlsonj vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd)
35467c478bd9Sstevel@tonic-gate {
3547fa9e4066Sahrens 	if (!mount_cmd && validate_datasets(zlogp) != 0) {
3548fa9e4066Sahrens 		lofs_discard_mnttab();
3549fa9e4066Sahrens 		return (-1);
3550fa9e4066Sahrens 	}
3551fa9e4066Sahrens 
3552108322fbScarlsonj 	if (create_dev_files(zlogp) != 0 ||
3553108322fbScarlsonj 	    mount_filesystems(zlogp, mount_cmd) != 0) {
3554108322fbScarlsonj 		lofs_discard_mnttab();
35557c478bd9Sstevel@tonic-gate 		return (-1);
3556108322fbScarlsonj 	}
3557108322fbScarlsonj 	if (!mount_cmd && (devfsadm_register(zlogp) != 0 ||
3558108322fbScarlsonj 	    configure_network_interfaces(zlogp) != 0)) {
3559108322fbScarlsonj 		lofs_discard_mnttab();
35607c478bd9Sstevel@tonic-gate 		return (-1);
3561108322fbScarlsonj 	}
3562108322fbScarlsonj 	lofs_discard_mnttab();
35637c478bd9Sstevel@tonic-gate 	return (0);
35647c478bd9Sstevel@tonic-gate }
35657c478bd9Sstevel@tonic-gate 
3566108322fbScarlsonj static int
3567108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
35687c478bd9Sstevel@tonic-gate {
3569108322fbScarlsonj 	char zroot[MAXPATHLEN];
3570108322fbScarlsonj 
3571108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
3572108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
3573108322fbScarlsonj 		return (-1);
3574108322fbScarlsonj 	}
3575108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
3576108322fbScarlsonj 
3577108322fbScarlsonj 	/*
3578108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
3579108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
3580108322fbScarlsonj 	 * still be creds floating about in the system that reference the
3581108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
3582108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
3583108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
3584108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
3585108322fbScarlsonj 	 */
3586108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
3587108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
3588108322fbScarlsonj 			goto unmounted;
3589108322fbScarlsonj 		if (errno == EBUSY) {
3590108322fbScarlsonj 			int tries = 10;
3591108322fbScarlsonj 
3592108322fbScarlsonj 			while (--tries >= 0) {
3593108322fbScarlsonj 				(void) sleep(1);
3594108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
3595108322fbScarlsonj 					goto unmounted;
3596108322fbScarlsonj 				if (errno != EBUSY)
3597108322fbScarlsonj 					break;
3598108322fbScarlsonj 			}
3599108322fbScarlsonj 		}
3600108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
3601108322fbScarlsonj 		return (-1);
3602108322fbScarlsonj 	}
3603108322fbScarlsonj unmounted:
3604108322fbScarlsonj 
3605108322fbScarlsonj 	/*
3606108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
3607108322fbScarlsonj 	 * entries.
3608108322fbScarlsonj 	 */
3609108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
3610108322fbScarlsonj 		FILE *fp;
3611108322fbScarlsonj 		int retv;
3612108322fbScarlsonj 
3613108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
3614108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
3615108322fbScarlsonj 			return (-1);
3616108322fbScarlsonj 		}
3617108322fbScarlsonj 		retv = -1;
3618108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
3619108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
3620108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
3621108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
3622108322fbScarlsonj 		else
3623108322fbScarlsonj 			retv = 0;
3624108322fbScarlsonj 		zonecfg_close_scratch(fp);
3625108322fbScarlsonj 		return (retv);
3626108322fbScarlsonj 	} else {
3627108322fbScarlsonj 		return (0);
3628108322fbScarlsonj 	}
3629108322fbScarlsonj }
3630108322fbScarlsonj 
3631108322fbScarlsonj int
3632108322fbScarlsonj vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd)
3633108322fbScarlsonj {
3634108322fbScarlsonj 	char *kzone;
36357c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
36367c478bd9Sstevel@tonic-gate 
3637108322fbScarlsonj 	kzone = zone_name;
3638108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
3639108322fbScarlsonj 		FILE *fp;
3640108322fbScarlsonj 
3641108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
3642108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
3643108322fbScarlsonj 			goto error;
3644108322fbScarlsonj 		}
3645108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
3646108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
3647108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
3648108322fbScarlsonj 			zonecfg_close_scratch(fp);
3649108322fbScarlsonj 			goto error;
3650108322fbScarlsonj 		}
3651108322fbScarlsonj 		zonecfg_close_scratch(fp);
3652108322fbScarlsonj 		kzone = kernzone;
3653108322fbScarlsonj 	}
3654108322fbScarlsonj 
3655108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
36567c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
36577c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
3658108322fbScarlsonj 		if (unmount_cmd)
3659108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
36607c478bd9Sstevel@tonic-gate 		goto error;
36617c478bd9Sstevel@tonic-gate 	}
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
36647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
36657c478bd9Sstevel@tonic-gate 		goto error;
36667c478bd9Sstevel@tonic-gate 	}
36677c478bd9Sstevel@tonic-gate 
3668108322fbScarlsonj 	if (!unmount_cmd && devfsadm_unregister(zlogp) != 0)
36697c478bd9Sstevel@tonic-gate 		goto error;
36707c478bd9Sstevel@tonic-gate 
3671108322fbScarlsonj 	if (!unmount_cmd &&
3672108322fbScarlsonj 	    unconfigure_network_interfaces(zlogp, zoneid) != 0) {
36737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
36747c478bd9Sstevel@tonic-gate 		    "unable to unconfigure network interfaces in zone");
36757c478bd9Sstevel@tonic-gate 		goto error;
36767c478bd9Sstevel@tonic-gate 	}
36777c478bd9Sstevel@tonic-gate 
3678108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
36797c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
36807c478bd9Sstevel@tonic-gate 		goto error;
36817c478bd9Sstevel@tonic-gate 	}
36827c478bd9Sstevel@tonic-gate 
3683108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
36847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
36857c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
36867c478bd9Sstevel@tonic-gate 		goto error;
36877c478bd9Sstevel@tonic-gate 	}
36887c478bd9Sstevel@tonic-gate 
368945916cd2Sjpk 	remove_mlps(zlogp, zoneid);
369045916cd2Sjpk 
36917c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
36927c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
36937c478bd9Sstevel@tonic-gate 		goto error;
36947c478bd9Sstevel@tonic-gate 	}
3695108322fbScarlsonj 
3696108322fbScarlsonj 	/*
3697108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
3698108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
3699108322fbScarlsonj 	 */
3700108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
3701108322fbScarlsonj 		goto error;
3702108322fbScarlsonj 
3703108322fbScarlsonj 	if (!unmount_cmd)
37047c478bd9Sstevel@tonic-gate 		destroy_console_slave();
37057c478bd9Sstevel@tonic-gate 
3706108322fbScarlsonj 	lofs_discard_mnttab();
37077c478bd9Sstevel@tonic-gate 	return (0);
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate error:
3710108322fbScarlsonj 	lofs_discard_mnttab();
37117c478bd9Sstevel@tonic-gate 	return (-1);
37127c478bd9Sstevel@tonic-gate }
3713