xref: /titanic_53/usr/src/cmd/zoneadmd/vplat.c (revision a3002e0aebb2c7fba71fcbbba97ad98ab8340dc4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
230fbb751dSJohn Levon  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*a3002e0aSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
28*a3002e0aSGarrett D'Amore  */
29*a3002e0aSGarrett D'Amore 
30*a3002e0aSGarrett D'Amore /*
317c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
327c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
337c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
347c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
357c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
367c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
377c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
38108322fbScarlsonj  *
39108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
40108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
41108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
42108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
43108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
44108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
45108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
46108322fbScarlsonj  * without relying on any binaries in the zone itself.
47108322fbScarlsonj  *
48108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
49108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
50108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
51108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
52108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
53108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
54108322fbScarlsonj  * process.
55108322fbScarlsonj  *
56108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
57108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
58108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
59108322fbScarlsonj  * $ZONEPATH/lu as the root.
60108322fbScarlsonj  *
61108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
62108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
63108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
64108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <sys/param.h>
687c478bd9Sstevel@tonic-gate #include <sys/mount.h>
697c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
707c478bd9Sstevel@tonic-gate #include <sys/socket.h>
717c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
727c478bd9Sstevel@tonic-gate #include <sys/types.h>
737c478bd9Sstevel@tonic-gate #include <sys/stat.h>
747c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
757c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
767c478bd9Sstevel@tonic-gate #include <sys/conf.h>
775679c89fSjv227347 #include <sys/systeminfo.h>
787c478bd9Sstevel@tonic-gate 
79f4b3ec61Sdh155122 #include <libdlpi.h>
80f595a68aSyz147064 #include <libdllink.h>
81d62bc4baSyz147064 #include <libdlvlan.h>
82f4b3ec61Sdh155122 
837c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
847c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
857c478bd9Sstevel@tonic-gate #include <netinet/in.h>
867c478bd9Sstevel@tonic-gate #include <net/route.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #include <stdio.h>
897c478bd9Sstevel@tonic-gate #include <errno.h>
907c478bd9Sstevel@tonic-gate #include <fcntl.h>
917c478bd9Sstevel@tonic-gate #include <unistd.h>
927c478bd9Sstevel@tonic-gate #include <rctl.h>
937c478bd9Sstevel@tonic-gate #include <stdlib.h>
947c478bd9Sstevel@tonic-gate #include <string.h>
957c478bd9Sstevel@tonic-gate #include <strings.h>
967c478bd9Sstevel@tonic-gate #include <wait.h>
977c478bd9Sstevel@tonic-gate #include <limits.h>
987c478bd9Sstevel@tonic-gate #include <libgen.h>
99fa9e4066Sahrens #include <libzfs.h>
100facf4a8dSllai1 #include <libdevinfo.h>
1017c478bd9Sstevel@tonic-gate #include <zone.h>
1027c478bd9Sstevel@tonic-gate #include <assert.h>
103555afedfScarlsonj #include <libcontract.h>
104555afedfScarlsonj #include <libcontract_priv.h>
105555afedfScarlsonj #include <uuid/uuid.h>
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1087c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1097c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1107c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
111fa9e4066Sahrens #include <sys/fs/zfs.h>
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #include <pool.h>
1147c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1150209230bSgjelinek #include <sys/priocntl.h>
1167c478bd9Sstevel@tonic-gate 
1179acbbeafSnn35248 #include <libbrand.h>
1189acbbeafSnn35248 #include <sys/brand.h>
1197c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
12039d3e169Sevanl #include <synch.h>
12122321485Svp157776 
1227c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
12345916cd2Sjpk #include <tsol/label.h>
12445916cd2Sjpk #include <libtsnet.h>
12545916cd2Sjpk #include <sys/priv.h>
126550b6e40SSowmini Varadhan #include <libinetutil.h>
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1297c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1307c478bd9Sstevel@tonic-gate 
1316e1ae2a3SGary Pennington #define	RESOURCE_DEFAULT_OPTS \
1327c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13545916cd2Sjpk #define	MAXTNZLEN	2048
1367c478bd9Sstevel@tonic-gate 
1376cfd72c6Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1386cfd72c6Sgjelinek 
139ff19e029SMenno Lageman /* a reasonable estimate for the number of lwps per process */
140ff19e029SMenno Lageman #define	LWPS_PER_PROCESS	10
141ff19e029SMenno Lageman 
1427c478bd9Sstevel@tonic-gate /* for routing socket */
1437c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1447c478bd9Sstevel@tonic-gate 
145108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
146108322fbScarlsonj static char kernzone[ZONENAME_MAX];
147108322fbScarlsonj 
148108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
149108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
150108322fbScarlsonj 
15145916cd2Sjpk /* for Trusted Extensions */
15245916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
15345916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
15445916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
1559d48116cSdh155122 
15645916cd2Sjpk static m_label_t *zlabel = NULL;
15745916cd2Sjpk static m_label_t *zid_label = NULL;
15845916cd2Sjpk static priv_set_t *zprivs = NULL;
15945916cd2Sjpk 
1607c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1617c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1627c478bd9Sstevel@tonic-gate 
163c5cd6260S /* from zoneadmd */
164c5cd6260S extern char query_hook[];
165c5cd6260S 
1667c478bd9Sstevel@tonic-gate /*
167550b6e40SSowmini Varadhan  * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
168550b6e40SSowmini Varadhan  * node in a linked list that is sorted by linkid.  The list is constructed as
169550b6e40SSowmini Varadhan  * the xml configuration file is parsed, and the information
170550b6e40SSowmini Varadhan  * contained in each node is added to the kernel before the zone is
171550b6e40SSowmini Varadhan  * booted, to be retrieved and applied from within the exclusive-IP NGZ
172550b6e40SSowmini Varadhan  * on boot.
173550b6e40SSowmini Varadhan  */
174550b6e40SSowmini Varadhan typedef struct zone_addr_list {
175550b6e40SSowmini Varadhan 	struct zone_addr_list *za_next;
176550b6e40SSowmini Varadhan 	datalink_id_t za_linkid;	/* datalink_id_t of interface */
177550b6e40SSowmini Varadhan 	struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
178550b6e40SSowmini Varadhan } zone_addr_list_t;
179550b6e40SSowmini Varadhan 
180550b6e40SSowmini Varadhan /*
181108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
182108322fbScarlsonj  * data) only once every N times through the loop.
183108322fbScarlsonj  */
184108322fbScarlsonj #define	MNTTAB_HUNK	32
185108322fbScarlsonj 
186550b6e40SSowmini Varadhan /* some handy macros */
187550b6e40SSowmini Varadhan #define	SIN(s)	((struct sockaddr_in *)s)
188550b6e40SSowmini Varadhan #define	SIN6(s)	((struct sockaddr_in6 *)s)
189550b6e40SSowmini Varadhan 
190108322fbScarlsonj /*
1917c478bd9Sstevel@tonic-gate  * Private autofs system call
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static int
1967c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
204108322fbScarlsonj static void
205108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
206108322fbScarlsonj {
207108322fbScarlsonj 	uint_t i;
208108322fbScarlsonj 
209108322fbScarlsonj 	if (mnt_array == NULL)
210108322fbScarlsonj 		return;
211108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
212108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
213108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
214108322fbScarlsonj 		free(mnt_array[i].mnt_special);
215108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
216108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
217108322fbScarlsonj 	}
218108322fbScarlsonj 	free(mnt_array);
219108322fbScarlsonj }
220108322fbScarlsonj 
221108322fbScarlsonj /*
222108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
223108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
224108322fbScarlsonj  * array in "nelemp".
225108322fbScarlsonj  */
226108322fbScarlsonj static int
227108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
228108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
229108322fbScarlsonj {
230108322fbScarlsonj 	struct mnttab mnt;
231108322fbScarlsonj 	struct mnttab *mnts;
232108322fbScarlsonj 	struct mnttab *mnp;
233108322fbScarlsonj 	uint_t nmnt;
234108322fbScarlsonj 
235108322fbScarlsonj 	rewind(mnttab);
236108322fbScarlsonj 	resetmnttab(mnttab);
237108322fbScarlsonj 	nmnt = 0;
238108322fbScarlsonj 	mnts = NULL;
239108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
240108322fbScarlsonj 		struct mnttab *tmp_array;
241108322fbScarlsonj 
242108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
243108322fbScarlsonj 			continue;
244108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
245108322fbScarlsonj 			tmp_array = realloc(mnts,
246108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
247108322fbScarlsonj 			if (tmp_array == NULL) {
248108322fbScarlsonj 				free_mnttable(mnts, nmnt);
249108322fbScarlsonj 				return (-1);
250108322fbScarlsonj 			}
251108322fbScarlsonj 			mnts = tmp_array;
252108322fbScarlsonj 		}
253108322fbScarlsonj 		mnp = &mnts[nmnt++];
254108322fbScarlsonj 
255108322fbScarlsonj 		/*
256108322fbScarlsonj 		 * Zero out any fields we're not using.
257108322fbScarlsonj 		 */
258108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
259108322fbScarlsonj 
260108322fbScarlsonj 		if (mnt.mnt_special != NULL)
261108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
262108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
263108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
264108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
265108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
266108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
267108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
268108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
269108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
270108322fbScarlsonj 			free_mnttable(mnts, nmnt);
271108322fbScarlsonj 			return (-1);
272108322fbScarlsonj 		}
273108322fbScarlsonj 	}
274108322fbScarlsonj 	*mnt_arrayp = mnts;
275108322fbScarlsonj 	*nelemp = nmnt;
276108322fbScarlsonj 	return (0);
277108322fbScarlsonj }
278108322fbScarlsonj 
279108322fbScarlsonj /*
280108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
281108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
282108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
283108322fbScarlsonj  * reread of the list of mount points
284108322fbScarlsonj  */
285108322fbScarlsonj static void
286108322fbScarlsonj lofs_discard_mnttab(void)
287108322fbScarlsonj {
288108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
289108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
290108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
291108322fbScarlsonj }
292108322fbScarlsonj 
293108322fbScarlsonj static int
294108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
295108322fbScarlsonj {
296108322fbScarlsonj 	FILE *mnttab;
297108322fbScarlsonj 	uint_t nmnts;
298108322fbScarlsonj 
299108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
300108322fbScarlsonj 		return (-1);
301108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
302108322fbScarlsonj 	    &nmnts) == -1) {
303108322fbScarlsonj 		(void) fclose(mnttab);
304108322fbScarlsonj 		return (-1);
305108322fbScarlsonj 	}
306108322fbScarlsonj 	(void) fclose(mnttab);
307108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
308108322fbScarlsonj 	return (0);
309108322fbScarlsonj }
310108322fbScarlsonj 
311108322fbScarlsonj /*
312108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
313108322fbScarlsonj  * path and resolves them all down to an absolute path.
314108322fbScarlsonj  */
315910f48daSedp void
316108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
317108322fbScarlsonj {
318108322fbScarlsonj 	int len, arlen;
319108322fbScarlsonj 	const char *altroot;
320108322fbScarlsonj 	char tmppath[MAXPATHLEN];
321108322fbScarlsonj 	boolean_t outside_altroot;
322108322fbScarlsonj 
323108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
324108322fbScarlsonj 		return;
325108322fbScarlsonj 	tmppath[len] = '\0';
326108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
327108322fbScarlsonj 
328108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
329108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
330108322fbScarlsonj 		return;
331108322fbScarlsonj 
332108322fbScarlsonj 	altroot = zonecfg_get_root();
333108322fbScarlsonj 	arlen = strlen(altroot);
334108322fbScarlsonj 	outside_altroot = B_FALSE;
335108322fbScarlsonj 	for (;;) {
336108322fbScarlsonj 		struct mnttab *mnp;
337108322fbScarlsonj 
3381e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3391e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3401e9d8f9fSdminer 		    mnp--) {
341108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
342108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3431e9d8f9fSdminer 			    mnp->mnt_special == NULL)
344108322fbScarlsonj 				continue;
345108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
346108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
347108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
348108322fbScarlsonj 				break;
349108322fbScarlsonj 		}
3501e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3511e9d8f9fSdminer 			break;
3521e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3531e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
354108322fbScarlsonj 			break;
355108322fbScarlsonj 		if (outside_altroot) {
356108322fbScarlsonj 			char *cp;
357108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
358108322fbScarlsonj 
359108322fbScarlsonj 			/*
360108322fbScarlsonj 			 * If we run into a read-only mount outside of the
361108322fbScarlsonj 			 * alternate root environment, then the user doesn't
362108322fbScarlsonj 			 * want this path to be made read-write.
363108322fbScarlsonj 			 */
364108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
365108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
366108322fbScarlsonj 			    NULL &&
367108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
368108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
369108322fbScarlsonj 				break;
370108322fbScarlsonj 			}
371108322fbScarlsonj 		} else if (arlen > 0 &&
372108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
373108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
374108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
375108322fbScarlsonj 			outside_altroot = B_TRUE;
376108322fbScarlsonj 		}
377108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
378108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
379108322fbScarlsonj 		    mnp->mnt_special, path + len);
380108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
381108322fbScarlsonj 			break;
382108322fbScarlsonj 		path[len] = '\0';
383108322fbScarlsonj 	}
384108322fbScarlsonj }
385108322fbScarlsonj 
386108322fbScarlsonj /*
387108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
388108322fbScarlsonj  * referenced device is already mounted somewhere.
389108322fbScarlsonj  */
390108322fbScarlsonj static int
391108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
392108322fbScarlsonj {
393108322fbScarlsonj 	struct mnttab *mnp;
394108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
395108322fbScarlsonj 
396108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
397108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
398108322fbScarlsonj 		return (-1);
399108322fbScarlsonj 
400108322fbScarlsonj 	/*
401108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
402108322fbScarlsonj 	 * no need to worry about conflicting mounts.
403108322fbScarlsonj 	 */
404108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
405108322fbScarlsonj 	    mnp++) {
406108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
407108322fbScarlsonj 			break;
408108322fbScarlsonj 	}
409108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
410108322fbScarlsonj 		return (0);
411108322fbScarlsonj 
412108322fbScarlsonj 	/*
413108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
414108322fbScarlsonj 	 */
415108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
416108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
417108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
418108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
419108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
420108322fbScarlsonj 
421108322fbScarlsonj 	/*
4226e1ae2a3SGary Pennington 	 * Discard all but one of the original options and set that to our
4236e1ae2a3SGary Pennington 	 * default set of options used for resources.
424108322fbScarlsonj 	 */
425108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
426108322fbScarlsonj 	if (optptr == NULL) {
427108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
428108322fbScarlsonj 		if (optptr == NULL) {
429108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
430108322fbScarlsonj 			    fsptr->zone_fs_dir);
431108322fbScarlsonj 			return (-1);
432108322fbScarlsonj 		}
433108322fbScarlsonj 	} else {
434108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
435108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
436108322fbScarlsonj 			free(onext);
437108322fbScarlsonj 		}
438108322fbScarlsonj 	}
4396e1ae2a3SGary Pennington 	(void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
440108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
441108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
442108322fbScarlsonj 	return (0);
443108322fbScarlsonj }
444108322fbScarlsonj 
445d314f035Sedp int
44627e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
44727e6fb21Sdp     uid_t userid, gid_t groupid)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4507c478bd9Sstevel@tonic-gate 	struct stat st;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4537c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4557c478bd9Sstevel@tonic-gate 		    subdir);
4567c478bd9Sstevel@tonic-gate 		return (-1);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4607c478bd9Sstevel@tonic-gate 		/*
4617c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4627c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4637c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4647c478bd9Sstevel@tonic-gate 		 */
4657c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
466756cf395SRic Aleshire 			if (S_ISREG(st.st_mode)) {
46745916cd2Sjpk 				/*
468756cf395SRic Aleshire 				 * Allow readonly mounts of /etc/ files; this
469756cf395SRic Aleshire 				 * is needed most by Trusted Extensions.
47045916cd2Sjpk 				 */
47145916cd2Sjpk 				if (strncmp(subdir, "/etc/",
47245916cd2Sjpk 				    strlen("/etc/")) != 0) {
47345916cd2Sjpk 					zerror(zlogp, B_FALSE,
47445916cd2Sjpk 					    "%s is not in /etc", path);
4757c478bd9Sstevel@tonic-gate 					return (-1);
4767c478bd9Sstevel@tonic-gate 				}
47745916cd2Sjpk 			} else {
47845916cd2Sjpk 				zerror(zlogp, B_FALSE,
47945916cd2Sjpk 				    "%s is not a directory", path);
48045916cd2Sjpk 				return (-1);
48145916cd2Sjpk 			}
48245916cd2Sjpk 		}
48327e6fb21Sdp 		return (0);
48427e6fb21Sdp 	}
48527e6fb21Sdp 
48627e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
4877c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4887c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4897c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4907c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4917c478bd9Sstevel@tonic-gate 		else
4927c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4937c478bd9Sstevel@tonic-gate 		return (-1);
4947c478bd9Sstevel@tonic-gate 	}
49527e6fb21Sdp 
49627e6fb21Sdp 	(void) chown(path, userid, groupid);
4977c478bd9Sstevel@tonic-gate 	return (0);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate static void
5017c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	uint_t i;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (types == NULL)
5067c478bd9Sstevel@tonic-gate 		return;
5077c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
5087c478bd9Sstevel@tonic-gate 		free(types[i]);
5097c478bd9Sstevel@tonic-gate 	free(types);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate static char **
5137c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	char **types = NULL;
5167c478bd9Sstevel@tonic-gate 	FILE *fp;
5177c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
5187c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
5197c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
5207c478bd9Sstevel@tonic-gate 	uint_t i;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5247c478bd9Sstevel@tonic-gate 		return (NULL);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	/*
5277c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5307c478bd9Sstevel@tonic-gate 		lines++;
5317c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5327c478bd9Sstevel@tonic-gate 		goto out;
5337c478bd9Sstevel@tonic-gate 	rewind(fp);
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5367c478bd9Sstevel@tonic-gate 	 */
5377c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5387c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5397c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5407c478bd9Sstevel@tonic-gate 		goto out;
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	i = 0;
5437c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5447c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5457c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5467c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5477c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5487c478bd9Sstevel@tonic-gate 			types = NULL;
5497c478bd9Sstevel@tonic-gate 			goto out;
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5527c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5537c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5547c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5557c478bd9Sstevel@tonic-gate 			types = NULL;
5567c478bd9Sstevel@tonic-gate 			goto out;
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 		i++;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate out:
5617c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5627c478bd9Sstevel@tonic-gate 	return (types);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate static boolean_t
5667c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	uint_t i;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5717c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5727c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5737c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5747c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
579108322fbScarlsonj /*
580108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
581108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
582108322fbScarlsonj  */
5837c478bd9Sstevel@tonic-gate static void
584108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5857c478bd9Sstevel@tonic-gate {
586108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
587108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
588108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5957c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5967c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5977c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5987c478bd9Sstevel@tonic-gate  *
5997c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
6007c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
6017c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
6027c478bd9Sstevel@tonic-gate  *
6037c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
6047c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
6057c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
6067c478bd9Sstevel@tonic-gate  *
6077c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
6087c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
6097c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
6107c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
6117c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
6127c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
6137c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
6147c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
6157c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
6167c478bd9Sstevel@tonic-gate  * graceful mounts first.
6177c478bd9Sstevel@tonic-gate  *
6187c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate static int
621108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	int error = 0;
6247c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6257c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6267c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6277c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6287c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6297c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6307c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6317c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6347c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6357c478bd9Sstevel@tonic-gate 		return (-1);
6367c478bd9Sstevel@tonic-gate 	}
637108322fbScarlsonj 	if (unmount_cmd)
638108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6417c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6427c478bd9Sstevel@tonic-gate 
64345916cd2Sjpk 	/*
64445916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
64545916cd2Sjpk 	 * of our zone's /export/home
64645916cd2Sjpk 	 */
64748451833Scarlsonj 	if (!unmount_cmd)
64845916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
64945916cd2Sjpk 
6507c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6517c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6527c478bd9Sstevel@tonic-gate 		return (-1);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6567c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6577c478bd9Sstevel@tonic-gate 	 */
6587c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6597c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6607c478bd9Sstevel@tonic-gate 		error++;
6617c478bd9Sstevel@tonic-gate 		goto out;
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/*
6657c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6667c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6677c478bd9Sstevel@tonic-gate 	 */
6687c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6697c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6707c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6717c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6727c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6737c478bd9Sstevel@tonic-gate 		char *path;
6747c478bd9Sstevel@tonic-gate 		uint_t i;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		mnts = NULL;
6777c478bd9Sstevel@tonic-gate 		nmnt = 0;
6787c478bd9Sstevel@tonic-gate 		/*
6797c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6807c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6817c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6827c478bd9Sstevel@tonic-gate 		 * filesystems.
6837c478bd9Sstevel@tonic-gate 		 */
6847c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6857c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6867c478bd9Sstevel@tonic-gate 			error++;
6877c478bd9Sstevel@tonic-gate 			goto out;
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6907c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6917c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6927c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6937c478bd9Sstevel@tonic-gate 			/*
6947c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6957c478bd9Sstevel@tonic-gate 			 *
6967c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6977c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6987c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
6997c478bd9Sstevel@tonic-gate 			 */
7007c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
7017c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
7027c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7037c478bd9Sstevel@tonic-gate 			}
7047c478bd9Sstevel@tonic-gate 			/*
7057c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
7067c478bd9Sstevel@tonic-gate 			 */
7077c478bd9Sstevel@tonic-gate 			if (stuck) {
7087c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
7097c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7107c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
7117c478bd9Sstevel@tonic-gate 				} else {
7127c478bd9Sstevel@tonic-gate 					/*
7137c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
7147c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
7157c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
7167c478bd9Sstevel@tonic-gate 					 * bail.
7177c478bd9Sstevel@tonic-gate 					 */
7187c478bd9Sstevel@tonic-gate 					error++;
7197c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
7207c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
7217c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
7227c478bd9Sstevel@tonic-gate 					goto out;
7237c478bd9Sstevel@tonic-gate 				}
7247c478bd9Sstevel@tonic-gate 			}
7257c478bd9Sstevel@tonic-gate 			/*
7267c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7277c478bd9Sstevel@tonic-gate 			 */
7287c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7297c478bd9Sstevel@tonic-gate 				newcount++;
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7347c478bd9Sstevel@tonic-gate 			break;
7357c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7367c478bd9Sstevel@tonic-gate 			/*
7377c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7387c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7397c478bd9Sstevel@tonic-gate 			 */
7407c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		/*
7457c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7467c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7477c478bd9Sstevel@tonic-gate 		 */
7487c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7497c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7507c478bd9Sstevel@tonic-gate 			error++;
7517c478bd9Sstevel@tonic-gate 			goto out;
7527c478bd9Sstevel@tonic-gate 		}
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate out:
7567c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7577c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7587c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate static int
7627c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7657c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate /*
7717c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7727c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7737c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7747c478bd9Sstevel@tonic-gate  *
7757c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7767c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7777c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7787c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7797c478bd9Sstevel@tonic-gate  * remains the case.
7807c478bd9Sstevel@tonic-gate  */
7817c478bd9Sstevel@tonic-gate static int
7827c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7857c478bd9Sstevel@tonic-gate 	int child_status = 0;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/*
7887c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7897c478bd9Sstevel@tonic-gate 	 */
7907c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7917c478bd9Sstevel@tonic-gate 	child_pid = fork();
7927c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7937c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
7947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7957c478bd9Sstevel@tonic-gate 		return (-1);
7967c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
7977c478bd9Sstevel@tonic-gate 		closefrom(0);
7981390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7991390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
8001390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
8011390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
8027c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
8037c478bd9Sstevel@tonic-gate 		/*
8047c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
8057c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
8067c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
8077c478bd9Sstevel@tonic-gate 		 * accordingly.
8087c478bd9Sstevel@tonic-gate 		 */
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
8117c478bd9Sstevel@tonic-gate 	} else {
8127c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
8167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8177c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
8187c478bd9Sstevel@tonic-gate 		return (-1);
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
8217c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
8227c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8237c478bd9Sstevel@tonic-gate 		return (-1);
8247c478bd9Sstevel@tonic-gate 	}
8257c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate static int
82993239addSjohnlev isregfile(const char *path)
83093239addSjohnlev {
83193239addSjohnlev 	struct stat64 st;
83293239addSjohnlev 
83393239addSjohnlev 	if (stat64(path, &st) == -1)
83493239addSjohnlev 		return (-1);
83593239addSjohnlev 
83693239addSjohnlev 	return (S_ISREG(st.st_mode));
83793239addSjohnlev }
83893239addSjohnlev 
83993239addSjohnlev static int
8407c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
84357a250fbSbatschul 	char *argv[5];
8447c478bd9Sstevel@tonic-gate 	int status;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8487c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8497c478bd9Sstevel@tonic-gate 	 */
8507c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8519acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8527c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8537c478bd9Sstevel@tonic-gate 		return (-1);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
85693239addSjohnlev 	/*
85793239addSjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
85893239addSjohnlev 	 * in zoneadm.
85993239addSjohnlev 	 */
86093239addSjohnlev 	if (isregfile(cmdbuf) == -1)
86193239addSjohnlev 		return (0);
86293239addSjohnlev 
8637c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
86457a250fbSbatschul 	argv[1] = "-o";
86557a250fbSbatschul 	argv[2] = "p";
86657a250fbSbatschul 	argv[3] = (char *)rawdev;
86757a250fbSbatschul 	argv[4] = NULL;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8707c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8717c478bd9Sstevel@tonic-gate 		return (status);
8727c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8737c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8747c478bd9Sstevel@tonic-gate 	return (-1);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate static int
8787c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8797c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8827c478bd9Sstevel@tonic-gate 	char *argv[6];
8837c478bd9Sstevel@tonic-gate 	int status;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/*
8867c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8877c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8887c478bd9Sstevel@tonic-gate 	 */
8897c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8909acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8917c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8927c478bd9Sstevel@tonic-gate 		return (-1);
8937c478bd9Sstevel@tonic-gate 	}
8947c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
8957c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
8967c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
8977c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
8987c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
8997c478bd9Sstevel@tonic-gate 	} else {
9007c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
9017c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
9027c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
9037c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
9047c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
9087c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
9097c478bd9Sstevel@tonic-gate 		return (status);
9107c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
9117c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
9127c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9137c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
9147c478bd9Sstevel@tonic-gate 	else
9157c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
9167c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9177c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
9187c478bd9Sstevel@tonic-gate 	return (-1);
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate /*
922d314f035Sedp  * Check if a given mount point path exists.
923d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
924d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
925d314f035Sedp  * component of the mount point path, so it must be a directory.
926d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
927d314f035Sedp  * path, so the mount point itself can be anything aside from a
928d314f035Sedp  * symbolic link.
929d314f035Sedp  *
930d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
931d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
932d314f035Sedp  * If the path doesn't exist return a positive value.
9337c478bd9Sstevel@tonic-gate  */
9347c478bd9Sstevel@tonic-gate static int
935d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9387c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9397c478bd9Sstevel@tonic-gate 	int res;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9427c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
943d314f035Sedp 			return (1);
9447c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9457c478bd9Sstevel@tonic-gate 		return (-1);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9487c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9497c478bd9Sstevel@tonic-gate 		return (-1);
9507c478bd9Sstevel@tonic-gate 	}
951d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9527c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9537c478bd9Sstevel@tonic-gate 		return (-1);
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9567c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9577c478bd9Sstevel@tonic-gate 		return (-1);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9607c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9617c478bd9Sstevel@tonic-gate 		/*
962d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9637c478bd9Sstevel@tonic-gate 		 */
9647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9657c478bd9Sstevel@tonic-gate 		return (-1);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 	return (0);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /*
971d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
972d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
973d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
974d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
975d314f035Sedp  * object.  (Different filesystems can support different mount points,
976d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
977d314f035Sedp  * while "ufs" just supports directories.)
9787c478bd9Sstevel@tonic-gate  *
979d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
980d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
981d314f035Sedp  * If the path doesn't exist return a positive value.
9827c478bd9Sstevel@tonic-gate  */
983d314f035Sedp int
984d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
985d314f035Sedp     const char *dir, const char *fstype)
9867c478bd9Sstevel@tonic-gate {
987d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
988d314f035Sedp 	int rv;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/*
991d314f035Sedp 	 * Sanity check the target mount point path.
992d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
9937c478bd9Sstevel@tonic-gate 	 */
994d314f035Sedp 	if (dir[0] != '/') {
995d314f035Sedp 		/* Something went wrong. */
996d314f035Sedp 		zerror(zlogp, B_FALSE, "invalid mount directory, "
997d314f035Sedp 		    "type: \"%s\", special: \"%s\", dir: \"%s\"",
998d314f035Sedp 		    fstype, spec, dir);
999d314f035Sedp 		return (-1);
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 
1002d314f035Sedp 	/*
1003d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
1004d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
1005d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
1006d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
1007d314f035Sedp 	 * cannonical path check in valid_mount_point()).
1008d314f035Sedp 	 */
1009d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
1010d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
1011d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
1012d314f035Sedp 		    rootpath, dir);
1013d314f035Sedp 		return (-1);
1014d314f035Sedp 	}
1015d314f035Sedp 
1016d314f035Sedp 	/*
1017d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
1018d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
1019d314f035Sedp 	 */
10207c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10217c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10227c478bd9Sstevel@tonic-gate 	do {
1023d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
10247c478bd9Sstevel@tonic-gate 		*slashp = '\0';
1025d314f035Sedp 		if (slashp_next != NULL) {
1026d314f035Sedp 			/* This is an intermediary mount path component. */
1027d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1028d314f035Sedp 		} else {
1029d314f035Sedp 			/* This is the last component of the mount path. */
1030d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1031d314f035Sedp 		}
1032d314f035Sedp 		if (rv < 0)
1033d314f035Sedp 			return (rv);
10347c478bd9Sstevel@tonic-gate 		*slashp = '/';
1035d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1036d314f035Sedp 	return (rv);
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate static int
10409acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10419acbbeafSnn35248 {
10429acbbeafSnn35248 	di_prof_t prof = arg;
10439acbbeafSnn35248 
10449acbbeafSnn35248 	if (name == NULL)
10459acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
10469acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
10479acbbeafSnn35248 }
10489acbbeafSnn35248 
10499acbbeafSnn35248 static int
10509acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10519acbbeafSnn35248 {
10529acbbeafSnn35248 	di_prof_t prof = arg;
10539acbbeafSnn35248 
10549acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
10559acbbeafSnn35248 }
10569acbbeafSnn35248 
10572b24ab6bSSebastien Roy int
10582b24ab6bSSebastien Roy vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1059f4b3ec61Sdh155122 {
1060f4b3ec61Sdh155122 	zone_dochandle_t handle;
1061f4b3ec61Sdh155122 
1062f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
1063f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1064f4b3ec61Sdh155122 		return (-1);
1065f4b3ec61Sdh155122 	}
1066f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1067f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
1068f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1069f4b3ec61Sdh155122 		return (-1);
1070f4b3ec61Sdh155122 	}
1071f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1072f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1073f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1074f4b3ec61Sdh155122 		return (-1);
1075f4b3ec61Sdh155122 	}
1076f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1077f4b3ec61Sdh155122 	return (0);
1078f4b3ec61Sdh155122 }
1079f4b3ec61Sdh155122 
10809acbbeafSnn35248 /*
10819acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10829acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10839acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10849acbbeafSnn35248  */
10859acbbeafSnn35248 static int
10860679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10879acbbeafSnn35248 {
10889acbbeafSnn35248 	char			brand[MAXNAMELEN];
10899acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1090123807fbSedp 	brand_handle_t		bh = NULL;
10919acbbeafSnn35248 	struct zone_devtab	ztab;
10929acbbeafSnn35248 	di_prof_t		prof = NULL;
10939acbbeafSnn35248 	int			err;
10949acbbeafSnn35248 	int			retval = -1;
1095f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1096f4b3ec61Sdh155122 	const char 		*curr_iptype;
10979acbbeafSnn35248 
10989acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
10999acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
11009acbbeafSnn35248 		goto cleanup;
11019acbbeafSnn35248 	}
11029acbbeafSnn35248 
11030679f794S 	/*
11040679f794S 	 * Get a handle to the brand info for this zone.
1105e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
11060679f794S 	 * brand device mounts.
11070679f794S 	 */
11080679f794S 	if (ALT_MOUNT(mount_cmd)) {
1109e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
11100679f794S 	} else {
111162868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
11120679f794S 	}
11130679f794S 
11140679f794S 	if ((bh = brand_open(brand)) == NULL) {
11159acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
11169acbbeafSnn35248 		goto cleanup;
11179acbbeafSnn35248 	}
11189acbbeafSnn35248 
11192b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
1120f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1121f4b3ec61Sdh155122 		goto cleanup;
1122f4b3ec61Sdh155122 	}
1123f4b3ec61Sdh155122 	switch (iptype) {
1124f4b3ec61Sdh155122 	case ZS_SHARED:
1125f4b3ec61Sdh155122 		curr_iptype = "shared";
1126f4b3ec61Sdh155122 		break;
1127f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1128f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1129f4b3ec61Sdh155122 		break;
1130f4b3ec61Sdh155122 	}
1131f4b3ec61Sdh155122 
1132123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1133f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11349acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11359acbbeafSnn35248 		goto cleanup;
11369acbbeafSnn35248 	}
11379acbbeafSnn35248 
1138123807fbSedp 	if (brand_platform_iter_link(bh,
11399acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11409acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11419acbbeafSnn35248 		goto cleanup;
11429acbbeafSnn35248 	}
11439acbbeafSnn35248 
11449acbbeafSnn35248 	/* Add user-specified devices and directories */
11459acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11469acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11479acbbeafSnn35248 		goto cleanup;
11489acbbeafSnn35248 	}
11499acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11509acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11519acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11529acbbeafSnn35248 		goto cleanup;
11539acbbeafSnn35248 	}
11549acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
11559acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11569acbbeafSnn35248 		    zonecfg_strerror(err));
11579acbbeafSnn35248 		goto cleanup;
11589acbbeafSnn35248 	}
11599acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11609acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11619acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
11629acbbeafSnn35248 			    "user-specified device");
11639acbbeafSnn35248 			goto cleanup;
11649acbbeafSnn35248 		}
11659acbbeafSnn35248 	}
11669acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
11679acbbeafSnn35248 
11689acbbeafSnn35248 	/* Send profile to kernel */
11699acbbeafSnn35248 	if (di_prof_commit(prof)) {
11709acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11719acbbeafSnn35248 		goto cleanup;
11729acbbeafSnn35248 	}
11739acbbeafSnn35248 
11749acbbeafSnn35248 	retval = 0;
11759acbbeafSnn35248 
11769acbbeafSnn35248 cleanup:
1177123807fbSedp 	if (bh != NULL)
1178123807fbSedp 		brand_close(bh);
1179e767a340Sgjelinek 	if (handle != NULL)
11809acbbeafSnn35248 		zonecfg_fini_handle(handle);
11819acbbeafSnn35248 	if (prof)
11829acbbeafSnn35248 		di_prof_fini(prof);
11839acbbeafSnn35248 	return (retval);
11849acbbeafSnn35248 }
11859acbbeafSnn35248 
11869acbbeafSnn35248 static int
11870679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11880679f794S     zone_mnt_t mount_cmd)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
11917c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11927c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11939acbbeafSnn35248 	int rv;
11947c478bd9Sstevel@tonic-gate 
1195d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1196d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11987c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11997c478bd9Sstevel@tonic-gate 		return (-1);
1200d314f035Sedp 	} else if (rv > 0) {
1201d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1202d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1203d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1204d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1205d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1206d314f035Sedp 			return (-1);
12077c478bd9Sstevel@tonic-gate 		}
12087c478bd9Sstevel@tonic-gate 
1209d314f035Sedp 		/*
1210d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1211d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1212d314f035Sedp 		 * to make sure that the mount point path is canonical,
1213d314f035Sedp 		 * which it can only do if the path exists, so now that
1214d314f035Sedp 		 * we've created the path we have to verify it again.
1215d314f035Sedp 		 */
1216d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1217d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1218d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1219d314f035Sedp 			zerror(zlogp, B_FALSE,
1220d314f035Sedp 			    "%s%s is not a valid mount point",
1221d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
12227c478bd9Sstevel@tonic-gate 			return (-1);
1223d314f035Sedp 		}
1224d314f035Sedp 	}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12277c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	/*
12307c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12317c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12327c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12337c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12347c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12357c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12367c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12377c478bd9Sstevel@tonic-gate 	 */
12387c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12397c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12407c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12417c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12427c478bd9Sstevel@tonic-gate 		return (-1);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/*
1246108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1247fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1248fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1249fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1250fa3d7ae1Sedp 	 * time we get here.
1251108322fbScarlsonj 	 */
1252108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1253108322fbScarlsonj 		struct stat64 st;
1254108322fbScarlsonj 
1255108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
125668eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1257fa3d7ae1Sedp 			/*
1258fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1259fa3d7ae1Sedp 			 * to check if that device is already mounted
1260fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1261fa3d7ae1Sedp 			 * of the device instead of a direct mount
1262fa3d7ae1Sedp 			 */
126368eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1264108322fbScarlsonj 				return (-1);
126568eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1266fa3d7ae1Sedp 			/*
1267fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1268fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1269fa3d7ae1Sedp 			 * this case in order to get at the underlying
1270fa3d7ae1Sedp 			 * read-write path.
1271fa3d7ae1Sedp 			 */
1272fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1273108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1274108322fbScarlsonj 		}
1275108322fbScarlsonj 	}
1276108322fbScarlsonj 
1277108322fbScarlsonj 	/*
12787c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12797c478bd9Sstevel@tonic-gate 	 */
12807c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
128193239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12827c478bd9Sstevel@tonic-gate 		return (-1);
128393239addSjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
128493239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
128593239addSjohnlev 		return (-1);
128693239addSjohnlev 	}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
12907c478bd9Sstevel@tonic-gate 	 */
12917c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
12927c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
12937c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12947c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
12957c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
12967c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
12977c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
12987c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
12997c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
13007c478bd9Sstevel@tonic-gate 		}
13017c478bd9Sstevel@tonic-gate 	}
13029acbbeafSnn35248 
13039acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13049acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
13059acbbeafSnn35248 		return (rv);
13069acbbeafSnn35248 
13079acbbeafSnn35248 	/*
13089acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13099acbbeafSnn35248 	 * we're done.
13109acbbeafSnn35248 	 */
13119acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13129acbbeafSnn35248 		return (0);
13139acbbeafSnn35248 
13149acbbeafSnn35248 	/*
13159acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13169acbbeafSnn35248 	 * need to configure it.
13179acbbeafSnn35248 	 */
13180679f794S 	return (mount_one_dev(zlogp, path, mount_cmd));
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate static void
13227c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13237c478bd9Sstevel@tonic-gate {
13247c478bd9Sstevel@tonic-gate 	uint_t i;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13277c478bd9Sstevel@tonic-gate 		return;
13287c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13297c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13307c478bd9Sstevel@tonic-gate 	free(fsarray);
13317c478bd9Sstevel@tonic-gate }
13327c478bd9Sstevel@tonic-gate 
1333108322fbScarlsonj /*
1334f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1335f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1336f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1337f4368d3dSvp157776  * is done this way because:
1338f4368d3dSvp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
1339f4368d3dSvp157776  * 	We loopback mount zone's own /etc and /var into the root of the
1340f4368d3dSvp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1341f4368d3dSvp157776  * 	need to delay the mount of /var till the zone's root gets populated.
1342f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1343f4368d3dSvp157776  * 	build_mounted_post_var() which gets called only after the zone
1344f4368d3dSvp157776  * 	specific filesystems are mounted.
13456cfd72c6Sgjelinek  *
13466cfd72c6Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13476cfd72c6Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13486cfd72c6Sgjelinek  * scratch zone.
1349108322fbScarlsonj  */
1350108322fbScarlsonj static boolean_t
1351f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
135216bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1353108322fbScarlsonj {
1354108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1355108322fbScarlsonj 	const char **cpp;
1356108322fbScarlsonj 	static const char *mkdirs[] = {
13573f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13583f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1359108322fbScarlsonj 	};
1360108322fbScarlsonj 	char *altstr;
1361f4368d3dSvp157776 	FILE *fp;
1362108322fbScarlsonj 	uuid_t uuid;
1363108322fbScarlsonj 
1364108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
136516bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
136616bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1367108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1368108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1369108322fbScarlsonj 
1370108322fbScarlsonj 	/*
1371108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1372108322fbScarlsonj 	 * zone_mount_early.)
1373108322fbScarlsonj 	 */
1374108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1375108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1376108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1377108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1378108322fbScarlsonj 			return (B_FALSE);
1379108322fbScarlsonj 		}
1380108322fbScarlsonj 	}
1381f4368d3dSvp157776 	/*
1382f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1383f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1384f4368d3dSvp157776 	 * read-only inside the scratch zone.
1385f4368d3dSvp157776 	 */
1386f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1387f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1388f4368d3dSvp157776 	if (altstr == NULL) {
1389f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1390f4368d3dSvp157776 		return (B_FALSE);
1391f4368d3dSvp157776 	}
1392f4368d3dSvp157776 	zonecfg_set_root("");
1393f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1394f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1395f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1396f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1397f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1398f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1399f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1400f4368d3dSvp157776 			return (B_FALSE);
1401f4368d3dSvp157776 		}
14026e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1403f4368d3dSvp157776 		    tmp) != 0) {
1404f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1405f4368d3dSvp157776 			    fromdir);
1406f4368d3dSvp157776 			return (B_FALSE);
1407f4368d3dSvp157776 		}
1408f4368d3dSvp157776 	}
1409f4368d3dSvp157776 	zonecfg_set_root(altstr);
1410f4368d3dSvp157776 	free(altstr);
1411f4368d3dSvp157776 
1412f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1413f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1414f4368d3dSvp157776 		return (B_FALSE);
1415f4368d3dSvp157776 	}
1416f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1417f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1418f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1419f4368d3dSvp157776 	}
1420f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1421f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1422f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1423f4368d3dSvp157776 		return (B_FALSE);
1424f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1425f4368d3dSvp157776 	return (B_TRUE);
1426f4368d3dSvp157776 }
1427f4368d3dSvp157776 
1428f4368d3dSvp157776 
1429f4368d3dSvp157776 static boolean_t
14306cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14316cfd72c6Sgjelinek     const char *luroot)
1432f4368d3dSvp157776 {
1433f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1434f4368d3dSvp157776 	const char **cpp;
14356cfd72c6Sgjelinek 	const char **loopdirs;
14366cfd72c6Sgjelinek 	const char **tmpdirs;
1437f4368d3dSvp157776 	static const char *localdirs[] = {
1438f4368d3dSvp157776 		"/etc", "/var", NULL
1439f4368d3dSvp157776 	};
14406cfd72c6Sgjelinek 	static const char *scr_loopdirs[] = {
1441f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1442f4368d3dSvp157776 		"/usr", NULL
1443f4368d3dSvp157776 	};
14446cfd72c6Sgjelinek 	static const char *upd_loopdirs[] = {
14456cfd72c6Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14466cfd72c6Sgjelinek 		"/usr", "/var", NULL
14476cfd72c6Sgjelinek 	};
14486cfd72c6Sgjelinek 	static const char *scr_tmpdirs[] = {
1449f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1450f4368d3dSvp157776 	};
14516cfd72c6Sgjelinek 	static const char *upd_tmpdirs[] = {
14526cfd72c6Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14536cfd72c6Sgjelinek 	};
1454f4368d3dSvp157776 	struct stat st;
1455f4368d3dSvp157776 
14566cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
1457108322fbScarlsonj 		/*
14586cfd72c6Sgjelinek 		 * These are mounted read-write from the zone undergoing
14596cfd72c6Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14606cfd72c6Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
1461108322fbScarlsonj 		 */
1462108322fbScarlsonj 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14636cfd72c6Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1464108322fbScarlsonj 			    *cpp);
14656cfd72c6Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14666cfd72c6Sgjelinek 			    rootpath, *cpp);
1467108322fbScarlsonj 			if (mkdir(tmp, 0755) != 0) {
1468108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1469108322fbScarlsonj 				return (B_FALSE);
1470108322fbScarlsonj 			}
14716cfd72c6Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14726cfd72c6Sgjelinek 			    != 0) {
14736cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14746cfd72c6Sgjelinek 				    tmp, *cpp);
1475108322fbScarlsonj 				return (B_FALSE);
1476108322fbScarlsonj 			}
1477108322fbScarlsonj 		}
14786cfd72c6Sgjelinek 	}
14796cfd72c6Sgjelinek 
14806cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14816cfd72c6Sgjelinek 		loopdirs = upd_loopdirs;
14826cfd72c6Sgjelinek 	else
14836cfd72c6Sgjelinek 		loopdirs = scr_loopdirs;
1484108322fbScarlsonj 
1485108322fbScarlsonj 	/*
1486108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1487108322fbScarlsonj 	 * they contain binaries that must match system.
1488108322fbScarlsonj 	 */
1489108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1490108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1491108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1492108322fbScarlsonj 			if (errno != EEXIST) {
1493108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1494108322fbScarlsonj 				return (B_FALSE);
1495108322fbScarlsonj 			}
1496108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1497108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1498108322fbScarlsonj 				return (B_FALSE);
1499108322fbScarlsonj 			}
1500108322fbScarlsonj 			/*
1501108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1502108322fbScarlsonj 			 * things that have been converted into symlinks
1503108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1504108322fbScarlsonj 			 * fixup.
1505108322fbScarlsonj 			 */
1506108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1507108322fbScarlsonj 				continue;
1508108322fbScarlsonj 		}
15096e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1510108322fbScarlsonj 		    tmp) != 0) {
1511108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1512108322fbScarlsonj 			    *cpp);
1513108322fbScarlsonj 			return (B_FALSE);
1514108322fbScarlsonj 		}
1515108322fbScarlsonj 	}
1516108322fbScarlsonj 
15176cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15186cfd72c6Sgjelinek 		tmpdirs = upd_tmpdirs;
15196cfd72c6Sgjelinek 	else
15206cfd72c6Sgjelinek 		tmpdirs = scr_tmpdirs;
15216cfd72c6Sgjelinek 
1522108322fbScarlsonj 	/*
1523108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1524108322fbScarlsonj 	 */
1525108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1526108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15276cfd72c6Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15286cfd72c6Sgjelinek 		    errno != EEXIST) {
1529108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1530108322fbScarlsonj 			return (B_FALSE);
1531108322fbScarlsonj 		}
1532d30e996aSgjelinek 
1533d30e996aSgjelinek 		/*
1534d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1535d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1536d30e996aSgjelinek 		 * the correct mode for /tmp now.
1537d30e996aSgjelinek 		 */
1538d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1539d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1540d30e996aSgjelinek 			return (B_FALSE);
1541d30e996aSgjelinek 		}
1542d30e996aSgjelinek 
1543108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1544108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1545108322fbScarlsonj 			return (B_FALSE);
1546108322fbScarlsonj 		}
1547108322fbScarlsonj 	}
1548108322fbScarlsonj 	return (B_TRUE);
1549108322fbScarlsonj }
1550108322fbScarlsonj 
15519acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15529acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
15539acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
15549acbbeafSnn35248 	int			*pgcd_num_fs;
15559acbbeafSnn35248 } plat_gmount_cb_data_t;
15569acbbeafSnn35248 
15579acbbeafSnn35248 /*
15589acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15599acbbeafSnn35248  * through all global brand platform mounts.
15609acbbeafSnn35248  */
15619acbbeafSnn35248 int
15629acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15639acbbeafSnn35248     const char *fstype, const char *opt)
15649acbbeafSnn35248 {
15659acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
15669acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15679acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15689acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
15699acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15709acbbeafSnn35248 
15719acbbeafSnn35248 	num_fs++;
15729acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15739acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15749acbbeafSnn35248 		return (-1);
15759acbbeafSnn35248 	}
15769acbbeafSnn35248 
15779acbbeafSnn35248 	fs_ptr = tmp_ptr;
15789acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
15799acbbeafSnn35248 
15809acbbeafSnn35248 	/* update the callback struct passed in */
15819acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
15829acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
15839acbbeafSnn35248 
15849acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
15859acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15869acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
15879acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15889acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15899acbbeafSnn35248 	fsp->zone_fs_options = NULL;
1590fa3d7ae1Sedp 	if ((opt != NULL) &&
1591fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15929acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
15939acbbeafSnn35248 		return (-1);
15949acbbeafSnn35248 	}
15959acbbeafSnn35248 
15969acbbeafSnn35248 	return (0);
15979acbbeafSnn35248 }
15989acbbeafSnn35248 
15999acbbeafSnn35248 static int
16009acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16016cfd72c6Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16029acbbeafSnn35248 {
16039acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16049acbbeafSnn35248 	int num_fs;
16059acbbeafSnn35248 
16069acbbeafSnn35248 	num_fs = *num_fsp;
16079acbbeafSnn35248 	fs_ptr = *fs_tabp;
16089acbbeafSnn35248 
16099acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16109acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16119acbbeafSnn35248 		return (-1);
16129acbbeafSnn35248 	}
16139acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16149acbbeafSnn35248 		/*
16159acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
16169acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
16179acbbeafSnn35248 		 * case.
16189acbbeafSnn35248 		 */
16196cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16206cfd72c6Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16219acbbeafSnn35248 			continue;
16229acbbeafSnn35248 
16239acbbeafSnn35248 		num_fs++;
16249acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16259acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16269acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16279acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
16289acbbeafSnn35248 			return (-1);
16299acbbeafSnn35248 		}
16309acbbeafSnn35248 		/* update the pointers passed in */
16319acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16329acbbeafSnn35248 		*num_fsp = num_fs;
16339acbbeafSnn35248 
16349acbbeafSnn35248 		fs_ptr = tmp_ptr;
16359acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16369acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16379acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16389acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16399acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
16409acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16419acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
16429acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
1643fa3d7ae1Sedp 
1644fa3d7ae1Sedp 		/*
1645fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1646fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1647fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1648fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1649fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1650fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1651fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1652fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1653fa3d7ae1Sedp 		 */
1654fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1655fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1656fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1657fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1658fa3d7ae1Sedp 		}
1659fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1660fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
16619acbbeafSnn35248 	}
16629acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
16639acbbeafSnn35248 	return (0);
16649acbbeafSnn35248 }
16659acbbeafSnn35248 
16667c478bd9Sstevel@tonic-gate static int
16676cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
16687c478bd9Sstevel@tonic-gate {
16697c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
16707c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
16719acbbeafSnn35248 	char brand[MAXNAMELEN];
167216bca938Svp157776 	char luroot[MAXPATHLEN];
16739acbbeafSnn35248 	int i, num_fs = 0;
16749acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
16757c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
16767c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1677123807fbSedp 	brand_handle_t bh;
16789acbbeafSnn35248 	plat_gmount_cb_data_t cb;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1681108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
16827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1683108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1684108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1685108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
16867c478bd9Sstevel@tonic-gate 		goto bad;
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
16907c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
16917c478bd9Sstevel@tonic-gate 		goto bad;
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
16957c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
16967c478bd9Sstevel@tonic-gate 		goto bad;
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1700ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17017c478bd9Sstevel@tonic-gate 		goto bad;
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17047c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17057c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17067c478bd9Sstevel@tonic-gate 		goto bad;
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17090679f794S 	/*
1710e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
17110679f794S 	 * brand global mounts.
17120679f794S 	 */
17130679f794S 	if (ALT_MOUNT(mount_cmd)) {
1714e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
17150679f794S 	} else {
171662868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
17170679f794S 	}
17180679f794S 
17199acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
17200679f794S 	if ((bh = brand_open(brand)) == NULL) {
17219acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1722e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17239acbbeafSnn35248 		return (-1);
17249acbbeafSnn35248 	}
17259acbbeafSnn35248 
17269acbbeafSnn35248 	/*
17279acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
17289acbbeafSnn35248 	 * configuration.
17299acbbeafSnn35248 	 */
17309acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
17319acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
17329acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1733123807fbSedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17349acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
17359acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1736123807fbSedp 		brand_close(bh);
1737e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17389acbbeafSnn35248 		return (-1);
17399acbbeafSnn35248 	}
1740123807fbSedp 	brand_close(bh);
17419acbbeafSnn35248 
17427c478bd9Sstevel@tonic-gate 	/*
17436e1ae2a3SGary Pennington 	 * Iterate through the rest of the filesystems. Sort them all,
17446e1ae2a3SGary Pennington 	 * then mount them in sorted order. This is to make sure the
17456e1ae2a3SGary Pennington 	 * higher level directories (e.g., /usr) get mounted before
17466e1ae2a3SGary Pennington 	 * any beneath them (e.g., /usr/local).
17477c478bd9Sstevel@tonic-gate 	 */
17489acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17499acbbeafSnn35248 	    mount_cmd) != 0)
17507c478bd9Sstevel@tonic-gate 		goto bad;
1751fa9e4066Sahrens 
17527c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17537c478bd9Sstevel@tonic-gate 	handle = NULL;
17547c478bd9Sstevel@tonic-gate 
1755108322fbScarlsonj 	/*
17569acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
17579acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
17589acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
17599acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
17609acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
17619acbbeafSnn35248 	 * the zones filesystems there instead.
17629acbbeafSnn35248 	 *
17639acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
17649acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
17659acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
17669acbbeafSnn35248 	 *
17679acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
17689acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
17699acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
17709acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
17719acbbeafSnn35248 	 * upgrade environment.)
17729acbbeafSnn35248 	 *
17739acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
17749acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
17759acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
17769acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
17779acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
17789acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
17799acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
17809acbbeafSnn35248 	 *
17819acbbeafSnn35248 	 * All this work is done in three phases:
1782f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1783f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1784f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1785f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1786108322fbScarlsonj 	 */
17876cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
178816bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1789108322fbScarlsonj 		goto bad;
1790108322fbScarlsonj 
17917c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
17929acbbeafSnn35248 
17937c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
17946cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
17959acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
17969acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
17979acbbeafSnn35248 
17989acbbeafSnn35248 			/*
17999acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
18009acbbeafSnn35248 			 * but /dev is special and always goes at the top
18019acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
18029acbbeafSnn35248 			 */
18039acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18049acbbeafSnn35248 			rootpath[slen] = '\0';
18050679f794S 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18060679f794S 			    != 0)
18079acbbeafSnn35248 				goto bad;
18089acbbeafSnn35248 			rootpath[slen] = '/';
18099acbbeafSnn35248 			continue;
18109acbbeafSnn35248 		}
18110679f794S 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18127c478bd9Sstevel@tonic-gate 			goto bad;
18137c478bd9Sstevel@tonic-gate 	}
18146cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18156cfd72c6Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1816f4368d3dSvp157776 		goto bad;
181745916cd2Sjpk 
181845916cd2Sjpk 	/*
181945916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
182045916cd2Sjpk 	 */
18216cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18226cfd72c6Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
182345916cd2Sjpk 		goto bad;
182445916cd2Sjpk 
18257c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	/*
18287c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18297c478bd9Sstevel@tonic-gate 	 */
18307c478bd9Sstevel@tonic-gate 	return (0);
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate bad:
18337c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18347c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18357c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18367c478bd9Sstevel@tonic-gate 	return (-1);
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18407c478bd9Sstevel@tonic-gate static int
18417c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18427c478bd9Sstevel@tonic-gate {
18437c478bd9Sstevel@tonic-gate 	int prefixlen;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18467c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18477c478bd9Sstevel@tonic-gate 		return (1);
18487c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
18497c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
18507c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18517c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
18527c478bd9Sstevel@tonic-gate 			continue;
18537c478bd9Sstevel@tonic-gate 		}
18547c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18557c478bd9Sstevel@tonic-gate 		prefixlen--;
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 	return (0);
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate /*
18617c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
18627c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
18637c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
18647c478bd9Sstevel@tonic-gate  *
18657c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
18667c478bd9Sstevel@tonic-gate  */
18677c478bd9Sstevel@tonic-gate static int
1868f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
18697c478bd9Sstevel@tonic-gate {
18707c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
18717c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
18727c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
18737c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
18747c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
18757c478bd9Sstevel@tonic-gate 	uint_t bufsize;
18767c478bd9Sstevel@tonic-gate 	char *buf = NULL;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
18797c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
18807c478bd9Sstevel@tonic-gate 		ret_code = -1;
18817c478bd9Sstevel@tonic-gate 		goto bad;
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
18847c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
18857c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
18867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1887f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
18887c478bd9Sstevel@tonic-gate 		ret_code = -1;
18897c478bd9Sstevel@tonic-gate 		goto bad;
18907c478bd9Sstevel@tonic-gate 	}
18917c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
18927c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
18937c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
18947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
18957c478bd9Sstevel@tonic-gate 		ret_code = -1;
18967c478bd9Sstevel@tonic-gate 		goto bad;
18977c478bd9Sstevel@tonic-gate 	}
18987c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
18997c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19007c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19017c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19027c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1903f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1904f4b3ec61Sdh155122 		    "interfaces");
19057c478bd9Sstevel@tonic-gate 		ret_code = -1;
19067c478bd9Sstevel@tonic-gate 		goto bad;
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19097c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19107c478bd9Sstevel@tonic-gate 		(void) close(s);
19117c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19127c478bd9Sstevel@tonic-gate 		    0) {
19137c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19147c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
19157c478bd9Sstevel@tonic-gate 			ret_code = -1;
19167c478bd9Sstevel@tonic-gate 			continue;
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19197c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19207c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19217c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1922c1c0ebd5Ssl108498 			if (errno == ENXIO)
1923c1c0ebd5Ssl108498 				/*
1924c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1925c1c0ebd5Ssl108498 				 * another zone halting.
1926c1c0ebd5Ssl108498 				 */
1927c1c0ebd5Ssl108498 				continue;
19287c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1929c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1930f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19317c478bd9Sstevel@tonic-gate 			ret_code = -1;
19327c478bd9Sstevel@tonic-gate 			continue;
19337c478bd9Sstevel@tonic-gate 		}
19347c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19357c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19367c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1937f4b3ec61Sdh155122 				    "%s: could not remove network interface",
19387c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
19397c478bd9Sstevel@tonic-gate 				ret_code = -1;
19407c478bd9Sstevel@tonic-gate 				continue;
19417c478bd9Sstevel@tonic-gate 			}
19427c478bd9Sstevel@tonic-gate 		}
19437c478bd9Sstevel@tonic-gate 	}
19447c478bd9Sstevel@tonic-gate bad:
19457c478bd9Sstevel@tonic-gate 	if (s > 0)
19467c478bd9Sstevel@tonic-gate 		(void) close(s);
19477c478bd9Sstevel@tonic-gate 	if (buf)
19487c478bd9Sstevel@tonic-gate 		free(buf);
19497c478bd9Sstevel@tonic-gate 	return (ret_code);
19507c478bd9Sstevel@tonic-gate }
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate static union	sockunion {
19537c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
19547c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
19557c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19567c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19577c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate static struct {
19607c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
19617c478bd9Sstevel@tonic-gate 	char	space[512];
19627c478bd9Sstevel@tonic-gate } rtmsg;
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate static int
19657c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
19667c478bd9Sstevel@tonic-gate {
19677c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
19687c478bd9Sstevel@tonic-gate 	case AF_INET:
19697c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
19707c478bd9Sstevel@tonic-gate 	case AF_LINK:
19717c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
19727c478bd9Sstevel@tonic-gate 	case AF_INET6:
19737c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
19747c478bd9Sstevel@tonic-gate 	default:
19757c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
19767c478bd9Sstevel@tonic-gate 	}
19777c478bd9Sstevel@tonic-gate }
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
19807c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate /*
19837c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
19847c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
19857c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
19867c478bd9Sstevel@tonic-gate  *
19877c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
19887c478bd9Sstevel@tonic-gate  *
19897c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
19907c478bd9Sstevel@tonic-gate  *
19917c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
19927c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
19937c478bd9Sstevel@tonic-gate  */
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate static char *
19967c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
19977c478bd9Sstevel@tonic-gate {
19987c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
19997c478bd9Sstevel@tonic-gate 	pid_t pid;
20007c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
20017c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
20027c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20037c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
20047c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	pid = getpid();
20097c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20117c478bd9Sstevel@tonic-gate 		return (NULL);
20127c478bd9Sstevel@tonic-gate 	}
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20157c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20187c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20197c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20207c478bd9Sstevel@tonic-gate 	} else {
20217c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20247c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20257c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20267c478bd9Sstevel@tonic-gate 	}
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20317c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20327c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20337c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20347c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20357c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20387c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20397c478bd9Sstevel@tonic-gate 	cp += l;
20407c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20417c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20427c478bd9Sstevel@tonic-gate 	cp += l;
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20487c478bd9Sstevel@tonic-gate 		return (NULL);
20497c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20517c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20527c478bd9Sstevel@tonic-gate 		return (NULL);
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate 	do {
20557c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20567c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20577c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20587c478bd9Sstevel@tonic-gate 	if (l < 0) {
20597c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
20607c478bd9Sstevel@tonic-gate 		return (NULL);
20617c478bd9Sstevel@tonic-gate 	}
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
20647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
20657c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
20667c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
20677c478bd9Sstevel@tonic-gate 		return (NULL);
20687c478bd9Sstevel@tonic-gate 	}
20697c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
20707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
20717c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
20727c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
20737c478bd9Sstevel@tonic-gate 		return (NULL);
20747c478bd9Sstevel@tonic-gate 	}
20757c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
20767c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
20777c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
20787c478bd9Sstevel@tonic-gate 		return (NULL);
20797c478bd9Sstevel@tonic-gate 	}
20807c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2081f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
20827c478bd9Sstevel@tonic-gate 		return (NULL);
20837c478bd9Sstevel@tonic-gate 	}
20847c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
20857c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
20867c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
20877c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
20887c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
20897c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
20907c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
20917c478bd9Sstevel@tonic-gate 			continue;
20927c478bd9Sstevel@tonic-gate 		}
20937c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
20947c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
20957c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
20967c478bd9Sstevel@tonic-gate 		break;
20977c478bd9Sstevel@tonic-gate 	}
20987c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2099f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2100f4b3ec61Sdh155122 		    "determined");
21017c478bd9Sstevel@tonic-gate 		return (NULL);
21027c478bd9Sstevel@tonic-gate 	}
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	/*
21057c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21067c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21077c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21087c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21097c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21107c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21117c478bd9Sstevel@tonic-gate 	 */
21127c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21137c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21147c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21157c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21167c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21177c478bd9Sstevel@tonic-gate 	if (i < 0) {
21187c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2119f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2120f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
21217c478bd9Sstevel@tonic-gate 		return (NULL);
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21247c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21257c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21287c478bd9Sstevel@tonic-gate 		return (answer);
21297c478bd9Sstevel@tonic-gate 	return (NULL);
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate /*
21337c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21347c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21357c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21367c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21377c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21387c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21397c478bd9Sstevel@tonic-gate  *
21407c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21417c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
21427c478bd9Sstevel@tonic-gate  */
21437c478bd9Sstevel@tonic-gate static int
21447c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2145f14f3ae7Sjv227347     struct zone_nwiftab *nwiftabptr)
21467c478bd9Sstevel@tonic-gate {
21477c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
21487c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
21497c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
21502e481403SVamsi Nagineni 	struct sockaddr_storage laddr;
21517c478bd9Sstevel@tonic-gate 	struct in_addr in4;
21527c478bd9Sstevel@tonic-gate 	sa_family_t af;
21537c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
21547c478bd9Sstevel@tonic-gate 	int s;
21557c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
2156f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	boolean_t is_loopback = B_FALSE;
21577c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
21587c478bd9Sstevel@tonic-gate 	int res;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
21617c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
21627c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
21637c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
21647c478bd9Sstevel@tonic-gate 		return (-1);
21657c478bd9Sstevel@tonic-gate 	}
21667c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
21677c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21687c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21697c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
21717c478bd9Sstevel@tonic-gate 		return (-1);
21727c478bd9Sstevel@tonic-gate 	}
21737c478bd9Sstevel@tonic-gate 
21742e481403SVamsi Nagineni 	/*
21752e481403SVamsi Nagineni 	 * This is a similar kind of "hack" like in addif() to get around
21762e481403SVamsi Nagineni 	 * the problem of SIOCLIFADDIF.  The problem is that this ioctl
21772e481403SVamsi Nagineni 	 * does not include the netmask when adding a logical interface.
21782e481403SVamsi Nagineni 	 * To get around this problem, we first add the logical interface
21792e481403SVamsi Nagineni 	 * with a 0 address.  After that, we set the netmask if provided.
21802e481403SVamsi Nagineni 	 * Finally we set the interface address.
21812e481403SVamsi Nagineni 	 */
21822e481403SVamsi Nagineni 	laddr = lifr.lifr_addr;
21837c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
21847c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
21852e481403SVamsi Nagineni 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
21862e481403SVamsi Nagineni 
21877c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
218822321485Svp157776 		/*
218922321485Svp157776 		 * Here, we know that the interface can't be brought up.
219022321485Svp157776 		 * A similar warning message was already printed out to
219122321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
219222321485Svp157776 		 * message to syslog and continue.
219322321485Svp157776 		 */
2194f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
219522321485Svp157776 		    "'%s' which may not be present/plumbed in the "
219622321485Svp157776 		    "global zone.", lifr.lifr_name);
21977c478bd9Sstevel@tonic-gate 		(void) close(s);
219822321485Svp157776 		return (Z_OK);
21997c478bd9Sstevel@tonic-gate 	}
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22027c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22037c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22067c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2207f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2208f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
22097c478bd9Sstevel@tonic-gate 		goto bad;
22107c478bd9Sstevel@tonic-gate 	}
22117c478bd9Sstevel@tonic-gate 
2212f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	/*
2213f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * Loopback interface will use the default netmask assigned, if no
2214f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * netmask is found.
2215f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 */
22167c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2217f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		is_loopback = B_TRUE;
2218f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	}
22197c478bd9Sstevel@tonic-gate 	if (af == AF_INET) {
22207c478bd9Sstevel@tonic-gate 		/*
22217c478bd9Sstevel@tonic-gate 		 * The IPv4 netmask can be determined either
22227c478bd9Sstevel@tonic-gate 		 * directly if a prefix length was supplied with
22237c478bd9Sstevel@tonic-gate 		 * the address or via the netmasks database.  Not
22247c478bd9Sstevel@tonic-gate 		 * being able to determine it is a common failure,
22257c478bd9Sstevel@tonic-gate 		 * but it often is not fatal to operation of the
22267c478bd9Sstevel@tonic-gate 		 * interface.  In that case, a warning will be
22277c478bd9Sstevel@tonic-gate 		 * printed after the rest of the interface's
22287c478bd9Sstevel@tonic-gate 		 * parameters have been configured.
22297c478bd9Sstevel@tonic-gate 		 */
22307c478bd9Sstevel@tonic-gate 		(void) memset(&netmask4, 0, sizeof (netmask4));
22317c478bd9Sstevel@tonic-gate 		if (slashp != NULL) {
22327c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22337c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask4.sin_addr) != 0) {
22347c478bd9Sstevel@tonic-gate 				*slashp = '/';
22357c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22367c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22377c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
22387c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22397c478bd9Sstevel@tonic-gate 				goto bad;
22407c478bd9Sstevel@tonic-gate 			}
22417c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22427c478bd9Sstevel@tonic-gate 		} else if (getnetmaskbyaddr(in4,
22437c478bd9Sstevel@tonic-gate 		    &netmask4.sin_addr) == 0) {
22447c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22457c478bd9Sstevel@tonic-gate 		}
22467c478bd9Sstevel@tonic-gate 		if (got_netmask) {
22477c478bd9Sstevel@tonic-gate 			netmask4.sin_family = af;
22487c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask4,
22497c478bd9Sstevel@tonic-gate 			    sizeof (netmask4));
22507c478bd9Sstevel@tonic-gate 		}
22517c478bd9Sstevel@tonic-gate 	} else {
22527c478bd9Sstevel@tonic-gate 		(void) memset(&netmask6, 0, sizeof (netmask6));
22537c478bd9Sstevel@tonic-gate 		if (addr2netmask(slashp + 1, V6_ADDR_LEN,
22547c478bd9Sstevel@tonic-gate 		    (uchar_t *)&netmask6.sin6_addr) != 0) {
22557c478bd9Sstevel@tonic-gate 			*slashp = '/';
22567c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
22577c478bd9Sstevel@tonic-gate 			    "%s: invalid prefix length in %s",
22587c478bd9Sstevel@tonic-gate 			    lifr.lifr_name,
22597c478bd9Sstevel@tonic-gate 			    nwiftabptr->zone_nwif_address);
22607c478bd9Sstevel@tonic-gate 			goto bad;
22617c478bd9Sstevel@tonic-gate 		}
22627c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;
22637c478bd9Sstevel@tonic-gate 		netmask6.sin6_family = af;
22647c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &netmask6,
22657c478bd9Sstevel@tonic-gate 		    sizeof (netmask6));
22667c478bd9Sstevel@tonic-gate 	}
22677c478bd9Sstevel@tonic-gate 	if (got_netmask &&
22687c478bd9Sstevel@tonic-gate 	    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
22697c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not set netmask",
22707c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22717c478bd9Sstevel@tonic-gate 		goto bad;
22727c478bd9Sstevel@tonic-gate 	}
22737c478bd9Sstevel@tonic-gate 
22742e481403SVamsi Nagineni 	/* Set the interface address */
22752e481403SVamsi Nagineni 	lifr.lifr_addr = laddr;
22767c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22777c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
22782e481403SVamsi Nagineni 		    "%s: could not set IP address to %s",
22792e481403SVamsi Nagineni 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22807c478bd9Sstevel@tonic-gate 		goto bad;
22817c478bd9Sstevel@tonic-gate 	}
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
22857c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
22867c478bd9Sstevel@tonic-gate 		goto bad;
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
22897c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
22907c478bd9Sstevel@tonic-gate 		int save_errno = errno;
22917c478bd9Sstevel@tonic-gate 		char *zone_using;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 		/*
22947c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
22957c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
22967c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
22977c478bd9Sstevel@tonic-gate 		 * using that address.
22987c478bd9Sstevel@tonic-gate 		 */
22997c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23007c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2301f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2302f4b3ec61Sdh155122 			    lifr.lifr_name);
23037c478bd9Sstevel@tonic-gate 			goto bad;
23047c478bd9Sstevel@tonic-gate 		}
23057c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23067c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23077c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23087c478bd9Sstevel@tonic-gate 			goto bad;
23097c478bd9Sstevel@tonic-gate 		}
23107c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23117c478bd9Sstevel@tonic-gate 		errno = save_errno;
23127c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
23137c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2314f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2315f4b3ec61Sdh155122 			    lifr.lifr_name);
23167c478bd9Sstevel@tonic-gate 		else
2317f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2318f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2319f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
23207c478bd9Sstevel@tonic-gate 		goto bad;
23217c478bd9Sstevel@tonic-gate 	}
23227c478bd9Sstevel@tonic-gate 
2323f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (!got_netmask && !is_loopback) {
23247c478bd9Sstevel@tonic-gate 		/*
23257c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23267c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23277c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23287c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23297c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23307c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23317c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23327c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23337c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23347c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
23357c478bd9Sstevel@tonic-gate 		 */
23367c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23377c478bd9Sstevel@tonic-gate 		void  *addr;
2338e11c3f44Smeem 		const char *nomatch = "no matching subnet found in netmasks(4)";
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
23417c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23427c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23437c478bd9Sstevel@tonic-gate 		else
23447c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
23457c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
23467c478bd9Sstevel@tonic-gate 
2347e11c3f44Smeem 		/*
2348e11c3f44Smeem 		 * Find out what netmask the interface is going to be using.
2349e11c3f44Smeem 		 * If we just brought up an IPMP data address on an underlying
2350e11c3f44Smeem 		 * interface above, the address will have already migrated, so
2351e11c3f44Smeem 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
2352e11c3f44Smeem 		 * to bring the address up to get the actual netmask).  Just
2353e11c3f44Smeem 		 * omit printing the actual netmask in this corner-case.
2354e11c3f44Smeem 		 */
23557c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2356e11c3f44Smeem 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2357e11c3f44Smeem 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2358e11c3f44Smeem 			    nomatch);
2359e11c3f44Smeem 		} else {
23607c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
2361e11c3f44Smeem 			    "WARNING: %s: %s: %s; using default of %s.",
2362e11c3f44Smeem 			    lifr.lifr_name, nomatch, addrstr4, buffer);
2363e11c3f44Smeem 		}
23647c478bd9Sstevel@tonic-gate 	}
23657c478bd9Sstevel@tonic-gate 
2366de860bd9Sgfaden 	/*
2367de860bd9Sgfaden 	 * If a default router was specified for this interface
2368de860bd9Sgfaden 	 * set the route now. Ignore if already set.
2369de860bd9Sgfaden 	 */
2370de860bd9Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2371de860bd9Sgfaden 		int status;
2372de860bd9Sgfaden 		char *argv[7];
2373de860bd9Sgfaden 
2374de860bd9Sgfaden 		argv[0] = "route";
2375de860bd9Sgfaden 		argv[1] = "add";
2376de860bd9Sgfaden 		argv[2] = "-ifp";
2377de860bd9Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
2378de860bd9Sgfaden 		argv[4] = "default";
2379de860bd9Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2380de860bd9Sgfaden 		argv[6] = NULL;
2381de860bd9Sgfaden 
2382de860bd9Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2383de860bd9Sgfaden 		if (status != 0 && status != EEXIST)
2384de860bd9Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
2385de860bd9Sgfaden 			    "interface %s to %s\n",
2386de860bd9Sgfaden 			    nwiftabptr->zone_nwif_physical,
2387de860bd9Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
2388de860bd9Sgfaden 	}
2389de860bd9Sgfaden 
23907c478bd9Sstevel@tonic-gate 	(void) close(s);
23917c478bd9Sstevel@tonic-gate 	return (Z_OK);
23927c478bd9Sstevel@tonic-gate bad:
23937c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
23947c478bd9Sstevel@tonic-gate 	(void) close(s);
23957c478bd9Sstevel@tonic-gate 	return (-1);
23967c478bd9Sstevel@tonic-gate }
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate /*
23997c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
2400f14f3ae7Sjv227347  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2401f14f3ae7Sjv227347  * system.
24027c478bd9Sstevel@tonic-gate  *
24037c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24047c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
24057c478bd9Sstevel@tonic-gate  */
24067c478bd9Sstevel@tonic-gate static int
2407f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24087c478bd9Sstevel@tonic-gate {
24097c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24107c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24117c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24157c478bd9Sstevel@tonic-gate 		return (-1);
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24197c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24207c478bd9Sstevel@tonic-gate 		return (-1);
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24247c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24257c478bd9Sstevel@tonic-gate 		return (-1);
24267c478bd9Sstevel@tonic-gate 	}
24277c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24287c478bd9Sstevel@tonic-gate 		for (;;) {
24297c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24307c478bd9Sstevel@tonic-gate 				break;
2431f14f3ae7Sjv227347 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24327c478bd9Sstevel@tonic-gate 			    Z_OK) {
24337c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24347c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24357c478bd9Sstevel@tonic-gate 				return (-1);
24367c478bd9Sstevel@tonic-gate 			}
24377c478bd9Sstevel@tonic-gate 		}
24387c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24397c478bd9Sstevel@tonic-gate 	}
24407c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
2441fdb7141fSgfaden 	if (is_system_labeled()) {
2442fdb7141fSgfaden 		/*
2443fdb7141fSgfaden 		 * Labeled zones share the loopback interface
2444fdb7141fSgfaden 		 * so it is not plumbed for shared stack instances.
2445fdb7141fSgfaden 		 */
2446fdb7141fSgfaden 		return (0);
2447fdb7141fSgfaden 	}
24487c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
24497c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
24507c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
24517c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2452442d3e9eSgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2453f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24547c478bd9Sstevel@tonic-gate 		return (-1);
2455f14f3ae7Sjv227347 
2456f14f3ae7Sjv227347 	/* Always plumb up the IPv6 loopback interface. */
24577c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
24587c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2459f14f3ae7Sjv227347 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24607c478bd9Sstevel@tonic-gate 		return (-1);
24617c478bd9Sstevel@tonic-gate 	return (0);
24627c478bd9Sstevel@tonic-gate }
24637c478bd9Sstevel@tonic-gate 
246489b1c373Smeem static void
246589b1c373Smeem zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
246689b1c373Smeem {
246789b1c373Smeem 	char errmsg[DLADM_STRSIZE];
246889b1c373Smeem 
246989b1c373Smeem 	(void) dladm_status2str(err, errmsg);
247089b1c373Smeem 	zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
247189b1c373Smeem }
247289b1c373Smeem 
2473f4b3ec61Sdh155122 static int
24742b24ab6bSSebastien Roy add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2475f4b3ec61Sdh155122 {
247689b1c373Smeem 	dladm_status_t err;
24770dc2366fSVenugopal Iyer 	boolean_t cpuset, poolset;
2478efd4c9b6SSteve Lawrence 	char *poolp;
247989b1c373Smeem 
2480f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2481f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2482f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
248389b1c373Smeem 		zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
248489b1c373Smeem 		    "'%s' which is used in the global zone", dlname);
2485f4b3ec61Sdh155122 		return (-1);
2486f4b3ec61Sdh155122 	}
2487f4b3ec61Sdh155122 
2488d62bc4baSyz147064 	/* Set zoneid of this link. */
24892b24ab6bSSebastien Roy 	err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
24902b24ab6bSSebastien Roy 	    DLADM_OPT_ACTIVE);
249189b1c373Smeem 	if (err != DLADM_STATUS_OK) {
249289b1c373Smeem 		zdlerror(zlogp, err, dlname,
249389b1c373Smeem 		    "WARNING: unable to add network interface");
2494f4b3ec61Sdh155122 		return (-1);
2495f4b3ec61Sdh155122 	}
24960dc2366fSVenugopal Iyer 
24970dc2366fSVenugopal Iyer 	/*
24980dc2366fSVenugopal Iyer 	 * Set the pool of this link if the zone has a pool and
24990dc2366fSVenugopal Iyer 	 * neither the cpus nor the pool datalink property is
25000dc2366fSVenugopal Iyer 	 * already set.
25010dc2366fSVenugopal Iyer 	 */
25020dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25030dc2366fSVenugopal Iyer 	    "cpus", &cpuset);
25040dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
25050dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
25060dc2366fSVenugopal Iyer 		    "WARNING: unable to check if cpus link property is set");
25070dc2366fSVenugopal Iyer 	}
25080dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25090dc2366fSVenugopal Iyer 	    "pool", &poolset);
25100dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
25110dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
25120dc2366fSVenugopal Iyer 		    "WARNING: unable to check if pool link property is set");
25130dc2366fSVenugopal Iyer 	}
25140dc2366fSVenugopal Iyer 
25150dc2366fSVenugopal Iyer 	if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2516efd4c9b6SSteve Lawrence 		poolp = pool_name;
25170dc2366fSVenugopal Iyer 		err = dladm_set_linkprop(dld_handle, linkid, "pool",
2518efd4c9b6SSteve Lawrence 		    &poolp, 1, DLADM_OPT_ACTIVE);
25190dc2366fSVenugopal Iyer 		if (err != DLADM_STATUS_OK) {
25200dc2366fSVenugopal Iyer 			zerror(zlogp, B_FALSE, "WARNING: unable to set "
25210dc2366fSVenugopal Iyer 			    "pool %s to datalink %s", pool_name, dlname);
2522*a3002e0aSGarrett D'Amore 			bzero(pool_name, sizeof (pool_name));
25230dc2366fSVenugopal Iyer 		}
25240dc2366fSVenugopal Iyer 	} else {
2525*a3002e0aSGarrett D'Amore 		bzero(pool_name, sizeof (pool_name));
25260dc2366fSVenugopal Iyer 	}
2527f4b3ec61Sdh155122 	return (0);
2528f4b3ec61Sdh155122 }
2529f4b3ec61Sdh155122 
2530550b6e40SSowmini Varadhan static boolean_t
2531550b6e40SSowmini Varadhan sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
2532550b6e40SSowmini Varadhan     char *straddr, size_t len)
2533550b6e40SSowmini Varadhan {
2534550b6e40SSowmini Varadhan 	struct sockaddr_in *sin;
2535550b6e40SSowmini Varadhan 	struct sockaddr_in6 *sin6;
2536550b6e40SSowmini Varadhan 	const char *str = NULL;
2537550b6e40SSowmini Varadhan 
2538550b6e40SSowmini Varadhan 	if (af == AF_INET) {
2539550b6e40SSowmini Varadhan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2540550b6e40SSowmini Varadhan 		sin = SIN(sockaddr);
2541550b6e40SSowmini Varadhan 		str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
2542550b6e40SSowmini Varadhan 	} else if (af == AF_INET6) {
2543550b6e40SSowmini Varadhan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2544550b6e40SSowmini Varadhan 		sin6 = SIN6(sockaddr);
2545550b6e40SSowmini Varadhan 		str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
2546550b6e40SSowmini Varadhan 		    len);
2547550b6e40SSowmini Varadhan 	}
2548550b6e40SSowmini Varadhan 
2549550b6e40SSowmini Varadhan 	return (str != NULL);
2550550b6e40SSowmini Varadhan }
2551550b6e40SSowmini Varadhan 
2552550b6e40SSowmini Varadhan static int
2553550b6e40SSowmini Varadhan ipv4_prefixlen(struct sockaddr_in *sin)
2554550b6e40SSowmini Varadhan {
2555550b6e40SSowmini Varadhan 	struct sockaddr_in *m;
2556550b6e40SSowmini Varadhan 	struct sockaddr_storage mask;
2557550b6e40SSowmini Varadhan 
2558550b6e40SSowmini Varadhan 	m = SIN(&mask);
2559550b6e40SSowmini Varadhan 	m->sin_family = AF_INET;
2560550b6e40SSowmini Varadhan 	if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
256164639aafSDarren Reed 		return (mask2plen((struct sockaddr *)&mask));
2562550b6e40SSowmini Varadhan 	} else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
2563550b6e40SSowmini Varadhan 		return (8);
2564550b6e40SSowmini Varadhan 	} else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
2565550b6e40SSowmini Varadhan 		return (16);
2566550b6e40SSowmini Varadhan 	} else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
2567550b6e40SSowmini Varadhan 		return (24);
2568550b6e40SSowmini Varadhan 	}
2569550b6e40SSowmini Varadhan 	return (0);
2570550b6e40SSowmini Varadhan }
2571550b6e40SSowmini Varadhan 
2572550b6e40SSowmini Varadhan static int
2573550b6e40SSowmini Varadhan zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
2574550b6e40SSowmini Varadhan     void *buf, size_t bufsize)
2575550b6e40SSowmini Varadhan {
2576550b6e40SSowmini Varadhan 	zone_net_data_t *zndata;
2577550b6e40SSowmini Varadhan 	size_t znsize;
2578550b6e40SSowmini Varadhan 	int err;
2579550b6e40SSowmini Varadhan 
2580550b6e40SSowmini Varadhan 	znsize = sizeof (*zndata) + bufsize;
2581550b6e40SSowmini Varadhan 	zndata = calloc(1, znsize);
2582550b6e40SSowmini Varadhan 	if (zndata == NULL)
2583550b6e40SSowmini Varadhan 		return (ENOMEM);
2584550b6e40SSowmini Varadhan 	zndata->zn_type = type;
2585550b6e40SSowmini Varadhan 	zndata->zn_len = bufsize;
2586550b6e40SSowmini Varadhan 	zndata->zn_linkid = linkid;
2587550b6e40SSowmini Varadhan 	bcopy(buf, zndata->zn_val, zndata->zn_len);
2588550b6e40SSowmini Varadhan 	err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
2589550b6e40SSowmini Varadhan 	free(zndata);
2590550b6e40SSowmini Varadhan 	return (err);
2591550b6e40SSowmini Varadhan }
2592550b6e40SSowmini Varadhan 
2593550b6e40SSowmini Varadhan static int
2594550b6e40SSowmini Varadhan add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
2595550b6e40SSowmini Varadhan {
2596550b6e40SSowmini Varadhan 	struct lifreq lifr;
2597550b6e40SSowmini Varadhan 	char **astr, *address;
2598550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
2599550b6e40SSowmini Varadhan 	char *ip_nospoof = "ip-nospoof";
2600550b6e40SSowmini Varadhan 	int nnet, naddr, err = 0, j;
2601550b6e40SSowmini Varadhan 	size_t zlen, cpleft;
2602550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *end;
2603550b6e40SSowmini Varadhan 	char  tmp[INET6_ADDRSTRLEN], *maskstr;
2604550b6e40SSowmini Varadhan 	char *zaddr, *cp;
2605550b6e40SSowmini Varadhan 	struct in6_addr *routes = NULL;
2606550b6e40SSowmini Varadhan 	boolean_t is_set;
2607550b6e40SSowmini Varadhan 	datalink_id_t linkid;
2608550b6e40SSowmini Varadhan 
2609550b6e40SSowmini Varadhan 	assert(start != NULL);
2610550b6e40SSowmini Varadhan 	naddr = 0; /* number of addresses */
2611550b6e40SSowmini Varadhan 	nnet = 0; /* number of net resources */
2612550b6e40SSowmini Varadhan 	linkid = start->za_linkid;
2613550b6e40SSowmini Varadhan 	for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
2614550b6e40SSowmini Varadhan 	    ptr = ptr->za_next) {
2615550b6e40SSowmini Varadhan 		nnet++;
2616550b6e40SSowmini Varadhan 	}
2617550b6e40SSowmini Varadhan 	end = ptr;
2618550b6e40SSowmini Varadhan 	zlen = nnet * (INET6_ADDRSTRLEN + 1);
2619550b6e40SSowmini Varadhan 	astr = calloc(1, nnet * sizeof (uintptr_t));
2620550b6e40SSowmini Varadhan 	zaddr = calloc(1, zlen);
2621550b6e40SSowmini Varadhan 	if (astr == NULL || zaddr == NULL) {
2622550b6e40SSowmini Varadhan 		err = ENOMEM;
2623550b6e40SSowmini Varadhan 		goto done;
2624550b6e40SSowmini Varadhan 	}
2625550b6e40SSowmini Varadhan 	cp = zaddr;
2626550b6e40SSowmini Varadhan 	cpleft = zlen;
2627550b6e40SSowmini Varadhan 	j = 0;
2628550b6e40SSowmini Varadhan 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
2629550b6e40SSowmini Varadhan 		address = ptr->za_nwiftab.zone_nwif_allowed_address;
2630550b6e40SSowmini Varadhan 		if (address[0] == '\0')
2631550b6e40SSowmini Varadhan 			continue;
2632550b6e40SSowmini Varadhan 		(void) snprintf(tmp, sizeof (tmp), "%s", address);
2633550b6e40SSowmini Varadhan 		/*
2634550b6e40SSowmini Varadhan 		 * Validate the data. zonecfg_valid_net_address() clobbers
2635550b6e40SSowmini Varadhan 		 * the /<mask> in the address string.
2636550b6e40SSowmini Varadhan 		 */
2637550b6e40SSowmini Varadhan 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2638550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "invalid address [%s]\n",
2639550b6e40SSowmini Varadhan 			    address);
2640550b6e40SSowmini Varadhan 			err = EINVAL;
2641550b6e40SSowmini Varadhan 			goto done;
2642550b6e40SSowmini Varadhan 		}
2643550b6e40SSowmini Varadhan 		/*
2644550b6e40SSowmini Varadhan 		 * convert any hostnames to numeric address strings.
2645550b6e40SSowmini Varadhan 		 */
2646550b6e40SSowmini Varadhan 		if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
2647550b6e40SSowmini Varadhan 		    (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
2648550b6e40SSowmini Varadhan 			err = EINVAL;
2649550b6e40SSowmini Varadhan 			goto done;
2650550b6e40SSowmini Varadhan 		}
2651550b6e40SSowmini Varadhan 		/*
2652550b6e40SSowmini Varadhan 		 * make a copy of the numeric string for the data needed
2653550b6e40SSowmini Varadhan 		 * by the "allowed-ips" datalink property.
2654550b6e40SSowmini Varadhan 		 */
2655550b6e40SSowmini Varadhan 		astr[j] = strdup(cp);
2656550b6e40SSowmini Varadhan 		if (astr[j] == NULL) {
2657550b6e40SSowmini Varadhan 			err = ENOMEM;
2658550b6e40SSowmini Varadhan 			goto done;
2659550b6e40SSowmini Varadhan 		}
2660550b6e40SSowmini Varadhan 		j++;
2661550b6e40SSowmini Varadhan 		/*
2662550b6e40SSowmini Varadhan 		 * compute the default netmask from the address, if necessary
2663550b6e40SSowmini Varadhan 		 */
2664550b6e40SSowmini Varadhan 		if ((maskstr = strchr(tmp, '/')) == NULL) {
2665550b6e40SSowmini Varadhan 			int prefixlen;
2666550b6e40SSowmini Varadhan 
2667550b6e40SSowmini Varadhan 			if (lifr.lifr_addr.ss_family == AF_INET) {
2668550b6e40SSowmini Varadhan 				prefixlen = ipv4_prefixlen(
2669550b6e40SSowmini Varadhan 				    SIN(&lifr.lifr_addr));
2670550b6e40SSowmini Varadhan 			} else {
2671550b6e40SSowmini Varadhan 				struct sockaddr_in6 *sin6;
2672550b6e40SSowmini Varadhan 
2673550b6e40SSowmini Varadhan 				sin6 = SIN6(&lifr.lifr_addr);
2674550b6e40SSowmini Varadhan 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2675550b6e40SSowmini Varadhan 					prefixlen = 10;
2676550b6e40SSowmini Varadhan 				else
2677550b6e40SSowmini Varadhan 					prefixlen = 64;
2678550b6e40SSowmini Varadhan 			}
2679550b6e40SSowmini Varadhan 			(void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
2680550b6e40SSowmini Varadhan 			maskstr = tmp;
2681550b6e40SSowmini Varadhan 		} else {
2682550b6e40SSowmini Varadhan 			maskstr++;
2683550b6e40SSowmini Varadhan 		}
2684550b6e40SSowmini Varadhan 		/* append the "/<netmask>" */
2685550b6e40SSowmini Varadhan 		(void) strlcat(cp, "/", cpleft);
2686550b6e40SSowmini Varadhan 		(void) strlcat(cp, maskstr, cpleft);
2687550b6e40SSowmini Varadhan 		(void) strlcat(cp, ",", cpleft);
2688550b6e40SSowmini Varadhan 		cp += strnlen(cp, zlen);
2689550b6e40SSowmini Varadhan 		cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
2690550b6e40SSowmini Varadhan 	}
2691550b6e40SSowmini Varadhan 	naddr = j; /* the actual number of addresses in the net resource */
2692550b6e40SSowmini Varadhan 	assert(naddr <= nnet);
2693550b6e40SSowmini Varadhan 
2694550b6e40SSowmini Varadhan 	/*
2695550b6e40SSowmini Varadhan 	 * zonecfg has already verified that the defrouter property can only
2696550b6e40SSowmini Varadhan 	 * be set if there is at least one address defined for the net resource.
2697550b6e40SSowmini Varadhan 	 * If j is 0, there are no addresses defined, and therefore no routers
2698550b6e40SSowmini Varadhan 	 * to configure, and we are done at that point.
2699550b6e40SSowmini Varadhan 	 */
2700550b6e40SSowmini Varadhan 	if (j == 0)
2701550b6e40SSowmini Varadhan 		goto done;
2702550b6e40SSowmini Varadhan 
2703550b6e40SSowmini Varadhan 	/* over-write last ',' with '\0' */
2704550b6e40SSowmini Varadhan 	zaddr[strnlen(zaddr, zlen) + 1] = '\0';
2705550b6e40SSowmini Varadhan 
2706550b6e40SSowmini Varadhan 	/*
2707550b6e40SSowmini Varadhan 	 * First make sure L3 protection is not already set on the link.
2708550b6e40SSowmini Varadhan 	 */
2709550b6e40SSowmini Varadhan 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2710550b6e40SSowmini Varadhan 	    "protection", &is_set);
2711550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2712550b6e40SSowmini Varadhan 		err = EINVAL;
2713550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "unable to check if protection is set");
2714550b6e40SSowmini Varadhan 		goto done;
2715550b6e40SSowmini Varadhan 	}
2716550b6e40SSowmini Varadhan 	if (is_set) {
2717550b6e40SSowmini Varadhan 		err = EINVAL;
2718550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "Protection is already set");
2719550b6e40SSowmini Varadhan 		goto done;
2720550b6e40SSowmini Varadhan 	}
2721550b6e40SSowmini Varadhan 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2722550b6e40SSowmini Varadhan 	    "allowed-ips", &is_set);
2723550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2724550b6e40SSowmini Varadhan 		err = EINVAL;
2725550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
2726550b6e40SSowmini Varadhan 		goto done;
2727550b6e40SSowmini Varadhan 	}
2728550b6e40SSowmini Varadhan 	if (is_set) {
2729550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "allowed-ips is already set");
2730550b6e40SSowmini Varadhan 		err = EINVAL;
2731550b6e40SSowmini Varadhan 		goto done;
2732550b6e40SSowmini Varadhan 	}
2733550b6e40SSowmini Varadhan 
2734550b6e40SSowmini Varadhan 	/*
2735550b6e40SSowmini Varadhan 	 * Enable ip-nospoof for the link, and add address to the allowed-ips
2736550b6e40SSowmini Varadhan 	 * list.
2737550b6e40SSowmini Varadhan 	 */
2738550b6e40SSowmini Varadhan 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
2739550b6e40SSowmini Varadhan 	    &ip_nospoof, 1, DLADM_OPT_ACTIVE);
2740550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2741550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "could not set protection\n");
2742550b6e40SSowmini Varadhan 		err = EINVAL;
2743550b6e40SSowmini Varadhan 		goto done;
2744550b6e40SSowmini Varadhan 	}
2745550b6e40SSowmini Varadhan 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
2746550b6e40SSowmini Varadhan 	    astr, naddr, DLADM_OPT_ACTIVE);
2747550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2748550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
2749550b6e40SSowmini Varadhan 		err = EINVAL;
2750550b6e40SSowmini Varadhan 		goto done;
2751550b6e40SSowmini Varadhan 	}
2752550b6e40SSowmini Varadhan 
2753550b6e40SSowmini Varadhan 	/* now set the address in the data-store */
2754550b6e40SSowmini Varadhan 	err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
2755550b6e40SSowmini Varadhan 	    zaddr, strnlen(zaddr, zlen) + 1);
2756550b6e40SSowmini Varadhan 	if (err != 0)
2757550b6e40SSowmini Varadhan 		goto done;
2758550b6e40SSowmini Varadhan 
2759550b6e40SSowmini Varadhan 	/*
2760550b6e40SSowmini Varadhan 	 * add the defaultrouters
2761550b6e40SSowmini Varadhan 	 */
2762550b6e40SSowmini Varadhan 	routes = calloc(1, nnet * sizeof (*routes));
2763550b6e40SSowmini Varadhan 	j = 0;
2764550b6e40SSowmini Varadhan 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
2765550b6e40SSowmini Varadhan 		address = ptr->za_nwiftab.zone_nwif_defrouter;
2766550b6e40SSowmini Varadhan 		if (address[0] == '\0')
2767550b6e40SSowmini Varadhan 			continue;
2768550b6e40SSowmini Varadhan 		if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
2769550b6e40SSowmini Varadhan 			/*
2770550b6e40SSowmini Varadhan 			 * zonecfg_valid_net_address() expects numeric IPv6
2771550b6e40SSowmini Varadhan 			 * addresses to have a CIDR format netmask.
2772550b6e40SSowmini Varadhan 			 */
2773550b6e40SSowmini Varadhan 			(void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
2774550b6e40SSowmini Varadhan 			(void) strlcat(address, tmp, INET6_ADDRSTRLEN);
2775550b6e40SSowmini Varadhan 		}
2776550b6e40SSowmini Varadhan 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2777550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE,
2778550b6e40SSowmini Varadhan 			    "invalid router [%s]\n", address);
2779550b6e40SSowmini Varadhan 			err = EINVAL;
2780550b6e40SSowmini Varadhan 			goto done;
2781550b6e40SSowmini Varadhan 		}
2782550b6e40SSowmini Varadhan 		if (lifr.lifr_addr.ss_family == AF_INET6) {
2783550b6e40SSowmini Varadhan 			routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
2784550b6e40SSowmini Varadhan 		} else {
2785550b6e40SSowmini Varadhan 			IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
2786550b6e40SSowmini Varadhan 			    &routes[j]);
2787550b6e40SSowmini Varadhan 		}
2788550b6e40SSowmini Varadhan 		j++;
2789550b6e40SSowmini Varadhan 	}
2790550b6e40SSowmini Varadhan 	assert(j <= nnet);
2791550b6e40SSowmini Varadhan 	if (j > 0) {
2792550b6e40SSowmini Varadhan 		err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
2793550b6e40SSowmini Varadhan 		    linkid, routes, j * sizeof (*routes));
2794550b6e40SSowmini Varadhan 	}
2795550b6e40SSowmini Varadhan done:
2796550b6e40SSowmini Varadhan 	free(routes);
2797550b6e40SSowmini Varadhan 	for (j = 0; j < naddr; j++)
2798550b6e40SSowmini Varadhan 		free(astr[j]);
2799550b6e40SSowmini Varadhan 	free(astr);
2800550b6e40SSowmini Varadhan 	free(zaddr);
2801550b6e40SSowmini Varadhan 	return (err);
2802550b6e40SSowmini Varadhan 
2803550b6e40SSowmini Varadhan }
2804550b6e40SSowmini Varadhan 
2805550b6e40SSowmini Varadhan static int
2806550b6e40SSowmini Varadhan add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
2807550b6e40SSowmini Varadhan {
2808550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr;
2809550b6e40SSowmini Varadhan 	datalink_id_t linkid;
2810550b6e40SSowmini Varadhan 	int err;
2811550b6e40SSowmini Varadhan 
2812550b6e40SSowmini Varadhan 	if (zalist == NULL)
2813550b6e40SSowmini Varadhan 		return (0);
2814550b6e40SSowmini Varadhan 
2815550b6e40SSowmini Varadhan 	linkid = zalist->za_linkid;
2816550b6e40SSowmini Varadhan 
2817550b6e40SSowmini Varadhan 	err = add_net_for_linkid(zlogp, zoneid, zalist);
2818550b6e40SSowmini Varadhan 	if (err != 0)
2819550b6e40SSowmini Varadhan 		return (err);
2820550b6e40SSowmini Varadhan 
2821550b6e40SSowmini Varadhan 	for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
2822550b6e40SSowmini Varadhan 		if (ptr->za_linkid == linkid)
2823550b6e40SSowmini Varadhan 			continue;
2824550b6e40SSowmini Varadhan 		linkid = ptr->za_linkid;
2825550b6e40SSowmini Varadhan 		err = add_net_for_linkid(zlogp, zoneid, ptr);
2826550b6e40SSowmini Varadhan 		if (err != 0)
2827550b6e40SSowmini Varadhan 			return (err);
2828550b6e40SSowmini Varadhan 	}
2829550b6e40SSowmini Varadhan 	return (0);
2830550b6e40SSowmini Varadhan }
2831550b6e40SSowmini Varadhan 
2832550b6e40SSowmini Varadhan /*
2833550b6e40SSowmini Varadhan  * Add "new" to the list of network interfaces to be configured  by
2834550b6e40SSowmini Varadhan  * add_net on zone boot in "old". The list of interfaces in "old" is
2835550b6e40SSowmini Varadhan  * sorted by datalink_id_t, with interfaces sorted FIFO for a given
2836550b6e40SSowmini Varadhan  * datalink_id_t.
2837550b6e40SSowmini Varadhan  *
2838550b6e40SSowmini Varadhan  * Returns the merged list of IP interfaces containing "old" and "new"
2839550b6e40SSowmini Varadhan  */
2840550b6e40SSowmini Varadhan static zone_addr_list_t *
2841550b6e40SSowmini Varadhan add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
2842550b6e40SSowmini Varadhan {
2843550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *next;
2844550b6e40SSowmini Varadhan 	datalink_id_t linkid = new->za_linkid;
2845550b6e40SSowmini Varadhan 
2846550b6e40SSowmini Varadhan 	assert(old != new);
2847550b6e40SSowmini Varadhan 
2848550b6e40SSowmini Varadhan 	if (old == NULL)
2849550b6e40SSowmini Varadhan 		return (new);
2850550b6e40SSowmini Varadhan 	for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
2851550b6e40SSowmini Varadhan 		if (ptr->za_linkid == linkid)
2852550b6e40SSowmini Varadhan 			break;
2853550b6e40SSowmini Varadhan 	}
2854550b6e40SSowmini Varadhan 	if (ptr == NULL) {
2855550b6e40SSowmini Varadhan 		/* linkid does not already exist, add to the beginning */
2856550b6e40SSowmini Varadhan 		new->za_next = old;
2857550b6e40SSowmini Varadhan 		return (new);
2858550b6e40SSowmini Varadhan 	}
2859550b6e40SSowmini Varadhan 	/*
2860550b6e40SSowmini Varadhan 	 * adding to the middle of the list; ptr points at the first
2861550b6e40SSowmini Varadhan 	 * occurrence of linkid. Find the last occurrence.
2862550b6e40SSowmini Varadhan 	 */
2863550b6e40SSowmini Varadhan 	while ((next = ptr->za_next) != NULL) {
2864550b6e40SSowmini Varadhan 		if (next->za_linkid != linkid)
2865550b6e40SSowmini Varadhan 			break;
2866550b6e40SSowmini Varadhan 		ptr = next;
2867550b6e40SSowmini Varadhan 	}
2868550b6e40SSowmini Varadhan 	/* insert new after ptr */
2869550b6e40SSowmini Varadhan 	new->za_next = next;
2870550b6e40SSowmini Varadhan 	ptr->za_next = new;
2871550b6e40SSowmini Varadhan 	return (old);
2872550b6e40SSowmini Varadhan }
2873550b6e40SSowmini Varadhan 
2874550b6e40SSowmini Varadhan void
2875550b6e40SSowmini Varadhan free_ip_interface(zone_addr_list_t *zalist)
2876550b6e40SSowmini Varadhan {
2877550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *new;
2878550b6e40SSowmini Varadhan 
2879550b6e40SSowmini Varadhan 	for (ptr = zalist; ptr != NULL; ) {
2880550b6e40SSowmini Varadhan 		new = ptr;
2881550b6e40SSowmini Varadhan 		ptr = ptr->za_next;
2882550b6e40SSowmini Varadhan 		free(new);
2883550b6e40SSowmini Varadhan 	}
2884550b6e40SSowmini Varadhan }
2885550b6e40SSowmini Varadhan 
2886f4b3ec61Sdh155122 /*
2887f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2888f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2889f4b3ec61Sdh155122  * whatever we set up, and return an error.
2890f4b3ec61Sdh155122  */
2891f4b3ec61Sdh155122 static int
2892550b6e40SSowmini Varadhan configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2893f4b3ec61Sdh155122 {
2894f4b3ec61Sdh155122 	zone_dochandle_t handle;
2895f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2896f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2897f4b3ec61Sdh155122 	char path[MAXPATHLEN];
28982b24ab6bSSebastien Roy 	datalink_id_t linkid;
2899f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2900f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2901550b6e40SSowmini Varadhan 	zone_addr_list_t *zalist = NULL, *new;
2902f4b3ec61Sdh155122 
2903f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2904f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2905f4b3ec61Sdh155122 		return (-1);
2906f4b3ec61Sdh155122 	}
2907f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2908f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2909f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2910f4b3ec61Sdh155122 		return (-1);
2911f4b3ec61Sdh155122 	}
2912f4b3ec61Sdh155122 
2913f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2914f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2915f4b3ec61Sdh155122 		return (0);
2916f4b3ec61Sdh155122 	}
2917f4b3ec61Sdh155122 
2918f4b3ec61Sdh155122 	for (;;) {
2919f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2920f4b3ec61Sdh155122 			break;
2921f4b3ec61Sdh155122 
2922f4b3ec61Sdh155122 		if (prof == NULL) {
2923f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2924f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2925f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2926f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2927f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2928f4b3ec61Sdh155122 				    "unable to determine dev root");
2929f4b3ec61Sdh155122 				return (-1);
2930f4b3ec61Sdh155122 			}
2931f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2932f4b3ec61Sdh155122 			    "/dev");
2933f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2934f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2935f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2936f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2937f4b3ec61Sdh155122 				    "failed to initialize profile");
2938f4b3ec61Sdh155122 				return (-1);
2939f4b3ec61Sdh155122 			}
2940f4b3ec61Sdh155122 		}
2941f4b3ec61Sdh155122 
2942f4b3ec61Sdh155122 		/*
2943d62bc4baSyz147064 		 * Create the /dev entry for backward compatibility.
2944f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2945d62bc4baSyz147064 		 * Note that the zone still boots when the assigned
2946d62bc4baSyz147064 		 * interface is inaccessible, used by others, etc.
2947d62bc4baSyz147064 		 * Also, when vanity naming is used, some interface do
2948d62bc4baSyz147064 		 * do not have corresponding /dev node names (for example,
2949d62bc4baSyz147064 		 * vanity named aggregations).  The /dev entry is not
2950d62bc4baSyz147064 		 * created in that case.  The /dev/net entry is always
2951d62bc4baSyz147064 		 * accessible.
2952f4b3ec61Sdh155122 		 */
29532b24ab6bSSebastien Roy 		if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
29542b24ab6bSSebastien Roy 		    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
29552b24ab6bSSebastien Roy 		    add_datalink(zlogp, zone_name, linkid,
29562b24ab6bSSebastien Roy 		    nwiftab.zone_nwif_physical) == 0) {
29573bc21d0aSAruna Ramakrishna - Sun Microsystems 			added = B_TRUE;
29583bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
2959f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2960f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
29613bc21d0aSAruna Ramakrishna - Sun Microsystems 			zerror(zlogp, B_TRUE, "failed to add network device");
2962f4b3ec61Sdh155122 			return (-1);
2963f4b3ec61Sdh155122 		}
2964550b6e40SSowmini Varadhan 		/* set up the new IP interface, and add them all later */
2965550b6e40SSowmini Varadhan 		new = malloc(sizeof (*new));
2966550b6e40SSowmini Varadhan 		if (new == NULL) {
2967550b6e40SSowmini Varadhan 			zerror(zlogp, B_TRUE, "no memory for %s",
2968550b6e40SSowmini Varadhan 			    nwiftab.zone_nwif_physical);
2969550b6e40SSowmini Varadhan 			zonecfg_fini_handle(handle);
2970550b6e40SSowmini Varadhan 			free_ip_interface(zalist);
2971550b6e40SSowmini Varadhan 		}
2972550b6e40SSowmini Varadhan 		bzero(new, sizeof (*new));
2973550b6e40SSowmini Varadhan 		new->za_nwiftab = nwiftab;
2974550b6e40SSowmini Varadhan 		new->za_linkid = linkid;
2975550b6e40SSowmini Varadhan 		zalist = add_ip_interface(zalist, new);
2976550b6e40SSowmini Varadhan 	}
2977550b6e40SSowmini Varadhan 	if (zalist != NULL) {
2978550b6e40SSowmini Varadhan 		if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
2979550b6e40SSowmini Varadhan 			(void) zonecfg_endnwifent(handle);
2980550b6e40SSowmini Varadhan 			zonecfg_fini_handle(handle);
2981550b6e40SSowmini Varadhan 			zerror(zlogp, B_TRUE, "failed to add address");
2982550b6e40SSowmini Varadhan 			free_ip_interface(zalist);
2983550b6e40SSowmini Varadhan 			return (-1);
2984550b6e40SSowmini Varadhan 		}
2985550b6e40SSowmini Varadhan 		free_ip_interface(zalist);
2986d62bc4baSyz147064 	}
2987f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2988f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2989f4b3ec61Sdh155122 
2990f4b3ec61Sdh155122 	if (prof != NULL && added) {
2991f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2992f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
2993f4b3ec61Sdh155122 			return (-1);
2994f4b3ec61Sdh155122 		}
2995f4b3ec61Sdh155122 	}
2996f4b3ec61Sdh155122 	if (prof != NULL)
2997f4b3ec61Sdh155122 		di_prof_fini(prof);
2998f4b3ec61Sdh155122 
2999f4b3ec61Sdh155122 	return (0);
3000f4b3ec61Sdh155122 }
3001f4b3ec61Sdh155122 
3002f4b3ec61Sdh155122 static int
30030dc2366fSVenugopal Iyer remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
30040dc2366fSVenugopal Iyer {
30050dc2366fSVenugopal Iyer 	ushort_t flags;
30060dc2366fSVenugopal Iyer 	zone_iptype_t iptype;
30070dc2366fSVenugopal Iyer 	int i, dlnum = 0;
30080dc2366fSVenugopal Iyer 	datalink_id_t *dllink, *dllinks = NULL;
30090dc2366fSVenugopal Iyer 	dladm_status_t err;
30100dc2366fSVenugopal Iyer 
30110dc2366fSVenugopal Iyer 	if (strlen(pool_name) == 0)
30120dc2366fSVenugopal Iyer 		return (0);
30130dc2366fSVenugopal Iyer 
30140dc2366fSVenugopal Iyer 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
30150dc2366fSVenugopal Iyer 	    sizeof (flags)) < 0) {
30160dc2366fSVenugopal Iyer 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
3017550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
30180dc2366fSVenugopal Iyer 			return (-1);
30190dc2366fSVenugopal Iyer 		}
30200dc2366fSVenugopal Iyer 	} else {
30210dc2366fSVenugopal Iyer 		if (flags & ZF_NET_EXCL)
30220dc2366fSVenugopal Iyer 			iptype = ZS_EXCLUSIVE;
30230dc2366fSVenugopal Iyer 		else
30240dc2366fSVenugopal Iyer 			iptype = ZS_SHARED;
30250dc2366fSVenugopal Iyer 	}
30260dc2366fSVenugopal Iyer 
30270dc2366fSVenugopal Iyer 	if (iptype == ZS_EXCLUSIVE) {
30280dc2366fSVenugopal Iyer 		/*
30290dc2366fSVenugopal Iyer 		 * Get the datalink count and for each datalink,
30300dc2366fSVenugopal Iyer 		 * attempt to clear the pool property and clear
30310dc2366fSVenugopal Iyer 		 * the pool_name.
30320dc2366fSVenugopal Iyer 		 */
30330dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
30340dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to count network "
30350dc2366fSVenugopal Iyer 			    "interfaces");
30360dc2366fSVenugopal Iyer 			return (-1);
30370dc2366fSVenugopal Iyer 		}
30380dc2366fSVenugopal Iyer 
30390dc2366fSVenugopal Iyer 		if (dlnum == 0)
30400dc2366fSVenugopal Iyer 			return (0);
30410dc2366fSVenugopal Iyer 
30420dc2366fSVenugopal Iyer 		if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
30430dc2366fSVenugopal Iyer 		    == NULL) {
30440dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "memory allocation failed");
30450dc2366fSVenugopal Iyer 			return (-1);
30460dc2366fSVenugopal Iyer 		}
30470dc2366fSVenugopal Iyer 		if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
30480dc2366fSVenugopal Iyer 			zerror(zlogp, B_TRUE, "unable to list network "
30490dc2366fSVenugopal Iyer 			    "interfaces");
30500dc2366fSVenugopal Iyer 			return (-1);
30510dc2366fSVenugopal Iyer 		}
30520dc2366fSVenugopal Iyer 
3053*a3002e0aSGarrett D'Amore 		bzero(pool_name, sizeof (pool_name));
30540dc2366fSVenugopal Iyer 		for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
30550dc2366fSVenugopal Iyer 			err = dladm_set_linkprop(dld_handle, *dllink, "pool",
30560dc2366fSVenugopal Iyer 			    NULL, 0, DLADM_OPT_ACTIVE);
30570dc2366fSVenugopal Iyer 			if (err != DLADM_STATUS_OK) {
30580dc2366fSVenugopal Iyer 				zerror(zlogp, B_TRUE,
30590dc2366fSVenugopal Iyer 				    "WARNING: unable to clear pool");
30600dc2366fSVenugopal Iyer 			}
30610dc2366fSVenugopal Iyer 		}
30620dc2366fSVenugopal Iyer 		free(dllinks);
30630dc2366fSVenugopal Iyer 	}
30640dc2366fSVenugopal Iyer 	return (0);
30650dc2366fSVenugopal Iyer }
30660dc2366fSVenugopal Iyer 
30670dc2366fSVenugopal Iyer static int
3068550b6e40SSowmini Varadhan remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
3069550b6e40SSowmini Varadhan {
3070550b6e40SSowmini Varadhan 	ushort_t flags;
3071550b6e40SSowmini Varadhan 	zone_iptype_t iptype;
3072550b6e40SSowmini Varadhan 	int i, dlnum = 0;
3073550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
3074550b6e40SSowmini Varadhan 	datalink_id_t *dllink, *dllinks = NULL;
3075550b6e40SSowmini Varadhan 
3076550b6e40SSowmini Varadhan 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3077550b6e40SSowmini Varadhan 	    sizeof (flags)) < 0) {
3078550b6e40SSowmini Varadhan 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
3079550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
3080550b6e40SSowmini Varadhan 			return (-1);
3081550b6e40SSowmini Varadhan 		}
3082550b6e40SSowmini Varadhan 	} else {
3083550b6e40SSowmini Varadhan 		if (flags & ZF_NET_EXCL)
3084550b6e40SSowmini Varadhan 			iptype = ZS_EXCLUSIVE;
3085550b6e40SSowmini Varadhan 		else
3086550b6e40SSowmini Varadhan 			iptype = ZS_SHARED;
3087550b6e40SSowmini Varadhan 	}
3088550b6e40SSowmini Varadhan 
3089550b6e40SSowmini Varadhan 	if (iptype != ZS_EXCLUSIVE)
3090550b6e40SSowmini Varadhan 		return (0);
3091550b6e40SSowmini Varadhan 
3092550b6e40SSowmini Varadhan 	/*
3093550b6e40SSowmini Varadhan 	 * Get the datalink count and for each datalink,
3094550b6e40SSowmini Varadhan 	 * attempt to clear the pool property and clear
3095550b6e40SSowmini Varadhan 	 * the pool_name.
3096550b6e40SSowmini Varadhan 	 */
3097550b6e40SSowmini Varadhan 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3098550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "unable to count network interfaces");
3099550b6e40SSowmini Varadhan 		return (-1);
3100550b6e40SSowmini Varadhan 	}
3101550b6e40SSowmini Varadhan 
3102550b6e40SSowmini Varadhan 	if (dlnum == 0)
3103550b6e40SSowmini Varadhan 		return (0);
3104550b6e40SSowmini Varadhan 
3105550b6e40SSowmini Varadhan 	if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
3106550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "memory allocation failed");
3107550b6e40SSowmini Varadhan 		return (-1);
3108550b6e40SSowmini Varadhan 	}
3109550b6e40SSowmini Varadhan 	if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3110550b6e40SSowmini Varadhan 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
3111550b6e40SSowmini Varadhan 		free(dllinks);
3112550b6e40SSowmini Varadhan 		return (-1);
3113550b6e40SSowmini Varadhan 	}
3114550b6e40SSowmini Varadhan 
3115550b6e40SSowmini Varadhan 	for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3116fff7ec1dSSowmini Varadhan 		char dlerr[DLADM_STRSIZE];
3117fff7ec1dSSowmini Varadhan 
3118550b6e40SSowmini Varadhan 		dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3119550b6e40SSowmini Varadhan 		    "protection", NULL, 0, DLADM_OPT_ACTIVE);
3120fff7ec1dSSowmini Varadhan 		if (dlstatus == DLADM_STATUS_NOTFOUND) {
3121fff7ec1dSSowmini Varadhan 			/* datalink does not belong to the GZ */
3122fff7ec1dSSowmini Varadhan 			continue;
3123fff7ec1dSSowmini Varadhan 		}
3124550b6e40SSowmini Varadhan 		if (dlstatus != DLADM_STATUS_OK) {
3125fff7ec1dSSowmini Varadhan 			zerror(zlogp, B_FALSE,
3126fff7ec1dSSowmini Varadhan 			    dladm_status2str(dlstatus, dlerr));
3127550b6e40SSowmini Varadhan 			free(dllinks);
3128550b6e40SSowmini Varadhan 			return (-1);
3129550b6e40SSowmini Varadhan 		}
3130550b6e40SSowmini Varadhan 		dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3131550b6e40SSowmini Varadhan 		    "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
3132550b6e40SSowmini Varadhan 		if (dlstatus != DLADM_STATUS_OK) {
3133fff7ec1dSSowmini Varadhan 			zerror(zlogp, B_FALSE,
3134fff7ec1dSSowmini Varadhan 			    dladm_status2str(dlstatus, dlerr));
3135550b6e40SSowmini Varadhan 			free(dllinks);
3136550b6e40SSowmini Varadhan 			return (-1);
3137550b6e40SSowmini Varadhan 		}
3138550b6e40SSowmini Varadhan 	}
3139550b6e40SSowmini Varadhan 	free(dllinks);
3140550b6e40SSowmini Varadhan 	return (0);
3141550b6e40SSowmini Varadhan }
3142550b6e40SSowmini Varadhan 
3143550b6e40SSowmini Varadhan static int
31442b24ab6bSSebastien Roy unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
3145f4b3ec61Sdh155122 {
31462b24ab6bSSebastien Roy 	int dlnum = 0;
3147f4b3ec61Sdh155122 
31482b24ab6bSSebastien Roy 	/*
31492b24ab6bSSebastien Roy 	 * The kernel shutdown callback for the dls module should have removed
31502b24ab6bSSebastien Roy 	 * all datalinks from this zone.  If any remain, then there's a
31512b24ab6bSSebastien Roy 	 * problem.
31522b24ab6bSSebastien Roy 	 */
3153f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3154f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
3155f4b3ec61Sdh155122 		return (-1);
3156f4b3ec61Sdh155122 	}
31572b24ab6bSSebastien Roy 	if (dlnum != 0) {
31582b24ab6bSSebastien Roy 		zerror(zlogp, B_FALSE,
31592b24ab6bSSebastien Roy 		    "datalinks remain in zone after shutdown");
3160f4b3ec61Sdh155122 		return (-1);
3161f4b3ec61Sdh155122 	}
3162f4b3ec61Sdh155122 	return (0);
3163f4b3ec61Sdh155122 }
3164f4b3ec61Sdh155122 
31657c478bd9Sstevel@tonic-gate static int
31667c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
31677c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
31687c478bd9Sstevel@tonic-gate {
31697c478bd9Sstevel@tonic-gate 	int fd;
31707c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
31717c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
31727c478bd9Sstevel@tonic-gate 	int error;
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
31757c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
31767c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
31777c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
31787c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
31817c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
31827c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
31837c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
31867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
31877c478bd9Sstevel@tonic-gate 		return (-1);
31887c478bd9Sstevel@tonic-gate 	}
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
31917c478bd9Sstevel@tonic-gate 	(void) close(fd);
31927c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
31937c478bd9Sstevel@tonic-gate 		return (0);
31947c478bd9Sstevel@tonic-gate 	return (-1);
31957c478bd9Sstevel@tonic-gate }
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate static int
31987c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
31997c478bd9Sstevel@tonic-gate {
32007c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
32017c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
32027c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
32037c478bd9Sstevel@tonic-gate 	int error;
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 	/*
32067c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
32077c478bd9Sstevel@tonic-gate 	 */
32087c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
32097c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
32107c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
32117c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
32127c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
32157c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
32167c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
32177c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
32187c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32217c478bd9Sstevel@tonic-gate 		return (error);
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	/*
32247c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
32257c478bd9Sstevel@tonic-gate 	 */
32267c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
32277c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
32287c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
32297c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
32307c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
32337c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
32347c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
32357c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
32367c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32397c478bd9Sstevel@tonic-gate 		return (error);
32407c478bd9Sstevel@tonic-gate 	return (0);
32417c478bd9Sstevel@tonic-gate }
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate static int
32446cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
3245ffbafc53Scomay {
3246ffbafc53Scomay 	int error = -1;
3247ffbafc53Scomay 	zone_dochandle_t handle;
3248ffbafc53Scomay 	char *privname = NULL;
3249ffbafc53Scomay 
3250ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
3251ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3252ffbafc53Scomay 		return (-1);
3253ffbafc53Scomay 	}
3254ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3255ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
3256ffbafc53Scomay 		zonecfg_fini_handle(handle);
3257ffbafc53Scomay 		return (-1);
3258ffbafc53Scomay 	}
3259ffbafc53Scomay 
32606cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
3261bf1d7e28Sdh155122 		zone_iptype_t	iptype;
3262bf1d7e28Sdh155122 		const char	*curr_iptype;
3263bf1d7e28Sdh155122 
3264bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
3265bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
3266bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
3267bf1d7e28Sdh155122 			return (-1);
3268bf1d7e28Sdh155122 		}
3269bf1d7e28Sdh155122 
3270bf1d7e28Sdh155122 		switch (iptype) {
3271bf1d7e28Sdh155122 		case ZS_SHARED:
3272bf1d7e28Sdh155122 			curr_iptype = "shared";
3273bf1d7e28Sdh155122 			break;
3274bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
3275bf1d7e28Sdh155122 			curr_iptype = "exclusive";
3276bf1d7e28Sdh155122 			break;
3277bf1d7e28Sdh155122 		}
3278bf1d7e28Sdh155122 
3279e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
3280e767a340Sgjelinek 			zonecfg_fini_handle(handle);
32819acbbeafSnn35248 			return (0);
3282e767a340Sgjelinek 		}
32839acbbeafSnn35248 		zerror(zlogp, B_FALSE,
32849acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
32859acbbeafSnn35248 		zonecfg_fini_handle(handle);
32869acbbeafSnn35248 		return (-1);
32879acbbeafSnn35248 	}
32889acbbeafSnn35248 
3289ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
3290ffbafc53Scomay 	case Z_OK:
3291ffbafc53Scomay 		error = 0;
3292ffbafc53Scomay 		break;
3293ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3294ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3295ffbafc53Scomay 		    "within the zone's privilege set", privname);
3296ffbafc53Scomay 		break;
3297ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3298ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3299ffbafc53Scomay 		    "from the zone's privilege set", privname);
3300ffbafc53Scomay 		break;
3301ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3302ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3303ffbafc53Scomay 		    "in the zone's privilege set", privname);
3304ffbafc53Scomay 		break;
3305ffbafc53Scomay 	default:
3306ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
3307ffbafc53Scomay 		    "privilege set");
3308ffbafc53Scomay 		break;
3309ffbafc53Scomay 	}
3310ffbafc53Scomay 
3311ffbafc53Scomay 	free(privname);
3312ffbafc53Scomay 	zonecfg_fini_handle(handle);
3313ffbafc53Scomay 	return (error);
3314ffbafc53Scomay }
3315ffbafc53Scomay 
3316ffbafc53Scomay static int
33177c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
33187c478bd9Sstevel@tonic-gate {
33197c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
33207c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
33217c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
33227c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
33237c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
33247c478bd9Sstevel@tonic-gate 	int error = -1;
33257c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
33267c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
33277c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
3328ff19e029SMenno Lageman 	uint64_t maxlwps;
3329ff19e029SMenno Lageman 	uint64_t maxprocs;
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	*bufp = NULL;
33327c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
33357c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
33367c478bd9Sstevel@tonic-gate 		return (-1);
33377c478bd9Sstevel@tonic-gate 	}
33387c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
33397c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
33407c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
33417c478bd9Sstevel@tonic-gate 		return (-1);
33427c478bd9Sstevel@tonic-gate 	}
33437c478bd9Sstevel@tonic-gate 
33447c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
33457c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
33467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
33477c478bd9Sstevel@tonic-gate 		goto out;
33487c478bd9Sstevel@tonic-gate 	}
33497c478bd9Sstevel@tonic-gate 
3350ff19e029SMenno Lageman 	/*
3351ff19e029SMenno Lageman 	 * Allow the administrator to control both the maximum number of
3352ff19e029SMenno Lageman 	 * process table slots and the maximum number of lwps with just the
3353ff19e029SMenno Lageman 	 * max-processes property.  If only the max-processes property is set,
3354ff19e029SMenno Lageman 	 * we add a max-lwps property with a limit derived from max-processes.
3355ff19e029SMenno Lageman 	 */
3356ff19e029SMenno Lageman 	if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs)
3357ff19e029SMenno Lageman 	    == Z_OK &&
3358ff19e029SMenno Lageman 	    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps)
3359ff19e029SMenno Lageman 	    == Z_NO_ENTRY) {
3360ff19e029SMenno Lageman 		if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
3361ff19e029SMenno Lageman 		    maxprocs * LWPS_PER_PROCESS) != Z_OK) {
3362ff19e029SMenno Lageman 			zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
3363ff19e029SMenno Lageman 			goto out;
3364ff19e029SMenno Lageman 		}
3365ff19e029SMenno Lageman 	}
3366ff19e029SMenno Lageman 
33677c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
33687c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
33697c478bd9Sstevel@tonic-gate 		goto out;
33707c478bd9Sstevel@tonic-gate 	}
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
33737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
33747c478bd9Sstevel@tonic-gate 		goto out;
33757c478bd9Sstevel@tonic-gate 	}
33767c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
33777c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
33787c478bd9Sstevel@tonic-gate 		uint_t i, count;
33797c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
33827c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
33837c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
33847c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
33857c478bd9Sstevel@tonic-gate 			continue;
33867c478bd9Sstevel@tonic-gate 		}
33877c478bd9Sstevel@tonic-gate 		count = 0;
33887c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
33897c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
33907c478bd9Sstevel@tonic-gate 			count++;
33917c478bd9Sstevel@tonic-gate 		}
33927c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
33937c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
33947c478bd9Sstevel@tonic-gate 		}
33957c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
33967c478bd9Sstevel@tonic-gate 			goto out;
33977c478bd9Sstevel@tonic-gate 		i = 0;
33987c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
33997c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
34007c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
34017c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
34027c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
34037c478bd9Sstevel@tonic-gate 				goto out;
34047c478bd9Sstevel@tonic-gate 			}
34057c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
34067c478bd9Sstevel@tonic-gate 			    != Z_OK) {
34077c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
34087c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
34097c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
34107c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
34117c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
34127c478bd9Sstevel@tonic-gate 				goto out;
34137c478bd9Sstevel@tonic-gate 			}
34147c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
34157c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
34167c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
34177c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
34187c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
34197c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
34207c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
34217c478bd9Sstevel@tonic-gate 				    name);
34227c478bd9Sstevel@tonic-gate 				goto out;
34237c478bd9Sstevel@tonic-gate 			}
34247c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
34257c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
34267c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34277c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34287c478bd9Sstevel@tonic-gate 				goto out;
34297c478bd9Sstevel@tonic-gate 			}
34307c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
34317c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
34327c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34337c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34347c478bd9Sstevel@tonic-gate 				goto out;
34357c478bd9Sstevel@tonic-gate 			}
34367c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
34377c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
34387c478bd9Sstevel@tonic-gate 			    != 0) {
34397c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34407c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
34417c478bd9Sstevel@tonic-gate 				goto out;
34427c478bd9Sstevel@tonic-gate 			}
34437c478bd9Sstevel@tonic-gate 		}
34447c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34457c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
34467c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
34477c478bd9Sstevel@tonic-gate 		    != 0) {
34487c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
34497c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
34507c478bd9Sstevel@tonic-gate 			goto out;
34517c478bd9Sstevel@tonic-gate 		}
34527c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
34537c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
34547c478bd9Sstevel@tonic-gate 		free(nvlv);
34557c478bd9Sstevel@tonic-gate 		nvlv = NULL;
34567c478bd9Sstevel@tonic-gate 		rctlcount++;
34577c478bd9Sstevel@tonic-gate 	}
34587c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
34617c478bd9Sstevel@tonic-gate 		error = 0;
34627c478bd9Sstevel@tonic-gate 		goto out;
34637c478bd9Sstevel@tonic-gate 	}
34647c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
34657c478bd9Sstevel@tonic-gate 	    != 0) {
34667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
34677c478bd9Sstevel@tonic-gate 		goto out;
34687c478bd9Sstevel@tonic-gate 	}
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	error = 0;
34717c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
34727c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate out:
34757c478bd9Sstevel@tonic-gate 	free(rctlblk);
34767c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34777c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
34787c478bd9Sstevel@tonic-gate 		free(nvl_packed);
34797c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
34807c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
34817c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
34827c478bd9Sstevel@tonic-gate 		free(nvlv);
34837c478bd9Sstevel@tonic-gate 	if (handle != NULL)
34847c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
34857c478bd9Sstevel@tonic-gate 	return (error);
34867c478bd9Sstevel@tonic-gate }
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate static int
3489c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr)
3490c5cd6260S {
3491c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
3492c5cd6260S 
3493c5cd6260S 	if (query_hook[0] == '\0')
3494c5cd6260S 		return (0);
3495c5cd6260S 
3496c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3497c5cd6260S 	    > sizeof (cmdbuf))
3498c5cd6260S 		return (-1);
3499c5cd6260S 
3500c5cd6260S 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3501c5cd6260S 		return (-1);
3502c5cd6260S 
3503c5cd6260S 	return (0);
3504c5cd6260S }
3505c5cd6260S 
3506c5cd6260S static int
3507fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3508fa9e4066Sahrens {
3509fa9e4066Sahrens 	zone_dochandle_t handle;
3510fa9e4066Sahrens 	struct zone_dstab dstab;
3511fa9e4066Sahrens 	size_t total, offset, len;
3512fa9e4066Sahrens 	int error = -1;
3513e5a17f6aSgjelinek 	char *str = NULL;
3514c5cd6260S 	char *implicit_datasets = NULL;
3515c5cd6260S 	int implicit_len = 0;
3516fa9e4066Sahrens 
3517fa9e4066Sahrens 	*bufp = NULL;
3518fa9e4066Sahrens 	*bufsizep = 0;
3519fa9e4066Sahrens 
3520fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3521fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3522fa9e4066Sahrens 		return (-1);
3523fa9e4066Sahrens 	}
3524fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3525fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3526fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3527fa9e4066Sahrens 		return (-1);
3528fa9e4066Sahrens 	}
3529fa9e4066Sahrens 
3530c5cd6260S 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3531c5cd6260S 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3532c5cd6260S 		goto out;
3533c5cd6260S 	}
3534c5cd6260S 
3535fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3536fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3537fa9e4066Sahrens 		goto out;
3538fa9e4066Sahrens 	}
3539fa9e4066Sahrens 
3540fa9e4066Sahrens 	total = 0;
3541fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3542fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3543fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3544fa9e4066Sahrens 
3545c5cd6260S 	if (implicit_datasets != NULL)
3546c5cd6260S 		implicit_len = strlen(implicit_datasets);
3547c5cd6260S 	if (implicit_len > 0)
3548c5cd6260S 		total += implicit_len + 1;
3549c5cd6260S 
3550fa9e4066Sahrens 	if (total == 0) {
3551fa9e4066Sahrens 		error = 0;
3552fa9e4066Sahrens 		goto out;
3553fa9e4066Sahrens 	}
3554fa9e4066Sahrens 
3555fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3556fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3557fa9e4066Sahrens 		goto out;
3558fa9e4066Sahrens 	}
3559fa9e4066Sahrens 
3560fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3561fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3562fa9e4066Sahrens 		goto out;
3563fa9e4066Sahrens 	}
3564fa9e4066Sahrens 	offset = 0;
3565fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3566fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3567fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3568e5a17f6aSgjelinek 		    total - offset);
3569fa9e4066Sahrens 		offset += len;
3570e5a17f6aSgjelinek 		if (offset < total - 1)
3571fa9e4066Sahrens 			str[offset++] = ',';
3572fa9e4066Sahrens 	}
3573fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3574fa9e4066Sahrens 
3575c5cd6260S 	if (implicit_len > 0)
3576c5cd6260S 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3577c5cd6260S 
3578fa9e4066Sahrens 	error = 0;
3579fa9e4066Sahrens 	*bufp = str;
3580fa9e4066Sahrens 	*bufsizep = total;
3581fa9e4066Sahrens 
3582fa9e4066Sahrens out:
3583fa9e4066Sahrens 	if (error != 0 && str != NULL)
3584fa9e4066Sahrens 		free(str);
3585fa9e4066Sahrens 	if (handle != NULL)
3586fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3587c5cd6260S 	if (implicit_datasets != NULL)
3588c5cd6260S 		free(implicit_datasets);
3589fa9e4066Sahrens 
3590fa9e4066Sahrens 	return (error);
3591fa9e4066Sahrens }
3592fa9e4066Sahrens 
3593fa9e4066Sahrens static int
3594fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3595fa9e4066Sahrens {
3596fa9e4066Sahrens 	zone_dochandle_t handle;
3597fa9e4066Sahrens 	struct zone_dstab dstab;
3598fa9e4066Sahrens 	zfs_handle_t *zhp;
359999653d4eSeschrock 	libzfs_handle_t *hdl;
3600fa9e4066Sahrens 
3601fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3602fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3603fa9e4066Sahrens 		return (-1);
3604fa9e4066Sahrens 	}
3605fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3606fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3607fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3608fa9e4066Sahrens 		return (-1);
3609fa9e4066Sahrens 	}
3610fa9e4066Sahrens 
3611fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3612fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3613fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3614fa9e4066Sahrens 		return (-1);
3615fa9e4066Sahrens 	}
3616fa9e4066Sahrens 
361799653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
361899653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
361999653d4eSeschrock 		zonecfg_fini_handle(handle);
362099653d4eSeschrock 		return (-1);
362199653d4eSeschrock 	}
3622fa9e4066Sahrens 
3623fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3624fa9e4066Sahrens 
362599653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3626fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3627fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3628fa9e4066Sahrens 			    dstab.zone_dataset_name);
3629fa9e4066Sahrens 			zonecfg_fini_handle(handle);
363099653d4eSeschrock 			libzfs_fini(hdl);
3631fa9e4066Sahrens 			return (-1);
3632fa9e4066Sahrens 		}
3633fa9e4066Sahrens 
3634fa9e4066Sahrens 		/*
3635fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3636fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3637fa9e4066Sahrens 		 */
3638fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3639e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3640e9dbad6fSeschrock 		    "on") != 0) {
3641fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3642fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3643fa9e4066Sahrens 			    dstab.zone_dataset_name);
3644fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3645fa9e4066Sahrens 			zfs_close(zhp);
364699653d4eSeschrock 			libzfs_fini(hdl);
3647fa9e4066Sahrens 			return (-1);
3648fa9e4066Sahrens 		}
3649fa9e4066Sahrens 
3650fa9e4066Sahrens 		zfs_close(zhp);
3651fa9e4066Sahrens 	}
3652fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3653fa9e4066Sahrens 
3654fa9e4066Sahrens 	zonecfg_fini_handle(handle);
365599653d4eSeschrock 	libzfs_fini(hdl);
3656fa9e4066Sahrens 
3657fa9e4066Sahrens 	return (0);
3658fa9e4066Sahrens }
3659fa9e4066Sahrens 
366045916cd2Sjpk /*
36614201a95eSRic Aleshire  * Return true if the path is its own zfs file system.  We determine this
36624201a95eSRic Aleshire  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
36634201a95eSRic Aleshire  * if it is a different fs.
36644201a95eSRic Aleshire  */
36654201a95eSRic Aleshire boolean_t
36664201a95eSRic Aleshire is_zonepath_zfs(char *zonepath)
36674201a95eSRic Aleshire {
36684201a95eSRic Aleshire 	int res;
36694201a95eSRic Aleshire 	char *path;
36704201a95eSRic Aleshire 	char *parent;
36714201a95eSRic Aleshire 	struct statvfs64 buf1, buf2;
36724201a95eSRic Aleshire 
36734201a95eSRic Aleshire 	if (statvfs64(zonepath, &buf1) != 0)
36744201a95eSRic Aleshire 		return (B_FALSE);
36754201a95eSRic Aleshire 
36764201a95eSRic Aleshire 	if (strcmp(buf1.f_basetype, "zfs") != 0)
36774201a95eSRic Aleshire 		return (B_FALSE);
36784201a95eSRic Aleshire 
36794201a95eSRic Aleshire 	if ((path = strdup(zonepath)) == NULL)
36804201a95eSRic Aleshire 		return (B_FALSE);
36814201a95eSRic Aleshire 
36824201a95eSRic Aleshire 	parent = dirname(path);
36834201a95eSRic Aleshire 	res = statvfs64(parent, &buf2);
36844201a95eSRic Aleshire 	free(path);
36854201a95eSRic Aleshire 
36864201a95eSRic Aleshire 	if (res != 0)
36874201a95eSRic Aleshire 		return (B_FALSE);
36884201a95eSRic Aleshire 
36894201a95eSRic Aleshire 	if (buf1.f_fsid == buf2.f_fsid)
36904201a95eSRic Aleshire 		return (B_FALSE);
36914201a95eSRic Aleshire 
36924201a95eSRic Aleshire 	return (B_TRUE);
36934201a95eSRic Aleshire }
36944201a95eSRic Aleshire 
36954201a95eSRic Aleshire /*
36964201a95eSRic Aleshire  * Verify the MAC label in the root dataset for the zone.
36974201a95eSRic Aleshire  * If the label exists, it must match the label configured for the zone.
36984201a95eSRic Aleshire  * Otherwise if there's no label on the dataset, create one here.
36994201a95eSRic Aleshire  */
37004201a95eSRic Aleshire 
37014201a95eSRic Aleshire static int
37024201a95eSRic Aleshire validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
37034201a95eSRic Aleshire {
37044201a95eSRic Aleshire 	int		error = -1;
37054201a95eSRic Aleshire 	zfs_handle_t	*zhp;
37064201a95eSRic Aleshire 	libzfs_handle_t	*hdl;
37074201a95eSRic Aleshire 	m_label_t	ds_sl;
37084201a95eSRic Aleshire 	char		zonepath[MAXPATHLEN];
37094201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
37104201a95eSRic Aleshire 
37114201a95eSRic Aleshire 	if (!is_system_labeled())
37124201a95eSRic Aleshire 		return (0);
37134201a95eSRic Aleshire 
37144201a95eSRic Aleshire 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
37154201a95eSRic Aleshire 		zerror(zlogp, B_TRUE, "unable to determine zone path");
37164201a95eSRic Aleshire 		return (-1);
37174201a95eSRic Aleshire 	}
37184201a95eSRic Aleshire 
37194201a95eSRic Aleshire 	if (!is_zonepath_zfs(zonepath))
37204201a95eSRic Aleshire 		return (0);
37214201a95eSRic Aleshire 
37224201a95eSRic Aleshire 	if ((hdl = libzfs_init()) == NULL) {
37234201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "opening ZFS library");
37244201a95eSRic Aleshire 		return (-1);
37254201a95eSRic Aleshire 	}
37264201a95eSRic Aleshire 
37274201a95eSRic Aleshire 	if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
37284201a95eSRic Aleshire 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
37294201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
37304201a95eSRic Aleshire 		    rootpath);
37314201a95eSRic Aleshire 		libzfs_fini(hdl);
37324201a95eSRic Aleshire 		return (-1);
37334201a95eSRic Aleshire 	}
37344201a95eSRic Aleshire 
37354201a95eSRic Aleshire 	/* Get the mlslabel property if it exists. */
37364201a95eSRic Aleshire 	if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
37374201a95eSRic Aleshire 	    NULL, NULL, 0, B_TRUE) != 0) ||
37384201a95eSRic Aleshire 	    (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
37394201a95eSRic Aleshire 		char		*str2 = NULL;
37404201a95eSRic Aleshire 
37414201a95eSRic Aleshire 		/*
37424201a95eSRic Aleshire 		 * No label on the dataset (or default only); create one.
37434201a95eSRic Aleshire 		 * (Only do this automatic labeling for the labeled brand.)
37444201a95eSRic Aleshire 		 */
37454201a95eSRic Aleshire 		if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
37464201a95eSRic Aleshire 			error = 0;
37474201a95eSRic Aleshire 			goto out;
37484201a95eSRic Aleshire 		}
37494201a95eSRic Aleshire 
37504201a95eSRic Aleshire 		error = l_to_str_internal(zone_sl, &str2);
37514201a95eSRic Aleshire 		if (error)
37524201a95eSRic Aleshire 			goto out;
37534201a95eSRic Aleshire 		if (str2 == NULL) {
37544201a95eSRic Aleshire 			error = -1;
37554201a95eSRic Aleshire 			goto out;
37564201a95eSRic Aleshire 		}
37574201a95eSRic Aleshire 		if ((error = zfs_prop_set(zhp,
37584201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
37594201a95eSRic Aleshire 			zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
37604201a95eSRic Aleshire 			    "property for root dataset at '%s'\n", rootpath);
37614201a95eSRic Aleshire 		}
37624201a95eSRic Aleshire 		free(str2);
37634201a95eSRic Aleshire 		goto out;
37644201a95eSRic Aleshire 	}
37654201a95eSRic Aleshire 
37664201a95eSRic Aleshire 	/* Convert the retrieved dataset label to binary form. */
37674201a95eSRic Aleshire 	error = hexstr_to_label(ds_hexsl, &ds_sl);
37684201a95eSRic Aleshire 	if (error) {
37694201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
37704201a95eSRic Aleshire 		    "property on root dataset at '%s'\n", rootpath);
37714201a95eSRic Aleshire 		goto out;			/* exit with error */
37724201a95eSRic Aleshire 	}
37734201a95eSRic Aleshire 
37744201a95eSRic Aleshire 	/*
37754201a95eSRic Aleshire 	 * Perform a MAC check by comparing the zone label with the
37764201a95eSRic Aleshire 	 * dataset label.
37774201a95eSRic Aleshire 	 */
37784201a95eSRic Aleshire 	error = (!blequal(zone_sl, &ds_sl));
37794201a95eSRic Aleshire 	if (error)
37804201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
37814201a95eSRic Aleshire out:
37824201a95eSRic Aleshire 	zfs_close(zhp);
37834201a95eSRic Aleshire 	libzfs_fini(hdl);
37844201a95eSRic Aleshire 
37854201a95eSRic Aleshire 	return (error);
37864201a95eSRic Aleshire }
37874201a95eSRic Aleshire 
37884201a95eSRic Aleshire /*
378945916cd2Sjpk  * Mount lower level home directories into/from current zone
379045916cd2Sjpk  * Share exported directories specified in dfstab for zone
379145916cd2Sjpk  */
379245916cd2Sjpk static int
379345916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
379445916cd2Sjpk {
379545916cd2Sjpk 	zoneid_t *zids = NULL;
379645916cd2Sjpk 	priv_set_t *zid_privs;
379745916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
379845916cd2Sjpk 	uint_t nzents_saved;
379945916cd2Sjpk 	uint_t nzents;
380045916cd2Sjpk 	int i;
380145916cd2Sjpk 	char readonly[] = "ro";
380245916cd2Sjpk 	struct zone_fstab lower_fstab;
380345916cd2Sjpk 	char *argv[4];
380445916cd2Sjpk 
380545916cd2Sjpk 	if (!is_system_labeled())
380645916cd2Sjpk 		return (0);
380745916cd2Sjpk 
380845916cd2Sjpk 	if (zid_label == NULL) {
380945916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
381045916cd2Sjpk 		if (zid_label == NULL)
381145916cd2Sjpk 			return (-1);
381245916cd2Sjpk 	}
381345916cd2Sjpk 
381445916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
381545916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
381627e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
381745916cd2Sjpk 
381845916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
381945916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
382045916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
382145916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
382245916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
382345916cd2Sjpk 
382445916cd2Sjpk 	/*
382545916cd2Sjpk 	 * Get the list of zones from the kernel
382645916cd2Sjpk 	 */
382745916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
382845916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
382945916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
383045916cd2Sjpk 		return (-1);
383145916cd2Sjpk 	}
383245916cd2Sjpk again:
383345916cd2Sjpk 	if (nzents == 0) {
383445916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
383545916cd2Sjpk 		return (-1);
383645916cd2Sjpk 	}
383745916cd2Sjpk 
383845916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
383945916cd2Sjpk 	if (zids == NULL) {
38403f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
384145916cd2Sjpk 		return (-1);
384245916cd2Sjpk 	}
384345916cd2Sjpk 	nzents_saved = nzents;
384445916cd2Sjpk 
384545916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
384645916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
384745916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
384845916cd2Sjpk 		free(zids);
384945916cd2Sjpk 		return (-1);
385045916cd2Sjpk 	}
385145916cd2Sjpk 	if (nzents != nzents_saved) {
385245916cd2Sjpk 		/* list changed, try again */
385345916cd2Sjpk 		free(zids);
385445916cd2Sjpk 		goto again;
385545916cd2Sjpk 	}
385645916cd2Sjpk 
385745916cd2Sjpk 	ip = getprivimplinfo();
385845916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
385945916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
386045916cd2Sjpk 		zonecfg_free_fs_option_list(
386145916cd2Sjpk 		    lower_fstab.zone_fs_options);
386245916cd2Sjpk 		free(zids);
386345916cd2Sjpk 		return (-1);
386445916cd2Sjpk 	}
386545916cd2Sjpk 
386645916cd2Sjpk 	for (i = 0; i < nzents; i++) {
386745916cd2Sjpk 		char zid_name[ZONENAME_MAX];
386845916cd2Sjpk 		zone_state_t zid_state;
386945916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
387045916cd2Sjpk 		struct stat stat_buf;
387145916cd2Sjpk 
387245916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
387345916cd2Sjpk 			continue;
387445916cd2Sjpk 
387545916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
387645916cd2Sjpk 			continue;
387745916cd2Sjpk 
387845916cd2Sjpk 		/*
387945916cd2Sjpk 		 * Do special setup for the zone we are booting
388045916cd2Sjpk 		 */
388145916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
388245916cd2Sjpk 			struct zone_fstab autofs_fstab;
388345916cd2Sjpk 			char map_path[MAXPATHLEN];
388445916cd2Sjpk 			int fd;
388545916cd2Sjpk 
388645916cd2Sjpk 			/*
388745916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
38889acbbeafSnn35248 			 * in the global zone. The non-global zone entry
388945916cd2Sjpk 			 * will be created by automount when the zone
389045916cd2Sjpk 			 * is booted.
389145916cd2Sjpk 			 */
389245916cd2Sjpk 
389345916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
389445916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
389545916cd2Sjpk 
389645916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
389745916cd2Sjpk 			    "/zone/%s/home", zid_name);
389845916cd2Sjpk 
389945916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
390045916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
390145916cd2Sjpk 			/*
390245916cd2Sjpk 			 * If the map file doesn't exist create a template
390345916cd2Sjpk 			 */
390445916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
390545916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
390645916cd2Sjpk 				int len;
390745916cd2Sjpk 				char map_rec[MAXPATHLEN];
390845916cd2Sjpk 
390945916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
391045916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
391145916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
391245916cd2Sjpk 				(void) write(fd, map_rec, len);
391345916cd2Sjpk 				(void) close(fd);
391445916cd2Sjpk 			}
391545916cd2Sjpk 
391645916cd2Sjpk 			/*
391745916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
391845916cd2Sjpk 			 * If it's already of type autofs, then
391945916cd2Sjpk 			 * don't mount it again.
392045916cd2Sjpk 			 */
392145916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
392245916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
392345916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
392445916cd2Sjpk 
392545916cd2Sjpk 				(void) make_one_dir(zlogp, "",
392627e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
392727e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
392845916cd2Sjpk 
392945916cd2Sjpk 				/*
393045916cd2Sjpk 				 * Mount will fail if automounter has already
393145916cd2Sjpk 				 * processed the auto_home_<zonename> map
393245916cd2Sjpk 				 */
393345916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
393445916cd2Sjpk 				    autofs_fstab.zone_fs_special,
393545916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
393645916cd2Sjpk 			}
393745916cd2Sjpk 			continue;
393845916cd2Sjpk 		}
393945916cd2Sjpk 
394045916cd2Sjpk 
394145916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
394248451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
394348451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
394445916cd2Sjpk 			/* Skip over zones without mounted filesystems */
394545916cd2Sjpk 			continue;
394645916cd2Sjpk 
394745916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
394845916cd2Sjpk 		    sizeof (m_label_t)) < 0)
394945916cd2Sjpk 			/* Skip over zones with unspecified label */
395045916cd2Sjpk 			continue;
395145916cd2Sjpk 
395245916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
395345916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
395445916cd2Sjpk 			/* Skip over zones with bad path */
395545916cd2Sjpk 			continue;
395645916cd2Sjpk 
395745916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
395845916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
395945916cd2Sjpk 			/* Skip over zones with bad privs */
396045916cd2Sjpk 			continue;
396145916cd2Sjpk 
396245916cd2Sjpk 		/*
396345916cd2Sjpk 		 * Reading down is valid according to our label model
396445916cd2Sjpk 		 * but some customers want to disable it because it
396545916cd2Sjpk 		 * allows execute down and other possible attacks.
396645916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
396745916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
396845916cd2Sjpk 		 * for NFS read-down semantics.
396945916cd2Sjpk 		 */
397045916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
397145916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
397245916cd2Sjpk 			/*
397345916cd2Sjpk 			 * Our zone dominates this one.
397445916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
397545916cd2Sjpk 			 */
397645916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
397745916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
397845916cd2Sjpk 
397945916cd2Sjpk 			/*
398045916cd2Sjpk 			 * If the target is already an LOFS mount
398145916cd2Sjpk 			 * then don't do it again.
398245916cd2Sjpk 			 */
398345916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
398445916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
398545916cd2Sjpk 
398645916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
398745916cd2Sjpk 				    MAXPATHLEN, "%s/export",
398845916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
398945916cd2Sjpk 					continue;
399045916cd2Sjpk 
399145916cd2Sjpk 				/*
399245916cd2Sjpk 				 * Make sure the lower-level home exists
399345916cd2Sjpk 				 */
399445916cd2Sjpk 				if (make_one_dir(zlogp,
399527e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
399627e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
399727e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
399845916cd2Sjpk 					continue;
399945916cd2Sjpk 
400045916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
400145916cd2Sjpk 				    "/home", MAXPATHLEN);
400245916cd2Sjpk 
400345916cd2Sjpk 				/*
400445916cd2Sjpk 				 * Mount can fail because the lower-level
400545916cd2Sjpk 				 * zone may have already done a mount up.
400645916cd2Sjpk 				 */
40070679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
40080679f794S 				    Z_MNT_BOOT);
400945916cd2Sjpk 			}
401045916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
401145916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
401245916cd2Sjpk 			/*
401345916cd2Sjpk 			 * This zone dominates our zone.
401445916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
401545916cd2Sjpk 			 */
401645916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
401745916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
401845916cd2Sjpk 			    zone_name) > MAXPATHLEN)
401945916cd2Sjpk 				continue;
402045916cd2Sjpk 
402145916cd2Sjpk 			/*
402245916cd2Sjpk 			 * If the target is already an LOFS mount
402345916cd2Sjpk 			 * then don't do it again.
402445916cd2Sjpk 			 */
402545916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
402645916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
402745916cd2Sjpk 
402845916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
402945916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
403045916cd2Sjpk 
403145916cd2Sjpk 				/*
403245916cd2Sjpk 				 * Mount can fail because the higher-level
403345916cd2Sjpk 				 * zone may have already done a mount down.
403445916cd2Sjpk 				 */
40350679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
40360679f794S 				    Z_MNT_BOOT);
403745916cd2Sjpk 			}
403845916cd2Sjpk 		}
403945916cd2Sjpk 	}
404045916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
404145916cd2Sjpk 	priv_freeset(zid_privs);
404245916cd2Sjpk 	free(zids);
404345916cd2Sjpk 
404445916cd2Sjpk 	/*
404545916cd2Sjpk 	 * Now share any exported directories from this zone.
404645916cd2Sjpk 	 * Each zone can have its own dfstab.
404745916cd2Sjpk 	 */
404845916cd2Sjpk 
404945916cd2Sjpk 	argv[0] = "zoneshare";
405045916cd2Sjpk 	argv[1] = "-z";
405145916cd2Sjpk 	argv[2] = zone_name;
405245916cd2Sjpk 	argv[3] = NULL;
405345916cd2Sjpk 
405445916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
405545916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
405645916cd2Sjpk 
405745916cd2Sjpk 	return (0);
405845916cd2Sjpk }
405945916cd2Sjpk 
406045916cd2Sjpk /*
406145916cd2Sjpk  * Unmount lofs mounts from higher level zones
406245916cd2Sjpk  * Unshare nfs exported directories
406345916cd2Sjpk  */
406445916cd2Sjpk static void
406545916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
406645916cd2Sjpk {
406745916cd2Sjpk 	zoneid_t *zids = NULL;
406845916cd2Sjpk 	uint_t nzents_saved;
406945916cd2Sjpk 	uint_t nzents;
407045916cd2Sjpk 	int i;
407145916cd2Sjpk 	char *argv[4];
407245916cd2Sjpk 	char path[MAXPATHLEN];
407345916cd2Sjpk 
407445916cd2Sjpk 	if (!is_system_labeled())
407545916cd2Sjpk 		return;
407645916cd2Sjpk 
407745916cd2Sjpk 	/*
407845916cd2Sjpk 	 * Get the list of zones from the kernel
407945916cd2Sjpk 	 */
408045916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
408145916cd2Sjpk 		return;
408245916cd2Sjpk 	}
408345916cd2Sjpk 
408445916cd2Sjpk 	if (zid_label == NULL) {
408545916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
408645916cd2Sjpk 		if (zid_label == NULL)
408745916cd2Sjpk 			return;
408845916cd2Sjpk 	}
408945916cd2Sjpk 
409045916cd2Sjpk again:
409145916cd2Sjpk 	if (nzents == 0)
409245916cd2Sjpk 		return;
409345916cd2Sjpk 
409445916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
409545916cd2Sjpk 	if (zids == NULL) {
40963f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
409745916cd2Sjpk 		return;
409845916cd2Sjpk 	}
409945916cd2Sjpk 	nzents_saved = nzents;
410045916cd2Sjpk 
410145916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
410245916cd2Sjpk 		free(zids);
410345916cd2Sjpk 		return;
410445916cd2Sjpk 	}
410545916cd2Sjpk 	if (nzents != nzents_saved) {
410645916cd2Sjpk 		/* list changed, try again */
410745916cd2Sjpk 		free(zids);
410845916cd2Sjpk 		goto again;
410945916cd2Sjpk 	}
411045916cd2Sjpk 
411145916cd2Sjpk 	for (i = 0; i < nzents; i++) {
411245916cd2Sjpk 		char zid_name[ZONENAME_MAX];
411345916cd2Sjpk 		zone_state_t zid_state;
411445916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
411545916cd2Sjpk 
411645916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
411745916cd2Sjpk 			continue;
411845916cd2Sjpk 
411945916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
412045916cd2Sjpk 			continue;
412145916cd2Sjpk 
412245916cd2Sjpk 		/*
412345916cd2Sjpk 		 * Skip the zone we are halting
412445916cd2Sjpk 		 */
412545916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
412645916cd2Sjpk 			continue;
412745916cd2Sjpk 
412845916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
412945916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
413045916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
413145916cd2Sjpk 			/* Skip over zones without mounted filesystems */
413245916cd2Sjpk 			continue;
413345916cd2Sjpk 
413445916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
413545916cd2Sjpk 		    sizeof (m_label_t)) < 0)
413645916cd2Sjpk 			/* Skip over zones with unspecified label */
413745916cd2Sjpk 			continue;
413845916cd2Sjpk 
413945916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
414045916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
414145916cd2Sjpk 			/* Skip over zones with bad path */
414245916cd2Sjpk 			continue;
414345916cd2Sjpk 
414445916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
414545916cd2Sjpk 			/*
414645916cd2Sjpk 			 * This zone dominates our zone.
414745916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
414845916cd2Sjpk 			 */
414945916cd2Sjpk 
415045916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
415145916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
415245916cd2Sjpk 			    zone_name) > MAXPATHLEN)
415345916cd2Sjpk 				continue;
415445916cd2Sjpk 
415545916cd2Sjpk 			/* Skip over mount failures */
415645916cd2Sjpk 			(void) umount(path);
415745916cd2Sjpk 		}
415845916cd2Sjpk 	}
415945916cd2Sjpk 	free(zids);
416045916cd2Sjpk 
416145916cd2Sjpk 	/*
416245916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
416345916cd2Sjpk 	 */
416445916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
416545916cd2Sjpk 	/* Skip over mount failures */
416645916cd2Sjpk 	(void) umount(path);
416745916cd2Sjpk 
416845916cd2Sjpk 	/*
416945916cd2Sjpk 	 * Next unshare any exported directories from this zone.
417045916cd2Sjpk 	 */
417145916cd2Sjpk 
417245916cd2Sjpk 	argv[0] = "zoneunshare";
417345916cd2Sjpk 	argv[1] = "-z";
417445916cd2Sjpk 	argv[2] = zone_name;
417545916cd2Sjpk 	argv[3] = NULL;
417645916cd2Sjpk 
417745916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
417845916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
417945916cd2Sjpk 
418045916cd2Sjpk 	/*
418145916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
418245916cd2Sjpk 	 */
418345916cd2Sjpk 
418445916cd2Sjpk 	argv[0] = "deallocate";
418545916cd2Sjpk 	argv[1] = "-Isz";
418645916cd2Sjpk 	argv[2] = zone_name;
418745916cd2Sjpk 	argv[3] = NULL;
418845916cd2Sjpk 
418945916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
419045916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
419145916cd2Sjpk }
419245916cd2Sjpk 
419345916cd2Sjpk /*
419445916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
419545916cd2Sjpk  * this zone.
419645916cd2Sjpk  */
419745916cd2Sjpk static tsol_zcent_t *
419845916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
419945916cd2Sjpk {
420045916cd2Sjpk 	FILE *fp;
420145916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
420245916cd2Sjpk 	char line[MAXTNZLEN];
420345916cd2Sjpk 
420445916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
420545916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
420645916cd2Sjpk 		return (NULL);
420745916cd2Sjpk 	}
420845916cd2Sjpk 
420945916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
421045916cd2Sjpk 		/*
421145916cd2Sjpk 		 * Check for malformed database
421245916cd2Sjpk 		 */
421345916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
421445916cd2Sjpk 			break;
421545916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
421645916cd2Sjpk 			continue;
421745916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
421845916cd2Sjpk 			break;
421945916cd2Sjpk 		tsol_freezcent(zcent);
422045916cd2Sjpk 		zcent = NULL;
422145916cd2Sjpk 	}
422245916cd2Sjpk 	(void) fclose(fp);
422345916cd2Sjpk 
422445916cd2Sjpk 	if (zcent == NULL) {
422545916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
422645916cd2Sjpk 		    "See tnzonecfg(4)");
422745916cd2Sjpk 	} else {
422845916cd2Sjpk 		if (zlabel == NULL)
422945916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
423045916cd2Sjpk 		/*
423145916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
423245916cd2Sjpk 		 */
423345916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
423445916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
423545916cd2Sjpk 			return (NULL);
423645916cd2Sjpk 		} else {
423745916cd2Sjpk 			priv_copyset(privs, zprivs);
423845916cd2Sjpk 		}
423945916cd2Sjpk 	}
424045916cd2Sjpk 	return (zcent);
424145916cd2Sjpk }
424245916cd2Sjpk 
424345916cd2Sjpk /*
424445916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
424545916cd2Sjpk  */
424645916cd2Sjpk static void
424745916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
424845916cd2Sjpk {
424945916cd2Sjpk 	tsol_mlp_t *mlp;
425045916cd2Sjpk 	tsol_mlpent_t tsme;
425145916cd2Sjpk 
425245916cd2Sjpk 	if (!is_system_labeled())
425345916cd2Sjpk 		return;
425445916cd2Sjpk 
425545916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
425645916cd2Sjpk 	tsme.tsme_flags = 0;
425745916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
425845916cd2Sjpk 		tsme.tsme_mlp = *mlp;
425945916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
426045916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
426145916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
426245916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
426345916cd2Sjpk 		}
426445916cd2Sjpk 	}
426545916cd2Sjpk 
426645916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
426745916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
426845916cd2Sjpk 		tsme.tsme_mlp = *mlp;
426945916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
427045916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
427145916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
427245916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
427345916cd2Sjpk 		}
427445916cd2Sjpk 	}
427545916cd2Sjpk }
427645916cd2Sjpk 
427745916cd2Sjpk static void
427845916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
427945916cd2Sjpk {
428045916cd2Sjpk 	tsol_mlpent_t tsme;
428145916cd2Sjpk 
428245916cd2Sjpk 	if (!is_system_labeled())
428345916cd2Sjpk 		return;
428445916cd2Sjpk 
428545916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
428645916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
428745916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
428845916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
428945916cd2Sjpk }
429045916cd2Sjpk 
42917c478bd9Sstevel@tonic-gate int
42920094b373Sjv227347 prtmount(const struct mnttab *fs, void *x) {
42930094b373Sjv227347 	zerror((zlog_t *)x, B_FALSE, "  %s", fs->mnt_mountp);
42947c478bd9Sstevel@tonic-gate 	return (0);
42957c478bd9Sstevel@tonic-gate }
42967c478bd9Sstevel@tonic-gate 
4297108322fbScarlsonj /*
4298108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
4299108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
4300108322fbScarlsonj  * is found or if we can't tell.
4301108322fbScarlsonj  */
4302108322fbScarlsonj static boolean_t
4303108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
43047c478bd9Sstevel@tonic-gate {
4305108322fbScarlsonj 	zoneid_t *zids = NULL;
4306108322fbScarlsonj 	uint_t nzids = 0;
4307108322fbScarlsonj 	boolean_t retv;
4308108322fbScarlsonj 	int rlen, zlen;
4309108322fbScarlsonj 	char zroot[MAXPATHLEN];
4310108322fbScarlsonj 	char zonename[ZONENAME_MAX];
4311108322fbScarlsonj 
4312108322fbScarlsonj 	for (;;) {
4313108322fbScarlsonj 		nzids += 10;
4314108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
4315108322fbScarlsonj 		if (zids == NULL) {
43163f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
4317108322fbScarlsonj 			return (B_TRUE);
4318108322fbScarlsonj 		}
4319108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
4320108322fbScarlsonj 			break;
4321108322fbScarlsonj 		free(zids);
4322108322fbScarlsonj 	}
4323108322fbScarlsonj 	retv = B_FALSE;
4324108322fbScarlsonj 	rlen = strlen(rootpath);
4325108322fbScarlsonj 	while (nzids > 0) {
4326108322fbScarlsonj 		/*
4327108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
4328108322fbScarlsonj 		 * while we were busy.
4329108322fbScarlsonj 		 */
4330108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4331108322fbScarlsonj 		    sizeof (zroot)) == -1)
4332108322fbScarlsonj 			continue;
4333108322fbScarlsonj 		zlen = strlen(zroot);
4334108322fbScarlsonj 		if (zlen > rlen)
4335108322fbScarlsonj 			zlen = rlen;
4336108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
4337108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4338108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4339108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
4340108322fbScarlsonj 			    sizeof (zonename)) == -1)
4341108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
4342108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
4343108322fbScarlsonj 			zerror(zlogp, B_FALSE,
4344108322fbScarlsonj 			    "zone root %s already in use by zone %s",
4345108322fbScarlsonj 			    rootpath, zonename);
4346108322fbScarlsonj 			retv = B_TRUE;
4347108322fbScarlsonj 			break;
4348108322fbScarlsonj 		}
4349108322fbScarlsonj 	}
4350108322fbScarlsonj 	free(zids);
4351108322fbScarlsonj 	return (retv);
4352108322fbScarlsonj }
4353108322fbScarlsonj 
4354108322fbScarlsonj /*
4355108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
4356108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
4357108322fbScarlsonj  */
4358108322fbScarlsonj static boolean_t
4359108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4360108322fbScarlsonj {
4361108322fbScarlsonj 	struct stat64 rst, zst;
4362108322fbScarlsonj 	struct mnttab *mnp;
4363108322fbScarlsonj 
4364108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
4365108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4366108322fbScarlsonj 		return (B_TRUE);
4367108322fbScarlsonj 	}
4368108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4369108322fbScarlsonj 		return (B_TRUE);
4370108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4371108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
4372108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4373108322fbScarlsonj 			continue;
4374108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
4375108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
4376108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
4377108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4378108322fbScarlsonj 			zerror(zlogp, B_FALSE,
4379108322fbScarlsonj 			    "zone root %s is reachable through %s",
4380108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
4381108322fbScarlsonj 			return (B_TRUE);
4382108322fbScarlsonj 		}
4383108322fbScarlsonj 	}
4384108322fbScarlsonj 	return (B_FALSE);
4385108322fbScarlsonj }
4386108322fbScarlsonj 
43870209230bSgjelinek /*
43880209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
43890209230bSgjelinek  * configuration.
43900209230bSgjelinek  */
43910209230bSgjelinek static int
43920209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
43930209230bSgjelinek {
43940209230bSgjelinek 	int res;
43950209230bSgjelinek 	uint64_t tmp;
43960209230bSgjelinek 	struct zone_mcaptab mcap;
43970209230bSgjelinek 	char sched[MAXNAMELEN];
43980209230bSgjelinek 	zone_dochandle_t handle = NULL;
43990209230bSgjelinek 	char pool_err[128];
44000209230bSgjelinek 
44010209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
44020209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
44030209230bSgjelinek 		return (Z_BAD_HANDLE);
44040209230bSgjelinek 	}
44050209230bSgjelinek 
44060209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
44070209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
44080209230bSgjelinek 		zonecfg_fini_handle(handle);
44090209230bSgjelinek 		return (res);
44100209230bSgjelinek 	}
44110209230bSgjelinek 
44120209230bSgjelinek 	/*
44130209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
44140209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
44150209230bSgjelinek 	 */
44160209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
44170209230bSgjelinek 		uint64_t num;
44180209230bSgjelinek 		char smf_err[128];
44190209230bSgjelinek 
44200209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
44210209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
44220209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
44230209230bSgjelinek 			zonecfg_fini_handle(handle);
44240209230bSgjelinek 			return (Z_INVAL);
44250209230bSgjelinek 		}
44260209230bSgjelinek 
44270209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
44280209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
44290209230bSgjelinek 			    "failed: %s", smf_err);
44300209230bSgjelinek 			zonecfg_fini_handle(handle);
44310209230bSgjelinek 			return (Z_INVAL);
44320209230bSgjelinek 		}
44330209230bSgjelinek 	}
44340209230bSgjelinek 
44350209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
44360209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
44370209230bSgjelinek 	    strlen(sched) > 0) {
44380209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
44390209230bSgjelinek 		    strlen(sched)) == -1)
44400209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
44410209230bSgjelinek 			    "default scheduling class");
44420209230bSgjelinek 
44430209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
44440209230bSgjelinek 	    == Z_OK) {
44450209230bSgjelinek 		/*
44460209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
44470209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
44480209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
44490209230bSgjelinek 		 * in by default so we can print a warning and modify the class
44500209230bSgjelinek 		 * if we wouldn't be using FSS.
44510209230bSgjelinek 		 */
44520209230bSgjelinek 		char class_name[PC_CLNMSZ];
44530209230bSgjelinek 
44540209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
44550209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
44560209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
44570209230bSgjelinek 			    "the zone's scheduling class");
44580209230bSgjelinek 
44590209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
44600209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
44610209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
44620209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
44630209230bSgjelinek 			    "used for processes\nin the zone but to get the "
44640209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
44650209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
44660209230bSgjelinek 			    "details.");
44670209230bSgjelinek 
44680209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
44690209230bSgjelinek 			    strlen("FSS")) == -1)
44700209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
44710209230bSgjelinek 				    "zone scheduling class to FSS");
44720209230bSgjelinek 		}
44730209230bSgjelinek 	}
44740209230bSgjelinek 
44750209230bSgjelinek 	/*
44760209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
44770209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
44780209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
44790209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
44800209230bSgjelinek 	 * and just return if there is nothing to do.
44810209230bSgjelinek 	 *
44820209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
44830209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
44840209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
44850209230bSgjelinek 	 * zonecfg.
44860209230bSgjelinek 	 */
44870209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
44880209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
44890209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
44900209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
44910209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
44920209230bSgjelinek 			    pool_err);
44930209230bSgjelinek 		else
44940209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
44950209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
44960209230bSgjelinek 		zonecfg_fini_handle(handle);
44970209230bSgjelinek 		return (Z_POOL_BIND);
44980209230bSgjelinek 	}
44990209230bSgjelinek 
45000209230bSgjelinek 	/*
45010209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
45020209230bSgjelinek 	 */
45030209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
45040209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
45050209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
45060209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
45070209230bSgjelinek 		    "processor allocation within the specified range\n"
45080209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
45090209230bSgjelinek 		    "See poold(1M).");
45100209230bSgjelinek 	}
45110209230bSgjelinek 
45120209230bSgjelinek 	/* The following is a warning, not an error. */
45130209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
45140209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
45150209230bSgjelinek 		if (res == Z_POOL_BIND)
45160209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
45170209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
45180209230bSgjelinek 		else if (res == Z_POOL)
45190209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
45200209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
45210209230bSgjelinek 		else
45220209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
45230209230bSgjelinek 			    zonecfg_strerror(res));
45240209230bSgjelinek 	}
4525efd4c9b6SSteve Lawrence 
4526efd4c9b6SSteve Lawrence 	/* Update saved pool name in case it has changed */
4527*a3002e0aSGarrett D'Amore 	(void) zonecfg_get_poolname(handle, zone_name, pool_name,
4528*a3002e0aSGarrett D'Amore 	    sizeof (pool_name));
45290209230bSgjelinek 
45300209230bSgjelinek 	zonecfg_fini_handle(handle);
45310209230bSgjelinek 	return (Z_OK);
45320209230bSgjelinek }
45330209230bSgjelinek 
45340fbb751dSJohn Levon static void
45350fbb751dSJohn Levon report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
45360fbb751dSJohn Levon {
45370fbb751dSJohn Levon 	switch (res) {
45380fbb751dSJohn Levon 	case Z_TOO_BIG:
45390fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value is too large.", name);
45400fbb751dSJohn Levon 		break;
45410fbb751dSJohn Levon 
45420fbb751dSJohn Levon 	case Z_INVALID_PROPERTY:
45430fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
45440fbb751dSJohn Levon 		    name, value);
45450fbb751dSJohn Levon 		break;
45460fbb751dSJohn Levon 
45470fbb751dSJohn Levon 	default:
45480fbb751dSJohn Levon 		zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
45490fbb751dSJohn Levon 		break;
45500fbb751dSJohn Levon 	}
45510fbb751dSJohn Levon }
45520fbb751dSJohn Levon 
45535679c89fSjv227347 /*
45545679c89fSjv227347  * Sets the hostid of the new zone based on its configured value.  The zone's
45555679c89fSjv227347  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
45565679c89fSjv227347  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
45575679c89fSjv227347  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
45585679c89fSjv227347  * ID of the new zone.
45595679c89fSjv227347  *
45605679c89fSjv227347  * This function returns zero on success and a nonzero error code on failure.
45615679c89fSjv227347  */
45625679c89fSjv227347 static int
45630fbb751dSJohn Levon setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
45645679c89fSjv227347 {
45655679c89fSjv227347 	int res;
45665679c89fSjv227347 	char hostidp[HW_HOSTID_LEN];
45675679c89fSjv227347 	unsigned int hostid;
45685679c89fSjv227347 
45690fbb751dSJohn Levon 	res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
45700fbb751dSJohn Levon 
45710fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
45720fbb751dSJohn Levon 		return (Z_OK);
45730fbb751dSJohn Levon 	} else if (res != Z_OK) {
45740fbb751dSJohn Levon 		report_prop_err(zlogp, "hostid", hostidp, res);
45750fbb751dSJohn Levon 		return (res);
45760fbb751dSJohn Levon 	}
45770fbb751dSJohn Levon 
45780fbb751dSJohn Levon 	hostid = (unsigned int)strtoul(hostidp, NULL, 16);
45790fbb751dSJohn Levon 	if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
45800fbb751dSJohn Levon 	    sizeof (hostid))) != 0) {
45810fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
45820fbb751dSJohn Levon 		    "zone hostid is not valid: %s: %d", hostidp, res);
45830fbb751dSJohn Levon 		return (Z_SYSTEM);
45840fbb751dSJohn Levon 	}
45850fbb751dSJohn Levon 
45860fbb751dSJohn Levon 	return (res);
45870fbb751dSJohn Levon }
45880fbb751dSJohn Levon 
45890fbb751dSJohn Levon static int
45900fbb751dSJohn Levon setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
45910fbb751dSJohn Levon {
45920fbb751dSJohn Levon 	char fsallowedp[ZONE_FS_ALLOWED_MAX];
45930fbb751dSJohn Levon 	int res;
45940fbb751dSJohn Levon 
45950fbb751dSJohn Levon 	res = zonecfg_get_fs_allowed(handle, fsallowedp, sizeof (fsallowedp));
45960fbb751dSJohn Levon 
45970fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
45980fbb751dSJohn Levon 		return (Z_OK);
45990fbb751dSJohn Levon 	} else if (res != Z_OK) {
46000fbb751dSJohn Levon 		report_prop_err(zlogp, "fs-allowed", fsallowedp, res);
46010fbb751dSJohn Levon 		return (res);
46020fbb751dSJohn Levon 	}
46030fbb751dSJohn Levon 
46040fbb751dSJohn Levon 	if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, &fsallowedp,
46050fbb751dSJohn Levon 	    sizeof (fsallowedp)) != 0) {
46060fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
46070fbb751dSJohn Levon 		    "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
46080fbb751dSJohn Levon 		return (Z_SYSTEM);
46090fbb751dSJohn Levon 	}
46100fbb751dSJohn Levon 
46110fbb751dSJohn Levon 	return (res);
46120fbb751dSJohn Levon }
46130fbb751dSJohn Levon 
46140fbb751dSJohn Levon static int
46150fbb751dSJohn Levon setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
46160fbb751dSJohn Levon {
46170fbb751dSJohn Levon 	zone_dochandle_t handle;
46180fbb751dSJohn Levon 	int res = Z_OK;
46190fbb751dSJohn Levon 
46205679c89fSjv227347 	if ((handle = zonecfg_init_handle()) == NULL) {
46215679c89fSjv227347 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
46225679c89fSjv227347 		return (Z_BAD_HANDLE);
46235679c89fSjv227347 	}
46245679c89fSjv227347 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
46255679c89fSjv227347 		zerror(zlogp, B_FALSE, "invalid configuration");
46260fbb751dSJohn Levon 		goto out;
46275679c89fSjv227347 	}
46285679c89fSjv227347 
46290fbb751dSJohn Levon 	if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
46300fbb751dSJohn Levon 		goto out;
46310fbb751dSJohn Levon 
46320fbb751dSJohn Levon 	if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
46330fbb751dSJohn Levon 		goto out;
46340fbb751dSJohn Levon 
46350fbb751dSJohn Levon out:
46365679c89fSjv227347 	zonecfg_fini_handle(handle);
46375679c89fSjv227347 	return (res);
46385679c89fSjv227347 }
46395679c89fSjv227347 
4640108322fbScarlsonj zoneid_t
46416cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4642108322fbScarlsonj {
4643108322fbScarlsonj 	zoneid_t rval = -1;
46447c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
46457c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
46467c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
4647108322fbScarlsonj 	size_t rctlbufsz = 0;
4648fa9e4066Sahrens 	char *zfsbuf = NULL;
4649fa9e4066Sahrens 	size_t zfsbufsz = 0;
4650108322fbScarlsonj 	zoneid_t zoneid = -1;
46517c478bd9Sstevel@tonic-gate 	int xerr;
4652108322fbScarlsonj 	char *kzone;
4653108322fbScarlsonj 	FILE *fp = NULL;
465445916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
465545916cd2Sjpk 	int match = 0;
465645916cd2Sjpk 	int doi = 0;
4657f4b3ec61Sdh155122 	int flags;
4658f4b3ec61Sdh155122 	zone_iptype_t iptype;
46597c478bd9Sstevel@tonic-gate 
46607c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
46617c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
46627c478bd9Sstevel@tonic-gate 		return (-1);
46637c478bd9Sstevel@tonic-gate 	}
4664108322fbScarlsonj 	if (zonecfg_in_alt_root())
4665108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
46667c478bd9Sstevel@tonic-gate 
46672b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
4668f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4669f4b3ec61Sdh155122 		return (-1);
4670f4b3ec61Sdh155122 	}
4671f4b3ec61Sdh155122 	switch (iptype) {
4672f4b3ec61Sdh155122 	case ZS_SHARED:
4673f4b3ec61Sdh155122 		flags = 0;
4674f4b3ec61Sdh155122 		break;
4675f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
4676f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
4677f4b3ec61Sdh155122 		break;
4678f4b3ec61Sdh155122 	}
4679f4b3ec61Sdh155122 
46807c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
46817c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
46827c478bd9Sstevel@tonic-gate 		return (-1);
46837c478bd9Sstevel@tonic-gate 	}
46847c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4685ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
46867c478bd9Sstevel@tonic-gate 		goto error;
4687ffbafc53Scomay 
46886cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
46896cfd72c6Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
46907c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
46917c478bd9Sstevel@tonic-gate 		goto error;
46927c478bd9Sstevel@tonic-gate 	}
4693ffbafc53Scomay 
4694fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4695fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4696fa9e4066Sahrens 		goto error;
4697fa9e4066Sahrens 	}
46987c478bd9Sstevel@tonic-gate 
46996cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
470045916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
470148451833Scarlsonj 		if (zcent != NULL) {
470245916cd2Sjpk 			match = zcent->zc_match;
470345916cd2Sjpk 			doi = zcent->zc_doi;
470445916cd2Sjpk 			*zlabel = zcent->zc_label;
470545916cd2Sjpk 		} else {
470645916cd2Sjpk 			goto error;
470745916cd2Sjpk 		}
47084201a95eSRic Aleshire 		if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
47094201a95eSRic Aleshire 			goto error;
471045916cd2Sjpk 	}
471145916cd2Sjpk 
4712108322fbScarlsonj 	kzone = zone_name;
4713108322fbScarlsonj 
4714108322fbScarlsonj 	/*
4715108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4716108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4717108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4718108322fbScarlsonj 	 * that use this same source node (same device and inode).
4719108322fbScarlsonj 	 */
4720108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4721108322fbScarlsonj 		goto error;
4722108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4723108322fbScarlsonj 		goto error;
4724108322fbScarlsonj 
47256cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4726108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4727108322fbScarlsonj 
4728108322fbScarlsonj 		/*
4729108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4730108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4731108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4732108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4733108322fbScarlsonj 		 * executables.
4734108322fbScarlsonj 		 *
4735108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4736108322fbScarlsonj 		 */
4737108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4738108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4739108322fbScarlsonj 			goto error;
4740108322fbScarlsonj 		}
4741108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4742108322fbScarlsonj 			goto error;
4743108322fbScarlsonj 	}
4744108322fbScarlsonj 
4745108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4746108322fbScarlsonj 		/*
4747108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4748108322fbScarlsonj 		 * then we have some additional work to do before starting the
4749108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4750108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4751108322fbScarlsonj 		 * the zone.
4752108322fbScarlsonj 		 */
4753108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4754108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4755108322fbScarlsonj 			goto error;
4756108322fbScarlsonj 		}
4757108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4758108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4759108322fbScarlsonj 			goto error;
4760108322fbScarlsonj 		}
4761108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4762108322fbScarlsonj 		    NULL, 0) == 0) {
4763108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4764108322fbScarlsonj 			goto error;
4765108322fbScarlsonj 		}
4766108322fbScarlsonj 		/* This is the preferred name */
4767108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4768108322fbScarlsonj 		    zone_name);
4769108322fbScarlsonj 		srandom(getpid());
4770108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4771108322fbScarlsonj 		    0) == 0) {
4772108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4773108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4774108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4775108322fbScarlsonj 		}
4776108322fbScarlsonj 		kzone = kernzone;
4777108322fbScarlsonj 	}
4778108322fbScarlsonj 
47797c478bd9Sstevel@tonic-gate 	xerr = 0;
4780108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4781f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4782f4b3ec61Sdh155122 	    flags)) == -1) {
47837c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
47847c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
47857c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
47867c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
47877c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
47887c478bd9Sstevel@tonic-gate 			} else {
47897c478bd9Sstevel@tonic-gate 
47907c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
47917c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
47927c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
47937c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
47947c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
47957c478bd9Sstevel@tonic-gate 			}
47967c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
47977c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
47987c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
47997c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
48008c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
48018c55461bSton 			char zonename[ZONENAME_MAX];
48028c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
48038c55461bSton 			    zonename, ZONENAME_MAX);
48048c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
48058c55461bSton 			    "used by the zone '%s'.", zonename);
48067c478bd9Sstevel@tonic-gate 		} else {
48077c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
48087c478bd9Sstevel@tonic-gate 		}
48097c478bd9Sstevel@tonic-gate 		goto error;
48107c478bd9Sstevel@tonic-gate 	}
4811108322fbScarlsonj 
4812108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4813108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4814108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4815108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4816108322fbScarlsonj 		goto error;
4817108322fbScarlsonj 	}
4818108322fbScarlsonj 
48190679f794S 	/*
48200679f794S 	 * The following actions are not performed when merely mounting a zone
48210679f794S 	 * for administrative use.
48220679f794S 	 */
48230679f794S 	if (mount_cmd == Z_MNT_BOOT) {
48240679f794S 		brand_handle_t bh;
48250679f794S 		struct brand_attr attr;
48260679f794S 		char modname[MAXPATHLEN];
48270679f794S 
48280fbb751dSJohn Levon 		if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
48295679c89fSjv227347 			goto error;
48305679c89fSjv227347 
483162868012SSteve Lawrence 		if ((bh = brand_open(brand_name)) == NULL) {
48320679f794S 			zerror(zlogp, B_FALSE,
48330679f794S 			    "unable to determine brand name");
48345679c89fSjv227347 			goto error;
48359acbbeafSnn35248 		}
48369acbbeafSnn35248 
48379a5d73e0SRic Aleshire 		if (!is_system_labeled() &&
483862868012SSteve Lawrence 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
48399a5d73e0SRic Aleshire 			brand_close(bh);
48409a5d73e0SRic Aleshire 			zerror(zlogp, B_FALSE,
48419a5d73e0SRic Aleshire 			    "cannot boot labeled zone on unlabeled system");
48429a5d73e0SRic Aleshire 			goto error;
48439a5d73e0SRic Aleshire 		}
48449a5d73e0SRic Aleshire 
48459acbbeafSnn35248 		/*
48469acbbeafSnn35248 		 * If this brand requires any kernel support, now is the time to
48479acbbeafSnn35248 		 * get it loaded and initialized.
48489acbbeafSnn35248 		 */
4849123807fbSedp 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4850e767a340Sgjelinek 			brand_close(bh);
48510679f794S 			zerror(zlogp, B_FALSE,
48520679f794S 			    "unable to determine brand kernel module");
48535679c89fSjv227347 			goto error;
48549acbbeafSnn35248 		}
4855e767a340Sgjelinek 		brand_close(bh);
48569acbbeafSnn35248 
48579acbbeafSnn35248 		if (strlen(modname) > 0) {
485862868012SSteve Lawrence 			(void) strlcpy(attr.ba_brandname, brand_name,
485962868012SSteve Lawrence 			    sizeof (attr.ba_brandname));
486062868012SSteve Lawrence 			(void) strlcpy(attr.ba_modname, modname,
486162868012SSteve Lawrence 			    sizeof (attr.ba_modname));
48629acbbeafSnn35248 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
48639acbbeafSnn35248 			    sizeof (attr) != 0)) {
48640679f794S 				zerror(zlogp, B_TRUE,
48650679f794S 				    "could not set zone brand attribute.");
48669acbbeafSnn35248 				goto error;
48679acbbeafSnn35248 			}
48689acbbeafSnn35248 		}
48699acbbeafSnn35248 
48705679c89fSjv227347 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
48710209230bSgjelinek 			goto error;
48720209230bSgjelinek 
487345916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
48740209230bSgjelinek 	}
48750209230bSgjelinek 
4876108322fbScarlsonj 	rval = zoneid;
4877108322fbScarlsonj 	zoneid = -1;
4878108322fbScarlsonj 
48797c478bd9Sstevel@tonic-gate error:
48805679c89fSjv227347 	if (zoneid != -1) {
48815679c89fSjv227347 		(void) zone_shutdown(zoneid);
4882108322fbScarlsonj 		(void) zone_destroy(zoneid);
48835679c89fSjv227347 	}
48847c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
48857c478bd9Sstevel@tonic-gate 		free(rctlbuf);
48867c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
4887108322fbScarlsonj 	if (fp != NULL)
4888108322fbScarlsonj 		zonecfg_close_scratch(fp);
4889108322fbScarlsonj 	lofs_discard_mnttab();
489045916cd2Sjpk 	if (zcent != NULL)
489145916cd2Sjpk 		tsol_freezcent(zcent);
48927c478bd9Sstevel@tonic-gate 	return (rval);
48937c478bd9Sstevel@tonic-gate }
48947c478bd9Sstevel@tonic-gate 
4895555afedfScarlsonj /*
4896555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
4897555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4898555afedfScarlsonj  * details from inside the zone.
4899555afedfScarlsonj  */
4900555afedfScarlsonj static void
4901555afedfScarlsonj write_index_file(zoneid_t zoneid)
4902555afedfScarlsonj {
4903555afedfScarlsonj 	FILE *zef;
4904555afedfScarlsonj 	FILE *zet;
4905555afedfScarlsonj 	struct zoneent *zep;
4906555afedfScarlsonj 	pid_t child;
4907555afedfScarlsonj 	int tmpl_fd;
4908555afedfScarlsonj 	ctid_t ct;
4909555afedfScarlsonj 	int fd;
4910555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4911555afedfScarlsonj 
4912555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
4913555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
4914555afedfScarlsonj 		return;
4915555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
4916555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
4917555afedfScarlsonj 			break;
4918555afedfScarlsonj 		free(zep);
4919555afedfScarlsonj 	}
4920555afedfScarlsonj 	endzoneent(zef);
4921555afedfScarlsonj 	if (zep == NULL)
4922555afedfScarlsonj 		return;
4923555afedfScarlsonj 
4924555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
4925555afedfScarlsonj 		free(zep);
4926555afedfScarlsonj 		return;
4927555afedfScarlsonj 	}
4928555afedfScarlsonj 
4929555afedfScarlsonj 	if ((child = fork()) == -1) {
4930555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4931555afedfScarlsonj 		(void) close(tmpl_fd);
4932555afedfScarlsonj 		free(zep);
4933555afedfScarlsonj 		return;
4934555afedfScarlsonj 	}
4935555afedfScarlsonj 
4936555afedfScarlsonj 	/* parent waits for child to finish */
4937555afedfScarlsonj 	if (child != 0) {
4938555afedfScarlsonj 		free(zep);
4939555afedfScarlsonj 		if (contract_latest(&ct) == -1)
4940555afedfScarlsonj 			ct = -1;
4941555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4942555afedfScarlsonj 		(void) close(tmpl_fd);
4943555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
4944555afedfScarlsonj 		(void) contract_abandon_id(ct);
4945555afedfScarlsonj 		return;
4946555afedfScarlsonj 	}
4947555afedfScarlsonj 
4948555afedfScarlsonj 	/* child enters zone and sets up index file */
4949555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
4950555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
4951555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4952555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4953555afedfScarlsonj 		    ZONE_CONFIG_GID);
4954555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4955555afedfScarlsonj 		    ZONE_INDEX_MODE);
4956555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4957555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4958555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
4959555afedfScarlsonj 				uuidstr[0] = '\0';
4960555afedfScarlsonj 			else
4961555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
4962555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4963555afedfScarlsonj 			    zone_state_str(zep->zone_state),
4964555afedfScarlsonj 			    uuidstr);
4965555afedfScarlsonj 			(void) fclose(zet);
4966555afedfScarlsonj 		}
4967555afedfScarlsonj 	}
4968555afedfScarlsonj 	_exit(0);
4969555afedfScarlsonj }
4970555afedfScarlsonj 
49717c478bd9Sstevel@tonic-gate int
49726cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
49737c478bd9Sstevel@tonic-gate {
49749acbbeafSnn35248 	char zonepath[MAXPATHLEN];
49755749802bSdp 
49766cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4977fa9e4066Sahrens 		lofs_discard_mnttab();
4978fa9e4066Sahrens 		return (-1);
4979fa9e4066Sahrens 	}
4980fa9e4066Sahrens 
49819acbbeafSnn35248 	/*
49829acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
49839acbbeafSnn35248 	 * attribute backing store for /dev
49849acbbeafSnn35248 	 */
49859acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
49869acbbeafSnn35248 		lofs_discard_mnttab();
49879acbbeafSnn35248 		return (-1);
49889acbbeafSnn35248 	}
498916bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
499027e6fb21Sdp 
499127e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
499227e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
499327e6fb21Sdp 	    0, 3) != 0) {
4994108322fbScarlsonj 		lofs_discard_mnttab();
49957c478bd9Sstevel@tonic-gate 		return (-1);
4996108322fbScarlsonj 	}
4997facf4a8dSllai1 
49989acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4999facf4a8dSllai1 		lofs_discard_mnttab();
5000facf4a8dSllai1 		return (-1);
5001facf4a8dSllai1 	}
5002facf4a8dSllai1 
50036cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
5004f4b3ec61Sdh155122 		zone_iptype_t iptype;
5005f4b3ec61Sdh155122 
50062b24ab6bSSebastien Roy 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
5007f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
5008108322fbScarlsonj 			lofs_discard_mnttab();
50097c478bd9Sstevel@tonic-gate 			return (-1);
5010108322fbScarlsonj 		}
5011555afedfScarlsonj 
5012f4b3ec61Sdh155122 		switch (iptype) {
5013f4b3ec61Sdh155122 		case ZS_SHARED:
5014f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
5015f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
5016f4b3ec61Sdh155122 				lofs_discard_mnttab();
5017f4b3ec61Sdh155122 				return (-1);
5018f4b3ec61Sdh155122 			}
5019f4b3ec61Sdh155122 			break;
5020f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
5021550b6e40SSowmini Varadhan 			if (configure_exclusive_network_interfaces(zlogp,
5022550b6e40SSowmini Varadhan 			    zoneid) !=
5023f4b3ec61Sdh155122 			    0) {
5024f4b3ec61Sdh155122 				lofs_discard_mnttab();
5025f4b3ec61Sdh155122 				return (-1);
5026f4b3ec61Sdh155122 			}
5027f4b3ec61Sdh155122 			break;
5028f4b3ec61Sdh155122 		}
5029f4b3ec61Sdh155122 	}
5030f4b3ec61Sdh155122 
5031555afedfScarlsonj 	write_index_file(zoneid);
5032555afedfScarlsonj 
5033108322fbScarlsonj 	lofs_discard_mnttab();
50347c478bd9Sstevel@tonic-gate 	return (0);
50357c478bd9Sstevel@tonic-gate }
50367c478bd9Sstevel@tonic-gate 
5037108322fbScarlsonj static int
5038108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
50397c478bd9Sstevel@tonic-gate {
5040108322fbScarlsonj 	char zroot[MAXPATHLEN];
5041108322fbScarlsonj 
5042108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5043108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
5044108322fbScarlsonj 		return (-1);
5045108322fbScarlsonj 	}
5046108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
5047108322fbScarlsonj 
5048108322fbScarlsonj 	/*
5049108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
5050108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
5051108322fbScarlsonj 	 * still be creds floating about in the system that reference the
5052108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
5053108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
5054108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
5055108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
5056108322fbScarlsonj 	 */
5057108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
5058108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
5059108322fbScarlsonj 			goto unmounted;
5060108322fbScarlsonj 		if (errno == EBUSY) {
5061108322fbScarlsonj 			int tries = 10;
5062108322fbScarlsonj 
5063108322fbScarlsonj 			while (--tries >= 0) {
5064108322fbScarlsonj 				(void) sleep(1);
5065108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
5066108322fbScarlsonj 					goto unmounted;
5067108322fbScarlsonj 				if (errno != EBUSY)
5068108322fbScarlsonj 					break;
5069108322fbScarlsonj 			}
5070108322fbScarlsonj 		}
5071108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
5072108322fbScarlsonj 		return (-1);
5073108322fbScarlsonj 	}
5074108322fbScarlsonj unmounted:
5075108322fbScarlsonj 
5076108322fbScarlsonj 	/*
5077108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
5078108322fbScarlsonj 	 * entries.
5079108322fbScarlsonj 	 */
5080108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5081108322fbScarlsonj 		FILE *fp;
5082108322fbScarlsonj 		int retv;
5083108322fbScarlsonj 
5084108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5085108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
5086108322fbScarlsonj 			return (-1);
5087108322fbScarlsonj 		}
5088108322fbScarlsonj 		retv = -1;
5089108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
5090108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
5091108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
5092108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
5093108322fbScarlsonj 		else
5094108322fbScarlsonj 			retv = 0;
5095108322fbScarlsonj 		zonecfg_close_scratch(fp);
5096108322fbScarlsonj 		return (retv);
5097108322fbScarlsonj 	} else {
5098108322fbScarlsonj 		return (0);
5099108322fbScarlsonj 	}
5100108322fbScarlsonj }
5101108322fbScarlsonj 
5102108322fbScarlsonj int
51030209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
5104108322fbScarlsonj {
5105108322fbScarlsonj 	char *kzone;
51067c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
51070209230bSgjelinek 	int res;
51080209230bSgjelinek 	char pool_err[128];
5109ff17c8bfSgjelinek 	char zpath[MAXPATHLEN];
51109acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
5111123807fbSedp 	brand_handle_t bh = NULL;
51122b24ab6bSSebastien Roy 	dladm_status_t status;
51132b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
5114f4b3ec61Sdh155122 	ushort_t flags;
51157c478bd9Sstevel@tonic-gate 
5116108322fbScarlsonj 	kzone = zone_name;
5117108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5118108322fbScarlsonj 		FILE *fp;
5119108322fbScarlsonj 
5120108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5121108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
5122108322fbScarlsonj 			goto error;
5123108322fbScarlsonj 		}
5124108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5125108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
5126108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
5127108322fbScarlsonj 			zonecfg_close_scratch(fp);
5128108322fbScarlsonj 			goto error;
5129108322fbScarlsonj 		}
5130108322fbScarlsonj 		zonecfg_close_scratch(fp);
5131108322fbScarlsonj 		kzone = kernzone;
5132108322fbScarlsonj 	}
5133108322fbScarlsonj 
5134108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
51357c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
51367c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
5137108322fbScarlsonj 		if (unmount_cmd)
5138108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
51397c478bd9Sstevel@tonic-gate 		goto error;
51407c478bd9Sstevel@tonic-gate 	}
51417c478bd9Sstevel@tonic-gate 
51420dc2366fSVenugopal Iyer 	if (remove_datalink_pool(zlogp, zoneid) != 0) {
51430dc2366fSVenugopal Iyer 		zerror(zlogp, B_FALSE, "unable clear datalink pool property");
51440dc2366fSVenugopal Iyer 		goto error;
51450dc2366fSVenugopal Iyer 	}
51460dc2366fSVenugopal Iyer 
5147550b6e40SSowmini Varadhan 	if (remove_datalink_protect(zlogp, zoneid) != 0) {
5148550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE,
5149550b6e40SSowmini Varadhan 		    "unable clear datalink protect property");
5150550b6e40SSowmini Varadhan 		goto error;
5151550b6e40SSowmini Varadhan 	}
5152550b6e40SSowmini Varadhan 
5153550b6e40SSowmini Varadhan 	/*
5154550b6e40SSowmini Varadhan 	 * The datalinks assigned to the zone will be removed from the NGZ as
5155550b6e40SSowmini Varadhan 	 * part of zone_shutdown() so that we need to remove protect/pool etc.
5156550b6e40SSowmini Varadhan 	 * before zone_shutdown(). Even if the shutdown itself fails, the zone
5157550b6e40SSowmini Varadhan 	 * will not be able to violate any constraints applied because the
5158550b6e40SSowmini Varadhan 	 * datalinks are no longer available to the zone.
5159550b6e40SSowmini Varadhan 	 */
51607c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
51617c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
51627c478bd9Sstevel@tonic-gate 		goto error;
51637c478bd9Sstevel@tonic-gate 	}
51647c478bd9Sstevel@tonic-gate 
5165ff17c8bfSgjelinek 	/* Get the zonepath of this zone */
5166ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
5167ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
51689acbbeafSnn35248 		goto error;
51699acbbeafSnn35248 	}
51709acbbeafSnn35248 
51719acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
517262868012SSteve Lawrence 	if ((bh = brand_open(brand_name)) == NULL) {
51739acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
51749acbbeafSnn35248 		return (-1);
51759acbbeafSnn35248 	}
51769acbbeafSnn35248 	/*
51779acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
51789acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
51799acbbeafSnn35248 	 */
51809acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
5181ff17c8bfSgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
5182ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
5183123807fbSedp 		brand_close(bh);
51849acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
51859acbbeafSnn35248 		    "halt callback.");
51869acbbeafSnn35248 		goto error;
51879acbbeafSnn35248 	}
5188123807fbSedp 	brand_close(bh);
51899acbbeafSnn35248 
51909acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
5191c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
51929acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
51939acbbeafSnn35248 		goto error;
51949acbbeafSnn35248 	}
51959acbbeafSnn35248 
5196f4b3ec61Sdh155122 	if (!unmount_cmd) {
5197f4b3ec61Sdh155122 		zone_iptype_t iptype;
5198f4b3ec61Sdh155122 
5199f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
5200f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
52012b24ab6bSSebastien Roy 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
5202f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
5203f4b3ec61Sdh155122 				    "ip-type");
52047c478bd9Sstevel@tonic-gate 				goto error;
52057c478bd9Sstevel@tonic-gate 			}
5206f4b3ec61Sdh155122 		} else {
5207f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
5208f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
5209f4b3ec61Sdh155122 			else
5210f4b3ec61Sdh155122 				iptype = ZS_SHARED;
5211f4b3ec61Sdh155122 		}
5212f4b3ec61Sdh155122 
5213f4b3ec61Sdh155122 		switch (iptype) {
5214f4b3ec61Sdh155122 		case ZS_SHARED:
5215f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
5216f4b3ec61Sdh155122 			    zoneid) != 0) {
5217f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
5218f4b3ec61Sdh155122 				    "network interfaces in zone");
5219f4b3ec61Sdh155122 				goto error;
5220f4b3ec61Sdh155122 			}
5221f4b3ec61Sdh155122 			break;
5222f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
5223f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
5224f4b3ec61Sdh155122 			    zoneid) != 0) {
5225f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
5226f4b3ec61Sdh155122 				    "network interfaces in zone");
5227f4b3ec61Sdh155122 				goto error;
5228f4b3ec61Sdh155122 			}
52292b24ab6bSSebastien Roy 			status = dladm_zone_halt(dld_handle, zoneid);
52302b24ab6bSSebastien Roy 			if (status != DLADM_STATUS_OK) {
52312b24ab6bSSebastien Roy 				zerror(zlogp, B_FALSE, "unable to notify "
52322b24ab6bSSebastien Roy 				    "dlmgmtd of zone halt: %s",
52332b24ab6bSSebastien Roy 				    dladm_status2str(status, errmsg));
52342b24ab6bSSebastien Roy 			}
5235f4b3ec61Sdh155122 			break;
5236f4b3ec61Sdh155122 		}
5237f4b3ec61Sdh155122 	}
52387c478bd9Sstevel@tonic-gate 
5239108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
52407c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
52417c478bd9Sstevel@tonic-gate 		goto error;
52427c478bd9Sstevel@tonic-gate 	}
52437c478bd9Sstevel@tonic-gate 
5244108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
52457c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
52467c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
52477c478bd9Sstevel@tonic-gate 		goto error;
52487c478bd9Sstevel@tonic-gate 	}
52497c478bd9Sstevel@tonic-gate 
52500209230bSgjelinek 	/*
5251cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
5252cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
5253cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
5254cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
5255cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
5256cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
5257cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
5258cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
52590209230bSgjelinek 	 */
5260cf6b13d2Sgjelinek 	if (!unmount_cmd) {
5261cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
5262cf6b13d2Sgjelinek 
5263cf6b13d2Sgjelinek 		if (rebooting) {
5264cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
5265cf6b13d2Sgjelinek 			zone_dochandle_t handle;
5266cf6b13d2Sgjelinek 
5267cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
5268cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
5269cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
5270cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
5271e767a340Sgjelinek 
5272e767a340Sgjelinek 			zonecfg_fini_handle(handle);
5273cf6b13d2Sgjelinek 		}
5274cf6b13d2Sgjelinek 
5275cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
52760209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
52770209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
52780209230bSgjelinek 				if (res == Z_POOL)
52790209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
52800209230bSgjelinek 			}
52810209230bSgjelinek 		}
5282cf6b13d2Sgjelinek 	}
52830209230bSgjelinek 
528445916cd2Sjpk 	remove_mlps(zlogp, zoneid);
528545916cd2Sjpk 
52867c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
52877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
52887c478bd9Sstevel@tonic-gate 		goto error;
52897c478bd9Sstevel@tonic-gate 	}
5290108322fbScarlsonj 
5291108322fbScarlsonj 	/*
5292108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
5293108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
5294108322fbScarlsonj 	 */
5295108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5296108322fbScarlsonj 		goto error;
5297108322fbScarlsonj 
5298108322fbScarlsonj 	lofs_discard_mnttab();
52997c478bd9Sstevel@tonic-gate 	return (0);
53007c478bd9Sstevel@tonic-gate 
53017c478bd9Sstevel@tonic-gate error:
5302108322fbScarlsonj 	lofs_discard_mnttab();
53037c478bd9Sstevel@tonic-gate 	return (-1);
53047c478bd9Sstevel@tonic-gate }
5305