xref: /titanic_54/usr/src/cmd/zoneadmd/vplat.c (revision 8cd81a20c40b49e1fad4022a2774ec6ad5066532)
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.
24*8cd81a20SJerry Jelinek  * Copyright (c) 2013, Joyent Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
28a3002e0aSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
29a3002e0aSGarrett D'Amore  */
30a3002e0aSGarrett D'Amore 
31a3002e0aSGarrett D'Amore /*
327c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
337c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
347c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
357c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
367c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
377c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
387c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
39108322fbScarlsonj  *
40108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
41108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
42108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
43108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
44108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
45108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
46108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
47108322fbScarlsonj  * without relying on any binaries in the zone itself.
48108322fbScarlsonj  *
49108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
50108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
51108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
52108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
53108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
54108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
55108322fbScarlsonj  * process.
56108322fbScarlsonj  *
57108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
58108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
59108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
60108322fbScarlsonj  * $ZONEPATH/lu as the root.
61108322fbScarlsonj  *
62108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
63108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
64108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
65108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <sys/param.h>
697c478bd9Sstevel@tonic-gate #include <sys/mount.h>
707c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
717c478bd9Sstevel@tonic-gate #include <sys/socket.h>
727c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
737c478bd9Sstevel@tonic-gate #include <sys/types.h>
747c478bd9Sstevel@tonic-gate #include <sys/stat.h>
757c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
767c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
777c478bd9Sstevel@tonic-gate #include <sys/conf.h>
785679c89fSjv227347 #include <sys/systeminfo.h>
797c478bd9Sstevel@tonic-gate 
80f4b3ec61Sdh155122 #include <libdlpi.h>
81f595a68aSyz147064 #include <libdllink.h>
82d62bc4baSyz147064 #include <libdlvlan.h>
83f4b3ec61Sdh155122 
847c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
857c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
867c478bd9Sstevel@tonic-gate #include <netinet/in.h>
877c478bd9Sstevel@tonic-gate #include <net/route.h>
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #include <stdio.h>
907c478bd9Sstevel@tonic-gate #include <errno.h>
917c478bd9Sstevel@tonic-gate #include <fcntl.h>
927c478bd9Sstevel@tonic-gate #include <unistd.h>
937c478bd9Sstevel@tonic-gate #include <rctl.h>
947c478bd9Sstevel@tonic-gate #include <stdlib.h>
957c478bd9Sstevel@tonic-gate #include <string.h>
967c478bd9Sstevel@tonic-gate #include <strings.h>
977c478bd9Sstevel@tonic-gate #include <wait.h>
987c478bd9Sstevel@tonic-gate #include <limits.h>
997c478bd9Sstevel@tonic-gate #include <libgen.h>
100fa9e4066Sahrens #include <libzfs.h>
101facf4a8dSllai1 #include <libdevinfo.h>
1027c478bd9Sstevel@tonic-gate #include <zone.h>
1037c478bd9Sstevel@tonic-gate #include <assert.h>
104555afedfScarlsonj #include <libcontract.h>
105555afedfScarlsonj #include <libcontract_priv.h>
106555afedfScarlsonj #include <uuid/uuid.h>
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1097c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1107c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1117c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
112fa9e4066Sahrens #include <sys/fs/zfs.h>
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #include <pool.h>
1157c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1160209230bSgjelinek #include <sys/priocntl.h>
1177c478bd9Sstevel@tonic-gate 
1189acbbeafSnn35248 #include <libbrand.h>
1199acbbeafSnn35248 #include <sys/brand.h>
1207c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
12139d3e169Sevanl #include <synch.h>
12222321485Svp157776 
1237c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
12445916cd2Sjpk #include <tsol/label.h>
12545916cd2Sjpk #include <libtsnet.h>
12645916cd2Sjpk #include <sys/priv.h>
127550b6e40SSowmini Varadhan #include <libinetutil.h>
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1307c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1317c478bd9Sstevel@tonic-gate 
1326e1ae2a3SGary Pennington #define	RESOURCE_DEFAULT_OPTS \
1337c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13645916cd2Sjpk #define	MAXTNZLEN	2048
1377c478bd9Sstevel@tonic-gate 
1386cfd72c6Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1396cfd72c6Sgjelinek 
140ff19e029SMenno Lageman /* a reasonable estimate for the number of lwps per process */
141ff19e029SMenno Lageman #define	LWPS_PER_PROCESS	10
142ff19e029SMenno Lageman 
1437c478bd9Sstevel@tonic-gate /* for routing socket */
1447c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1457c478bd9Sstevel@tonic-gate 
146108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
147108322fbScarlsonj static char kernzone[ZONENAME_MAX];
148108322fbScarlsonj 
149108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
150108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
151108322fbScarlsonj 
15245916cd2Sjpk /* for Trusted Extensions */
15345916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
15445916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
15545916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
1569d48116cSdh155122 
15745916cd2Sjpk static m_label_t *zlabel = NULL;
15845916cd2Sjpk static m_label_t *zid_label = NULL;
15945916cd2Sjpk static priv_set_t *zprivs = NULL;
16045916cd2Sjpk 
161*8cd81a20SJerry Jelinek static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn";
162*8cd81a20SJerry Jelinek 
1637c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1647c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1657c478bd9Sstevel@tonic-gate 
166c5cd6260S /* from zoneadmd */
167c5cd6260S extern char query_hook[];
168c5cd6260S 
1697c478bd9Sstevel@tonic-gate /*
170550b6e40SSowmini Varadhan  * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
171550b6e40SSowmini Varadhan  * node in a linked list that is sorted by linkid.  The list is constructed as
172550b6e40SSowmini Varadhan  * the xml configuration file is parsed, and the information
173550b6e40SSowmini Varadhan  * contained in each node is added to the kernel before the zone is
174550b6e40SSowmini Varadhan  * booted, to be retrieved and applied from within the exclusive-IP NGZ
175550b6e40SSowmini Varadhan  * on boot.
176550b6e40SSowmini Varadhan  */
177550b6e40SSowmini Varadhan typedef struct zone_addr_list {
178550b6e40SSowmini Varadhan 	struct zone_addr_list *za_next;
179550b6e40SSowmini Varadhan 	datalink_id_t za_linkid;	/* datalink_id_t of interface */
180550b6e40SSowmini Varadhan 	struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
181550b6e40SSowmini Varadhan } zone_addr_list_t;
182550b6e40SSowmini Varadhan 
183550b6e40SSowmini Varadhan /*
184108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
185108322fbScarlsonj  * data) only once every N times through the loop.
186108322fbScarlsonj  */
187108322fbScarlsonj #define	MNTTAB_HUNK	32
188108322fbScarlsonj 
189550b6e40SSowmini Varadhan /* some handy macros */
190550b6e40SSowmini Varadhan #define	SIN(s)	((struct sockaddr_in *)s)
191550b6e40SSowmini Varadhan #define	SIN6(s)	((struct sockaddr_in6 *)s)
192550b6e40SSowmini Varadhan 
193108322fbScarlsonj /*
1947c478bd9Sstevel@tonic-gate  * Private autofs system call
1957c478bd9Sstevel@tonic-gate  */
1967c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate static int
1997c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
207108322fbScarlsonj static void
208108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
209108322fbScarlsonj {
210108322fbScarlsonj 	uint_t i;
211108322fbScarlsonj 
212108322fbScarlsonj 	if (mnt_array == NULL)
213108322fbScarlsonj 		return;
214108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
215108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
216108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
217108322fbScarlsonj 		free(mnt_array[i].mnt_special);
218108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
219108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
220108322fbScarlsonj 	}
221108322fbScarlsonj 	free(mnt_array);
222108322fbScarlsonj }
223108322fbScarlsonj 
224108322fbScarlsonj /*
225108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
226108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
227108322fbScarlsonj  * array in "nelemp".
228108322fbScarlsonj  */
229108322fbScarlsonj static int
230108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
231108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
232108322fbScarlsonj {
233108322fbScarlsonj 	struct mnttab mnt;
234108322fbScarlsonj 	struct mnttab *mnts;
235108322fbScarlsonj 	struct mnttab *mnp;
236108322fbScarlsonj 	uint_t nmnt;
237108322fbScarlsonj 
238108322fbScarlsonj 	rewind(mnttab);
239108322fbScarlsonj 	resetmnttab(mnttab);
240108322fbScarlsonj 	nmnt = 0;
241108322fbScarlsonj 	mnts = NULL;
242108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
243108322fbScarlsonj 		struct mnttab *tmp_array;
244108322fbScarlsonj 
245108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
246108322fbScarlsonj 			continue;
247108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
248108322fbScarlsonj 			tmp_array = realloc(mnts,
249108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
250108322fbScarlsonj 			if (tmp_array == NULL) {
251108322fbScarlsonj 				free_mnttable(mnts, nmnt);
252108322fbScarlsonj 				return (-1);
253108322fbScarlsonj 			}
254108322fbScarlsonj 			mnts = tmp_array;
255108322fbScarlsonj 		}
256108322fbScarlsonj 		mnp = &mnts[nmnt++];
257108322fbScarlsonj 
258108322fbScarlsonj 		/*
259108322fbScarlsonj 		 * Zero out any fields we're not using.
260108322fbScarlsonj 		 */
261108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
262108322fbScarlsonj 
263108322fbScarlsonj 		if (mnt.mnt_special != NULL)
264108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
265108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
266108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
267108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
268108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
269108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
270108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
271108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
272108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
273108322fbScarlsonj 			free_mnttable(mnts, nmnt);
274108322fbScarlsonj 			return (-1);
275108322fbScarlsonj 		}
276108322fbScarlsonj 	}
277108322fbScarlsonj 	*mnt_arrayp = mnts;
278108322fbScarlsonj 	*nelemp = nmnt;
279108322fbScarlsonj 	return (0);
280108322fbScarlsonj }
281108322fbScarlsonj 
282108322fbScarlsonj /*
283108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
284108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
285108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
286108322fbScarlsonj  * reread of the list of mount points
287108322fbScarlsonj  */
288108322fbScarlsonj static void
289108322fbScarlsonj lofs_discard_mnttab(void)
290108322fbScarlsonj {
291108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
292108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
293108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
294108322fbScarlsonj }
295108322fbScarlsonj 
296108322fbScarlsonj static int
297108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
298108322fbScarlsonj {
299108322fbScarlsonj 	FILE *mnttab;
300108322fbScarlsonj 	uint_t nmnts;
301108322fbScarlsonj 
302108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
303108322fbScarlsonj 		return (-1);
304108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
305108322fbScarlsonj 	    &nmnts) == -1) {
306108322fbScarlsonj 		(void) fclose(mnttab);
307108322fbScarlsonj 		return (-1);
308108322fbScarlsonj 	}
309108322fbScarlsonj 	(void) fclose(mnttab);
310108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
311108322fbScarlsonj 	return (0);
312108322fbScarlsonj }
313108322fbScarlsonj 
314108322fbScarlsonj /*
315108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
316108322fbScarlsonj  * path and resolves them all down to an absolute path.
317108322fbScarlsonj  */
318910f48daSedp void
319108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
320108322fbScarlsonj {
321108322fbScarlsonj 	int len, arlen;
322108322fbScarlsonj 	const char *altroot;
323108322fbScarlsonj 	char tmppath[MAXPATHLEN];
324108322fbScarlsonj 	boolean_t outside_altroot;
325108322fbScarlsonj 
326108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
327108322fbScarlsonj 		return;
328108322fbScarlsonj 	tmppath[len] = '\0';
329108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
330108322fbScarlsonj 
331108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
332108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
333108322fbScarlsonj 		return;
334108322fbScarlsonj 
335108322fbScarlsonj 	altroot = zonecfg_get_root();
336108322fbScarlsonj 	arlen = strlen(altroot);
337108322fbScarlsonj 	outside_altroot = B_FALSE;
338108322fbScarlsonj 	for (;;) {
339108322fbScarlsonj 		struct mnttab *mnp;
340108322fbScarlsonj 
3411e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3421e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3431e9d8f9fSdminer 		    mnp--) {
344108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
345108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3461e9d8f9fSdminer 			    mnp->mnt_special == NULL)
347108322fbScarlsonj 				continue;
348108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
349108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
350108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
351108322fbScarlsonj 				break;
352108322fbScarlsonj 		}
3531e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3541e9d8f9fSdminer 			break;
3551e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3561e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
357108322fbScarlsonj 			break;
358108322fbScarlsonj 		if (outside_altroot) {
359108322fbScarlsonj 			char *cp;
360108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
361108322fbScarlsonj 
362108322fbScarlsonj 			/*
363108322fbScarlsonj 			 * If we run into a read-only mount outside of the
364108322fbScarlsonj 			 * alternate root environment, then the user doesn't
365108322fbScarlsonj 			 * want this path to be made read-write.
366108322fbScarlsonj 			 */
367108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
368108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
369108322fbScarlsonj 			    NULL &&
370108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
371108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
372108322fbScarlsonj 				break;
373108322fbScarlsonj 			}
374108322fbScarlsonj 		} else if (arlen > 0 &&
375108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
376108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
377108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
378108322fbScarlsonj 			outside_altroot = B_TRUE;
379108322fbScarlsonj 		}
380108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
381108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
382108322fbScarlsonj 		    mnp->mnt_special, path + len);
383108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
384108322fbScarlsonj 			break;
385108322fbScarlsonj 		path[len] = '\0';
386108322fbScarlsonj 	}
387108322fbScarlsonj }
388108322fbScarlsonj 
389108322fbScarlsonj /*
390108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
391108322fbScarlsonj  * referenced device is already mounted somewhere.
392108322fbScarlsonj  */
393108322fbScarlsonj static int
394108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
395108322fbScarlsonj {
396108322fbScarlsonj 	struct mnttab *mnp;
397108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
398108322fbScarlsonj 
399108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
400108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
401108322fbScarlsonj 		return (-1);
402108322fbScarlsonj 
403108322fbScarlsonj 	/*
404108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
405108322fbScarlsonj 	 * no need to worry about conflicting mounts.
406108322fbScarlsonj 	 */
407108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
408108322fbScarlsonj 	    mnp++) {
409108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
410108322fbScarlsonj 			break;
411108322fbScarlsonj 	}
412108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
413108322fbScarlsonj 		return (0);
414108322fbScarlsonj 
415108322fbScarlsonj 	/*
416108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
417108322fbScarlsonj 	 */
418108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
419108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
420108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
421108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
422108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
423108322fbScarlsonj 
424108322fbScarlsonj 	/*
4256e1ae2a3SGary Pennington 	 * Discard all but one of the original options and set that to our
4266e1ae2a3SGary Pennington 	 * default set of options used for resources.
427108322fbScarlsonj 	 */
428108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
429108322fbScarlsonj 	if (optptr == NULL) {
430108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
431108322fbScarlsonj 		if (optptr == NULL) {
432108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
433108322fbScarlsonj 			    fsptr->zone_fs_dir);
434108322fbScarlsonj 			return (-1);
435108322fbScarlsonj 		}
436108322fbScarlsonj 	} else {
437108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
438108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
439108322fbScarlsonj 			free(onext);
440108322fbScarlsonj 		}
441108322fbScarlsonj 	}
4426e1ae2a3SGary Pennington 	(void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
443108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
444108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
445108322fbScarlsonj 	return (0);
446108322fbScarlsonj }
447108322fbScarlsonj 
448d314f035Sedp int
44927e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
45027e6fb21Sdp     uid_t userid, gid_t groupid)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4537c478bd9Sstevel@tonic-gate 	struct stat st;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4567c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4577c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4587c478bd9Sstevel@tonic-gate 		    subdir);
4597c478bd9Sstevel@tonic-gate 		return (-1);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4637c478bd9Sstevel@tonic-gate 		/*
4647c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4657c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4667c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4677c478bd9Sstevel@tonic-gate 		 */
4687c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
469756cf395SRic Aleshire 			if (S_ISREG(st.st_mode)) {
47045916cd2Sjpk 				/*
471756cf395SRic Aleshire 				 * Allow readonly mounts of /etc/ files; this
472756cf395SRic Aleshire 				 * is needed most by Trusted Extensions.
47345916cd2Sjpk 				 */
47445916cd2Sjpk 				if (strncmp(subdir, "/etc/",
47545916cd2Sjpk 				    strlen("/etc/")) != 0) {
47645916cd2Sjpk 					zerror(zlogp, B_FALSE,
47745916cd2Sjpk 					    "%s is not in /etc", path);
4787c478bd9Sstevel@tonic-gate 					return (-1);
4797c478bd9Sstevel@tonic-gate 				}
48045916cd2Sjpk 			} else {
48145916cd2Sjpk 				zerror(zlogp, B_FALSE,
48245916cd2Sjpk 				    "%s is not a directory", path);
48345916cd2Sjpk 				return (-1);
48445916cd2Sjpk 			}
48545916cd2Sjpk 		}
48627e6fb21Sdp 		return (0);
48727e6fb21Sdp 	}
48827e6fb21Sdp 
48927e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
4907c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4917c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4927c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4937c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4947c478bd9Sstevel@tonic-gate 		else
4957c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4967c478bd9Sstevel@tonic-gate 		return (-1);
4977c478bd9Sstevel@tonic-gate 	}
49827e6fb21Sdp 
49927e6fb21Sdp 	(void) chown(path, userid, groupid);
5007c478bd9Sstevel@tonic-gate 	return (0);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate static void
5047c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	uint_t i;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	if (types == NULL)
5097c478bd9Sstevel@tonic-gate 		return;
5107c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
5117c478bd9Sstevel@tonic-gate 		free(types[i]);
5127c478bd9Sstevel@tonic-gate 	free(types);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static char **
5167c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	char **types = NULL;
5197c478bd9Sstevel@tonic-gate 	FILE *fp;
5207c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
5217c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
5227c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
5237c478bd9Sstevel@tonic-gate 	uint_t i;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5267c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5277c478bd9Sstevel@tonic-gate 		return (NULL);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5317c478bd9Sstevel@tonic-gate 	 */
5327c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5337c478bd9Sstevel@tonic-gate 		lines++;
5347c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5357c478bd9Sstevel@tonic-gate 		goto out;
5367c478bd9Sstevel@tonic-gate 	rewind(fp);
5377c478bd9Sstevel@tonic-gate 	/*
5387c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5397c478bd9Sstevel@tonic-gate 	 */
5407c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5417c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5437c478bd9Sstevel@tonic-gate 		goto out;
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 	i = 0;
5467c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5477c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5487c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5497c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5507c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5517c478bd9Sstevel@tonic-gate 			types = NULL;
5527c478bd9Sstevel@tonic-gate 			goto out;
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5557c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5567c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5577c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5587c478bd9Sstevel@tonic-gate 			types = NULL;
5597c478bd9Sstevel@tonic-gate 			goto out;
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		i++;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate out:
5647c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5657c478bd9Sstevel@tonic-gate 	return (types);
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate static boolean_t
5697c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	uint_t i;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5747c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5757c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5767c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5777c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate 
582108322fbScarlsonj /*
583108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
584108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
585108322fbScarlsonj  */
5867c478bd9Sstevel@tonic-gate static void
587108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5887c478bd9Sstevel@tonic-gate {
589108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
590108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
591108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5967c478bd9Sstevel@tonic-gate  *
5977c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5987c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5997c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
6007c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
6017c478bd9Sstevel@tonic-gate  *
6027c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
6037c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
6047c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
6057c478bd9Sstevel@tonic-gate  *
6067c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
6077c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
6087c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
6097c478bd9Sstevel@tonic-gate  *
6107c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
6117c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
6127c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
6137c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
6147c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
6157c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
6167c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
6177c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
6187c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
6197c478bd9Sstevel@tonic-gate  * graceful mounts first.
6207c478bd9Sstevel@tonic-gate  *
6217c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate static int
624108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	int error = 0;
6277c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6287c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6297c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6307c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6317c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6327c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6337c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6347c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6387c478bd9Sstevel@tonic-gate 		return (-1);
6397c478bd9Sstevel@tonic-gate 	}
640108322fbScarlsonj 	if (unmount_cmd)
641108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6447c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6457c478bd9Sstevel@tonic-gate 
64645916cd2Sjpk 	/*
64745916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
64845916cd2Sjpk 	 * of our zone's /export/home
64945916cd2Sjpk 	 */
65048451833Scarlsonj 	if (!unmount_cmd)
65145916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
65245916cd2Sjpk 
6537c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6557c478bd9Sstevel@tonic-gate 		return (-1);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6597c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6627c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6637c478bd9Sstevel@tonic-gate 		error++;
6647c478bd9Sstevel@tonic-gate 		goto out;
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6697c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6707c478bd9Sstevel@tonic-gate 	 */
6717c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6727c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6737c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6747c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6757c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6767c478bd9Sstevel@tonic-gate 		char *path;
6777c478bd9Sstevel@tonic-gate 		uint_t i;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		mnts = NULL;
6807c478bd9Sstevel@tonic-gate 		nmnt = 0;
6817c478bd9Sstevel@tonic-gate 		/*
6827c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6837c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6847c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6857c478bd9Sstevel@tonic-gate 		 * filesystems.
6867c478bd9Sstevel@tonic-gate 		 */
6877c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6887c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6897c478bd9Sstevel@tonic-gate 			error++;
6907c478bd9Sstevel@tonic-gate 			goto out;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6937c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6947c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6957c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6967c478bd9Sstevel@tonic-gate 			/*
6977c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6987c478bd9Sstevel@tonic-gate 			 *
6997c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
7007c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
7017c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
7027c478bd9Sstevel@tonic-gate 			 */
7037c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
7047c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
7057c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7067c478bd9Sstevel@tonic-gate 			}
7077c478bd9Sstevel@tonic-gate 			/*
7087c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
7097c478bd9Sstevel@tonic-gate 			 */
7107c478bd9Sstevel@tonic-gate 			if (stuck) {
7117c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
7127c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7137c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
7147c478bd9Sstevel@tonic-gate 				} else {
7157c478bd9Sstevel@tonic-gate 					/*
7167c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
7177c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
7187c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
7197c478bd9Sstevel@tonic-gate 					 * bail.
7207c478bd9Sstevel@tonic-gate 					 */
7217c478bd9Sstevel@tonic-gate 					error++;
7227c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
7237c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
7247c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
7257c478bd9Sstevel@tonic-gate 					goto out;
7267c478bd9Sstevel@tonic-gate 				}
7277c478bd9Sstevel@tonic-gate 			}
7287c478bd9Sstevel@tonic-gate 			/*
7297c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7307c478bd9Sstevel@tonic-gate 			 */
7317c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7327c478bd9Sstevel@tonic-gate 				newcount++;
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7377c478bd9Sstevel@tonic-gate 			break;
7387c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7397c478bd9Sstevel@tonic-gate 			/*
7407c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7417c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7427c478bd9Sstevel@tonic-gate 			 */
7437c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		/*
7487c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7497c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7507c478bd9Sstevel@tonic-gate 		 */
7517c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7527c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7537c478bd9Sstevel@tonic-gate 			error++;
7547c478bd9Sstevel@tonic-gate 			goto out;
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate out:
7597c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7607c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7617c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate static int
7657c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7667c478bd9Sstevel@tonic-gate {
7677c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7687c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7757c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7767c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7777c478bd9Sstevel@tonic-gate  *
7787c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7797c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7807c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7817c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7827c478bd9Sstevel@tonic-gate  * remains the case.
7837c478bd9Sstevel@tonic-gate  */
7847c478bd9Sstevel@tonic-gate static int
7857c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7887c478bd9Sstevel@tonic-gate 	int child_status = 0;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	/*
7917c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7927c478bd9Sstevel@tonic-gate 	 */
7937c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7947c478bd9Sstevel@tonic-gate 	child_pid = fork();
7957c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7967c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
7977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7987c478bd9Sstevel@tonic-gate 		return (-1);
7997c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
8007c478bd9Sstevel@tonic-gate 		closefrom(0);
8011390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
8021390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
8031390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
8041390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
8057c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
8067c478bd9Sstevel@tonic-gate 		/*
8077c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
8087c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
8097c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
8107c478bd9Sstevel@tonic-gate 		 * accordingly.
8117c478bd9Sstevel@tonic-gate 		 */
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
8147c478bd9Sstevel@tonic-gate 	} else {
8157c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
8197c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8207c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
8217c478bd9Sstevel@tonic-gate 		return (-1);
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
8247c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
8257c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8267c478bd9Sstevel@tonic-gate 		return (-1);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate static int
83293239addSjohnlev isregfile(const char *path)
83393239addSjohnlev {
83493239addSjohnlev 	struct stat64 st;
83593239addSjohnlev 
83693239addSjohnlev 	if (stat64(path, &st) == -1)
83793239addSjohnlev 		return (-1);
83893239addSjohnlev 
83993239addSjohnlev 	return (S_ISREG(st.st_mode));
84093239addSjohnlev }
84193239addSjohnlev 
84293239addSjohnlev static int
8437c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
84657a250fbSbatschul 	char *argv[5];
8477c478bd9Sstevel@tonic-gate 	int status;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	/*
8507c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8517c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8527c478bd9Sstevel@tonic-gate 	 */
8537c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8549acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8557c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8567c478bd9Sstevel@tonic-gate 		return (-1);
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 
85993239addSjohnlev 	/*
86093239addSjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
86193239addSjohnlev 	 * in zoneadm.
86293239addSjohnlev 	 */
86393239addSjohnlev 	if (isregfile(cmdbuf) == -1)
86493239addSjohnlev 		return (0);
86593239addSjohnlev 
8667c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
86757a250fbSbatschul 	argv[1] = "-o";
86857a250fbSbatschul 	argv[2] = "p";
86957a250fbSbatschul 	argv[3] = (char *)rawdev;
87057a250fbSbatschul 	argv[4] = NULL;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8737c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8747c478bd9Sstevel@tonic-gate 		return (status);
8757c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8767c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8777c478bd9Sstevel@tonic-gate 	return (-1);
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate static int
8817c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8827c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8857c478bd9Sstevel@tonic-gate 	char *argv[6];
8867c478bd9Sstevel@tonic-gate 	int status;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/*
8897c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8907c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8917c478bd9Sstevel@tonic-gate 	 */
8927c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8939acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8957c478bd9Sstevel@tonic-gate 		return (-1);
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
8987c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
8997c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
9007c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
9017c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
9027c478bd9Sstevel@tonic-gate 	} else {
9037c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
9047c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
9057c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
9067c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
9077c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
9117c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
9127c478bd9Sstevel@tonic-gate 		return (status);
9137c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
9147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
9157c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9167c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
9177c478bd9Sstevel@tonic-gate 	else
9187c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
9197c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9207c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
9217c478bd9Sstevel@tonic-gate 	return (-1);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /*
925d314f035Sedp  * Check if a given mount point path exists.
926d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
927d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
928d314f035Sedp  * component of the mount point path, so it must be a directory.
929d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
930d314f035Sedp  * path, so the mount point itself can be anything aside from a
931d314f035Sedp  * symbolic link.
932d314f035Sedp  *
933d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
934d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
935d314f035Sedp  * If the path doesn't exist return a positive value.
9367c478bd9Sstevel@tonic-gate  */
9377c478bd9Sstevel@tonic-gate static int
938d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9397c478bd9Sstevel@tonic-gate {
9407c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9417c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9427c478bd9Sstevel@tonic-gate 	int res;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9457c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
946d314f035Sedp 			return (1);
9477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9487c478bd9Sstevel@tonic-gate 		return (-1);
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9517c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9527c478bd9Sstevel@tonic-gate 		return (-1);
9537c478bd9Sstevel@tonic-gate 	}
954d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9557c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9567c478bd9Sstevel@tonic-gate 		return (-1);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9597c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9607c478bd9Sstevel@tonic-gate 		return (-1);
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9637c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9647c478bd9Sstevel@tonic-gate 		/*
965d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9667c478bd9Sstevel@tonic-gate 		 */
9677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9687c478bd9Sstevel@tonic-gate 		return (-1);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 	return (0);
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate /*
974d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
975d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
976d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
977d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
978d314f035Sedp  * object.  (Different filesystems can support different mount points,
979d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
980d314f035Sedp  * while "ufs" just supports directories.)
9817c478bd9Sstevel@tonic-gate  *
982d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
983d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
984d314f035Sedp  * If the path doesn't exist return a positive value.
9857c478bd9Sstevel@tonic-gate  */
986d314f035Sedp int
987d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
988d314f035Sedp     const char *dir, const char *fstype)
9897c478bd9Sstevel@tonic-gate {
990d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
991d314f035Sedp 	int rv;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/*
994d314f035Sedp 	 * Sanity check the target mount point path.
995d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
9967c478bd9Sstevel@tonic-gate 	 */
997d314f035Sedp 	if (dir[0] != '/') {
998d314f035Sedp 		/* Something went wrong. */
999d314f035Sedp 		zerror(zlogp, B_FALSE, "invalid mount directory, "
1000d314f035Sedp 		    "type: \"%s\", special: \"%s\", dir: \"%s\"",
1001d314f035Sedp 		    fstype, spec, dir);
1002d314f035Sedp 		return (-1);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
1005d314f035Sedp 	/*
1006d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
1007d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
1008d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
1009d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
1010d314f035Sedp 	 * cannonical path check in valid_mount_point()).
1011d314f035Sedp 	 */
1012d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
1013d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
1014d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
1015d314f035Sedp 		    rootpath, dir);
1016d314f035Sedp 		return (-1);
1017d314f035Sedp 	}
1018d314f035Sedp 
1019d314f035Sedp 	/*
1020d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
1021d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
1022d314f035Sedp 	 */
10237c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10247c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10257c478bd9Sstevel@tonic-gate 	do {
1026d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
10277c478bd9Sstevel@tonic-gate 		*slashp = '\0';
1028d314f035Sedp 		if (slashp_next != NULL) {
1029d314f035Sedp 			/* This is an intermediary mount path component. */
1030d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1031d314f035Sedp 		} else {
1032d314f035Sedp 			/* This is the last component of the mount path. */
1033d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1034d314f035Sedp 		}
1035d314f035Sedp 		if (rv < 0)
1036d314f035Sedp 			return (rv);
10377c478bd9Sstevel@tonic-gate 		*slashp = '/';
1038d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1039d314f035Sedp 	return (rv);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate static int
10439acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10449acbbeafSnn35248 {
10459acbbeafSnn35248 	di_prof_t prof = arg;
10469acbbeafSnn35248 
10479acbbeafSnn35248 	if (name == NULL)
10489acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
10499acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
10509acbbeafSnn35248 }
10519acbbeafSnn35248 
10529acbbeafSnn35248 static int
10539acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10549acbbeafSnn35248 {
10559acbbeafSnn35248 	di_prof_t prof = arg;
10569acbbeafSnn35248 
10579acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
10589acbbeafSnn35248 }
10599acbbeafSnn35248 
10602b24ab6bSSebastien Roy int
10612b24ab6bSSebastien Roy vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1062f4b3ec61Sdh155122 {
1063f4b3ec61Sdh155122 	zone_dochandle_t handle;
1064f4b3ec61Sdh155122 
1065f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
1066f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1067f4b3ec61Sdh155122 		return (-1);
1068f4b3ec61Sdh155122 	}
1069f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1070f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
1071f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1072f4b3ec61Sdh155122 		return (-1);
1073f4b3ec61Sdh155122 	}
1074f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1075f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1076f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1077f4b3ec61Sdh155122 		return (-1);
1078f4b3ec61Sdh155122 	}
1079f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1080f4b3ec61Sdh155122 	return (0);
1081f4b3ec61Sdh155122 }
1082f4b3ec61Sdh155122 
10839acbbeafSnn35248 /*
10849acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10859acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10869acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10879acbbeafSnn35248  */
10889acbbeafSnn35248 static int
10890679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10909acbbeafSnn35248 {
10919acbbeafSnn35248 	char			brand[MAXNAMELEN];
10929acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1093123807fbSedp 	brand_handle_t		bh = NULL;
10949acbbeafSnn35248 	struct zone_devtab	ztab;
10959acbbeafSnn35248 	di_prof_t		prof = NULL;
10969acbbeafSnn35248 	int			err;
10979acbbeafSnn35248 	int			retval = -1;
1098f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1099f4b3ec61Sdh155122 	const char 		*curr_iptype;
11009acbbeafSnn35248 
11019acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
11029acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
11039acbbeafSnn35248 		goto cleanup;
11049acbbeafSnn35248 	}
11059acbbeafSnn35248 
11060679f794S 	/*
11070679f794S 	 * Get a handle to the brand info for this zone.
1108e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
11090679f794S 	 * brand device mounts.
11100679f794S 	 */
11110679f794S 	if (ALT_MOUNT(mount_cmd)) {
1112e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
11130679f794S 	} else {
111462868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
11150679f794S 	}
11160679f794S 
11170679f794S 	if ((bh = brand_open(brand)) == NULL) {
11189acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
11199acbbeafSnn35248 		goto cleanup;
11209acbbeafSnn35248 	}
11219acbbeafSnn35248 
11222b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
1123f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1124f4b3ec61Sdh155122 		goto cleanup;
1125f4b3ec61Sdh155122 	}
1126f4b3ec61Sdh155122 	switch (iptype) {
1127f4b3ec61Sdh155122 	case ZS_SHARED:
1128f4b3ec61Sdh155122 		curr_iptype = "shared";
1129f4b3ec61Sdh155122 		break;
1130f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1131f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1132f4b3ec61Sdh155122 		break;
1133f4b3ec61Sdh155122 	}
1134f4b3ec61Sdh155122 
1135123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1136f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11379acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11389acbbeafSnn35248 		goto cleanup;
11399acbbeafSnn35248 	}
11409acbbeafSnn35248 
1141123807fbSedp 	if (brand_platform_iter_link(bh,
11429acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11439acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11449acbbeafSnn35248 		goto cleanup;
11459acbbeafSnn35248 	}
11469acbbeafSnn35248 
11479acbbeafSnn35248 	/* Add user-specified devices and directories */
11489acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11499acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11509acbbeafSnn35248 		goto cleanup;
11519acbbeafSnn35248 	}
11529acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11539acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11549acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11559acbbeafSnn35248 		goto cleanup;
11569acbbeafSnn35248 	}
11579acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
11589acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11599acbbeafSnn35248 		    zonecfg_strerror(err));
11609acbbeafSnn35248 		goto cleanup;
11619acbbeafSnn35248 	}
11629acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11639acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11649acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
11659acbbeafSnn35248 			    "user-specified device");
11669acbbeafSnn35248 			goto cleanup;
11679acbbeafSnn35248 		}
11689acbbeafSnn35248 	}
11699acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
11709acbbeafSnn35248 
11719acbbeafSnn35248 	/* Send profile to kernel */
11729acbbeafSnn35248 	if (di_prof_commit(prof)) {
11739acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11749acbbeafSnn35248 		goto cleanup;
11759acbbeafSnn35248 	}
11769acbbeafSnn35248 
11779acbbeafSnn35248 	retval = 0;
11789acbbeafSnn35248 
11799acbbeafSnn35248 cleanup:
1180123807fbSedp 	if (bh != NULL)
1181123807fbSedp 		brand_close(bh);
1182e767a340Sgjelinek 	if (handle != NULL)
11839acbbeafSnn35248 		zonecfg_fini_handle(handle);
11849acbbeafSnn35248 	if (prof)
11859acbbeafSnn35248 		di_prof_fini(prof);
11869acbbeafSnn35248 	return (retval);
11879acbbeafSnn35248 }
11889acbbeafSnn35248 
11899acbbeafSnn35248 static int
11900679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11910679f794S     zone_mnt_t mount_cmd)
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
11947c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11957c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11969acbbeafSnn35248 	int rv;
11977c478bd9Sstevel@tonic-gate 
1198d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1199d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
12007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
12017c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
12027c478bd9Sstevel@tonic-gate 		return (-1);
1203d314f035Sedp 	} else if (rv > 0) {
1204d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1205d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1206d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1207d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1208d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1209d314f035Sedp 			return (-1);
12107c478bd9Sstevel@tonic-gate 		}
12117c478bd9Sstevel@tonic-gate 
1212d314f035Sedp 		/*
1213d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1214d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1215d314f035Sedp 		 * to make sure that the mount point path is canonical,
1216d314f035Sedp 		 * which it can only do if the path exists, so now that
1217d314f035Sedp 		 * we've created the path we have to verify it again.
1218d314f035Sedp 		 */
1219d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1220d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1221d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1222d314f035Sedp 			zerror(zlogp, B_FALSE,
1223d314f035Sedp 			    "%s%s is not a valid mount point",
1224d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
12257c478bd9Sstevel@tonic-gate 			return (-1);
1226d314f035Sedp 		}
1227d314f035Sedp 	}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12307c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	/*
12337c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12347c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12357c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12367c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12377c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12387c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12397c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12407c478bd9Sstevel@tonic-gate 	 */
12417c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12437c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12447c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12457c478bd9Sstevel@tonic-gate 		return (-1);
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	/*
1249108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1250fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1251fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1252fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1253fa3d7ae1Sedp 	 * time we get here.
1254108322fbScarlsonj 	 */
1255108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1256108322fbScarlsonj 		struct stat64 st;
1257108322fbScarlsonj 
1258108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
125968eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1260fa3d7ae1Sedp 			/*
1261fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1262fa3d7ae1Sedp 			 * to check if that device is already mounted
1263fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1264fa3d7ae1Sedp 			 * of the device instead of a direct mount
1265fa3d7ae1Sedp 			 */
126668eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1267108322fbScarlsonj 				return (-1);
126868eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1269fa3d7ae1Sedp 			/*
1270fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1271fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1272fa3d7ae1Sedp 			 * this case in order to get at the underlying
1273fa3d7ae1Sedp 			 * read-write path.
1274fa3d7ae1Sedp 			 */
1275fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1276108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1277108322fbScarlsonj 		}
1278108322fbScarlsonj 	}
1279108322fbScarlsonj 
1280108322fbScarlsonj 	/*
12817c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12827c478bd9Sstevel@tonic-gate 	 */
12837c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
128493239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12857c478bd9Sstevel@tonic-gate 		return (-1);
128693239addSjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
128793239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
128893239addSjohnlev 		return (-1);
128993239addSjohnlev 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	/*
12927c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
12937c478bd9Sstevel@tonic-gate 	 */
12947c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
12957c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
12967c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12977c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
12987c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
12997c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
13007c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
13017c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
13027c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
13037c478bd9Sstevel@tonic-gate 		}
13047c478bd9Sstevel@tonic-gate 	}
13059acbbeafSnn35248 
13069acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13079acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
13089acbbeafSnn35248 		return (rv);
13099acbbeafSnn35248 
13109acbbeafSnn35248 	/*
13119acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13129acbbeafSnn35248 	 * we're done.
13139acbbeafSnn35248 	 */
13149acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13159acbbeafSnn35248 		return (0);
13169acbbeafSnn35248 
13179acbbeafSnn35248 	/*
13189acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13199acbbeafSnn35248 	 * need to configure it.
13209acbbeafSnn35248 	 */
13210679f794S 	return (mount_one_dev(zlogp, path, mount_cmd));
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate static void
13257c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	uint_t i;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13307c478bd9Sstevel@tonic-gate 		return;
13317c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13327c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13337c478bd9Sstevel@tonic-gate 	free(fsarray);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate 
1336108322fbScarlsonj /*
1337f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1338f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1339f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1340f4368d3dSvp157776  * is done this way because:
1341f4368d3dSvp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
1342f4368d3dSvp157776  * 	We loopback mount zone's own /etc and /var into the root of the
1343f4368d3dSvp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1344f4368d3dSvp157776  * 	need to delay the mount of /var till the zone's root gets populated.
1345f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1346f4368d3dSvp157776  * 	build_mounted_post_var() which gets called only after the zone
1347f4368d3dSvp157776  * 	specific filesystems are mounted.
13486cfd72c6Sgjelinek  *
13496cfd72c6Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13506cfd72c6Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13516cfd72c6Sgjelinek  * scratch zone.
1352108322fbScarlsonj  */
1353108322fbScarlsonj static boolean_t
1354f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
135516bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1356108322fbScarlsonj {
1357108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1358108322fbScarlsonj 	const char **cpp;
1359108322fbScarlsonj 	static const char *mkdirs[] = {
13603f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13613f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1362108322fbScarlsonj 	};
1363108322fbScarlsonj 	char *altstr;
1364f4368d3dSvp157776 	FILE *fp;
1365108322fbScarlsonj 	uuid_t uuid;
1366108322fbScarlsonj 
1367108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
136816bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
136916bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1370108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1371108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1372108322fbScarlsonj 
1373108322fbScarlsonj 	/*
1374108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1375108322fbScarlsonj 	 * zone_mount_early.)
1376108322fbScarlsonj 	 */
1377108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1378108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1379108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1380108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1381108322fbScarlsonj 			return (B_FALSE);
1382108322fbScarlsonj 		}
1383108322fbScarlsonj 	}
1384f4368d3dSvp157776 	/*
1385f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1386f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1387f4368d3dSvp157776 	 * read-only inside the scratch zone.
1388f4368d3dSvp157776 	 */
1389f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1390f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1391f4368d3dSvp157776 	if (altstr == NULL) {
1392f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1393f4368d3dSvp157776 		return (B_FALSE);
1394f4368d3dSvp157776 	}
1395f4368d3dSvp157776 	zonecfg_set_root("");
1396f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1397f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1398f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1399f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1400f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1401f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1402f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1403f4368d3dSvp157776 			return (B_FALSE);
1404f4368d3dSvp157776 		}
14056e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1406f4368d3dSvp157776 		    tmp) != 0) {
1407f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1408f4368d3dSvp157776 			    fromdir);
1409f4368d3dSvp157776 			return (B_FALSE);
1410f4368d3dSvp157776 		}
1411f4368d3dSvp157776 	}
1412f4368d3dSvp157776 	zonecfg_set_root(altstr);
1413f4368d3dSvp157776 	free(altstr);
1414f4368d3dSvp157776 
1415f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1416f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1417f4368d3dSvp157776 		return (B_FALSE);
1418f4368d3dSvp157776 	}
1419f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1420f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1421f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1422f4368d3dSvp157776 	}
1423f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1424f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1425f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1426f4368d3dSvp157776 		return (B_FALSE);
1427f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1428f4368d3dSvp157776 	return (B_TRUE);
1429f4368d3dSvp157776 }
1430f4368d3dSvp157776 
1431f4368d3dSvp157776 
1432f4368d3dSvp157776 static boolean_t
14336cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14346cfd72c6Sgjelinek     const char *luroot)
1435f4368d3dSvp157776 {
1436f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1437f4368d3dSvp157776 	const char **cpp;
14386cfd72c6Sgjelinek 	const char **loopdirs;
14396cfd72c6Sgjelinek 	const char **tmpdirs;
1440f4368d3dSvp157776 	static const char *localdirs[] = {
1441f4368d3dSvp157776 		"/etc", "/var", NULL
1442f4368d3dSvp157776 	};
14436cfd72c6Sgjelinek 	static const char *scr_loopdirs[] = {
1444f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1445f4368d3dSvp157776 		"/usr", NULL
1446f4368d3dSvp157776 	};
14476cfd72c6Sgjelinek 	static const char *upd_loopdirs[] = {
14486cfd72c6Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14496cfd72c6Sgjelinek 		"/usr", "/var", NULL
14506cfd72c6Sgjelinek 	};
14516cfd72c6Sgjelinek 	static const char *scr_tmpdirs[] = {
1452f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1453f4368d3dSvp157776 	};
14546cfd72c6Sgjelinek 	static const char *upd_tmpdirs[] = {
14556cfd72c6Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14566cfd72c6Sgjelinek 	};
1457f4368d3dSvp157776 	struct stat st;
1458f4368d3dSvp157776 
14596cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
1460108322fbScarlsonj 		/*
14616cfd72c6Sgjelinek 		 * These are mounted read-write from the zone undergoing
14626cfd72c6Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14636cfd72c6Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
1464108322fbScarlsonj 		 */
1465108322fbScarlsonj 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14666cfd72c6Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1467108322fbScarlsonj 			    *cpp);
14686cfd72c6Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14696cfd72c6Sgjelinek 			    rootpath, *cpp);
1470108322fbScarlsonj 			if (mkdir(tmp, 0755) != 0) {
1471108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1472108322fbScarlsonj 				return (B_FALSE);
1473108322fbScarlsonj 			}
14746cfd72c6Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14756cfd72c6Sgjelinek 			    != 0) {
14766cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14776cfd72c6Sgjelinek 				    tmp, *cpp);
1478108322fbScarlsonj 				return (B_FALSE);
1479108322fbScarlsonj 			}
1480108322fbScarlsonj 		}
14816cfd72c6Sgjelinek 	}
14826cfd72c6Sgjelinek 
14836cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14846cfd72c6Sgjelinek 		loopdirs = upd_loopdirs;
14856cfd72c6Sgjelinek 	else
14866cfd72c6Sgjelinek 		loopdirs = scr_loopdirs;
1487108322fbScarlsonj 
1488108322fbScarlsonj 	/*
1489108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1490108322fbScarlsonj 	 * they contain binaries that must match system.
1491108322fbScarlsonj 	 */
1492108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1493108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1494108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1495108322fbScarlsonj 			if (errno != EEXIST) {
1496108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1497108322fbScarlsonj 				return (B_FALSE);
1498108322fbScarlsonj 			}
1499108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1500108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1501108322fbScarlsonj 				return (B_FALSE);
1502108322fbScarlsonj 			}
1503108322fbScarlsonj 			/*
1504108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1505108322fbScarlsonj 			 * things that have been converted into symlinks
1506108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1507108322fbScarlsonj 			 * fixup.
1508108322fbScarlsonj 			 */
1509108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1510108322fbScarlsonj 				continue;
1511108322fbScarlsonj 		}
15126e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1513108322fbScarlsonj 		    tmp) != 0) {
1514108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1515108322fbScarlsonj 			    *cpp);
1516108322fbScarlsonj 			return (B_FALSE);
1517108322fbScarlsonj 		}
1518108322fbScarlsonj 	}
1519108322fbScarlsonj 
15206cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15216cfd72c6Sgjelinek 		tmpdirs = upd_tmpdirs;
15226cfd72c6Sgjelinek 	else
15236cfd72c6Sgjelinek 		tmpdirs = scr_tmpdirs;
15246cfd72c6Sgjelinek 
1525108322fbScarlsonj 	/*
1526108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1527108322fbScarlsonj 	 */
1528108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1529108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15306cfd72c6Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15316cfd72c6Sgjelinek 		    errno != EEXIST) {
1532108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1533108322fbScarlsonj 			return (B_FALSE);
1534108322fbScarlsonj 		}
1535d30e996aSgjelinek 
1536d30e996aSgjelinek 		/*
1537d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1538d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1539d30e996aSgjelinek 		 * the correct mode for /tmp now.
1540d30e996aSgjelinek 		 */
1541d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1542d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1543d30e996aSgjelinek 			return (B_FALSE);
1544d30e996aSgjelinek 		}
1545d30e996aSgjelinek 
1546108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1547108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1548108322fbScarlsonj 			return (B_FALSE);
1549108322fbScarlsonj 		}
1550108322fbScarlsonj 	}
1551108322fbScarlsonj 	return (B_TRUE);
1552108322fbScarlsonj }
1553108322fbScarlsonj 
15549acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15559acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
15569acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
15579acbbeafSnn35248 	int			*pgcd_num_fs;
15589acbbeafSnn35248 } plat_gmount_cb_data_t;
15599acbbeafSnn35248 
15609acbbeafSnn35248 /*
15619acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15629acbbeafSnn35248  * through all global brand platform mounts.
15639acbbeafSnn35248  */
15649acbbeafSnn35248 int
15659acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15669acbbeafSnn35248     const char *fstype, const char *opt)
15679acbbeafSnn35248 {
15689acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
15699acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15709acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15719acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
15729acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15739acbbeafSnn35248 
15749acbbeafSnn35248 	num_fs++;
15759acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15769acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15779acbbeafSnn35248 		return (-1);
15789acbbeafSnn35248 	}
15799acbbeafSnn35248 
15809acbbeafSnn35248 	fs_ptr = tmp_ptr;
15819acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
15829acbbeafSnn35248 
15839acbbeafSnn35248 	/* update the callback struct passed in */
15849acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
15859acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
15869acbbeafSnn35248 
15879acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
15889acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15899acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
15909acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15919acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15929acbbeafSnn35248 	fsp->zone_fs_options = NULL;
1593fa3d7ae1Sedp 	if ((opt != NULL) &&
1594fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15959acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
15969acbbeafSnn35248 		return (-1);
15979acbbeafSnn35248 	}
15989acbbeafSnn35248 
15999acbbeafSnn35248 	return (0);
16009acbbeafSnn35248 }
16019acbbeafSnn35248 
16029acbbeafSnn35248 static int
16039acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16046cfd72c6Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16059acbbeafSnn35248 {
16069acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16079acbbeafSnn35248 	int num_fs;
16089acbbeafSnn35248 
16099acbbeafSnn35248 	num_fs = *num_fsp;
16109acbbeafSnn35248 	fs_ptr = *fs_tabp;
16119acbbeafSnn35248 
16129acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16139acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16149acbbeafSnn35248 		return (-1);
16159acbbeafSnn35248 	}
16169acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16179acbbeafSnn35248 		/*
16189acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
16199acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
16209acbbeafSnn35248 		 * case.
16219acbbeafSnn35248 		 */
16226cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16236cfd72c6Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16249acbbeafSnn35248 			continue;
16259acbbeafSnn35248 
16269acbbeafSnn35248 		num_fs++;
16279acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16289acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16299acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16309acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
16319acbbeafSnn35248 			return (-1);
16329acbbeafSnn35248 		}
16339acbbeafSnn35248 		/* update the pointers passed in */
16349acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16359acbbeafSnn35248 		*num_fsp = num_fs;
16369acbbeafSnn35248 
16379acbbeafSnn35248 		fs_ptr = tmp_ptr;
16389acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16399acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16409acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16419acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16429acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
16439acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16449acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
16459acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
1646fa3d7ae1Sedp 
1647fa3d7ae1Sedp 		/*
1648fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1649fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1650fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1651fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1652fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1653fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1654fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1655fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1656fa3d7ae1Sedp 		 */
1657fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1658fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1659fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1660fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1661fa3d7ae1Sedp 		}
1662fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1663fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
16649acbbeafSnn35248 	}
16659acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
16669acbbeafSnn35248 	return (0);
16679acbbeafSnn35248 }
16689acbbeafSnn35248 
16697c478bd9Sstevel@tonic-gate static int
16706cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
16717c478bd9Sstevel@tonic-gate {
16727c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
16737c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
16749acbbeafSnn35248 	char brand[MAXNAMELEN];
167516bca938Svp157776 	char luroot[MAXPATHLEN];
16769acbbeafSnn35248 	int i, num_fs = 0;
16779acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
16787c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
16797c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1680123807fbSedp 	brand_handle_t bh;
16819acbbeafSnn35248 	plat_gmount_cb_data_t cb;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1684108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
16857c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1686108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1687108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1688108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
16897c478bd9Sstevel@tonic-gate 		goto bad;
16907c478bd9Sstevel@tonic-gate 	}
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
16937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
16947c478bd9Sstevel@tonic-gate 		goto bad;
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
16987c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
16997c478bd9Sstevel@tonic-gate 		goto bad;
17007c478bd9Sstevel@tonic-gate 	}
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1703ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17047c478bd9Sstevel@tonic-gate 		goto bad;
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17077c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17087c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17097c478bd9Sstevel@tonic-gate 		goto bad;
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate 
17120679f794S 	/*
1713e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
17140679f794S 	 * brand global mounts.
17150679f794S 	 */
17160679f794S 	if (ALT_MOUNT(mount_cmd)) {
1717e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
17180679f794S 	} else {
171962868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
17200679f794S 	}
17210679f794S 
17229acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
17230679f794S 	if ((bh = brand_open(brand)) == NULL) {
17249acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1725e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17269acbbeafSnn35248 		return (-1);
17279acbbeafSnn35248 	}
17289acbbeafSnn35248 
17299acbbeafSnn35248 	/*
17309acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
17319acbbeafSnn35248 	 * configuration.
17329acbbeafSnn35248 	 */
17339acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
17349acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
17359acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1736123807fbSedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17379acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
17389acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1739123807fbSedp 		brand_close(bh);
1740e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17419acbbeafSnn35248 		return (-1);
17429acbbeafSnn35248 	}
1743123807fbSedp 	brand_close(bh);
17449acbbeafSnn35248 
17457c478bd9Sstevel@tonic-gate 	/*
17466e1ae2a3SGary Pennington 	 * Iterate through the rest of the filesystems. Sort them all,
17476e1ae2a3SGary Pennington 	 * then mount them in sorted order. This is to make sure the
17486e1ae2a3SGary Pennington 	 * higher level directories (e.g., /usr) get mounted before
17496e1ae2a3SGary Pennington 	 * any beneath them (e.g., /usr/local).
17507c478bd9Sstevel@tonic-gate 	 */
17519acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17529acbbeafSnn35248 	    mount_cmd) != 0)
17537c478bd9Sstevel@tonic-gate 		goto bad;
1754fa9e4066Sahrens 
17557c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17567c478bd9Sstevel@tonic-gate 	handle = NULL;
17577c478bd9Sstevel@tonic-gate 
1758108322fbScarlsonj 	/*
17599acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
17609acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
17619acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
17629acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
17639acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
17649acbbeafSnn35248 	 * the zones filesystems there instead.
17659acbbeafSnn35248 	 *
17669acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
17679acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
17689acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
17699acbbeafSnn35248 	 *
17709acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
17719acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
17729acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
17739acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
17749acbbeafSnn35248 	 * upgrade environment.)
17759acbbeafSnn35248 	 *
17769acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
17779acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
17789acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
17799acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
17809acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
17819acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
17829acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
17839acbbeafSnn35248 	 *
17849acbbeafSnn35248 	 * All this work is done in three phases:
1785f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1786f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1787f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1788f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1789108322fbScarlsonj 	 */
17906cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
179116bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1792108322fbScarlsonj 		goto bad;
1793108322fbScarlsonj 
17947c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
17959acbbeafSnn35248 
17967c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
17976cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
17989acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
17999acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
18009acbbeafSnn35248 
18019acbbeafSnn35248 			/*
18029acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
18039acbbeafSnn35248 			 * but /dev is special and always goes at the top
18049acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
18059acbbeafSnn35248 			 */
18069acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18079acbbeafSnn35248 			rootpath[slen] = '\0';
18080679f794S 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18090679f794S 			    != 0)
18109acbbeafSnn35248 				goto bad;
18119acbbeafSnn35248 			rootpath[slen] = '/';
18129acbbeafSnn35248 			continue;
18139acbbeafSnn35248 		}
18140679f794S 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18157c478bd9Sstevel@tonic-gate 			goto bad;
18167c478bd9Sstevel@tonic-gate 	}
18176cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18186cfd72c6Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1819f4368d3dSvp157776 		goto bad;
182045916cd2Sjpk 
182145916cd2Sjpk 	/*
182245916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
182345916cd2Sjpk 	 */
18246cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18256cfd72c6Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
182645916cd2Sjpk 		goto bad;
182745916cd2Sjpk 
18287c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	/*
18317c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18327c478bd9Sstevel@tonic-gate 	 */
18337c478bd9Sstevel@tonic-gate 	return (0);
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate bad:
18367c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18377c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18387c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18397c478bd9Sstevel@tonic-gate 	return (-1);
18407c478bd9Sstevel@tonic-gate }
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18437c478bd9Sstevel@tonic-gate static int
18447c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18457c478bd9Sstevel@tonic-gate {
18467c478bd9Sstevel@tonic-gate 	int prefixlen;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18497c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18507c478bd9Sstevel@tonic-gate 		return (1);
18517c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
18527c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
18537c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18547c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
18557c478bd9Sstevel@tonic-gate 			continue;
18567c478bd9Sstevel@tonic-gate 		}
18577c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18587c478bd9Sstevel@tonic-gate 		prefixlen--;
18597c478bd9Sstevel@tonic-gate 	}
18607c478bd9Sstevel@tonic-gate 	return (0);
18617c478bd9Sstevel@tonic-gate }
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate /*
18647c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
18657c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
18667c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
18677c478bd9Sstevel@tonic-gate  *
18687c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
18697c478bd9Sstevel@tonic-gate  */
18707c478bd9Sstevel@tonic-gate static int
1871f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
18747c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
18757c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
18767c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
18777c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
18787c478bd9Sstevel@tonic-gate 	uint_t bufsize;
18797c478bd9Sstevel@tonic-gate 	char *buf = NULL;
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
18827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
18837c478bd9Sstevel@tonic-gate 		ret_code = -1;
18847c478bd9Sstevel@tonic-gate 		goto bad;
18857c478bd9Sstevel@tonic-gate 	}
18867c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
18877c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
18887c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
18897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1890f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
18917c478bd9Sstevel@tonic-gate 		ret_code = -1;
18927c478bd9Sstevel@tonic-gate 		goto bad;
18937c478bd9Sstevel@tonic-gate 	}
18947c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
18957c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
18967c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
18977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
18987c478bd9Sstevel@tonic-gate 		ret_code = -1;
18997c478bd9Sstevel@tonic-gate 		goto bad;
19007c478bd9Sstevel@tonic-gate 	}
19017c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
19027c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19037c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19047c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19057c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1906f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1907f4b3ec61Sdh155122 		    "interfaces");
19087c478bd9Sstevel@tonic-gate 		ret_code = -1;
19097c478bd9Sstevel@tonic-gate 		goto bad;
19107c478bd9Sstevel@tonic-gate 	}
19117c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19127c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19137c478bd9Sstevel@tonic-gate 		(void) close(s);
19147c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19157c478bd9Sstevel@tonic-gate 		    0) {
19167c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19177c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
19187c478bd9Sstevel@tonic-gate 			ret_code = -1;
19197c478bd9Sstevel@tonic-gate 			continue;
19207c478bd9Sstevel@tonic-gate 		}
19217c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19227c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19237c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19247c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1925c1c0ebd5Ssl108498 			if (errno == ENXIO)
1926c1c0ebd5Ssl108498 				/*
1927c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1928c1c0ebd5Ssl108498 				 * another zone halting.
1929c1c0ebd5Ssl108498 				 */
1930c1c0ebd5Ssl108498 				continue;
19317c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1932c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1933f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19347c478bd9Sstevel@tonic-gate 			ret_code = -1;
19357c478bd9Sstevel@tonic-gate 			continue;
19367c478bd9Sstevel@tonic-gate 		}
19377c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19387c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19397c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1940f4b3ec61Sdh155122 				    "%s: could not remove network interface",
19417c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
19427c478bd9Sstevel@tonic-gate 				ret_code = -1;
19437c478bd9Sstevel@tonic-gate 				continue;
19447c478bd9Sstevel@tonic-gate 			}
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate bad:
19487c478bd9Sstevel@tonic-gate 	if (s > 0)
19497c478bd9Sstevel@tonic-gate 		(void) close(s);
19507c478bd9Sstevel@tonic-gate 	if (buf)
19517c478bd9Sstevel@tonic-gate 		free(buf);
19527c478bd9Sstevel@tonic-gate 	return (ret_code);
19537c478bd9Sstevel@tonic-gate }
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate static union	sockunion {
19567c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
19577c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
19587c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19597c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19607c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate static struct {
19637c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
19647c478bd9Sstevel@tonic-gate 	char	space[512];
19657c478bd9Sstevel@tonic-gate } rtmsg;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate static int
19687c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
19697c478bd9Sstevel@tonic-gate {
19707c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
19717c478bd9Sstevel@tonic-gate 	case AF_INET:
19727c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
19737c478bd9Sstevel@tonic-gate 	case AF_LINK:
19747c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
19757c478bd9Sstevel@tonic-gate 	case AF_INET6:
19767c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
19777c478bd9Sstevel@tonic-gate 	default:
19787c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
19797c478bd9Sstevel@tonic-gate 	}
19807c478bd9Sstevel@tonic-gate }
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
19837c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate /*
19867c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
19877c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
19887c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
19897c478bd9Sstevel@tonic-gate  *
19907c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
19917c478bd9Sstevel@tonic-gate  *
19927c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
19937c478bd9Sstevel@tonic-gate  *
19947c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
19957c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
19967c478bd9Sstevel@tonic-gate  */
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate static char *
19997c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20007c478bd9Sstevel@tonic-gate {
20017c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
20027c478bd9Sstevel@tonic-gate 	pid_t pid;
20037c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
20047c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
20057c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20067c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
20077c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	pid = getpid();
20127c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20147c478bd9Sstevel@tonic-gate 		return (NULL);
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20187c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20217c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20227c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20237c478bd9Sstevel@tonic-gate 	} else {
20247c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20277c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20287c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20297c478bd9Sstevel@tonic-gate 	}
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20347c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20357c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20367c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20377c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20387c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20417c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20427c478bd9Sstevel@tonic-gate 	cp += l;
20437c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20447c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20457c478bd9Sstevel@tonic-gate 	cp += l;
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20517c478bd9Sstevel@tonic-gate 		return (NULL);
20527c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20537c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20547c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20557c478bd9Sstevel@tonic-gate 		return (NULL);
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 	do {
20587c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20597c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20607c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20617c478bd9Sstevel@tonic-gate 	if (l < 0) {
20627c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
20637c478bd9Sstevel@tonic-gate 		return (NULL);
20647c478bd9Sstevel@tonic-gate 	}
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
20677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
20687c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
20697c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
20707c478bd9Sstevel@tonic-gate 		return (NULL);
20717c478bd9Sstevel@tonic-gate 	}
20727c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
20737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
20747c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
20757c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
20767c478bd9Sstevel@tonic-gate 		return (NULL);
20777c478bd9Sstevel@tonic-gate 	}
20787c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
20797c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
20807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
20817c478bd9Sstevel@tonic-gate 		return (NULL);
20827c478bd9Sstevel@tonic-gate 	}
20837c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2084f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
20857c478bd9Sstevel@tonic-gate 		return (NULL);
20867c478bd9Sstevel@tonic-gate 	}
20877c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
20887c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
20897c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
20907c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
20917c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
20927c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
20937c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
20947c478bd9Sstevel@tonic-gate 			continue;
20957c478bd9Sstevel@tonic-gate 		}
20967c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
20977c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
20987c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
20997c478bd9Sstevel@tonic-gate 		break;
21007c478bd9Sstevel@tonic-gate 	}
21017c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2102f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2103f4b3ec61Sdh155122 		    "determined");
21047c478bd9Sstevel@tonic-gate 		return (NULL);
21057c478bd9Sstevel@tonic-gate 	}
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	/*
21087c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21097c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21107c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21117c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21127c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21137c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21147c478bd9Sstevel@tonic-gate 	 */
21157c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21167c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21177c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21187c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21197c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21207c478bd9Sstevel@tonic-gate 	if (i < 0) {
21217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2122f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2123f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
21247c478bd9Sstevel@tonic-gate 		return (NULL);
21257c478bd9Sstevel@tonic-gate 	}
21267c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21277c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21287c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21317c478bd9Sstevel@tonic-gate 		return (answer);
21327c478bd9Sstevel@tonic-gate 	return (NULL);
21337c478bd9Sstevel@tonic-gate }
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate /*
21367c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21377c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21387c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21397c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21407c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21417c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21427c478bd9Sstevel@tonic-gate  *
21437c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21447c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
21457c478bd9Sstevel@tonic-gate  */
21467c478bd9Sstevel@tonic-gate static int
21477c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2148f14f3ae7Sjv227347     struct zone_nwiftab *nwiftabptr)
21497c478bd9Sstevel@tonic-gate {
21507c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
21517c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
21527c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
21532e481403SVamsi Nagineni 	struct sockaddr_storage laddr;
21547c478bd9Sstevel@tonic-gate 	struct in_addr in4;
21557c478bd9Sstevel@tonic-gate 	sa_family_t af;
21567c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
21577c478bd9Sstevel@tonic-gate 	int s;
21587c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
2159f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	boolean_t is_loopback = B_FALSE;
21607c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
21617c478bd9Sstevel@tonic-gate 	int res;
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
21647c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
21657c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
21667c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
21677c478bd9Sstevel@tonic-gate 		return (-1);
21687c478bd9Sstevel@tonic-gate 	}
21697c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
21707c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21717c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21727c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
21747c478bd9Sstevel@tonic-gate 		return (-1);
21757c478bd9Sstevel@tonic-gate 	}
21767c478bd9Sstevel@tonic-gate 
21772e481403SVamsi Nagineni 	/*
21782e481403SVamsi Nagineni 	 * This is a similar kind of "hack" like in addif() to get around
21792e481403SVamsi Nagineni 	 * the problem of SIOCLIFADDIF.  The problem is that this ioctl
21802e481403SVamsi Nagineni 	 * does not include the netmask when adding a logical interface.
21812e481403SVamsi Nagineni 	 * To get around this problem, we first add the logical interface
21822e481403SVamsi Nagineni 	 * with a 0 address.  After that, we set the netmask if provided.
21832e481403SVamsi Nagineni 	 * Finally we set the interface address.
21842e481403SVamsi Nagineni 	 */
21852e481403SVamsi Nagineni 	laddr = lifr.lifr_addr;
21867c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
21877c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
21882e481403SVamsi Nagineni 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
21892e481403SVamsi Nagineni 
21907c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
219122321485Svp157776 		/*
219222321485Svp157776 		 * Here, we know that the interface can't be brought up.
219322321485Svp157776 		 * A similar warning message was already printed out to
219422321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
219522321485Svp157776 		 * message to syslog and continue.
219622321485Svp157776 		 */
2197f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
219822321485Svp157776 		    "'%s' which may not be present/plumbed in the "
219922321485Svp157776 		    "global zone.", lifr.lifr_name);
22007c478bd9Sstevel@tonic-gate 		(void) close(s);
220122321485Svp157776 		return (Z_OK);
22027c478bd9Sstevel@tonic-gate 	}
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22057c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22067c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22097c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2210f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2211f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
22127c478bd9Sstevel@tonic-gate 		goto bad;
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 
2215f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	/*
2216f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * Loopback interface will use the default netmask assigned, if no
2217f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * netmask is found.
2218f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 */
22197c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2220f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		is_loopback = B_TRUE;
2221f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	}
22227c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
22237c478bd9Sstevel@tonic-gate 		/*
22247c478bd9Sstevel@tonic-gate 		 * The IPv4 netmask can be determined either
22257c478bd9Sstevel@tonic-gate 		 * directly if a prefix length was supplied with
22267c478bd9Sstevel@tonic-gate 		 * the address or via the netmasks database.  Not
22277c478bd9Sstevel@tonic-gate 		 * being able to determine it is a common failure,
22287c478bd9Sstevel@tonic-gate 		 * but it often is not fatal to operation of the
22297c478bd9Sstevel@tonic-gate 		 * interface.  In that case, a warning will be
22307c478bd9Sstevel@tonic-gate 		 * printed after the rest of the interface's
22317c478bd9Sstevel@tonic-gate 		 * parameters have been configured.
22327c478bd9Sstevel@tonic-gate 		 */
22337c478bd9Sstevel@tonic-gate 		(void) memset(&netmask4, 0, sizeof (netmask4));
22347c478bd9Sstevel@tonic-gate 		if (slashp != NULL) {
22357c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22367c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask4.sin_addr) != 0) {
22377c478bd9Sstevel@tonic-gate 				*slashp = '/';
22387c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22397c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22407c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
22417c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22427c478bd9Sstevel@tonic-gate 				goto bad;
22437c478bd9Sstevel@tonic-gate 			}
22447c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22457c478bd9Sstevel@tonic-gate 		} else if (getnetmaskbyaddr(in4,
22467c478bd9Sstevel@tonic-gate 		    &netmask4.sin_addr) == 0) {
22477c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22487c478bd9Sstevel@tonic-gate 		}
22497c478bd9Sstevel@tonic-gate 		if (got_netmask) {
22507c478bd9Sstevel@tonic-gate 			netmask4.sin_family = af;
22517c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask4,
22527c478bd9Sstevel@tonic-gate 			    sizeof (netmask4));
22537c478bd9Sstevel@tonic-gate 		}
22547c478bd9Sstevel@tonic-gate 	} else {
22557c478bd9Sstevel@tonic-gate 		(void) memset(&netmask6, 0, sizeof (netmask6));
22567c478bd9Sstevel@tonic-gate 		if (addr2netmask(slashp + 1, V6_ADDR_LEN,
22577c478bd9Sstevel@tonic-gate 		    (uchar_t *)&netmask6.sin6_addr) != 0) {
22587c478bd9Sstevel@tonic-gate 			*slashp = '/';
22597c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
22607c478bd9Sstevel@tonic-gate 			    "%s: invalid prefix length in %s",
22617c478bd9Sstevel@tonic-gate 			    lifr.lifr_name,
22627c478bd9Sstevel@tonic-gate 			    nwiftabptr->zone_nwif_address);
22637c478bd9Sstevel@tonic-gate 			goto bad;
22647c478bd9Sstevel@tonic-gate 		}
22657c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;
22667c478bd9Sstevel@tonic-gate 		netmask6.sin6_family = af;
22677c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &netmask6,
22687c478bd9Sstevel@tonic-gate 		    sizeof (netmask6));
22697c478bd9Sstevel@tonic-gate 	}
22707c478bd9Sstevel@tonic-gate 	if (got_netmask &&
22717c478bd9Sstevel@tonic-gate 	    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
22727c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not set netmask",
22737c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22747c478bd9Sstevel@tonic-gate 		goto bad;
22757c478bd9Sstevel@tonic-gate 	}
22767c478bd9Sstevel@tonic-gate 
22772e481403SVamsi Nagineni 	/* Set the interface address */
22782e481403SVamsi Nagineni 	lifr.lifr_addr = laddr;
22797c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
22812e481403SVamsi Nagineni 		    "%s: could not set IP address to %s",
22822e481403SVamsi Nagineni 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22837c478bd9Sstevel@tonic-gate 		goto bad;
22847c478bd9Sstevel@tonic-gate 	}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
22887c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22897c478bd9Sstevel@tonic-gate 		goto bad;
22907c478bd9Sstevel@tonic-gate 	}
22917c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
22927c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
22937c478bd9Sstevel@tonic-gate 		int save_errno = errno;
22947c478bd9Sstevel@tonic-gate 		char *zone_using;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 		/*
22977c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
22987c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
22997c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23007c478bd9Sstevel@tonic-gate 		 * using that address.
23017c478bd9Sstevel@tonic-gate 		 */
23027c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23037c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2304f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2305f4b3ec61Sdh155122 			    lifr.lifr_name);
23067c478bd9Sstevel@tonic-gate 			goto bad;
23077c478bd9Sstevel@tonic-gate 		}
23087c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23097c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23107c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23117c478bd9Sstevel@tonic-gate 			goto bad;
23127c478bd9Sstevel@tonic-gate 		}
23137c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23147c478bd9Sstevel@tonic-gate 		errno = save_errno;
23157c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
23167c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2317f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2318f4b3ec61Sdh155122 			    lifr.lifr_name);
23197c478bd9Sstevel@tonic-gate 		else
2320f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2321f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2322f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
23237c478bd9Sstevel@tonic-gate 		goto bad;
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 
2326f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (!got_netmask && !is_loopback) {
23277c478bd9Sstevel@tonic-gate 		/*
23287c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23297c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23307c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23317c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23327c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23337c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23347c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23357c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23367c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23377c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
23387c478bd9Sstevel@tonic-gate 		 */
23397c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23407c478bd9Sstevel@tonic-gate 		void  *addr;
2341e11c3f44Smeem 		const char *nomatch = "no matching subnet found in netmasks(4)";
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
23447c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23457c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23467c478bd9Sstevel@tonic-gate 		else
23477c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
23487c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
23497c478bd9Sstevel@tonic-gate 
2350e11c3f44Smeem 		/*
2351e11c3f44Smeem 		 * Find out what netmask the interface is going to be using.
2352e11c3f44Smeem 		 * If we just brought up an IPMP data address on an underlying
2353e11c3f44Smeem 		 * interface above, the address will have already migrated, so
2354e11c3f44Smeem 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
2355e11c3f44Smeem 		 * to bring the address up to get the actual netmask).  Just
2356e11c3f44Smeem 		 * omit printing the actual netmask in this corner-case.
2357e11c3f44Smeem 		 */
23587c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2359e11c3f44Smeem 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2360e11c3f44Smeem 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2361e11c3f44Smeem 			    nomatch);
2362e11c3f44Smeem 		} else {
23637c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
2364e11c3f44Smeem 			    "WARNING: %s: %s: %s; using default of %s.",
2365e11c3f44Smeem 			    lifr.lifr_name, nomatch, addrstr4, buffer);
2366e11c3f44Smeem 		}
23677c478bd9Sstevel@tonic-gate 	}
23687c478bd9Sstevel@tonic-gate 
2369de860bd9Sgfaden 	/*
2370de860bd9Sgfaden 	 * If a default router was specified for this interface
2371de860bd9Sgfaden 	 * set the route now. Ignore if already set.
2372de860bd9Sgfaden 	 */
2373de860bd9Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2374de860bd9Sgfaden 		int status;
2375de860bd9Sgfaden 		char *argv[7];
2376de860bd9Sgfaden 
2377de860bd9Sgfaden 		argv[0] = "route";
2378de860bd9Sgfaden 		argv[1] = "add";
2379de860bd9Sgfaden 		argv[2] = "-ifp";
2380de860bd9Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
2381de860bd9Sgfaden 		argv[4] = "default";
2382de860bd9Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2383de860bd9Sgfaden 		argv[6] = NULL;
2384de860bd9Sgfaden 
2385de860bd9Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2386de860bd9Sgfaden 		if (status != 0 && status != EEXIST)
2387de860bd9Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
2388de860bd9Sgfaden 			    "interface %s to %s\n",
2389de860bd9Sgfaden 			    nwiftabptr->zone_nwif_physical,
2390de860bd9Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
2391de860bd9Sgfaden 	}
2392de860bd9Sgfaden 
23937c478bd9Sstevel@tonic-gate 	(void) close(s);
23947c478bd9Sstevel@tonic-gate 	return (Z_OK);
23957c478bd9Sstevel@tonic-gate bad:
23967c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
23977c478bd9Sstevel@tonic-gate 	(void) close(s);
23987c478bd9Sstevel@tonic-gate 	return (-1);
23997c478bd9Sstevel@tonic-gate }
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate /*
24027c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
2403f14f3ae7Sjv227347  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2404f14f3ae7Sjv227347  * system.
24057c478bd9Sstevel@tonic-gate  *
24067c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24077c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
24087c478bd9Sstevel@tonic-gate  */
24097c478bd9Sstevel@tonic-gate static int
2410f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24117c478bd9Sstevel@tonic-gate {
24127c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24137c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24147c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24187c478bd9Sstevel@tonic-gate 		return (-1);
24197c478bd9Sstevel@tonic-gate 	}
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24227c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24237c478bd9Sstevel@tonic-gate 		return (-1);
24247c478bd9Sstevel@tonic-gate 	}
24257c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24267c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24277c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24287c478bd9Sstevel@tonic-gate 		return (-1);
24297c478bd9Sstevel@tonic-gate 	}
24307c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24317c478bd9Sstevel@tonic-gate 		for (;;) {
24327c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24337c478bd9Sstevel@tonic-gate 				break;
2434f14f3ae7Sjv227347 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24357c478bd9Sstevel@tonic-gate 			    Z_OK) {
24367c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24377c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24387c478bd9Sstevel@tonic-gate 				return (-1);
24397c478bd9Sstevel@tonic-gate 			}
24407c478bd9Sstevel@tonic-gate 		}
24417c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
2444fdb7141fSgfaden 	if (is_system_labeled()) {
2445fdb7141fSgfaden 		/*
2446fdb7141fSgfaden 		 * Labeled zones share the loopback interface
2447fdb7141fSgfaden 		 * so it is not plumbed for shared stack instances.
2448fdb7141fSgfaden 		 */
2449fdb7141fSgfaden 		return (0);
2450fdb7141fSgfaden 	}
24517c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
24527c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
24537c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
24547c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2455442d3e9eSgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2456f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24577c478bd9Sstevel@tonic-gate 		return (-1);
2458f14f3ae7Sjv227347 
2459f14f3ae7Sjv227347 	/* Always plumb up the IPv6 loopback interface. */
24607c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
24617c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2462f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24637c478bd9Sstevel@tonic-gate 		return (-1);
24647c478bd9Sstevel@tonic-gate 	return (0);
24657c478bd9Sstevel@tonic-gate }
24667c478bd9Sstevel@tonic-gate 
246789b1c373Smeem static void
246889b1c373Smeem zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
246989b1c373Smeem {
247089b1c373Smeem 	char errmsg[DLADM_STRSIZE];
247189b1c373Smeem 
247289b1c373Smeem 	(void) dladm_status2str(err, errmsg);
247389b1c373Smeem 	zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
247489b1c373Smeem }
247589b1c373Smeem 
2476f4b3ec61Sdh155122 static int
24772b24ab6bSSebastien Roy add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2478f4b3ec61Sdh155122 {
247989b1c373Smeem 	dladm_status_t err;
24800dc2366fSVenugopal Iyer 	boolean_t cpuset, poolset;
2481efd4c9b6SSteve Lawrence 	char *poolp;
248289b1c373Smeem 
2483f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2484f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2485f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
248689b1c373Smeem 		zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
248789b1c373Smeem 		    "'%s' which is used in the global zone", dlname);
2488f4b3ec61Sdh155122 		return (-1);
2489f4b3ec61Sdh155122 	}
2490f4b3ec61Sdh155122 
2491d62bc4baSyz147064 	/* Set zoneid of this link. */
24922b24ab6bSSebastien Roy 	err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
24932b24ab6bSSebastien Roy 	    DLADM_OPT_ACTIVE);
249489b1c373Smeem 	if (err != DLADM_STATUS_OK) {
249589b1c373Smeem 		zdlerror(zlogp, err, dlname,
249689b1c373Smeem 		    "WARNING: unable to add network interface");
2497f4b3ec61Sdh155122 		return (-1);
2498f4b3ec61Sdh155122 	}
24990dc2366fSVenugopal Iyer 
25000dc2366fSVenugopal Iyer 	/*
25010dc2366fSVenugopal Iyer 	 * Set the pool of this link if the zone has a pool and
25020dc2366fSVenugopal Iyer 	 * neither the cpus nor the pool datalink property is
25030dc2366fSVenugopal Iyer 	 * already set.
25040dc2366fSVenugopal Iyer 	 */
25050dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25060dc2366fSVenugopal Iyer 	    "cpus", &cpuset);
25070dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
25080dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
25090dc2366fSVenugopal Iyer 		    "WARNING: unable to check if cpus link property is set");
25100dc2366fSVenugopal Iyer 	}
25110dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25120dc2366fSVenugopal Iyer 	    "pool", &poolset);
25130dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
25140dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
25150dc2366fSVenugopal Iyer 		    "WARNING: unable to check if pool link property is set");
25160dc2366fSVenugopal Iyer 	}
25170dc2366fSVenugopal Iyer 
25180dc2366fSVenugopal Iyer 	if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2519efd4c9b6SSteve Lawrence 		poolp = pool_name;
25200dc2366fSVenugopal Iyer 		err = dladm_set_linkprop(dld_handle, linkid, "pool",
2521efd4c9b6SSteve Lawrence 		    &poolp, 1, DLADM_OPT_ACTIVE);
25220dc2366fSVenugopal Iyer 		if (err != DLADM_STATUS_OK) {
25230dc2366fSVenugopal Iyer 			zerror(zlogp, B_FALSE, "WARNING: unable to set "
25240dc2366fSVenugopal Iyer 			    "pool %s to datalink %s", pool_name, dlname);
2525a3002e0aSGarrett D'Amore 			bzero(pool_name, sizeof (pool_name));
25260dc2366fSVenugopal Iyer 		}
25270dc2366fSVenugopal Iyer 	} else {
2528a3002e0aSGarrett D'Amore 		bzero(pool_name, sizeof (pool_name));
25290dc2366fSVenugopal Iyer 	}
2530f4b3ec61Sdh155122 	return (0);
2531f4b3ec61Sdh155122 }
2532f4b3ec61Sdh155122 
2533550b6e40SSowmini Varadhan static boolean_t
2534550b6e40SSowmini Varadhan sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
2535550b6e40SSowmini Varadhan     char *straddr, size_t len)
2536550b6e40SSowmini Varadhan {
2537550b6e40SSowmini Varadhan 	struct sockaddr_in *sin;
2538550b6e40SSowmini Varadhan 	struct sockaddr_in6 *sin6;
2539550b6e40SSowmini Varadhan 	const char *str = NULL;
2540550b6e40SSowmini Varadhan 
2541550b6e40SSowmini Varadhan 	if (af == AF_INET) {
2542550b6e40SSowmini Varadhan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2543550b6e40SSowmini Varadhan 		sin = SIN(sockaddr);
2544550b6e40SSowmini Varadhan 		str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
2545550b6e40SSowmini Varadhan 	} else if (af == AF_INET6) {
2546550b6e40SSowmini Varadhan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2547550b6e40SSowmini Varadhan 		sin6 = SIN6(sockaddr);
2548550b6e40SSowmini Varadhan 		str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
2549550b6e40SSowmini Varadhan 		    len);
2550550b6e40SSowmini Varadhan 	}
2551550b6e40SSowmini Varadhan 
2552550b6e40SSowmini Varadhan 	return (str != NULL);
2553550b6e40SSowmini Varadhan }
2554550b6e40SSowmini Varadhan 
2555550b6e40SSowmini Varadhan static int
2556550b6e40SSowmini Varadhan ipv4_prefixlen(struct sockaddr_in *sin)
2557550b6e40SSowmini Varadhan {
2558550b6e40SSowmini Varadhan 	struct sockaddr_in *m;
2559550b6e40SSowmini Varadhan 	struct sockaddr_storage mask;
2560550b6e40SSowmini Varadhan 
2561550b6e40SSowmini Varadhan 	m = SIN(&mask);
2562550b6e40SSowmini Varadhan 	m->sin_family = AF_INET;
2563550b6e40SSowmini Varadhan 	if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
256464639aafSDarren Reed 		return (mask2plen((struct sockaddr *)&mask));
2565550b6e40SSowmini Varadhan 	} else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
2566550b6e40SSowmini Varadhan 		return (8);
2567550b6e40SSowmini Varadhan 	} else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
2568550b6e40SSowmini Varadhan 		return (16);
2569550b6e40SSowmini Varadhan 	} else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
2570550b6e40SSowmini Varadhan 		return (24);
2571550b6e40SSowmini Varadhan 	}
2572550b6e40SSowmini Varadhan 	return (0);
2573550b6e40SSowmini Varadhan }
2574550b6e40SSowmini Varadhan 
2575550b6e40SSowmini Varadhan static int
2576550b6e40SSowmini Varadhan zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
2577550b6e40SSowmini Varadhan     void *buf, size_t bufsize)
2578550b6e40SSowmini Varadhan {
2579550b6e40SSowmini Varadhan 	zone_net_data_t *zndata;
2580550b6e40SSowmini Varadhan 	size_t znsize;
2581550b6e40SSowmini Varadhan 	int err;
2582550b6e40SSowmini Varadhan 
2583550b6e40SSowmini Varadhan 	znsize = sizeof (*zndata) + bufsize;
2584550b6e40SSowmini Varadhan 	zndata = calloc(1, znsize);
2585550b6e40SSowmini Varadhan 	if (zndata == NULL)
2586550b6e40SSowmini Varadhan 		return (ENOMEM);
2587550b6e40SSowmini Varadhan 	zndata->zn_type = type;
2588550b6e40SSowmini Varadhan 	zndata->zn_len = bufsize;
2589550b6e40SSowmini Varadhan 	zndata->zn_linkid = linkid;
2590550b6e40SSowmini Varadhan 	bcopy(buf, zndata->zn_val, zndata->zn_len);
2591550b6e40SSowmini Varadhan 	err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
2592550b6e40SSowmini Varadhan 	free(zndata);
2593550b6e40SSowmini Varadhan 	return (err);
2594550b6e40SSowmini Varadhan }
2595550b6e40SSowmini Varadhan 
2596550b6e40SSowmini Varadhan static int
2597550b6e40SSowmini Varadhan add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
2598550b6e40SSowmini Varadhan {
2599550b6e40SSowmini Varadhan 	struct lifreq lifr;
2600550b6e40SSowmini Varadhan 	char **astr, *address;
2601550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
2602550b6e40SSowmini Varadhan 	char *ip_nospoof = "ip-nospoof";
2603550b6e40SSowmini Varadhan 	int nnet, naddr, err = 0, j;
2604550b6e40SSowmini Varadhan 	size_t zlen, cpleft;
2605550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *end;
2606550b6e40SSowmini Varadhan 	char  tmp[INET6_ADDRSTRLEN], *maskstr;
2607550b6e40SSowmini Varadhan 	char *zaddr, *cp;
2608550b6e40SSowmini Varadhan 	struct in6_addr *routes = NULL;
2609550b6e40SSowmini Varadhan 	boolean_t is_set;
2610550b6e40SSowmini Varadhan 	datalink_id_t linkid;
2611550b6e40SSowmini Varadhan 
2612550b6e40SSowmini Varadhan 	assert(start != NULL);
2613550b6e40SSowmini Varadhan 	naddr = 0; /* number of addresses */
2614550b6e40SSowmini Varadhan 	nnet = 0; /* number of net resources */
2615550b6e40SSowmini Varadhan 	linkid = start->za_linkid;
2616550b6e40SSowmini Varadhan 	for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
2617550b6e40SSowmini Varadhan 	    ptr = ptr->za_next) {
2618550b6e40SSowmini Varadhan 		nnet++;
2619550b6e40SSowmini Varadhan 	}
2620550b6e40SSowmini Varadhan 	end = ptr;
2621550b6e40SSowmini Varadhan 	zlen = nnet * (INET6_ADDRSTRLEN + 1);
2622550b6e40SSowmini Varadhan 	astr = calloc(1, nnet * sizeof (uintptr_t));
2623550b6e40SSowmini Varadhan 	zaddr = calloc(1, zlen);
2624550b6e40SSowmini Varadhan 	if (astr == NULL || zaddr == NULL) {
2625550b6e40SSowmini Varadhan 		err = ENOMEM;
2626550b6e40SSowmini Varadhan 		goto done;
2627550b6e40SSowmini Varadhan 	}
2628550b6e40SSowmini Varadhan 	cp = zaddr;
2629550b6e40SSowmini Varadhan 	cpleft = zlen;
2630550b6e40SSowmini Varadhan 	j = 0;
2631550b6e40SSowmini Varadhan 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
2632550b6e40SSowmini Varadhan 		address = ptr->za_nwiftab.zone_nwif_allowed_address;
2633550b6e40SSowmini Varadhan 		if (address[0] == '\0')
2634550b6e40SSowmini Varadhan 			continue;
2635550b6e40SSowmini Varadhan 		(void) snprintf(tmp, sizeof (tmp), "%s", address);
2636550b6e40SSowmini Varadhan 		/*
2637550b6e40SSowmini Varadhan 		 * Validate the data. zonecfg_valid_net_address() clobbers
2638550b6e40SSowmini Varadhan 		 * the /<mask> in the address string.
2639550b6e40SSowmini Varadhan 		 */
2640550b6e40SSowmini Varadhan 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2641550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "invalid address [%s]\n",
2642550b6e40SSowmini Varadhan 			    address);
2643550b6e40SSowmini Varadhan 			err = EINVAL;
2644550b6e40SSowmini Varadhan 			goto done;
2645550b6e40SSowmini Varadhan 		}
2646550b6e40SSowmini Varadhan 		/*
2647550b6e40SSowmini Varadhan 		 * convert any hostnames to numeric address strings.
2648550b6e40SSowmini Varadhan 		 */
2649550b6e40SSowmini Varadhan 		if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
2650550b6e40SSowmini Varadhan 		    (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
2651550b6e40SSowmini Varadhan 			err = EINVAL;
2652550b6e40SSowmini Varadhan 			goto done;
2653550b6e40SSowmini Varadhan 		}
2654550b6e40SSowmini Varadhan 		/*
2655550b6e40SSowmini Varadhan 		 * make a copy of the numeric string for the data needed
2656550b6e40SSowmini Varadhan 		 * by the "allowed-ips" datalink property.
2657550b6e40SSowmini Varadhan 		 */
2658550b6e40SSowmini Varadhan 		astr[j] = strdup(cp);
2659550b6e40SSowmini Varadhan 		if (astr[j] == NULL) {
2660550b6e40SSowmini Varadhan 			err = ENOMEM;
2661550b6e40SSowmini Varadhan 			goto done;
2662550b6e40SSowmini Varadhan 		}
2663550b6e40SSowmini Varadhan 		j++;
2664550b6e40SSowmini Varadhan 		/*
2665550b6e40SSowmini Varadhan 		 * compute the default netmask from the address, if necessary
2666550b6e40SSowmini Varadhan 		 */
2667550b6e40SSowmini Varadhan 		if ((maskstr = strchr(tmp, '/')) == NULL) {
2668550b6e40SSowmini Varadhan 			int prefixlen;
2669550b6e40SSowmini Varadhan 
2670550b6e40SSowmini Varadhan 			if (lifr.lifr_addr.ss_family == AF_INET) {
2671550b6e40SSowmini Varadhan 				prefixlen = ipv4_prefixlen(
2672550b6e40SSowmini Varadhan 				    SIN(&lifr.lifr_addr));
2673550b6e40SSowmini Varadhan 			} else {
2674550b6e40SSowmini Varadhan 				struct sockaddr_in6 *sin6;
2675550b6e40SSowmini Varadhan 
2676550b6e40SSowmini Varadhan 				sin6 = SIN6(&lifr.lifr_addr);
2677550b6e40SSowmini Varadhan 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2678550b6e40SSowmini Varadhan 					prefixlen = 10;
2679550b6e40SSowmini Varadhan 				else
2680550b6e40SSowmini Varadhan 					prefixlen = 64;
2681550b6e40SSowmini Varadhan 			}
2682550b6e40SSowmini Varadhan 			(void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
2683550b6e40SSowmini Varadhan 			maskstr = tmp;
2684550b6e40SSowmini Varadhan 		} else {
2685550b6e40SSowmini Varadhan 			maskstr++;
2686550b6e40SSowmini Varadhan 		}
2687550b6e40SSowmini Varadhan 		/* append the "/<netmask>" */
2688550b6e40SSowmini Varadhan 		(void) strlcat(cp, "/", cpleft);
2689550b6e40SSowmini Varadhan 		(void) strlcat(cp, maskstr, cpleft);
2690550b6e40SSowmini Varadhan 		(void) strlcat(cp, ",", cpleft);
2691550b6e40SSowmini Varadhan 		cp += strnlen(cp, zlen);
2692550b6e40SSowmini Varadhan 		cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
2693550b6e40SSowmini Varadhan 	}
2694550b6e40SSowmini Varadhan 	naddr = j; /* the actual number of addresses in the net resource */
2695550b6e40SSowmini Varadhan 	assert(naddr <= nnet);
2696550b6e40SSowmini Varadhan 
2697550b6e40SSowmini Varadhan 	/*
2698550b6e40SSowmini Varadhan 	 * zonecfg has already verified that the defrouter property can only
2699550b6e40SSowmini Varadhan 	 * be set if there is at least one address defined for the net resource.
2700550b6e40SSowmini Varadhan 	 * If j is 0, there are no addresses defined, and therefore no routers
2701550b6e40SSowmini Varadhan 	 * to configure, and we are done at that point.
2702550b6e40SSowmini Varadhan 	 */
2703550b6e40SSowmini Varadhan 	if (j == 0)
2704550b6e40SSowmini Varadhan 		goto done;
2705550b6e40SSowmini Varadhan 
2706550b6e40SSowmini Varadhan 	/* over-write last ',' with '\0' */
2707550b6e40SSowmini Varadhan 	zaddr[strnlen(zaddr, zlen) + 1] = '\0';
2708550b6e40SSowmini Varadhan 
2709550b6e40SSowmini Varadhan 	/*
2710550b6e40SSowmini Varadhan 	 * First make sure L3 protection is not already set on the link.
2711550b6e40SSowmini Varadhan 	 */
2712550b6e40SSowmini Varadhan 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2713550b6e40SSowmini Varadhan 	    "protection", &is_set);
2714550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2715550b6e40SSowmini Varadhan 		err = EINVAL;
2716550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "unable to check if protection is set");
2717550b6e40SSowmini Varadhan 		goto done;
2718550b6e40SSowmini Varadhan 	}
2719550b6e40SSowmini Varadhan 	if (is_set) {
2720550b6e40SSowmini Varadhan 		err = EINVAL;
2721550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "Protection is already set");
2722550b6e40SSowmini Varadhan 		goto done;
2723550b6e40SSowmini Varadhan 	}
2724550b6e40SSowmini Varadhan 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2725550b6e40SSowmini Varadhan 	    "allowed-ips", &is_set);
2726550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2727550b6e40SSowmini Varadhan 		err = EINVAL;
2728550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
2729550b6e40SSowmini Varadhan 		goto done;
2730550b6e40SSowmini Varadhan 	}
2731550b6e40SSowmini Varadhan 	if (is_set) {
2732550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "allowed-ips is already set");
2733550b6e40SSowmini Varadhan 		err = EINVAL;
2734550b6e40SSowmini Varadhan 		goto done;
2735550b6e40SSowmini Varadhan 	}
2736550b6e40SSowmini Varadhan 
2737550b6e40SSowmini Varadhan 	/*
2738550b6e40SSowmini Varadhan 	 * Enable ip-nospoof for the link, and add address to the allowed-ips
2739550b6e40SSowmini Varadhan 	 * list.
2740550b6e40SSowmini Varadhan 	 */
2741550b6e40SSowmini Varadhan 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
2742550b6e40SSowmini Varadhan 	    &ip_nospoof, 1, DLADM_OPT_ACTIVE);
2743550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2744550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "could not set protection\n");
2745550b6e40SSowmini Varadhan 		err = EINVAL;
2746550b6e40SSowmini Varadhan 		goto done;
2747550b6e40SSowmini Varadhan 	}
2748550b6e40SSowmini Varadhan 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
2749550b6e40SSowmini Varadhan 	    astr, naddr, DLADM_OPT_ACTIVE);
2750550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2751550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
2752550b6e40SSowmini Varadhan 		err = EINVAL;
2753550b6e40SSowmini Varadhan 		goto done;
2754550b6e40SSowmini Varadhan 	}
2755550b6e40SSowmini Varadhan 
2756550b6e40SSowmini Varadhan 	/* now set the address in the data-store */
2757550b6e40SSowmini Varadhan 	err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
2758550b6e40SSowmini Varadhan 	    zaddr, strnlen(zaddr, zlen) + 1);
2759550b6e40SSowmini Varadhan 	if (err != 0)
2760550b6e40SSowmini Varadhan 		goto done;
2761550b6e40SSowmini Varadhan 
2762550b6e40SSowmini Varadhan 	/*
2763550b6e40SSowmini Varadhan 	 * add the defaultrouters
2764550b6e40SSowmini Varadhan 	 */
2765550b6e40SSowmini Varadhan 	routes = calloc(1, nnet * sizeof (*routes));
2766550b6e40SSowmini Varadhan 	j = 0;
2767550b6e40SSowmini Varadhan 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
2768550b6e40SSowmini Varadhan 		address = ptr->za_nwiftab.zone_nwif_defrouter;
2769550b6e40SSowmini Varadhan 		if (address[0] == '\0')
2770550b6e40SSowmini Varadhan 			continue;
2771550b6e40SSowmini Varadhan 		if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
2772550b6e40SSowmini Varadhan 			/*
2773550b6e40SSowmini Varadhan 			 * zonecfg_valid_net_address() expects numeric IPv6
2774550b6e40SSowmini Varadhan 			 * addresses to have a CIDR format netmask.
2775550b6e40SSowmini Varadhan 			 */
2776550b6e40SSowmini Varadhan 			(void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
2777550b6e40SSowmini Varadhan 			(void) strlcat(address, tmp, INET6_ADDRSTRLEN);
2778550b6e40SSowmini Varadhan 		}
2779550b6e40SSowmini Varadhan 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2780550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE,
2781550b6e40SSowmini Varadhan 			    "invalid router [%s]\n", address);
2782550b6e40SSowmini Varadhan 			err = EINVAL;
2783550b6e40SSowmini Varadhan 			goto done;
2784550b6e40SSowmini Varadhan 		}
2785550b6e40SSowmini Varadhan 		if (lifr.lifr_addr.ss_family == AF_INET6) {
2786550b6e40SSowmini Varadhan 			routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
2787550b6e40SSowmini Varadhan 		} else {
2788550b6e40SSowmini Varadhan 			IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
2789550b6e40SSowmini Varadhan 			    &routes[j]);
2790550b6e40SSowmini Varadhan 		}
2791550b6e40SSowmini Varadhan 		j++;
2792550b6e40SSowmini Varadhan 	}
2793550b6e40SSowmini Varadhan 	assert(j <= nnet);
2794550b6e40SSowmini Varadhan 	if (j > 0) {
2795550b6e40SSowmini Varadhan 		err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
2796550b6e40SSowmini Varadhan 		    linkid, routes, j * sizeof (*routes));
2797550b6e40SSowmini Varadhan 	}
2798550b6e40SSowmini Varadhan done:
2799550b6e40SSowmini Varadhan 	free(routes);
2800550b6e40SSowmini Varadhan 	for (j = 0; j < naddr; j++)
2801550b6e40SSowmini Varadhan 		free(astr[j]);
2802550b6e40SSowmini Varadhan 	free(astr);
2803550b6e40SSowmini Varadhan 	free(zaddr);
2804550b6e40SSowmini Varadhan 	return (err);
2805550b6e40SSowmini Varadhan 
2806550b6e40SSowmini Varadhan }
2807550b6e40SSowmini Varadhan 
2808550b6e40SSowmini Varadhan static int
2809550b6e40SSowmini Varadhan add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
2810550b6e40SSowmini Varadhan {
2811550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr;
2812550b6e40SSowmini Varadhan 	datalink_id_t linkid;
2813550b6e40SSowmini Varadhan 	int err;
2814550b6e40SSowmini Varadhan 
2815550b6e40SSowmini Varadhan 	if (zalist == NULL)
2816550b6e40SSowmini Varadhan 		return (0);
2817550b6e40SSowmini Varadhan 
2818550b6e40SSowmini Varadhan 	linkid = zalist->za_linkid;
2819550b6e40SSowmini Varadhan 
2820550b6e40SSowmini Varadhan 	err = add_net_for_linkid(zlogp, zoneid, zalist);
2821550b6e40SSowmini Varadhan 	if (err != 0)
2822550b6e40SSowmini Varadhan 		return (err);
2823550b6e40SSowmini Varadhan 
2824550b6e40SSowmini Varadhan 	for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
2825550b6e40SSowmini Varadhan 		if (ptr->za_linkid == linkid)
2826550b6e40SSowmini Varadhan 			continue;
2827550b6e40SSowmini Varadhan 		linkid = ptr->za_linkid;
2828550b6e40SSowmini Varadhan 		err = add_net_for_linkid(zlogp, zoneid, ptr);
2829550b6e40SSowmini Varadhan 		if (err != 0)
2830550b6e40SSowmini Varadhan 			return (err);
2831550b6e40SSowmini Varadhan 	}
2832550b6e40SSowmini Varadhan 	return (0);
2833550b6e40SSowmini Varadhan }
2834550b6e40SSowmini Varadhan 
2835550b6e40SSowmini Varadhan /*
2836550b6e40SSowmini Varadhan  * Add "new" to the list of network interfaces to be configured  by
2837550b6e40SSowmini Varadhan  * add_net on zone boot in "old". The list of interfaces in "old" is
2838550b6e40SSowmini Varadhan  * sorted by datalink_id_t, with interfaces sorted FIFO for a given
2839550b6e40SSowmini Varadhan  * datalink_id_t.
2840550b6e40SSowmini Varadhan  *
2841550b6e40SSowmini Varadhan  * Returns the merged list of IP interfaces containing "old" and "new"
2842550b6e40SSowmini Varadhan  */
2843550b6e40SSowmini Varadhan static zone_addr_list_t *
2844550b6e40SSowmini Varadhan add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
2845550b6e40SSowmini Varadhan {
2846550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *next;
2847550b6e40SSowmini Varadhan 	datalink_id_t linkid = new->za_linkid;
2848550b6e40SSowmini Varadhan 
2849550b6e40SSowmini Varadhan 	assert(old != new);
2850550b6e40SSowmini Varadhan 
2851550b6e40SSowmini Varadhan 	if (old == NULL)
2852550b6e40SSowmini Varadhan 		return (new);
2853550b6e40SSowmini Varadhan 	for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
2854550b6e40SSowmini Varadhan 		if (ptr->za_linkid == linkid)
2855550b6e40SSowmini Varadhan 			break;
2856550b6e40SSowmini Varadhan 	}
2857550b6e40SSowmini Varadhan 	if (ptr == NULL) {
2858550b6e40SSowmini Varadhan 		/* linkid does not already exist, add to the beginning */
2859550b6e40SSowmini Varadhan 		new->za_next = old;
2860550b6e40SSowmini Varadhan 		return (new);
2861550b6e40SSowmini Varadhan 	}
2862550b6e40SSowmini Varadhan 	/*
2863550b6e40SSowmini Varadhan 	 * adding to the middle of the list; ptr points at the first
2864550b6e40SSowmini Varadhan 	 * occurrence of linkid. Find the last occurrence.
2865550b6e40SSowmini Varadhan 	 */
2866550b6e40SSowmini Varadhan 	while ((next = ptr->za_next) != NULL) {
2867550b6e40SSowmini Varadhan 		if (next->za_linkid != linkid)
2868550b6e40SSowmini Varadhan 			break;
2869550b6e40SSowmini Varadhan 		ptr = next;
2870550b6e40SSowmini Varadhan 	}
2871550b6e40SSowmini Varadhan 	/* insert new after ptr */
2872550b6e40SSowmini Varadhan 	new->za_next = next;
2873550b6e40SSowmini Varadhan 	ptr->za_next = new;
2874550b6e40SSowmini Varadhan 	return (old);
2875550b6e40SSowmini Varadhan }
2876550b6e40SSowmini Varadhan 
2877550b6e40SSowmini Varadhan void
2878550b6e40SSowmini Varadhan free_ip_interface(zone_addr_list_t *zalist)
2879550b6e40SSowmini Varadhan {
2880550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *new;
2881550b6e40SSowmini Varadhan 
2882550b6e40SSowmini Varadhan 	for (ptr = zalist; ptr != NULL; ) {
2883550b6e40SSowmini Varadhan 		new = ptr;
2884550b6e40SSowmini Varadhan 		ptr = ptr->za_next;
2885550b6e40SSowmini Varadhan 		free(new);
2886550b6e40SSowmini Varadhan 	}
2887550b6e40SSowmini Varadhan }
2888550b6e40SSowmini Varadhan 
2889f4b3ec61Sdh155122 /*
2890f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2891f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2892f4b3ec61Sdh155122  * whatever we set up, and return an error.
2893f4b3ec61Sdh155122  */
2894f4b3ec61Sdh155122 static int
2895550b6e40SSowmini Varadhan configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2896f4b3ec61Sdh155122 {
2897f4b3ec61Sdh155122 	zone_dochandle_t handle;
2898f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2899f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2900f4b3ec61Sdh155122 	char path[MAXPATHLEN];
29012b24ab6bSSebastien Roy 	datalink_id_t linkid;
2902f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2903f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2904550b6e40SSowmini Varadhan 	zone_addr_list_t *zalist = NULL, *new;
2905f4b3ec61Sdh155122 
2906f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2907f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2908f4b3ec61Sdh155122 		return (-1);
2909f4b3ec61Sdh155122 	}
2910f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2911f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2912f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2913f4b3ec61Sdh155122 		return (-1);
2914f4b3ec61Sdh155122 	}
2915f4b3ec61Sdh155122 
2916f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2917f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2918f4b3ec61Sdh155122 		return (0);
2919f4b3ec61Sdh155122 	}
2920f4b3ec61Sdh155122 
2921f4b3ec61Sdh155122 	for (;;) {
2922f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2923f4b3ec61Sdh155122 			break;
2924f4b3ec61Sdh155122 
2925f4b3ec61Sdh155122 		if (prof == NULL) {
2926f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2927f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2928f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2929f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2930f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2931f4b3ec61Sdh155122 				    "unable to determine dev root");
2932f4b3ec61Sdh155122 				return (-1);
2933f4b3ec61Sdh155122 			}
2934f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2935f4b3ec61Sdh155122 			    "/dev");
2936f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2937f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2938f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2939f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2940f4b3ec61Sdh155122 				    "failed to initialize profile");
2941f4b3ec61Sdh155122 				return (-1);
2942f4b3ec61Sdh155122 			}
2943f4b3ec61Sdh155122 		}
2944f4b3ec61Sdh155122 
2945f4b3ec61Sdh155122 		/*
2946d62bc4baSyz147064 		 * Create the /dev entry for backward compatibility.
2947f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2948d62bc4baSyz147064 		 * Note that the zone still boots when the assigned
2949d62bc4baSyz147064 		 * interface is inaccessible, used by others, etc.
2950d62bc4baSyz147064 		 * Also, when vanity naming is used, some interface do
2951d62bc4baSyz147064 		 * do not have corresponding /dev node names (for example,
2952d62bc4baSyz147064 		 * vanity named aggregations).  The /dev entry is not
2953d62bc4baSyz147064 		 * created in that case.  The /dev/net entry is always
2954d62bc4baSyz147064 		 * accessible.
2955f4b3ec61Sdh155122 		 */
29562b24ab6bSSebastien Roy 		if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
29572b24ab6bSSebastien Roy 		    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
29582b24ab6bSSebastien Roy 		    add_datalink(zlogp, zone_name, linkid,
29592b24ab6bSSebastien Roy 		    nwiftab.zone_nwif_physical) == 0) {
29603bc21d0aSAruna Ramakrishna - Sun Microsystems 			added = B_TRUE;
29613bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
2962f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2963f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
29643bc21d0aSAruna Ramakrishna - Sun Microsystems 			zerror(zlogp, B_TRUE, "failed to add network device");
2965f4b3ec61Sdh155122 			return (-1);
2966f4b3ec61Sdh155122 		}
2967550b6e40SSowmini Varadhan 		/* set up the new IP interface, and add them all later */
2968550b6e40SSowmini Varadhan 		new = malloc(sizeof (*new));
2969550b6e40SSowmini Varadhan 		if (new == NULL) {
2970550b6e40SSowmini Varadhan 			zerror(zlogp, B_TRUE, "no memory for %s",
2971550b6e40SSowmini Varadhan 			    nwiftab.zone_nwif_physical);
2972550b6e40SSowmini Varadhan 			zonecfg_fini_handle(handle);
2973550b6e40SSowmini Varadhan 			free_ip_interface(zalist);
2974550b6e40SSowmini Varadhan 		}
2975550b6e40SSowmini Varadhan 		bzero(new, sizeof (*new));
2976550b6e40SSowmini Varadhan 		new->za_nwiftab = nwiftab;
2977550b6e40SSowmini Varadhan 		new->za_linkid = linkid;
2978550b6e40SSowmini Varadhan 		zalist = add_ip_interface(zalist, new);
2979550b6e40SSowmini Varadhan 	}
2980550b6e40SSowmini Varadhan 	if (zalist != NULL) {
2981550b6e40SSowmini Varadhan 		if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
2982550b6e40SSowmini Varadhan 			(void) zonecfg_endnwifent(handle);
2983550b6e40SSowmini Varadhan 			zonecfg_fini_handle(handle);
2984550b6e40SSowmini Varadhan 			zerror(zlogp, B_TRUE, "failed to add address");
2985550b6e40SSowmini Varadhan 			free_ip_interface(zalist);
2986550b6e40SSowmini Varadhan 			return (-1);
2987550b6e40SSowmini Varadhan 		}
2988550b6e40SSowmini Varadhan 		free_ip_interface(zalist);
2989d62bc4baSyz147064 	}
2990f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2991f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2992f4b3ec61Sdh155122 
2993f4b3ec61Sdh155122 	if (prof != NULL && added) {
2994f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2995f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
2996f4b3ec61Sdh155122 			return (-1);
2997f4b3ec61Sdh155122 		}
2998f4b3ec61Sdh155122 	}
2999f4b3ec61Sdh155122 	if (prof != NULL)
3000f4b3ec61Sdh155122 		di_prof_fini(prof);
3001f4b3ec61Sdh155122 
3002f4b3ec61Sdh155122 	return (0);
3003f4b3ec61Sdh155122 }
3004f4b3ec61Sdh155122 
3005f4b3ec61Sdh155122 static int
30060dc2366fSVenugopal Iyer remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
30070dc2366fSVenugopal Iyer {
30080dc2366fSVenugopal Iyer 	ushort_t flags;
30090dc2366fSVenugopal Iyer 	zone_iptype_t iptype;
30100dc2366fSVenugopal Iyer 	int i, dlnum = 0;
30110dc2366fSVenugopal Iyer 	datalink_id_t *dllink, *dllinks = NULL;
30120dc2366fSVenugopal Iyer 	dladm_status_t err;
30130dc2366fSVenugopal Iyer 
30140dc2366fSVenugopal Iyer 	if (strlen(pool_name) == 0)
30150dc2366fSVenugopal Iyer 		return (0);
30160dc2366fSVenugopal Iyer 
30170dc2366fSVenugopal Iyer 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
30180dc2366fSVenugopal Iyer 	    sizeof (flags)) < 0) {
30190dc2366fSVenugopal Iyer 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
3020550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
30210dc2366fSVenugopal Iyer 			return (-1);
30220dc2366fSVenugopal Iyer 		}
30230dc2366fSVenugopal Iyer 	} else {
30240dc2366fSVenugopal Iyer 		if (flags & ZF_NET_EXCL)
30250dc2366fSVenugopal Iyer 			iptype = ZS_EXCLUSIVE;
30260dc2366fSVenugopal Iyer 		else
30270dc2366fSVenugopal Iyer 			iptype = ZS_SHARED;
30280dc2366fSVenugopal Iyer 	}
30290dc2366fSVenugopal Iyer 
30300dc2366fSVenugopal Iyer 	if (iptype == ZS_EXCLUSIVE) {
30310dc2366fSVenugopal Iyer 		/*
30320dc2366fSVenugopal Iyer 		 * Get the datalink count and for each datalink,
30330dc2366fSVenugopal Iyer 		 * attempt to clear the pool property and clear
30340dc2366fSVenugopal Iyer 		 * the pool_name.
30350dc2366fSVenugopal Iyer 		 */
30360dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
30370dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to count network "
30380dc2366fSVenugopal Iyer 			    "interfaces");
30390dc2366fSVenugopal Iyer 			return (-1);
30400dc2366fSVenugopal Iyer 		}
30410dc2366fSVenugopal Iyer 
30420dc2366fSVenugopal Iyer 		if (dlnum == 0)
30430dc2366fSVenugopal Iyer 			return (0);
30440dc2366fSVenugopal Iyer 
30450dc2366fSVenugopal Iyer 		if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
30460dc2366fSVenugopal Iyer 		    == NULL) {
30470dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "memory allocation failed");
30480dc2366fSVenugopal Iyer 			return (-1);
30490dc2366fSVenugopal Iyer 		}
30500dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
30510dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to list network "
30520dc2366fSVenugopal Iyer 			    "interfaces");
30530dc2366fSVenugopal Iyer 			return (-1);
30540dc2366fSVenugopal Iyer 		}
30550dc2366fSVenugopal Iyer 
3056a3002e0aSGarrett D'Amore 		bzero(pool_name, sizeof (pool_name));
30570dc2366fSVenugopal Iyer 		for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
30580dc2366fSVenugopal Iyer 			err = dladm_set_linkprop(dld_handle, *dllink, "pool",
30590dc2366fSVenugopal Iyer 			    NULL, 0, DLADM_OPT_ACTIVE);
30600dc2366fSVenugopal Iyer 			if (err != DLADM_STATUS_OK) {
30610dc2366fSVenugopal Iyer 				zerror(zlogp, B_TRUE,
30620dc2366fSVenugopal Iyer 				    "WARNING: unable to clear pool");
30630dc2366fSVenugopal Iyer 			}
30640dc2366fSVenugopal Iyer 		}
30650dc2366fSVenugopal Iyer 		free(dllinks);
30660dc2366fSVenugopal Iyer 	}
30670dc2366fSVenugopal Iyer 	return (0);
30680dc2366fSVenugopal Iyer }
30690dc2366fSVenugopal Iyer 
30700dc2366fSVenugopal Iyer static int
3071550b6e40SSowmini Varadhan remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
3072550b6e40SSowmini Varadhan {
3073550b6e40SSowmini Varadhan 	ushort_t flags;
3074550b6e40SSowmini Varadhan 	zone_iptype_t iptype;
3075550b6e40SSowmini Varadhan 	int i, dlnum = 0;
3076550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
3077550b6e40SSowmini Varadhan 	datalink_id_t *dllink, *dllinks = NULL;
3078550b6e40SSowmini Varadhan 
3079550b6e40SSowmini Varadhan 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3080550b6e40SSowmini Varadhan 	    sizeof (flags)) < 0) {
3081550b6e40SSowmini Varadhan 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
3082550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
3083550b6e40SSowmini Varadhan 			return (-1);
3084550b6e40SSowmini Varadhan 		}
3085550b6e40SSowmini Varadhan 	} else {
3086550b6e40SSowmini Varadhan 		if (flags & ZF_NET_EXCL)
3087550b6e40SSowmini Varadhan 			iptype = ZS_EXCLUSIVE;
3088550b6e40SSowmini Varadhan 		else
3089550b6e40SSowmini Varadhan 			iptype = ZS_SHARED;
3090550b6e40SSowmini Varadhan 	}
3091550b6e40SSowmini Varadhan 
3092550b6e40SSowmini Varadhan 	if (iptype != ZS_EXCLUSIVE)
3093550b6e40SSowmini Varadhan 		return (0);
3094550b6e40SSowmini Varadhan 
3095550b6e40SSowmini Varadhan 	/*
3096550b6e40SSowmini Varadhan 	 * Get the datalink count and for each datalink,
3097550b6e40SSowmini Varadhan 	 * attempt to clear the pool property and clear
3098550b6e40SSowmini Varadhan 	 * the pool_name.
3099550b6e40SSowmini Varadhan 	 */
3100550b6e40SSowmini Varadhan 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3101550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "unable to count network interfaces");
3102550b6e40SSowmini Varadhan 		return (-1);
3103550b6e40SSowmini Varadhan 	}
3104550b6e40SSowmini Varadhan 
3105550b6e40SSowmini Varadhan 	if (dlnum == 0)
3106550b6e40SSowmini Varadhan 		return (0);
3107550b6e40SSowmini Varadhan 
3108550b6e40SSowmini Varadhan 	if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
3109550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "memory allocation failed");
3110550b6e40SSowmini Varadhan 		return (-1);
3111550b6e40SSowmini Varadhan 	}
3112550b6e40SSowmini Varadhan 	if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3113550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
3114550b6e40SSowmini Varadhan 		free(dllinks);
3115550b6e40SSowmini Varadhan 		return (-1);
3116550b6e40SSowmini Varadhan 	}
3117550b6e40SSowmini Varadhan 
3118550b6e40SSowmini Varadhan 	for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3119fff7ec1dSSowmini Varadhan 		char dlerr[DLADM_STRSIZE];
3120fff7ec1dSSowmini Varadhan 
3121550b6e40SSowmini Varadhan 		dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3122550b6e40SSowmini Varadhan 		    "protection", NULL, 0, DLADM_OPT_ACTIVE);
3123fff7ec1dSSowmini Varadhan 		if (dlstatus == DLADM_STATUS_NOTFOUND) {
3124fff7ec1dSSowmini Varadhan 			/* datalink does not belong to the GZ */
3125fff7ec1dSSowmini Varadhan 			continue;
3126fff7ec1dSSowmini Varadhan 		}
3127550b6e40SSowmini Varadhan 		if (dlstatus != DLADM_STATUS_OK) {
3128fff7ec1dSSowmini Varadhan 			zerror(zlogp, B_FALSE,
3129fff7ec1dSSowmini Varadhan 			    dladm_status2str(dlstatus, dlerr));
3130550b6e40SSowmini Varadhan 			free(dllinks);
3131550b6e40SSowmini Varadhan 			return (-1);
3132550b6e40SSowmini Varadhan 		}
3133550b6e40SSowmini Varadhan 		dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3134550b6e40SSowmini Varadhan 		    "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
3135550b6e40SSowmini Varadhan 		if (dlstatus != DLADM_STATUS_OK) {
3136fff7ec1dSSowmini Varadhan 			zerror(zlogp, B_FALSE,
3137fff7ec1dSSowmini Varadhan 			    dladm_status2str(dlstatus, dlerr));
3138550b6e40SSowmini Varadhan 			free(dllinks);
3139550b6e40SSowmini Varadhan 			return (-1);
3140550b6e40SSowmini Varadhan 		}
3141550b6e40SSowmini Varadhan 	}
3142550b6e40SSowmini Varadhan 	free(dllinks);
3143550b6e40SSowmini Varadhan 	return (0);
3144550b6e40SSowmini Varadhan }
3145550b6e40SSowmini Varadhan 
3146550b6e40SSowmini Varadhan static int
31472b24ab6bSSebastien Roy unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
3148f4b3ec61Sdh155122 {
31492b24ab6bSSebastien Roy 	int dlnum = 0;
3150f4b3ec61Sdh155122 
31512b24ab6bSSebastien Roy 	/*
31522b24ab6bSSebastien Roy 	 * The kernel shutdown callback for the dls module should have removed
31532b24ab6bSSebastien Roy 	 * all datalinks from this zone.  If any remain, then there's a
31542b24ab6bSSebastien Roy 	 * problem.
31552b24ab6bSSebastien Roy 	 */
3156f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3157f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
3158f4b3ec61Sdh155122 		return (-1);
3159f4b3ec61Sdh155122 	}
31602b24ab6bSSebastien Roy 	if (dlnum != 0) {
31612b24ab6bSSebastien Roy 		zerror(zlogp, B_FALSE,
31622b24ab6bSSebastien Roy 		    "datalinks remain in zone after shutdown");
3163f4b3ec61Sdh155122 		return (-1);
3164f4b3ec61Sdh155122 	}
3165f4b3ec61Sdh155122 	return (0);
3166f4b3ec61Sdh155122 }
3167f4b3ec61Sdh155122 
31687c478bd9Sstevel@tonic-gate static int
31697c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
31707c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
31717c478bd9Sstevel@tonic-gate {
31727c478bd9Sstevel@tonic-gate 	int fd;
31737c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
31747c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
31757c478bd9Sstevel@tonic-gate 	int error;
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
31787c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
31797c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
31807c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
31817c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
31847c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
31857c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
31867c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
31897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
31907c478bd9Sstevel@tonic-gate 		return (-1);
31917c478bd9Sstevel@tonic-gate 	}
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
31947c478bd9Sstevel@tonic-gate 	(void) close(fd);
31957c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
31967c478bd9Sstevel@tonic-gate 		return (0);
31977c478bd9Sstevel@tonic-gate 	return (-1);
31987c478bd9Sstevel@tonic-gate }
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate static int
32017c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
32027c478bd9Sstevel@tonic-gate {
32037c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
32047c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
32057c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
32067c478bd9Sstevel@tonic-gate 	int error;
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 	/*
32097c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
32107c478bd9Sstevel@tonic-gate 	 */
32117c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
32127c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
32137c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
32147c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
32157c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
32187c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
32197c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
32207c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
32217c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32247c478bd9Sstevel@tonic-gate 		return (error);
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate 	/*
32277c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
32287c478bd9Sstevel@tonic-gate 	 */
32297c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
32307c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
32317c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
32327c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
32337c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
32367c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
32377c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
32387c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
32397c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32427c478bd9Sstevel@tonic-gate 		return (error);
32437c478bd9Sstevel@tonic-gate 	return (0);
32447c478bd9Sstevel@tonic-gate }
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate static int
32476cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
3248ffbafc53Scomay {
3249ffbafc53Scomay 	int error = -1;
3250ffbafc53Scomay 	zone_dochandle_t handle;
3251ffbafc53Scomay 	char *privname = NULL;
3252ffbafc53Scomay 
3253ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
3254ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3255ffbafc53Scomay 		return (-1);
3256ffbafc53Scomay 	}
3257ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3258ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
3259ffbafc53Scomay 		zonecfg_fini_handle(handle);
3260ffbafc53Scomay 		return (-1);
3261ffbafc53Scomay 	}
3262ffbafc53Scomay 
32636cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
3264bf1d7e28Sdh155122 		zone_iptype_t	iptype;
3265bf1d7e28Sdh155122 		const char	*curr_iptype;
3266bf1d7e28Sdh155122 
3267bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
3268bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
3269bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
3270bf1d7e28Sdh155122 			return (-1);
3271bf1d7e28Sdh155122 		}
3272bf1d7e28Sdh155122 
3273bf1d7e28Sdh155122 		switch (iptype) {
3274bf1d7e28Sdh155122 		case ZS_SHARED:
3275bf1d7e28Sdh155122 			curr_iptype = "shared";
3276bf1d7e28Sdh155122 			break;
3277bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
3278bf1d7e28Sdh155122 			curr_iptype = "exclusive";
3279bf1d7e28Sdh155122 			break;
3280bf1d7e28Sdh155122 		}
3281bf1d7e28Sdh155122 
3282e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
3283e767a340Sgjelinek 			zonecfg_fini_handle(handle);
32849acbbeafSnn35248 			return (0);
3285e767a340Sgjelinek 		}
32869acbbeafSnn35248 		zerror(zlogp, B_FALSE,
32879acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
32889acbbeafSnn35248 		zonecfg_fini_handle(handle);
32899acbbeafSnn35248 		return (-1);
32909acbbeafSnn35248 	}
32919acbbeafSnn35248 
3292ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
3293ffbafc53Scomay 	case Z_OK:
3294ffbafc53Scomay 		error = 0;
3295ffbafc53Scomay 		break;
3296ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3297ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3298ffbafc53Scomay 		    "within the zone's privilege set", privname);
3299ffbafc53Scomay 		break;
3300ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3301ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3302ffbafc53Scomay 		    "from the zone's privilege set", privname);
3303ffbafc53Scomay 		break;
3304ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3305ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3306ffbafc53Scomay 		    "in the zone's privilege set", privname);
3307ffbafc53Scomay 		break;
3308ffbafc53Scomay 	default:
3309ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
3310ffbafc53Scomay 		    "privilege set");
3311ffbafc53Scomay 		break;
3312ffbafc53Scomay 	}
3313ffbafc53Scomay 
3314ffbafc53Scomay 	free(privname);
3315ffbafc53Scomay 	zonecfg_fini_handle(handle);
3316ffbafc53Scomay 	return (error);
3317ffbafc53Scomay }
3318ffbafc53Scomay 
3319ffbafc53Scomay static int
33207c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
33217c478bd9Sstevel@tonic-gate {
33227c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
33237c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
33247c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
33257c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
33267c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
33277c478bd9Sstevel@tonic-gate 	int error = -1;
33287c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
33297c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
33307c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
3331ff19e029SMenno Lageman 	uint64_t maxlwps;
3332ff19e029SMenno Lageman 	uint64_t maxprocs;
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	*bufp = NULL;
33357c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
33387c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
33397c478bd9Sstevel@tonic-gate 		return (-1);
33407c478bd9Sstevel@tonic-gate 	}
33417c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
33427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
33437c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
33447c478bd9Sstevel@tonic-gate 		return (-1);
33457c478bd9Sstevel@tonic-gate 	}
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
33487c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
33497c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
33507c478bd9Sstevel@tonic-gate 		goto out;
33517c478bd9Sstevel@tonic-gate 	}
33527c478bd9Sstevel@tonic-gate 
3353ff19e029SMenno Lageman 	/*
3354ff19e029SMenno Lageman 	 * Allow the administrator to control both the maximum number of
3355ff19e029SMenno Lageman 	 * process table slots and the maximum number of lwps with just the
3356ff19e029SMenno Lageman 	 * max-processes property.  If only the max-processes property is set,
3357ff19e029SMenno Lageman 	 * we add a max-lwps property with a limit derived from max-processes.
3358ff19e029SMenno Lageman 	 */
3359ff19e029SMenno Lageman 	if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs)
3360ff19e029SMenno Lageman 	    == Z_OK &&
3361ff19e029SMenno Lageman 	    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps)
3362ff19e029SMenno Lageman 	    == Z_NO_ENTRY) {
3363ff19e029SMenno Lageman 		if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
3364ff19e029SMenno Lageman 		    maxprocs * LWPS_PER_PROCESS) != Z_OK) {
3365ff19e029SMenno Lageman 			zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
3366ff19e029SMenno Lageman 			goto out;
3367ff19e029SMenno Lageman 		}
3368ff19e029SMenno Lageman 	}
3369ff19e029SMenno Lageman 
33707c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
33717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
33727c478bd9Sstevel@tonic-gate 		goto out;
33737c478bd9Sstevel@tonic-gate 	}
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
33767c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
33777c478bd9Sstevel@tonic-gate 		goto out;
33787c478bd9Sstevel@tonic-gate 	}
33797c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
33807c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
33817c478bd9Sstevel@tonic-gate 		uint_t i, count;
33827c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
33857c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
33867c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
33877c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
33887c478bd9Sstevel@tonic-gate 			continue;
33897c478bd9Sstevel@tonic-gate 		}
33907c478bd9Sstevel@tonic-gate 		count = 0;
33917c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
33927c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
33937c478bd9Sstevel@tonic-gate 			count++;
33947c478bd9Sstevel@tonic-gate 		}
33957c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
33967c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
33977c478bd9Sstevel@tonic-gate 		}
33987c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
33997c478bd9Sstevel@tonic-gate 			goto out;
34007c478bd9Sstevel@tonic-gate 		i = 0;
34017c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
34027c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
34037c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
34047c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
34057c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
34067c478bd9Sstevel@tonic-gate 				goto out;
34077c478bd9Sstevel@tonic-gate 			}
34087c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
34097c478bd9Sstevel@tonic-gate 			    != Z_OK) {
34107c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
34117c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
34127c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
34137c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
34147c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
34157c478bd9Sstevel@tonic-gate 				goto out;
34167c478bd9Sstevel@tonic-gate 			}
34177c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
34187c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
34197c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
34207c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
34217c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
34227c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
34237c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
34247c478bd9Sstevel@tonic-gate 				    name);
34257c478bd9Sstevel@tonic-gate 				goto out;
34267c478bd9Sstevel@tonic-gate 			}
34277c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
34287c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
34297c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34307c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34317c478bd9Sstevel@tonic-gate 				goto out;
34327c478bd9Sstevel@tonic-gate 			}
34337c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
34347c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
34357c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34367c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34377c478bd9Sstevel@tonic-gate 				goto out;
34387c478bd9Sstevel@tonic-gate 			}
34397c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
34407c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
34417c478bd9Sstevel@tonic-gate 			    != 0) {
34427c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34437c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34447c478bd9Sstevel@tonic-gate 				goto out;
34457c478bd9Sstevel@tonic-gate 			}
34467c478bd9Sstevel@tonic-gate 		}
34477c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34487c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
34497c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
34507c478bd9Sstevel@tonic-gate 		    != 0) {
34517c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
34527c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
34537c478bd9Sstevel@tonic-gate 			goto out;
34547c478bd9Sstevel@tonic-gate 		}
34557c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
34567c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
34577c478bd9Sstevel@tonic-gate 		free(nvlv);
34587c478bd9Sstevel@tonic-gate 		nvlv = NULL;
34597c478bd9Sstevel@tonic-gate 		rctlcount++;
34607c478bd9Sstevel@tonic-gate 	}
34617c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
34647c478bd9Sstevel@tonic-gate 		error = 0;
34657c478bd9Sstevel@tonic-gate 		goto out;
34667c478bd9Sstevel@tonic-gate 	}
34677c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
34687c478bd9Sstevel@tonic-gate 	    != 0) {
34697c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
34707c478bd9Sstevel@tonic-gate 		goto out;
34717c478bd9Sstevel@tonic-gate 	}
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate 	error = 0;
34747c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
34757c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate out:
34787c478bd9Sstevel@tonic-gate 	free(rctlblk);
34797c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34807c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
34817c478bd9Sstevel@tonic-gate 		free(nvl_packed);
34827c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
34837c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
34847c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
34857c478bd9Sstevel@tonic-gate 		free(nvlv);
34867c478bd9Sstevel@tonic-gate 	if (handle != NULL)
34877c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
34887c478bd9Sstevel@tonic-gate 	return (error);
34897c478bd9Sstevel@tonic-gate }
34907c478bd9Sstevel@tonic-gate 
34917c478bd9Sstevel@tonic-gate static int
3492c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr)
3493c5cd6260S {
3494c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
3495c5cd6260S 
3496c5cd6260S 	if (query_hook[0] == '\0')
3497c5cd6260S 		return (0);
3498c5cd6260S 
3499c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3500c5cd6260S 	    > sizeof (cmdbuf))
3501c5cd6260S 		return (-1);
3502c5cd6260S 
3503c5cd6260S 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3504c5cd6260S 		return (-1);
3505c5cd6260S 
3506c5cd6260S 	return (0);
3507c5cd6260S }
3508c5cd6260S 
3509c5cd6260S static int
3510fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3511fa9e4066Sahrens {
3512fa9e4066Sahrens 	zone_dochandle_t handle;
3513fa9e4066Sahrens 	struct zone_dstab dstab;
3514fa9e4066Sahrens 	size_t total, offset, len;
3515fa9e4066Sahrens 	int error = -1;
3516e5a17f6aSgjelinek 	char *str = NULL;
3517c5cd6260S 	char *implicit_datasets = NULL;
3518c5cd6260S 	int implicit_len = 0;
3519fa9e4066Sahrens 
3520fa9e4066Sahrens 	*bufp = NULL;
3521fa9e4066Sahrens 	*bufsizep = 0;
3522fa9e4066Sahrens 
3523fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3524fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3525fa9e4066Sahrens 		return (-1);
3526fa9e4066Sahrens 	}
3527fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3528fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3529fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3530fa9e4066Sahrens 		return (-1);
3531fa9e4066Sahrens 	}
3532fa9e4066Sahrens 
3533c5cd6260S 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3534c5cd6260S 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3535c5cd6260S 		goto out;
3536c5cd6260S 	}
3537c5cd6260S 
3538fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3539fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3540fa9e4066Sahrens 		goto out;
3541fa9e4066Sahrens 	}
3542fa9e4066Sahrens 
3543fa9e4066Sahrens 	total = 0;
3544fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3545fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3546fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3547fa9e4066Sahrens 
3548c5cd6260S 	if (implicit_datasets != NULL)
3549c5cd6260S 		implicit_len = strlen(implicit_datasets);
3550c5cd6260S 	if (implicit_len > 0)
3551c5cd6260S 		total += implicit_len + 1;
3552c5cd6260S 
3553fa9e4066Sahrens 	if (total == 0) {
3554fa9e4066Sahrens 		error = 0;
3555fa9e4066Sahrens 		goto out;
3556fa9e4066Sahrens 	}
3557fa9e4066Sahrens 
3558fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3559fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3560fa9e4066Sahrens 		goto out;
3561fa9e4066Sahrens 	}
3562fa9e4066Sahrens 
3563fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3564fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3565fa9e4066Sahrens 		goto out;
3566fa9e4066Sahrens 	}
3567fa9e4066Sahrens 	offset = 0;
3568fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3569fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3570fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3571e5a17f6aSgjelinek 		    total - offset);
3572fa9e4066Sahrens 		offset += len;
3573e5a17f6aSgjelinek 		if (offset < total - 1)
3574fa9e4066Sahrens 			str[offset++] = ',';
3575fa9e4066Sahrens 	}
3576fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3577fa9e4066Sahrens 
3578c5cd6260S 	if (implicit_len > 0)
3579c5cd6260S 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3580c5cd6260S 
3581fa9e4066Sahrens 	error = 0;
3582fa9e4066Sahrens 	*bufp = str;
3583fa9e4066Sahrens 	*bufsizep = total;
3584fa9e4066Sahrens 
3585fa9e4066Sahrens out:
3586fa9e4066Sahrens 	if (error != 0 && str != NULL)
3587fa9e4066Sahrens 		free(str);
3588fa9e4066Sahrens 	if (handle != NULL)
3589fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3590c5cd6260S 	if (implicit_datasets != NULL)
3591c5cd6260S 		free(implicit_datasets);
3592fa9e4066Sahrens 
3593fa9e4066Sahrens 	return (error);
3594fa9e4066Sahrens }
3595fa9e4066Sahrens 
3596fa9e4066Sahrens static int
3597fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3598fa9e4066Sahrens {
3599fa9e4066Sahrens 	zone_dochandle_t handle;
3600fa9e4066Sahrens 	struct zone_dstab dstab;
3601fa9e4066Sahrens 	zfs_handle_t *zhp;
360299653d4eSeschrock 	libzfs_handle_t *hdl;
3603fa9e4066Sahrens 
3604fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3605fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3606fa9e4066Sahrens 		return (-1);
3607fa9e4066Sahrens 	}
3608fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3609fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3610fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3611fa9e4066Sahrens 		return (-1);
3612fa9e4066Sahrens 	}
3613fa9e4066Sahrens 
3614fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3615fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3616fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3617fa9e4066Sahrens 		return (-1);
3618fa9e4066Sahrens 	}
3619fa9e4066Sahrens 
362099653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
362199653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
362299653d4eSeschrock 		zonecfg_fini_handle(handle);
362399653d4eSeschrock 		return (-1);
362499653d4eSeschrock 	}
3625fa9e4066Sahrens 
3626fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3627fa9e4066Sahrens 
362899653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3629fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3630fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3631fa9e4066Sahrens 			    dstab.zone_dataset_name);
3632fa9e4066Sahrens 			zonecfg_fini_handle(handle);
363399653d4eSeschrock 			libzfs_fini(hdl);
3634fa9e4066Sahrens 			return (-1);
3635fa9e4066Sahrens 		}
3636fa9e4066Sahrens 
3637fa9e4066Sahrens 		/*
3638fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3639fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3640fa9e4066Sahrens 		 */
3641fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3642e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3643e9dbad6fSeschrock 		    "on") != 0) {
3644fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3645fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3646fa9e4066Sahrens 			    dstab.zone_dataset_name);
3647fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3648fa9e4066Sahrens 			zfs_close(zhp);
364999653d4eSeschrock 			libzfs_fini(hdl);
3650fa9e4066Sahrens 			return (-1);
3651fa9e4066Sahrens 		}
3652fa9e4066Sahrens 
3653fa9e4066Sahrens 		zfs_close(zhp);
3654fa9e4066Sahrens 	}
3655fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3656fa9e4066Sahrens 
3657fa9e4066Sahrens 	zonecfg_fini_handle(handle);
365899653d4eSeschrock 	libzfs_fini(hdl);
3659fa9e4066Sahrens 
3660fa9e4066Sahrens 	return (0);
3661fa9e4066Sahrens }
3662fa9e4066Sahrens 
366345916cd2Sjpk /*
36644201a95eSRic Aleshire  * Return true if the path is its own zfs file system.  We determine this
36654201a95eSRic Aleshire  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
36664201a95eSRic Aleshire  * if it is a different fs.
36674201a95eSRic Aleshire  */
36684201a95eSRic Aleshire boolean_t
36694201a95eSRic Aleshire is_zonepath_zfs(char *zonepath)
36704201a95eSRic Aleshire {
36714201a95eSRic Aleshire 	int res;
36724201a95eSRic Aleshire 	char *path;
36734201a95eSRic Aleshire 	char *parent;
36744201a95eSRic Aleshire 	struct statvfs64 buf1, buf2;
36754201a95eSRic Aleshire 
36764201a95eSRic Aleshire 	if (statvfs64(zonepath, &buf1) != 0)
36774201a95eSRic Aleshire 		return (B_FALSE);
36784201a95eSRic Aleshire 
36794201a95eSRic Aleshire 	if (strcmp(buf1.f_basetype, "zfs") != 0)
36804201a95eSRic Aleshire 		return (B_FALSE);
36814201a95eSRic Aleshire 
36824201a95eSRic Aleshire 	if ((path = strdup(zonepath)) == NULL)
36834201a95eSRic Aleshire 		return (B_FALSE);
36844201a95eSRic Aleshire 
36854201a95eSRic Aleshire 	parent = dirname(path);
36864201a95eSRic Aleshire 	res = statvfs64(parent, &buf2);
36874201a95eSRic Aleshire 	free(path);
36884201a95eSRic Aleshire 
36894201a95eSRic Aleshire 	if (res != 0)
36904201a95eSRic Aleshire 		return (B_FALSE);
36914201a95eSRic Aleshire 
36924201a95eSRic Aleshire 	if (buf1.f_fsid == buf2.f_fsid)
36934201a95eSRic Aleshire 		return (B_FALSE);
36944201a95eSRic Aleshire 
36954201a95eSRic Aleshire 	return (B_TRUE);
36964201a95eSRic Aleshire }
36974201a95eSRic Aleshire 
36984201a95eSRic Aleshire /*
36994201a95eSRic Aleshire  * Verify the MAC label in the root dataset for the zone.
37004201a95eSRic Aleshire  * If the label exists, it must match the label configured for the zone.
37014201a95eSRic Aleshire  * Otherwise if there's no label on the dataset, create one here.
37024201a95eSRic Aleshire  */
37034201a95eSRic Aleshire 
37044201a95eSRic Aleshire static int
37054201a95eSRic Aleshire validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
37064201a95eSRic Aleshire {
37074201a95eSRic Aleshire 	int		error = -1;
37084201a95eSRic Aleshire 	zfs_handle_t	*zhp;
37094201a95eSRic Aleshire 	libzfs_handle_t	*hdl;
37104201a95eSRic Aleshire 	m_label_t	ds_sl;
37114201a95eSRic Aleshire 	char		zonepath[MAXPATHLEN];
37124201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
37134201a95eSRic Aleshire 
37144201a95eSRic Aleshire 	if (!is_system_labeled())
37154201a95eSRic Aleshire 		return (0);
37164201a95eSRic Aleshire 
37174201a95eSRic Aleshire 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
37184201a95eSRic Aleshire 		zerror(zlogp, B_TRUE, "unable to determine zone path");
37194201a95eSRic Aleshire 		return (-1);
37204201a95eSRic Aleshire 	}
37214201a95eSRic Aleshire 
37224201a95eSRic Aleshire 	if (!is_zonepath_zfs(zonepath))
37234201a95eSRic Aleshire 		return (0);
37244201a95eSRic Aleshire 
37254201a95eSRic Aleshire 	if ((hdl = libzfs_init()) == NULL) {
37264201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "opening ZFS library");
37274201a95eSRic Aleshire 		return (-1);
37284201a95eSRic Aleshire 	}
37294201a95eSRic Aleshire 
37304201a95eSRic Aleshire 	if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
37314201a95eSRic Aleshire 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
37324201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
37334201a95eSRic Aleshire 		    rootpath);
37344201a95eSRic Aleshire 		libzfs_fini(hdl);
37354201a95eSRic Aleshire 		return (-1);
37364201a95eSRic Aleshire 	}
37374201a95eSRic Aleshire 
37384201a95eSRic Aleshire 	/* Get the mlslabel property if it exists. */
37394201a95eSRic Aleshire 	if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
37404201a95eSRic Aleshire 	    NULL, NULL, 0, B_TRUE) != 0) ||
37414201a95eSRic Aleshire 	    (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
37424201a95eSRic Aleshire 		char		*str2 = NULL;
37434201a95eSRic Aleshire 
37444201a95eSRic Aleshire 		/*
37454201a95eSRic Aleshire 		 * No label on the dataset (or default only); create one.
37464201a95eSRic Aleshire 		 * (Only do this automatic labeling for the labeled brand.)
37474201a95eSRic Aleshire 		 */
37484201a95eSRic Aleshire 		if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
37494201a95eSRic Aleshire 			error = 0;
37504201a95eSRic Aleshire 			goto out;
37514201a95eSRic Aleshire 		}
37524201a95eSRic Aleshire 
37534201a95eSRic Aleshire 		error = l_to_str_internal(zone_sl, &str2);
37544201a95eSRic Aleshire 		if (error)
37554201a95eSRic Aleshire 			goto out;
37564201a95eSRic Aleshire 		if (str2 == NULL) {
37574201a95eSRic Aleshire 			error = -1;
37584201a95eSRic Aleshire 			goto out;
37594201a95eSRic Aleshire 		}
37604201a95eSRic Aleshire 		if ((error = zfs_prop_set(zhp,
37614201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
37624201a95eSRic Aleshire 			zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
37634201a95eSRic Aleshire 			    "property for root dataset at '%s'\n", rootpath);
37644201a95eSRic Aleshire 		}
37654201a95eSRic Aleshire 		free(str2);
37664201a95eSRic Aleshire 		goto out;
37674201a95eSRic Aleshire 	}
37684201a95eSRic Aleshire 
37694201a95eSRic Aleshire 	/* Convert the retrieved dataset label to binary form. */
37704201a95eSRic Aleshire 	error = hexstr_to_label(ds_hexsl, &ds_sl);
37714201a95eSRic Aleshire 	if (error) {
37724201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
37734201a95eSRic Aleshire 		    "property on root dataset at '%s'\n", rootpath);
37744201a95eSRic Aleshire 		goto out;			/* exit with error */
37754201a95eSRic Aleshire 	}
37764201a95eSRic Aleshire 
37774201a95eSRic Aleshire 	/*
37784201a95eSRic Aleshire 	 * Perform a MAC check by comparing the zone label with the
37794201a95eSRic Aleshire 	 * dataset label.
37804201a95eSRic Aleshire 	 */
37814201a95eSRic Aleshire 	error = (!blequal(zone_sl, &ds_sl));
37824201a95eSRic Aleshire 	if (error)
37834201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
37844201a95eSRic Aleshire out:
37854201a95eSRic Aleshire 	zfs_close(zhp);
37864201a95eSRic Aleshire 	libzfs_fini(hdl);
37874201a95eSRic Aleshire 
37884201a95eSRic Aleshire 	return (error);
37894201a95eSRic Aleshire }
37904201a95eSRic Aleshire 
37914201a95eSRic Aleshire /*
379245916cd2Sjpk  * Mount lower level home directories into/from current zone
379345916cd2Sjpk  * Share exported directories specified in dfstab for zone
379445916cd2Sjpk  */
379545916cd2Sjpk static int
379645916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
379745916cd2Sjpk {
379845916cd2Sjpk 	zoneid_t *zids = NULL;
379945916cd2Sjpk 	priv_set_t *zid_privs;
380045916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
380145916cd2Sjpk 	uint_t nzents_saved;
380245916cd2Sjpk 	uint_t nzents;
380345916cd2Sjpk 	int i;
380445916cd2Sjpk 	char readonly[] = "ro";
380545916cd2Sjpk 	struct zone_fstab lower_fstab;
380645916cd2Sjpk 	char *argv[4];
380745916cd2Sjpk 
380845916cd2Sjpk 	if (!is_system_labeled())
380945916cd2Sjpk 		return (0);
381045916cd2Sjpk 
381145916cd2Sjpk 	if (zid_label == NULL) {
381245916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
381345916cd2Sjpk 		if (zid_label == NULL)
381445916cd2Sjpk 			return (-1);
381545916cd2Sjpk 	}
381645916cd2Sjpk 
381745916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
381845916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
381927e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
382045916cd2Sjpk 
382145916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
382245916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
382345916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
382445916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
382545916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
382645916cd2Sjpk 
382745916cd2Sjpk 	/*
382845916cd2Sjpk 	 * Get the list of zones from the kernel
382945916cd2Sjpk 	 */
383045916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
383145916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
383245916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
383345916cd2Sjpk 		return (-1);
383445916cd2Sjpk 	}
383545916cd2Sjpk again:
383645916cd2Sjpk 	if (nzents == 0) {
383745916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
383845916cd2Sjpk 		return (-1);
383945916cd2Sjpk 	}
384045916cd2Sjpk 
384145916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
384245916cd2Sjpk 	if (zids == NULL) {
38433f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
384445916cd2Sjpk 		return (-1);
384545916cd2Sjpk 	}
384645916cd2Sjpk 	nzents_saved = nzents;
384745916cd2Sjpk 
384845916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
384945916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
385045916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
385145916cd2Sjpk 		free(zids);
385245916cd2Sjpk 		return (-1);
385345916cd2Sjpk 	}
385445916cd2Sjpk 	if (nzents != nzents_saved) {
385545916cd2Sjpk 		/* list changed, try again */
385645916cd2Sjpk 		free(zids);
385745916cd2Sjpk 		goto again;
385845916cd2Sjpk 	}
385945916cd2Sjpk 
386045916cd2Sjpk 	ip = getprivimplinfo();
386145916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
386245916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
386345916cd2Sjpk 		zonecfg_free_fs_option_list(
386445916cd2Sjpk 		    lower_fstab.zone_fs_options);
386545916cd2Sjpk 		free(zids);
386645916cd2Sjpk 		return (-1);
386745916cd2Sjpk 	}
386845916cd2Sjpk 
386945916cd2Sjpk 	for (i = 0; i < nzents; i++) {
387045916cd2Sjpk 		char zid_name[ZONENAME_MAX];
387145916cd2Sjpk 		zone_state_t zid_state;
387245916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
387345916cd2Sjpk 		struct stat stat_buf;
387445916cd2Sjpk 
387545916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
387645916cd2Sjpk 			continue;
387745916cd2Sjpk 
387845916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
387945916cd2Sjpk 			continue;
388045916cd2Sjpk 
388145916cd2Sjpk 		/*
388245916cd2Sjpk 		 * Do special setup for the zone we are booting
388345916cd2Sjpk 		 */
388445916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
388545916cd2Sjpk 			struct zone_fstab autofs_fstab;
388645916cd2Sjpk 			char map_path[MAXPATHLEN];
388745916cd2Sjpk 			int fd;
388845916cd2Sjpk 
388945916cd2Sjpk 			/*
389045916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
38919acbbeafSnn35248 			 * in the global zone. The non-global zone entry
389245916cd2Sjpk 			 * will be created by automount when the zone
389345916cd2Sjpk 			 * is booted.
389445916cd2Sjpk 			 */
389545916cd2Sjpk 
389645916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
389745916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
389845916cd2Sjpk 
389945916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
390045916cd2Sjpk 			    "/zone/%s/home", zid_name);
390145916cd2Sjpk 
390245916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
390345916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
390445916cd2Sjpk 			/*
390545916cd2Sjpk 			 * If the map file doesn't exist create a template
390645916cd2Sjpk 			 */
390745916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
390845916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
390945916cd2Sjpk 				int len;
391045916cd2Sjpk 				char map_rec[MAXPATHLEN];
391145916cd2Sjpk 
391245916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
391345916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
391445916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
391545916cd2Sjpk 				(void) write(fd, map_rec, len);
391645916cd2Sjpk 				(void) close(fd);
391745916cd2Sjpk 			}
391845916cd2Sjpk 
391945916cd2Sjpk 			/*
392045916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
392145916cd2Sjpk 			 * If it's already of type autofs, then
392245916cd2Sjpk 			 * don't mount it again.
392345916cd2Sjpk 			 */
392445916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
392545916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
392645916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
392745916cd2Sjpk 
392845916cd2Sjpk 				(void) make_one_dir(zlogp, "",
392927e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
393027e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
393145916cd2Sjpk 
393245916cd2Sjpk 				/*
393345916cd2Sjpk 				 * Mount will fail if automounter has already
393445916cd2Sjpk 				 * processed the auto_home_<zonename> map
393545916cd2Sjpk 				 */
393645916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
393745916cd2Sjpk 				    autofs_fstab.zone_fs_special,
393845916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
393945916cd2Sjpk 			}
394045916cd2Sjpk 			continue;
394145916cd2Sjpk 		}
394245916cd2Sjpk 
394345916cd2Sjpk 
394445916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
394548451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
394648451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
394745916cd2Sjpk 			/* Skip over zones without mounted filesystems */
394845916cd2Sjpk 			continue;
394945916cd2Sjpk 
395045916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
395145916cd2Sjpk 		    sizeof (m_label_t)) < 0)
395245916cd2Sjpk 			/* Skip over zones with unspecified label */
395345916cd2Sjpk 			continue;
395445916cd2Sjpk 
395545916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
395645916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
395745916cd2Sjpk 			/* Skip over zones with bad path */
395845916cd2Sjpk 			continue;
395945916cd2Sjpk 
396045916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
396145916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
396245916cd2Sjpk 			/* Skip over zones with bad privs */
396345916cd2Sjpk 			continue;
396445916cd2Sjpk 
396545916cd2Sjpk 		/*
396645916cd2Sjpk 		 * Reading down is valid according to our label model
396745916cd2Sjpk 		 * but some customers want to disable it because it
396845916cd2Sjpk 		 * allows execute down and other possible attacks.
396945916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
397045916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
397145916cd2Sjpk 		 * for NFS read-down semantics.
397245916cd2Sjpk 		 */
397345916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
397445916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
397545916cd2Sjpk 			/*
397645916cd2Sjpk 			 * Our zone dominates this one.
397745916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
397845916cd2Sjpk 			 */
397945916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
398045916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
398145916cd2Sjpk 
398245916cd2Sjpk 			/*
398345916cd2Sjpk 			 * If the target is already an LOFS mount
398445916cd2Sjpk 			 * then don't do it again.
398545916cd2Sjpk 			 */
398645916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
398745916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
398845916cd2Sjpk 
398945916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
399045916cd2Sjpk 				    MAXPATHLEN, "%s/export",
399145916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
399245916cd2Sjpk 					continue;
399345916cd2Sjpk 
399445916cd2Sjpk 				/*
399545916cd2Sjpk 				 * Make sure the lower-level home exists
399645916cd2Sjpk 				 */
399745916cd2Sjpk 				if (make_one_dir(zlogp,
399827e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
399927e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
400027e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
400145916cd2Sjpk 					continue;
400245916cd2Sjpk 
400345916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
400445916cd2Sjpk 				    "/home", MAXPATHLEN);
400545916cd2Sjpk 
400645916cd2Sjpk 				/*
400745916cd2Sjpk 				 * Mount can fail because the lower-level
400845916cd2Sjpk 				 * zone may have already done a mount up.
400945916cd2Sjpk 				 */
40100679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
40110679f794S 				    Z_MNT_BOOT);
401245916cd2Sjpk 			}
401345916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
401445916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
401545916cd2Sjpk 			/*
401645916cd2Sjpk 			 * This zone dominates our zone.
401745916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
401845916cd2Sjpk 			 */
401945916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
402045916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
402145916cd2Sjpk 			    zone_name) > MAXPATHLEN)
402245916cd2Sjpk 				continue;
402345916cd2Sjpk 
402445916cd2Sjpk 			/*
402545916cd2Sjpk 			 * If the target is already an LOFS mount
402645916cd2Sjpk 			 * then don't do it again.
402745916cd2Sjpk 			 */
402845916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
402945916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
403045916cd2Sjpk 
403145916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
403245916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
403345916cd2Sjpk 
403445916cd2Sjpk 				/*
403545916cd2Sjpk 				 * Mount can fail because the higher-level
403645916cd2Sjpk 				 * zone may have already done a mount down.
403745916cd2Sjpk 				 */
40380679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
40390679f794S 				    Z_MNT_BOOT);
404045916cd2Sjpk 			}
404145916cd2Sjpk 		}
404245916cd2Sjpk 	}
404345916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
404445916cd2Sjpk 	priv_freeset(zid_privs);
404545916cd2Sjpk 	free(zids);
404645916cd2Sjpk 
404745916cd2Sjpk 	/*
404845916cd2Sjpk 	 * Now share any exported directories from this zone.
404945916cd2Sjpk 	 * Each zone can have its own dfstab.
405045916cd2Sjpk 	 */
405145916cd2Sjpk 
405245916cd2Sjpk 	argv[0] = "zoneshare";
405345916cd2Sjpk 	argv[1] = "-z";
405445916cd2Sjpk 	argv[2] = zone_name;
405545916cd2Sjpk 	argv[3] = NULL;
405645916cd2Sjpk 
405745916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
405845916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
405945916cd2Sjpk 
406045916cd2Sjpk 	return (0);
406145916cd2Sjpk }
406245916cd2Sjpk 
406345916cd2Sjpk /*
406445916cd2Sjpk  * Unmount lofs mounts from higher level zones
406545916cd2Sjpk  * Unshare nfs exported directories
406645916cd2Sjpk  */
406745916cd2Sjpk static void
406845916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
406945916cd2Sjpk {
407045916cd2Sjpk 	zoneid_t *zids = NULL;
407145916cd2Sjpk 	uint_t nzents_saved;
407245916cd2Sjpk 	uint_t nzents;
407345916cd2Sjpk 	int i;
407445916cd2Sjpk 	char *argv[4];
407545916cd2Sjpk 	char path[MAXPATHLEN];
407645916cd2Sjpk 
407745916cd2Sjpk 	if (!is_system_labeled())
407845916cd2Sjpk 		return;
407945916cd2Sjpk 
408045916cd2Sjpk 	/*
408145916cd2Sjpk 	 * Get the list of zones from the kernel
408245916cd2Sjpk 	 */
408345916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
408445916cd2Sjpk 		return;
408545916cd2Sjpk 	}
408645916cd2Sjpk 
408745916cd2Sjpk 	if (zid_label == NULL) {
408845916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
408945916cd2Sjpk 		if (zid_label == NULL)
409045916cd2Sjpk 			return;
409145916cd2Sjpk 	}
409245916cd2Sjpk 
409345916cd2Sjpk again:
409445916cd2Sjpk 	if (nzents == 0)
409545916cd2Sjpk 		return;
409645916cd2Sjpk 
409745916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
409845916cd2Sjpk 	if (zids == NULL) {
40993f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
410045916cd2Sjpk 		return;
410145916cd2Sjpk 	}
410245916cd2Sjpk 	nzents_saved = nzents;
410345916cd2Sjpk 
410445916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
410545916cd2Sjpk 		free(zids);
410645916cd2Sjpk 		return;
410745916cd2Sjpk 	}
410845916cd2Sjpk 	if (nzents != nzents_saved) {
410945916cd2Sjpk 		/* list changed, try again */
411045916cd2Sjpk 		free(zids);
411145916cd2Sjpk 		goto again;
411245916cd2Sjpk 	}
411345916cd2Sjpk 
411445916cd2Sjpk 	for (i = 0; i < nzents; i++) {
411545916cd2Sjpk 		char zid_name[ZONENAME_MAX];
411645916cd2Sjpk 		zone_state_t zid_state;
411745916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
411845916cd2Sjpk 
411945916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
412045916cd2Sjpk 			continue;
412145916cd2Sjpk 
412245916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
412345916cd2Sjpk 			continue;
412445916cd2Sjpk 
412545916cd2Sjpk 		/*
412645916cd2Sjpk 		 * Skip the zone we are halting
412745916cd2Sjpk 		 */
412845916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
412945916cd2Sjpk 			continue;
413045916cd2Sjpk 
413145916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
413245916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
413345916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
413445916cd2Sjpk 			/* Skip over zones without mounted filesystems */
413545916cd2Sjpk 			continue;
413645916cd2Sjpk 
413745916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
413845916cd2Sjpk 		    sizeof (m_label_t)) < 0)
413945916cd2Sjpk 			/* Skip over zones with unspecified label */
414045916cd2Sjpk 			continue;
414145916cd2Sjpk 
414245916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
414345916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
414445916cd2Sjpk 			/* Skip over zones with bad path */
414545916cd2Sjpk 			continue;
414645916cd2Sjpk 
414745916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
414845916cd2Sjpk 			/*
414945916cd2Sjpk 			 * This zone dominates our zone.
415045916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
415145916cd2Sjpk 			 */
415245916cd2Sjpk 
415345916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
415445916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
415545916cd2Sjpk 			    zone_name) > MAXPATHLEN)
415645916cd2Sjpk 				continue;
415745916cd2Sjpk 
415845916cd2Sjpk 			/* Skip over mount failures */
415945916cd2Sjpk 			(void) umount(path);
416045916cd2Sjpk 		}
416145916cd2Sjpk 	}
416245916cd2Sjpk 	free(zids);
416345916cd2Sjpk 
416445916cd2Sjpk 	/*
416545916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
416645916cd2Sjpk 	 */
416745916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
416845916cd2Sjpk 	/* Skip over mount failures */
416945916cd2Sjpk 	(void) umount(path);
417045916cd2Sjpk 
417145916cd2Sjpk 	/*
417245916cd2Sjpk 	 * Next unshare any exported directories from this zone.
417345916cd2Sjpk 	 */
417445916cd2Sjpk 
417545916cd2Sjpk 	argv[0] = "zoneunshare";
417645916cd2Sjpk 	argv[1] = "-z";
417745916cd2Sjpk 	argv[2] = zone_name;
417845916cd2Sjpk 	argv[3] = NULL;
417945916cd2Sjpk 
418045916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
418145916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
418245916cd2Sjpk 
418345916cd2Sjpk 	/*
418445916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
418545916cd2Sjpk 	 */
418645916cd2Sjpk 
418745916cd2Sjpk 	argv[0] = "deallocate";
418845916cd2Sjpk 	argv[1] = "-Isz";
418945916cd2Sjpk 	argv[2] = zone_name;
419045916cd2Sjpk 	argv[3] = NULL;
419145916cd2Sjpk 
419245916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
419345916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
419445916cd2Sjpk }
419545916cd2Sjpk 
419645916cd2Sjpk /*
419745916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
419845916cd2Sjpk  * this zone.
419945916cd2Sjpk  */
420045916cd2Sjpk static tsol_zcent_t *
420145916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
420245916cd2Sjpk {
420345916cd2Sjpk 	FILE *fp;
420445916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
420545916cd2Sjpk 	char line[MAXTNZLEN];
420645916cd2Sjpk 
420745916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
420845916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
420945916cd2Sjpk 		return (NULL);
421045916cd2Sjpk 	}
421145916cd2Sjpk 
421245916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
421345916cd2Sjpk 		/*
421445916cd2Sjpk 		 * Check for malformed database
421545916cd2Sjpk 		 */
421645916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
421745916cd2Sjpk 			break;
421845916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
421945916cd2Sjpk 			continue;
422045916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
422145916cd2Sjpk 			break;
422245916cd2Sjpk 		tsol_freezcent(zcent);
422345916cd2Sjpk 		zcent = NULL;
422445916cd2Sjpk 	}
422545916cd2Sjpk 	(void) fclose(fp);
422645916cd2Sjpk 
422745916cd2Sjpk 	if (zcent == NULL) {
422845916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
422945916cd2Sjpk 		    "See tnzonecfg(4)");
423045916cd2Sjpk 	} else {
423145916cd2Sjpk 		if (zlabel == NULL)
423245916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
423345916cd2Sjpk 		/*
423445916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
423545916cd2Sjpk 		 */
423645916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
423745916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
423845916cd2Sjpk 			return (NULL);
423945916cd2Sjpk 		} else {
424045916cd2Sjpk 			priv_copyset(privs, zprivs);
424145916cd2Sjpk 		}
424245916cd2Sjpk 	}
424345916cd2Sjpk 	return (zcent);
424445916cd2Sjpk }
424545916cd2Sjpk 
424645916cd2Sjpk /*
424745916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
424845916cd2Sjpk  */
424945916cd2Sjpk static void
425045916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
425145916cd2Sjpk {
425245916cd2Sjpk 	tsol_mlp_t *mlp;
425345916cd2Sjpk 	tsol_mlpent_t tsme;
425445916cd2Sjpk 
425545916cd2Sjpk 	if (!is_system_labeled())
425645916cd2Sjpk 		return;
425745916cd2Sjpk 
425845916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
425945916cd2Sjpk 	tsme.tsme_flags = 0;
426045916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
426145916cd2Sjpk 		tsme.tsme_mlp = *mlp;
426245916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
426345916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
426445916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
426545916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
426645916cd2Sjpk 		}
426745916cd2Sjpk 	}
426845916cd2Sjpk 
426945916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
427045916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
427145916cd2Sjpk 		tsme.tsme_mlp = *mlp;
427245916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
427345916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
427445916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
427545916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
427645916cd2Sjpk 		}
427745916cd2Sjpk 	}
427845916cd2Sjpk }
427945916cd2Sjpk 
428045916cd2Sjpk static void
428145916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
428245916cd2Sjpk {
428345916cd2Sjpk 	tsol_mlpent_t tsme;
428445916cd2Sjpk 
428545916cd2Sjpk 	if (!is_system_labeled())
428645916cd2Sjpk 		return;
428745916cd2Sjpk 
428845916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
428945916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
429045916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
429145916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
429245916cd2Sjpk }
429345916cd2Sjpk 
42947c478bd9Sstevel@tonic-gate int
42950094b373Sjv227347 prtmount(const struct mnttab *fs, void *x) {
42960094b373Sjv227347 	zerror((zlog_t *)x, B_FALSE, "  %s", fs->mnt_mountp);
42977c478bd9Sstevel@tonic-gate 	return (0);
42987c478bd9Sstevel@tonic-gate }
42997c478bd9Sstevel@tonic-gate 
4300108322fbScarlsonj /*
4301108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
4302108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
4303108322fbScarlsonj  * is found or if we can't tell.
4304108322fbScarlsonj  */
4305108322fbScarlsonj static boolean_t
4306108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
43077c478bd9Sstevel@tonic-gate {
4308108322fbScarlsonj 	zoneid_t *zids = NULL;
4309108322fbScarlsonj 	uint_t nzids = 0;
4310108322fbScarlsonj 	boolean_t retv;
4311108322fbScarlsonj 	int rlen, zlen;
4312108322fbScarlsonj 	char zroot[MAXPATHLEN];
4313108322fbScarlsonj 	char zonename[ZONENAME_MAX];
4314108322fbScarlsonj 
4315108322fbScarlsonj 	for (;;) {
4316108322fbScarlsonj 		nzids += 10;
4317108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
4318108322fbScarlsonj 		if (zids == NULL) {
43193f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
4320108322fbScarlsonj 			return (B_TRUE);
4321108322fbScarlsonj 		}
4322108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
4323108322fbScarlsonj 			break;
4324108322fbScarlsonj 		free(zids);
4325108322fbScarlsonj 	}
4326108322fbScarlsonj 	retv = B_FALSE;
4327108322fbScarlsonj 	rlen = strlen(rootpath);
4328108322fbScarlsonj 	while (nzids > 0) {
4329108322fbScarlsonj 		/*
4330108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
4331108322fbScarlsonj 		 * while we were busy.
4332108322fbScarlsonj 		 */
4333108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4334108322fbScarlsonj 		    sizeof (zroot)) == -1)
4335108322fbScarlsonj 			continue;
4336108322fbScarlsonj 		zlen = strlen(zroot);
4337108322fbScarlsonj 		if (zlen > rlen)
4338108322fbScarlsonj 			zlen = rlen;
4339108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
4340108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4341108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4342108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
4343108322fbScarlsonj 			    sizeof (zonename)) == -1)
4344108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
4345108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
4346108322fbScarlsonj 			zerror(zlogp, B_FALSE,
4347108322fbScarlsonj 			    "zone root %s already in use by zone %s",
4348108322fbScarlsonj 			    rootpath, zonename);
4349108322fbScarlsonj 			retv = B_TRUE;
4350108322fbScarlsonj 			break;
4351108322fbScarlsonj 		}
4352108322fbScarlsonj 	}
4353108322fbScarlsonj 	free(zids);
4354108322fbScarlsonj 	return (retv);
4355108322fbScarlsonj }
4356108322fbScarlsonj 
4357108322fbScarlsonj /*
4358108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
4359108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
4360108322fbScarlsonj  */
4361108322fbScarlsonj static boolean_t
4362108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4363108322fbScarlsonj {
4364108322fbScarlsonj 	struct stat64 rst, zst;
4365108322fbScarlsonj 	struct mnttab *mnp;
4366108322fbScarlsonj 
4367108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
4368108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4369108322fbScarlsonj 		return (B_TRUE);
4370108322fbScarlsonj 	}
4371108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4372108322fbScarlsonj 		return (B_TRUE);
4373108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4374108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
4375108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4376108322fbScarlsonj 			continue;
4377108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
4378108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
4379108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
4380108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4381108322fbScarlsonj 			zerror(zlogp, B_FALSE,
4382108322fbScarlsonj 			    "zone root %s is reachable through %s",
4383108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
4384108322fbScarlsonj 			return (B_TRUE);
4385108322fbScarlsonj 		}
4386108322fbScarlsonj 	}
4387108322fbScarlsonj 	return (B_FALSE);
4388108322fbScarlsonj }
4389108322fbScarlsonj 
43900209230bSgjelinek /*
43910209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
43920209230bSgjelinek  * configuration.
43930209230bSgjelinek  */
43940209230bSgjelinek static int
43950209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
43960209230bSgjelinek {
43970209230bSgjelinek 	int res;
43980209230bSgjelinek 	uint64_t tmp;
43990209230bSgjelinek 	struct zone_mcaptab mcap;
44000209230bSgjelinek 	char sched[MAXNAMELEN];
44010209230bSgjelinek 	zone_dochandle_t handle = NULL;
44020209230bSgjelinek 	char pool_err[128];
44030209230bSgjelinek 
44040209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
44050209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
44060209230bSgjelinek 		return (Z_BAD_HANDLE);
44070209230bSgjelinek 	}
44080209230bSgjelinek 
44090209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
44100209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
44110209230bSgjelinek 		zonecfg_fini_handle(handle);
44120209230bSgjelinek 		return (res);
44130209230bSgjelinek 	}
44140209230bSgjelinek 
44150209230bSgjelinek 	/*
44160209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
44170209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
44180209230bSgjelinek 	 */
44190209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
44200209230bSgjelinek 		uint64_t num;
44210209230bSgjelinek 		char smf_err[128];
44220209230bSgjelinek 
44230209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
44240209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
44250209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
44260209230bSgjelinek 			zonecfg_fini_handle(handle);
44270209230bSgjelinek 			return (Z_INVAL);
44280209230bSgjelinek 		}
44290209230bSgjelinek 
44300209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
44310209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
44320209230bSgjelinek 			    "failed: %s", smf_err);
44330209230bSgjelinek 			zonecfg_fini_handle(handle);
44340209230bSgjelinek 			return (Z_INVAL);
44350209230bSgjelinek 		}
44360209230bSgjelinek 	}
44370209230bSgjelinek 
44380209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
44390209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
44400209230bSgjelinek 	    strlen(sched) > 0) {
44410209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
44420209230bSgjelinek 		    strlen(sched)) == -1)
44430209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
44440209230bSgjelinek 			    "default scheduling class");
44450209230bSgjelinek 
44460209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
44470209230bSgjelinek 	    == Z_OK) {
44480209230bSgjelinek 		/*
44490209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
44500209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
44510209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
44520209230bSgjelinek 		 * in by default so we can print a warning and modify the class
44530209230bSgjelinek 		 * if we wouldn't be using FSS.
44540209230bSgjelinek 		 */
44550209230bSgjelinek 		char class_name[PC_CLNMSZ];
44560209230bSgjelinek 
44570209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
44580209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
44590209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
44600209230bSgjelinek 			    "the zone's scheduling class");
44610209230bSgjelinek 
44620209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
44630209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
44640209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
44650209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
44660209230bSgjelinek 			    "used for processes\nin the zone but to get the "
44670209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
44680209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
44690209230bSgjelinek 			    "details.");
44700209230bSgjelinek 
44710209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
44720209230bSgjelinek 			    strlen("FSS")) == -1)
44730209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
44740209230bSgjelinek 				    "zone scheduling class to FSS");
44750209230bSgjelinek 		}
44760209230bSgjelinek 	}
44770209230bSgjelinek 
44780209230bSgjelinek 	/*
44790209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
44800209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
44810209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
44820209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
44830209230bSgjelinek 	 * and just return if there is nothing to do.
44840209230bSgjelinek 	 *
44850209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
44860209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
44870209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
44880209230bSgjelinek 	 * zonecfg.
44890209230bSgjelinek 	 */
44900209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
44910209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
44920209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
44930209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
44940209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
44950209230bSgjelinek 			    pool_err);
44960209230bSgjelinek 		else
44970209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
44980209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
44990209230bSgjelinek 		zonecfg_fini_handle(handle);
45000209230bSgjelinek 		return (Z_POOL_BIND);
45010209230bSgjelinek 	}
45020209230bSgjelinek 
45030209230bSgjelinek 	/*
45040209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
45050209230bSgjelinek 	 */
45060209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
45070209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
45080209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
45090209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
45100209230bSgjelinek 		    "processor allocation within the specified range\n"
45110209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
45120209230bSgjelinek 		    "See poold(1M).");
45130209230bSgjelinek 	}
45140209230bSgjelinek 
45150209230bSgjelinek 	/* The following is a warning, not an error. */
45160209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
45170209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
45180209230bSgjelinek 		if (res == Z_POOL_BIND)
45190209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
45200209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
45210209230bSgjelinek 		else if (res == Z_POOL)
45220209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
45230209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
45240209230bSgjelinek 		else
45250209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
45260209230bSgjelinek 			    zonecfg_strerror(res));
45270209230bSgjelinek 	}
4528efd4c9b6SSteve Lawrence 
4529efd4c9b6SSteve Lawrence 	/* Update saved pool name in case it has changed */
4530a3002e0aSGarrett D'Amore 	(void) zonecfg_get_poolname(handle, zone_name, pool_name,
4531a3002e0aSGarrett D'Amore 	    sizeof (pool_name));
45320209230bSgjelinek 
45330209230bSgjelinek 	zonecfg_fini_handle(handle);
45340209230bSgjelinek 	return (Z_OK);
45350209230bSgjelinek }
45360209230bSgjelinek 
45370fbb751dSJohn Levon static void
45380fbb751dSJohn Levon report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
45390fbb751dSJohn Levon {
45400fbb751dSJohn Levon 	switch (res) {
45410fbb751dSJohn Levon 	case Z_TOO_BIG:
45420fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value is too large.", name);
45430fbb751dSJohn Levon 		break;
45440fbb751dSJohn Levon 
45450fbb751dSJohn Levon 	case Z_INVALID_PROPERTY:
45460fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
45470fbb751dSJohn Levon 		    name, value);
45480fbb751dSJohn Levon 		break;
45490fbb751dSJohn Levon 
45500fbb751dSJohn Levon 	default:
45510fbb751dSJohn Levon 		zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
45520fbb751dSJohn Levon 		break;
45530fbb751dSJohn Levon 	}
45540fbb751dSJohn Levon }
45550fbb751dSJohn Levon 
45565679c89fSjv227347 /*
45575679c89fSjv227347  * Sets the hostid of the new zone based on its configured value.  The zone's
45585679c89fSjv227347  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
45595679c89fSjv227347  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
45605679c89fSjv227347  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
45615679c89fSjv227347  * ID of the new zone.
45625679c89fSjv227347  *
45635679c89fSjv227347  * This function returns zero on success and a nonzero error code on failure.
45645679c89fSjv227347  */
45655679c89fSjv227347 static int
45660fbb751dSJohn Levon setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
45675679c89fSjv227347 {
45685679c89fSjv227347 	int res;
45695679c89fSjv227347 	char hostidp[HW_HOSTID_LEN];
45705679c89fSjv227347 	unsigned int hostid;
45715679c89fSjv227347 
45720fbb751dSJohn Levon 	res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
45730fbb751dSJohn Levon 
45740fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
45750fbb751dSJohn Levon 		return (Z_OK);
45760fbb751dSJohn Levon 	} else if (res != Z_OK) {
45770fbb751dSJohn Levon 		report_prop_err(zlogp, "hostid", hostidp, res);
45780fbb751dSJohn Levon 		return (res);
45790fbb751dSJohn Levon 	}
45800fbb751dSJohn Levon 
45810fbb751dSJohn Levon 	hostid = (unsigned int)strtoul(hostidp, NULL, 16);
45820fbb751dSJohn Levon 	if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
45830fbb751dSJohn Levon 	    sizeof (hostid))) != 0) {
45840fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
45850fbb751dSJohn Levon 		    "zone hostid is not valid: %s: %d", hostidp, res);
45860fbb751dSJohn Levon 		return (Z_SYSTEM);
45870fbb751dSJohn Levon 	}
45880fbb751dSJohn Levon 
45890fbb751dSJohn Levon 	return (res);
45900fbb751dSJohn Levon }
45910fbb751dSJohn Levon 
45920fbb751dSJohn Levon static int
45930fbb751dSJohn Levon setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
45940fbb751dSJohn Levon {
4595*8cd81a20SJerry Jelinek 	char fsallowed[ZONE_FS_ALLOWED_MAX];
4596*8cd81a20SJerry Jelinek 	char *fsallowedp = fsallowed;
4597*8cd81a20SJerry Jelinek 	int len = sizeof (fsallowed);
45980fbb751dSJohn Levon 	int res;
45990fbb751dSJohn Levon 
4600*8cd81a20SJerry Jelinek 	res = zonecfg_get_fs_allowed(handle, fsallowed, len);
46010fbb751dSJohn Levon 
46020fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
4603*8cd81a20SJerry Jelinek 		/* No value, set the defaults */
4604*8cd81a20SJerry Jelinek 		(void) strlcpy(fsallowed, DFLT_FS_ALLOWED, len);
46050fbb751dSJohn Levon 	} else if (res != Z_OK) {
4606*8cd81a20SJerry Jelinek 		report_prop_err(zlogp, "fs-allowed", fsallowed, res);
46070fbb751dSJohn Levon 		return (res);
4608*8cd81a20SJerry Jelinek 	} else if (fsallowed[0] == '-') {
4609*8cd81a20SJerry Jelinek 		/* dropping default privs - use remaining list */
4610*8cd81a20SJerry Jelinek 		if (fsallowed[1] != ',')
4611*8cd81a20SJerry Jelinek 			return (Z_OK);
4612*8cd81a20SJerry Jelinek 		fsallowedp += 2;
4613*8cd81a20SJerry Jelinek 		len -= 2;
4614*8cd81a20SJerry Jelinek 	} else {
4615*8cd81a20SJerry Jelinek 		/* Has a value, append the defaults */
4616*8cd81a20SJerry Jelinek 		if (strlcat(fsallowed, ",", len) >= len ||
4617*8cd81a20SJerry Jelinek 		    strlcat(fsallowed, DFLT_FS_ALLOWED, len) >= len) {
4618*8cd81a20SJerry Jelinek 			report_prop_err(zlogp, "fs-allowed", fsallowed,
4619*8cd81a20SJerry Jelinek 			    Z_TOO_BIG);
4620*8cd81a20SJerry Jelinek 			return (Z_TOO_BIG);
4621*8cd81a20SJerry Jelinek 		}
46220fbb751dSJohn Levon 	}
46230fbb751dSJohn Levon 
4624*8cd81a20SJerry Jelinek 	if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, fsallowedp, len) != 0) {
46250fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
46260fbb751dSJohn Levon 		    "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
46270fbb751dSJohn Levon 		return (Z_SYSTEM);
46280fbb751dSJohn Levon 	}
46290fbb751dSJohn Levon 
4630*8cd81a20SJerry Jelinek 	return (Z_OK);
46310fbb751dSJohn Levon }
46320fbb751dSJohn Levon 
46330fbb751dSJohn Levon static int
46340fbb751dSJohn Levon setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
46350fbb751dSJohn Levon {
46360fbb751dSJohn Levon 	zone_dochandle_t handle;
46370fbb751dSJohn Levon 	int res = Z_OK;
46380fbb751dSJohn Levon 
46395679c89fSjv227347 	if ((handle = zonecfg_init_handle()) == NULL) {
46405679c89fSjv227347 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
46415679c89fSjv227347 		return (Z_BAD_HANDLE);
46425679c89fSjv227347 	}
46435679c89fSjv227347 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
46445679c89fSjv227347 		zerror(zlogp, B_FALSE, "invalid configuration");
46450fbb751dSJohn Levon 		goto out;
46465679c89fSjv227347 	}
46475679c89fSjv227347 
46480fbb751dSJohn Levon 	if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
46490fbb751dSJohn Levon 		goto out;
46500fbb751dSJohn Levon 
46510fbb751dSJohn Levon 	if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
46520fbb751dSJohn Levon 		goto out;
46530fbb751dSJohn Levon 
46540fbb751dSJohn Levon out:
46555679c89fSjv227347 	zonecfg_fini_handle(handle);
46565679c89fSjv227347 	return (res);
46575679c89fSjv227347 }
46585679c89fSjv227347 
4659108322fbScarlsonj zoneid_t
46606cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4661108322fbScarlsonj {
4662108322fbScarlsonj 	zoneid_t rval = -1;
46637c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
46647c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
46657c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
4666108322fbScarlsonj 	size_t rctlbufsz = 0;
4667fa9e4066Sahrens 	char *zfsbuf = NULL;
4668fa9e4066Sahrens 	size_t zfsbufsz = 0;
4669108322fbScarlsonj 	zoneid_t zoneid = -1;
46707c478bd9Sstevel@tonic-gate 	int xerr;
4671108322fbScarlsonj 	char *kzone;
4672108322fbScarlsonj 	FILE *fp = NULL;
467345916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
467445916cd2Sjpk 	int match = 0;
467545916cd2Sjpk 	int doi = 0;
4676f4b3ec61Sdh155122 	int flags;
4677f4b3ec61Sdh155122 	zone_iptype_t iptype;
46787c478bd9Sstevel@tonic-gate 
46797c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
46807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
46817c478bd9Sstevel@tonic-gate 		return (-1);
46827c478bd9Sstevel@tonic-gate 	}
4683108322fbScarlsonj 	if (zonecfg_in_alt_root())
4684108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
46857c478bd9Sstevel@tonic-gate 
46862b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
4687f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4688f4b3ec61Sdh155122 		return (-1);
4689f4b3ec61Sdh155122 	}
4690f4b3ec61Sdh155122 	switch (iptype) {
4691f4b3ec61Sdh155122 	case ZS_SHARED:
4692f4b3ec61Sdh155122 		flags = 0;
4693f4b3ec61Sdh155122 		break;
4694f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
4695f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
4696f4b3ec61Sdh155122 		break;
4697f4b3ec61Sdh155122 	}
4698f4b3ec61Sdh155122 
46997c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
47007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
47017c478bd9Sstevel@tonic-gate 		return (-1);
47027c478bd9Sstevel@tonic-gate 	}
47037c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4704ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
47057c478bd9Sstevel@tonic-gate 		goto error;
4706ffbafc53Scomay 
47076cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
47086cfd72c6Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
47097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
47107c478bd9Sstevel@tonic-gate 		goto error;
47117c478bd9Sstevel@tonic-gate 	}
4712ffbafc53Scomay 
4713fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4714fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4715fa9e4066Sahrens 		goto error;
4716fa9e4066Sahrens 	}
47177c478bd9Sstevel@tonic-gate 
47186cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
471945916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
472048451833Scarlsonj 		if (zcent != NULL) {
472145916cd2Sjpk 			match = zcent->zc_match;
472245916cd2Sjpk 			doi = zcent->zc_doi;
472345916cd2Sjpk 			*zlabel = zcent->zc_label;
472445916cd2Sjpk 		} else {
472545916cd2Sjpk 			goto error;
472645916cd2Sjpk 		}
47274201a95eSRic Aleshire 		if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
47284201a95eSRic Aleshire 			goto error;
472945916cd2Sjpk 	}
473045916cd2Sjpk 
4731108322fbScarlsonj 	kzone = zone_name;
4732108322fbScarlsonj 
4733108322fbScarlsonj 	/*
4734108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4735108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4736108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4737108322fbScarlsonj 	 * that use this same source node (same device and inode).
4738108322fbScarlsonj 	 */
4739108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4740108322fbScarlsonj 		goto error;
4741108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4742108322fbScarlsonj 		goto error;
4743108322fbScarlsonj 
47446cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4745108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4746108322fbScarlsonj 
4747108322fbScarlsonj 		/*
4748108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4749108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4750108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4751108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4752108322fbScarlsonj 		 * executables.
4753108322fbScarlsonj 		 *
4754108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4755108322fbScarlsonj 		 */
4756108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4757108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4758108322fbScarlsonj 			goto error;
4759108322fbScarlsonj 		}
4760108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4761108322fbScarlsonj 			goto error;
4762108322fbScarlsonj 	}
4763108322fbScarlsonj 
4764108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4765108322fbScarlsonj 		/*
4766108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4767108322fbScarlsonj 		 * then we have some additional work to do before starting the
4768108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4769108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4770108322fbScarlsonj 		 * the zone.
4771108322fbScarlsonj 		 */
4772108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4773108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4774108322fbScarlsonj 			goto error;
4775108322fbScarlsonj 		}
4776108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4777108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4778108322fbScarlsonj 			goto error;
4779108322fbScarlsonj 		}
4780108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4781108322fbScarlsonj 		    NULL, 0) == 0) {
4782108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4783108322fbScarlsonj 			goto error;
4784108322fbScarlsonj 		}
4785108322fbScarlsonj 		/* This is the preferred name */
4786108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4787108322fbScarlsonj 		    zone_name);
4788108322fbScarlsonj 		srandom(getpid());
4789108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4790108322fbScarlsonj 		    0) == 0) {
4791108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4792108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4793108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4794108322fbScarlsonj 		}
4795108322fbScarlsonj 		kzone = kernzone;
4796108322fbScarlsonj 	}
4797108322fbScarlsonj 
47987c478bd9Sstevel@tonic-gate 	xerr = 0;
4799108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4800f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4801f4b3ec61Sdh155122 	    flags)) == -1) {
48027c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
48037c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
48047c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
48057c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
48067c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
48077c478bd9Sstevel@tonic-gate 			} else {
48087c478bd9Sstevel@tonic-gate 
48097c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
48107c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
48117c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
48127c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
48137c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
48147c478bd9Sstevel@tonic-gate 			}
48157c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
48167c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
48177c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
48187c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
48198c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
48208c55461bSton 			char zonename[ZONENAME_MAX];
48218c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
48228c55461bSton 			    zonename, ZONENAME_MAX);
48238c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
48248c55461bSton 			    "used by the zone '%s'.", zonename);
48257c478bd9Sstevel@tonic-gate 		} else {
48267c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
48277c478bd9Sstevel@tonic-gate 		}
48287c478bd9Sstevel@tonic-gate 		goto error;
48297c478bd9Sstevel@tonic-gate 	}
4830108322fbScarlsonj 
4831108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4832108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4833108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4834108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4835108322fbScarlsonj 		goto error;
4836108322fbScarlsonj 	}
4837108322fbScarlsonj 
48380679f794S 	/*
48390679f794S 	 * The following actions are not performed when merely mounting a zone
48400679f794S 	 * for administrative use.
48410679f794S 	 */
48420679f794S 	if (mount_cmd == Z_MNT_BOOT) {
48430679f794S 		brand_handle_t bh;
48440679f794S 		struct brand_attr attr;
48450679f794S 		char modname[MAXPATHLEN];
48460679f794S 
48470fbb751dSJohn Levon 		if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
48485679c89fSjv227347 			goto error;
48495679c89fSjv227347 
485062868012SSteve Lawrence 		if ((bh = brand_open(brand_name)) == NULL) {
48510679f794S 			zerror(zlogp, B_FALSE,
48520679f794S 			    "unable to determine brand name");
48535679c89fSjv227347 			goto error;
48549acbbeafSnn35248 		}
48559acbbeafSnn35248 
48569a5d73e0SRic Aleshire 		if (!is_system_labeled() &&
485762868012SSteve Lawrence 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
48589a5d73e0SRic Aleshire 			brand_close(bh);
48599a5d73e0SRic Aleshire 			zerror(zlogp, B_FALSE,
48609a5d73e0SRic Aleshire 			    "cannot boot labeled zone on unlabeled system");
48619a5d73e0SRic Aleshire 			goto error;
48629a5d73e0SRic Aleshire 		}
48639a5d73e0SRic Aleshire 
48649acbbeafSnn35248 		/*
48659acbbeafSnn35248 		 * If this brand requires any kernel support, now is the time to
48669acbbeafSnn35248 		 * get it loaded and initialized.
48679acbbeafSnn35248 		 */
4868123807fbSedp 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4869e767a340Sgjelinek 			brand_close(bh);
48700679f794S 			zerror(zlogp, B_FALSE,
48710679f794S 			    "unable to determine brand kernel module");
48725679c89fSjv227347 			goto error;
48739acbbeafSnn35248 		}
4874e767a340Sgjelinek 		brand_close(bh);
48759acbbeafSnn35248 
48769acbbeafSnn35248 		if (strlen(modname) > 0) {
487762868012SSteve Lawrence 			(void) strlcpy(attr.ba_brandname, brand_name,
487862868012SSteve Lawrence 			    sizeof (attr.ba_brandname));
487962868012SSteve Lawrence 			(void) strlcpy(attr.ba_modname, modname,
488062868012SSteve Lawrence 			    sizeof (attr.ba_modname));
48819acbbeafSnn35248 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
48829acbbeafSnn35248 			    sizeof (attr) != 0)) {
48830679f794S 				zerror(zlogp, B_TRUE,
48840679f794S 				    "could not set zone brand attribute.");
48859acbbeafSnn35248 				goto error;
48869acbbeafSnn35248 			}
48879acbbeafSnn35248 		}
48889acbbeafSnn35248 
48895679c89fSjv227347 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
48900209230bSgjelinek 			goto error;
48910209230bSgjelinek 
489245916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
48930209230bSgjelinek 	}
48940209230bSgjelinek 
4895108322fbScarlsonj 	rval = zoneid;
4896108322fbScarlsonj 	zoneid = -1;
4897108322fbScarlsonj 
48987c478bd9Sstevel@tonic-gate error:
48995679c89fSjv227347 	if (zoneid != -1) {
49005679c89fSjv227347 		(void) zone_shutdown(zoneid);
4901108322fbScarlsonj 		(void) zone_destroy(zoneid);
49025679c89fSjv227347 	}
49037c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
49047c478bd9Sstevel@tonic-gate 		free(rctlbuf);
49057c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
4906108322fbScarlsonj 	if (fp != NULL)
4907108322fbScarlsonj 		zonecfg_close_scratch(fp);
4908108322fbScarlsonj 	lofs_discard_mnttab();
490945916cd2Sjpk 	if (zcent != NULL)
491045916cd2Sjpk 		tsol_freezcent(zcent);
49117c478bd9Sstevel@tonic-gate 	return (rval);
49127c478bd9Sstevel@tonic-gate }
49137c478bd9Sstevel@tonic-gate 
4914555afedfScarlsonj /*
4915555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
4916555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4917555afedfScarlsonj  * details from inside the zone.
4918555afedfScarlsonj  */
4919555afedfScarlsonj static void
4920555afedfScarlsonj write_index_file(zoneid_t zoneid)
4921555afedfScarlsonj {
4922555afedfScarlsonj 	FILE *zef;
4923555afedfScarlsonj 	FILE *zet;
4924555afedfScarlsonj 	struct zoneent *zep;
4925555afedfScarlsonj 	pid_t child;
4926555afedfScarlsonj 	int tmpl_fd;
4927555afedfScarlsonj 	ctid_t ct;
4928555afedfScarlsonj 	int fd;
4929555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4930555afedfScarlsonj 
4931555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
4932555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
4933555afedfScarlsonj 		return;
4934555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
4935555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
4936555afedfScarlsonj 			break;
4937555afedfScarlsonj 		free(zep);
4938555afedfScarlsonj 	}
4939555afedfScarlsonj 	endzoneent(zef);
4940555afedfScarlsonj 	if (zep == NULL)
4941555afedfScarlsonj 		return;
4942555afedfScarlsonj 
4943555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
4944555afedfScarlsonj 		free(zep);
4945555afedfScarlsonj 		return;
4946555afedfScarlsonj 	}
4947555afedfScarlsonj 
4948555afedfScarlsonj 	if ((child = fork()) == -1) {
4949555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4950555afedfScarlsonj 		(void) close(tmpl_fd);
4951555afedfScarlsonj 		free(zep);
4952555afedfScarlsonj 		return;
4953555afedfScarlsonj 	}
4954555afedfScarlsonj 
4955555afedfScarlsonj 	/* parent waits for child to finish */
4956555afedfScarlsonj 	if (child != 0) {
4957555afedfScarlsonj 		free(zep);
4958555afedfScarlsonj 		if (contract_latest(&ct) == -1)
4959555afedfScarlsonj 			ct = -1;
4960555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4961555afedfScarlsonj 		(void) close(tmpl_fd);
4962555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
4963555afedfScarlsonj 		(void) contract_abandon_id(ct);
4964555afedfScarlsonj 		return;
4965555afedfScarlsonj 	}
4966555afedfScarlsonj 
4967555afedfScarlsonj 	/* child enters zone and sets up index file */
4968555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
4969555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
4970555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4971555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4972555afedfScarlsonj 		    ZONE_CONFIG_GID);
4973555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4974555afedfScarlsonj 		    ZONE_INDEX_MODE);
4975555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4976555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4977555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
4978555afedfScarlsonj 				uuidstr[0] = '\0';
4979555afedfScarlsonj 			else
4980555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
4981555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4982555afedfScarlsonj 			    zone_state_str(zep->zone_state),
4983555afedfScarlsonj 			    uuidstr);
4984555afedfScarlsonj 			(void) fclose(zet);
4985555afedfScarlsonj 		}
4986555afedfScarlsonj 	}
4987555afedfScarlsonj 	_exit(0);
4988555afedfScarlsonj }
4989555afedfScarlsonj 
49907c478bd9Sstevel@tonic-gate int
49916cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
49927c478bd9Sstevel@tonic-gate {
49939acbbeafSnn35248 	char zonepath[MAXPATHLEN];
49945749802bSdp 
49956cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4996fa9e4066Sahrens 		lofs_discard_mnttab();
4997fa9e4066Sahrens 		return (-1);
4998fa9e4066Sahrens 	}
4999fa9e4066Sahrens 
50009acbbeafSnn35248 	/*
50019acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
50029acbbeafSnn35248 	 * attribute backing store for /dev
50039acbbeafSnn35248 	 */
50049acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
50059acbbeafSnn35248 		lofs_discard_mnttab();
50069acbbeafSnn35248 		return (-1);
50079acbbeafSnn35248 	}
500816bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
500927e6fb21Sdp 
501027e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
501127e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
501227e6fb21Sdp 	    0, 3) != 0) {
5013108322fbScarlsonj 		lofs_discard_mnttab();
50147c478bd9Sstevel@tonic-gate 		return (-1);
5015108322fbScarlsonj 	}
5016facf4a8dSllai1 
50179acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
5018facf4a8dSllai1 		lofs_discard_mnttab();
5019facf4a8dSllai1 		return (-1);
5020facf4a8dSllai1 	}
5021facf4a8dSllai1 
50226cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
5023f4b3ec61Sdh155122 		zone_iptype_t iptype;
5024f4b3ec61Sdh155122 
50252b24ab6bSSebastien Roy 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
5026f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
5027108322fbScarlsonj 			lofs_discard_mnttab();
50287c478bd9Sstevel@tonic-gate 			return (-1);
5029108322fbScarlsonj 		}
5030555afedfScarlsonj 
5031f4b3ec61Sdh155122 		switch (iptype) {
5032f4b3ec61Sdh155122 		case ZS_SHARED:
5033f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
5034f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
5035f4b3ec61Sdh155122 				lofs_discard_mnttab();
5036f4b3ec61Sdh155122 				return (-1);
5037f4b3ec61Sdh155122 			}
5038f4b3ec61Sdh155122 			break;
5039f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
5040550b6e40SSowmini Varadhan 			if (configure_exclusive_network_interfaces(zlogp,
5041550b6e40SSowmini Varadhan 			    zoneid) !=
5042f4b3ec61Sdh155122 			    0) {
5043f4b3ec61Sdh155122 				lofs_discard_mnttab();
5044f4b3ec61Sdh155122 				return (-1);
5045f4b3ec61Sdh155122 			}
5046f4b3ec61Sdh155122 			break;
5047f4b3ec61Sdh155122 		}
5048f4b3ec61Sdh155122 	}
5049f4b3ec61Sdh155122 
5050555afedfScarlsonj 	write_index_file(zoneid);
5051555afedfScarlsonj 
5052108322fbScarlsonj 	lofs_discard_mnttab();
50537c478bd9Sstevel@tonic-gate 	return (0);
50547c478bd9Sstevel@tonic-gate }
50557c478bd9Sstevel@tonic-gate 
5056108322fbScarlsonj static int
5057108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
50587c478bd9Sstevel@tonic-gate {
5059108322fbScarlsonj 	char zroot[MAXPATHLEN];
5060108322fbScarlsonj 
5061108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5062108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
5063108322fbScarlsonj 		return (-1);
5064108322fbScarlsonj 	}
5065108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
5066108322fbScarlsonj 
5067108322fbScarlsonj 	/*
5068108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
5069108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
5070108322fbScarlsonj 	 * still be creds floating about in the system that reference the
5071108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
5072108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
5073108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
5074108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
5075108322fbScarlsonj 	 */
5076108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
5077108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
5078108322fbScarlsonj 			goto unmounted;
5079108322fbScarlsonj 		if (errno == EBUSY) {
5080108322fbScarlsonj 			int tries = 10;
5081108322fbScarlsonj 
5082108322fbScarlsonj 			while (--tries >= 0) {
5083108322fbScarlsonj 				(void) sleep(1);
5084108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
5085108322fbScarlsonj 					goto unmounted;
5086108322fbScarlsonj 				if (errno != EBUSY)
5087108322fbScarlsonj 					break;
5088108322fbScarlsonj 			}
5089108322fbScarlsonj 		}
5090108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
5091108322fbScarlsonj 		return (-1);
5092108322fbScarlsonj 	}
5093108322fbScarlsonj unmounted:
5094108322fbScarlsonj 
5095108322fbScarlsonj 	/*
5096108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
5097108322fbScarlsonj 	 * entries.
5098108322fbScarlsonj 	 */
5099108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5100108322fbScarlsonj 		FILE *fp;
5101108322fbScarlsonj 		int retv;
5102108322fbScarlsonj 
5103108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5104108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
5105108322fbScarlsonj 			return (-1);
5106108322fbScarlsonj 		}
5107108322fbScarlsonj 		retv = -1;
5108108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
5109108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
5110108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
5111108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
5112108322fbScarlsonj 		else
5113108322fbScarlsonj 			retv = 0;
5114108322fbScarlsonj 		zonecfg_close_scratch(fp);
5115108322fbScarlsonj 		return (retv);
5116108322fbScarlsonj 	} else {
5117108322fbScarlsonj 		return (0);
5118108322fbScarlsonj 	}
5119108322fbScarlsonj }
5120108322fbScarlsonj 
5121108322fbScarlsonj int
51220209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
5123108322fbScarlsonj {
5124108322fbScarlsonj 	char *kzone;
51257c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
51260209230bSgjelinek 	int res;
51270209230bSgjelinek 	char pool_err[128];
5128ff17c8bfSgjelinek 	char zpath[MAXPATHLEN];
51299acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
5130123807fbSedp 	brand_handle_t bh = NULL;
51312b24ab6bSSebastien Roy 	dladm_status_t status;
51322b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
5133f4b3ec61Sdh155122 	ushort_t flags;
51347c478bd9Sstevel@tonic-gate 
5135108322fbScarlsonj 	kzone = zone_name;
5136108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5137108322fbScarlsonj 		FILE *fp;
5138108322fbScarlsonj 
5139108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5140108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
5141108322fbScarlsonj 			goto error;
5142108322fbScarlsonj 		}
5143108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5144108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
5145108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
5146108322fbScarlsonj 			zonecfg_close_scratch(fp);
5147108322fbScarlsonj 			goto error;
5148108322fbScarlsonj 		}
5149108322fbScarlsonj 		zonecfg_close_scratch(fp);
5150108322fbScarlsonj 		kzone = kernzone;
5151108322fbScarlsonj 	}
5152108322fbScarlsonj 
5153108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
51547c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
51557c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
5156108322fbScarlsonj 		if (unmount_cmd)
5157108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
51587c478bd9Sstevel@tonic-gate 		goto error;
51597c478bd9Sstevel@tonic-gate 	}
51607c478bd9Sstevel@tonic-gate 
51610dc2366fSVenugopal Iyer 	if (remove_datalink_pool(zlogp, zoneid) != 0) {
51620dc2366fSVenugopal Iyer 		zerror(zlogp, B_FALSE, "unable clear datalink pool property");
51630dc2366fSVenugopal Iyer 		goto error;
51640dc2366fSVenugopal Iyer 	}
51650dc2366fSVenugopal Iyer 
5166550b6e40SSowmini Varadhan 	if (remove_datalink_protect(zlogp, zoneid) != 0) {
5167550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE,
5168550b6e40SSowmini Varadhan 		    "unable clear datalink protect property");
5169550b6e40SSowmini Varadhan 		goto error;
5170550b6e40SSowmini Varadhan 	}
5171550b6e40SSowmini Varadhan 
5172550b6e40SSowmini Varadhan 	/*
5173550b6e40SSowmini Varadhan 	 * The datalinks assigned to the zone will be removed from the NGZ as
5174550b6e40SSowmini Varadhan 	 * part of zone_shutdown() so that we need to remove protect/pool etc.
5175550b6e40SSowmini Varadhan 	 * before zone_shutdown(). Even if the shutdown itself fails, the zone
5176550b6e40SSowmini Varadhan 	 * will not be able to violate any constraints applied because the
5177550b6e40SSowmini Varadhan 	 * datalinks are no longer available to the zone.
5178550b6e40SSowmini Varadhan 	 */
51797c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
51807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
51817c478bd9Sstevel@tonic-gate 		goto error;
51827c478bd9Sstevel@tonic-gate 	}
51837c478bd9Sstevel@tonic-gate 
5184ff17c8bfSgjelinek 	/* Get the zonepath of this zone */
5185ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
5186ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
51879acbbeafSnn35248 		goto error;
51889acbbeafSnn35248 	}
51899acbbeafSnn35248 
51909acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
519162868012SSteve Lawrence 	if ((bh = brand_open(brand_name)) == NULL) {
51929acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
51939acbbeafSnn35248 		return (-1);
51949acbbeafSnn35248 	}
51959acbbeafSnn35248 	/*
51969acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
51979acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
51989acbbeafSnn35248 	 */
51999acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
5200ff17c8bfSgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
5201ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
5202123807fbSedp 		brand_close(bh);
52039acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
52049acbbeafSnn35248 		    "halt callback.");
52059acbbeafSnn35248 		goto error;
52069acbbeafSnn35248 	}
5207123807fbSedp 	brand_close(bh);
52089acbbeafSnn35248 
52099acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
5210c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
52119acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
52129acbbeafSnn35248 		goto error;
52139acbbeafSnn35248 	}
52149acbbeafSnn35248 
5215f4b3ec61Sdh155122 	if (!unmount_cmd) {
5216f4b3ec61Sdh155122 		zone_iptype_t iptype;
5217f4b3ec61Sdh155122 
5218f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
5219f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
52202b24ab6bSSebastien Roy 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
5221f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
5222f4b3ec61Sdh155122 				    "ip-type");
52237c478bd9Sstevel@tonic-gate 				goto error;
52247c478bd9Sstevel@tonic-gate 			}
5225f4b3ec61Sdh155122 		} else {
5226f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
5227f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
5228f4b3ec61Sdh155122 			else
5229f4b3ec61Sdh155122 				iptype = ZS_SHARED;
5230f4b3ec61Sdh155122 		}
5231f4b3ec61Sdh155122 
5232f4b3ec61Sdh155122 		switch (iptype) {
5233f4b3ec61Sdh155122 		case ZS_SHARED:
5234f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
5235f4b3ec61Sdh155122 			    zoneid) != 0) {
5236f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
5237f4b3ec61Sdh155122 				    "network interfaces in zone");
5238f4b3ec61Sdh155122 				goto error;
5239f4b3ec61Sdh155122 			}
5240f4b3ec61Sdh155122 			break;
5241f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
5242f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
5243f4b3ec61Sdh155122 			    zoneid) != 0) {
5244f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
5245f4b3ec61Sdh155122 				    "network interfaces in zone");
5246f4b3ec61Sdh155122 				goto error;
5247f4b3ec61Sdh155122 			}
52482b24ab6bSSebastien Roy 			status = dladm_zone_halt(dld_handle, zoneid);
52492b24ab6bSSebastien Roy 			if (status != DLADM_STATUS_OK) {
52502b24ab6bSSebastien Roy 				zerror(zlogp, B_FALSE, "unable to notify "
52512b24ab6bSSebastien Roy 				    "dlmgmtd of zone halt: %s",
52522b24ab6bSSebastien Roy 				    dladm_status2str(status, errmsg));
52532b24ab6bSSebastien Roy 			}
5254f4b3ec61Sdh155122 			break;
5255f4b3ec61Sdh155122 		}
5256f4b3ec61Sdh155122 	}
52577c478bd9Sstevel@tonic-gate 
5258108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
52597c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
52607c478bd9Sstevel@tonic-gate 		goto error;
52617c478bd9Sstevel@tonic-gate 	}
52627c478bd9Sstevel@tonic-gate 
5263108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
52647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
52657c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
52667c478bd9Sstevel@tonic-gate 		goto error;
52677c478bd9Sstevel@tonic-gate 	}
52687c478bd9Sstevel@tonic-gate 
52690209230bSgjelinek 	/*
5270cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
5271cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
5272cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
5273cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
5274cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
5275cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
5276cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
5277cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
52780209230bSgjelinek 	 */
5279cf6b13d2Sgjelinek 	if (!unmount_cmd) {
5280cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
5281cf6b13d2Sgjelinek 
5282cf6b13d2Sgjelinek 		if (rebooting) {
5283cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
5284cf6b13d2Sgjelinek 			zone_dochandle_t handle;
5285cf6b13d2Sgjelinek 
5286cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
5287cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
5288cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
5289cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
5290e767a340Sgjelinek 
5291e767a340Sgjelinek 			zonecfg_fini_handle(handle);
5292cf6b13d2Sgjelinek 		}
5293cf6b13d2Sgjelinek 
5294cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
52950209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
52960209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
52970209230bSgjelinek 				if (res == Z_POOL)
52980209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
52990209230bSgjelinek 			}
53000209230bSgjelinek 		}
5301cf6b13d2Sgjelinek 	}
53020209230bSgjelinek 
530345916cd2Sjpk 	remove_mlps(zlogp, zoneid);
530445916cd2Sjpk 
53057c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
53067c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
53077c478bd9Sstevel@tonic-gate 		goto error;
53087c478bd9Sstevel@tonic-gate 	}
5309108322fbScarlsonj 
5310108322fbScarlsonj 	/*
5311108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
5312108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
5313108322fbScarlsonj 	 */
5314108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5315108322fbScarlsonj 		goto error;
5316108322fbScarlsonj 
5317108322fbScarlsonj 	lofs_discard_mnttab();
53187c478bd9Sstevel@tonic-gate 	return (0);
53197c478bd9Sstevel@tonic-gate 
53207c478bd9Sstevel@tonic-gate error:
5321108322fbScarlsonj 	lofs_discard_mnttab();
53227c478bd9Sstevel@tonic-gate 	return (-1);
53237c478bd9Sstevel@tonic-gate }
5324