xref: /onnv-gate/usr/src/cmd/zoneadmd/vplat.c (revision 2772:284bda1a2ceb)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211645Scomay 
220Sstevel@tonic-gate /*
231544Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
310Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
320Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
330Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
340Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
350Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
360Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
37766Scarlsonj  *
38766Scarlsonj  * This module also has primary responsibility for the layout of "scratch
39766Scarlsonj  * zones."  These are mounted, but inactive, zones that are used during
40766Scarlsonj  * operating system upgrade and potentially other administrative action.  The
41766Scarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
42766Scarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
43766Scarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
44766Scarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
45766Scarlsonj  * without relying on any binaries in the zone itself.
46766Scarlsonj  *
47766Scarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
48766Scarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
49766Scarlsonj  * writable (unshared) resources.  Shared resources, though, are always
50766Scarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
51766Scarlsonj  * currently running, then "/b" inside the zone points to the running zone's
52766Scarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
53766Scarlsonj  * process.
54766Scarlsonj  *
55766Scarlsonj  * To construct this environment, this module creates a tmpfs mount on
56766Scarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
57766Scarlsonj  * described above is constructed on the fly.  The zone is then created using
58766Scarlsonj  * $ZONEPATH/lu as the root.
59766Scarlsonj  *
60766Scarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
61766Scarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
62766Scarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
63766Scarlsonj  * is not a part of the usual halt/ready/boot state machine.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/param.h>
670Sstevel@tonic-gate #include <sys/mount.h>
680Sstevel@tonic-gate #include <sys/mntent.h>
690Sstevel@tonic-gate #include <sys/socket.h>
700Sstevel@tonic-gate #include <sys/utsname.h>
710Sstevel@tonic-gate #include <sys/types.h>
720Sstevel@tonic-gate #include <sys/stat.h>
730Sstevel@tonic-gate #include <sys/sockio.h>
740Sstevel@tonic-gate #include <sys/stropts.h>
750Sstevel@tonic-gate #include <sys/conf.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #include <inet/tcp.h>
780Sstevel@tonic-gate #include <arpa/inet.h>
790Sstevel@tonic-gate #include <netinet/in.h>
800Sstevel@tonic-gate #include <net/route.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #include <stdio.h>
830Sstevel@tonic-gate #include <errno.h>
840Sstevel@tonic-gate #include <fcntl.h>
850Sstevel@tonic-gate #include <unistd.h>
860Sstevel@tonic-gate #include <rctl.h>
870Sstevel@tonic-gate #include <stdlib.h>
880Sstevel@tonic-gate #include <string.h>
890Sstevel@tonic-gate #include <strings.h>
900Sstevel@tonic-gate #include <wait.h>
910Sstevel@tonic-gate #include <limits.h>
920Sstevel@tonic-gate #include <libgen.h>
93789Sahrens #include <libzfs.h>
942621Sllai1 #include <libdevinfo.h>
950Sstevel@tonic-gate #include <zone.h>
960Sstevel@tonic-gate #include <assert.h>
972303Scarlsonj #include <libcontract.h>
982303Scarlsonj #include <libcontract_priv.h>
992303Scarlsonj #include <uuid/uuid.h>
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #include <sys/mntio.h>
1020Sstevel@tonic-gate #include <sys/mnttab.h>
1030Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1040Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
105789Sahrens #include <sys/fs/zfs.h>
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #include <pool.h>
1080Sstevel@tonic-gate #include <sys/pool.h>
1090Sstevel@tonic-gate 
1102712Snn35248 #include <libbrand.h>
1112712Snn35248 #include <sys/brand.h>
1120Sstevel@tonic-gate #include <libzonecfg.h>
1132170Sevanl #include <synch.h>
1142611Svp157776 
1150Sstevel@tonic-gate #include "zoneadmd.h"
1161676Sjpk #include <tsol/label.h>
1171676Sjpk #include <libtsnet.h>
1181676Sjpk #include <sys/priv.h>
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1210Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /* 0755 is the default directory mode. */
1240Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1250Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1280Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
1311676Sjpk #define	MAXTNZLEN	2048
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* for routing socket */
1340Sstevel@tonic-gate static int rts_seqno = 0;
1350Sstevel@tonic-gate 
136766Scarlsonj /* mangled zone name when mounting in an alternate root environment */
137766Scarlsonj static char kernzone[ZONENAME_MAX];
138766Scarlsonj 
139766Scarlsonj /* array of cached mount entries for resolve_lofs */
140766Scarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
141766Scarlsonj 
1421676Sjpk /* for Trusted Extensions */
1431676Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
1441676Sjpk static int tsol_mounts(zlog_t *, char *, char *);
1451676Sjpk static void tsol_unmounts(zlog_t *, char *);
1461676Sjpk static m_label_t *zlabel = NULL;
1471676Sjpk static m_label_t *zid_label = NULL;
1481676Sjpk static priv_set_t *zprivs = NULL;
1491676Sjpk 
1500Sstevel@tonic-gate /* from libsocket, not in any header file */
1510Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
154766Scarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
155766Scarlsonj  * data) only once every N times through the loop.
156766Scarlsonj  */
157766Scarlsonj #define	MNTTAB_HUNK	32
158766Scarlsonj 
159766Scarlsonj /*
1600Sstevel@tonic-gate  * Private autofs system call
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate extern int _autofssys(int, void *);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static int
1650Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1690Sstevel@tonic-gate 	 */
1700Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
173766Scarlsonj static void
174766Scarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
175766Scarlsonj {
176766Scarlsonj 	uint_t i;
177766Scarlsonj 
178766Scarlsonj 	if (mnt_array == NULL)
179766Scarlsonj 		return;
180766Scarlsonj 	for (i = 0; i < nelem; i++) {
181766Scarlsonj 		free(mnt_array[i].mnt_mountp);
182766Scarlsonj 		free(mnt_array[i].mnt_fstype);
183766Scarlsonj 		free(mnt_array[i].mnt_special);
184766Scarlsonj 		free(mnt_array[i].mnt_mntopts);
185766Scarlsonj 		assert(mnt_array[i].mnt_time == NULL);
186766Scarlsonj 	}
187766Scarlsonj 	free(mnt_array);
188766Scarlsonj }
189766Scarlsonj 
190766Scarlsonj /*
191766Scarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
192766Scarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
193766Scarlsonj  * array in "nelemp".
194766Scarlsonj  */
195766Scarlsonj static int
196766Scarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
197766Scarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
198766Scarlsonj {
199766Scarlsonj 	struct mnttab mnt;
200766Scarlsonj 	struct mnttab *mnts;
201766Scarlsonj 	struct mnttab *mnp;
202766Scarlsonj 	uint_t nmnt;
203766Scarlsonj 
204766Scarlsonj 	rewind(mnttab);
205766Scarlsonj 	resetmnttab(mnttab);
206766Scarlsonj 	nmnt = 0;
207766Scarlsonj 	mnts = NULL;
208766Scarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
209766Scarlsonj 		struct mnttab *tmp_array;
210766Scarlsonj 
211766Scarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
212766Scarlsonj 			continue;
213766Scarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
214766Scarlsonj 			tmp_array = realloc(mnts,
215766Scarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
216766Scarlsonj 			if (tmp_array == NULL) {
217766Scarlsonj 				free_mnttable(mnts, nmnt);
218766Scarlsonj 				return (-1);
219766Scarlsonj 			}
220766Scarlsonj 			mnts = tmp_array;
221766Scarlsonj 		}
222766Scarlsonj 		mnp = &mnts[nmnt++];
223766Scarlsonj 
224766Scarlsonj 		/*
225766Scarlsonj 		 * Zero out any fields we're not using.
226766Scarlsonj 		 */
227766Scarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
228766Scarlsonj 
229766Scarlsonj 		if (mnt.mnt_special != NULL)
230766Scarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
231766Scarlsonj 		if (mnt.mnt_mntopts != NULL)
232766Scarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
233766Scarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
234766Scarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
235766Scarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
236766Scarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
237766Scarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
238766Scarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
239766Scarlsonj 			free_mnttable(mnts, nmnt);
240766Scarlsonj 			return (-1);
241766Scarlsonj 		}
242766Scarlsonj 	}
243766Scarlsonj 	*mnt_arrayp = mnts;
244766Scarlsonj 	*nelemp = nmnt;
245766Scarlsonj 	return (0);
246766Scarlsonj }
247766Scarlsonj 
248766Scarlsonj /*
249766Scarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
250766Scarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
251766Scarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
252766Scarlsonj  * reread of the list of mount points
253766Scarlsonj  */
254766Scarlsonj static void
255766Scarlsonj lofs_discard_mnttab(void)
256766Scarlsonj {
257766Scarlsonj 	free_mnttable(resolve_lofs_mnts,
258766Scarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
259766Scarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
260766Scarlsonj }
261766Scarlsonj 
262766Scarlsonj static int
263766Scarlsonj lofs_read_mnttab(zlog_t *zlogp)
264766Scarlsonj {
265766Scarlsonj 	FILE *mnttab;
266766Scarlsonj 	uint_t nmnts;
267766Scarlsonj 
268766Scarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
269766Scarlsonj 		return (-1);
270766Scarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
271766Scarlsonj 	    &nmnts) == -1) {
272766Scarlsonj 		(void) fclose(mnttab);
273766Scarlsonj 		return (-1);
274766Scarlsonj 	}
275766Scarlsonj 	(void) fclose(mnttab);
276766Scarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
277766Scarlsonj 	return (0);
278766Scarlsonj }
279766Scarlsonj 
280766Scarlsonj /*
281766Scarlsonj  * This function loops over potential loopback mounts and symlinks in a given
282766Scarlsonj  * path and resolves them all down to an absolute path.
283766Scarlsonj  */
284766Scarlsonj static void
285766Scarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
286766Scarlsonj {
287766Scarlsonj 	int len, arlen;
288766Scarlsonj 	const char *altroot;
289766Scarlsonj 	char tmppath[MAXPATHLEN];
290766Scarlsonj 	boolean_t outside_altroot;
291766Scarlsonj 
292766Scarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
293766Scarlsonj 		return;
294766Scarlsonj 	tmppath[len] = '\0';
295766Scarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
296766Scarlsonj 
297766Scarlsonj 	/* This happens once per zoneadmd operation. */
298766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
299766Scarlsonj 		return;
300766Scarlsonj 
301766Scarlsonj 	altroot = zonecfg_get_root();
302766Scarlsonj 	arlen = strlen(altroot);
303766Scarlsonj 	outside_altroot = B_FALSE;
304766Scarlsonj 	for (;;) {
305766Scarlsonj 		struct mnttab *mnp;
306766Scarlsonj 
307766Scarlsonj 		for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
308766Scarlsonj 		    mnp++) {
309766Scarlsonj 			if (mnp->mnt_fstype == NULL ||
310766Scarlsonj 			    mnp->mnt_mountp == NULL ||
311766Scarlsonj 			    mnp->mnt_special == NULL ||
312766Scarlsonj 			    strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
313766Scarlsonj 				continue;
314766Scarlsonj 			len = strlen(mnp->mnt_mountp);
315766Scarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
316766Scarlsonj 			    (path[len] == '/' || path[len] == '\0'))
317766Scarlsonj 				break;
318766Scarlsonj 		}
319766Scarlsonj 		if (mnp >= resolve_lofs_mnt_max)
320766Scarlsonj 			break;
321766Scarlsonj 		if (outside_altroot) {
322766Scarlsonj 			char *cp;
323766Scarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
324766Scarlsonj 
325766Scarlsonj 			/*
326766Scarlsonj 			 * If we run into a read-only mount outside of the
327766Scarlsonj 			 * alternate root environment, then the user doesn't
328766Scarlsonj 			 * want this path to be made read-write.
329766Scarlsonj 			 */
330766Scarlsonj 			if (mnp->mnt_mntopts != NULL &&
331766Scarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
332766Scarlsonj 			    NULL &&
333766Scarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
334766Scarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
335766Scarlsonj 				break;
336766Scarlsonj 			}
337766Scarlsonj 		} else if (arlen > 0 &&
338766Scarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
339766Scarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
340766Scarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
341766Scarlsonj 			outside_altroot = B_TRUE;
342766Scarlsonj 		}
343766Scarlsonj 		/* use temporary buffer because new path might be longer */
344766Scarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
345766Scarlsonj 		    mnp->mnt_special, path + len);
346766Scarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
347766Scarlsonj 			break;
348766Scarlsonj 		path[len] = '\0';
349766Scarlsonj 	}
350766Scarlsonj }
351766Scarlsonj 
352766Scarlsonj /*
353766Scarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
354766Scarlsonj  * referenced device is already mounted somewhere.
355766Scarlsonj  */
356766Scarlsonj static int
357766Scarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
358766Scarlsonj {
359766Scarlsonj 	struct mnttab *mnp;
360766Scarlsonj 	zone_fsopt_t *optptr, *onext;
361766Scarlsonj 
362766Scarlsonj 	/* This happens once per zoneadmd operation. */
363766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
364766Scarlsonj 		return (-1);
365766Scarlsonj 
366766Scarlsonj 	/*
367766Scarlsonj 	 * If this special node isn't already in use, then it's ours alone;
368766Scarlsonj 	 * no need to worry about conflicting mounts.
369766Scarlsonj 	 */
370766Scarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
371766Scarlsonj 	    mnp++) {
372766Scarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
373766Scarlsonj 			break;
374766Scarlsonj 	}
375766Scarlsonj 	if (mnp >= resolve_lofs_mnt_max)
376766Scarlsonj 		return (0);
377766Scarlsonj 
378766Scarlsonj 	/*
379766Scarlsonj 	 * Convert this duplicate mount into a lofs mount.
380766Scarlsonj 	 */
381766Scarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
382766Scarlsonj 	    sizeof (fsptr->zone_fs_special));
383766Scarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
384766Scarlsonj 	    sizeof (fsptr->zone_fs_type));
385766Scarlsonj 	fsptr->zone_fs_raw[0] = '\0';
386766Scarlsonj 
387766Scarlsonj 	/*
388766Scarlsonj 	 * Discard all but one of the original options and set that to be the
389766Scarlsonj 	 * same set of options used for inherit package directory resources.
390766Scarlsonj 	 */
391766Scarlsonj 	optptr = fsptr->zone_fs_options;
392766Scarlsonj 	if (optptr == NULL) {
393766Scarlsonj 		optptr = malloc(sizeof (*optptr));
394766Scarlsonj 		if (optptr == NULL) {
395766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
396766Scarlsonj 			    fsptr->zone_fs_dir);
397766Scarlsonj 			return (-1);
398766Scarlsonj 		}
399766Scarlsonj 	} else {
400766Scarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
401766Scarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
402766Scarlsonj 			free(onext);
403766Scarlsonj 		}
404766Scarlsonj 	}
405766Scarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
406766Scarlsonj 	optptr->zone_fsopt_next = NULL;
407766Scarlsonj 	fsptr->zone_fs_options = optptr;
408766Scarlsonj 	return (0);
409766Scarlsonj }
410766Scarlsonj 
4110Sstevel@tonic-gate static int
4120Sstevel@tonic-gate make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	char path[MAXPATHLEN];
4150Sstevel@tonic-gate 	struct stat st;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4180Sstevel@tonic-gate 	    sizeof (path)) {
4190Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4200Sstevel@tonic-gate 		    subdir);
4210Sstevel@tonic-gate 		return (-1);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4250Sstevel@tonic-gate 		/*
4260Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4270Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4280Sstevel@tonic-gate 		 * and we don't need to second guess him.
4290Sstevel@tonic-gate 		 */
4300Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
4311676Sjpk 			if (is_system_labeled() &&
4321676Sjpk 			    S_ISREG(st.st_mode)) {
4331676Sjpk 				/*
4341676Sjpk 				 * The need to mount readonly copies of
4351676Sjpk 				 * global zone /etc/ files is unique to
4361676Sjpk 				 * Trusted Extensions.
4371676Sjpk 				 */
4381676Sjpk 				if (strncmp(subdir, "/etc/",
4391676Sjpk 				    strlen("/etc/")) != 0) {
4401676Sjpk 					zerror(zlogp, B_FALSE,
4411676Sjpk 					    "%s is not in /etc", path);
4421676Sjpk 					return (-1);
4431676Sjpk 				}
4441676Sjpk 			} else {
4451676Sjpk 				zerror(zlogp, B_FALSE,
4461676Sjpk 				    "%s is not a directory", path);
4471676Sjpk 				return (-1);
4481676Sjpk 			}
4490Sstevel@tonic-gate 		}
4500Sstevel@tonic-gate 	} else if (mkdirp(path, mode) != 0) {
4510Sstevel@tonic-gate 		if (errno == EROFS)
4520Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4530Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4540Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4550Sstevel@tonic-gate 		else
4560Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4570Sstevel@tonic-gate 		return (-1);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 	return (0);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate static void
4630Sstevel@tonic-gate free_remote_fstypes(char **types)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	uint_t i;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (types == NULL)
4680Sstevel@tonic-gate 		return;
4690Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
4700Sstevel@tonic-gate 		free(types[i]);
4710Sstevel@tonic-gate 	free(types);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate static char **
4750Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	char **types = NULL;
4780Sstevel@tonic-gate 	FILE *fp;
4790Sstevel@tonic-gate 	char buf[MAXPATHLEN];
4800Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
4810Sstevel@tonic-gate 	uint_t lines = 0;
4820Sstevel@tonic-gate 	uint_t i;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
4850Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
4860Sstevel@tonic-gate 		return (NULL);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	/*
4890Sstevel@tonic-gate 	 * Count the number of lines
4900Sstevel@tonic-gate 	 */
4910Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
4920Sstevel@tonic-gate 		lines++;
4930Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
4940Sstevel@tonic-gate 		goto out;
4950Sstevel@tonic-gate 	rewind(fp);
4960Sstevel@tonic-gate 	/*
4970Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
4980Sstevel@tonic-gate 	 */
4990Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5000Sstevel@tonic-gate 	if (types == NULL) {
5010Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5020Sstevel@tonic-gate 		goto out;
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 	i = 0;
5050Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5060Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5070Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5080Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5090Sstevel@tonic-gate 			free_remote_fstypes(types);
5100Sstevel@tonic-gate 			types = NULL;
5110Sstevel@tonic-gate 			goto out;
5120Sstevel@tonic-gate 		}
5130Sstevel@tonic-gate 		types[i] = strdup(fstype);
5140Sstevel@tonic-gate 		if (types[i] == NULL) {
5150Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5160Sstevel@tonic-gate 			free_remote_fstypes(types);
5170Sstevel@tonic-gate 			types = NULL;
5180Sstevel@tonic-gate 			goto out;
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 		i++;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate out:
5230Sstevel@tonic-gate 	(void) fclose(fp);
5240Sstevel@tonic-gate 	return (types);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate static boolean_t
5280Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	uint_t i;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5330Sstevel@tonic-gate 		return (B_FALSE);
5340Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5350Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5360Sstevel@tonic-gate 			return (B_TRUE);
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 	return (B_FALSE);
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate 
541766Scarlsonj /*
542766Scarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
543766Scarlsonj  * Upgrade scratch zone root (of the form .../lu).
544766Scarlsonj  */
5450Sstevel@tonic-gate static void
546766Scarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5470Sstevel@tonic-gate {
5482712Snn35248 	assert(zone_isnative);
5492712Snn35248 
550766Scarlsonj 	if (!isresolved && zonecfg_in_alt_root())
551766Scarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
552766Scarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5570Sstevel@tonic-gate  *
5580Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5590Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5600Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5610Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5620Sstevel@tonic-gate  *
5630Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5640Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5650Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5660Sstevel@tonic-gate  *
5670Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5680Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5690Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5700Sstevel@tonic-gate  *
5710Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5720Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5730Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5740Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5750Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5760Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5770Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5780Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5790Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5800Sstevel@tonic-gate  * graceful mounts first.
5810Sstevel@tonic-gate  *
5820Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5830Sstevel@tonic-gate  */
5840Sstevel@tonic-gate static int
585766Scarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate 	int error = 0;
5880Sstevel@tonic-gate 	FILE *mnttab;
5890Sstevel@tonic-gate 	struct mnttab *mnts;
5900Sstevel@tonic-gate 	uint_t nmnt;
5910Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
5920Sstevel@tonic-gate 	size_t zrootlen;
5930Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
5940Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
5950Sstevel@tonic-gate 	char **remote_fstypes = NULL;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5980Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
5990Sstevel@tonic-gate 		return (-1);
6000Sstevel@tonic-gate 	}
601766Scarlsonj 	if (unmount_cmd)
602766Scarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6050Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6060Sstevel@tonic-gate 
6071676Sjpk 	/*
6081676Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
6091676Sjpk 	 * of our zone's /export/home
6101676Sjpk 	 */
6111769Scarlsonj 	if (!unmount_cmd)
6121769Scarlsonj 		tsol_unmounts(zlogp, zone_name);
6131676Sjpk 
6140Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6150Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6160Sstevel@tonic-gate 		return (-1);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	/*
6190Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6200Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6210Sstevel@tonic-gate 	 */
6220Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6230Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6240Sstevel@tonic-gate 		error++;
6250Sstevel@tonic-gate 		goto out;
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6300Sstevel@tonic-gate 	 * should forcibly unmount.
6310Sstevel@tonic-gate 	 */
6320Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6330Sstevel@tonic-gate 	for (; /* ever */; ) {
6340Sstevel@tonic-gate 		uint_t newcount = 0;
6350Sstevel@tonic-gate 		boolean_t unmounted;
6360Sstevel@tonic-gate 		struct mnttab *mnp;
6370Sstevel@tonic-gate 		char *path;
6380Sstevel@tonic-gate 		uint_t i;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		mnts = NULL;
6410Sstevel@tonic-gate 		nmnt = 0;
6420Sstevel@tonic-gate 		/*
6430Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6440Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6450Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6460Sstevel@tonic-gate 		 * filesystems.
6470Sstevel@tonic-gate 		 */
6480Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6490Sstevel@tonic-gate 		    &nmnt) != 0) {
6500Sstevel@tonic-gate 			error++;
6510Sstevel@tonic-gate 			goto out;
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6540Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6550Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6560Sstevel@tonic-gate 			unmounted = B_FALSE;
6570Sstevel@tonic-gate 			/*
6580Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6590Sstevel@tonic-gate 			 *
6600Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6610Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6620Sstevel@tonic-gate 			 * and try a regular unmount.
6630Sstevel@tonic-gate 			 */
6640Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6650Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6660Sstevel@tonic-gate 					unmounted = B_TRUE;
6670Sstevel@tonic-gate 			}
6680Sstevel@tonic-gate 			/*
6690Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6700Sstevel@tonic-gate 			 */
6710Sstevel@tonic-gate 			if (stuck) {
6720Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6730Sstevel@tonic-gate 					unmounted = B_TRUE;
6740Sstevel@tonic-gate 					stuck = B_FALSE;
6750Sstevel@tonic-gate 				} else {
6760Sstevel@tonic-gate 					/*
6770Sstevel@tonic-gate 					 * The first failure indicates a
6780Sstevel@tonic-gate 					 * mount we won't be able to get
6790Sstevel@tonic-gate 					 * rid of automatically, so we
6800Sstevel@tonic-gate 					 * bail.
6810Sstevel@tonic-gate 					 */
6820Sstevel@tonic-gate 					error++;
6830Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6840Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
6850Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
6860Sstevel@tonic-gate 					goto out;
6870Sstevel@tonic-gate 				}
6880Sstevel@tonic-gate 			}
6890Sstevel@tonic-gate 			/*
6900Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
6910Sstevel@tonic-gate 			 */
6920Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
6930Sstevel@tonic-gate 				newcount++;
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		if (newcount == 0)
6980Sstevel@tonic-gate 			break;
6990Sstevel@tonic-gate 		if (newcount >= oldcount) {
7000Sstevel@tonic-gate 			/*
7010Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7020Sstevel@tonic-gate 			 * should start trying forced unmounts.
7030Sstevel@tonic-gate 			 */
7040Sstevel@tonic-gate 			stuck = B_TRUE;
7050Sstevel@tonic-gate 		}
7060Sstevel@tonic-gate 		oldcount = newcount;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		/*
7090Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7100Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7110Sstevel@tonic-gate 		 */
7120Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7130Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7140Sstevel@tonic-gate 			error++;
7150Sstevel@tonic-gate 			goto out;
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate out:
7200Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7210Sstevel@tonic-gate 	(void) fclose(mnttab);
7220Sstevel@tonic-gate 	return (error ? -1 : 0);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate static int
7260Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7270Sstevel@tonic-gate {
7280Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7290Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7360Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7370Sstevel@tonic-gate  * returns the exit status otherwise.
7380Sstevel@tonic-gate  *
7390Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7400Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7410Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7420Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7430Sstevel@tonic-gate  * remains the case.
7440Sstevel@tonic-gate  */
7450Sstevel@tonic-gate static int
7460Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate 	pid_t child_pid;
7490Sstevel@tonic-gate 	int child_status = 0;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7550Sstevel@tonic-gate 	child_pid = fork();
7560Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7570Sstevel@tonic-gate 	if (child_pid == -1) {
7580Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7590Sstevel@tonic-gate 		return (-1);
7600Sstevel@tonic-gate 	} else if (child_pid == 0) {
7610Sstevel@tonic-gate 		closefrom(0);
7621915Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7631915Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7641915Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7651915Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7660Sstevel@tonic-gate 		(void) execv(path, argv);
7670Sstevel@tonic-gate 		/*
7680Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7690Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7700Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7710Sstevel@tonic-gate 		 * accordingly.
7720Sstevel@tonic-gate 		 */
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7750Sstevel@tonic-gate 	} else {
7760Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7800Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7810Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7820Sstevel@tonic-gate 		return (-1);
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
7850Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
7860Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
7870Sstevel@tonic-gate 		return (-1);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
7900Sstevel@tonic-gate }
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate static int
7930Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
7960Sstevel@tonic-gate 	char *argv[4];
7970Sstevel@tonic-gate 	int status;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	/*
8000Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8010Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8020Sstevel@tonic-gate 	 */
8030Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8042712Snn35248 	    >= sizeof (cmdbuf)) {
8050Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8060Sstevel@tonic-gate 		return (-1);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	argv[0] = "fsck";
8100Sstevel@tonic-gate 	argv[1] = "-m";
8110Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
8120Sstevel@tonic-gate 	argv[3] = NULL;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8150Sstevel@tonic-gate 	if (status == 0 || status == -1)
8160Sstevel@tonic-gate 		return (status);
8170Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8180Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8190Sstevel@tonic-gate 	return (-1);
8200Sstevel@tonic-gate }
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate static int
8230Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8240Sstevel@tonic-gate     const char *special, const char *directory)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8270Sstevel@tonic-gate 	char *argv[6];
8280Sstevel@tonic-gate 	int status;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	/*
8310Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8320Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8330Sstevel@tonic-gate 	 */
8340Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8352712Snn35248 	    >= sizeof (cmdbuf)) {
8360Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8370Sstevel@tonic-gate 		return (-1);
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 	argv[0] = "mount";
8400Sstevel@tonic-gate 	if (opts[0] == '\0') {
8410Sstevel@tonic-gate 		argv[1] = (char *)special;
8420Sstevel@tonic-gate 		argv[2] = (char *)directory;
8430Sstevel@tonic-gate 		argv[3] = NULL;
8440Sstevel@tonic-gate 	} else {
8450Sstevel@tonic-gate 		argv[1] = "-o";
8460Sstevel@tonic-gate 		argv[2] = (char *)opts;
8470Sstevel@tonic-gate 		argv[3] = (char *)special;
8480Sstevel@tonic-gate 		argv[4] = (char *)directory;
8490Sstevel@tonic-gate 		argv[5] = NULL;
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8530Sstevel@tonic-gate 	if (status == 0 || status == -1)
8540Sstevel@tonic-gate 		return (status);
8550Sstevel@tonic-gate 	if (opts[0] == '\0')
8560Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8570Sstevel@tonic-gate 		    "failed with exit code %d",
8580Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8590Sstevel@tonic-gate 	else
8600Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8610Sstevel@tonic-gate 		    "failed with exit code %d",
8620Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8630Sstevel@tonic-gate 	return (-1);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate /*
8670Sstevel@tonic-gate  * Make sure if a given path exists, it is not a sym-link, and is a directory.
8680Sstevel@tonic-gate  */
8690Sstevel@tonic-gate static int
8700Sstevel@tonic-gate check_path(zlog_t *zlogp, const char *path)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 	struct stat statbuf;
8730Sstevel@tonic-gate 	char respath[MAXPATHLEN];
8740Sstevel@tonic-gate 	int res;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
8770Sstevel@tonic-gate 		if (errno == ENOENT)
8780Sstevel@tonic-gate 			return (0);
8790Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
8800Sstevel@tonic-gate 		return (-1);
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
8830Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
8840Sstevel@tonic-gate 		return (-1);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 	if (!S_ISDIR(statbuf.st_mode)) {
8871676Sjpk 		if (is_system_labeled() && S_ISREG(statbuf.st_mode)) {
8881676Sjpk 			/*
8891676Sjpk 			 * The need to mount readonly copies of
8901676Sjpk 			 * global zone /etc/ files is unique to
8911676Sjpk 			 * Trusted Extensions.
8921676Sjpk 			 * The check for /etc/ via strstr() is to
8931676Sjpk 			 * allow paths like $ZONEROOT/etc/passwd
8941676Sjpk 			 */
8951676Sjpk 			if (strstr(path, "/etc/") == NULL) {
8961676Sjpk 				zerror(zlogp, B_FALSE,
8971676Sjpk 				    "%s is not in /etc", path);
8981676Sjpk 				return (-1);
8991676Sjpk 			}
9001676Sjpk 		} else {
9011676Sjpk 			zerror(zlogp, B_FALSE, "%s is not a directory", path);
9021676Sjpk 			return (-1);
9031676Sjpk 		}
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9060Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9070Sstevel@tonic-gate 		return (-1);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 	respath[res] = '\0';
9100Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9110Sstevel@tonic-gate 		/*
9120Sstevel@tonic-gate 		 * We don't like ".."s and "."s throwing us off
9130Sstevel@tonic-gate 		 */
9140Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9150Sstevel@tonic-gate 		return (-1);
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 	return (0);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate  * Check every component of rootpath/relpath.  If any component fails (ie,
9220Sstevel@tonic-gate  * exists but isn't the canonical path to a directory), it is returned in
9230Sstevel@tonic-gate  * badpath, which is assumed to be at least of size MAXPATHLEN.
9240Sstevel@tonic-gate  *
9250Sstevel@tonic-gate  * Relpath must begin with '/'.
9260Sstevel@tonic-gate  */
9270Sstevel@tonic-gate static boolean_t
9280Sstevel@tonic-gate valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate 	char abspath[MAXPATHLEN], *slashp;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	/*
9330Sstevel@tonic-gate 	 * Make sure abspath has at least one '/' after its rootpath
9340Sstevel@tonic-gate 	 * component, and ends with '/'.
9350Sstevel@tonic-gate 	 */
9362712Snn35248 	if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) >=
9370Sstevel@tonic-gate 	    sizeof (abspath)) {
9380Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath,
9390Sstevel@tonic-gate 		    relpath);
9400Sstevel@tonic-gate 		return (B_FALSE);
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
9440Sstevel@tonic-gate 	assert(*slashp == '/');
9450Sstevel@tonic-gate 	do {
9460Sstevel@tonic-gate 		*slashp = '\0';
9470Sstevel@tonic-gate 		if (check_path(zlogp, abspath) != 0)
9480Sstevel@tonic-gate 			return (B_FALSE);
9490Sstevel@tonic-gate 		*slashp = '/';
9500Sstevel@tonic-gate 		slashp++;
9510Sstevel@tonic-gate 	} while ((slashp = strchr(slashp, '/')) != NULL);
9520Sstevel@tonic-gate 	return (B_TRUE);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate static int
9562712Snn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
9572712Snn35248 {
9582712Snn35248 	di_prof_t prof = arg;
9592712Snn35248 
9602712Snn35248 	if (name == NULL)
9612712Snn35248 		return (di_prof_add_dev(prof, match));
9622712Snn35248 	return (di_prof_add_map(prof, match, name));
9632712Snn35248 }
9642712Snn35248 
9652712Snn35248 static int
9662712Snn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
9672712Snn35248 {
9682712Snn35248 	di_prof_t prof = arg;
9692712Snn35248 
9702712Snn35248 	return (di_prof_add_symlink(prof, source, target));
9712712Snn35248 }
9722712Snn35248 
9732712Snn35248 /*
9742712Snn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
9752712Snn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
9762712Snn35248  * use these as a profile/filter to determine what exists in /dev.
9772712Snn35248  */
9782712Snn35248 static int
9792712Snn35248 mount_one_dev(zlog_t *zlogp, char *devpath)
9802712Snn35248 {
9812712Snn35248 	char			brand[MAXNAMELEN];
9822712Snn35248 	zone_dochandle_t	handle = NULL;
9832727Sedp 	brand_handle_t		bh = NULL;
9842712Snn35248 	struct zone_devtab	ztab;
9852712Snn35248 	di_prof_t		prof = NULL;
9862712Snn35248 	int			err;
9872712Snn35248 	int			retval = -1;
9882712Snn35248 
9892712Snn35248 	if (di_prof_init(devpath, &prof)) {
9902712Snn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
9912712Snn35248 		goto cleanup;
9922712Snn35248 	}
9932712Snn35248 
9942712Snn35248 	/* Get a handle to the brand info for this zone */
9952712Snn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
9962727Sedp 	    (bh = brand_open(brand)) == NULL) {
9972712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
9982712Snn35248 		goto cleanup;
9992712Snn35248 	}
10002712Snn35248 
10012727Sedp 	if (brand_platform_iter_devices(bh, zone_name,
10022712Snn35248 	    mount_one_dev_device_cb, prof) != 0) {
10032712Snn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
10042712Snn35248 		goto cleanup;
10052712Snn35248 	}
10062712Snn35248 
10072727Sedp 	if (brand_platform_iter_link(bh,
10082712Snn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
10092712Snn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
10102712Snn35248 		goto cleanup;
10112712Snn35248 	}
10122712Snn35248 
10132712Snn35248 	/* Add user-specified devices and directories */
10142712Snn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
10152712Snn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
10162712Snn35248 		goto cleanup;
10172712Snn35248 	}
10182712Snn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
10192712Snn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
10202712Snn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
10212712Snn35248 		goto cleanup;
10222712Snn35248 	}
10232712Snn35248 	if (err = zonecfg_setdevent(handle)) {
10242712Snn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
10252712Snn35248 		    zonecfg_strerror(err));
10262712Snn35248 		goto cleanup;
10272712Snn35248 	}
10282712Snn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
10292712Snn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
10302712Snn35248 			zerror(zlogp, B_TRUE, "failed to add "
10312712Snn35248 			    "user-specified device");
10322712Snn35248 			goto cleanup;
10332712Snn35248 		}
10342712Snn35248 	}
10352712Snn35248 	(void) zonecfg_enddevent(handle);
10362712Snn35248 
10372712Snn35248 	/* Send profile to kernel */
10382712Snn35248 	if (di_prof_commit(prof)) {
10392712Snn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
10402712Snn35248 		goto cleanup;
10412712Snn35248 	}
10422712Snn35248 
10432712Snn35248 	retval = 0;
10442712Snn35248 
10452712Snn35248 cleanup:
10462727Sedp 	if (bh != NULL)
10472727Sedp 		brand_close(bh);
10482712Snn35248 	if (handle)
10492712Snn35248 		zonecfg_fini_handle(handle);
10502712Snn35248 	if (prof)
10512712Snn35248 		di_prof_fini(prof);
10522712Snn35248 	return (retval);
10532712Snn35248 }
10542712Snn35248 
10552712Snn35248 static int
10560Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
10570Sstevel@tonic-gate {
10582712Snn35248 	char path[MAXPATHLEN];
10592712Snn35248 	char specpath[MAXPATHLEN];
10602712Snn35248 	char optstr[MAX_MNTOPT_STR];
10610Sstevel@tonic-gate 	zone_fsopt_t *optptr;
10622712Snn35248 	int rv;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) {
10650Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
10660Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
10670Sstevel@tonic-gate 		return (-1);
10680Sstevel@tonic-gate 	}
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
10710Sstevel@tonic-gate 	    DEFAULT_DIR_MODE) != 0)
10720Sstevel@tonic-gate 		return (-1);
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
10750Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
10780Sstevel@tonic-gate 		/*
10790Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1080766Scarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1081766Scarlsonj 		 * can be seen via the alternate zone's root.
10820Sstevel@tonic-gate 		 */
1083766Scarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1084766Scarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1085766Scarlsonj 		    sizeof (specpath)) {
1086766Scarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1087766Scarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1088766Scarlsonj 			return (-1);
1089766Scarlsonj 		}
1090766Scarlsonj 		if (zonecfg_in_alt_root())
1091766Scarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
10920Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1093766Scarlsonj 		    specpath, path) != 0) {
10940Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1095766Scarlsonj 			    specpath);
10960Sstevel@tonic-gate 			return (-1);
10970Sstevel@tonic-gate 		}
10980Sstevel@tonic-gate 		return (0);
10990Sstevel@tonic-gate 	}
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	/*
11020Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
11030Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
11040Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
11050Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
11060Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
11070Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
11080Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
11090Sstevel@tonic-gate 	 */
11100Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
11110Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
11120Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
11130Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
11140Sstevel@tonic-gate 		return (-1);
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	/*
1118766Scarlsonj 	 * If we're looking at an alternate root environment, then construct
1119766Scarlsonj 	 * read-only loopback mounts as necessary.  For all lofs mounts, make
1120766Scarlsonj 	 * sure that the 'special' entry points inside the alternate root.  (We
1121766Scarlsonj 	 * don't do this with other mounts, as devfs isn't in the alternate
1122766Scarlsonj 	 * root, and we need to assume the device environment is roughly the
1123766Scarlsonj 	 * same.)
1124766Scarlsonj 	 */
1125766Scarlsonj 	if (zonecfg_in_alt_root()) {
1126766Scarlsonj 		struct stat64 st;
1127766Scarlsonj 
1128766Scarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
1129*2772Scarlsonj 		    S_ISBLK(st.st_mode)) {
1130*2772Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1131*2772Scarlsonj 				return (-1);
1132*2772Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1133766Scarlsonj 			if (snprintf(specpath, sizeof (specpath), "%s%s",
1134766Scarlsonj 			    zonecfg_get_root(), fsptr->zone_fs_special) >=
1135766Scarlsonj 			    sizeof (specpath)) {
1136766Scarlsonj 				zerror(zlogp, B_FALSE, "cannot mount %s: path "
1137766Scarlsonj 				    "too long in alternate root",
1138766Scarlsonj 				    fsptr->zone_fs_special);
1139766Scarlsonj 				return (-1);
1140766Scarlsonj 			}
1141766Scarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
1142766Scarlsonj 			(void) strlcpy(fsptr->zone_fs_special, specpath,
1143766Scarlsonj 			    sizeof (fsptr->zone_fs_special));
1144766Scarlsonj 		}
1145766Scarlsonj 	}
1146766Scarlsonj 
1147766Scarlsonj 	/*
11480Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
11490Sstevel@tonic-gate 	 */
11500Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
11510Sstevel@tonic-gate 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0)
11520Sstevel@tonic-gate 		return (-1);
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	/*
11550Sstevel@tonic-gate 	 * Build up mount option string.
11560Sstevel@tonic-gate 	 */
11570Sstevel@tonic-gate 	optstr[0] = '\0';
11580Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
11590Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
11600Sstevel@tonic-gate 		    sizeof (optstr));
11610Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
11620Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
11630Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
11640Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
11650Sstevel@tonic-gate 			    sizeof (optstr));
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 	}
11682712Snn35248 
11692712Snn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
11702712Snn35248 	    fsptr->zone_fs_special, path)) != 0)
11712712Snn35248 		return (rv);
11722712Snn35248 
11732712Snn35248 	/*
11742712Snn35248 	 * The mount succeeded.  If this was not a mount of /dev then
11752712Snn35248 	 * we're done.
11762712Snn35248 	 */
11772712Snn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
11782712Snn35248 		return (0);
11792712Snn35248 
11802712Snn35248 	/*
11812712Snn35248 	 * We just mounted an instance of a /dev filesystem, so now we
11822712Snn35248 	 * need to configure it.
11832712Snn35248 	 */
11842712Snn35248 	return (mount_one_dev(zlogp, path));
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate static void
11880Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
11890Sstevel@tonic-gate {
11900Sstevel@tonic-gate 	uint_t i;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	if (fsarray == NULL)
11930Sstevel@tonic-gate 		return;
11940Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
11950Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
11960Sstevel@tonic-gate 	free(fsarray);
11970Sstevel@tonic-gate }
11980Sstevel@tonic-gate 
1199766Scarlsonj /*
12002653Svp157776  * This function initiates the creation of a small Solaris Environment for
12012653Svp157776  * scratch zone. The Environment creation process is split up into two
12022653Svp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
12032653Svp157776  * is done this way because:
12042653Svp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
12052653Svp157776  * 	We loopback mount zone's own /etc and /var into the root of the
12062653Svp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
12072653Svp157776  * 	need to delay the mount of /var till the zone's root gets populated.
12082653Svp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
12092653Svp157776  * 	build_mounted_post_var() which gets called only after the zone
12102653Svp157776  * 	specific filesystems are mounted.
1211766Scarlsonj  */
1212766Scarlsonj static boolean_t
12132653Svp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
12142653Svp157776     size_t rootlen, const char *zonepath)
1215766Scarlsonj {
1216766Scarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1217766Scarlsonj 	char luroot[MAXPATHLEN];
1218766Scarlsonj 	const char **cpp;
1219766Scarlsonj 	static const char *mkdirs[] = {
12202592Sdp 		"/system", "/system/contract", "/system/object", "/proc",
12212592Sdp 		"/dev", "/tmp", "/a", NULL
1222766Scarlsonj 	};
12232653Svp157776 	char *altstr;
1224766Scarlsonj 	FILE *fp;
1225766Scarlsonj 	uuid_t uuid;
1226766Scarlsonj 
12272712Snn35248 	assert(zone_isnative);
12282712Snn35248 
1229766Scarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
1230766Scarlsonj 	(void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
1231766Scarlsonj 	resolve_lofs(zlogp, luroot, sizeof (luroot));
1232766Scarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1233766Scarlsonj 	(void) symlink("./usr/bin", tmp);
1234766Scarlsonj 
1235766Scarlsonj 	/*
1236766Scarlsonj 	 * These are mostly special mount points; not handled here.  (See
1237766Scarlsonj 	 * zone_mount_early.)
1238766Scarlsonj 	 */
1239766Scarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1240766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1241766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1242766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1243766Scarlsonj 			return (B_FALSE);
1244766Scarlsonj 		}
1245766Scarlsonj 	}
12462653Svp157776 	/*
12472653Svp157776 	 * This is here to support lucopy.  If there's an instance of this same
12482653Svp157776 	 * zone on the current running system, then we mount its root up as
12492653Svp157776 	 * read-only inside the scratch zone.
12502653Svp157776 	 */
12512653Svp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
12522653Svp157776 	altstr = strdup(zonecfg_get_root());
12532653Svp157776 	if (altstr == NULL) {
12542653Svp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
12552653Svp157776 		return (B_FALSE);
12562653Svp157776 	}
12572653Svp157776 	zonecfg_set_root("");
12582653Svp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
12592653Svp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
12602653Svp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
12612653Svp157776 	    strcmp(fromdir, rootpath) != 0) {
12622653Svp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
12632653Svp157776 		if (mkdir(tmp, 0755) != 0) {
12642653Svp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
12652653Svp157776 			return (B_FALSE);
12662653Svp157776 		}
12672653Svp157776 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
12682653Svp157776 		    tmp) != 0) {
12692653Svp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
12702653Svp157776 			    fromdir);
12712653Svp157776 			return (B_FALSE);
12722653Svp157776 		}
12732653Svp157776 	}
12742653Svp157776 	zonecfg_set_root(altstr);
12752653Svp157776 	free(altstr);
12762653Svp157776 
12772653Svp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
12782653Svp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
12792653Svp157776 		return (B_FALSE);
12802653Svp157776 	}
12812653Svp157776 	(void) ftruncate(fileno(fp), 0);
12822653Svp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
12832653Svp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
12842653Svp157776 	}
12852653Svp157776 	zonecfg_close_scratch(fp);
12862653Svp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
12872653Svp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
12882653Svp157776 		return (B_FALSE);
12892653Svp157776 	(void) strlcpy(rootpath, tmp, rootlen);
12902653Svp157776 	return (B_TRUE);
12912653Svp157776 }
12922653Svp157776 
12932653Svp157776 
12942653Svp157776 static boolean_t
12952653Svp157776 build_mounted_post_var(zlog_t *zlogp, char *rootpath, const char *zonepath)
12962653Svp157776 {
12972653Svp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
12982653Svp157776 	char luroot[MAXPATHLEN];
12992653Svp157776 	const char **cpp;
13002653Svp157776 	static const char *localdirs[] = {
13012653Svp157776 		"/etc", "/var", NULL
13022653Svp157776 	};
13032653Svp157776 	static const char *loopdirs[] = {
13042653Svp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
13052653Svp157776 		"/usr", NULL
13062653Svp157776 	};
13072653Svp157776 	static const char *tmpdirs[] = {
13082653Svp157776 		"/tmp", "/var/run", NULL
13092653Svp157776 	};
13102653Svp157776 	struct stat st;
13112653Svp157776 
13122653Svp157776 	(void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
1313766Scarlsonj 
1314766Scarlsonj 	/*
1315766Scarlsonj 	 * These are mounted read-write from the zone undergoing upgrade.  We
1316766Scarlsonj 	 * must be careful not to 'leak' things from the main system into the
1317766Scarlsonj 	 * zone, and this accomplishes that goal.
1318766Scarlsonj 	 */
1319766Scarlsonj 	for (cpp = localdirs; *cpp != NULL; cpp++) {
1320766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1321766Scarlsonj 		(void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath,
1322766Scarlsonj 		    *cpp);
1323766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1324766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1325766Scarlsonj 			return (B_FALSE);
1326766Scarlsonj 		}
1327766Scarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) {
1328766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1329766Scarlsonj 			    *cpp);
1330766Scarlsonj 			return (B_FALSE);
1331766Scarlsonj 		}
1332766Scarlsonj 	}
1333766Scarlsonj 
1334766Scarlsonj 	/*
1335766Scarlsonj 	 * These are things mounted read-only from the running system because
1336766Scarlsonj 	 * they contain binaries that must match system.
1337766Scarlsonj 	 */
1338766Scarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1339766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1340766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1341766Scarlsonj 			if (errno != EEXIST) {
1342766Scarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1343766Scarlsonj 				return (B_FALSE);
1344766Scarlsonj 			}
1345766Scarlsonj 			if (lstat(tmp, &st) != 0) {
1346766Scarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1347766Scarlsonj 				return (B_FALSE);
1348766Scarlsonj 			}
1349766Scarlsonj 			/*
1350766Scarlsonj 			 * Ignore any non-directories encountered.  These are
1351766Scarlsonj 			 * things that have been converted into symlinks
1352766Scarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1353766Scarlsonj 			 * fixup.
1354766Scarlsonj 			 */
1355766Scarlsonj 			if (!S_ISDIR(st.st_mode))
1356766Scarlsonj 				continue;
1357766Scarlsonj 		}
1358766Scarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1359766Scarlsonj 		    tmp) != 0) {
1360766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1361766Scarlsonj 			    *cpp);
1362766Scarlsonj 			return (B_FALSE);
1363766Scarlsonj 		}
1364766Scarlsonj 	}
1365766Scarlsonj 
1366766Scarlsonj 	/*
1367766Scarlsonj 	 * These are things with tmpfs mounted inside.
1368766Scarlsonj 	 */
1369766Scarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1370766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1371766Scarlsonj 		if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
1372766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1373766Scarlsonj 			return (B_FALSE);
1374766Scarlsonj 		}
1375766Scarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1376766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1377766Scarlsonj 			return (B_FALSE);
1378766Scarlsonj 		}
1379766Scarlsonj 	}
1380766Scarlsonj 	return (B_TRUE);
1381766Scarlsonj }
1382766Scarlsonj 
13832712Snn35248 typedef struct plat_gmount_cb_data {
13842712Snn35248 	zlog_t			*pgcd_zlogp;
13852712Snn35248 	struct zone_fstab	**pgcd_fs_tab;
13862712Snn35248 	int			*pgcd_num_fs;
13872712Snn35248 } plat_gmount_cb_data_t;
13882712Snn35248 
13892712Snn35248 /*
13902712Snn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
13912712Snn35248  * through all global brand platform mounts.
13922712Snn35248  */
13932712Snn35248 int
13942712Snn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
13952712Snn35248     const char *fstype, const char *opt)
13962712Snn35248 {
13972712Snn35248 	plat_gmount_cb_data_t	*cp = data;
13982712Snn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
13992712Snn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
14002712Snn35248 	int			num_fs = *cp->pgcd_num_fs;
14012712Snn35248 	struct zone_fstab	*fsp, *tmp_ptr;
14022712Snn35248 
14032712Snn35248 	num_fs++;
14042712Snn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
14052712Snn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
14062712Snn35248 		return (-1);
14072712Snn35248 	}
14082712Snn35248 
14092712Snn35248 	fs_ptr = tmp_ptr;
14102712Snn35248 	fsp = &fs_ptr[num_fs - 1];
14112712Snn35248 
14122712Snn35248 	/* update the callback struct passed in */
14132712Snn35248 	*cp->pgcd_fs_tab = fs_ptr;
14142712Snn35248 	*cp->pgcd_num_fs = num_fs;
14152712Snn35248 
14162712Snn35248 	fsp->zone_fs_raw[0] = '\0';
14172712Snn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
14182712Snn35248 	    sizeof (fsp->zone_fs_special));
14192712Snn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
14202712Snn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
14212712Snn35248 	fsp->zone_fs_options = NULL;
14222712Snn35248 	if (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK) {
14232712Snn35248 		zerror(zlogp, B_FALSE, "error adding property");
14242712Snn35248 		return (-1);
14252712Snn35248 	}
14262712Snn35248 
14272712Snn35248 	return (0);
14282712Snn35248 }
14292712Snn35248 
14302712Snn35248 static int
14312712Snn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
14322712Snn35248     struct zone_fstab **fs_tabp, int *num_fsp)
14332712Snn35248 {
14342712Snn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
14352712Snn35248 	int num_fs;
14362712Snn35248 
14372712Snn35248 	num_fs = *num_fsp;
14382712Snn35248 	fs_ptr = *fs_tabp;
14392712Snn35248 
14402712Snn35248 	if (zonecfg_setipdent(handle) != Z_OK) {
14412712Snn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
14422712Snn35248 		return (-1);
14432712Snn35248 	}
14442712Snn35248 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
14452712Snn35248 		num_fs++;
14462712Snn35248 		if ((tmp_ptr = realloc(fs_ptr,
14472712Snn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
14482712Snn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
14492712Snn35248 			(void) zonecfg_endipdent(handle);
14502712Snn35248 			return (-1);
14512712Snn35248 		}
14522712Snn35248 
14532712Snn35248 		/* update the pointers passed in */
14542712Snn35248 		*fs_tabp = tmp_ptr;
14552712Snn35248 		*num_fsp = num_fs;
14562712Snn35248 
14572712Snn35248 		/*
14582712Snn35248 		 * IPDs logically only have a mount point; all other properties
14592712Snn35248 		 * are implied.
14602712Snn35248 		 */
14612712Snn35248 		fs_ptr = tmp_ptr;
14622712Snn35248 		fsp = &fs_ptr[num_fs - 1];
14632712Snn35248 		(void) strlcpy(fsp->zone_fs_dir,
14642712Snn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
14652712Snn35248 		fsp->zone_fs_special[0] = '\0';
14662712Snn35248 		fsp->zone_fs_raw[0] = '\0';
14672712Snn35248 		fsp->zone_fs_type[0] = '\0';
14682712Snn35248 		fsp->zone_fs_options = NULL;
14692712Snn35248 	}
14702712Snn35248 	(void) zonecfg_endipdent(handle);
14712712Snn35248 	return (0);
14722712Snn35248 }
14732712Snn35248 
14742712Snn35248 static int
14752712Snn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
14762712Snn35248     struct zone_fstab **fs_tabp, int *num_fsp, int mount_cmd)
14772712Snn35248 {
14782712Snn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
14792712Snn35248 	int num_fs;
14802712Snn35248 
14812712Snn35248 	num_fs = *num_fsp;
14822712Snn35248 	fs_ptr = *fs_tabp;
14832712Snn35248 
14842712Snn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
14852712Snn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
14862712Snn35248 		return (-1);
14872712Snn35248 	}
14882712Snn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
14892712Snn35248 		/*
14902712Snn35248 		 * ZFS filesystems will not be accessible under an alternate
14912712Snn35248 		 * root, since the pool will not be known.  Ignore them in this
14922712Snn35248 		 * case.
14932712Snn35248 		 */
14942712Snn35248 		if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
14952712Snn35248 			continue;
14962712Snn35248 
14972712Snn35248 		num_fs++;
14982712Snn35248 		if ((tmp_ptr = realloc(fs_ptr,
14992712Snn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
15002712Snn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
15012712Snn35248 			(void) zonecfg_endfsent(handle);
15022712Snn35248 			return (-1);
15032712Snn35248 		}
15042712Snn35248 		/* update the pointers passed in */
15052712Snn35248 		*fs_tabp = tmp_ptr;
15062712Snn35248 		*num_fsp = num_fs;
15072712Snn35248 
15082712Snn35248 		fs_ptr = tmp_ptr;
15092712Snn35248 		fsp = &fs_ptr[num_fs - 1];
15102712Snn35248 		(void) strlcpy(fsp->zone_fs_dir,
15112712Snn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
15122712Snn35248 		(void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special,
15132712Snn35248 		    sizeof (fsp->zone_fs_special));
15142712Snn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
15152712Snn35248 		    sizeof (fsp->zone_fs_raw));
15162712Snn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
15172712Snn35248 		    sizeof (fsp->zone_fs_type));
15182712Snn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
15192712Snn35248 	}
15202712Snn35248 	(void) zonecfg_endfsent(handle);
15212712Snn35248 	return (0);
15222712Snn35248 }
15232712Snn35248 
15240Sstevel@tonic-gate static int
1525766Scarlsonj mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd)
15260Sstevel@tonic-gate {
15272712Snn35248 	char rootpath[MAXPATHLEN];
15282712Snn35248 	char zonepath[MAXPATHLEN];
15292712Snn35248 	char brand[MAXNAMELEN];
15302712Snn35248 	int i, num_fs = 0;
15312712Snn35248 	struct zone_fstab *fs_ptr = NULL;
15320Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
15330Sstevel@tonic-gate 	zone_state_t zstate;
15342727Sedp 	brand_handle_t bh;
15352712Snn35248 	plat_gmount_cb_data_t cb;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1538766Scarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
15390Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1540766Scarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1541766Scarlsonj 		    zone_state_str(ZONE_STATE_READY),
1542766Scarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
15430Sstevel@tonic-gate 		goto bad;
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
15470Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
15480Sstevel@tonic-gate 		goto bad;
15490Sstevel@tonic-gate 	}
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
15520Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
15530Sstevel@tonic-gate 		goto bad;
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
15571645Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
15580Sstevel@tonic-gate 		goto bad;
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
15610Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
15620Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
15630Sstevel@tonic-gate 		goto bad;
15640Sstevel@tonic-gate 	}
15650Sstevel@tonic-gate 
15662712Snn35248 	/* Get a handle to the brand info for this zone */
15672712Snn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
15682727Sedp 	    (bh = brand_open(brand)) == NULL) {
15692712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
15702712Snn35248 		return (-1);
15712712Snn35248 	}
15722712Snn35248 
15732712Snn35248 	/*
15742712Snn35248 	 * Get the list of global filesystems to mount from the brand
15752712Snn35248 	 * configuration.
15762712Snn35248 	 */
15772712Snn35248 	cb.pgcd_zlogp = zlogp;
15782712Snn35248 	cb.pgcd_fs_tab = &fs_ptr;
15792712Snn35248 	cb.pgcd_num_fs = &num_fs;
15802727Sedp 	if (brand_platform_iter_gmounts(bh, zonepath,
15812712Snn35248 	    plat_gmount_cb, &cb) != 0) {
15822712Snn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
15832727Sedp 		brand_close(bh);
15842712Snn35248 		return (-1);
15852712Snn35248 	}
15862727Sedp 	brand_close(bh);
15872712Snn35248 
15880Sstevel@tonic-gate 	/*
15890Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
15900Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
15910Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
15920Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
15930Sstevel@tonic-gate 	 */
15942712Snn35248 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
15950Sstevel@tonic-gate 		goto bad;
15962712Snn35248 
15972712Snn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
15982712Snn35248 	    mount_cmd) != 0)
15992712Snn35248 		goto bad;
16002712Snn35248 
16010Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
16020Sstevel@tonic-gate 	handle = NULL;
16030Sstevel@tonic-gate 
1604766Scarlsonj 	/*
16052712Snn35248 	 * Normally when we mount a zone all the zone filesystems
16062712Snn35248 	 * get mounted relative to rootpath, which is usually
16072712Snn35248 	 * <zonepath>/root.  But when mounting a zone for administration
16082712Snn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
16092712Snn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
16102712Snn35248 	 * the zones filesystems there instead.
16112712Snn35248 	 *
16122712Snn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
16132712Snn35248 	 * also do some extra work to create directories and lofs mount
16142712Snn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
16152712Snn35248 	 *
16162712Snn35248 	 * This allows us to be able to enter the zone (now rooted at
16172712Snn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
16182712Snn35248 	 * global zone and have them upgrade the to-be-modified zone's
16192712Snn35248 	 * files mounted on /a.  (Which mirrors the existing standard
16202712Snn35248 	 * upgrade environment.)
16212712Snn35248 	 *
16222712Snn35248 	 * There is of course one catch.  When doing the upgrade
16232712Snn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
16242712Snn35248 	 * for the zone and we don't want to have any /dev filesystem
16252712Snn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
16262712Snn35248 	 * as a normal zone filesystem by default we'll try to mount
16272712Snn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
16282712Snn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
16292712Snn35248 	 *
16302712Snn35248 	 * All this work is done in three phases:
16312653Svp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
16322653Svp157776 	 *   2) Mount the required filesystems as per the zone configuration.
16332653Svp157776 	 *   3) Set up the rest of the scratch zone environment
16342653Svp157776 	 *	(build_mounted_post_var()).
1635766Scarlsonj 	 */
1636766Scarlsonj 	if (mount_cmd &&
16372653Svp157776 	    !build_mounted_pre_var(zlogp,
16382653Svp157776 	    rootpath, sizeof (rootpath), zonepath))
1639766Scarlsonj 		goto bad;
1640766Scarlsonj 
16410Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
16422712Snn35248 
16430Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
16442712Snn35248 		if (mount_cmd &&
16452712Snn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
16462712Snn35248 			size_t slen = strlen(rootpath) - 2;
16472712Snn35248 
16482712Snn35248 			/*
16492712Snn35248 			 * By default we'll try to mount /dev as /a/dev
16502712Snn35248 			 * but /dev is special and always goes at the top
16512712Snn35248 			 * so strip the trailing '/a' from the rootpath.
16522712Snn35248 			 */
16532712Snn35248 			assert(zone_isnative);
16542712Snn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
16552712Snn35248 			rootpath[slen] = '\0';
16562712Snn35248 			if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
16572712Snn35248 				goto bad;
16582712Snn35248 			rootpath[slen] = '/';
16592712Snn35248 			continue;
16602712Snn35248 		}
16610Sstevel@tonic-gate 		if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
16620Sstevel@tonic-gate 			goto bad;
16630Sstevel@tonic-gate 	}
16642653Svp157776 	if (mount_cmd &&
16652653Svp157776 	    !build_mounted_post_var(zlogp, rootpath, zonepath))
16662653Svp157776 		goto bad;
16671676Sjpk 
16681676Sjpk 	/*
16691676Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
16701676Sjpk 	 */
16711769Scarlsonj 	if (!mount_cmd && tsol_mounts(zlogp, zone_name, rootpath) != 0)
16721676Sjpk 		goto bad;
16731676Sjpk 
16740Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	/*
16770Sstevel@tonic-gate 	 * Everything looks fine.
16780Sstevel@tonic-gate 	 */
16790Sstevel@tonic-gate 	return (0);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate bad:
16820Sstevel@tonic-gate 	if (handle != NULL)
16830Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
16840Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
16850Sstevel@tonic-gate 	return (-1);
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
16890Sstevel@tonic-gate static int
16900Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
16910Sstevel@tonic-gate {
16920Sstevel@tonic-gate 	int prefixlen;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
16950Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
16960Sstevel@tonic-gate 		return (1);
16970Sstevel@tonic-gate 	while (prefixlen > 0) {
16980Sstevel@tonic-gate 		if (prefixlen >= 8) {
16990Sstevel@tonic-gate 			*maskstr++ = 0xFF;
17000Sstevel@tonic-gate 			prefixlen -= 8;
17010Sstevel@tonic-gate 			continue;
17020Sstevel@tonic-gate 		}
17030Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
17040Sstevel@tonic-gate 		prefixlen--;
17050Sstevel@tonic-gate 	}
17060Sstevel@tonic-gate 	return (0);
17070Sstevel@tonic-gate }
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate /*
17100Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
17110Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
17120Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
17130Sstevel@tonic-gate  *
17140Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
17150Sstevel@tonic-gate  */
17160Sstevel@tonic-gate static int
17170Sstevel@tonic-gate unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
17180Sstevel@tonic-gate {
17190Sstevel@tonic-gate 	struct lifnum lifn;
17200Sstevel@tonic-gate 	struct lifconf lifc;
17210Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
17220Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
17230Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
17240Sstevel@tonic-gate 	uint_t bufsize;
17250Sstevel@tonic-gate 	char *buf = NULL;
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
17280Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
17290Sstevel@tonic-gate 		ret_code = -1;
17300Sstevel@tonic-gate 		goto bad;
17310Sstevel@tonic-gate 	}
17320Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
17330Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
17340Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
17350Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
17360Sstevel@tonic-gate 		    "could not determine number of interfaces");
17370Sstevel@tonic-gate 		ret_code = -1;
17380Sstevel@tonic-gate 		goto bad;
17390Sstevel@tonic-gate 	}
17400Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
17410Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
17420Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
17430Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
17440Sstevel@tonic-gate 		ret_code = -1;
17450Sstevel@tonic-gate 		goto bad;
17460Sstevel@tonic-gate 	}
17470Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
17480Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
17490Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
17500Sstevel@tonic-gate 	lifc.lifc_buf = buf;
17510Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
17520Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get configured interfaces");
17530Sstevel@tonic-gate 		ret_code = -1;
17540Sstevel@tonic-gate 		goto bad;
17550Sstevel@tonic-gate 	}
17560Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
17570Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
17580Sstevel@tonic-gate 		(void) close(s);
17590Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
17600Sstevel@tonic-gate 		    0) {
17610Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
17620Sstevel@tonic-gate 			    lifrl.lifr_name);
17630Sstevel@tonic-gate 			ret_code = -1;
17640Sstevel@tonic-gate 			continue;
17650Sstevel@tonic-gate 		}
17660Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
17670Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
17680Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
17690Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
17700Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
17710Sstevel@tonic-gate 			    "%s: could not determine zone interface belongs to",
17720Sstevel@tonic-gate 			    lifrl.lifr_name);
17730Sstevel@tonic-gate 			ret_code = -1;
17740Sstevel@tonic-gate 			continue;
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
17770Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
17780Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
17790Sstevel@tonic-gate 				    "%s: could not remove interface",
17800Sstevel@tonic-gate 				    lifrl.lifr_name);
17810Sstevel@tonic-gate 				ret_code = -1;
17820Sstevel@tonic-gate 				continue;
17830Sstevel@tonic-gate 			}
17840Sstevel@tonic-gate 		}
17850Sstevel@tonic-gate 	}
17860Sstevel@tonic-gate bad:
17870Sstevel@tonic-gate 	if (s > 0)
17880Sstevel@tonic-gate 		(void) close(s);
17890Sstevel@tonic-gate 	if (buf)
17900Sstevel@tonic-gate 		free(buf);
17910Sstevel@tonic-gate 	return (ret_code);
17920Sstevel@tonic-gate }
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate static union	sockunion {
17950Sstevel@tonic-gate 	struct	sockaddr sa;
17960Sstevel@tonic-gate 	struct	sockaddr_in sin;
17970Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
17980Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
17990Sstevel@tonic-gate } so_dst, so_ifp;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate static struct {
18020Sstevel@tonic-gate 	struct	rt_msghdr hdr;
18030Sstevel@tonic-gate 	char	space[512];
18040Sstevel@tonic-gate } rtmsg;
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate static int
18070Sstevel@tonic-gate salen(struct sockaddr *sa)
18080Sstevel@tonic-gate {
18090Sstevel@tonic-gate 	switch (sa->sa_family) {
18100Sstevel@tonic-gate 	case AF_INET:
18110Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
18120Sstevel@tonic-gate 	case AF_LINK:
18130Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
18140Sstevel@tonic-gate 	case AF_INET6:
18150Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
18160Sstevel@tonic-gate 	default:
18170Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
18180Sstevel@tonic-gate 	}
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
18220Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate /*
18250Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
18260Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
18270Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
18280Sstevel@tonic-gate  *
18290Sstevel@tonic-gate  * This is done using black router socket magic.
18300Sstevel@tonic-gate  *
18310Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
18320Sstevel@tonic-gate  *
18330Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
18340Sstevel@tonic-gate  * of this might be a useful RFE.
18350Sstevel@tonic-gate  */
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate static char *
18380Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
18390Sstevel@tonic-gate {
18400Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
18410Sstevel@tonic-gate 	pid_t pid;
18420Sstevel@tonic-gate 	int s, rlen, l, i;
18430Sstevel@tonic-gate 	char *cp = rtmsg.space;
18440Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
18450Sstevel@tonic-gate 	struct sockaddr *sa;
18460Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	answer[0] = '\0';
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	pid = getpid();
18510Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
18520Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
18530Sstevel@tonic-gate 		return (NULL);
18540Sstevel@tonic-gate 	}
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
18570Sstevel@tonic-gate 		struct sockaddr_in *sin4;
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
18600Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
18610Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
18620Sstevel@tonic-gate 	} else {
18630Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
18660Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
18670Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
18680Sstevel@tonic-gate 	}
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
18730Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
18740Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
18750Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
18760Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
18770Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
18800Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
18810Sstevel@tonic-gate 	cp += l;
18820Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
18830Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
18840Sstevel@tonic-gate 	cp += l;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
18890Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
18900Sstevel@tonic-gate 		return (NULL);
18910Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
18920Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
18930Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
18940Sstevel@tonic-gate 		return (NULL);
18950Sstevel@tonic-gate 	}
18960Sstevel@tonic-gate 	do {
18970Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
18980Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
18990Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
19000Sstevel@tonic-gate 	if (l < 0) {
19010Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
19020Sstevel@tonic-gate 		return (NULL);
19030Sstevel@tonic-gate 	}
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
19060Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
19070Sstevel@tonic-gate 		    "routing message version %d not understood",
19080Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
19090Sstevel@tonic-gate 		return (NULL);
19100Sstevel@tonic-gate 	}
19110Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
19120Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
19130Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
19140Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
19150Sstevel@tonic-gate 		return (NULL);
19160Sstevel@tonic-gate 	}
19170Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
19180Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
19190Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
19200Sstevel@tonic-gate 		return (NULL);
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
19230Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "interface not found");
19240Sstevel@tonic-gate 		return (NULL);
19250Sstevel@tonic-gate 	}
19260Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
19270Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
19280Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
19290Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
19300Sstevel@tonic-gate 		if (i != RTA_IFP) {
19310Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
19320Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
19330Sstevel@tonic-gate 			continue;
19340Sstevel@tonic-gate 		}
19350Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
19360Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
19370Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
19380Sstevel@tonic-gate 		break;
19390Sstevel@tonic-gate 	}
19400Sstevel@tonic-gate 	if (ifp == NULL) {
19410Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "interface could not be determined");
19420Sstevel@tonic-gate 		return (NULL);
19430Sstevel@tonic-gate 	}
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	/*
19460Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
19470Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
19480Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
19490Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
19500Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
19510Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
19520Sstevel@tonic-gate 	 */
19530Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
19540Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
19550Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
19560Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
19570Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
19580Sstevel@tonic-gate 	if (i < 0) {
19590Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
19600Sstevel@tonic-gate 		    "%s: could not determine the zone interface belongs to",
19610Sstevel@tonic-gate 		    lifr->lifr_name);
19620Sstevel@tonic-gate 		return (NULL);
19630Sstevel@tonic-gate 	}
19640Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
19650Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
19660Sstevel@tonic-gate 		    lifr->lifr_zoneid);
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	if (strlen(answer) > 0)
19690Sstevel@tonic-gate 		return (answer);
19700Sstevel@tonic-gate 	return (NULL);
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate typedef struct mcast_rtmsg_s {
19740Sstevel@tonic-gate 	struct rt_msghdr	m_rtm;
19750Sstevel@tonic-gate 	union {
19760Sstevel@tonic-gate 		struct {
19770Sstevel@tonic-gate 			struct sockaddr_in	m_dst;
19780Sstevel@tonic-gate 			struct sockaddr_in	m_gw;
19790Sstevel@tonic-gate 			struct sockaddr_in	m_netmask;
19800Sstevel@tonic-gate 		} m_v4;
19810Sstevel@tonic-gate 		struct {
19820Sstevel@tonic-gate 			struct sockaddr_in6	m_dst;
19830Sstevel@tonic-gate 			struct sockaddr_in6	m_gw;
19840Sstevel@tonic-gate 			struct sockaddr_in6	m_netmask;
19850Sstevel@tonic-gate 		} m_v6;
19860Sstevel@tonic-gate 	} m_u;
19870Sstevel@tonic-gate } mcast_rtmsg_t;
19880Sstevel@tonic-gate #define	m_dst4		m_u.m_v4.m_dst
19890Sstevel@tonic-gate #define	m_dst6		m_u.m_v6.m_dst
19900Sstevel@tonic-gate #define	m_gw4		m_u.m_v4.m_gw
19910Sstevel@tonic-gate #define	m_gw6		m_u.m_v6.m_gw
19920Sstevel@tonic-gate #define	m_netmask4	m_u.m_v4.m_netmask
19930Sstevel@tonic-gate #define	m_netmask6	m_u.m_v6.m_netmask
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate /*
19960Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
19970Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
19980Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
19990Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
20000Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
20010Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
20020Sstevel@tonic-gate  *
20030Sstevel@tonic-gate  * A default interface route for multicast is created on the first IPv4 and
20040Sstevel@tonic-gate  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
20050Sstevel@tonic-gate  * This should really be done in the init scripts if we ever allow zones to
20060Sstevel@tonic-gate  * modify the routing tables.
20070Sstevel@tonic-gate  *
20080Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
20090Sstevel@tonic-gate  * down whatever we set up and return an error.
20100Sstevel@tonic-gate  */
20110Sstevel@tonic-gate static int
20120Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
20130Sstevel@tonic-gate     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
20140Sstevel@tonic-gate     boolean_t *mcast_rt_v6_setp)
20150Sstevel@tonic-gate {
20160Sstevel@tonic-gate 	struct lifreq lifr;
20170Sstevel@tonic-gate 	struct sockaddr_in netmask4;
20180Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
20190Sstevel@tonic-gate 	struct in_addr in4;
20200Sstevel@tonic-gate 	struct in6_addr in6;
20210Sstevel@tonic-gate 	sa_family_t af;
20220Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
20230Sstevel@tonic-gate 	mcast_rtmsg_t mcast_rtmsg;
20240Sstevel@tonic-gate 	int s;
20250Sstevel@tonic-gate 	int rs;
20260Sstevel@tonic-gate 	int rlen;
20270Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
20280Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
20290Sstevel@tonic-gate 	int res;
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
20320Sstevel@tonic-gate 	if (res != Z_OK) {
20330Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
20340Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
20350Sstevel@tonic-gate 		return (-1);
20360Sstevel@tonic-gate 	}
20370Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
20380Sstevel@tonic-gate 	if (af == AF_INET)
20390Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
20400Sstevel@tonic-gate 	else
20410Sstevel@tonic-gate 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
20440Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
20450Sstevel@tonic-gate 		return (-1);
20460Sstevel@tonic-gate 	}
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
20490Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
20500Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
20512611Svp157776 		/*
20522611Svp157776 		 * Here, we know that the interface can't be brought up.
20532611Svp157776 		 * A similar warning message was already printed out to
20542611Svp157776 		 * the console by zoneadm(1M) so instead we log the
20552611Svp157776 		 * message to syslog and continue.
20562611Svp157776 		 */
20572611Svp157776 		zerror(&logsys, B_TRUE, "WARNING: skipping interface "
20582611Svp157776 		    "'%s' which may not be present/plumbed in the "
20592611Svp157776 		    "global zone.", lifr.lifr_name);
20600Sstevel@tonic-gate 		(void) close(s);
20612611Svp157776 		return (Z_OK);
20620Sstevel@tonic-gate 	}
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
20650Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20660Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
20670Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
20680Sstevel@tonic-gate 		goto bad;
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
20720Sstevel@tonic-gate 	if (af == AF_INET)
20730Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
20760Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
20770Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not place interface into zone",
20780Sstevel@tonic-gate 		    lifr.lifr_name);
20790Sstevel@tonic-gate 		goto bad;
20800Sstevel@tonic-gate 	}
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
20830Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
20840Sstevel@tonic-gate 	} else {
20850Sstevel@tonic-gate 		if (af == AF_INET) {
20860Sstevel@tonic-gate 			/*
20870Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
20880Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
20890Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
20900Sstevel@tonic-gate 			 * being able to determine it is a common failure,
20910Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
20920Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
20930Sstevel@tonic-gate 			 * printed after the rest of the interface's
20940Sstevel@tonic-gate 			 * parameters have been configured.
20950Sstevel@tonic-gate 			 */
20960Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
20970Sstevel@tonic-gate 			if (slashp != NULL) {
20980Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
20990Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
21000Sstevel@tonic-gate 					*slashp = '/';
21010Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
21020Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
21030Sstevel@tonic-gate 					    lifr.lifr_name,
21040Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
21050Sstevel@tonic-gate 					goto bad;
21060Sstevel@tonic-gate 				}
21070Sstevel@tonic-gate 				got_netmask = B_TRUE;
21080Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
21090Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
21100Sstevel@tonic-gate 				got_netmask = B_TRUE;
21110Sstevel@tonic-gate 			}
21120Sstevel@tonic-gate 			if (got_netmask) {
21130Sstevel@tonic-gate 				netmask4.sin_family = af;
21140Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
21150Sstevel@tonic-gate 				    sizeof (netmask4));
21160Sstevel@tonic-gate 			}
21170Sstevel@tonic-gate 		} else {
21180Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
21190Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
21200Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
21210Sstevel@tonic-gate 				*slashp = '/';
21220Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
21230Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
21240Sstevel@tonic-gate 				    lifr.lifr_name,
21250Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
21260Sstevel@tonic-gate 				goto bad;
21270Sstevel@tonic-gate 			}
21280Sstevel@tonic-gate 			got_netmask = B_TRUE;
21290Sstevel@tonic-gate 			netmask6.sin6_family = af;
21300Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
21310Sstevel@tonic-gate 			    sizeof (netmask6));
21320Sstevel@tonic-gate 		}
21330Sstevel@tonic-gate 		if (got_netmask &&
21340Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
21350Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
21360Sstevel@tonic-gate 			    lifr.lifr_name);
21370Sstevel@tonic-gate 			goto bad;
21380Sstevel@tonic-gate 		}
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 		/*
21410Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
21420Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
21430Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
21440Sstevel@tonic-gate 		 */
21450Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
21460Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
21470Sstevel@tonic-gate 			    lifr.lifr_name);
21480Sstevel@tonic-gate 			goto bad;
21490Sstevel@tonic-gate 		}
21500Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
21510Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
21520Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
21530Sstevel@tonic-gate 			    lifr.lifr_name);
21540Sstevel@tonic-gate 			goto bad;
21550Sstevel@tonic-gate 		}
21560Sstevel@tonic-gate 	}
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
21590Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
21600Sstevel@tonic-gate 		    lifr.lifr_name);
21610Sstevel@tonic-gate 		goto bad;
21620Sstevel@tonic-gate 	}
21630Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
21640Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
21650Sstevel@tonic-gate 		int save_errno = errno;
21660Sstevel@tonic-gate 		char *zone_using;
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 		/*
21690Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
21700Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
21710Sstevel@tonic-gate 		 * then call a function to determine which zone is already
21720Sstevel@tonic-gate 		 * using that address.
21730Sstevel@tonic-gate 		 */
21740Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
21750Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
21760Sstevel@tonic-gate 			    "%s: could not bring interface up", lifr.lifr_name);
21770Sstevel@tonic-gate 			goto bad;
21780Sstevel@tonic-gate 		}
21790Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
21800Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
21810Sstevel@tonic-gate 			    lifr.lifr_name);
21820Sstevel@tonic-gate 			goto bad;
21830Sstevel@tonic-gate 		}
21840Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
21850Sstevel@tonic-gate 		errno = save_errno;
21860Sstevel@tonic-gate 		if (zone_using == NULL)
21870Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
21880Sstevel@tonic-gate 			    "%s: could not bring interface up", lifr.lifr_name);
21890Sstevel@tonic-gate 		else
21900Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not bring interface "
21910Sstevel@tonic-gate 			    "up: address in use by zone '%s'", lifr.lifr_name,
21920Sstevel@tonic-gate 			    zone_using);
21930Sstevel@tonic-gate 		goto bad;
21940Sstevel@tonic-gate 	}
21950Sstevel@tonic-gate 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
21960Sstevel@tonic-gate 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
21970Sstevel@tonic-gate 	    (af == AF_INET6 &&
21980Sstevel@tonic-gate 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
21990Sstevel@tonic-gate 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
22000Sstevel@tonic-gate 		if (rs < 0) {
22010Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not create "
22020Sstevel@tonic-gate 			    "routing socket", lifr.lifr_name);
22030Sstevel@tonic-gate 			goto bad;
22040Sstevel@tonic-gate 		}
22050Sstevel@tonic-gate 		(void) shutdown(rs, 0);
22060Sstevel@tonic-gate 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
22070Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
22080Sstevel@tonic-gate 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
22090Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
22100Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
22110Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
22120Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
22130Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_addrs =
22140Sstevel@tonic-gate 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
22150Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
22160Sstevel@tonic-gate 		if (af == AF_INET) {
22170Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
22180Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
22190Sstevel@tonic-gate 			    htonl(INADDR_UNSPEC_GROUP);
22200Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
22210Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_addr = in4;
22220Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
22230Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
22240Sstevel@tonic-gate 			    htonl(IN_CLASSD_NET);
22250Sstevel@tonic-gate 		} else {
22260Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
22270Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
22280Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
22290Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_addr = in6;
22300Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
22310Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
22320Sstevel@tonic-gate 		}
22330Sstevel@tonic-gate 		rlen = write(rs, (char *)&mcast_rtmsg,
22340Sstevel@tonic-gate 		    mcast_rtmsg.m_rtm.rtm_msglen);
22352611Svp157776 		/*
22362611Svp157776 		 * The write to the multicast socket will fail if the
22372611Svp157776 		 * interface belongs to a failed IPMP group. This is a
22382611Svp157776 		 * non-fatal error and the zone will continue booting.
22392611Svp157776 		 * While the zone is running, if any interface in the
22402611Svp157776 		 * failed IPMP group recovers, the zone will fallback to
22412611Svp157776 		 * using that interface.
22422611Svp157776 		 */
22430Sstevel@tonic-gate 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
22440Sstevel@tonic-gate 			if (rlen < 0) {
22452611Svp157776 				zerror(zlogp, B_TRUE, "WARNING: interface "
22462611Svp157776 				    "'%s' not available as default for "
22472611Svp157776 				    "multicast.", lifr.lifr_name);
22480Sstevel@tonic-gate 			} else {
22492611Svp157776 				zerror(zlogp, B_FALSE, "WARNING: interface "
22502611Svp157776 				    "'%s' not available as default for "
22512611Svp157776 				    "multicast; routing socket returned "
22522611Svp157776 				    "unexpected %d bytes.",
22532611Svp157776 				    lifr.lifr_name, rlen);
22540Sstevel@tonic-gate 			}
22550Sstevel@tonic-gate 		} else {
22562611Svp157776 
22572611Svp157776 			if (af == AF_INET) {
22582611Svp157776 				*mcast_rt_v4_setp = B_TRUE;
22592611Svp157776 			} else {
22602611Svp157776 				*mcast_rt_v6_setp = B_TRUE;
22612611Svp157776 			}
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 		(void) close(rs);
22640Sstevel@tonic-gate 	}
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	if (!got_netmask) {
22670Sstevel@tonic-gate 		/*
22680Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
22690Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
22700Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
22710Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
22720Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
22730Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
22740Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
22750Sstevel@tonic-gate 		 * available. We warn the user here that something like this
22760Sstevel@tonic-gate 		 * has happened and we're just running with a default and
22770Sstevel@tonic-gate 		 * possible incorrect netmask.
22780Sstevel@tonic-gate 		 */
22790Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
22800Sstevel@tonic-gate 		void  *addr;
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 		if (af == AF_INET)
22830Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
22840Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
22850Sstevel@tonic-gate 		else
22860Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
22870Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
22900Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
22910Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
22920Sstevel@tonic-gate 			goto bad;
22930Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
22940Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
22950Sstevel@tonic-gate 		    "%s; using default of %s.",
22960Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
22970Sstevel@tonic-gate 	}
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 	(void) close(s);
23000Sstevel@tonic-gate 	return (Z_OK);
23010Sstevel@tonic-gate bad:
23020Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
23030Sstevel@tonic-gate 	(void) close(s);
23040Sstevel@tonic-gate 	return (-1);
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate /*
23080Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
23090Sstevel@tonic-gate  * An IPv4 loopback interface is set up "for free", modeling the global system.
23100Sstevel@tonic-gate  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
23110Sstevel@tonic-gate  * address is set up as well.
23120Sstevel@tonic-gate  *
23130Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
23140Sstevel@tonic-gate  * whatever we set up, and return an error.
23150Sstevel@tonic-gate  */
23160Sstevel@tonic-gate static int
23170Sstevel@tonic-gate configure_network_interfaces(zlog_t *zlogp)
23180Sstevel@tonic-gate {
23190Sstevel@tonic-gate 	zone_dochandle_t handle;
23200Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
23210Sstevel@tonic-gate 	boolean_t saw_v6 = B_FALSE;
23220Sstevel@tonic-gate 	boolean_t mcast_rt_v4_set = B_FALSE;
23230Sstevel@tonic-gate 	boolean_t mcast_rt_v6_set = B_FALSE;
23240Sstevel@tonic-gate 	zoneid_t zoneid;
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
23270Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
23280Sstevel@tonic-gate 		return (-1);
23290Sstevel@tonic-gate 	}
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
23320Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
23330Sstevel@tonic-gate 		return (-1);
23340Sstevel@tonic-gate 	}
23350Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
23360Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
23370Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
23380Sstevel@tonic-gate 		return (-1);
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
23410Sstevel@tonic-gate 		for (;;) {
23420Sstevel@tonic-gate 			struct in6_addr in6;
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
23450Sstevel@tonic-gate 				break;
23460Sstevel@tonic-gate 			if (configure_one_interface(zlogp, zoneid,
23470Sstevel@tonic-gate 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
23480Sstevel@tonic-gate 			    Z_OK) {
23490Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
23500Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
23510Sstevel@tonic-gate 				return (-1);
23520Sstevel@tonic-gate 			}
23530Sstevel@tonic-gate 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
23540Sstevel@tonic-gate 			    &in6) == 1)
23550Sstevel@tonic-gate 				saw_v6 = B_TRUE;
23560Sstevel@tonic-gate 		}
23570Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
23580Sstevel@tonic-gate 	}
23590Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
23600Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
23610Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
23620Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
23630Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
23640Sstevel@tonic-gate 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
23650Sstevel@tonic-gate 	    != Z_OK) {
23660Sstevel@tonic-gate 		return (-1);
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate 	if (saw_v6) {
23690Sstevel@tonic-gate 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
23700Sstevel@tonic-gate 		    sizeof (loopback_iftab.zone_nwif_address));
23710Sstevel@tonic-gate 		if (configure_one_interface(zlogp, zoneid,
23720Sstevel@tonic-gate 		    &loopback_iftab, NULL, NULL) != Z_OK) {
23730Sstevel@tonic-gate 			return (-1);
23740Sstevel@tonic-gate 		}
23750Sstevel@tonic-gate 	}
23760Sstevel@tonic-gate 	return (0);
23770Sstevel@tonic-gate }
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate static int
23800Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
23810Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
23820Sstevel@tonic-gate {
23830Sstevel@tonic-gate 	int fd;
23840Sstevel@tonic-gate 	struct strioctl ioc;
23850Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
23860Sstevel@tonic-gate 	int error;
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	conn.ac_local = *local;
23890Sstevel@tonic-gate 	conn.ac_remote = *remote;
23900Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
23910Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
23920Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
23950Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
23960Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
23970Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
24000Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
24010Sstevel@tonic-gate 		return (-1);
24020Sstevel@tonic-gate 	}
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
24050Sstevel@tonic-gate 	(void) close(fd);
24060Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
24070Sstevel@tonic-gate 		return (0);
24080Sstevel@tonic-gate 	return (-1);
24090Sstevel@tonic-gate }
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate static int
24120Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
24130Sstevel@tonic-gate {
24140Sstevel@tonic-gate 	struct sockaddr_storage l, r;
24150Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
24160Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
24170Sstevel@tonic-gate 	int error;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	/*
24200Sstevel@tonic-gate 	 * Abort IPv4 connections.
24210Sstevel@tonic-gate 	 */
24220Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
24230Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
24240Sstevel@tonic-gate 	local->sin_family = AF_INET;
24250Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
24260Sstevel@tonic-gate 	local->sin_port = 0;
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
24290Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
24300Sstevel@tonic-gate 	remote->sin_family = AF_INET;
24310Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
24320Sstevel@tonic-gate 	remote->sin_port = 0;
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
24350Sstevel@tonic-gate 		return (error);
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	/*
24380Sstevel@tonic-gate 	 * Abort IPv6 connections.
24390Sstevel@tonic-gate 	 */
24400Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
24410Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
24420Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
24430Sstevel@tonic-gate 	local6->sin6_port = 0;
24440Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
24470Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
24480Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
24490Sstevel@tonic-gate 	remote6->sin6_port = 0;
24500Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
24530Sstevel@tonic-gate 		return (error);
24540Sstevel@tonic-gate 	return (0);
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate static int
24581645Scomay get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd)
24591645Scomay {
24601645Scomay 	int error = -1;
24611645Scomay 	zone_dochandle_t handle;
24621645Scomay 	char *privname = NULL;
24631645Scomay 
24641645Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
24651645Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24661645Scomay 		return (-1);
24671645Scomay 	}
24681645Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24691645Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
24701645Scomay 		zonecfg_fini_handle(handle);
24711645Scomay 		return (-1);
24721645Scomay 	}
24731645Scomay 
24742712Snn35248 	if (mount_cmd) {
24752712Snn35248 		if (zonecfg_default_privset(privs) == Z_OK)
24762712Snn35248 			return (0);
24772712Snn35248 		zerror(zlogp, B_FALSE,
24782712Snn35248 		    "failed to determine the zone's default privilege set");
24792712Snn35248 		zonecfg_fini_handle(handle);
24802712Snn35248 		return (-1);
24812712Snn35248 	}
24822712Snn35248 
24831645Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
24841645Scomay 	case Z_OK:
24851645Scomay 		error = 0;
24861645Scomay 		break;
24871645Scomay 	case Z_PRIV_PROHIBITED:
24881645Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
24891645Scomay 		    "within the zone's privilege set", privname);
24901645Scomay 		break;
24911645Scomay 	case Z_PRIV_REQUIRED:
24921645Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
24931645Scomay 		    "from the zone's privilege set", privname);
24941645Scomay 		break;
24951645Scomay 	case Z_PRIV_UNKNOWN:
24961645Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
24971645Scomay 		    "in the zone's privilege set", privname);
24981645Scomay 		break;
24991645Scomay 	default:
25001645Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
25011645Scomay 		    "privilege set");
25021645Scomay 		break;
25031645Scomay 	}
25041645Scomay 
25051645Scomay 	free(privname);
25061645Scomay 	zonecfg_fini_handle(handle);
25071645Scomay 	return (error);
25081645Scomay }
25091645Scomay 
25101645Scomay static int
25110Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
25120Sstevel@tonic-gate {
25130Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
25140Sstevel@tonic-gate 	char *nvl_packed = NULL;
25150Sstevel@tonic-gate 	size_t nvl_size = 0;
25160Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
25170Sstevel@tonic-gate 	int rctlcount = 0;
25180Sstevel@tonic-gate 	int error = -1;
25190Sstevel@tonic-gate 	zone_dochandle_t handle;
25200Sstevel@tonic-gate 	struct zone_rctltab rctltab;
25210Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	*bufp = NULL;
25240Sstevel@tonic-gate 	*bufsizep = 0;
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
25270Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
25280Sstevel@tonic-gate 		return (-1);
25290Sstevel@tonic-gate 	}
25300Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
25310Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
25320Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
25330Sstevel@tonic-gate 		return (-1);
25340Sstevel@tonic-gate 	}
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
25370Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
25380Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
25390Sstevel@tonic-gate 		goto out;
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
25430Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
25440Sstevel@tonic-gate 		goto out;
25450Sstevel@tonic-gate 	}
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
25480Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
25490Sstevel@tonic-gate 		goto out;
25500Sstevel@tonic-gate 	}
25510Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
25520Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
25530Sstevel@tonic-gate 		uint_t i, count;
25540Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
25570Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
25580Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
25590Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
25600Sstevel@tonic-gate 			continue;
25610Sstevel@tonic-gate 		}
25620Sstevel@tonic-gate 		count = 0;
25630Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
25640Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
25650Sstevel@tonic-gate 			count++;
25660Sstevel@tonic-gate 		}
25670Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
25680Sstevel@tonic-gate 			continue;	/* Nothing to free */
25690Sstevel@tonic-gate 		}
25700Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
25710Sstevel@tonic-gate 			goto out;
25720Sstevel@tonic-gate 		i = 0;
25730Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
25740Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
25750Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
25760Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
25770Sstevel@tonic-gate 				    "nvlist_alloc");
25780Sstevel@tonic-gate 				goto out;
25790Sstevel@tonic-gate 			}
25800Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
25810Sstevel@tonic-gate 			    != Z_OK) {
25820Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
25830Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
25840Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
25850Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
25860Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
25870Sstevel@tonic-gate 				goto out;
25880Sstevel@tonic-gate 			}
25890Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
25900Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
25910Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
25920Sstevel@tonic-gate 				    "valid value for rctl '%s'",
25930Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
25940Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
25950Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
25960Sstevel@tonic-gate 				    name);
25970Sstevel@tonic-gate 				goto out;
25980Sstevel@tonic-gate 			}
25990Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
26001645Scomay 			    rctlblk_get_privilege(rctlblk)) != 0) {
26010Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
26020Sstevel@tonic-gate 				    "nvlist_add_uint64");
26030Sstevel@tonic-gate 				goto out;
26040Sstevel@tonic-gate 			}
26050Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
26061645Scomay 			    rctlblk_get_value(rctlblk)) != 0) {
26070Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
26080Sstevel@tonic-gate 				    "nvlist_add_uint64");
26090Sstevel@tonic-gate 				goto out;
26100Sstevel@tonic-gate 			}
26110Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
26120Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
26130Sstevel@tonic-gate 			    != 0) {
26140Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
26150Sstevel@tonic-gate 				    "nvlist_add_uint64");
26160Sstevel@tonic-gate 				goto out;
26170Sstevel@tonic-gate 			}
26180Sstevel@tonic-gate 		}
26190Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
26200Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
26210Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
26220Sstevel@tonic-gate 		    != 0) {
26230Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
26240Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
26250Sstevel@tonic-gate 			goto out;
26260Sstevel@tonic-gate 		}
26270Sstevel@tonic-gate 		for (i = 0; i < count; i++)
26280Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
26290Sstevel@tonic-gate 		free(nvlv);
26300Sstevel@tonic-gate 		nvlv = NULL;
26310Sstevel@tonic-gate 		rctlcount++;
26320Sstevel@tonic-gate 	}
26330Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	if (rctlcount == 0) {
26360Sstevel@tonic-gate 		error = 0;
26370Sstevel@tonic-gate 		goto out;
26380Sstevel@tonic-gate 	}
26390Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
26400Sstevel@tonic-gate 	    != 0) {
26410Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
26420Sstevel@tonic-gate 		goto out;
26430Sstevel@tonic-gate 	}
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	error = 0;
26460Sstevel@tonic-gate 	*bufp = nvl_packed;
26470Sstevel@tonic-gate 	*bufsizep = nvl_size;
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate out:
26500Sstevel@tonic-gate 	free(rctlblk);
26510Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
26520Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
26530Sstevel@tonic-gate 		free(nvl_packed);
26540Sstevel@tonic-gate 	if (nvl != NULL)
26550Sstevel@tonic-gate 		nvlist_free(nvl);
26560Sstevel@tonic-gate 	if (nvlv != NULL)
26570Sstevel@tonic-gate 		free(nvlv);
26580Sstevel@tonic-gate 	if (handle != NULL)
26590Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
26600Sstevel@tonic-gate 	return (error);
26610Sstevel@tonic-gate }
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate static int
26640Sstevel@tonic-gate get_zone_pool(zlog_t *zlogp, char *poolbuf, size_t bufsz)
26650Sstevel@tonic-gate {
26660Sstevel@tonic-gate 	zone_dochandle_t handle;
26670Sstevel@tonic-gate 	int error;
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
26700Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
26711645Scomay 		return (Z_NOMEM);
26720Sstevel@tonic-gate 	}
26731645Scomay 	error = zonecfg_get_snapshot_handle(zone_name, handle);
26741645Scomay 	if (error != Z_OK) {
26750Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
26760Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
26771645Scomay 		return (error);
26780Sstevel@tonic-gate 	}
26790Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolbuf, bufsz);
26800Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
26810Sstevel@tonic-gate 	return (error);
26820Sstevel@tonic-gate }
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate static int
2685789Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
2686789Sahrens {
2687789Sahrens 	zone_dochandle_t handle;
2688789Sahrens 	struct zone_dstab dstab;
2689789Sahrens 	size_t total, offset, len;
2690789Sahrens 	int error = -1;
2691789Sahrens 	char *str;
2692789Sahrens 
2693789Sahrens 	*bufp = NULL;
2694789Sahrens 	*bufsizep = 0;
2695789Sahrens 
2696789Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
2697789Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2698789Sahrens 		return (-1);
2699789Sahrens 	}
2700789Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2701789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
2702789Sahrens 		zonecfg_fini_handle(handle);
2703789Sahrens 		return (-1);
2704789Sahrens 	}
2705789Sahrens 
2706789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2707789Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
2708789Sahrens 		goto out;
2709789Sahrens 	}
2710789Sahrens 
2711789Sahrens 	total = 0;
2712789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
2713789Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
2714789Sahrens 	(void) zonecfg_enddsent(handle);
2715789Sahrens 
2716789Sahrens 	if (total == 0) {
2717789Sahrens 		error = 0;
2718789Sahrens 		goto out;
2719789Sahrens 	}
2720789Sahrens 
2721789Sahrens 	if ((str = malloc(total)) == NULL) {
2722789Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
2723789Sahrens 		goto out;
2724789Sahrens 	}
2725789Sahrens 
2726789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2727789Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
2728789Sahrens 		goto out;
2729789Sahrens 	}
2730789Sahrens 	offset = 0;
2731789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
2732789Sahrens 		len = strlen(dstab.zone_dataset_name);
2733789Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
2734789Sahrens 		    sizeof (dstab.zone_dataset_name) - offset);
2735789Sahrens 		offset += len;
2736789Sahrens 		if (offset != total - 1)
2737789Sahrens 			str[offset++] = ',';
2738789Sahrens 	}
2739789Sahrens 	(void) zonecfg_enddsent(handle);
2740789Sahrens 
2741789Sahrens 	error = 0;
2742789Sahrens 	*bufp = str;
2743789Sahrens 	*bufsizep = total;
2744789Sahrens 
2745789Sahrens out:
2746789Sahrens 	if (error != 0 && str != NULL)
2747789Sahrens 		free(str);
2748789Sahrens 	if (handle != NULL)
2749789Sahrens 		zonecfg_fini_handle(handle);
2750789Sahrens 
2751789Sahrens 	return (error);
2752789Sahrens }
2753789Sahrens 
2754789Sahrens static int
2755789Sahrens validate_datasets(zlog_t *zlogp)
2756789Sahrens {
2757789Sahrens 	zone_dochandle_t handle;
2758789Sahrens 	struct zone_dstab dstab;
2759789Sahrens 	zfs_handle_t *zhp;
27602082Seschrock 	libzfs_handle_t *hdl;
2761789Sahrens 
2762789Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
2763789Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2764789Sahrens 		return (-1);
2765789Sahrens 	}
2766789Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2767789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
2768789Sahrens 		zonecfg_fini_handle(handle);
2769789Sahrens 		return (-1);
2770789Sahrens 	}
2771789Sahrens 
2772789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
2773789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
2774789Sahrens 		zonecfg_fini_handle(handle);
2775789Sahrens 		return (-1);
2776789Sahrens 	}
2777789Sahrens 
27782082Seschrock 	if ((hdl = libzfs_init()) == NULL) {
27792082Seschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
27802082Seschrock 		zonecfg_fini_handle(handle);
27812082Seschrock 		return (-1);
27822082Seschrock 	}
2783789Sahrens 
2784789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
2785789Sahrens 
27862082Seschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
2787789Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
2788789Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
2789789Sahrens 			    dstab.zone_dataset_name);
2790789Sahrens 			zonecfg_fini_handle(handle);
27912082Seschrock 			libzfs_fini(hdl);
2792789Sahrens 			return (-1);
2793789Sahrens 		}
2794789Sahrens 
2795789Sahrens 		/*
2796789Sahrens 		 * Automatically set the 'zoned' property.  We check the value
2797789Sahrens 		 * first because we'll get EPERM if it is already set.
2798789Sahrens 		 */
2799789Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
28002676Seschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
28012676Seschrock 		    "on") != 0) {
2802789Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
2803789Sahrens 			    "property for ZFS dataset '%s'\n",
2804789Sahrens 			    dstab.zone_dataset_name);
2805789Sahrens 			zonecfg_fini_handle(handle);
2806789Sahrens 			zfs_close(zhp);
28072082Seschrock 			libzfs_fini(hdl);
2808789Sahrens 			return (-1);
2809789Sahrens 		}
2810789Sahrens 
2811789Sahrens 		zfs_close(zhp);
2812789Sahrens 	}
2813789Sahrens 	(void) zonecfg_enddsent(handle);
2814789Sahrens 
2815789Sahrens 	zonecfg_fini_handle(handle);
28162082Seschrock 	libzfs_fini(hdl);
2817789Sahrens 
2818789Sahrens 	return (0);
2819789Sahrens }
2820789Sahrens 
2821789Sahrens static int
28220Sstevel@tonic-gate bind_to_pool(zlog_t *zlogp, zoneid_t zoneid)
28230Sstevel@tonic-gate {
28240Sstevel@tonic-gate 	pool_conf_t *poolconf;
28250Sstevel@tonic-gate 	pool_t *pool;
28260Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
28270Sstevel@tonic-gate 	int status;
28280Sstevel@tonic-gate 	int error;
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	/*
28310Sstevel@tonic-gate 	 * Find the pool mentioned in the zone configuration, and bind to it.
28320Sstevel@tonic-gate 	 */
28330Sstevel@tonic-gate 	error = get_zone_pool(zlogp, poolname, sizeof (poolname));
28340Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
28350Sstevel@tonic-gate 		/*
28360Sstevel@tonic-gate 		 * The property is not set on the zone, so the pool
28370Sstevel@tonic-gate 		 * should be bound to the default pool.  But that's
28380Sstevel@tonic-gate 		 * already done by the kernel, so we can just return.
28390Sstevel@tonic-gate 		 */
28400Sstevel@tonic-gate 		return (0);
28410Sstevel@tonic-gate 	}
28420Sstevel@tonic-gate 	if (error != Z_OK) {
28430Sstevel@tonic-gate 		/*
28440Sstevel@tonic-gate 		 * Not an error, even though it shouldn't be happening.
28450Sstevel@tonic-gate 		 */
28460Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
28470Sstevel@tonic-gate 		    "WARNING: unable to retrieve default pool.");
28480Sstevel@tonic-gate 		return (0);
28490Sstevel@tonic-gate 	}
28500Sstevel@tonic-gate 	/*
28510Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
28520Sstevel@tonic-gate 	 */
28530Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
28540Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: pools facility not active; "
28550Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'.", poolname);
28560Sstevel@tonic-gate 		return (0);
28570Sstevel@tonic-gate 	}
28580Sstevel@tonic-gate 	/*
28590Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
28600Sstevel@tonic-gate 	 * exist.
28610Sstevel@tonic-gate 	 */
28620Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
28630Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "pool_conf_alloc");
28640Sstevel@tonic-gate 		return (-1);
28650Sstevel@tonic-gate 	}
28660Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
28670Sstevel@tonic-gate 	    PO_SUCCESS) {
28680Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "pool_conf_open");
28690Sstevel@tonic-gate 		pool_conf_free(poolconf);
28700Sstevel@tonic-gate 		return (-1);
28710Sstevel@tonic-gate 	}
28720Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
28730Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
28740Sstevel@tonic-gate 	pool_conf_free(poolconf);
28750Sstevel@tonic-gate 	if (pool == NULL) {
28760Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: pool '%s' not found; "
28770Sstevel@tonic-gate 		    "using default pool.", poolname);
28780Sstevel@tonic-gate 		return (0);
28790Sstevel@tonic-gate 	}
28800Sstevel@tonic-gate 	/*
28810Sstevel@tonic-gate 	 * Bind the zone to the pool.
28820Sstevel@tonic-gate 	 */
28830Sstevel@tonic-gate 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
28840Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: unable to bind to pool '%s'; "
28850Sstevel@tonic-gate 		    "using default pool.", poolname);
28860Sstevel@tonic-gate 	}
28870Sstevel@tonic-gate 	return (0);
28880Sstevel@tonic-gate }
28890Sstevel@tonic-gate 
28901676Sjpk /*
28911676Sjpk  * Mount lower level home directories into/from current zone
28921676Sjpk  * Share exported directories specified in dfstab for zone
28931676Sjpk  */
28941676Sjpk static int
28951676Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
28961676Sjpk {
28971676Sjpk 	zoneid_t *zids = NULL;
28981676Sjpk 	priv_set_t *zid_privs;
28991676Sjpk 	const priv_impl_info_t *ip = NULL;
29001676Sjpk 	uint_t nzents_saved;
29011676Sjpk 	uint_t nzents;
29021676Sjpk 	int i;
29031676Sjpk 	char readonly[] = "ro";
29041676Sjpk 	struct zone_fstab lower_fstab;
29051676Sjpk 	char *argv[4];
29061676Sjpk 
29071676Sjpk 	if (!is_system_labeled())
29081676Sjpk 		return (0);
29091676Sjpk 
29101676Sjpk 	if (zid_label == NULL) {
29111676Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
29121676Sjpk 		if (zid_label == NULL)
29131676Sjpk 			return (-1);
29141676Sjpk 	}
29151676Sjpk 
29161676Sjpk 	/* Make sure our zone has an /export/home dir */
29171676Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
29181676Sjpk 	    DEFAULT_DIR_MODE);
29191676Sjpk 
29201676Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
29211676Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
29221676Sjpk 	    sizeof (lower_fstab.zone_fs_type));
29231676Sjpk 	lower_fstab.zone_fs_options = NULL;
29241676Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
29251676Sjpk 
29261676Sjpk 	/*
29271676Sjpk 	 * Get the list of zones from the kernel
29281676Sjpk 	 */
29291676Sjpk 	if (zone_list(NULL, &nzents) != 0) {
29301676Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
29311676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
29321676Sjpk 		return (-1);
29331676Sjpk 	}
29341676Sjpk again:
29351676Sjpk 	if (nzents == 0) {
29361676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
29371676Sjpk 		return (-1);
29381676Sjpk 	}
29391676Sjpk 
29401676Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
29411676Sjpk 	if (zids == NULL) {
29422267Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
29431676Sjpk 		return (-1);
29441676Sjpk 	}
29451676Sjpk 	nzents_saved = nzents;
29461676Sjpk 
29471676Sjpk 	if (zone_list(zids, &nzents) != 0) {
29481676Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
29491676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
29501676Sjpk 		free(zids);
29511676Sjpk 		return (-1);
29521676Sjpk 	}
29531676Sjpk 	if (nzents != nzents_saved) {
29541676Sjpk 		/* list changed, try again */
29551676Sjpk 		free(zids);
29561676Sjpk 		goto again;
29571676Sjpk 	}
29581676Sjpk 
29591676Sjpk 	ip = getprivimplinfo();
29601676Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
29611676Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
29621676Sjpk 		zonecfg_free_fs_option_list(
29631676Sjpk 		    lower_fstab.zone_fs_options);
29641676Sjpk 		free(zids);
29651676Sjpk 		return (-1);
29661676Sjpk 	}
29671676Sjpk 
29681676Sjpk 	for (i = 0; i < nzents; i++) {
29691676Sjpk 		char zid_name[ZONENAME_MAX];
29701676Sjpk 		zone_state_t zid_state;
29711676Sjpk 		char zid_rpath[MAXPATHLEN];
29721676Sjpk 		struct stat stat_buf;
29731676Sjpk 
29741676Sjpk 		if (zids[i] == GLOBAL_ZONEID)
29751676Sjpk 			continue;
29761676Sjpk 
29771676Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
29781676Sjpk 			continue;
29791676Sjpk 
29801676Sjpk 		/*
29811676Sjpk 		 * Do special setup for the zone we are booting
29821676Sjpk 		 */
29831676Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
29841676Sjpk 			struct zone_fstab autofs_fstab;
29851676Sjpk 			char map_path[MAXPATHLEN];
29861676Sjpk 			int fd;
29871676Sjpk 
29881676Sjpk 			/*
29891676Sjpk 			 * Create auto_home_<zone> map for this zone
29902712Snn35248 			 * in the global zone. The non-global zone entry
29911676Sjpk 			 * will be created by automount when the zone
29921676Sjpk 			 * is booted.
29931676Sjpk 			 */
29941676Sjpk 
29951676Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
29961676Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
29971676Sjpk 
29981676Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
29991676Sjpk 			    "/zone/%s/home", zid_name);
30001676Sjpk 
30011676Sjpk 			(void) snprintf(map_path, sizeof (map_path),
30021676Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
30031676Sjpk 			/*
30041676Sjpk 			 * If the map file doesn't exist create a template
30051676Sjpk 			 */
30061676Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
30071676Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
30081676Sjpk 				int len;
30091676Sjpk 				char map_rec[MAXPATHLEN];
30101676Sjpk 
30111676Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
30121676Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
30131676Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
30141676Sjpk 				(void) write(fd, map_rec, len);
30151676Sjpk 				(void) close(fd);
30161676Sjpk 			}
30171676Sjpk 
30181676Sjpk 			/*
30191676Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
30201676Sjpk 			 * If it's already of type autofs, then
30211676Sjpk 			 * don't mount it again.
30221676Sjpk 			 */
30231676Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
30241676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
30251676Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
30261676Sjpk 
30271676Sjpk 				(void) make_one_dir(zlogp, "",
30281676Sjpk 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE);
30291676Sjpk 
30301676Sjpk 				/*
30311676Sjpk 				 * Mount will fail if automounter has already
30321676Sjpk 				 * processed the auto_home_<zonename> map
30331676Sjpk 				 */
30341676Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
30351676Sjpk 				    autofs_fstab.zone_fs_special,
30361676Sjpk 				    autofs_fstab.zone_fs_dir);
30371676Sjpk 			}
30381676Sjpk 			continue;
30391676Sjpk 		}
30401676Sjpk 
30411676Sjpk 
30421676Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
30431769Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
30441769Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
30451676Sjpk 			/* Skip over zones without mounted filesystems */
30461676Sjpk 			continue;
30471676Sjpk 
30481676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
30491676Sjpk 		    sizeof (m_label_t)) < 0)
30501676Sjpk 			/* Skip over zones with unspecified label */
30511676Sjpk 			continue;
30521676Sjpk 
30531676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
30541676Sjpk 		    sizeof (zid_rpath)) == -1)
30551676Sjpk 			/* Skip over zones with bad path */
30561676Sjpk 			continue;
30571676Sjpk 
30581676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
30591676Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
30601676Sjpk 			/* Skip over zones with bad privs */
30611676Sjpk 			continue;
30621676Sjpk 
30631676Sjpk 		/*
30641676Sjpk 		 * Reading down is valid according to our label model
30651676Sjpk 		 * but some customers want to disable it because it
30661676Sjpk 		 * allows execute down and other possible attacks.
30671676Sjpk 		 * Therefore, we restrict this feature to zones that
30681676Sjpk 		 * have the NET_MAC_AWARE privilege which is required
30691676Sjpk 		 * for NFS read-down semantics.
30701676Sjpk 		 */
30711676Sjpk 		if ((bldominates(zlabel, zid_label)) &&
30721676Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
30731676Sjpk 			/*
30741676Sjpk 			 * Our zone dominates this one.
30751676Sjpk 			 * Create a lofs mount from lower zone's /export/home
30761676Sjpk 			 */
30771676Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
30781676Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
30791676Sjpk 
30801676Sjpk 			/*
30811676Sjpk 			 * If the target is already an LOFS mount
30821676Sjpk 			 * then don't do it again.
30831676Sjpk 			 */
30841676Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
30851676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
30861676Sjpk 
30871676Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
30881676Sjpk 				    MAXPATHLEN, "%s/export",
30891676Sjpk 				    zid_rpath) > MAXPATHLEN)
30901676Sjpk 					continue;
30911676Sjpk 
30921676Sjpk 				/*
30931676Sjpk 				 * Make sure the lower-level home exists
30941676Sjpk 				 */
30951676Sjpk 				if (make_one_dir(zlogp,
30961676Sjpk 				    lower_fstab.zone_fs_special,
30971676Sjpk 				    "/home", DEFAULT_DIR_MODE) != 0)
30981676Sjpk 					continue;
30991676Sjpk 
31001676Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
31011676Sjpk 				    "/home", MAXPATHLEN);
31021676Sjpk 
31031676Sjpk 				/*
31041676Sjpk 				 * Mount can fail because the lower-level
31051676Sjpk 				 * zone may have already done a mount up.
31061676Sjpk 				 */
31071676Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
31081676Sjpk 			}
31091676Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
31101676Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
31111676Sjpk 			/*
31121676Sjpk 			 * This zone dominates our zone.
31131676Sjpk 			 * Create a lofs mount from our zone's /export/home
31141676Sjpk 			 */
31151676Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
31161676Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
31171676Sjpk 			    zone_name) > MAXPATHLEN)
31181676Sjpk 				continue;
31191676Sjpk 
31201676Sjpk 			/*
31211676Sjpk 			 * If the target is already an LOFS mount
31221676Sjpk 			 * then don't do it again.
31231676Sjpk 			 */
31241676Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
31251676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
31261676Sjpk 
31271676Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
31281676Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
31291676Sjpk 
31301676Sjpk 				/*
31311676Sjpk 				 * Mount can fail because the higher-level
31321676Sjpk 				 * zone may have already done a mount down.
31331676Sjpk 				 */
31341676Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
31351676Sjpk 			}
31361676Sjpk 		}
31371676Sjpk 	}
31381676Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
31391676Sjpk 	priv_freeset(zid_privs);
31401676Sjpk 	free(zids);
31411676Sjpk 
31421676Sjpk 	/*
31431676Sjpk 	 * Now share any exported directories from this zone.
31441676Sjpk 	 * Each zone can have its own dfstab.
31451676Sjpk 	 */
31461676Sjpk 
31471676Sjpk 	argv[0] = "zoneshare";
31481676Sjpk 	argv[1] = "-z";
31491676Sjpk 	argv[2] = zone_name;
31501676Sjpk 	argv[3] = NULL;
31511676Sjpk 
31521676Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
31531676Sjpk 	/* Don't check for errors since they don't affect the zone */
31541676Sjpk 
31551676Sjpk 	return (0);
31561676Sjpk }
31571676Sjpk 
31581676Sjpk /*
31591676Sjpk  * Unmount lofs mounts from higher level zones
31601676Sjpk  * Unshare nfs exported directories
31611676Sjpk  */
31621676Sjpk static void
31631676Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
31641676Sjpk {
31651676Sjpk 	zoneid_t *zids = NULL;
31661676Sjpk 	uint_t nzents_saved;
31671676Sjpk 	uint_t nzents;
31681676Sjpk 	int i;
31691676Sjpk 	char *argv[4];
31701676Sjpk 	char path[MAXPATHLEN];
31711676Sjpk 
31721676Sjpk 	if (!is_system_labeled())
31731676Sjpk 		return;
31741676Sjpk 
31751676Sjpk 	/*
31761676Sjpk 	 * Get the list of zones from the kernel
31771676Sjpk 	 */
31781676Sjpk 	if (zone_list(NULL, &nzents) != 0) {
31791676Sjpk 		return;
31801676Sjpk 	}
31811676Sjpk 
31821676Sjpk 	if (zid_label == NULL) {
31831676Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
31841676Sjpk 		if (zid_label == NULL)
31851676Sjpk 			return;
31861676Sjpk 	}
31871676Sjpk 
31881676Sjpk again:
31891676Sjpk 	if (nzents == 0)
31901676Sjpk 		return;
31911676Sjpk 
31921676Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
31931676Sjpk 	if (zids == NULL) {
31942267Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
31951676Sjpk 		return;
31961676Sjpk 	}
31971676Sjpk 	nzents_saved = nzents;
31981676Sjpk 
31991676Sjpk 	if (zone_list(zids, &nzents) != 0) {
32001676Sjpk 		free(zids);
32011676Sjpk 		return;
32021676Sjpk 	}
32031676Sjpk 	if (nzents != nzents_saved) {
32041676Sjpk 		/* list changed, try again */
32051676Sjpk 		free(zids);
32061676Sjpk 		goto again;
32071676Sjpk 	}
32081676Sjpk 
32091676Sjpk 	for (i = 0; i < nzents; i++) {
32101676Sjpk 		char zid_name[ZONENAME_MAX];
32111676Sjpk 		zone_state_t zid_state;
32121676Sjpk 		char zid_rpath[MAXPATHLEN];
32131676Sjpk 
32141676Sjpk 		if (zids[i] == GLOBAL_ZONEID)
32151676Sjpk 			continue;
32161676Sjpk 
32171676Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
32181676Sjpk 			continue;
32191676Sjpk 
32201676Sjpk 		/*
32211676Sjpk 		 * Skip the zone we are halting
32221676Sjpk 		 */
32231676Sjpk 		if (strcmp(zid_name, zone_name) == 0)
32241676Sjpk 			continue;
32251676Sjpk 
32261676Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
32271676Sjpk 		    sizeof (zid_state)) < 0) ||
32281676Sjpk 		    (zid_state < ZONE_IS_READY))
32291676Sjpk 			/* Skip over zones without mounted filesystems */
32301676Sjpk 			continue;
32311676Sjpk 
32321676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
32331676Sjpk 		    sizeof (m_label_t)) < 0)
32341676Sjpk 			/* Skip over zones with unspecified label */
32351676Sjpk 			continue;
32361676Sjpk 
32371676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
32381676Sjpk 		    sizeof (zid_rpath)) == -1)
32391676Sjpk 			/* Skip over zones with bad path */
32401676Sjpk 			continue;
32411676Sjpk 
32421676Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
32431676Sjpk 			/*
32441676Sjpk 			 * This zone dominates our zone.
32451676Sjpk 			 * Unmount the lofs mount of our zone's /export/home
32461676Sjpk 			 */
32471676Sjpk 
32481676Sjpk 			if (snprintf(path, MAXPATHLEN,
32491676Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
32501676Sjpk 			    zone_name) > MAXPATHLEN)
32511676Sjpk 				continue;
32521676Sjpk 
32531676Sjpk 			/* Skip over mount failures */
32541676Sjpk 			(void) umount(path);
32551676Sjpk 		}
32561676Sjpk 	}
32571676Sjpk 	free(zids);
32581676Sjpk 
32591676Sjpk 	/*
32601676Sjpk 	 * Unmount global zone autofs trigger for this zone
32611676Sjpk 	 */
32621676Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
32631676Sjpk 	/* Skip over mount failures */
32641676Sjpk 	(void) umount(path);
32651676Sjpk 
32661676Sjpk 	/*
32671676Sjpk 	 * Next unshare any exported directories from this zone.
32681676Sjpk 	 */
32691676Sjpk 
32701676Sjpk 	argv[0] = "zoneunshare";
32711676Sjpk 	argv[1] = "-z";
32721676Sjpk 	argv[2] = zone_name;
32731676Sjpk 	argv[3] = NULL;
32741676Sjpk 
32751676Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
32761676Sjpk 	/* Don't check for errors since they don't affect the zone */
32771676Sjpk 
32781676Sjpk 	/*
32791676Sjpk 	 * Finally, deallocate any devices in the zone.
32801676Sjpk 	 */
32811676Sjpk 
32821676Sjpk 	argv[0] = "deallocate";
32831676Sjpk 	argv[1] = "-Isz";
32841676Sjpk 	argv[2] = zone_name;
32851676Sjpk 	argv[3] = NULL;
32861676Sjpk 
32871676Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
32881676Sjpk 	/* Don't check for errors since they don't affect the zone */
32891676Sjpk }
32901676Sjpk 
32911676Sjpk /*
32921676Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
32931676Sjpk  * this zone.
32941676Sjpk  */
32951676Sjpk static tsol_zcent_t *
32961676Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
32971676Sjpk {
32981676Sjpk 	FILE *fp;
32991676Sjpk 	tsol_zcent_t *zcent = NULL;
33001676Sjpk 	char line[MAXTNZLEN];
33011676Sjpk 
33021676Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
33031676Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
33041676Sjpk 		return (NULL);
33051676Sjpk 	}
33061676Sjpk 
33071676Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
33081676Sjpk 		/*
33091676Sjpk 		 * Check for malformed database
33101676Sjpk 		 */
33111676Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
33121676Sjpk 			break;
33131676Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
33141676Sjpk 			continue;
33151676Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
33161676Sjpk 			break;
33171676Sjpk 		tsol_freezcent(zcent);
33181676Sjpk 		zcent = NULL;
33191676Sjpk 	}
33201676Sjpk 	(void) fclose(fp);
33211676Sjpk 
33221676Sjpk 	if (zcent == NULL) {
33231676Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
33241676Sjpk 		    "See tnzonecfg(4)");
33251676Sjpk 	} else {
33261676Sjpk 		if (zlabel == NULL)
33271676Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
33281676Sjpk 		/*
33291676Sjpk 		 * Save this zone's privileges for later read-down processing
33301676Sjpk 		 */
33311676Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
33321676Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
33331676Sjpk 			return (NULL);
33341676Sjpk 		} else {
33351676Sjpk 			priv_copyset(privs, zprivs);
33361676Sjpk 		}
33371676Sjpk 	}
33381676Sjpk 	return (zcent);
33391676Sjpk }
33401676Sjpk 
33411676Sjpk /*
33421676Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
33431676Sjpk  */
33441676Sjpk static void
33451676Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
33461676Sjpk {
33471676Sjpk 	tsol_mlp_t *mlp;
33481676Sjpk 	tsol_mlpent_t tsme;
33491676Sjpk 
33501676Sjpk 	if (!is_system_labeled())
33511676Sjpk 		return;
33521676Sjpk 
33531676Sjpk 	tsme.tsme_zoneid = zoneid;
33541676Sjpk 	tsme.tsme_flags = 0;
33551676Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
33561676Sjpk 		tsme.tsme_mlp = *mlp;
33571676Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
33581676Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
33591676Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
33601676Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
33611676Sjpk 		}
33621676Sjpk 	}
33631676Sjpk 
33641676Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
33651676Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
33661676Sjpk 		tsme.tsme_mlp = *mlp;
33671676Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
33681676Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
33691676Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
33701676Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
33711676Sjpk 		}
33721676Sjpk 	}
33731676Sjpk }
33741676Sjpk 
33751676Sjpk static void
33761676Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
33771676Sjpk {
33781676Sjpk 	tsol_mlpent_t tsme;
33791676Sjpk 
33801676Sjpk 	if (!is_system_labeled())
33811676Sjpk 		return;
33821676Sjpk 
33831676Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
33841676Sjpk 	tsme.tsme_zoneid = zoneid;
33851676Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
33861676Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
33871676Sjpk }
33881676Sjpk 
33890Sstevel@tonic-gate int
33900Sstevel@tonic-gate prtmount(const char *fs, void *x) {
33910Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
33920Sstevel@tonic-gate 	return (0);
33930Sstevel@tonic-gate }
33940Sstevel@tonic-gate 
3395766Scarlsonj /*
3396766Scarlsonj  * Look for zones running on the main system that are using this root (or any
3397766Scarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3398766Scarlsonj  * is found or if we can't tell.
3399766Scarlsonj  */
3400766Scarlsonj static boolean_t
3401766Scarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
34020Sstevel@tonic-gate {
3403766Scarlsonj 	zoneid_t *zids = NULL;
3404766Scarlsonj 	uint_t nzids = 0;
3405766Scarlsonj 	boolean_t retv;
3406766Scarlsonj 	int rlen, zlen;
3407766Scarlsonj 	char zroot[MAXPATHLEN];
3408766Scarlsonj 	char zonename[ZONENAME_MAX];
3409766Scarlsonj 
3410766Scarlsonj 	for (;;) {
3411766Scarlsonj 		nzids += 10;
3412766Scarlsonj 		zids = malloc(nzids * sizeof (*zids));
3413766Scarlsonj 		if (zids == NULL) {
34142267Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3415766Scarlsonj 			return (B_TRUE);
3416766Scarlsonj 		}
3417766Scarlsonj 		if (zone_list(zids, &nzids) == 0)
3418766Scarlsonj 			break;
3419766Scarlsonj 		free(zids);
3420766Scarlsonj 	}
3421766Scarlsonj 	retv = B_FALSE;
3422766Scarlsonj 	rlen = strlen(rootpath);
3423766Scarlsonj 	while (nzids > 0) {
3424766Scarlsonj 		/*
3425766Scarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3426766Scarlsonj 		 * while we were busy.
3427766Scarlsonj 		 */
3428766Scarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3429766Scarlsonj 		    sizeof (zroot)) == -1)
3430766Scarlsonj 			continue;
3431766Scarlsonj 		zlen = strlen(zroot);
3432766Scarlsonj 		if (zlen > rlen)
3433766Scarlsonj 			zlen = rlen;
3434766Scarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3435766Scarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3436766Scarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3437766Scarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3438766Scarlsonj 			    sizeof (zonename)) == -1)
3439766Scarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3440766Scarlsonj 				    "id %d", (int)zids[nzids]);
3441766Scarlsonj 			zerror(zlogp, B_FALSE,
3442766Scarlsonj 			    "zone root %s already in use by zone %s",
3443766Scarlsonj 			    rootpath, zonename);
3444766Scarlsonj 			retv = B_TRUE;
3445766Scarlsonj 			break;
3446766Scarlsonj 		}
3447766Scarlsonj 	}
3448766Scarlsonj 	free(zids);
3449766Scarlsonj 	return (retv);
3450766Scarlsonj }
3451766Scarlsonj 
3452766Scarlsonj /*
3453766Scarlsonj  * Search for loopback mounts that use this same source node (same device and
3454766Scarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3455766Scarlsonj  */
3456766Scarlsonj static boolean_t
3457766Scarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3458766Scarlsonj {
3459766Scarlsonj 	struct stat64 rst, zst;
3460766Scarlsonj 	struct mnttab *mnp;
3461766Scarlsonj 
3462766Scarlsonj 	if (stat64(rootpath, &rst) == -1) {
3463766Scarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3464766Scarlsonj 		return (B_TRUE);
3465766Scarlsonj 	}
3466766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3467766Scarlsonj 		return (B_TRUE);
3468766Scarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3469766Scarlsonj 		if (mnp->mnt_fstype == NULL ||
3470766Scarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3471766Scarlsonj 			continue;
3472766Scarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3473766Scarlsonj 		if (mnp->mnt_special != NULL &&
3474766Scarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3475766Scarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3476766Scarlsonj 			zerror(zlogp, B_FALSE,
3477766Scarlsonj 			    "zone root %s is reachable through %s",
3478766Scarlsonj 			    rootpath, mnp->mnt_mountp);
3479766Scarlsonj 			return (B_TRUE);
3480766Scarlsonj 		}
3481766Scarlsonj 	}
3482766Scarlsonj 	return (B_FALSE);
3483766Scarlsonj }
3484766Scarlsonj 
3485766Scarlsonj zoneid_t
3486766Scarlsonj vplat_create(zlog_t *zlogp, boolean_t mount_cmd)
3487766Scarlsonj {
3488766Scarlsonj 	zoneid_t rval = -1;
34890Sstevel@tonic-gate 	priv_set_t *privs;
34900Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
34912712Snn35248 	char modname[MAXPATHLEN];
34922712Snn35248 	struct brand_attr attr;
34932727Sedp 	brand_handle_t bh;
34940Sstevel@tonic-gate 	char *rctlbuf = NULL;
3495766Scarlsonj 	size_t rctlbufsz = 0;
3496789Sahrens 	char *zfsbuf = NULL;
3497789Sahrens 	size_t zfsbufsz = 0;
3498766Scarlsonj 	zoneid_t zoneid = -1;
34990Sstevel@tonic-gate 	int xerr;
3500766Scarlsonj 	char *kzone;
3501766Scarlsonj 	FILE *fp = NULL;
35021676Sjpk 	tsol_zcent_t *zcent = NULL;
35031676Sjpk 	int match = 0;
35041676Sjpk 	int doi = 0;
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
35070Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
35080Sstevel@tonic-gate 		return (-1);
35090Sstevel@tonic-gate 	}
3510766Scarlsonj 	if (zonecfg_in_alt_root())
3511766Scarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
35140Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
35150Sstevel@tonic-gate 		return (-1);
35160Sstevel@tonic-gate 	}
35170Sstevel@tonic-gate 	priv_emptyset(privs);
35181645Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
35190Sstevel@tonic-gate 		goto error;
35201645Scomay 
3521766Scarlsonj 	if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
35220Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
35230Sstevel@tonic-gate 		goto error;
35240Sstevel@tonic-gate 	}
35251645Scomay 
3526789Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
3527789Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
3528789Sahrens 		goto error;
3529789Sahrens 	}
35300Sstevel@tonic-gate 
35311769Scarlsonj 	if (!mount_cmd && is_system_labeled()) {
35321676Sjpk 		zcent = get_zone_label(zlogp, privs);
35331769Scarlsonj 		if (zcent != NULL) {
35341676Sjpk 			match = zcent->zc_match;
35351676Sjpk 			doi = zcent->zc_doi;
35361676Sjpk 			*zlabel = zcent->zc_label;
35371676Sjpk 		} else {
35381676Sjpk 			goto error;
35391676Sjpk 		}
35401676Sjpk 	}
35411676Sjpk 
3542766Scarlsonj 	kzone = zone_name;
3543766Scarlsonj 
3544766Scarlsonj 	/*
3545766Scarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
3546766Scarlsonj 	 * main system that are using this root (or any subdirectory of it).
3547766Scarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
3548766Scarlsonj 	 * that use this same source node (same device and inode).
3549766Scarlsonj 	 */
3550766Scarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
3551766Scarlsonj 		goto error;
3552766Scarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
3553766Scarlsonj 		goto error;
3554766Scarlsonj 
3555766Scarlsonj 	if (mount_cmd) {
35562712Snn35248 		assert(zone_isnative);
3557766Scarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
3558766Scarlsonj 
3559766Scarlsonj 		/*
3560766Scarlsonj 		 * Forge up a special root for this zone.  When a zone is
3561766Scarlsonj 		 * mounted, we can't let the zone have its own root because the
3562766Scarlsonj 		 * tools that will be used in this "scratch zone" need access
3563766Scarlsonj 		 * to both the zone's resources and the running machine's
3564766Scarlsonj 		 * executables.
3565766Scarlsonj 		 *
3566766Scarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
3567766Scarlsonj 		 */
3568766Scarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
3569766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
3570766Scarlsonj 			goto error;
3571766Scarlsonj 		}
3572766Scarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
3573766Scarlsonj 			goto error;
3574766Scarlsonj 	}
3575766Scarlsonj 
3576766Scarlsonj 	if (zonecfg_in_alt_root()) {
3577766Scarlsonj 		/*
3578766Scarlsonj 		 * If we are mounting up a zone in an alternate root partition,
3579766Scarlsonj 		 * then we have some additional work to do before starting the
3580766Scarlsonj 		 * zone.  First, resolve the root path down so that we're not
3581766Scarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
3582766Scarlsonj 		 * the zone.
3583766Scarlsonj 		 */
3584766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
3585766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
3586766Scarlsonj 			goto error;
3587766Scarlsonj 		}
3588766Scarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
3589766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
3590766Scarlsonj 			goto error;
3591766Scarlsonj 		}
3592766Scarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
3593766Scarlsonj 		    NULL, 0) == 0) {
3594766Scarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
3595766Scarlsonj 			goto error;
3596766Scarlsonj 		}
3597766Scarlsonj 		/* This is the preferred name */
3598766Scarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
3599766Scarlsonj 		    zone_name);
3600766Scarlsonj 		srandom(getpid());
3601766Scarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
3602766Scarlsonj 		    0) == 0) {
3603766Scarlsonj 			/* This is just an arbitrary name; note "." usage */
3604766Scarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
3605766Scarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
3606766Scarlsonj 		}
3607766Scarlsonj 		kzone = kernzone;
3608766Scarlsonj 	}
3609766Scarlsonj 
36100Sstevel@tonic-gate 	xerr = 0;
3611766Scarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
36121676Sjpk 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel)) == -1) {
36130Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
36140Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
36150Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
36160Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
36170Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
36180Sstevel@tonic-gate 			} else {
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
36210Sstevel@tonic-gate 				    "These file-systems are mounted on "
36220Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
36230Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
36240Sstevel@tonic-gate 				    prtmount, zlogp);
36250Sstevel@tonic-gate 			}
36260Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
36270Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
36280Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
36290Sstevel@tonic-gate 			    "environment", "zone_create");
36300Sstevel@tonic-gate 		} else {
36310Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
36320Sstevel@tonic-gate 		}
36330Sstevel@tonic-gate 		goto error;
36340Sstevel@tonic-gate 	}
3635766Scarlsonj 
3636766Scarlsonj 	if (zonecfg_in_alt_root() &&
3637766Scarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
3638766Scarlsonj 	    zonecfg_get_root()) == -1) {
3639766Scarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
3640766Scarlsonj 		goto error;
3641766Scarlsonj 	}
3642766Scarlsonj 
36432712Snn35248 	if ((zone_get_brand(zone_name, attr.ba_brandname,
36442712Snn35248 	    MAXNAMELEN) != Z_OK) ||
36452727Sedp 	    (bh = brand_open(attr.ba_brandname)) == NULL) {
36462712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine brand name");
36472712Snn35248 		return (-1);
36482712Snn35248 	}
36492712Snn35248 
36502712Snn35248 	/*
36512712Snn35248 	 * If this brand requires any kernel support, now is the time to
36522712Snn35248 	 * get it loaded and initialized.
36532712Snn35248 	 */
36542727Sedp 	if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
36552712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine brand kernel "
36562712Snn35248 		    "module");
36572712Snn35248 		return (-1);
36582712Snn35248 	}
36592712Snn35248 
36602712Snn35248 	if (strlen(modname) > 0) {
36612712Snn35248 		(void) strlcpy(attr.ba_modname, modname, MAXPATHLEN);
36622712Snn35248 		if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
36632712Snn35248 		    sizeof (attr) != 0)) {
36642712Snn35248 			zerror(zlogp, B_TRUE, "could not set zone brand "
36652712Snn35248 			    "attribute.");
36662712Snn35248 			goto error;
36672712Snn35248 		}
36682712Snn35248 	}
36692712Snn35248 
36700Sstevel@tonic-gate 	/*
3671766Scarlsonj 	 * The following is a warning, not an error, and is not performed when
3672766Scarlsonj 	 * merely mounting a zone for administrative use.
36730Sstevel@tonic-gate 	 */
3674766Scarlsonj 	if (!mount_cmd && bind_to_pool(zlogp, zoneid) != 0)
36750Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: unable to bind zone to "
36760Sstevel@tonic-gate 		    "requested pool; using default pool.");
36771769Scarlsonj 	if (!mount_cmd)
36781769Scarlsonj 		set_mlps(zlogp, zoneid, zcent);
3679766Scarlsonj 	rval = zoneid;
3680766Scarlsonj 	zoneid = -1;
3681766Scarlsonj 
36820Sstevel@tonic-gate error:
3683766Scarlsonj 	if (zoneid != -1)
3684766Scarlsonj 		(void) zone_destroy(zoneid);
36850Sstevel@tonic-gate 	if (rctlbuf != NULL)
36860Sstevel@tonic-gate 		free(rctlbuf);
36870Sstevel@tonic-gate 	priv_freeset(privs);
3688766Scarlsonj 	if (fp != NULL)
3689766Scarlsonj 		zonecfg_close_scratch(fp);
3690766Scarlsonj 	lofs_discard_mnttab();
36911676Sjpk 	if (zcent != NULL)
36921676Sjpk 		tsol_freezcent(zcent);
36930Sstevel@tonic-gate 	return (rval);
36940Sstevel@tonic-gate }
36950Sstevel@tonic-gate 
36962303Scarlsonj /*
36972303Scarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
36982303Scarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
36992303Scarlsonj  * details from inside the zone.
37002303Scarlsonj  */
37012303Scarlsonj static void
37022303Scarlsonj write_index_file(zoneid_t zoneid)
37032303Scarlsonj {
37042303Scarlsonj 	FILE *zef;
37052303Scarlsonj 	FILE *zet;
37062303Scarlsonj 	struct zoneent *zep;
37072303Scarlsonj 	pid_t child;
37082303Scarlsonj 	int tmpl_fd;
37092303Scarlsonj 	ctid_t ct;
37102303Scarlsonj 	int fd;
37112303Scarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
37122303Scarlsonj 
37132303Scarlsonj 	/* Locate the zone entry in the global zone's index file */
37142303Scarlsonj 	if ((zef = setzoneent()) == NULL)
37152303Scarlsonj 		return;
37162303Scarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
37172303Scarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
37182303Scarlsonj 			break;
37192303Scarlsonj 		free(zep);
37202303Scarlsonj 	}
37212303Scarlsonj 	endzoneent(zef);
37222303Scarlsonj 	if (zep == NULL)
37232303Scarlsonj 		return;
37242303Scarlsonj 
37252303Scarlsonj 	if ((tmpl_fd = init_template()) == -1) {
37262303Scarlsonj 		free(zep);
37272303Scarlsonj 		return;
37282303Scarlsonj 	}
37292303Scarlsonj 
37302303Scarlsonj 	if ((child = fork()) == -1) {
37312303Scarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
37322303Scarlsonj 		(void) close(tmpl_fd);
37332303Scarlsonj 		free(zep);
37342303Scarlsonj 		return;
37352303Scarlsonj 	}
37362303Scarlsonj 
37372303Scarlsonj 	/* parent waits for child to finish */
37382303Scarlsonj 	if (child != 0) {
37392303Scarlsonj 		free(zep);
37402303Scarlsonj 		if (contract_latest(&ct) == -1)
37412303Scarlsonj 			ct = -1;
37422303Scarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
37432303Scarlsonj 		(void) close(tmpl_fd);
37442303Scarlsonj 		(void) waitpid(child, NULL, 0);
37452303Scarlsonj 		(void) contract_abandon_id(ct);
37462303Scarlsonj 		return;
37472303Scarlsonj 	}
37482303Scarlsonj 
37492303Scarlsonj 	/* child enters zone and sets up index file */
37502303Scarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
37512303Scarlsonj 	if (zone_enter(zoneid) != -1) {
37522303Scarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
37532303Scarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
37542303Scarlsonj 		    ZONE_CONFIG_GID);
37552303Scarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
37562303Scarlsonj 		    ZONE_INDEX_MODE);
37572303Scarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
37582303Scarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
37592303Scarlsonj 			if (uuid_is_null(zep->zone_uuid))
37602303Scarlsonj 				uuidstr[0] = '\0';
37612303Scarlsonj 			else
37622303Scarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
37632303Scarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
37642303Scarlsonj 			    zone_state_str(zep->zone_state),
37652303Scarlsonj 			    uuidstr);
37662303Scarlsonj 			(void) fclose(zet);
37672303Scarlsonj 		}
37682303Scarlsonj 	}
37692303Scarlsonj 	_exit(0);
37702303Scarlsonj }
37712303Scarlsonj 
37720Sstevel@tonic-gate int
37732303Scarlsonj vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd, zoneid_t zoneid)
37740Sstevel@tonic-gate {
37752712Snn35248 	char zonepath[MAXPATHLEN];
37762503Sdp 
3777789Sahrens 	if (!mount_cmd && validate_datasets(zlogp) != 0) {
3778789Sahrens 		lofs_discard_mnttab();
3779789Sahrens 		return (-1);
3780789Sahrens 	}
3781789Sahrens 
37822712Snn35248 	/*
37832712Snn35248 	 * Before we try to mount filesystems we need to create the
37842712Snn35248 	 * attribute backing store for /dev
37852712Snn35248 	 */
37862712Snn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
37872712Snn35248 		lofs_discard_mnttab();
37882712Snn35248 		return (-1);
37892712Snn35248 	}
37902712Snn35248 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE) != 0) {
37912712Snn35248 		lofs_discard_mnttab();
37922712Snn35248 		return (-1);
37932712Snn35248 	}
37942712Snn35248 
37952621Sllai1 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
3796766Scarlsonj 		lofs_discard_mnttab();
37970Sstevel@tonic-gate 		return (-1);
3798766Scarlsonj 	}
37992621Sllai1 
38002621Sllai1 	if (!mount_cmd && configure_network_interfaces(zlogp) != 0) {
3801766Scarlsonj 		lofs_discard_mnttab();
38020Sstevel@tonic-gate 		return (-1);
3803766Scarlsonj 	}
38042303Scarlsonj 
38052303Scarlsonj 	write_index_file(zoneid);
38062303Scarlsonj 
3807766Scarlsonj 	lofs_discard_mnttab();
38080Sstevel@tonic-gate 	return (0);
38090Sstevel@tonic-gate }
38100Sstevel@tonic-gate 
3811766Scarlsonj static int
3812766Scarlsonj lu_root_teardown(zlog_t *zlogp)
3813766Scarlsonj {
3814766Scarlsonj 	char zroot[MAXPATHLEN];
3815766Scarlsonj 
38162712Snn35248 	assert(zone_isnative);
38172712Snn35248 
3818766Scarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
3819766Scarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
3820766Scarlsonj 		return (-1);
3821766Scarlsonj 	}
3822766Scarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
3823766Scarlsonj 
3824766Scarlsonj 	/*
3825766Scarlsonj 	 * At this point, the processes are gone, the filesystems (save the
3826766Scarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
3827766Scarlsonj 	 * still be creds floating about in the system that reference the
3828766Scarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
3829766Scarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
3830766Scarlsonj 	 * (How I wish this were not true, and umount2 just did the right
3831766Scarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
3832766Scarlsonj 	 */
3833766Scarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
3834766Scarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
3835766Scarlsonj 			goto unmounted;
3836766Scarlsonj 		if (errno == EBUSY) {
3837766Scarlsonj 			int tries = 10;
3838766Scarlsonj 
3839766Scarlsonj 			while (--tries >= 0) {
3840766Scarlsonj 				(void) sleep(1);
3841766Scarlsonj 				if (umount2(zroot, 0) == 0)
3842766Scarlsonj 					goto unmounted;
3843766Scarlsonj 				if (errno != EBUSY)
3844766Scarlsonj 					break;
3845766Scarlsonj 			}
3846766Scarlsonj 		}
3847766Scarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
3848766Scarlsonj 		return (-1);
3849766Scarlsonj 	}
3850766Scarlsonj unmounted:
3851766Scarlsonj 
3852766Scarlsonj 	/*
3853766Scarlsonj 	 * Only zones in an alternate root environment have scratch zone
3854766Scarlsonj 	 * entries.
3855766Scarlsonj 	 */
3856766Scarlsonj 	if (zonecfg_in_alt_root()) {
3857766Scarlsonj 		FILE *fp;
3858766Scarlsonj 		int retv;
3859766Scarlsonj 
3860766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
3861766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
3862766Scarlsonj 			return (-1);
3863766Scarlsonj 		}
3864766Scarlsonj 		retv = -1;
3865766Scarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
3866766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
3867766Scarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
3868766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
3869766Scarlsonj 		else
3870766Scarlsonj 			retv = 0;
3871766Scarlsonj 		zonecfg_close_scratch(fp);
3872766Scarlsonj 		return (retv);
3873766Scarlsonj 	} else {
3874766Scarlsonj 		return (0);
3875766Scarlsonj 	}
3876766Scarlsonj }
3877766Scarlsonj 
38780Sstevel@tonic-gate int
3879766Scarlsonj vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd)
38800Sstevel@tonic-gate {
3881766Scarlsonj 	char *kzone;
38820Sstevel@tonic-gate 	zoneid_t zoneid;
38832712Snn35248 	char zroot[MAXPATHLEN];
38842712Snn35248 	char cmdbuf[MAXPATHLEN];
38852712Snn35248 	char brand[MAXNAMELEN];
38862727Sedp 	brand_handle_t bh = NULL;
38870Sstevel@tonic-gate 
3888766Scarlsonj 	kzone = zone_name;
3889766Scarlsonj 	if (zonecfg_in_alt_root()) {
3890766Scarlsonj 		FILE *fp;
3891766Scarlsonj 
3892766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
3893766Scarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
3894766Scarlsonj 			goto error;
3895766Scarlsonj 		}
3896766Scarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
3897766Scarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
3898766Scarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
3899766Scarlsonj 			zonecfg_close_scratch(fp);
3900766Scarlsonj 			goto error;
3901766Scarlsonj 		}
3902766Scarlsonj 		zonecfg_close_scratch(fp);
3903766Scarlsonj 		kzone = kernzone;
3904766Scarlsonj 	}
3905766Scarlsonj 
3906766Scarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
39070Sstevel@tonic-gate 		if (!bringup_failure_recovery)
39080Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
3909766Scarlsonj 		if (unmount_cmd)
3910766Scarlsonj 			(void) lu_root_teardown(zlogp);
39110Sstevel@tonic-gate 		goto error;
39120Sstevel@tonic-gate 	}
39130Sstevel@tonic-gate 
39140Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
39150Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
39160Sstevel@tonic-gate 		goto error;
39170Sstevel@tonic-gate 	}
39180Sstevel@tonic-gate 
39192712Snn35248 	/* Get the path to the root of this zone */
39202712Snn35248 	if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
39212712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone root");
39222712Snn35248 		goto error;
39232712Snn35248 	}
39242712Snn35248 
39252712Snn35248 	/* Get a handle to the brand info for this zone */
39262712Snn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
39272727Sedp 	    (bh = brand_open(brand)) == NULL) {
39282712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
39292712Snn35248 		return (-1);
39302712Snn35248 	}
39312712Snn35248 	/*
39322712Snn35248 	 * If there is a brand 'halt' callback, execute it now to give the
39332712Snn35248 	 * brand a chance to cleanup any custom configuration.
39342712Snn35248 	 */
39352712Snn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
39362727Sedp 	if (brand_get_halt(bh, zone_name, zroot, cmdbuf + EXEC_LEN,
39372712Snn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) < 0) {
39382727Sedp 		brand_close(bh);
39392712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
39402712Snn35248 		    "halt callback.");
39412712Snn35248 		goto error;
39422712Snn35248 	}
39432727Sedp 	brand_close(bh);
39442712Snn35248 
39452712Snn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
39462712Snn35248 	    (do_subproc(zlogp, cmdbuf) != Z_OK)) {
39472712Snn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
39482712Snn35248 		goto error;
39492712Snn35248 	}
39502712Snn35248 
3951766Scarlsonj 	if (!unmount_cmd &&
3952766Scarlsonj 	    unconfigure_network_interfaces(zlogp, zoneid) != 0) {
39530Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
39540Sstevel@tonic-gate 		    "unable to unconfigure network interfaces in zone");
39550Sstevel@tonic-gate 		goto error;
39560Sstevel@tonic-gate 	}
39570Sstevel@tonic-gate 
3958766Scarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
39590Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
39600Sstevel@tonic-gate 		goto error;
39610Sstevel@tonic-gate 	}
39620Sstevel@tonic-gate 
39632621Sllai1 	/* destroy zconsole before umount /dev */
39642621Sllai1 	if (!unmount_cmd)
39652621Sllai1 		destroy_console_slave();
39662621Sllai1 
3967766Scarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
39680Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
39690Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
39700Sstevel@tonic-gate 		goto error;
39710Sstevel@tonic-gate 	}
39720Sstevel@tonic-gate 
39731676Sjpk 	remove_mlps(zlogp, zoneid);
39741676Sjpk 
39750Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
39760Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
39770Sstevel@tonic-gate 		goto error;
39780Sstevel@tonic-gate 	}
39790Sstevel@tonic-gate 
3980766Scarlsonj 	/*
3981766Scarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
3982766Scarlsonj 	 * root for the zone and then remove it from the map file.
3983766Scarlsonj 	 */
3984766Scarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
3985766Scarlsonj 		goto error;
3986766Scarlsonj 
3987766Scarlsonj 	lofs_discard_mnttab();
39880Sstevel@tonic-gate 	return (0);
39890Sstevel@tonic-gate 
39900Sstevel@tonic-gate error:
3991766Scarlsonj 	lofs_discard_mnttab();
39920Sstevel@tonic-gate 	return (-1);
39930Sstevel@tonic-gate }
3994