xref: /titanic_50/usr/src/cmd/zoneadmd/vplat.c (revision 6e1ae2a33c618c4c2b14aec7d2f21743ddea5837)
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 /*
230fbb751dSJohn Levon  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
287c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
297c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
307c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
317c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
327c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
337c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
34108322fbScarlsonj  *
35108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
36108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
37108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
38108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
39108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
40108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
41108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
42108322fbScarlsonj  * without relying on any binaries in the zone itself.
43108322fbScarlsonj  *
44108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
45108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
46108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
47108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
48108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
49108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
50108322fbScarlsonj  * process.
51108322fbScarlsonj  *
52108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
53108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
54108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
55108322fbScarlsonj  * $ZONEPATH/lu as the root.
56108322fbScarlsonj  *
57108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
58108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
59108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
60108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <sys/param.h>
647c478bd9Sstevel@tonic-gate #include <sys/mount.h>
657c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
667c478bd9Sstevel@tonic-gate #include <sys/socket.h>
677c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
687c478bd9Sstevel@tonic-gate #include <sys/types.h>
697c478bd9Sstevel@tonic-gate #include <sys/stat.h>
707c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
717c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
727c478bd9Sstevel@tonic-gate #include <sys/conf.h>
735679c89fSjv227347 #include <sys/systeminfo.h>
747c478bd9Sstevel@tonic-gate 
75f4b3ec61Sdh155122 #include <libdlpi.h>
76f595a68aSyz147064 #include <libdllink.h>
77d62bc4baSyz147064 #include <libdlvlan.h>
78f4b3ec61Sdh155122 
797c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
807c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
817c478bd9Sstevel@tonic-gate #include <netinet/in.h>
827c478bd9Sstevel@tonic-gate #include <net/route.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <stdio.h>
857c478bd9Sstevel@tonic-gate #include <errno.h>
867c478bd9Sstevel@tonic-gate #include <fcntl.h>
877c478bd9Sstevel@tonic-gate #include <unistd.h>
887c478bd9Sstevel@tonic-gate #include <rctl.h>
897c478bd9Sstevel@tonic-gate #include <stdlib.h>
907c478bd9Sstevel@tonic-gate #include <string.h>
917c478bd9Sstevel@tonic-gate #include <strings.h>
927c478bd9Sstevel@tonic-gate #include <wait.h>
937c478bd9Sstevel@tonic-gate #include <limits.h>
947c478bd9Sstevel@tonic-gate #include <libgen.h>
95fa9e4066Sahrens #include <libzfs.h>
96facf4a8dSllai1 #include <libdevinfo.h>
977c478bd9Sstevel@tonic-gate #include <zone.h>
987c478bd9Sstevel@tonic-gate #include <assert.h>
99555afedfScarlsonj #include <libcontract.h>
100555afedfScarlsonj #include <libcontract_priv.h>
101555afedfScarlsonj #include <uuid/uuid.h>
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1047c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1057c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1067c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
107fa9e4066Sahrens #include <sys/fs/zfs.h>
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #include <pool.h>
1107c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1110209230bSgjelinek #include <sys/priocntl.h>
1127c478bd9Sstevel@tonic-gate 
1139acbbeafSnn35248 #include <libbrand.h>
1149acbbeafSnn35248 #include <sys/brand.h>
1157c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
11639d3e169Sevanl #include <synch.h>
11722321485Svp157776 
1187c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
11945916cd2Sjpk #include <tsol/label.h>
12045916cd2Sjpk #include <libtsnet.h>
12145916cd2Sjpk #include <sys/priv.h>
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1247c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1257c478bd9Sstevel@tonic-gate 
126*6e1ae2a3SGary Pennington #define	RESOURCE_DEFAULT_OPTS \
1277c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13045916cd2Sjpk #define	MAXTNZLEN	2048
1317c478bd9Sstevel@tonic-gate 
1326cfd72c6Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1336cfd72c6Sgjelinek 
134ff19e029SMenno Lageman /* a reasonable estimate for the number of lwps per process */
135ff19e029SMenno Lageman #define	LWPS_PER_PROCESS	10
136ff19e029SMenno Lageman 
1377c478bd9Sstevel@tonic-gate /* for routing socket */
1387c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1397c478bd9Sstevel@tonic-gate 
140108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
141108322fbScarlsonj static char kernzone[ZONENAME_MAX];
142108322fbScarlsonj 
143108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
144108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
145108322fbScarlsonj 
14645916cd2Sjpk /* for Trusted Extensions */
14745916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
14845916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
14945916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
1509d48116cSdh155122 
15145916cd2Sjpk static m_label_t *zlabel = NULL;
15245916cd2Sjpk static m_label_t *zid_label = NULL;
15345916cd2Sjpk static priv_set_t *zprivs = NULL;
15445916cd2Sjpk 
1557c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1567c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1577c478bd9Sstevel@tonic-gate 
158c5cd6260S /* from zoneadmd */
159c5cd6260S extern char query_hook[];
160c5cd6260S 
1617c478bd9Sstevel@tonic-gate /*
162108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
163108322fbScarlsonj  * data) only once every N times through the loop.
164108322fbScarlsonj  */
165108322fbScarlsonj #define	MNTTAB_HUNK	32
166108322fbScarlsonj 
167108322fbScarlsonj /*
1687c478bd9Sstevel@tonic-gate  * Private autofs system call
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static int
1737c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
181108322fbScarlsonj static void
182108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
183108322fbScarlsonj {
184108322fbScarlsonj 	uint_t i;
185108322fbScarlsonj 
186108322fbScarlsonj 	if (mnt_array == NULL)
187108322fbScarlsonj 		return;
188108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
189108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
190108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
191108322fbScarlsonj 		free(mnt_array[i].mnt_special);
192108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
193108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
194108322fbScarlsonj 	}
195108322fbScarlsonj 	free(mnt_array);
196108322fbScarlsonj }
197108322fbScarlsonj 
198108322fbScarlsonj /*
199108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
200108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
201108322fbScarlsonj  * array in "nelemp".
202108322fbScarlsonj  */
203108322fbScarlsonj static int
204108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
205108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
206108322fbScarlsonj {
207108322fbScarlsonj 	struct mnttab mnt;
208108322fbScarlsonj 	struct mnttab *mnts;
209108322fbScarlsonj 	struct mnttab *mnp;
210108322fbScarlsonj 	uint_t nmnt;
211108322fbScarlsonj 
212108322fbScarlsonj 	rewind(mnttab);
213108322fbScarlsonj 	resetmnttab(mnttab);
214108322fbScarlsonj 	nmnt = 0;
215108322fbScarlsonj 	mnts = NULL;
216108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
217108322fbScarlsonj 		struct mnttab *tmp_array;
218108322fbScarlsonj 
219108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
220108322fbScarlsonj 			continue;
221108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
222108322fbScarlsonj 			tmp_array = realloc(mnts,
223108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
224108322fbScarlsonj 			if (tmp_array == NULL) {
225108322fbScarlsonj 				free_mnttable(mnts, nmnt);
226108322fbScarlsonj 				return (-1);
227108322fbScarlsonj 			}
228108322fbScarlsonj 			mnts = tmp_array;
229108322fbScarlsonj 		}
230108322fbScarlsonj 		mnp = &mnts[nmnt++];
231108322fbScarlsonj 
232108322fbScarlsonj 		/*
233108322fbScarlsonj 		 * Zero out any fields we're not using.
234108322fbScarlsonj 		 */
235108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
236108322fbScarlsonj 
237108322fbScarlsonj 		if (mnt.mnt_special != NULL)
238108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
239108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
240108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
241108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
242108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
243108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
244108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
245108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
246108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
247108322fbScarlsonj 			free_mnttable(mnts, nmnt);
248108322fbScarlsonj 			return (-1);
249108322fbScarlsonj 		}
250108322fbScarlsonj 	}
251108322fbScarlsonj 	*mnt_arrayp = mnts;
252108322fbScarlsonj 	*nelemp = nmnt;
253108322fbScarlsonj 	return (0);
254108322fbScarlsonj }
255108322fbScarlsonj 
256108322fbScarlsonj /*
257108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
258108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
259108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
260108322fbScarlsonj  * reread of the list of mount points
261108322fbScarlsonj  */
262108322fbScarlsonj static void
263108322fbScarlsonj lofs_discard_mnttab(void)
264108322fbScarlsonj {
265108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
266108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
267108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
268108322fbScarlsonj }
269108322fbScarlsonj 
270108322fbScarlsonj static int
271108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
272108322fbScarlsonj {
273108322fbScarlsonj 	FILE *mnttab;
274108322fbScarlsonj 	uint_t nmnts;
275108322fbScarlsonj 
276108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
277108322fbScarlsonj 		return (-1);
278108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
279108322fbScarlsonj 	    &nmnts) == -1) {
280108322fbScarlsonj 		(void) fclose(mnttab);
281108322fbScarlsonj 		return (-1);
282108322fbScarlsonj 	}
283108322fbScarlsonj 	(void) fclose(mnttab);
284108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
285108322fbScarlsonj 	return (0);
286108322fbScarlsonj }
287108322fbScarlsonj 
288108322fbScarlsonj /*
289108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
290108322fbScarlsonj  * path and resolves them all down to an absolute path.
291108322fbScarlsonj  */
292910f48daSedp void
293108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
294108322fbScarlsonj {
295108322fbScarlsonj 	int len, arlen;
296108322fbScarlsonj 	const char *altroot;
297108322fbScarlsonj 	char tmppath[MAXPATHLEN];
298108322fbScarlsonj 	boolean_t outside_altroot;
299108322fbScarlsonj 
300108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
301108322fbScarlsonj 		return;
302108322fbScarlsonj 	tmppath[len] = '\0';
303108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
304108322fbScarlsonj 
305108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
306108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
307108322fbScarlsonj 		return;
308108322fbScarlsonj 
309108322fbScarlsonj 	altroot = zonecfg_get_root();
310108322fbScarlsonj 	arlen = strlen(altroot);
311108322fbScarlsonj 	outside_altroot = B_FALSE;
312108322fbScarlsonj 	for (;;) {
313108322fbScarlsonj 		struct mnttab *mnp;
314108322fbScarlsonj 
3151e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3161e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3171e9d8f9fSdminer 		    mnp--) {
318108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
319108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3201e9d8f9fSdminer 			    mnp->mnt_special == NULL)
321108322fbScarlsonj 				continue;
322108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
323108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
324108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
325108322fbScarlsonj 				break;
326108322fbScarlsonj 		}
3271e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3281e9d8f9fSdminer 			break;
3291e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3301e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
331108322fbScarlsonj 			break;
332108322fbScarlsonj 		if (outside_altroot) {
333108322fbScarlsonj 			char *cp;
334108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
335108322fbScarlsonj 
336108322fbScarlsonj 			/*
337108322fbScarlsonj 			 * If we run into a read-only mount outside of the
338108322fbScarlsonj 			 * alternate root environment, then the user doesn't
339108322fbScarlsonj 			 * want this path to be made read-write.
340108322fbScarlsonj 			 */
341108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
342108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
343108322fbScarlsonj 			    NULL &&
344108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
345108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
346108322fbScarlsonj 				break;
347108322fbScarlsonj 			}
348108322fbScarlsonj 		} else if (arlen > 0 &&
349108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
350108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
351108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
352108322fbScarlsonj 			outside_altroot = B_TRUE;
353108322fbScarlsonj 		}
354108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
355108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
356108322fbScarlsonj 		    mnp->mnt_special, path + len);
357108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
358108322fbScarlsonj 			break;
359108322fbScarlsonj 		path[len] = '\0';
360108322fbScarlsonj 	}
361108322fbScarlsonj }
362108322fbScarlsonj 
363108322fbScarlsonj /*
364108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
365108322fbScarlsonj  * referenced device is already mounted somewhere.
366108322fbScarlsonj  */
367108322fbScarlsonj static int
368108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
369108322fbScarlsonj {
370108322fbScarlsonj 	struct mnttab *mnp;
371108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
372108322fbScarlsonj 
373108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
374108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
375108322fbScarlsonj 		return (-1);
376108322fbScarlsonj 
377108322fbScarlsonj 	/*
378108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
379108322fbScarlsonj 	 * no need to worry about conflicting mounts.
380108322fbScarlsonj 	 */
381108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
382108322fbScarlsonj 	    mnp++) {
383108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
384108322fbScarlsonj 			break;
385108322fbScarlsonj 	}
386108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
387108322fbScarlsonj 		return (0);
388108322fbScarlsonj 
389108322fbScarlsonj 	/*
390108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
391108322fbScarlsonj 	 */
392108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
393108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
394108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
395108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
396108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
397108322fbScarlsonj 
398108322fbScarlsonj 	/*
399*6e1ae2a3SGary Pennington 	 * Discard all but one of the original options and set that to our
400*6e1ae2a3SGary Pennington 	 * default set of options used for resources.
401108322fbScarlsonj 	 */
402108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
403108322fbScarlsonj 	if (optptr == NULL) {
404108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
405108322fbScarlsonj 		if (optptr == NULL) {
406108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
407108322fbScarlsonj 			    fsptr->zone_fs_dir);
408108322fbScarlsonj 			return (-1);
409108322fbScarlsonj 		}
410108322fbScarlsonj 	} else {
411108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
412108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
413108322fbScarlsonj 			free(onext);
414108322fbScarlsonj 		}
415108322fbScarlsonj 	}
416*6e1ae2a3SGary Pennington 	(void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
417108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
418108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
419108322fbScarlsonj 	return (0);
420108322fbScarlsonj }
421108322fbScarlsonj 
422d314f035Sedp int
42327e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
42427e6fb21Sdp     uid_t userid, gid_t groupid)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4277c478bd9Sstevel@tonic-gate 	struct stat st;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4307c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4317c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4327c478bd9Sstevel@tonic-gate 		    subdir);
4337c478bd9Sstevel@tonic-gate 		return (-1);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4377c478bd9Sstevel@tonic-gate 		/*
4387c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4397c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4407c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4417c478bd9Sstevel@tonic-gate 		 */
4427c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
443756cf395SRic Aleshire 			if (S_ISREG(st.st_mode)) {
44445916cd2Sjpk 				/*
445756cf395SRic Aleshire 				 * Allow readonly mounts of /etc/ files; this
446756cf395SRic Aleshire 				 * is needed most by Trusted Extensions.
44745916cd2Sjpk 				 */
44845916cd2Sjpk 				if (strncmp(subdir, "/etc/",
44945916cd2Sjpk 				    strlen("/etc/")) != 0) {
45045916cd2Sjpk 					zerror(zlogp, B_FALSE,
45145916cd2Sjpk 					    "%s is not in /etc", path);
4527c478bd9Sstevel@tonic-gate 					return (-1);
4537c478bd9Sstevel@tonic-gate 				}
45445916cd2Sjpk 			} else {
45545916cd2Sjpk 				zerror(zlogp, B_FALSE,
45645916cd2Sjpk 				    "%s is not a directory", path);
45745916cd2Sjpk 				return (-1);
45845916cd2Sjpk 			}
45945916cd2Sjpk 		}
46027e6fb21Sdp 		return (0);
46127e6fb21Sdp 	}
46227e6fb21Sdp 
46327e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
4647c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4657c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4667c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4677c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4687c478bd9Sstevel@tonic-gate 		else
4697c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4707c478bd9Sstevel@tonic-gate 		return (-1);
4717c478bd9Sstevel@tonic-gate 	}
47227e6fb21Sdp 
47327e6fb21Sdp 	(void) chown(path, userid, groupid);
4747c478bd9Sstevel@tonic-gate 	return (0);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate static void
4787c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	uint_t i;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (types == NULL)
4837c478bd9Sstevel@tonic-gate 		return;
4847c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
4857c478bd9Sstevel@tonic-gate 		free(types[i]);
4867c478bd9Sstevel@tonic-gate 	free(types);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate static char **
4907c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	char **types = NULL;
4937c478bd9Sstevel@tonic-gate 	FILE *fp;
4947c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
4957c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
4967c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
4977c478bd9Sstevel@tonic-gate 	uint_t i;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5017c478bd9Sstevel@tonic-gate 		return (NULL);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5077c478bd9Sstevel@tonic-gate 		lines++;
5087c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5097c478bd9Sstevel@tonic-gate 		goto out;
5107c478bd9Sstevel@tonic-gate 	rewind(fp);
5117c478bd9Sstevel@tonic-gate 	/*
5127c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5137c478bd9Sstevel@tonic-gate 	 */
5147c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5157c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5177c478bd9Sstevel@tonic-gate 		goto out;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 	i = 0;
5207c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5217c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5227c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5237c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5247c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5257c478bd9Sstevel@tonic-gate 			types = NULL;
5267c478bd9Sstevel@tonic-gate 			goto out;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5297c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5307c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5317c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5327c478bd9Sstevel@tonic-gate 			types = NULL;
5337c478bd9Sstevel@tonic-gate 			goto out;
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		i++;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate out:
5387c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5397c478bd9Sstevel@tonic-gate 	return (types);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate static boolean_t
5437c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	uint_t i;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5487c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5497c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5507c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5517c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
556108322fbScarlsonj /*
557108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
558108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
559108322fbScarlsonj  */
5607c478bd9Sstevel@tonic-gate static void
561108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5627c478bd9Sstevel@tonic-gate {
563108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
564108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
565108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5707c478bd9Sstevel@tonic-gate  *
5717c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5727c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5737c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5747c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5757c478bd9Sstevel@tonic-gate  *
5767c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5777c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5787c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5797c478bd9Sstevel@tonic-gate  *
5807c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5817c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5827c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5857c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5867c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5877c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5887c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5897c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5907c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5917c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5927c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5937c478bd9Sstevel@tonic-gate  * graceful mounts first.
5947c478bd9Sstevel@tonic-gate  *
5957c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5967c478bd9Sstevel@tonic-gate  */
5977c478bd9Sstevel@tonic-gate static int
598108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	int error = 0;
6017c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6027c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6037c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6047c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6057c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6067c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6077c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6087c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6117c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6127c478bd9Sstevel@tonic-gate 		return (-1);
6137c478bd9Sstevel@tonic-gate 	}
614108322fbScarlsonj 	if (unmount_cmd)
615108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6187c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6197c478bd9Sstevel@tonic-gate 
62045916cd2Sjpk 	/*
62145916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
62245916cd2Sjpk 	 * of our zone's /export/home
62345916cd2Sjpk 	 */
62448451833Scarlsonj 	if (!unmount_cmd)
62545916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
62645916cd2Sjpk 
6277c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6297c478bd9Sstevel@tonic-gate 		return (-1);
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6337c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6377c478bd9Sstevel@tonic-gate 		error++;
6387c478bd9Sstevel@tonic-gate 		goto out;
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6437c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6447c478bd9Sstevel@tonic-gate 	 */
6457c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6467c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6477c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6487c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6497c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6507c478bd9Sstevel@tonic-gate 		char *path;
6517c478bd9Sstevel@tonic-gate 		uint_t i;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		mnts = NULL;
6547c478bd9Sstevel@tonic-gate 		nmnt = 0;
6557c478bd9Sstevel@tonic-gate 		/*
6567c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6577c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6587c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6597c478bd9Sstevel@tonic-gate 		 * filesystems.
6607c478bd9Sstevel@tonic-gate 		 */
6617c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6627c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6637c478bd9Sstevel@tonic-gate 			error++;
6647c478bd9Sstevel@tonic-gate 			goto out;
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6677c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6687c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6697c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6707c478bd9Sstevel@tonic-gate 			/*
6717c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6727c478bd9Sstevel@tonic-gate 			 *
6737c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6747c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6757c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
6767c478bd9Sstevel@tonic-gate 			 */
6777c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6787c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6797c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 			/*
6827c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6837c478bd9Sstevel@tonic-gate 			 */
6847c478bd9Sstevel@tonic-gate 			if (stuck) {
6857c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6867c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6877c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
6887c478bd9Sstevel@tonic-gate 				} else {
6897c478bd9Sstevel@tonic-gate 					/*
6907c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
6917c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
6927c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
6937c478bd9Sstevel@tonic-gate 					 * bail.
6947c478bd9Sstevel@tonic-gate 					 */
6957c478bd9Sstevel@tonic-gate 					error++;
6967c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6977c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
6987c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
6997c478bd9Sstevel@tonic-gate 					goto out;
7007c478bd9Sstevel@tonic-gate 				}
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 			/*
7037c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7047c478bd9Sstevel@tonic-gate 			 */
7057c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7067c478bd9Sstevel@tonic-gate 				newcount++;
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7137c478bd9Sstevel@tonic-gate 			/*
7147c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7157c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7167c478bd9Sstevel@tonic-gate 			 */
7177c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		/*
7227c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7237c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7247c478bd9Sstevel@tonic-gate 		 */
7257c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7267c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7277c478bd9Sstevel@tonic-gate 			error++;
7287c478bd9Sstevel@tonic-gate 			goto out;
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate out:
7337c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7347c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7357c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate static int
7397c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7427c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7497c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7507c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7517c478bd9Sstevel@tonic-gate  *
7527c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7537c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7547c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7557c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7567c478bd9Sstevel@tonic-gate  * remains the case.
7577c478bd9Sstevel@tonic-gate  */
7587c478bd9Sstevel@tonic-gate static int
7597c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7627c478bd9Sstevel@tonic-gate 	int child_status = 0;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7667c478bd9Sstevel@tonic-gate 	 */
7677c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7687c478bd9Sstevel@tonic-gate 	child_pid = fork();
7697c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7707c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
7717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7727c478bd9Sstevel@tonic-gate 		return (-1);
7737c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
7747c478bd9Sstevel@tonic-gate 		closefrom(0);
7751390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7761390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7771390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7781390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7797c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
7807c478bd9Sstevel@tonic-gate 		/*
7817c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7827c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7837c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7847c478bd9Sstevel@tonic-gate 		 * accordingly.
7857c478bd9Sstevel@tonic-gate 		 */
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7887c478bd9Sstevel@tonic-gate 	} else {
7897c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7947c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7957c478bd9Sstevel@tonic-gate 		return (-1);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
7987c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
7997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8007c478bd9Sstevel@tonic-gate 		return (-1);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate static int
80693239addSjohnlev isregfile(const char *path)
80793239addSjohnlev {
80893239addSjohnlev 	struct stat64 st;
80993239addSjohnlev 
81093239addSjohnlev 	if (stat64(path, &st) == -1)
81193239addSjohnlev 		return (-1);
81293239addSjohnlev 
81393239addSjohnlev 	return (S_ISREG(st.st_mode));
81493239addSjohnlev }
81593239addSjohnlev 
81693239addSjohnlev static int
8177c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
82057a250fbSbatschul 	char *argv[5];
8217c478bd9Sstevel@tonic-gate 	int status;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	/*
8247c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8257c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8267c478bd9Sstevel@tonic-gate 	 */
8277c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8289acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8307c478bd9Sstevel@tonic-gate 		return (-1);
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 
83393239addSjohnlev 	/*
83493239addSjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
83593239addSjohnlev 	 * in zoneadm.
83693239addSjohnlev 	 */
83793239addSjohnlev 	if (isregfile(cmdbuf) == -1)
83893239addSjohnlev 		return (0);
83993239addSjohnlev 
8407c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
84157a250fbSbatschul 	argv[1] = "-o";
84257a250fbSbatschul 	argv[2] = "p";
84357a250fbSbatschul 	argv[3] = (char *)rawdev;
84457a250fbSbatschul 	argv[4] = NULL;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8477c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8487c478bd9Sstevel@tonic-gate 		return (status);
8497c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8507c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8517c478bd9Sstevel@tonic-gate 	return (-1);
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate static int
8557c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8567c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8597c478bd9Sstevel@tonic-gate 	char *argv[6];
8607c478bd9Sstevel@tonic-gate 	int status;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	/*
8637c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8647c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8657c478bd9Sstevel@tonic-gate 	 */
8667c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8679acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8687c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8697c478bd9Sstevel@tonic-gate 		return (-1);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
8727c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
8737c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
8747c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
8757c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
8767c478bd9Sstevel@tonic-gate 	} else {
8777c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
8787c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
8797c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
8807c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
8817c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8857c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8867c478bd9Sstevel@tonic-gate 		return (status);
8877c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
8887c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8897c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8907c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8917c478bd9Sstevel@tonic-gate 	else
8927c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8937c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8947c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8957c478bd9Sstevel@tonic-gate 	return (-1);
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate /*
899d314f035Sedp  * Check if a given mount point path exists.
900d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
901d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
902d314f035Sedp  * component of the mount point path, so it must be a directory.
903d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
904d314f035Sedp  * path, so the mount point itself can be anything aside from a
905d314f035Sedp  * symbolic link.
906d314f035Sedp  *
907d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
908d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
909d314f035Sedp  * If the path doesn't exist return a positive value.
9107c478bd9Sstevel@tonic-gate  */
9117c478bd9Sstevel@tonic-gate static int
912d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9157c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9167c478bd9Sstevel@tonic-gate 	int res;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9197c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
920d314f035Sedp 			return (1);
9217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9227c478bd9Sstevel@tonic-gate 		return (-1);
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9267c478bd9Sstevel@tonic-gate 		return (-1);
9277c478bd9Sstevel@tonic-gate 	}
928d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9307c478bd9Sstevel@tonic-gate 		return (-1);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9347c478bd9Sstevel@tonic-gate 		return (-1);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9377c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9387c478bd9Sstevel@tonic-gate 		/*
939d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9407c478bd9Sstevel@tonic-gate 		 */
9417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9427c478bd9Sstevel@tonic-gate 		return (-1);
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 	return (0);
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate /*
948d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
949d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
950d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
951d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
952d314f035Sedp  * object.  (Different filesystems can support different mount points,
953d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
954d314f035Sedp  * while "ufs" just supports directories.)
9557c478bd9Sstevel@tonic-gate  *
956d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
957d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
958d314f035Sedp  * If the path doesn't exist return a positive value.
9597c478bd9Sstevel@tonic-gate  */
960d314f035Sedp int
961d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
962d314f035Sedp     const char *dir, const char *fstype)
9637c478bd9Sstevel@tonic-gate {
964d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
965d314f035Sedp 	int rv;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	/*
968d314f035Sedp 	 * Sanity check the target mount point path.
969d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
9707c478bd9Sstevel@tonic-gate 	 */
971d314f035Sedp 	if (dir[0] != '/') {
972d314f035Sedp 		/* Something went wrong. */
973d314f035Sedp 		zerror(zlogp, B_FALSE, "invalid mount directory, "
974d314f035Sedp 		    "type: \"%s\", special: \"%s\", dir: \"%s\"",
975d314f035Sedp 		    fstype, spec, dir);
976d314f035Sedp 		return (-1);
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
979d314f035Sedp 	/*
980d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
981d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
982d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
983d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
984d314f035Sedp 	 * cannonical path check in valid_mount_point()).
985d314f035Sedp 	 */
986d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
987d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
988d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
989d314f035Sedp 		    rootpath, dir);
990d314f035Sedp 		return (-1);
991d314f035Sedp 	}
992d314f035Sedp 
993d314f035Sedp 	/*
994d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
995d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
996d314f035Sedp 	 */
9977c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
9987c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
9997c478bd9Sstevel@tonic-gate 	do {
1000d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
10017c478bd9Sstevel@tonic-gate 		*slashp = '\0';
1002d314f035Sedp 		if (slashp_next != NULL) {
1003d314f035Sedp 			/* This is an intermediary mount path component. */
1004d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1005d314f035Sedp 		} else {
1006d314f035Sedp 			/* This is the last component of the mount path. */
1007d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1008d314f035Sedp 		}
1009d314f035Sedp 		if (rv < 0)
1010d314f035Sedp 			return (rv);
10117c478bd9Sstevel@tonic-gate 		*slashp = '/';
1012d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1013d314f035Sedp 	return (rv);
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate static int
10179acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10189acbbeafSnn35248 {
10199acbbeafSnn35248 	di_prof_t prof = arg;
10209acbbeafSnn35248 
10219acbbeafSnn35248 	if (name == NULL)
10229acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
10239acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
10249acbbeafSnn35248 }
10259acbbeafSnn35248 
10269acbbeafSnn35248 static int
10279acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10289acbbeafSnn35248 {
10299acbbeafSnn35248 	di_prof_t prof = arg;
10309acbbeafSnn35248 
10319acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
10329acbbeafSnn35248 }
10339acbbeafSnn35248 
10342b24ab6bSSebastien Roy int
10352b24ab6bSSebastien Roy vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1036f4b3ec61Sdh155122 {
1037f4b3ec61Sdh155122 	zone_dochandle_t handle;
1038f4b3ec61Sdh155122 
1039f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
1040f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1041f4b3ec61Sdh155122 		return (-1);
1042f4b3ec61Sdh155122 	}
1043f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1044f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
1045f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1046f4b3ec61Sdh155122 		return (-1);
1047f4b3ec61Sdh155122 	}
1048f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1049f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1050f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1051f4b3ec61Sdh155122 		return (-1);
1052f4b3ec61Sdh155122 	}
1053f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1054f4b3ec61Sdh155122 	return (0);
1055f4b3ec61Sdh155122 }
1056f4b3ec61Sdh155122 
10579acbbeafSnn35248 /*
10589acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10599acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10609acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10619acbbeafSnn35248  */
10629acbbeafSnn35248 static int
10630679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10649acbbeafSnn35248 {
10659acbbeafSnn35248 	char			brand[MAXNAMELEN];
10669acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1067123807fbSedp 	brand_handle_t		bh = NULL;
10689acbbeafSnn35248 	struct zone_devtab	ztab;
10699acbbeafSnn35248 	di_prof_t		prof = NULL;
10709acbbeafSnn35248 	int			err;
10719acbbeafSnn35248 	int			retval = -1;
1072f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1073f4b3ec61Sdh155122 	const char 		*curr_iptype;
10749acbbeafSnn35248 
10759acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
10769acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10779acbbeafSnn35248 		goto cleanup;
10789acbbeafSnn35248 	}
10799acbbeafSnn35248 
10800679f794S 	/*
10810679f794S 	 * Get a handle to the brand info for this zone.
1082e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
10830679f794S 	 * brand device mounts.
10840679f794S 	 */
10850679f794S 	if (ALT_MOUNT(mount_cmd)) {
1086e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
10870679f794S 	} else {
108862868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
10890679f794S 	}
10900679f794S 
10910679f794S 	if ((bh = brand_open(brand)) == NULL) {
10929acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
10939acbbeafSnn35248 		goto cleanup;
10949acbbeafSnn35248 	}
10959acbbeafSnn35248 
10962b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
1097f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1098f4b3ec61Sdh155122 		goto cleanup;
1099f4b3ec61Sdh155122 	}
1100f4b3ec61Sdh155122 	switch (iptype) {
1101f4b3ec61Sdh155122 	case ZS_SHARED:
1102f4b3ec61Sdh155122 		curr_iptype = "shared";
1103f4b3ec61Sdh155122 		break;
1104f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1105f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1106f4b3ec61Sdh155122 		break;
1107f4b3ec61Sdh155122 	}
1108f4b3ec61Sdh155122 
1109123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1110f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11119acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11129acbbeafSnn35248 		goto cleanup;
11139acbbeafSnn35248 	}
11149acbbeafSnn35248 
1115123807fbSedp 	if (brand_platform_iter_link(bh,
11169acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11179acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11189acbbeafSnn35248 		goto cleanup;
11199acbbeafSnn35248 	}
11209acbbeafSnn35248 
11219acbbeafSnn35248 	/* Add user-specified devices and directories */
11229acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11239acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11249acbbeafSnn35248 		goto cleanup;
11259acbbeafSnn35248 	}
11269acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11279acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11289acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11299acbbeafSnn35248 		goto cleanup;
11309acbbeafSnn35248 	}
11319acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
11329acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11339acbbeafSnn35248 		    zonecfg_strerror(err));
11349acbbeafSnn35248 		goto cleanup;
11359acbbeafSnn35248 	}
11369acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11379acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11389acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
11399acbbeafSnn35248 			    "user-specified device");
11409acbbeafSnn35248 			goto cleanup;
11419acbbeafSnn35248 		}
11429acbbeafSnn35248 	}
11439acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
11449acbbeafSnn35248 
11459acbbeafSnn35248 	/* Send profile to kernel */
11469acbbeafSnn35248 	if (di_prof_commit(prof)) {
11479acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11489acbbeafSnn35248 		goto cleanup;
11499acbbeafSnn35248 	}
11509acbbeafSnn35248 
11519acbbeafSnn35248 	retval = 0;
11529acbbeafSnn35248 
11539acbbeafSnn35248 cleanup:
1154123807fbSedp 	if (bh != NULL)
1155123807fbSedp 		brand_close(bh);
1156e767a340Sgjelinek 	if (handle != NULL)
11579acbbeafSnn35248 		zonecfg_fini_handle(handle);
11589acbbeafSnn35248 	if (prof)
11599acbbeafSnn35248 		di_prof_fini(prof);
11609acbbeafSnn35248 	return (retval);
11619acbbeafSnn35248 }
11629acbbeafSnn35248 
11639acbbeafSnn35248 static int
11640679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11650679f794S     zone_mnt_t mount_cmd)
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
11687c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11697c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11709acbbeafSnn35248 	int rv;
11717c478bd9Sstevel@tonic-gate 
1172d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1173d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11747c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11757c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11767c478bd9Sstevel@tonic-gate 		return (-1);
1177d314f035Sedp 	} else if (rv > 0) {
1178d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1179d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1180d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1181d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1182d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1183d314f035Sedp 			return (-1);
11847c478bd9Sstevel@tonic-gate 		}
11857c478bd9Sstevel@tonic-gate 
1186d314f035Sedp 		/*
1187d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1188d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1189d314f035Sedp 		 * to make sure that the mount point path is canonical,
1190d314f035Sedp 		 * which it can only do if the path exists, so now that
1191d314f035Sedp 		 * we've created the path we have to verify it again.
1192d314f035Sedp 		 */
1193d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1194d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1195d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1196d314f035Sedp 			zerror(zlogp, B_FALSE,
1197d314f035Sedp 			    "%s%s is not a valid mount point",
1198d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
11997c478bd9Sstevel@tonic-gate 			return (-1);
1200d314f035Sedp 		}
1201d314f035Sedp 	}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12047c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	/*
12077c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12087c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12097c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12107c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12117c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12127c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12137c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12147c478bd9Sstevel@tonic-gate 	 */
12157c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12177c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12187c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12197c478bd9Sstevel@tonic-gate 		return (-1);
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	/*
1223108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1224fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1225fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1226fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1227fa3d7ae1Sedp 	 * time we get here.
1228108322fbScarlsonj 	 */
1229108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1230108322fbScarlsonj 		struct stat64 st;
1231108322fbScarlsonj 
1232108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
123368eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1234fa3d7ae1Sedp 			/*
1235fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1236fa3d7ae1Sedp 			 * to check if that device is already mounted
1237fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1238fa3d7ae1Sedp 			 * of the device instead of a direct mount
1239fa3d7ae1Sedp 			 */
124068eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1241108322fbScarlsonj 				return (-1);
124268eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1243fa3d7ae1Sedp 			/*
1244fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1245fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1246fa3d7ae1Sedp 			 * this case in order to get at the underlying
1247fa3d7ae1Sedp 			 * read-write path.
1248fa3d7ae1Sedp 			 */
1249fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1250108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1251108322fbScarlsonj 		}
1252108322fbScarlsonj 	}
1253108322fbScarlsonj 
1254108322fbScarlsonj 	/*
12557c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12567c478bd9Sstevel@tonic-gate 	 */
12577c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
125893239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12597c478bd9Sstevel@tonic-gate 		return (-1);
126093239addSjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
126193239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
126293239addSjohnlev 		return (-1);
126393239addSjohnlev 	}
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	/*
12667c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
12677c478bd9Sstevel@tonic-gate 	 */
12687c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
12697c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
12707c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12717c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
12727c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
12737c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
12747c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
12757c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
12767c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
12777c478bd9Sstevel@tonic-gate 		}
12787c478bd9Sstevel@tonic-gate 	}
12799acbbeafSnn35248 
12809acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
12819acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
12829acbbeafSnn35248 		return (rv);
12839acbbeafSnn35248 
12849acbbeafSnn35248 	/*
12859acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
12869acbbeafSnn35248 	 * we're done.
12879acbbeafSnn35248 	 */
12889acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
12899acbbeafSnn35248 		return (0);
12909acbbeafSnn35248 
12919acbbeafSnn35248 	/*
12929acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
12939acbbeafSnn35248 	 * need to configure it.
12949acbbeafSnn35248 	 */
12950679f794S 	return (mount_one_dev(zlogp, path, mount_cmd));
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate static void
12997c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13007c478bd9Sstevel@tonic-gate {
13017c478bd9Sstevel@tonic-gate 	uint_t i;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13047c478bd9Sstevel@tonic-gate 		return;
13057c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13067c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13077c478bd9Sstevel@tonic-gate 	free(fsarray);
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
1310108322fbScarlsonj /*
1311f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1312f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1313f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1314f4368d3dSvp157776  * is done this way because:
1315f4368d3dSvp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
1316f4368d3dSvp157776  * 	We loopback mount zone's own /etc and /var into the root of the
1317f4368d3dSvp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1318f4368d3dSvp157776  * 	need to delay the mount of /var till the zone's root gets populated.
1319f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1320f4368d3dSvp157776  * 	build_mounted_post_var() which gets called only after the zone
1321f4368d3dSvp157776  * 	specific filesystems are mounted.
13226cfd72c6Sgjelinek  *
13236cfd72c6Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13246cfd72c6Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13256cfd72c6Sgjelinek  * scratch zone.
1326108322fbScarlsonj  */
1327108322fbScarlsonj static boolean_t
1328f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
132916bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1330108322fbScarlsonj {
1331108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1332108322fbScarlsonj 	const char **cpp;
1333108322fbScarlsonj 	static const char *mkdirs[] = {
13343f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13353f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1336108322fbScarlsonj 	};
1337108322fbScarlsonj 	char *altstr;
1338f4368d3dSvp157776 	FILE *fp;
1339108322fbScarlsonj 	uuid_t uuid;
1340108322fbScarlsonj 
1341108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
134216bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
134316bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1344108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1345108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1346108322fbScarlsonj 
1347108322fbScarlsonj 	/*
1348108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1349108322fbScarlsonj 	 * zone_mount_early.)
1350108322fbScarlsonj 	 */
1351108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1352108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1353108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1354108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1355108322fbScarlsonj 			return (B_FALSE);
1356108322fbScarlsonj 		}
1357108322fbScarlsonj 	}
1358f4368d3dSvp157776 	/*
1359f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1360f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1361f4368d3dSvp157776 	 * read-only inside the scratch zone.
1362f4368d3dSvp157776 	 */
1363f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1364f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1365f4368d3dSvp157776 	if (altstr == NULL) {
1366f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1367f4368d3dSvp157776 		return (B_FALSE);
1368f4368d3dSvp157776 	}
1369f4368d3dSvp157776 	zonecfg_set_root("");
1370f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1371f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1372f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1373f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1374f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1375f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1376f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1377f4368d3dSvp157776 			return (B_FALSE);
1378f4368d3dSvp157776 		}
1379*6e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1380f4368d3dSvp157776 		    tmp) != 0) {
1381f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1382f4368d3dSvp157776 			    fromdir);
1383f4368d3dSvp157776 			return (B_FALSE);
1384f4368d3dSvp157776 		}
1385f4368d3dSvp157776 	}
1386f4368d3dSvp157776 	zonecfg_set_root(altstr);
1387f4368d3dSvp157776 	free(altstr);
1388f4368d3dSvp157776 
1389f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1390f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1391f4368d3dSvp157776 		return (B_FALSE);
1392f4368d3dSvp157776 	}
1393f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1394f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1395f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1396f4368d3dSvp157776 	}
1397f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1398f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1399f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1400f4368d3dSvp157776 		return (B_FALSE);
1401f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1402f4368d3dSvp157776 	return (B_TRUE);
1403f4368d3dSvp157776 }
1404f4368d3dSvp157776 
1405f4368d3dSvp157776 
1406f4368d3dSvp157776 static boolean_t
14076cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14086cfd72c6Sgjelinek     const char *luroot)
1409f4368d3dSvp157776 {
1410f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1411f4368d3dSvp157776 	const char **cpp;
14126cfd72c6Sgjelinek 	const char **loopdirs;
14136cfd72c6Sgjelinek 	const char **tmpdirs;
1414f4368d3dSvp157776 	static const char *localdirs[] = {
1415f4368d3dSvp157776 		"/etc", "/var", NULL
1416f4368d3dSvp157776 	};
14176cfd72c6Sgjelinek 	static const char *scr_loopdirs[] = {
1418f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1419f4368d3dSvp157776 		"/usr", NULL
1420f4368d3dSvp157776 	};
14216cfd72c6Sgjelinek 	static const char *upd_loopdirs[] = {
14226cfd72c6Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14236cfd72c6Sgjelinek 		"/usr", "/var", NULL
14246cfd72c6Sgjelinek 	};
14256cfd72c6Sgjelinek 	static const char *scr_tmpdirs[] = {
1426f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1427f4368d3dSvp157776 	};
14286cfd72c6Sgjelinek 	static const char *upd_tmpdirs[] = {
14296cfd72c6Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14306cfd72c6Sgjelinek 	};
1431f4368d3dSvp157776 	struct stat st;
1432f4368d3dSvp157776 
14336cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
1434108322fbScarlsonj 		/*
14356cfd72c6Sgjelinek 		 * These are mounted read-write from the zone undergoing
14366cfd72c6Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14376cfd72c6Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
1438108322fbScarlsonj 		 */
1439108322fbScarlsonj 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14406cfd72c6Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1441108322fbScarlsonj 			    *cpp);
14426cfd72c6Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14436cfd72c6Sgjelinek 			    rootpath, *cpp);
1444108322fbScarlsonj 			if (mkdir(tmp, 0755) != 0) {
1445108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1446108322fbScarlsonj 				return (B_FALSE);
1447108322fbScarlsonj 			}
14486cfd72c6Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14496cfd72c6Sgjelinek 			    != 0) {
14506cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14516cfd72c6Sgjelinek 				    tmp, *cpp);
1452108322fbScarlsonj 				return (B_FALSE);
1453108322fbScarlsonj 			}
1454108322fbScarlsonj 		}
14556cfd72c6Sgjelinek 	}
14566cfd72c6Sgjelinek 
14576cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14586cfd72c6Sgjelinek 		loopdirs = upd_loopdirs;
14596cfd72c6Sgjelinek 	else
14606cfd72c6Sgjelinek 		loopdirs = scr_loopdirs;
1461108322fbScarlsonj 
1462108322fbScarlsonj 	/*
1463108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1464108322fbScarlsonj 	 * they contain binaries that must match system.
1465108322fbScarlsonj 	 */
1466108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1467108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1468108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1469108322fbScarlsonj 			if (errno != EEXIST) {
1470108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1471108322fbScarlsonj 				return (B_FALSE);
1472108322fbScarlsonj 			}
1473108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1474108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1475108322fbScarlsonj 				return (B_FALSE);
1476108322fbScarlsonj 			}
1477108322fbScarlsonj 			/*
1478108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1479108322fbScarlsonj 			 * things that have been converted into symlinks
1480108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1481108322fbScarlsonj 			 * fixup.
1482108322fbScarlsonj 			 */
1483108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1484108322fbScarlsonj 				continue;
1485108322fbScarlsonj 		}
1486*6e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1487108322fbScarlsonj 		    tmp) != 0) {
1488108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1489108322fbScarlsonj 			    *cpp);
1490108322fbScarlsonj 			return (B_FALSE);
1491108322fbScarlsonj 		}
1492108322fbScarlsonj 	}
1493108322fbScarlsonj 
14946cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14956cfd72c6Sgjelinek 		tmpdirs = upd_tmpdirs;
14966cfd72c6Sgjelinek 	else
14976cfd72c6Sgjelinek 		tmpdirs = scr_tmpdirs;
14986cfd72c6Sgjelinek 
1499108322fbScarlsonj 	/*
1500108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1501108322fbScarlsonj 	 */
1502108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1503108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15046cfd72c6Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15056cfd72c6Sgjelinek 		    errno != EEXIST) {
1506108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1507108322fbScarlsonj 			return (B_FALSE);
1508108322fbScarlsonj 		}
1509d30e996aSgjelinek 
1510d30e996aSgjelinek 		/*
1511d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1512d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1513d30e996aSgjelinek 		 * the correct mode for /tmp now.
1514d30e996aSgjelinek 		 */
1515d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1516d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1517d30e996aSgjelinek 			return (B_FALSE);
1518d30e996aSgjelinek 		}
1519d30e996aSgjelinek 
1520108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1521108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1522108322fbScarlsonj 			return (B_FALSE);
1523108322fbScarlsonj 		}
1524108322fbScarlsonj 	}
1525108322fbScarlsonj 	return (B_TRUE);
1526108322fbScarlsonj }
1527108322fbScarlsonj 
15289acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15299acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
15309acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
15319acbbeafSnn35248 	int			*pgcd_num_fs;
15329acbbeafSnn35248 } plat_gmount_cb_data_t;
15339acbbeafSnn35248 
15349acbbeafSnn35248 /*
15359acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15369acbbeafSnn35248  * through all global brand platform mounts.
15379acbbeafSnn35248  */
15389acbbeafSnn35248 int
15399acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15409acbbeafSnn35248     const char *fstype, const char *opt)
15419acbbeafSnn35248 {
15429acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
15439acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15449acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15459acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
15469acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15479acbbeafSnn35248 
15489acbbeafSnn35248 	num_fs++;
15499acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15509acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15519acbbeafSnn35248 		return (-1);
15529acbbeafSnn35248 	}
15539acbbeafSnn35248 
15549acbbeafSnn35248 	fs_ptr = tmp_ptr;
15559acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
15569acbbeafSnn35248 
15579acbbeafSnn35248 	/* update the callback struct passed in */
15589acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
15599acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
15609acbbeafSnn35248 
15619acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
15629acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15639acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
15649acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15659acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15669acbbeafSnn35248 	fsp->zone_fs_options = NULL;
1567fa3d7ae1Sedp 	if ((opt != NULL) &&
1568fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15699acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
15709acbbeafSnn35248 		return (-1);
15719acbbeafSnn35248 	}
15729acbbeafSnn35248 
15739acbbeafSnn35248 	return (0);
15749acbbeafSnn35248 }
15759acbbeafSnn35248 
15769acbbeafSnn35248 static int
15779acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
15786cfd72c6Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
15799acbbeafSnn35248 {
15809acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
15819acbbeafSnn35248 	int num_fs;
15829acbbeafSnn35248 
15839acbbeafSnn35248 	num_fs = *num_fsp;
15849acbbeafSnn35248 	fs_ptr = *fs_tabp;
15859acbbeafSnn35248 
15869acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
15879acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
15889acbbeafSnn35248 		return (-1);
15899acbbeafSnn35248 	}
15909acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
15919acbbeafSnn35248 		/*
15929acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
15939acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
15949acbbeafSnn35248 		 * case.
15959acbbeafSnn35248 		 */
15966cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
15976cfd72c6Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
15989acbbeafSnn35248 			continue;
15999acbbeafSnn35248 
16009acbbeafSnn35248 		num_fs++;
16019acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16029acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16039acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16049acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
16059acbbeafSnn35248 			return (-1);
16069acbbeafSnn35248 		}
16079acbbeafSnn35248 		/* update the pointers passed in */
16089acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16099acbbeafSnn35248 		*num_fsp = num_fs;
16109acbbeafSnn35248 
16119acbbeafSnn35248 		fs_ptr = tmp_ptr;
16129acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16139acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16149acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16159acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16169acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
16179acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16189acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
16199acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
1620fa3d7ae1Sedp 
1621fa3d7ae1Sedp 		/*
1622fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1623fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1624fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1625fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1626fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1627fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1628fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1629fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1630fa3d7ae1Sedp 		 */
1631fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1632fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1633fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1634fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1635fa3d7ae1Sedp 		}
1636fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1637fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
16389acbbeafSnn35248 	}
16399acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
16409acbbeafSnn35248 	return (0);
16419acbbeafSnn35248 }
16429acbbeafSnn35248 
16437c478bd9Sstevel@tonic-gate static int
16446cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
16457c478bd9Sstevel@tonic-gate {
16467c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
16477c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
16489acbbeafSnn35248 	char brand[MAXNAMELEN];
164916bca938Svp157776 	char luroot[MAXPATHLEN];
16509acbbeafSnn35248 	int i, num_fs = 0;
16519acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
16527c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
16537c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1654123807fbSedp 	brand_handle_t bh;
16559acbbeafSnn35248 	plat_gmount_cb_data_t cb;
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1658108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
16597c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1660108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1661108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1662108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
16637c478bd9Sstevel@tonic-gate 		goto bad;
16647c478bd9Sstevel@tonic-gate 	}
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
16677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
16687c478bd9Sstevel@tonic-gate 		goto bad;
16697c478bd9Sstevel@tonic-gate 	}
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
16727c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
16737c478bd9Sstevel@tonic-gate 		goto bad;
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1677ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
16787c478bd9Sstevel@tonic-gate 		goto bad;
16797c478bd9Sstevel@tonic-gate 	}
16807c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
16817c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
16827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
16837c478bd9Sstevel@tonic-gate 		goto bad;
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 
16860679f794S 	/*
1687e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
16880679f794S 	 * brand global mounts.
16890679f794S 	 */
16900679f794S 	if (ALT_MOUNT(mount_cmd)) {
1691e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
16920679f794S 	} else {
169362868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
16940679f794S 	}
16950679f794S 
16969acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
16970679f794S 	if ((bh = brand_open(brand)) == NULL) {
16989acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1699e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17009acbbeafSnn35248 		return (-1);
17019acbbeafSnn35248 	}
17029acbbeafSnn35248 
17039acbbeafSnn35248 	/*
17049acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
17059acbbeafSnn35248 	 * configuration.
17069acbbeafSnn35248 	 */
17079acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
17089acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
17099acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1710123807fbSedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17119acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
17129acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1713123807fbSedp 		brand_close(bh);
1714e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17159acbbeafSnn35248 		return (-1);
17169acbbeafSnn35248 	}
1717123807fbSedp 	brand_close(bh);
17189acbbeafSnn35248 
17197c478bd9Sstevel@tonic-gate 	/*
1720*6e1ae2a3SGary Pennington 	 * Iterate through the rest of the filesystems. Sort them all,
1721*6e1ae2a3SGary Pennington 	 * then mount them in sorted order. This is to make sure the
1722*6e1ae2a3SGary Pennington 	 * higher level directories (e.g., /usr) get mounted before
1723*6e1ae2a3SGary Pennington 	 * any beneath them (e.g., /usr/local).
17247c478bd9Sstevel@tonic-gate 	 */
17259acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17269acbbeafSnn35248 	    mount_cmd) != 0)
17277c478bd9Sstevel@tonic-gate 		goto bad;
1728fa9e4066Sahrens 
17297c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17307c478bd9Sstevel@tonic-gate 	handle = NULL;
17317c478bd9Sstevel@tonic-gate 
1732108322fbScarlsonj 	/*
17339acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
17349acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
17359acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
17369acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
17379acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
17389acbbeafSnn35248 	 * the zones filesystems there instead.
17399acbbeafSnn35248 	 *
17409acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
17419acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
17429acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
17439acbbeafSnn35248 	 *
17449acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
17459acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
17469acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
17479acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
17489acbbeafSnn35248 	 * upgrade environment.)
17499acbbeafSnn35248 	 *
17509acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
17519acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
17529acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
17539acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
17549acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
17559acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
17569acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
17579acbbeafSnn35248 	 *
17589acbbeafSnn35248 	 * All this work is done in three phases:
1759f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1760f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1761f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1762f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1763108322fbScarlsonj 	 */
17646cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
176516bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1766108322fbScarlsonj 		goto bad;
1767108322fbScarlsonj 
17687c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
17699acbbeafSnn35248 
17707c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
17716cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
17729acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
17739acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
17749acbbeafSnn35248 
17759acbbeafSnn35248 			/*
17769acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
17779acbbeafSnn35248 			 * but /dev is special and always goes at the top
17789acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
17799acbbeafSnn35248 			 */
17809acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
17819acbbeafSnn35248 			rootpath[slen] = '\0';
17820679f794S 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
17830679f794S 			    != 0)
17849acbbeafSnn35248 				goto bad;
17859acbbeafSnn35248 			rootpath[slen] = '/';
17869acbbeafSnn35248 			continue;
17879acbbeafSnn35248 		}
17880679f794S 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
17897c478bd9Sstevel@tonic-gate 			goto bad;
17907c478bd9Sstevel@tonic-gate 	}
17916cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
17926cfd72c6Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1793f4368d3dSvp157776 		goto bad;
179445916cd2Sjpk 
179545916cd2Sjpk 	/*
179645916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
179745916cd2Sjpk 	 */
17986cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
17996cfd72c6Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
180045916cd2Sjpk 		goto bad;
180145916cd2Sjpk 
18027c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	/*
18057c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18067c478bd9Sstevel@tonic-gate 	 */
18077c478bd9Sstevel@tonic-gate 	return (0);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate bad:
18107c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18117c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18127c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18137c478bd9Sstevel@tonic-gate 	return (-1);
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18177c478bd9Sstevel@tonic-gate static int
18187c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18197c478bd9Sstevel@tonic-gate {
18207c478bd9Sstevel@tonic-gate 	int prefixlen;
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18237c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18247c478bd9Sstevel@tonic-gate 		return (1);
18257c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
18267c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
18277c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18287c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
18297c478bd9Sstevel@tonic-gate 			continue;
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18327c478bd9Sstevel@tonic-gate 		prefixlen--;
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 	return (0);
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate /*
18387c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
18397c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
18407c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
18417c478bd9Sstevel@tonic-gate  *
18427c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
18437c478bd9Sstevel@tonic-gate  */
18447c478bd9Sstevel@tonic-gate static int
1845f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
18487c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
18497c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
18507c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
18517c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
18527c478bd9Sstevel@tonic-gate 	uint_t bufsize;
18537c478bd9Sstevel@tonic-gate 	char *buf = NULL;
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
18567c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
18577c478bd9Sstevel@tonic-gate 		ret_code = -1;
18587c478bd9Sstevel@tonic-gate 		goto bad;
18597c478bd9Sstevel@tonic-gate 	}
18607c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
18617c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
18627c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
18637c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1864f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
18657c478bd9Sstevel@tonic-gate 		ret_code = -1;
18667c478bd9Sstevel@tonic-gate 		goto bad;
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
18697c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
18707c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
18717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
18727c478bd9Sstevel@tonic-gate 		ret_code = -1;
18737c478bd9Sstevel@tonic-gate 		goto bad;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
18767c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
18777c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
18787c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
18797c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1880f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1881f4b3ec61Sdh155122 		    "interfaces");
18827c478bd9Sstevel@tonic-gate 		ret_code = -1;
18837c478bd9Sstevel@tonic-gate 		goto bad;
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
18867c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
18877c478bd9Sstevel@tonic-gate 		(void) close(s);
18887c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
18897c478bd9Sstevel@tonic-gate 		    0) {
18907c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
18917c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
18927c478bd9Sstevel@tonic-gate 			ret_code = -1;
18937c478bd9Sstevel@tonic-gate 			continue;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
18967c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
18977c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
18987c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1899c1c0ebd5Ssl108498 			if (errno == ENXIO)
1900c1c0ebd5Ssl108498 				/*
1901c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1902c1c0ebd5Ssl108498 				 * another zone halting.
1903c1c0ebd5Ssl108498 				 */
1904c1c0ebd5Ssl108498 				continue;
19057c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1906c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1907f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19087c478bd9Sstevel@tonic-gate 			ret_code = -1;
19097c478bd9Sstevel@tonic-gate 			continue;
19107c478bd9Sstevel@tonic-gate 		}
19117c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19127c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19137c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1914f4b3ec61Sdh155122 				    "%s: could not remove network interface",
19157c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
19167c478bd9Sstevel@tonic-gate 				ret_code = -1;
19177c478bd9Sstevel@tonic-gate 				continue;
19187c478bd9Sstevel@tonic-gate 			}
19197c478bd9Sstevel@tonic-gate 		}
19207c478bd9Sstevel@tonic-gate 	}
19217c478bd9Sstevel@tonic-gate bad:
19227c478bd9Sstevel@tonic-gate 	if (s > 0)
19237c478bd9Sstevel@tonic-gate 		(void) close(s);
19247c478bd9Sstevel@tonic-gate 	if (buf)
19257c478bd9Sstevel@tonic-gate 		free(buf);
19267c478bd9Sstevel@tonic-gate 	return (ret_code);
19277c478bd9Sstevel@tonic-gate }
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate static union	sockunion {
19307c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
19317c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
19327c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19337c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19347c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate static struct {
19377c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
19387c478bd9Sstevel@tonic-gate 	char	space[512];
19397c478bd9Sstevel@tonic-gate } rtmsg;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate static int
19427c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
19437c478bd9Sstevel@tonic-gate {
19447c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
19457c478bd9Sstevel@tonic-gate 	case AF_INET:
19467c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
19477c478bd9Sstevel@tonic-gate 	case AF_LINK:
19487c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
19497c478bd9Sstevel@tonic-gate 	case AF_INET6:
19507c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
19517c478bd9Sstevel@tonic-gate 	default:
19527c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate }
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
19577c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate /*
19607c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
19617c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
19627c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
19637c478bd9Sstevel@tonic-gate  *
19647c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
19657c478bd9Sstevel@tonic-gate  *
19667c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
19677c478bd9Sstevel@tonic-gate  *
19687c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
19697c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
19707c478bd9Sstevel@tonic-gate  */
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate static char *
19737c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
19747c478bd9Sstevel@tonic-gate {
19757c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
19767c478bd9Sstevel@tonic-gate 	pid_t pid;
19777c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
19787c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
19797c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
19807c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
19817c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	pid = getpid();
19867c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
19877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
19887c478bd9Sstevel@tonic-gate 		return (NULL);
19897c478bd9Sstevel@tonic-gate 	}
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
19927c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
19957c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
19967c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
19977c478bd9Sstevel@tonic-gate 	} else {
19987c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20017c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20027c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20037c478bd9Sstevel@tonic-gate 	}
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20087c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20097c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20107c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20117c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20127c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20157c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20167c478bd9Sstevel@tonic-gate 	cp += l;
20177c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20187c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20197c478bd9Sstevel@tonic-gate 	cp += l;
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20247c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20257c478bd9Sstevel@tonic-gate 		return (NULL);
20267c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20287c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20297c478bd9Sstevel@tonic-gate 		return (NULL);
20307c478bd9Sstevel@tonic-gate 	}
20317c478bd9Sstevel@tonic-gate 	do {
20327c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20337c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20347c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20357c478bd9Sstevel@tonic-gate 	if (l < 0) {
20367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
20377c478bd9Sstevel@tonic-gate 		return (NULL);
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
20417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
20427c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
20437c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
20447c478bd9Sstevel@tonic-gate 		return (NULL);
20457c478bd9Sstevel@tonic-gate 	}
20467c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
20477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
20487c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
20497c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
20507c478bd9Sstevel@tonic-gate 		return (NULL);
20517c478bd9Sstevel@tonic-gate 	}
20527c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
20537c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
20547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
20557c478bd9Sstevel@tonic-gate 		return (NULL);
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2058f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
20597c478bd9Sstevel@tonic-gate 		return (NULL);
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
20627c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
20637c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
20647c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
20657c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
20667c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
20677c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
20687c478bd9Sstevel@tonic-gate 			continue;
20697c478bd9Sstevel@tonic-gate 		}
20707c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
20717c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
20727c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
20737c478bd9Sstevel@tonic-gate 		break;
20747c478bd9Sstevel@tonic-gate 	}
20757c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2076f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2077f4b3ec61Sdh155122 		    "determined");
20787c478bd9Sstevel@tonic-gate 		return (NULL);
20797c478bd9Sstevel@tonic-gate 	}
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	/*
20827c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
20837c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
20847c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
20857c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
20867c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
20877c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
20887c478bd9Sstevel@tonic-gate 	 */
20897c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
20907c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
20917c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
20927c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
20937c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
20947c478bd9Sstevel@tonic-gate 	if (i < 0) {
20957c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2096f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2097f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
20987c478bd9Sstevel@tonic-gate 		return (NULL);
20997c478bd9Sstevel@tonic-gate 	}
21007c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21017c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21027c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21057c478bd9Sstevel@tonic-gate 		return (answer);
21067c478bd9Sstevel@tonic-gate 	return (NULL);
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate /*
21107c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21117c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21127c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21137c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21147c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21157c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21167c478bd9Sstevel@tonic-gate  *
21177c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21187c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
21197c478bd9Sstevel@tonic-gate  */
21207c478bd9Sstevel@tonic-gate static int
21217c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2122f14f3ae7Sjv227347     struct zone_nwiftab *nwiftabptr)
21237c478bd9Sstevel@tonic-gate {
21247c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
21257c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
21267c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
21272e481403SVamsi Nagineni 	struct sockaddr_storage laddr;
21287c478bd9Sstevel@tonic-gate 	struct in_addr in4;
21297c478bd9Sstevel@tonic-gate 	sa_family_t af;
21307c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
21317c478bd9Sstevel@tonic-gate 	int s;
21327c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
2133f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	boolean_t is_loopback = B_FALSE;
21347c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
21357c478bd9Sstevel@tonic-gate 	int res;
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
21387c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
21397c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
21407c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
21417c478bd9Sstevel@tonic-gate 		return (-1);
21427c478bd9Sstevel@tonic-gate 	}
21437c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
21447c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21457c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21467c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
21487c478bd9Sstevel@tonic-gate 		return (-1);
21497c478bd9Sstevel@tonic-gate 	}
21507c478bd9Sstevel@tonic-gate 
21512e481403SVamsi Nagineni 	/*
21522e481403SVamsi Nagineni 	 * This is a similar kind of "hack" like in addif() to get around
21532e481403SVamsi Nagineni 	 * the problem of SIOCLIFADDIF.  The problem is that this ioctl
21542e481403SVamsi Nagineni 	 * does not include the netmask when adding a logical interface.
21552e481403SVamsi Nagineni 	 * To get around this problem, we first add the logical interface
21562e481403SVamsi Nagineni 	 * with a 0 address.  After that, we set the netmask if provided.
21572e481403SVamsi Nagineni 	 * Finally we set the interface address.
21582e481403SVamsi Nagineni 	 */
21592e481403SVamsi Nagineni 	laddr = lifr.lifr_addr;
21607c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
21617c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
21622e481403SVamsi Nagineni 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
21632e481403SVamsi Nagineni 
21647c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
216522321485Svp157776 		/*
216622321485Svp157776 		 * Here, we know that the interface can't be brought up.
216722321485Svp157776 		 * A similar warning message was already printed out to
216822321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
216922321485Svp157776 		 * message to syslog and continue.
217022321485Svp157776 		 */
2171f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
217222321485Svp157776 		    "'%s' which may not be present/plumbed in the "
217322321485Svp157776 		    "global zone.", lifr.lifr_name);
21747c478bd9Sstevel@tonic-gate 		(void) close(s);
217522321485Svp157776 		return (Z_OK);
21767c478bd9Sstevel@tonic-gate 	}
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
21797c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21807c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
21837c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2184f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2185f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
21867c478bd9Sstevel@tonic-gate 		goto bad;
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 
2189f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	/*
2190f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * Loopback interface will use the default netmask assigned, if no
2191f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * netmask is found.
2192f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 */
21937c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2194f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		is_loopback = B_TRUE;
2195f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	}
21967c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
21977c478bd9Sstevel@tonic-gate 		/*
21987c478bd9Sstevel@tonic-gate 		 * The IPv4 netmask can be determined either
21997c478bd9Sstevel@tonic-gate 		 * directly if a prefix length was supplied with
22007c478bd9Sstevel@tonic-gate 		 * the address or via the netmasks database.  Not
22017c478bd9Sstevel@tonic-gate 		 * being able to determine it is a common failure,
22027c478bd9Sstevel@tonic-gate 		 * but it often is not fatal to operation of the
22037c478bd9Sstevel@tonic-gate 		 * interface.  In that case, a warning will be
22047c478bd9Sstevel@tonic-gate 		 * printed after the rest of the interface's
22057c478bd9Sstevel@tonic-gate 		 * parameters have been configured.
22067c478bd9Sstevel@tonic-gate 		 */
22077c478bd9Sstevel@tonic-gate 		(void) memset(&netmask4, 0, sizeof (netmask4));
22087c478bd9Sstevel@tonic-gate 		if (slashp != NULL) {
22097c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22107c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask4.sin_addr) != 0) {
22117c478bd9Sstevel@tonic-gate 				*slashp = '/';
22127c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22137c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22147c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
22157c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22167c478bd9Sstevel@tonic-gate 				goto bad;
22177c478bd9Sstevel@tonic-gate 			}
22187c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22197c478bd9Sstevel@tonic-gate 		} else if (getnetmaskbyaddr(in4,
22207c478bd9Sstevel@tonic-gate 		    &netmask4.sin_addr) == 0) {
22217c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22227c478bd9Sstevel@tonic-gate 		}
22237c478bd9Sstevel@tonic-gate 		if (got_netmask) {
22247c478bd9Sstevel@tonic-gate 			netmask4.sin_family = af;
22257c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask4,
22267c478bd9Sstevel@tonic-gate 			    sizeof (netmask4));
22277c478bd9Sstevel@tonic-gate 		}
22287c478bd9Sstevel@tonic-gate 	} else {
22297c478bd9Sstevel@tonic-gate 		(void) memset(&netmask6, 0, sizeof (netmask6));
22307c478bd9Sstevel@tonic-gate 		if (addr2netmask(slashp + 1, V6_ADDR_LEN,
22317c478bd9Sstevel@tonic-gate 		    (uchar_t *)&netmask6.sin6_addr) != 0) {
22327c478bd9Sstevel@tonic-gate 			*slashp = '/';
22337c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
22347c478bd9Sstevel@tonic-gate 			    "%s: invalid prefix length in %s",
22357c478bd9Sstevel@tonic-gate 			    lifr.lifr_name,
22367c478bd9Sstevel@tonic-gate 			    nwiftabptr->zone_nwif_address);
22377c478bd9Sstevel@tonic-gate 			goto bad;
22387c478bd9Sstevel@tonic-gate 		}
22397c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;
22407c478bd9Sstevel@tonic-gate 		netmask6.sin6_family = af;
22417c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &netmask6,
22427c478bd9Sstevel@tonic-gate 		    sizeof (netmask6));
22437c478bd9Sstevel@tonic-gate 	}
22447c478bd9Sstevel@tonic-gate 	if (got_netmask &&
22457c478bd9Sstevel@tonic-gate 	    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
22467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not set netmask",
22477c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22487c478bd9Sstevel@tonic-gate 		goto bad;
22497c478bd9Sstevel@tonic-gate 	}
22507c478bd9Sstevel@tonic-gate 
22512e481403SVamsi Nagineni 	/* Set the interface address */
22522e481403SVamsi Nagineni 	lifr.lifr_addr = laddr;
22537c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
22552e481403SVamsi Nagineni 		    "%s: could not set IP address to %s",
22562e481403SVamsi Nagineni 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22577c478bd9Sstevel@tonic-gate 		goto bad;
22587c478bd9Sstevel@tonic-gate 	}
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22617c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
22627c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22637c478bd9Sstevel@tonic-gate 		goto bad;
22647c478bd9Sstevel@tonic-gate 	}
22657c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
22667c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
22677c478bd9Sstevel@tonic-gate 		int save_errno = errno;
22687c478bd9Sstevel@tonic-gate 		char *zone_using;
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 		/*
22717c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
22727c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
22737c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
22747c478bd9Sstevel@tonic-gate 		 * using that address.
22757c478bd9Sstevel@tonic-gate 		 */
22767c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
22777c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2278f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2279f4b3ec61Sdh155122 			    lifr.lifr_name);
22807c478bd9Sstevel@tonic-gate 			goto bad;
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
22837c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
22847c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
22857c478bd9Sstevel@tonic-gate 			goto bad;
22867c478bd9Sstevel@tonic-gate 		}
22877c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
22887c478bd9Sstevel@tonic-gate 		errno = save_errno;
22897c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
22907c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2291f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2292f4b3ec61Sdh155122 			    lifr.lifr_name);
22937c478bd9Sstevel@tonic-gate 		else
2294f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2295f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2296f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
22977c478bd9Sstevel@tonic-gate 		goto bad;
22987c478bd9Sstevel@tonic-gate 	}
22997c478bd9Sstevel@tonic-gate 
2300f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (!got_netmask && !is_loopback) {
23017c478bd9Sstevel@tonic-gate 		/*
23027c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23037c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23047c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23057c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23067c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23077c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23087c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23097c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23107c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23117c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
23127c478bd9Sstevel@tonic-gate 		 */
23137c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23147c478bd9Sstevel@tonic-gate 		void  *addr;
2315e11c3f44Smeem 		const char *nomatch = "no matching subnet found in netmasks(4)";
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
23187c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23197c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23207c478bd9Sstevel@tonic-gate 		else
23217c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
23227c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
23237c478bd9Sstevel@tonic-gate 
2324e11c3f44Smeem 		/*
2325e11c3f44Smeem 		 * Find out what netmask the interface is going to be using.
2326e11c3f44Smeem 		 * If we just brought up an IPMP data address on an underlying
2327e11c3f44Smeem 		 * interface above, the address will have already migrated, so
2328e11c3f44Smeem 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
2329e11c3f44Smeem 		 * to bring the address up to get the actual netmask).  Just
2330e11c3f44Smeem 		 * omit printing the actual netmask in this corner-case.
2331e11c3f44Smeem 		 */
23327c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2333e11c3f44Smeem 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2334e11c3f44Smeem 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2335e11c3f44Smeem 			    nomatch);
2336e11c3f44Smeem 		} else {
23377c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
2338e11c3f44Smeem 			    "WARNING: %s: %s: %s; using default of %s.",
2339e11c3f44Smeem 			    lifr.lifr_name, nomatch, addrstr4, buffer);
2340e11c3f44Smeem 		}
23417c478bd9Sstevel@tonic-gate 	}
23427c478bd9Sstevel@tonic-gate 
2343de860bd9Sgfaden 	/*
2344de860bd9Sgfaden 	 * If a default router was specified for this interface
2345de860bd9Sgfaden 	 * set the route now. Ignore if already set.
2346de860bd9Sgfaden 	 */
2347de860bd9Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2348de860bd9Sgfaden 		int status;
2349de860bd9Sgfaden 		char *argv[7];
2350de860bd9Sgfaden 
2351de860bd9Sgfaden 		argv[0] = "route";
2352de860bd9Sgfaden 		argv[1] = "add";
2353de860bd9Sgfaden 		argv[2] = "-ifp";
2354de860bd9Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
2355de860bd9Sgfaden 		argv[4] = "default";
2356de860bd9Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2357de860bd9Sgfaden 		argv[6] = NULL;
2358de860bd9Sgfaden 
2359de860bd9Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2360de860bd9Sgfaden 		if (status != 0 && status != EEXIST)
2361de860bd9Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
2362de860bd9Sgfaden 			    "interface %s to %s\n",
2363de860bd9Sgfaden 			    nwiftabptr->zone_nwif_physical,
2364de860bd9Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
2365de860bd9Sgfaden 	}
2366de860bd9Sgfaden 
23677c478bd9Sstevel@tonic-gate 	(void) close(s);
23687c478bd9Sstevel@tonic-gate 	return (Z_OK);
23697c478bd9Sstevel@tonic-gate bad:
23707c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
23717c478bd9Sstevel@tonic-gate 	(void) close(s);
23727c478bd9Sstevel@tonic-gate 	return (-1);
23737c478bd9Sstevel@tonic-gate }
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate /*
23767c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
2377f14f3ae7Sjv227347  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2378f14f3ae7Sjv227347  * system.
23797c478bd9Sstevel@tonic-gate  *
23807c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
23817c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
23827c478bd9Sstevel@tonic-gate  */
23837c478bd9Sstevel@tonic-gate static int
2384f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
23857c478bd9Sstevel@tonic-gate {
23867c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
23877c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
23887c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
23917c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
23927c478bd9Sstevel@tonic-gate 		return (-1);
23937c478bd9Sstevel@tonic-gate 	}
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
23967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
23977c478bd9Sstevel@tonic-gate 		return (-1);
23987c478bd9Sstevel@tonic-gate 	}
23997c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24017c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24027c478bd9Sstevel@tonic-gate 		return (-1);
24037c478bd9Sstevel@tonic-gate 	}
24047c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24057c478bd9Sstevel@tonic-gate 		for (;;) {
24067c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24077c478bd9Sstevel@tonic-gate 				break;
2408f14f3ae7Sjv227347 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24097c478bd9Sstevel@tonic-gate 			    Z_OK) {
24107c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24117c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24127c478bd9Sstevel@tonic-gate 				return (-1);
24137c478bd9Sstevel@tonic-gate 			}
24147c478bd9Sstevel@tonic-gate 		}
24157c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
2418fdb7141fSgfaden 	if (is_system_labeled()) {
2419fdb7141fSgfaden 		/*
2420fdb7141fSgfaden 		 * Labeled zones share the loopback interface
2421fdb7141fSgfaden 		 * so it is not plumbed for shared stack instances.
2422fdb7141fSgfaden 		 */
2423fdb7141fSgfaden 		return (0);
2424fdb7141fSgfaden 	}
24257c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
24267c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
24277c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
24287c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2429442d3e9eSgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2430f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24317c478bd9Sstevel@tonic-gate 		return (-1);
2432f14f3ae7Sjv227347 
2433f14f3ae7Sjv227347 	/* Always plumb up the IPv6 loopback interface. */
24347c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
24357c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2436f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24377c478bd9Sstevel@tonic-gate 		return (-1);
24387c478bd9Sstevel@tonic-gate 	return (0);
24397c478bd9Sstevel@tonic-gate }
24407c478bd9Sstevel@tonic-gate 
244189b1c373Smeem static void
244289b1c373Smeem zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
244389b1c373Smeem {
244489b1c373Smeem 	char errmsg[DLADM_STRSIZE];
244589b1c373Smeem 
244689b1c373Smeem 	(void) dladm_status2str(err, errmsg);
244789b1c373Smeem 	zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
244889b1c373Smeem }
244989b1c373Smeem 
2450f4b3ec61Sdh155122 static int
24512b24ab6bSSebastien Roy add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2452f4b3ec61Sdh155122 {
245389b1c373Smeem 	dladm_status_t err;
24540dc2366fSVenugopal Iyer 	boolean_t cpuset, poolset;
245589b1c373Smeem 
2456f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2457f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2458f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
245989b1c373Smeem 		zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
246089b1c373Smeem 		    "'%s' which is used in the global zone", dlname);
2461f4b3ec61Sdh155122 		return (-1);
2462f4b3ec61Sdh155122 	}
2463f4b3ec61Sdh155122 
2464d62bc4baSyz147064 	/* Set zoneid of this link. */
24652b24ab6bSSebastien Roy 	err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
24662b24ab6bSSebastien Roy 	    DLADM_OPT_ACTIVE);
246789b1c373Smeem 	if (err != DLADM_STATUS_OK) {
246889b1c373Smeem 		zdlerror(zlogp, err, dlname,
246989b1c373Smeem 		    "WARNING: unable to add network interface");
2470f4b3ec61Sdh155122 		return (-1);
2471f4b3ec61Sdh155122 	}
24720dc2366fSVenugopal Iyer 
24730dc2366fSVenugopal Iyer 	/*
24740dc2366fSVenugopal Iyer 	 * Set the pool of this link if the zone has a pool and
24750dc2366fSVenugopal Iyer 	 * neither the cpus nor the pool datalink property is
24760dc2366fSVenugopal Iyer 	 * already set.
24770dc2366fSVenugopal Iyer 	 */
24780dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
24790dc2366fSVenugopal Iyer 	    "cpus", &cpuset);
24800dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
24810dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
24820dc2366fSVenugopal Iyer 		    "WARNING: unable to check if cpus link property is set");
24830dc2366fSVenugopal Iyer 	}
24840dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
24850dc2366fSVenugopal Iyer 	    "pool", &poolset);
24860dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
24870dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
24880dc2366fSVenugopal Iyer 		    "WARNING: unable to check if pool link property is set");
24890dc2366fSVenugopal Iyer 	}
24900dc2366fSVenugopal Iyer 
24910dc2366fSVenugopal Iyer 	if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
24920dc2366fSVenugopal Iyer 		err = dladm_set_linkprop(dld_handle, linkid, "pool",
24930dc2366fSVenugopal Iyer 		    &pool_name, 1, DLADM_OPT_ACTIVE);
24940dc2366fSVenugopal Iyer 		if (err != DLADM_STATUS_OK) {
24950dc2366fSVenugopal Iyer 			zerror(zlogp, B_FALSE, "WARNING: unable to set "
24960dc2366fSVenugopal Iyer 			    "pool %s to datalink %s", pool_name, dlname);
24970dc2366fSVenugopal Iyer 			bzero(pool_name, MAXPATHLEN);
24980dc2366fSVenugopal Iyer 		}
24990dc2366fSVenugopal Iyer 	} else {
25000dc2366fSVenugopal Iyer 		bzero(pool_name, MAXPATHLEN);
25010dc2366fSVenugopal Iyer 	}
2502f4b3ec61Sdh155122 	return (0);
2503f4b3ec61Sdh155122 }
2504f4b3ec61Sdh155122 
2505f4b3ec61Sdh155122 /*
2506f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2507f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2508f4b3ec61Sdh155122  * whatever we set up, and return an error.
2509f4b3ec61Sdh155122  */
2510f4b3ec61Sdh155122 static int
2511f4b3ec61Sdh155122 configure_exclusive_network_interfaces(zlog_t *zlogp)
2512f4b3ec61Sdh155122 {
2513f4b3ec61Sdh155122 	zone_dochandle_t handle;
2514f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2515f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2516f4b3ec61Sdh155122 	char path[MAXPATHLEN];
25172b24ab6bSSebastien Roy 	datalink_id_t linkid;
2518f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2519f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2520f4b3ec61Sdh155122 
2521f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2522f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2523f4b3ec61Sdh155122 		return (-1);
2524f4b3ec61Sdh155122 	}
2525f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2526f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2527f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2528f4b3ec61Sdh155122 		return (-1);
2529f4b3ec61Sdh155122 	}
2530f4b3ec61Sdh155122 
2531f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2532f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2533f4b3ec61Sdh155122 		return (0);
2534f4b3ec61Sdh155122 	}
2535f4b3ec61Sdh155122 
2536f4b3ec61Sdh155122 	for (;;) {
2537f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2538f4b3ec61Sdh155122 			break;
2539f4b3ec61Sdh155122 
2540f4b3ec61Sdh155122 		if (prof == NULL) {
2541f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2542f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2543f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2544f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2545f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2546f4b3ec61Sdh155122 				    "unable to determine dev root");
2547f4b3ec61Sdh155122 				return (-1);
2548f4b3ec61Sdh155122 			}
2549f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2550f4b3ec61Sdh155122 			    "/dev");
2551f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2552f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2553f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2554f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2555f4b3ec61Sdh155122 				    "failed to initialize profile");
2556f4b3ec61Sdh155122 				return (-1);
2557f4b3ec61Sdh155122 			}
2558f4b3ec61Sdh155122 		}
2559f4b3ec61Sdh155122 
2560f4b3ec61Sdh155122 		/*
2561d62bc4baSyz147064 		 * Create the /dev entry for backward compatibility.
2562f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2563d62bc4baSyz147064 		 * Note that the zone still boots when the assigned
2564d62bc4baSyz147064 		 * interface is inaccessible, used by others, etc.
2565d62bc4baSyz147064 		 * Also, when vanity naming is used, some interface do
2566d62bc4baSyz147064 		 * do not have corresponding /dev node names (for example,
2567d62bc4baSyz147064 		 * vanity named aggregations).  The /dev entry is not
2568d62bc4baSyz147064 		 * created in that case.  The /dev/net entry is always
2569d62bc4baSyz147064 		 * accessible.
2570f4b3ec61Sdh155122 		 */
25712b24ab6bSSebastien Roy 		if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
25722b24ab6bSSebastien Roy 		    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
25732b24ab6bSSebastien Roy 		    add_datalink(zlogp, zone_name, linkid,
25742b24ab6bSSebastien Roy 		    nwiftab.zone_nwif_physical) == 0) {
25753bc21d0aSAruna Ramakrishna - Sun Microsystems 			added = B_TRUE;
25763bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
2577f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2578f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
25793bc21d0aSAruna Ramakrishna - Sun Microsystems 			zerror(zlogp, B_TRUE, "failed to add network device");
2580f4b3ec61Sdh155122 			return (-1);
2581f4b3ec61Sdh155122 		}
2582d62bc4baSyz147064 	}
2583f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2584f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2585f4b3ec61Sdh155122 
2586f4b3ec61Sdh155122 	if (prof != NULL && added) {
2587f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2588f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
2589f4b3ec61Sdh155122 			return (-1);
2590f4b3ec61Sdh155122 		}
2591f4b3ec61Sdh155122 	}
2592f4b3ec61Sdh155122 	if (prof != NULL)
2593f4b3ec61Sdh155122 		di_prof_fini(prof);
2594f4b3ec61Sdh155122 
2595f4b3ec61Sdh155122 	return (0);
2596f4b3ec61Sdh155122 }
2597f4b3ec61Sdh155122 
2598f4b3ec61Sdh155122 static int
25990dc2366fSVenugopal Iyer remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
26000dc2366fSVenugopal Iyer {
26010dc2366fSVenugopal Iyer 	ushort_t flags;
26020dc2366fSVenugopal Iyer 	zone_iptype_t iptype;
26030dc2366fSVenugopal Iyer 	int i, dlnum = 0;
26040dc2366fSVenugopal Iyer 	datalink_id_t *dllink, *dllinks = NULL;
26050dc2366fSVenugopal Iyer 	dladm_status_t err;
26060dc2366fSVenugopal Iyer 
26070dc2366fSVenugopal Iyer 	if (strlen(pool_name) == 0)
26080dc2366fSVenugopal Iyer 		return (0);
26090dc2366fSVenugopal Iyer 
26100dc2366fSVenugopal Iyer 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
26110dc2366fSVenugopal Iyer 	    sizeof (flags)) < 0) {
26120dc2366fSVenugopal Iyer 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
26130dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to determine "
26140dc2366fSVenugopal Iyer 			    "ip-type");
26150dc2366fSVenugopal Iyer 			return (-1);
26160dc2366fSVenugopal Iyer 		}
26170dc2366fSVenugopal Iyer 	} else {
26180dc2366fSVenugopal Iyer 		if (flags & ZF_NET_EXCL)
26190dc2366fSVenugopal Iyer 			iptype = ZS_EXCLUSIVE;
26200dc2366fSVenugopal Iyer 		else
26210dc2366fSVenugopal Iyer 			iptype = ZS_SHARED;
26220dc2366fSVenugopal Iyer 	}
26230dc2366fSVenugopal Iyer 
26240dc2366fSVenugopal Iyer 	if (iptype == ZS_EXCLUSIVE) {
26250dc2366fSVenugopal Iyer 		/*
26260dc2366fSVenugopal Iyer 		 * Get the datalink count and for each datalink,
26270dc2366fSVenugopal Iyer 		 * attempt to clear the pool property and clear
26280dc2366fSVenugopal Iyer 		 * the pool_name.
26290dc2366fSVenugopal Iyer 		 */
26300dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
26310dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to count network "
26320dc2366fSVenugopal Iyer 			    "interfaces");
26330dc2366fSVenugopal Iyer 			return (-1);
26340dc2366fSVenugopal Iyer 		}
26350dc2366fSVenugopal Iyer 
26360dc2366fSVenugopal Iyer 		if (dlnum == 0)
26370dc2366fSVenugopal Iyer 			return (0);
26380dc2366fSVenugopal Iyer 
26390dc2366fSVenugopal Iyer 		if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
26400dc2366fSVenugopal Iyer 		    == NULL) {
26410dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "memory allocation failed");
26420dc2366fSVenugopal Iyer 			return (-1);
26430dc2366fSVenugopal Iyer 		}
26440dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
26450dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to list network "
26460dc2366fSVenugopal Iyer 			    "interfaces");
26470dc2366fSVenugopal Iyer 			return (-1);
26480dc2366fSVenugopal Iyer 		}
26490dc2366fSVenugopal Iyer 
26500dc2366fSVenugopal Iyer 		bzero(pool_name, MAXPATHLEN);
26510dc2366fSVenugopal Iyer 		for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
26520dc2366fSVenugopal Iyer 			err = dladm_set_linkprop(dld_handle, *dllink, "pool",
26530dc2366fSVenugopal Iyer 			    NULL, 0, DLADM_OPT_ACTIVE);
26540dc2366fSVenugopal Iyer 			if (err != DLADM_STATUS_OK) {
26550dc2366fSVenugopal Iyer 				zerror(zlogp, B_TRUE,
26560dc2366fSVenugopal Iyer 				    "WARNING: unable to clear pool");
26570dc2366fSVenugopal Iyer 			}
26580dc2366fSVenugopal Iyer 		}
26590dc2366fSVenugopal Iyer 		free(dllinks);
26600dc2366fSVenugopal Iyer 	}
26610dc2366fSVenugopal Iyer 	return (0);
26620dc2366fSVenugopal Iyer }
26630dc2366fSVenugopal Iyer 
26640dc2366fSVenugopal Iyer static int
26652b24ab6bSSebastien Roy unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2666f4b3ec61Sdh155122 {
26672b24ab6bSSebastien Roy 	int dlnum = 0;
2668f4b3ec61Sdh155122 
26692b24ab6bSSebastien Roy 	/*
26702b24ab6bSSebastien Roy 	 * The kernel shutdown callback for the dls module should have removed
26712b24ab6bSSebastien Roy 	 * all datalinks from this zone.  If any remain, then there's a
26722b24ab6bSSebastien Roy 	 * problem.
26732b24ab6bSSebastien Roy 	 */
2674f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2675f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2676f4b3ec61Sdh155122 		return (-1);
2677f4b3ec61Sdh155122 	}
26782b24ab6bSSebastien Roy 	if (dlnum != 0) {
26792b24ab6bSSebastien Roy 		zerror(zlogp, B_FALSE,
26802b24ab6bSSebastien Roy 		    "datalinks remain in zone after shutdown");
2681f4b3ec61Sdh155122 		return (-1);
2682f4b3ec61Sdh155122 	}
2683f4b3ec61Sdh155122 	return (0);
2684f4b3ec61Sdh155122 }
2685f4b3ec61Sdh155122 
26867c478bd9Sstevel@tonic-gate static int
26877c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
26887c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
26897c478bd9Sstevel@tonic-gate {
26907c478bd9Sstevel@tonic-gate 	int fd;
26917c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
26927c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
26937c478bd9Sstevel@tonic-gate 	int error;
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
26967c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
26977c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
26987c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
26997c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
27027c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
27037c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
27047c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
27077c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
27087c478bd9Sstevel@tonic-gate 		return (-1);
27097c478bd9Sstevel@tonic-gate 	}
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
27127c478bd9Sstevel@tonic-gate 	(void) close(fd);
27137c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
27147c478bd9Sstevel@tonic-gate 		return (0);
27157c478bd9Sstevel@tonic-gate 	return (-1);
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate static int
27197c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
27207c478bd9Sstevel@tonic-gate {
27217c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
27227c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
27237c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
27247c478bd9Sstevel@tonic-gate 	int error;
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	/*
27277c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
27287c478bd9Sstevel@tonic-gate 	 */
27297c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
27307c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
27317c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
27327c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
27337c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
27367c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
27377c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
27387c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
27397c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
27427c478bd9Sstevel@tonic-gate 		return (error);
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	/*
27457c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
27467c478bd9Sstevel@tonic-gate 	 */
27477c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
27487c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
27497c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
27507c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
27517c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
27547c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
27557c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
27567c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
27577c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
27607c478bd9Sstevel@tonic-gate 		return (error);
27617c478bd9Sstevel@tonic-gate 	return (0);
27627c478bd9Sstevel@tonic-gate }
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate static int
27656cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
2766ffbafc53Scomay {
2767ffbafc53Scomay 	int error = -1;
2768ffbafc53Scomay 	zone_dochandle_t handle;
2769ffbafc53Scomay 	char *privname = NULL;
2770ffbafc53Scomay 
2771ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
2772ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2773ffbafc53Scomay 		return (-1);
2774ffbafc53Scomay 	}
2775ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2776ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
2777ffbafc53Scomay 		zonecfg_fini_handle(handle);
2778ffbafc53Scomay 		return (-1);
2779ffbafc53Scomay 	}
2780ffbafc53Scomay 
27816cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
2782bf1d7e28Sdh155122 		zone_iptype_t	iptype;
2783bf1d7e28Sdh155122 		const char	*curr_iptype;
2784bf1d7e28Sdh155122 
2785bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
2786bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
2787bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
2788bf1d7e28Sdh155122 			return (-1);
2789bf1d7e28Sdh155122 		}
2790bf1d7e28Sdh155122 
2791bf1d7e28Sdh155122 		switch (iptype) {
2792bf1d7e28Sdh155122 		case ZS_SHARED:
2793bf1d7e28Sdh155122 			curr_iptype = "shared";
2794bf1d7e28Sdh155122 			break;
2795bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
2796bf1d7e28Sdh155122 			curr_iptype = "exclusive";
2797bf1d7e28Sdh155122 			break;
2798bf1d7e28Sdh155122 		}
2799bf1d7e28Sdh155122 
2800e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
2801e767a340Sgjelinek 			zonecfg_fini_handle(handle);
28029acbbeafSnn35248 			return (0);
2803e767a340Sgjelinek 		}
28049acbbeafSnn35248 		zerror(zlogp, B_FALSE,
28059acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
28069acbbeafSnn35248 		zonecfg_fini_handle(handle);
28079acbbeafSnn35248 		return (-1);
28089acbbeafSnn35248 	}
28099acbbeafSnn35248 
2810ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
2811ffbafc53Scomay 	case Z_OK:
2812ffbafc53Scomay 		error = 0;
2813ffbafc53Scomay 		break;
2814ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2815ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
2816ffbafc53Scomay 		    "within the zone's privilege set", privname);
2817ffbafc53Scomay 		break;
2818ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2819ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
2820ffbafc53Scomay 		    "from the zone's privilege set", privname);
2821ffbafc53Scomay 		break;
2822ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2823ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
2824ffbafc53Scomay 		    "in the zone's privilege set", privname);
2825ffbafc53Scomay 		break;
2826ffbafc53Scomay 	default:
2827ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
2828ffbafc53Scomay 		    "privilege set");
2829ffbafc53Scomay 		break;
2830ffbafc53Scomay 	}
2831ffbafc53Scomay 
2832ffbafc53Scomay 	free(privname);
2833ffbafc53Scomay 	zonecfg_fini_handle(handle);
2834ffbafc53Scomay 	return (error);
2835ffbafc53Scomay }
2836ffbafc53Scomay 
2837ffbafc53Scomay static int
28387c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
28397c478bd9Sstevel@tonic-gate {
28407c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
28417c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
28427c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
28437c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
28447c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
28457c478bd9Sstevel@tonic-gate 	int error = -1;
28467c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
28477c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
28487c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
2849ff19e029SMenno Lageman 	uint64_t maxlwps;
2850ff19e029SMenno Lageman 	uint64_t maxprocs;
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	*bufp = NULL;
28537c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
28567c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
28577c478bd9Sstevel@tonic-gate 		return (-1);
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
28607c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
28617c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
28627c478bd9Sstevel@tonic-gate 		return (-1);
28637c478bd9Sstevel@tonic-gate 	}
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
28667c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
28677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
28687c478bd9Sstevel@tonic-gate 		goto out;
28697c478bd9Sstevel@tonic-gate 	}
28707c478bd9Sstevel@tonic-gate 
2871ff19e029SMenno Lageman 	/*
2872ff19e029SMenno Lageman 	 * Allow the administrator to control both the maximum number of
2873ff19e029SMenno Lageman 	 * process table slots and the maximum number of lwps with just the
2874ff19e029SMenno Lageman 	 * max-processes property.  If only the max-processes property is set,
2875ff19e029SMenno Lageman 	 * we add a max-lwps property with a limit derived from max-processes.
2876ff19e029SMenno Lageman 	 */
2877ff19e029SMenno Lageman 	if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs)
2878ff19e029SMenno Lageman 	    == Z_OK &&
2879ff19e029SMenno Lageman 	    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps)
2880ff19e029SMenno Lageman 	    == Z_NO_ENTRY) {
2881ff19e029SMenno Lageman 		if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
2882ff19e029SMenno Lageman 		    maxprocs * LWPS_PER_PROCESS) != Z_OK) {
2883ff19e029SMenno Lageman 			zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
2884ff19e029SMenno Lageman 			goto out;
2885ff19e029SMenno Lageman 		}
2886ff19e029SMenno Lageman 	}
2887ff19e029SMenno Lageman 
28887c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
28897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
28907c478bd9Sstevel@tonic-gate 		goto out;
28917c478bd9Sstevel@tonic-gate 	}
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
28947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
28957c478bd9Sstevel@tonic-gate 		goto out;
28967c478bd9Sstevel@tonic-gate 	}
28977c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
28987c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
28997c478bd9Sstevel@tonic-gate 		uint_t i, count;
29007c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
29037c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
29047c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29057c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
29067c478bd9Sstevel@tonic-gate 			continue;
29077c478bd9Sstevel@tonic-gate 		}
29087c478bd9Sstevel@tonic-gate 		count = 0;
29097c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
29107c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
29117c478bd9Sstevel@tonic-gate 			count++;
29127c478bd9Sstevel@tonic-gate 		}
29137c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
29147c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
29157c478bd9Sstevel@tonic-gate 		}
29167c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
29177c478bd9Sstevel@tonic-gate 			goto out;
29187c478bd9Sstevel@tonic-gate 		i = 0;
29197c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
29207c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
29217c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
29227c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
29237c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
29247c478bd9Sstevel@tonic-gate 				goto out;
29257c478bd9Sstevel@tonic-gate 			}
29267c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
29277c478bd9Sstevel@tonic-gate 			    != Z_OK) {
29287c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
29297c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
29307c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
29317c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
29327c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
29337c478bd9Sstevel@tonic-gate 				goto out;
29347c478bd9Sstevel@tonic-gate 			}
29357c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
29367c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
29377c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
29387c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
29397c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
29407c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
29417c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
29427c478bd9Sstevel@tonic-gate 				    name);
29437c478bd9Sstevel@tonic-gate 				goto out;
29447c478bd9Sstevel@tonic-gate 			}
29457c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
29467c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
29477c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29487c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29497c478bd9Sstevel@tonic-gate 				goto out;
29507c478bd9Sstevel@tonic-gate 			}
29517c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
29527c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
29537c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29547c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29557c478bd9Sstevel@tonic-gate 				goto out;
29567c478bd9Sstevel@tonic-gate 			}
29577c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
29587c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
29597c478bd9Sstevel@tonic-gate 			    != 0) {
29607c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29617c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
29627c478bd9Sstevel@tonic-gate 				goto out;
29637c478bd9Sstevel@tonic-gate 			}
29647c478bd9Sstevel@tonic-gate 		}
29657c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29667c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
29677c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
29687c478bd9Sstevel@tonic-gate 		    != 0) {
29697c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
29707c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
29717c478bd9Sstevel@tonic-gate 			goto out;
29727c478bd9Sstevel@tonic-gate 		}
29737c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
29747c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
29757c478bd9Sstevel@tonic-gate 		free(nvlv);
29767c478bd9Sstevel@tonic-gate 		nvlv = NULL;
29777c478bd9Sstevel@tonic-gate 		rctlcount++;
29787c478bd9Sstevel@tonic-gate 	}
29797c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
29827c478bd9Sstevel@tonic-gate 		error = 0;
29837c478bd9Sstevel@tonic-gate 		goto out;
29847c478bd9Sstevel@tonic-gate 	}
29857c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
29867c478bd9Sstevel@tonic-gate 	    != 0) {
29877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
29887c478bd9Sstevel@tonic-gate 		goto out;
29897c478bd9Sstevel@tonic-gate 	}
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 	error = 0;
29927c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
29937c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate out:
29967c478bd9Sstevel@tonic-gate 	free(rctlblk);
29977c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29987c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
29997c478bd9Sstevel@tonic-gate 		free(nvl_packed);
30007c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
30017c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
30027c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
30037c478bd9Sstevel@tonic-gate 		free(nvlv);
30047c478bd9Sstevel@tonic-gate 	if (handle != NULL)
30057c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
30067c478bd9Sstevel@tonic-gate 	return (error);
30077c478bd9Sstevel@tonic-gate }
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate static int
3010c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr)
3011c5cd6260S {
3012c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
3013c5cd6260S 
3014c5cd6260S 	if (query_hook[0] == '\0')
3015c5cd6260S 		return (0);
3016c5cd6260S 
3017c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3018c5cd6260S 	    > sizeof (cmdbuf))
3019c5cd6260S 		return (-1);
3020c5cd6260S 
3021c5cd6260S 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3022c5cd6260S 		return (-1);
3023c5cd6260S 
3024c5cd6260S 	return (0);
3025c5cd6260S }
3026c5cd6260S 
3027c5cd6260S static int
3028fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3029fa9e4066Sahrens {
3030fa9e4066Sahrens 	zone_dochandle_t handle;
3031fa9e4066Sahrens 	struct zone_dstab dstab;
3032fa9e4066Sahrens 	size_t total, offset, len;
3033fa9e4066Sahrens 	int error = -1;
3034e5a17f6aSgjelinek 	char *str = NULL;
3035c5cd6260S 	char *implicit_datasets = NULL;
3036c5cd6260S 	int implicit_len = 0;
3037fa9e4066Sahrens 
3038fa9e4066Sahrens 	*bufp = NULL;
3039fa9e4066Sahrens 	*bufsizep = 0;
3040fa9e4066Sahrens 
3041fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3042fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3043fa9e4066Sahrens 		return (-1);
3044fa9e4066Sahrens 	}
3045fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3046fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3047fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3048fa9e4066Sahrens 		return (-1);
3049fa9e4066Sahrens 	}
3050fa9e4066Sahrens 
3051c5cd6260S 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3052c5cd6260S 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3053c5cd6260S 		goto out;
3054c5cd6260S 	}
3055c5cd6260S 
3056fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3057fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3058fa9e4066Sahrens 		goto out;
3059fa9e4066Sahrens 	}
3060fa9e4066Sahrens 
3061fa9e4066Sahrens 	total = 0;
3062fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3063fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3064fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3065fa9e4066Sahrens 
3066c5cd6260S 	if (implicit_datasets != NULL)
3067c5cd6260S 		implicit_len = strlen(implicit_datasets);
3068c5cd6260S 	if (implicit_len > 0)
3069c5cd6260S 		total += implicit_len + 1;
3070c5cd6260S 
3071fa9e4066Sahrens 	if (total == 0) {
3072fa9e4066Sahrens 		error = 0;
3073fa9e4066Sahrens 		goto out;
3074fa9e4066Sahrens 	}
3075fa9e4066Sahrens 
3076fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3077fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3078fa9e4066Sahrens 		goto out;
3079fa9e4066Sahrens 	}
3080fa9e4066Sahrens 
3081fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3082fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3083fa9e4066Sahrens 		goto out;
3084fa9e4066Sahrens 	}
3085fa9e4066Sahrens 	offset = 0;
3086fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3087fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3088fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3089e5a17f6aSgjelinek 		    total - offset);
3090fa9e4066Sahrens 		offset += len;
3091e5a17f6aSgjelinek 		if (offset < total - 1)
3092fa9e4066Sahrens 			str[offset++] = ',';
3093fa9e4066Sahrens 	}
3094fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3095fa9e4066Sahrens 
3096c5cd6260S 	if (implicit_len > 0)
3097c5cd6260S 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3098c5cd6260S 
3099fa9e4066Sahrens 	error = 0;
3100fa9e4066Sahrens 	*bufp = str;
3101fa9e4066Sahrens 	*bufsizep = total;
3102fa9e4066Sahrens 
3103fa9e4066Sahrens out:
3104fa9e4066Sahrens 	if (error != 0 && str != NULL)
3105fa9e4066Sahrens 		free(str);
3106fa9e4066Sahrens 	if (handle != NULL)
3107fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3108c5cd6260S 	if (implicit_datasets != NULL)
3109c5cd6260S 		free(implicit_datasets);
3110fa9e4066Sahrens 
3111fa9e4066Sahrens 	return (error);
3112fa9e4066Sahrens }
3113fa9e4066Sahrens 
3114fa9e4066Sahrens static int
3115fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3116fa9e4066Sahrens {
3117fa9e4066Sahrens 	zone_dochandle_t handle;
3118fa9e4066Sahrens 	struct zone_dstab dstab;
3119fa9e4066Sahrens 	zfs_handle_t *zhp;
312099653d4eSeschrock 	libzfs_handle_t *hdl;
3121fa9e4066Sahrens 
3122fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3123fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3124fa9e4066Sahrens 		return (-1);
3125fa9e4066Sahrens 	}
3126fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3127fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3128fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3129fa9e4066Sahrens 		return (-1);
3130fa9e4066Sahrens 	}
3131fa9e4066Sahrens 
3132fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3133fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3134fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3135fa9e4066Sahrens 		return (-1);
3136fa9e4066Sahrens 	}
3137fa9e4066Sahrens 
313899653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
313999653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
314099653d4eSeschrock 		zonecfg_fini_handle(handle);
314199653d4eSeschrock 		return (-1);
314299653d4eSeschrock 	}
3143fa9e4066Sahrens 
3144fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3145fa9e4066Sahrens 
314699653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3147fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3148fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3149fa9e4066Sahrens 			    dstab.zone_dataset_name);
3150fa9e4066Sahrens 			zonecfg_fini_handle(handle);
315199653d4eSeschrock 			libzfs_fini(hdl);
3152fa9e4066Sahrens 			return (-1);
3153fa9e4066Sahrens 		}
3154fa9e4066Sahrens 
3155fa9e4066Sahrens 		/*
3156fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3157fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3158fa9e4066Sahrens 		 */
3159fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3160e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3161e9dbad6fSeschrock 		    "on") != 0) {
3162fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3163fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3164fa9e4066Sahrens 			    dstab.zone_dataset_name);
3165fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3166fa9e4066Sahrens 			zfs_close(zhp);
316799653d4eSeschrock 			libzfs_fini(hdl);
3168fa9e4066Sahrens 			return (-1);
3169fa9e4066Sahrens 		}
3170fa9e4066Sahrens 
3171fa9e4066Sahrens 		zfs_close(zhp);
3172fa9e4066Sahrens 	}
3173fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3174fa9e4066Sahrens 
3175fa9e4066Sahrens 	zonecfg_fini_handle(handle);
317699653d4eSeschrock 	libzfs_fini(hdl);
3177fa9e4066Sahrens 
3178fa9e4066Sahrens 	return (0);
3179fa9e4066Sahrens }
3180fa9e4066Sahrens 
318145916cd2Sjpk /*
31824201a95eSRic Aleshire  * Return true if the path is its own zfs file system.  We determine this
31834201a95eSRic Aleshire  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
31844201a95eSRic Aleshire  * if it is a different fs.
31854201a95eSRic Aleshire  */
31864201a95eSRic Aleshire boolean_t
31874201a95eSRic Aleshire is_zonepath_zfs(char *zonepath)
31884201a95eSRic Aleshire {
31894201a95eSRic Aleshire 	int res;
31904201a95eSRic Aleshire 	char *path;
31914201a95eSRic Aleshire 	char *parent;
31924201a95eSRic Aleshire 	struct statvfs64 buf1, buf2;
31934201a95eSRic Aleshire 
31944201a95eSRic Aleshire 	if (statvfs64(zonepath, &buf1) != 0)
31954201a95eSRic Aleshire 		return (B_FALSE);
31964201a95eSRic Aleshire 
31974201a95eSRic Aleshire 	if (strcmp(buf1.f_basetype, "zfs") != 0)
31984201a95eSRic Aleshire 		return (B_FALSE);
31994201a95eSRic Aleshire 
32004201a95eSRic Aleshire 	if ((path = strdup(zonepath)) == NULL)
32014201a95eSRic Aleshire 		return (B_FALSE);
32024201a95eSRic Aleshire 
32034201a95eSRic Aleshire 	parent = dirname(path);
32044201a95eSRic Aleshire 	res = statvfs64(parent, &buf2);
32054201a95eSRic Aleshire 	free(path);
32064201a95eSRic Aleshire 
32074201a95eSRic Aleshire 	if (res != 0)
32084201a95eSRic Aleshire 		return (B_FALSE);
32094201a95eSRic Aleshire 
32104201a95eSRic Aleshire 	if (buf1.f_fsid == buf2.f_fsid)
32114201a95eSRic Aleshire 		return (B_FALSE);
32124201a95eSRic Aleshire 
32134201a95eSRic Aleshire 	return (B_TRUE);
32144201a95eSRic Aleshire }
32154201a95eSRic Aleshire 
32164201a95eSRic Aleshire /*
32174201a95eSRic Aleshire  * Verify the MAC label in the root dataset for the zone.
32184201a95eSRic Aleshire  * If the label exists, it must match the label configured for the zone.
32194201a95eSRic Aleshire  * Otherwise if there's no label on the dataset, create one here.
32204201a95eSRic Aleshire  */
32214201a95eSRic Aleshire 
32224201a95eSRic Aleshire static int
32234201a95eSRic Aleshire validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
32244201a95eSRic Aleshire {
32254201a95eSRic Aleshire 	int		error = -1;
32264201a95eSRic Aleshire 	zfs_handle_t	*zhp;
32274201a95eSRic Aleshire 	libzfs_handle_t	*hdl;
32284201a95eSRic Aleshire 	m_label_t	ds_sl;
32294201a95eSRic Aleshire 	char		zonepath[MAXPATHLEN];
32304201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
32314201a95eSRic Aleshire 
32324201a95eSRic Aleshire 	if (!is_system_labeled())
32334201a95eSRic Aleshire 		return (0);
32344201a95eSRic Aleshire 
32354201a95eSRic Aleshire 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
32364201a95eSRic Aleshire 		zerror(zlogp, B_TRUE, "unable to determine zone path");
32374201a95eSRic Aleshire 		return (-1);
32384201a95eSRic Aleshire 	}
32394201a95eSRic Aleshire 
32404201a95eSRic Aleshire 	if (!is_zonepath_zfs(zonepath))
32414201a95eSRic Aleshire 		return (0);
32424201a95eSRic Aleshire 
32434201a95eSRic Aleshire 	if ((hdl = libzfs_init()) == NULL) {
32444201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "opening ZFS library");
32454201a95eSRic Aleshire 		return (-1);
32464201a95eSRic Aleshire 	}
32474201a95eSRic Aleshire 
32484201a95eSRic Aleshire 	if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
32494201a95eSRic Aleshire 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
32504201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
32514201a95eSRic Aleshire 		    rootpath);
32524201a95eSRic Aleshire 		libzfs_fini(hdl);
32534201a95eSRic Aleshire 		return (-1);
32544201a95eSRic Aleshire 	}
32554201a95eSRic Aleshire 
32564201a95eSRic Aleshire 	/* Get the mlslabel property if it exists. */
32574201a95eSRic Aleshire 	if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
32584201a95eSRic Aleshire 	    NULL, NULL, 0, B_TRUE) != 0) ||
32594201a95eSRic Aleshire 	    (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
32604201a95eSRic Aleshire 		char		*str2 = NULL;
32614201a95eSRic Aleshire 
32624201a95eSRic Aleshire 		/*
32634201a95eSRic Aleshire 		 * No label on the dataset (or default only); create one.
32644201a95eSRic Aleshire 		 * (Only do this automatic labeling for the labeled brand.)
32654201a95eSRic Aleshire 		 */
32664201a95eSRic Aleshire 		if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
32674201a95eSRic Aleshire 			error = 0;
32684201a95eSRic Aleshire 			goto out;
32694201a95eSRic Aleshire 		}
32704201a95eSRic Aleshire 
32714201a95eSRic Aleshire 		error = l_to_str_internal(zone_sl, &str2);
32724201a95eSRic Aleshire 		if (error)
32734201a95eSRic Aleshire 			goto out;
32744201a95eSRic Aleshire 		if (str2 == NULL) {
32754201a95eSRic Aleshire 			error = -1;
32764201a95eSRic Aleshire 			goto out;
32774201a95eSRic Aleshire 		}
32784201a95eSRic Aleshire 		if ((error = zfs_prop_set(zhp,
32794201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
32804201a95eSRic Aleshire 			zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
32814201a95eSRic Aleshire 			    "property for root dataset at '%s'\n", rootpath);
32824201a95eSRic Aleshire 		}
32834201a95eSRic Aleshire 		free(str2);
32844201a95eSRic Aleshire 		goto out;
32854201a95eSRic Aleshire 	}
32864201a95eSRic Aleshire 
32874201a95eSRic Aleshire 	/* Convert the retrieved dataset label to binary form. */
32884201a95eSRic Aleshire 	error = hexstr_to_label(ds_hexsl, &ds_sl);
32894201a95eSRic Aleshire 	if (error) {
32904201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
32914201a95eSRic Aleshire 		    "property on root dataset at '%s'\n", rootpath);
32924201a95eSRic Aleshire 		goto out;			/* exit with error */
32934201a95eSRic Aleshire 	}
32944201a95eSRic Aleshire 
32954201a95eSRic Aleshire 	/*
32964201a95eSRic Aleshire 	 * Perform a MAC check by comparing the zone label with the
32974201a95eSRic Aleshire 	 * dataset label.
32984201a95eSRic Aleshire 	 */
32994201a95eSRic Aleshire 	error = (!blequal(zone_sl, &ds_sl));
33004201a95eSRic Aleshire 	if (error)
33014201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
33024201a95eSRic Aleshire out:
33034201a95eSRic Aleshire 	zfs_close(zhp);
33044201a95eSRic Aleshire 	libzfs_fini(hdl);
33054201a95eSRic Aleshire 
33064201a95eSRic Aleshire 	return (error);
33074201a95eSRic Aleshire }
33084201a95eSRic Aleshire 
33094201a95eSRic Aleshire /*
331045916cd2Sjpk  * Mount lower level home directories into/from current zone
331145916cd2Sjpk  * Share exported directories specified in dfstab for zone
331245916cd2Sjpk  */
331345916cd2Sjpk static int
331445916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
331545916cd2Sjpk {
331645916cd2Sjpk 	zoneid_t *zids = NULL;
331745916cd2Sjpk 	priv_set_t *zid_privs;
331845916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
331945916cd2Sjpk 	uint_t nzents_saved;
332045916cd2Sjpk 	uint_t nzents;
332145916cd2Sjpk 	int i;
332245916cd2Sjpk 	char readonly[] = "ro";
332345916cd2Sjpk 	struct zone_fstab lower_fstab;
332445916cd2Sjpk 	char *argv[4];
332545916cd2Sjpk 
332645916cd2Sjpk 	if (!is_system_labeled())
332745916cd2Sjpk 		return (0);
332845916cd2Sjpk 
332945916cd2Sjpk 	if (zid_label == NULL) {
333045916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
333145916cd2Sjpk 		if (zid_label == NULL)
333245916cd2Sjpk 			return (-1);
333345916cd2Sjpk 	}
333445916cd2Sjpk 
333545916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
333645916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
333727e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
333845916cd2Sjpk 
333945916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
334045916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
334145916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
334245916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
334345916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
334445916cd2Sjpk 
334545916cd2Sjpk 	/*
334645916cd2Sjpk 	 * Get the list of zones from the kernel
334745916cd2Sjpk 	 */
334845916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
334945916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
335045916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
335145916cd2Sjpk 		return (-1);
335245916cd2Sjpk 	}
335345916cd2Sjpk again:
335445916cd2Sjpk 	if (nzents == 0) {
335545916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
335645916cd2Sjpk 		return (-1);
335745916cd2Sjpk 	}
335845916cd2Sjpk 
335945916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
336045916cd2Sjpk 	if (zids == NULL) {
33613f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
336245916cd2Sjpk 		return (-1);
336345916cd2Sjpk 	}
336445916cd2Sjpk 	nzents_saved = nzents;
336545916cd2Sjpk 
336645916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
336745916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
336845916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
336945916cd2Sjpk 		free(zids);
337045916cd2Sjpk 		return (-1);
337145916cd2Sjpk 	}
337245916cd2Sjpk 	if (nzents != nzents_saved) {
337345916cd2Sjpk 		/* list changed, try again */
337445916cd2Sjpk 		free(zids);
337545916cd2Sjpk 		goto again;
337645916cd2Sjpk 	}
337745916cd2Sjpk 
337845916cd2Sjpk 	ip = getprivimplinfo();
337945916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
338045916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
338145916cd2Sjpk 		zonecfg_free_fs_option_list(
338245916cd2Sjpk 		    lower_fstab.zone_fs_options);
338345916cd2Sjpk 		free(zids);
338445916cd2Sjpk 		return (-1);
338545916cd2Sjpk 	}
338645916cd2Sjpk 
338745916cd2Sjpk 	for (i = 0; i < nzents; i++) {
338845916cd2Sjpk 		char zid_name[ZONENAME_MAX];
338945916cd2Sjpk 		zone_state_t zid_state;
339045916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
339145916cd2Sjpk 		struct stat stat_buf;
339245916cd2Sjpk 
339345916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
339445916cd2Sjpk 			continue;
339545916cd2Sjpk 
339645916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
339745916cd2Sjpk 			continue;
339845916cd2Sjpk 
339945916cd2Sjpk 		/*
340045916cd2Sjpk 		 * Do special setup for the zone we are booting
340145916cd2Sjpk 		 */
340245916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
340345916cd2Sjpk 			struct zone_fstab autofs_fstab;
340445916cd2Sjpk 			char map_path[MAXPATHLEN];
340545916cd2Sjpk 			int fd;
340645916cd2Sjpk 
340745916cd2Sjpk 			/*
340845916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
34099acbbeafSnn35248 			 * in the global zone. The non-global zone entry
341045916cd2Sjpk 			 * will be created by automount when the zone
341145916cd2Sjpk 			 * is booted.
341245916cd2Sjpk 			 */
341345916cd2Sjpk 
341445916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
341545916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
341645916cd2Sjpk 
341745916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
341845916cd2Sjpk 			    "/zone/%s/home", zid_name);
341945916cd2Sjpk 
342045916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
342145916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
342245916cd2Sjpk 			/*
342345916cd2Sjpk 			 * If the map file doesn't exist create a template
342445916cd2Sjpk 			 */
342545916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
342645916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
342745916cd2Sjpk 				int len;
342845916cd2Sjpk 				char map_rec[MAXPATHLEN];
342945916cd2Sjpk 
343045916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
343145916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
343245916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
343345916cd2Sjpk 				(void) write(fd, map_rec, len);
343445916cd2Sjpk 				(void) close(fd);
343545916cd2Sjpk 			}
343645916cd2Sjpk 
343745916cd2Sjpk 			/*
343845916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
343945916cd2Sjpk 			 * If it's already of type autofs, then
344045916cd2Sjpk 			 * don't mount it again.
344145916cd2Sjpk 			 */
344245916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
344345916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
344445916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
344545916cd2Sjpk 
344645916cd2Sjpk 				(void) make_one_dir(zlogp, "",
344727e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
344827e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
344945916cd2Sjpk 
345045916cd2Sjpk 				/*
345145916cd2Sjpk 				 * Mount will fail if automounter has already
345245916cd2Sjpk 				 * processed the auto_home_<zonename> map
345345916cd2Sjpk 				 */
345445916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
345545916cd2Sjpk 				    autofs_fstab.zone_fs_special,
345645916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
345745916cd2Sjpk 			}
345845916cd2Sjpk 			continue;
345945916cd2Sjpk 		}
346045916cd2Sjpk 
346145916cd2Sjpk 
346245916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
346348451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
346448451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
346545916cd2Sjpk 			/* Skip over zones without mounted filesystems */
346645916cd2Sjpk 			continue;
346745916cd2Sjpk 
346845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
346945916cd2Sjpk 		    sizeof (m_label_t)) < 0)
347045916cd2Sjpk 			/* Skip over zones with unspecified label */
347145916cd2Sjpk 			continue;
347245916cd2Sjpk 
347345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
347445916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
347545916cd2Sjpk 			/* Skip over zones with bad path */
347645916cd2Sjpk 			continue;
347745916cd2Sjpk 
347845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
347945916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
348045916cd2Sjpk 			/* Skip over zones with bad privs */
348145916cd2Sjpk 			continue;
348245916cd2Sjpk 
348345916cd2Sjpk 		/*
348445916cd2Sjpk 		 * Reading down is valid according to our label model
348545916cd2Sjpk 		 * but some customers want to disable it because it
348645916cd2Sjpk 		 * allows execute down and other possible attacks.
348745916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
348845916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
348945916cd2Sjpk 		 * for NFS read-down semantics.
349045916cd2Sjpk 		 */
349145916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
349245916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
349345916cd2Sjpk 			/*
349445916cd2Sjpk 			 * Our zone dominates this one.
349545916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
349645916cd2Sjpk 			 */
349745916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
349845916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
349945916cd2Sjpk 
350045916cd2Sjpk 			/*
350145916cd2Sjpk 			 * If the target is already an LOFS mount
350245916cd2Sjpk 			 * then don't do it again.
350345916cd2Sjpk 			 */
350445916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
350545916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
350645916cd2Sjpk 
350745916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
350845916cd2Sjpk 				    MAXPATHLEN, "%s/export",
350945916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
351045916cd2Sjpk 					continue;
351145916cd2Sjpk 
351245916cd2Sjpk 				/*
351345916cd2Sjpk 				 * Make sure the lower-level home exists
351445916cd2Sjpk 				 */
351545916cd2Sjpk 				if (make_one_dir(zlogp,
351627e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
351727e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
351827e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
351945916cd2Sjpk 					continue;
352045916cd2Sjpk 
352145916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
352245916cd2Sjpk 				    "/home", MAXPATHLEN);
352345916cd2Sjpk 
352445916cd2Sjpk 				/*
352545916cd2Sjpk 				 * Mount can fail because the lower-level
352645916cd2Sjpk 				 * zone may have already done a mount up.
352745916cd2Sjpk 				 */
35280679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
35290679f794S 				    Z_MNT_BOOT);
353045916cd2Sjpk 			}
353145916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
353245916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
353345916cd2Sjpk 			/*
353445916cd2Sjpk 			 * This zone dominates our zone.
353545916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
353645916cd2Sjpk 			 */
353745916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
353845916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
353945916cd2Sjpk 			    zone_name) > MAXPATHLEN)
354045916cd2Sjpk 				continue;
354145916cd2Sjpk 
354245916cd2Sjpk 			/*
354345916cd2Sjpk 			 * If the target is already an LOFS mount
354445916cd2Sjpk 			 * then don't do it again.
354545916cd2Sjpk 			 */
354645916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
354745916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
354845916cd2Sjpk 
354945916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
355045916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
355145916cd2Sjpk 
355245916cd2Sjpk 				/*
355345916cd2Sjpk 				 * Mount can fail because the higher-level
355445916cd2Sjpk 				 * zone may have already done a mount down.
355545916cd2Sjpk 				 */
35560679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
35570679f794S 				    Z_MNT_BOOT);
355845916cd2Sjpk 			}
355945916cd2Sjpk 		}
356045916cd2Sjpk 	}
356145916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
356245916cd2Sjpk 	priv_freeset(zid_privs);
356345916cd2Sjpk 	free(zids);
356445916cd2Sjpk 
356545916cd2Sjpk 	/*
356645916cd2Sjpk 	 * Now share any exported directories from this zone.
356745916cd2Sjpk 	 * Each zone can have its own dfstab.
356845916cd2Sjpk 	 */
356945916cd2Sjpk 
357045916cd2Sjpk 	argv[0] = "zoneshare";
357145916cd2Sjpk 	argv[1] = "-z";
357245916cd2Sjpk 	argv[2] = zone_name;
357345916cd2Sjpk 	argv[3] = NULL;
357445916cd2Sjpk 
357545916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
357645916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
357745916cd2Sjpk 
357845916cd2Sjpk 	return (0);
357945916cd2Sjpk }
358045916cd2Sjpk 
358145916cd2Sjpk /*
358245916cd2Sjpk  * Unmount lofs mounts from higher level zones
358345916cd2Sjpk  * Unshare nfs exported directories
358445916cd2Sjpk  */
358545916cd2Sjpk static void
358645916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
358745916cd2Sjpk {
358845916cd2Sjpk 	zoneid_t *zids = NULL;
358945916cd2Sjpk 	uint_t nzents_saved;
359045916cd2Sjpk 	uint_t nzents;
359145916cd2Sjpk 	int i;
359245916cd2Sjpk 	char *argv[4];
359345916cd2Sjpk 	char path[MAXPATHLEN];
359445916cd2Sjpk 
359545916cd2Sjpk 	if (!is_system_labeled())
359645916cd2Sjpk 		return;
359745916cd2Sjpk 
359845916cd2Sjpk 	/*
359945916cd2Sjpk 	 * Get the list of zones from the kernel
360045916cd2Sjpk 	 */
360145916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
360245916cd2Sjpk 		return;
360345916cd2Sjpk 	}
360445916cd2Sjpk 
360545916cd2Sjpk 	if (zid_label == NULL) {
360645916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
360745916cd2Sjpk 		if (zid_label == NULL)
360845916cd2Sjpk 			return;
360945916cd2Sjpk 	}
361045916cd2Sjpk 
361145916cd2Sjpk again:
361245916cd2Sjpk 	if (nzents == 0)
361345916cd2Sjpk 		return;
361445916cd2Sjpk 
361545916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
361645916cd2Sjpk 	if (zids == NULL) {
36173f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
361845916cd2Sjpk 		return;
361945916cd2Sjpk 	}
362045916cd2Sjpk 	nzents_saved = nzents;
362145916cd2Sjpk 
362245916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
362345916cd2Sjpk 		free(zids);
362445916cd2Sjpk 		return;
362545916cd2Sjpk 	}
362645916cd2Sjpk 	if (nzents != nzents_saved) {
362745916cd2Sjpk 		/* list changed, try again */
362845916cd2Sjpk 		free(zids);
362945916cd2Sjpk 		goto again;
363045916cd2Sjpk 	}
363145916cd2Sjpk 
363245916cd2Sjpk 	for (i = 0; i < nzents; i++) {
363345916cd2Sjpk 		char zid_name[ZONENAME_MAX];
363445916cd2Sjpk 		zone_state_t zid_state;
363545916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
363645916cd2Sjpk 
363745916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
363845916cd2Sjpk 			continue;
363945916cd2Sjpk 
364045916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
364145916cd2Sjpk 			continue;
364245916cd2Sjpk 
364345916cd2Sjpk 		/*
364445916cd2Sjpk 		 * Skip the zone we are halting
364545916cd2Sjpk 		 */
364645916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
364745916cd2Sjpk 			continue;
364845916cd2Sjpk 
364945916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
365045916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
365145916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
365245916cd2Sjpk 			/* Skip over zones without mounted filesystems */
365345916cd2Sjpk 			continue;
365445916cd2Sjpk 
365545916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
365645916cd2Sjpk 		    sizeof (m_label_t)) < 0)
365745916cd2Sjpk 			/* Skip over zones with unspecified label */
365845916cd2Sjpk 			continue;
365945916cd2Sjpk 
366045916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
366145916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
366245916cd2Sjpk 			/* Skip over zones with bad path */
366345916cd2Sjpk 			continue;
366445916cd2Sjpk 
366545916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
366645916cd2Sjpk 			/*
366745916cd2Sjpk 			 * This zone dominates our zone.
366845916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
366945916cd2Sjpk 			 */
367045916cd2Sjpk 
367145916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
367245916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
367345916cd2Sjpk 			    zone_name) > MAXPATHLEN)
367445916cd2Sjpk 				continue;
367545916cd2Sjpk 
367645916cd2Sjpk 			/* Skip over mount failures */
367745916cd2Sjpk 			(void) umount(path);
367845916cd2Sjpk 		}
367945916cd2Sjpk 	}
368045916cd2Sjpk 	free(zids);
368145916cd2Sjpk 
368245916cd2Sjpk 	/*
368345916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
368445916cd2Sjpk 	 */
368545916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
368645916cd2Sjpk 	/* Skip over mount failures */
368745916cd2Sjpk 	(void) umount(path);
368845916cd2Sjpk 
368945916cd2Sjpk 	/*
369045916cd2Sjpk 	 * Next unshare any exported directories from this zone.
369145916cd2Sjpk 	 */
369245916cd2Sjpk 
369345916cd2Sjpk 	argv[0] = "zoneunshare";
369445916cd2Sjpk 	argv[1] = "-z";
369545916cd2Sjpk 	argv[2] = zone_name;
369645916cd2Sjpk 	argv[3] = NULL;
369745916cd2Sjpk 
369845916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
369945916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
370045916cd2Sjpk 
370145916cd2Sjpk 	/*
370245916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
370345916cd2Sjpk 	 */
370445916cd2Sjpk 
370545916cd2Sjpk 	argv[0] = "deallocate";
370645916cd2Sjpk 	argv[1] = "-Isz";
370745916cd2Sjpk 	argv[2] = zone_name;
370845916cd2Sjpk 	argv[3] = NULL;
370945916cd2Sjpk 
371045916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
371145916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
371245916cd2Sjpk }
371345916cd2Sjpk 
371445916cd2Sjpk /*
371545916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
371645916cd2Sjpk  * this zone.
371745916cd2Sjpk  */
371845916cd2Sjpk static tsol_zcent_t *
371945916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
372045916cd2Sjpk {
372145916cd2Sjpk 	FILE *fp;
372245916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
372345916cd2Sjpk 	char line[MAXTNZLEN];
372445916cd2Sjpk 
372545916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
372645916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
372745916cd2Sjpk 		return (NULL);
372845916cd2Sjpk 	}
372945916cd2Sjpk 
373045916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
373145916cd2Sjpk 		/*
373245916cd2Sjpk 		 * Check for malformed database
373345916cd2Sjpk 		 */
373445916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
373545916cd2Sjpk 			break;
373645916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
373745916cd2Sjpk 			continue;
373845916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
373945916cd2Sjpk 			break;
374045916cd2Sjpk 		tsol_freezcent(zcent);
374145916cd2Sjpk 		zcent = NULL;
374245916cd2Sjpk 	}
374345916cd2Sjpk 	(void) fclose(fp);
374445916cd2Sjpk 
374545916cd2Sjpk 	if (zcent == NULL) {
374645916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
374745916cd2Sjpk 		    "See tnzonecfg(4)");
374845916cd2Sjpk 	} else {
374945916cd2Sjpk 		if (zlabel == NULL)
375045916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
375145916cd2Sjpk 		/*
375245916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
375345916cd2Sjpk 		 */
375445916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
375545916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
375645916cd2Sjpk 			return (NULL);
375745916cd2Sjpk 		} else {
375845916cd2Sjpk 			priv_copyset(privs, zprivs);
375945916cd2Sjpk 		}
376045916cd2Sjpk 	}
376145916cd2Sjpk 	return (zcent);
376245916cd2Sjpk }
376345916cd2Sjpk 
376445916cd2Sjpk /*
376545916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
376645916cd2Sjpk  */
376745916cd2Sjpk static void
376845916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
376945916cd2Sjpk {
377045916cd2Sjpk 	tsol_mlp_t *mlp;
377145916cd2Sjpk 	tsol_mlpent_t tsme;
377245916cd2Sjpk 
377345916cd2Sjpk 	if (!is_system_labeled())
377445916cd2Sjpk 		return;
377545916cd2Sjpk 
377645916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
377745916cd2Sjpk 	tsme.tsme_flags = 0;
377845916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
377945916cd2Sjpk 		tsme.tsme_mlp = *mlp;
378045916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
378145916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
378245916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
378345916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
378445916cd2Sjpk 		}
378545916cd2Sjpk 	}
378645916cd2Sjpk 
378745916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
378845916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
378945916cd2Sjpk 		tsme.tsme_mlp = *mlp;
379045916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
379145916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
379245916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
379345916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
379445916cd2Sjpk 		}
379545916cd2Sjpk 	}
379645916cd2Sjpk }
379745916cd2Sjpk 
379845916cd2Sjpk static void
379945916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
380045916cd2Sjpk {
380145916cd2Sjpk 	tsol_mlpent_t tsme;
380245916cd2Sjpk 
380345916cd2Sjpk 	if (!is_system_labeled())
380445916cd2Sjpk 		return;
380545916cd2Sjpk 
380645916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
380745916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
380845916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
380945916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
381045916cd2Sjpk }
381145916cd2Sjpk 
38127c478bd9Sstevel@tonic-gate int
38130094b373Sjv227347 prtmount(const struct mnttab *fs, void *x) {
38140094b373Sjv227347 	zerror((zlog_t *)x, B_FALSE, "  %s", fs->mnt_mountp);
38157c478bd9Sstevel@tonic-gate 	return (0);
38167c478bd9Sstevel@tonic-gate }
38177c478bd9Sstevel@tonic-gate 
3818108322fbScarlsonj /*
3819108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
3820108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3821108322fbScarlsonj  * is found or if we can't tell.
3822108322fbScarlsonj  */
3823108322fbScarlsonj static boolean_t
3824108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
38257c478bd9Sstevel@tonic-gate {
3826108322fbScarlsonj 	zoneid_t *zids = NULL;
3827108322fbScarlsonj 	uint_t nzids = 0;
3828108322fbScarlsonj 	boolean_t retv;
3829108322fbScarlsonj 	int rlen, zlen;
3830108322fbScarlsonj 	char zroot[MAXPATHLEN];
3831108322fbScarlsonj 	char zonename[ZONENAME_MAX];
3832108322fbScarlsonj 
3833108322fbScarlsonj 	for (;;) {
3834108322fbScarlsonj 		nzids += 10;
3835108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
3836108322fbScarlsonj 		if (zids == NULL) {
38373f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3838108322fbScarlsonj 			return (B_TRUE);
3839108322fbScarlsonj 		}
3840108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
3841108322fbScarlsonj 			break;
3842108322fbScarlsonj 		free(zids);
3843108322fbScarlsonj 	}
3844108322fbScarlsonj 	retv = B_FALSE;
3845108322fbScarlsonj 	rlen = strlen(rootpath);
3846108322fbScarlsonj 	while (nzids > 0) {
3847108322fbScarlsonj 		/*
3848108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3849108322fbScarlsonj 		 * while we were busy.
3850108322fbScarlsonj 		 */
3851108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3852108322fbScarlsonj 		    sizeof (zroot)) == -1)
3853108322fbScarlsonj 			continue;
3854108322fbScarlsonj 		zlen = strlen(zroot);
3855108322fbScarlsonj 		if (zlen > rlen)
3856108322fbScarlsonj 			zlen = rlen;
3857108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3858108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3859108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3860108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3861108322fbScarlsonj 			    sizeof (zonename)) == -1)
3862108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3863108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
3864108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3865108322fbScarlsonj 			    "zone root %s already in use by zone %s",
3866108322fbScarlsonj 			    rootpath, zonename);
3867108322fbScarlsonj 			retv = B_TRUE;
3868108322fbScarlsonj 			break;
3869108322fbScarlsonj 		}
3870108322fbScarlsonj 	}
3871108322fbScarlsonj 	free(zids);
3872108322fbScarlsonj 	return (retv);
3873108322fbScarlsonj }
3874108322fbScarlsonj 
3875108322fbScarlsonj /*
3876108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
3877108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3878108322fbScarlsonj  */
3879108322fbScarlsonj static boolean_t
3880108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3881108322fbScarlsonj {
3882108322fbScarlsonj 	struct stat64 rst, zst;
3883108322fbScarlsonj 	struct mnttab *mnp;
3884108322fbScarlsonj 
3885108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
3886108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3887108322fbScarlsonj 		return (B_TRUE);
3888108322fbScarlsonj 	}
3889108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3890108322fbScarlsonj 		return (B_TRUE);
3891108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3892108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
3893108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3894108322fbScarlsonj 			continue;
3895108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3896108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
3897108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3898108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3899108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3900108322fbScarlsonj 			    "zone root %s is reachable through %s",
3901108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
3902108322fbScarlsonj 			return (B_TRUE);
3903108322fbScarlsonj 		}
3904108322fbScarlsonj 	}
3905108322fbScarlsonj 	return (B_FALSE);
3906108322fbScarlsonj }
3907108322fbScarlsonj 
39080209230bSgjelinek /*
39090209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
39100209230bSgjelinek  * configuration.
39110209230bSgjelinek  */
39120209230bSgjelinek static int
39130209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
39140209230bSgjelinek {
39150209230bSgjelinek 	int res;
39160209230bSgjelinek 	uint64_t tmp;
39170209230bSgjelinek 	struct zone_mcaptab mcap;
39180209230bSgjelinek 	char sched[MAXNAMELEN];
39190209230bSgjelinek 	zone_dochandle_t handle = NULL;
39200209230bSgjelinek 	char pool_err[128];
39210209230bSgjelinek 
39220209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
39230209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
39240209230bSgjelinek 		return (Z_BAD_HANDLE);
39250209230bSgjelinek 	}
39260209230bSgjelinek 
39270209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
39280209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
39290209230bSgjelinek 		zonecfg_fini_handle(handle);
39300209230bSgjelinek 		return (res);
39310209230bSgjelinek 	}
39320209230bSgjelinek 
39330209230bSgjelinek 	/*
39340209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
39350209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
39360209230bSgjelinek 	 */
39370209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
39380209230bSgjelinek 		uint64_t num;
39390209230bSgjelinek 		char smf_err[128];
39400209230bSgjelinek 
39410209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
39420209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
39430209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
39440209230bSgjelinek 			zonecfg_fini_handle(handle);
39450209230bSgjelinek 			return (Z_INVAL);
39460209230bSgjelinek 		}
39470209230bSgjelinek 
39480209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
39490209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
39500209230bSgjelinek 			    "failed: %s", smf_err);
39510209230bSgjelinek 			zonecfg_fini_handle(handle);
39520209230bSgjelinek 			return (Z_INVAL);
39530209230bSgjelinek 		}
39540209230bSgjelinek 	}
39550209230bSgjelinek 
39560209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
39570209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
39580209230bSgjelinek 	    strlen(sched) > 0) {
39590209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
39600209230bSgjelinek 		    strlen(sched)) == -1)
39610209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
39620209230bSgjelinek 			    "default scheduling class");
39630209230bSgjelinek 
39640209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
39650209230bSgjelinek 	    == Z_OK) {
39660209230bSgjelinek 		/*
39670209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
39680209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
39690209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
39700209230bSgjelinek 		 * in by default so we can print a warning and modify the class
39710209230bSgjelinek 		 * if we wouldn't be using FSS.
39720209230bSgjelinek 		 */
39730209230bSgjelinek 		char class_name[PC_CLNMSZ];
39740209230bSgjelinek 
39750209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
39760209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
39770209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
39780209230bSgjelinek 			    "the zone's scheduling class");
39790209230bSgjelinek 
39800209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
39810209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
39820209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
39830209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
39840209230bSgjelinek 			    "used for processes\nin the zone but to get the "
39850209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
39860209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
39870209230bSgjelinek 			    "details.");
39880209230bSgjelinek 
39890209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
39900209230bSgjelinek 			    strlen("FSS")) == -1)
39910209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
39920209230bSgjelinek 				    "zone scheduling class to FSS");
39930209230bSgjelinek 		}
39940209230bSgjelinek 	}
39950209230bSgjelinek 
39960209230bSgjelinek 	/*
39970209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
39980209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
39990209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
40000209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
40010209230bSgjelinek 	 * and just return if there is nothing to do.
40020209230bSgjelinek 	 *
40030209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
40040209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
40050209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
40060209230bSgjelinek 	 * zonecfg.
40070209230bSgjelinek 	 */
40080209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
40090209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
40100209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
40110209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
40120209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
40130209230bSgjelinek 			    pool_err);
40140209230bSgjelinek 		else
40150209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
40160209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
40170209230bSgjelinek 		zonecfg_fini_handle(handle);
40180209230bSgjelinek 		return (Z_POOL_BIND);
40190209230bSgjelinek 	}
40200209230bSgjelinek 
40210209230bSgjelinek 	/*
40220209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
40230209230bSgjelinek 	 */
40240209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
40250209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
40260209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
40270209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
40280209230bSgjelinek 		    "processor allocation within the specified range\n"
40290209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
40300209230bSgjelinek 		    "See poold(1M).");
40310209230bSgjelinek 	}
40320209230bSgjelinek 
40330209230bSgjelinek 	/* The following is a warning, not an error. */
40340209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
40350209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
40360209230bSgjelinek 		if (res == Z_POOL_BIND)
40370209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
40380209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
40390209230bSgjelinek 		else if (res == Z_POOL)
40400209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
40410209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
40420209230bSgjelinek 		else
40430209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
40440209230bSgjelinek 			    zonecfg_strerror(res));
40450209230bSgjelinek 	}
40460dc2366fSVenugopal Iyer 	(void) zonecfg_get_poolname(handle, zone_name, pool_name, MAXPATHLEN);
40470209230bSgjelinek 
40480209230bSgjelinek 	zonecfg_fini_handle(handle);
40490209230bSgjelinek 	return (Z_OK);
40500209230bSgjelinek }
40510209230bSgjelinek 
40520fbb751dSJohn Levon static void
40530fbb751dSJohn Levon report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
40540fbb751dSJohn Levon {
40550fbb751dSJohn Levon 	switch (res) {
40560fbb751dSJohn Levon 	case Z_TOO_BIG:
40570fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value is too large.", name);
40580fbb751dSJohn Levon 		break;
40590fbb751dSJohn Levon 
40600fbb751dSJohn Levon 	case Z_INVALID_PROPERTY:
40610fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
40620fbb751dSJohn Levon 		    name, value);
40630fbb751dSJohn Levon 		break;
40640fbb751dSJohn Levon 
40650fbb751dSJohn Levon 	default:
40660fbb751dSJohn Levon 		zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
40670fbb751dSJohn Levon 		break;
40680fbb751dSJohn Levon 	}
40690fbb751dSJohn Levon }
40700fbb751dSJohn Levon 
40715679c89fSjv227347 /*
40725679c89fSjv227347  * Sets the hostid of the new zone based on its configured value.  The zone's
40735679c89fSjv227347  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
40745679c89fSjv227347  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
40755679c89fSjv227347  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
40765679c89fSjv227347  * ID of the new zone.
40775679c89fSjv227347  *
40785679c89fSjv227347  * This function returns zero on success and a nonzero error code on failure.
40795679c89fSjv227347  */
40805679c89fSjv227347 static int
40810fbb751dSJohn Levon setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
40825679c89fSjv227347 {
40835679c89fSjv227347 	int res;
40845679c89fSjv227347 	char hostidp[HW_HOSTID_LEN];
40855679c89fSjv227347 	unsigned int hostid;
40865679c89fSjv227347 
40870fbb751dSJohn Levon 	res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
40880fbb751dSJohn Levon 
40890fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
40900fbb751dSJohn Levon 		return (Z_OK);
40910fbb751dSJohn Levon 	} else if (res != Z_OK) {
40920fbb751dSJohn Levon 		report_prop_err(zlogp, "hostid", hostidp, res);
40930fbb751dSJohn Levon 		return (res);
40940fbb751dSJohn Levon 	}
40950fbb751dSJohn Levon 
40960fbb751dSJohn Levon 	hostid = (unsigned int)strtoul(hostidp, NULL, 16);
40970fbb751dSJohn Levon 	if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
40980fbb751dSJohn Levon 	    sizeof (hostid))) != 0) {
40990fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
41000fbb751dSJohn Levon 		    "zone hostid is not valid: %s: %d", hostidp, res);
41010fbb751dSJohn Levon 		return (Z_SYSTEM);
41020fbb751dSJohn Levon 	}
41030fbb751dSJohn Levon 
41040fbb751dSJohn Levon 	return (res);
41050fbb751dSJohn Levon }
41060fbb751dSJohn Levon 
41070fbb751dSJohn Levon static int
41080fbb751dSJohn Levon setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
41090fbb751dSJohn Levon {
41100fbb751dSJohn Levon 	char fsallowedp[ZONE_FS_ALLOWED_MAX];
41110fbb751dSJohn Levon 	int res;
41120fbb751dSJohn Levon 
41130fbb751dSJohn Levon 	res = zonecfg_get_fs_allowed(handle, fsallowedp, sizeof (fsallowedp));
41140fbb751dSJohn Levon 
41150fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
41160fbb751dSJohn Levon 		return (Z_OK);
41170fbb751dSJohn Levon 	} else if (res != Z_OK) {
41180fbb751dSJohn Levon 		report_prop_err(zlogp, "fs-allowed", fsallowedp, res);
41190fbb751dSJohn Levon 		return (res);
41200fbb751dSJohn Levon 	}
41210fbb751dSJohn Levon 
41220fbb751dSJohn Levon 	if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, &fsallowedp,
41230fbb751dSJohn Levon 	    sizeof (fsallowedp)) != 0) {
41240fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
41250fbb751dSJohn Levon 		    "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
41260fbb751dSJohn Levon 		return (Z_SYSTEM);
41270fbb751dSJohn Levon 	}
41280fbb751dSJohn Levon 
41290fbb751dSJohn Levon 	return (res);
41300fbb751dSJohn Levon }
41310fbb751dSJohn Levon 
41320fbb751dSJohn Levon static int
41330fbb751dSJohn Levon setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
41340fbb751dSJohn Levon {
41350fbb751dSJohn Levon 	zone_dochandle_t handle;
41360fbb751dSJohn Levon 	int res = Z_OK;
41370fbb751dSJohn Levon 
41385679c89fSjv227347 	if ((handle = zonecfg_init_handle()) == NULL) {
41395679c89fSjv227347 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
41405679c89fSjv227347 		return (Z_BAD_HANDLE);
41415679c89fSjv227347 	}
41425679c89fSjv227347 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
41435679c89fSjv227347 		zerror(zlogp, B_FALSE, "invalid configuration");
41440fbb751dSJohn Levon 		goto out;
41455679c89fSjv227347 	}
41465679c89fSjv227347 
41470fbb751dSJohn Levon 	if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
41480fbb751dSJohn Levon 		goto out;
41490fbb751dSJohn Levon 
41500fbb751dSJohn Levon 	if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
41510fbb751dSJohn Levon 		goto out;
41520fbb751dSJohn Levon 
41530fbb751dSJohn Levon out:
41545679c89fSjv227347 	zonecfg_fini_handle(handle);
41555679c89fSjv227347 	return (res);
41565679c89fSjv227347 }
41575679c89fSjv227347 
4158108322fbScarlsonj zoneid_t
41596cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4160108322fbScarlsonj {
4161108322fbScarlsonj 	zoneid_t rval = -1;
41627c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
41637c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
41647c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
4165108322fbScarlsonj 	size_t rctlbufsz = 0;
4166fa9e4066Sahrens 	char *zfsbuf = NULL;
4167fa9e4066Sahrens 	size_t zfsbufsz = 0;
4168108322fbScarlsonj 	zoneid_t zoneid = -1;
41697c478bd9Sstevel@tonic-gate 	int xerr;
4170108322fbScarlsonj 	char *kzone;
4171108322fbScarlsonj 	FILE *fp = NULL;
417245916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
417345916cd2Sjpk 	int match = 0;
417445916cd2Sjpk 	int doi = 0;
4175f4b3ec61Sdh155122 	int flags;
4176f4b3ec61Sdh155122 	zone_iptype_t iptype;
41777c478bd9Sstevel@tonic-gate 
41787c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
41797c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
41807c478bd9Sstevel@tonic-gate 		return (-1);
41817c478bd9Sstevel@tonic-gate 	}
4182108322fbScarlsonj 	if (zonecfg_in_alt_root())
4183108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
41847c478bd9Sstevel@tonic-gate 
41852b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
4186f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4187f4b3ec61Sdh155122 		return (-1);
4188f4b3ec61Sdh155122 	}
4189f4b3ec61Sdh155122 	switch (iptype) {
4190f4b3ec61Sdh155122 	case ZS_SHARED:
4191f4b3ec61Sdh155122 		flags = 0;
4192f4b3ec61Sdh155122 		break;
4193f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
4194f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
4195f4b3ec61Sdh155122 		break;
4196f4b3ec61Sdh155122 	}
4197f4b3ec61Sdh155122 
41987c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
41997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
42007c478bd9Sstevel@tonic-gate 		return (-1);
42017c478bd9Sstevel@tonic-gate 	}
42027c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4203ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
42047c478bd9Sstevel@tonic-gate 		goto error;
4205ffbafc53Scomay 
42066cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
42076cfd72c6Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
42087c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
42097c478bd9Sstevel@tonic-gate 		goto error;
42107c478bd9Sstevel@tonic-gate 	}
4211ffbafc53Scomay 
4212fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4213fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4214fa9e4066Sahrens 		goto error;
4215fa9e4066Sahrens 	}
42167c478bd9Sstevel@tonic-gate 
42176cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
421845916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
421948451833Scarlsonj 		if (zcent != NULL) {
422045916cd2Sjpk 			match = zcent->zc_match;
422145916cd2Sjpk 			doi = zcent->zc_doi;
422245916cd2Sjpk 			*zlabel = zcent->zc_label;
422345916cd2Sjpk 		} else {
422445916cd2Sjpk 			goto error;
422545916cd2Sjpk 		}
42264201a95eSRic Aleshire 		if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
42274201a95eSRic Aleshire 			goto error;
422845916cd2Sjpk 	}
422945916cd2Sjpk 
4230108322fbScarlsonj 	kzone = zone_name;
4231108322fbScarlsonj 
4232108322fbScarlsonj 	/*
4233108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4234108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4235108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4236108322fbScarlsonj 	 * that use this same source node (same device and inode).
4237108322fbScarlsonj 	 */
4238108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4239108322fbScarlsonj 		goto error;
4240108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4241108322fbScarlsonj 		goto error;
4242108322fbScarlsonj 
42436cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4244108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4245108322fbScarlsonj 
4246108322fbScarlsonj 		/*
4247108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4248108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4249108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4250108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4251108322fbScarlsonj 		 * executables.
4252108322fbScarlsonj 		 *
4253108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4254108322fbScarlsonj 		 */
4255108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4256108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4257108322fbScarlsonj 			goto error;
4258108322fbScarlsonj 		}
4259108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4260108322fbScarlsonj 			goto error;
4261108322fbScarlsonj 	}
4262108322fbScarlsonj 
4263108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4264108322fbScarlsonj 		/*
4265108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4266108322fbScarlsonj 		 * then we have some additional work to do before starting the
4267108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4268108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4269108322fbScarlsonj 		 * the zone.
4270108322fbScarlsonj 		 */
4271108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4272108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4273108322fbScarlsonj 			goto error;
4274108322fbScarlsonj 		}
4275108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4276108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4277108322fbScarlsonj 			goto error;
4278108322fbScarlsonj 		}
4279108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4280108322fbScarlsonj 		    NULL, 0) == 0) {
4281108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4282108322fbScarlsonj 			goto error;
4283108322fbScarlsonj 		}
4284108322fbScarlsonj 		/* This is the preferred name */
4285108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4286108322fbScarlsonj 		    zone_name);
4287108322fbScarlsonj 		srandom(getpid());
4288108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4289108322fbScarlsonj 		    0) == 0) {
4290108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4291108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4292108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4293108322fbScarlsonj 		}
4294108322fbScarlsonj 		kzone = kernzone;
4295108322fbScarlsonj 	}
4296108322fbScarlsonj 
42977c478bd9Sstevel@tonic-gate 	xerr = 0;
4298108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4299f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4300f4b3ec61Sdh155122 	    flags)) == -1) {
43017c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
43027c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
43037c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
43047c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
43057c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
43067c478bd9Sstevel@tonic-gate 			} else {
43077c478bd9Sstevel@tonic-gate 
43087c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
43097c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
43107c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
43117c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
43127c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
43137c478bd9Sstevel@tonic-gate 			}
43147c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
43157c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
43167c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
43177c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
43188c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
43198c55461bSton 			char zonename[ZONENAME_MAX];
43208c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
43218c55461bSton 			    zonename, ZONENAME_MAX);
43228c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
43238c55461bSton 			    "used by the zone '%s'.", zonename);
43247c478bd9Sstevel@tonic-gate 		} else {
43257c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
43267c478bd9Sstevel@tonic-gate 		}
43277c478bd9Sstevel@tonic-gate 		goto error;
43287c478bd9Sstevel@tonic-gate 	}
4329108322fbScarlsonj 
4330108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4331108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4332108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4333108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4334108322fbScarlsonj 		goto error;
4335108322fbScarlsonj 	}
4336108322fbScarlsonj 
43370dc2366fSVenugopal Iyer 	if ((pool_name = malloc(MAXPATHLEN)) == NULL) {
43380dc2366fSVenugopal Iyer 		zerror(zlogp, B_TRUE, "memory allocation failed");
43390dc2366fSVenugopal Iyer 		return (Z_NOMEM);
43400dc2366fSVenugopal Iyer 	}
43410dc2366fSVenugopal Iyer 	bzero(pool_name, MAXPATHLEN);
43420dc2366fSVenugopal Iyer 
43430679f794S 	/*
43440679f794S 	 * The following actions are not performed when merely mounting a zone
43450679f794S 	 * for administrative use.
43460679f794S 	 */
43470679f794S 	if (mount_cmd == Z_MNT_BOOT) {
43480679f794S 		brand_handle_t bh;
43490679f794S 		struct brand_attr attr;
43500679f794S 		char modname[MAXPATHLEN];
43510679f794S 
43520fbb751dSJohn Levon 		if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
43535679c89fSjv227347 			goto error;
43545679c89fSjv227347 
435562868012SSteve Lawrence 		if ((bh = brand_open(brand_name)) == NULL) {
43560679f794S 			zerror(zlogp, B_FALSE,
43570679f794S 			    "unable to determine brand name");
43585679c89fSjv227347 			goto error;
43599acbbeafSnn35248 		}
43609acbbeafSnn35248 
43619a5d73e0SRic Aleshire 		if (!is_system_labeled() &&
436262868012SSteve Lawrence 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
43639a5d73e0SRic Aleshire 			brand_close(bh);
43649a5d73e0SRic Aleshire 			zerror(zlogp, B_FALSE,
43659a5d73e0SRic Aleshire 			    "cannot boot labeled zone on unlabeled system");
43669a5d73e0SRic Aleshire 			goto error;
43679a5d73e0SRic Aleshire 		}
43689a5d73e0SRic Aleshire 
43699acbbeafSnn35248 		/*
43709acbbeafSnn35248 		 * If this brand requires any kernel support, now is the time to
43719acbbeafSnn35248 		 * get it loaded and initialized.
43729acbbeafSnn35248 		 */
4373123807fbSedp 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4374e767a340Sgjelinek 			brand_close(bh);
43750679f794S 			zerror(zlogp, B_FALSE,
43760679f794S 			    "unable to determine brand kernel module");
43775679c89fSjv227347 			goto error;
43789acbbeafSnn35248 		}
4379e767a340Sgjelinek 		brand_close(bh);
43809acbbeafSnn35248 
43819acbbeafSnn35248 		if (strlen(modname) > 0) {
438262868012SSteve Lawrence 			(void) strlcpy(attr.ba_brandname, brand_name,
438362868012SSteve Lawrence 			    sizeof (attr.ba_brandname));
438462868012SSteve Lawrence 			(void) strlcpy(attr.ba_modname, modname,
438562868012SSteve Lawrence 			    sizeof (attr.ba_modname));
43869acbbeafSnn35248 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
43879acbbeafSnn35248 			    sizeof (attr) != 0)) {
43880679f794S 				zerror(zlogp, B_TRUE,
43890679f794S 				    "could not set zone brand attribute.");
43909acbbeafSnn35248 				goto error;
43919acbbeafSnn35248 			}
43929acbbeafSnn35248 		}
43939acbbeafSnn35248 
43945679c89fSjv227347 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
43950209230bSgjelinek 			goto error;
43960209230bSgjelinek 
439745916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
43980209230bSgjelinek 	}
43990209230bSgjelinek 
4400108322fbScarlsonj 	rval = zoneid;
4401108322fbScarlsonj 	zoneid = -1;
4402108322fbScarlsonj 
44037c478bd9Sstevel@tonic-gate error:
44045679c89fSjv227347 	if (zoneid != -1) {
44055679c89fSjv227347 		(void) zone_shutdown(zoneid);
4406108322fbScarlsonj 		(void) zone_destroy(zoneid);
44075679c89fSjv227347 	}
44087c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
44097c478bd9Sstevel@tonic-gate 		free(rctlbuf);
44107c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
4411108322fbScarlsonj 	if (fp != NULL)
4412108322fbScarlsonj 		zonecfg_close_scratch(fp);
4413108322fbScarlsonj 	lofs_discard_mnttab();
441445916cd2Sjpk 	if (zcent != NULL)
441545916cd2Sjpk 		tsol_freezcent(zcent);
44167c478bd9Sstevel@tonic-gate 	return (rval);
44177c478bd9Sstevel@tonic-gate }
44187c478bd9Sstevel@tonic-gate 
4419555afedfScarlsonj /*
4420555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
4421555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4422555afedfScarlsonj  * details from inside the zone.
4423555afedfScarlsonj  */
4424555afedfScarlsonj static void
4425555afedfScarlsonj write_index_file(zoneid_t zoneid)
4426555afedfScarlsonj {
4427555afedfScarlsonj 	FILE *zef;
4428555afedfScarlsonj 	FILE *zet;
4429555afedfScarlsonj 	struct zoneent *zep;
4430555afedfScarlsonj 	pid_t child;
4431555afedfScarlsonj 	int tmpl_fd;
4432555afedfScarlsonj 	ctid_t ct;
4433555afedfScarlsonj 	int fd;
4434555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4435555afedfScarlsonj 
4436555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
4437555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
4438555afedfScarlsonj 		return;
4439555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
4440555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
4441555afedfScarlsonj 			break;
4442555afedfScarlsonj 		free(zep);
4443555afedfScarlsonj 	}
4444555afedfScarlsonj 	endzoneent(zef);
4445555afedfScarlsonj 	if (zep == NULL)
4446555afedfScarlsonj 		return;
4447555afedfScarlsonj 
4448555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
4449555afedfScarlsonj 		free(zep);
4450555afedfScarlsonj 		return;
4451555afedfScarlsonj 	}
4452555afedfScarlsonj 
4453555afedfScarlsonj 	if ((child = fork()) == -1) {
4454555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4455555afedfScarlsonj 		(void) close(tmpl_fd);
4456555afedfScarlsonj 		free(zep);
4457555afedfScarlsonj 		return;
4458555afedfScarlsonj 	}
4459555afedfScarlsonj 
4460555afedfScarlsonj 	/* parent waits for child to finish */
4461555afedfScarlsonj 	if (child != 0) {
4462555afedfScarlsonj 		free(zep);
4463555afedfScarlsonj 		if (contract_latest(&ct) == -1)
4464555afedfScarlsonj 			ct = -1;
4465555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4466555afedfScarlsonj 		(void) close(tmpl_fd);
4467555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
4468555afedfScarlsonj 		(void) contract_abandon_id(ct);
4469555afedfScarlsonj 		return;
4470555afedfScarlsonj 	}
4471555afedfScarlsonj 
4472555afedfScarlsonj 	/* child enters zone and sets up index file */
4473555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
4474555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
4475555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4476555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4477555afedfScarlsonj 		    ZONE_CONFIG_GID);
4478555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4479555afedfScarlsonj 		    ZONE_INDEX_MODE);
4480555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4481555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4482555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
4483555afedfScarlsonj 				uuidstr[0] = '\0';
4484555afedfScarlsonj 			else
4485555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
4486555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4487555afedfScarlsonj 			    zone_state_str(zep->zone_state),
4488555afedfScarlsonj 			    uuidstr);
4489555afedfScarlsonj 			(void) fclose(zet);
4490555afedfScarlsonj 		}
4491555afedfScarlsonj 	}
4492555afedfScarlsonj 	_exit(0);
4493555afedfScarlsonj }
4494555afedfScarlsonj 
44957c478bd9Sstevel@tonic-gate int
44966cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
44977c478bd9Sstevel@tonic-gate {
44989acbbeafSnn35248 	char zonepath[MAXPATHLEN];
44995749802bSdp 
45006cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4501fa9e4066Sahrens 		lofs_discard_mnttab();
4502fa9e4066Sahrens 		return (-1);
4503fa9e4066Sahrens 	}
4504fa9e4066Sahrens 
45059acbbeafSnn35248 	/*
45069acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
45079acbbeafSnn35248 	 * attribute backing store for /dev
45089acbbeafSnn35248 	 */
45099acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
45109acbbeafSnn35248 		lofs_discard_mnttab();
45119acbbeafSnn35248 		return (-1);
45129acbbeafSnn35248 	}
451316bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
451427e6fb21Sdp 
451527e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
451627e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
451727e6fb21Sdp 	    0, 3) != 0) {
4518108322fbScarlsonj 		lofs_discard_mnttab();
45197c478bd9Sstevel@tonic-gate 		return (-1);
4520108322fbScarlsonj 	}
4521facf4a8dSllai1 
45229acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4523facf4a8dSllai1 		lofs_discard_mnttab();
4524facf4a8dSllai1 		return (-1);
4525facf4a8dSllai1 	}
4526facf4a8dSllai1 
45276cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
4528f4b3ec61Sdh155122 		zone_iptype_t iptype;
4529f4b3ec61Sdh155122 
45302b24ab6bSSebastien Roy 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
4531f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4532108322fbScarlsonj 			lofs_discard_mnttab();
45337c478bd9Sstevel@tonic-gate 			return (-1);
4534108322fbScarlsonj 		}
4535555afedfScarlsonj 
4536f4b3ec61Sdh155122 		switch (iptype) {
4537f4b3ec61Sdh155122 		case ZS_SHARED:
4538f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
4539f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
4540f4b3ec61Sdh155122 				lofs_discard_mnttab();
4541f4b3ec61Sdh155122 				return (-1);
4542f4b3ec61Sdh155122 			}
4543f4b3ec61Sdh155122 			break;
4544f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4545f4b3ec61Sdh155122 			if (configure_exclusive_network_interfaces(zlogp) !=
4546f4b3ec61Sdh155122 			    0) {
4547f4b3ec61Sdh155122 				lofs_discard_mnttab();
4548f4b3ec61Sdh155122 				return (-1);
4549f4b3ec61Sdh155122 			}
4550f4b3ec61Sdh155122 			break;
4551f4b3ec61Sdh155122 		}
4552f4b3ec61Sdh155122 	}
4553f4b3ec61Sdh155122 
4554555afedfScarlsonj 	write_index_file(zoneid);
4555555afedfScarlsonj 
4556108322fbScarlsonj 	lofs_discard_mnttab();
45577c478bd9Sstevel@tonic-gate 	return (0);
45587c478bd9Sstevel@tonic-gate }
45597c478bd9Sstevel@tonic-gate 
4560108322fbScarlsonj static int
4561108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
45627c478bd9Sstevel@tonic-gate {
4563108322fbScarlsonj 	char zroot[MAXPATHLEN];
4564108322fbScarlsonj 
4565108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4566108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4567108322fbScarlsonj 		return (-1);
4568108322fbScarlsonj 	}
4569108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4570108322fbScarlsonj 
4571108322fbScarlsonj 	/*
4572108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
4573108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
4574108322fbScarlsonj 	 * still be creds floating about in the system that reference the
4575108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4576108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4577108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
4578108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4579108322fbScarlsonj 	 */
4580108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
4581108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4582108322fbScarlsonj 			goto unmounted;
4583108322fbScarlsonj 		if (errno == EBUSY) {
4584108322fbScarlsonj 			int tries = 10;
4585108322fbScarlsonj 
4586108322fbScarlsonj 			while (--tries >= 0) {
4587108322fbScarlsonj 				(void) sleep(1);
4588108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
4589108322fbScarlsonj 					goto unmounted;
4590108322fbScarlsonj 				if (errno != EBUSY)
4591108322fbScarlsonj 					break;
4592108322fbScarlsonj 			}
4593108322fbScarlsonj 		}
4594108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4595108322fbScarlsonj 		return (-1);
4596108322fbScarlsonj 	}
4597108322fbScarlsonj unmounted:
4598108322fbScarlsonj 
4599108322fbScarlsonj 	/*
4600108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
4601108322fbScarlsonj 	 * entries.
4602108322fbScarlsonj 	 */
4603108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4604108322fbScarlsonj 		FILE *fp;
4605108322fbScarlsonj 		int retv;
4606108322fbScarlsonj 
4607108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4608108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4609108322fbScarlsonj 			return (-1);
4610108322fbScarlsonj 		}
4611108322fbScarlsonj 		retv = -1;
4612108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
4613108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4614108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4615108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4616108322fbScarlsonj 		else
4617108322fbScarlsonj 			retv = 0;
4618108322fbScarlsonj 		zonecfg_close_scratch(fp);
4619108322fbScarlsonj 		return (retv);
4620108322fbScarlsonj 	} else {
4621108322fbScarlsonj 		return (0);
4622108322fbScarlsonj 	}
4623108322fbScarlsonj }
4624108322fbScarlsonj 
4625108322fbScarlsonj int
46260209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4627108322fbScarlsonj {
4628108322fbScarlsonj 	char *kzone;
46297c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
46300209230bSgjelinek 	int res;
46310209230bSgjelinek 	char pool_err[128];
4632ff17c8bfSgjelinek 	char zpath[MAXPATHLEN];
46339acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
4634123807fbSedp 	brand_handle_t bh = NULL;
46352b24ab6bSSebastien Roy 	dladm_status_t status;
46362b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
4637f4b3ec61Sdh155122 	ushort_t flags;
46387c478bd9Sstevel@tonic-gate 
4639108322fbScarlsonj 	kzone = zone_name;
4640108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4641108322fbScarlsonj 		FILE *fp;
4642108322fbScarlsonj 
4643108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4644108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
4645108322fbScarlsonj 			goto error;
4646108322fbScarlsonj 		}
4647108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4648108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
4649108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4650108322fbScarlsonj 			zonecfg_close_scratch(fp);
4651108322fbScarlsonj 			goto error;
4652108322fbScarlsonj 		}
4653108322fbScarlsonj 		zonecfg_close_scratch(fp);
4654108322fbScarlsonj 		kzone = kernzone;
4655108322fbScarlsonj 	}
4656108322fbScarlsonj 
4657108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
46587c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
46597c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4660108322fbScarlsonj 		if (unmount_cmd)
4661108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
46627c478bd9Sstevel@tonic-gate 		goto error;
46637c478bd9Sstevel@tonic-gate 	}
46647c478bd9Sstevel@tonic-gate 
46650dc2366fSVenugopal Iyer 	if (remove_datalink_pool(zlogp, zoneid) != 0) {
46660dc2366fSVenugopal Iyer 		zerror(zlogp, B_FALSE, "unable clear datalink pool property");
46670dc2366fSVenugopal Iyer 		goto error;
46680dc2366fSVenugopal Iyer 	}
46690dc2366fSVenugopal Iyer 
46707c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
46717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
46727c478bd9Sstevel@tonic-gate 		goto error;
46737c478bd9Sstevel@tonic-gate 	}
46747c478bd9Sstevel@tonic-gate 
4675ff17c8bfSgjelinek 	/* Get the zonepath of this zone */
4676ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
4677ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
46789acbbeafSnn35248 		goto error;
46799acbbeafSnn35248 	}
46809acbbeafSnn35248 
46819acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
468262868012SSteve Lawrence 	if ((bh = brand_open(brand_name)) == NULL) {
46839acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
46849acbbeafSnn35248 		return (-1);
46859acbbeafSnn35248 	}
46869acbbeafSnn35248 	/*
46879acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
46889acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
46899acbbeafSnn35248 	 */
46909acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4691ff17c8bfSgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
4692ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
4693123807fbSedp 		brand_close(bh);
46949acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
46959acbbeafSnn35248 		    "halt callback.");
46969acbbeafSnn35248 		goto error;
46979acbbeafSnn35248 	}
4698123807fbSedp 	brand_close(bh);
46999acbbeafSnn35248 
47009acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
4701c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
47029acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
47039acbbeafSnn35248 		goto error;
47049acbbeafSnn35248 	}
47059acbbeafSnn35248 
4706f4b3ec61Sdh155122 	if (!unmount_cmd) {
4707f4b3ec61Sdh155122 		zone_iptype_t iptype;
4708f4b3ec61Sdh155122 
4709f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4710f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
47112b24ab6bSSebastien Roy 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
4712f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
4713f4b3ec61Sdh155122 				    "ip-type");
47147c478bd9Sstevel@tonic-gate 				goto error;
47157c478bd9Sstevel@tonic-gate 			}
4716f4b3ec61Sdh155122 		} else {
4717f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
4718f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
4719f4b3ec61Sdh155122 			else
4720f4b3ec61Sdh155122 				iptype = ZS_SHARED;
4721f4b3ec61Sdh155122 		}
4722f4b3ec61Sdh155122 
4723f4b3ec61Sdh155122 		switch (iptype) {
4724f4b3ec61Sdh155122 		case ZS_SHARED:
4725f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
4726f4b3ec61Sdh155122 			    zoneid) != 0) {
4727f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4728f4b3ec61Sdh155122 				    "network interfaces in zone");
4729f4b3ec61Sdh155122 				goto error;
4730f4b3ec61Sdh155122 			}
4731f4b3ec61Sdh155122 			break;
4732f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4733f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
4734f4b3ec61Sdh155122 			    zoneid) != 0) {
4735f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4736f4b3ec61Sdh155122 				    "network interfaces in zone");
4737f4b3ec61Sdh155122 				goto error;
4738f4b3ec61Sdh155122 			}
47392b24ab6bSSebastien Roy 			status = dladm_zone_halt(dld_handle, zoneid);
47402b24ab6bSSebastien Roy 			if (status != DLADM_STATUS_OK) {
47412b24ab6bSSebastien Roy 				zerror(zlogp, B_FALSE, "unable to notify "
47422b24ab6bSSebastien Roy 				    "dlmgmtd of zone halt: %s",
47432b24ab6bSSebastien Roy 				    dladm_status2str(status, errmsg));
47442b24ab6bSSebastien Roy 			}
4745f4b3ec61Sdh155122 			break;
4746f4b3ec61Sdh155122 		}
4747f4b3ec61Sdh155122 	}
47487c478bd9Sstevel@tonic-gate 
4749108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
47507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
47517c478bd9Sstevel@tonic-gate 		goto error;
47527c478bd9Sstevel@tonic-gate 	}
47537c478bd9Sstevel@tonic-gate 
4754108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
47557c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
47567c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
47577c478bd9Sstevel@tonic-gate 		goto error;
47587c478bd9Sstevel@tonic-gate 	}
47597c478bd9Sstevel@tonic-gate 
47600209230bSgjelinek 	/*
4761cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
4762cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
4763cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
4764cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
4765cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
4766cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
4767cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
4768cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
47690209230bSgjelinek 	 */
4770cf6b13d2Sgjelinek 	if (!unmount_cmd) {
4771cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
4772cf6b13d2Sgjelinek 
4773cf6b13d2Sgjelinek 		if (rebooting) {
4774cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
4775cf6b13d2Sgjelinek 			zone_dochandle_t handle;
4776cf6b13d2Sgjelinek 
4777cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
4778cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4779cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4780cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
4781e767a340Sgjelinek 
4782e767a340Sgjelinek 			zonecfg_fini_handle(handle);
4783cf6b13d2Sgjelinek 		}
4784cf6b13d2Sgjelinek 
4785cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
47860209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
47870209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
47880209230bSgjelinek 				if (res == Z_POOL)
47890209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
47900209230bSgjelinek 			}
47910209230bSgjelinek 		}
4792cf6b13d2Sgjelinek 	}
47930209230bSgjelinek 
47940dc2366fSVenugopal Iyer 	free(pool_name);
47950dc2366fSVenugopal Iyer 
479645916cd2Sjpk 	remove_mlps(zlogp, zoneid);
479745916cd2Sjpk 
47987c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
47997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
48007c478bd9Sstevel@tonic-gate 		goto error;
48017c478bd9Sstevel@tonic-gate 	}
4802108322fbScarlsonj 
4803108322fbScarlsonj 	/*
4804108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
4805108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
4806108322fbScarlsonj 	 */
4807108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4808108322fbScarlsonj 		goto error;
4809108322fbScarlsonj 
4810108322fbScarlsonj 	lofs_discard_mnttab();
48117c478bd9Sstevel@tonic-gate 	return (0);
48127c478bd9Sstevel@tonic-gate 
48137c478bd9Sstevel@tonic-gate error:
4814108322fbScarlsonj 	lofs_discard_mnttab();
48157c478bd9Sstevel@tonic-gate 	return (-1);
48167c478bd9Sstevel@tonic-gate }
4817