xref: /illumos-gate/usr/src/cmd/zoneadmd/vplat.c (revision af34582fe571c99d9f74acf7c271e26c744fef5b)
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.
248cd81a20SJerry Jelinek  * Copyright (c) 2013, Joyent Inc. All rights reserved.
2548bbca81SDaniel Hoffman  * Copyright (c) 2015, 2016 by Delphix. All rights reserved.
26*af34582fSAndy Fiddaman  * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
30a3002e0aSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
31a3002e0aSGarrett D'Amore  */
32a3002e0aSGarrett D'Amore 
33a3002e0aSGarrett D'Amore /*
347c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
357c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
367c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
377c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
387c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
397c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
407c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
41108322fbScarlsonj  *
42108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
43108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
44108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
45108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
46108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
47108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
48108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
49108322fbScarlsonj  * without relying on any binaries in the zone itself.
50108322fbScarlsonj  *
51108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
52108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
53108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
54108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
55108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
56108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
57108322fbScarlsonj  * process.
58108322fbScarlsonj  *
59108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
60108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
61108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
62108322fbScarlsonj  * $ZONEPATH/lu as the root.
63108322fbScarlsonj  *
64108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
65108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
66108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
67108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/param.h>
717c478bd9Sstevel@tonic-gate #include <sys/mount.h>
727c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
737c478bd9Sstevel@tonic-gate #include <sys/socket.h>
747c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
757c478bd9Sstevel@tonic-gate #include <sys/types.h>
767c478bd9Sstevel@tonic-gate #include <sys/stat.h>
777c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
787c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
797c478bd9Sstevel@tonic-gate #include <sys/conf.h>
805679c89fSjv227347 #include <sys/systeminfo.h>
81d2a70789SRichard Lowe #include <sys/secflags.h>
827c478bd9Sstevel@tonic-gate 
83f4b3ec61Sdh155122 #include <libdlpi.h>
84f595a68aSyz147064 #include <libdllink.h>
85d62bc4baSyz147064 #include <libdlvlan.h>
86f4b3ec61Sdh155122 
877c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
887c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
897c478bd9Sstevel@tonic-gate #include <netinet/in.h>
907c478bd9Sstevel@tonic-gate #include <net/route.h>
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #include <stdio.h>
937c478bd9Sstevel@tonic-gate #include <errno.h>
947c478bd9Sstevel@tonic-gate #include <fcntl.h>
957c478bd9Sstevel@tonic-gate #include <unistd.h>
967c478bd9Sstevel@tonic-gate #include <rctl.h>
977c478bd9Sstevel@tonic-gate #include <stdlib.h>
987c478bd9Sstevel@tonic-gate #include <string.h>
997c478bd9Sstevel@tonic-gate #include <strings.h>
1007c478bd9Sstevel@tonic-gate #include <wait.h>
1017c478bd9Sstevel@tonic-gate #include <limits.h>
1027c478bd9Sstevel@tonic-gate #include <libgen.h>
103fa9e4066Sahrens #include <libzfs.h>
104facf4a8dSllai1 #include <libdevinfo.h>
1057c478bd9Sstevel@tonic-gate #include <zone.h>
1067c478bd9Sstevel@tonic-gate #include <assert.h>
107555afedfScarlsonj #include <libcontract.h>
108555afedfScarlsonj #include <libcontract_priv.h>
109555afedfScarlsonj #include <uuid/uuid.h>
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1127c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1137c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1147c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
115fa9e4066Sahrens #include <sys/fs/zfs.h>
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #include <pool.h>
1187c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1190209230bSgjelinek #include <sys/priocntl.h>
1207c478bd9Sstevel@tonic-gate 
1219acbbeafSnn35248 #include <libbrand.h>
1229acbbeafSnn35248 #include <sys/brand.h>
1237c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
12439d3e169Sevanl #include <synch.h>
12522321485Svp157776 
1267c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
12745916cd2Sjpk #include <tsol/label.h>
12845916cd2Sjpk #include <libtsnet.h>
12945916cd2Sjpk #include <sys/priv.h>
130550b6e40SSowmini Varadhan #include <libinetutil.h>
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1337c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1347c478bd9Sstevel@tonic-gate 
1356e1ae2a3SGary Pennington #define	RESOURCE_DEFAULT_OPTS \
1367c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13945916cd2Sjpk #define	MAXTNZLEN	2048
1407c478bd9Sstevel@tonic-gate 
1416cfd72c6Sgjelinek #define	ALT_MOUNT(mount_cmd)	((mount_cmd) != Z_MNT_BOOT)
1426cfd72c6Sgjelinek 
143ff19e029SMenno Lageman /* a reasonable estimate for the number of lwps per process */
144ff19e029SMenno Lageman #define	LWPS_PER_PROCESS	10
145ff19e029SMenno Lageman 
1467c478bd9Sstevel@tonic-gate /* for routing socket */
1477c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1487c478bd9Sstevel@tonic-gate 
149108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
150108322fbScarlsonj static char kernzone[ZONENAME_MAX];
151108322fbScarlsonj 
152108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
153108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
154108322fbScarlsonj 
15545916cd2Sjpk /* for Trusted Extensions */
15645916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
15745916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
15845916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
1599d48116cSdh155122 
16045916cd2Sjpk static m_label_t *zlabel = NULL;
16145916cd2Sjpk static m_label_t *zid_label = NULL;
16245916cd2Sjpk static priv_set_t *zprivs = NULL;
16345916cd2Sjpk 
1648cd81a20SJerry Jelinek static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn";
1658cd81a20SJerry Jelinek 
1667c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1677c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1687c478bd9Sstevel@tonic-gate 
169c5cd6260S /* from zoneadmd */
170c5cd6260S extern char query_hook[];
171c5cd6260S 
1727c478bd9Sstevel@tonic-gate /*
173550b6e40SSowmini Varadhan  * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
174550b6e40SSowmini Varadhan  * node in a linked list that is sorted by linkid.  The list is constructed as
175550b6e40SSowmini Varadhan  * the xml configuration file is parsed, and the information
176550b6e40SSowmini Varadhan  * contained in each node is added to the kernel before the zone is
177550b6e40SSowmini Varadhan  * booted, to be retrieved and applied from within the exclusive-IP NGZ
178550b6e40SSowmini Varadhan  * on boot.
179550b6e40SSowmini Varadhan  */
180550b6e40SSowmini Varadhan typedef struct zone_addr_list {
181550b6e40SSowmini Varadhan 	struct zone_addr_list *za_next;
182550b6e40SSowmini Varadhan 	datalink_id_t za_linkid;	/* datalink_id_t of interface */
183550b6e40SSowmini Varadhan 	struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
184550b6e40SSowmini Varadhan } zone_addr_list_t;
185550b6e40SSowmini Varadhan 
186550b6e40SSowmini Varadhan /*
187108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
188108322fbScarlsonj  * data) only once every N times through the loop.
189108322fbScarlsonj  */
190108322fbScarlsonj #define	MNTTAB_HUNK	32
191108322fbScarlsonj 
192550b6e40SSowmini Varadhan /* some handy macros */
193550b6e40SSowmini Varadhan #define	SIN(s)	((struct sockaddr_in *)s)
194550b6e40SSowmini Varadhan #define	SIN6(s)	((struct sockaddr_in6 *)s)
195550b6e40SSowmini Varadhan 
196108322fbScarlsonj /*
1977c478bd9Sstevel@tonic-gate  * Private autofs system call
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static int
2027c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
2067c478bd9Sstevel@tonic-gate 	 */
2077c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
210108322fbScarlsonj static void
211108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
212108322fbScarlsonj {
213108322fbScarlsonj 	uint_t i;
214108322fbScarlsonj 
215108322fbScarlsonj 	if (mnt_array == NULL)
216108322fbScarlsonj 		return;
217108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
218108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
219108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
220108322fbScarlsonj 		free(mnt_array[i].mnt_special);
221108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
222108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
223108322fbScarlsonj 	}
224108322fbScarlsonj 	free(mnt_array);
225108322fbScarlsonj }
226108322fbScarlsonj 
227108322fbScarlsonj /*
228108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
229108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
230108322fbScarlsonj  * array in "nelemp".
231108322fbScarlsonj  */
232108322fbScarlsonj static int
233108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
234108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
235108322fbScarlsonj {
236108322fbScarlsonj 	struct mnttab mnt;
237108322fbScarlsonj 	struct mnttab *mnts;
238108322fbScarlsonj 	struct mnttab *mnp;
239108322fbScarlsonj 	uint_t nmnt;
240108322fbScarlsonj 
241108322fbScarlsonj 	rewind(mnttab);
242108322fbScarlsonj 	resetmnttab(mnttab);
243108322fbScarlsonj 	nmnt = 0;
244108322fbScarlsonj 	mnts = NULL;
245108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
246108322fbScarlsonj 		struct mnttab *tmp_array;
247108322fbScarlsonj 
248108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
249108322fbScarlsonj 			continue;
250108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
251108322fbScarlsonj 			tmp_array = realloc(mnts,
252108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
253108322fbScarlsonj 			if (tmp_array == NULL) {
254108322fbScarlsonj 				free_mnttable(mnts, nmnt);
255108322fbScarlsonj 				return (-1);
256108322fbScarlsonj 			}
257108322fbScarlsonj 			mnts = tmp_array;
258108322fbScarlsonj 		}
259108322fbScarlsonj 		mnp = &mnts[nmnt++];
260108322fbScarlsonj 
261108322fbScarlsonj 		/*
262108322fbScarlsonj 		 * Zero out any fields we're not using.
263108322fbScarlsonj 		 */
264108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
265108322fbScarlsonj 
266108322fbScarlsonj 		if (mnt.mnt_special != NULL)
267108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
268108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
269108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
270108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
271108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
272108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
273108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
274108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
275108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
276108322fbScarlsonj 			free_mnttable(mnts, nmnt);
277108322fbScarlsonj 			return (-1);
278108322fbScarlsonj 		}
279108322fbScarlsonj 	}
280108322fbScarlsonj 	*mnt_arrayp = mnts;
281108322fbScarlsonj 	*nelemp = nmnt;
282108322fbScarlsonj 	return (0);
283108322fbScarlsonj }
284108322fbScarlsonj 
285108322fbScarlsonj /*
286108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
287108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
288108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
289108322fbScarlsonj  * reread of the list of mount points
290108322fbScarlsonj  */
291108322fbScarlsonj static void
292108322fbScarlsonj lofs_discard_mnttab(void)
293108322fbScarlsonj {
294108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
295108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
296108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
297108322fbScarlsonj }
298108322fbScarlsonj 
299108322fbScarlsonj static int
300108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
301108322fbScarlsonj {
302108322fbScarlsonj 	FILE *mnttab;
303108322fbScarlsonj 	uint_t nmnts;
304108322fbScarlsonj 
305108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
306108322fbScarlsonj 		return (-1);
307108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
308108322fbScarlsonj 	    &nmnts) == -1) {
309108322fbScarlsonj 		(void) fclose(mnttab);
310108322fbScarlsonj 		return (-1);
311108322fbScarlsonj 	}
312108322fbScarlsonj 	(void) fclose(mnttab);
313108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
314108322fbScarlsonj 	return (0);
315108322fbScarlsonj }
316108322fbScarlsonj 
317108322fbScarlsonj /*
318108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
319108322fbScarlsonj  * path and resolves them all down to an absolute path.
320108322fbScarlsonj  */
321910f48daSedp void
322108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
323108322fbScarlsonj {
324108322fbScarlsonj 	int len, arlen;
325108322fbScarlsonj 	const char *altroot;
326108322fbScarlsonj 	char tmppath[MAXPATHLEN];
327108322fbScarlsonj 	boolean_t outside_altroot;
328108322fbScarlsonj 
329108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
330108322fbScarlsonj 		return;
331108322fbScarlsonj 	tmppath[len] = '\0';
332108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
333108322fbScarlsonj 
334108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
335108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
336108322fbScarlsonj 		return;
337108322fbScarlsonj 
338108322fbScarlsonj 	altroot = zonecfg_get_root();
339108322fbScarlsonj 	arlen = strlen(altroot);
340108322fbScarlsonj 	outside_altroot = B_FALSE;
341108322fbScarlsonj 	for (;;) {
342108322fbScarlsonj 		struct mnttab *mnp;
343108322fbScarlsonj 
3441e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3451e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3461e9d8f9fSdminer 		    mnp--) {
347108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
348108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3491e9d8f9fSdminer 			    mnp->mnt_special == NULL)
350108322fbScarlsonj 				continue;
351108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
352108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
353108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
354108322fbScarlsonj 				break;
355108322fbScarlsonj 		}
3561e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3571e9d8f9fSdminer 			break;
3581e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3591e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
360108322fbScarlsonj 			break;
361108322fbScarlsonj 		if (outside_altroot) {
362108322fbScarlsonj 			char *cp;
363108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
364108322fbScarlsonj 
365108322fbScarlsonj 			/*
366108322fbScarlsonj 			 * If we run into a read-only mount outside of the
367108322fbScarlsonj 			 * alternate root environment, then the user doesn't
368108322fbScarlsonj 			 * want this path to be made read-write.
369108322fbScarlsonj 			 */
370108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
371108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
372108322fbScarlsonj 			    NULL &&
373108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
374108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
375108322fbScarlsonj 				break;
376108322fbScarlsonj 			}
377108322fbScarlsonj 		} else if (arlen > 0 &&
378108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
379108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
380108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
381108322fbScarlsonj 			outside_altroot = B_TRUE;
382108322fbScarlsonj 		}
383108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
384108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
385108322fbScarlsonj 		    mnp->mnt_special, path + len);
386108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
387108322fbScarlsonj 			break;
388108322fbScarlsonj 		path[len] = '\0';
389108322fbScarlsonj 	}
390108322fbScarlsonj }
391108322fbScarlsonj 
392108322fbScarlsonj /*
393108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
394108322fbScarlsonj  * referenced device is already mounted somewhere.
395108322fbScarlsonj  */
396108322fbScarlsonj static int
397108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
398108322fbScarlsonj {
399108322fbScarlsonj 	struct mnttab *mnp;
400108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
401108322fbScarlsonj 
402108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
403108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
404108322fbScarlsonj 		return (-1);
405108322fbScarlsonj 
406108322fbScarlsonj 	/*
407108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
408108322fbScarlsonj 	 * no need to worry about conflicting mounts.
409108322fbScarlsonj 	 */
410108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
411108322fbScarlsonj 	    mnp++) {
412108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
413108322fbScarlsonj 			break;
414108322fbScarlsonj 	}
415108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
416108322fbScarlsonj 		return (0);
417108322fbScarlsonj 
418108322fbScarlsonj 	/*
419108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
420108322fbScarlsonj 	 */
421108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
422108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
423108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
424108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
425108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
426108322fbScarlsonj 
427108322fbScarlsonj 	/*
4286e1ae2a3SGary Pennington 	 * Discard all but one of the original options and set that to our
4296e1ae2a3SGary Pennington 	 * default set of options used for resources.
430108322fbScarlsonj 	 */
431108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
432108322fbScarlsonj 	if (optptr == NULL) {
433108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
434108322fbScarlsonj 		if (optptr == NULL) {
435108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
436108322fbScarlsonj 			    fsptr->zone_fs_dir);
437108322fbScarlsonj 			return (-1);
438108322fbScarlsonj 		}
439108322fbScarlsonj 	} else {
440108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
441108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
442108322fbScarlsonj 			free(onext);
443108322fbScarlsonj 		}
444108322fbScarlsonj 	}
4456e1ae2a3SGary Pennington 	(void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
446108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
447108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
448108322fbScarlsonj 	return (0);
449108322fbScarlsonj }
450108322fbScarlsonj 
451d314f035Sedp int
45227e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
45327e6fb21Sdp     uid_t userid, gid_t groupid)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4567c478bd9Sstevel@tonic-gate 	struct stat st;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4597c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4607c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4617c478bd9Sstevel@tonic-gate 		    subdir);
4627c478bd9Sstevel@tonic-gate 		return (-1);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4687c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
46948bbca81SDaniel Hoffman 		 * and we don't need to second guess them.
4707c478bd9Sstevel@tonic-gate 		 */
4717c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
472756cf395SRic Aleshire 			if (S_ISREG(st.st_mode)) {
47345916cd2Sjpk 				/*
474756cf395SRic Aleshire 				 * Allow readonly mounts of /etc/ files; this
475756cf395SRic Aleshire 				 * is needed most by Trusted Extensions.
47645916cd2Sjpk 				 */
47745916cd2Sjpk 				if (strncmp(subdir, "/etc/",
47845916cd2Sjpk 				    strlen("/etc/")) != 0) {
47945916cd2Sjpk 					zerror(zlogp, B_FALSE,
48045916cd2Sjpk 					    "%s is not in /etc", path);
4817c478bd9Sstevel@tonic-gate 					return (-1);
4827c478bd9Sstevel@tonic-gate 				}
48345916cd2Sjpk 			} else {
48445916cd2Sjpk 				zerror(zlogp, B_FALSE,
48545916cd2Sjpk 				    "%s is not a directory", path);
48645916cd2Sjpk 				return (-1);
48745916cd2Sjpk 			}
48845916cd2Sjpk 		}
48927e6fb21Sdp 		return (0);
49027e6fb21Sdp 	}
49127e6fb21Sdp 
49227e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
4937c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4947c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4957c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4967c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4977c478bd9Sstevel@tonic-gate 		else
4987c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4997c478bd9Sstevel@tonic-gate 		return (-1);
5007c478bd9Sstevel@tonic-gate 	}
50127e6fb21Sdp 
50227e6fb21Sdp 	(void) chown(path, userid, groupid);
5037c478bd9Sstevel@tonic-gate 	return (0);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate static void
5077c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	uint_t i;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (types == NULL)
5127c478bd9Sstevel@tonic-gate 		return;
5137c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
5147c478bd9Sstevel@tonic-gate 		free(types[i]);
5157c478bd9Sstevel@tonic-gate 	free(types);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate static char **
5197c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	char **types = NULL;
5227c478bd9Sstevel@tonic-gate 	FILE *fp;
5237c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
5247c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
5257c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
5267c478bd9Sstevel@tonic-gate 	uint_t i;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5307c478bd9Sstevel@tonic-gate 		return (NULL);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5367c478bd9Sstevel@tonic-gate 		lines++;
5377c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5387c478bd9Sstevel@tonic-gate 		goto out;
5397c478bd9Sstevel@tonic-gate 	rewind(fp);
5407c478bd9Sstevel@tonic-gate 	/*
5417c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5427c478bd9Sstevel@tonic-gate 	 */
5437c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5447c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5457c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5467c478bd9Sstevel@tonic-gate 		goto out;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 	i = 0;
5497c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5507c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5517c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5527c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5537c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5547c478bd9Sstevel@tonic-gate 			types = NULL;
5557c478bd9Sstevel@tonic-gate 			goto out;
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5587c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5597c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5607c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5617c478bd9Sstevel@tonic-gate 			types = NULL;
5627c478bd9Sstevel@tonic-gate 			goto out;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 		i++;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate out:
5677c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5687c478bd9Sstevel@tonic-gate 	return (types);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate static boolean_t
5727c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	uint_t i;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5777c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5787c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5797c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5807c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
585108322fbScarlsonj /*
586108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
587108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
588108322fbScarlsonj  */
5897c478bd9Sstevel@tonic-gate static void
590108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5917c478bd9Sstevel@tonic-gate {
592108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
593108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
594108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5997c478bd9Sstevel@tonic-gate  *
6007c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
6017c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
6027c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
6037c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
6047c478bd9Sstevel@tonic-gate  *
6057c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
6067c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
6077c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
6107c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
6117c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
6127c478bd9Sstevel@tonic-gate  *
6137c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
6147c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
6157c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
6167c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
6177c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
6187c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
6197c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
6207c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
6217c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
6227c478bd9Sstevel@tonic-gate  * graceful mounts first.
6237c478bd9Sstevel@tonic-gate  *
6247c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
6257c478bd9Sstevel@tonic-gate  */
6267c478bd9Sstevel@tonic-gate static int
627108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate 	int error = 0;
6307c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6317c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6327c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6337c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6347c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6357c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6367c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6377c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6407c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6417c478bd9Sstevel@tonic-gate 		return (-1);
6427c478bd9Sstevel@tonic-gate 	}
643108322fbScarlsonj 	if (unmount_cmd)
644108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6477c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6487c478bd9Sstevel@tonic-gate 
64945916cd2Sjpk 	/*
65045916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
65145916cd2Sjpk 	 * of our zone's /export/home
65245916cd2Sjpk 	 */
65348451833Scarlsonj 	if (!unmount_cmd)
65445916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
65545916cd2Sjpk 
6567c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6577c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6587c478bd9Sstevel@tonic-gate 		return (-1);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6627c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6657c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6667c478bd9Sstevel@tonic-gate 		error++;
6677c478bd9Sstevel@tonic-gate 		goto out;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/*
6717c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6727c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6737c478bd9Sstevel@tonic-gate 	 */
6747c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6757c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6767c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6777c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6787c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6797c478bd9Sstevel@tonic-gate 		char *path;
6807c478bd9Sstevel@tonic-gate 		uint_t i;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		mnts = NULL;
6837c478bd9Sstevel@tonic-gate 		nmnt = 0;
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6867c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6877c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6887c478bd9Sstevel@tonic-gate 		 * filesystems.
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6917c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6927c478bd9Sstevel@tonic-gate 			error++;
6937c478bd9Sstevel@tonic-gate 			goto out;
6947c478bd9Sstevel@tonic-gate 		}
6957c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6967c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6977c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6987c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6997c478bd9Sstevel@tonic-gate 			/*
7007c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
7017c478bd9Sstevel@tonic-gate 			 *
7027c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
7037c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
7047c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
7057c478bd9Sstevel@tonic-gate 			 */
7067c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
7077c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
7087c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7097c478bd9Sstevel@tonic-gate 			}
7107c478bd9Sstevel@tonic-gate 			/*
7117c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
7127c478bd9Sstevel@tonic-gate 			 */
7137c478bd9Sstevel@tonic-gate 			if (stuck) {
7147c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
7157c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7167c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
7177c478bd9Sstevel@tonic-gate 				} else {
7187c478bd9Sstevel@tonic-gate 					/*
7197c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
7207c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
7217c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
7227c478bd9Sstevel@tonic-gate 					 * bail.
7237c478bd9Sstevel@tonic-gate 					 */
7247c478bd9Sstevel@tonic-gate 					error++;
7257c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
7267c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
7277c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
7287c478bd9Sstevel@tonic-gate 					goto out;
7297c478bd9Sstevel@tonic-gate 				}
7307c478bd9Sstevel@tonic-gate 			}
7317c478bd9Sstevel@tonic-gate 			/*
7327c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7337c478bd9Sstevel@tonic-gate 			 */
7347c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7357c478bd9Sstevel@tonic-gate 				newcount++;
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7407c478bd9Sstevel@tonic-gate 			break;
7417c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7427c478bd9Sstevel@tonic-gate 			/*
7437c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7447c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7457c478bd9Sstevel@tonic-gate 			 */
7467c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		/*
7517c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7527c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7537c478bd9Sstevel@tonic-gate 		 */
7547c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7557c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7567c478bd9Sstevel@tonic-gate 			error++;
7577c478bd9Sstevel@tonic-gate 			goto out;
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate out:
7627c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7637c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7647c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate static int
7687c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7717c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate /*
7777c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7787c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7797c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7807c478bd9Sstevel@tonic-gate  *
7817c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7827c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7837c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7847c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7857c478bd9Sstevel@tonic-gate  * remains the case.
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate static int
7887c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7917c478bd9Sstevel@tonic-gate 	int child_status = 0;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/*
7947c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7957c478bd9Sstevel@tonic-gate 	 */
7967c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7977c478bd9Sstevel@tonic-gate 	child_pid = fork();
7987c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7997c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
8007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
8017c478bd9Sstevel@tonic-gate 		return (-1);
8027c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
8037c478bd9Sstevel@tonic-gate 		closefrom(0);
8041390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
8051390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
8061390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
8071390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
8087c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
8097c478bd9Sstevel@tonic-gate 		/*
8107c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
8117c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
8127c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
8137c478bd9Sstevel@tonic-gate 		 * accordingly.
8147c478bd9Sstevel@tonic-gate 		 */
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
8177c478bd9Sstevel@tonic-gate 	} else {
8187c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
8227c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8237c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
8247c478bd9Sstevel@tonic-gate 		return (-1);
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
8277c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
8287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8297c478bd9Sstevel@tonic-gate 		return (-1);
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate static int
83593239addSjohnlev isregfile(const char *path)
83693239addSjohnlev {
83793239addSjohnlev 	struct stat64 st;
83893239addSjohnlev 
83993239addSjohnlev 	if (stat64(path, &st) == -1)
84093239addSjohnlev 		return (-1);
84193239addSjohnlev 
84293239addSjohnlev 	return (S_ISREG(st.st_mode));
84393239addSjohnlev }
84493239addSjohnlev 
84593239addSjohnlev static int
8467c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
84957a250fbSbatschul 	char *argv[5];
8507c478bd9Sstevel@tonic-gate 	int status;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	/*
8537c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8547c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8557c478bd9Sstevel@tonic-gate 	 */
8567c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8579acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8587c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8597c478bd9Sstevel@tonic-gate 		return (-1);
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
86293239addSjohnlev 	/*
86393239addSjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
86493239addSjohnlev 	 * in zoneadm.
86593239addSjohnlev 	 */
86693239addSjohnlev 	if (isregfile(cmdbuf) == -1)
86793239addSjohnlev 		return (0);
86893239addSjohnlev 
8697c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
87057a250fbSbatschul 	argv[1] = "-o";
87157a250fbSbatschul 	argv[2] = "p";
87257a250fbSbatschul 	argv[3] = (char *)rawdev;
87357a250fbSbatschul 	argv[4] = NULL;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8767c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8777c478bd9Sstevel@tonic-gate 		return (status);
8787c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8797c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8807c478bd9Sstevel@tonic-gate 	return (-1);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate static int
8847c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8857c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8887c478bd9Sstevel@tonic-gate 	char *argv[6];
8897c478bd9Sstevel@tonic-gate 	int status;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	/*
8927c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8937c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8947c478bd9Sstevel@tonic-gate 	 */
8957c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8969acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8987c478bd9Sstevel@tonic-gate 		return (-1);
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
9017c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
9027c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
9037c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
9047c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
9057c478bd9Sstevel@tonic-gate 	} else {
9067c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
9077c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
9087c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
9097c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
9107c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
9147c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
9157c478bd9Sstevel@tonic-gate 		return (status);
9167c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
9177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
9187c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9197c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
9207c478bd9Sstevel@tonic-gate 	else
9217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
9227c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9237c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
9247c478bd9Sstevel@tonic-gate 	return (-1);
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate /*
928d314f035Sedp  * Check if a given mount point path exists.
929d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
930d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
931d314f035Sedp  * component of the mount point path, so it must be a directory.
932d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
933d314f035Sedp  * path, so the mount point itself can be anything aside from a
934d314f035Sedp  * symbolic link.
935d314f035Sedp  *
936d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
937d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
938d314f035Sedp  * If the path doesn't exist return a positive value.
9397c478bd9Sstevel@tonic-gate  */
9407c478bd9Sstevel@tonic-gate static int
941d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9447c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9457c478bd9Sstevel@tonic-gate 	int res;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9487c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
949d314f035Sedp 			return (1);
9507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9517c478bd9Sstevel@tonic-gate 		return (-1);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9557c478bd9Sstevel@tonic-gate 		return (-1);
9567c478bd9Sstevel@tonic-gate 	}
957d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9587c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9597c478bd9Sstevel@tonic-gate 		return (-1);
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9627c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9637c478bd9Sstevel@tonic-gate 		return (-1);
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9667c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9677c478bd9Sstevel@tonic-gate 		/*
968d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9697c478bd9Sstevel@tonic-gate 		 */
9707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9717c478bd9Sstevel@tonic-gate 		return (-1);
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 	return (0);
9747c478bd9Sstevel@tonic-gate }
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate /*
977d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
978d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
979d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
980d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
981d314f035Sedp  * object.  (Different filesystems can support different mount points,
982d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
983d314f035Sedp  * while "ufs" just supports directories.)
9847c478bd9Sstevel@tonic-gate  *
985d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
986d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
987d314f035Sedp  * If the path doesn't exist return a positive value.
9887c478bd9Sstevel@tonic-gate  */
989d314f035Sedp int
990d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
991d314f035Sedp     const char *dir, const char *fstype)
9927c478bd9Sstevel@tonic-gate {
993d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
994d314f035Sedp 	int rv;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	/*
997d314f035Sedp 	 * Sanity check the target mount point path.
998d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
9997c478bd9Sstevel@tonic-gate 	 */
1000d314f035Sedp 	if (dir[0] != '/') {
1001d314f035Sedp 		/* Something went wrong. */
1002d314f035Sedp 		zerror(zlogp, B_FALSE, "invalid mount directory, "
1003d314f035Sedp 		    "type: \"%s\", special: \"%s\", dir: \"%s\"",
1004d314f035Sedp 		    fstype, spec, dir);
1005d314f035Sedp 		return (-1);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
1008d314f035Sedp 	/*
1009d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
1010d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
1011d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
1012d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
1013d314f035Sedp 	 * cannonical path check in valid_mount_point()).
1014d314f035Sedp 	 */
1015d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
1016d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
1017d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
1018d314f035Sedp 		    rootpath, dir);
1019d314f035Sedp 		return (-1);
1020d314f035Sedp 	}
1021d314f035Sedp 
1022d314f035Sedp 	/*
1023d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
1024d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
1025d314f035Sedp 	 */
10267c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10277c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10287c478bd9Sstevel@tonic-gate 	do {
1029d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
10307c478bd9Sstevel@tonic-gate 		*slashp = '\0';
1031d314f035Sedp 		if (slashp_next != NULL) {
1032d314f035Sedp 			/* This is an intermediary mount path component. */
1033d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1034d314f035Sedp 		} else {
1035d314f035Sedp 			/* This is the last component of the mount path. */
1036d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1037d314f035Sedp 		}
1038d314f035Sedp 		if (rv < 0)
1039d314f035Sedp 			return (rv);
10407c478bd9Sstevel@tonic-gate 		*slashp = '/';
1041d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1042d314f035Sedp 	return (rv);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate static int
10469acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10479acbbeafSnn35248 {
10489acbbeafSnn35248 	di_prof_t prof = arg;
10499acbbeafSnn35248 
10509acbbeafSnn35248 	if (name == NULL)
10519acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
10529acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
10539acbbeafSnn35248 }
10549acbbeafSnn35248 
10559acbbeafSnn35248 static int
10569acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10579acbbeafSnn35248 {
10589acbbeafSnn35248 	di_prof_t prof = arg;
10599acbbeafSnn35248 
10609acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
10619acbbeafSnn35248 }
10629acbbeafSnn35248 
10632b24ab6bSSebastien Roy int
10642b24ab6bSSebastien Roy vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1065f4b3ec61Sdh155122 {
1066f4b3ec61Sdh155122 	zone_dochandle_t handle;
1067f4b3ec61Sdh155122 
1068f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
1069f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1070f4b3ec61Sdh155122 		return (-1);
1071f4b3ec61Sdh155122 	}
1072f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1073f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
1074f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1075f4b3ec61Sdh155122 		return (-1);
1076f4b3ec61Sdh155122 	}
1077f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1078f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1079f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1080f4b3ec61Sdh155122 		return (-1);
1081f4b3ec61Sdh155122 	}
1082f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1083f4b3ec61Sdh155122 	return (0);
1084f4b3ec61Sdh155122 }
1085f4b3ec61Sdh155122 
10869acbbeafSnn35248 /*
10879acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10889acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10899acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10909acbbeafSnn35248  */
10919acbbeafSnn35248 static int
10920679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10939acbbeafSnn35248 {
10949acbbeafSnn35248 	char			brand[MAXNAMELEN];
10959acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1096123807fbSedp 	brand_handle_t		bh = NULL;
10979acbbeafSnn35248 	struct zone_devtab	ztab;
10989acbbeafSnn35248 	di_prof_t		prof = NULL;
10999acbbeafSnn35248 	int			err;
11009acbbeafSnn35248 	int			retval = -1;
1101f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1102f4b3ec61Sdh155122 	const char		*curr_iptype;
11039acbbeafSnn35248 
11049acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
11059acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
11069acbbeafSnn35248 		goto cleanup;
11079acbbeafSnn35248 	}
11089acbbeafSnn35248 
11090679f794S 	/*
11100679f794S 	 * Get a handle to the brand info for this zone.
1111e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
11120679f794S 	 * brand device mounts.
11130679f794S 	 */
11140679f794S 	if (ALT_MOUNT(mount_cmd)) {
1115e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
11160679f794S 	} else {
111762868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
11180679f794S 	}
11190679f794S 
11200679f794S 	if ((bh = brand_open(brand)) == NULL) {
11219acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
11229acbbeafSnn35248 		goto cleanup;
11239acbbeafSnn35248 	}
11249acbbeafSnn35248 
11252b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
1126f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1127f4b3ec61Sdh155122 		goto cleanup;
1128f4b3ec61Sdh155122 	}
1129f4b3ec61Sdh155122 	switch (iptype) {
1130f4b3ec61Sdh155122 	case ZS_SHARED:
1131f4b3ec61Sdh155122 		curr_iptype = "shared";
1132f4b3ec61Sdh155122 		break;
1133f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1134f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1135f4b3ec61Sdh155122 		break;
1136f4b3ec61Sdh155122 	}
1137f4b3ec61Sdh155122 
1138123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1139f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11409acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11419acbbeafSnn35248 		goto cleanup;
11429acbbeafSnn35248 	}
11439acbbeafSnn35248 
1144123807fbSedp 	if (brand_platform_iter_link(bh,
11459acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11469acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11479acbbeafSnn35248 		goto cleanup;
11489acbbeafSnn35248 	}
11499acbbeafSnn35248 
11509acbbeafSnn35248 	/* Add user-specified devices and directories */
11519acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11529acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11539acbbeafSnn35248 		goto cleanup;
11549acbbeafSnn35248 	}
11559acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11569acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11579acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11589acbbeafSnn35248 		goto cleanup;
11599acbbeafSnn35248 	}
11609acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
11619acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11629acbbeafSnn35248 		    zonecfg_strerror(err));
11639acbbeafSnn35248 		goto cleanup;
11649acbbeafSnn35248 	}
11659acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11669acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11679acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
11689acbbeafSnn35248 			    "user-specified device");
11699acbbeafSnn35248 			goto cleanup;
11709acbbeafSnn35248 		}
11719acbbeafSnn35248 	}
11729acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
11739acbbeafSnn35248 
11749acbbeafSnn35248 	/* Send profile to kernel */
11759acbbeafSnn35248 	if (di_prof_commit(prof)) {
11769acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11779acbbeafSnn35248 		goto cleanup;
11789acbbeafSnn35248 	}
11799acbbeafSnn35248 
11809acbbeafSnn35248 	retval = 0;
11819acbbeafSnn35248 
11829acbbeafSnn35248 cleanup:
1183123807fbSedp 	if (bh != NULL)
1184123807fbSedp 		brand_close(bh);
1185e767a340Sgjelinek 	if (handle != NULL)
11869acbbeafSnn35248 		zonecfg_fini_handle(handle);
11879acbbeafSnn35248 	if (prof)
11889acbbeafSnn35248 		di_prof_fini(prof);
11899acbbeafSnn35248 	return (retval);
11909acbbeafSnn35248 }
11919acbbeafSnn35248 
11929acbbeafSnn35248 static int
11930679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11940679f794S     zone_mnt_t mount_cmd)
11957c478bd9Sstevel@tonic-gate {
11967c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
11977c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11987c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11999acbbeafSnn35248 	int rv;
12007c478bd9Sstevel@tonic-gate 
1201d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1202d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
12037c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
12047c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
12057c478bd9Sstevel@tonic-gate 		return (-1);
1206d314f035Sedp 	} else if (rv > 0) {
1207d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1208d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1209d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1210d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1211d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1212d314f035Sedp 			return (-1);
12137c478bd9Sstevel@tonic-gate 		}
12147c478bd9Sstevel@tonic-gate 
1215d314f035Sedp 		/*
1216d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1217d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1218d314f035Sedp 		 * to make sure that the mount point path is canonical,
1219d314f035Sedp 		 * which it can only do if the path exists, so now that
1220d314f035Sedp 		 * we've created the path we have to verify it again.
1221d314f035Sedp 		 */
1222d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1223d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1224d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1225d314f035Sedp 			zerror(zlogp, B_FALSE,
1226d314f035Sedp 			    "%s%s is not a valid mount point",
1227d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
12287c478bd9Sstevel@tonic-gate 			return (-1);
1229d314f035Sedp 		}
1230d314f035Sedp 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12337c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/*
12367c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12377c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
123848bbca81SDaniel Hoffman 	 * administrator initiated the operation via zoneadm(1m), they'll get
123948bbca81SDaniel Hoffman 	 * auto-verification which will let them know what's wrong.  If they
124048bbca81SDaniel Hoffman 	 * modify the zone configuration of a running zone, and don't attempt
124148bbca81SDaniel Hoffman 	 * to verify that it's OK, then we won't crash but won't bother trying
124248bbca81SDaniel Hoffman 	 * to be too helpful either. zoneadm verify is only a couple keystrokes
124348bbca81SDaniel Hoffman 	 * away.
12447c478bd9Sstevel@tonic-gate 	 */
12457c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12477c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12487c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12497c478bd9Sstevel@tonic-gate 		return (-1);
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/*
1253108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1254fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1255fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1256fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1257fa3d7ae1Sedp 	 * time we get here.
1258108322fbScarlsonj 	 */
1259108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1260108322fbScarlsonj 		struct stat64 st;
1261108322fbScarlsonj 
1262108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
126368eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1264fa3d7ae1Sedp 			/*
1265fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1266fa3d7ae1Sedp 			 * to check if that device is already mounted
1267fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1268fa3d7ae1Sedp 			 * of the device instead of a direct mount
1269fa3d7ae1Sedp 			 */
127068eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1271108322fbScarlsonj 				return (-1);
127268eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1273fa3d7ae1Sedp 			/*
1274fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1275fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1276fa3d7ae1Sedp 			 * this case in order to get at the underlying
1277fa3d7ae1Sedp 			 * read-write path.
1278fa3d7ae1Sedp 			 */
1279fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1280108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1281108322fbScarlsonj 		}
1282108322fbScarlsonj 	}
1283108322fbScarlsonj 
1284108322fbScarlsonj 	/*
12857c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12867c478bd9Sstevel@tonic-gate 	 */
12877c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
128893239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12897c478bd9Sstevel@tonic-gate 		return (-1);
129093239addSjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
129193239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
129293239addSjohnlev 		return (-1);
129393239addSjohnlev 	}
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
12977c478bd9Sstevel@tonic-gate 	 */
12987c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
12997c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
13007c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
13017c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
13027c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
13037c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
13047c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
13057c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
13067c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
13077c478bd9Sstevel@tonic-gate 		}
13087c478bd9Sstevel@tonic-gate 	}
13099acbbeafSnn35248 
13109acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13119acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
13129acbbeafSnn35248 		return (rv);
13139acbbeafSnn35248 
13149acbbeafSnn35248 	/*
13159acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13169acbbeafSnn35248 	 * we're done.
13179acbbeafSnn35248 	 */
13189acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13199acbbeafSnn35248 		return (0);
13209acbbeafSnn35248 
13219acbbeafSnn35248 	/*
13229acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13239acbbeafSnn35248 	 * need to configure it.
13249acbbeafSnn35248 	 */
13250679f794S 	return (mount_one_dev(zlogp, path, mount_cmd));
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate static void
13297c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13307c478bd9Sstevel@tonic-gate {
13317c478bd9Sstevel@tonic-gate 	uint_t i;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13347c478bd9Sstevel@tonic-gate 		return;
13357c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13367c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13377c478bd9Sstevel@tonic-gate 	free(fsarray);
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate 
1340108322fbScarlsonj /*
1341f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1342f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1343f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1344f4368d3dSvp157776  * is done this way because:
1345f4368d3dSvp157776  *	We need to have both /etc and /var in the root of the scratchzone.
1346f4368d3dSvp157776  *	We loopback mount zone's own /etc and /var into the root of the
1347f4368d3dSvp157776  *	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1348f4368d3dSvp157776  *	need to delay the mount of /var till the zone's root gets populated.
1349f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1350f4368d3dSvp157776  *	build_mounted_post_var() which gets called only after the zone
1351f4368d3dSvp157776  *	specific filesystems are mounted.
13526cfd72c6Sgjelinek  *
13536cfd72c6Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13546cfd72c6Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13556cfd72c6Sgjelinek  * scratch zone.
1356108322fbScarlsonj  */
1357108322fbScarlsonj static boolean_t
1358f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
135916bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1360108322fbScarlsonj {
1361108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1362108322fbScarlsonj 	const char **cpp;
1363108322fbScarlsonj 	static const char *mkdirs[] = {
13643f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13653f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1366108322fbScarlsonj 	};
1367108322fbScarlsonj 	char *altstr;
1368f4368d3dSvp157776 	FILE *fp;
1369108322fbScarlsonj 	uuid_t uuid;
1370108322fbScarlsonj 
1371108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
137216bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
137316bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1374108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1375108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1376108322fbScarlsonj 
1377108322fbScarlsonj 	/*
1378108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1379108322fbScarlsonj 	 * zone_mount_early.)
1380108322fbScarlsonj 	 */
1381108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1382108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1383108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1384108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1385108322fbScarlsonj 			return (B_FALSE);
1386108322fbScarlsonj 		}
1387108322fbScarlsonj 	}
1388f4368d3dSvp157776 	/*
1389f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1390f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1391f4368d3dSvp157776 	 * read-only inside the scratch zone.
1392f4368d3dSvp157776 	 */
1393f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1394f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1395f4368d3dSvp157776 	if (altstr == NULL) {
1396f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1397f4368d3dSvp157776 		return (B_FALSE);
1398f4368d3dSvp157776 	}
1399f4368d3dSvp157776 	zonecfg_set_root("");
1400f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1401f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1402f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1403f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1404f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1405f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1406f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1407f4368d3dSvp157776 			return (B_FALSE);
1408f4368d3dSvp157776 		}
14096e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1410f4368d3dSvp157776 		    tmp) != 0) {
1411f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1412f4368d3dSvp157776 			    fromdir);
1413f4368d3dSvp157776 			return (B_FALSE);
1414f4368d3dSvp157776 		}
1415f4368d3dSvp157776 	}
1416f4368d3dSvp157776 	zonecfg_set_root(altstr);
1417f4368d3dSvp157776 	free(altstr);
1418f4368d3dSvp157776 
1419f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1420f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1421f4368d3dSvp157776 		return (B_FALSE);
1422f4368d3dSvp157776 	}
1423f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1424f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1425f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1426f4368d3dSvp157776 	}
1427f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1428f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1429f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1430f4368d3dSvp157776 		return (B_FALSE);
1431f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1432f4368d3dSvp157776 	return (B_TRUE);
1433f4368d3dSvp157776 }
1434f4368d3dSvp157776 
1435f4368d3dSvp157776 
1436f4368d3dSvp157776 static boolean_t
14376cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14386cfd72c6Sgjelinek     const char *luroot)
1439f4368d3dSvp157776 {
1440f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1441f4368d3dSvp157776 	const char **cpp;
14426cfd72c6Sgjelinek 	const char **loopdirs;
14436cfd72c6Sgjelinek 	const char **tmpdirs;
1444f4368d3dSvp157776 	static const char *localdirs[] = {
1445f4368d3dSvp157776 		"/etc", "/var", NULL
1446f4368d3dSvp157776 	};
14476cfd72c6Sgjelinek 	static const char *scr_loopdirs[] = {
1448f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1449f4368d3dSvp157776 		"/usr", NULL
1450f4368d3dSvp157776 	};
14516cfd72c6Sgjelinek 	static const char *upd_loopdirs[] = {
14526cfd72c6Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14536cfd72c6Sgjelinek 		"/usr", "/var", NULL
14546cfd72c6Sgjelinek 	};
14556cfd72c6Sgjelinek 	static const char *scr_tmpdirs[] = {
1456f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1457f4368d3dSvp157776 	};
14586cfd72c6Sgjelinek 	static const char *upd_tmpdirs[] = {
14596cfd72c6Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14606cfd72c6Sgjelinek 	};
1461f4368d3dSvp157776 	struct stat st;
1462f4368d3dSvp157776 
14636cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
1464108322fbScarlsonj 		/*
14656cfd72c6Sgjelinek 		 * These are mounted read-write from the zone undergoing
14666cfd72c6Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14676cfd72c6Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
1468108322fbScarlsonj 		 */
1469108322fbScarlsonj 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14706cfd72c6Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1471108322fbScarlsonj 			    *cpp);
14726cfd72c6Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14736cfd72c6Sgjelinek 			    rootpath, *cpp);
1474108322fbScarlsonj 			if (mkdir(tmp, 0755) != 0) {
1475108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1476108322fbScarlsonj 				return (B_FALSE);
1477108322fbScarlsonj 			}
14786cfd72c6Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14796cfd72c6Sgjelinek 			    != 0) {
14806cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14816cfd72c6Sgjelinek 				    tmp, *cpp);
1482108322fbScarlsonj 				return (B_FALSE);
1483108322fbScarlsonj 			}
1484108322fbScarlsonj 		}
14856cfd72c6Sgjelinek 	}
14866cfd72c6Sgjelinek 
14876cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14886cfd72c6Sgjelinek 		loopdirs = upd_loopdirs;
14896cfd72c6Sgjelinek 	else
14906cfd72c6Sgjelinek 		loopdirs = scr_loopdirs;
1491108322fbScarlsonj 
1492108322fbScarlsonj 	/*
1493108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1494108322fbScarlsonj 	 * they contain binaries that must match system.
1495108322fbScarlsonj 	 */
1496108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1497108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1498108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1499108322fbScarlsonj 			if (errno != EEXIST) {
1500108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1501108322fbScarlsonj 				return (B_FALSE);
1502108322fbScarlsonj 			}
1503108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1504108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1505108322fbScarlsonj 				return (B_FALSE);
1506108322fbScarlsonj 			}
1507108322fbScarlsonj 			/*
1508108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1509108322fbScarlsonj 			 * things that have been converted into symlinks
1510108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1511108322fbScarlsonj 			 * fixup.
1512108322fbScarlsonj 			 */
1513108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1514108322fbScarlsonj 				continue;
1515108322fbScarlsonj 		}
15166e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1517108322fbScarlsonj 		    tmp) != 0) {
1518108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1519108322fbScarlsonj 			    *cpp);
1520108322fbScarlsonj 			return (B_FALSE);
1521108322fbScarlsonj 		}
1522108322fbScarlsonj 	}
1523108322fbScarlsonj 
15246cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15256cfd72c6Sgjelinek 		tmpdirs = upd_tmpdirs;
15266cfd72c6Sgjelinek 	else
15276cfd72c6Sgjelinek 		tmpdirs = scr_tmpdirs;
15286cfd72c6Sgjelinek 
1529108322fbScarlsonj 	/*
1530108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1531108322fbScarlsonj 	 */
1532108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1533108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15346cfd72c6Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15356cfd72c6Sgjelinek 		    errno != EEXIST) {
1536108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1537108322fbScarlsonj 			return (B_FALSE);
1538108322fbScarlsonj 		}
1539d30e996aSgjelinek 
1540d30e996aSgjelinek 		/*
1541d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1542d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1543d30e996aSgjelinek 		 * the correct mode for /tmp now.
1544d30e996aSgjelinek 		 */
1545d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1546d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1547d30e996aSgjelinek 			return (B_FALSE);
1548d30e996aSgjelinek 		}
1549d30e996aSgjelinek 
1550108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1551108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1552108322fbScarlsonj 			return (B_FALSE);
1553108322fbScarlsonj 		}
1554108322fbScarlsonj 	}
1555108322fbScarlsonj 	return (B_TRUE);
1556108322fbScarlsonj }
1557108322fbScarlsonj 
15589acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15599acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
15609acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
15619acbbeafSnn35248 	int			*pgcd_num_fs;
15629acbbeafSnn35248 } plat_gmount_cb_data_t;
15639acbbeafSnn35248 
15649acbbeafSnn35248 /*
15659acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15669acbbeafSnn35248  * through all global brand platform mounts.
15679acbbeafSnn35248  */
15689acbbeafSnn35248 int
15699acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15709acbbeafSnn35248     const char *fstype, const char *opt)
15719acbbeafSnn35248 {
15729acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
15739acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15749acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15759acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
15769acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15779acbbeafSnn35248 
15789acbbeafSnn35248 	num_fs++;
15799acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15809acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15819acbbeafSnn35248 		return (-1);
15829acbbeafSnn35248 	}
15839acbbeafSnn35248 
15849acbbeafSnn35248 	fs_ptr = tmp_ptr;
15859acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
15869acbbeafSnn35248 
15879acbbeafSnn35248 	/* update the callback struct passed in */
15889acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
15899acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
15909acbbeafSnn35248 
15919acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
15929acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15939acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
15949acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15959acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15969acbbeafSnn35248 	fsp->zone_fs_options = NULL;
1597fa3d7ae1Sedp 	if ((opt != NULL) &&
1598fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15999acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
16009acbbeafSnn35248 		return (-1);
16019acbbeafSnn35248 	}
16029acbbeafSnn35248 
16039acbbeafSnn35248 	return (0);
16049acbbeafSnn35248 }
16059acbbeafSnn35248 
16069acbbeafSnn35248 static int
16079acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16086cfd72c6Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16099acbbeafSnn35248 {
16109acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16119acbbeafSnn35248 	int num_fs;
16129acbbeafSnn35248 
16139acbbeafSnn35248 	num_fs = *num_fsp;
16149acbbeafSnn35248 	fs_ptr = *fs_tabp;
16159acbbeafSnn35248 
16169acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16179acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16189acbbeafSnn35248 		return (-1);
16199acbbeafSnn35248 	}
16209acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16219acbbeafSnn35248 		/*
16229acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
16239acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
16249acbbeafSnn35248 		 * case.
16259acbbeafSnn35248 		 */
16266cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16276cfd72c6Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16289acbbeafSnn35248 			continue;
16299acbbeafSnn35248 
16309acbbeafSnn35248 		num_fs++;
16319acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16329acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16339acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16349acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
16359acbbeafSnn35248 			return (-1);
16369acbbeafSnn35248 		}
16379acbbeafSnn35248 		/* update the pointers passed in */
16389acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16399acbbeafSnn35248 		*num_fsp = num_fs;
16409acbbeafSnn35248 
16419acbbeafSnn35248 		fs_ptr = tmp_ptr;
16429acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16439acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16449acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16459acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16469acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
16479acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16489acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
16499acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
1650fa3d7ae1Sedp 
1651fa3d7ae1Sedp 		/*
1652fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1653fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1654fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1655fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1656fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1657fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1658fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1659fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1660fa3d7ae1Sedp 		 */
1661fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1662fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1663fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1664fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1665fa3d7ae1Sedp 		}
1666fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1667fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
16689acbbeafSnn35248 	}
16699acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
16709acbbeafSnn35248 	return (0);
16719acbbeafSnn35248 }
16729acbbeafSnn35248 
16737c478bd9Sstevel@tonic-gate static int
16746cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
16757c478bd9Sstevel@tonic-gate {
16767c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
16777c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
16789acbbeafSnn35248 	char brand[MAXNAMELEN];
167916bca938Svp157776 	char luroot[MAXPATHLEN];
16809acbbeafSnn35248 	int i, num_fs = 0;
16819acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
16827c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
16837c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1684123807fbSedp 	brand_handle_t bh;
16859acbbeafSnn35248 	plat_gmount_cb_data_t cb;
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1688108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
16897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1690108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1691108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1692108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
16937c478bd9Sstevel@tonic-gate 		goto bad;
16947c478bd9Sstevel@tonic-gate 	}
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
16977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
16987c478bd9Sstevel@tonic-gate 		goto bad;
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
17027c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
17037c478bd9Sstevel@tonic-gate 		goto bad;
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1707ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17087c478bd9Sstevel@tonic-gate 		goto bad;
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17117c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17127c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17137c478bd9Sstevel@tonic-gate 		goto bad;
17147c478bd9Sstevel@tonic-gate 	}
17157c478bd9Sstevel@tonic-gate 
17160679f794S 	/*
1717e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
17180679f794S 	 * brand global mounts.
17190679f794S 	 */
17200679f794S 	if (ALT_MOUNT(mount_cmd)) {
1721e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
17220679f794S 	} else {
172362868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
17240679f794S 	}
17250679f794S 
17269acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
17270679f794S 	if ((bh = brand_open(brand)) == NULL) {
17289acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1729e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17309acbbeafSnn35248 		return (-1);
17319acbbeafSnn35248 	}
17329acbbeafSnn35248 
17339acbbeafSnn35248 	/*
17349acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
17359acbbeafSnn35248 	 * configuration.
17369acbbeafSnn35248 	 */
17379acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
17389acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
17399acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1740c6596baeSPatrick Mooney 	if (brand_platform_iter_gmounts(bh, zone_name, zonepath,
17419acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
17429acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1743123807fbSedp 		brand_close(bh);
1744e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17459acbbeafSnn35248 		return (-1);
17469acbbeafSnn35248 	}
1747123807fbSedp 	brand_close(bh);
17489acbbeafSnn35248 
17497c478bd9Sstevel@tonic-gate 	/*
17506e1ae2a3SGary Pennington 	 * Iterate through the rest of the filesystems. Sort them all,
17516e1ae2a3SGary Pennington 	 * then mount them in sorted order. This is to make sure the
17526e1ae2a3SGary Pennington 	 * higher level directories (e.g., /usr) get mounted before
17536e1ae2a3SGary Pennington 	 * any beneath them (e.g., /usr/local).
17547c478bd9Sstevel@tonic-gate 	 */
17559acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17569acbbeafSnn35248 	    mount_cmd) != 0)
17577c478bd9Sstevel@tonic-gate 		goto bad;
1758fa9e4066Sahrens 
17597c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17607c478bd9Sstevel@tonic-gate 	handle = NULL;
17617c478bd9Sstevel@tonic-gate 
1762108322fbScarlsonj 	/*
17639acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
17649acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
17659acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
17669acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
17679acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
17689acbbeafSnn35248 	 * the zones filesystems there instead.
17699acbbeafSnn35248 	 *
17709acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
17719acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
17729acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
17739acbbeafSnn35248 	 *
17749acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
17759acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
17769acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
17779acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
17789acbbeafSnn35248 	 * upgrade environment.)
17799acbbeafSnn35248 	 *
17809acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
17819acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
17829acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
17839acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
17849acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
17859acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
17869acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
17879acbbeafSnn35248 	 *
17889acbbeafSnn35248 	 * All this work is done in three phases:
1789f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1790f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1791f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1792f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1793108322fbScarlsonj 	 */
17946cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
179516bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1796108322fbScarlsonj 		goto bad;
1797108322fbScarlsonj 
17987c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
17999acbbeafSnn35248 
18007c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
18016cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
18029acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
18039acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
18049acbbeafSnn35248 
18059acbbeafSnn35248 			/*
18069acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
18079acbbeafSnn35248 			 * but /dev is special and always goes at the top
18089acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
18099acbbeafSnn35248 			 */
18109acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18119acbbeafSnn35248 			rootpath[slen] = '\0';
18120679f794S 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18130679f794S 			    != 0)
18149acbbeafSnn35248 				goto bad;
18159acbbeafSnn35248 			rootpath[slen] = '/';
18169acbbeafSnn35248 			continue;
18179acbbeafSnn35248 		}
18180679f794S 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18197c478bd9Sstevel@tonic-gate 			goto bad;
18207c478bd9Sstevel@tonic-gate 	}
18216cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18226cfd72c6Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1823f4368d3dSvp157776 		goto bad;
182445916cd2Sjpk 
182545916cd2Sjpk 	/*
182645916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
182745916cd2Sjpk 	 */
18286cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18296cfd72c6Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
183045916cd2Sjpk 		goto bad;
183145916cd2Sjpk 
18327c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/*
18357c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18367c478bd9Sstevel@tonic-gate 	 */
18377c478bd9Sstevel@tonic-gate 	return (0);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate bad:
18407c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18417c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18427c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18437c478bd9Sstevel@tonic-gate 	return (-1);
18447c478bd9Sstevel@tonic-gate }
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18477c478bd9Sstevel@tonic-gate static int
18487c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18497c478bd9Sstevel@tonic-gate {
18507c478bd9Sstevel@tonic-gate 	int prefixlen;
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18537c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18547c478bd9Sstevel@tonic-gate 		return (1);
18557c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
18567c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
18577c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18587c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
18597c478bd9Sstevel@tonic-gate 			continue;
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18627c478bd9Sstevel@tonic-gate 		prefixlen--;
18637c478bd9Sstevel@tonic-gate 	}
18647c478bd9Sstevel@tonic-gate 	return (0);
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate /*
18687c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
18697c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
18707c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
18717c478bd9Sstevel@tonic-gate  *
18727c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
18737c478bd9Sstevel@tonic-gate  */
18747c478bd9Sstevel@tonic-gate static int
1875f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
18767c478bd9Sstevel@tonic-gate {
18777c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
18787c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
18797c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
18807c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
18817c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
18827c478bd9Sstevel@tonic-gate 	uint_t bufsize;
18837c478bd9Sstevel@tonic-gate 	char *buf = NULL;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
18867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
18877c478bd9Sstevel@tonic-gate 		ret_code = -1;
18887c478bd9Sstevel@tonic-gate 		goto bad;
18897c478bd9Sstevel@tonic-gate 	}
18907c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
18917c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
18927c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
18937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1894f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
18957c478bd9Sstevel@tonic-gate 		ret_code = -1;
18967c478bd9Sstevel@tonic-gate 		goto bad;
18977c478bd9Sstevel@tonic-gate 	}
18987c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
18997c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
19007c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
19017c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
19027c478bd9Sstevel@tonic-gate 		ret_code = -1;
19037c478bd9Sstevel@tonic-gate 		goto bad;
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
19067c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19077c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19087c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19097c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1910f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1911f4b3ec61Sdh155122 		    "interfaces");
19127c478bd9Sstevel@tonic-gate 		ret_code = -1;
19137c478bd9Sstevel@tonic-gate 		goto bad;
19147c478bd9Sstevel@tonic-gate 	}
19157c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19167c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19177c478bd9Sstevel@tonic-gate 		(void) close(s);
19187c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19197c478bd9Sstevel@tonic-gate 		    0) {
19207c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19217c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
19227c478bd9Sstevel@tonic-gate 			ret_code = -1;
19237c478bd9Sstevel@tonic-gate 			continue;
19247c478bd9Sstevel@tonic-gate 		}
19257c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19267c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19277c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19287c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1929c1c0ebd5Ssl108498 			if (errno == ENXIO)
1930c1c0ebd5Ssl108498 				/*
1931c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1932c1c0ebd5Ssl108498 				 * another zone halting.
1933c1c0ebd5Ssl108498 				 */
1934c1c0ebd5Ssl108498 				continue;
19357c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1936c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1937f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19387c478bd9Sstevel@tonic-gate 			ret_code = -1;
19397c478bd9Sstevel@tonic-gate 			continue;
19407c478bd9Sstevel@tonic-gate 		}
19417c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19427c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19437c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1944f4b3ec61Sdh155122 				    "%s: could not remove network interface",
19457c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
19467c478bd9Sstevel@tonic-gate 				ret_code = -1;
19477c478bd9Sstevel@tonic-gate 				continue;
19487c478bd9Sstevel@tonic-gate 			}
19497c478bd9Sstevel@tonic-gate 		}
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate bad:
19527c478bd9Sstevel@tonic-gate 	if (s > 0)
19537c478bd9Sstevel@tonic-gate 		(void) close(s);
19547c478bd9Sstevel@tonic-gate 	if (buf)
19557c478bd9Sstevel@tonic-gate 		free(buf);
19567c478bd9Sstevel@tonic-gate 	return (ret_code);
19577c478bd9Sstevel@tonic-gate }
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate static union	sockunion {
19607c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
19617c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
19627c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19637c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19647c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate static struct {
19677c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
19687c478bd9Sstevel@tonic-gate 	char	space[512];
19697c478bd9Sstevel@tonic-gate } rtmsg;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate static int
19727c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
19737c478bd9Sstevel@tonic-gate {
19747c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
19757c478bd9Sstevel@tonic-gate 	case AF_INET:
19767c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
19777c478bd9Sstevel@tonic-gate 	case AF_LINK:
19787c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
19797c478bd9Sstevel@tonic-gate 	case AF_INET6:
19807c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
19817c478bd9Sstevel@tonic-gate 	default:
19827c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate }
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
19877c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate /*
19907c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
19917c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
19927c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
19937c478bd9Sstevel@tonic-gate  *
19947c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
19957c478bd9Sstevel@tonic-gate  *
19967c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
19977c478bd9Sstevel@tonic-gate  *
19987c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
19997c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
20007c478bd9Sstevel@tonic-gate  */
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate static char *
20037c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20047c478bd9Sstevel@tonic-gate {
20057c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
20067c478bd9Sstevel@tonic-gate 	pid_t pid;
20077c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
20087c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
20097c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20107c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
20117c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	pid = getpid();
20167c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20187c478bd9Sstevel@tonic-gate 		return (NULL);
20197c478bd9Sstevel@tonic-gate 	}
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20227c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20257c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20267c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20277c478bd9Sstevel@tonic-gate 	} else {
20287c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20317c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20327c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20337c478bd9Sstevel@tonic-gate 	}
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20387c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20397c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20407c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20417c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20427c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20457c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20467c478bd9Sstevel@tonic-gate 	cp += l;
20477c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20487c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20497c478bd9Sstevel@tonic-gate 	cp += l;
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20557c478bd9Sstevel@tonic-gate 		return (NULL);
20567c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20577c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20587c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20597c478bd9Sstevel@tonic-gate 		return (NULL);
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 	do {
20627c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20637c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20647c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20657c478bd9Sstevel@tonic-gate 	if (l < 0) {
20667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
20677c478bd9Sstevel@tonic-gate 		return (NULL);
20687c478bd9Sstevel@tonic-gate 	}
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
20717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
20727c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
20737c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
20747c478bd9Sstevel@tonic-gate 		return (NULL);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
20777c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
20787c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
20797c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
20807c478bd9Sstevel@tonic-gate 		return (NULL);
20817c478bd9Sstevel@tonic-gate 	}
20827c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
20837c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
20847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
20857c478bd9Sstevel@tonic-gate 		return (NULL);
20867c478bd9Sstevel@tonic-gate 	}
20877c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2088f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
20897c478bd9Sstevel@tonic-gate 		return (NULL);
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
20927c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
20937c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
20947c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
20957c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
20967c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
20977c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
20987c478bd9Sstevel@tonic-gate 			continue;
20997c478bd9Sstevel@tonic-gate 		}
21007c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
21017c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
21027c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
21037c478bd9Sstevel@tonic-gate 		break;
21047c478bd9Sstevel@tonic-gate 	}
21057c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2106f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2107f4b3ec61Sdh155122 		    "determined");
21087c478bd9Sstevel@tonic-gate 		return (NULL);
21097c478bd9Sstevel@tonic-gate 	}
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	/*
21127c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21137c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21147c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21157c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21167c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21177c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21187c478bd9Sstevel@tonic-gate 	 */
21197c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21207c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21217c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21227c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21237c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21247c478bd9Sstevel@tonic-gate 	if (i < 0) {
21257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2126f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2127f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
21287c478bd9Sstevel@tonic-gate 		return (NULL);
21297c478bd9Sstevel@tonic-gate 	}
21307c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21317c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21327c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21357c478bd9Sstevel@tonic-gate 		return (answer);
21367c478bd9Sstevel@tonic-gate 	return (NULL);
21377c478bd9Sstevel@tonic-gate }
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate /*
21407c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21417c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21427c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21437c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21447c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21457c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21467c478bd9Sstevel@tonic-gate  *
21477c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21487c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
21497c478bd9Sstevel@tonic-gate  */
21507c478bd9Sstevel@tonic-gate static int
21517c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2152f14f3ae7Sjv227347     struct zone_nwiftab *nwiftabptr)
21537c478bd9Sstevel@tonic-gate {
21547c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
21557c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
21567c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
21572e481403SVamsi Nagineni 	struct sockaddr_storage laddr;
21587c478bd9Sstevel@tonic-gate 	struct in_addr in4;
21597c478bd9Sstevel@tonic-gate 	sa_family_t af;
21607c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
21617c478bd9Sstevel@tonic-gate 	int s;
21627c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
2163f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	boolean_t is_loopback = B_FALSE;
21647c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
21657c478bd9Sstevel@tonic-gate 	int res;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
21687c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
21697c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
21707c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
21717c478bd9Sstevel@tonic-gate 		return (-1);
21727c478bd9Sstevel@tonic-gate 	}
21737c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
21747c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21757c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21767c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21777c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
21787c478bd9Sstevel@tonic-gate 		return (-1);
21797c478bd9Sstevel@tonic-gate 	}
21807c478bd9Sstevel@tonic-gate 
21812e481403SVamsi Nagineni 	/*
21822e481403SVamsi Nagineni 	 * This is a similar kind of "hack" like in addif() to get around
21832e481403SVamsi Nagineni 	 * the problem of SIOCLIFADDIF.  The problem is that this ioctl
21842e481403SVamsi Nagineni 	 * does not include the netmask when adding a logical interface.
21852e481403SVamsi Nagineni 	 * To get around this problem, we first add the logical interface
21862e481403SVamsi Nagineni 	 * with a 0 address.  After that, we set the netmask if provided.
21872e481403SVamsi Nagineni 	 * Finally we set the interface address.
21882e481403SVamsi Nagineni 	 */
21892e481403SVamsi Nagineni 	laddr = lifr.lifr_addr;
21907c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
21917c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
21922e481403SVamsi Nagineni 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
21932e481403SVamsi Nagineni 
21947c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
219522321485Svp157776 		/*
219622321485Svp157776 		 * Here, we know that the interface can't be brought up.
219722321485Svp157776 		 * A similar warning message was already printed out to
219822321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
219922321485Svp157776 		 * message to syslog and continue.
220022321485Svp157776 		 */
2201f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
220222321485Svp157776 		    "'%s' which may not be present/plumbed in the "
220322321485Svp157776 		    "global zone.", lifr.lifr_name);
22047c478bd9Sstevel@tonic-gate 		(void) close(s);
220522321485Svp157776 		return (Z_OK);
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22097c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22107c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22137c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2214f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2215f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
22167c478bd9Sstevel@tonic-gate 		goto bad;
22177c478bd9Sstevel@tonic-gate 	}
22187c478bd9Sstevel@tonic-gate 
2219f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	/*
2220f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * Loopback interface will use the default netmask assigned, if no
2221f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * netmask is found.
2222f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 */
22237c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2224f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		is_loopback = B_TRUE;
2225f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	}
22267c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
22277c478bd9Sstevel@tonic-gate 		/*
22287c478bd9Sstevel@tonic-gate 		 * The IPv4 netmask can be determined either
22297c478bd9Sstevel@tonic-gate 		 * directly if a prefix length was supplied with
22307c478bd9Sstevel@tonic-gate 		 * the address or via the netmasks database.  Not
22317c478bd9Sstevel@tonic-gate 		 * being able to determine it is a common failure,
22327c478bd9Sstevel@tonic-gate 		 * but it often is not fatal to operation of the
22337c478bd9Sstevel@tonic-gate 		 * interface.  In that case, a warning will be
22347c478bd9Sstevel@tonic-gate 		 * printed after the rest of the interface's
22357c478bd9Sstevel@tonic-gate 		 * parameters have been configured.
22367c478bd9Sstevel@tonic-gate 		 */
22377c478bd9Sstevel@tonic-gate 		(void) memset(&netmask4, 0, sizeof (netmask4));
22387c478bd9Sstevel@tonic-gate 		if (slashp != NULL) {
22397c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22407c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask4.sin_addr) != 0) {
22417c478bd9Sstevel@tonic-gate 				*slashp = '/';
22427c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22437c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22447c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
22457c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22467c478bd9Sstevel@tonic-gate 				goto bad;
22477c478bd9Sstevel@tonic-gate 			}
22487c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22497c478bd9Sstevel@tonic-gate 		} else if (getnetmaskbyaddr(in4,
22507c478bd9Sstevel@tonic-gate 		    &netmask4.sin_addr) == 0) {
22517c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22527c478bd9Sstevel@tonic-gate 		}
22537c478bd9Sstevel@tonic-gate 		if (got_netmask) {
22547c478bd9Sstevel@tonic-gate 			netmask4.sin_family = af;
22557c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask4,
22567c478bd9Sstevel@tonic-gate 			    sizeof (netmask4));
22577c478bd9Sstevel@tonic-gate 		}
22587c478bd9Sstevel@tonic-gate 	} else {
22597c478bd9Sstevel@tonic-gate 		(void) memset(&netmask6, 0, sizeof (netmask6));
22607c478bd9Sstevel@tonic-gate 		if (addr2netmask(slashp + 1, V6_ADDR_LEN,
22617c478bd9Sstevel@tonic-gate 		    (uchar_t *)&netmask6.sin6_addr) != 0) {
22627c478bd9Sstevel@tonic-gate 			*slashp = '/';
22637c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
22647c478bd9Sstevel@tonic-gate 			    "%s: invalid prefix length in %s",
22657c478bd9Sstevel@tonic-gate 			    lifr.lifr_name,
22667c478bd9Sstevel@tonic-gate 			    nwiftabptr->zone_nwif_address);
22677c478bd9Sstevel@tonic-gate 			goto bad;
22687c478bd9Sstevel@tonic-gate 		}
22697c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;
22707c478bd9Sstevel@tonic-gate 		netmask6.sin6_family = af;
22717c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &netmask6,
22727c478bd9Sstevel@tonic-gate 		    sizeof (netmask6));
22737c478bd9Sstevel@tonic-gate 	}
22747c478bd9Sstevel@tonic-gate 	if (got_netmask &&
22757c478bd9Sstevel@tonic-gate 	    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
22767c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not set netmask",
22777c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22787c478bd9Sstevel@tonic-gate 		goto bad;
22797c478bd9Sstevel@tonic-gate 	}
22807c478bd9Sstevel@tonic-gate 
22812e481403SVamsi Nagineni 	/* Set the interface address */
22822e481403SVamsi Nagineni 	lifr.lifr_addr = laddr;
22837c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
22852e481403SVamsi Nagineni 		    "%s: could not set IP address to %s",
22862e481403SVamsi Nagineni 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22877c478bd9Sstevel@tonic-gate 		goto bad;
22887c478bd9Sstevel@tonic-gate 	}
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22917c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
22927c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22937c478bd9Sstevel@tonic-gate 		goto bad;
22947c478bd9Sstevel@tonic-gate 	}
22957c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
22967c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
22977c478bd9Sstevel@tonic-gate 		int save_errno = errno;
22987c478bd9Sstevel@tonic-gate 		char *zone_using;
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 		/*
23017c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
23027c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
23037c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23047c478bd9Sstevel@tonic-gate 		 * using that address.
23057c478bd9Sstevel@tonic-gate 		 */
23067c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23077c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2308f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2309f4b3ec61Sdh155122 			    lifr.lifr_name);
23107c478bd9Sstevel@tonic-gate 			goto bad;
23117c478bd9Sstevel@tonic-gate 		}
23127c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23137c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23147c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23157c478bd9Sstevel@tonic-gate 			goto bad;
23167c478bd9Sstevel@tonic-gate 		}
23177c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23187c478bd9Sstevel@tonic-gate 		errno = save_errno;
23197c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
23207c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2321f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2322f4b3ec61Sdh155122 			    lifr.lifr_name);
23237c478bd9Sstevel@tonic-gate 		else
2324f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2325f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2326f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
23277c478bd9Sstevel@tonic-gate 		goto bad;
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 
2330f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (!got_netmask && !is_loopback) {
23317c478bd9Sstevel@tonic-gate 		/*
23327c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23337c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23347c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23357c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23367c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23377c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23387c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23397c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23407c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23417c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
23427c478bd9Sstevel@tonic-gate 		 */
23437c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23447c478bd9Sstevel@tonic-gate 		void  *addr;
2345e11c3f44Smeem 		const char *nomatch = "no matching subnet found in netmasks(4)";
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
23487c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23497c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23507c478bd9Sstevel@tonic-gate 		else
23517c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
23527c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
23537c478bd9Sstevel@tonic-gate 
2354e11c3f44Smeem 		/*
2355e11c3f44Smeem 		 * Find out what netmask the interface is going to be using.
2356e11c3f44Smeem 		 * If we just brought up an IPMP data address on an underlying
2357e11c3f44Smeem 		 * interface above, the address will have already migrated, so
2358e11c3f44Smeem 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
2359e11c3f44Smeem 		 * to bring the address up to get the actual netmask).  Just
2360e11c3f44Smeem 		 * omit printing the actual netmask in this corner-case.
2361e11c3f44Smeem 		 */
23627c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2363e11c3f44Smeem 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2364e11c3f44Smeem 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2365e11c3f44Smeem 			    nomatch);
2366e11c3f44Smeem 		} else {
23677c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
2368e11c3f44Smeem 			    "WARNING: %s: %s: %s; using default of %s.",
2369e11c3f44Smeem 			    lifr.lifr_name, nomatch, addrstr4, buffer);
2370e11c3f44Smeem 		}
23717c478bd9Sstevel@tonic-gate 	}
23727c478bd9Sstevel@tonic-gate 
2373de860bd9Sgfaden 	/*
2374de860bd9Sgfaden 	 * If a default router was specified for this interface
2375de860bd9Sgfaden 	 * set the route now. Ignore if already set.
2376de860bd9Sgfaden 	 */
2377de860bd9Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2378de860bd9Sgfaden 		int status;
2379de860bd9Sgfaden 		char *argv[7];
2380de860bd9Sgfaden 
2381de860bd9Sgfaden 		argv[0] = "route";
2382de860bd9Sgfaden 		argv[1] = "add";
2383de860bd9Sgfaden 		argv[2] = "-ifp";
2384de860bd9Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
2385de860bd9Sgfaden 		argv[4] = "default";
2386de860bd9Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2387de860bd9Sgfaden 		argv[6] = NULL;
2388de860bd9Sgfaden 
2389de860bd9Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2390de860bd9Sgfaden 		if (status != 0 && status != EEXIST)
2391de860bd9Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
2392de860bd9Sgfaden 			    "interface %s to %s\n",
2393de860bd9Sgfaden 			    nwiftabptr->zone_nwif_physical,
2394de860bd9Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
2395de860bd9Sgfaden 	}
2396de860bd9Sgfaden 
23977c478bd9Sstevel@tonic-gate 	(void) close(s);
23987c478bd9Sstevel@tonic-gate 	return (Z_OK);
23997c478bd9Sstevel@tonic-gate bad:
24007c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
24017c478bd9Sstevel@tonic-gate 	(void) close(s);
24027c478bd9Sstevel@tonic-gate 	return (-1);
24037c478bd9Sstevel@tonic-gate }
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate /*
24067c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
2407f14f3ae7Sjv227347  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2408f14f3ae7Sjv227347  * system.
24097c478bd9Sstevel@tonic-gate  *
24107c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24117c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
24127c478bd9Sstevel@tonic-gate  */
24137c478bd9Sstevel@tonic-gate static int
2414f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24157c478bd9Sstevel@tonic-gate {
24167c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24177c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24187c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24227c478bd9Sstevel@tonic-gate 		return (-1);
24237c478bd9Sstevel@tonic-gate 	}
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24267c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24277c478bd9Sstevel@tonic-gate 		return (-1);
24287c478bd9Sstevel@tonic-gate 	}
24297c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24307c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24317c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24327c478bd9Sstevel@tonic-gate 		return (-1);
24337c478bd9Sstevel@tonic-gate 	}
24347c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24357c478bd9Sstevel@tonic-gate 		for (;;) {
24367c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24377c478bd9Sstevel@tonic-gate 				break;
2438f14f3ae7Sjv227347 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24397c478bd9Sstevel@tonic-gate 			    Z_OK) {
24407c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24417c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24427c478bd9Sstevel@tonic-gate 				return (-1);
24437c478bd9Sstevel@tonic-gate 			}
24447c478bd9Sstevel@tonic-gate 		}
24457c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24467c478bd9Sstevel@tonic-gate 	}
24477c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
2448fdb7141fSgfaden 	if (is_system_labeled()) {
2449fdb7141fSgfaden 		/*
2450fdb7141fSgfaden 		 * Labeled zones share the loopback interface
2451fdb7141fSgfaden 		 * so it is not plumbed for shared stack instances.
2452fdb7141fSgfaden 		 */
2453fdb7141fSgfaden 		return (0);
2454fdb7141fSgfaden 	}
24557c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
24567c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
24577c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
24587c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2459442d3e9eSgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2460f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24617c478bd9Sstevel@tonic-gate 		return (-1);
2462f14f3ae7Sjv227347 
2463f14f3ae7Sjv227347 	/* Always plumb up the IPv6 loopback interface. */
24647c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
24657c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2466f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24677c478bd9Sstevel@tonic-gate 		return (-1);
24687c478bd9Sstevel@tonic-gate 	return (0);
24697c478bd9Sstevel@tonic-gate }
24707c478bd9Sstevel@tonic-gate 
247189b1c373Smeem static void
247289b1c373Smeem zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
247389b1c373Smeem {
247489b1c373Smeem 	char errmsg[DLADM_STRSIZE];
247589b1c373Smeem 
247689b1c373Smeem 	(void) dladm_status2str(err, errmsg);
247789b1c373Smeem 	zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
247889b1c373Smeem }
247989b1c373Smeem 
2480f4b3ec61Sdh155122 static int
24812b24ab6bSSebastien Roy add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2482f4b3ec61Sdh155122 {
248389b1c373Smeem 	dladm_status_t err;
24840dc2366fSVenugopal Iyer 	boolean_t cpuset, poolset;
2485efd4c9b6SSteve Lawrence 	char *poolp;
248689b1c373Smeem 
2487f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2488f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2489f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
249089b1c373Smeem 		zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
249189b1c373Smeem 		    "'%s' which is used in the global zone", dlname);
2492f4b3ec61Sdh155122 		return (-1);
2493f4b3ec61Sdh155122 	}
2494f4b3ec61Sdh155122 
2495d62bc4baSyz147064 	/* Set zoneid of this link. */
24962b24ab6bSSebastien Roy 	err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
24972b24ab6bSSebastien Roy 	    DLADM_OPT_ACTIVE);
249889b1c373Smeem 	if (err != DLADM_STATUS_OK) {
249989b1c373Smeem 		zdlerror(zlogp, err, dlname,
250089b1c373Smeem 		    "WARNING: unable to add network interface");
2501f4b3ec61Sdh155122 		return (-1);
2502f4b3ec61Sdh155122 	}
25030dc2366fSVenugopal Iyer 
25040dc2366fSVenugopal Iyer 	/*
25050dc2366fSVenugopal Iyer 	 * Set the pool of this link if the zone has a pool and
25060dc2366fSVenugopal Iyer 	 * neither the cpus nor the pool datalink property is
25070dc2366fSVenugopal Iyer 	 * already set.
25080dc2366fSVenugopal Iyer 	 */
25090dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25100dc2366fSVenugopal Iyer 	    "cpus", &cpuset);
25110dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
25120dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
25130dc2366fSVenugopal Iyer 		    "WARNING: unable to check if cpus link property is set");
25140dc2366fSVenugopal Iyer 	}
25150dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25160dc2366fSVenugopal Iyer 	    "pool", &poolset);
25170dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
25180dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
25190dc2366fSVenugopal Iyer 		    "WARNING: unable to check if pool link property is set");
25200dc2366fSVenugopal Iyer 	}
25210dc2366fSVenugopal Iyer 
25220dc2366fSVenugopal Iyer 	if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2523efd4c9b6SSteve Lawrence 		poolp = pool_name;
25240dc2366fSVenugopal Iyer 		err = dladm_set_linkprop(dld_handle, linkid, "pool",
2525efd4c9b6SSteve Lawrence 		    &poolp, 1, DLADM_OPT_ACTIVE);
25260dc2366fSVenugopal Iyer 		if (err != DLADM_STATUS_OK) {
25270dc2366fSVenugopal Iyer 			zerror(zlogp, B_FALSE, "WARNING: unable to set "
25280dc2366fSVenugopal Iyer 			    "pool %s to datalink %s", pool_name, dlname);
2529a3002e0aSGarrett D'Amore 			bzero(pool_name, sizeof (pool_name));
25300dc2366fSVenugopal Iyer 		}
25310dc2366fSVenugopal Iyer 	} else {
2532a3002e0aSGarrett D'Amore 		bzero(pool_name, sizeof (pool_name));
25330dc2366fSVenugopal Iyer 	}
2534f4b3ec61Sdh155122 	return (0);
2535f4b3ec61Sdh155122 }
2536f4b3ec61Sdh155122 
2537550b6e40SSowmini Varadhan static boolean_t
2538550b6e40SSowmini Varadhan sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
2539550b6e40SSowmini Varadhan     char *straddr, size_t len)
2540550b6e40SSowmini Varadhan {
2541550b6e40SSowmini Varadhan 	struct sockaddr_in *sin;
2542550b6e40SSowmini Varadhan 	struct sockaddr_in6 *sin6;
2543550b6e40SSowmini Varadhan 	const char *str = NULL;
2544550b6e40SSowmini Varadhan 
2545550b6e40SSowmini Varadhan 	if (af == AF_INET) {
2546550b6e40SSowmini Varadhan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2547550b6e40SSowmini Varadhan 		sin = SIN(sockaddr);
2548550b6e40SSowmini Varadhan 		str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
2549550b6e40SSowmini Varadhan 	} else if (af == AF_INET6) {
2550550b6e40SSowmini Varadhan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2551550b6e40SSowmini Varadhan 		sin6 = SIN6(sockaddr);
2552550b6e40SSowmini Varadhan 		str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
2553550b6e40SSowmini Varadhan 		    len);
2554550b6e40SSowmini Varadhan 	}
2555550b6e40SSowmini Varadhan 
2556550b6e40SSowmini Varadhan 	return (str != NULL);
2557550b6e40SSowmini Varadhan }
2558550b6e40SSowmini Varadhan 
2559550b6e40SSowmini Varadhan static int
2560550b6e40SSowmini Varadhan ipv4_prefixlen(struct sockaddr_in *sin)
2561550b6e40SSowmini Varadhan {
2562550b6e40SSowmini Varadhan 	struct sockaddr_in *m;
2563550b6e40SSowmini Varadhan 	struct sockaddr_storage mask;
2564550b6e40SSowmini Varadhan 
2565550b6e40SSowmini Varadhan 	m = SIN(&mask);
2566550b6e40SSowmini Varadhan 	m->sin_family = AF_INET;
2567550b6e40SSowmini Varadhan 	if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
256864639aafSDarren Reed 		return (mask2plen((struct sockaddr *)&mask));
2569550b6e40SSowmini Varadhan 	} else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
2570550b6e40SSowmini Varadhan 		return (8);
2571550b6e40SSowmini Varadhan 	} else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
2572550b6e40SSowmini Varadhan 		return (16);
2573550b6e40SSowmini Varadhan 	} else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
2574550b6e40SSowmini Varadhan 		return (24);
2575550b6e40SSowmini Varadhan 	}
2576550b6e40SSowmini Varadhan 	return (0);
2577550b6e40SSowmini Varadhan }
2578550b6e40SSowmini Varadhan 
2579550b6e40SSowmini Varadhan static int
2580550b6e40SSowmini Varadhan zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
2581550b6e40SSowmini Varadhan     void *buf, size_t bufsize)
2582550b6e40SSowmini Varadhan {
2583550b6e40SSowmini Varadhan 	zone_net_data_t *zndata;
2584550b6e40SSowmini Varadhan 	size_t znsize;
2585550b6e40SSowmini Varadhan 	int err;
2586550b6e40SSowmini Varadhan 
2587550b6e40SSowmini Varadhan 	znsize = sizeof (*zndata) + bufsize;
2588550b6e40SSowmini Varadhan 	zndata = calloc(1, znsize);
2589550b6e40SSowmini Varadhan 	if (zndata == NULL)
2590550b6e40SSowmini Varadhan 		return (ENOMEM);
2591550b6e40SSowmini Varadhan 	zndata->zn_type = type;
2592550b6e40SSowmini Varadhan 	zndata->zn_len = bufsize;
2593550b6e40SSowmini Varadhan 	zndata->zn_linkid = linkid;
2594550b6e40SSowmini Varadhan 	bcopy(buf, zndata->zn_val, zndata->zn_len);
2595550b6e40SSowmini Varadhan 	err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
2596550b6e40SSowmini Varadhan 	free(zndata);
2597550b6e40SSowmini Varadhan 	return (err);
2598550b6e40SSowmini Varadhan }
2599550b6e40SSowmini Varadhan 
2600550b6e40SSowmini Varadhan static int
2601550b6e40SSowmini Varadhan add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
2602550b6e40SSowmini Varadhan {
2603550b6e40SSowmini Varadhan 	struct lifreq lifr;
2604550b6e40SSowmini Varadhan 	char **astr, *address;
2605550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
2606550b6e40SSowmini Varadhan 	char *ip_nospoof = "ip-nospoof";
2607550b6e40SSowmini Varadhan 	int nnet, naddr, err = 0, j;
2608550b6e40SSowmini Varadhan 	size_t zlen, cpleft;
2609550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *end;
2610550b6e40SSowmini Varadhan 	char  tmp[INET6_ADDRSTRLEN], *maskstr;
2611550b6e40SSowmini Varadhan 	char *zaddr, *cp;
2612550b6e40SSowmini Varadhan 	struct in6_addr *routes = NULL;
2613550b6e40SSowmini Varadhan 	boolean_t is_set;
2614550b6e40SSowmini Varadhan 	datalink_id_t linkid;
2615550b6e40SSowmini Varadhan 
2616550b6e40SSowmini Varadhan 	assert(start != NULL);
2617550b6e40SSowmini Varadhan 	naddr = 0; /* number of addresses */
2618550b6e40SSowmini Varadhan 	nnet = 0; /* number of net resources */
2619550b6e40SSowmini Varadhan 	linkid = start->za_linkid;
2620550b6e40SSowmini Varadhan 	for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
2621550b6e40SSowmini Varadhan 	    ptr = ptr->za_next) {
2622550b6e40SSowmini Varadhan 		nnet++;
2623550b6e40SSowmini Varadhan 	}
2624550b6e40SSowmini Varadhan 	end = ptr;
2625550b6e40SSowmini Varadhan 	zlen = nnet * (INET6_ADDRSTRLEN + 1);
2626550b6e40SSowmini Varadhan 	astr = calloc(1, nnet * sizeof (uintptr_t));
2627550b6e40SSowmini Varadhan 	zaddr = calloc(1, zlen);
2628550b6e40SSowmini Varadhan 	if (astr == NULL || zaddr == NULL) {
2629550b6e40SSowmini Varadhan 		err = ENOMEM;
2630550b6e40SSowmini Varadhan 		goto done;
2631550b6e40SSowmini Varadhan 	}
2632550b6e40SSowmini Varadhan 	cp = zaddr;
2633550b6e40SSowmini Varadhan 	cpleft = zlen;
2634550b6e40SSowmini Varadhan 	j = 0;
2635550b6e40SSowmini Varadhan 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
2636550b6e40SSowmini Varadhan 		address = ptr->za_nwiftab.zone_nwif_allowed_address;
2637550b6e40SSowmini Varadhan 		if (address[0] == '\0')
2638550b6e40SSowmini Varadhan 			continue;
2639550b6e40SSowmini Varadhan 		(void) snprintf(tmp, sizeof (tmp), "%s", address);
2640550b6e40SSowmini Varadhan 		/*
2641550b6e40SSowmini Varadhan 		 * Validate the data. zonecfg_valid_net_address() clobbers
2642550b6e40SSowmini Varadhan 		 * the /<mask> in the address string.
2643550b6e40SSowmini Varadhan 		 */
2644550b6e40SSowmini Varadhan 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2645550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "invalid address [%s]\n",
2646550b6e40SSowmini Varadhan 			    address);
2647550b6e40SSowmini Varadhan 			err = EINVAL;
2648550b6e40SSowmini Varadhan 			goto done;
2649550b6e40SSowmini Varadhan 		}
2650550b6e40SSowmini Varadhan 		/*
2651550b6e40SSowmini Varadhan 		 * convert any hostnames to numeric address strings.
2652550b6e40SSowmini Varadhan 		 */
2653550b6e40SSowmini Varadhan 		if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
2654550b6e40SSowmini Varadhan 		    (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
2655550b6e40SSowmini Varadhan 			err = EINVAL;
2656550b6e40SSowmini Varadhan 			goto done;
2657550b6e40SSowmini Varadhan 		}
2658550b6e40SSowmini Varadhan 		/*
2659550b6e40SSowmini Varadhan 		 * make a copy of the numeric string for the data needed
2660550b6e40SSowmini Varadhan 		 * by the "allowed-ips" datalink property.
2661550b6e40SSowmini Varadhan 		 */
2662550b6e40SSowmini Varadhan 		astr[j] = strdup(cp);
2663550b6e40SSowmini Varadhan 		if (astr[j] == NULL) {
2664550b6e40SSowmini Varadhan 			err = ENOMEM;
2665550b6e40SSowmini Varadhan 			goto done;
2666550b6e40SSowmini Varadhan 		}
2667550b6e40SSowmini Varadhan 		j++;
2668550b6e40SSowmini Varadhan 		/*
2669550b6e40SSowmini Varadhan 		 * compute the default netmask from the address, if necessary
2670550b6e40SSowmini Varadhan 		 */
2671550b6e40SSowmini Varadhan 		if ((maskstr = strchr(tmp, '/')) == NULL) {
2672550b6e40SSowmini Varadhan 			int prefixlen;
2673550b6e40SSowmini Varadhan 
2674550b6e40SSowmini Varadhan 			if (lifr.lifr_addr.ss_family == AF_INET) {
2675550b6e40SSowmini Varadhan 				prefixlen = ipv4_prefixlen(
2676550b6e40SSowmini Varadhan 				    SIN(&lifr.lifr_addr));
2677550b6e40SSowmini Varadhan 			} else {
2678550b6e40SSowmini Varadhan 				struct sockaddr_in6 *sin6;
2679550b6e40SSowmini Varadhan 
2680550b6e40SSowmini Varadhan 				sin6 = SIN6(&lifr.lifr_addr);
2681550b6e40SSowmini Varadhan 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2682550b6e40SSowmini Varadhan 					prefixlen = 10;
2683550b6e40SSowmini Varadhan 				else
2684550b6e40SSowmini Varadhan 					prefixlen = 64;
2685550b6e40SSowmini Varadhan 			}
2686550b6e40SSowmini Varadhan 			(void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
2687550b6e40SSowmini Varadhan 			maskstr = tmp;
2688550b6e40SSowmini Varadhan 		} else {
2689550b6e40SSowmini Varadhan 			maskstr++;
2690550b6e40SSowmini Varadhan 		}
2691550b6e40SSowmini Varadhan 		/* append the "/<netmask>" */
2692550b6e40SSowmini Varadhan 		(void) strlcat(cp, "/", cpleft);
2693550b6e40SSowmini Varadhan 		(void) strlcat(cp, maskstr, cpleft);
2694550b6e40SSowmini Varadhan 		(void) strlcat(cp, ",", cpleft);
2695550b6e40SSowmini Varadhan 		cp += strnlen(cp, zlen);
2696550b6e40SSowmini Varadhan 		cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
2697550b6e40SSowmini Varadhan 	}
2698550b6e40SSowmini Varadhan 	naddr = j; /* the actual number of addresses in the net resource */
2699550b6e40SSowmini Varadhan 	assert(naddr <= nnet);
2700550b6e40SSowmini Varadhan 
2701550b6e40SSowmini Varadhan 	/*
2702550b6e40SSowmini Varadhan 	 * zonecfg has already verified that the defrouter property can only
2703550b6e40SSowmini Varadhan 	 * be set if there is at least one address defined for the net resource.
2704550b6e40SSowmini Varadhan 	 * If j is 0, there are no addresses defined, and therefore no routers
2705550b6e40SSowmini Varadhan 	 * to configure, and we are done at that point.
2706550b6e40SSowmini Varadhan 	 */
2707550b6e40SSowmini Varadhan 	if (j == 0)
2708550b6e40SSowmini Varadhan 		goto done;
2709550b6e40SSowmini Varadhan 
2710550b6e40SSowmini Varadhan 	/* over-write last ',' with '\0' */
2711*af34582fSAndy Fiddaman 	zaddr[strnlen(zaddr, zlen) - 1] = '\0';
2712550b6e40SSowmini Varadhan 
2713550b6e40SSowmini Varadhan 	/*
2714550b6e40SSowmini Varadhan 	 * First make sure L3 protection is not already set on the link.
2715550b6e40SSowmini Varadhan 	 */
2716550b6e40SSowmini Varadhan 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2717550b6e40SSowmini Varadhan 	    "protection", &is_set);
2718550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2719550b6e40SSowmini Varadhan 		err = EINVAL;
2720550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "unable to check if protection is set");
2721550b6e40SSowmini Varadhan 		goto done;
2722550b6e40SSowmini Varadhan 	}
2723550b6e40SSowmini Varadhan 	if (is_set) {
2724550b6e40SSowmini Varadhan 		err = EINVAL;
2725550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "Protection is already set");
2726550b6e40SSowmini Varadhan 		goto done;
2727550b6e40SSowmini Varadhan 	}
2728550b6e40SSowmini Varadhan 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2729550b6e40SSowmini Varadhan 	    "allowed-ips", &is_set);
2730550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2731550b6e40SSowmini Varadhan 		err = EINVAL;
2732550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
2733550b6e40SSowmini Varadhan 		goto done;
2734550b6e40SSowmini Varadhan 	}
2735550b6e40SSowmini Varadhan 	if (is_set) {
2736550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "allowed-ips is already set");
2737550b6e40SSowmini Varadhan 		err = EINVAL;
2738550b6e40SSowmini Varadhan 		goto done;
2739550b6e40SSowmini Varadhan 	}
2740550b6e40SSowmini Varadhan 
2741550b6e40SSowmini Varadhan 	/*
2742550b6e40SSowmini Varadhan 	 * Enable ip-nospoof for the link, and add address to the allowed-ips
2743550b6e40SSowmini Varadhan 	 * list.
2744550b6e40SSowmini Varadhan 	 */
2745550b6e40SSowmini Varadhan 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
2746550b6e40SSowmini Varadhan 	    &ip_nospoof, 1, DLADM_OPT_ACTIVE);
2747550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2748550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "could not set protection\n");
2749550b6e40SSowmini Varadhan 		err = EINVAL;
2750550b6e40SSowmini Varadhan 		goto done;
2751550b6e40SSowmini Varadhan 	}
2752550b6e40SSowmini Varadhan 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
2753550b6e40SSowmini Varadhan 	    astr, naddr, DLADM_OPT_ACTIVE);
2754550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2755550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
2756550b6e40SSowmini Varadhan 		err = EINVAL;
2757550b6e40SSowmini Varadhan 		goto done;
2758550b6e40SSowmini Varadhan 	}
2759550b6e40SSowmini Varadhan 
2760550b6e40SSowmini Varadhan 	/* now set the address in the data-store */
2761550b6e40SSowmini Varadhan 	err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
2762550b6e40SSowmini Varadhan 	    zaddr, strnlen(zaddr, zlen) + 1);
2763550b6e40SSowmini Varadhan 	if (err != 0)
2764550b6e40SSowmini Varadhan 		goto done;
2765550b6e40SSowmini Varadhan 
2766550b6e40SSowmini Varadhan 	/*
2767550b6e40SSowmini Varadhan 	 * add the defaultrouters
2768550b6e40SSowmini Varadhan 	 */
2769550b6e40SSowmini Varadhan 	routes = calloc(1, nnet * sizeof (*routes));
2770550b6e40SSowmini Varadhan 	j = 0;
2771550b6e40SSowmini Varadhan 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
2772550b6e40SSowmini Varadhan 		address = ptr->za_nwiftab.zone_nwif_defrouter;
2773550b6e40SSowmini Varadhan 		if (address[0] == '\0')
2774550b6e40SSowmini Varadhan 			continue;
2775550b6e40SSowmini Varadhan 		if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
2776550b6e40SSowmini Varadhan 			/*
2777550b6e40SSowmini Varadhan 			 * zonecfg_valid_net_address() expects numeric IPv6
2778550b6e40SSowmini Varadhan 			 * addresses to have a CIDR format netmask.
2779550b6e40SSowmini Varadhan 			 */
2780550b6e40SSowmini Varadhan 			(void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
2781550b6e40SSowmini Varadhan 			(void) strlcat(address, tmp, INET6_ADDRSTRLEN);
2782550b6e40SSowmini Varadhan 		}
2783550b6e40SSowmini Varadhan 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2784550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE,
2785550b6e40SSowmini Varadhan 			    "invalid router [%s]\n", address);
2786550b6e40SSowmini Varadhan 			err = EINVAL;
2787550b6e40SSowmini Varadhan 			goto done;
2788550b6e40SSowmini Varadhan 		}
2789550b6e40SSowmini Varadhan 		if (lifr.lifr_addr.ss_family == AF_INET6) {
2790550b6e40SSowmini Varadhan 			routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
2791550b6e40SSowmini Varadhan 		} else {
2792550b6e40SSowmini Varadhan 			IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
2793550b6e40SSowmini Varadhan 			    &routes[j]);
2794550b6e40SSowmini Varadhan 		}
2795550b6e40SSowmini Varadhan 		j++;
2796550b6e40SSowmini Varadhan 	}
2797550b6e40SSowmini Varadhan 	assert(j <= nnet);
2798550b6e40SSowmini Varadhan 	if (j > 0) {
2799550b6e40SSowmini Varadhan 		err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
2800550b6e40SSowmini Varadhan 		    linkid, routes, j * sizeof (*routes));
2801550b6e40SSowmini Varadhan 	}
2802550b6e40SSowmini Varadhan done:
2803550b6e40SSowmini Varadhan 	free(routes);
2804550b6e40SSowmini Varadhan 	for (j = 0; j < naddr; j++)
2805550b6e40SSowmini Varadhan 		free(astr[j]);
2806550b6e40SSowmini Varadhan 	free(astr);
2807550b6e40SSowmini Varadhan 	free(zaddr);
2808550b6e40SSowmini Varadhan 	return (err);
2809550b6e40SSowmini Varadhan 
2810550b6e40SSowmini Varadhan }
2811550b6e40SSowmini Varadhan 
2812550b6e40SSowmini Varadhan static int
2813550b6e40SSowmini Varadhan add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
2814550b6e40SSowmini Varadhan {
2815550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr;
2816550b6e40SSowmini Varadhan 	datalink_id_t linkid;
2817550b6e40SSowmini Varadhan 	int err;
2818550b6e40SSowmini Varadhan 
2819550b6e40SSowmini Varadhan 	if (zalist == NULL)
2820550b6e40SSowmini Varadhan 		return (0);
2821550b6e40SSowmini Varadhan 
2822550b6e40SSowmini Varadhan 	linkid = zalist->za_linkid;
2823550b6e40SSowmini Varadhan 
2824550b6e40SSowmini Varadhan 	err = add_net_for_linkid(zlogp, zoneid, zalist);
2825550b6e40SSowmini Varadhan 	if (err != 0)
2826550b6e40SSowmini Varadhan 		return (err);
2827550b6e40SSowmini Varadhan 
2828550b6e40SSowmini Varadhan 	for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
2829550b6e40SSowmini Varadhan 		if (ptr->za_linkid == linkid)
2830550b6e40SSowmini Varadhan 			continue;
2831550b6e40SSowmini Varadhan 		linkid = ptr->za_linkid;
2832550b6e40SSowmini Varadhan 		err = add_net_for_linkid(zlogp, zoneid, ptr);
2833550b6e40SSowmini Varadhan 		if (err != 0)
2834550b6e40SSowmini Varadhan 			return (err);
2835550b6e40SSowmini Varadhan 	}
2836550b6e40SSowmini Varadhan 	return (0);
2837550b6e40SSowmini Varadhan }
2838550b6e40SSowmini Varadhan 
2839550b6e40SSowmini Varadhan /*
2840550b6e40SSowmini Varadhan  * Add "new" to the list of network interfaces to be configured  by
2841550b6e40SSowmini Varadhan  * add_net on zone boot in "old". The list of interfaces in "old" is
2842550b6e40SSowmini Varadhan  * sorted by datalink_id_t, with interfaces sorted FIFO for a given
2843550b6e40SSowmini Varadhan  * datalink_id_t.
2844550b6e40SSowmini Varadhan  *
2845550b6e40SSowmini Varadhan  * Returns the merged list of IP interfaces containing "old" and "new"
2846550b6e40SSowmini Varadhan  */
2847550b6e40SSowmini Varadhan static zone_addr_list_t *
2848550b6e40SSowmini Varadhan add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
2849550b6e40SSowmini Varadhan {
2850550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *next;
2851550b6e40SSowmini Varadhan 	datalink_id_t linkid = new->za_linkid;
2852550b6e40SSowmini Varadhan 
2853550b6e40SSowmini Varadhan 	assert(old != new);
2854550b6e40SSowmini Varadhan 
2855550b6e40SSowmini Varadhan 	if (old == NULL)
2856550b6e40SSowmini Varadhan 		return (new);
2857550b6e40SSowmini Varadhan 	for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
2858550b6e40SSowmini Varadhan 		if (ptr->za_linkid == linkid)
2859550b6e40SSowmini Varadhan 			break;
2860550b6e40SSowmini Varadhan 	}
2861550b6e40SSowmini Varadhan 	if (ptr == NULL) {
2862550b6e40SSowmini Varadhan 		/* linkid does not already exist, add to the beginning */
2863550b6e40SSowmini Varadhan 		new->za_next = old;
2864550b6e40SSowmini Varadhan 		return (new);
2865550b6e40SSowmini Varadhan 	}
2866550b6e40SSowmini Varadhan 	/*
2867550b6e40SSowmini Varadhan 	 * adding to the middle of the list; ptr points at the first
2868550b6e40SSowmini Varadhan 	 * occurrence of linkid. Find the last occurrence.
2869550b6e40SSowmini Varadhan 	 */
2870550b6e40SSowmini Varadhan 	while ((next = ptr->za_next) != NULL) {
2871550b6e40SSowmini Varadhan 		if (next->za_linkid != linkid)
2872550b6e40SSowmini Varadhan 			break;
2873550b6e40SSowmini Varadhan 		ptr = next;
2874550b6e40SSowmini Varadhan 	}
2875550b6e40SSowmini Varadhan 	/* insert new after ptr */
2876550b6e40SSowmini Varadhan 	new->za_next = next;
2877550b6e40SSowmini Varadhan 	ptr->za_next = new;
2878550b6e40SSowmini Varadhan 	return (old);
2879550b6e40SSowmini Varadhan }
2880550b6e40SSowmini Varadhan 
2881550b6e40SSowmini Varadhan void
2882550b6e40SSowmini Varadhan free_ip_interface(zone_addr_list_t *zalist)
2883550b6e40SSowmini Varadhan {
2884550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *new;
2885550b6e40SSowmini Varadhan 
2886550b6e40SSowmini Varadhan 	for (ptr = zalist; ptr != NULL; ) {
2887550b6e40SSowmini Varadhan 		new = ptr;
2888550b6e40SSowmini Varadhan 		ptr = ptr->za_next;
2889550b6e40SSowmini Varadhan 		free(new);
2890550b6e40SSowmini Varadhan 	}
2891550b6e40SSowmini Varadhan }
2892550b6e40SSowmini Varadhan 
2893f4b3ec61Sdh155122 /*
2894f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2895f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2896f4b3ec61Sdh155122  * whatever we set up, and return an error.
2897f4b3ec61Sdh155122  */
2898f4b3ec61Sdh155122 static int
2899550b6e40SSowmini Varadhan configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2900f4b3ec61Sdh155122 {
2901f4b3ec61Sdh155122 	zone_dochandle_t handle;
2902f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2903f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2904f4b3ec61Sdh155122 	char path[MAXPATHLEN];
29052b24ab6bSSebastien Roy 	datalink_id_t linkid;
2906f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2907f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2908550b6e40SSowmini Varadhan 	zone_addr_list_t *zalist = NULL, *new;
2909f4b3ec61Sdh155122 
2910f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2911f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2912f4b3ec61Sdh155122 		return (-1);
2913f4b3ec61Sdh155122 	}
2914f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2915f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2916f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2917f4b3ec61Sdh155122 		return (-1);
2918f4b3ec61Sdh155122 	}
2919f4b3ec61Sdh155122 
2920f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2921f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2922f4b3ec61Sdh155122 		return (0);
2923f4b3ec61Sdh155122 	}
2924f4b3ec61Sdh155122 
2925f4b3ec61Sdh155122 	for (;;) {
2926f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2927f4b3ec61Sdh155122 			break;
2928f4b3ec61Sdh155122 
2929f4b3ec61Sdh155122 		if (prof == NULL) {
2930f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2931f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2932f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2933f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2934f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2935f4b3ec61Sdh155122 				    "unable to determine dev root");
2936f4b3ec61Sdh155122 				return (-1);
2937f4b3ec61Sdh155122 			}
2938f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2939f4b3ec61Sdh155122 			    "/dev");
2940f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2941f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2942f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2943f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2944f4b3ec61Sdh155122 				    "failed to initialize profile");
2945f4b3ec61Sdh155122 				return (-1);
2946f4b3ec61Sdh155122 			}
2947f4b3ec61Sdh155122 		}
2948f4b3ec61Sdh155122 
2949f4b3ec61Sdh155122 		/*
2950d62bc4baSyz147064 		 * Create the /dev entry for backward compatibility.
2951f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2952d62bc4baSyz147064 		 * Note that the zone still boots when the assigned
2953d62bc4baSyz147064 		 * interface is inaccessible, used by others, etc.
2954d62bc4baSyz147064 		 * Also, when vanity naming is used, some interface do
2955d62bc4baSyz147064 		 * do not have corresponding /dev node names (for example,
2956d62bc4baSyz147064 		 * vanity named aggregations).  The /dev entry is not
2957d62bc4baSyz147064 		 * created in that case.  The /dev/net entry is always
2958d62bc4baSyz147064 		 * accessible.
2959f4b3ec61Sdh155122 		 */
29602b24ab6bSSebastien Roy 		if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
29612b24ab6bSSebastien Roy 		    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
29622b24ab6bSSebastien Roy 		    add_datalink(zlogp, zone_name, linkid,
29632b24ab6bSSebastien Roy 		    nwiftab.zone_nwif_physical) == 0) {
29643bc21d0aSAruna Ramakrishna - Sun Microsystems 			added = B_TRUE;
29653bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
2966f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2967f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
29683bc21d0aSAruna Ramakrishna - Sun Microsystems 			zerror(zlogp, B_TRUE, "failed to add network device");
2969f4b3ec61Sdh155122 			return (-1);
2970f4b3ec61Sdh155122 		}
2971550b6e40SSowmini Varadhan 		/* set up the new IP interface, and add them all later */
2972550b6e40SSowmini Varadhan 		new = malloc(sizeof (*new));
2973550b6e40SSowmini Varadhan 		if (new == NULL) {
2974550b6e40SSowmini Varadhan 			zerror(zlogp, B_TRUE, "no memory for %s",
2975550b6e40SSowmini Varadhan 			    nwiftab.zone_nwif_physical);
2976550b6e40SSowmini Varadhan 			zonecfg_fini_handle(handle);
2977550b6e40SSowmini Varadhan 			free_ip_interface(zalist);
2978550b6e40SSowmini Varadhan 		}
2979550b6e40SSowmini Varadhan 		bzero(new, sizeof (*new));
2980550b6e40SSowmini Varadhan 		new->za_nwiftab = nwiftab;
2981550b6e40SSowmini Varadhan 		new->za_linkid = linkid;
2982550b6e40SSowmini Varadhan 		zalist = add_ip_interface(zalist, new);
2983550b6e40SSowmini Varadhan 	}
2984550b6e40SSowmini Varadhan 	if (zalist != NULL) {
2985550b6e40SSowmini Varadhan 		if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
2986550b6e40SSowmini Varadhan 			(void) zonecfg_endnwifent(handle);
2987550b6e40SSowmini Varadhan 			zonecfg_fini_handle(handle);
2988550b6e40SSowmini Varadhan 			zerror(zlogp, B_TRUE, "failed to add address");
2989550b6e40SSowmini Varadhan 			free_ip_interface(zalist);
2990550b6e40SSowmini Varadhan 			return (-1);
2991550b6e40SSowmini Varadhan 		}
2992550b6e40SSowmini Varadhan 		free_ip_interface(zalist);
2993d62bc4baSyz147064 	}
2994f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2995f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2996f4b3ec61Sdh155122 
2997f4b3ec61Sdh155122 	if (prof != NULL && added) {
2998f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2999f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
3000f4b3ec61Sdh155122 			return (-1);
3001f4b3ec61Sdh155122 		}
3002f4b3ec61Sdh155122 	}
3003f4b3ec61Sdh155122 	if (prof != NULL)
3004f4b3ec61Sdh155122 		di_prof_fini(prof);
3005f4b3ec61Sdh155122 
3006f4b3ec61Sdh155122 	return (0);
3007f4b3ec61Sdh155122 }
3008f4b3ec61Sdh155122 
3009f4b3ec61Sdh155122 static int
30100dc2366fSVenugopal Iyer remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
30110dc2366fSVenugopal Iyer {
30120dc2366fSVenugopal Iyer 	ushort_t flags;
30130dc2366fSVenugopal Iyer 	zone_iptype_t iptype;
30140dc2366fSVenugopal Iyer 	int i, dlnum = 0;
30150dc2366fSVenugopal Iyer 	datalink_id_t *dllink, *dllinks = NULL;
30160dc2366fSVenugopal Iyer 	dladm_status_t err;
30170dc2366fSVenugopal Iyer 
30180dc2366fSVenugopal Iyer 	if (strlen(pool_name) == 0)
30190dc2366fSVenugopal Iyer 		return (0);
30200dc2366fSVenugopal Iyer 
30210dc2366fSVenugopal Iyer 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
30220dc2366fSVenugopal Iyer 	    sizeof (flags)) < 0) {
30230dc2366fSVenugopal Iyer 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
3024550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
30250dc2366fSVenugopal Iyer 			return (-1);
30260dc2366fSVenugopal Iyer 		}
30270dc2366fSVenugopal Iyer 	} else {
30280dc2366fSVenugopal Iyer 		if (flags & ZF_NET_EXCL)
30290dc2366fSVenugopal Iyer 			iptype = ZS_EXCLUSIVE;
30300dc2366fSVenugopal Iyer 		else
30310dc2366fSVenugopal Iyer 			iptype = ZS_SHARED;
30320dc2366fSVenugopal Iyer 	}
30330dc2366fSVenugopal Iyer 
30340dc2366fSVenugopal Iyer 	if (iptype == ZS_EXCLUSIVE) {
30350dc2366fSVenugopal Iyer 		/*
30360dc2366fSVenugopal Iyer 		 * Get the datalink count and for each datalink,
30370dc2366fSVenugopal Iyer 		 * attempt to clear the pool property and clear
30380dc2366fSVenugopal Iyer 		 * the pool_name.
30390dc2366fSVenugopal Iyer 		 */
30400dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
30410dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to count network "
30420dc2366fSVenugopal Iyer 			    "interfaces");
30430dc2366fSVenugopal Iyer 			return (-1);
30440dc2366fSVenugopal Iyer 		}
30450dc2366fSVenugopal Iyer 
30460dc2366fSVenugopal Iyer 		if (dlnum == 0)
30470dc2366fSVenugopal Iyer 			return (0);
30480dc2366fSVenugopal Iyer 
30490dc2366fSVenugopal Iyer 		if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
30500dc2366fSVenugopal Iyer 		    == NULL) {
30510dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "memory allocation failed");
30520dc2366fSVenugopal Iyer 			return (-1);
30530dc2366fSVenugopal Iyer 		}
30540dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
30550dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to list network "
30560dc2366fSVenugopal Iyer 			    "interfaces");
30570dc2366fSVenugopal Iyer 			return (-1);
30580dc2366fSVenugopal Iyer 		}
30590dc2366fSVenugopal Iyer 
3060a3002e0aSGarrett D'Amore 		bzero(pool_name, sizeof (pool_name));
30610dc2366fSVenugopal Iyer 		for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
30620dc2366fSVenugopal Iyer 			err = dladm_set_linkprop(dld_handle, *dllink, "pool",
30630dc2366fSVenugopal Iyer 			    NULL, 0, DLADM_OPT_ACTIVE);
30640dc2366fSVenugopal Iyer 			if (err != DLADM_STATUS_OK) {
30650dc2366fSVenugopal Iyer 				zerror(zlogp, B_TRUE,
30660dc2366fSVenugopal Iyer 				    "WARNING: unable to clear pool");
30670dc2366fSVenugopal Iyer 			}
30680dc2366fSVenugopal Iyer 		}
30690dc2366fSVenugopal Iyer 		free(dllinks);
30700dc2366fSVenugopal Iyer 	}
30710dc2366fSVenugopal Iyer 	return (0);
30720dc2366fSVenugopal Iyer }
30730dc2366fSVenugopal Iyer 
30740dc2366fSVenugopal Iyer static int
3075550b6e40SSowmini Varadhan remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
3076550b6e40SSowmini Varadhan {
3077550b6e40SSowmini Varadhan 	ushort_t flags;
3078550b6e40SSowmini Varadhan 	zone_iptype_t iptype;
3079550b6e40SSowmini Varadhan 	int i, dlnum = 0;
3080550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
3081550b6e40SSowmini Varadhan 	datalink_id_t *dllink, *dllinks = NULL;
3082550b6e40SSowmini Varadhan 
3083550b6e40SSowmini Varadhan 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3084550b6e40SSowmini Varadhan 	    sizeof (flags)) < 0) {
3085550b6e40SSowmini Varadhan 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
3086550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
3087550b6e40SSowmini Varadhan 			return (-1);
3088550b6e40SSowmini Varadhan 		}
3089550b6e40SSowmini Varadhan 	} else {
3090550b6e40SSowmini Varadhan 		if (flags & ZF_NET_EXCL)
3091550b6e40SSowmini Varadhan 			iptype = ZS_EXCLUSIVE;
3092550b6e40SSowmini Varadhan 		else
3093550b6e40SSowmini Varadhan 			iptype = ZS_SHARED;
3094550b6e40SSowmini Varadhan 	}
3095550b6e40SSowmini Varadhan 
3096550b6e40SSowmini Varadhan 	if (iptype != ZS_EXCLUSIVE)
3097550b6e40SSowmini Varadhan 		return (0);
3098550b6e40SSowmini Varadhan 
3099550b6e40SSowmini Varadhan 	/*
3100550b6e40SSowmini Varadhan 	 * Get the datalink count and for each datalink,
3101550b6e40SSowmini Varadhan 	 * attempt to clear the pool property and clear
3102550b6e40SSowmini Varadhan 	 * the pool_name.
3103550b6e40SSowmini Varadhan 	 */
3104550b6e40SSowmini Varadhan 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3105550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "unable to count network interfaces");
3106550b6e40SSowmini Varadhan 		return (-1);
3107550b6e40SSowmini Varadhan 	}
3108550b6e40SSowmini Varadhan 
3109550b6e40SSowmini Varadhan 	if (dlnum == 0)
3110550b6e40SSowmini Varadhan 		return (0);
3111550b6e40SSowmini Varadhan 
3112550b6e40SSowmini Varadhan 	if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
3113550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "memory allocation failed");
3114550b6e40SSowmini Varadhan 		return (-1);
3115550b6e40SSowmini Varadhan 	}
3116550b6e40SSowmini Varadhan 	if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3117550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
3118550b6e40SSowmini Varadhan 		free(dllinks);
3119550b6e40SSowmini Varadhan 		return (-1);
3120550b6e40SSowmini Varadhan 	}
3121550b6e40SSowmini Varadhan 
3122550b6e40SSowmini Varadhan 	for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3123fff7ec1dSSowmini Varadhan 		char dlerr[DLADM_STRSIZE];
3124fff7ec1dSSowmini Varadhan 
3125550b6e40SSowmini Varadhan 		dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3126550b6e40SSowmini Varadhan 		    "protection", NULL, 0, DLADM_OPT_ACTIVE);
3127fff7ec1dSSowmini Varadhan 		if (dlstatus == DLADM_STATUS_NOTFOUND) {
3128fff7ec1dSSowmini Varadhan 			/* datalink does not belong to the GZ */
3129fff7ec1dSSowmini Varadhan 			continue;
3130fff7ec1dSSowmini Varadhan 		}
3131550b6e40SSowmini Varadhan 		if (dlstatus != DLADM_STATUS_OK) {
3132fff7ec1dSSowmini Varadhan 			zerror(zlogp, B_FALSE,
3133fff7ec1dSSowmini Varadhan 			    dladm_status2str(dlstatus, dlerr));
3134550b6e40SSowmini Varadhan 			free(dllinks);
3135550b6e40SSowmini Varadhan 			return (-1);
3136550b6e40SSowmini Varadhan 		}
3137550b6e40SSowmini Varadhan 		dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3138550b6e40SSowmini Varadhan 		    "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
3139550b6e40SSowmini Varadhan 		if (dlstatus != DLADM_STATUS_OK) {
3140fff7ec1dSSowmini Varadhan 			zerror(zlogp, B_FALSE,
3141fff7ec1dSSowmini Varadhan 			    dladm_status2str(dlstatus, dlerr));
3142550b6e40SSowmini Varadhan 			free(dllinks);
3143550b6e40SSowmini Varadhan 			return (-1);
3144550b6e40SSowmini Varadhan 		}
3145550b6e40SSowmini Varadhan 	}
3146550b6e40SSowmini Varadhan 	free(dllinks);
3147550b6e40SSowmini Varadhan 	return (0);
3148550b6e40SSowmini Varadhan }
3149550b6e40SSowmini Varadhan 
3150550b6e40SSowmini Varadhan static int
31512b24ab6bSSebastien Roy unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
3152f4b3ec61Sdh155122 {
31532b24ab6bSSebastien Roy 	int dlnum = 0;
3154f4b3ec61Sdh155122 
31552b24ab6bSSebastien Roy 	/*
31562b24ab6bSSebastien Roy 	 * The kernel shutdown callback for the dls module should have removed
31572b24ab6bSSebastien Roy 	 * all datalinks from this zone.  If any remain, then there's a
31582b24ab6bSSebastien Roy 	 * problem.
31592b24ab6bSSebastien Roy 	 */
3160f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3161f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
3162f4b3ec61Sdh155122 		return (-1);
3163f4b3ec61Sdh155122 	}
31642b24ab6bSSebastien Roy 	if (dlnum != 0) {
31652b24ab6bSSebastien Roy 		zerror(zlogp, B_FALSE,
31662b24ab6bSSebastien Roy 		    "datalinks remain in zone after shutdown");
3167f4b3ec61Sdh155122 		return (-1);
3168f4b3ec61Sdh155122 	}
3169f4b3ec61Sdh155122 	return (0);
3170f4b3ec61Sdh155122 }
3171f4b3ec61Sdh155122 
31727c478bd9Sstevel@tonic-gate static int
31737c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
31747c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
31757c478bd9Sstevel@tonic-gate {
31767c478bd9Sstevel@tonic-gate 	int fd;
31777c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
31787c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
31797c478bd9Sstevel@tonic-gate 	int error;
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
31827c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
31837c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
31847c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
31857c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
31887c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
31897c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
31907c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
31937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
31947c478bd9Sstevel@tonic-gate 		return (-1);
31957c478bd9Sstevel@tonic-gate 	}
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
31987c478bd9Sstevel@tonic-gate 	(void) close(fd);
31997c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
32007c478bd9Sstevel@tonic-gate 		return (0);
32017c478bd9Sstevel@tonic-gate 	return (-1);
32027c478bd9Sstevel@tonic-gate }
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate static int
32057c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
32067c478bd9Sstevel@tonic-gate {
32077c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
32087c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
32097c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
32107c478bd9Sstevel@tonic-gate 	int error;
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	/*
32137c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
32147c478bd9Sstevel@tonic-gate 	 */
32157c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
32167c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
32177c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
32187c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
32197c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
32227c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
32237c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
32247c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
32257c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32287c478bd9Sstevel@tonic-gate 		return (error);
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 	/*
32317c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
32327c478bd9Sstevel@tonic-gate 	 */
32337c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
32347c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
32357c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
32367c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
32377c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
32407c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
32417c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
32427c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
32437c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32467c478bd9Sstevel@tonic-gate 		return (error);
32477c478bd9Sstevel@tonic-gate 	return (0);
32487c478bd9Sstevel@tonic-gate }
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate static int
32516cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
3252ffbafc53Scomay {
3253ffbafc53Scomay 	int error = -1;
3254ffbafc53Scomay 	zone_dochandle_t handle;
3255ffbafc53Scomay 	char *privname = NULL;
3256ffbafc53Scomay 
3257ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
3258ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3259ffbafc53Scomay 		return (-1);
3260ffbafc53Scomay 	}
3261ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3262ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
3263ffbafc53Scomay 		zonecfg_fini_handle(handle);
3264ffbafc53Scomay 		return (-1);
3265ffbafc53Scomay 	}
3266ffbafc53Scomay 
32676cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
3268bf1d7e28Sdh155122 		zone_iptype_t	iptype;
3269bf1d7e28Sdh155122 		const char	*curr_iptype;
3270bf1d7e28Sdh155122 
3271bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
3272bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
3273bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
3274bf1d7e28Sdh155122 			return (-1);
3275bf1d7e28Sdh155122 		}
3276bf1d7e28Sdh155122 
3277bf1d7e28Sdh155122 		switch (iptype) {
3278bf1d7e28Sdh155122 		case ZS_SHARED:
3279bf1d7e28Sdh155122 			curr_iptype = "shared";
3280bf1d7e28Sdh155122 			break;
3281bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
3282bf1d7e28Sdh155122 			curr_iptype = "exclusive";
3283bf1d7e28Sdh155122 			break;
3284bf1d7e28Sdh155122 		}
3285bf1d7e28Sdh155122 
3286e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
3287e767a340Sgjelinek 			zonecfg_fini_handle(handle);
32889acbbeafSnn35248 			return (0);
3289e767a340Sgjelinek 		}
32909acbbeafSnn35248 		zerror(zlogp, B_FALSE,
32919acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
32929acbbeafSnn35248 		zonecfg_fini_handle(handle);
32939acbbeafSnn35248 		return (-1);
32949acbbeafSnn35248 	}
32959acbbeafSnn35248 
3296ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
3297ffbafc53Scomay 	case Z_OK:
3298ffbafc53Scomay 		error = 0;
3299ffbafc53Scomay 		break;
3300ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3301ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3302ffbafc53Scomay 		    "within the zone's privilege set", privname);
3303ffbafc53Scomay 		break;
3304ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3305ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3306ffbafc53Scomay 		    "from the zone's privilege set", privname);
3307ffbafc53Scomay 		break;
3308ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3309ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3310ffbafc53Scomay 		    "in the zone's privilege set", privname);
3311ffbafc53Scomay 		break;
3312ffbafc53Scomay 	default:
3313ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
3314ffbafc53Scomay 		    "privilege set");
3315ffbafc53Scomay 		break;
3316ffbafc53Scomay 	}
3317ffbafc53Scomay 
3318ffbafc53Scomay 	free(privname);
3319ffbafc53Scomay 	zonecfg_fini_handle(handle);
3320ffbafc53Scomay 	return (error);
3321ffbafc53Scomay }
3322ffbafc53Scomay 
3323ffbafc53Scomay static int
33247c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
33257c478bd9Sstevel@tonic-gate {
33267c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
33277c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
33287c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
33297c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
33307c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
33317c478bd9Sstevel@tonic-gate 	int error = -1;
33327c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
33337c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
33347c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
3335ff19e029SMenno Lageman 	uint64_t maxlwps;
3336ff19e029SMenno Lageman 	uint64_t maxprocs;
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 	*bufp = NULL;
33397c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
33427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
33437c478bd9Sstevel@tonic-gate 		return (-1);
33447c478bd9Sstevel@tonic-gate 	}
33457c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
33467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
33477c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
33487c478bd9Sstevel@tonic-gate 		return (-1);
33497c478bd9Sstevel@tonic-gate 	}
33507c478bd9Sstevel@tonic-gate 
33517c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
33527c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
33537c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
33547c478bd9Sstevel@tonic-gate 		goto out;
33557c478bd9Sstevel@tonic-gate 	}
33567c478bd9Sstevel@tonic-gate 
3357ff19e029SMenno Lageman 	/*
3358ff19e029SMenno Lageman 	 * Allow the administrator to control both the maximum number of
3359ff19e029SMenno Lageman 	 * process table slots and the maximum number of lwps with just the
3360ff19e029SMenno Lageman 	 * max-processes property.  If only the max-processes property is set,
3361ff19e029SMenno Lageman 	 * we add a max-lwps property with a limit derived from max-processes.
3362ff19e029SMenno Lageman 	 */
3363ff19e029SMenno Lageman 	if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs)
3364ff19e029SMenno Lageman 	    == Z_OK &&
3365ff19e029SMenno Lageman 	    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps)
3366ff19e029SMenno Lageman 	    == Z_NO_ENTRY) {
3367ff19e029SMenno Lageman 		if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
3368ff19e029SMenno Lageman 		    maxprocs * LWPS_PER_PROCESS) != Z_OK) {
3369ff19e029SMenno Lageman 			zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
3370ff19e029SMenno Lageman 			goto out;
3371ff19e029SMenno Lageman 		}
3372ff19e029SMenno Lageman 	}
3373ff19e029SMenno Lageman 
33747c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
33757c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
33767c478bd9Sstevel@tonic-gate 		goto out;
33777c478bd9Sstevel@tonic-gate 	}
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
33807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
33817c478bd9Sstevel@tonic-gate 		goto out;
33827c478bd9Sstevel@tonic-gate 	}
33837c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
33847c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
33857c478bd9Sstevel@tonic-gate 		uint_t i, count;
33867c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
33897c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
33907c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
33917c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
33927c478bd9Sstevel@tonic-gate 			continue;
33937c478bd9Sstevel@tonic-gate 		}
33947c478bd9Sstevel@tonic-gate 		count = 0;
33957c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
33967c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
33977c478bd9Sstevel@tonic-gate 			count++;
33987c478bd9Sstevel@tonic-gate 		}
33997c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
34007c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
34017c478bd9Sstevel@tonic-gate 		}
34027c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
34037c478bd9Sstevel@tonic-gate 			goto out;
34047c478bd9Sstevel@tonic-gate 		i = 0;
34057c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
34067c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
34077c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
34087c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
34097c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
34107c478bd9Sstevel@tonic-gate 				goto out;
34117c478bd9Sstevel@tonic-gate 			}
34127c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
34137c478bd9Sstevel@tonic-gate 			    != Z_OK) {
34147c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
34157c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
34167c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
34177c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
34187c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
34197c478bd9Sstevel@tonic-gate 				goto out;
34207c478bd9Sstevel@tonic-gate 			}
34217c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
34227c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
34237c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
34247c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
34257c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
34267c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
34277c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
34287c478bd9Sstevel@tonic-gate 				    name);
34297c478bd9Sstevel@tonic-gate 				goto out;
34307c478bd9Sstevel@tonic-gate 			}
34317c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
34327c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
34337c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34347c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34357c478bd9Sstevel@tonic-gate 				goto out;
34367c478bd9Sstevel@tonic-gate 			}
34377c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
34387c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
34397c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34407c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34417c478bd9Sstevel@tonic-gate 				goto out;
34427c478bd9Sstevel@tonic-gate 			}
34437c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
34447c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
34457c478bd9Sstevel@tonic-gate 			    != 0) {
34467c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34477c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34487c478bd9Sstevel@tonic-gate 				goto out;
34497c478bd9Sstevel@tonic-gate 			}
34507c478bd9Sstevel@tonic-gate 		}
34517c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34527c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
34537c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
34547c478bd9Sstevel@tonic-gate 		    != 0) {
34557c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
34567c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
34577c478bd9Sstevel@tonic-gate 			goto out;
34587c478bd9Sstevel@tonic-gate 		}
34597c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
34607c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
34617c478bd9Sstevel@tonic-gate 		free(nvlv);
34627c478bd9Sstevel@tonic-gate 		nvlv = NULL;
34637c478bd9Sstevel@tonic-gate 		rctlcount++;
34647c478bd9Sstevel@tonic-gate 	}
34657c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
34667c478bd9Sstevel@tonic-gate 
34677c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
34687c478bd9Sstevel@tonic-gate 		error = 0;
34697c478bd9Sstevel@tonic-gate 		goto out;
34707c478bd9Sstevel@tonic-gate 	}
34717c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
34727c478bd9Sstevel@tonic-gate 	    != 0) {
34737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
34747c478bd9Sstevel@tonic-gate 		goto out;
34757c478bd9Sstevel@tonic-gate 	}
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 	error = 0;
34787c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
34797c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
34807c478bd9Sstevel@tonic-gate 
34817c478bd9Sstevel@tonic-gate out:
34827c478bd9Sstevel@tonic-gate 	free(rctlblk);
34837c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34847c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
34857c478bd9Sstevel@tonic-gate 		free(nvl_packed);
34867c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
34877c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
34887c478bd9Sstevel@tonic-gate 		free(nvlv);
34897c478bd9Sstevel@tonic-gate 	if (handle != NULL)
34907c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
34917c478bd9Sstevel@tonic-gate 	return (error);
34927c478bd9Sstevel@tonic-gate }
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate static int
3495c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr)
3496c5cd6260S {
3497c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
3498c5cd6260S 
3499c5cd6260S 	if (query_hook[0] == '\0')
3500c5cd6260S 		return (0);
3501c5cd6260S 
3502c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3503c5cd6260S 	    > sizeof (cmdbuf))
3504c5cd6260S 		return (-1);
3505c5cd6260S 
3506c5cd6260S 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3507c5cd6260S 		return (-1);
3508c5cd6260S 
3509c5cd6260S 	return (0);
3510c5cd6260S }
3511c5cd6260S 
3512c5cd6260S static int
3513fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3514fa9e4066Sahrens {
3515fa9e4066Sahrens 	zone_dochandle_t handle;
3516fa9e4066Sahrens 	struct zone_dstab dstab;
3517fa9e4066Sahrens 	size_t total, offset, len;
3518fa9e4066Sahrens 	int error = -1;
3519e5a17f6aSgjelinek 	char *str = NULL;
3520c5cd6260S 	char *implicit_datasets = NULL;
3521c5cd6260S 	int implicit_len = 0;
3522fa9e4066Sahrens 
3523fa9e4066Sahrens 	*bufp = NULL;
3524fa9e4066Sahrens 	*bufsizep = 0;
3525fa9e4066Sahrens 
3526fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3527fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3528fa9e4066Sahrens 		return (-1);
3529fa9e4066Sahrens 	}
3530fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3531fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3532fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3533fa9e4066Sahrens 		return (-1);
3534fa9e4066Sahrens 	}
3535fa9e4066Sahrens 
3536c5cd6260S 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3537c5cd6260S 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3538c5cd6260S 		goto out;
3539c5cd6260S 	}
3540c5cd6260S 
3541fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3542fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3543fa9e4066Sahrens 		goto out;
3544fa9e4066Sahrens 	}
3545fa9e4066Sahrens 
3546fa9e4066Sahrens 	total = 0;
3547fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3548fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3549fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3550fa9e4066Sahrens 
3551c5cd6260S 	if (implicit_datasets != NULL)
3552c5cd6260S 		implicit_len = strlen(implicit_datasets);
3553c5cd6260S 	if (implicit_len > 0)
3554c5cd6260S 		total += implicit_len + 1;
3555c5cd6260S 
3556fa9e4066Sahrens 	if (total == 0) {
3557fa9e4066Sahrens 		error = 0;
3558fa9e4066Sahrens 		goto out;
3559fa9e4066Sahrens 	}
3560fa9e4066Sahrens 
3561fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3562fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3563fa9e4066Sahrens 		goto out;
3564fa9e4066Sahrens 	}
3565fa9e4066Sahrens 
3566fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3567fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3568fa9e4066Sahrens 		goto out;
3569fa9e4066Sahrens 	}
3570fa9e4066Sahrens 	offset = 0;
3571fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3572fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3573fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3574e5a17f6aSgjelinek 		    total - offset);
3575fa9e4066Sahrens 		offset += len;
3576e5a17f6aSgjelinek 		if (offset < total - 1)
3577fa9e4066Sahrens 			str[offset++] = ',';
3578fa9e4066Sahrens 	}
3579fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3580fa9e4066Sahrens 
3581c5cd6260S 	if (implicit_len > 0)
3582c5cd6260S 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3583c5cd6260S 
3584fa9e4066Sahrens 	error = 0;
3585fa9e4066Sahrens 	*bufp = str;
3586fa9e4066Sahrens 	*bufsizep = total;
3587fa9e4066Sahrens 
3588fa9e4066Sahrens out:
3589fa9e4066Sahrens 	if (error != 0 && str != NULL)
3590fa9e4066Sahrens 		free(str);
3591fa9e4066Sahrens 	if (handle != NULL)
3592fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3593c5cd6260S 	if (implicit_datasets != NULL)
3594c5cd6260S 		free(implicit_datasets);
3595fa9e4066Sahrens 
3596fa9e4066Sahrens 	return (error);
3597fa9e4066Sahrens }
3598fa9e4066Sahrens 
3599fa9e4066Sahrens static int
3600fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3601fa9e4066Sahrens {
3602fa9e4066Sahrens 	zone_dochandle_t handle;
3603fa9e4066Sahrens 	struct zone_dstab dstab;
3604fa9e4066Sahrens 	zfs_handle_t *zhp;
360599653d4eSeschrock 	libzfs_handle_t *hdl;
3606fa9e4066Sahrens 
3607fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3608fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3609fa9e4066Sahrens 		return (-1);
3610fa9e4066Sahrens 	}
3611fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3612fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3613fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3614fa9e4066Sahrens 		return (-1);
3615fa9e4066Sahrens 	}
3616fa9e4066Sahrens 
3617fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3618fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3619fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3620fa9e4066Sahrens 		return (-1);
3621fa9e4066Sahrens 	}
3622fa9e4066Sahrens 
362399653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
362499653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
362599653d4eSeschrock 		zonecfg_fini_handle(handle);
362699653d4eSeschrock 		return (-1);
362799653d4eSeschrock 	}
3628fa9e4066Sahrens 
3629fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3630fa9e4066Sahrens 
363199653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3632fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3633fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3634fa9e4066Sahrens 			    dstab.zone_dataset_name);
3635fa9e4066Sahrens 			zonecfg_fini_handle(handle);
363699653d4eSeschrock 			libzfs_fini(hdl);
3637fa9e4066Sahrens 			return (-1);
3638fa9e4066Sahrens 		}
3639fa9e4066Sahrens 
3640fa9e4066Sahrens 		/*
3641fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3642fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3643fa9e4066Sahrens 		 */
3644fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3645e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3646e9dbad6fSeschrock 		    "on") != 0) {
3647fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3648fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3649fa9e4066Sahrens 			    dstab.zone_dataset_name);
3650fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3651fa9e4066Sahrens 			zfs_close(zhp);
365299653d4eSeschrock 			libzfs_fini(hdl);
3653fa9e4066Sahrens 			return (-1);
3654fa9e4066Sahrens 		}
3655fa9e4066Sahrens 
3656fa9e4066Sahrens 		zfs_close(zhp);
3657fa9e4066Sahrens 	}
3658fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3659fa9e4066Sahrens 
3660fa9e4066Sahrens 	zonecfg_fini_handle(handle);
366199653d4eSeschrock 	libzfs_fini(hdl);
3662fa9e4066Sahrens 
3663fa9e4066Sahrens 	return (0);
3664fa9e4066Sahrens }
3665fa9e4066Sahrens 
366645916cd2Sjpk /*
36674201a95eSRic Aleshire  * Return true if the path is its own zfs file system.  We determine this
36684201a95eSRic Aleshire  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
36694201a95eSRic Aleshire  * if it is a different fs.
36704201a95eSRic Aleshire  */
36714201a95eSRic Aleshire boolean_t
36724201a95eSRic Aleshire is_zonepath_zfs(char *zonepath)
36734201a95eSRic Aleshire {
36744201a95eSRic Aleshire 	int res;
36754201a95eSRic Aleshire 	char *path;
36764201a95eSRic Aleshire 	char *parent;
36774201a95eSRic Aleshire 	struct statvfs64 buf1, buf2;
36784201a95eSRic Aleshire 
36794201a95eSRic Aleshire 	if (statvfs64(zonepath, &buf1) != 0)
36804201a95eSRic Aleshire 		return (B_FALSE);
36814201a95eSRic Aleshire 
36824201a95eSRic Aleshire 	if (strcmp(buf1.f_basetype, "zfs") != 0)
36834201a95eSRic Aleshire 		return (B_FALSE);
36844201a95eSRic Aleshire 
36854201a95eSRic Aleshire 	if ((path = strdup(zonepath)) == NULL)
36864201a95eSRic Aleshire 		return (B_FALSE);
36874201a95eSRic Aleshire 
36884201a95eSRic Aleshire 	parent = dirname(path);
36894201a95eSRic Aleshire 	res = statvfs64(parent, &buf2);
36904201a95eSRic Aleshire 	free(path);
36914201a95eSRic Aleshire 
36924201a95eSRic Aleshire 	if (res != 0)
36934201a95eSRic Aleshire 		return (B_FALSE);
36944201a95eSRic Aleshire 
36954201a95eSRic Aleshire 	if (buf1.f_fsid == buf2.f_fsid)
36964201a95eSRic Aleshire 		return (B_FALSE);
36974201a95eSRic Aleshire 
36984201a95eSRic Aleshire 	return (B_TRUE);
36994201a95eSRic Aleshire }
37004201a95eSRic Aleshire 
37014201a95eSRic Aleshire /*
37024201a95eSRic Aleshire  * Verify the MAC label in the root dataset for the zone.
37034201a95eSRic Aleshire  * If the label exists, it must match the label configured for the zone.
37044201a95eSRic Aleshire  * Otherwise if there's no label on the dataset, create one here.
37054201a95eSRic Aleshire  */
37064201a95eSRic Aleshire 
37074201a95eSRic Aleshire static int
37084201a95eSRic Aleshire validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
37094201a95eSRic Aleshire {
37104201a95eSRic Aleshire 	int		error = -1;
37114201a95eSRic Aleshire 	zfs_handle_t	*zhp;
37124201a95eSRic Aleshire 	libzfs_handle_t	*hdl;
37134201a95eSRic Aleshire 	m_label_t	ds_sl;
37144201a95eSRic Aleshire 	char		zonepath[MAXPATHLEN];
37154201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
37164201a95eSRic Aleshire 
37174201a95eSRic Aleshire 	if (!is_system_labeled())
37184201a95eSRic Aleshire 		return (0);
37194201a95eSRic Aleshire 
37204201a95eSRic Aleshire 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
37214201a95eSRic Aleshire 		zerror(zlogp, B_TRUE, "unable to determine zone path");
37224201a95eSRic Aleshire 		return (-1);
37234201a95eSRic Aleshire 	}
37244201a95eSRic Aleshire 
37254201a95eSRic Aleshire 	if (!is_zonepath_zfs(zonepath))
37264201a95eSRic Aleshire 		return (0);
37274201a95eSRic Aleshire 
37284201a95eSRic Aleshire 	if ((hdl = libzfs_init()) == NULL) {
37294201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "opening ZFS library");
37304201a95eSRic Aleshire 		return (-1);
37314201a95eSRic Aleshire 	}
37324201a95eSRic Aleshire 
37334201a95eSRic Aleshire 	if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
37344201a95eSRic Aleshire 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
37354201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
37364201a95eSRic Aleshire 		    rootpath);
37374201a95eSRic Aleshire 		libzfs_fini(hdl);
37384201a95eSRic Aleshire 		return (-1);
37394201a95eSRic Aleshire 	}
37404201a95eSRic Aleshire 
37414201a95eSRic Aleshire 	/* Get the mlslabel property if it exists. */
37424201a95eSRic Aleshire 	if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
37434201a95eSRic Aleshire 	    NULL, NULL, 0, B_TRUE) != 0) ||
37444201a95eSRic Aleshire 	    (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
37454201a95eSRic Aleshire 		char		*str2 = NULL;
37464201a95eSRic Aleshire 
37474201a95eSRic Aleshire 		/*
37484201a95eSRic Aleshire 		 * No label on the dataset (or default only); create one.
37494201a95eSRic Aleshire 		 * (Only do this automatic labeling for the labeled brand.)
37504201a95eSRic Aleshire 		 */
37514201a95eSRic Aleshire 		if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
37524201a95eSRic Aleshire 			error = 0;
37534201a95eSRic Aleshire 			goto out;
37544201a95eSRic Aleshire 		}
37554201a95eSRic Aleshire 
37564201a95eSRic Aleshire 		error = l_to_str_internal(zone_sl, &str2);
37574201a95eSRic Aleshire 		if (error)
37584201a95eSRic Aleshire 			goto out;
37594201a95eSRic Aleshire 		if (str2 == NULL) {
37604201a95eSRic Aleshire 			error = -1;
37614201a95eSRic Aleshire 			goto out;
37624201a95eSRic Aleshire 		}
37634201a95eSRic Aleshire 		if ((error = zfs_prop_set(zhp,
37644201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
37654201a95eSRic Aleshire 			zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
37664201a95eSRic Aleshire 			    "property for root dataset at '%s'\n", rootpath);
37674201a95eSRic Aleshire 		}
37684201a95eSRic Aleshire 		free(str2);
37694201a95eSRic Aleshire 		goto out;
37704201a95eSRic Aleshire 	}
37714201a95eSRic Aleshire 
37724201a95eSRic Aleshire 	/* Convert the retrieved dataset label to binary form. */
37734201a95eSRic Aleshire 	error = hexstr_to_label(ds_hexsl, &ds_sl);
37744201a95eSRic Aleshire 	if (error) {
37754201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
37764201a95eSRic Aleshire 		    "property on root dataset at '%s'\n", rootpath);
37774201a95eSRic Aleshire 		goto out;			/* exit with error */
37784201a95eSRic Aleshire 	}
37794201a95eSRic Aleshire 
37804201a95eSRic Aleshire 	/*
37814201a95eSRic Aleshire 	 * Perform a MAC check by comparing the zone label with the
37824201a95eSRic Aleshire 	 * dataset label.
37834201a95eSRic Aleshire 	 */
37844201a95eSRic Aleshire 	error = (!blequal(zone_sl, &ds_sl));
37854201a95eSRic Aleshire 	if (error)
37864201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
37874201a95eSRic Aleshire out:
37884201a95eSRic Aleshire 	zfs_close(zhp);
37894201a95eSRic Aleshire 	libzfs_fini(hdl);
37904201a95eSRic Aleshire 
37914201a95eSRic Aleshire 	return (error);
37924201a95eSRic Aleshire }
37934201a95eSRic Aleshire 
37944201a95eSRic Aleshire /*
379545916cd2Sjpk  * Mount lower level home directories into/from current zone
379645916cd2Sjpk  * Share exported directories specified in dfstab for zone
379745916cd2Sjpk  */
379845916cd2Sjpk static int
379945916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
380045916cd2Sjpk {
380145916cd2Sjpk 	zoneid_t *zids = NULL;
380245916cd2Sjpk 	priv_set_t *zid_privs;
380345916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
380445916cd2Sjpk 	uint_t nzents_saved;
380545916cd2Sjpk 	uint_t nzents;
380645916cd2Sjpk 	int i;
380745916cd2Sjpk 	char readonly[] = "ro";
380845916cd2Sjpk 	struct zone_fstab lower_fstab;
380945916cd2Sjpk 	char *argv[4];
381045916cd2Sjpk 
381145916cd2Sjpk 	if (!is_system_labeled())
381245916cd2Sjpk 		return (0);
381345916cd2Sjpk 
381445916cd2Sjpk 	if (zid_label == NULL) {
381545916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
381645916cd2Sjpk 		if (zid_label == NULL)
381745916cd2Sjpk 			return (-1);
381845916cd2Sjpk 	}
381945916cd2Sjpk 
382045916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
382145916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
382227e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
382345916cd2Sjpk 
382445916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
382545916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
382645916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
382745916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
382845916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
382945916cd2Sjpk 
383045916cd2Sjpk 	/*
383145916cd2Sjpk 	 * Get the list of zones from the kernel
383245916cd2Sjpk 	 */
383345916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
383445916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
383545916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
383645916cd2Sjpk 		return (-1);
383745916cd2Sjpk 	}
383845916cd2Sjpk again:
383945916cd2Sjpk 	if (nzents == 0) {
384045916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
384145916cd2Sjpk 		return (-1);
384245916cd2Sjpk 	}
384345916cd2Sjpk 
384445916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
384545916cd2Sjpk 	if (zids == NULL) {
38463f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
384745916cd2Sjpk 		return (-1);
384845916cd2Sjpk 	}
384945916cd2Sjpk 	nzents_saved = nzents;
385045916cd2Sjpk 
385145916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
385245916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
385345916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
385445916cd2Sjpk 		free(zids);
385545916cd2Sjpk 		return (-1);
385645916cd2Sjpk 	}
385745916cd2Sjpk 	if (nzents != nzents_saved) {
385845916cd2Sjpk 		/* list changed, try again */
385945916cd2Sjpk 		free(zids);
386045916cd2Sjpk 		goto again;
386145916cd2Sjpk 	}
386245916cd2Sjpk 
386345916cd2Sjpk 	ip = getprivimplinfo();
386445916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
386545916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
386645916cd2Sjpk 		zonecfg_free_fs_option_list(
386745916cd2Sjpk 		    lower_fstab.zone_fs_options);
386845916cd2Sjpk 		free(zids);
386945916cd2Sjpk 		return (-1);
387045916cd2Sjpk 	}
387145916cd2Sjpk 
387245916cd2Sjpk 	for (i = 0; i < nzents; i++) {
387345916cd2Sjpk 		char zid_name[ZONENAME_MAX];
387445916cd2Sjpk 		zone_state_t zid_state;
387545916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
387645916cd2Sjpk 		struct stat stat_buf;
387745916cd2Sjpk 
387845916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
387945916cd2Sjpk 			continue;
388045916cd2Sjpk 
388145916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
388245916cd2Sjpk 			continue;
388345916cd2Sjpk 
388445916cd2Sjpk 		/*
388545916cd2Sjpk 		 * Do special setup for the zone we are booting
388645916cd2Sjpk 		 */
388745916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
388845916cd2Sjpk 			struct zone_fstab autofs_fstab;
388945916cd2Sjpk 			char map_path[MAXPATHLEN];
389045916cd2Sjpk 			int fd;
389145916cd2Sjpk 
389245916cd2Sjpk 			/*
389345916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
38949acbbeafSnn35248 			 * in the global zone. The non-global zone entry
389545916cd2Sjpk 			 * will be created by automount when the zone
389645916cd2Sjpk 			 * is booted.
389745916cd2Sjpk 			 */
389845916cd2Sjpk 
389945916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
390045916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
390145916cd2Sjpk 
390245916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
390345916cd2Sjpk 			    "/zone/%s/home", zid_name);
390445916cd2Sjpk 
390545916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
390645916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
390745916cd2Sjpk 			/*
390845916cd2Sjpk 			 * If the map file doesn't exist create a template
390945916cd2Sjpk 			 */
391045916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
391145916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
391245916cd2Sjpk 				int len;
391345916cd2Sjpk 				char map_rec[MAXPATHLEN];
391445916cd2Sjpk 
391545916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
391645916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
391745916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
391845916cd2Sjpk 				(void) write(fd, map_rec, len);
391945916cd2Sjpk 				(void) close(fd);
392045916cd2Sjpk 			}
392145916cd2Sjpk 
392245916cd2Sjpk 			/*
392345916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
392445916cd2Sjpk 			 * If it's already of type autofs, then
392545916cd2Sjpk 			 * don't mount it again.
392645916cd2Sjpk 			 */
392745916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
392845916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
392945916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
393045916cd2Sjpk 
393145916cd2Sjpk 				(void) make_one_dir(zlogp, "",
393227e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
393327e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
393445916cd2Sjpk 
393545916cd2Sjpk 				/*
393645916cd2Sjpk 				 * Mount will fail if automounter has already
393745916cd2Sjpk 				 * processed the auto_home_<zonename> map
393845916cd2Sjpk 				 */
393945916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
394045916cd2Sjpk 				    autofs_fstab.zone_fs_special,
394145916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
394245916cd2Sjpk 			}
394345916cd2Sjpk 			continue;
394445916cd2Sjpk 		}
394545916cd2Sjpk 
394645916cd2Sjpk 
394745916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
394848451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
394948451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
395045916cd2Sjpk 			/* Skip over zones without mounted filesystems */
395145916cd2Sjpk 			continue;
395245916cd2Sjpk 
395345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
395445916cd2Sjpk 		    sizeof (m_label_t)) < 0)
395545916cd2Sjpk 			/* Skip over zones with unspecified label */
395645916cd2Sjpk 			continue;
395745916cd2Sjpk 
395845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
395945916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
396045916cd2Sjpk 			/* Skip over zones with bad path */
396145916cd2Sjpk 			continue;
396245916cd2Sjpk 
396345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
396445916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
396545916cd2Sjpk 			/* Skip over zones with bad privs */
396645916cd2Sjpk 			continue;
396745916cd2Sjpk 
396845916cd2Sjpk 		/*
396945916cd2Sjpk 		 * Reading down is valid according to our label model
397045916cd2Sjpk 		 * but some customers want to disable it because it
397145916cd2Sjpk 		 * allows execute down and other possible attacks.
397245916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
397345916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
397445916cd2Sjpk 		 * for NFS read-down semantics.
397545916cd2Sjpk 		 */
397645916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
397745916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
397845916cd2Sjpk 			/*
397945916cd2Sjpk 			 * Our zone dominates this one.
398045916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
398145916cd2Sjpk 			 */
398245916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
398345916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
398445916cd2Sjpk 
398545916cd2Sjpk 			/*
398645916cd2Sjpk 			 * If the target is already an LOFS mount
398745916cd2Sjpk 			 * then don't do it again.
398845916cd2Sjpk 			 */
398945916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
399045916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
399145916cd2Sjpk 
399245916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
399345916cd2Sjpk 				    MAXPATHLEN, "%s/export",
399445916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
399545916cd2Sjpk 					continue;
399645916cd2Sjpk 
399745916cd2Sjpk 				/*
399845916cd2Sjpk 				 * Make sure the lower-level home exists
399945916cd2Sjpk 				 */
400045916cd2Sjpk 				if (make_one_dir(zlogp,
400127e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
400227e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
400327e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
400445916cd2Sjpk 					continue;
400545916cd2Sjpk 
400645916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
400745916cd2Sjpk 				    "/home", MAXPATHLEN);
400845916cd2Sjpk 
400945916cd2Sjpk 				/*
401045916cd2Sjpk 				 * Mount can fail because the lower-level
401145916cd2Sjpk 				 * zone may have already done a mount up.
401245916cd2Sjpk 				 */
40130679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
40140679f794S 				    Z_MNT_BOOT);
401545916cd2Sjpk 			}
401645916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
401745916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
401845916cd2Sjpk 			/*
401945916cd2Sjpk 			 * This zone dominates our zone.
402045916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
402145916cd2Sjpk 			 */
402245916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
402345916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
402445916cd2Sjpk 			    zone_name) > MAXPATHLEN)
402545916cd2Sjpk 				continue;
402645916cd2Sjpk 
402745916cd2Sjpk 			/*
402845916cd2Sjpk 			 * If the target is already an LOFS mount
402945916cd2Sjpk 			 * then don't do it again.
403045916cd2Sjpk 			 */
403145916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
403245916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
403345916cd2Sjpk 
403445916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
403545916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
403645916cd2Sjpk 
403745916cd2Sjpk 				/*
403845916cd2Sjpk 				 * Mount can fail because the higher-level
403945916cd2Sjpk 				 * zone may have already done a mount down.
404045916cd2Sjpk 				 */
40410679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
40420679f794S 				    Z_MNT_BOOT);
404345916cd2Sjpk 			}
404445916cd2Sjpk 		}
404545916cd2Sjpk 	}
404645916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
404745916cd2Sjpk 	priv_freeset(zid_privs);
404845916cd2Sjpk 	free(zids);
404945916cd2Sjpk 
405045916cd2Sjpk 	/*
405145916cd2Sjpk 	 * Now share any exported directories from this zone.
405245916cd2Sjpk 	 * Each zone can have its own dfstab.
405345916cd2Sjpk 	 */
405445916cd2Sjpk 
405545916cd2Sjpk 	argv[0] = "zoneshare";
405645916cd2Sjpk 	argv[1] = "-z";
405745916cd2Sjpk 	argv[2] = zone_name;
405845916cd2Sjpk 	argv[3] = NULL;
405945916cd2Sjpk 
406045916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
406145916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
406245916cd2Sjpk 
406345916cd2Sjpk 	return (0);
406445916cd2Sjpk }
406545916cd2Sjpk 
406645916cd2Sjpk /*
406745916cd2Sjpk  * Unmount lofs mounts from higher level zones
406845916cd2Sjpk  * Unshare nfs exported directories
406945916cd2Sjpk  */
407045916cd2Sjpk static void
407145916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
407245916cd2Sjpk {
407345916cd2Sjpk 	zoneid_t *zids = NULL;
407445916cd2Sjpk 	uint_t nzents_saved;
407545916cd2Sjpk 	uint_t nzents;
407645916cd2Sjpk 	int i;
407745916cd2Sjpk 	char *argv[4];
407845916cd2Sjpk 	char path[MAXPATHLEN];
407945916cd2Sjpk 
408045916cd2Sjpk 	if (!is_system_labeled())
408145916cd2Sjpk 		return;
408245916cd2Sjpk 
408345916cd2Sjpk 	/*
408445916cd2Sjpk 	 * Get the list of zones from the kernel
408545916cd2Sjpk 	 */
408645916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
408745916cd2Sjpk 		return;
408845916cd2Sjpk 	}
408945916cd2Sjpk 
409045916cd2Sjpk 	if (zid_label == NULL) {
409145916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
409245916cd2Sjpk 		if (zid_label == NULL)
409345916cd2Sjpk 			return;
409445916cd2Sjpk 	}
409545916cd2Sjpk 
409645916cd2Sjpk again:
409745916cd2Sjpk 	if (nzents == 0)
409845916cd2Sjpk 		return;
409945916cd2Sjpk 
410045916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
410145916cd2Sjpk 	if (zids == NULL) {
41023f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
410345916cd2Sjpk 		return;
410445916cd2Sjpk 	}
410545916cd2Sjpk 	nzents_saved = nzents;
410645916cd2Sjpk 
410745916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
410845916cd2Sjpk 		free(zids);
410945916cd2Sjpk 		return;
411045916cd2Sjpk 	}
411145916cd2Sjpk 	if (nzents != nzents_saved) {
411245916cd2Sjpk 		/* list changed, try again */
411345916cd2Sjpk 		free(zids);
411445916cd2Sjpk 		goto again;
411545916cd2Sjpk 	}
411645916cd2Sjpk 
411745916cd2Sjpk 	for (i = 0; i < nzents; i++) {
411845916cd2Sjpk 		char zid_name[ZONENAME_MAX];
411945916cd2Sjpk 		zone_state_t zid_state;
412045916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
412145916cd2Sjpk 
412245916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
412345916cd2Sjpk 			continue;
412445916cd2Sjpk 
412545916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
412645916cd2Sjpk 			continue;
412745916cd2Sjpk 
412845916cd2Sjpk 		/*
412945916cd2Sjpk 		 * Skip the zone we are halting
413045916cd2Sjpk 		 */
413145916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
413245916cd2Sjpk 			continue;
413345916cd2Sjpk 
413445916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
413545916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
413645916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
413745916cd2Sjpk 			/* Skip over zones without mounted filesystems */
413845916cd2Sjpk 			continue;
413945916cd2Sjpk 
414045916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
414145916cd2Sjpk 		    sizeof (m_label_t)) < 0)
414245916cd2Sjpk 			/* Skip over zones with unspecified label */
414345916cd2Sjpk 			continue;
414445916cd2Sjpk 
414545916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
414645916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
414745916cd2Sjpk 			/* Skip over zones with bad path */
414845916cd2Sjpk 			continue;
414945916cd2Sjpk 
415045916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
415145916cd2Sjpk 			/*
415245916cd2Sjpk 			 * This zone dominates our zone.
415345916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
415445916cd2Sjpk 			 */
415545916cd2Sjpk 
415645916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
415745916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
415845916cd2Sjpk 			    zone_name) > MAXPATHLEN)
415945916cd2Sjpk 				continue;
416045916cd2Sjpk 
416145916cd2Sjpk 			/* Skip over mount failures */
416245916cd2Sjpk 			(void) umount(path);
416345916cd2Sjpk 		}
416445916cd2Sjpk 	}
416545916cd2Sjpk 	free(zids);
416645916cd2Sjpk 
416745916cd2Sjpk 	/*
416845916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
416945916cd2Sjpk 	 */
417045916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
417145916cd2Sjpk 	/* Skip over mount failures */
417245916cd2Sjpk 	(void) umount(path);
417345916cd2Sjpk 
417445916cd2Sjpk 	/*
417545916cd2Sjpk 	 * Next unshare any exported directories from this zone.
417645916cd2Sjpk 	 */
417745916cd2Sjpk 
417845916cd2Sjpk 	argv[0] = "zoneunshare";
417945916cd2Sjpk 	argv[1] = "-z";
418045916cd2Sjpk 	argv[2] = zone_name;
418145916cd2Sjpk 	argv[3] = NULL;
418245916cd2Sjpk 
418345916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
418445916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
418545916cd2Sjpk 
418645916cd2Sjpk 	/*
418745916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
418845916cd2Sjpk 	 */
418945916cd2Sjpk 
419045916cd2Sjpk 	argv[0] = "deallocate";
419145916cd2Sjpk 	argv[1] = "-Isz";
419245916cd2Sjpk 	argv[2] = zone_name;
419345916cd2Sjpk 	argv[3] = NULL;
419445916cd2Sjpk 
419545916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
419645916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
419745916cd2Sjpk }
419845916cd2Sjpk 
419945916cd2Sjpk /*
420045916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
420145916cd2Sjpk  * this zone.
420245916cd2Sjpk  */
420345916cd2Sjpk static tsol_zcent_t *
420445916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
420545916cd2Sjpk {
420645916cd2Sjpk 	FILE *fp;
420745916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
420845916cd2Sjpk 	char line[MAXTNZLEN];
420945916cd2Sjpk 
421045916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
421145916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
421245916cd2Sjpk 		return (NULL);
421345916cd2Sjpk 	}
421445916cd2Sjpk 
421545916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
421645916cd2Sjpk 		/*
421745916cd2Sjpk 		 * Check for malformed database
421845916cd2Sjpk 		 */
421945916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
422045916cd2Sjpk 			break;
422145916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
422245916cd2Sjpk 			continue;
422345916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
422445916cd2Sjpk 			break;
422545916cd2Sjpk 		tsol_freezcent(zcent);
422645916cd2Sjpk 		zcent = NULL;
422745916cd2Sjpk 	}
422845916cd2Sjpk 	(void) fclose(fp);
422945916cd2Sjpk 
423045916cd2Sjpk 	if (zcent == NULL) {
423145916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
423245916cd2Sjpk 		    "See tnzonecfg(4)");
423345916cd2Sjpk 	} else {
423445916cd2Sjpk 		if (zlabel == NULL)
423545916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
423645916cd2Sjpk 		/*
423745916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
423845916cd2Sjpk 		 */
423945916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
424045916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
424145916cd2Sjpk 			return (NULL);
424245916cd2Sjpk 		} else {
424345916cd2Sjpk 			priv_copyset(privs, zprivs);
424445916cd2Sjpk 		}
424545916cd2Sjpk 	}
424645916cd2Sjpk 	return (zcent);
424745916cd2Sjpk }
424845916cd2Sjpk 
424945916cd2Sjpk /*
425045916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
425145916cd2Sjpk  */
425245916cd2Sjpk static void
425345916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
425445916cd2Sjpk {
425545916cd2Sjpk 	tsol_mlp_t *mlp;
425645916cd2Sjpk 	tsol_mlpent_t tsme;
425745916cd2Sjpk 
425845916cd2Sjpk 	if (!is_system_labeled())
425945916cd2Sjpk 		return;
426045916cd2Sjpk 
426145916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
426245916cd2Sjpk 	tsme.tsme_flags = 0;
426345916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
426445916cd2Sjpk 		tsme.tsme_mlp = *mlp;
426545916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
426645916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
426745916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
426845916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
426945916cd2Sjpk 		}
427045916cd2Sjpk 	}
427145916cd2Sjpk 
427245916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
427345916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
427445916cd2Sjpk 		tsme.tsme_mlp = *mlp;
427545916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
427645916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
427745916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
427845916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
427945916cd2Sjpk 		}
428045916cd2Sjpk 	}
428145916cd2Sjpk }
428245916cd2Sjpk 
428345916cd2Sjpk static void
428445916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
428545916cd2Sjpk {
428645916cd2Sjpk 	tsol_mlpent_t tsme;
428745916cd2Sjpk 
428845916cd2Sjpk 	if (!is_system_labeled())
428945916cd2Sjpk 		return;
429045916cd2Sjpk 
429145916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
429245916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
429345916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
429445916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
429545916cd2Sjpk }
429645916cd2Sjpk 
42977c478bd9Sstevel@tonic-gate int
42989a686fbcSPaul Dagnelie prtmount(const struct mnttab *fs, void *x)
42999a686fbcSPaul Dagnelie {
43000094b373Sjv227347 	zerror((zlog_t *)x, B_FALSE, "  %s", fs->mnt_mountp);
43017c478bd9Sstevel@tonic-gate 	return (0);
43027c478bd9Sstevel@tonic-gate }
43037c478bd9Sstevel@tonic-gate 
4304108322fbScarlsonj /*
4305108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
4306108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
4307108322fbScarlsonj  * is found or if we can't tell.
4308108322fbScarlsonj  */
4309108322fbScarlsonj static boolean_t
4310108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
43117c478bd9Sstevel@tonic-gate {
4312108322fbScarlsonj 	zoneid_t *zids = NULL;
4313108322fbScarlsonj 	uint_t nzids = 0;
4314108322fbScarlsonj 	boolean_t retv;
4315108322fbScarlsonj 	int rlen, zlen;
4316108322fbScarlsonj 	char zroot[MAXPATHLEN];
4317108322fbScarlsonj 	char zonename[ZONENAME_MAX];
4318108322fbScarlsonj 
4319108322fbScarlsonj 	for (;;) {
4320108322fbScarlsonj 		nzids += 10;
4321108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
4322108322fbScarlsonj 		if (zids == NULL) {
43233f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
4324108322fbScarlsonj 			return (B_TRUE);
4325108322fbScarlsonj 		}
4326108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
4327108322fbScarlsonj 			break;
4328108322fbScarlsonj 		free(zids);
4329108322fbScarlsonj 	}
4330108322fbScarlsonj 	retv = B_FALSE;
4331108322fbScarlsonj 	rlen = strlen(rootpath);
4332108322fbScarlsonj 	while (nzids > 0) {
4333108322fbScarlsonj 		/*
4334108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
4335108322fbScarlsonj 		 * while we were busy.
4336108322fbScarlsonj 		 */
4337108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4338108322fbScarlsonj 		    sizeof (zroot)) == -1)
4339108322fbScarlsonj 			continue;
4340108322fbScarlsonj 		zlen = strlen(zroot);
4341108322fbScarlsonj 		if (zlen > rlen)
4342108322fbScarlsonj 			zlen = rlen;
4343108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
4344108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4345108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4346108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
4347108322fbScarlsonj 			    sizeof (zonename)) == -1)
4348108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
4349108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
4350108322fbScarlsonj 			zerror(zlogp, B_FALSE,
4351108322fbScarlsonj 			    "zone root %s already in use by zone %s",
4352108322fbScarlsonj 			    rootpath, zonename);
4353108322fbScarlsonj 			retv = B_TRUE;
4354108322fbScarlsonj 			break;
4355108322fbScarlsonj 		}
4356108322fbScarlsonj 	}
4357108322fbScarlsonj 	free(zids);
4358108322fbScarlsonj 	return (retv);
4359108322fbScarlsonj }
4360108322fbScarlsonj 
4361108322fbScarlsonj /*
4362108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
4363108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
4364108322fbScarlsonj  */
4365108322fbScarlsonj static boolean_t
4366108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4367108322fbScarlsonj {
4368108322fbScarlsonj 	struct stat64 rst, zst;
4369108322fbScarlsonj 	struct mnttab *mnp;
4370108322fbScarlsonj 
4371108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
4372108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4373108322fbScarlsonj 		return (B_TRUE);
4374108322fbScarlsonj 	}
4375108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4376108322fbScarlsonj 		return (B_TRUE);
4377108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4378108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
4379108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4380108322fbScarlsonj 			continue;
4381108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
4382108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
4383108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
4384108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4385108322fbScarlsonj 			zerror(zlogp, B_FALSE,
4386108322fbScarlsonj 			    "zone root %s is reachable through %s",
4387108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
4388108322fbScarlsonj 			return (B_TRUE);
4389108322fbScarlsonj 		}
4390108322fbScarlsonj 	}
4391108322fbScarlsonj 	return (B_FALSE);
4392108322fbScarlsonj }
4393108322fbScarlsonj 
43940209230bSgjelinek /*
43950209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
43960209230bSgjelinek  * configuration.
43970209230bSgjelinek  */
43980209230bSgjelinek static int
43990209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
44000209230bSgjelinek {
44010209230bSgjelinek 	int res;
44020209230bSgjelinek 	uint64_t tmp;
44030209230bSgjelinek 	struct zone_mcaptab mcap;
44040209230bSgjelinek 	char sched[MAXNAMELEN];
44050209230bSgjelinek 	zone_dochandle_t handle = NULL;
44060209230bSgjelinek 	char pool_err[128];
44070209230bSgjelinek 
44080209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
44090209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
44100209230bSgjelinek 		return (Z_BAD_HANDLE);
44110209230bSgjelinek 	}
44120209230bSgjelinek 
44130209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
44140209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
44150209230bSgjelinek 		zonecfg_fini_handle(handle);
44160209230bSgjelinek 		return (res);
44170209230bSgjelinek 	}
44180209230bSgjelinek 
44190209230bSgjelinek 	/*
44200209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
44210209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
44220209230bSgjelinek 	 */
44230209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
44240209230bSgjelinek 		uint64_t num;
44250209230bSgjelinek 		char smf_err[128];
44260209230bSgjelinek 
44270209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
44280209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
44290209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
44300209230bSgjelinek 			zonecfg_fini_handle(handle);
44310209230bSgjelinek 			return (Z_INVAL);
44320209230bSgjelinek 		}
44330209230bSgjelinek 
44340209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
44350209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
44360209230bSgjelinek 			    "failed: %s", smf_err);
44370209230bSgjelinek 			zonecfg_fini_handle(handle);
44380209230bSgjelinek 			return (Z_INVAL);
44390209230bSgjelinek 		}
44400209230bSgjelinek 	}
44410209230bSgjelinek 
44420209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
44430209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
44440209230bSgjelinek 	    strlen(sched) > 0) {
44450209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
44460209230bSgjelinek 		    strlen(sched)) == -1)
44470209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
44480209230bSgjelinek 			    "default scheduling class");
44490209230bSgjelinek 
44500209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
44510209230bSgjelinek 	    == Z_OK) {
44520209230bSgjelinek 		/*
44530209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
44540209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
44550209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
44560209230bSgjelinek 		 * in by default so we can print a warning and modify the class
44570209230bSgjelinek 		 * if we wouldn't be using FSS.
44580209230bSgjelinek 		 */
44590209230bSgjelinek 		char class_name[PC_CLNMSZ];
44600209230bSgjelinek 
44610209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
44620209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
44630209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
44640209230bSgjelinek 			    "the zone's scheduling class");
44650209230bSgjelinek 
44660209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
44670209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
44680209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
44690209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
44700209230bSgjelinek 			    "used for processes\nin the zone but to get the "
44710209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
44720209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
44730209230bSgjelinek 			    "details.");
44740209230bSgjelinek 
44750209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
44760209230bSgjelinek 			    strlen("FSS")) == -1)
44770209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
44780209230bSgjelinek 				    "zone scheduling class to FSS");
44790209230bSgjelinek 		}
44800209230bSgjelinek 	}
44810209230bSgjelinek 
44820209230bSgjelinek 	/*
44830209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
44840209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
44850209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
44860209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
44870209230bSgjelinek 	 * and just return if there is nothing to do.
44880209230bSgjelinek 	 *
44890209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
44900209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
44910209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
44920209230bSgjelinek 	 * zonecfg.
44930209230bSgjelinek 	 */
44940209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
44950209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
44960209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
44970209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
44980209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
44990209230bSgjelinek 			    pool_err);
45000209230bSgjelinek 		else
45010209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
45020209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
45030209230bSgjelinek 		zonecfg_fini_handle(handle);
45040209230bSgjelinek 		return (Z_POOL_BIND);
45050209230bSgjelinek 	}
45060209230bSgjelinek 
45070209230bSgjelinek 	/*
45080209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
45090209230bSgjelinek 	 */
45100209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
45110209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
45120209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
45130209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
45140209230bSgjelinek 		    "processor allocation within the specified range\n"
45150209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
45160209230bSgjelinek 		    "See poold(1M).");
45170209230bSgjelinek 	}
45180209230bSgjelinek 
45190209230bSgjelinek 	/* The following is a warning, not an error. */
45200209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
45210209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
45220209230bSgjelinek 		if (res == Z_POOL_BIND)
45230209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
45240209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
45250209230bSgjelinek 		else if (res == Z_POOL)
45260209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
45270209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
45280209230bSgjelinek 		else
45290209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
45300209230bSgjelinek 			    zonecfg_strerror(res));
45310209230bSgjelinek 	}
4532efd4c9b6SSteve Lawrence 
4533efd4c9b6SSteve Lawrence 	/* Update saved pool name in case it has changed */
4534a3002e0aSGarrett D'Amore 	(void) zonecfg_get_poolname(handle, zone_name, pool_name,
4535a3002e0aSGarrett D'Amore 	    sizeof (pool_name));
45360209230bSgjelinek 
45370209230bSgjelinek 	zonecfg_fini_handle(handle);
45380209230bSgjelinek 	return (Z_OK);
45390209230bSgjelinek }
45400209230bSgjelinek 
45410fbb751dSJohn Levon static void
45420fbb751dSJohn Levon report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
45430fbb751dSJohn Levon {
45440fbb751dSJohn Levon 	switch (res) {
45450fbb751dSJohn Levon 	case Z_TOO_BIG:
45460fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value is too large.", name);
45470fbb751dSJohn Levon 		break;
45480fbb751dSJohn Levon 
45490fbb751dSJohn Levon 	case Z_INVALID_PROPERTY:
45500fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
45510fbb751dSJohn Levon 		    name, value);
45520fbb751dSJohn Levon 		break;
45530fbb751dSJohn Levon 
45540fbb751dSJohn Levon 	default:
45550fbb751dSJohn Levon 		zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
45560fbb751dSJohn Levon 		break;
45570fbb751dSJohn Levon 	}
45580fbb751dSJohn Levon }
45590fbb751dSJohn Levon 
45605679c89fSjv227347 /*
45615679c89fSjv227347  * Sets the hostid of the new zone based on its configured value.  The zone's
45625679c89fSjv227347  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
45635679c89fSjv227347  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
45645679c89fSjv227347  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
45655679c89fSjv227347  * ID of the new zone.
45665679c89fSjv227347  *
45675679c89fSjv227347  * This function returns zero on success and a nonzero error code on failure.
45685679c89fSjv227347  */
45695679c89fSjv227347 static int
45700fbb751dSJohn Levon setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
45715679c89fSjv227347 {
45725679c89fSjv227347 	int res;
45735679c89fSjv227347 	char hostidp[HW_HOSTID_LEN];
45745679c89fSjv227347 	unsigned int hostid;
45755679c89fSjv227347 
45760fbb751dSJohn Levon 	res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
45770fbb751dSJohn Levon 
45780fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
45790fbb751dSJohn Levon 		return (Z_OK);
45800fbb751dSJohn Levon 	} else if (res != Z_OK) {
45810fbb751dSJohn Levon 		report_prop_err(zlogp, "hostid", hostidp, res);
45820fbb751dSJohn Levon 		return (res);
45830fbb751dSJohn Levon 	}
45840fbb751dSJohn Levon 
45850fbb751dSJohn Levon 	hostid = (unsigned int)strtoul(hostidp, NULL, 16);
45860fbb751dSJohn Levon 	if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
45870fbb751dSJohn Levon 	    sizeof (hostid))) != 0) {
45880fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
45890fbb751dSJohn Levon 		    "zone hostid is not valid: %s: %d", hostidp, res);
45900fbb751dSJohn Levon 		return (Z_SYSTEM);
45910fbb751dSJohn Levon 	}
45920fbb751dSJohn Levon 
45930fbb751dSJohn Levon 	return (res);
45940fbb751dSJohn Levon }
45950fbb751dSJohn Levon 
45960fbb751dSJohn Levon static int
4597d2a70789SRichard Lowe setup_zone_secflags(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4598d2a70789SRichard Lowe {
4599d2a70789SRichard Lowe 	psecflags_t secflags;
4600d2a70789SRichard Lowe 	struct zone_secflagstab tab = {0};
4601d2a70789SRichard Lowe 	secflagdelta_t delt;
4602d2a70789SRichard Lowe 	int res;
4603d2a70789SRichard Lowe 
4604d2a70789SRichard Lowe 	res = zonecfg_lookup_secflags(handle, &tab);
4605d2a70789SRichard Lowe 
4606d2a70789SRichard Lowe 	if ((res != Z_OK) &&
4607d2a70789SRichard Lowe 	    /* The general defaulting code will handle this */
4608d2a70789SRichard Lowe 	    (res != Z_NO_ENTRY) && (res != Z_BAD_PROPERTY)) {
4609d2a70789SRichard Lowe 		zerror(zlogp, B_FALSE, "security-flags property is "
4610d2a70789SRichard Lowe 		    "invalid: %d", res);
4611d2a70789SRichard Lowe 		return (res);
4612d2a70789SRichard Lowe 	}
4613d2a70789SRichard Lowe 
4614d2a70789SRichard Lowe 	if (strlen(tab.zone_secflags_lower) == 0)
4615d2a70789SRichard Lowe 		(void) strlcpy(tab.zone_secflags_lower, "none",
4616d2a70789SRichard Lowe 		    sizeof (tab.zone_secflags_lower));
4617d2a70789SRichard Lowe 	if (strlen(tab.zone_secflags_default) == 0)
4618d2a70789SRichard Lowe 		(void) strlcpy(tab.zone_secflags_default,
4619d2a70789SRichard Lowe 		    tab.zone_secflags_lower,
4620d2a70789SRichard Lowe 		    sizeof (tab.zone_secflags_default));
4621d2a70789SRichard Lowe 	if (strlen(tab.zone_secflags_upper) == 0)
4622d2a70789SRichard Lowe 		(void) strlcpy(tab.zone_secflags_upper, "all",
4623d2a70789SRichard Lowe 		    sizeof (tab.zone_secflags_upper));
4624d2a70789SRichard Lowe 
4625d2a70789SRichard Lowe 	if (secflags_parse(NULL, tab.zone_secflags_default,
4626d2a70789SRichard Lowe 	    &delt) == -1) {
4627d2a70789SRichard Lowe 		zerror(zlogp, B_FALSE, "default security-flags: '%s'"
4628d2a70789SRichard Lowe 		    "are invalid", tab.zone_secflags_default);
4629d2a70789SRichard Lowe 		return (Z_BAD_PROPERTY);
4630d2a70789SRichard Lowe 	} else if (delt.psd_ass_active != B_TRUE) {
4631d2a70789SRichard Lowe 		zerror(zlogp, B_FALSE, "relative security-flags are not "
4632d2a70789SRichard Lowe 		    "allowed in zone configuration (default "
4633d2a70789SRichard Lowe 		    "security-flags: '%s')",
4634d2a70789SRichard Lowe 		    tab.zone_secflags_default);
4635d2a70789SRichard Lowe 		return (Z_BAD_PROPERTY);
4636d2a70789SRichard Lowe 	} else {
4637d2a70789SRichard Lowe 		secflags_copy(&secflags.psf_inherit, &delt.psd_assign);
4638d2a70789SRichard Lowe 		secflags_copy(&secflags.psf_effective, &delt.psd_assign);
4639d2a70789SRichard Lowe 	}
4640d2a70789SRichard Lowe 
4641d2a70789SRichard Lowe 	if (secflags_parse(NULL, tab.zone_secflags_lower,
4642d2a70789SRichard Lowe 	    &delt) == -1) {
4643d2a70789SRichard Lowe 		zerror(zlogp, B_FALSE, "lower security-flags: '%s'"
4644d2a70789SRichard Lowe 		    "are invalid", tab.zone_secflags_lower);
4645d2a70789SRichard Lowe 		return (Z_BAD_PROPERTY);
4646d2a70789SRichard Lowe 	} else if (delt.psd_ass_active != B_TRUE) {
4647d2a70789SRichard Lowe 		zerror(zlogp, B_FALSE, "relative security-flags are not "
4648d2a70789SRichard Lowe 		    "allowed in zone configuration (lower "
4649d2a70789SRichard Lowe 		    "security-flags: '%s')",
4650d2a70789SRichard Lowe 		    tab.zone_secflags_lower);
4651d2a70789SRichard Lowe 		return (Z_BAD_PROPERTY);
4652d2a70789SRichard Lowe 	} else {
4653d2a70789SRichard Lowe 		secflags_copy(&secflags.psf_lower, &delt.psd_assign);
4654d2a70789SRichard Lowe 	}
4655d2a70789SRichard Lowe 
4656d2a70789SRichard Lowe 	if (secflags_parse(NULL, tab.zone_secflags_upper,
4657d2a70789SRichard Lowe 	    &delt) == -1) {
4658d2a70789SRichard Lowe 		zerror(zlogp, B_FALSE, "upper security-flags: '%s'"
4659d2a70789SRichard Lowe 		    "are invalid", tab.zone_secflags_upper);
4660d2a70789SRichard Lowe 		return (Z_BAD_PROPERTY);
4661d2a70789SRichard Lowe 	} else if (delt.psd_ass_active != B_TRUE) {
4662d2a70789SRichard Lowe 		zerror(zlogp, B_FALSE, "relative security-flags are not "
4663d2a70789SRichard Lowe 		    "allowed in zone configuration (upper "
4664d2a70789SRichard Lowe 		    "security-flags: '%s')",
4665d2a70789SRichard Lowe 		    tab.zone_secflags_upper);
4666d2a70789SRichard Lowe 		return (Z_BAD_PROPERTY);
4667d2a70789SRichard Lowe 	} else {
4668d2a70789SRichard Lowe 		secflags_copy(&secflags.psf_upper, &delt.psd_assign);
4669d2a70789SRichard Lowe 	}
4670d2a70789SRichard Lowe 
4671d2a70789SRichard Lowe 	if (!psecflags_validate(&secflags)) {
4672d2a70789SRichard Lowe 		zerror(zlogp, B_TRUE, "security-flags violate invariants");
4673d2a70789SRichard Lowe 		return (Z_BAD_PROPERTY);
4674d2a70789SRichard Lowe 	}
4675d2a70789SRichard Lowe 
4676d2a70789SRichard Lowe 	if ((res = zone_setattr(zoneid, ZONE_ATTR_SECFLAGS, &secflags,
4677d2a70789SRichard Lowe 	    sizeof (secflags))) != 0) {
4678d2a70789SRichard Lowe 		zerror(zlogp, B_TRUE,
4679d2a70789SRichard Lowe 		    "security-flags couldn't be set: %d", res);
4680d2a70789SRichard Lowe 		return (Z_SYSTEM);
4681d2a70789SRichard Lowe 	}
4682d2a70789SRichard Lowe 
4683d2a70789SRichard Lowe 	return (Z_OK);
4684d2a70789SRichard Lowe }
4685d2a70789SRichard Lowe 
4686d2a70789SRichard Lowe static int
46870fbb751dSJohn Levon setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
46880fbb751dSJohn Levon {
46898cd81a20SJerry Jelinek 	char fsallowed[ZONE_FS_ALLOWED_MAX];
46908cd81a20SJerry Jelinek 	char *fsallowedp = fsallowed;
46918cd81a20SJerry Jelinek 	int len = sizeof (fsallowed);
46920fbb751dSJohn Levon 	int res;
46930fbb751dSJohn Levon 
46948cd81a20SJerry Jelinek 	res = zonecfg_get_fs_allowed(handle, fsallowed, len);
46950fbb751dSJohn Levon 
46960fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
46978cd81a20SJerry Jelinek 		/* No value, set the defaults */
46988cd81a20SJerry Jelinek 		(void) strlcpy(fsallowed, DFLT_FS_ALLOWED, len);
46990fbb751dSJohn Levon 	} else if (res != Z_OK) {
47008cd81a20SJerry Jelinek 		report_prop_err(zlogp, "fs-allowed", fsallowed, res);
47010fbb751dSJohn Levon 		return (res);
47028cd81a20SJerry Jelinek 	} else if (fsallowed[0] == '-') {
4703d2a70789SRichard Lowe 		/* dropping default filesystems - use remaining list */
47048cd81a20SJerry Jelinek 		if (fsallowed[1] != ',')
47058cd81a20SJerry Jelinek 			return (Z_OK);
47068cd81a20SJerry Jelinek 		fsallowedp += 2;
47078cd81a20SJerry Jelinek 		len -= 2;
47088cd81a20SJerry Jelinek 	} else {
47098cd81a20SJerry Jelinek 		/* Has a value, append the defaults */
47108cd81a20SJerry Jelinek 		if (strlcat(fsallowed, ",", len) >= len ||
47118cd81a20SJerry Jelinek 		    strlcat(fsallowed, DFLT_FS_ALLOWED, len) >= len) {
47128cd81a20SJerry Jelinek 			report_prop_err(zlogp, "fs-allowed", fsallowed,
47138cd81a20SJerry Jelinek 			    Z_TOO_BIG);
47148cd81a20SJerry Jelinek 			return (Z_TOO_BIG);
47158cd81a20SJerry Jelinek 		}
47160fbb751dSJohn Levon 	}
47170fbb751dSJohn Levon 
47188cd81a20SJerry Jelinek 	if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, fsallowedp, len) != 0) {
47190fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
47200fbb751dSJohn Levon 		    "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
47210fbb751dSJohn Levon 		return (Z_SYSTEM);
47220fbb751dSJohn Levon 	}
47230fbb751dSJohn Levon 
47248cd81a20SJerry Jelinek 	return (Z_OK);
47250fbb751dSJohn Levon }
47260fbb751dSJohn Levon 
47270fbb751dSJohn Levon static int
47280fbb751dSJohn Levon setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
47290fbb751dSJohn Levon {
47300fbb751dSJohn Levon 	zone_dochandle_t handle;
47310fbb751dSJohn Levon 	int res = Z_OK;
47320fbb751dSJohn Levon 
47335679c89fSjv227347 	if ((handle = zonecfg_init_handle()) == NULL) {
47345679c89fSjv227347 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
47355679c89fSjv227347 		return (Z_BAD_HANDLE);
47365679c89fSjv227347 	}
47375679c89fSjv227347 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
47385679c89fSjv227347 		zerror(zlogp, B_FALSE, "invalid configuration");
47390fbb751dSJohn Levon 		goto out;
47405679c89fSjv227347 	}
47415679c89fSjv227347 
47420fbb751dSJohn Levon 	if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
47430fbb751dSJohn Levon 		goto out;
47440fbb751dSJohn Levon 
47450fbb751dSJohn Levon 	if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
47460fbb751dSJohn Levon 		goto out;
47470fbb751dSJohn Levon 
4748d2a70789SRichard Lowe 	if ((res = setup_zone_secflags(handle, zlogp, zoneid)) != Z_OK)
4749d2a70789SRichard Lowe 		goto out;
4750d2a70789SRichard Lowe 
47510fbb751dSJohn Levon out:
47525679c89fSjv227347 	zonecfg_fini_handle(handle);
47535679c89fSjv227347 	return (res);
47545679c89fSjv227347 }
47555679c89fSjv227347 
4756108322fbScarlsonj zoneid_t
47576cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4758108322fbScarlsonj {
4759108322fbScarlsonj 	zoneid_t rval = -1;
47607c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
47617c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
47627c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
4763108322fbScarlsonj 	size_t rctlbufsz = 0;
4764fa9e4066Sahrens 	char *zfsbuf = NULL;
4765fa9e4066Sahrens 	size_t zfsbufsz = 0;
4766108322fbScarlsonj 	zoneid_t zoneid = -1;
47677c478bd9Sstevel@tonic-gate 	int xerr;
4768108322fbScarlsonj 	char *kzone;
4769108322fbScarlsonj 	FILE *fp = NULL;
477045916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
477145916cd2Sjpk 	int match = 0;
477245916cd2Sjpk 	int doi = 0;
4773f4b3ec61Sdh155122 	int flags;
4774f4b3ec61Sdh155122 	zone_iptype_t iptype;
47757c478bd9Sstevel@tonic-gate 
47767c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
47777c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
47787c478bd9Sstevel@tonic-gate 		return (-1);
47797c478bd9Sstevel@tonic-gate 	}
4780108322fbScarlsonj 	if (zonecfg_in_alt_root())
4781108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
47827c478bd9Sstevel@tonic-gate 
47832b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
4784f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4785f4b3ec61Sdh155122 		return (-1);
4786f4b3ec61Sdh155122 	}
4787f4b3ec61Sdh155122 	switch (iptype) {
4788f4b3ec61Sdh155122 	case ZS_SHARED:
4789f4b3ec61Sdh155122 		flags = 0;
4790f4b3ec61Sdh155122 		break;
4791f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
4792f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
4793f4b3ec61Sdh155122 		break;
4794f4b3ec61Sdh155122 	}
4795f4b3ec61Sdh155122 
47967c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
47977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
47987c478bd9Sstevel@tonic-gate 		return (-1);
47997c478bd9Sstevel@tonic-gate 	}
48007c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4801ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
48027c478bd9Sstevel@tonic-gate 		goto error;
4803ffbafc53Scomay 
48046cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
48056cfd72c6Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
48067c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
48077c478bd9Sstevel@tonic-gate 		goto error;
48087c478bd9Sstevel@tonic-gate 	}
4809ffbafc53Scomay 
4810fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4811fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4812fa9e4066Sahrens 		goto error;
4813fa9e4066Sahrens 	}
48147c478bd9Sstevel@tonic-gate 
48156cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
481645916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
481748451833Scarlsonj 		if (zcent != NULL) {
481845916cd2Sjpk 			match = zcent->zc_match;
481945916cd2Sjpk 			doi = zcent->zc_doi;
482045916cd2Sjpk 			*zlabel = zcent->zc_label;
482145916cd2Sjpk 		} else {
482245916cd2Sjpk 			goto error;
482345916cd2Sjpk 		}
48244201a95eSRic Aleshire 		if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
48254201a95eSRic Aleshire 			goto error;
482645916cd2Sjpk 	}
482745916cd2Sjpk 
4828108322fbScarlsonj 	kzone = zone_name;
4829108322fbScarlsonj 
4830108322fbScarlsonj 	/*
4831108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4832108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4833108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4834108322fbScarlsonj 	 * that use this same source node (same device and inode).
4835108322fbScarlsonj 	 */
4836108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4837108322fbScarlsonj 		goto error;
4838108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4839108322fbScarlsonj 		goto error;
4840108322fbScarlsonj 
48416cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4842108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4843108322fbScarlsonj 
4844108322fbScarlsonj 		/*
4845108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4846108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4847108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4848108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4849108322fbScarlsonj 		 * executables.
4850108322fbScarlsonj 		 *
4851108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4852108322fbScarlsonj 		 */
4853108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4854108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4855108322fbScarlsonj 			goto error;
4856108322fbScarlsonj 		}
4857108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4858108322fbScarlsonj 			goto error;
4859108322fbScarlsonj 	}
4860108322fbScarlsonj 
4861108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4862108322fbScarlsonj 		/*
4863108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4864108322fbScarlsonj 		 * then we have some additional work to do before starting the
4865108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4866108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4867108322fbScarlsonj 		 * the zone.
4868108322fbScarlsonj 		 */
4869108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4870108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4871108322fbScarlsonj 			goto error;
4872108322fbScarlsonj 		}
4873108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4874108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4875108322fbScarlsonj 			goto error;
4876108322fbScarlsonj 		}
4877108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4878108322fbScarlsonj 		    NULL, 0) == 0) {
4879108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4880108322fbScarlsonj 			goto error;
4881108322fbScarlsonj 		}
4882108322fbScarlsonj 		/* This is the preferred name */
4883108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4884108322fbScarlsonj 		    zone_name);
4885108322fbScarlsonj 		srandom(getpid());
4886108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4887108322fbScarlsonj 		    0) == 0) {
4888108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4889108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4890108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4891108322fbScarlsonj 		}
4892108322fbScarlsonj 		kzone = kernzone;
4893108322fbScarlsonj 	}
4894108322fbScarlsonj 
48957c478bd9Sstevel@tonic-gate 	xerr = 0;
4896108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4897f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4898f4b3ec61Sdh155122 	    flags)) == -1) {
48997c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
49007c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
49017c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
49027c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
49037c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
49047c478bd9Sstevel@tonic-gate 			} else {
49057c478bd9Sstevel@tonic-gate 
49067c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
49077c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
49087c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
49097c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
49107c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
49117c478bd9Sstevel@tonic-gate 			}
49127c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
49137c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
49147c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
49157c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
49168c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
49178c55461bSton 			char zonename[ZONENAME_MAX];
49188c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
49198c55461bSton 			    zonename, ZONENAME_MAX);
49208c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
49218c55461bSton 			    "used by the zone '%s'.", zonename);
49227c478bd9Sstevel@tonic-gate 		} else {
49237c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
49247c478bd9Sstevel@tonic-gate 		}
49257c478bd9Sstevel@tonic-gate 		goto error;
49267c478bd9Sstevel@tonic-gate 	}
4927108322fbScarlsonj 
4928108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4929108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4930108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4931108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4932108322fbScarlsonj 		goto error;
4933108322fbScarlsonj 	}
4934108322fbScarlsonj 
49350679f794S 	/*
49360679f794S 	 * The following actions are not performed when merely mounting a zone
49370679f794S 	 * for administrative use.
49380679f794S 	 */
49390679f794S 	if (mount_cmd == Z_MNT_BOOT) {
49400679f794S 		brand_handle_t bh;
49410679f794S 		struct brand_attr attr;
49420679f794S 		char modname[MAXPATHLEN];
49430679f794S 
49440fbb751dSJohn Levon 		if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
49455679c89fSjv227347 			goto error;
49465679c89fSjv227347 
494762868012SSteve Lawrence 		if ((bh = brand_open(brand_name)) == NULL) {
49480679f794S 			zerror(zlogp, B_FALSE,
49490679f794S 			    "unable to determine brand name");
49505679c89fSjv227347 			goto error;
49519acbbeafSnn35248 		}
49529acbbeafSnn35248 
49539a5d73e0SRic Aleshire 		if (!is_system_labeled() &&
495462868012SSteve Lawrence 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
49559a5d73e0SRic Aleshire 			brand_close(bh);
49569a5d73e0SRic Aleshire 			zerror(zlogp, B_FALSE,
49579a5d73e0SRic Aleshire 			    "cannot boot labeled zone on unlabeled system");
49589a5d73e0SRic Aleshire 			goto error;
49599a5d73e0SRic Aleshire 		}
49609a5d73e0SRic Aleshire 
49619acbbeafSnn35248 		/*
49629acbbeafSnn35248 		 * If this brand requires any kernel support, now is the time to
49639acbbeafSnn35248 		 * get it loaded and initialized.
49649acbbeafSnn35248 		 */
4965123807fbSedp 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4966e767a340Sgjelinek 			brand_close(bh);
49670679f794S 			zerror(zlogp, B_FALSE,
49680679f794S 			    "unable to determine brand kernel module");
49695679c89fSjv227347 			goto error;
49709acbbeafSnn35248 		}
4971e767a340Sgjelinek 		brand_close(bh);
49729acbbeafSnn35248 
49739acbbeafSnn35248 		if (strlen(modname) > 0) {
497462868012SSteve Lawrence 			(void) strlcpy(attr.ba_brandname, brand_name,
497562868012SSteve Lawrence 			    sizeof (attr.ba_brandname));
497662868012SSteve Lawrence 			(void) strlcpy(attr.ba_modname, modname,
497762868012SSteve Lawrence 			    sizeof (attr.ba_modname));
49789acbbeafSnn35248 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
49799acbbeafSnn35248 			    sizeof (attr) != 0)) {
49800679f794S 				zerror(zlogp, B_TRUE,
49810679f794S 				    "could not set zone brand attribute.");
49829acbbeafSnn35248 				goto error;
49839acbbeafSnn35248 			}
49849acbbeafSnn35248 		}
49859acbbeafSnn35248 
49865679c89fSjv227347 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
49870209230bSgjelinek 			goto error;
49880209230bSgjelinek 
498945916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
49900209230bSgjelinek 	}
49910209230bSgjelinek 
4992108322fbScarlsonj 	rval = zoneid;
4993108322fbScarlsonj 	zoneid = -1;
4994108322fbScarlsonj 
49957c478bd9Sstevel@tonic-gate error:
49965679c89fSjv227347 	if (zoneid != -1) {
49975679c89fSjv227347 		(void) zone_shutdown(zoneid);
4998108322fbScarlsonj 		(void) zone_destroy(zoneid);
49995679c89fSjv227347 	}
50007c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
50017c478bd9Sstevel@tonic-gate 		free(rctlbuf);
50027c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
5003108322fbScarlsonj 	if (fp != NULL)
5004108322fbScarlsonj 		zonecfg_close_scratch(fp);
5005108322fbScarlsonj 	lofs_discard_mnttab();
500645916cd2Sjpk 	if (zcent != NULL)
500745916cd2Sjpk 		tsol_freezcent(zcent);
50087c478bd9Sstevel@tonic-gate 	return (rval);
50097c478bd9Sstevel@tonic-gate }
50107c478bd9Sstevel@tonic-gate 
5011555afedfScarlsonj /*
5012555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
5013555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
5014555afedfScarlsonj  * details from inside the zone.
5015555afedfScarlsonj  */
5016555afedfScarlsonj static void
5017555afedfScarlsonj write_index_file(zoneid_t zoneid)
5018555afedfScarlsonj {
5019555afedfScarlsonj 	FILE *zef;
5020555afedfScarlsonj 	FILE *zet;
5021555afedfScarlsonj 	struct zoneent *zep;
5022555afedfScarlsonj 	pid_t child;
5023555afedfScarlsonj 	int tmpl_fd;
5024555afedfScarlsonj 	ctid_t ct;
5025555afedfScarlsonj 	int fd;
5026555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
5027555afedfScarlsonj 
5028555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
5029555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
5030555afedfScarlsonj 		return;
5031555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
5032555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
5033555afedfScarlsonj 			break;
5034555afedfScarlsonj 		free(zep);
5035555afedfScarlsonj 	}
5036555afedfScarlsonj 	endzoneent(zef);
5037555afedfScarlsonj 	if (zep == NULL)
5038555afedfScarlsonj 		return;
5039555afedfScarlsonj 
5040555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
5041555afedfScarlsonj 		free(zep);
5042555afedfScarlsonj 		return;
5043555afedfScarlsonj 	}
5044555afedfScarlsonj 
5045555afedfScarlsonj 	if ((child = fork()) == -1) {
5046555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
5047555afedfScarlsonj 		(void) close(tmpl_fd);
5048555afedfScarlsonj 		free(zep);
5049555afedfScarlsonj 		return;
5050555afedfScarlsonj 	}
5051555afedfScarlsonj 
5052555afedfScarlsonj 	/* parent waits for child to finish */
5053555afedfScarlsonj 	if (child != 0) {
5054555afedfScarlsonj 		free(zep);
5055555afedfScarlsonj 		if (contract_latest(&ct) == -1)
5056555afedfScarlsonj 			ct = -1;
5057555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
5058555afedfScarlsonj 		(void) close(tmpl_fd);
5059555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
5060555afedfScarlsonj 		(void) contract_abandon_id(ct);
5061555afedfScarlsonj 		return;
5062555afedfScarlsonj 	}
5063555afedfScarlsonj 
5064555afedfScarlsonj 	/* child enters zone and sets up index file */
5065555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
5066555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
5067555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
5068555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
5069555afedfScarlsonj 		    ZONE_CONFIG_GID);
5070555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
5071555afedfScarlsonj 		    ZONE_INDEX_MODE);
5072555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
5073555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
5074555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
5075555afedfScarlsonj 				uuidstr[0] = '\0';
5076555afedfScarlsonj 			else
5077555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
5078555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
5079555afedfScarlsonj 			    zone_state_str(zep->zone_state),
5080555afedfScarlsonj 			    uuidstr);
5081555afedfScarlsonj 			(void) fclose(zet);
5082555afedfScarlsonj 		}
5083555afedfScarlsonj 	}
5084555afedfScarlsonj 	_exit(0);
5085555afedfScarlsonj }
5086555afedfScarlsonj 
50877c478bd9Sstevel@tonic-gate int
50886cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
50897c478bd9Sstevel@tonic-gate {
50909acbbeafSnn35248 	char zonepath[MAXPATHLEN];
50915749802bSdp 
50926cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
5093fa9e4066Sahrens 		lofs_discard_mnttab();
5094fa9e4066Sahrens 		return (-1);
5095fa9e4066Sahrens 	}
5096fa9e4066Sahrens 
50979acbbeafSnn35248 	/*
50989acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
50999acbbeafSnn35248 	 * attribute backing store for /dev
51009acbbeafSnn35248 	 */
51019acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
51029acbbeafSnn35248 		lofs_discard_mnttab();
51039acbbeafSnn35248 		return (-1);
51049acbbeafSnn35248 	}
510516bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
510627e6fb21Sdp 
510727e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
510827e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
510927e6fb21Sdp 	    0, 3) != 0) {
5110108322fbScarlsonj 		lofs_discard_mnttab();
51117c478bd9Sstevel@tonic-gate 		return (-1);
5112108322fbScarlsonj 	}
5113facf4a8dSllai1 
51149acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
5115facf4a8dSllai1 		lofs_discard_mnttab();
5116facf4a8dSllai1 		return (-1);
5117facf4a8dSllai1 	}
5118facf4a8dSllai1 
51196cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
5120f4b3ec61Sdh155122 		zone_iptype_t iptype;
5121f4b3ec61Sdh155122 
51222b24ab6bSSebastien Roy 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
5123f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
5124108322fbScarlsonj 			lofs_discard_mnttab();
51257c478bd9Sstevel@tonic-gate 			return (-1);
5126108322fbScarlsonj 		}
5127555afedfScarlsonj 
5128f4b3ec61Sdh155122 		switch (iptype) {
5129f4b3ec61Sdh155122 		case ZS_SHARED:
5130f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
5131f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
5132f4b3ec61Sdh155122 				lofs_discard_mnttab();
5133f4b3ec61Sdh155122 				return (-1);
5134f4b3ec61Sdh155122 			}
5135f4b3ec61Sdh155122 			break;
5136f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
5137550b6e40SSowmini Varadhan 			if (configure_exclusive_network_interfaces(zlogp,
5138550b6e40SSowmini Varadhan 			    zoneid) !=
5139f4b3ec61Sdh155122 			    0) {
5140f4b3ec61Sdh155122 				lofs_discard_mnttab();
5141f4b3ec61Sdh155122 				return (-1);
5142f4b3ec61Sdh155122 			}
5143f4b3ec61Sdh155122 			break;
5144f4b3ec61Sdh155122 		}
5145f4b3ec61Sdh155122 	}
5146f4b3ec61Sdh155122 
5147555afedfScarlsonj 	write_index_file(zoneid);
5148555afedfScarlsonj 
5149108322fbScarlsonj 	lofs_discard_mnttab();
51507c478bd9Sstevel@tonic-gate 	return (0);
51517c478bd9Sstevel@tonic-gate }
51527c478bd9Sstevel@tonic-gate 
5153108322fbScarlsonj static int
5154108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
51557c478bd9Sstevel@tonic-gate {
5156108322fbScarlsonj 	char zroot[MAXPATHLEN];
5157108322fbScarlsonj 
5158108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5159108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
5160108322fbScarlsonj 		return (-1);
5161108322fbScarlsonj 	}
5162108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
5163108322fbScarlsonj 
5164108322fbScarlsonj 	/*
5165108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
5166108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
5167108322fbScarlsonj 	 * still be creds floating about in the system that reference the
5168108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
5169108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
5170108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
5171108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
5172108322fbScarlsonj 	 */
5173108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
5174108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
5175108322fbScarlsonj 			goto unmounted;
5176108322fbScarlsonj 		if (errno == EBUSY) {
5177108322fbScarlsonj 			int tries = 10;
5178108322fbScarlsonj 
5179108322fbScarlsonj 			while (--tries >= 0) {
5180108322fbScarlsonj 				(void) sleep(1);
5181108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
5182108322fbScarlsonj 					goto unmounted;
5183108322fbScarlsonj 				if (errno != EBUSY)
5184108322fbScarlsonj 					break;
5185108322fbScarlsonj 			}
5186108322fbScarlsonj 		}
5187108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
5188108322fbScarlsonj 		return (-1);
5189108322fbScarlsonj 	}
5190108322fbScarlsonj unmounted:
5191108322fbScarlsonj 
5192108322fbScarlsonj 	/*
5193108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
5194108322fbScarlsonj 	 * entries.
5195108322fbScarlsonj 	 */
5196108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5197108322fbScarlsonj 		FILE *fp;
5198108322fbScarlsonj 		int retv;
5199108322fbScarlsonj 
5200108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5201108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
5202108322fbScarlsonj 			return (-1);
5203108322fbScarlsonj 		}
5204108322fbScarlsonj 		retv = -1;
5205108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
5206108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
5207108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
5208108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
5209108322fbScarlsonj 		else
5210108322fbScarlsonj 			retv = 0;
5211108322fbScarlsonj 		zonecfg_close_scratch(fp);
5212108322fbScarlsonj 		return (retv);
5213108322fbScarlsonj 	} else {
5214108322fbScarlsonj 		return (0);
5215108322fbScarlsonj 	}
5216108322fbScarlsonj }
5217108322fbScarlsonj 
5218108322fbScarlsonj int
52190209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
5220108322fbScarlsonj {
5221108322fbScarlsonj 	char *kzone;
52227c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
52230209230bSgjelinek 	int res;
52240209230bSgjelinek 	char pool_err[128];
5225ff17c8bfSgjelinek 	char zpath[MAXPATHLEN];
52269acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
5227123807fbSedp 	brand_handle_t bh = NULL;
52282b24ab6bSSebastien Roy 	dladm_status_t status;
52292b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
5230f4b3ec61Sdh155122 	ushort_t flags;
52317c478bd9Sstevel@tonic-gate 
5232108322fbScarlsonj 	kzone = zone_name;
5233108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5234108322fbScarlsonj 		FILE *fp;
5235108322fbScarlsonj 
5236108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5237108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
5238108322fbScarlsonj 			goto error;
5239108322fbScarlsonj 		}
5240108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5241108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
5242108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
5243108322fbScarlsonj 			zonecfg_close_scratch(fp);
5244108322fbScarlsonj 			goto error;
5245108322fbScarlsonj 		}
5246108322fbScarlsonj 		zonecfg_close_scratch(fp);
5247108322fbScarlsonj 		kzone = kernzone;
5248108322fbScarlsonj 	}
5249108322fbScarlsonj 
5250108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
52517c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
52527c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
5253108322fbScarlsonj 		if (unmount_cmd)
5254108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
52557c478bd9Sstevel@tonic-gate 		goto error;
52567c478bd9Sstevel@tonic-gate 	}
52577c478bd9Sstevel@tonic-gate 
52580dc2366fSVenugopal Iyer 	if (remove_datalink_pool(zlogp, zoneid) != 0) {
52590dc2366fSVenugopal Iyer 		zerror(zlogp, B_FALSE, "unable clear datalink pool property");
52600dc2366fSVenugopal Iyer 		goto error;
52610dc2366fSVenugopal Iyer 	}
52620dc2366fSVenugopal Iyer 
5263550b6e40SSowmini Varadhan 	if (remove_datalink_protect(zlogp, zoneid) != 0) {
5264550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE,
5265550b6e40SSowmini Varadhan 		    "unable clear datalink protect property");
5266550b6e40SSowmini Varadhan 		goto error;
5267550b6e40SSowmini Varadhan 	}
5268550b6e40SSowmini Varadhan 
5269550b6e40SSowmini Varadhan 	/*
5270550b6e40SSowmini Varadhan 	 * The datalinks assigned to the zone will be removed from the NGZ as
5271550b6e40SSowmini Varadhan 	 * part of zone_shutdown() so that we need to remove protect/pool etc.
5272550b6e40SSowmini Varadhan 	 * before zone_shutdown(). Even if the shutdown itself fails, the zone
5273550b6e40SSowmini Varadhan 	 * will not be able to violate any constraints applied because the
5274550b6e40SSowmini Varadhan 	 * datalinks are no longer available to the zone.
5275550b6e40SSowmini Varadhan 	 */
52767c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
52777c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
52787c478bd9Sstevel@tonic-gate 		goto error;
52797c478bd9Sstevel@tonic-gate 	}
52807c478bd9Sstevel@tonic-gate 
5281ff17c8bfSgjelinek 	/* Get the zonepath of this zone */
5282ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
5283ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
52849acbbeafSnn35248 		goto error;
52859acbbeafSnn35248 	}
52869acbbeafSnn35248 
52879acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
528862868012SSteve Lawrence 	if ((bh = brand_open(brand_name)) == NULL) {
52899acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
52909acbbeafSnn35248 		return (-1);
52919acbbeafSnn35248 	}
52929acbbeafSnn35248 	/*
52939acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
52949acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
52959acbbeafSnn35248 	 */
52969acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
5297ff17c8bfSgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
5298ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
5299123807fbSedp 		brand_close(bh);
53009acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
53019acbbeafSnn35248 		    "halt callback.");
53029acbbeafSnn35248 		goto error;
53039acbbeafSnn35248 	}
5304123807fbSedp 	brand_close(bh);
53059acbbeafSnn35248 
53069acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
5307c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
53089acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
53099acbbeafSnn35248 		goto error;
53109acbbeafSnn35248 	}
53119acbbeafSnn35248 
5312f4b3ec61Sdh155122 	if (!unmount_cmd) {
5313f4b3ec61Sdh155122 		zone_iptype_t iptype;
5314f4b3ec61Sdh155122 
5315f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
5316f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
53172b24ab6bSSebastien Roy 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
5318f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
5319f4b3ec61Sdh155122 				    "ip-type");
53207c478bd9Sstevel@tonic-gate 				goto error;
53217c478bd9Sstevel@tonic-gate 			}
5322f4b3ec61Sdh155122 		} else {
5323f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
5324f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
5325f4b3ec61Sdh155122 			else
5326f4b3ec61Sdh155122 				iptype = ZS_SHARED;
5327f4b3ec61Sdh155122 		}
5328f4b3ec61Sdh155122 
5329f4b3ec61Sdh155122 		switch (iptype) {
5330f4b3ec61Sdh155122 		case ZS_SHARED:
5331f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
5332f4b3ec61Sdh155122 			    zoneid) != 0) {
5333f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
5334f4b3ec61Sdh155122 				    "network interfaces in zone");
5335f4b3ec61Sdh155122 				goto error;
5336f4b3ec61Sdh155122 			}
5337f4b3ec61Sdh155122 			break;
5338f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
5339f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
5340f4b3ec61Sdh155122 			    zoneid) != 0) {
5341f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
5342f4b3ec61Sdh155122 				    "network interfaces in zone");
5343f4b3ec61Sdh155122 				goto error;
5344f4b3ec61Sdh155122 			}
53452b24ab6bSSebastien Roy 			status = dladm_zone_halt(dld_handle, zoneid);
53462b24ab6bSSebastien Roy 			if (status != DLADM_STATUS_OK) {
53472b24ab6bSSebastien Roy 				zerror(zlogp, B_FALSE, "unable to notify "
53482b24ab6bSSebastien Roy 				    "dlmgmtd of zone halt: %s",
53492b24ab6bSSebastien Roy 				    dladm_status2str(status, errmsg));
53502b24ab6bSSebastien Roy 			}
5351f4b3ec61Sdh155122 			break;
5352f4b3ec61Sdh155122 		}
5353f4b3ec61Sdh155122 	}
53547c478bd9Sstevel@tonic-gate 
5355108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
53567c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
53577c478bd9Sstevel@tonic-gate 		goto error;
53587c478bd9Sstevel@tonic-gate 	}
53597c478bd9Sstevel@tonic-gate 
5360108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
53617c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
53627c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
53637c478bd9Sstevel@tonic-gate 		goto error;
53647c478bd9Sstevel@tonic-gate 	}
53657c478bd9Sstevel@tonic-gate 
53660209230bSgjelinek 	/*
5367cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
5368cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
5369cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
5370cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
5371cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
5372cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
5373cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
5374cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
53750209230bSgjelinek 	 */
5376cf6b13d2Sgjelinek 	if (!unmount_cmd) {
5377cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
5378cf6b13d2Sgjelinek 
5379cf6b13d2Sgjelinek 		if (rebooting) {
5380cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
5381cf6b13d2Sgjelinek 			zone_dochandle_t handle;
5382cf6b13d2Sgjelinek 
5383cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
5384cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
5385cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
5386cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
5387e767a340Sgjelinek 
5388e767a340Sgjelinek 			zonecfg_fini_handle(handle);
5389cf6b13d2Sgjelinek 		}
5390cf6b13d2Sgjelinek 
5391cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
53920209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
53930209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
53940209230bSgjelinek 				if (res == Z_POOL)
53950209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
53960209230bSgjelinek 			}
53970209230bSgjelinek 		}
5398cf6b13d2Sgjelinek 	}
53990209230bSgjelinek 
540045916cd2Sjpk 	remove_mlps(zlogp, zoneid);
540145916cd2Sjpk 
54027c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
54037c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
54047c478bd9Sstevel@tonic-gate 		goto error;
54057c478bd9Sstevel@tonic-gate 	}
5406108322fbScarlsonj 
5407108322fbScarlsonj 	/*
5408108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
5409108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
5410108322fbScarlsonj 	 */
5411108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5412108322fbScarlsonj 		goto error;
5413108322fbScarlsonj 
5414108322fbScarlsonj 	lofs_discard_mnttab();
54157c478bd9Sstevel@tonic-gate 	return (0);
54167c478bd9Sstevel@tonic-gate 
54177c478bd9Sstevel@tonic-gate error:
5418108322fbScarlsonj 	lofs_discard_mnttab();
54197c478bd9Sstevel@tonic-gate 	return (-1);
54207c478bd9Sstevel@tonic-gate }
5421