xref: /titanic_53/usr/src/cmd/zoneadmd/vplat.c (revision f14f3ae7296249c648ea8c6330e49a8b587c8c7b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
236cfd72c6Sgjelinek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
297c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
307c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
317c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
327c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
337c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
347c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
35108322fbScarlsonj  *
36108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
37108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
38108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
39108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
40108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
41108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
42108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
43108322fbScarlsonj  * without relying on any binaries in the zone itself.
44108322fbScarlsonj  *
45108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
46108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
47108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
48108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
49108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
50108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
51108322fbScarlsonj  * process.
52108322fbScarlsonj  *
53108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
54108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
55108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
56108322fbScarlsonj  * $ZONEPATH/lu as the root.
57108322fbScarlsonj  *
58108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
59108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
60108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
61108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/param.h>
657c478bd9Sstevel@tonic-gate #include <sys/mount.h>
667c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
677c478bd9Sstevel@tonic-gate #include <sys/socket.h>
687c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
697c478bd9Sstevel@tonic-gate #include <sys/types.h>
707c478bd9Sstevel@tonic-gate #include <sys/stat.h>
717c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
727c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
737c478bd9Sstevel@tonic-gate #include <sys/conf.h>
747c478bd9Sstevel@tonic-gate 
75f4b3ec61Sdh155122 #include <libdlpi.h>
76f595a68aSyz147064 #include <libdllink.h>
77d62bc4baSyz147064 #include <libdlvlan.h>
78f4b3ec61Sdh155122 
797c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
807c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
817c478bd9Sstevel@tonic-gate #include <netinet/in.h>
827c478bd9Sstevel@tonic-gate #include <net/route.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <stdio.h>
857c478bd9Sstevel@tonic-gate #include <errno.h>
867c478bd9Sstevel@tonic-gate #include <fcntl.h>
877c478bd9Sstevel@tonic-gate #include <unistd.h>
887c478bd9Sstevel@tonic-gate #include <rctl.h>
897c478bd9Sstevel@tonic-gate #include <stdlib.h>
907c478bd9Sstevel@tonic-gate #include <string.h>
917c478bd9Sstevel@tonic-gate #include <strings.h>
927c478bd9Sstevel@tonic-gate #include <wait.h>
937c478bd9Sstevel@tonic-gate #include <limits.h>
947c478bd9Sstevel@tonic-gate #include <libgen.h>
95fa9e4066Sahrens #include <libzfs.h>
96facf4a8dSllai1 #include <libdevinfo.h>
977c478bd9Sstevel@tonic-gate #include <zone.h>
987c478bd9Sstevel@tonic-gate #include <assert.h>
99555afedfScarlsonj #include <libcontract.h>
100555afedfScarlsonj #include <libcontract_priv.h>
101555afedfScarlsonj #include <uuid/uuid.h>
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1047c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1057c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1067c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
107fa9e4066Sahrens #include <sys/fs/zfs.h>
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #include <pool.h>
1107c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1110209230bSgjelinek #include <sys/priocntl.h>
1127c478bd9Sstevel@tonic-gate 
1139acbbeafSnn35248 #include <libbrand.h>
1149acbbeafSnn35248 #include <sys/brand.h>
1157c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
11639d3e169Sevanl #include <synch.h>
11722321485Svp157776 
1187c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
11945916cd2Sjpk #include <tsol/label.h>
12045916cd2Sjpk #include <libtsnet.h>
12145916cd2Sjpk #include <sys/priv.h>
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1247c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1277c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13045916cd2Sjpk #define	MAXTNZLEN	2048
1317c478bd9Sstevel@tonic-gate 
1326cfd72c6Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1336cfd72c6Sgjelinek 
1347c478bd9Sstevel@tonic-gate /* for routing socket */
1357c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1367c478bd9Sstevel@tonic-gate 
137108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
138108322fbScarlsonj static char kernzone[ZONENAME_MAX];
139108322fbScarlsonj 
140108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
141108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
142108322fbScarlsonj 
14345916cd2Sjpk /* for Trusted Extensions */
14445916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
14545916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
14645916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
1479d48116cSdh155122 
14845916cd2Sjpk static m_label_t *zlabel = NULL;
14945916cd2Sjpk static m_label_t *zid_label = NULL;
15045916cd2Sjpk static priv_set_t *zprivs = NULL;
15145916cd2Sjpk 
1527c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1537c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1547c478bd9Sstevel@tonic-gate 
155c5cd6260S /* from zoneadmd */
156c5cd6260S extern char query_hook[];
157c5cd6260S 
1587c478bd9Sstevel@tonic-gate /*
159108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
160108322fbScarlsonj  * data) only once every N times through the loop.
161108322fbScarlsonj  */
162108322fbScarlsonj #define	MNTTAB_HUNK	32
163108322fbScarlsonj 
164108322fbScarlsonj /*
1657c478bd9Sstevel@tonic-gate  * Private autofs system call
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static int
1707c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
178108322fbScarlsonj static void
179108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
180108322fbScarlsonj {
181108322fbScarlsonj 	uint_t i;
182108322fbScarlsonj 
183108322fbScarlsonj 	if (mnt_array == NULL)
184108322fbScarlsonj 		return;
185108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
186108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
187108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
188108322fbScarlsonj 		free(mnt_array[i].mnt_special);
189108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
190108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
191108322fbScarlsonj 	}
192108322fbScarlsonj 	free(mnt_array);
193108322fbScarlsonj }
194108322fbScarlsonj 
195108322fbScarlsonj /*
196108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
197108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
198108322fbScarlsonj  * array in "nelemp".
199108322fbScarlsonj  */
200108322fbScarlsonj static int
201108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
202108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
203108322fbScarlsonj {
204108322fbScarlsonj 	struct mnttab mnt;
205108322fbScarlsonj 	struct mnttab *mnts;
206108322fbScarlsonj 	struct mnttab *mnp;
207108322fbScarlsonj 	uint_t nmnt;
208108322fbScarlsonj 
209108322fbScarlsonj 	rewind(mnttab);
210108322fbScarlsonj 	resetmnttab(mnttab);
211108322fbScarlsonj 	nmnt = 0;
212108322fbScarlsonj 	mnts = NULL;
213108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
214108322fbScarlsonj 		struct mnttab *tmp_array;
215108322fbScarlsonj 
216108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
217108322fbScarlsonj 			continue;
218108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
219108322fbScarlsonj 			tmp_array = realloc(mnts,
220108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
221108322fbScarlsonj 			if (tmp_array == NULL) {
222108322fbScarlsonj 				free_mnttable(mnts, nmnt);
223108322fbScarlsonj 				return (-1);
224108322fbScarlsonj 			}
225108322fbScarlsonj 			mnts = tmp_array;
226108322fbScarlsonj 		}
227108322fbScarlsonj 		mnp = &mnts[nmnt++];
228108322fbScarlsonj 
229108322fbScarlsonj 		/*
230108322fbScarlsonj 		 * Zero out any fields we're not using.
231108322fbScarlsonj 		 */
232108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
233108322fbScarlsonj 
234108322fbScarlsonj 		if (mnt.mnt_special != NULL)
235108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
236108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
237108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
238108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
239108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
240108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
241108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
242108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
243108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
244108322fbScarlsonj 			free_mnttable(mnts, nmnt);
245108322fbScarlsonj 			return (-1);
246108322fbScarlsonj 		}
247108322fbScarlsonj 	}
248108322fbScarlsonj 	*mnt_arrayp = mnts;
249108322fbScarlsonj 	*nelemp = nmnt;
250108322fbScarlsonj 	return (0);
251108322fbScarlsonj }
252108322fbScarlsonj 
253108322fbScarlsonj /*
254108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
255108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
256108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
257108322fbScarlsonj  * reread of the list of mount points
258108322fbScarlsonj  */
259108322fbScarlsonj static void
260108322fbScarlsonj lofs_discard_mnttab(void)
261108322fbScarlsonj {
262108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
263108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
264108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
265108322fbScarlsonj }
266108322fbScarlsonj 
267108322fbScarlsonj static int
268108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
269108322fbScarlsonj {
270108322fbScarlsonj 	FILE *mnttab;
271108322fbScarlsonj 	uint_t nmnts;
272108322fbScarlsonj 
273108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
274108322fbScarlsonj 		return (-1);
275108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
276108322fbScarlsonj 	    &nmnts) == -1) {
277108322fbScarlsonj 		(void) fclose(mnttab);
278108322fbScarlsonj 		return (-1);
279108322fbScarlsonj 	}
280108322fbScarlsonj 	(void) fclose(mnttab);
281108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
282108322fbScarlsonj 	return (0);
283108322fbScarlsonj }
284108322fbScarlsonj 
285108322fbScarlsonj /*
286108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
287108322fbScarlsonj  * path and resolves them all down to an absolute path.
288108322fbScarlsonj  */
289910f48daSedp void
290108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
291108322fbScarlsonj {
292108322fbScarlsonj 	int len, arlen;
293108322fbScarlsonj 	const char *altroot;
294108322fbScarlsonj 	char tmppath[MAXPATHLEN];
295108322fbScarlsonj 	boolean_t outside_altroot;
296108322fbScarlsonj 
297108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
298108322fbScarlsonj 		return;
299108322fbScarlsonj 	tmppath[len] = '\0';
300108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
301108322fbScarlsonj 
302108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
303108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
304108322fbScarlsonj 		return;
305108322fbScarlsonj 
306108322fbScarlsonj 	altroot = zonecfg_get_root();
307108322fbScarlsonj 	arlen = strlen(altroot);
308108322fbScarlsonj 	outside_altroot = B_FALSE;
309108322fbScarlsonj 	for (;;) {
310108322fbScarlsonj 		struct mnttab *mnp;
311108322fbScarlsonj 
3121e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3131e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3141e9d8f9fSdminer 		    mnp--) {
315108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
316108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3171e9d8f9fSdminer 			    mnp->mnt_special == NULL)
318108322fbScarlsonj 				continue;
319108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
320108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
321108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
322108322fbScarlsonj 				break;
323108322fbScarlsonj 		}
3241e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3251e9d8f9fSdminer 			break;
3261e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3271e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
328108322fbScarlsonj 			break;
329108322fbScarlsonj 		if (outside_altroot) {
330108322fbScarlsonj 			char *cp;
331108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
332108322fbScarlsonj 
333108322fbScarlsonj 			/*
334108322fbScarlsonj 			 * If we run into a read-only mount outside of the
335108322fbScarlsonj 			 * alternate root environment, then the user doesn't
336108322fbScarlsonj 			 * want this path to be made read-write.
337108322fbScarlsonj 			 */
338108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
339108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
340108322fbScarlsonj 			    NULL &&
341108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
342108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
343108322fbScarlsonj 				break;
344108322fbScarlsonj 			}
345108322fbScarlsonj 		} else if (arlen > 0 &&
346108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
347108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
348108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
349108322fbScarlsonj 			outside_altroot = B_TRUE;
350108322fbScarlsonj 		}
351108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
352108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
353108322fbScarlsonj 		    mnp->mnt_special, path + len);
354108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
355108322fbScarlsonj 			break;
356108322fbScarlsonj 		path[len] = '\0';
357108322fbScarlsonj 	}
358108322fbScarlsonj }
359108322fbScarlsonj 
360108322fbScarlsonj /*
361108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
362108322fbScarlsonj  * referenced device is already mounted somewhere.
363108322fbScarlsonj  */
364108322fbScarlsonj static int
365108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
366108322fbScarlsonj {
367108322fbScarlsonj 	struct mnttab *mnp;
368108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
369108322fbScarlsonj 
370108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
371108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
372108322fbScarlsonj 		return (-1);
373108322fbScarlsonj 
374108322fbScarlsonj 	/*
375108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
376108322fbScarlsonj 	 * no need to worry about conflicting mounts.
377108322fbScarlsonj 	 */
378108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
379108322fbScarlsonj 	    mnp++) {
380108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
381108322fbScarlsonj 			break;
382108322fbScarlsonj 	}
383108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
384108322fbScarlsonj 		return (0);
385108322fbScarlsonj 
386108322fbScarlsonj 	/*
387108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
388108322fbScarlsonj 	 */
389108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
390108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
391108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
392108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
393108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
394108322fbScarlsonj 
395108322fbScarlsonj 	/*
396108322fbScarlsonj 	 * Discard all but one of the original options and set that to be the
397108322fbScarlsonj 	 * same set of options used for inherit package directory resources.
398108322fbScarlsonj 	 */
399108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
400108322fbScarlsonj 	if (optptr == NULL) {
401108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
402108322fbScarlsonj 		if (optptr == NULL) {
403108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
404108322fbScarlsonj 			    fsptr->zone_fs_dir);
405108322fbScarlsonj 			return (-1);
406108322fbScarlsonj 		}
407108322fbScarlsonj 	} else {
408108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
409108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
410108322fbScarlsonj 			free(onext);
411108322fbScarlsonj 		}
412108322fbScarlsonj 	}
413108322fbScarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
414108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
415108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
416108322fbScarlsonj 	return (0);
417108322fbScarlsonj }
418108322fbScarlsonj 
419d314f035Sedp int
42027e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
42127e6fb21Sdp     uid_t userid, gid_t groupid)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4247c478bd9Sstevel@tonic-gate 	struct stat st;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4277c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4297c478bd9Sstevel@tonic-gate 		    subdir);
4307c478bd9Sstevel@tonic-gate 		return (-1);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4347c478bd9Sstevel@tonic-gate 		/*
4357c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4367c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4377c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4387c478bd9Sstevel@tonic-gate 		 */
4397c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
440756cf395SRic Aleshire 			if (S_ISREG(st.st_mode)) {
44145916cd2Sjpk 				/*
442756cf395SRic Aleshire 				 * Allow readonly mounts of /etc/ files; this
443756cf395SRic Aleshire 				 * is needed most by Trusted Extensions.
44445916cd2Sjpk 				 */
44545916cd2Sjpk 				if (strncmp(subdir, "/etc/",
44645916cd2Sjpk 				    strlen("/etc/")) != 0) {
44745916cd2Sjpk 					zerror(zlogp, B_FALSE,
44845916cd2Sjpk 					    "%s is not in /etc", path);
4497c478bd9Sstevel@tonic-gate 					return (-1);
4507c478bd9Sstevel@tonic-gate 				}
45145916cd2Sjpk 			} else {
45245916cd2Sjpk 				zerror(zlogp, B_FALSE,
45345916cd2Sjpk 				    "%s is not a directory", path);
45445916cd2Sjpk 				return (-1);
45545916cd2Sjpk 			}
45645916cd2Sjpk 		}
45727e6fb21Sdp 		return (0);
45827e6fb21Sdp 	}
45927e6fb21Sdp 
46027e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
4617c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4627c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4637c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4647c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4657c478bd9Sstevel@tonic-gate 		else
4667c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4677c478bd9Sstevel@tonic-gate 		return (-1);
4687c478bd9Sstevel@tonic-gate 	}
46927e6fb21Sdp 
47027e6fb21Sdp 	(void) chown(path, userid, groupid);
4717c478bd9Sstevel@tonic-gate 	return (0);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate static void
4757c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	uint_t i;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (types == NULL)
4807c478bd9Sstevel@tonic-gate 		return;
4817c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
4827c478bd9Sstevel@tonic-gate 		free(types[i]);
4837c478bd9Sstevel@tonic-gate 	free(types);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate static char **
4877c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	char **types = NULL;
4907c478bd9Sstevel@tonic-gate 	FILE *fp;
4917c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
4927c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
4937c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
4947c478bd9Sstevel@tonic-gate 	uint_t i;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
4977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
4987c478bd9Sstevel@tonic-gate 		return (NULL);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5047c478bd9Sstevel@tonic-gate 		lines++;
5057c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5067c478bd9Sstevel@tonic-gate 		goto out;
5077c478bd9Sstevel@tonic-gate 	rewind(fp);
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5107c478bd9Sstevel@tonic-gate 	 */
5117c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5127c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5147c478bd9Sstevel@tonic-gate 		goto out;
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	i = 0;
5177c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5187c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5197c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5207c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5217c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5227c478bd9Sstevel@tonic-gate 			types = NULL;
5237c478bd9Sstevel@tonic-gate 			goto out;
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5267c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5277c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5287c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5297c478bd9Sstevel@tonic-gate 			types = NULL;
5307c478bd9Sstevel@tonic-gate 			goto out;
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 		i++;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate out:
5357c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5367c478bd9Sstevel@tonic-gate 	return (types);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate static boolean_t
5407c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	uint_t i;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5457c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5467c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5477c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5487c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
553108322fbScarlsonj /*
554108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
555108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
556108322fbScarlsonj  */
5577c478bd9Sstevel@tonic-gate static void
558108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5597c478bd9Sstevel@tonic-gate {
560108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
561108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
562108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5677c478bd9Sstevel@tonic-gate  *
5687c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5697c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5707c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5717c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5727c478bd9Sstevel@tonic-gate  *
5737c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5747c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5757c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5767c478bd9Sstevel@tonic-gate  *
5777c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5787c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5797c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5807c478bd9Sstevel@tonic-gate  *
5817c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5827c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5837c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5847c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5857c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5867c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5877c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5887c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5897c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5907c478bd9Sstevel@tonic-gate  * graceful mounts first.
5917c478bd9Sstevel@tonic-gate  *
5927c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5937c478bd9Sstevel@tonic-gate  */
5947c478bd9Sstevel@tonic-gate static int
595108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	int error = 0;
5987c478bd9Sstevel@tonic-gate 	FILE *mnttab;
5997c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6007c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6017c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6027c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6037c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6047c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6057c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6087c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6097c478bd9Sstevel@tonic-gate 		return (-1);
6107c478bd9Sstevel@tonic-gate 	}
611108322fbScarlsonj 	if (unmount_cmd)
612108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6157c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6167c478bd9Sstevel@tonic-gate 
61745916cd2Sjpk 	/*
61845916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
61945916cd2Sjpk 	 * of our zone's /export/home
62045916cd2Sjpk 	 */
62148451833Scarlsonj 	if (!unmount_cmd)
62245916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
62345916cd2Sjpk 
6247c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6267c478bd9Sstevel@tonic-gate 		return (-1);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6307c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6347c478bd9Sstevel@tonic-gate 		error++;
6357c478bd9Sstevel@tonic-gate 		goto out;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6407c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6417c478bd9Sstevel@tonic-gate 	 */
6427c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6437c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6447c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6457c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6467c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6477c478bd9Sstevel@tonic-gate 		char *path;
6487c478bd9Sstevel@tonic-gate 		uint_t i;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		mnts = NULL;
6517c478bd9Sstevel@tonic-gate 		nmnt = 0;
6527c478bd9Sstevel@tonic-gate 		/*
6537c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6547c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6557c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6567c478bd9Sstevel@tonic-gate 		 * filesystems.
6577c478bd9Sstevel@tonic-gate 		 */
6587c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6597c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6607c478bd9Sstevel@tonic-gate 			error++;
6617c478bd9Sstevel@tonic-gate 			goto out;
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6647c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6657c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6667c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6677c478bd9Sstevel@tonic-gate 			/*
6687c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6697c478bd9Sstevel@tonic-gate 			 *
6707c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6717c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6727c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
6737c478bd9Sstevel@tonic-gate 			 */
6747c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6757c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6767c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6777c478bd9Sstevel@tonic-gate 			}
6787c478bd9Sstevel@tonic-gate 			/*
6797c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6807c478bd9Sstevel@tonic-gate 			 */
6817c478bd9Sstevel@tonic-gate 			if (stuck) {
6827c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6837c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6847c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
6857c478bd9Sstevel@tonic-gate 				} else {
6867c478bd9Sstevel@tonic-gate 					/*
6877c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
6887c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
6897c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
6907c478bd9Sstevel@tonic-gate 					 * bail.
6917c478bd9Sstevel@tonic-gate 					 */
6927c478bd9Sstevel@tonic-gate 					error++;
6937c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6947c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
6957c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
6967c478bd9Sstevel@tonic-gate 					goto out;
6977c478bd9Sstevel@tonic-gate 				}
6987c478bd9Sstevel@tonic-gate 			}
6997c478bd9Sstevel@tonic-gate 			/*
7007c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7017c478bd9Sstevel@tonic-gate 			 */
7027c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7037c478bd9Sstevel@tonic-gate 				newcount++;
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7107c478bd9Sstevel@tonic-gate 			/*
7117c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7127c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7137c478bd9Sstevel@tonic-gate 			 */
7147c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 		/*
7197c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7207c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7217c478bd9Sstevel@tonic-gate 		 */
7227c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7237c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7247c478bd9Sstevel@tonic-gate 			error++;
7257c478bd9Sstevel@tonic-gate 			goto out;
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate out:
7307c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7317c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7327c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate static int
7367c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7397c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7467c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7477c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7487c478bd9Sstevel@tonic-gate  *
7497c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7507c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7517c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7527c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7537c478bd9Sstevel@tonic-gate  * remains the case.
7547c478bd9Sstevel@tonic-gate  */
7557c478bd9Sstevel@tonic-gate static int
7567c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7597c478bd9Sstevel@tonic-gate 	int child_status = 0;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7657c478bd9Sstevel@tonic-gate 	child_pid = fork();
7667c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7677c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
7687c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7697c478bd9Sstevel@tonic-gate 		return (-1);
7707c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
7717c478bd9Sstevel@tonic-gate 		closefrom(0);
7721390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7731390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7741390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7751390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7767c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
7777c478bd9Sstevel@tonic-gate 		/*
7787c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7797c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7807c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7817c478bd9Sstevel@tonic-gate 		 * accordingly.
7827c478bd9Sstevel@tonic-gate 		 */
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7857c478bd9Sstevel@tonic-gate 	} else {
7867c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7907c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7917c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7927c478bd9Sstevel@tonic-gate 		return (-1);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
7957c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
7967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
7977c478bd9Sstevel@tonic-gate 		return (-1);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate static int
80393239addSjohnlev isregfile(const char *path)
80493239addSjohnlev {
80593239addSjohnlev 	struct stat64 st;
80693239addSjohnlev 
80793239addSjohnlev 	if (stat64(path, &st) == -1)
80893239addSjohnlev 		return (-1);
80993239addSjohnlev 
81093239addSjohnlev 	return (S_ISREG(st.st_mode));
81193239addSjohnlev }
81293239addSjohnlev 
81393239addSjohnlev static int
8147c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8177c478bd9Sstevel@tonic-gate 	char *argv[4];
8187c478bd9Sstevel@tonic-gate 	int status;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	/*
8217c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8227c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8237c478bd9Sstevel@tonic-gate 	 */
8247c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8259acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8267c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8277c478bd9Sstevel@tonic-gate 		return (-1);
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 
83093239addSjohnlev 	/*
83193239addSjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
83293239addSjohnlev 	 * in zoneadm.
83393239addSjohnlev 	 */
83493239addSjohnlev 	if (isregfile(cmdbuf) == -1)
83593239addSjohnlev 		return (0);
83693239addSjohnlev 
8377c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
8387c478bd9Sstevel@tonic-gate 	argv[1] = "-m";
8397c478bd9Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
8407c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8437c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8447c478bd9Sstevel@tonic-gate 		return (status);
8457c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8467c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8477c478bd9Sstevel@tonic-gate 	return (-1);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate static int
8517c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8527c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8537c478bd9Sstevel@tonic-gate {
8547c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8557c478bd9Sstevel@tonic-gate 	char *argv[6];
8567c478bd9Sstevel@tonic-gate 	int status;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8607c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8617c478bd9Sstevel@tonic-gate 	 */
8627c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8639acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8657c478bd9Sstevel@tonic-gate 		return (-1);
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
8687c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
8697c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
8707c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
8717c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
8727c478bd9Sstevel@tonic-gate 	} else {
8737c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
8747c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
8757c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
8767c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
8777c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8817c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8827c478bd9Sstevel@tonic-gate 		return (status);
8837c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
8847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8857c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8867c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8877c478bd9Sstevel@tonic-gate 	else
8887c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8897c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8907c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8917c478bd9Sstevel@tonic-gate 	return (-1);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
895d314f035Sedp  * Check if a given mount point path exists.
896d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
897d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
898d314f035Sedp  * component of the mount point path, so it must be a directory.
899d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
900d314f035Sedp  * path, so the mount point itself can be anything aside from a
901d314f035Sedp  * symbolic link.
902d314f035Sedp  *
903d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
904d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
905d314f035Sedp  * If the path doesn't exist return a positive value.
9067c478bd9Sstevel@tonic-gate  */
9077c478bd9Sstevel@tonic-gate static int
908d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9117c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9127c478bd9Sstevel@tonic-gate 	int res;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9157c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
916d314f035Sedp 			return (1);
9177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9187c478bd9Sstevel@tonic-gate 		return (-1);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9227c478bd9Sstevel@tonic-gate 		return (-1);
9237c478bd9Sstevel@tonic-gate 	}
924d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9267c478bd9Sstevel@tonic-gate 		return (-1);
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9307c478bd9Sstevel@tonic-gate 		return (-1);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9337c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9347c478bd9Sstevel@tonic-gate 		/*
935d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9367c478bd9Sstevel@tonic-gate 		 */
9377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9387c478bd9Sstevel@tonic-gate 		return (-1);
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 	return (0);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate /*
944d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
945d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
946d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
947d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
948d314f035Sedp  * object.  (Different filesystems can support different mount points,
949d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
950d314f035Sedp  * while "ufs" just supports directories.)
9517c478bd9Sstevel@tonic-gate  *
952d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
953d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
954d314f035Sedp  * If the path doesn't exist return a positive value.
9557c478bd9Sstevel@tonic-gate  */
956d314f035Sedp int
957d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
958d314f035Sedp     const char *dir, const char *fstype)
9597c478bd9Sstevel@tonic-gate {
960d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
961d314f035Sedp 	int rv;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	/*
964d314f035Sedp 	 * Sanity check the target mount point path.
965d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
9667c478bd9Sstevel@tonic-gate 	 */
967d314f035Sedp 	if (dir[0] != '/') {
968d314f035Sedp 		if (spec[0] == '\0') {
969d314f035Sedp 			/*
970d314f035Sedp 			 * This must be an invalid ipd entry (see comments
971d314f035Sedp 			 * in mount_filesystems_ipdent()).
972d314f035Sedp 			 */
973d314f035Sedp 			zerror(zlogp, B_FALSE,
974d314f035Sedp 			    "invalid inherit-pkg-dir entry: \"%s\"", dir);
975d314f035Sedp 		} else {
976d314f035Sedp 			/* Something went wrong. */
977d314f035Sedp 			zerror(zlogp, B_FALSE, "invalid mount directory, "
978d314f035Sedp 			    "type: \"%s\", special: \"%s\", dir: \"%s\"",
979d314f035Sedp 			    fstype, spec, dir);
980d314f035Sedp 		}
981d314f035Sedp 		return (-1);
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
984d314f035Sedp 	/*
985d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
986d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
987d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
988d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
989d314f035Sedp 	 * cannonical path check in valid_mount_point()).
990d314f035Sedp 	 */
991d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
992d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
993d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
994d314f035Sedp 		    rootpath, dir);
995d314f035Sedp 		return (-1);
996d314f035Sedp 	}
997d314f035Sedp 
998d314f035Sedp 	/*
999d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
1000d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
1001d314f035Sedp 	 */
10027c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10037c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10047c478bd9Sstevel@tonic-gate 	do {
1005d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
10067c478bd9Sstevel@tonic-gate 		*slashp = '\0';
1007d314f035Sedp 		if (slashp_next != NULL) {
1008d314f035Sedp 			/* This is an intermediary mount path component. */
1009d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1010d314f035Sedp 		} else {
1011d314f035Sedp 			/* This is the last component of the mount path. */
1012d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1013d314f035Sedp 		}
1014d314f035Sedp 		if (rv < 0)
1015d314f035Sedp 			return (rv);
10167c478bd9Sstevel@tonic-gate 		*slashp = '/';
1017d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1018d314f035Sedp 	return (rv);
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate static int
10229acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10239acbbeafSnn35248 {
10249acbbeafSnn35248 	di_prof_t prof = arg;
10259acbbeafSnn35248 
10269acbbeafSnn35248 	if (name == NULL)
10279acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
10289acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
10299acbbeafSnn35248 }
10309acbbeafSnn35248 
10319acbbeafSnn35248 static int
10329acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10339acbbeafSnn35248 {
10349acbbeafSnn35248 	di_prof_t prof = arg;
10359acbbeafSnn35248 
10369acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
10379acbbeafSnn35248 }
10389acbbeafSnn35248 
1039f4b3ec61Sdh155122 static int
1040f4b3ec61Sdh155122 get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1041f4b3ec61Sdh155122 {
1042f4b3ec61Sdh155122 	zone_dochandle_t handle;
1043f4b3ec61Sdh155122 
1044f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
1045f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1046f4b3ec61Sdh155122 		return (-1);
1047f4b3ec61Sdh155122 	}
1048f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1049f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
1050f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1051f4b3ec61Sdh155122 		return (-1);
1052f4b3ec61Sdh155122 	}
1053f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1054f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1055f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1056f4b3ec61Sdh155122 		return (-1);
1057f4b3ec61Sdh155122 	}
1058f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1059f4b3ec61Sdh155122 	return (0);
1060f4b3ec61Sdh155122 }
1061f4b3ec61Sdh155122 
10629acbbeafSnn35248 /*
10639acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10649acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10659acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10669acbbeafSnn35248  */
10679acbbeafSnn35248 static int
10680679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10699acbbeafSnn35248 {
10709acbbeafSnn35248 	char			brand[MAXNAMELEN];
10719acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1072123807fbSedp 	brand_handle_t		bh = NULL;
10739acbbeafSnn35248 	struct zone_devtab	ztab;
10749acbbeafSnn35248 	di_prof_t		prof = NULL;
10759acbbeafSnn35248 	int			err;
10769acbbeafSnn35248 	int			retval = -1;
1077f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1078f4b3ec61Sdh155122 	const char 		*curr_iptype;
10799acbbeafSnn35248 
10809acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
10819acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10829acbbeafSnn35248 		goto cleanup;
10839acbbeafSnn35248 	}
10849acbbeafSnn35248 
10850679f794S 	/*
10860679f794S 	 * Get a handle to the brand info for this zone.
10870679f794S 	 * If we are mounting the zone, then we must always use the native
10880679f794S 	 * brand device mounts.
10890679f794S 	 */
10900679f794S 	if (ALT_MOUNT(mount_cmd)) {
10910679f794S 		(void) strlcpy(brand, NATIVE_BRAND_NAME, sizeof (brand));
10920679f794S 	} else {
10930679f794S 		if (zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) {
10940679f794S 			zerror(zlogp, B_FALSE,
10950679f794S 			    "unable to determine zone brand");
10960679f794S 			goto cleanup;
10970679f794S 		}
10980679f794S 	}
10990679f794S 
11000679f794S 	if ((bh = brand_open(brand)) == NULL) {
11019acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
11029acbbeafSnn35248 		goto cleanup;
11039acbbeafSnn35248 	}
11049acbbeafSnn35248 
1105f4b3ec61Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
1106f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1107f4b3ec61Sdh155122 		goto cleanup;
1108f4b3ec61Sdh155122 	}
1109f4b3ec61Sdh155122 	switch (iptype) {
1110f4b3ec61Sdh155122 	case ZS_SHARED:
1111f4b3ec61Sdh155122 		curr_iptype = "shared";
1112f4b3ec61Sdh155122 		break;
1113f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1114f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1115f4b3ec61Sdh155122 		break;
1116f4b3ec61Sdh155122 	}
1117f4b3ec61Sdh155122 
1118123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1119f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11209acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11219acbbeafSnn35248 		goto cleanup;
11229acbbeafSnn35248 	}
11239acbbeafSnn35248 
1124123807fbSedp 	if (brand_platform_iter_link(bh,
11259acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11269acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11279acbbeafSnn35248 		goto cleanup;
11289acbbeafSnn35248 	}
11299acbbeafSnn35248 
11309acbbeafSnn35248 	/* Add user-specified devices and directories */
11319acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11329acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11339acbbeafSnn35248 		goto cleanup;
11349acbbeafSnn35248 	}
11359acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11369acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11379acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11389acbbeafSnn35248 		goto cleanup;
11399acbbeafSnn35248 	}
11409acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
11419acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11429acbbeafSnn35248 		    zonecfg_strerror(err));
11439acbbeafSnn35248 		goto cleanup;
11449acbbeafSnn35248 	}
11459acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11469acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11479acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
11489acbbeafSnn35248 			    "user-specified device");
11499acbbeafSnn35248 			goto cleanup;
11509acbbeafSnn35248 		}
11519acbbeafSnn35248 	}
11529acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
11539acbbeafSnn35248 
11549acbbeafSnn35248 	/* Send profile to kernel */
11559acbbeafSnn35248 	if (di_prof_commit(prof)) {
11569acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11579acbbeafSnn35248 		goto cleanup;
11589acbbeafSnn35248 	}
11599acbbeafSnn35248 
11609acbbeafSnn35248 	retval = 0;
11619acbbeafSnn35248 
11629acbbeafSnn35248 cleanup:
1163123807fbSedp 	if (bh != NULL)
1164123807fbSedp 		brand_close(bh);
1165e767a340Sgjelinek 	if (handle != NULL)
11669acbbeafSnn35248 		zonecfg_fini_handle(handle);
11679acbbeafSnn35248 	if (prof)
11689acbbeafSnn35248 		di_prof_fini(prof);
11699acbbeafSnn35248 	return (retval);
11709acbbeafSnn35248 }
11719acbbeafSnn35248 
11729acbbeafSnn35248 static int
11730679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11740679f794S     zone_mnt_t mount_cmd)
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
1177108322fbScarlsonj 	char specpath[MAXPATHLEN];
11787c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11797c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11809acbbeafSnn35248 	int rv;
11817c478bd9Sstevel@tonic-gate 
1182d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1183d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11857c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11867c478bd9Sstevel@tonic-gate 		return (-1);
1187d314f035Sedp 	} else if (rv > 0) {
1188d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1189d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1190d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1191d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1192d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1193d314f035Sedp 			return (-1);
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 
1196d314f035Sedp 		/*
1197d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1198d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1199d314f035Sedp 		 * to make sure that the mount point path is canonical,
1200d314f035Sedp 		 * which it can only do if the path exists, so now that
1201d314f035Sedp 		 * we've created the path we have to verify it again.
1202d314f035Sedp 		 */
1203d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1204d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1205d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1206d314f035Sedp 			zerror(zlogp, B_FALSE,
1207d314f035Sedp 			    "%s%s is not a valid mount point",
1208d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
12097c478bd9Sstevel@tonic-gate 			return (-1);
1210d314f035Sedp 		}
1211d314f035Sedp 	}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12147c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
12177c478bd9Sstevel@tonic-gate 		/*
12187c478bd9Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1219108322fbScarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1220108322fbScarlsonj 		 * can be seen via the alternate zone's root.
12217c478bd9Sstevel@tonic-gate 		 */
1222108322fbScarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1223108322fbScarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1224108322fbScarlsonj 		    sizeof (specpath)) {
1225108322fbScarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1226108322fbScarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1227108322fbScarlsonj 			return (-1);
1228108322fbScarlsonj 		}
1229108322fbScarlsonj 		if (zonecfg_in_alt_root())
1230108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
12317c478bd9Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1232108322fbScarlsonj 		    specpath, path) != 0) {
12337c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1234108322fbScarlsonj 			    specpath);
12357c478bd9Sstevel@tonic-gate 			return (-1);
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 		return (0);
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	/*
12417c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12427c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12437c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12447c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12457c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12467c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12477c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12487c478bd9Sstevel@tonic-gate 	 */
12497c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12517c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12527c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12537c478bd9Sstevel@tonic-gate 		return (-1);
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	/*
1257108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1258fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1259fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1260fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1261fa3d7ae1Sedp 	 * time we get here.
1262108322fbScarlsonj 	 */
1263108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1264108322fbScarlsonj 		struct stat64 st;
1265108322fbScarlsonj 
1266108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
126768eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1268fa3d7ae1Sedp 			/*
1269fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1270fa3d7ae1Sedp 			 * to check if that device is already mounted
1271fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1272fa3d7ae1Sedp 			 * of the device instead of a direct mount
1273fa3d7ae1Sedp 			 */
127468eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1275108322fbScarlsonj 				return (-1);
127668eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1277fa3d7ae1Sedp 			/*
1278fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1279fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1280fa3d7ae1Sedp 			 * this case in order to get at the underlying
1281fa3d7ae1Sedp 			 * read-write path.
1282fa3d7ae1Sedp 			 */
1283fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1284108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1285108322fbScarlsonj 		}
1286108322fbScarlsonj 	}
1287108322fbScarlsonj 
1288108322fbScarlsonj 	/*
12897c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12907c478bd9Sstevel@tonic-gate 	 */
12917c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
129293239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12937c478bd9Sstevel@tonic-gate 		return (-1);
129493239addSjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
129593239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
129693239addSjohnlev 		return (-1);
129793239addSjohnlev 	}
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	/*
13007c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
13017c478bd9Sstevel@tonic-gate 	 */
13027c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
13037c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
13047c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
13057c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
13067c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
13077c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
13087c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
13097c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
13107c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
13117c478bd9Sstevel@tonic-gate 		}
13127c478bd9Sstevel@tonic-gate 	}
13139acbbeafSnn35248 
13149acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13159acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
13169acbbeafSnn35248 		return (rv);
13179acbbeafSnn35248 
13189acbbeafSnn35248 	/*
13199acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13209acbbeafSnn35248 	 * we're done.
13219acbbeafSnn35248 	 */
13229acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13239acbbeafSnn35248 		return (0);
13249acbbeafSnn35248 
13259acbbeafSnn35248 	/*
13269acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13279acbbeafSnn35248 	 * need to configure it.
13289acbbeafSnn35248 	 */
13290679f794S 	return (mount_one_dev(zlogp, path, mount_cmd));
13307c478bd9Sstevel@tonic-gate }
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate static void
13337c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13347c478bd9Sstevel@tonic-gate {
13357c478bd9Sstevel@tonic-gate 	uint_t i;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13387c478bd9Sstevel@tonic-gate 		return;
13397c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13407c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13417c478bd9Sstevel@tonic-gate 	free(fsarray);
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate 
1344108322fbScarlsonj /*
1345f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1346f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1347f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1348f4368d3dSvp157776  * is done this way because:
1349f4368d3dSvp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
1350f4368d3dSvp157776  * 	We loopback mount zone's own /etc and /var into the root of the
1351f4368d3dSvp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1352f4368d3dSvp157776  * 	need to delay the mount of /var till the zone's root gets populated.
1353f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1354f4368d3dSvp157776  * 	build_mounted_post_var() which gets called only after the zone
1355f4368d3dSvp157776  * 	specific filesystems are mounted.
13566cfd72c6Sgjelinek  *
13576cfd72c6Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13586cfd72c6Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13596cfd72c6Sgjelinek  * scratch zone.
1360108322fbScarlsonj  */
1361108322fbScarlsonj static boolean_t
1362f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
136316bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1364108322fbScarlsonj {
1365108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1366108322fbScarlsonj 	const char **cpp;
1367108322fbScarlsonj 	static const char *mkdirs[] = {
13683f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13693f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1370108322fbScarlsonj 	};
1371108322fbScarlsonj 	char *altstr;
1372f4368d3dSvp157776 	FILE *fp;
1373108322fbScarlsonj 	uuid_t uuid;
1374108322fbScarlsonj 
1375108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
137616bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
137716bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1378108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1379108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1380108322fbScarlsonj 
1381108322fbScarlsonj 	/*
1382108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1383108322fbScarlsonj 	 * zone_mount_early.)
1384108322fbScarlsonj 	 */
1385108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1386108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1387108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1388108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1389108322fbScarlsonj 			return (B_FALSE);
1390108322fbScarlsonj 		}
1391108322fbScarlsonj 	}
1392f4368d3dSvp157776 	/*
1393f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1394f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1395f4368d3dSvp157776 	 * read-only inside the scratch zone.
1396f4368d3dSvp157776 	 */
1397f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1398f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1399f4368d3dSvp157776 	if (altstr == NULL) {
1400f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1401f4368d3dSvp157776 		return (B_FALSE);
1402f4368d3dSvp157776 	}
1403f4368d3dSvp157776 	zonecfg_set_root("");
1404f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1405f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1406f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1407f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1408f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1409f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1410f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1411f4368d3dSvp157776 			return (B_FALSE);
1412f4368d3dSvp157776 		}
1413f4368d3dSvp157776 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1414f4368d3dSvp157776 		    tmp) != 0) {
1415f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1416f4368d3dSvp157776 			    fromdir);
1417f4368d3dSvp157776 			return (B_FALSE);
1418f4368d3dSvp157776 		}
1419f4368d3dSvp157776 	}
1420f4368d3dSvp157776 	zonecfg_set_root(altstr);
1421f4368d3dSvp157776 	free(altstr);
1422f4368d3dSvp157776 
1423f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1424f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1425f4368d3dSvp157776 		return (B_FALSE);
1426f4368d3dSvp157776 	}
1427f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1428f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1429f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1430f4368d3dSvp157776 	}
1431f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1432f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1433f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1434f4368d3dSvp157776 		return (B_FALSE);
1435f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1436f4368d3dSvp157776 	return (B_TRUE);
1437f4368d3dSvp157776 }
1438f4368d3dSvp157776 
1439f4368d3dSvp157776 
1440f4368d3dSvp157776 static boolean_t
14416cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14426cfd72c6Sgjelinek     const char *luroot)
1443f4368d3dSvp157776 {
1444f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1445f4368d3dSvp157776 	const char **cpp;
14466cfd72c6Sgjelinek 	const char **loopdirs;
14476cfd72c6Sgjelinek 	const char **tmpdirs;
1448f4368d3dSvp157776 	static const char *localdirs[] = {
1449f4368d3dSvp157776 		"/etc", "/var", NULL
1450f4368d3dSvp157776 	};
14516cfd72c6Sgjelinek 	static const char *scr_loopdirs[] = {
1452f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1453f4368d3dSvp157776 		"/usr", NULL
1454f4368d3dSvp157776 	};
14556cfd72c6Sgjelinek 	static const char *upd_loopdirs[] = {
14566cfd72c6Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14576cfd72c6Sgjelinek 		"/usr", "/var", NULL
14586cfd72c6Sgjelinek 	};
14596cfd72c6Sgjelinek 	static const char *scr_tmpdirs[] = {
1460f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1461f4368d3dSvp157776 	};
14626cfd72c6Sgjelinek 	static const char *upd_tmpdirs[] = {
14636cfd72c6Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14646cfd72c6Sgjelinek 	};
1465f4368d3dSvp157776 	struct stat st;
1466f4368d3dSvp157776 
14676cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
1468108322fbScarlsonj 		/*
14696cfd72c6Sgjelinek 		 * These are mounted read-write from the zone undergoing
14706cfd72c6Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14716cfd72c6Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
1472108322fbScarlsonj 		 */
1473108322fbScarlsonj 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14746cfd72c6Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1475108322fbScarlsonj 			    *cpp);
14766cfd72c6Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14776cfd72c6Sgjelinek 			    rootpath, *cpp);
1478108322fbScarlsonj 			if (mkdir(tmp, 0755) != 0) {
1479108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1480108322fbScarlsonj 				return (B_FALSE);
1481108322fbScarlsonj 			}
14826cfd72c6Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14836cfd72c6Sgjelinek 			    != 0) {
14846cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14856cfd72c6Sgjelinek 				    tmp, *cpp);
1486108322fbScarlsonj 				return (B_FALSE);
1487108322fbScarlsonj 			}
1488108322fbScarlsonj 		}
14896cfd72c6Sgjelinek 	}
14906cfd72c6Sgjelinek 
14916cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14926cfd72c6Sgjelinek 		loopdirs = upd_loopdirs;
14936cfd72c6Sgjelinek 	else
14946cfd72c6Sgjelinek 		loopdirs = scr_loopdirs;
1495108322fbScarlsonj 
1496108322fbScarlsonj 	/*
1497108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1498108322fbScarlsonj 	 * they contain binaries that must match system.
1499108322fbScarlsonj 	 */
1500108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1501108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1502108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1503108322fbScarlsonj 			if (errno != EEXIST) {
1504108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1505108322fbScarlsonj 				return (B_FALSE);
1506108322fbScarlsonj 			}
1507108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1508108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1509108322fbScarlsonj 				return (B_FALSE);
1510108322fbScarlsonj 			}
1511108322fbScarlsonj 			/*
1512108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1513108322fbScarlsonj 			 * things that have been converted into symlinks
1514108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1515108322fbScarlsonj 			 * fixup.
1516108322fbScarlsonj 			 */
1517108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1518108322fbScarlsonj 				continue;
1519108322fbScarlsonj 		}
1520108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1521108322fbScarlsonj 		    tmp) != 0) {
1522108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1523108322fbScarlsonj 			    *cpp);
1524108322fbScarlsonj 			return (B_FALSE);
1525108322fbScarlsonj 		}
1526108322fbScarlsonj 	}
1527108322fbScarlsonj 
15286cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15296cfd72c6Sgjelinek 		tmpdirs = upd_tmpdirs;
15306cfd72c6Sgjelinek 	else
15316cfd72c6Sgjelinek 		tmpdirs = scr_tmpdirs;
15326cfd72c6Sgjelinek 
1533108322fbScarlsonj 	/*
1534108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1535108322fbScarlsonj 	 */
1536108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1537108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15386cfd72c6Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15396cfd72c6Sgjelinek 		    errno != EEXIST) {
1540108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1541108322fbScarlsonj 			return (B_FALSE);
1542108322fbScarlsonj 		}
1543d30e996aSgjelinek 
1544d30e996aSgjelinek 		/*
1545d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1546d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1547d30e996aSgjelinek 		 * the correct mode for /tmp now.
1548d30e996aSgjelinek 		 */
1549d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1550d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1551d30e996aSgjelinek 			return (B_FALSE);
1552d30e996aSgjelinek 		}
1553d30e996aSgjelinek 
1554108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1555108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1556108322fbScarlsonj 			return (B_FALSE);
1557108322fbScarlsonj 		}
1558108322fbScarlsonj 	}
1559108322fbScarlsonj 	return (B_TRUE);
1560108322fbScarlsonj }
1561108322fbScarlsonj 
15629acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15639acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
15649acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
15659acbbeafSnn35248 	int			*pgcd_num_fs;
15669acbbeafSnn35248 } plat_gmount_cb_data_t;
15679acbbeafSnn35248 
15689acbbeafSnn35248 /*
15699acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15709acbbeafSnn35248  * through all global brand platform mounts.
15719acbbeafSnn35248  */
15729acbbeafSnn35248 int
15739acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15749acbbeafSnn35248     const char *fstype, const char *opt)
15759acbbeafSnn35248 {
15769acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
15779acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15789acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15799acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
15809acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15819acbbeafSnn35248 
15829acbbeafSnn35248 	num_fs++;
15839acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15849acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15859acbbeafSnn35248 		return (-1);
15869acbbeafSnn35248 	}
15879acbbeafSnn35248 
15889acbbeafSnn35248 	fs_ptr = tmp_ptr;
15899acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
15909acbbeafSnn35248 
15919acbbeafSnn35248 	/* update the callback struct passed in */
15929acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
15939acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
15949acbbeafSnn35248 
15959acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
15969acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15979acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
15989acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15999acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
16009acbbeafSnn35248 	fsp->zone_fs_options = NULL;
1601fa3d7ae1Sedp 	if ((opt != NULL) &&
1602fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
16039acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
16049acbbeafSnn35248 		return (-1);
16059acbbeafSnn35248 	}
16069acbbeafSnn35248 
16079acbbeafSnn35248 	return (0);
16089acbbeafSnn35248 }
16099acbbeafSnn35248 
16109acbbeafSnn35248 static int
16119acbbeafSnn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
16129acbbeafSnn35248     struct zone_fstab **fs_tabp, int *num_fsp)
16139acbbeafSnn35248 {
16149acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16159acbbeafSnn35248 	int num_fs;
16169acbbeafSnn35248 
16179acbbeafSnn35248 	num_fs = *num_fsp;
16189acbbeafSnn35248 	fs_ptr = *fs_tabp;
16199acbbeafSnn35248 
16209acbbeafSnn35248 	if (zonecfg_setipdent(handle) != Z_OK) {
16219acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16229acbbeafSnn35248 		return (-1);
16239acbbeafSnn35248 	}
16249acbbeafSnn35248 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
16259acbbeafSnn35248 		num_fs++;
16269acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16279acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16289acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16299acbbeafSnn35248 			(void) zonecfg_endipdent(handle);
16309acbbeafSnn35248 			return (-1);
16319acbbeafSnn35248 		}
16329acbbeafSnn35248 
16339acbbeafSnn35248 		/* update the pointers passed in */
16349acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16359acbbeafSnn35248 		*num_fsp = num_fs;
16369acbbeafSnn35248 
16379acbbeafSnn35248 		/*
16389acbbeafSnn35248 		 * IPDs logically only have a mount point; all other properties
16399acbbeafSnn35248 		 * are implied.
16409acbbeafSnn35248 		 */
16419acbbeafSnn35248 		fs_ptr = tmp_ptr;
16429acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16439acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16449acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16459acbbeafSnn35248 		fsp->zone_fs_special[0] = '\0';
16469acbbeafSnn35248 		fsp->zone_fs_raw[0] = '\0';
16479acbbeafSnn35248 		fsp->zone_fs_type[0] = '\0';
16489acbbeafSnn35248 		fsp->zone_fs_options = NULL;
16499acbbeafSnn35248 	}
16509acbbeafSnn35248 	(void) zonecfg_endipdent(handle);
16519acbbeafSnn35248 	return (0);
16529acbbeafSnn35248 }
16539acbbeafSnn35248 
16549acbbeafSnn35248 static int
16559acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16566cfd72c6Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16579acbbeafSnn35248 {
16589acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16599acbbeafSnn35248 	int num_fs;
16609acbbeafSnn35248 
16619acbbeafSnn35248 	num_fs = *num_fsp;
16629acbbeafSnn35248 	fs_ptr = *fs_tabp;
16639acbbeafSnn35248 
16649acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16659acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16669acbbeafSnn35248 		return (-1);
16679acbbeafSnn35248 	}
16689acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16699acbbeafSnn35248 		/*
16709acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
16719acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
16729acbbeafSnn35248 		 * case.
16739acbbeafSnn35248 		 */
16746cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16756cfd72c6Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16769acbbeafSnn35248 			continue;
16779acbbeafSnn35248 
16789acbbeafSnn35248 		num_fs++;
16799acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16809acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16819acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16829acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
16839acbbeafSnn35248 			return (-1);
16849acbbeafSnn35248 		}
16859acbbeafSnn35248 		/* update the pointers passed in */
16869acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16879acbbeafSnn35248 		*num_fsp = num_fs;
16889acbbeafSnn35248 
16899acbbeafSnn35248 		fs_ptr = tmp_ptr;
16909acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16919acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16929acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16939acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16949acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
16959acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16969acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
16979acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
1698fa3d7ae1Sedp 
1699fa3d7ae1Sedp 		/*
1700fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1701fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1702fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1703fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1704fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1705fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1706fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1707fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1708fa3d7ae1Sedp 		 */
1709fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1710fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1711fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1712fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1713fa3d7ae1Sedp 		}
1714fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1715fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
17169acbbeafSnn35248 	}
17179acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
17189acbbeafSnn35248 	return (0);
17199acbbeafSnn35248 }
17209acbbeafSnn35248 
17217c478bd9Sstevel@tonic-gate static int
17226cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
17257c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
17269acbbeafSnn35248 	char brand[MAXNAMELEN];
172716bca938Svp157776 	char luroot[MAXPATHLEN];
17289acbbeafSnn35248 	int i, num_fs = 0;
17299acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
17307c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
17317c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1732123807fbSedp 	brand_handle_t bh;
17339acbbeafSnn35248 	plat_gmount_cb_data_t cb;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1736108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
17377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1738108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1739108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1740108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
17417c478bd9Sstevel@tonic-gate 		goto bad;
17427c478bd9Sstevel@tonic-gate 	}
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
17457c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
17467c478bd9Sstevel@tonic-gate 		goto bad;
17477c478bd9Sstevel@tonic-gate 	}
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
17507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
17517c478bd9Sstevel@tonic-gate 		goto bad;
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1755ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17567c478bd9Sstevel@tonic-gate 		goto bad;
17577c478bd9Sstevel@tonic-gate 	}
17587c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17597c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17607c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17617c478bd9Sstevel@tonic-gate 		goto bad;
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 
17640679f794S 	/*
17650679f794S 	 * If we are mounting the zone, then we must always use the native
17660679f794S 	 * brand global mounts.
17670679f794S 	 */
17680679f794S 	if (ALT_MOUNT(mount_cmd)) {
17690679f794S 		(void) strlcpy(brand, NATIVE_BRAND_NAME, sizeof (brand));
17700679f794S 	} else {
17710679f794S 		if (zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) {
17720679f794S 			zerror(zlogp, B_FALSE,
17730679f794S 			    "unable to determine zone brand");
17740679f794S 			zonecfg_fini_handle(handle);
17750679f794S 			return (-1);
17760679f794S 		}
17770679f794S 	}
17780679f794S 
17799acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
17800679f794S 	if ((bh = brand_open(brand)) == NULL) {
17819acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1782e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17839acbbeafSnn35248 		return (-1);
17849acbbeafSnn35248 	}
17859acbbeafSnn35248 
17869acbbeafSnn35248 	/*
17879acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
17889acbbeafSnn35248 	 * configuration.
17899acbbeafSnn35248 	 */
17909acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
17919acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
17929acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1793123807fbSedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17949acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
17959acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1796123807fbSedp 		brand_close(bh);
1797e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17989acbbeafSnn35248 		return (-1);
17999acbbeafSnn35248 	}
1800123807fbSedp 	brand_close(bh);
18019acbbeafSnn35248 
18027c478bd9Sstevel@tonic-gate 	/*
18037c478bd9Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
18047c478bd9Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
18057c478bd9Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
18067c478bd9Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
18077c478bd9Sstevel@tonic-gate 	 */
18089acbbeafSnn35248 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
18097c478bd9Sstevel@tonic-gate 		goto bad;
18107c478bd9Sstevel@tonic-gate 
18119acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
18129acbbeafSnn35248 	    mount_cmd) != 0)
18137c478bd9Sstevel@tonic-gate 		goto bad;
1814fa9e4066Sahrens 
18157c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
18167c478bd9Sstevel@tonic-gate 	handle = NULL;
18177c478bd9Sstevel@tonic-gate 
1818108322fbScarlsonj 	/*
18199acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
18209acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
18219acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
18229acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
18239acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
18249acbbeafSnn35248 	 * the zones filesystems there instead.
18259acbbeafSnn35248 	 *
18269acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
18279acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
18289acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
18299acbbeafSnn35248 	 *
18309acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
18319acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
18329acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
18339acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
18349acbbeafSnn35248 	 * upgrade environment.)
18359acbbeafSnn35248 	 *
18369acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
18379acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
18389acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
18399acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
18409acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
18419acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
18429acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
18439acbbeafSnn35248 	 *
18449acbbeafSnn35248 	 * All this work is done in three phases:
1845f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1846f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1847f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1848f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1849108322fbScarlsonj 	 */
18506cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
185116bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1852108322fbScarlsonj 		goto bad;
1853108322fbScarlsonj 
18547c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
18559acbbeafSnn35248 
18567c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
18576cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
18589acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
18599acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
18609acbbeafSnn35248 
18619acbbeafSnn35248 			/*
18629acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
18639acbbeafSnn35248 			 * but /dev is special and always goes at the top
18649acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
18659acbbeafSnn35248 			 */
18669acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18679acbbeafSnn35248 			rootpath[slen] = '\0';
18680679f794S 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18690679f794S 			    != 0)
18709acbbeafSnn35248 				goto bad;
18719acbbeafSnn35248 			rootpath[slen] = '/';
18729acbbeafSnn35248 			continue;
18739acbbeafSnn35248 		}
18740679f794S 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18757c478bd9Sstevel@tonic-gate 			goto bad;
18767c478bd9Sstevel@tonic-gate 	}
18776cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18786cfd72c6Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1879f4368d3dSvp157776 		goto bad;
188045916cd2Sjpk 
188145916cd2Sjpk 	/*
188245916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
188345916cd2Sjpk 	 */
18846cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18856cfd72c6Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
188645916cd2Sjpk 		goto bad;
188745916cd2Sjpk 
18887c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 	/*
18917c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18927c478bd9Sstevel@tonic-gate 	 */
18937c478bd9Sstevel@tonic-gate 	return (0);
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate bad:
18967c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18977c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18987c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18997c478bd9Sstevel@tonic-gate 	return (-1);
19007c478bd9Sstevel@tonic-gate }
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
19037c478bd9Sstevel@tonic-gate static int
19047c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
19057c478bd9Sstevel@tonic-gate {
19067c478bd9Sstevel@tonic-gate 	int prefixlen;
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
19097c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
19107c478bd9Sstevel@tonic-gate 		return (1);
19117c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
19127c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
19137c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
19147c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
19157c478bd9Sstevel@tonic-gate 			continue;
19167c478bd9Sstevel@tonic-gate 		}
19177c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
19187c478bd9Sstevel@tonic-gate 		prefixlen--;
19197c478bd9Sstevel@tonic-gate 	}
19207c478bd9Sstevel@tonic-gate 	return (0);
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate /*
19247c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
19257c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
19267c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
19277c478bd9Sstevel@tonic-gate  *
19287c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
19297c478bd9Sstevel@tonic-gate  */
19307c478bd9Sstevel@tonic-gate static int
1931f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
19327c478bd9Sstevel@tonic-gate {
19337c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
19347c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
19357c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
19367c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
19377c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
19387c478bd9Sstevel@tonic-gate 	uint_t bufsize;
19397c478bd9Sstevel@tonic-gate 	char *buf = NULL;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
19427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
19437c478bd9Sstevel@tonic-gate 		ret_code = -1;
19447c478bd9Sstevel@tonic-gate 		goto bad;
19457c478bd9Sstevel@tonic-gate 	}
19467c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
19477c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
19487c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
19497c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1950f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
19517c478bd9Sstevel@tonic-gate 		ret_code = -1;
19527c478bd9Sstevel@tonic-gate 		goto bad;
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
19557c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
19567c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
19577c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
19587c478bd9Sstevel@tonic-gate 		ret_code = -1;
19597c478bd9Sstevel@tonic-gate 		goto bad;
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
19627c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19637c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19647c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19657c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1966f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1967f4b3ec61Sdh155122 		    "interfaces");
19687c478bd9Sstevel@tonic-gate 		ret_code = -1;
19697c478bd9Sstevel@tonic-gate 		goto bad;
19707c478bd9Sstevel@tonic-gate 	}
19717c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19727c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19737c478bd9Sstevel@tonic-gate 		(void) close(s);
19747c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19757c478bd9Sstevel@tonic-gate 		    0) {
19767c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19777c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
19787c478bd9Sstevel@tonic-gate 			ret_code = -1;
19797c478bd9Sstevel@tonic-gate 			continue;
19807c478bd9Sstevel@tonic-gate 		}
19817c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19827c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19837c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19847c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1985c1c0ebd5Ssl108498 			if (errno == ENXIO)
1986c1c0ebd5Ssl108498 				/*
1987c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1988c1c0ebd5Ssl108498 				 * another zone halting.
1989c1c0ebd5Ssl108498 				 */
1990c1c0ebd5Ssl108498 				continue;
19917c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1992c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1993f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19947c478bd9Sstevel@tonic-gate 			ret_code = -1;
19957c478bd9Sstevel@tonic-gate 			continue;
19967c478bd9Sstevel@tonic-gate 		}
19977c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19987c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19997c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
2000f4b3ec61Sdh155122 				    "%s: could not remove network interface",
20017c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
20027c478bd9Sstevel@tonic-gate 				ret_code = -1;
20037c478bd9Sstevel@tonic-gate 				continue;
20047c478bd9Sstevel@tonic-gate 			}
20057c478bd9Sstevel@tonic-gate 		}
20067c478bd9Sstevel@tonic-gate 	}
20077c478bd9Sstevel@tonic-gate bad:
20087c478bd9Sstevel@tonic-gate 	if (s > 0)
20097c478bd9Sstevel@tonic-gate 		(void) close(s);
20107c478bd9Sstevel@tonic-gate 	if (buf)
20117c478bd9Sstevel@tonic-gate 		free(buf);
20127c478bd9Sstevel@tonic-gate 	return (ret_code);
20137c478bd9Sstevel@tonic-gate }
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate static union	sockunion {
20167c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
20177c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
20187c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
20197c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
20207c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate static struct {
20237c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
20247c478bd9Sstevel@tonic-gate 	char	space[512];
20257c478bd9Sstevel@tonic-gate } rtmsg;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate static int
20287c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
20297c478bd9Sstevel@tonic-gate {
20307c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
20317c478bd9Sstevel@tonic-gate 	case AF_INET:
20327c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
20337c478bd9Sstevel@tonic-gate 	case AF_LINK:
20347c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
20357c478bd9Sstevel@tonic-gate 	case AF_INET6:
20367c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
20377c478bd9Sstevel@tonic-gate 	default:
20387c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
20397c478bd9Sstevel@tonic-gate 	}
20407c478bd9Sstevel@tonic-gate }
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
20437c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /*
20467c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
20477c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
20487c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
20497c478bd9Sstevel@tonic-gate  *
20507c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
20517c478bd9Sstevel@tonic-gate  *
20527c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
20537c478bd9Sstevel@tonic-gate  *
20547c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
20557c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
20567c478bd9Sstevel@tonic-gate  */
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate static char *
20597c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
20627c478bd9Sstevel@tonic-gate 	pid_t pid;
20637c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
20647c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
20657c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20667c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
20677c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	pid = getpid();
20727c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20747c478bd9Sstevel@tonic-gate 		return (NULL);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20787c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20817c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20827c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20837c478bd9Sstevel@tonic-gate 	} else {
20847c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20877c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20887c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20947c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20957c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20967c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20977c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20987c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
21017c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
21027c478bd9Sstevel@tonic-gate 	cp += l;
21037c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
21047c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
21057c478bd9Sstevel@tonic-gate 	cp += l;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
21107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
21117c478bd9Sstevel@tonic-gate 		return (NULL);
21127c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
21137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
21147c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
21157c478bd9Sstevel@tonic-gate 		return (NULL);
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate 	do {
21187c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
21197c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
21207c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
21217c478bd9Sstevel@tonic-gate 	if (l < 0) {
21227c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
21237c478bd9Sstevel@tonic-gate 		return (NULL);
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
21277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
21287c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
21297c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
21307c478bd9Sstevel@tonic-gate 		return (NULL);
21317c478bd9Sstevel@tonic-gate 	}
21327c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
21337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
21347c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
21357c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
21367c478bd9Sstevel@tonic-gate 		return (NULL);
21377c478bd9Sstevel@tonic-gate 	}
21387c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
21397c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
21407c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
21417c478bd9Sstevel@tonic-gate 		return (NULL);
21427c478bd9Sstevel@tonic-gate 	}
21437c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2144f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
21457c478bd9Sstevel@tonic-gate 		return (NULL);
21467c478bd9Sstevel@tonic-gate 	}
21477c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
21487c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
21497c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
21507c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
21517c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
21527c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
21537c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
21547c478bd9Sstevel@tonic-gate 			continue;
21557c478bd9Sstevel@tonic-gate 		}
21567c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
21577c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
21587c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
21597c478bd9Sstevel@tonic-gate 		break;
21607c478bd9Sstevel@tonic-gate 	}
21617c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2162f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2163f4b3ec61Sdh155122 		    "determined");
21647c478bd9Sstevel@tonic-gate 		return (NULL);
21657c478bd9Sstevel@tonic-gate 	}
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	/*
21687c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21697c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21707c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21717c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21727c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21737c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21747c478bd9Sstevel@tonic-gate 	 */
21757c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21767c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21777c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21787c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21797c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21807c478bd9Sstevel@tonic-gate 	if (i < 0) {
21817c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2182f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2183f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
21847c478bd9Sstevel@tonic-gate 		return (NULL);
21857c478bd9Sstevel@tonic-gate 	}
21867c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21877c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21887c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21917c478bd9Sstevel@tonic-gate 		return (answer);
21927c478bd9Sstevel@tonic-gate 	return (NULL);
21937c478bd9Sstevel@tonic-gate }
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate /*
21967c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21977c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21987c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21997c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
22007c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
22017c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
22027c478bd9Sstevel@tonic-gate  *
22037c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
22047c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
22057c478bd9Sstevel@tonic-gate  */
22067c478bd9Sstevel@tonic-gate static int
22077c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2208*f14f3ae7Sjv227347     struct zone_nwiftab *nwiftabptr)
22097c478bd9Sstevel@tonic-gate {
22107c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
22117c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
22127c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
22137c478bd9Sstevel@tonic-gate 	struct in_addr in4;
22147c478bd9Sstevel@tonic-gate 	sa_family_t af;
22157c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
22167c478bd9Sstevel@tonic-gate 	int s;
22177c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
22187c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
22197c478bd9Sstevel@tonic-gate 	int res;
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
22227c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
22237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
22247c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
22257c478bd9Sstevel@tonic-gate 		return (-1);
22267c478bd9Sstevel@tonic-gate 	}
22277c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
22287c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22297c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
22307c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
22317c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
22327c478bd9Sstevel@tonic-gate 		return (-1);
22337c478bd9Sstevel@tonic-gate 	}
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
22367c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
22377c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
223822321485Svp157776 		/*
223922321485Svp157776 		 * Here, we know that the interface can't be brought up.
224022321485Svp157776 		 * A similar warning message was already printed out to
224122321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
224222321485Svp157776 		 * message to syslog and continue.
224322321485Svp157776 		 */
2244f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
224522321485Svp157776 		    "'%s' which may not be present/plumbed in the "
224622321485Svp157776 		    "global zone.", lifr.lifr_name);
22477c478bd9Sstevel@tonic-gate 		(void) close(s);
224822321485Svp157776 		return (Z_OK);
22497c478bd9Sstevel@tonic-gate 	}
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22527c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
22537c478bd9Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
22547c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22557c478bd9Sstevel@tonic-gate 		goto bad;
22567c478bd9Sstevel@tonic-gate 	}
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22597c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22607c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22637c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2264f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2265f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
22667c478bd9Sstevel@tonic-gate 		goto bad;
22677c478bd9Sstevel@tonic-gate 	}
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
22707c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
22717c478bd9Sstevel@tonic-gate 	} else {
22727c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
22737c478bd9Sstevel@tonic-gate 			/*
22747c478bd9Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
22757c478bd9Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
22767c478bd9Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
22777c478bd9Sstevel@tonic-gate 			 * being able to determine it is a common failure,
22787c478bd9Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
22797c478bd9Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
22807c478bd9Sstevel@tonic-gate 			 * printed after the rest of the interface's
22817c478bd9Sstevel@tonic-gate 			 * parameters have been configured.
22827c478bd9Sstevel@tonic-gate 			 */
22837c478bd9Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
22847c478bd9Sstevel@tonic-gate 			if (slashp != NULL) {
22857c478bd9Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22867c478bd9Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
22877c478bd9Sstevel@tonic-gate 					*slashp = '/';
22887c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
22897c478bd9Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
22907c478bd9Sstevel@tonic-gate 					    lifr.lifr_name,
22917c478bd9Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
22927c478bd9Sstevel@tonic-gate 					goto bad;
22937c478bd9Sstevel@tonic-gate 				}
22947c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
22957c478bd9Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
22967c478bd9Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
22977c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
22987c478bd9Sstevel@tonic-gate 			}
22997c478bd9Sstevel@tonic-gate 			if (got_netmask) {
23007c478bd9Sstevel@tonic-gate 				netmask4.sin_family = af;
23017c478bd9Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
23027c478bd9Sstevel@tonic-gate 				    sizeof (netmask4));
23037c478bd9Sstevel@tonic-gate 			}
23047c478bd9Sstevel@tonic-gate 		} else {
23057c478bd9Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
23067c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
23077c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
23087c478bd9Sstevel@tonic-gate 				*slashp = '/';
23097c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
23107c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
23117c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
23127c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
23137c478bd9Sstevel@tonic-gate 				goto bad;
23147c478bd9Sstevel@tonic-gate 			}
23157c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
23167c478bd9Sstevel@tonic-gate 			netmask6.sin6_family = af;
23177c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
23187c478bd9Sstevel@tonic-gate 			    sizeof (netmask6));
23197c478bd9Sstevel@tonic-gate 		}
23207c478bd9Sstevel@tonic-gate 		if (got_netmask &&
23217c478bd9Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
23227c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
23237c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23247c478bd9Sstevel@tonic-gate 			goto bad;
23257c478bd9Sstevel@tonic-gate 		}
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 		/*
23287c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
23297c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
23307c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
23317c478bd9Sstevel@tonic-gate 		 */
23327c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23337c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23347c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23357c478bd9Sstevel@tonic-gate 			goto bad;
23367c478bd9Sstevel@tonic-gate 		}
23377c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
23387c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23397c478bd9Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
23407c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23417c478bd9Sstevel@tonic-gate 			goto bad;
23427c478bd9Sstevel@tonic-gate 		}
23437c478bd9Sstevel@tonic-gate 	}
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
23477c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
23487c478bd9Sstevel@tonic-gate 		goto bad;
23497c478bd9Sstevel@tonic-gate 	}
23507c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
23517c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
23527c478bd9Sstevel@tonic-gate 		int save_errno = errno;
23537c478bd9Sstevel@tonic-gate 		char *zone_using;
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 		/*
23567c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
23577c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
23587c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23597c478bd9Sstevel@tonic-gate 		 * using that address.
23607c478bd9Sstevel@tonic-gate 		 */
23617c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23627c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2363f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2364f4b3ec61Sdh155122 			    lifr.lifr_name);
23657c478bd9Sstevel@tonic-gate 			goto bad;
23667c478bd9Sstevel@tonic-gate 		}
23677c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23687c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23697c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23707c478bd9Sstevel@tonic-gate 			goto bad;
23717c478bd9Sstevel@tonic-gate 		}
23727c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23737c478bd9Sstevel@tonic-gate 		errno = save_errno;
23747c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
23757c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2376f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2377f4b3ec61Sdh155122 			    lifr.lifr_name);
23787c478bd9Sstevel@tonic-gate 		else
2379f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2380f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2381f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
23827c478bd9Sstevel@tonic-gate 		goto bad;
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	if (!got_netmask) {
23867c478bd9Sstevel@tonic-gate 		/*
23877c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23887c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23897c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23907c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23917c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23927c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23937c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23947c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23957c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23967c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
23977c478bd9Sstevel@tonic-gate 		 */
23987c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23997c478bd9Sstevel@tonic-gate 		void  *addr;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
24027c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
24037c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
24047c478bd9Sstevel@tonic-gate 		else
24057c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
24067c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
24097c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
24107c478bd9Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
24117c478bd9Sstevel@tonic-gate 			goto bad;
24127c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
24137c478bd9Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
24147c478bd9Sstevel@tonic-gate 		    "%s; using default of %s.",
24157c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate 
2418de860bd9Sgfaden 	/*
2419de860bd9Sgfaden 	 * If a default router was specified for this interface
2420de860bd9Sgfaden 	 * set the route now. Ignore if already set.
2421de860bd9Sgfaden 	 */
2422de860bd9Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2423de860bd9Sgfaden 		int status;
2424de860bd9Sgfaden 		char *argv[7];
2425de860bd9Sgfaden 
2426de860bd9Sgfaden 		argv[0] = "route";
2427de860bd9Sgfaden 		argv[1] = "add";
2428de860bd9Sgfaden 		argv[2] = "-ifp";
2429de860bd9Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
2430de860bd9Sgfaden 		argv[4] = "default";
2431de860bd9Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2432de860bd9Sgfaden 		argv[6] = NULL;
2433de860bd9Sgfaden 
2434de860bd9Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2435de860bd9Sgfaden 		if (status != 0 && status != EEXIST)
2436de860bd9Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
2437de860bd9Sgfaden 			    "interface %s to %s\n",
2438de860bd9Sgfaden 			    nwiftabptr->zone_nwif_physical,
2439de860bd9Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
2440de860bd9Sgfaden 	}
2441de860bd9Sgfaden 
24427c478bd9Sstevel@tonic-gate 	(void) close(s);
24437c478bd9Sstevel@tonic-gate 	return (Z_OK);
24447c478bd9Sstevel@tonic-gate bad:
24457c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
24467c478bd9Sstevel@tonic-gate 	(void) close(s);
24477c478bd9Sstevel@tonic-gate 	return (-1);
24487c478bd9Sstevel@tonic-gate }
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate /*
24517c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
2452*f14f3ae7Sjv227347  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2453*f14f3ae7Sjv227347  * system.
24547c478bd9Sstevel@tonic-gate  *
24557c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24567c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
24577c478bd9Sstevel@tonic-gate  */
24587c478bd9Sstevel@tonic-gate static int
2459f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24607c478bd9Sstevel@tonic-gate {
24617c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24627c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24637c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24677c478bd9Sstevel@tonic-gate 		return (-1);
24687c478bd9Sstevel@tonic-gate 	}
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24727c478bd9Sstevel@tonic-gate 		return (-1);
24737c478bd9Sstevel@tonic-gate 	}
24747c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24757c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24767c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24777c478bd9Sstevel@tonic-gate 		return (-1);
24787c478bd9Sstevel@tonic-gate 	}
24797c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24807c478bd9Sstevel@tonic-gate 		for (;;) {
24817c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24827c478bd9Sstevel@tonic-gate 				break;
2483*f14f3ae7Sjv227347 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24847c478bd9Sstevel@tonic-gate 			    Z_OK) {
24857c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24867c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24877c478bd9Sstevel@tonic-gate 				return (-1);
24887c478bd9Sstevel@tonic-gate 			}
24897c478bd9Sstevel@tonic-gate 		}
24907c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24917c478bd9Sstevel@tonic-gate 	}
24927c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
2493fdb7141fSgfaden 	if (is_system_labeled()) {
2494fdb7141fSgfaden 		/*
2495fdb7141fSgfaden 		 * Labeled zones share the loopback interface
2496fdb7141fSgfaden 		 * so it is not plumbed for shared stack instances.
2497fdb7141fSgfaden 		 */
2498fdb7141fSgfaden 		return (0);
2499fdb7141fSgfaden 	}
25007c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
25017c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
25027c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
25037c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2504442d3e9eSgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2505*f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
25067c478bd9Sstevel@tonic-gate 		return (-1);
2507*f14f3ae7Sjv227347 
2508*f14f3ae7Sjv227347 	/* Always plumb up the IPv6 loopback interface. */
25097c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
25107c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2511*f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
25127c478bd9Sstevel@tonic-gate 		return (-1);
25137c478bd9Sstevel@tonic-gate 	return (0);
25147c478bd9Sstevel@tonic-gate }
25157c478bd9Sstevel@tonic-gate 
2516f4b3ec61Sdh155122 static int
25173bc21d0aSAruna Ramakrishna - Sun Microsystems add_datalink(zlog_t *zlogp, char *zone_name, char *dlname)
2518f4b3ec61Sdh155122 {
2519f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2520f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2521f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
2522f4b3ec61Sdh155122 		errno = EPERM;
2523f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "WARNING: skipping network interface "
2524f4b3ec61Sdh155122 		    "'%s' which is used in the global zone.", dlname);
2525f4b3ec61Sdh155122 		return (-1);
2526f4b3ec61Sdh155122 	}
2527f4b3ec61Sdh155122 
2528d62bc4baSyz147064 	/* Set zoneid of this link. */
25293bc21d0aSAruna Ramakrishna - Sun Microsystems 	if (dladm_setzid(dlname, zone_name) != DLADM_STATUS_OK) {
2530d62bc4baSyz147064 		zerror(zlogp, B_TRUE, "WARNING: unable to add network "
2531f4b3ec61Sdh155122 		    "interface '%s'.", dlname);
2532f4b3ec61Sdh155122 		return (-1);
2533f4b3ec61Sdh155122 	}
2534f4b3ec61Sdh155122 
2535f4b3ec61Sdh155122 	return (0);
2536f4b3ec61Sdh155122 }
2537f4b3ec61Sdh155122 
2538f4b3ec61Sdh155122 static int
25393bc21d0aSAruna Ramakrishna - Sun Microsystems remove_datalink(zlog_t *zlogp, char *dlname)
2540f4b3ec61Sdh155122 {
25413bc21d0aSAruna Ramakrishna - Sun Microsystems 	if (dladm_setzid(dlname, GLOBAL_ZONENAME)
25423bc21d0aSAruna Ramakrishna - Sun Microsystems 	    != DLADM_STATUS_OK) {
2543f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to release network "
2544f4b3ec61Sdh155122 		    "interface '%s'", dlname);
2545f4b3ec61Sdh155122 		return (-1);
2546f4b3ec61Sdh155122 	}
2547f4b3ec61Sdh155122 	return (0);
2548f4b3ec61Sdh155122 }
2549f4b3ec61Sdh155122 
2550f4b3ec61Sdh155122 /*
2551f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2552f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2553f4b3ec61Sdh155122  * whatever we set up, and return an error.
2554f4b3ec61Sdh155122  */
2555f4b3ec61Sdh155122 static int
2556f4b3ec61Sdh155122 configure_exclusive_network_interfaces(zlog_t *zlogp)
2557f4b3ec61Sdh155122 {
2558f4b3ec61Sdh155122 	zone_dochandle_t handle;
2559f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2560f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2561f4b3ec61Sdh155122 	char path[MAXPATHLEN];
2562f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2563f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2564f4b3ec61Sdh155122 
2565f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2566f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2567f4b3ec61Sdh155122 		return (-1);
2568f4b3ec61Sdh155122 	}
2569f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2570f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2571f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2572f4b3ec61Sdh155122 		return (-1);
2573f4b3ec61Sdh155122 	}
2574f4b3ec61Sdh155122 
2575f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2576f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2577f4b3ec61Sdh155122 		return (0);
2578f4b3ec61Sdh155122 	}
2579f4b3ec61Sdh155122 
2580f4b3ec61Sdh155122 	for (;;) {
2581f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2582f4b3ec61Sdh155122 			break;
2583f4b3ec61Sdh155122 
2584f4b3ec61Sdh155122 		if (prof == NULL) {
2585f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2586f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2587f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2588f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2589f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2590f4b3ec61Sdh155122 				    "unable to determine dev root");
2591f4b3ec61Sdh155122 				return (-1);
2592f4b3ec61Sdh155122 			}
2593f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2594f4b3ec61Sdh155122 			    "/dev");
2595f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2596f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2597f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2598f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2599f4b3ec61Sdh155122 				    "failed to initialize profile");
2600f4b3ec61Sdh155122 				return (-1);
2601f4b3ec61Sdh155122 			}
2602f4b3ec61Sdh155122 		}
2603f4b3ec61Sdh155122 
2604f4b3ec61Sdh155122 		/*
2605d62bc4baSyz147064 		 * Create the /dev entry for backward compatibility.
2606f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2607d62bc4baSyz147064 		 * Note that the zone still boots when the assigned
2608d62bc4baSyz147064 		 * interface is inaccessible, used by others, etc.
2609d62bc4baSyz147064 		 * Also, when vanity naming is used, some interface do
2610d62bc4baSyz147064 		 * do not have corresponding /dev node names (for example,
2611d62bc4baSyz147064 		 * vanity named aggregations).  The /dev entry is not
2612d62bc4baSyz147064 		 * created in that case.  The /dev/net entry is always
2613d62bc4baSyz147064 		 * accessible.
2614f4b3ec61Sdh155122 		 */
26153bc21d0aSAruna Ramakrishna - Sun Microsystems 		if (add_datalink(zlogp, zone_name, nwiftab.zone_nwif_physical)
2616f4b3ec61Sdh155122 		    == 0) {
26173bc21d0aSAruna Ramakrishna - Sun Microsystems 			added = B_TRUE;
26183bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
2619f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2620f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
26213bc21d0aSAruna Ramakrishna - Sun Microsystems 			zerror(zlogp, B_TRUE, "failed to add network device");
2622f4b3ec61Sdh155122 			return (-1);
2623f4b3ec61Sdh155122 		}
2624d62bc4baSyz147064 	}
2625f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2626f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2627f4b3ec61Sdh155122 
2628f4b3ec61Sdh155122 	if (prof != NULL && added) {
2629f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2630f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
2631f4b3ec61Sdh155122 			return (-1);
2632f4b3ec61Sdh155122 		}
2633f4b3ec61Sdh155122 	}
2634f4b3ec61Sdh155122 	if (prof != NULL)
2635f4b3ec61Sdh155122 		di_prof_fini(prof);
2636f4b3ec61Sdh155122 
2637f4b3ec61Sdh155122 	return (0);
2638f4b3ec61Sdh155122 }
2639f4b3ec61Sdh155122 
2640f4b3ec61Sdh155122 /*
2641f4b3ec61Sdh155122  * Get the list of the data-links from kernel, and try to remove it
2642f4b3ec61Sdh155122  */
2643f4b3ec61Sdh155122 static int
2644f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces_run(zlog_t *zlogp, zoneid_t zoneid)
2645f4b3ec61Sdh155122 {
2646f4b3ec61Sdh155122 	char *dlnames, *ptr;
2647f4b3ec61Sdh155122 	int dlnum, dlnum_saved, i;
2648f4b3ec61Sdh155122 
2649f4b3ec61Sdh155122 	dlnum = 0;
2650f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2651f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2652f4b3ec61Sdh155122 		return (-1);
2653f4b3ec61Sdh155122 	}
2654f4b3ec61Sdh155122 again:
2655f4b3ec61Sdh155122 	/* this zone doesn't have any data-links */
2656f4b3ec61Sdh155122 	if (dlnum == 0)
2657f4b3ec61Sdh155122 		return (0);
2658f4b3ec61Sdh155122 
2659f4b3ec61Sdh155122 	dlnames = malloc(dlnum * LIFNAMSIZ);
2660f4b3ec61Sdh155122 	if (dlnames == NULL) {
2661f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "memory allocation failed");
2662f4b3ec61Sdh155122 		return (-1);
2663f4b3ec61Sdh155122 	}
2664f4b3ec61Sdh155122 	dlnum_saved = dlnum;
2665f4b3ec61Sdh155122 
2666f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, dlnames) != 0) {
2667f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2668f4b3ec61Sdh155122 		free(dlnames);
2669f4b3ec61Sdh155122 		return (-1);
2670f4b3ec61Sdh155122 	}
2671f4b3ec61Sdh155122 	if (dlnum_saved < dlnum) {
2672f4b3ec61Sdh155122 		/* list increased, try again */
2673f4b3ec61Sdh155122 		free(dlnames);
2674f4b3ec61Sdh155122 		goto again;
2675f4b3ec61Sdh155122 	}
2676f4b3ec61Sdh155122 	ptr = dlnames;
2677f4b3ec61Sdh155122 	for (i = 0; i < dlnum; i++) {
2678f4b3ec61Sdh155122 		/* Remove access control information */
26793bc21d0aSAruna Ramakrishna - Sun Microsystems 		if (remove_datalink(zlogp, ptr) != 0) {
2680f4b3ec61Sdh155122 			free(dlnames);
2681f4b3ec61Sdh155122 			return (-1);
2682f4b3ec61Sdh155122 		}
2683f4b3ec61Sdh155122 		ptr += LIFNAMSIZ;
2684f4b3ec61Sdh155122 	}
2685f4b3ec61Sdh155122 	free(dlnames);
2686f4b3ec61Sdh155122 	return (0);
2687f4b3ec61Sdh155122 }
2688f4b3ec61Sdh155122 
2689f4b3ec61Sdh155122 /*
2690f4b3ec61Sdh155122  * Get the list of the data-links from configuration, and try to remove it
2691f4b3ec61Sdh155122  */
2692f4b3ec61Sdh155122 static int
26933bc21d0aSAruna Ramakrishna - Sun Microsystems unconfigure_exclusive_network_interfaces_static(zlog_t *zlogp)
2694f4b3ec61Sdh155122 {
2695f4b3ec61Sdh155122 	zone_dochandle_t handle;
2696f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2697f4b3ec61Sdh155122 
2698f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2699f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2700f4b3ec61Sdh155122 		return (-1);
2701f4b3ec61Sdh155122 	}
2702f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2703f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2704f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2705f4b3ec61Sdh155122 		return (-1);
2706f4b3ec61Sdh155122 	}
2707f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2708f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2709f4b3ec61Sdh155122 		return (0);
2710f4b3ec61Sdh155122 	}
2711f4b3ec61Sdh155122 	for (;;) {
2712f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2713f4b3ec61Sdh155122 			break;
2714f4b3ec61Sdh155122 		/* Remove access control information */
27153bc21d0aSAruna Ramakrishna - Sun Microsystems 		if (remove_datalink(zlogp, nwiftab.zone_nwif_physical)
2716f4b3ec61Sdh155122 		    != 0) {
2717f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2718f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
2719f4b3ec61Sdh155122 			return (-1);
2720f4b3ec61Sdh155122 		}
2721f4b3ec61Sdh155122 	}
2722f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2723f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2724f4b3ec61Sdh155122 	return (0);
2725f4b3ec61Sdh155122 }
2726f4b3ec61Sdh155122 
2727f4b3ec61Sdh155122 /*
2728f4b3ec61Sdh155122  * Remove the access control information from the kernel for the exclusive
2729f4b3ec61Sdh155122  * network interfaces.
2730f4b3ec61Sdh155122  */
2731f4b3ec61Sdh155122 static int
2732f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2733f4b3ec61Sdh155122 {
2734f4b3ec61Sdh155122 	if (unconfigure_exclusive_network_interfaces_run(zlogp, zoneid) != 0) {
27353bc21d0aSAruna Ramakrishna - Sun Microsystems 		return (unconfigure_exclusive_network_interfaces_static(zlogp));
2736f4b3ec61Sdh155122 	}
2737f4b3ec61Sdh155122 
2738f4b3ec61Sdh155122 	return (0);
2739f4b3ec61Sdh155122 }
2740f4b3ec61Sdh155122 
27417c478bd9Sstevel@tonic-gate static int
27427c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
27437c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
27447c478bd9Sstevel@tonic-gate {
27457c478bd9Sstevel@tonic-gate 	int fd;
27467c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
27477c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
27487c478bd9Sstevel@tonic-gate 	int error;
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
27517c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
27527c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
27537c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
27547c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
27577c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
27587c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
27597c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
27627c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
27637c478bd9Sstevel@tonic-gate 		return (-1);
27647c478bd9Sstevel@tonic-gate 	}
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
27677c478bd9Sstevel@tonic-gate 	(void) close(fd);
27687c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
27697c478bd9Sstevel@tonic-gate 		return (0);
27707c478bd9Sstevel@tonic-gate 	return (-1);
27717c478bd9Sstevel@tonic-gate }
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate static int
27747c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
27757c478bd9Sstevel@tonic-gate {
27767c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
27777c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
27787c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
27797c478bd9Sstevel@tonic-gate 	int error;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	/*
27827c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
27837c478bd9Sstevel@tonic-gate 	 */
27847c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
27857c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
27867c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
27877c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
27887c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
27917c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
27927c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
27937c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
27947c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
27977c478bd9Sstevel@tonic-gate 		return (error);
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	/*
28007c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
28017c478bd9Sstevel@tonic-gate 	 */
28027c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
28037c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
28047c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
28057c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
28067c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
28097c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
28107c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
28117c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
28127c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
28157c478bd9Sstevel@tonic-gate 		return (error);
28167c478bd9Sstevel@tonic-gate 	return (0);
28177c478bd9Sstevel@tonic-gate }
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate static int
28206cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
2821ffbafc53Scomay {
2822ffbafc53Scomay 	int error = -1;
2823ffbafc53Scomay 	zone_dochandle_t handle;
2824ffbafc53Scomay 	char *privname = NULL;
2825ffbafc53Scomay 
2826ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
2827ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2828ffbafc53Scomay 		return (-1);
2829ffbafc53Scomay 	}
2830ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2831ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
2832ffbafc53Scomay 		zonecfg_fini_handle(handle);
2833ffbafc53Scomay 		return (-1);
2834ffbafc53Scomay 	}
2835ffbafc53Scomay 
28366cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
2837bf1d7e28Sdh155122 		zone_iptype_t	iptype;
2838bf1d7e28Sdh155122 		const char	*curr_iptype;
2839bf1d7e28Sdh155122 
2840bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
2841bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
2842bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
2843bf1d7e28Sdh155122 			return (-1);
2844bf1d7e28Sdh155122 		}
2845bf1d7e28Sdh155122 
2846bf1d7e28Sdh155122 		switch (iptype) {
2847bf1d7e28Sdh155122 		case ZS_SHARED:
2848bf1d7e28Sdh155122 			curr_iptype = "shared";
2849bf1d7e28Sdh155122 			break;
2850bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
2851bf1d7e28Sdh155122 			curr_iptype = "exclusive";
2852bf1d7e28Sdh155122 			break;
2853bf1d7e28Sdh155122 		}
2854bf1d7e28Sdh155122 
2855e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
2856e767a340Sgjelinek 			zonecfg_fini_handle(handle);
28579acbbeafSnn35248 			return (0);
2858e767a340Sgjelinek 		}
28599acbbeafSnn35248 		zerror(zlogp, B_FALSE,
28609acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
28619acbbeafSnn35248 		zonecfg_fini_handle(handle);
28629acbbeafSnn35248 		return (-1);
28639acbbeafSnn35248 	}
28649acbbeafSnn35248 
2865ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
2866ffbafc53Scomay 	case Z_OK:
2867ffbafc53Scomay 		error = 0;
2868ffbafc53Scomay 		break;
2869ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2870ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
2871ffbafc53Scomay 		    "within the zone's privilege set", privname);
2872ffbafc53Scomay 		break;
2873ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2874ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
2875ffbafc53Scomay 		    "from the zone's privilege set", privname);
2876ffbafc53Scomay 		break;
2877ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2878ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
2879ffbafc53Scomay 		    "in the zone's privilege set", privname);
2880ffbafc53Scomay 		break;
2881ffbafc53Scomay 	default:
2882ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
2883ffbafc53Scomay 		    "privilege set");
2884ffbafc53Scomay 		break;
2885ffbafc53Scomay 	}
2886ffbafc53Scomay 
2887ffbafc53Scomay 	free(privname);
2888ffbafc53Scomay 	zonecfg_fini_handle(handle);
2889ffbafc53Scomay 	return (error);
2890ffbafc53Scomay }
2891ffbafc53Scomay 
2892ffbafc53Scomay static int
28937c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
28947c478bd9Sstevel@tonic-gate {
28957c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
28967c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
28977c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
28987c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
28997c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
29007c478bd9Sstevel@tonic-gate 	int error = -1;
29017c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
29027c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
29037c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	*bufp = NULL;
29067c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
29097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
29107c478bd9Sstevel@tonic-gate 		return (-1);
29117c478bd9Sstevel@tonic-gate 	}
29127c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
29137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
29147c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
29157c478bd9Sstevel@tonic-gate 		return (-1);
29167c478bd9Sstevel@tonic-gate 	}
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
29197c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
29207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
29217c478bd9Sstevel@tonic-gate 		goto out;
29227c478bd9Sstevel@tonic-gate 	}
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
29257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
29267c478bd9Sstevel@tonic-gate 		goto out;
29277c478bd9Sstevel@tonic-gate 	}
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
29307c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
29317c478bd9Sstevel@tonic-gate 		goto out;
29327c478bd9Sstevel@tonic-gate 	}
29337c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
29347c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
29357c478bd9Sstevel@tonic-gate 		uint_t i, count;
29367c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
29397c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
29407c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29417c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
29427c478bd9Sstevel@tonic-gate 			continue;
29437c478bd9Sstevel@tonic-gate 		}
29447c478bd9Sstevel@tonic-gate 		count = 0;
29457c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
29467c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
29477c478bd9Sstevel@tonic-gate 			count++;
29487c478bd9Sstevel@tonic-gate 		}
29497c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
29507c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
29517c478bd9Sstevel@tonic-gate 		}
29527c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
29537c478bd9Sstevel@tonic-gate 			goto out;
29547c478bd9Sstevel@tonic-gate 		i = 0;
29557c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
29567c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
29577c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
29587c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
29597c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
29607c478bd9Sstevel@tonic-gate 				goto out;
29617c478bd9Sstevel@tonic-gate 			}
29627c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
29637c478bd9Sstevel@tonic-gate 			    != Z_OK) {
29647c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
29657c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
29667c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
29677c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
29687c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
29697c478bd9Sstevel@tonic-gate 				goto out;
29707c478bd9Sstevel@tonic-gate 			}
29717c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
29727c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
29737c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
29747c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
29757c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
29767c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
29777c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
29787c478bd9Sstevel@tonic-gate 				    name);
29797c478bd9Sstevel@tonic-gate 				goto out;
29807c478bd9Sstevel@tonic-gate 			}
29817c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
29827c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
29837c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29847c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29857c478bd9Sstevel@tonic-gate 				goto out;
29867c478bd9Sstevel@tonic-gate 			}
29877c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
29887c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
29897c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29907c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29917c478bd9Sstevel@tonic-gate 				goto out;
29927c478bd9Sstevel@tonic-gate 			}
29937c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
29947c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
29957c478bd9Sstevel@tonic-gate 			    != 0) {
29967c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29977c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29987c478bd9Sstevel@tonic-gate 				goto out;
29997c478bd9Sstevel@tonic-gate 			}
30007c478bd9Sstevel@tonic-gate 		}
30017c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30027c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
30037c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
30047c478bd9Sstevel@tonic-gate 		    != 0) {
30057c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
30067c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
30077c478bd9Sstevel@tonic-gate 			goto out;
30087c478bd9Sstevel@tonic-gate 		}
30097c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
30107c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
30117c478bd9Sstevel@tonic-gate 		free(nvlv);
30127c478bd9Sstevel@tonic-gate 		nvlv = NULL;
30137c478bd9Sstevel@tonic-gate 		rctlcount++;
30147c478bd9Sstevel@tonic-gate 	}
30157c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
30187c478bd9Sstevel@tonic-gate 		error = 0;
30197c478bd9Sstevel@tonic-gate 		goto out;
30207c478bd9Sstevel@tonic-gate 	}
30217c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
30227c478bd9Sstevel@tonic-gate 	    != 0) {
30237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
30247c478bd9Sstevel@tonic-gate 		goto out;
30257c478bd9Sstevel@tonic-gate 	}
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 	error = 0;
30287c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
30297c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate out:
30327c478bd9Sstevel@tonic-gate 	free(rctlblk);
30337c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30347c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
30357c478bd9Sstevel@tonic-gate 		free(nvl_packed);
30367c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
30377c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
30387c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
30397c478bd9Sstevel@tonic-gate 		free(nvlv);
30407c478bd9Sstevel@tonic-gate 	if (handle != NULL)
30417c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
30427c478bd9Sstevel@tonic-gate 	return (error);
30437c478bd9Sstevel@tonic-gate }
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate static int
3046c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr)
3047c5cd6260S {
3048c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
3049c5cd6260S 
3050c5cd6260S 	if (query_hook[0] == '\0')
3051c5cd6260S 		return (0);
3052c5cd6260S 
3053c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3054c5cd6260S 	    > sizeof (cmdbuf))
3055c5cd6260S 		return (-1);
3056c5cd6260S 
3057c5cd6260S 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3058c5cd6260S 		return (-1);
3059c5cd6260S 
3060c5cd6260S 	return (0);
3061c5cd6260S }
3062c5cd6260S 
3063c5cd6260S static int
3064fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3065fa9e4066Sahrens {
3066fa9e4066Sahrens 	zone_dochandle_t handle;
3067fa9e4066Sahrens 	struct zone_dstab dstab;
3068fa9e4066Sahrens 	size_t total, offset, len;
3069fa9e4066Sahrens 	int error = -1;
3070e5a17f6aSgjelinek 	char *str = NULL;
3071c5cd6260S 	char *implicit_datasets = NULL;
3072c5cd6260S 	int implicit_len = 0;
3073fa9e4066Sahrens 
3074fa9e4066Sahrens 	*bufp = NULL;
3075fa9e4066Sahrens 	*bufsizep = 0;
3076fa9e4066Sahrens 
3077fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3078fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3079fa9e4066Sahrens 		return (-1);
3080fa9e4066Sahrens 	}
3081fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3082fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3083fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3084fa9e4066Sahrens 		return (-1);
3085fa9e4066Sahrens 	}
3086fa9e4066Sahrens 
3087c5cd6260S 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3088c5cd6260S 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3089c5cd6260S 		goto out;
3090c5cd6260S 	}
3091c5cd6260S 
3092fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3093fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3094fa9e4066Sahrens 		goto out;
3095fa9e4066Sahrens 	}
3096fa9e4066Sahrens 
3097fa9e4066Sahrens 	total = 0;
3098fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3099fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3100fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3101fa9e4066Sahrens 
3102c5cd6260S 	if (implicit_datasets != NULL)
3103c5cd6260S 		implicit_len = strlen(implicit_datasets);
3104c5cd6260S 	if (implicit_len > 0)
3105c5cd6260S 		total += implicit_len + 1;
3106c5cd6260S 
3107fa9e4066Sahrens 	if (total == 0) {
3108fa9e4066Sahrens 		error = 0;
3109fa9e4066Sahrens 		goto out;
3110fa9e4066Sahrens 	}
3111fa9e4066Sahrens 
3112fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3113fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3114fa9e4066Sahrens 		goto out;
3115fa9e4066Sahrens 	}
3116fa9e4066Sahrens 
3117fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3118fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3119fa9e4066Sahrens 		goto out;
3120fa9e4066Sahrens 	}
3121fa9e4066Sahrens 	offset = 0;
3122fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3123fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3124fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3125e5a17f6aSgjelinek 		    total - offset);
3126fa9e4066Sahrens 		offset += len;
3127e5a17f6aSgjelinek 		if (offset < total - 1)
3128fa9e4066Sahrens 			str[offset++] = ',';
3129fa9e4066Sahrens 	}
3130fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3131fa9e4066Sahrens 
3132c5cd6260S 	if (implicit_len > 0)
3133c5cd6260S 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3134c5cd6260S 
3135fa9e4066Sahrens 	error = 0;
3136fa9e4066Sahrens 	*bufp = str;
3137fa9e4066Sahrens 	*bufsizep = total;
3138fa9e4066Sahrens 
3139fa9e4066Sahrens out:
3140fa9e4066Sahrens 	if (error != 0 && str != NULL)
3141fa9e4066Sahrens 		free(str);
3142fa9e4066Sahrens 	if (handle != NULL)
3143fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3144c5cd6260S 	if (implicit_datasets != NULL)
3145c5cd6260S 		free(implicit_datasets);
3146fa9e4066Sahrens 
3147fa9e4066Sahrens 	return (error);
3148fa9e4066Sahrens }
3149fa9e4066Sahrens 
3150fa9e4066Sahrens static int
3151fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3152fa9e4066Sahrens {
3153fa9e4066Sahrens 	zone_dochandle_t handle;
3154fa9e4066Sahrens 	struct zone_dstab dstab;
3155fa9e4066Sahrens 	zfs_handle_t *zhp;
315699653d4eSeschrock 	libzfs_handle_t *hdl;
3157fa9e4066Sahrens 
3158fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3159fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3160fa9e4066Sahrens 		return (-1);
3161fa9e4066Sahrens 	}
3162fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3163fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3164fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3165fa9e4066Sahrens 		return (-1);
3166fa9e4066Sahrens 	}
3167fa9e4066Sahrens 
3168fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3169fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3170fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3171fa9e4066Sahrens 		return (-1);
3172fa9e4066Sahrens 	}
3173fa9e4066Sahrens 
317499653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
317599653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
317699653d4eSeschrock 		zonecfg_fini_handle(handle);
317799653d4eSeschrock 		return (-1);
317899653d4eSeschrock 	}
3179fa9e4066Sahrens 
3180fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3181fa9e4066Sahrens 
318299653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3183fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3184fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3185fa9e4066Sahrens 			    dstab.zone_dataset_name);
3186fa9e4066Sahrens 			zonecfg_fini_handle(handle);
318799653d4eSeschrock 			libzfs_fini(hdl);
3188fa9e4066Sahrens 			return (-1);
3189fa9e4066Sahrens 		}
3190fa9e4066Sahrens 
3191fa9e4066Sahrens 		/*
3192fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3193fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3194fa9e4066Sahrens 		 */
3195fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3196e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3197e9dbad6fSeschrock 		    "on") != 0) {
3198fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3199fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3200fa9e4066Sahrens 			    dstab.zone_dataset_name);
3201fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3202fa9e4066Sahrens 			zfs_close(zhp);
320399653d4eSeschrock 			libzfs_fini(hdl);
3204fa9e4066Sahrens 			return (-1);
3205fa9e4066Sahrens 		}
3206fa9e4066Sahrens 
3207fa9e4066Sahrens 		zfs_close(zhp);
3208fa9e4066Sahrens 	}
3209fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3210fa9e4066Sahrens 
3211fa9e4066Sahrens 	zonecfg_fini_handle(handle);
321299653d4eSeschrock 	libzfs_fini(hdl);
3213fa9e4066Sahrens 
3214fa9e4066Sahrens 	return (0);
3215fa9e4066Sahrens }
3216fa9e4066Sahrens 
321745916cd2Sjpk /*
321845916cd2Sjpk  * Mount lower level home directories into/from current zone
321945916cd2Sjpk  * Share exported directories specified in dfstab for zone
322045916cd2Sjpk  */
322145916cd2Sjpk static int
322245916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
322345916cd2Sjpk {
322445916cd2Sjpk 	zoneid_t *zids = NULL;
322545916cd2Sjpk 	priv_set_t *zid_privs;
322645916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
322745916cd2Sjpk 	uint_t nzents_saved;
322845916cd2Sjpk 	uint_t nzents;
322945916cd2Sjpk 	int i;
323045916cd2Sjpk 	char readonly[] = "ro";
323145916cd2Sjpk 	struct zone_fstab lower_fstab;
323245916cd2Sjpk 	char *argv[4];
323345916cd2Sjpk 
323445916cd2Sjpk 	if (!is_system_labeled())
323545916cd2Sjpk 		return (0);
323645916cd2Sjpk 
323745916cd2Sjpk 	if (zid_label == NULL) {
323845916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
323945916cd2Sjpk 		if (zid_label == NULL)
324045916cd2Sjpk 			return (-1);
324145916cd2Sjpk 	}
324245916cd2Sjpk 
324345916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
324445916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
324527e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
324645916cd2Sjpk 
324745916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
324845916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
324945916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
325045916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
325145916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
325245916cd2Sjpk 
325345916cd2Sjpk 	/*
325445916cd2Sjpk 	 * Get the list of zones from the kernel
325545916cd2Sjpk 	 */
325645916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
325745916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
325845916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
325945916cd2Sjpk 		return (-1);
326045916cd2Sjpk 	}
326145916cd2Sjpk again:
326245916cd2Sjpk 	if (nzents == 0) {
326345916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
326445916cd2Sjpk 		return (-1);
326545916cd2Sjpk 	}
326645916cd2Sjpk 
326745916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
326845916cd2Sjpk 	if (zids == NULL) {
32693f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
327045916cd2Sjpk 		return (-1);
327145916cd2Sjpk 	}
327245916cd2Sjpk 	nzents_saved = nzents;
327345916cd2Sjpk 
327445916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
327545916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
327645916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
327745916cd2Sjpk 		free(zids);
327845916cd2Sjpk 		return (-1);
327945916cd2Sjpk 	}
328045916cd2Sjpk 	if (nzents != nzents_saved) {
328145916cd2Sjpk 		/* list changed, try again */
328245916cd2Sjpk 		free(zids);
328345916cd2Sjpk 		goto again;
328445916cd2Sjpk 	}
328545916cd2Sjpk 
328645916cd2Sjpk 	ip = getprivimplinfo();
328745916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
328845916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
328945916cd2Sjpk 		zonecfg_free_fs_option_list(
329045916cd2Sjpk 		    lower_fstab.zone_fs_options);
329145916cd2Sjpk 		free(zids);
329245916cd2Sjpk 		return (-1);
329345916cd2Sjpk 	}
329445916cd2Sjpk 
329545916cd2Sjpk 	for (i = 0; i < nzents; i++) {
329645916cd2Sjpk 		char zid_name[ZONENAME_MAX];
329745916cd2Sjpk 		zone_state_t zid_state;
329845916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
329945916cd2Sjpk 		struct stat stat_buf;
330045916cd2Sjpk 
330145916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
330245916cd2Sjpk 			continue;
330345916cd2Sjpk 
330445916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
330545916cd2Sjpk 			continue;
330645916cd2Sjpk 
330745916cd2Sjpk 		/*
330845916cd2Sjpk 		 * Do special setup for the zone we are booting
330945916cd2Sjpk 		 */
331045916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
331145916cd2Sjpk 			struct zone_fstab autofs_fstab;
331245916cd2Sjpk 			char map_path[MAXPATHLEN];
331345916cd2Sjpk 			int fd;
331445916cd2Sjpk 
331545916cd2Sjpk 			/*
331645916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
33179acbbeafSnn35248 			 * in the global zone. The non-global zone entry
331845916cd2Sjpk 			 * will be created by automount when the zone
331945916cd2Sjpk 			 * is booted.
332045916cd2Sjpk 			 */
332145916cd2Sjpk 
332245916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
332345916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
332445916cd2Sjpk 
332545916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
332645916cd2Sjpk 			    "/zone/%s/home", zid_name);
332745916cd2Sjpk 
332845916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
332945916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
333045916cd2Sjpk 			/*
333145916cd2Sjpk 			 * If the map file doesn't exist create a template
333245916cd2Sjpk 			 */
333345916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
333445916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
333545916cd2Sjpk 				int len;
333645916cd2Sjpk 				char map_rec[MAXPATHLEN];
333745916cd2Sjpk 
333845916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
333945916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
334045916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
334145916cd2Sjpk 				(void) write(fd, map_rec, len);
334245916cd2Sjpk 				(void) close(fd);
334345916cd2Sjpk 			}
334445916cd2Sjpk 
334545916cd2Sjpk 			/*
334645916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
334745916cd2Sjpk 			 * If it's already of type autofs, then
334845916cd2Sjpk 			 * don't mount it again.
334945916cd2Sjpk 			 */
335045916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
335145916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
335245916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
335345916cd2Sjpk 
335445916cd2Sjpk 				(void) make_one_dir(zlogp, "",
335527e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
335627e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
335745916cd2Sjpk 
335845916cd2Sjpk 				/*
335945916cd2Sjpk 				 * Mount will fail if automounter has already
336045916cd2Sjpk 				 * processed the auto_home_<zonename> map
336145916cd2Sjpk 				 */
336245916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
336345916cd2Sjpk 				    autofs_fstab.zone_fs_special,
336445916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
336545916cd2Sjpk 			}
336645916cd2Sjpk 			continue;
336745916cd2Sjpk 		}
336845916cd2Sjpk 
336945916cd2Sjpk 
337045916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
337148451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
337248451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
337345916cd2Sjpk 			/* Skip over zones without mounted filesystems */
337445916cd2Sjpk 			continue;
337545916cd2Sjpk 
337645916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
337745916cd2Sjpk 		    sizeof (m_label_t)) < 0)
337845916cd2Sjpk 			/* Skip over zones with unspecified label */
337945916cd2Sjpk 			continue;
338045916cd2Sjpk 
338145916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
338245916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
338345916cd2Sjpk 			/* Skip over zones with bad path */
338445916cd2Sjpk 			continue;
338545916cd2Sjpk 
338645916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
338745916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
338845916cd2Sjpk 			/* Skip over zones with bad privs */
338945916cd2Sjpk 			continue;
339045916cd2Sjpk 
339145916cd2Sjpk 		/*
339245916cd2Sjpk 		 * Reading down is valid according to our label model
339345916cd2Sjpk 		 * but some customers want to disable it because it
339445916cd2Sjpk 		 * allows execute down and other possible attacks.
339545916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
339645916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
339745916cd2Sjpk 		 * for NFS read-down semantics.
339845916cd2Sjpk 		 */
339945916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
340045916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
340145916cd2Sjpk 			/*
340245916cd2Sjpk 			 * Our zone dominates this one.
340345916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
340445916cd2Sjpk 			 */
340545916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
340645916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
340745916cd2Sjpk 
340845916cd2Sjpk 			/*
340945916cd2Sjpk 			 * If the target is already an LOFS mount
341045916cd2Sjpk 			 * then don't do it again.
341145916cd2Sjpk 			 */
341245916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
341345916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
341445916cd2Sjpk 
341545916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
341645916cd2Sjpk 				    MAXPATHLEN, "%s/export",
341745916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
341845916cd2Sjpk 					continue;
341945916cd2Sjpk 
342045916cd2Sjpk 				/*
342145916cd2Sjpk 				 * Make sure the lower-level home exists
342245916cd2Sjpk 				 */
342345916cd2Sjpk 				if (make_one_dir(zlogp,
342427e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
342527e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
342627e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
342745916cd2Sjpk 					continue;
342845916cd2Sjpk 
342945916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
343045916cd2Sjpk 				    "/home", MAXPATHLEN);
343145916cd2Sjpk 
343245916cd2Sjpk 				/*
343345916cd2Sjpk 				 * Mount can fail because the lower-level
343445916cd2Sjpk 				 * zone may have already done a mount up.
343545916cd2Sjpk 				 */
34360679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
34370679f794S 				    Z_MNT_BOOT);
343845916cd2Sjpk 			}
343945916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
344045916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
344145916cd2Sjpk 			/*
344245916cd2Sjpk 			 * This zone dominates our zone.
344345916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
344445916cd2Sjpk 			 */
344545916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
344645916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
344745916cd2Sjpk 			    zone_name) > MAXPATHLEN)
344845916cd2Sjpk 				continue;
344945916cd2Sjpk 
345045916cd2Sjpk 			/*
345145916cd2Sjpk 			 * If the target is already an LOFS mount
345245916cd2Sjpk 			 * then don't do it again.
345345916cd2Sjpk 			 */
345445916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
345545916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
345645916cd2Sjpk 
345745916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
345845916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
345945916cd2Sjpk 
346045916cd2Sjpk 				/*
346145916cd2Sjpk 				 * Mount can fail because the higher-level
346245916cd2Sjpk 				 * zone may have already done a mount down.
346345916cd2Sjpk 				 */
34640679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
34650679f794S 				    Z_MNT_BOOT);
346645916cd2Sjpk 			}
346745916cd2Sjpk 		}
346845916cd2Sjpk 	}
346945916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
347045916cd2Sjpk 	priv_freeset(zid_privs);
347145916cd2Sjpk 	free(zids);
347245916cd2Sjpk 
347345916cd2Sjpk 	/*
347445916cd2Sjpk 	 * Now share any exported directories from this zone.
347545916cd2Sjpk 	 * Each zone can have its own dfstab.
347645916cd2Sjpk 	 */
347745916cd2Sjpk 
347845916cd2Sjpk 	argv[0] = "zoneshare";
347945916cd2Sjpk 	argv[1] = "-z";
348045916cd2Sjpk 	argv[2] = zone_name;
348145916cd2Sjpk 	argv[3] = NULL;
348245916cd2Sjpk 
348345916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
348445916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
348545916cd2Sjpk 
348645916cd2Sjpk 	return (0);
348745916cd2Sjpk }
348845916cd2Sjpk 
348945916cd2Sjpk /*
349045916cd2Sjpk  * Unmount lofs mounts from higher level zones
349145916cd2Sjpk  * Unshare nfs exported directories
349245916cd2Sjpk  */
349345916cd2Sjpk static void
349445916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
349545916cd2Sjpk {
349645916cd2Sjpk 	zoneid_t *zids = NULL;
349745916cd2Sjpk 	uint_t nzents_saved;
349845916cd2Sjpk 	uint_t nzents;
349945916cd2Sjpk 	int i;
350045916cd2Sjpk 	char *argv[4];
350145916cd2Sjpk 	char path[MAXPATHLEN];
350245916cd2Sjpk 
350345916cd2Sjpk 	if (!is_system_labeled())
350445916cd2Sjpk 		return;
350545916cd2Sjpk 
350645916cd2Sjpk 	/*
350745916cd2Sjpk 	 * Get the list of zones from the kernel
350845916cd2Sjpk 	 */
350945916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
351045916cd2Sjpk 		return;
351145916cd2Sjpk 	}
351245916cd2Sjpk 
351345916cd2Sjpk 	if (zid_label == NULL) {
351445916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
351545916cd2Sjpk 		if (zid_label == NULL)
351645916cd2Sjpk 			return;
351745916cd2Sjpk 	}
351845916cd2Sjpk 
351945916cd2Sjpk again:
352045916cd2Sjpk 	if (nzents == 0)
352145916cd2Sjpk 		return;
352245916cd2Sjpk 
352345916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
352445916cd2Sjpk 	if (zids == NULL) {
35253f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
352645916cd2Sjpk 		return;
352745916cd2Sjpk 	}
352845916cd2Sjpk 	nzents_saved = nzents;
352945916cd2Sjpk 
353045916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
353145916cd2Sjpk 		free(zids);
353245916cd2Sjpk 		return;
353345916cd2Sjpk 	}
353445916cd2Sjpk 	if (nzents != nzents_saved) {
353545916cd2Sjpk 		/* list changed, try again */
353645916cd2Sjpk 		free(zids);
353745916cd2Sjpk 		goto again;
353845916cd2Sjpk 	}
353945916cd2Sjpk 
354045916cd2Sjpk 	for (i = 0; i < nzents; i++) {
354145916cd2Sjpk 		char zid_name[ZONENAME_MAX];
354245916cd2Sjpk 		zone_state_t zid_state;
354345916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
354445916cd2Sjpk 
354545916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
354645916cd2Sjpk 			continue;
354745916cd2Sjpk 
354845916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
354945916cd2Sjpk 			continue;
355045916cd2Sjpk 
355145916cd2Sjpk 		/*
355245916cd2Sjpk 		 * Skip the zone we are halting
355345916cd2Sjpk 		 */
355445916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
355545916cd2Sjpk 			continue;
355645916cd2Sjpk 
355745916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
355845916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
355945916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
356045916cd2Sjpk 			/* Skip over zones without mounted filesystems */
356145916cd2Sjpk 			continue;
356245916cd2Sjpk 
356345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
356445916cd2Sjpk 		    sizeof (m_label_t)) < 0)
356545916cd2Sjpk 			/* Skip over zones with unspecified label */
356645916cd2Sjpk 			continue;
356745916cd2Sjpk 
356845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
356945916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
357045916cd2Sjpk 			/* Skip over zones with bad path */
357145916cd2Sjpk 			continue;
357245916cd2Sjpk 
357345916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
357445916cd2Sjpk 			/*
357545916cd2Sjpk 			 * This zone dominates our zone.
357645916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
357745916cd2Sjpk 			 */
357845916cd2Sjpk 
357945916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
358045916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
358145916cd2Sjpk 			    zone_name) > MAXPATHLEN)
358245916cd2Sjpk 				continue;
358345916cd2Sjpk 
358445916cd2Sjpk 			/* Skip over mount failures */
358545916cd2Sjpk 			(void) umount(path);
358645916cd2Sjpk 		}
358745916cd2Sjpk 	}
358845916cd2Sjpk 	free(zids);
358945916cd2Sjpk 
359045916cd2Sjpk 	/*
359145916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
359245916cd2Sjpk 	 */
359345916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
359445916cd2Sjpk 	/* Skip over mount failures */
359545916cd2Sjpk 	(void) umount(path);
359645916cd2Sjpk 
359745916cd2Sjpk 	/*
359845916cd2Sjpk 	 * Next unshare any exported directories from this zone.
359945916cd2Sjpk 	 */
360045916cd2Sjpk 
360145916cd2Sjpk 	argv[0] = "zoneunshare";
360245916cd2Sjpk 	argv[1] = "-z";
360345916cd2Sjpk 	argv[2] = zone_name;
360445916cd2Sjpk 	argv[3] = NULL;
360545916cd2Sjpk 
360645916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
360745916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
360845916cd2Sjpk 
360945916cd2Sjpk 	/*
361045916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
361145916cd2Sjpk 	 */
361245916cd2Sjpk 
361345916cd2Sjpk 	argv[0] = "deallocate";
361445916cd2Sjpk 	argv[1] = "-Isz";
361545916cd2Sjpk 	argv[2] = zone_name;
361645916cd2Sjpk 	argv[3] = NULL;
361745916cd2Sjpk 
361845916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
361945916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
362045916cd2Sjpk }
362145916cd2Sjpk 
362245916cd2Sjpk /*
362345916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
362445916cd2Sjpk  * this zone.
362545916cd2Sjpk  */
362645916cd2Sjpk static tsol_zcent_t *
362745916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
362845916cd2Sjpk {
362945916cd2Sjpk 	FILE *fp;
363045916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
363145916cd2Sjpk 	char line[MAXTNZLEN];
363245916cd2Sjpk 
363345916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
363445916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
363545916cd2Sjpk 		return (NULL);
363645916cd2Sjpk 	}
363745916cd2Sjpk 
363845916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
363945916cd2Sjpk 		/*
364045916cd2Sjpk 		 * Check for malformed database
364145916cd2Sjpk 		 */
364245916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
364345916cd2Sjpk 			break;
364445916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
364545916cd2Sjpk 			continue;
364645916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
364745916cd2Sjpk 			break;
364845916cd2Sjpk 		tsol_freezcent(zcent);
364945916cd2Sjpk 		zcent = NULL;
365045916cd2Sjpk 	}
365145916cd2Sjpk 	(void) fclose(fp);
365245916cd2Sjpk 
365345916cd2Sjpk 	if (zcent == NULL) {
365445916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
365545916cd2Sjpk 		    "See tnzonecfg(4)");
365645916cd2Sjpk 	} else {
365745916cd2Sjpk 		if (zlabel == NULL)
365845916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
365945916cd2Sjpk 		/*
366045916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
366145916cd2Sjpk 		 */
366245916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
366345916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
366445916cd2Sjpk 			return (NULL);
366545916cd2Sjpk 		} else {
366645916cd2Sjpk 			priv_copyset(privs, zprivs);
366745916cd2Sjpk 		}
366845916cd2Sjpk 	}
366945916cd2Sjpk 	return (zcent);
367045916cd2Sjpk }
367145916cd2Sjpk 
367245916cd2Sjpk /*
367345916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
367445916cd2Sjpk  */
367545916cd2Sjpk static void
367645916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
367745916cd2Sjpk {
367845916cd2Sjpk 	tsol_mlp_t *mlp;
367945916cd2Sjpk 	tsol_mlpent_t tsme;
368045916cd2Sjpk 
368145916cd2Sjpk 	if (!is_system_labeled())
368245916cd2Sjpk 		return;
368345916cd2Sjpk 
368445916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
368545916cd2Sjpk 	tsme.tsme_flags = 0;
368645916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
368745916cd2Sjpk 		tsme.tsme_mlp = *mlp;
368845916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
368945916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
369045916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
369145916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
369245916cd2Sjpk 		}
369345916cd2Sjpk 	}
369445916cd2Sjpk 
369545916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
369645916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
369745916cd2Sjpk 		tsme.tsme_mlp = *mlp;
369845916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
369945916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
370045916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
370145916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
370245916cd2Sjpk 		}
370345916cd2Sjpk 	}
370445916cd2Sjpk }
370545916cd2Sjpk 
370645916cd2Sjpk static void
370745916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
370845916cd2Sjpk {
370945916cd2Sjpk 	tsol_mlpent_t tsme;
371045916cd2Sjpk 
371145916cd2Sjpk 	if (!is_system_labeled())
371245916cd2Sjpk 		return;
371345916cd2Sjpk 
371445916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
371545916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
371645916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
371745916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
371845916cd2Sjpk }
371945916cd2Sjpk 
37207c478bd9Sstevel@tonic-gate int
37217c478bd9Sstevel@tonic-gate prtmount(const char *fs, void *x) {
37227c478bd9Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
37237c478bd9Sstevel@tonic-gate 	return (0);
37247c478bd9Sstevel@tonic-gate }
37257c478bd9Sstevel@tonic-gate 
3726108322fbScarlsonj /*
3727108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
3728108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3729108322fbScarlsonj  * is found or if we can't tell.
3730108322fbScarlsonj  */
3731108322fbScarlsonj static boolean_t
3732108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
37337c478bd9Sstevel@tonic-gate {
3734108322fbScarlsonj 	zoneid_t *zids = NULL;
3735108322fbScarlsonj 	uint_t nzids = 0;
3736108322fbScarlsonj 	boolean_t retv;
3737108322fbScarlsonj 	int rlen, zlen;
3738108322fbScarlsonj 	char zroot[MAXPATHLEN];
3739108322fbScarlsonj 	char zonename[ZONENAME_MAX];
3740108322fbScarlsonj 
3741108322fbScarlsonj 	for (;;) {
3742108322fbScarlsonj 		nzids += 10;
3743108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
3744108322fbScarlsonj 		if (zids == NULL) {
37453f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3746108322fbScarlsonj 			return (B_TRUE);
3747108322fbScarlsonj 		}
3748108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
3749108322fbScarlsonj 			break;
3750108322fbScarlsonj 		free(zids);
3751108322fbScarlsonj 	}
3752108322fbScarlsonj 	retv = B_FALSE;
3753108322fbScarlsonj 	rlen = strlen(rootpath);
3754108322fbScarlsonj 	while (nzids > 0) {
3755108322fbScarlsonj 		/*
3756108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3757108322fbScarlsonj 		 * while we were busy.
3758108322fbScarlsonj 		 */
3759108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3760108322fbScarlsonj 		    sizeof (zroot)) == -1)
3761108322fbScarlsonj 			continue;
3762108322fbScarlsonj 		zlen = strlen(zroot);
3763108322fbScarlsonj 		if (zlen > rlen)
3764108322fbScarlsonj 			zlen = rlen;
3765108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3766108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3767108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3768108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3769108322fbScarlsonj 			    sizeof (zonename)) == -1)
3770108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3771108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
3772108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3773108322fbScarlsonj 			    "zone root %s already in use by zone %s",
3774108322fbScarlsonj 			    rootpath, zonename);
3775108322fbScarlsonj 			retv = B_TRUE;
3776108322fbScarlsonj 			break;
3777108322fbScarlsonj 		}
3778108322fbScarlsonj 	}
3779108322fbScarlsonj 	free(zids);
3780108322fbScarlsonj 	return (retv);
3781108322fbScarlsonj }
3782108322fbScarlsonj 
3783108322fbScarlsonj /*
3784108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
3785108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3786108322fbScarlsonj  */
3787108322fbScarlsonj static boolean_t
3788108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3789108322fbScarlsonj {
3790108322fbScarlsonj 	struct stat64 rst, zst;
3791108322fbScarlsonj 	struct mnttab *mnp;
3792108322fbScarlsonj 
3793108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
3794108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3795108322fbScarlsonj 		return (B_TRUE);
3796108322fbScarlsonj 	}
3797108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3798108322fbScarlsonj 		return (B_TRUE);
3799108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3800108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
3801108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3802108322fbScarlsonj 			continue;
3803108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3804108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
3805108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3806108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3807108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3808108322fbScarlsonj 			    "zone root %s is reachable through %s",
3809108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
3810108322fbScarlsonj 			return (B_TRUE);
3811108322fbScarlsonj 		}
3812108322fbScarlsonj 	}
3813108322fbScarlsonj 	return (B_FALSE);
3814108322fbScarlsonj }
3815108322fbScarlsonj 
38160209230bSgjelinek /*
38170209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
38180209230bSgjelinek  * configuration.
38190209230bSgjelinek  */
38200209230bSgjelinek static int
38210209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
38220209230bSgjelinek {
38230209230bSgjelinek 	int res;
38240209230bSgjelinek 	uint64_t tmp;
38250209230bSgjelinek 	struct zone_mcaptab mcap;
38260209230bSgjelinek 	char sched[MAXNAMELEN];
38270209230bSgjelinek 	zone_dochandle_t handle = NULL;
38280209230bSgjelinek 	char pool_err[128];
38290209230bSgjelinek 
38300209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
38310209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
38320209230bSgjelinek 		return (Z_BAD_HANDLE);
38330209230bSgjelinek 	}
38340209230bSgjelinek 
38350209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
38360209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
38370209230bSgjelinek 		zonecfg_fini_handle(handle);
38380209230bSgjelinek 		return (res);
38390209230bSgjelinek 	}
38400209230bSgjelinek 
38410209230bSgjelinek 	/*
38420209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
38430209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
38440209230bSgjelinek 	 */
38450209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
38460209230bSgjelinek 		uint64_t num;
38470209230bSgjelinek 		char smf_err[128];
38480209230bSgjelinek 
38490209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
38500209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
38510209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
38520209230bSgjelinek 			zonecfg_fini_handle(handle);
38530209230bSgjelinek 			return (Z_INVAL);
38540209230bSgjelinek 		}
38550209230bSgjelinek 
38560209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
38570209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
38580209230bSgjelinek 			    "failed: %s", smf_err);
38590209230bSgjelinek 			zonecfg_fini_handle(handle);
38600209230bSgjelinek 			return (Z_INVAL);
38610209230bSgjelinek 		}
38620209230bSgjelinek 	}
38630209230bSgjelinek 
38640209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
38650209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
38660209230bSgjelinek 	    strlen(sched) > 0) {
38670209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
38680209230bSgjelinek 		    strlen(sched)) == -1)
38690209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
38700209230bSgjelinek 			    "default scheduling class");
38710209230bSgjelinek 
38720209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
38730209230bSgjelinek 	    == Z_OK) {
38740209230bSgjelinek 		/*
38750209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
38760209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
38770209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
38780209230bSgjelinek 		 * in by default so we can print a warning and modify the class
38790209230bSgjelinek 		 * if we wouldn't be using FSS.
38800209230bSgjelinek 		 */
38810209230bSgjelinek 		char class_name[PC_CLNMSZ];
38820209230bSgjelinek 
38830209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
38840209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
38850209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
38860209230bSgjelinek 			    "the zone's scheduling class");
38870209230bSgjelinek 
38880209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
38890209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
38900209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
38910209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
38920209230bSgjelinek 			    "used for processes\nin the zone but to get the "
38930209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
38940209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
38950209230bSgjelinek 			    "details.");
38960209230bSgjelinek 
38970209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
38980209230bSgjelinek 			    strlen("FSS")) == -1)
38990209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
39000209230bSgjelinek 				    "zone scheduling class to FSS");
39010209230bSgjelinek 		}
39020209230bSgjelinek 	}
39030209230bSgjelinek 
39040209230bSgjelinek 	/*
39050209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
39060209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
39070209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
39080209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
39090209230bSgjelinek 	 * and just return if there is nothing to do.
39100209230bSgjelinek 	 *
39110209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
39120209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
39130209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
39140209230bSgjelinek 	 * zonecfg.
39150209230bSgjelinek 	 */
39160209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
39170209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
39180209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
39190209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
39200209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
39210209230bSgjelinek 			    pool_err);
39220209230bSgjelinek 		else
39230209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
39240209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
39250209230bSgjelinek 		zonecfg_fini_handle(handle);
39260209230bSgjelinek 		return (Z_POOL_BIND);
39270209230bSgjelinek 	}
39280209230bSgjelinek 
39290209230bSgjelinek 	/*
39300209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
39310209230bSgjelinek 	 */
39320209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
39330209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
39340209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
39350209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
39360209230bSgjelinek 		    "processor allocation within the specified range\n"
39370209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
39380209230bSgjelinek 		    "See poold(1M).");
39390209230bSgjelinek 	}
39400209230bSgjelinek 
39410209230bSgjelinek 	/* The following is a warning, not an error. */
39420209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
39430209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
39440209230bSgjelinek 		if (res == Z_POOL_BIND)
39450209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
39460209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
39470209230bSgjelinek 		else if (res == Z_POOL)
39480209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
39490209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
39500209230bSgjelinek 		else
39510209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
39520209230bSgjelinek 			    zonecfg_strerror(res));
39530209230bSgjelinek 	}
39540209230bSgjelinek 
39550209230bSgjelinek 	zonecfg_fini_handle(handle);
39560209230bSgjelinek 	return (Z_OK);
39570209230bSgjelinek }
39580209230bSgjelinek 
3959108322fbScarlsonj zoneid_t
39606cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
3961108322fbScarlsonj {
3962108322fbScarlsonj 	zoneid_t rval = -1;
39637c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
39647c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
39657c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
3966108322fbScarlsonj 	size_t rctlbufsz = 0;
3967fa9e4066Sahrens 	char *zfsbuf = NULL;
3968fa9e4066Sahrens 	size_t zfsbufsz = 0;
3969108322fbScarlsonj 	zoneid_t zoneid = -1;
39707c478bd9Sstevel@tonic-gate 	int xerr;
3971108322fbScarlsonj 	char *kzone;
3972108322fbScarlsonj 	FILE *fp = NULL;
397345916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
397445916cd2Sjpk 	int match = 0;
397545916cd2Sjpk 	int doi = 0;
3976f4b3ec61Sdh155122 	int flags;
3977f4b3ec61Sdh155122 	zone_iptype_t iptype;
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
39807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
39817c478bd9Sstevel@tonic-gate 		return (-1);
39827c478bd9Sstevel@tonic-gate 	}
3983108322fbScarlsonj 	if (zonecfg_in_alt_root())
3984108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
39857c478bd9Sstevel@tonic-gate 
3986f4b3ec61Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
3987f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
3988f4b3ec61Sdh155122 		return (-1);
3989f4b3ec61Sdh155122 	}
3990f4b3ec61Sdh155122 	switch (iptype) {
3991f4b3ec61Sdh155122 	case ZS_SHARED:
3992f4b3ec61Sdh155122 		flags = 0;
3993f4b3ec61Sdh155122 		break;
3994f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
3995f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
3996f4b3ec61Sdh155122 		break;
3997f4b3ec61Sdh155122 	}
3998f4b3ec61Sdh155122 
39997c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
40007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
40017c478bd9Sstevel@tonic-gate 		return (-1);
40027c478bd9Sstevel@tonic-gate 	}
40037c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4004ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
40057c478bd9Sstevel@tonic-gate 		goto error;
4006ffbafc53Scomay 
40076cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
40086cfd72c6Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
40097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
40107c478bd9Sstevel@tonic-gate 		goto error;
40117c478bd9Sstevel@tonic-gate 	}
4012ffbafc53Scomay 
4013fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4014fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4015fa9e4066Sahrens 		goto error;
4016fa9e4066Sahrens 	}
40177c478bd9Sstevel@tonic-gate 
40186cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
401945916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
402048451833Scarlsonj 		if (zcent != NULL) {
402145916cd2Sjpk 			match = zcent->zc_match;
402245916cd2Sjpk 			doi = zcent->zc_doi;
402345916cd2Sjpk 			*zlabel = zcent->zc_label;
402445916cd2Sjpk 		} else {
402545916cd2Sjpk 			goto error;
402645916cd2Sjpk 		}
402745916cd2Sjpk 	}
402845916cd2Sjpk 
4029108322fbScarlsonj 	kzone = zone_name;
4030108322fbScarlsonj 
4031108322fbScarlsonj 	/*
4032108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4033108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4034108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4035108322fbScarlsonj 	 * that use this same source node (same device and inode).
4036108322fbScarlsonj 	 */
4037108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4038108322fbScarlsonj 		goto error;
4039108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4040108322fbScarlsonj 		goto error;
4041108322fbScarlsonj 
40426cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4043108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4044108322fbScarlsonj 
4045108322fbScarlsonj 		/*
4046108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4047108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4048108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4049108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4050108322fbScarlsonj 		 * executables.
4051108322fbScarlsonj 		 *
4052108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4053108322fbScarlsonj 		 */
4054108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4055108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4056108322fbScarlsonj 			goto error;
4057108322fbScarlsonj 		}
4058108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4059108322fbScarlsonj 			goto error;
4060108322fbScarlsonj 	}
4061108322fbScarlsonj 
4062108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4063108322fbScarlsonj 		/*
4064108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4065108322fbScarlsonj 		 * then we have some additional work to do before starting the
4066108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4067108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4068108322fbScarlsonj 		 * the zone.
4069108322fbScarlsonj 		 */
4070108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4071108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4072108322fbScarlsonj 			goto error;
4073108322fbScarlsonj 		}
4074108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4075108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4076108322fbScarlsonj 			goto error;
4077108322fbScarlsonj 		}
4078108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4079108322fbScarlsonj 		    NULL, 0) == 0) {
4080108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4081108322fbScarlsonj 			goto error;
4082108322fbScarlsonj 		}
4083108322fbScarlsonj 		/* This is the preferred name */
4084108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4085108322fbScarlsonj 		    zone_name);
4086108322fbScarlsonj 		srandom(getpid());
4087108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4088108322fbScarlsonj 		    0) == 0) {
4089108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4090108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4091108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4092108322fbScarlsonj 		}
4093108322fbScarlsonj 		kzone = kernzone;
4094108322fbScarlsonj 	}
4095108322fbScarlsonj 
40967c478bd9Sstevel@tonic-gate 	xerr = 0;
4097108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4098f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4099f4b3ec61Sdh155122 	    flags)) == -1) {
41007c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
41017c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
41027c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
41037c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
41047c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
41057c478bd9Sstevel@tonic-gate 			} else {
41067c478bd9Sstevel@tonic-gate 
41077c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
41087c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
41097c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
41107c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
41117c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
41127c478bd9Sstevel@tonic-gate 			}
41137c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
41147c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
41157c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
41167c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
41178c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
41188c55461bSton 			char zonename[ZONENAME_MAX];
41198c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
41208c55461bSton 			    zonename, ZONENAME_MAX);
41218c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
41228c55461bSton 			    "used by the zone '%s'.", zonename);
41237c478bd9Sstevel@tonic-gate 		} else {
41247c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
41257c478bd9Sstevel@tonic-gate 		}
41267c478bd9Sstevel@tonic-gate 		goto error;
41277c478bd9Sstevel@tonic-gate 	}
4128108322fbScarlsonj 
4129108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4130108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4131108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4132108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4133108322fbScarlsonj 		goto error;
4134108322fbScarlsonj 	}
4135108322fbScarlsonj 
41360679f794S 	/*
41370679f794S 	 * The following actions are not performed when merely mounting a zone
41380679f794S 	 * for administrative use.
41390679f794S 	 */
41400679f794S 	if (mount_cmd == Z_MNT_BOOT) {
41410679f794S 		brand_handle_t bh;
41420679f794S 		struct brand_attr attr;
41430679f794S 		char modname[MAXPATHLEN];
41440679f794S 
41459acbbeafSnn35248 		if ((zone_get_brand(zone_name, attr.ba_brandname,
41469acbbeafSnn35248 		    MAXNAMELEN) != Z_OK) ||
4147123807fbSedp 		    (bh = brand_open(attr.ba_brandname)) == NULL) {
41480679f794S 			zerror(zlogp, B_FALSE,
41490679f794S 			    "unable to determine brand name");
41509acbbeafSnn35248 			return (-1);
41519acbbeafSnn35248 		}
41529acbbeafSnn35248 
41539acbbeafSnn35248 		/*
41549acbbeafSnn35248 		 * If this brand requires any kernel support, now is the time to
41559acbbeafSnn35248 		 * get it loaded and initialized.
41569acbbeafSnn35248 		 */
4157123807fbSedp 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4158e767a340Sgjelinek 			brand_close(bh);
41590679f794S 			zerror(zlogp, B_FALSE,
41600679f794S 			    "unable to determine brand kernel module");
41619acbbeafSnn35248 			return (-1);
41629acbbeafSnn35248 		}
4163e767a340Sgjelinek 		brand_close(bh);
41649acbbeafSnn35248 
41659acbbeafSnn35248 		if (strlen(modname) > 0) {
41669acbbeafSnn35248 			(void) strlcpy(attr.ba_modname, modname, MAXPATHLEN);
41679acbbeafSnn35248 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
41689acbbeafSnn35248 			    sizeof (attr) != 0)) {
41690679f794S 				zerror(zlogp, B_TRUE,
41700679f794S 				    "could not set zone brand attribute.");
41719acbbeafSnn35248 				goto error;
41729acbbeafSnn35248 			}
41739acbbeafSnn35248 		}
41749acbbeafSnn35248 
41750209230bSgjelinek 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) {
41760209230bSgjelinek 			(void) zone_shutdown(zoneid);
41770209230bSgjelinek 			goto error;
41780209230bSgjelinek 		}
41790209230bSgjelinek 
418045916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
41810209230bSgjelinek 	}
41820209230bSgjelinek 
4183108322fbScarlsonj 	rval = zoneid;
4184108322fbScarlsonj 	zoneid = -1;
4185108322fbScarlsonj 
41867c478bd9Sstevel@tonic-gate error:
4187108322fbScarlsonj 	if (zoneid != -1)
4188108322fbScarlsonj 		(void) zone_destroy(zoneid);
41897c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
41907c478bd9Sstevel@tonic-gate 		free(rctlbuf);
41917c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
4192108322fbScarlsonj 	if (fp != NULL)
4193108322fbScarlsonj 		zonecfg_close_scratch(fp);
4194108322fbScarlsonj 	lofs_discard_mnttab();
419545916cd2Sjpk 	if (zcent != NULL)
419645916cd2Sjpk 		tsol_freezcent(zcent);
41977c478bd9Sstevel@tonic-gate 	return (rval);
41987c478bd9Sstevel@tonic-gate }
41997c478bd9Sstevel@tonic-gate 
4200555afedfScarlsonj /*
4201555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
4202555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4203555afedfScarlsonj  * details from inside the zone.
4204555afedfScarlsonj  */
4205555afedfScarlsonj static void
4206555afedfScarlsonj write_index_file(zoneid_t zoneid)
4207555afedfScarlsonj {
4208555afedfScarlsonj 	FILE *zef;
4209555afedfScarlsonj 	FILE *zet;
4210555afedfScarlsonj 	struct zoneent *zep;
4211555afedfScarlsonj 	pid_t child;
4212555afedfScarlsonj 	int tmpl_fd;
4213555afedfScarlsonj 	ctid_t ct;
4214555afedfScarlsonj 	int fd;
4215555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4216555afedfScarlsonj 
4217555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
4218555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
4219555afedfScarlsonj 		return;
4220555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
4221555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
4222555afedfScarlsonj 			break;
4223555afedfScarlsonj 		free(zep);
4224555afedfScarlsonj 	}
4225555afedfScarlsonj 	endzoneent(zef);
4226555afedfScarlsonj 	if (zep == NULL)
4227555afedfScarlsonj 		return;
4228555afedfScarlsonj 
4229555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
4230555afedfScarlsonj 		free(zep);
4231555afedfScarlsonj 		return;
4232555afedfScarlsonj 	}
4233555afedfScarlsonj 
4234555afedfScarlsonj 	if ((child = fork()) == -1) {
4235555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4236555afedfScarlsonj 		(void) close(tmpl_fd);
4237555afedfScarlsonj 		free(zep);
4238555afedfScarlsonj 		return;
4239555afedfScarlsonj 	}
4240555afedfScarlsonj 
4241555afedfScarlsonj 	/* parent waits for child to finish */
4242555afedfScarlsonj 	if (child != 0) {
4243555afedfScarlsonj 		free(zep);
4244555afedfScarlsonj 		if (contract_latest(&ct) == -1)
4245555afedfScarlsonj 			ct = -1;
4246555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4247555afedfScarlsonj 		(void) close(tmpl_fd);
4248555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
4249555afedfScarlsonj 		(void) contract_abandon_id(ct);
4250555afedfScarlsonj 		return;
4251555afedfScarlsonj 	}
4252555afedfScarlsonj 
4253555afedfScarlsonj 	/* child enters zone and sets up index file */
4254555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
4255555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
4256555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4257555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4258555afedfScarlsonj 		    ZONE_CONFIG_GID);
4259555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4260555afedfScarlsonj 		    ZONE_INDEX_MODE);
4261555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4262555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4263555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
4264555afedfScarlsonj 				uuidstr[0] = '\0';
4265555afedfScarlsonj 			else
4266555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
4267555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4268555afedfScarlsonj 			    zone_state_str(zep->zone_state),
4269555afedfScarlsonj 			    uuidstr);
4270555afedfScarlsonj 			(void) fclose(zet);
4271555afedfScarlsonj 		}
4272555afedfScarlsonj 	}
4273555afedfScarlsonj 	_exit(0);
4274555afedfScarlsonj }
4275555afedfScarlsonj 
42767c478bd9Sstevel@tonic-gate int
42776cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
42787c478bd9Sstevel@tonic-gate {
42799acbbeafSnn35248 	char zonepath[MAXPATHLEN];
42805749802bSdp 
42816cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4282fa9e4066Sahrens 		lofs_discard_mnttab();
4283fa9e4066Sahrens 		return (-1);
4284fa9e4066Sahrens 	}
4285fa9e4066Sahrens 
42869acbbeafSnn35248 	/*
42879acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
42889acbbeafSnn35248 	 * attribute backing store for /dev
42899acbbeafSnn35248 	 */
42909acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
42919acbbeafSnn35248 		lofs_discard_mnttab();
42929acbbeafSnn35248 		return (-1);
42939acbbeafSnn35248 	}
429416bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
429527e6fb21Sdp 
429627e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
429727e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
429827e6fb21Sdp 	    0, 3) != 0) {
4299108322fbScarlsonj 		lofs_discard_mnttab();
43007c478bd9Sstevel@tonic-gate 		return (-1);
4301108322fbScarlsonj 	}
4302facf4a8dSllai1 
43039acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4304facf4a8dSllai1 		lofs_discard_mnttab();
4305facf4a8dSllai1 		return (-1);
4306facf4a8dSllai1 	}
4307facf4a8dSllai1 
43086cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
4309f4b3ec61Sdh155122 		zone_iptype_t iptype;
4310f4b3ec61Sdh155122 
4311f4b3ec61Sdh155122 		if (get_iptype(zlogp, &iptype) < 0) {
4312f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4313108322fbScarlsonj 			lofs_discard_mnttab();
43147c478bd9Sstevel@tonic-gate 			return (-1);
4315108322fbScarlsonj 		}
4316555afedfScarlsonj 
4317f4b3ec61Sdh155122 		switch (iptype) {
4318f4b3ec61Sdh155122 		case ZS_SHARED:
4319f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
4320f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
4321f4b3ec61Sdh155122 				lofs_discard_mnttab();
4322f4b3ec61Sdh155122 				return (-1);
4323f4b3ec61Sdh155122 			}
4324f4b3ec61Sdh155122 			break;
4325f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4326f4b3ec61Sdh155122 			if (configure_exclusive_network_interfaces(zlogp) !=
4327f4b3ec61Sdh155122 			    0) {
4328f4b3ec61Sdh155122 				lofs_discard_mnttab();
4329f4b3ec61Sdh155122 				return (-1);
4330f4b3ec61Sdh155122 			}
4331f4b3ec61Sdh155122 			break;
4332f4b3ec61Sdh155122 		}
4333f4b3ec61Sdh155122 	}
4334f4b3ec61Sdh155122 
4335555afedfScarlsonj 	write_index_file(zoneid);
4336555afedfScarlsonj 
4337108322fbScarlsonj 	lofs_discard_mnttab();
43387c478bd9Sstevel@tonic-gate 	return (0);
43397c478bd9Sstevel@tonic-gate }
43407c478bd9Sstevel@tonic-gate 
4341108322fbScarlsonj static int
4342108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
43437c478bd9Sstevel@tonic-gate {
4344108322fbScarlsonj 	char zroot[MAXPATHLEN];
4345108322fbScarlsonj 
4346108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4347108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4348108322fbScarlsonj 		return (-1);
4349108322fbScarlsonj 	}
4350108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4351108322fbScarlsonj 
4352108322fbScarlsonj 	/*
4353108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
4354108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
4355108322fbScarlsonj 	 * still be creds floating about in the system that reference the
4356108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4357108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4358108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
4359108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4360108322fbScarlsonj 	 */
4361108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
4362108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4363108322fbScarlsonj 			goto unmounted;
4364108322fbScarlsonj 		if (errno == EBUSY) {
4365108322fbScarlsonj 			int tries = 10;
4366108322fbScarlsonj 
4367108322fbScarlsonj 			while (--tries >= 0) {
4368108322fbScarlsonj 				(void) sleep(1);
4369108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
4370108322fbScarlsonj 					goto unmounted;
4371108322fbScarlsonj 				if (errno != EBUSY)
4372108322fbScarlsonj 					break;
4373108322fbScarlsonj 			}
4374108322fbScarlsonj 		}
4375108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4376108322fbScarlsonj 		return (-1);
4377108322fbScarlsonj 	}
4378108322fbScarlsonj unmounted:
4379108322fbScarlsonj 
4380108322fbScarlsonj 	/*
4381108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
4382108322fbScarlsonj 	 * entries.
4383108322fbScarlsonj 	 */
4384108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4385108322fbScarlsonj 		FILE *fp;
4386108322fbScarlsonj 		int retv;
4387108322fbScarlsonj 
4388108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4389108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4390108322fbScarlsonj 			return (-1);
4391108322fbScarlsonj 		}
4392108322fbScarlsonj 		retv = -1;
4393108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
4394108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4395108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4396108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4397108322fbScarlsonj 		else
4398108322fbScarlsonj 			retv = 0;
4399108322fbScarlsonj 		zonecfg_close_scratch(fp);
4400108322fbScarlsonj 		return (retv);
4401108322fbScarlsonj 	} else {
4402108322fbScarlsonj 		return (0);
4403108322fbScarlsonj 	}
4404108322fbScarlsonj }
4405108322fbScarlsonj 
4406108322fbScarlsonj int
44070209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4408108322fbScarlsonj {
4409108322fbScarlsonj 	char *kzone;
44107c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
44110209230bSgjelinek 	int res;
44120209230bSgjelinek 	char pool_err[128];
4413ff17c8bfSgjelinek 	char zpath[MAXPATHLEN];
44149acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
44159acbbeafSnn35248 	char brand[MAXNAMELEN];
4416123807fbSedp 	brand_handle_t bh = NULL;
4417f4b3ec61Sdh155122 	ushort_t flags;
44187c478bd9Sstevel@tonic-gate 
4419108322fbScarlsonj 	kzone = zone_name;
4420108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4421108322fbScarlsonj 		FILE *fp;
4422108322fbScarlsonj 
4423108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4424108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
4425108322fbScarlsonj 			goto error;
4426108322fbScarlsonj 		}
4427108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4428108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
4429108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4430108322fbScarlsonj 			zonecfg_close_scratch(fp);
4431108322fbScarlsonj 			goto error;
4432108322fbScarlsonj 		}
4433108322fbScarlsonj 		zonecfg_close_scratch(fp);
4434108322fbScarlsonj 		kzone = kernzone;
4435108322fbScarlsonj 	}
4436108322fbScarlsonj 
4437108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
44387c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
44397c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4440108322fbScarlsonj 		if (unmount_cmd)
4441108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
44427c478bd9Sstevel@tonic-gate 		goto error;
44437c478bd9Sstevel@tonic-gate 	}
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
44467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
44477c478bd9Sstevel@tonic-gate 		goto error;
44487c478bd9Sstevel@tonic-gate 	}
44497c478bd9Sstevel@tonic-gate 
4450ff17c8bfSgjelinek 	/* Get the zonepath of this zone */
4451ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
4452ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
44539acbbeafSnn35248 		goto error;
44549acbbeafSnn35248 	}
44559acbbeafSnn35248 
44569acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
44579acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
4458123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
44599acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
44609acbbeafSnn35248 		return (-1);
44619acbbeafSnn35248 	}
44629acbbeafSnn35248 	/*
44639acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
44649acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
44659acbbeafSnn35248 	 */
44669acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4467ff17c8bfSgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
4468ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
4469123807fbSedp 		brand_close(bh);
44709acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
44719acbbeafSnn35248 		    "halt callback.");
44729acbbeafSnn35248 		goto error;
44739acbbeafSnn35248 	}
4474123807fbSedp 	brand_close(bh);
44759acbbeafSnn35248 
44769acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
4477c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
44789acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
44799acbbeafSnn35248 		goto error;
44809acbbeafSnn35248 	}
44819acbbeafSnn35248 
4482f4b3ec61Sdh155122 	if (!unmount_cmd) {
4483f4b3ec61Sdh155122 		zone_iptype_t iptype;
4484f4b3ec61Sdh155122 
4485f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4486f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
4487f4b3ec61Sdh155122 			if (get_iptype(zlogp, &iptype) < 0) {
4488f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
4489f4b3ec61Sdh155122 				    "ip-type");
44907c478bd9Sstevel@tonic-gate 				goto error;
44917c478bd9Sstevel@tonic-gate 			}
4492f4b3ec61Sdh155122 		} else {
4493f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
4494f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
4495f4b3ec61Sdh155122 			else
4496f4b3ec61Sdh155122 				iptype = ZS_SHARED;
4497f4b3ec61Sdh155122 		}
4498f4b3ec61Sdh155122 
4499f4b3ec61Sdh155122 		switch (iptype) {
4500f4b3ec61Sdh155122 		case ZS_SHARED:
4501f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
4502f4b3ec61Sdh155122 			    zoneid) != 0) {
4503f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4504f4b3ec61Sdh155122 				    "network interfaces in zone");
4505f4b3ec61Sdh155122 				goto error;
4506f4b3ec61Sdh155122 			}
4507f4b3ec61Sdh155122 			break;
4508f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4509f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
4510f4b3ec61Sdh155122 			    zoneid) != 0) {
4511f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4512f4b3ec61Sdh155122 				    "network interfaces in zone");
4513f4b3ec61Sdh155122 				goto error;
4514f4b3ec61Sdh155122 			}
4515f4b3ec61Sdh155122 			break;
4516f4b3ec61Sdh155122 		}
4517f4b3ec61Sdh155122 	}
45187c478bd9Sstevel@tonic-gate 
4519108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
45207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
45217c478bd9Sstevel@tonic-gate 		goto error;
45227c478bd9Sstevel@tonic-gate 	}
45237c478bd9Sstevel@tonic-gate 
4524facf4a8dSllai1 	/* destroy zconsole before umount /dev */
4525facf4a8dSllai1 	if (!unmount_cmd)
4526facf4a8dSllai1 		destroy_console_slave();
4527facf4a8dSllai1 
4528108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
45297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
45307c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
45317c478bd9Sstevel@tonic-gate 		goto error;
45327c478bd9Sstevel@tonic-gate 	}
45337c478bd9Sstevel@tonic-gate 
45340209230bSgjelinek 	/*
4535cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
4536cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
4537cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
4538cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
4539cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
4540cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
4541cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
4542cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
45430209230bSgjelinek 	 */
4544cf6b13d2Sgjelinek 	if (!unmount_cmd) {
4545cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
4546cf6b13d2Sgjelinek 
4547cf6b13d2Sgjelinek 		if (rebooting) {
4548cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
4549cf6b13d2Sgjelinek 			zone_dochandle_t handle;
4550cf6b13d2Sgjelinek 
4551cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
4552cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4553cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4554cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
4555e767a340Sgjelinek 
4556e767a340Sgjelinek 			zonecfg_fini_handle(handle);
4557cf6b13d2Sgjelinek 		}
4558cf6b13d2Sgjelinek 
4559cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
45600209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
45610209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
45620209230bSgjelinek 				if (res == Z_POOL)
45630209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
45640209230bSgjelinek 			}
45650209230bSgjelinek 		}
4566cf6b13d2Sgjelinek 	}
45670209230bSgjelinek 
456845916cd2Sjpk 	remove_mlps(zlogp, zoneid);
456945916cd2Sjpk 
45707c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
45717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
45727c478bd9Sstevel@tonic-gate 		goto error;
45737c478bd9Sstevel@tonic-gate 	}
4574108322fbScarlsonj 
4575108322fbScarlsonj 	/*
4576108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
4577108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
4578108322fbScarlsonj 	 */
4579108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4580108322fbScarlsonj 		goto error;
4581108322fbScarlsonj 
4582108322fbScarlsonj 	lofs_discard_mnttab();
45837c478bd9Sstevel@tonic-gate 	return (0);
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate error:
4586108322fbScarlsonj 	lofs_discard_mnttab();
45877c478bd9Sstevel@tonic-gate 	return (-1);
45887c478bd9Sstevel@tonic-gate }
4589