xref: /titanic_50/usr/src/cmd/zoneadmd/vplat.c (revision 0dc2366f7b9f9f36e10909b1e95edbf2a261c2ac)
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 /*
2357a250fbSbatschul  * Copyright 2010 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>
745679c89fSjv227347 #include <sys/systeminfo.h>
757c478bd9Sstevel@tonic-gate 
76f4b3ec61Sdh155122 #include <libdlpi.h>
77f595a68aSyz147064 #include <libdllink.h>
78d62bc4baSyz147064 #include <libdlvlan.h>
79f4b3ec61Sdh155122 
807c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
817c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
827c478bd9Sstevel@tonic-gate #include <netinet/in.h>
837c478bd9Sstevel@tonic-gate #include <net/route.h>
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #include <stdio.h>
867c478bd9Sstevel@tonic-gate #include <errno.h>
877c478bd9Sstevel@tonic-gate #include <fcntl.h>
887c478bd9Sstevel@tonic-gate #include <unistd.h>
897c478bd9Sstevel@tonic-gate #include <rctl.h>
907c478bd9Sstevel@tonic-gate #include <stdlib.h>
917c478bd9Sstevel@tonic-gate #include <string.h>
927c478bd9Sstevel@tonic-gate #include <strings.h>
937c478bd9Sstevel@tonic-gate #include <wait.h>
947c478bd9Sstevel@tonic-gate #include <limits.h>
957c478bd9Sstevel@tonic-gate #include <libgen.h>
96fa9e4066Sahrens #include <libzfs.h>
97facf4a8dSllai1 #include <libdevinfo.h>
987c478bd9Sstevel@tonic-gate #include <zone.h>
997c478bd9Sstevel@tonic-gate #include <assert.h>
100555afedfScarlsonj #include <libcontract.h>
101555afedfScarlsonj #include <libcontract_priv.h>
102555afedfScarlsonj #include <uuid/uuid.h>
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1057c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1067c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1077c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
108fa9e4066Sahrens #include <sys/fs/zfs.h>
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #include <pool.h>
1117c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1120209230bSgjelinek #include <sys/priocntl.h>
1137c478bd9Sstevel@tonic-gate 
1149acbbeafSnn35248 #include <libbrand.h>
1159acbbeafSnn35248 #include <sys/brand.h>
1167c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
11739d3e169Sevanl #include <synch.h>
11822321485Svp157776 
1197c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
12045916cd2Sjpk #include <tsol/label.h>
12145916cd2Sjpk #include <libtsnet.h>
12245916cd2Sjpk #include <sys/priv.h>
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1257c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1287c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13145916cd2Sjpk #define	MAXTNZLEN	2048
1327c478bd9Sstevel@tonic-gate 
1336cfd72c6Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1346cfd72c6Sgjelinek 
1357c478bd9Sstevel@tonic-gate /* for routing socket */
1367c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1377c478bd9Sstevel@tonic-gate 
138108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
139108322fbScarlsonj static char kernzone[ZONENAME_MAX];
140108322fbScarlsonj 
141108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
142108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
143108322fbScarlsonj 
14445916cd2Sjpk /* for Trusted Extensions */
14545916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
14645916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
14745916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
1489d48116cSdh155122 
14945916cd2Sjpk static m_label_t *zlabel = NULL;
15045916cd2Sjpk static m_label_t *zid_label = NULL;
15145916cd2Sjpk static priv_set_t *zprivs = NULL;
15245916cd2Sjpk 
1537c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1547c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1557c478bd9Sstevel@tonic-gate 
156c5cd6260S /* from zoneadmd */
157c5cd6260S extern char query_hook[];
158c5cd6260S 
1597c478bd9Sstevel@tonic-gate /*
160108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
161108322fbScarlsonj  * data) only once every N times through the loop.
162108322fbScarlsonj  */
163108322fbScarlsonj #define	MNTTAB_HUNK	32
164108322fbScarlsonj 
165108322fbScarlsonj /*
1667c478bd9Sstevel@tonic-gate  * Private autofs system call
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate static int
1717c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
179108322fbScarlsonj static void
180108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
181108322fbScarlsonj {
182108322fbScarlsonj 	uint_t i;
183108322fbScarlsonj 
184108322fbScarlsonj 	if (mnt_array == NULL)
185108322fbScarlsonj 		return;
186108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
187108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
188108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
189108322fbScarlsonj 		free(mnt_array[i].mnt_special);
190108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
191108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
192108322fbScarlsonj 	}
193108322fbScarlsonj 	free(mnt_array);
194108322fbScarlsonj }
195108322fbScarlsonj 
196108322fbScarlsonj /*
197108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
198108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
199108322fbScarlsonj  * array in "nelemp".
200108322fbScarlsonj  */
201108322fbScarlsonj static int
202108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
203108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
204108322fbScarlsonj {
205108322fbScarlsonj 	struct mnttab mnt;
206108322fbScarlsonj 	struct mnttab *mnts;
207108322fbScarlsonj 	struct mnttab *mnp;
208108322fbScarlsonj 	uint_t nmnt;
209108322fbScarlsonj 
210108322fbScarlsonj 	rewind(mnttab);
211108322fbScarlsonj 	resetmnttab(mnttab);
212108322fbScarlsonj 	nmnt = 0;
213108322fbScarlsonj 	mnts = NULL;
214108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
215108322fbScarlsonj 		struct mnttab *tmp_array;
216108322fbScarlsonj 
217108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
218108322fbScarlsonj 			continue;
219108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
220108322fbScarlsonj 			tmp_array = realloc(mnts,
221108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
222108322fbScarlsonj 			if (tmp_array == NULL) {
223108322fbScarlsonj 				free_mnttable(mnts, nmnt);
224108322fbScarlsonj 				return (-1);
225108322fbScarlsonj 			}
226108322fbScarlsonj 			mnts = tmp_array;
227108322fbScarlsonj 		}
228108322fbScarlsonj 		mnp = &mnts[nmnt++];
229108322fbScarlsonj 
230108322fbScarlsonj 		/*
231108322fbScarlsonj 		 * Zero out any fields we're not using.
232108322fbScarlsonj 		 */
233108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
234108322fbScarlsonj 
235108322fbScarlsonj 		if (mnt.mnt_special != NULL)
236108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
237108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
238108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
239108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
240108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
241108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
242108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
243108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
244108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
245108322fbScarlsonj 			free_mnttable(mnts, nmnt);
246108322fbScarlsonj 			return (-1);
247108322fbScarlsonj 		}
248108322fbScarlsonj 	}
249108322fbScarlsonj 	*mnt_arrayp = mnts;
250108322fbScarlsonj 	*nelemp = nmnt;
251108322fbScarlsonj 	return (0);
252108322fbScarlsonj }
253108322fbScarlsonj 
254108322fbScarlsonj /*
255108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
256108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
257108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
258108322fbScarlsonj  * reread of the list of mount points
259108322fbScarlsonj  */
260108322fbScarlsonj static void
261108322fbScarlsonj lofs_discard_mnttab(void)
262108322fbScarlsonj {
263108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
264108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
265108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
266108322fbScarlsonj }
267108322fbScarlsonj 
268108322fbScarlsonj static int
269108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
270108322fbScarlsonj {
271108322fbScarlsonj 	FILE *mnttab;
272108322fbScarlsonj 	uint_t nmnts;
273108322fbScarlsonj 
274108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
275108322fbScarlsonj 		return (-1);
276108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
277108322fbScarlsonj 	    &nmnts) == -1) {
278108322fbScarlsonj 		(void) fclose(mnttab);
279108322fbScarlsonj 		return (-1);
280108322fbScarlsonj 	}
281108322fbScarlsonj 	(void) fclose(mnttab);
282108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
283108322fbScarlsonj 	return (0);
284108322fbScarlsonj }
285108322fbScarlsonj 
286108322fbScarlsonj /*
287108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
288108322fbScarlsonj  * path and resolves them all down to an absolute path.
289108322fbScarlsonj  */
290910f48daSedp void
291108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
292108322fbScarlsonj {
293108322fbScarlsonj 	int len, arlen;
294108322fbScarlsonj 	const char *altroot;
295108322fbScarlsonj 	char tmppath[MAXPATHLEN];
296108322fbScarlsonj 	boolean_t outside_altroot;
297108322fbScarlsonj 
298108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
299108322fbScarlsonj 		return;
300108322fbScarlsonj 	tmppath[len] = '\0';
301108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
302108322fbScarlsonj 
303108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
304108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
305108322fbScarlsonj 		return;
306108322fbScarlsonj 
307108322fbScarlsonj 	altroot = zonecfg_get_root();
308108322fbScarlsonj 	arlen = strlen(altroot);
309108322fbScarlsonj 	outside_altroot = B_FALSE;
310108322fbScarlsonj 	for (;;) {
311108322fbScarlsonj 		struct mnttab *mnp;
312108322fbScarlsonj 
3131e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3141e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3151e9d8f9fSdminer 		    mnp--) {
316108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
317108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3181e9d8f9fSdminer 			    mnp->mnt_special == NULL)
319108322fbScarlsonj 				continue;
320108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
321108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
322108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
323108322fbScarlsonj 				break;
324108322fbScarlsonj 		}
3251e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3261e9d8f9fSdminer 			break;
3271e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3281e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
329108322fbScarlsonj 			break;
330108322fbScarlsonj 		if (outside_altroot) {
331108322fbScarlsonj 			char *cp;
332108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
333108322fbScarlsonj 
334108322fbScarlsonj 			/*
335108322fbScarlsonj 			 * If we run into a read-only mount outside of the
336108322fbScarlsonj 			 * alternate root environment, then the user doesn't
337108322fbScarlsonj 			 * want this path to be made read-write.
338108322fbScarlsonj 			 */
339108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
340108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
341108322fbScarlsonj 			    NULL &&
342108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
343108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
344108322fbScarlsonj 				break;
345108322fbScarlsonj 			}
346108322fbScarlsonj 		} else if (arlen > 0 &&
347108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
348108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
349108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
350108322fbScarlsonj 			outside_altroot = B_TRUE;
351108322fbScarlsonj 		}
352108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
353108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
354108322fbScarlsonj 		    mnp->mnt_special, path + len);
355108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
356108322fbScarlsonj 			break;
357108322fbScarlsonj 		path[len] = '\0';
358108322fbScarlsonj 	}
359108322fbScarlsonj }
360108322fbScarlsonj 
361108322fbScarlsonj /*
362108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
363108322fbScarlsonj  * referenced device is already mounted somewhere.
364108322fbScarlsonj  */
365108322fbScarlsonj static int
366108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
367108322fbScarlsonj {
368108322fbScarlsonj 	struct mnttab *mnp;
369108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
370108322fbScarlsonj 
371108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
372108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
373108322fbScarlsonj 		return (-1);
374108322fbScarlsonj 
375108322fbScarlsonj 	/*
376108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
377108322fbScarlsonj 	 * no need to worry about conflicting mounts.
378108322fbScarlsonj 	 */
379108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
380108322fbScarlsonj 	    mnp++) {
381108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
382108322fbScarlsonj 			break;
383108322fbScarlsonj 	}
384108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
385108322fbScarlsonj 		return (0);
386108322fbScarlsonj 
387108322fbScarlsonj 	/*
388108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
389108322fbScarlsonj 	 */
390108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
391108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
392108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
393108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
394108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
395108322fbScarlsonj 
396108322fbScarlsonj 	/*
397108322fbScarlsonj 	 * Discard all but one of the original options and set that to be the
398108322fbScarlsonj 	 * same set of options used for inherit package directory resources.
399108322fbScarlsonj 	 */
400108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
401108322fbScarlsonj 	if (optptr == NULL) {
402108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
403108322fbScarlsonj 		if (optptr == NULL) {
404108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
405108322fbScarlsonj 			    fsptr->zone_fs_dir);
406108322fbScarlsonj 			return (-1);
407108322fbScarlsonj 		}
408108322fbScarlsonj 	} else {
409108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
410108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
411108322fbScarlsonj 			free(onext);
412108322fbScarlsonj 		}
413108322fbScarlsonj 	}
414108322fbScarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
415108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
416108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
417108322fbScarlsonj 	return (0);
418108322fbScarlsonj }
419108322fbScarlsonj 
420d314f035Sedp int
42127e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
42227e6fb21Sdp     uid_t userid, gid_t groupid)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4257c478bd9Sstevel@tonic-gate 	struct stat st;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4287c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4307c478bd9Sstevel@tonic-gate 		    subdir);
4317c478bd9Sstevel@tonic-gate 		return (-1);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4357c478bd9Sstevel@tonic-gate 		/*
4367c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4377c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4387c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4397c478bd9Sstevel@tonic-gate 		 */
4407c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
441756cf395SRic Aleshire 			if (S_ISREG(st.st_mode)) {
44245916cd2Sjpk 				/*
443756cf395SRic Aleshire 				 * Allow readonly mounts of /etc/ files; this
444756cf395SRic Aleshire 				 * is needed most by Trusted Extensions.
44545916cd2Sjpk 				 */
44645916cd2Sjpk 				if (strncmp(subdir, "/etc/",
44745916cd2Sjpk 				    strlen("/etc/")) != 0) {
44845916cd2Sjpk 					zerror(zlogp, B_FALSE,
44945916cd2Sjpk 					    "%s is not in /etc", path);
4507c478bd9Sstevel@tonic-gate 					return (-1);
4517c478bd9Sstevel@tonic-gate 				}
45245916cd2Sjpk 			} else {
45345916cd2Sjpk 				zerror(zlogp, B_FALSE,
45445916cd2Sjpk 				    "%s is not a directory", path);
45545916cd2Sjpk 				return (-1);
45645916cd2Sjpk 			}
45745916cd2Sjpk 		}
45827e6fb21Sdp 		return (0);
45927e6fb21Sdp 	}
46027e6fb21Sdp 
46127e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
4627c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4637c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4647c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4657c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4667c478bd9Sstevel@tonic-gate 		else
4677c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4687c478bd9Sstevel@tonic-gate 		return (-1);
4697c478bd9Sstevel@tonic-gate 	}
47027e6fb21Sdp 
47127e6fb21Sdp 	(void) chown(path, userid, groupid);
4727c478bd9Sstevel@tonic-gate 	return (0);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate static void
4767c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	uint_t i;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (types == NULL)
4817c478bd9Sstevel@tonic-gate 		return;
4827c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
4837c478bd9Sstevel@tonic-gate 		free(types[i]);
4847c478bd9Sstevel@tonic-gate 	free(types);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate static char **
4887c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	char **types = NULL;
4917c478bd9Sstevel@tonic-gate 	FILE *fp;
4927c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
4937c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
4947c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
4957c478bd9Sstevel@tonic-gate 	uint_t i;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
4987c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
4997c478bd9Sstevel@tonic-gate 		return (NULL);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	/*
5027c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5057c478bd9Sstevel@tonic-gate 		lines++;
5067c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5077c478bd9Sstevel@tonic-gate 		goto out;
5087c478bd9Sstevel@tonic-gate 	rewind(fp);
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5117c478bd9Sstevel@tonic-gate 	 */
5127c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5137c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5157c478bd9Sstevel@tonic-gate 		goto out;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	i = 0;
5187c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5197c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5207c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5217c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5227c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5237c478bd9Sstevel@tonic-gate 			types = NULL;
5247c478bd9Sstevel@tonic-gate 			goto out;
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5277c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5287c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5297c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5307c478bd9Sstevel@tonic-gate 			types = NULL;
5317c478bd9Sstevel@tonic-gate 			goto out;
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 		i++;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate out:
5367c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5377c478bd9Sstevel@tonic-gate 	return (types);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate static boolean_t
5417c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate 	uint_t i;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5467c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5477c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5487c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5497c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
554108322fbScarlsonj /*
555108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
556108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
557108322fbScarlsonj  */
5587c478bd9Sstevel@tonic-gate static void
559108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5607c478bd9Sstevel@tonic-gate {
561108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
562108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
563108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5687c478bd9Sstevel@tonic-gate  *
5697c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5707c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5717c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5727c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5737c478bd9Sstevel@tonic-gate  *
5747c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5757c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5767c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5797c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5807c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5817c478bd9Sstevel@tonic-gate  *
5827c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5837c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5847c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5857c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5867c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5877c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5887c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5897c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5907c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5917c478bd9Sstevel@tonic-gate  * graceful mounts first.
5927c478bd9Sstevel@tonic-gate  *
5937c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate static int
596108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	int error = 0;
5997c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6007c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6017c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6027c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6037c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6047c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6057c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6067c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6107c478bd9Sstevel@tonic-gate 		return (-1);
6117c478bd9Sstevel@tonic-gate 	}
612108322fbScarlsonj 	if (unmount_cmd)
613108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6167c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6177c478bd9Sstevel@tonic-gate 
61845916cd2Sjpk 	/*
61945916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
62045916cd2Sjpk 	 * of our zone's /export/home
62145916cd2Sjpk 	 */
62248451833Scarlsonj 	if (!unmount_cmd)
62345916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
62445916cd2Sjpk 
6257c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6267c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6277c478bd9Sstevel@tonic-gate 		return (-1);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6317c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6327c478bd9Sstevel@tonic-gate 	 */
6337c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6347c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6357c478bd9Sstevel@tonic-gate 		error++;
6367c478bd9Sstevel@tonic-gate 		goto out;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	/*
6407c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6417c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6427c478bd9Sstevel@tonic-gate 	 */
6437c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6447c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6457c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6467c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6477c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6487c478bd9Sstevel@tonic-gate 		char *path;
6497c478bd9Sstevel@tonic-gate 		uint_t i;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		mnts = NULL;
6527c478bd9Sstevel@tonic-gate 		nmnt = 0;
6537c478bd9Sstevel@tonic-gate 		/*
6547c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6557c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6567c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6577c478bd9Sstevel@tonic-gate 		 * filesystems.
6587c478bd9Sstevel@tonic-gate 		 */
6597c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6607c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6617c478bd9Sstevel@tonic-gate 			error++;
6627c478bd9Sstevel@tonic-gate 			goto out;
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6657c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6667c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6677c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6687c478bd9Sstevel@tonic-gate 			/*
6697c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6707c478bd9Sstevel@tonic-gate 			 *
6717c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6727c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6737c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
6747c478bd9Sstevel@tonic-gate 			 */
6757c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6767c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6777c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6787c478bd9Sstevel@tonic-gate 			}
6797c478bd9Sstevel@tonic-gate 			/*
6807c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6817c478bd9Sstevel@tonic-gate 			 */
6827c478bd9Sstevel@tonic-gate 			if (stuck) {
6837c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6847c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6857c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
6867c478bd9Sstevel@tonic-gate 				} else {
6877c478bd9Sstevel@tonic-gate 					/*
6887c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
6897c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
6907c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
6917c478bd9Sstevel@tonic-gate 					 * bail.
6927c478bd9Sstevel@tonic-gate 					 */
6937c478bd9Sstevel@tonic-gate 					error++;
6947c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6957c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
6967c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
6977c478bd9Sstevel@tonic-gate 					goto out;
6987c478bd9Sstevel@tonic-gate 				}
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate 			/*
7017c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7027c478bd9Sstevel@tonic-gate 			 */
7037c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7047c478bd9Sstevel@tonic-gate 				newcount++;
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7097c478bd9Sstevel@tonic-gate 			break;
7107c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7117c478bd9Sstevel@tonic-gate 			/*
7127c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7137c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7147c478bd9Sstevel@tonic-gate 			 */
7157c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		/*
7207c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7217c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7247c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7257c478bd9Sstevel@tonic-gate 			error++;
7267c478bd9Sstevel@tonic-gate 			goto out;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate out:
7317c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7327c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7337c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static int
7377c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7407c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7477c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7487c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7497c478bd9Sstevel@tonic-gate  *
7507c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7517c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7527c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7537c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7547c478bd9Sstevel@tonic-gate  * remains the case.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate static int
7577c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7607c478bd9Sstevel@tonic-gate 	int child_status = 0;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/*
7637c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7647c478bd9Sstevel@tonic-gate 	 */
7657c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7667c478bd9Sstevel@tonic-gate 	child_pid = fork();
7677c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7687c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
7697c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7707c478bd9Sstevel@tonic-gate 		return (-1);
7717c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
7727c478bd9Sstevel@tonic-gate 		closefrom(0);
7731390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7741390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7751390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7761390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7777c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
7787c478bd9Sstevel@tonic-gate 		/*
7797c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7807c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7817c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7827c478bd9Sstevel@tonic-gate 		 * accordingly.
7837c478bd9Sstevel@tonic-gate 		 */
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7867c478bd9Sstevel@tonic-gate 	} else {
7877c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7917c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7927c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7937c478bd9Sstevel@tonic-gate 		return (-1);
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
7967c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
7977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
7987c478bd9Sstevel@tonic-gate 		return (-1);
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate static int
80493239addSjohnlev isregfile(const char *path)
80593239addSjohnlev {
80693239addSjohnlev 	struct stat64 st;
80793239addSjohnlev 
80893239addSjohnlev 	if (stat64(path, &st) == -1)
80993239addSjohnlev 		return (-1);
81093239addSjohnlev 
81193239addSjohnlev 	return (S_ISREG(st.st_mode));
81293239addSjohnlev }
81393239addSjohnlev 
81493239addSjohnlev static int
8157c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
81857a250fbSbatschul 	char *argv[5];
8197c478bd9Sstevel@tonic-gate 	int status;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/*
8227c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8237c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8247c478bd9Sstevel@tonic-gate 	 */
8257c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8269acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8287c478bd9Sstevel@tonic-gate 		return (-1);
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
83193239addSjohnlev 	/*
83293239addSjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
83393239addSjohnlev 	 * in zoneadm.
83493239addSjohnlev 	 */
83593239addSjohnlev 	if (isregfile(cmdbuf) == -1)
83693239addSjohnlev 		return (0);
83793239addSjohnlev 
8387c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
83957a250fbSbatschul 	argv[1] = "-o";
84057a250fbSbatschul 	argv[2] = "p";
84157a250fbSbatschul 	argv[3] = (char *)rawdev;
84257a250fbSbatschul 	argv[4] = NULL;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8457c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8467c478bd9Sstevel@tonic-gate 		return (status);
8477c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8487c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8497c478bd9Sstevel@tonic-gate 	return (-1);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate static int
8537c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8547c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8577c478bd9Sstevel@tonic-gate 	char *argv[6];
8587c478bd9Sstevel@tonic-gate 	int status;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/*
8617c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8627c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8637c478bd9Sstevel@tonic-gate 	 */
8647c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8659acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8677c478bd9Sstevel@tonic-gate 		return (-1);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
8707c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
8717c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
8727c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
8737c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
8747c478bd9Sstevel@tonic-gate 	} else {
8757c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
8767c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
8777c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
8787c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
8797c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8837c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8847c478bd9Sstevel@tonic-gate 		return (status);
8857c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
8867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8877c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8887c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8897c478bd9Sstevel@tonic-gate 	else
8907c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8917c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8927c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8937c478bd9Sstevel@tonic-gate 	return (-1);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate /*
897d314f035Sedp  * Check if a given mount point path exists.
898d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
899d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
900d314f035Sedp  * component of the mount point path, so it must be a directory.
901d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
902d314f035Sedp  * path, so the mount point itself can be anything aside from a
903d314f035Sedp  * symbolic link.
904d314f035Sedp  *
905d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
906d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
907d314f035Sedp  * If the path doesn't exist return a positive value.
9087c478bd9Sstevel@tonic-gate  */
9097c478bd9Sstevel@tonic-gate static int
910d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9137c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9147c478bd9Sstevel@tonic-gate 	int res;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9177c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
918d314f035Sedp 			return (1);
9197c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9207c478bd9Sstevel@tonic-gate 		return (-1);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9247c478bd9Sstevel@tonic-gate 		return (-1);
9257c478bd9Sstevel@tonic-gate 	}
926d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9287c478bd9Sstevel@tonic-gate 		return (-1);
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9317c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9327c478bd9Sstevel@tonic-gate 		return (-1);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9357c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9367c478bd9Sstevel@tonic-gate 		/*
937d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9387c478bd9Sstevel@tonic-gate 		 */
9397c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9407c478bd9Sstevel@tonic-gate 		return (-1);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 	return (0);
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate /*
946d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
947d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
948d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
949d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
950d314f035Sedp  * object.  (Different filesystems can support different mount points,
951d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
952d314f035Sedp  * while "ufs" just supports directories.)
9537c478bd9Sstevel@tonic-gate  *
954d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
955d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
956d314f035Sedp  * If the path doesn't exist return a positive value.
9577c478bd9Sstevel@tonic-gate  */
958d314f035Sedp int
959d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
960d314f035Sedp     const char *dir, const char *fstype)
9617c478bd9Sstevel@tonic-gate {
962d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
963d314f035Sedp 	int rv;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/*
966d314f035Sedp 	 * Sanity check the target mount point path.
967d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
9687c478bd9Sstevel@tonic-gate 	 */
969d314f035Sedp 	if (dir[0] != '/') {
970d314f035Sedp 		if (spec[0] == '\0') {
971d314f035Sedp 			/*
972d314f035Sedp 			 * This must be an invalid ipd entry (see comments
973d314f035Sedp 			 * in mount_filesystems_ipdent()).
974d314f035Sedp 			 */
975d314f035Sedp 			zerror(zlogp, B_FALSE,
976d314f035Sedp 			    "invalid inherit-pkg-dir entry: \"%s\"", dir);
977d314f035Sedp 		} else {
978d314f035Sedp 			/* Something went wrong. */
979d314f035Sedp 			zerror(zlogp, B_FALSE, "invalid mount directory, "
980d314f035Sedp 			    "type: \"%s\", special: \"%s\", dir: \"%s\"",
981d314f035Sedp 			    fstype, spec, dir);
982d314f035Sedp 		}
983d314f035Sedp 		return (-1);
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 
986d314f035Sedp 	/*
987d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
988d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
989d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
990d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
991d314f035Sedp 	 * cannonical path check in valid_mount_point()).
992d314f035Sedp 	 */
993d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
994d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
995d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
996d314f035Sedp 		    rootpath, dir);
997d314f035Sedp 		return (-1);
998d314f035Sedp 	}
999d314f035Sedp 
1000d314f035Sedp 	/*
1001d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
1002d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
1003d314f035Sedp 	 */
10047c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10057c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10067c478bd9Sstevel@tonic-gate 	do {
1007d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
10087c478bd9Sstevel@tonic-gate 		*slashp = '\0';
1009d314f035Sedp 		if (slashp_next != NULL) {
1010d314f035Sedp 			/* This is an intermediary mount path component. */
1011d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1012d314f035Sedp 		} else {
1013d314f035Sedp 			/* This is the last component of the mount path. */
1014d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1015d314f035Sedp 		}
1016d314f035Sedp 		if (rv < 0)
1017d314f035Sedp 			return (rv);
10187c478bd9Sstevel@tonic-gate 		*slashp = '/';
1019d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1020d314f035Sedp 	return (rv);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate static int
10249acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10259acbbeafSnn35248 {
10269acbbeafSnn35248 	di_prof_t prof = arg;
10279acbbeafSnn35248 
10289acbbeafSnn35248 	if (name == NULL)
10299acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
10309acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
10319acbbeafSnn35248 }
10329acbbeafSnn35248 
10339acbbeafSnn35248 static int
10349acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10359acbbeafSnn35248 {
10369acbbeafSnn35248 	di_prof_t prof = arg;
10379acbbeafSnn35248 
10389acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
10399acbbeafSnn35248 }
10409acbbeafSnn35248 
10412b24ab6bSSebastien Roy int
10422b24ab6bSSebastien Roy vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1043f4b3ec61Sdh155122 {
1044f4b3ec61Sdh155122 	zone_dochandle_t handle;
1045f4b3ec61Sdh155122 
1046f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
1047f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1048f4b3ec61Sdh155122 		return (-1);
1049f4b3ec61Sdh155122 	}
1050f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1051f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
1052f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1053f4b3ec61Sdh155122 		return (-1);
1054f4b3ec61Sdh155122 	}
1055f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1056f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1057f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1058f4b3ec61Sdh155122 		return (-1);
1059f4b3ec61Sdh155122 	}
1060f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1061f4b3ec61Sdh155122 	return (0);
1062f4b3ec61Sdh155122 }
1063f4b3ec61Sdh155122 
10649acbbeafSnn35248 /*
10659acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10669acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10679acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10689acbbeafSnn35248  */
10699acbbeafSnn35248 static int
10700679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10719acbbeafSnn35248 {
10729acbbeafSnn35248 	char			brand[MAXNAMELEN];
10739acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1074123807fbSedp 	brand_handle_t		bh = NULL;
10759acbbeafSnn35248 	struct zone_devtab	ztab;
10769acbbeafSnn35248 	di_prof_t		prof = NULL;
10779acbbeafSnn35248 	int			err;
10789acbbeafSnn35248 	int			retval = -1;
1079f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1080f4b3ec61Sdh155122 	const char 		*curr_iptype;
10819acbbeafSnn35248 
10829acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
10839acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10849acbbeafSnn35248 		goto cleanup;
10859acbbeafSnn35248 	}
10869acbbeafSnn35248 
10870679f794S 	/*
10880679f794S 	 * Get a handle to the brand info for this zone.
1089e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
10900679f794S 	 * brand device mounts.
10910679f794S 	 */
10920679f794S 	if (ALT_MOUNT(mount_cmd)) {
1093e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
10940679f794S 	} else {
109562868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
10960679f794S 	}
10970679f794S 
10980679f794S 	if ((bh = brand_open(brand)) == NULL) {
10999acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
11009acbbeafSnn35248 		goto cleanup;
11019acbbeafSnn35248 	}
11029acbbeafSnn35248 
11032b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
1104f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1105f4b3ec61Sdh155122 		goto cleanup;
1106f4b3ec61Sdh155122 	}
1107f4b3ec61Sdh155122 	switch (iptype) {
1108f4b3ec61Sdh155122 	case ZS_SHARED:
1109f4b3ec61Sdh155122 		curr_iptype = "shared";
1110f4b3ec61Sdh155122 		break;
1111f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1112f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1113f4b3ec61Sdh155122 		break;
1114f4b3ec61Sdh155122 	}
1115f4b3ec61Sdh155122 
1116123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1117f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11189acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11199acbbeafSnn35248 		goto cleanup;
11209acbbeafSnn35248 	}
11219acbbeafSnn35248 
1122123807fbSedp 	if (brand_platform_iter_link(bh,
11239acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11249acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11259acbbeafSnn35248 		goto cleanup;
11269acbbeafSnn35248 	}
11279acbbeafSnn35248 
11289acbbeafSnn35248 	/* Add user-specified devices and directories */
11299acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11309acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11319acbbeafSnn35248 		goto cleanup;
11329acbbeafSnn35248 	}
11339acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11349acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11359acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11369acbbeafSnn35248 		goto cleanup;
11379acbbeafSnn35248 	}
11389acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
11399acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11409acbbeafSnn35248 		    zonecfg_strerror(err));
11419acbbeafSnn35248 		goto cleanup;
11429acbbeafSnn35248 	}
11439acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11449acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11459acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
11469acbbeafSnn35248 			    "user-specified device");
11479acbbeafSnn35248 			goto cleanup;
11489acbbeafSnn35248 		}
11499acbbeafSnn35248 	}
11509acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
11519acbbeafSnn35248 
11529acbbeafSnn35248 	/* Send profile to kernel */
11539acbbeafSnn35248 	if (di_prof_commit(prof)) {
11549acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11559acbbeafSnn35248 		goto cleanup;
11569acbbeafSnn35248 	}
11579acbbeafSnn35248 
11589acbbeafSnn35248 	retval = 0;
11599acbbeafSnn35248 
11609acbbeafSnn35248 cleanup:
1161123807fbSedp 	if (bh != NULL)
1162123807fbSedp 		brand_close(bh);
1163e767a340Sgjelinek 	if (handle != NULL)
11649acbbeafSnn35248 		zonecfg_fini_handle(handle);
11659acbbeafSnn35248 	if (prof)
11669acbbeafSnn35248 		di_prof_fini(prof);
11679acbbeafSnn35248 	return (retval);
11689acbbeafSnn35248 }
11699acbbeafSnn35248 
11709acbbeafSnn35248 static int
11710679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11720679f794S     zone_mnt_t mount_cmd)
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
1175108322fbScarlsonj 	char specpath[MAXPATHLEN];
11767c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11777c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11789acbbeafSnn35248 	int rv;
11797c478bd9Sstevel@tonic-gate 
1180d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1181d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11837c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11847c478bd9Sstevel@tonic-gate 		return (-1);
1185d314f035Sedp 	} else if (rv > 0) {
1186d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1187d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1188d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1189d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1190d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1191d314f035Sedp 			return (-1);
11927c478bd9Sstevel@tonic-gate 		}
11937c478bd9Sstevel@tonic-gate 
1194d314f035Sedp 		/*
1195d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1196d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1197d314f035Sedp 		 * to make sure that the mount point path is canonical,
1198d314f035Sedp 		 * which it can only do if the path exists, so now that
1199d314f035Sedp 		 * we've created the path we have to verify it again.
1200d314f035Sedp 		 */
1201d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1202d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1203d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1204d314f035Sedp 			zerror(zlogp, B_FALSE,
1205d314f035Sedp 			    "%s%s is not a valid mount point",
1206d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
12077c478bd9Sstevel@tonic-gate 			return (-1);
1208d314f035Sedp 		}
1209d314f035Sedp 	}
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12127c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
12157c478bd9Sstevel@tonic-gate 		/*
12167c478bd9Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1217108322fbScarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1218108322fbScarlsonj 		 * can be seen via the alternate zone's root.
12197c478bd9Sstevel@tonic-gate 		 */
1220108322fbScarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1221108322fbScarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1222108322fbScarlsonj 		    sizeof (specpath)) {
1223108322fbScarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1224108322fbScarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1225108322fbScarlsonj 			return (-1);
1226108322fbScarlsonj 		}
1227108322fbScarlsonj 		if (zonecfg_in_alt_root())
1228108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
12297c478bd9Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1230108322fbScarlsonj 		    specpath, path) != 0) {
12317c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1232108322fbScarlsonj 			    specpath);
12337c478bd9Sstevel@tonic-gate 			return (-1);
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 		return (0);
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	/*
12397c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12407c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12417c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12427c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12437c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12447c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12457c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12467c478bd9Sstevel@tonic-gate 	 */
12477c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12487c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12497c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12507c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12517c478bd9Sstevel@tonic-gate 		return (-1);
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	/*
1255108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1256fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1257fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1258fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1259fa3d7ae1Sedp 	 * time we get here.
1260108322fbScarlsonj 	 */
1261108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1262108322fbScarlsonj 		struct stat64 st;
1263108322fbScarlsonj 
1264108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
126568eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1266fa3d7ae1Sedp 			/*
1267fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1268fa3d7ae1Sedp 			 * to check if that device is already mounted
1269fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1270fa3d7ae1Sedp 			 * of the device instead of a direct mount
1271fa3d7ae1Sedp 			 */
127268eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1273108322fbScarlsonj 				return (-1);
127468eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1275fa3d7ae1Sedp 			/*
1276fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1277fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1278fa3d7ae1Sedp 			 * this case in order to get at the underlying
1279fa3d7ae1Sedp 			 * read-write path.
1280fa3d7ae1Sedp 			 */
1281fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1282108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1283108322fbScarlsonj 		}
1284108322fbScarlsonj 	}
1285108322fbScarlsonj 
1286108322fbScarlsonj 	/*
12877c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12887c478bd9Sstevel@tonic-gate 	 */
12897c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
129093239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12917c478bd9Sstevel@tonic-gate 		return (-1);
129293239addSjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
129393239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
129493239addSjohnlev 		return (-1);
129593239addSjohnlev 	}
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	/*
12987c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
12997c478bd9Sstevel@tonic-gate 	 */
13007c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
13017c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
13027c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
13037c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
13047c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
13057c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
13067c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
13077c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
13087c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
13097c478bd9Sstevel@tonic-gate 		}
13107c478bd9Sstevel@tonic-gate 	}
13119acbbeafSnn35248 
13129acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13139acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
13149acbbeafSnn35248 		return (rv);
13159acbbeafSnn35248 
13169acbbeafSnn35248 	/*
13179acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13189acbbeafSnn35248 	 * we're done.
13199acbbeafSnn35248 	 */
13209acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13219acbbeafSnn35248 		return (0);
13229acbbeafSnn35248 
13239acbbeafSnn35248 	/*
13249acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13259acbbeafSnn35248 	 * need to configure it.
13269acbbeafSnn35248 	 */
13270679f794S 	return (mount_one_dev(zlogp, path, mount_cmd));
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate static void
13317c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	uint_t i;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13367c478bd9Sstevel@tonic-gate 		return;
13377c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13387c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13397c478bd9Sstevel@tonic-gate 	free(fsarray);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
1342108322fbScarlsonj /*
1343f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1344f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1345f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1346f4368d3dSvp157776  * is done this way because:
1347f4368d3dSvp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
1348f4368d3dSvp157776  * 	We loopback mount zone's own /etc and /var into the root of the
1349f4368d3dSvp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1350f4368d3dSvp157776  * 	need to delay the mount of /var till the zone's root gets populated.
1351f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1352f4368d3dSvp157776  * 	build_mounted_post_var() which gets called only after the zone
1353f4368d3dSvp157776  * 	specific filesystems are mounted.
13546cfd72c6Sgjelinek  *
13556cfd72c6Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13566cfd72c6Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13576cfd72c6Sgjelinek  * scratch zone.
1358108322fbScarlsonj  */
1359108322fbScarlsonj static boolean_t
1360f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
136116bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1362108322fbScarlsonj {
1363108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1364108322fbScarlsonj 	const char **cpp;
1365108322fbScarlsonj 	static const char *mkdirs[] = {
13663f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13673f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1368108322fbScarlsonj 	};
1369108322fbScarlsonj 	char *altstr;
1370f4368d3dSvp157776 	FILE *fp;
1371108322fbScarlsonj 	uuid_t uuid;
1372108322fbScarlsonj 
1373108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
137416bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
137516bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1376108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1377108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1378108322fbScarlsonj 
1379108322fbScarlsonj 	/*
1380108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1381108322fbScarlsonj 	 * zone_mount_early.)
1382108322fbScarlsonj 	 */
1383108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1384108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1385108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1386108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1387108322fbScarlsonj 			return (B_FALSE);
1388108322fbScarlsonj 		}
1389108322fbScarlsonj 	}
1390f4368d3dSvp157776 	/*
1391f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1392f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1393f4368d3dSvp157776 	 * read-only inside the scratch zone.
1394f4368d3dSvp157776 	 */
1395f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1396f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1397f4368d3dSvp157776 	if (altstr == NULL) {
1398f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1399f4368d3dSvp157776 		return (B_FALSE);
1400f4368d3dSvp157776 	}
1401f4368d3dSvp157776 	zonecfg_set_root("");
1402f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1403f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1404f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1405f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1406f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1407f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1408f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1409f4368d3dSvp157776 			return (B_FALSE);
1410f4368d3dSvp157776 		}
1411f4368d3dSvp157776 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1412f4368d3dSvp157776 		    tmp) != 0) {
1413f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1414f4368d3dSvp157776 			    fromdir);
1415f4368d3dSvp157776 			return (B_FALSE);
1416f4368d3dSvp157776 		}
1417f4368d3dSvp157776 	}
1418f4368d3dSvp157776 	zonecfg_set_root(altstr);
1419f4368d3dSvp157776 	free(altstr);
1420f4368d3dSvp157776 
1421f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1422f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1423f4368d3dSvp157776 		return (B_FALSE);
1424f4368d3dSvp157776 	}
1425f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1426f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1427f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1428f4368d3dSvp157776 	}
1429f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1430f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1431f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1432f4368d3dSvp157776 		return (B_FALSE);
1433f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1434f4368d3dSvp157776 	return (B_TRUE);
1435f4368d3dSvp157776 }
1436f4368d3dSvp157776 
1437f4368d3dSvp157776 
1438f4368d3dSvp157776 static boolean_t
14396cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14406cfd72c6Sgjelinek     const char *luroot)
1441f4368d3dSvp157776 {
1442f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1443f4368d3dSvp157776 	const char **cpp;
14446cfd72c6Sgjelinek 	const char **loopdirs;
14456cfd72c6Sgjelinek 	const char **tmpdirs;
1446f4368d3dSvp157776 	static const char *localdirs[] = {
1447f4368d3dSvp157776 		"/etc", "/var", NULL
1448f4368d3dSvp157776 	};
14496cfd72c6Sgjelinek 	static const char *scr_loopdirs[] = {
1450f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1451f4368d3dSvp157776 		"/usr", NULL
1452f4368d3dSvp157776 	};
14536cfd72c6Sgjelinek 	static const char *upd_loopdirs[] = {
14546cfd72c6Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14556cfd72c6Sgjelinek 		"/usr", "/var", NULL
14566cfd72c6Sgjelinek 	};
14576cfd72c6Sgjelinek 	static const char *scr_tmpdirs[] = {
1458f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1459f4368d3dSvp157776 	};
14606cfd72c6Sgjelinek 	static const char *upd_tmpdirs[] = {
14616cfd72c6Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14626cfd72c6Sgjelinek 	};
1463f4368d3dSvp157776 	struct stat st;
1464f4368d3dSvp157776 
14656cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
1466108322fbScarlsonj 		/*
14676cfd72c6Sgjelinek 		 * These are mounted read-write from the zone undergoing
14686cfd72c6Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14696cfd72c6Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
1470108322fbScarlsonj 		 */
1471108322fbScarlsonj 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14726cfd72c6Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1473108322fbScarlsonj 			    *cpp);
14746cfd72c6Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14756cfd72c6Sgjelinek 			    rootpath, *cpp);
1476108322fbScarlsonj 			if (mkdir(tmp, 0755) != 0) {
1477108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1478108322fbScarlsonj 				return (B_FALSE);
1479108322fbScarlsonj 			}
14806cfd72c6Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14816cfd72c6Sgjelinek 			    != 0) {
14826cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14836cfd72c6Sgjelinek 				    tmp, *cpp);
1484108322fbScarlsonj 				return (B_FALSE);
1485108322fbScarlsonj 			}
1486108322fbScarlsonj 		}
14876cfd72c6Sgjelinek 	}
14886cfd72c6Sgjelinek 
14896cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14906cfd72c6Sgjelinek 		loopdirs = upd_loopdirs;
14916cfd72c6Sgjelinek 	else
14926cfd72c6Sgjelinek 		loopdirs = scr_loopdirs;
1493108322fbScarlsonj 
1494108322fbScarlsonj 	/*
1495108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1496108322fbScarlsonj 	 * they contain binaries that must match system.
1497108322fbScarlsonj 	 */
1498108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1499108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1500108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1501108322fbScarlsonj 			if (errno != EEXIST) {
1502108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1503108322fbScarlsonj 				return (B_FALSE);
1504108322fbScarlsonj 			}
1505108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1506108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1507108322fbScarlsonj 				return (B_FALSE);
1508108322fbScarlsonj 			}
1509108322fbScarlsonj 			/*
1510108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1511108322fbScarlsonj 			 * things that have been converted into symlinks
1512108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1513108322fbScarlsonj 			 * fixup.
1514108322fbScarlsonj 			 */
1515108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1516108322fbScarlsonj 				continue;
1517108322fbScarlsonj 		}
1518108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1519108322fbScarlsonj 		    tmp) != 0) {
1520108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1521108322fbScarlsonj 			    *cpp);
1522108322fbScarlsonj 			return (B_FALSE);
1523108322fbScarlsonj 		}
1524108322fbScarlsonj 	}
1525108322fbScarlsonj 
15266cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15276cfd72c6Sgjelinek 		tmpdirs = upd_tmpdirs;
15286cfd72c6Sgjelinek 	else
15296cfd72c6Sgjelinek 		tmpdirs = scr_tmpdirs;
15306cfd72c6Sgjelinek 
1531108322fbScarlsonj 	/*
1532108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1533108322fbScarlsonj 	 */
1534108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1535108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15366cfd72c6Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15376cfd72c6Sgjelinek 		    errno != EEXIST) {
1538108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1539108322fbScarlsonj 			return (B_FALSE);
1540108322fbScarlsonj 		}
1541d30e996aSgjelinek 
1542d30e996aSgjelinek 		/*
1543d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1544d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1545d30e996aSgjelinek 		 * the correct mode for /tmp now.
1546d30e996aSgjelinek 		 */
1547d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1548d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1549d30e996aSgjelinek 			return (B_FALSE);
1550d30e996aSgjelinek 		}
1551d30e996aSgjelinek 
1552108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1553108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1554108322fbScarlsonj 			return (B_FALSE);
1555108322fbScarlsonj 		}
1556108322fbScarlsonj 	}
1557108322fbScarlsonj 	return (B_TRUE);
1558108322fbScarlsonj }
1559108322fbScarlsonj 
15609acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15619acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
15629acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
15639acbbeafSnn35248 	int			*pgcd_num_fs;
15649acbbeafSnn35248 } plat_gmount_cb_data_t;
15659acbbeafSnn35248 
15669acbbeafSnn35248 /*
15679acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15689acbbeafSnn35248  * through all global brand platform mounts.
15699acbbeafSnn35248  */
15709acbbeafSnn35248 int
15719acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15729acbbeafSnn35248     const char *fstype, const char *opt)
15739acbbeafSnn35248 {
15749acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
15759acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15769acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15779acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
15789acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15799acbbeafSnn35248 
15809acbbeafSnn35248 	num_fs++;
15819acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15829acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15839acbbeafSnn35248 		return (-1);
15849acbbeafSnn35248 	}
15859acbbeafSnn35248 
15869acbbeafSnn35248 	fs_ptr = tmp_ptr;
15879acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
15889acbbeafSnn35248 
15899acbbeafSnn35248 	/* update the callback struct passed in */
15909acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
15919acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
15929acbbeafSnn35248 
15939acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
15949acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15959acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
15969acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15979acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15989acbbeafSnn35248 	fsp->zone_fs_options = NULL;
1599fa3d7ae1Sedp 	if ((opt != NULL) &&
1600fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
16019acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
16029acbbeafSnn35248 		return (-1);
16039acbbeafSnn35248 	}
16049acbbeafSnn35248 
16059acbbeafSnn35248 	return (0);
16069acbbeafSnn35248 }
16079acbbeafSnn35248 
16089acbbeafSnn35248 static int
16099acbbeafSnn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
16109acbbeafSnn35248     struct zone_fstab **fs_tabp, int *num_fsp)
16119acbbeafSnn35248 {
16129acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16139acbbeafSnn35248 	int num_fs;
16149acbbeafSnn35248 
16159acbbeafSnn35248 	num_fs = *num_fsp;
16169acbbeafSnn35248 	fs_ptr = *fs_tabp;
16179acbbeafSnn35248 
16189acbbeafSnn35248 	if (zonecfg_setipdent(handle) != Z_OK) {
16199acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16209acbbeafSnn35248 		return (-1);
16219acbbeafSnn35248 	}
16229acbbeafSnn35248 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
16239acbbeafSnn35248 		num_fs++;
16249acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16259acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16269acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16279acbbeafSnn35248 			(void) zonecfg_endipdent(handle);
16289acbbeafSnn35248 			return (-1);
16299acbbeafSnn35248 		}
16309acbbeafSnn35248 
16319acbbeafSnn35248 		/* update the pointers passed in */
16329acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16339acbbeafSnn35248 		*num_fsp = num_fs;
16349acbbeafSnn35248 
16359acbbeafSnn35248 		/*
16369acbbeafSnn35248 		 * IPDs logically only have a mount point; all other properties
16379acbbeafSnn35248 		 * are implied.
16389acbbeafSnn35248 		 */
16399acbbeafSnn35248 		fs_ptr = tmp_ptr;
16409acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16419acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16429acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16439acbbeafSnn35248 		fsp->zone_fs_special[0] = '\0';
16449acbbeafSnn35248 		fsp->zone_fs_raw[0] = '\0';
16459acbbeafSnn35248 		fsp->zone_fs_type[0] = '\0';
16469acbbeafSnn35248 		fsp->zone_fs_options = NULL;
16479acbbeafSnn35248 	}
16489acbbeafSnn35248 	(void) zonecfg_endipdent(handle);
16499acbbeafSnn35248 	return (0);
16509acbbeafSnn35248 }
16519acbbeafSnn35248 
16529acbbeafSnn35248 static int
16539acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16546cfd72c6Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16559acbbeafSnn35248 {
16569acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16579acbbeafSnn35248 	int num_fs;
16589acbbeafSnn35248 
16599acbbeafSnn35248 	num_fs = *num_fsp;
16609acbbeafSnn35248 	fs_ptr = *fs_tabp;
16619acbbeafSnn35248 
16629acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16639acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16649acbbeafSnn35248 		return (-1);
16659acbbeafSnn35248 	}
16669acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16679acbbeafSnn35248 		/*
16689acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
16699acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
16709acbbeafSnn35248 		 * case.
16719acbbeafSnn35248 		 */
16726cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16736cfd72c6Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16749acbbeafSnn35248 			continue;
16759acbbeafSnn35248 
16769acbbeafSnn35248 		num_fs++;
16779acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16789acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16799acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16809acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
16819acbbeafSnn35248 			return (-1);
16829acbbeafSnn35248 		}
16839acbbeafSnn35248 		/* update the pointers passed in */
16849acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16859acbbeafSnn35248 		*num_fsp = num_fs;
16869acbbeafSnn35248 
16879acbbeafSnn35248 		fs_ptr = tmp_ptr;
16889acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16899acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16909acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16919acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16929acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
16939acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16949acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
16959acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
1696fa3d7ae1Sedp 
1697fa3d7ae1Sedp 		/*
1698fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1699fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1700fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1701fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1702fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1703fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1704fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1705fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1706fa3d7ae1Sedp 		 */
1707fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1708fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1709fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1710fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1711fa3d7ae1Sedp 		}
1712fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1713fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
17149acbbeafSnn35248 	}
17159acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
17169acbbeafSnn35248 	return (0);
17179acbbeafSnn35248 }
17189acbbeafSnn35248 
17197c478bd9Sstevel@tonic-gate static int
17206cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
17217c478bd9Sstevel@tonic-gate {
17227c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
17237c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
17249acbbeafSnn35248 	char brand[MAXNAMELEN];
172516bca938Svp157776 	char luroot[MAXPATHLEN];
17269acbbeafSnn35248 	int i, num_fs = 0;
17279acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
17287c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
17297c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1730123807fbSedp 	brand_handle_t bh;
17319acbbeafSnn35248 	plat_gmount_cb_data_t cb;
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1734108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
17357c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1736108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1737108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1738108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
17397c478bd9Sstevel@tonic-gate 		goto bad;
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
17437c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
17447c478bd9Sstevel@tonic-gate 		goto bad;
17457c478bd9Sstevel@tonic-gate 	}
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
17487c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
17497c478bd9Sstevel@tonic-gate 		goto bad;
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1753ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17547c478bd9Sstevel@tonic-gate 		goto bad;
17557c478bd9Sstevel@tonic-gate 	}
17567c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17577c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17587c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17597c478bd9Sstevel@tonic-gate 		goto bad;
17607c478bd9Sstevel@tonic-gate 	}
17617c478bd9Sstevel@tonic-gate 
17620679f794S 	/*
1763e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
17640679f794S 	 * brand global mounts.
17650679f794S 	 */
17660679f794S 	if (ALT_MOUNT(mount_cmd)) {
1767e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
17680679f794S 	} else {
176962868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
17700679f794S 	}
17710679f794S 
17729acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
17730679f794S 	if ((bh = brand_open(brand)) == NULL) {
17749acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1775e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17769acbbeafSnn35248 		return (-1);
17779acbbeafSnn35248 	}
17789acbbeafSnn35248 
17799acbbeafSnn35248 	/*
17809acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
17819acbbeafSnn35248 	 * configuration.
17829acbbeafSnn35248 	 */
17839acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
17849acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
17859acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1786123807fbSedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17879acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
17889acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1789123807fbSedp 		brand_close(bh);
1790e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17919acbbeafSnn35248 		return (-1);
17929acbbeafSnn35248 	}
1793123807fbSedp 	brand_close(bh);
17949acbbeafSnn35248 
17957c478bd9Sstevel@tonic-gate 	/*
17967c478bd9Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
17977c478bd9Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
17987c478bd9Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
17997c478bd9Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
18007c478bd9Sstevel@tonic-gate 	 */
18019acbbeafSnn35248 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
18027c478bd9Sstevel@tonic-gate 		goto bad;
18037c478bd9Sstevel@tonic-gate 
18049acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
18059acbbeafSnn35248 	    mount_cmd) != 0)
18067c478bd9Sstevel@tonic-gate 		goto bad;
1807fa9e4066Sahrens 
18087c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
18097c478bd9Sstevel@tonic-gate 	handle = NULL;
18107c478bd9Sstevel@tonic-gate 
1811108322fbScarlsonj 	/*
18129acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
18139acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
18149acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
18159acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
18169acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
18179acbbeafSnn35248 	 * the zones filesystems there instead.
18189acbbeafSnn35248 	 *
18199acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
18209acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
18219acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
18229acbbeafSnn35248 	 *
18239acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
18249acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
18259acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
18269acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
18279acbbeafSnn35248 	 * upgrade environment.)
18289acbbeafSnn35248 	 *
18299acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
18309acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
18319acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
18329acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
18339acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
18349acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
18359acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
18369acbbeafSnn35248 	 *
18379acbbeafSnn35248 	 * All this work is done in three phases:
1838f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1839f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1840f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1841f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1842108322fbScarlsonj 	 */
18436cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
184416bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1845108322fbScarlsonj 		goto bad;
1846108322fbScarlsonj 
18477c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
18489acbbeafSnn35248 
18497c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
18506cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
18519acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
18529acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
18539acbbeafSnn35248 
18549acbbeafSnn35248 			/*
18559acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
18569acbbeafSnn35248 			 * but /dev is special and always goes at the top
18579acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
18589acbbeafSnn35248 			 */
18599acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18609acbbeafSnn35248 			rootpath[slen] = '\0';
18610679f794S 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18620679f794S 			    != 0)
18639acbbeafSnn35248 				goto bad;
18649acbbeafSnn35248 			rootpath[slen] = '/';
18659acbbeafSnn35248 			continue;
18669acbbeafSnn35248 		}
18670679f794S 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18687c478bd9Sstevel@tonic-gate 			goto bad;
18697c478bd9Sstevel@tonic-gate 	}
18706cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18716cfd72c6Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1872f4368d3dSvp157776 		goto bad;
187345916cd2Sjpk 
187445916cd2Sjpk 	/*
187545916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
187645916cd2Sjpk 	 */
18776cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18786cfd72c6Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
187945916cd2Sjpk 		goto bad;
188045916cd2Sjpk 
18817c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	/*
18847c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18857c478bd9Sstevel@tonic-gate 	 */
18867c478bd9Sstevel@tonic-gate 	return (0);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate bad:
18897c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18907c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18917c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18927c478bd9Sstevel@tonic-gate 	return (-1);
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18967c478bd9Sstevel@tonic-gate static int
18977c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18987c478bd9Sstevel@tonic-gate {
18997c478bd9Sstevel@tonic-gate 	int prefixlen;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
19027c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
19037c478bd9Sstevel@tonic-gate 		return (1);
19047c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
19057c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
19067c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
19077c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
19087c478bd9Sstevel@tonic-gate 			continue;
19097c478bd9Sstevel@tonic-gate 		}
19107c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
19117c478bd9Sstevel@tonic-gate 		prefixlen--;
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 	return (0);
19147c478bd9Sstevel@tonic-gate }
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate /*
19177c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
19187c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
19197c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
19207c478bd9Sstevel@tonic-gate  *
19217c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
19227c478bd9Sstevel@tonic-gate  */
19237c478bd9Sstevel@tonic-gate static int
1924f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
19257c478bd9Sstevel@tonic-gate {
19267c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
19277c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
19287c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
19297c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
19307c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
19317c478bd9Sstevel@tonic-gate 	uint_t bufsize;
19327c478bd9Sstevel@tonic-gate 	char *buf = NULL;
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
19357c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
19367c478bd9Sstevel@tonic-gate 		ret_code = -1;
19377c478bd9Sstevel@tonic-gate 		goto bad;
19387c478bd9Sstevel@tonic-gate 	}
19397c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
19407c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
19417c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
19427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1943f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
19447c478bd9Sstevel@tonic-gate 		ret_code = -1;
19457c478bd9Sstevel@tonic-gate 		goto bad;
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
19487c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
19497c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
19507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
19517c478bd9Sstevel@tonic-gate 		ret_code = -1;
19527c478bd9Sstevel@tonic-gate 		goto bad;
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
19557c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19567c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19577c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19587c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1959f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1960f4b3ec61Sdh155122 		    "interfaces");
19617c478bd9Sstevel@tonic-gate 		ret_code = -1;
19627c478bd9Sstevel@tonic-gate 		goto bad;
19637c478bd9Sstevel@tonic-gate 	}
19647c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19657c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19667c478bd9Sstevel@tonic-gate 		(void) close(s);
19677c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19687c478bd9Sstevel@tonic-gate 		    0) {
19697c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19707c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
19717c478bd9Sstevel@tonic-gate 			ret_code = -1;
19727c478bd9Sstevel@tonic-gate 			continue;
19737c478bd9Sstevel@tonic-gate 		}
19747c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19757c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19767c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19777c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1978c1c0ebd5Ssl108498 			if (errno == ENXIO)
1979c1c0ebd5Ssl108498 				/*
1980c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1981c1c0ebd5Ssl108498 				 * another zone halting.
1982c1c0ebd5Ssl108498 				 */
1983c1c0ebd5Ssl108498 				continue;
19847c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1985c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1986f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19877c478bd9Sstevel@tonic-gate 			ret_code = -1;
19887c478bd9Sstevel@tonic-gate 			continue;
19897c478bd9Sstevel@tonic-gate 		}
19907c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19917c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19927c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1993f4b3ec61Sdh155122 				    "%s: could not remove network interface",
19947c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
19957c478bd9Sstevel@tonic-gate 				ret_code = -1;
19967c478bd9Sstevel@tonic-gate 				continue;
19977c478bd9Sstevel@tonic-gate 			}
19987c478bd9Sstevel@tonic-gate 		}
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate bad:
20017c478bd9Sstevel@tonic-gate 	if (s > 0)
20027c478bd9Sstevel@tonic-gate 		(void) close(s);
20037c478bd9Sstevel@tonic-gate 	if (buf)
20047c478bd9Sstevel@tonic-gate 		free(buf);
20057c478bd9Sstevel@tonic-gate 	return (ret_code);
20067c478bd9Sstevel@tonic-gate }
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate static union	sockunion {
20097c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
20107c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
20117c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
20127c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
20137c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate static struct {
20167c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
20177c478bd9Sstevel@tonic-gate 	char	space[512];
20187c478bd9Sstevel@tonic-gate } rtmsg;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate static int
20217c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
20227c478bd9Sstevel@tonic-gate {
20237c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
20247c478bd9Sstevel@tonic-gate 	case AF_INET:
20257c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
20267c478bd9Sstevel@tonic-gate 	case AF_LINK:
20277c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
20287c478bd9Sstevel@tonic-gate 	case AF_INET6:
20297c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
20307c478bd9Sstevel@tonic-gate 	default:
20317c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
20327c478bd9Sstevel@tonic-gate 	}
20337c478bd9Sstevel@tonic-gate }
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
20367c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate /*
20397c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
20407c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
20417c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
20427c478bd9Sstevel@tonic-gate  *
20437c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
20447c478bd9Sstevel@tonic-gate  *
20457c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
20467c478bd9Sstevel@tonic-gate  *
20477c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
20487c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
20497c478bd9Sstevel@tonic-gate  */
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate static char *
20527c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20537c478bd9Sstevel@tonic-gate {
20547c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
20557c478bd9Sstevel@tonic-gate 	pid_t pid;
20567c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
20577c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
20587c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20597c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
20607c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	pid = getpid();
20657c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20677c478bd9Sstevel@tonic-gate 		return (NULL);
20687c478bd9Sstevel@tonic-gate 	}
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20717c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20747c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20757c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20767c478bd9Sstevel@tonic-gate 	} else {
20777c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20807c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20817c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20827c478bd9Sstevel@tonic-gate 	}
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20877c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20887c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20897c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20907c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20917c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20947c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20957c478bd9Sstevel@tonic-gate 	cp += l;
20967c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20977c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20987c478bd9Sstevel@tonic-gate 	cp += l;
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
21037c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
21047c478bd9Sstevel@tonic-gate 		return (NULL);
21057c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
21067c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
21077c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
21087c478bd9Sstevel@tonic-gate 		return (NULL);
21097c478bd9Sstevel@tonic-gate 	}
21107c478bd9Sstevel@tonic-gate 	do {
21117c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
21127c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
21137c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
21147c478bd9Sstevel@tonic-gate 	if (l < 0) {
21157c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
21167c478bd9Sstevel@tonic-gate 		return (NULL);
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
21207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
21217c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
21227c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
21237c478bd9Sstevel@tonic-gate 		return (NULL);
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
21267c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
21277c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
21287c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
21297c478bd9Sstevel@tonic-gate 		return (NULL);
21307c478bd9Sstevel@tonic-gate 	}
21317c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
21327c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
21337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
21347c478bd9Sstevel@tonic-gate 		return (NULL);
21357c478bd9Sstevel@tonic-gate 	}
21367c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2137f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
21387c478bd9Sstevel@tonic-gate 		return (NULL);
21397c478bd9Sstevel@tonic-gate 	}
21407c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
21417c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
21427c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
21437c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
21447c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
21457c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
21467c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
21477c478bd9Sstevel@tonic-gate 			continue;
21487c478bd9Sstevel@tonic-gate 		}
21497c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
21507c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
21517c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
21527c478bd9Sstevel@tonic-gate 		break;
21537c478bd9Sstevel@tonic-gate 	}
21547c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2155f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2156f4b3ec61Sdh155122 		    "determined");
21577c478bd9Sstevel@tonic-gate 		return (NULL);
21587c478bd9Sstevel@tonic-gate 	}
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	/*
21617c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21627c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21637c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21647c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21657c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21667c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21677c478bd9Sstevel@tonic-gate 	 */
21687c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21697c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21707c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21717c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21727c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21737c478bd9Sstevel@tonic-gate 	if (i < 0) {
21747c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2175f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2176f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
21777c478bd9Sstevel@tonic-gate 		return (NULL);
21787c478bd9Sstevel@tonic-gate 	}
21797c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21807c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21817c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21847c478bd9Sstevel@tonic-gate 		return (answer);
21857c478bd9Sstevel@tonic-gate 	return (NULL);
21867c478bd9Sstevel@tonic-gate }
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate /*
21897c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21907c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21917c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21927c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21937c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21947c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21957c478bd9Sstevel@tonic-gate  *
21967c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21977c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
21987c478bd9Sstevel@tonic-gate  */
21997c478bd9Sstevel@tonic-gate static int
22007c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2201f14f3ae7Sjv227347     struct zone_nwiftab *nwiftabptr)
22027c478bd9Sstevel@tonic-gate {
22037c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
22047c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
22057c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
22062e481403SVamsi Nagineni 	struct sockaddr_storage laddr;
22077c478bd9Sstevel@tonic-gate 	struct in_addr in4;
22087c478bd9Sstevel@tonic-gate 	sa_family_t af;
22097c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
22107c478bd9Sstevel@tonic-gate 	int s;
22117c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
2212f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	boolean_t is_loopback = B_FALSE;
22137c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
22147c478bd9Sstevel@tonic-gate 	int res;
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
22177c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
22187c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
22197c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
22207c478bd9Sstevel@tonic-gate 		return (-1);
22217c478bd9Sstevel@tonic-gate 	}
22227c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
22237c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22247c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
22257c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
22267c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
22277c478bd9Sstevel@tonic-gate 		return (-1);
22287c478bd9Sstevel@tonic-gate 	}
22297c478bd9Sstevel@tonic-gate 
22302e481403SVamsi Nagineni 	/*
22312e481403SVamsi Nagineni 	 * This is a similar kind of "hack" like in addif() to get around
22322e481403SVamsi Nagineni 	 * the problem of SIOCLIFADDIF.  The problem is that this ioctl
22332e481403SVamsi Nagineni 	 * does not include the netmask when adding a logical interface.
22342e481403SVamsi Nagineni 	 * To get around this problem, we first add the logical interface
22352e481403SVamsi Nagineni 	 * with a 0 address.  After that, we set the netmask if provided.
22362e481403SVamsi Nagineni 	 * Finally we set the interface address.
22372e481403SVamsi Nagineni 	 */
22382e481403SVamsi Nagineni 	laddr = lifr.lifr_addr;
22397c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
22407c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
22412e481403SVamsi Nagineni 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
22422e481403SVamsi Nagineni 
22437c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
224422321485Svp157776 		/*
224522321485Svp157776 		 * Here, we know that the interface can't be brought up.
224622321485Svp157776 		 * A similar warning message was already printed out to
224722321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
224822321485Svp157776 		 * message to syslog and continue.
224922321485Svp157776 		 */
2250f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
225122321485Svp157776 		    "'%s' which may not be present/plumbed in the "
225222321485Svp157776 		    "global zone.", lifr.lifr_name);
22537c478bd9Sstevel@tonic-gate 		(void) close(s);
225422321485Svp157776 		return (Z_OK);
22557c478bd9Sstevel@tonic-gate 	}
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22587c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22597c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22627c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2263f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2264f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
22657c478bd9Sstevel@tonic-gate 		goto bad;
22667c478bd9Sstevel@tonic-gate 	}
22677c478bd9Sstevel@tonic-gate 
2268f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	/*
2269f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * Loopback interface will use the default netmask assigned, if no
2270f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * netmask is found.
2271f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 */
22727c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2273f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		is_loopback = B_TRUE;
2274f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	}
22757c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
22767c478bd9Sstevel@tonic-gate 		/*
22777c478bd9Sstevel@tonic-gate 		 * The IPv4 netmask can be determined either
22787c478bd9Sstevel@tonic-gate 		 * directly if a prefix length was supplied with
22797c478bd9Sstevel@tonic-gate 		 * the address or via the netmasks database.  Not
22807c478bd9Sstevel@tonic-gate 		 * being able to determine it is a common failure,
22817c478bd9Sstevel@tonic-gate 		 * but it often is not fatal to operation of the
22827c478bd9Sstevel@tonic-gate 		 * interface.  In that case, a warning will be
22837c478bd9Sstevel@tonic-gate 		 * printed after the rest of the interface's
22847c478bd9Sstevel@tonic-gate 		 * parameters have been configured.
22857c478bd9Sstevel@tonic-gate 		 */
22867c478bd9Sstevel@tonic-gate 		(void) memset(&netmask4, 0, sizeof (netmask4));
22877c478bd9Sstevel@tonic-gate 		if (slashp != NULL) {
22887c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22897c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask4.sin_addr) != 0) {
22907c478bd9Sstevel@tonic-gate 				*slashp = '/';
22917c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22927c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22937c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
22947c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22957c478bd9Sstevel@tonic-gate 				goto bad;
22967c478bd9Sstevel@tonic-gate 			}
22977c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22987c478bd9Sstevel@tonic-gate 		} else if (getnetmaskbyaddr(in4,
22997c478bd9Sstevel@tonic-gate 		    &netmask4.sin_addr) == 0) {
23007c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
23017c478bd9Sstevel@tonic-gate 		}
23027c478bd9Sstevel@tonic-gate 		if (got_netmask) {
23037c478bd9Sstevel@tonic-gate 			netmask4.sin_family = af;
23047c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask4,
23057c478bd9Sstevel@tonic-gate 			    sizeof (netmask4));
23067c478bd9Sstevel@tonic-gate 		}
23077c478bd9Sstevel@tonic-gate 	} else {
23087c478bd9Sstevel@tonic-gate 		(void) memset(&netmask6, 0, sizeof (netmask6));
23097c478bd9Sstevel@tonic-gate 		if (addr2netmask(slashp + 1, V6_ADDR_LEN,
23107c478bd9Sstevel@tonic-gate 		    (uchar_t *)&netmask6.sin6_addr) != 0) {
23117c478bd9Sstevel@tonic-gate 			*slashp = '/';
23127c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
23137c478bd9Sstevel@tonic-gate 			    "%s: invalid prefix length in %s",
23147c478bd9Sstevel@tonic-gate 			    lifr.lifr_name,
23157c478bd9Sstevel@tonic-gate 			    nwiftabptr->zone_nwif_address);
23167c478bd9Sstevel@tonic-gate 			goto bad;
23177c478bd9Sstevel@tonic-gate 		}
23187c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;
23197c478bd9Sstevel@tonic-gate 		netmask6.sin6_family = af;
23207c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &netmask6,
23217c478bd9Sstevel@tonic-gate 		    sizeof (netmask6));
23227c478bd9Sstevel@tonic-gate 	}
23237c478bd9Sstevel@tonic-gate 	if (got_netmask &&
23247c478bd9Sstevel@tonic-gate 	    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
23257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not set netmask",
23267c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
23277c478bd9Sstevel@tonic-gate 		goto bad;
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 
23302e481403SVamsi Nagineni 	/* Set the interface address */
23312e481403SVamsi Nagineni 	lifr.lifr_addr = laddr;
23327c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
23337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
23342e481403SVamsi Nagineni 		    "%s: could not set IP address to %s",
23352e481403SVamsi Nagineni 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
23367c478bd9Sstevel@tonic-gate 		goto bad;
23377c478bd9Sstevel@tonic-gate 	}
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23407c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
23417c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
23427c478bd9Sstevel@tonic-gate 		goto bad;
23437c478bd9Sstevel@tonic-gate 	}
23447c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
23457c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
23467c478bd9Sstevel@tonic-gate 		int save_errno = errno;
23477c478bd9Sstevel@tonic-gate 		char *zone_using;
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 		/*
23507c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
23517c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
23527c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23537c478bd9Sstevel@tonic-gate 		 * using that address.
23547c478bd9Sstevel@tonic-gate 		 */
23557c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23567c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2357f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2358f4b3ec61Sdh155122 			    lifr.lifr_name);
23597c478bd9Sstevel@tonic-gate 			goto bad;
23607c478bd9Sstevel@tonic-gate 		}
23617c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23627c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23637c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23647c478bd9Sstevel@tonic-gate 			goto bad;
23657c478bd9Sstevel@tonic-gate 		}
23667c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23677c478bd9Sstevel@tonic-gate 		errno = save_errno;
23687c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
23697c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2370f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2371f4b3ec61Sdh155122 			    lifr.lifr_name);
23727c478bd9Sstevel@tonic-gate 		else
2373f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2374f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2375f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
23767c478bd9Sstevel@tonic-gate 		goto bad;
23777c478bd9Sstevel@tonic-gate 	}
23787c478bd9Sstevel@tonic-gate 
2379f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (!got_netmask && !is_loopback) {
23807c478bd9Sstevel@tonic-gate 		/*
23817c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23827c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23837c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23847c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23857c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23867c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23877c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23887c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23897c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23907c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
23917c478bd9Sstevel@tonic-gate 		 */
23927c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23937c478bd9Sstevel@tonic-gate 		void  *addr;
2394e11c3f44Smeem 		const char *nomatch = "no matching subnet found in netmasks(4)";
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
23977c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23987c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23997c478bd9Sstevel@tonic-gate 		else
24007c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
24017c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
24027c478bd9Sstevel@tonic-gate 
2403e11c3f44Smeem 		/*
2404e11c3f44Smeem 		 * Find out what netmask the interface is going to be using.
2405e11c3f44Smeem 		 * If we just brought up an IPMP data address on an underlying
2406e11c3f44Smeem 		 * interface above, the address will have already migrated, so
2407e11c3f44Smeem 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
2408e11c3f44Smeem 		 * to bring the address up to get the actual netmask).  Just
2409e11c3f44Smeem 		 * omit printing the actual netmask in this corner-case.
2410e11c3f44Smeem 		 */
24117c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2412e11c3f44Smeem 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2413e11c3f44Smeem 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2414e11c3f44Smeem 			    nomatch);
2415e11c3f44Smeem 		} else {
24167c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
2417e11c3f44Smeem 			    "WARNING: %s: %s: %s; using default of %s.",
2418e11c3f44Smeem 			    lifr.lifr_name, nomatch, addrstr4, buffer);
2419e11c3f44Smeem 		}
24207c478bd9Sstevel@tonic-gate 	}
24217c478bd9Sstevel@tonic-gate 
2422de860bd9Sgfaden 	/*
2423de860bd9Sgfaden 	 * If a default router was specified for this interface
2424de860bd9Sgfaden 	 * set the route now. Ignore if already set.
2425de860bd9Sgfaden 	 */
2426de860bd9Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2427de860bd9Sgfaden 		int status;
2428de860bd9Sgfaden 		char *argv[7];
2429de860bd9Sgfaden 
2430de860bd9Sgfaden 		argv[0] = "route";
2431de860bd9Sgfaden 		argv[1] = "add";
2432de860bd9Sgfaden 		argv[2] = "-ifp";
2433de860bd9Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
2434de860bd9Sgfaden 		argv[4] = "default";
2435de860bd9Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2436de860bd9Sgfaden 		argv[6] = NULL;
2437de860bd9Sgfaden 
2438de860bd9Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2439de860bd9Sgfaden 		if (status != 0 && status != EEXIST)
2440de860bd9Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
2441de860bd9Sgfaden 			    "interface %s to %s\n",
2442de860bd9Sgfaden 			    nwiftabptr->zone_nwif_physical,
2443de860bd9Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
2444de860bd9Sgfaden 	}
2445de860bd9Sgfaden 
24467c478bd9Sstevel@tonic-gate 	(void) close(s);
24477c478bd9Sstevel@tonic-gate 	return (Z_OK);
24487c478bd9Sstevel@tonic-gate bad:
24497c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
24507c478bd9Sstevel@tonic-gate 	(void) close(s);
24517c478bd9Sstevel@tonic-gate 	return (-1);
24527c478bd9Sstevel@tonic-gate }
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate /*
24557c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
2456f14f3ae7Sjv227347  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2457f14f3ae7Sjv227347  * system.
24587c478bd9Sstevel@tonic-gate  *
24597c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24607c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
24617c478bd9Sstevel@tonic-gate  */
24627c478bd9Sstevel@tonic-gate static int
2463f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24647c478bd9Sstevel@tonic-gate {
24657c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24667c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24677c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24717c478bd9Sstevel@tonic-gate 		return (-1);
24727c478bd9Sstevel@tonic-gate 	}
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24757c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24767c478bd9Sstevel@tonic-gate 		return (-1);
24777c478bd9Sstevel@tonic-gate 	}
24787c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24797c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24807c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24817c478bd9Sstevel@tonic-gate 		return (-1);
24827c478bd9Sstevel@tonic-gate 	}
24837c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24847c478bd9Sstevel@tonic-gate 		for (;;) {
24857c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24867c478bd9Sstevel@tonic-gate 				break;
2487f14f3ae7Sjv227347 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24887c478bd9Sstevel@tonic-gate 			    Z_OK) {
24897c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24907c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24917c478bd9Sstevel@tonic-gate 				return (-1);
24927c478bd9Sstevel@tonic-gate 			}
24937c478bd9Sstevel@tonic-gate 		}
24947c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24957c478bd9Sstevel@tonic-gate 	}
24967c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
2497fdb7141fSgfaden 	if (is_system_labeled()) {
2498fdb7141fSgfaden 		/*
2499fdb7141fSgfaden 		 * Labeled zones share the loopback interface
2500fdb7141fSgfaden 		 * so it is not plumbed for shared stack instances.
2501fdb7141fSgfaden 		 */
2502fdb7141fSgfaden 		return (0);
2503fdb7141fSgfaden 	}
25047c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
25057c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
25067c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
25077c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2508442d3e9eSgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2509f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
25107c478bd9Sstevel@tonic-gate 		return (-1);
2511f14f3ae7Sjv227347 
2512f14f3ae7Sjv227347 	/* Always plumb up the IPv6 loopback interface. */
25137c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
25147c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2515f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
25167c478bd9Sstevel@tonic-gate 		return (-1);
25177c478bd9Sstevel@tonic-gate 	return (0);
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate 
252089b1c373Smeem static void
252189b1c373Smeem zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
252289b1c373Smeem {
252389b1c373Smeem 	char errmsg[DLADM_STRSIZE];
252489b1c373Smeem 
252589b1c373Smeem 	(void) dladm_status2str(err, errmsg);
252689b1c373Smeem 	zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
252789b1c373Smeem }
252889b1c373Smeem 
2529f4b3ec61Sdh155122 static int
25302b24ab6bSSebastien Roy add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2531f4b3ec61Sdh155122 {
253289b1c373Smeem 	dladm_status_t err;
2533*0dc2366fSVenugopal Iyer 	boolean_t cpuset, poolset;
253489b1c373Smeem 
2535f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2536f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2537f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
253889b1c373Smeem 		zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
253989b1c373Smeem 		    "'%s' which is used in the global zone", dlname);
2540f4b3ec61Sdh155122 		return (-1);
2541f4b3ec61Sdh155122 	}
2542f4b3ec61Sdh155122 
2543d62bc4baSyz147064 	/* Set zoneid of this link. */
25442b24ab6bSSebastien Roy 	err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
25452b24ab6bSSebastien Roy 	    DLADM_OPT_ACTIVE);
254689b1c373Smeem 	if (err != DLADM_STATUS_OK) {
254789b1c373Smeem 		zdlerror(zlogp, err, dlname,
254889b1c373Smeem 		    "WARNING: unable to add network interface");
2549f4b3ec61Sdh155122 		return (-1);
2550f4b3ec61Sdh155122 	}
2551*0dc2366fSVenugopal Iyer 
2552*0dc2366fSVenugopal Iyer 	/*
2553*0dc2366fSVenugopal Iyer 	 * Set the pool of this link if the zone has a pool and
2554*0dc2366fSVenugopal Iyer 	 * neither the cpus nor the pool datalink property is
2555*0dc2366fSVenugopal Iyer 	 * already set.
2556*0dc2366fSVenugopal Iyer 	 */
2557*0dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
2558*0dc2366fSVenugopal Iyer 	    "cpus", &cpuset);
2559*0dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
2560*0dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
2561*0dc2366fSVenugopal Iyer 		    "WARNING: unable to check if cpus link property is set");
2562*0dc2366fSVenugopal Iyer 	}
2563*0dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
2564*0dc2366fSVenugopal Iyer 	    "pool", &poolset);
2565*0dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
2566*0dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
2567*0dc2366fSVenugopal Iyer 		    "WARNING: unable to check if pool link property is set");
2568*0dc2366fSVenugopal Iyer 	}
2569*0dc2366fSVenugopal Iyer 
2570*0dc2366fSVenugopal Iyer 	if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2571*0dc2366fSVenugopal Iyer 		err = dladm_set_linkprop(dld_handle, linkid, "pool",
2572*0dc2366fSVenugopal Iyer 		    &pool_name, 1, DLADM_OPT_ACTIVE);
2573*0dc2366fSVenugopal Iyer 		if (err != DLADM_STATUS_OK) {
2574*0dc2366fSVenugopal Iyer 			zerror(zlogp, B_FALSE, "WARNING: unable to set "
2575*0dc2366fSVenugopal Iyer 			    "pool %s to datalink %s", pool_name, dlname);
2576*0dc2366fSVenugopal Iyer 			bzero(pool_name, MAXPATHLEN);
2577*0dc2366fSVenugopal Iyer 		}
2578*0dc2366fSVenugopal Iyer 	} else {
2579*0dc2366fSVenugopal Iyer 		bzero(pool_name, MAXPATHLEN);
2580*0dc2366fSVenugopal Iyer 	}
2581f4b3ec61Sdh155122 	return (0);
2582f4b3ec61Sdh155122 }
2583f4b3ec61Sdh155122 
2584f4b3ec61Sdh155122 /*
2585f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2586f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2587f4b3ec61Sdh155122  * whatever we set up, and return an error.
2588f4b3ec61Sdh155122  */
2589f4b3ec61Sdh155122 static int
2590f4b3ec61Sdh155122 configure_exclusive_network_interfaces(zlog_t *zlogp)
2591f4b3ec61Sdh155122 {
2592f4b3ec61Sdh155122 	zone_dochandle_t handle;
2593f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2594f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2595f4b3ec61Sdh155122 	char path[MAXPATHLEN];
25962b24ab6bSSebastien Roy 	datalink_id_t linkid;
2597f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2598f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2599f4b3ec61Sdh155122 
2600f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2601f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2602f4b3ec61Sdh155122 		return (-1);
2603f4b3ec61Sdh155122 	}
2604f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2605f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2606f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2607f4b3ec61Sdh155122 		return (-1);
2608f4b3ec61Sdh155122 	}
2609f4b3ec61Sdh155122 
2610f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2611f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2612f4b3ec61Sdh155122 		return (0);
2613f4b3ec61Sdh155122 	}
2614f4b3ec61Sdh155122 
2615f4b3ec61Sdh155122 	for (;;) {
2616f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2617f4b3ec61Sdh155122 			break;
2618f4b3ec61Sdh155122 
2619f4b3ec61Sdh155122 		if (prof == NULL) {
2620f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2621f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2622f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2623f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2624f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2625f4b3ec61Sdh155122 				    "unable to determine dev root");
2626f4b3ec61Sdh155122 				return (-1);
2627f4b3ec61Sdh155122 			}
2628f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2629f4b3ec61Sdh155122 			    "/dev");
2630f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2631f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2632f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2633f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2634f4b3ec61Sdh155122 				    "failed to initialize profile");
2635f4b3ec61Sdh155122 				return (-1);
2636f4b3ec61Sdh155122 			}
2637f4b3ec61Sdh155122 		}
2638f4b3ec61Sdh155122 
2639f4b3ec61Sdh155122 		/*
2640d62bc4baSyz147064 		 * Create the /dev entry for backward compatibility.
2641f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2642d62bc4baSyz147064 		 * Note that the zone still boots when the assigned
2643d62bc4baSyz147064 		 * interface is inaccessible, used by others, etc.
2644d62bc4baSyz147064 		 * Also, when vanity naming is used, some interface do
2645d62bc4baSyz147064 		 * do not have corresponding /dev node names (for example,
2646d62bc4baSyz147064 		 * vanity named aggregations).  The /dev entry is not
2647d62bc4baSyz147064 		 * created in that case.  The /dev/net entry is always
2648d62bc4baSyz147064 		 * accessible.
2649f4b3ec61Sdh155122 		 */
26502b24ab6bSSebastien Roy 		if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
26512b24ab6bSSebastien Roy 		    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
26522b24ab6bSSebastien Roy 		    add_datalink(zlogp, zone_name, linkid,
26532b24ab6bSSebastien Roy 		    nwiftab.zone_nwif_physical) == 0) {
26543bc21d0aSAruna Ramakrishna - Sun Microsystems 			added = B_TRUE;
26553bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
2656f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2657f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
26583bc21d0aSAruna Ramakrishna - Sun Microsystems 			zerror(zlogp, B_TRUE, "failed to add network device");
2659f4b3ec61Sdh155122 			return (-1);
2660f4b3ec61Sdh155122 		}
2661d62bc4baSyz147064 	}
2662f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2663f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2664f4b3ec61Sdh155122 
2665f4b3ec61Sdh155122 	if (prof != NULL && added) {
2666f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2667f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
2668f4b3ec61Sdh155122 			return (-1);
2669f4b3ec61Sdh155122 		}
2670f4b3ec61Sdh155122 	}
2671f4b3ec61Sdh155122 	if (prof != NULL)
2672f4b3ec61Sdh155122 		di_prof_fini(prof);
2673f4b3ec61Sdh155122 
2674f4b3ec61Sdh155122 	return (0);
2675f4b3ec61Sdh155122 }
2676f4b3ec61Sdh155122 
2677f4b3ec61Sdh155122 static int
2678*0dc2366fSVenugopal Iyer remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
2679*0dc2366fSVenugopal Iyer {
2680*0dc2366fSVenugopal Iyer 	ushort_t flags;
2681*0dc2366fSVenugopal Iyer 	zone_iptype_t iptype;
2682*0dc2366fSVenugopal Iyer 	int i, dlnum = 0;
2683*0dc2366fSVenugopal Iyer 	datalink_id_t *dllink, *dllinks = NULL;
2684*0dc2366fSVenugopal Iyer 	dladm_status_t err;
2685*0dc2366fSVenugopal Iyer 
2686*0dc2366fSVenugopal Iyer 	if (strlen(pool_name) == 0)
2687*0dc2366fSVenugopal Iyer 		return (0);
2688*0dc2366fSVenugopal Iyer 
2689*0dc2366fSVenugopal Iyer 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
2690*0dc2366fSVenugopal Iyer 	    sizeof (flags)) < 0) {
2691*0dc2366fSVenugopal Iyer 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
2692*0dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to determine "
2693*0dc2366fSVenugopal Iyer 			    "ip-type");
2694*0dc2366fSVenugopal Iyer 			return (-1);
2695*0dc2366fSVenugopal Iyer 		}
2696*0dc2366fSVenugopal Iyer 	} else {
2697*0dc2366fSVenugopal Iyer 		if (flags & ZF_NET_EXCL)
2698*0dc2366fSVenugopal Iyer 			iptype = ZS_EXCLUSIVE;
2699*0dc2366fSVenugopal Iyer 		else
2700*0dc2366fSVenugopal Iyer 			iptype = ZS_SHARED;
2701*0dc2366fSVenugopal Iyer 	}
2702*0dc2366fSVenugopal Iyer 
2703*0dc2366fSVenugopal Iyer 	if (iptype == ZS_EXCLUSIVE) {
2704*0dc2366fSVenugopal Iyer 		/*
2705*0dc2366fSVenugopal Iyer 		 * Get the datalink count and for each datalink,
2706*0dc2366fSVenugopal Iyer 		 * attempt to clear the pool property and clear
2707*0dc2366fSVenugopal Iyer 		 * the pool_name.
2708*0dc2366fSVenugopal Iyer 		 */
2709*0dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2710*0dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to count network "
2711*0dc2366fSVenugopal Iyer 			    "interfaces");
2712*0dc2366fSVenugopal Iyer 			return (-1);
2713*0dc2366fSVenugopal Iyer 		}
2714*0dc2366fSVenugopal Iyer 
2715*0dc2366fSVenugopal Iyer 		if (dlnum == 0)
2716*0dc2366fSVenugopal Iyer 			return (0);
2717*0dc2366fSVenugopal Iyer 
2718*0dc2366fSVenugopal Iyer 		if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
2719*0dc2366fSVenugopal Iyer 		    == NULL) {
2720*0dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "memory allocation failed");
2721*0dc2366fSVenugopal Iyer 			return (-1);
2722*0dc2366fSVenugopal Iyer 		}
2723*0dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
2724*0dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to list network "
2725*0dc2366fSVenugopal Iyer 			    "interfaces");
2726*0dc2366fSVenugopal Iyer 			return (-1);
2727*0dc2366fSVenugopal Iyer 		}
2728*0dc2366fSVenugopal Iyer 
2729*0dc2366fSVenugopal Iyer 		bzero(pool_name, MAXPATHLEN);
2730*0dc2366fSVenugopal Iyer 		for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
2731*0dc2366fSVenugopal Iyer 			err = dladm_set_linkprop(dld_handle, *dllink, "pool",
2732*0dc2366fSVenugopal Iyer 			    NULL, 0, DLADM_OPT_ACTIVE);
2733*0dc2366fSVenugopal Iyer 			if (err != DLADM_STATUS_OK) {
2734*0dc2366fSVenugopal Iyer 				zerror(zlogp, B_TRUE,
2735*0dc2366fSVenugopal Iyer 				    "WARNING: unable to clear pool");
2736*0dc2366fSVenugopal Iyer 			}
2737*0dc2366fSVenugopal Iyer 		}
2738*0dc2366fSVenugopal Iyer 		free(dllinks);
2739*0dc2366fSVenugopal Iyer 	}
2740*0dc2366fSVenugopal Iyer 	return (0);
2741*0dc2366fSVenugopal Iyer }
2742*0dc2366fSVenugopal Iyer 
2743*0dc2366fSVenugopal Iyer static int
27442b24ab6bSSebastien Roy unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2745f4b3ec61Sdh155122 {
27462b24ab6bSSebastien Roy 	int dlnum = 0;
2747f4b3ec61Sdh155122 
27482b24ab6bSSebastien Roy 	/*
27492b24ab6bSSebastien Roy 	 * The kernel shutdown callback for the dls module should have removed
27502b24ab6bSSebastien Roy 	 * all datalinks from this zone.  If any remain, then there's a
27512b24ab6bSSebastien Roy 	 * problem.
27522b24ab6bSSebastien Roy 	 */
2753f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2754f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2755f4b3ec61Sdh155122 		return (-1);
2756f4b3ec61Sdh155122 	}
27572b24ab6bSSebastien Roy 	if (dlnum != 0) {
27582b24ab6bSSebastien Roy 		zerror(zlogp, B_FALSE,
27592b24ab6bSSebastien Roy 		    "datalinks remain in zone after shutdown");
2760f4b3ec61Sdh155122 		return (-1);
2761f4b3ec61Sdh155122 	}
2762f4b3ec61Sdh155122 	return (0);
2763f4b3ec61Sdh155122 }
2764f4b3ec61Sdh155122 
27657c478bd9Sstevel@tonic-gate static int
27667c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
27677c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
27687c478bd9Sstevel@tonic-gate {
27697c478bd9Sstevel@tonic-gate 	int fd;
27707c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
27717c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
27727c478bd9Sstevel@tonic-gate 	int error;
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
27757c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
27767c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
27777c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
27787c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
27817c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
27827c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
27837c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
27867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
27877c478bd9Sstevel@tonic-gate 		return (-1);
27887c478bd9Sstevel@tonic-gate 	}
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
27917c478bd9Sstevel@tonic-gate 	(void) close(fd);
27927c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
27937c478bd9Sstevel@tonic-gate 		return (0);
27947c478bd9Sstevel@tonic-gate 	return (-1);
27957c478bd9Sstevel@tonic-gate }
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate static int
27987c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
27997c478bd9Sstevel@tonic-gate {
28007c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
28017c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
28027c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
28037c478bd9Sstevel@tonic-gate 	int error;
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 	/*
28067c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
28077c478bd9Sstevel@tonic-gate 	 */
28087c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
28097c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
28107c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
28117c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
28127c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
28157c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
28167c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
28177c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
28187c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
28217c478bd9Sstevel@tonic-gate 		return (error);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	/*
28247c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
28257c478bd9Sstevel@tonic-gate 	 */
28267c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
28277c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
28287c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
28297c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
28307c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
28337c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
28347c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
28357c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
28367c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
28397c478bd9Sstevel@tonic-gate 		return (error);
28407c478bd9Sstevel@tonic-gate 	return (0);
28417c478bd9Sstevel@tonic-gate }
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate static int
28446cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
2845ffbafc53Scomay {
2846ffbafc53Scomay 	int error = -1;
2847ffbafc53Scomay 	zone_dochandle_t handle;
2848ffbafc53Scomay 	char *privname = NULL;
2849ffbafc53Scomay 
2850ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
2851ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2852ffbafc53Scomay 		return (-1);
2853ffbafc53Scomay 	}
2854ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2855ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
2856ffbafc53Scomay 		zonecfg_fini_handle(handle);
2857ffbafc53Scomay 		return (-1);
2858ffbafc53Scomay 	}
2859ffbafc53Scomay 
28606cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
2861bf1d7e28Sdh155122 		zone_iptype_t	iptype;
2862bf1d7e28Sdh155122 		const char	*curr_iptype;
2863bf1d7e28Sdh155122 
2864bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
2865bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
2866bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
2867bf1d7e28Sdh155122 			return (-1);
2868bf1d7e28Sdh155122 		}
2869bf1d7e28Sdh155122 
2870bf1d7e28Sdh155122 		switch (iptype) {
2871bf1d7e28Sdh155122 		case ZS_SHARED:
2872bf1d7e28Sdh155122 			curr_iptype = "shared";
2873bf1d7e28Sdh155122 			break;
2874bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
2875bf1d7e28Sdh155122 			curr_iptype = "exclusive";
2876bf1d7e28Sdh155122 			break;
2877bf1d7e28Sdh155122 		}
2878bf1d7e28Sdh155122 
2879e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
2880e767a340Sgjelinek 			zonecfg_fini_handle(handle);
28819acbbeafSnn35248 			return (0);
2882e767a340Sgjelinek 		}
28839acbbeafSnn35248 		zerror(zlogp, B_FALSE,
28849acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
28859acbbeafSnn35248 		zonecfg_fini_handle(handle);
28869acbbeafSnn35248 		return (-1);
28879acbbeafSnn35248 	}
28889acbbeafSnn35248 
2889ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
2890ffbafc53Scomay 	case Z_OK:
2891ffbafc53Scomay 		error = 0;
2892ffbafc53Scomay 		break;
2893ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2894ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
2895ffbafc53Scomay 		    "within the zone's privilege set", privname);
2896ffbafc53Scomay 		break;
2897ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2898ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
2899ffbafc53Scomay 		    "from the zone's privilege set", privname);
2900ffbafc53Scomay 		break;
2901ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2902ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
2903ffbafc53Scomay 		    "in the zone's privilege set", privname);
2904ffbafc53Scomay 		break;
2905ffbafc53Scomay 	default:
2906ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
2907ffbafc53Scomay 		    "privilege set");
2908ffbafc53Scomay 		break;
2909ffbafc53Scomay 	}
2910ffbafc53Scomay 
2911ffbafc53Scomay 	free(privname);
2912ffbafc53Scomay 	zonecfg_fini_handle(handle);
2913ffbafc53Scomay 	return (error);
2914ffbafc53Scomay }
2915ffbafc53Scomay 
2916ffbafc53Scomay static int
29177c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
29187c478bd9Sstevel@tonic-gate {
29197c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
29207c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
29217c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
29227c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
29237c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
29247c478bd9Sstevel@tonic-gate 	int error = -1;
29257c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
29267c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
29277c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	*bufp = NULL;
29307c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
29337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
29347c478bd9Sstevel@tonic-gate 		return (-1);
29357c478bd9Sstevel@tonic-gate 	}
29367c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
29377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
29387c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
29397c478bd9Sstevel@tonic-gate 		return (-1);
29407c478bd9Sstevel@tonic-gate 	}
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
29437c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
29447c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
29457c478bd9Sstevel@tonic-gate 		goto out;
29467c478bd9Sstevel@tonic-gate 	}
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
29497c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
29507c478bd9Sstevel@tonic-gate 		goto out;
29517c478bd9Sstevel@tonic-gate 	}
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
29547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
29557c478bd9Sstevel@tonic-gate 		goto out;
29567c478bd9Sstevel@tonic-gate 	}
29577c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
29587c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
29597c478bd9Sstevel@tonic-gate 		uint_t i, count;
29607c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
29637c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
29647c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29657c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
29667c478bd9Sstevel@tonic-gate 			continue;
29677c478bd9Sstevel@tonic-gate 		}
29687c478bd9Sstevel@tonic-gate 		count = 0;
29697c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
29707c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
29717c478bd9Sstevel@tonic-gate 			count++;
29727c478bd9Sstevel@tonic-gate 		}
29737c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
29747c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
29757c478bd9Sstevel@tonic-gate 		}
29767c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
29777c478bd9Sstevel@tonic-gate 			goto out;
29787c478bd9Sstevel@tonic-gate 		i = 0;
29797c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
29807c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
29817c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
29827c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
29837c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
29847c478bd9Sstevel@tonic-gate 				goto out;
29857c478bd9Sstevel@tonic-gate 			}
29867c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
29877c478bd9Sstevel@tonic-gate 			    != Z_OK) {
29887c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
29897c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
29907c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
29917c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
29927c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
29937c478bd9Sstevel@tonic-gate 				goto out;
29947c478bd9Sstevel@tonic-gate 			}
29957c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
29967c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
29977c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
29987c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
29997c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
30007c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
30017c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
30027c478bd9Sstevel@tonic-gate 				    name);
30037c478bd9Sstevel@tonic-gate 				goto out;
30047c478bd9Sstevel@tonic-gate 			}
30057c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
30067c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
30077c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
30087c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
30097c478bd9Sstevel@tonic-gate 				goto out;
30107c478bd9Sstevel@tonic-gate 			}
30117c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
30127c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
30137c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
30147c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
30157c478bd9Sstevel@tonic-gate 				goto out;
30167c478bd9Sstevel@tonic-gate 			}
30177c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
30187c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
30197c478bd9Sstevel@tonic-gate 			    != 0) {
30207c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
30217c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
30227c478bd9Sstevel@tonic-gate 				goto out;
30237c478bd9Sstevel@tonic-gate 			}
30247c478bd9Sstevel@tonic-gate 		}
30257c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30267c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
30277c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
30287c478bd9Sstevel@tonic-gate 		    != 0) {
30297c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
30307c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
30317c478bd9Sstevel@tonic-gate 			goto out;
30327c478bd9Sstevel@tonic-gate 		}
30337c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
30347c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
30357c478bd9Sstevel@tonic-gate 		free(nvlv);
30367c478bd9Sstevel@tonic-gate 		nvlv = NULL;
30377c478bd9Sstevel@tonic-gate 		rctlcount++;
30387c478bd9Sstevel@tonic-gate 	}
30397c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
30427c478bd9Sstevel@tonic-gate 		error = 0;
30437c478bd9Sstevel@tonic-gate 		goto out;
30447c478bd9Sstevel@tonic-gate 	}
30457c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
30467c478bd9Sstevel@tonic-gate 	    != 0) {
30477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
30487c478bd9Sstevel@tonic-gate 		goto out;
30497c478bd9Sstevel@tonic-gate 	}
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 	error = 0;
30527c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
30537c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate out:
30567c478bd9Sstevel@tonic-gate 	free(rctlblk);
30577c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30587c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
30597c478bd9Sstevel@tonic-gate 		free(nvl_packed);
30607c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
30617c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
30627c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
30637c478bd9Sstevel@tonic-gate 		free(nvlv);
30647c478bd9Sstevel@tonic-gate 	if (handle != NULL)
30657c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
30667c478bd9Sstevel@tonic-gate 	return (error);
30677c478bd9Sstevel@tonic-gate }
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate static int
3070c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr)
3071c5cd6260S {
3072c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
3073c5cd6260S 
3074c5cd6260S 	if (query_hook[0] == '\0')
3075c5cd6260S 		return (0);
3076c5cd6260S 
3077c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3078c5cd6260S 	    > sizeof (cmdbuf))
3079c5cd6260S 		return (-1);
3080c5cd6260S 
3081c5cd6260S 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3082c5cd6260S 		return (-1);
3083c5cd6260S 
3084c5cd6260S 	return (0);
3085c5cd6260S }
3086c5cd6260S 
3087c5cd6260S static int
3088fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3089fa9e4066Sahrens {
3090fa9e4066Sahrens 	zone_dochandle_t handle;
3091fa9e4066Sahrens 	struct zone_dstab dstab;
3092fa9e4066Sahrens 	size_t total, offset, len;
3093fa9e4066Sahrens 	int error = -1;
3094e5a17f6aSgjelinek 	char *str = NULL;
3095c5cd6260S 	char *implicit_datasets = NULL;
3096c5cd6260S 	int implicit_len = 0;
3097fa9e4066Sahrens 
3098fa9e4066Sahrens 	*bufp = NULL;
3099fa9e4066Sahrens 	*bufsizep = 0;
3100fa9e4066Sahrens 
3101fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3102fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3103fa9e4066Sahrens 		return (-1);
3104fa9e4066Sahrens 	}
3105fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3106fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3107fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3108fa9e4066Sahrens 		return (-1);
3109fa9e4066Sahrens 	}
3110fa9e4066Sahrens 
3111c5cd6260S 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3112c5cd6260S 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3113c5cd6260S 		goto out;
3114c5cd6260S 	}
3115c5cd6260S 
3116fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3117fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3118fa9e4066Sahrens 		goto out;
3119fa9e4066Sahrens 	}
3120fa9e4066Sahrens 
3121fa9e4066Sahrens 	total = 0;
3122fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3123fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3124fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3125fa9e4066Sahrens 
3126c5cd6260S 	if (implicit_datasets != NULL)
3127c5cd6260S 		implicit_len = strlen(implicit_datasets);
3128c5cd6260S 	if (implicit_len > 0)
3129c5cd6260S 		total += implicit_len + 1;
3130c5cd6260S 
3131fa9e4066Sahrens 	if (total == 0) {
3132fa9e4066Sahrens 		error = 0;
3133fa9e4066Sahrens 		goto out;
3134fa9e4066Sahrens 	}
3135fa9e4066Sahrens 
3136fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3137fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3138fa9e4066Sahrens 		goto out;
3139fa9e4066Sahrens 	}
3140fa9e4066Sahrens 
3141fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3142fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3143fa9e4066Sahrens 		goto out;
3144fa9e4066Sahrens 	}
3145fa9e4066Sahrens 	offset = 0;
3146fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3147fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3148fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3149e5a17f6aSgjelinek 		    total - offset);
3150fa9e4066Sahrens 		offset += len;
3151e5a17f6aSgjelinek 		if (offset < total - 1)
3152fa9e4066Sahrens 			str[offset++] = ',';
3153fa9e4066Sahrens 	}
3154fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3155fa9e4066Sahrens 
3156c5cd6260S 	if (implicit_len > 0)
3157c5cd6260S 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3158c5cd6260S 
3159fa9e4066Sahrens 	error = 0;
3160fa9e4066Sahrens 	*bufp = str;
3161fa9e4066Sahrens 	*bufsizep = total;
3162fa9e4066Sahrens 
3163fa9e4066Sahrens out:
3164fa9e4066Sahrens 	if (error != 0 && str != NULL)
3165fa9e4066Sahrens 		free(str);
3166fa9e4066Sahrens 	if (handle != NULL)
3167fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3168c5cd6260S 	if (implicit_datasets != NULL)
3169c5cd6260S 		free(implicit_datasets);
3170fa9e4066Sahrens 
3171fa9e4066Sahrens 	return (error);
3172fa9e4066Sahrens }
3173fa9e4066Sahrens 
3174fa9e4066Sahrens static int
3175fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3176fa9e4066Sahrens {
3177fa9e4066Sahrens 	zone_dochandle_t handle;
3178fa9e4066Sahrens 	struct zone_dstab dstab;
3179fa9e4066Sahrens 	zfs_handle_t *zhp;
318099653d4eSeschrock 	libzfs_handle_t *hdl;
3181fa9e4066Sahrens 
3182fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3183fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3184fa9e4066Sahrens 		return (-1);
3185fa9e4066Sahrens 	}
3186fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3187fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3188fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3189fa9e4066Sahrens 		return (-1);
3190fa9e4066Sahrens 	}
3191fa9e4066Sahrens 
3192fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3193fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3194fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3195fa9e4066Sahrens 		return (-1);
3196fa9e4066Sahrens 	}
3197fa9e4066Sahrens 
319899653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
319999653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
320099653d4eSeschrock 		zonecfg_fini_handle(handle);
320199653d4eSeschrock 		return (-1);
320299653d4eSeschrock 	}
3203fa9e4066Sahrens 
3204fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3205fa9e4066Sahrens 
320699653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3207fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3208fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3209fa9e4066Sahrens 			    dstab.zone_dataset_name);
3210fa9e4066Sahrens 			zonecfg_fini_handle(handle);
321199653d4eSeschrock 			libzfs_fini(hdl);
3212fa9e4066Sahrens 			return (-1);
3213fa9e4066Sahrens 		}
3214fa9e4066Sahrens 
3215fa9e4066Sahrens 		/*
3216fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3217fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3218fa9e4066Sahrens 		 */
3219fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3220e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3221e9dbad6fSeschrock 		    "on") != 0) {
3222fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3223fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3224fa9e4066Sahrens 			    dstab.zone_dataset_name);
3225fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3226fa9e4066Sahrens 			zfs_close(zhp);
322799653d4eSeschrock 			libzfs_fini(hdl);
3228fa9e4066Sahrens 			return (-1);
3229fa9e4066Sahrens 		}
3230fa9e4066Sahrens 
3231fa9e4066Sahrens 		zfs_close(zhp);
3232fa9e4066Sahrens 	}
3233fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3234fa9e4066Sahrens 
3235fa9e4066Sahrens 	zonecfg_fini_handle(handle);
323699653d4eSeschrock 	libzfs_fini(hdl);
3237fa9e4066Sahrens 
3238fa9e4066Sahrens 	return (0);
3239fa9e4066Sahrens }
3240fa9e4066Sahrens 
324145916cd2Sjpk /*
32424201a95eSRic Aleshire  * Return true if the path is its own zfs file system.  We determine this
32434201a95eSRic Aleshire  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
32444201a95eSRic Aleshire  * if it is a different fs.
32454201a95eSRic Aleshire  */
32464201a95eSRic Aleshire boolean_t
32474201a95eSRic Aleshire is_zonepath_zfs(char *zonepath)
32484201a95eSRic Aleshire {
32494201a95eSRic Aleshire 	int res;
32504201a95eSRic Aleshire 	char *path;
32514201a95eSRic Aleshire 	char *parent;
32524201a95eSRic Aleshire 	struct statvfs64 buf1, buf2;
32534201a95eSRic Aleshire 
32544201a95eSRic Aleshire 	if (statvfs64(zonepath, &buf1) != 0)
32554201a95eSRic Aleshire 		return (B_FALSE);
32564201a95eSRic Aleshire 
32574201a95eSRic Aleshire 	if (strcmp(buf1.f_basetype, "zfs") != 0)
32584201a95eSRic Aleshire 		return (B_FALSE);
32594201a95eSRic Aleshire 
32604201a95eSRic Aleshire 	if ((path = strdup(zonepath)) == NULL)
32614201a95eSRic Aleshire 		return (B_FALSE);
32624201a95eSRic Aleshire 
32634201a95eSRic Aleshire 	parent = dirname(path);
32644201a95eSRic Aleshire 	res = statvfs64(parent, &buf2);
32654201a95eSRic Aleshire 	free(path);
32664201a95eSRic Aleshire 
32674201a95eSRic Aleshire 	if (res != 0)
32684201a95eSRic Aleshire 		return (B_FALSE);
32694201a95eSRic Aleshire 
32704201a95eSRic Aleshire 	if (buf1.f_fsid == buf2.f_fsid)
32714201a95eSRic Aleshire 		return (B_FALSE);
32724201a95eSRic Aleshire 
32734201a95eSRic Aleshire 	return (B_TRUE);
32744201a95eSRic Aleshire }
32754201a95eSRic Aleshire 
32764201a95eSRic Aleshire /*
32774201a95eSRic Aleshire  * Verify the MAC label in the root dataset for the zone.
32784201a95eSRic Aleshire  * If the label exists, it must match the label configured for the zone.
32794201a95eSRic Aleshire  * Otherwise if there's no label on the dataset, create one here.
32804201a95eSRic Aleshire  */
32814201a95eSRic Aleshire 
32824201a95eSRic Aleshire static int
32834201a95eSRic Aleshire validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
32844201a95eSRic Aleshire {
32854201a95eSRic Aleshire 	int		error = -1;
32864201a95eSRic Aleshire 	zfs_handle_t	*zhp;
32874201a95eSRic Aleshire 	libzfs_handle_t	*hdl;
32884201a95eSRic Aleshire 	m_label_t	ds_sl;
32894201a95eSRic Aleshire 	char		zonepath[MAXPATHLEN];
32904201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
32914201a95eSRic Aleshire 
32924201a95eSRic Aleshire 	if (!is_system_labeled())
32934201a95eSRic Aleshire 		return (0);
32944201a95eSRic Aleshire 
32954201a95eSRic Aleshire 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
32964201a95eSRic Aleshire 		zerror(zlogp, B_TRUE, "unable to determine zone path");
32974201a95eSRic Aleshire 		return (-1);
32984201a95eSRic Aleshire 	}
32994201a95eSRic Aleshire 
33004201a95eSRic Aleshire 	if (!is_zonepath_zfs(zonepath))
33014201a95eSRic Aleshire 		return (0);
33024201a95eSRic Aleshire 
33034201a95eSRic Aleshire 	if ((hdl = libzfs_init()) == NULL) {
33044201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "opening ZFS library");
33054201a95eSRic Aleshire 		return (-1);
33064201a95eSRic Aleshire 	}
33074201a95eSRic Aleshire 
33084201a95eSRic Aleshire 	if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
33094201a95eSRic Aleshire 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
33104201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
33114201a95eSRic Aleshire 		    rootpath);
33124201a95eSRic Aleshire 		libzfs_fini(hdl);
33134201a95eSRic Aleshire 		return (-1);
33144201a95eSRic Aleshire 	}
33154201a95eSRic Aleshire 
33164201a95eSRic Aleshire 	/* Get the mlslabel property if it exists. */
33174201a95eSRic Aleshire 	if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
33184201a95eSRic Aleshire 	    NULL, NULL, 0, B_TRUE) != 0) ||
33194201a95eSRic Aleshire 	    (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
33204201a95eSRic Aleshire 		char		*str2 = NULL;
33214201a95eSRic Aleshire 
33224201a95eSRic Aleshire 		/*
33234201a95eSRic Aleshire 		 * No label on the dataset (or default only); create one.
33244201a95eSRic Aleshire 		 * (Only do this automatic labeling for the labeled brand.)
33254201a95eSRic Aleshire 		 */
33264201a95eSRic Aleshire 		if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
33274201a95eSRic Aleshire 			error = 0;
33284201a95eSRic Aleshire 			goto out;
33294201a95eSRic Aleshire 		}
33304201a95eSRic Aleshire 
33314201a95eSRic Aleshire 		error = l_to_str_internal(zone_sl, &str2);
33324201a95eSRic Aleshire 		if (error)
33334201a95eSRic Aleshire 			goto out;
33344201a95eSRic Aleshire 		if (str2 == NULL) {
33354201a95eSRic Aleshire 			error = -1;
33364201a95eSRic Aleshire 			goto out;
33374201a95eSRic Aleshire 		}
33384201a95eSRic Aleshire 		if ((error = zfs_prop_set(zhp,
33394201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
33404201a95eSRic Aleshire 			zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
33414201a95eSRic Aleshire 			    "property for root dataset at '%s'\n", rootpath);
33424201a95eSRic Aleshire 		}
33434201a95eSRic Aleshire 		free(str2);
33444201a95eSRic Aleshire 		goto out;
33454201a95eSRic Aleshire 	}
33464201a95eSRic Aleshire 
33474201a95eSRic Aleshire 	/* Convert the retrieved dataset label to binary form. */
33484201a95eSRic Aleshire 	error = hexstr_to_label(ds_hexsl, &ds_sl);
33494201a95eSRic Aleshire 	if (error) {
33504201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
33514201a95eSRic Aleshire 		    "property on root dataset at '%s'\n", rootpath);
33524201a95eSRic Aleshire 		goto out;			/* exit with error */
33534201a95eSRic Aleshire 	}
33544201a95eSRic Aleshire 
33554201a95eSRic Aleshire 	/*
33564201a95eSRic Aleshire 	 * Perform a MAC check by comparing the zone label with the
33574201a95eSRic Aleshire 	 * dataset label.
33584201a95eSRic Aleshire 	 */
33594201a95eSRic Aleshire 	error = (!blequal(zone_sl, &ds_sl));
33604201a95eSRic Aleshire 	if (error)
33614201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
33624201a95eSRic Aleshire out:
33634201a95eSRic Aleshire 	zfs_close(zhp);
33644201a95eSRic Aleshire 	libzfs_fini(hdl);
33654201a95eSRic Aleshire 
33664201a95eSRic Aleshire 	return (error);
33674201a95eSRic Aleshire }
33684201a95eSRic Aleshire 
33694201a95eSRic Aleshire /*
337045916cd2Sjpk  * Mount lower level home directories into/from current zone
337145916cd2Sjpk  * Share exported directories specified in dfstab for zone
337245916cd2Sjpk  */
337345916cd2Sjpk static int
337445916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
337545916cd2Sjpk {
337645916cd2Sjpk 	zoneid_t *zids = NULL;
337745916cd2Sjpk 	priv_set_t *zid_privs;
337845916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
337945916cd2Sjpk 	uint_t nzents_saved;
338045916cd2Sjpk 	uint_t nzents;
338145916cd2Sjpk 	int i;
338245916cd2Sjpk 	char readonly[] = "ro";
338345916cd2Sjpk 	struct zone_fstab lower_fstab;
338445916cd2Sjpk 	char *argv[4];
338545916cd2Sjpk 
338645916cd2Sjpk 	if (!is_system_labeled())
338745916cd2Sjpk 		return (0);
338845916cd2Sjpk 
338945916cd2Sjpk 	if (zid_label == NULL) {
339045916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
339145916cd2Sjpk 		if (zid_label == NULL)
339245916cd2Sjpk 			return (-1);
339345916cd2Sjpk 	}
339445916cd2Sjpk 
339545916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
339645916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
339727e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
339845916cd2Sjpk 
339945916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
340045916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
340145916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
340245916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
340345916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
340445916cd2Sjpk 
340545916cd2Sjpk 	/*
340645916cd2Sjpk 	 * Get the list of zones from the kernel
340745916cd2Sjpk 	 */
340845916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
340945916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
341045916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
341145916cd2Sjpk 		return (-1);
341245916cd2Sjpk 	}
341345916cd2Sjpk again:
341445916cd2Sjpk 	if (nzents == 0) {
341545916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
341645916cd2Sjpk 		return (-1);
341745916cd2Sjpk 	}
341845916cd2Sjpk 
341945916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
342045916cd2Sjpk 	if (zids == NULL) {
34213f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
342245916cd2Sjpk 		return (-1);
342345916cd2Sjpk 	}
342445916cd2Sjpk 	nzents_saved = nzents;
342545916cd2Sjpk 
342645916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
342745916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
342845916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
342945916cd2Sjpk 		free(zids);
343045916cd2Sjpk 		return (-1);
343145916cd2Sjpk 	}
343245916cd2Sjpk 	if (nzents != nzents_saved) {
343345916cd2Sjpk 		/* list changed, try again */
343445916cd2Sjpk 		free(zids);
343545916cd2Sjpk 		goto again;
343645916cd2Sjpk 	}
343745916cd2Sjpk 
343845916cd2Sjpk 	ip = getprivimplinfo();
343945916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
344045916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
344145916cd2Sjpk 		zonecfg_free_fs_option_list(
344245916cd2Sjpk 		    lower_fstab.zone_fs_options);
344345916cd2Sjpk 		free(zids);
344445916cd2Sjpk 		return (-1);
344545916cd2Sjpk 	}
344645916cd2Sjpk 
344745916cd2Sjpk 	for (i = 0; i < nzents; i++) {
344845916cd2Sjpk 		char zid_name[ZONENAME_MAX];
344945916cd2Sjpk 		zone_state_t zid_state;
345045916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
345145916cd2Sjpk 		struct stat stat_buf;
345245916cd2Sjpk 
345345916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
345445916cd2Sjpk 			continue;
345545916cd2Sjpk 
345645916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
345745916cd2Sjpk 			continue;
345845916cd2Sjpk 
345945916cd2Sjpk 		/*
346045916cd2Sjpk 		 * Do special setup for the zone we are booting
346145916cd2Sjpk 		 */
346245916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
346345916cd2Sjpk 			struct zone_fstab autofs_fstab;
346445916cd2Sjpk 			char map_path[MAXPATHLEN];
346545916cd2Sjpk 			int fd;
346645916cd2Sjpk 
346745916cd2Sjpk 			/*
346845916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
34699acbbeafSnn35248 			 * in the global zone. The non-global zone entry
347045916cd2Sjpk 			 * will be created by automount when the zone
347145916cd2Sjpk 			 * is booted.
347245916cd2Sjpk 			 */
347345916cd2Sjpk 
347445916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
347545916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
347645916cd2Sjpk 
347745916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
347845916cd2Sjpk 			    "/zone/%s/home", zid_name);
347945916cd2Sjpk 
348045916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
348145916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
348245916cd2Sjpk 			/*
348345916cd2Sjpk 			 * If the map file doesn't exist create a template
348445916cd2Sjpk 			 */
348545916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
348645916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
348745916cd2Sjpk 				int len;
348845916cd2Sjpk 				char map_rec[MAXPATHLEN];
348945916cd2Sjpk 
349045916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
349145916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
349245916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
349345916cd2Sjpk 				(void) write(fd, map_rec, len);
349445916cd2Sjpk 				(void) close(fd);
349545916cd2Sjpk 			}
349645916cd2Sjpk 
349745916cd2Sjpk 			/*
349845916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
349945916cd2Sjpk 			 * If it's already of type autofs, then
350045916cd2Sjpk 			 * don't mount it again.
350145916cd2Sjpk 			 */
350245916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
350345916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
350445916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
350545916cd2Sjpk 
350645916cd2Sjpk 				(void) make_one_dir(zlogp, "",
350727e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
350827e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
350945916cd2Sjpk 
351045916cd2Sjpk 				/*
351145916cd2Sjpk 				 * Mount will fail if automounter has already
351245916cd2Sjpk 				 * processed the auto_home_<zonename> map
351345916cd2Sjpk 				 */
351445916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
351545916cd2Sjpk 				    autofs_fstab.zone_fs_special,
351645916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
351745916cd2Sjpk 			}
351845916cd2Sjpk 			continue;
351945916cd2Sjpk 		}
352045916cd2Sjpk 
352145916cd2Sjpk 
352245916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
352348451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
352448451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
352545916cd2Sjpk 			/* Skip over zones without mounted filesystems */
352645916cd2Sjpk 			continue;
352745916cd2Sjpk 
352845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
352945916cd2Sjpk 		    sizeof (m_label_t)) < 0)
353045916cd2Sjpk 			/* Skip over zones with unspecified label */
353145916cd2Sjpk 			continue;
353245916cd2Sjpk 
353345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
353445916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
353545916cd2Sjpk 			/* Skip over zones with bad path */
353645916cd2Sjpk 			continue;
353745916cd2Sjpk 
353845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
353945916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
354045916cd2Sjpk 			/* Skip over zones with bad privs */
354145916cd2Sjpk 			continue;
354245916cd2Sjpk 
354345916cd2Sjpk 		/*
354445916cd2Sjpk 		 * Reading down is valid according to our label model
354545916cd2Sjpk 		 * but some customers want to disable it because it
354645916cd2Sjpk 		 * allows execute down and other possible attacks.
354745916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
354845916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
354945916cd2Sjpk 		 * for NFS read-down semantics.
355045916cd2Sjpk 		 */
355145916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
355245916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
355345916cd2Sjpk 			/*
355445916cd2Sjpk 			 * Our zone dominates this one.
355545916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
355645916cd2Sjpk 			 */
355745916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
355845916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
355945916cd2Sjpk 
356045916cd2Sjpk 			/*
356145916cd2Sjpk 			 * If the target is already an LOFS mount
356245916cd2Sjpk 			 * then don't do it again.
356345916cd2Sjpk 			 */
356445916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
356545916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
356645916cd2Sjpk 
356745916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
356845916cd2Sjpk 				    MAXPATHLEN, "%s/export",
356945916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
357045916cd2Sjpk 					continue;
357145916cd2Sjpk 
357245916cd2Sjpk 				/*
357345916cd2Sjpk 				 * Make sure the lower-level home exists
357445916cd2Sjpk 				 */
357545916cd2Sjpk 				if (make_one_dir(zlogp,
357627e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
357727e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
357827e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
357945916cd2Sjpk 					continue;
358045916cd2Sjpk 
358145916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
358245916cd2Sjpk 				    "/home", MAXPATHLEN);
358345916cd2Sjpk 
358445916cd2Sjpk 				/*
358545916cd2Sjpk 				 * Mount can fail because the lower-level
358645916cd2Sjpk 				 * zone may have already done a mount up.
358745916cd2Sjpk 				 */
35880679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
35890679f794S 				    Z_MNT_BOOT);
359045916cd2Sjpk 			}
359145916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
359245916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
359345916cd2Sjpk 			/*
359445916cd2Sjpk 			 * This zone dominates our zone.
359545916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
359645916cd2Sjpk 			 */
359745916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
359845916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
359945916cd2Sjpk 			    zone_name) > MAXPATHLEN)
360045916cd2Sjpk 				continue;
360145916cd2Sjpk 
360245916cd2Sjpk 			/*
360345916cd2Sjpk 			 * If the target is already an LOFS mount
360445916cd2Sjpk 			 * then don't do it again.
360545916cd2Sjpk 			 */
360645916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
360745916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
360845916cd2Sjpk 
360945916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
361045916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
361145916cd2Sjpk 
361245916cd2Sjpk 				/*
361345916cd2Sjpk 				 * Mount can fail because the higher-level
361445916cd2Sjpk 				 * zone may have already done a mount down.
361545916cd2Sjpk 				 */
36160679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
36170679f794S 				    Z_MNT_BOOT);
361845916cd2Sjpk 			}
361945916cd2Sjpk 		}
362045916cd2Sjpk 	}
362145916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
362245916cd2Sjpk 	priv_freeset(zid_privs);
362345916cd2Sjpk 	free(zids);
362445916cd2Sjpk 
362545916cd2Sjpk 	/*
362645916cd2Sjpk 	 * Now share any exported directories from this zone.
362745916cd2Sjpk 	 * Each zone can have its own dfstab.
362845916cd2Sjpk 	 */
362945916cd2Sjpk 
363045916cd2Sjpk 	argv[0] = "zoneshare";
363145916cd2Sjpk 	argv[1] = "-z";
363245916cd2Sjpk 	argv[2] = zone_name;
363345916cd2Sjpk 	argv[3] = NULL;
363445916cd2Sjpk 
363545916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
363645916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
363745916cd2Sjpk 
363845916cd2Sjpk 	return (0);
363945916cd2Sjpk }
364045916cd2Sjpk 
364145916cd2Sjpk /*
364245916cd2Sjpk  * Unmount lofs mounts from higher level zones
364345916cd2Sjpk  * Unshare nfs exported directories
364445916cd2Sjpk  */
364545916cd2Sjpk static void
364645916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
364745916cd2Sjpk {
364845916cd2Sjpk 	zoneid_t *zids = NULL;
364945916cd2Sjpk 	uint_t nzents_saved;
365045916cd2Sjpk 	uint_t nzents;
365145916cd2Sjpk 	int i;
365245916cd2Sjpk 	char *argv[4];
365345916cd2Sjpk 	char path[MAXPATHLEN];
365445916cd2Sjpk 
365545916cd2Sjpk 	if (!is_system_labeled())
365645916cd2Sjpk 		return;
365745916cd2Sjpk 
365845916cd2Sjpk 	/*
365945916cd2Sjpk 	 * Get the list of zones from the kernel
366045916cd2Sjpk 	 */
366145916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
366245916cd2Sjpk 		return;
366345916cd2Sjpk 	}
366445916cd2Sjpk 
366545916cd2Sjpk 	if (zid_label == NULL) {
366645916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
366745916cd2Sjpk 		if (zid_label == NULL)
366845916cd2Sjpk 			return;
366945916cd2Sjpk 	}
367045916cd2Sjpk 
367145916cd2Sjpk again:
367245916cd2Sjpk 	if (nzents == 0)
367345916cd2Sjpk 		return;
367445916cd2Sjpk 
367545916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
367645916cd2Sjpk 	if (zids == NULL) {
36773f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
367845916cd2Sjpk 		return;
367945916cd2Sjpk 	}
368045916cd2Sjpk 	nzents_saved = nzents;
368145916cd2Sjpk 
368245916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
368345916cd2Sjpk 		free(zids);
368445916cd2Sjpk 		return;
368545916cd2Sjpk 	}
368645916cd2Sjpk 	if (nzents != nzents_saved) {
368745916cd2Sjpk 		/* list changed, try again */
368845916cd2Sjpk 		free(zids);
368945916cd2Sjpk 		goto again;
369045916cd2Sjpk 	}
369145916cd2Sjpk 
369245916cd2Sjpk 	for (i = 0; i < nzents; i++) {
369345916cd2Sjpk 		char zid_name[ZONENAME_MAX];
369445916cd2Sjpk 		zone_state_t zid_state;
369545916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
369645916cd2Sjpk 
369745916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
369845916cd2Sjpk 			continue;
369945916cd2Sjpk 
370045916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
370145916cd2Sjpk 			continue;
370245916cd2Sjpk 
370345916cd2Sjpk 		/*
370445916cd2Sjpk 		 * Skip the zone we are halting
370545916cd2Sjpk 		 */
370645916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
370745916cd2Sjpk 			continue;
370845916cd2Sjpk 
370945916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
371045916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
371145916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
371245916cd2Sjpk 			/* Skip over zones without mounted filesystems */
371345916cd2Sjpk 			continue;
371445916cd2Sjpk 
371545916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
371645916cd2Sjpk 		    sizeof (m_label_t)) < 0)
371745916cd2Sjpk 			/* Skip over zones with unspecified label */
371845916cd2Sjpk 			continue;
371945916cd2Sjpk 
372045916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
372145916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
372245916cd2Sjpk 			/* Skip over zones with bad path */
372345916cd2Sjpk 			continue;
372445916cd2Sjpk 
372545916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
372645916cd2Sjpk 			/*
372745916cd2Sjpk 			 * This zone dominates our zone.
372845916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
372945916cd2Sjpk 			 */
373045916cd2Sjpk 
373145916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
373245916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
373345916cd2Sjpk 			    zone_name) > MAXPATHLEN)
373445916cd2Sjpk 				continue;
373545916cd2Sjpk 
373645916cd2Sjpk 			/* Skip over mount failures */
373745916cd2Sjpk 			(void) umount(path);
373845916cd2Sjpk 		}
373945916cd2Sjpk 	}
374045916cd2Sjpk 	free(zids);
374145916cd2Sjpk 
374245916cd2Sjpk 	/*
374345916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
374445916cd2Sjpk 	 */
374545916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
374645916cd2Sjpk 	/* Skip over mount failures */
374745916cd2Sjpk 	(void) umount(path);
374845916cd2Sjpk 
374945916cd2Sjpk 	/*
375045916cd2Sjpk 	 * Next unshare any exported directories from this zone.
375145916cd2Sjpk 	 */
375245916cd2Sjpk 
375345916cd2Sjpk 	argv[0] = "zoneunshare";
375445916cd2Sjpk 	argv[1] = "-z";
375545916cd2Sjpk 	argv[2] = zone_name;
375645916cd2Sjpk 	argv[3] = NULL;
375745916cd2Sjpk 
375845916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
375945916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
376045916cd2Sjpk 
376145916cd2Sjpk 	/*
376245916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
376345916cd2Sjpk 	 */
376445916cd2Sjpk 
376545916cd2Sjpk 	argv[0] = "deallocate";
376645916cd2Sjpk 	argv[1] = "-Isz";
376745916cd2Sjpk 	argv[2] = zone_name;
376845916cd2Sjpk 	argv[3] = NULL;
376945916cd2Sjpk 
377045916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
377145916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
377245916cd2Sjpk }
377345916cd2Sjpk 
377445916cd2Sjpk /*
377545916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
377645916cd2Sjpk  * this zone.
377745916cd2Sjpk  */
377845916cd2Sjpk static tsol_zcent_t *
377945916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
378045916cd2Sjpk {
378145916cd2Sjpk 	FILE *fp;
378245916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
378345916cd2Sjpk 	char line[MAXTNZLEN];
378445916cd2Sjpk 
378545916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
378645916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
378745916cd2Sjpk 		return (NULL);
378845916cd2Sjpk 	}
378945916cd2Sjpk 
379045916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
379145916cd2Sjpk 		/*
379245916cd2Sjpk 		 * Check for malformed database
379345916cd2Sjpk 		 */
379445916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
379545916cd2Sjpk 			break;
379645916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
379745916cd2Sjpk 			continue;
379845916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
379945916cd2Sjpk 			break;
380045916cd2Sjpk 		tsol_freezcent(zcent);
380145916cd2Sjpk 		zcent = NULL;
380245916cd2Sjpk 	}
380345916cd2Sjpk 	(void) fclose(fp);
380445916cd2Sjpk 
380545916cd2Sjpk 	if (zcent == NULL) {
380645916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
380745916cd2Sjpk 		    "See tnzonecfg(4)");
380845916cd2Sjpk 	} else {
380945916cd2Sjpk 		if (zlabel == NULL)
381045916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
381145916cd2Sjpk 		/*
381245916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
381345916cd2Sjpk 		 */
381445916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
381545916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
381645916cd2Sjpk 			return (NULL);
381745916cd2Sjpk 		} else {
381845916cd2Sjpk 			priv_copyset(privs, zprivs);
381945916cd2Sjpk 		}
382045916cd2Sjpk 	}
382145916cd2Sjpk 	return (zcent);
382245916cd2Sjpk }
382345916cd2Sjpk 
382445916cd2Sjpk /*
382545916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
382645916cd2Sjpk  */
382745916cd2Sjpk static void
382845916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
382945916cd2Sjpk {
383045916cd2Sjpk 	tsol_mlp_t *mlp;
383145916cd2Sjpk 	tsol_mlpent_t tsme;
383245916cd2Sjpk 
383345916cd2Sjpk 	if (!is_system_labeled())
383445916cd2Sjpk 		return;
383545916cd2Sjpk 
383645916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
383745916cd2Sjpk 	tsme.tsme_flags = 0;
383845916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
383945916cd2Sjpk 		tsme.tsme_mlp = *mlp;
384045916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
384145916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
384245916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
384345916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
384445916cd2Sjpk 		}
384545916cd2Sjpk 	}
384645916cd2Sjpk 
384745916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
384845916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
384945916cd2Sjpk 		tsme.tsme_mlp = *mlp;
385045916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
385145916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
385245916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
385345916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
385445916cd2Sjpk 		}
385545916cd2Sjpk 	}
385645916cd2Sjpk }
385745916cd2Sjpk 
385845916cd2Sjpk static void
385945916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
386045916cd2Sjpk {
386145916cd2Sjpk 	tsol_mlpent_t tsme;
386245916cd2Sjpk 
386345916cd2Sjpk 	if (!is_system_labeled())
386445916cd2Sjpk 		return;
386545916cd2Sjpk 
386645916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
386745916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
386845916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
386945916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
387045916cd2Sjpk }
387145916cd2Sjpk 
38727c478bd9Sstevel@tonic-gate int
38730094b373Sjv227347 prtmount(const struct mnttab *fs, void *x) {
38740094b373Sjv227347 	zerror((zlog_t *)x, B_FALSE, "  %s", fs->mnt_mountp);
38757c478bd9Sstevel@tonic-gate 	return (0);
38767c478bd9Sstevel@tonic-gate }
38777c478bd9Sstevel@tonic-gate 
3878108322fbScarlsonj /*
3879108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
3880108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3881108322fbScarlsonj  * is found or if we can't tell.
3882108322fbScarlsonj  */
3883108322fbScarlsonj static boolean_t
3884108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
38857c478bd9Sstevel@tonic-gate {
3886108322fbScarlsonj 	zoneid_t *zids = NULL;
3887108322fbScarlsonj 	uint_t nzids = 0;
3888108322fbScarlsonj 	boolean_t retv;
3889108322fbScarlsonj 	int rlen, zlen;
3890108322fbScarlsonj 	char zroot[MAXPATHLEN];
3891108322fbScarlsonj 	char zonename[ZONENAME_MAX];
3892108322fbScarlsonj 
3893108322fbScarlsonj 	for (;;) {
3894108322fbScarlsonj 		nzids += 10;
3895108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
3896108322fbScarlsonj 		if (zids == NULL) {
38973f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3898108322fbScarlsonj 			return (B_TRUE);
3899108322fbScarlsonj 		}
3900108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
3901108322fbScarlsonj 			break;
3902108322fbScarlsonj 		free(zids);
3903108322fbScarlsonj 	}
3904108322fbScarlsonj 	retv = B_FALSE;
3905108322fbScarlsonj 	rlen = strlen(rootpath);
3906108322fbScarlsonj 	while (nzids > 0) {
3907108322fbScarlsonj 		/*
3908108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3909108322fbScarlsonj 		 * while we were busy.
3910108322fbScarlsonj 		 */
3911108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3912108322fbScarlsonj 		    sizeof (zroot)) == -1)
3913108322fbScarlsonj 			continue;
3914108322fbScarlsonj 		zlen = strlen(zroot);
3915108322fbScarlsonj 		if (zlen > rlen)
3916108322fbScarlsonj 			zlen = rlen;
3917108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3918108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3919108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3920108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3921108322fbScarlsonj 			    sizeof (zonename)) == -1)
3922108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3923108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
3924108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3925108322fbScarlsonj 			    "zone root %s already in use by zone %s",
3926108322fbScarlsonj 			    rootpath, zonename);
3927108322fbScarlsonj 			retv = B_TRUE;
3928108322fbScarlsonj 			break;
3929108322fbScarlsonj 		}
3930108322fbScarlsonj 	}
3931108322fbScarlsonj 	free(zids);
3932108322fbScarlsonj 	return (retv);
3933108322fbScarlsonj }
3934108322fbScarlsonj 
3935108322fbScarlsonj /*
3936108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
3937108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3938108322fbScarlsonj  */
3939108322fbScarlsonj static boolean_t
3940108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3941108322fbScarlsonj {
3942108322fbScarlsonj 	struct stat64 rst, zst;
3943108322fbScarlsonj 	struct mnttab *mnp;
3944108322fbScarlsonj 
3945108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
3946108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3947108322fbScarlsonj 		return (B_TRUE);
3948108322fbScarlsonj 	}
3949108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3950108322fbScarlsonj 		return (B_TRUE);
3951108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3952108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
3953108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3954108322fbScarlsonj 			continue;
3955108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3956108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
3957108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3958108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3959108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3960108322fbScarlsonj 			    "zone root %s is reachable through %s",
3961108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
3962108322fbScarlsonj 			return (B_TRUE);
3963108322fbScarlsonj 		}
3964108322fbScarlsonj 	}
3965108322fbScarlsonj 	return (B_FALSE);
3966108322fbScarlsonj }
3967108322fbScarlsonj 
39680209230bSgjelinek /*
39690209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
39700209230bSgjelinek  * configuration.
39710209230bSgjelinek  */
39720209230bSgjelinek static int
39730209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
39740209230bSgjelinek {
39750209230bSgjelinek 	int res;
39760209230bSgjelinek 	uint64_t tmp;
39770209230bSgjelinek 	struct zone_mcaptab mcap;
39780209230bSgjelinek 	char sched[MAXNAMELEN];
39790209230bSgjelinek 	zone_dochandle_t handle = NULL;
39800209230bSgjelinek 	char pool_err[128];
39810209230bSgjelinek 
39820209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
39830209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
39840209230bSgjelinek 		return (Z_BAD_HANDLE);
39850209230bSgjelinek 	}
39860209230bSgjelinek 
39870209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
39880209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
39890209230bSgjelinek 		zonecfg_fini_handle(handle);
39900209230bSgjelinek 		return (res);
39910209230bSgjelinek 	}
39920209230bSgjelinek 
39930209230bSgjelinek 	/*
39940209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
39950209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
39960209230bSgjelinek 	 */
39970209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
39980209230bSgjelinek 		uint64_t num;
39990209230bSgjelinek 		char smf_err[128];
40000209230bSgjelinek 
40010209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
40020209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
40030209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
40040209230bSgjelinek 			zonecfg_fini_handle(handle);
40050209230bSgjelinek 			return (Z_INVAL);
40060209230bSgjelinek 		}
40070209230bSgjelinek 
40080209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
40090209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
40100209230bSgjelinek 			    "failed: %s", smf_err);
40110209230bSgjelinek 			zonecfg_fini_handle(handle);
40120209230bSgjelinek 			return (Z_INVAL);
40130209230bSgjelinek 		}
40140209230bSgjelinek 	}
40150209230bSgjelinek 
40160209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
40170209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
40180209230bSgjelinek 	    strlen(sched) > 0) {
40190209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
40200209230bSgjelinek 		    strlen(sched)) == -1)
40210209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
40220209230bSgjelinek 			    "default scheduling class");
40230209230bSgjelinek 
40240209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
40250209230bSgjelinek 	    == Z_OK) {
40260209230bSgjelinek 		/*
40270209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
40280209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
40290209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
40300209230bSgjelinek 		 * in by default so we can print a warning and modify the class
40310209230bSgjelinek 		 * if we wouldn't be using FSS.
40320209230bSgjelinek 		 */
40330209230bSgjelinek 		char class_name[PC_CLNMSZ];
40340209230bSgjelinek 
40350209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
40360209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
40370209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
40380209230bSgjelinek 			    "the zone's scheduling class");
40390209230bSgjelinek 
40400209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
40410209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
40420209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
40430209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
40440209230bSgjelinek 			    "used for processes\nin the zone but to get the "
40450209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
40460209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
40470209230bSgjelinek 			    "details.");
40480209230bSgjelinek 
40490209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
40500209230bSgjelinek 			    strlen("FSS")) == -1)
40510209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
40520209230bSgjelinek 				    "zone scheduling class to FSS");
40530209230bSgjelinek 		}
40540209230bSgjelinek 	}
40550209230bSgjelinek 
40560209230bSgjelinek 	/*
40570209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
40580209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
40590209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
40600209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
40610209230bSgjelinek 	 * and just return if there is nothing to do.
40620209230bSgjelinek 	 *
40630209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
40640209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
40650209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
40660209230bSgjelinek 	 * zonecfg.
40670209230bSgjelinek 	 */
40680209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
40690209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
40700209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
40710209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
40720209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
40730209230bSgjelinek 			    pool_err);
40740209230bSgjelinek 		else
40750209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
40760209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
40770209230bSgjelinek 		zonecfg_fini_handle(handle);
40780209230bSgjelinek 		return (Z_POOL_BIND);
40790209230bSgjelinek 	}
40800209230bSgjelinek 
40810209230bSgjelinek 	/*
40820209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
40830209230bSgjelinek 	 */
40840209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
40850209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
40860209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
40870209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
40880209230bSgjelinek 		    "processor allocation within the specified range\n"
40890209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
40900209230bSgjelinek 		    "See poold(1M).");
40910209230bSgjelinek 	}
40920209230bSgjelinek 
40930209230bSgjelinek 	/* The following is a warning, not an error. */
40940209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
40950209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
40960209230bSgjelinek 		if (res == Z_POOL_BIND)
40970209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
40980209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
40990209230bSgjelinek 		else if (res == Z_POOL)
41000209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
41010209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
41020209230bSgjelinek 		else
41030209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
41040209230bSgjelinek 			    zonecfg_strerror(res));
41050209230bSgjelinek 	}
4106*0dc2366fSVenugopal Iyer 	(void) zonecfg_get_poolname(handle, zone_name, pool_name, MAXPATHLEN);
41070209230bSgjelinek 
41080209230bSgjelinek 	zonecfg_fini_handle(handle);
41090209230bSgjelinek 	return (Z_OK);
41100209230bSgjelinek }
41110209230bSgjelinek 
41125679c89fSjv227347 /*
41135679c89fSjv227347  * Sets the hostid of the new zone based on its configured value.  The zone's
41145679c89fSjv227347  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
41155679c89fSjv227347  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
41165679c89fSjv227347  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
41175679c89fSjv227347  * ID of the new zone.
41185679c89fSjv227347  *
41195679c89fSjv227347  * This function returns zero on success and a nonzero error code on failure.
41205679c89fSjv227347  */
41215679c89fSjv227347 static int
41225679c89fSjv227347 setup_zone_hostid(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
41235679c89fSjv227347 {
41245679c89fSjv227347 	int res;
41255679c89fSjv227347 	zone_dochandle_t handle;
41265679c89fSjv227347 	char hostidp[HW_HOSTID_LEN];
41275679c89fSjv227347 	unsigned int hostid;
41285679c89fSjv227347 
41295679c89fSjv227347 	if ((handle = zonecfg_init_handle()) == NULL) {
41305679c89fSjv227347 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
41315679c89fSjv227347 		return (Z_BAD_HANDLE);
41325679c89fSjv227347 	}
41335679c89fSjv227347 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
41345679c89fSjv227347 		zerror(zlogp, B_FALSE, "invalid configuration");
41355679c89fSjv227347 		zonecfg_fini_handle(handle);
41365679c89fSjv227347 		return (res);
41375679c89fSjv227347 	}
41385679c89fSjv227347 
41395679c89fSjv227347 	if ((res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp))) ==
41405679c89fSjv227347 	    Z_OK) {
41415679c89fSjv227347 		if (zonecfg_valid_hostid(hostidp) != Z_OK) {
41425679c89fSjv227347 			zerror(zlogp, B_FALSE,
41435679c89fSjv227347 			    "zone hostid is not valid: %s", hostidp);
41445679c89fSjv227347 			zonecfg_fini_handle(handle);
41455679c89fSjv227347 			return (Z_HOSTID_FUBAR);
41465679c89fSjv227347 		}
41475679c89fSjv227347 		hostid = (unsigned int)strtoul(hostidp, NULL, 16);
41485679c89fSjv227347 		if (zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
41495679c89fSjv227347 		    sizeof (hostid)) != 0) {
41505679c89fSjv227347 			zerror(zlogp, B_TRUE,
41515679c89fSjv227347 			    "zone hostid is not valid: %s", hostidp);
41525679c89fSjv227347 			zonecfg_fini_handle(handle);
41535679c89fSjv227347 			return (Z_SYSTEM);
41545679c89fSjv227347 		}
41555679c89fSjv227347 	} else if (res != Z_BAD_PROPERTY) {
41565679c89fSjv227347 		/*
41575679c89fSjv227347 		 * Z_BAD_PROPERTY is an acceptable error value (from
41585679c89fSjv227347 		 * zonecfg_get_hostid()) because it indicates that the zone
41595679c89fSjv227347 		 * doesn't have a hostid.
41605679c89fSjv227347 		 */
41615679c89fSjv227347 		if (res == Z_TOO_BIG)
41625679c89fSjv227347 			zerror(zlogp, B_FALSE, "hostid string in zone "
41635679c89fSjv227347 			    "configuration is too large.");
41645679c89fSjv227347 		else
41655679c89fSjv227347 			zerror(zlogp, B_TRUE, "fetching zone hostid from "
41665679c89fSjv227347 			    "configuration");
41675679c89fSjv227347 		zonecfg_fini_handle(handle);
41685679c89fSjv227347 		return (res);
41695679c89fSjv227347 	}
41705679c89fSjv227347 
41715679c89fSjv227347 	zonecfg_fini_handle(handle);
41725679c89fSjv227347 	return (Z_OK);
41735679c89fSjv227347 }
41745679c89fSjv227347 
4175108322fbScarlsonj zoneid_t
41766cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4177108322fbScarlsonj {
4178108322fbScarlsonj 	zoneid_t rval = -1;
41797c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
41807c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
41817c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
4182108322fbScarlsonj 	size_t rctlbufsz = 0;
4183fa9e4066Sahrens 	char *zfsbuf = NULL;
4184fa9e4066Sahrens 	size_t zfsbufsz = 0;
4185108322fbScarlsonj 	zoneid_t zoneid = -1;
41867c478bd9Sstevel@tonic-gate 	int xerr;
4187108322fbScarlsonj 	char *kzone;
4188108322fbScarlsonj 	FILE *fp = NULL;
418945916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
419045916cd2Sjpk 	int match = 0;
419145916cd2Sjpk 	int doi = 0;
4192f4b3ec61Sdh155122 	int flags;
4193f4b3ec61Sdh155122 	zone_iptype_t iptype;
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
41967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
41977c478bd9Sstevel@tonic-gate 		return (-1);
41987c478bd9Sstevel@tonic-gate 	}
4199108322fbScarlsonj 	if (zonecfg_in_alt_root())
4200108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
42017c478bd9Sstevel@tonic-gate 
42022b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
4203f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4204f4b3ec61Sdh155122 		return (-1);
4205f4b3ec61Sdh155122 	}
4206f4b3ec61Sdh155122 	switch (iptype) {
4207f4b3ec61Sdh155122 	case ZS_SHARED:
4208f4b3ec61Sdh155122 		flags = 0;
4209f4b3ec61Sdh155122 		break;
4210f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
4211f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
4212f4b3ec61Sdh155122 		break;
4213f4b3ec61Sdh155122 	}
4214f4b3ec61Sdh155122 
42157c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
42167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
42177c478bd9Sstevel@tonic-gate 		return (-1);
42187c478bd9Sstevel@tonic-gate 	}
42197c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4220ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
42217c478bd9Sstevel@tonic-gate 		goto error;
4222ffbafc53Scomay 
42236cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
42246cfd72c6Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
42257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
42267c478bd9Sstevel@tonic-gate 		goto error;
42277c478bd9Sstevel@tonic-gate 	}
4228ffbafc53Scomay 
4229fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4230fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4231fa9e4066Sahrens 		goto error;
4232fa9e4066Sahrens 	}
42337c478bd9Sstevel@tonic-gate 
42346cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
423545916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
423648451833Scarlsonj 		if (zcent != NULL) {
423745916cd2Sjpk 			match = zcent->zc_match;
423845916cd2Sjpk 			doi = zcent->zc_doi;
423945916cd2Sjpk 			*zlabel = zcent->zc_label;
424045916cd2Sjpk 		} else {
424145916cd2Sjpk 			goto error;
424245916cd2Sjpk 		}
42434201a95eSRic Aleshire 		if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
42444201a95eSRic Aleshire 			goto error;
424545916cd2Sjpk 	}
424645916cd2Sjpk 
4247108322fbScarlsonj 	kzone = zone_name;
4248108322fbScarlsonj 
4249108322fbScarlsonj 	/*
4250108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4251108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4252108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4253108322fbScarlsonj 	 * that use this same source node (same device and inode).
4254108322fbScarlsonj 	 */
4255108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4256108322fbScarlsonj 		goto error;
4257108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4258108322fbScarlsonj 		goto error;
4259108322fbScarlsonj 
42606cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4261108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4262108322fbScarlsonj 
4263108322fbScarlsonj 		/*
4264108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4265108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4266108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4267108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4268108322fbScarlsonj 		 * executables.
4269108322fbScarlsonj 		 *
4270108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4271108322fbScarlsonj 		 */
4272108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4273108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4274108322fbScarlsonj 			goto error;
4275108322fbScarlsonj 		}
4276108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4277108322fbScarlsonj 			goto error;
4278108322fbScarlsonj 	}
4279108322fbScarlsonj 
4280108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4281108322fbScarlsonj 		/*
4282108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4283108322fbScarlsonj 		 * then we have some additional work to do before starting the
4284108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4285108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4286108322fbScarlsonj 		 * the zone.
4287108322fbScarlsonj 		 */
4288108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4289108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4290108322fbScarlsonj 			goto error;
4291108322fbScarlsonj 		}
4292108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4293108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4294108322fbScarlsonj 			goto error;
4295108322fbScarlsonj 		}
4296108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4297108322fbScarlsonj 		    NULL, 0) == 0) {
4298108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4299108322fbScarlsonj 			goto error;
4300108322fbScarlsonj 		}
4301108322fbScarlsonj 		/* This is the preferred name */
4302108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4303108322fbScarlsonj 		    zone_name);
4304108322fbScarlsonj 		srandom(getpid());
4305108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4306108322fbScarlsonj 		    0) == 0) {
4307108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4308108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4309108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4310108322fbScarlsonj 		}
4311108322fbScarlsonj 		kzone = kernzone;
4312108322fbScarlsonj 	}
4313108322fbScarlsonj 
43147c478bd9Sstevel@tonic-gate 	xerr = 0;
4315108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4316f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4317f4b3ec61Sdh155122 	    flags)) == -1) {
43187c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
43197c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
43207c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
43217c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
43227c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
43237c478bd9Sstevel@tonic-gate 			} else {
43247c478bd9Sstevel@tonic-gate 
43257c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
43267c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
43277c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
43287c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
43297c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
43307c478bd9Sstevel@tonic-gate 			}
43317c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
43327c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
43337c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
43347c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
43358c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
43368c55461bSton 			char zonename[ZONENAME_MAX];
43378c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
43388c55461bSton 			    zonename, ZONENAME_MAX);
43398c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
43408c55461bSton 			    "used by the zone '%s'.", zonename);
43417c478bd9Sstevel@tonic-gate 		} else {
43427c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
43437c478bd9Sstevel@tonic-gate 		}
43447c478bd9Sstevel@tonic-gate 		goto error;
43457c478bd9Sstevel@tonic-gate 	}
4346108322fbScarlsonj 
4347108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4348108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4349108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4350108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4351108322fbScarlsonj 		goto error;
4352108322fbScarlsonj 	}
4353108322fbScarlsonj 
4354*0dc2366fSVenugopal Iyer 	if ((pool_name = malloc(MAXPATHLEN)) == NULL) {
4355*0dc2366fSVenugopal Iyer 		zerror(zlogp, B_TRUE, "memory allocation failed");
4356*0dc2366fSVenugopal Iyer 		return (Z_NOMEM);
4357*0dc2366fSVenugopal Iyer 	}
4358*0dc2366fSVenugopal Iyer 	bzero(pool_name, MAXPATHLEN);
4359*0dc2366fSVenugopal Iyer 
43600679f794S 	/*
43610679f794S 	 * The following actions are not performed when merely mounting a zone
43620679f794S 	 * for administrative use.
43630679f794S 	 */
43640679f794S 	if (mount_cmd == Z_MNT_BOOT) {
43650679f794S 		brand_handle_t bh;
43660679f794S 		struct brand_attr attr;
43670679f794S 		char modname[MAXPATHLEN];
43680679f794S 
43695679c89fSjv227347 		if (setup_zone_hostid(zlogp, zone_name, zoneid) != Z_OK)
43705679c89fSjv227347 			goto error;
43715679c89fSjv227347 
437262868012SSteve Lawrence 		if ((bh = brand_open(brand_name)) == NULL) {
43730679f794S 			zerror(zlogp, B_FALSE,
43740679f794S 			    "unable to determine brand name");
43755679c89fSjv227347 			goto error;
43769acbbeafSnn35248 		}
43779acbbeafSnn35248 
43789a5d73e0SRic Aleshire 		if (!is_system_labeled() &&
437962868012SSteve Lawrence 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
43809a5d73e0SRic Aleshire 			brand_close(bh);
43819a5d73e0SRic Aleshire 			zerror(zlogp, B_FALSE,
43829a5d73e0SRic Aleshire 			    "cannot boot labeled zone on unlabeled system");
43839a5d73e0SRic Aleshire 			goto error;
43849a5d73e0SRic Aleshire 		}
43859a5d73e0SRic Aleshire 
43869acbbeafSnn35248 		/*
43879acbbeafSnn35248 		 * If this brand requires any kernel support, now is the time to
43889acbbeafSnn35248 		 * get it loaded and initialized.
43899acbbeafSnn35248 		 */
4390123807fbSedp 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4391e767a340Sgjelinek 			brand_close(bh);
43920679f794S 			zerror(zlogp, B_FALSE,
43930679f794S 			    "unable to determine brand kernel module");
43945679c89fSjv227347 			goto error;
43959acbbeafSnn35248 		}
4396e767a340Sgjelinek 		brand_close(bh);
43979acbbeafSnn35248 
43989acbbeafSnn35248 		if (strlen(modname) > 0) {
439962868012SSteve Lawrence 			(void) strlcpy(attr.ba_brandname, brand_name,
440062868012SSteve Lawrence 			    sizeof (attr.ba_brandname));
440162868012SSteve Lawrence 			(void) strlcpy(attr.ba_modname, modname,
440262868012SSteve Lawrence 			    sizeof (attr.ba_modname));
44039acbbeafSnn35248 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
44049acbbeafSnn35248 			    sizeof (attr) != 0)) {
44050679f794S 				zerror(zlogp, B_TRUE,
44060679f794S 				    "could not set zone brand attribute.");
44079acbbeafSnn35248 				goto error;
44089acbbeafSnn35248 			}
44099acbbeafSnn35248 		}
44109acbbeafSnn35248 
44115679c89fSjv227347 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
44120209230bSgjelinek 			goto error;
44130209230bSgjelinek 
441445916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
44150209230bSgjelinek 	}
44160209230bSgjelinek 
4417108322fbScarlsonj 	rval = zoneid;
4418108322fbScarlsonj 	zoneid = -1;
4419108322fbScarlsonj 
44207c478bd9Sstevel@tonic-gate error:
44215679c89fSjv227347 	if (zoneid != -1) {
44225679c89fSjv227347 		(void) zone_shutdown(zoneid);
4423108322fbScarlsonj 		(void) zone_destroy(zoneid);
44245679c89fSjv227347 	}
44257c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
44267c478bd9Sstevel@tonic-gate 		free(rctlbuf);
44277c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
4428108322fbScarlsonj 	if (fp != NULL)
4429108322fbScarlsonj 		zonecfg_close_scratch(fp);
4430108322fbScarlsonj 	lofs_discard_mnttab();
443145916cd2Sjpk 	if (zcent != NULL)
443245916cd2Sjpk 		tsol_freezcent(zcent);
44337c478bd9Sstevel@tonic-gate 	return (rval);
44347c478bd9Sstevel@tonic-gate }
44357c478bd9Sstevel@tonic-gate 
4436555afedfScarlsonj /*
4437555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
4438555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4439555afedfScarlsonj  * details from inside the zone.
4440555afedfScarlsonj  */
4441555afedfScarlsonj static void
4442555afedfScarlsonj write_index_file(zoneid_t zoneid)
4443555afedfScarlsonj {
4444555afedfScarlsonj 	FILE *zef;
4445555afedfScarlsonj 	FILE *zet;
4446555afedfScarlsonj 	struct zoneent *zep;
4447555afedfScarlsonj 	pid_t child;
4448555afedfScarlsonj 	int tmpl_fd;
4449555afedfScarlsonj 	ctid_t ct;
4450555afedfScarlsonj 	int fd;
4451555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4452555afedfScarlsonj 
4453555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
4454555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
4455555afedfScarlsonj 		return;
4456555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
4457555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
4458555afedfScarlsonj 			break;
4459555afedfScarlsonj 		free(zep);
4460555afedfScarlsonj 	}
4461555afedfScarlsonj 	endzoneent(zef);
4462555afedfScarlsonj 	if (zep == NULL)
4463555afedfScarlsonj 		return;
4464555afedfScarlsonj 
4465555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
4466555afedfScarlsonj 		free(zep);
4467555afedfScarlsonj 		return;
4468555afedfScarlsonj 	}
4469555afedfScarlsonj 
4470555afedfScarlsonj 	if ((child = fork()) == -1) {
4471555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4472555afedfScarlsonj 		(void) close(tmpl_fd);
4473555afedfScarlsonj 		free(zep);
4474555afedfScarlsonj 		return;
4475555afedfScarlsonj 	}
4476555afedfScarlsonj 
4477555afedfScarlsonj 	/* parent waits for child to finish */
4478555afedfScarlsonj 	if (child != 0) {
4479555afedfScarlsonj 		free(zep);
4480555afedfScarlsonj 		if (contract_latest(&ct) == -1)
4481555afedfScarlsonj 			ct = -1;
4482555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4483555afedfScarlsonj 		(void) close(tmpl_fd);
4484555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
4485555afedfScarlsonj 		(void) contract_abandon_id(ct);
4486555afedfScarlsonj 		return;
4487555afedfScarlsonj 	}
4488555afedfScarlsonj 
4489555afedfScarlsonj 	/* child enters zone and sets up index file */
4490555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
4491555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
4492555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4493555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4494555afedfScarlsonj 		    ZONE_CONFIG_GID);
4495555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4496555afedfScarlsonj 		    ZONE_INDEX_MODE);
4497555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4498555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4499555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
4500555afedfScarlsonj 				uuidstr[0] = '\0';
4501555afedfScarlsonj 			else
4502555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
4503555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4504555afedfScarlsonj 			    zone_state_str(zep->zone_state),
4505555afedfScarlsonj 			    uuidstr);
4506555afedfScarlsonj 			(void) fclose(zet);
4507555afedfScarlsonj 		}
4508555afedfScarlsonj 	}
4509555afedfScarlsonj 	_exit(0);
4510555afedfScarlsonj }
4511555afedfScarlsonj 
45127c478bd9Sstevel@tonic-gate int
45136cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
45147c478bd9Sstevel@tonic-gate {
45159acbbeafSnn35248 	char zonepath[MAXPATHLEN];
45165749802bSdp 
45176cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4518fa9e4066Sahrens 		lofs_discard_mnttab();
4519fa9e4066Sahrens 		return (-1);
4520fa9e4066Sahrens 	}
4521fa9e4066Sahrens 
45229acbbeafSnn35248 	/*
45239acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
45249acbbeafSnn35248 	 * attribute backing store for /dev
45259acbbeafSnn35248 	 */
45269acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
45279acbbeafSnn35248 		lofs_discard_mnttab();
45289acbbeafSnn35248 		return (-1);
45299acbbeafSnn35248 	}
453016bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
453127e6fb21Sdp 
453227e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
453327e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
453427e6fb21Sdp 	    0, 3) != 0) {
4535108322fbScarlsonj 		lofs_discard_mnttab();
45367c478bd9Sstevel@tonic-gate 		return (-1);
4537108322fbScarlsonj 	}
4538facf4a8dSllai1 
45399acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4540facf4a8dSllai1 		lofs_discard_mnttab();
4541facf4a8dSllai1 		return (-1);
4542facf4a8dSllai1 	}
4543facf4a8dSllai1 
45446cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
4545f4b3ec61Sdh155122 		zone_iptype_t iptype;
4546f4b3ec61Sdh155122 
45472b24ab6bSSebastien Roy 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
4548f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4549108322fbScarlsonj 			lofs_discard_mnttab();
45507c478bd9Sstevel@tonic-gate 			return (-1);
4551108322fbScarlsonj 		}
4552555afedfScarlsonj 
4553f4b3ec61Sdh155122 		switch (iptype) {
4554f4b3ec61Sdh155122 		case ZS_SHARED:
4555f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
4556f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
4557f4b3ec61Sdh155122 				lofs_discard_mnttab();
4558f4b3ec61Sdh155122 				return (-1);
4559f4b3ec61Sdh155122 			}
4560f4b3ec61Sdh155122 			break;
4561f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4562f4b3ec61Sdh155122 			if (configure_exclusive_network_interfaces(zlogp) !=
4563f4b3ec61Sdh155122 			    0) {
4564f4b3ec61Sdh155122 				lofs_discard_mnttab();
4565f4b3ec61Sdh155122 				return (-1);
4566f4b3ec61Sdh155122 			}
4567f4b3ec61Sdh155122 			break;
4568f4b3ec61Sdh155122 		}
4569f4b3ec61Sdh155122 	}
4570f4b3ec61Sdh155122 
4571555afedfScarlsonj 	write_index_file(zoneid);
4572555afedfScarlsonj 
4573108322fbScarlsonj 	lofs_discard_mnttab();
45747c478bd9Sstevel@tonic-gate 	return (0);
45757c478bd9Sstevel@tonic-gate }
45767c478bd9Sstevel@tonic-gate 
4577108322fbScarlsonj static int
4578108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
45797c478bd9Sstevel@tonic-gate {
4580108322fbScarlsonj 	char zroot[MAXPATHLEN];
4581108322fbScarlsonj 
4582108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4583108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4584108322fbScarlsonj 		return (-1);
4585108322fbScarlsonj 	}
4586108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4587108322fbScarlsonj 
4588108322fbScarlsonj 	/*
4589108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
4590108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
4591108322fbScarlsonj 	 * still be creds floating about in the system that reference the
4592108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4593108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4594108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
4595108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4596108322fbScarlsonj 	 */
4597108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
4598108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4599108322fbScarlsonj 			goto unmounted;
4600108322fbScarlsonj 		if (errno == EBUSY) {
4601108322fbScarlsonj 			int tries = 10;
4602108322fbScarlsonj 
4603108322fbScarlsonj 			while (--tries >= 0) {
4604108322fbScarlsonj 				(void) sleep(1);
4605108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
4606108322fbScarlsonj 					goto unmounted;
4607108322fbScarlsonj 				if (errno != EBUSY)
4608108322fbScarlsonj 					break;
4609108322fbScarlsonj 			}
4610108322fbScarlsonj 		}
4611108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4612108322fbScarlsonj 		return (-1);
4613108322fbScarlsonj 	}
4614108322fbScarlsonj unmounted:
4615108322fbScarlsonj 
4616108322fbScarlsonj 	/*
4617108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
4618108322fbScarlsonj 	 * entries.
4619108322fbScarlsonj 	 */
4620108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4621108322fbScarlsonj 		FILE *fp;
4622108322fbScarlsonj 		int retv;
4623108322fbScarlsonj 
4624108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4625108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4626108322fbScarlsonj 			return (-1);
4627108322fbScarlsonj 		}
4628108322fbScarlsonj 		retv = -1;
4629108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
4630108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4631108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4632108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4633108322fbScarlsonj 		else
4634108322fbScarlsonj 			retv = 0;
4635108322fbScarlsonj 		zonecfg_close_scratch(fp);
4636108322fbScarlsonj 		return (retv);
4637108322fbScarlsonj 	} else {
4638108322fbScarlsonj 		return (0);
4639108322fbScarlsonj 	}
4640108322fbScarlsonj }
4641108322fbScarlsonj 
4642108322fbScarlsonj int
46430209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4644108322fbScarlsonj {
4645108322fbScarlsonj 	char *kzone;
46467c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
46470209230bSgjelinek 	int res;
46480209230bSgjelinek 	char pool_err[128];
4649ff17c8bfSgjelinek 	char zpath[MAXPATHLEN];
46509acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
4651123807fbSedp 	brand_handle_t bh = NULL;
46522b24ab6bSSebastien Roy 	dladm_status_t status;
46532b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
4654f4b3ec61Sdh155122 	ushort_t flags;
46557c478bd9Sstevel@tonic-gate 
4656108322fbScarlsonj 	kzone = zone_name;
4657108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4658108322fbScarlsonj 		FILE *fp;
4659108322fbScarlsonj 
4660108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4661108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
4662108322fbScarlsonj 			goto error;
4663108322fbScarlsonj 		}
4664108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4665108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
4666108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4667108322fbScarlsonj 			zonecfg_close_scratch(fp);
4668108322fbScarlsonj 			goto error;
4669108322fbScarlsonj 		}
4670108322fbScarlsonj 		zonecfg_close_scratch(fp);
4671108322fbScarlsonj 		kzone = kernzone;
4672108322fbScarlsonj 	}
4673108322fbScarlsonj 
4674108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
46757c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
46767c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4677108322fbScarlsonj 		if (unmount_cmd)
4678108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
46797c478bd9Sstevel@tonic-gate 		goto error;
46807c478bd9Sstevel@tonic-gate 	}
46817c478bd9Sstevel@tonic-gate 
4682*0dc2366fSVenugopal Iyer 	if (remove_datalink_pool(zlogp, zoneid) != 0) {
4683*0dc2366fSVenugopal Iyer 		zerror(zlogp, B_FALSE, "unable clear datalink pool property");
4684*0dc2366fSVenugopal Iyer 		goto error;
4685*0dc2366fSVenugopal Iyer 	}
4686*0dc2366fSVenugopal Iyer 
46877c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
46887c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
46897c478bd9Sstevel@tonic-gate 		goto error;
46907c478bd9Sstevel@tonic-gate 	}
46917c478bd9Sstevel@tonic-gate 
4692ff17c8bfSgjelinek 	/* Get the zonepath of this zone */
4693ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
4694ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
46959acbbeafSnn35248 		goto error;
46969acbbeafSnn35248 	}
46979acbbeafSnn35248 
46989acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
469962868012SSteve Lawrence 	if ((bh = brand_open(brand_name)) == NULL) {
47009acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
47019acbbeafSnn35248 		return (-1);
47029acbbeafSnn35248 	}
47039acbbeafSnn35248 	/*
47049acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
47059acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
47069acbbeafSnn35248 	 */
47079acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4708ff17c8bfSgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
4709ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
4710123807fbSedp 		brand_close(bh);
47119acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
47129acbbeafSnn35248 		    "halt callback.");
47139acbbeafSnn35248 		goto error;
47149acbbeafSnn35248 	}
4715123807fbSedp 	brand_close(bh);
47169acbbeafSnn35248 
47179acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
4718c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
47199acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
47209acbbeafSnn35248 		goto error;
47219acbbeafSnn35248 	}
47229acbbeafSnn35248 
4723f4b3ec61Sdh155122 	if (!unmount_cmd) {
4724f4b3ec61Sdh155122 		zone_iptype_t iptype;
4725f4b3ec61Sdh155122 
4726f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4727f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
47282b24ab6bSSebastien Roy 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
4729f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
4730f4b3ec61Sdh155122 				    "ip-type");
47317c478bd9Sstevel@tonic-gate 				goto error;
47327c478bd9Sstevel@tonic-gate 			}
4733f4b3ec61Sdh155122 		} else {
4734f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
4735f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
4736f4b3ec61Sdh155122 			else
4737f4b3ec61Sdh155122 				iptype = ZS_SHARED;
4738f4b3ec61Sdh155122 		}
4739f4b3ec61Sdh155122 
4740f4b3ec61Sdh155122 		switch (iptype) {
4741f4b3ec61Sdh155122 		case ZS_SHARED:
4742f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
4743f4b3ec61Sdh155122 			    zoneid) != 0) {
4744f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4745f4b3ec61Sdh155122 				    "network interfaces in zone");
4746f4b3ec61Sdh155122 				goto error;
4747f4b3ec61Sdh155122 			}
4748f4b3ec61Sdh155122 			break;
4749f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4750f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
4751f4b3ec61Sdh155122 			    zoneid) != 0) {
4752f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4753f4b3ec61Sdh155122 				    "network interfaces in zone");
4754f4b3ec61Sdh155122 				goto error;
4755f4b3ec61Sdh155122 			}
47562b24ab6bSSebastien Roy 			status = dladm_zone_halt(dld_handle, zoneid);
47572b24ab6bSSebastien Roy 			if (status != DLADM_STATUS_OK) {
47582b24ab6bSSebastien Roy 				zerror(zlogp, B_FALSE, "unable to notify "
47592b24ab6bSSebastien Roy 				    "dlmgmtd of zone halt: %s",
47602b24ab6bSSebastien Roy 				    dladm_status2str(status, errmsg));
47612b24ab6bSSebastien Roy 			}
4762f4b3ec61Sdh155122 			break;
4763f4b3ec61Sdh155122 		}
4764f4b3ec61Sdh155122 	}
47657c478bd9Sstevel@tonic-gate 
4766108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
47677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
47687c478bd9Sstevel@tonic-gate 		goto error;
47697c478bd9Sstevel@tonic-gate 	}
47707c478bd9Sstevel@tonic-gate 
4771108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
47727c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
47737c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
47747c478bd9Sstevel@tonic-gate 		goto error;
47757c478bd9Sstevel@tonic-gate 	}
47767c478bd9Sstevel@tonic-gate 
47770209230bSgjelinek 	/*
4778cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
4779cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
4780cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
4781cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
4782cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
4783cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
4784cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
4785cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
47860209230bSgjelinek 	 */
4787cf6b13d2Sgjelinek 	if (!unmount_cmd) {
4788cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
4789cf6b13d2Sgjelinek 
4790cf6b13d2Sgjelinek 		if (rebooting) {
4791cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
4792cf6b13d2Sgjelinek 			zone_dochandle_t handle;
4793cf6b13d2Sgjelinek 
4794cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
4795cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4796cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4797cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
4798e767a340Sgjelinek 
4799e767a340Sgjelinek 			zonecfg_fini_handle(handle);
4800cf6b13d2Sgjelinek 		}
4801cf6b13d2Sgjelinek 
4802cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
48030209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
48040209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
48050209230bSgjelinek 				if (res == Z_POOL)
48060209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
48070209230bSgjelinek 			}
48080209230bSgjelinek 		}
4809cf6b13d2Sgjelinek 	}
48100209230bSgjelinek 
4811*0dc2366fSVenugopal Iyer 	free(pool_name);
4812*0dc2366fSVenugopal Iyer 
481345916cd2Sjpk 	remove_mlps(zlogp, zoneid);
481445916cd2Sjpk 
48157c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
48167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
48177c478bd9Sstevel@tonic-gate 		goto error;
48187c478bd9Sstevel@tonic-gate 	}
4819108322fbScarlsonj 
4820108322fbScarlsonj 	/*
4821108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
4822108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
4823108322fbScarlsonj 	 */
4824108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4825108322fbScarlsonj 		goto error;
4826108322fbScarlsonj 
4827108322fbScarlsonj 	lofs_discard_mnttab();
48287c478bd9Sstevel@tonic-gate 	return (0);
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate error:
4831108322fbScarlsonj 	lofs_discard_mnttab();
48327c478bd9Sstevel@tonic-gate 	return (-1);
48337c478bd9Sstevel@tonic-gate }
4834