xref: /titanic_53/usr/src/cmd/zoneadmd/vplat.c (revision 9d48116cb5a39508b54e22ba155d54c7e6cbed02)
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 /*
23cf6b13d2Sgjelinek  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
317c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
327c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
337c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
347c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
357c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
367c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
37108322fbScarlsonj  *
38108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
39108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
40108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
41108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
42108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
43108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
44108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
45108322fbScarlsonj  * without relying on any binaries in the zone itself.
46108322fbScarlsonj  *
47108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
48108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
49108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
50108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
51108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
52108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
53108322fbScarlsonj  * process.
54108322fbScarlsonj  *
55108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
56108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
57108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
58108322fbScarlsonj  * $ZONEPATH/lu as the root.
59108322fbScarlsonj  *
60108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
61108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
62108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
63108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/param.h>
677c478bd9Sstevel@tonic-gate #include <sys/mount.h>
687c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
697c478bd9Sstevel@tonic-gate #include <sys/socket.h>
707c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
717c478bd9Sstevel@tonic-gate #include <sys/types.h>
727c478bd9Sstevel@tonic-gate #include <sys/stat.h>
737c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
747c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
757c478bd9Sstevel@tonic-gate #include <sys/conf.h>
767c478bd9Sstevel@tonic-gate 
77f4b3ec61Sdh155122 #include <sys/dlpi.h>
78f4b3ec61Sdh155122 #include <libdlpi.h>
79f595a68aSyz147064 #include <libdllink.h>
80f4b3ec61Sdh155122 
817c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
827c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
837c478bd9Sstevel@tonic-gate #include <netinet/in.h>
847c478bd9Sstevel@tonic-gate #include <net/route.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <stdio.h>
877c478bd9Sstevel@tonic-gate #include <errno.h>
887c478bd9Sstevel@tonic-gate #include <fcntl.h>
897c478bd9Sstevel@tonic-gate #include <unistd.h>
907c478bd9Sstevel@tonic-gate #include <rctl.h>
917c478bd9Sstevel@tonic-gate #include <stdlib.h>
927c478bd9Sstevel@tonic-gate #include <string.h>
937c478bd9Sstevel@tonic-gate #include <strings.h>
947c478bd9Sstevel@tonic-gate #include <wait.h>
957c478bd9Sstevel@tonic-gate #include <limits.h>
967c478bd9Sstevel@tonic-gate #include <libgen.h>
97fa9e4066Sahrens #include <libzfs.h>
98facf4a8dSllai1 #include <libdevinfo.h>
997c478bd9Sstevel@tonic-gate #include <zone.h>
1007c478bd9Sstevel@tonic-gate #include <assert.h>
101555afedfScarlsonj #include <libcontract.h>
102555afedfScarlsonj #include <libcontract_priv.h>
103555afedfScarlsonj #include <uuid/uuid.h>
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1067c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1077c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1087c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
109fa9e4066Sahrens #include <sys/fs/zfs.h>
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #include <pool.h>
1127c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1130209230bSgjelinek #include <sys/priocntl.h>
1147c478bd9Sstevel@tonic-gate 
1159acbbeafSnn35248 #include <libbrand.h>
1169acbbeafSnn35248 #include <sys/brand.h>
1177c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
11839d3e169Sevanl #include <synch.h>
11922321485Svp157776 
1207c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
12145916cd2Sjpk #include <tsol/label.h>
12245916cd2Sjpk #include <libtsnet.h>
12345916cd2Sjpk #include <sys/priv.h>
1247c478bd9Sstevel@tonic-gate 
125*9d48116cSdh155122 #include <sys/dld.h>	/* DLIOCHOLDVLAN and friends */
126*9d48116cSdh155122 
1277c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1287c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1317c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13445916cd2Sjpk #define	MAXTNZLEN	2048
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* for routing socket */
1377c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1387c478bd9Sstevel@tonic-gate 
139108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
140108322fbScarlsonj static char kernzone[ZONENAME_MAX];
141108322fbScarlsonj 
142108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
143108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
144108322fbScarlsonj 
14545916cd2Sjpk /* for Trusted Extensions */
14645916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
14745916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
14845916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
149*9d48116cSdh155122 static int driver_hold_link(const char *name, zoneid_t zoneid);
150*9d48116cSdh155122 static int driver_rele_link(const char *name, zoneid_t zoneid);
151*9d48116cSdh155122 
15245916cd2Sjpk static m_label_t *zlabel = NULL;
15345916cd2Sjpk static m_label_t *zid_label = NULL;
15445916cd2Sjpk static priv_set_t *zprivs = NULL;
15545916cd2Sjpk 
1567c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1577c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
160108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
161108322fbScarlsonj  * data) only once every N times through the loop.
162108322fbScarlsonj  */
163108322fbScarlsonj #define	MNTTAB_HUNK	32
164108322fbScarlsonj 
165108322fbScarlsonj /*
1667c478bd9Sstevel@tonic-gate  * Private autofs system call
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate static int
1717c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
179108322fbScarlsonj static void
180108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
181108322fbScarlsonj {
182108322fbScarlsonj 	uint_t i;
183108322fbScarlsonj 
184108322fbScarlsonj 	if (mnt_array == NULL)
185108322fbScarlsonj 		return;
186108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
187108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
188108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
189108322fbScarlsonj 		free(mnt_array[i].mnt_special);
190108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
191108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
192108322fbScarlsonj 	}
193108322fbScarlsonj 	free(mnt_array);
194108322fbScarlsonj }
195108322fbScarlsonj 
196108322fbScarlsonj /*
197108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
198108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
199108322fbScarlsonj  * array in "nelemp".
200108322fbScarlsonj  */
201108322fbScarlsonj static int
202108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
203108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
204108322fbScarlsonj {
205108322fbScarlsonj 	struct mnttab mnt;
206108322fbScarlsonj 	struct mnttab *mnts;
207108322fbScarlsonj 	struct mnttab *mnp;
208108322fbScarlsonj 	uint_t nmnt;
209108322fbScarlsonj 
210108322fbScarlsonj 	rewind(mnttab);
211108322fbScarlsonj 	resetmnttab(mnttab);
212108322fbScarlsonj 	nmnt = 0;
213108322fbScarlsonj 	mnts = NULL;
214108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
215108322fbScarlsonj 		struct mnttab *tmp_array;
216108322fbScarlsonj 
217108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
218108322fbScarlsonj 			continue;
219108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
220108322fbScarlsonj 			tmp_array = realloc(mnts,
221108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
222108322fbScarlsonj 			if (tmp_array == NULL) {
223108322fbScarlsonj 				free_mnttable(mnts, nmnt);
224108322fbScarlsonj 				return (-1);
225108322fbScarlsonj 			}
226108322fbScarlsonj 			mnts = tmp_array;
227108322fbScarlsonj 		}
228108322fbScarlsonj 		mnp = &mnts[nmnt++];
229108322fbScarlsonj 
230108322fbScarlsonj 		/*
231108322fbScarlsonj 		 * Zero out any fields we're not using.
232108322fbScarlsonj 		 */
233108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
234108322fbScarlsonj 
235108322fbScarlsonj 		if (mnt.mnt_special != NULL)
236108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
237108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
238108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
239108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
240108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
241108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
242108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
243108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
244108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
245108322fbScarlsonj 			free_mnttable(mnts, nmnt);
246108322fbScarlsonj 			return (-1);
247108322fbScarlsonj 		}
248108322fbScarlsonj 	}
249108322fbScarlsonj 	*mnt_arrayp = mnts;
250108322fbScarlsonj 	*nelemp = nmnt;
251108322fbScarlsonj 	return (0);
252108322fbScarlsonj }
253108322fbScarlsonj 
254108322fbScarlsonj /*
255108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
256108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
257108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
258108322fbScarlsonj  * reread of the list of mount points
259108322fbScarlsonj  */
260108322fbScarlsonj static void
261108322fbScarlsonj lofs_discard_mnttab(void)
262108322fbScarlsonj {
263108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
264108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
265108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
266108322fbScarlsonj }
267108322fbScarlsonj 
268108322fbScarlsonj static int
269108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
270108322fbScarlsonj {
271108322fbScarlsonj 	FILE *mnttab;
272108322fbScarlsonj 	uint_t nmnts;
273108322fbScarlsonj 
274108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
275108322fbScarlsonj 		return (-1);
276108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
277108322fbScarlsonj 	    &nmnts) == -1) {
278108322fbScarlsonj 		(void) fclose(mnttab);
279108322fbScarlsonj 		return (-1);
280108322fbScarlsonj 	}
281108322fbScarlsonj 	(void) fclose(mnttab);
282108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
283108322fbScarlsonj 	return (0);
284108322fbScarlsonj }
285108322fbScarlsonj 
286108322fbScarlsonj /*
287108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
288108322fbScarlsonj  * path and resolves them all down to an absolute path.
289108322fbScarlsonj  */
290910f48daSedp void
291108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
292108322fbScarlsonj {
293108322fbScarlsonj 	int len, arlen;
294108322fbScarlsonj 	const char *altroot;
295108322fbScarlsonj 	char tmppath[MAXPATHLEN];
296108322fbScarlsonj 	boolean_t outside_altroot;
297108322fbScarlsonj 
298108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
299108322fbScarlsonj 		return;
300108322fbScarlsonj 	tmppath[len] = '\0';
301108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
302108322fbScarlsonj 
303108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
304108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
305108322fbScarlsonj 		return;
306108322fbScarlsonj 
307108322fbScarlsonj 	altroot = zonecfg_get_root();
308108322fbScarlsonj 	arlen = strlen(altroot);
309108322fbScarlsonj 	outside_altroot = B_FALSE;
310108322fbScarlsonj 	for (;;) {
311108322fbScarlsonj 		struct mnttab *mnp;
312108322fbScarlsonj 
3131e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3141e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3151e9d8f9fSdminer 		    mnp--) {
316108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
317108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3181e9d8f9fSdminer 			    mnp->mnt_special == NULL)
319108322fbScarlsonj 				continue;
320108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
321108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
322108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
323108322fbScarlsonj 				break;
324108322fbScarlsonj 		}
3251e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3261e9d8f9fSdminer 			break;
3271e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3281e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
329108322fbScarlsonj 			break;
330108322fbScarlsonj 		if (outside_altroot) {
331108322fbScarlsonj 			char *cp;
332108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
333108322fbScarlsonj 
334108322fbScarlsonj 			/*
335108322fbScarlsonj 			 * If we run into a read-only mount outside of the
336108322fbScarlsonj 			 * alternate root environment, then the user doesn't
337108322fbScarlsonj 			 * want this path to be made read-write.
338108322fbScarlsonj 			 */
339108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
340108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
341108322fbScarlsonj 			    NULL &&
342108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
343108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
344108322fbScarlsonj 				break;
345108322fbScarlsonj 			}
346108322fbScarlsonj 		} else if (arlen > 0 &&
347108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
348108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
349108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
350108322fbScarlsonj 			outside_altroot = B_TRUE;
351108322fbScarlsonj 		}
352108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
353108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
354108322fbScarlsonj 		    mnp->mnt_special, path + len);
355108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
356108322fbScarlsonj 			break;
357108322fbScarlsonj 		path[len] = '\0';
358108322fbScarlsonj 	}
359108322fbScarlsonj }
360108322fbScarlsonj 
361108322fbScarlsonj /*
362108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
363108322fbScarlsonj  * referenced device is already mounted somewhere.
364108322fbScarlsonj  */
365108322fbScarlsonj static int
366108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
367108322fbScarlsonj {
368108322fbScarlsonj 	struct mnttab *mnp;
369108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
370108322fbScarlsonj 
371108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
372108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
373108322fbScarlsonj 		return (-1);
374108322fbScarlsonj 
375108322fbScarlsonj 	/*
376108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
377108322fbScarlsonj 	 * no need to worry about conflicting mounts.
378108322fbScarlsonj 	 */
379108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
380108322fbScarlsonj 	    mnp++) {
381108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
382108322fbScarlsonj 			break;
383108322fbScarlsonj 	}
384108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
385108322fbScarlsonj 		return (0);
386108322fbScarlsonj 
387108322fbScarlsonj 	/*
388108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
389108322fbScarlsonj 	 */
390108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
391108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
392108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
393108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
394108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
395108322fbScarlsonj 
396108322fbScarlsonj 	/*
397108322fbScarlsonj 	 * Discard all but one of the original options and set that to be the
398108322fbScarlsonj 	 * same set of options used for inherit package directory resources.
399108322fbScarlsonj 	 */
400108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
401108322fbScarlsonj 	if (optptr == NULL) {
402108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
403108322fbScarlsonj 		if (optptr == NULL) {
404108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
405108322fbScarlsonj 			    fsptr->zone_fs_dir);
406108322fbScarlsonj 			return (-1);
407108322fbScarlsonj 		}
408108322fbScarlsonj 	} else {
409108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
410108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
411108322fbScarlsonj 			free(onext);
412108322fbScarlsonj 		}
413108322fbScarlsonj 	}
414108322fbScarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
415108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
416108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
417108322fbScarlsonj 	return (0);
418108322fbScarlsonj }
419108322fbScarlsonj 
420d314f035Sedp int
42127e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
42227e6fb21Sdp     uid_t userid, gid_t groupid)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4257c478bd9Sstevel@tonic-gate 	struct stat st;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4287c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4307c478bd9Sstevel@tonic-gate 		    subdir);
4317c478bd9Sstevel@tonic-gate 		return (-1);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4357c478bd9Sstevel@tonic-gate 		/*
4367c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4377c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4387c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4397c478bd9Sstevel@tonic-gate 		 */
4407c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
44145916cd2Sjpk 			if (is_system_labeled() &&
44245916cd2Sjpk 			    S_ISREG(st.st_mode)) {
44345916cd2Sjpk 				/*
44445916cd2Sjpk 				 * The need to mount readonly copies of
44545916cd2Sjpk 				 * global zone /etc/ files is unique to
44645916cd2Sjpk 				 * Trusted Extensions.
44745916cd2Sjpk 				 */
44845916cd2Sjpk 				if (strncmp(subdir, "/etc/",
44945916cd2Sjpk 				    strlen("/etc/")) != 0) {
45045916cd2Sjpk 					zerror(zlogp, B_FALSE,
45145916cd2Sjpk 					    "%s is not in /etc", path);
4527c478bd9Sstevel@tonic-gate 					return (-1);
4537c478bd9Sstevel@tonic-gate 				}
45445916cd2Sjpk 			} else {
45545916cd2Sjpk 				zerror(zlogp, B_FALSE,
45645916cd2Sjpk 				    "%s is not a directory", path);
45745916cd2Sjpk 				return (-1);
45845916cd2Sjpk 			}
45945916cd2Sjpk 		}
46027e6fb21Sdp 		return (0);
46127e6fb21Sdp 	}
46227e6fb21Sdp 
46327e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
4647c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4657c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4667c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4677c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4687c478bd9Sstevel@tonic-gate 		else
4697c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4707c478bd9Sstevel@tonic-gate 		return (-1);
4717c478bd9Sstevel@tonic-gate 	}
47227e6fb21Sdp 
47327e6fb21Sdp 	(void) chown(path, userid, groupid);
4747c478bd9Sstevel@tonic-gate 	return (0);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate static void
4787c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	uint_t i;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (types == NULL)
4837c478bd9Sstevel@tonic-gate 		return;
4847c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
4857c478bd9Sstevel@tonic-gate 		free(types[i]);
4867c478bd9Sstevel@tonic-gate 	free(types);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate static char **
4907c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	char **types = NULL;
4937c478bd9Sstevel@tonic-gate 	FILE *fp;
4947c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
4957c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
4967c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
4977c478bd9Sstevel@tonic-gate 	uint_t i;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5017c478bd9Sstevel@tonic-gate 		return (NULL);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5057c478bd9Sstevel@tonic-gate 	 */
5067c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5077c478bd9Sstevel@tonic-gate 		lines++;
5087c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5097c478bd9Sstevel@tonic-gate 		goto out;
5107c478bd9Sstevel@tonic-gate 	rewind(fp);
5117c478bd9Sstevel@tonic-gate 	/*
5127c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5137c478bd9Sstevel@tonic-gate 	 */
5147c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5157c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5177c478bd9Sstevel@tonic-gate 		goto out;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 	i = 0;
5207c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5217c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5227c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5237c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5247c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5257c478bd9Sstevel@tonic-gate 			types = NULL;
5267c478bd9Sstevel@tonic-gate 			goto out;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5297c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5307c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5317c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5327c478bd9Sstevel@tonic-gate 			types = NULL;
5337c478bd9Sstevel@tonic-gate 			goto out;
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		i++;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate out:
5387c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5397c478bd9Sstevel@tonic-gate 	return (types);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate static boolean_t
5437c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	uint_t i;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5487c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5497c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5507c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5517c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
556108322fbScarlsonj /*
557108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
558108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
559108322fbScarlsonj  */
5607c478bd9Sstevel@tonic-gate static void
561108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5627c478bd9Sstevel@tonic-gate {
56384561e8cStd153743 	assert(zone_isnative || zone_iscluster);
5649acbbeafSnn35248 
565108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
566108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
567108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5727c478bd9Sstevel@tonic-gate  *
5737c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5747c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5757c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5767c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5797c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5807c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5817c478bd9Sstevel@tonic-gate  *
5827c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5837c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5847c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5857c478bd9Sstevel@tonic-gate  *
5867c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5877c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5887c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5897c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5907c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5917c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5927c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5937c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5947c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5957c478bd9Sstevel@tonic-gate  * graceful mounts first.
5967c478bd9Sstevel@tonic-gate  *
5977c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5987c478bd9Sstevel@tonic-gate  */
5997c478bd9Sstevel@tonic-gate static int
600108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	int error = 0;
6037c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6047c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6057c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6067c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6077c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6087c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6097c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6107c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6147c478bd9Sstevel@tonic-gate 		return (-1);
6157c478bd9Sstevel@tonic-gate 	}
616108322fbScarlsonj 	if (unmount_cmd)
617108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6207c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6217c478bd9Sstevel@tonic-gate 
62245916cd2Sjpk 	/*
62345916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
62445916cd2Sjpk 	 * of our zone's /export/home
62545916cd2Sjpk 	 */
62648451833Scarlsonj 	if (!unmount_cmd)
62745916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
62845916cd2Sjpk 
6297c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6307c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6317c478bd9Sstevel@tonic-gate 		return (-1);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6357c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6367c478bd9Sstevel@tonic-gate 	 */
6377c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6387c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6397c478bd9Sstevel@tonic-gate 		error++;
6407c478bd9Sstevel@tonic-gate 		goto out;
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/*
6447c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6457c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6487c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6497c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6507c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6517c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6527c478bd9Sstevel@tonic-gate 		char *path;
6537c478bd9Sstevel@tonic-gate 		uint_t i;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 		mnts = NULL;
6567c478bd9Sstevel@tonic-gate 		nmnt = 0;
6577c478bd9Sstevel@tonic-gate 		/*
6587c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6597c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6607c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6617c478bd9Sstevel@tonic-gate 		 * filesystems.
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6647c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6657c478bd9Sstevel@tonic-gate 			error++;
6667c478bd9Sstevel@tonic-gate 			goto out;
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6697c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6707c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6717c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6727c478bd9Sstevel@tonic-gate 			/*
6737c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6747c478bd9Sstevel@tonic-gate 			 *
6757c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6767c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6777c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
6787c478bd9Sstevel@tonic-gate 			 */
6797c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6807c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6817c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6827c478bd9Sstevel@tonic-gate 			}
6837c478bd9Sstevel@tonic-gate 			/*
6847c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6857c478bd9Sstevel@tonic-gate 			 */
6867c478bd9Sstevel@tonic-gate 			if (stuck) {
6877c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6887c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6897c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
6907c478bd9Sstevel@tonic-gate 				} else {
6917c478bd9Sstevel@tonic-gate 					/*
6927c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
6937c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
6947c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
6957c478bd9Sstevel@tonic-gate 					 * bail.
6967c478bd9Sstevel@tonic-gate 					 */
6977c478bd9Sstevel@tonic-gate 					error++;
6987c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6997c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
7007c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
7017c478bd9Sstevel@tonic-gate 					goto out;
7027c478bd9Sstevel@tonic-gate 				}
7037c478bd9Sstevel@tonic-gate 			}
7047c478bd9Sstevel@tonic-gate 			/*
7057c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7067c478bd9Sstevel@tonic-gate 			 */
7077c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7087c478bd9Sstevel@tonic-gate 				newcount++;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7137c478bd9Sstevel@tonic-gate 			break;
7147c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7157c478bd9Sstevel@tonic-gate 			/*
7167c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7177c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7187c478bd9Sstevel@tonic-gate 			 */
7197c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		/*
7247c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7257c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7267c478bd9Sstevel@tonic-gate 		 */
7277c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7287c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7297c478bd9Sstevel@tonic-gate 			error++;
7307c478bd9Sstevel@tonic-gate 			goto out;
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate out:
7357c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7367c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7377c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate static int
7417c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7447c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7517c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7527c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7537c478bd9Sstevel@tonic-gate  *
7547c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7557c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7567c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7577c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7587c478bd9Sstevel@tonic-gate  * remains the case.
7597c478bd9Sstevel@tonic-gate  */
7607c478bd9Sstevel@tonic-gate static int
7617c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7627c478bd9Sstevel@tonic-gate {
7637c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7647c478bd9Sstevel@tonic-gate 	int child_status = 0;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	/*
7677c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7707c478bd9Sstevel@tonic-gate 	child_pid = fork();
7717c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7727c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
7737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7747c478bd9Sstevel@tonic-gate 		return (-1);
7757c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
7767c478bd9Sstevel@tonic-gate 		closefrom(0);
7771390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7781390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7791390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7801390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7817c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
7827c478bd9Sstevel@tonic-gate 		/*
7837c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7847c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7857c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7867c478bd9Sstevel@tonic-gate 		 * accordingly.
7877c478bd9Sstevel@tonic-gate 		 */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7907c478bd9Sstevel@tonic-gate 	} else {
7917c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7957c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7967c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7977c478bd9Sstevel@tonic-gate 		return (-1);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
8007c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
8017c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8027c478bd9Sstevel@tonic-gate 		return (-1);
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate static int
8087c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8117c478bd9Sstevel@tonic-gate 	char *argv[4];
8127c478bd9Sstevel@tonic-gate 	int status;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8167c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8177c478bd9Sstevel@tonic-gate 	 */
8187c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8199acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8217c478bd9Sstevel@tonic-gate 		return (-1);
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
8257c478bd9Sstevel@tonic-gate 	argv[1] = "-m";
8267c478bd9Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
8277c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8307c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8317c478bd9Sstevel@tonic-gate 		return (status);
8327c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8337c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8347c478bd9Sstevel@tonic-gate 	return (-1);
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate static int
8387c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8397c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8427c478bd9Sstevel@tonic-gate 	char *argv[6];
8437c478bd9Sstevel@tonic-gate 	int status;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	/*
8467c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8477c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8487c478bd9Sstevel@tonic-gate 	 */
8497c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8509acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8517c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8527c478bd9Sstevel@tonic-gate 		return (-1);
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
8557c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
8567c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
8577c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
8587c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
8597c478bd9Sstevel@tonic-gate 	} else {
8607c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
8617c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
8627c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
8637c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
8647c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8687c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8697c478bd9Sstevel@tonic-gate 		return (status);
8707c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
8717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8727c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8737c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8747c478bd9Sstevel@tonic-gate 	else
8757c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8767c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8777c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8787c478bd9Sstevel@tonic-gate 	return (-1);
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate /*
882d314f035Sedp  * Check if a given mount point path exists.
883d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
884d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
885d314f035Sedp  * component of the mount point path, so it must be a directory.
886d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
887d314f035Sedp  * path, so the mount point itself can be anything aside from a
888d314f035Sedp  * symbolic link.
889d314f035Sedp  *
890d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
891d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
892d314f035Sedp  * If the path doesn't exist return a positive value.
8937c478bd9Sstevel@tonic-gate  */
8947c478bd9Sstevel@tonic-gate static int
895d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	struct stat statbuf;
8987c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
8997c478bd9Sstevel@tonic-gate 	int res;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9027c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
903d314f035Sedp 			return (1);
9047c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9057c478bd9Sstevel@tonic-gate 		return (-1);
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9087c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9097c478bd9Sstevel@tonic-gate 		return (-1);
9107c478bd9Sstevel@tonic-gate 	}
911d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9127c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9137c478bd9Sstevel@tonic-gate 		return (-1);
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9167c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9177c478bd9Sstevel@tonic-gate 		return (-1);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9207c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9217c478bd9Sstevel@tonic-gate 		/*
922d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9237c478bd9Sstevel@tonic-gate 		 */
9247c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9257c478bd9Sstevel@tonic-gate 		return (-1);
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 	return (0);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
931d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
932d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
933d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
934d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
935d314f035Sedp  * object.  (Different filesystems can support different mount points,
936d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
937d314f035Sedp  * while "ufs" just supports directories.)
9387c478bd9Sstevel@tonic-gate  *
939d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
940d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
941d314f035Sedp  * If the path doesn't exist return a positive value.
9427c478bd9Sstevel@tonic-gate  */
943d314f035Sedp int
944d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
945d314f035Sedp     const char *dir, const char *fstype)
9467c478bd9Sstevel@tonic-gate {
947d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
948d314f035Sedp 	int rv;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	/*
951d314f035Sedp 	 * Sanity check the target mount point path.
952d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
9537c478bd9Sstevel@tonic-gate 	 */
954d314f035Sedp 	if (dir[0] != '/') {
955d314f035Sedp 		if (spec[0] == '\0') {
956d314f035Sedp 			/*
957d314f035Sedp 			 * This must be an invalid ipd entry (see comments
958d314f035Sedp 			 * in mount_filesystems_ipdent()).
959d314f035Sedp 			 */
960d314f035Sedp 			zerror(zlogp, B_FALSE,
961d314f035Sedp 			    "invalid inherit-pkg-dir entry: \"%s\"", dir);
962d314f035Sedp 		} else {
963d314f035Sedp 			/* Something went wrong. */
964d314f035Sedp 			zerror(zlogp, B_FALSE, "invalid mount directory, "
965d314f035Sedp 			    "type: \"%s\", special: \"%s\", dir: \"%s\"",
966d314f035Sedp 			    fstype, spec, dir);
967d314f035Sedp 		}
968d314f035Sedp 		return (-1);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
971d314f035Sedp 	/*
972d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
973d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
974d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
975d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
976d314f035Sedp 	 * cannonical path check in valid_mount_point()).
977d314f035Sedp 	 */
978d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
979d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
980d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
981d314f035Sedp 		    rootpath, dir);
982d314f035Sedp 		return (-1);
983d314f035Sedp 	}
984d314f035Sedp 
985d314f035Sedp 	/*
986d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
987d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
988d314f035Sedp 	 */
9897c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
9907c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
9917c478bd9Sstevel@tonic-gate 	do {
992d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
9937c478bd9Sstevel@tonic-gate 		*slashp = '\0';
994d314f035Sedp 		if (slashp_next != NULL) {
995d314f035Sedp 			/* This is an intermediary mount path component. */
996d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
997d314f035Sedp 		} else {
998d314f035Sedp 			/* This is the last component of the mount path. */
999d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1000d314f035Sedp 		}
1001d314f035Sedp 		if (rv < 0)
1002d314f035Sedp 			return (rv);
10037c478bd9Sstevel@tonic-gate 		*slashp = '/';
1004d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1005d314f035Sedp 	return (rv);
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate static int
10099acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10109acbbeafSnn35248 {
10119acbbeafSnn35248 	di_prof_t prof = arg;
10129acbbeafSnn35248 
10139acbbeafSnn35248 	if (name == NULL)
10149acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
10159acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
10169acbbeafSnn35248 }
10179acbbeafSnn35248 
10189acbbeafSnn35248 static int
10199acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10209acbbeafSnn35248 {
10219acbbeafSnn35248 	di_prof_t prof = arg;
10229acbbeafSnn35248 
10239acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
10249acbbeafSnn35248 }
10259acbbeafSnn35248 
1026f4b3ec61Sdh155122 static int
1027f4b3ec61Sdh155122 get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1028f4b3ec61Sdh155122 {
1029f4b3ec61Sdh155122 	zone_dochandle_t handle;
1030f4b3ec61Sdh155122 
1031f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
1032f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1033f4b3ec61Sdh155122 		return (-1);
1034f4b3ec61Sdh155122 	}
1035f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1036f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
1037f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1038f4b3ec61Sdh155122 		return (-1);
1039f4b3ec61Sdh155122 	}
1040f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1041f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1042f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1043f4b3ec61Sdh155122 		return (-1);
1044f4b3ec61Sdh155122 	}
1045f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1046f4b3ec61Sdh155122 	return (0);
1047f4b3ec61Sdh155122 }
1048f4b3ec61Sdh155122 
10499acbbeafSnn35248 /*
10509acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10519acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10529acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10539acbbeafSnn35248  */
10549acbbeafSnn35248 static int
10559acbbeafSnn35248 mount_one_dev(zlog_t *zlogp, char *devpath)
10569acbbeafSnn35248 {
10579acbbeafSnn35248 	char			brand[MAXNAMELEN];
10589acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1059123807fbSedp 	brand_handle_t		bh = NULL;
10609acbbeafSnn35248 	struct zone_devtab	ztab;
10619acbbeafSnn35248 	di_prof_t		prof = NULL;
10629acbbeafSnn35248 	int			err;
10639acbbeafSnn35248 	int			retval = -1;
1064f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1065f4b3ec61Sdh155122 	const char 		*curr_iptype;
10669acbbeafSnn35248 
10679acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
10689acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10699acbbeafSnn35248 		goto cleanup;
10709acbbeafSnn35248 	}
10719acbbeafSnn35248 
10729acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
10739acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
1074123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
10759acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
10769acbbeafSnn35248 		goto cleanup;
10779acbbeafSnn35248 	}
10789acbbeafSnn35248 
1079f4b3ec61Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
1080f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1081f4b3ec61Sdh155122 		goto cleanup;
1082f4b3ec61Sdh155122 	}
1083f4b3ec61Sdh155122 	switch (iptype) {
1084f4b3ec61Sdh155122 	case ZS_SHARED:
1085f4b3ec61Sdh155122 		curr_iptype = "shared";
1086f4b3ec61Sdh155122 		break;
1087f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1088f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1089f4b3ec61Sdh155122 		break;
1090f4b3ec61Sdh155122 	}
1091f4b3ec61Sdh155122 
1092123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1093f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
10949acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
10959acbbeafSnn35248 		goto cleanup;
10969acbbeafSnn35248 	}
10979acbbeafSnn35248 
1098123807fbSedp 	if (brand_platform_iter_link(bh,
10999acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11009acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11019acbbeafSnn35248 		goto cleanup;
11029acbbeafSnn35248 	}
11039acbbeafSnn35248 
11049acbbeafSnn35248 	/* Add user-specified devices and directories */
11059acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11069acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11079acbbeafSnn35248 		goto cleanup;
11089acbbeafSnn35248 	}
11099acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11109acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11119acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11129acbbeafSnn35248 		goto cleanup;
11139acbbeafSnn35248 	}
11149acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
11159acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11169acbbeafSnn35248 		    zonecfg_strerror(err));
11179acbbeafSnn35248 		goto cleanup;
11189acbbeafSnn35248 	}
11199acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11209acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11219acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
11229acbbeafSnn35248 			    "user-specified device");
11239acbbeafSnn35248 			goto cleanup;
11249acbbeafSnn35248 		}
11259acbbeafSnn35248 	}
11269acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
11279acbbeafSnn35248 
11289acbbeafSnn35248 	/* Send profile to kernel */
11299acbbeafSnn35248 	if (di_prof_commit(prof)) {
11309acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11319acbbeafSnn35248 		goto cleanup;
11329acbbeafSnn35248 	}
11339acbbeafSnn35248 
11349acbbeafSnn35248 	retval = 0;
11359acbbeafSnn35248 
11369acbbeafSnn35248 cleanup:
1137123807fbSedp 	if (bh != NULL)
1138123807fbSedp 		brand_close(bh);
1139e767a340Sgjelinek 	if (handle != NULL)
11409acbbeafSnn35248 		zonecfg_fini_handle(handle);
11419acbbeafSnn35248 	if (prof)
11429acbbeafSnn35248 		di_prof_fini(prof);
11439acbbeafSnn35248 	return (retval);
11449acbbeafSnn35248 }
11459acbbeafSnn35248 
11469acbbeafSnn35248 static int
11477c478bd9Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
1150108322fbScarlsonj 	char specpath[MAXPATHLEN];
11517c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11527c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11539acbbeafSnn35248 	int rv;
11547c478bd9Sstevel@tonic-gate 
1155d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1156d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11577c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11587c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11597c478bd9Sstevel@tonic-gate 		return (-1);
1160d314f035Sedp 	} else if (rv > 0) {
1161d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1162d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1163d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1164d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1165d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1166d314f035Sedp 			return (-1);
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate 
1169d314f035Sedp 		/*
1170d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1171d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1172d314f035Sedp 		 * to make sure that the mount point path is canonical,
1173d314f035Sedp 		 * which it can only do if the path exists, so now that
1174d314f035Sedp 		 * we've created the path we have to verify it again.
1175d314f035Sedp 		 */
1176d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1177d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1178d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1179d314f035Sedp 			zerror(zlogp, B_FALSE,
1180d314f035Sedp 			    "%s%s is not a valid mount point",
1181d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
11827c478bd9Sstevel@tonic-gate 			return (-1);
1183d314f035Sedp 		}
1184d314f035Sedp 	}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
11877c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
11907c478bd9Sstevel@tonic-gate 		/*
11917c478bd9Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1192108322fbScarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1193108322fbScarlsonj 		 * can be seen via the alternate zone's root.
11947c478bd9Sstevel@tonic-gate 		 */
1195108322fbScarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1196108322fbScarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1197108322fbScarlsonj 		    sizeof (specpath)) {
1198108322fbScarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1199108322fbScarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1200108322fbScarlsonj 			return (-1);
1201108322fbScarlsonj 		}
1202108322fbScarlsonj 		if (zonecfg_in_alt_root())
1203108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
12047c478bd9Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1205108322fbScarlsonj 		    specpath, path) != 0) {
12067c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1207108322fbScarlsonj 			    specpath);
12087c478bd9Sstevel@tonic-gate 			return (-1);
12097c478bd9Sstevel@tonic-gate 		}
12107c478bd9Sstevel@tonic-gate 		return (0);
12117c478bd9Sstevel@tonic-gate 	}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	/*
12147c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12157c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12167c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12177c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12187c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12197c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12207c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12217c478bd9Sstevel@tonic-gate 	 */
12227c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12247c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12257c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12267c478bd9Sstevel@tonic-gate 		return (-1);
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	/*
1230108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1231fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1232fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1233fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1234fa3d7ae1Sedp 	 * time we get here.
1235108322fbScarlsonj 	 */
1236108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1237108322fbScarlsonj 		struct stat64 st;
1238108322fbScarlsonj 
1239108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
124068eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1241fa3d7ae1Sedp 			/*
1242fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1243fa3d7ae1Sedp 			 * to check if that device is already mounted
1244fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1245fa3d7ae1Sedp 			 * of the device instead of a direct mount
1246fa3d7ae1Sedp 			 */
124768eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1248108322fbScarlsonj 				return (-1);
124968eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1250fa3d7ae1Sedp 			/*
1251fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1252fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1253fa3d7ae1Sedp 			 * this case in order to get at the underlying
1254fa3d7ae1Sedp 			 * read-write path.
1255fa3d7ae1Sedp 			 */
1256fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1257108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1258108322fbScarlsonj 		}
1259108322fbScarlsonj 	}
1260108322fbScarlsonj 
1261108322fbScarlsonj 	/*
12627c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12637c478bd9Sstevel@tonic-gate 	 */
12647c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
12657c478bd9Sstevel@tonic-gate 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0)
12667c478bd9Sstevel@tonic-gate 		return (-1);
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	/*
12697c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
12707c478bd9Sstevel@tonic-gate 	 */
12717c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
12727c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
12737c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12747c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
12757c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
12767c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
12777c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
12787c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
12797c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
12807c478bd9Sstevel@tonic-gate 		}
12817c478bd9Sstevel@tonic-gate 	}
12829acbbeafSnn35248 
12839acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
12849acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
12859acbbeafSnn35248 		return (rv);
12869acbbeafSnn35248 
12879acbbeafSnn35248 	/*
12889acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
12899acbbeafSnn35248 	 * we're done.
12909acbbeafSnn35248 	 */
12919acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
12929acbbeafSnn35248 		return (0);
12939acbbeafSnn35248 
12949acbbeafSnn35248 	/*
12959acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
12969acbbeafSnn35248 	 * need to configure it.
12979acbbeafSnn35248 	 */
12989acbbeafSnn35248 	return (mount_one_dev(zlogp, path));
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate static void
13027c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13037c478bd9Sstevel@tonic-gate {
13047c478bd9Sstevel@tonic-gate 	uint_t i;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13077c478bd9Sstevel@tonic-gate 		return;
13087c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13097c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13107c478bd9Sstevel@tonic-gate 	free(fsarray);
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate 
1313108322fbScarlsonj /*
1314f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1315f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1316f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1317f4368d3dSvp157776  * is done this way because:
1318f4368d3dSvp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
1319f4368d3dSvp157776  * 	We loopback mount zone's own /etc and /var into the root of the
1320f4368d3dSvp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1321f4368d3dSvp157776  * 	need to delay the mount of /var till the zone's root gets populated.
1322f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1323f4368d3dSvp157776  * 	build_mounted_post_var() which gets called only after the zone
1324f4368d3dSvp157776  * 	specific filesystems are mounted.
1325108322fbScarlsonj  */
1326108322fbScarlsonj static boolean_t
1327f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
132816bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1329108322fbScarlsonj {
1330108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1331108322fbScarlsonj 	const char **cpp;
1332108322fbScarlsonj 	static const char *mkdirs[] = {
13333f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13343f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1335108322fbScarlsonj 	};
1336108322fbScarlsonj 	char *altstr;
1337f4368d3dSvp157776 	FILE *fp;
1338108322fbScarlsonj 	uuid_t uuid;
1339108322fbScarlsonj 
134084561e8cStd153743 	assert(zone_isnative || zone_iscluster);
13419acbbeafSnn35248 
1342108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
134316bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
134416bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1345108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1346108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1347108322fbScarlsonj 
1348108322fbScarlsonj 	/*
1349108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1350108322fbScarlsonj 	 * zone_mount_early.)
1351108322fbScarlsonj 	 */
1352108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1353108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1354108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1355108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1356108322fbScarlsonj 			return (B_FALSE);
1357108322fbScarlsonj 		}
1358108322fbScarlsonj 	}
1359f4368d3dSvp157776 	/*
1360f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1361f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1362f4368d3dSvp157776 	 * read-only inside the scratch zone.
1363f4368d3dSvp157776 	 */
1364f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1365f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1366f4368d3dSvp157776 	if (altstr == NULL) {
1367f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1368f4368d3dSvp157776 		return (B_FALSE);
1369f4368d3dSvp157776 	}
1370f4368d3dSvp157776 	zonecfg_set_root("");
1371f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1372f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1373f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1374f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1375f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1376f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1377f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1378f4368d3dSvp157776 			return (B_FALSE);
1379f4368d3dSvp157776 		}
1380f4368d3dSvp157776 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1381f4368d3dSvp157776 		    tmp) != 0) {
1382f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1383f4368d3dSvp157776 			    fromdir);
1384f4368d3dSvp157776 			return (B_FALSE);
1385f4368d3dSvp157776 		}
1386f4368d3dSvp157776 	}
1387f4368d3dSvp157776 	zonecfg_set_root(altstr);
1388f4368d3dSvp157776 	free(altstr);
1389f4368d3dSvp157776 
1390f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1391f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1392f4368d3dSvp157776 		return (B_FALSE);
1393f4368d3dSvp157776 	}
1394f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1395f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1396f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1397f4368d3dSvp157776 	}
1398f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1399f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1400f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1401f4368d3dSvp157776 		return (B_FALSE);
1402f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1403f4368d3dSvp157776 	return (B_TRUE);
1404f4368d3dSvp157776 }
1405f4368d3dSvp157776 
1406f4368d3dSvp157776 
1407f4368d3dSvp157776 static boolean_t
140816bca938Svp157776 build_mounted_post_var(zlog_t *zlogp, char *rootpath, const char *luroot)
1409f4368d3dSvp157776 {
1410f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1411f4368d3dSvp157776 	const char **cpp;
1412f4368d3dSvp157776 	static const char *localdirs[] = {
1413f4368d3dSvp157776 		"/etc", "/var", NULL
1414f4368d3dSvp157776 	};
1415f4368d3dSvp157776 	static const char *loopdirs[] = {
1416f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1417f4368d3dSvp157776 		"/usr", NULL
1418f4368d3dSvp157776 	};
1419f4368d3dSvp157776 	static const char *tmpdirs[] = {
1420f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1421f4368d3dSvp157776 	};
1422f4368d3dSvp157776 	struct stat st;
1423f4368d3dSvp157776 
1424108322fbScarlsonj 	/*
1425108322fbScarlsonj 	 * These are mounted read-write from the zone undergoing upgrade.  We
1426108322fbScarlsonj 	 * must be careful not to 'leak' things from the main system into the
1427108322fbScarlsonj 	 * zone, and this accomplishes that goal.
1428108322fbScarlsonj 	 */
1429108322fbScarlsonj 	for (cpp = localdirs; *cpp != NULL; cpp++) {
1430108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1431108322fbScarlsonj 		(void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath,
1432108322fbScarlsonj 		    *cpp);
1433108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1434108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1435108322fbScarlsonj 			return (B_FALSE);
1436108322fbScarlsonj 		}
1437108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) {
1438108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1439108322fbScarlsonj 			    *cpp);
1440108322fbScarlsonj 			return (B_FALSE);
1441108322fbScarlsonj 		}
1442108322fbScarlsonj 	}
1443108322fbScarlsonj 
1444108322fbScarlsonj 	/*
1445108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1446108322fbScarlsonj 	 * they contain binaries that must match system.
1447108322fbScarlsonj 	 */
1448108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1449108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1450108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1451108322fbScarlsonj 			if (errno != EEXIST) {
1452108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1453108322fbScarlsonj 				return (B_FALSE);
1454108322fbScarlsonj 			}
1455108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1456108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1457108322fbScarlsonj 				return (B_FALSE);
1458108322fbScarlsonj 			}
1459108322fbScarlsonj 			/*
1460108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1461108322fbScarlsonj 			 * things that have been converted into symlinks
1462108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1463108322fbScarlsonj 			 * fixup.
1464108322fbScarlsonj 			 */
1465108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1466108322fbScarlsonj 				continue;
1467108322fbScarlsonj 		}
1468108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1469108322fbScarlsonj 		    tmp) != 0) {
1470108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1471108322fbScarlsonj 			    *cpp);
1472108322fbScarlsonj 			return (B_FALSE);
1473108322fbScarlsonj 		}
1474108322fbScarlsonj 	}
1475108322fbScarlsonj 
1476108322fbScarlsonj 	/*
1477108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1478108322fbScarlsonj 	 */
1479108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1480108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1481108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
1482108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1483108322fbScarlsonj 			return (B_FALSE);
1484108322fbScarlsonj 		}
1485d30e996aSgjelinek 
1486d30e996aSgjelinek 		/*
1487d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1488d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1489d30e996aSgjelinek 		 * the correct mode for /tmp now.
1490d30e996aSgjelinek 		 */
1491d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1492d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1493d30e996aSgjelinek 			return (B_FALSE);
1494d30e996aSgjelinek 		}
1495d30e996aSgjelinek 
1496108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1497108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1498108322fbScarlsonj 			return (B_FALSE);
1499108322fbScarlsonj 		}
1500108322fbScarlsonj 	}
1501108322fbScarlsonj 	return (B_TRUE);
1502108322fbScarlsonj }
1503108322fbScarlsonj 
15049acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15059acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
15069acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
15079acbbeafSnn35248 	int			*pgcd_num_fs;
15089acbbeafSnn35248 } plat_gmount_cb_data_t;
15099acbbeafSnn35248 
15109acbbeafSnn35248 /*
15119acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15129acbbeafSnn35248  * through all global brand platform mounts.
15139acbbeafSnn35248  */
15149acbbeafSnn35248 int
15159acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15169acbbeafSnn35248     const char *fstype, const char *opt)
15179acbbeafSnn35248 {
15189acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
15199acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15209acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15219acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
15229acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15239acbbeafSnn35248 
15249acbbeafSnn35248 	num_fs++;
15259acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15269acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15279acbbeafSnn35248 		return (-1);
15289acbbeafSnn35248 	}
15299acbbeafSnn35248 
15309acbbeafSnn35248 	fs_ptr = tmp_ptr;
15319acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
15329acbbeafSnn35248 
15339acbbeafSnn35248 	/* update the callback struct passed in */
15349acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
15359acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
15369acbbeafSnn35248 
15379acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
15389acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15399acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
15409acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15419acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15429acbbeafSnn35248 	fsp->zone_fs_options = NULL;
1543fa3d7ae1Sedp 	if ((opt != NULL) &&
1544fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15459acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
15469acbbeafSnn35248 		return (-1);
15479acbbeafSnn35248 	}
15489acbbeafSnn35248 
15499acbbeafSnn35248 	return (0);
15509acbbeafSnn35248 }
15519acbbeafSnn35248 
15529acbbeafSnn35248 static int
15539acbbeafSnn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
15549acbbeafSnn35248     struct zone_fstab **fs_tabp, int *num_fsp)
15559acbbeafSnn35248 {
15569acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
15579acbbeafSnn35248 	int num_fs;
15589acbbeafSnn35248 
15599acbbeafSnn35248 	num_fs = *num_fsp;
15609acbbeafSnn35248 	fs_ptr = *fs_tabp;
15619acbbeafSnn35248 
15629acbbeafSnn35248 	if (zonecfg_setipdent(handle) != Z_OK) {
15639acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
15649acbbeafSnn35248 		return (-1);
15659acbbeafSnn35248 	}
15669acbbeafSnn35248 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
15679acbbeafSnn35248 		num_fs++;
15689acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
15699acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
15709acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
15719acbbeafSnn35248 			(void) zonecfg_endipdent(handle);
15729acbbeafSnn35248 			return (-1);
15739acbbeafSnn35248 		}
15749acbbeafSnn35248 
15759acbbeafSnn35248 		/* update the pointers passed in */
15769acbbeafSnn35248 		*fs_tabp = tmp_ptr;
15779acbbeafSnn35248 		*num_fsp = num_fs;
15789acbbeafSnn35248 
15799acbbeafSnn35248 		/*
15809acbbeafSnn35248 		 * IPDs logically only have a mount point; all other properties
15819acbbeafSnn35248 		 * are implied.
15829acbbeafSnn35248 		 */
15839acbbeafSnn35248 		fs_ptr = tmp_ptr;
15849acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
15859acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
15869acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
15879acbbeafSnn35248 		fsp->zone_fs_special[0] = '\0';
15889acbbeafSnn35248 		fsp->zone_fs_raw[0] = '\0';
15899acbbeafSnn35248 		fsp->zone_fs_type[0] = '\0';
15909acbbeafSnn35248 		fsp->zone_fs_options = NULL;
15919acbbeafSnn35248 	}
15929acbbeafSnn35248 	(void) zonecfg_endipdent(handle);
15939acbbeafSnn35248 	return (0);
15949acbbeafSnn35248 }
15959acbbeafSnn35248 
15969acbbeafSnn35248 static int
15979acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
15989acbbeafSnn35248     struct zone_fstab **fs_tabp, int *num_fsp, int mount_cmd)
15999acbbeafSnn35248 {
16009acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16019acbbeafSnn35248 	int num_fs;
16029acbbeafSnn35248 
16039acbbeafSnn35248 	num_fs = *num_fsp;
16049acbbeafSnn35248 	fs_ptr = *fs_tabp;
16059acbbeafSnn35248 
16069acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16079acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16089acbbeafSnn35248 		return (-1);
16099acbbeafSnn35248 	}
16109acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16119acbbeafSnn35248 		/*
16129acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
16139acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
16149acbbeafSnn35248 		 * case.
16159acbbeafSnn35248 		 */
16169acbbeafSnn35248 		if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16179acbbeafSnn35248 			continue;
16189acbbeafSnn35248 
16199acbbeafSnn35248 		num_fs++;
16209acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16219acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16229acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16239acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
16249acbbeafSnn35248 			return (-1);
16259acbbeafSnn35248 		}
16269acbbeafSnn35248 		/* update the pointers passed in */
16279acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16289acbbeafSnn35248 		*num_fsp = num_fs;
16299acbbeafSnn35248 
16309acbbeafSnn35248 		fs_ptr = tmp_ptr;
16319acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16329acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16339acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16349acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16359acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
16369acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16379acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
16389acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
1639fa3d7ae1Sedp 
1640fa3d7ae1Sedp 		/*
1641fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1642fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1643fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1644fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1645fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1646fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1647fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1648fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1649fa3d7ae1Sedp 		 */
1650fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1651fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1652fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1653fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1654fa3d7ae1Sedp 		}
1655fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1656fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
16579acbbeafSnn35248 	}
16589acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
16599acbbeafSnn35248 	return (0);
16609acbbeafSnn35248 }
16619acbbeafSnn35248 
16627c478bd9Sstevel@tonic-gate static int
1663108322fbScarlsonj mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd)
16647c478bd9Sstevel@tonic-gate {
16657c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
16667c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
16679acbbeafSnn35248 	char brand[MAXNAMELEN];
166816bca938Svp157776 	char luroot[MAXPATHLEN];
16699acbbeafSnn35248 	int i, num_fs = 0;
16709acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
16717c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
16727c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1673123807fbSedp 	brand_handle_t bh;
16749acbbeafSnn35248 	plat_gmount_cb_data_t cb;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1677108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
16787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1679108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1680108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1681108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
16827c478bd9Sstevel@tonic-gate 		goto bad;
16837c478bd9Sstevel@tonic-gate 	}
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
16867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
16877c478bd9Sstevel@tonic-gate 		goto bad;
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
16917c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
16927c478bd9Sstevel@tonic-gate 		goto bad;
16937c478bd9Sstevel@tonic-gate 	}
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1696ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
16977c478bd9Sstevel@tonic-gate 		goto bad;
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17007c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17017c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17027c478bd9Sstevel@tonic-gate 		goto bad;
17037c478bd9Sstevel@tonic-gate 	}
17047c478bd9Sstevel@tonic-gate 
17059acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
17069acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
1707123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
17089acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1709e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17109acbbeafSnn35248 		return (-1);
17119acbbeafSnn35248 	}
17129acbbeafSnn35248 
17139acbbeafSnn35248 	/*
17149acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
17159acbbeafSnn35248 	 * configuration.
17169acbbeafSnn35248 	 */
17179acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
17189acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
17199acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1720123807fbSedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17219acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
17229acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1723123807fbSedp 		brand_close(bh);
1724e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17259acbbeafSnn35248 		return (-1);
17269acbbeafSnn35248 	}
1727123807fbSedp 	brand_close(bh);
17289acbbeafSnn35248 
17297c478bd9Sstevel@tonic-gate 	/*
17307c478bd9Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
17317c478bd9Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
17327c478bd9Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
17337c478bd9Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
17347c478bd9Sstevel@tonic-gate 	 */
17359acbbeafSnn35248 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
17367c478bd9Sstevel@tonic-gate 		goto bad;
17377c478bd9Sstevel@tonic-gate 
17389acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17399acbbeafSnn35248 	    mount_cmd) != 0)
17407c478bd9Sstevel@tonic-gate 		goto bad;
1741fa9e4066Sahrens 
17427c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17437c478bd9Sstevel@tonic-gate 	handle = NULL;
17447c478bd9Sstevel@tonic-gate 
1745108322fbScarlsonj 	/*
17469acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
17479acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
17489acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
17499acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
17509acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
17519acbbeafSnn35248 	 * the zones filesystems there instead.
17529acbbeafSnn35248 	 *
17539acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
17549acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
17559acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
17569acbbeafSnn35248 	 *
17579acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
17589acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
17599acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
17609acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
17619acbbeafSnn35248 	 * upgrade environment.)
17629acbbeafSnn35248 	 *
17639acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
17649acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
17659acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
17669acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
17679acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
17689acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
17699acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
17709acbbeafSnn35248 	 *
17719acbbeafSnn35248 	 * All this work is done in three phases:
1772f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1773f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1774f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1775f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1776108322fbScarlsonj 	 */
1777108322fbScarlsonj 	if (mount_cmd &&
1778f4368d3dSvp157776 	    !build_mounted_pre_var(zlogp,
177916bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1780108322fbScarlsonj 		goto bad;
1781108322fbScarlsonj 
17827c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
17839acbbeafSnn35248 
17847c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
17859acbbeafSnn35248 		if (mount_cmd &&
17869acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
17879acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
17889acbbeafSnn35248 
17899acbbeafSnn35248 			/*
17909acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
17919acbbeafSnn35248 			 * but /dev is special and always goes at the top
17929acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
17939acbbeafSnn35248 			 */
179484561e8cStd153743 			assert(zone_isnative || zone_iscluster);
17959acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
17969acbbeafSnn35248 			rootpath[slen] = '\0';
17979acbbeafSnn35248 			if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
17989acbbeafSnn35248 				goto bad;
17999acbbeafSnn35248 			rootpath[slen] = '/';
18009acbbeafSnn35248 			continue;
18019acbbeafSnn35248 		}
18027c478bd9Sstevel@tonic-gate 		if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
18037c478bd9Sstevel@tonic-gate 			goto bad;
18047c478bd9Sstevel@tonic-gate 	}
1805f4368d3dSvp157776 	if (mount_cmd &&
180616bca938Svp157776 	    !build_mounted_post_var(zlogp, rootpath, luroot))
1807f4368d3dSvp157776 		goto bad;
180845916cd2Sjpk 
180945916cd2Sjpk 	/*
181045916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
181145916cd2Sjpk 	 */
181248451833Scarlsonj 	if (!mount_cmd && tsol_mounts(zlogp, zone_name, rootpath) != 0)
181345916cd2Sjpk 		goto bad;
181445916cd2Sjpk 
18157c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	/*
18187c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18197c478bd9Sstevel@tonic-gate 	 */
18207c478bd9Sstevel@tonic-gate 	return (0);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate bad:
18237c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18247c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18257c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18267c478bd9Sstevel@tonic-gate 	return (-1);
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18307c478bd9Sstevel@tonic-gate static int
18317c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18327c478bd9Sstevel@tonic-gate {
18337c478bd9Sstevel@tonic-gate 	int prefixlen;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18367c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18377c478bd9Sstevel@tonic-gate 		return (1);
18387c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
18397c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
18407c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18417c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
18427c478bd9Sstevel@tonic-gate 			continue;
18437c478bd9Sstevel@tonic-gate 		}
18447c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18457c478bd9Sstevel@tonic-gate 		prefixlen--;
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate 	return (0);
18487c478bd9Sstevel@tonic-gate }
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate /*
18517c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
18527c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
18537c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
18547c478bd9Sstevel@tonic-gate  *
18557c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
18567c478bd9Sstevel@tonic-gate  */
18577c478bd9Sstevel@tonic-gate static int
1858f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
18597c478bd9Sstevel@tonic-gate {
18607c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
18617c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
18627c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
18637c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
18647c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
18657c478bd9Sstevel@tonic-gate 	uint_t bufsize;
18667c478bd9Sstevel@tonic-gate 	char *buf = NULL;
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
18697c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
18707c478bd9Sstevel@tonic-gate 		ret_code = -1;
18717c478bd9Sstevel@tonic-gate 		goto bad;
18727c478bd9Sstevel@tonic-gate 	}
18737c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
18747c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
18757c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
18767c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1877f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
18787c478bd9Sstevel@tonic-gate 		ret_code = -1;
18797c478bd9Sstevel@tonic-gate 		goto bad;
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
18827c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
18837c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
18847c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
18857c478bd9Sstevel@tonic-gate 		ret_code = -1;
18867c478bd9Sstevel@tonic-gate 		goto bad;
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
18897c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
18907c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
18917c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
18927c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1893f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1894f4b3ec61Sdh155122 		    "interfaces");
18957c478bd9Sstevel@tonic-gate 		ret_code = -1;
18967c478bd9Sstevel@tonic-gate 		goto bad;
18977c478bd9Sstevel@tonic-gate 	}
18987c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
18997c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19007c478bd9Sstevel@tonic-gate 		(void) close(s);
19017c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19027c478bd9Sstevel@tonic-gate 		    0) {
19037c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19047c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
19057c478bd9Sstevel@tonic-gate 			ret_code = -1;
19067c478bd9Sstevel@tonic-gate 			continue;
19077c478bd9Sstevel@tonic-gate 		}
19087c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19097c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19107c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19117c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1912c1c0ebd5Ssl108498 			if (errno == ENXIO)
1913c1c0ebd5Ssl108498 				/*
1914c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1915c1c0ebd5Ssl108498 				 * another zone halting.
1916c1c0ebd5Ssl108498 				 */
1917c1c0ebd5Ssl108498 				continue;
19187c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1919c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1920f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19217c478bd9Sstevel@tonic-gate 			ret_code = -1;
19227c478bd9Sstevel@tonic-gate 			continue;
19237c478bd9Sstevel@tonic-gate 		}
19247c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19257c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19267c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1927f4b3ec61Sdh155122 				    "%s: could not remove network interface",
19287c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
19297c478bd9Sstevel@tonic-gate 				ret_code = -1;
19307c478bd9Sstevel@tonic-gate 				continue;
19317c478bd9Sstevel@tonic-gate 			}
19327c478bd9Sstevel@tonic-gate 		}
19337c478bd9Sstevel@tonic-gate 	}
19347c478bd9Sstevel@tonic-gate bad:
19357c478bd9Sstevel@tonic-gate 	if (s > 0)
19367c478bd9Sstevel@tonic-gate 		(void) close(s);
19377c478bd9Sstevel@tonic-gate 	if (buf)
19387c478bd9Sstevel@tonic-gate 		free(buf);
19397c478bd9Sstevel@tonic-gate 	return (ret_code);
19407c478bd9Sstevel@tonic-gate }
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate static union	sockunion {
19437c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
19447c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
19457c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19467c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19477c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate static struct {
19507c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
19517c478bd9Sstevel@tonic-gate 	char	space[512];
19527c478bd9Sstevel@tonic-gate } rtmsg;
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate static int
19557c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
19567c478bd9Sstevel@tonic-gate {
19577c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
19587c478bd9Sstevel@tonic-gate 	case AF_INET:
19597c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
19607c478bd9Sstevel@tonic-gate 	case AF_LINK:
19617c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
19627c478bd9Sstevel@tonic-gate 	case AF_INET6:
19637c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
19647c478bd9Sstevel@tonic-gate 	default:
19657c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate }
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
19707c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate /*
19737c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
19747c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
19757c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
19767c478bd9Sstevel@tonic-gate  *
19777c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
19787c478bd9Sstevel@tonic-gate  *
19797c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
19807c478bd9Sstevel@tonic-gate  *
19817c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
19827c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
19837c478bd9Sstevel@tonic-gate  */
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate static char *
19867c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
19877c478bd9Sstevel@tonic-gate {
19887c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
19897c478bd9Sstevel@tonic-gate 	pid_t pid;
19907c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
19917c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
19927c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
19937c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
19947c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	pid = getpid();
19997c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20017c478bd9Sstevel@tonic-gate 		return (NULL);
20027c478bd9Sstevel@tonic-gate 	}
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20057c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20087c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20097c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20107c478bd9Sstevel@tonic-gate 	} else {
20117c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20147c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20157c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20217c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20227c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20237c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20247c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20257c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20287c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20297c478bd9Sstevel@tonic-gate 	cp += l;
20307c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20317c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20327c478bd9Sstevel@tonic-gate 	cp += l;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20387c478bd9Sstevel@tonic-gate 		return (NULL);
20397c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20407c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20417c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20427c478bd9Sstevel@tonic-gate 		return (NULL);
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 	do {
20457c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20467c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20477c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20487c478bd9Sstevel@tonic-gate 	if (l < 0) {
20497c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
20507c478bd9Sstevel@tonic-gate 		return (NULL);
20517c478bd9Sstevel@tonic-gate 	}
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
20547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
20557c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
20567c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
20577c478bd9Sstevel@tonic-gate 		return (NULL);
20587c478bd9Sstevel@tonic-gate 	}
20597c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
20607c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
20617c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
20627c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
20637c478bd9Sstevel@tonic-gate 		return (NULL);
20647c478bd9Sstevel@tonic-gate 	}
20657c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
20667c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
20677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
20687c478bd9Sstevel@tonic-gate 		return (NULL);
20697c478bd9Sstevel@tonic-gate 	}
20707c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2071f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
20727c478bd9Sstevel@tonic-gate 		return (NULL);
20737c478bd9Sstevel@tonic-gate 	}
20747c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
20757c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
20767c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
20777c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
20787c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
20797c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
20807c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
20817c478bd9Sstevel@tonic-gate 			continue;
20827c478bd9Sstevel@tonic-gate 		}
20837c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
20847c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
20857c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
20867c478bd9Sstevel@tonic-gate 		break;
20877c478bd9Sstevel@tonic-gate 	}
20887c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2089f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2090f4b3ec61Sdh155122 		    "determined");
20917c478bd9Sstevel@tonic-gate 		return (NULL);
20927c478bd9Sstevel@tonic-gate 	}
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	/*
20957c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
20967c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
20977c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
20987c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
20997c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21007c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21017c478bd9Sstevel@tonic-gate 	 */
21027c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21037c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21047c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21057c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21067c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21077c478bd9Sstevel@tonic-gate 	if (i < 0) {
21087c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2109f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2110f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
21117c478bd9Sstevel@tonic-gate 		return (NULL);
21127c478bd9Sstevel@tonic-gate 	}
21137c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21147c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21157c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21187c478bd9Sstevel@tonic-gate 		return (answer);
21197c478bd9Sstevel@tonic-gate 	return (NULL);
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate typedef struct mcast_rtmsg_s {
21237c478bd9Sstevel@tonic-gate 	struct rt_msghdr	m_rtm;
21247c478bd9Sstevel@tonic-gate 	union {
21257c478bd9Sstevel@tonic-gate 		struct {
21267c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_dst;
21277c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_gw;
21287c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_netmask;
21297c478bd9Sstevel@tonic-gate 		} m_v4;
21307c478bd9Sstevel@tonic-gate 		struct {
21317c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_dst;
21327c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_gw;
21337c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_netmask;
21347c478bd9Sstevel@tonic-gate 		} m_v6;
21357c478bd9Sstevel@tonic-gate 	} m_u;
21367c478bd9Sstevel@tonic-gate } mcast_rtmsg_t;
21377c478bd9Sstevel@tonic-gate #define	m_dst4		m_u.m_v4.m_dst
21387c478bd9Sstevel@tonic-gate #define	m_dst6		m_u.m_v6.m_dst
21397c478bd9Sstevel@tonic-gate #define	m_gw4		m_u.m_v4.m_gw
21407c478bd9Sstevel@tonic-gate #define	m_gw6		m_u.m_v6.m_gw
21417c478bd9Sstevel@tonic-gate #define	m_netmask4	m_u.m_v4.m_netmask
21427c478bd9Sstevel@tonic-gate #define	m_netmask6	m_u.m_v6.m_netmask
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate /*
21457c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21467c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21477c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21487c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21497c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21507c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21517c478bd9Sstevel@tonic-gate  *
21527c478bd9Sstevel@tonic-gate  * A default interface route for multicast is created on the first IPv4 and
21537c478bd9Sstevel@tonic-gate  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
21547c478bd9Sstevel@tonic-gate  * This should really be done in the init scripts if we ever allow zones to
21557c478bd9Sstevel@tonic-gate  * modify the routing tables.
21567c478bd9Sstevel@tonic-gate  *
21577c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21587c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
21597c478bd9Sstevel@tonic-gate  */
21607c478bd9Sstevel@tonic-gate static int
21617c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
21627c478bd9Sstevel@tonic-gate     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
21637c478bd9Sstevel@tonic-gate     boolean_t *mcast_rt_v6_setp)
21647c478bd9Sstevel@tonic-gate {
21657c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
21667c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
21677c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
21687c478bd9Sstevel@tonic-gate 	struct in_addr in4;
21697c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
21707c478bd9Sstevel@tonic-gate 	sa_family_t af;
21717c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
21727c478bd9Sstevel@tonic-gate 	mcast_rtmsg_t mcast_rtmsg;
21737c478bd9Sstevel@tonic-gate 	int s;
21747c478bd9Sstevel@tonic-gate 	int rs;
21757c478bd9Sstevel@tonic-gate 	int rlen;
21767c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
21777c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
21787c478bd9Sstevel@tonic-gate 	int res;
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
21817c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
21827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
21837c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
21847c478bd9Sstevel@tonic-gate 		return (-1);
21857c478bd9Sstevel@tonic-gate 	}
21867c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
21877c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21887c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21897c478bd9Sstevel@tonic-gate 	else
21907c478bd9Sstevel@tonic-gate 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
21947c478bd9Sstevel@tonic-gate 		return (-1);
21957c478bd9Sstevel@tonic-gate 	}
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
21987c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
21997c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
220022321485Svp157776 		/*
220122321485Svp157776 		 * Here, we know that the interface can't be brought up.
220222321485Svp157776 		 * A similar warning message was already printed out to
220322321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
220422321485Svp157776 		 * message to syslog and continue.
220522321485Svp157776 		 */
2206f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
220722321485Svp157776 		    "'%s' which may not be present/plumbed in the "
220822321485Svp157776 		    "global zone.", lifr.lifr_name);
22097c478bd9Sstevel@tonic-gate 		(void) close(s);
221022321485Svp157776 		return (Z_OK);
22117c478bd9Sstevel@tonic-gate 	}
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
22157c478bd9Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
22167c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22177c478bd9Sstevel@tonic-gate 		goto bad;
22187c478bd9Sstevel@tonic-gate 	}
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22217c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22227c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22257c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2226f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2227f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
22287c478bd9Sstevel@tonic-gate 		goto bad;
22297c478bd9Sstevel@tonic-gate 	}
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
22327c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
22337c478bd9Sstevel@tonic-gate 	} else {
22347c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
22357c478bd9Sstevel@tonic-gate 			/*
22367c478bd9Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
22377c478bd9Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
22387c478bd9Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
22397c478bd9Sstevel@tonic-gate 			 * being able to determine it is a common failure,
22407c478bd9Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
22417c478bd9Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
22427c478bd9Sstevel@tonic-gate 			 * printed after the rest of the interface's
22437c478bd9Sstevel@tonic-gate 			 * parameters have been configured.
22447c478bd9Sstevel@tonic-gate 			 */
22457c478bd9Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
22467c478bd9Sstevel@tonic-gate 			if (slashp != NULL) {
22477c478bd9Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22487c478bd9Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
22497c478bd9Sstevel@tonic-gate 					*slashp = '/';
22507c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
22517c478bd9Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
22527c478bd9Sstevel@tonic-gate 					    lifr.lifr_name,
22537c478bd9Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
22547c478bd9Sstevel@tonic-gate 					goto bad;
22557c478bd9Sstevel@tonic-gate 				}
22567c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
22577c478bd9Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
22587c478bd9Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
22597c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
22607c478bd9Sstevel@tonic-gate 			}
22617c478bd9Sstevel@tonic-gate 			if (got_netmask) {
22627c478bd9Sstevel@tonic-gate 				netmask4.sin_family = af;
22637c478bd9Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
22647c478bd9Sstevel@tonic-gate 				    sizeof (netmask4));
22657c478bd9Sstevel@tonic-gate 			}
22667c478bd9Sstevel@tonic-gate 		} else {
22677c478bd9Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
22687c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
22697c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
22707c478bd9Sstevel@tonic-gate 				*slashp = '/';
22717c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22727c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22737c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
22747c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22757c478bd9Sstevel@tonic-gate 				goto bad;
22767c478bd9Sstevel@tonic-gate 			}
22777c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
22787c478bd9Sstevel@tonic-gate 			netmask6.sin6_family = af;
22797c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
22807c478bd9Sstevel@tonic-gate 			    sizeof (netmask6));
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 		if (got_netmask &&
22837c478bd9Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
22847c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
22857c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
22867c478bd9Sstevel@tonic-gate 			goto bad;
22877c478bd9Sstevel@tonic-gate 		}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		/*
22907c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
22917c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
22927c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
22937c478bd9Sstevel@tonic-gate 		 */
22947c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
22957c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
22967c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
22977c478bd9Sstevel@tonic-gate 			goto bad;
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
23007c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23017c478bd9Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
23027c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23037c478bd9Sstevel@tonic-gate 			goto bad;
23047c478bd9Sstevel@tonic-gate 		}
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23087c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
23097c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
23107c478bd9Sstevel@tonic-gate 		goto bad;
23117c478bd9Sstevel@tonic-gate 	}
23127c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
23137c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
23147c478bd9Sstevel@tonic-gate 		int save_errno = errno;
23157c478bd9Sstevel@tonic-gate 		char *zone_using;
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 		/*
23187c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
23197c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
23207c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23217c478bd9Sstevel@tonic-gate 		 * using that address.
23227c478bd9Sstevel@tonic-gate 		 */
23237c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23247c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2325f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2326f4b3ec61Sdh155122 			    lifr.lifr_name);
23277c478bd9Sstevel@tonic-gate 			goto bad;
23287c478bd9Sstevel@tonic-gate 		}
23297c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23307c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23317c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23327c478bd9Sstevel@tonic-gate 			goto bad;
23337c478bd9Sstevel@tonic-gate 		}
23347c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23357c478bd9Sstevel@tonic-gate 		errno = save_errno;
23367c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
23377c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2338f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2339f4b3ec61Sdh155122 			    lifr.lifr_name);
23407c478bd9Sstevel@tonic-gate 		else
2341f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2342f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2343f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
23447c478bd9Sstevel@tonic-gate 		goto bad;
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
23477c478bd9Sstevel@tonic-gate 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
23487c478bd9Sstevel@tonic-gate 	    (af == AF_INET6 &&
23497c478bd9Sstevel@tonic-gate 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
23507c478bd9Sstevel@tonic-gate 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
23517c478bd9Sstevel@tonic-gate 		if (rs < 0) {
23527c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not create "
23537c478bd9Sstevel@tonic-gate 			    "routing socket", lifr.lifr_name);
23547c478bd9Sstevel@tonic-gate 			goto bad;
23557c478bd9Sstevel@tonic-gate 		}
23567c478bd9Sstevel@tonic-gate 		(void) shutdown(rs, 0);
23577c478bd9Sstevel@tonic-gate 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
23587c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
23597c478bd9Sstevel@tonic-gate 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
23607c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
23617c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
23627c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
23637c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
23647c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_addrs =
23657c478bd9Sstevel@tonic-gate 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
23667c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
23677c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
23687c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
23697c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
23707c478bd9Sstevel@tonic-gate 			    htonl(INADDR_UNSPEC_GROUP);
23717c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
23727c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_addr = in4;
23737c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
23747c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
23757c478bd9Sstevel@tonic-gate 			    htonl(IN_CLASSD_NET);
23767c478bd9Sstevel@tonic-gate 		} else {
23777c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
23787c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
23797c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
23807c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_addr = in6;
23817c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
23827c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
23837c478bd9Sstevel@tonic-gate 		}
23847c478bd9Sstevel@tonic-gate 		rlen = write(rs, (char *)&mcast_rtmsg,
23857c478bd9Sstevel@tonic-gate 		    mcast_rtmsg.m_rtm.rtm_msglen);
238622321485Svp157776 		/*
238722321485Svp157776 		 * The write to the multicast socket will fail if the
238822321485Svp157776 		 * interface belongs to a failed IPMP group. This is a
238922321485Svp157776 		 * non-fatal error and the zone will continue booting.
239022321485Svp157776 		 * While the zone is running, if any interface in the
239122321485Svp157776 		 * failed IPMP group recovers, the zone will fallback to
239222321485Svp157776 		 * using that interface.
239322321485Svp157776 		 */
23947c478bd9Sstevel@tonic-gate 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
23957c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
2396f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "WARNING: network "
2397f4b3ec61Sdh155122 				    "interface '%s' not available as default "
2398f4b3ec61Sdh155122 				    "for multicast.", lifr.lifr_name);
23997c478bd9Sstevel@tonic-gate 			} else {
2400f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "WARNING: network "
2401f4b3ec61Sdh155122 				    "interface '%s' not available as default "
2402f4b3ec61Sdh155122 				    "for multicast; routing socket returned "
240322321485Svp157776 				    "unexpected %d bytes.",
240422321485Svp157776 				    lifr.lifr_name, rlen);
24057c478bd9Sstevel@tonic-gate 			}
240622321485Svp157776 		} else {
240722321485Svp157776 
24087c478bd9Sstevel@tonic-gate 			if (af == AF_INET) {
24097c478bd9Sstevel@tonic-gate 				*mcast_rt_v4_setp = B_TRUE;
24107c478bd9Sstevel@tonic-gate 			} else {
24117c478bd9Sstevel@tonic-gate 				*mcast_rt_v6_setp = B_TRUE;
24127c478bd9Sstevel@tonic-gate 			}
241322321485Svp157776 		}
24147c478bd9Sstevel@tonic-gate 		(void) close(rs);
24157c478bd9Sstevel@tonic-gate 	}
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	if (!got_netmask) {
24187c478bd9Sstevel@tonic-gate 		/*
24197c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
24207c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
24217c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
24227c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
24237c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
24247c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
24257c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
24267c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
24277c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
24287c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
24297c478bd9Sstevel@tonic-gate 		 */
24307c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
24317c478bd9Sstevel@tonic-gate 		void  *addr;
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
24347c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
24357c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
24367c478bd9Sstevel@tonic-gate 		else
24377c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
24387c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
24417c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
24427c478bd9Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
24437c478bd9Sstevel@tonic-gate 			goto bad;
24447c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
24457c478bd9Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
24467c478bd9Sstevel@tonic-gate 		    "%s; using default of %s.",
24477c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
24487c478bd9Sstevel@tonic-gate 	}
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 	(void) close(s);
24517c478bd9Sstevel@tonic-gate 	return (Z_OK);
24527c478bd9Sstevel@tonic-gate bad:
24537c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
24547c478bd9Sstevel@tonic-gate 	(void) close(s);
24557c478bd9Sstevel@tonic-gate 	return (-1);
24567c478bd9Sstevel@tonic-gate }
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate /*
24597c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
24607c478bd9Sstevel@tonic-gate  * An IPv4 loopback interface is set up "for free", modeling the global system.
24617c478bd9Sstevel@tonic-gate  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
24627c478bd9Sstevel@tonic-gate  * address is set up as well.
24637c478bd9Sstevel@tonic-gate  *
24647c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24657c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
24667c478bd9Sstevel@tonic-gate  */
24677c478bd9Sstevel@tonic-gate static int
2468f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24697c478bd9Sstevel@tonic-gate {
24707c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24717c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24727c478bd9Sstevel@tonic-gate 	boolean_t saw_v6 = B_FALSE;
24737c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v4_set = B_FALSE;
24747c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v6_set = B_FALSE;
24757c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24797c478bd9Sstevel@tonic-gate 		return (-1);
24807c478bd9Sstevel@tonic-gate 	}
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24837c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24847c478bd9Sstevel@tonic-gate 		return (-1);
24857c478bd9Sstevel@tonic-gate 	}
24867c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24887c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24897c478bd9Sstevel@tonic-gate 		return (-1);
24907c478bd9Sstevel@tonic-gate 	}
24917c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24927c478bd9Sstevel@tonic-gate 		for (;;) {
24937c478bd9Sstevel@tonic-gate 			struct in6_addr in6;
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24967c478bd9Sstevel@tonic-gate 				break;
24977c478bd9Sstevel@tonic-gate 			if (configure_one_interface(zlogp, zoneid,
24987c478bd9Sstevel@tonic-gate 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
24997c478bd9Sstevel@tonic-gate 			    Z_OK) {
25007c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
25017c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
25027c478bd9Sstevel@tonic-gate 				return (-1);
25037c478bd9Sstevel@tonic-gate 			}
25047c478bd9Sstevel@tonic-gate 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
25057c478bd9Sstevel@tonic-gate 			    &in6) == 1)
25067c478bd9Sstevel@tonic-gate 				saw_v6 = B_TRUE;
25077c478bd9Sstevel@tonic-gate 		}
25087c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
25097c478bd9Sstevel@tonic-gate 	}
25107c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
25117c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
25127c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
25137c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
25147c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
25157c478bd9Sstevel@tonic-gate 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
25167c478bd9Sstevel@tonic-gate 	    != Z_OK) {
25177c478bd9Sstevel@tonic-gate 		return (-1);
25187c478bd9Sstevel@tonic-gate 	}
25197c478bd9Sstevel@tonic-gate 	if (saw_v6) {
25207c478bd9Sstevel@tonic-gate 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
25217c478bd9Sstevel@tonic-gate 		    sizeof (loopback_iftab.zone_nwif_address));
25227c478bd9Sstevel@tonic-gate 		if (configure_one_interface(zlogp, zoneid,
25237c478bd9Sstevel@tonic-gate 		    &loopback_iftab, NULL, NULL) != Z_OK) {
25247c478bd9Sstevel@tonic-gate 			return (-1);
25257c478bd9Sstevel@tonic-gate 		}
25267c478bd9Sstevel@tonic-gate 	}
25277c478bd9Sstevel@tonic-gate 	return (0);
25287c478bd9Sstevel@tonic-gate }
25297c478bd9Sstevel@tonic-gate 
2530f4b3ec61Sdh155122 static void
2531f4b3ec61Sdh155122 show_owner(zlog_t *zlogp, char *dlname)
2532f4b3ec61Sdh155122 {
2533f4b3ec61Sdh155122 	zoneid_t dl_owner_zid;
2534f4b3ec61Sdh155122 	char dl_owner_zname[ZONENAME_MAX];
2535f4b3ec61Sdh155122 
2536f4b3ec61Sdh155122 	dl_owner_zid = ALL_ZONES;
2537f4b3ec61Sdh155122 	if (zone_check_datalink(&dl_owner_zid, dlname) != 0)
2538f4b3ec61Sdh155122 		(void) snprintf(dl_owner_zname, ZONENAME_MAX, "<unknown>");
2539f4b3ec61Sdh155122 	else if (getzonenamebyid(dl_owner_zid, dl_owner_zname, ZONENAME_MAX)
2540f4b3ec61Sdh155122 	    < 0)
2541f4b3ec61Sdh155122 		(void) snprintf(dl_owner_zname, ZONENAME_MAX, "<%d>",
2542f4b3ec61Sdh155122 		    dl_owner_zid);
2543f4b3ec61Sdh155122 
2544f4b3ec61Sdh155122 	errno = EPERM;
2545f4b3ec61Sdh155122 	zerror(zlogp, B_TRUE, "WARNING: skipping network interface '%s' "
2546f4b3ec61Sdh155122 	    "which is used by the non-global zone '%s'.\n",
2547f4b3ec61Sdh155122 	    dlname, dl_owner_zname);
2548f4b3ec61Sdh155122 }
2549f4b3ec61Sdh155122 
2550f4b3ec61Sdh155122 static int
2551f4b3ec61Sdh155122 add_datalink(zlog_t *zlogp, zoneid_t zoneid, char *dlname)
2552f4b3ec61Sdh155122 {
2553f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2554f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2555f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
2556f4b3ec61Sdh155122 		errno = EPERM;
2557f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "WARNING: skipping network interface "
2558f4b3ec61Sdh155122 		    "'%s' which is used in the global zone.", dlname);
2559f4b3ec61Sdh155122 		return (-1);
2560f4b3ec61Sdh155122 	}
2561f4b3ec61Sdh155122 
2562f4b3ec61Sdh155122 	/* Add access control information */
2563f4b3ec61Sdh155122 	if (zone_add_datalink(zoneid, dlname) != 0) {
2564f4b3ec61Sdh155122 		/* If someone got this link before us, show its name */
2565f4b3ec61Sdh155122 		if (errno == EPERM)
2566f4b3ec61Sdh155122 			show_owner(zlogp, dlname);
2567f4b3ec61Sdh155122 		else
2568f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "WARNING: unable to add network "
2569f4b3ec61Sdh155122 			    "interface '%s'.", dlname);
2570f4b3ec61Sdh155122 		return (-1);
2571f4b3ec61Sdh155122 	}
2572f4b3ec61Sdh155122 
2573f4b3ec61Sdh155122 	/* Hold the link for this zone */
2574f4b3ec61Sdh155122 	if (dladm_hold_link(dlname, zoneid, B_FALSE) < 0) {
25752ec67e04Sgjelinek 		int res, old_errno;
25762ec67e04Sgjelinek 		dladm_attr_t da;
25772ec67e04Sgjelinek 
25782ec67e04Sgjelinek 		/*
25792ec67e04Sgjelinek 		 * The following check is similar to 'dladm show-link'
25802ec67e04Sgjelinek 		 * to determine if this is a legacy interface.
25812ec67e04Sgjelinek 		 */
25822ec67e04Sgjelinek 		old_errno = errno;
25832ec67e04Sgjelinek 		res = dladm_info(dlname, &da);
25842ec67e04Sgjelinek 		if (res < 0 && errno == ENODEV) {
2585*9d48116cSdh155122 			/*
2586*9d48116cSdh155122 			 * Check if this is a link like 'ce*' which supports
2587*9d48116cSdh155122 			 * a direct ioctl.
2588*9d48116cSdh155122 			 */
2589*9d48116cSdh155122 			res = driver_hold_link(dlname, zoneid);
2590*9d48116cSdh155122 			if (res == 0)
2591*9d48116cSdh155122 				return (0);
2592*9d48116cSdh155122 
25932ec67e04Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: legacy network "
25942ec67e04Sgjelinek 			    "interface '%s'\nunsupported with an "
25952ec67e04Sgjelinek 			    "ip-type=exclusive configuration.", dlname);
25962ec67e04Sgjelinek 		} else {
25972ec67e04Sgjelinek 			errno = old_errno;
2598f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "WARNING: unable to hold network "
2599f4b3ec61Sdh155122 			    "interface '%s'.", dlname);
26002ec67e04Sgjelinek 		}
26012ec67e04Sgjelinek 
2602f4b3ec61Sdh155122 		(void) zone_remove_datalink(zoneid, dlname);
2603f4b3ec61Sdh155122 		return (-1);
2604f4b3ec61Sdh155122 	}
2605f4b3ec61Sdh155122 
2606f4b3ec61Sdh155122 	return (0);
2607f4b3ec61Sdh155122 }
2608f4b3ec61Sdh155122 
2609f4b3ec61Sdh155122 static int
2610f4b3ec61Sdh155122 remove_datalink(zlog_t *zlogp, zoneid_t zoneid, char *dlname)
2611f4b3ec61Sdh155122 {
2612f4b3ec61Sdh155122 	/*
2613f4b3ec61Sdh155122 	 * Remove access control information.
2614f4b3ec61Sdh155122 	 * If the errno is ENXIO, the interface is not added yet,
2615f4b3ec61Sdh155122 	 * nothing to report then.
2616f4b3ec61Sdh155122 	 */
2617f4b3ec61Sdh155122 	if (zone_remove_datalink(zoneid, dlname) != 0) {
2618f4b3ec61Sdh155122 		if (errno == ENXIO)
2619f4b3ec61Sdh155122 			return (0);
2620f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to remove network interface '%s'",
2621f4b3ec61Sdh155122 		    dlname);
2622f4b3ec61Sdh155122 		return (-1);
2623f4b3ec61Sdh155122 	}
2624f4b3ec61Sdh155122 
2625f4b3ec61Sdh155122 	if (dladm_rele_link(dlname, 0, B_FALSE) < 0) {
2626*9d48116cSdh155122 		/* Fallback to 'ce*' type link */
2627*9d48116cSdh155122 		if (driver_rele_link(dlname, 0) < 0) {
2628f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to release network "
2629f4b3ec61Sdh155122 			    "interface '%s'", dlname);
2630f4b3ec61Sdh155122 			return (-1);
2631f4b3ec61Sdh155122 		}
2632*9d48116cSdh155122 	}
2633f4b3ec61Sdh155122 	return (0);
2634f4b3ec61Sdh155122 }
2635f4b3ec61Sdh155122 
2636f4b3ec61Sdh155122 /*
2637f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2638f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2639f4b3ec61Sdh155122  * whatever we set up, and return an error.
2640f4b3ec61Sdh155122  */
2641f4b3ec61Sdh155122 static int
2642f4b3ec61Sdh155122 configure_exclusive_network_interfaces(zlog_t *zlogp)
2643f4b3ec61Sdh155122 {
2644f4b3ec61Sdh155122 	zone_dochandle_t handle;
2645f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2646f4b3ec61Sdh155122 	zoneid_t zoneid;
2647f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2648f4b3ec61Sdh155122 	char path[MAXPATHLEN];
2649f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2650f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2651f4b3ec61Sdh155122 
2652f4b3ec61Sdh155122 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
2653f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to get zoneid");
2654f4b3ec61Sdh155122 		return (-1);
2655f4b3ec61Sdh155122 	}
2656f4b3ec61Sdh155122 
2657f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2658f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2659f4b3ec61Sdh155122 		return (-1);
2660f4b3ec61Sdh155122 	}
2661f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2662f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2663f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2664f4b3ec61Sdh155122 		return (-1);
2665f4b3ec61Sdh155122 	}
2666f4b3ec61Sdh155122 
2667f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2668f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2669f4b3ec61Sdh155122 		return (0);
2670f4b3ec61Sdh155122 	}
2671f4b3ec61Sdh155122 
2672f4b3ec61Sdh155122 	for (;;) {
2673f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2674f4b3ec61Sdh155122 			break;
2675f4b3ec61Sdh155122 
2676f4b3ec61Sdh155122 		if (prof == NULL) {
2677f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2678f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2679f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2680f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2681f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2682f4b3ec61Sdh155122 				    "unable to determine dev root");
2683f4b3ec61Sdh155122 				return (-1);
2684f4b3ec61Sdh155122 			}
2685f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2686f4b3ec61Sdh155122 			    "/dev");
2687f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2688f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2689f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2690f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2691f4b3ec61Sdh155122 				    "failed to initialize profile");
2692f4b3ec61Sdh155122 				return (-1);
2693f4b3ec61Sdh155122 			}
2694f4b3ec61Sdh155122 		}
2695f4b3ec61Sdh155122 
2696f4b3ec61Sdh155122 		/*
2697f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2698f4b3ec61Sdh155122 		 * Note here the zone still boots when the interfaces
2699f4b3ec61Sdh155122 		 * assigned is inaccessible, used by others, etc.
2700f4b3ec61Sdh155122 		 */
2701f4b3ec61Sdh155122 		if (add_datalink(zlogp, zoneid, nwiftab.zone_nwif_physical)
2702f4b3ec61Sdh155122 		    == 0) {
2703f4b3ec61Sdh155122 			if (di_prof_add_dev(prof, nwiftab.zone_nwif_physical)
2704f4b3ec61Sdh155122 			    != 0) {
2705f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2706f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2707f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2708f4b3ec61Sdh155122 				    "failed to add network device");
2709f4b3ec61Sdh155122 				return (-1);
2710f4b3ec61Sdh155122 			}
2711f4b3ec61Sdh155122 			added = B_TRUE;
2712f4b3ec61Sdh155122 		}
2713f4b3ec61Sdh155122 	}
2714f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2715f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2716f4b3ec61Sdh155122 
2717f4b3ec61Sdh155122 	if (prof != NULL && added) {
2718f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2719f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
2720f4b3ec61Sdh155122 			return (-1);
2721f4b3ec61Sdh155122 		}
2722f4b3ec61Sdh155122 	}
2723f4b3ec61Sdh155122 	if (prof != NULL)
2724f4b3ec61Sdh155122 		di_prof_fini(prof);
2725f4b3ec61Sdh155122 
2726f4b3ec61Sdh155122 	return (0);
2727f4b3ec61Sdh155122 }
2728f4b3ec61Sdh155122 
2729f4b3ec61Sdh155122 /*
2730f4b3ec61Sdh155122  * Get the list of the data-links from kernel, and try to remove it
2731f4b3ec61Sdh155122  */
2732f4b3ec61Sdh155122 static int
2733f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces_run(zlog_t *zlogp, zoneid_t zoneid)
2734f4b3ec61Sdh155122 {
2735f4b3ec61Sdh155122 	char *dlnames, *ptr;
2736f4b3ec61Sdh155122 	int dlnum, dlnum_saved, i;
2737f4b3ec61Sdh155122 
2738f4b3ec61Sdh155122 	dlnum = 0;
2739f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2740f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2741f4b3ec61Sdh155122 		return (-1);
2742f4b3ec61Sdh155122 	}
2743f4b3ec61Sdh155122 again:
2744f4b3ec61Sdh155122 	/* this zone doesn't have any data-links */
2745f4b3ec61Sdh155122 	if (dlnum == 0)
2746f4b3ec61Sdh155122 		return (0);
2747f4b3ec61Sdh155122 
2748f4b3ec61Sdh155122 	dlnames = malloc(dlnum * LIFNAMSIZ);
2749f4b3ec61Sdh155122 	if (dlnames == NULL) {
2750f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "memory allocation failed");
2751f4b3ec61Sdh155122 		return (-1);
2752f4b3ec61Sdh155122 	}
2753f4b3ec61Sdh155122 	dlnum_saved = dlnum;
2754f4b3ec61Sdh155122 
2755f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, dlnames) != 0) {
2756f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2757f4b3ec61Sdh155122 		free(dlnames);
2758f4b3ec61Sdh155122 		return (-1);
2759f4b3ec61Sdh155122 	}
2760f4b3ec61Sdh155122 	if (dlnum_saved < dlnum) {
2761f4b3ec61Sdh155122 		/* list increased, try again */
2762f4b3ec61Sdh155122 		free(dlnames);
2763f4b3ec61Sdh155122 		goto again;
2764f4b3ec61Sdh155122 	}
2765f4b3ec61Sdh155122 	ptr = dlnames;
2766f4b3ec61Sdh155122 	for (i = 0; i < dlnum; i++) {
2767f4b3ec61Sdh155122 		/* Remove access control information */
2768f4b3ec61Sdh155122 		if (remove_datalink(zlogp, zoneid, ptr) != 0) {
2769f4b3ec61Sdh155122 			free(dlnames);
2770f4b3ec61Sdh155122 			return (-1);
2771f4b3ec61Sdh155122 		}
2772f4b3ec61Sdh155122 		ptr += LIFNAMSIZ;
2773f4b3ec61Sdh155122 	}
2774f4b3ec61Sdh155122 	free(dlnames);
2775f4b3ec61Sdh155122 	return (0);
2776f4b3ec61Sdh155122 }
2777f4b3ec61Sdh155122 
2778f4b3ec61Sdh155122 /*
2779f4b3ec61Sdh155122  * Get the list of the data-links from configuration, and try to remove it
2780f4b3ec61Sdh155122  */
2781f4b3ec61Sdh155122 static int
2782f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces_static(zlog_t *zlogp, zoneid_t zoneid)
2783f4b3ec61Sdh155122 {
2784f4b3ec61Sdh155122 	zone_dochandle_t handle;
2785f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2786f4b3ec61Sdh155122 
2787f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2788f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2789f4b3ec61Sdh155122 		return (-1);
2790f4b3ec61Sdh155122 	}
2791f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2792f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2793f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2794f4b3ec61Sdh155122 		return (-1);
2795f4b3ec61Sdh155122 	}
2796f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2797f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2798f4b3ec61Sdh155122 		return (0);
2799f4b3ec61Sdh155122 	}
2800f4b3ec61Sdh155122 	for (;;) {
2801f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2802f4b3ec61Sdh155122 			break;
2803f4b3ec61Sdh155122 		/* Remove access control information */
2804f4b3ec61Sdh155122 		if (remove_datalink(zlogp, zoneid, nwiftab.zone_nwif_physical)
2805f4b3ec61Sdh155122 		    != 0) {
2806f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2807f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
2808f4b3ec61Sdh155122 			return (-1);
2809f4b3ec61Sdh155122 		}
2810f4b3ec61Sdh155122 	}
2811f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2812f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2813f4b3ec61Sdh155122 	return (0);
2814f4b3ec61Sdh155122 }
2815f4b3ec61Sdh155122 
2816f4b3ec61Sdh155122 /*
2817f4b3ec61Sdh155122  * Remove the access control information from the kernel for the exclusive
2818f4b3ec61Sdh155122  * network interfaces.
2819f4b3ec61Sdh155122  */
2820f4b3ec61Sdh155122 static int
2821f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2822f4b3ec61Sdh155122 {
2823f4b3ec61Sdh155122 	if (unconfigure_exclusive_network_interfaces_run(zlogp, zoneid) != 0) {
2824f4b3ec61Sdh155122 		return (unconfigure_exclusive_network_interfaces_static(zlogp,
2825f4b3ec61Sdh155122 		    zoneid));
2826f4b3ec61Sdh155122 	}
2827f4b3ec61Sdh155122 
2828f4b3ec61Sdh155122 	return (0);
2829f4b3ec61Sdh155122 }
2830f4b3ec61Sdh155122 
28317c478bd9Sstevel@tonic-gate static int
28327c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
28337c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
28347c478bd9Sstevel@tonic-gate {
28357c478bd9Sstevel@tonic-gate 	int fd;
28367c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
28377c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
28387c478bd9Sstevel@tonic-gate 	int error;
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
28417c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
28427c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
28437c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
28447c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
28477c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
28487c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
28497c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
28527c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
28537c478bd9Sstevel@tonic-gate 		return (-1);
28547c478bd9Sstevel@tonic-gate 	}
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
28577c478bd9Sstevel@tonic-gate 	(void) close(fd);
28587c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
28597c478bd9Sstevel@tonic-gate 		return (0);
28607c478bd9Sstevel@tonic-gate 	return (-1);
28617c478bd9Sstevel@tonic-gate }
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate static int
28647c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
28657c478bd9Sstevel@tonic-gate {
28667c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
28677c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
28687c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
28697c478bd9Sstevel@tonic-gate 	int error;
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	/*
28727c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
28737c478bd9Sstevel@tonic-gate 	 */
28747c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
28757c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
28767c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
28777c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
28787c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
28817c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
28827c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
28837c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
28847c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
28877c478bd9Sstevel@tonic-gate 		return (error);
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	/*
28907c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
28917c478bd9Sstevel@tonic-gate 	 */
28927c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
28937c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
28947c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
28957c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
28967c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
28997c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
29007c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
29017c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
29027c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
29057c478bd9Sstevel@tonic-gate 		return (error);
29067c478bd9Sstevel@tonic-gate 	return (0);
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate static int
2910ffbafc53Scomay get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd)
2911ffbafc53Scomay {
2912ffbafc53Scomay 	int error = -1;
2913ffbafc53Scomay 	zone_dochandle_t handle;
2914ffbafc53Scomay 	char *privname = NULL;
2915ffbafc53Scomay 
2916ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
2917ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2918ffbafc53Scomay 		return (-1);
2919ffbafc53Scomay 	}
2920ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2921ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
2922ffbafc53Scomay 		zonecfg_fini_handle(handle);
2923ffbafc53Scomay 		return (-1);
2924ffbafc53Scomay 	}
2925ffbafc53Scomay 
29269acbbeafSnn35248 	if (mount_cmd) {
2927bf1d7e28Sdh155122 		zone_iptype_t	iptype;
2928bf1d7e28Sdh155122 		const char	*curr_iptype;
2929bf1d7e28Sdh155122 
2930bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
2931bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
2932bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
2933bf1d7e28Sdh155122 			return (-1);
2934bf1d7e28Sdh155122 		}
2935bf1d7e28Sdh155122 
2936bf1d7e28Sdh155122 		switch (iptype) {
2937bf1d7e28Sdh155122 		case ZS_SHARED:
2938bf1d7e28Sdh155122 			curr_iptype = "shared";
2939bf1d7e28Sdh155122 			break;
2940bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
2941bf1d7e28Sdh155122 			curr_iptype = "exclusive";
2942bf1d7e28Sdh155122 			break;
2943bf1d7e28Sdh155122 		}
2944bf1d7e28Sdh155122 
2945e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
2946e767a340Sgjelinek 			zonecfg_fini_handle(handle);
29479acbbeafSnn35248 			return (0);
2948e767a340Sgjelinek 		}
29499acbbeafSnn35248 		zerror(zlogp, B_FALSE,
29509acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
29519acbbeafSnn35248 		zonecfg_fini_handle(handle);
29529acbbeafSnn35248 		return (-1);
29539acbbeafSnn35248 	}
29549acbbeafSnn35248 
2955ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
2956ffbafc53Scomay 	case Z_OK:
2957ffbafc53Scomay 		error = 0;
2958ffbafc53Scomay 		break;
2959ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2960ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
2961ffbafc53Scomay 		    "within the zone's privilege set", privname);
2962ffbafc53Scomay 		break;
2963ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2964ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
2965ffbafc53Scomay 		    "from the zone's privilege set", privname);
2966ffbafc53Scomay 		break;
2967ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2968ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
2969ffbafc53Scomay 		    "in the zone's privilege set", privname);
2970ffbafc53Scomay 		break;
2971ffbafc53Scomay 	default:
2972ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
2973ffbafc53Scomay 		    "privilege set");
2974ffbafc53Scomay 		break;
2975ffbafc53Scomay 	}
2976ffbafc53Scomay 
2977ffbafc53Scomay 	free(privname);
2978ffbafc53Scomay 	zonecfg_fini_handle(handle);
2979ffbafc53Scomay 	return (error);
2980ffbafc53Scomay }
2981ffbafc53Scomay 
2982ffbafc53Scomay static int
29837c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
29847c478bd9Sstevel@tonic-gate {
29857c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
29867c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
29877c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
29887c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
29897c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
29907c478bd9Sstevel@tonic-gate 	int error = -1;
29917c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
29927c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
29937c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 	*bufp = NULL;
29967c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
29997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
30007c478bd9Sstevel@tonic-gate 		return (-1);
30017c478bd9Sstevel@tonic-gate 	}
30027c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
30037c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
30047c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
30057c478bd9Sstevel@tonic-gate 		return (-1);
30067c478bd9Sstevel@tonic-gate 	}
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
30097c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
30107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
30117c478bd9Sstevel@tonic-gate 		goto out;
30127c478bd9Sstevel@tonic-gate 	}
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
30157c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
30167c478bd9Sstevel@tonic-gate 		goto out;
30177c478bd9Sstevel@tonic-gate 	}
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
30207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
30217c478bd9Sstevel@tonic-gate 		goto out;
30227c478bd9Sstevel@tonic-gate 	}
30237c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
30247c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
30257c478bd9Sstevel@tonic-gate 		uint_t i, count;
30267c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
30297c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
30307c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30317c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
30327c478bd9Sstevel@tonic-gate 			continue;
30337c478bd9Sstevel@tonic-gate 		}
30347c478bd9Sstevel@tonic-gate 		count = 0;
30357c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
30367c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
30377c478bd9Sstevel@tonic-gate 			count++;
30387c478bd9Sstevel@tonic-gate 		}
30397c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
30407c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
30417c478bd9Sstevel@tonic-gate 		}
30427c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
30437c478bd9Sstevel@tonic-gate 			goto out;
30447c478bd9Sstevel@tonic-gate 		i = 0;
30457c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
30467c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
30477c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
30487c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
30497c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
30507c478bd9Sstevel@tonic-gate 				goto out;
30517c478bd9Sstevel@tonic-gate 			}
30527c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
30537c478bd9Sstevel@tonic-gate 			    != Z_OK) {
30547c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
30557c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
30567c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
30577c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
30587c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
30597c478bd9Sstevel@tonic-gate 				goto out;
30607c478bd9Sstevel@tonic-gate 			}
30617c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
30627c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
30637c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
30647c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
30657c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
30667c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
30677c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
30687c478bd9Sstevel@tonic-gate 				    name);
30697c478bd9Sstevel@tonic-gate 				goto out;
30707c478bd9Sstevel@tonic-gate 			}
30717c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
30727c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
30737c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
30747c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
30757c478bd9Sstevel@tonic-gate 				goto out;
30767c478bd9Sstevel@tonic-gate 			}
30777c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
30787c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
30797c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
30807c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
30817c478bd9Sstevel@tonic-gate 				goto out;
30827c478bd9Sstevel@tonic-gate 			}
30837c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
30847c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
30857c478bd9Sstevel@tonic-gate 			    != 0) {
30867c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
30877c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
30887c478bd9Sstevel@tonic-gate 				goto out;
30897c478bd9Sstevel@tonic-gate 			}
30907c478bd9Sstevel@tonic-gate 		}
30917c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30927c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
30937c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
30947c478bd9Sstevel@tonic-gate 		    != 0) {
30957c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
30967c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
30977c478bd9Sstevel@tonic-gate 			goto out;
30987c478bd9Sstevel@tonic-gate 		}
30997c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
31007c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
31017c478bd9Sstevel@tonic-gate 		free(nvlv);
31027c478bd9Sstevel@tonic-gate 		nvlv = NULL;
31037c478bd9Sstevel@tonic-gate 		rctlcount++;
31047c478bd9Sstevel@tonic-gate 	}
31057c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
31087c478bd9Sstevel@tonic-gate 		error = 0;
31097c478bd9Sstevel@tonic-gate 		goto out;
31107c478bd9Sstevel@tonic-gate 	}
31117c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
31127c478bd9Sstevel@tonic-gate 	    != 0) {
31137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
31147c478bd9Sstevel@tonic-gate 		goto out;
31157c478bd9Sstevel@tonic-gate 	}
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	error = 0;
31187c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
31197c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate out:
31227c478bd9Sstevel@tonic-gate 	free(rctlblk);
31237c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
31247c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
31257c478bd9Sstevel@tonic-gate 		free(nvl_packed);
31267c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
31277c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
31287c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
31297c478bd9Sstevel@tonic-gate 		free(nvlv);
31307c478bd9Sstevel@tonic-gate 	if (handle != NULL)
31317c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
31327c478bd9Sstevel@tonic-gate 	return (error);
31337c478bd9Sstevel@tonic-gate }
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate static int
3136fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3137fa9e4066Sahrens {
3138fa9e4066Sahrens 	zone_dochandle_t handle;
3139fa9e4066Sahrens 	struct zone_dstab dstab;
3140fa9e4066Sahrens 	size_t total, offset, len;
3141fa9e4066Sahrens 	int error = -1;
3142e5a17f6aSgjelinek 	char *str = NULL;
3143fa9e4066Sahrens 
3144fa9e4066Sahrens 	*bufp = NULL;
3145fa9e4066Sahrens 	*bufsizep = 0;
3146fa9e4066Sahrens 
3147fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3148fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3149fa9e4066Sahrens 		return (-1);
3150fa9e4066Sahrens 	}
3151fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3152fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3153fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3154fa9e4066Sahrens 		return (-1);
3155fa9e4066Sahrens 	}
3156fa9e4066Sahrens 
3157fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3158fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3159fa9e4066Sahrens 		goto out;
3160fa9e4066Sahrens 	}
3161fa9e4066Sahrens 
3162fa9e4066Sahrens 	total = 0;
3163fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3164fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3165fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3166fa9e4066Sahrens 
3167fa9e4066Sahrens 	if (total == 0) {
3168fa9e4066Sahrens 		error = 0;
3169fa9e4066Sahrens 		goto out;
3170fa9e4066Sahrens 	}
3171fa9e4066Sahrens 
3172fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3173fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3174fa9e4066Sahrens 		goto out;
3175fa9e4066Sahrens 	}
3176fa9e4066Sahrens 
3177fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3178fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3179fa9e4066Sahrens 		goto out;
3180fa9e4066Sahrens 	}
3181fa9e4066Sahrens 	offset = 0;
3182fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3183fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3184fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3185e5a17f6aSgjelinek 		    total - offset);
3186fa9e4066Sahrens 		offset += len;
3187e5a17f6aSgjelinek 		if (offset < total - 1)
3188fa9e4066Sahrens 			str[offset++] = ',';
3189fa9e4066Sahrens 	}
3190fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3191fa9e4066Sahrens 
3192fa9e4066Sahrens 	error = 0;
3193fa9e4066Sahrens 	*bufp = str;
3194fa9e4066Sahrens 	*bufsizep = total;
3195fa9e4066Sahrens 
3196fa9e4066Sahrens out:
3197fa9e4066Sahrens 	if (error != 0 && str != NULL)
3198fa9e4066Sahrens 		free(str);
3199fa9e4066Sahrens 	if (handle != NULL)
3200fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3201fa9e4066Sahrens 
3202fa9e4066Sahrens 	return (error);
3203fa9e4066Sahrens }
3204fa9e4066Sahrens 
3205fa9e4066Sahrens static int
3206fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3207fa9e4066Sahrens {
3208fa9e4066Sahrens 	zone_dochandle_t handle;
3209fa9e4066Sahrens 	struct zone_dstab dstab;
3210fa9e4066Sahrens 	zfs_handle_t *zhp;
321199653d4eSeschrock 	libzfs_handle_t *hdl;
3212fa9e4066Sahrens 
3213fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3214fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3215fa9e4066Sahrens 		return (-1);
3216fa9e4066Sahrens 	}
3217fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3218fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3219fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3220fa9e4066Sahrens 		return (-1);
3221fa9e4066Sahrens 	}
3222fa9e4066Sahrens 
3223fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3224fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3225fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3226fa9e4066Sahrens 		return (-1);
3227fa9e4066Sahrens 	}
3228fa9e4066Sahrens 
322999653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
323099653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
323199653d4eSeschrock 		zonecfg_fini_handle(handle);
323299653d4eSeschrock 		return (-1);
323399653d4eSeschrock 	}
3234fa9e4066Sahrens 
3235fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3236fa9e4066Sahrens 
323799653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3238fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3239fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3240fa9e4066Sahrens 			    dstab.zone_dataset_name);
3241fa9e4066Sahrens 			zonecfg_fini_handle(handle);
324299653d4eSeschrock 			libzfs_fini(hdl);
3243fa9e4066Sahrens 			return (-1);
3244fa9e4066Sahrens 		}
3245fa9e4066Sahrens 
3246fa9e4066Sahrens 		/*
3247fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3248fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3249fa9e4066Sahrens 		 */
3250fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3251e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3252e9dbad6fSeschrock 		    "on") != 0) {
3253fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3254fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3255fa9e4066Sahrens 			    dstab.zone_dataset_name);
3256fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3257fa9e4066Sahrens 			zfs_close(zhp);
325899653d4eSeschrock 			libzfs_fini(hdl);
3259fa9e4066Sahrens 			return (-1);
3260fa9e4066Sahrens 		}
3261fa9e4066Sahrens 
3262fa9e4066Sahrens 		zfs_close(zhp);
3263fa9e4066Sahrens 	}
3264fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3265fa9e4066Sahrens 
3266fa9e4066Sahrens 	zonecfg_fini_handle(handle);
326799653d4eSeschrock 	libzfs_fini(hdl);
3268fa9e4066Sahrens 
3269fa9e4066Sahrens 	return (0);
3270fa9e4066Sahrens }
3271fa9e4066Sahrens 
327245916cd2Sjpk /*
327345916cd2Sjpk  * Mount lower level home directories into/from current zone
327445916cd2Sjpk  * Share exported directories specified in dfstab for zone
327545916cd2Sjpk  */
327645916cd2Sjpk static int
327745916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
327845916cd2Sjpk {
327945916cd2Sjpk 	zoneid_t *zids = NULL;
328045916cd2Sjpk 	priv_set_t *zid_privs;
328145916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
328245916cd2Sjpk 	uint_t nzents_saved;
328345916cd2Sjpk 	uint_t nzents;
328445916cd2Sjpk 	int i;
328545916cd2Sjpk 	char readonly[] = "ro";
328645916cd2Sjpk 	struct zone_fstab lower_fstab;
328745916cd2Sjpk 	char *argv[4];
328845916cd2Sjpk 
328945916cd2Sjpk 	if (!is_system_labeled())
329045916cd2Sjpk 		return (0);
329145916cd2Sjpk 
329245916cd2Sjpk 	if (zid_label == NULL) {
329345916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
329445916cd2Sjpk 		if (zid_label == NULL)
329545916cd2Sjpk 			return (-1);
329645916cd2Sjpk 	}
329745916cd2Sjpk 
329845916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
329945916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
330027e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
330145916cd2Sjpk 
330245916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
330345916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
330445916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
330545916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
330645916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
330745916cd2Sjpk 
330845916cd2Sjpk 	/*
330945916cd2Sjpk 	 * Get the list of zones from the kernel
331045916cd2Sjpk 	 */
331145916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
331245916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
331345916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
331445916cd2Sjpk 		return (-1);
331545916cd2Sjpk 	}
331645916cd2Sjpk again:
331745916cd2Sjpk 	if (nzents == 0) {
331845916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
331945916cd2Sjpk 		return (-1);
332045916cd2Sjpk 	}
332145916cd2Sjpk 
332245916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
332345916cd2Sjpk 	if (zids == NULL) {
33243f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
332545916cd2Sjpk 		return (-1);
332645916cd2Sjpk 	}
332745916cd2Sjpk 	nzents_saved = nzents;
332845916cd2Sjpk 
332945916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
333045916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
333145916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
333245916cd2Sjpk 		free(zids);
333345916cd2Sjpk 		return (-1);
333445916cd2Sjpk 	}
333545916cd2Sjpk 	if (nzents != nzents_saved) {
333645916cd2Sjpk 		/* list changed, try again */
333745916cd2Sjpk 		free(zids);
333845916cd2Sjpk 		goto again;
333945916cd2Sjpk 	}
334045916cd2Sjpk 
334145916cd2Sjpk 	ip = getprivimplinfo();
334245916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
334345916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
334445916cd2Sjpk 		zonecfg_free_fs_option_list(
334545916cd2Sjpk 		    lower_fstab.zone_fs_options);
334645916cd2Sjpk 		free(zids);
334745916cd2Sjpk 		return (-1);
334845916cd2Sjpk 	}
334945916cd2Sjpk 
335045916cd2Sjpk 	for (i = 0; i < nzents; i++) {
335145916cd2Sjpk 		char zid_name[ZONENAME_MAX];
335245916cd2Sjpk 		zone_state_t zid_state;
335345916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
335445916cd2Sjpk 		struct stat stat_buf;
335545916cd2Sjpk 
335645916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
335745916cd2Sjpk 			continue;
335845916cd2Sjpk 
335945916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
336045916cd2Sjpk 			continue;
336145916cd2Sjpk 
336245916cd2Sjpk 		/*
336345916cd2Sjpk 		 * Do special setup for the zone we are booting
336445916cd2Sjpk 		 */
336545916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
336645916cd2Sjpk 			struct zone_fstab autofs_fstab;
336745916cd2Sjpk 			char map_path[MAXPATHLEN];
336845916cd2Sjpk 			int fd;
336945916cd2Sjpk 
337045916cd2Sjpk 			/*
337145916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
33729acbbeafSnn35248 			 * in the global zone. The non-global zone entry
337345916cd2Sjpk 			 * will be created by automount when the zone
337445916cd2Sjpk 			 * is booted.
337545916cd2Sjpk 			 */
337645916cd2Sjpk 
337745916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
337845916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
337945916cd2Sjpk 
338045916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
338145916cd2Sjpk 			    "/zone/%s/home", zid_name);
338245916cd2Sjpk 
338345916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
338445916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
338545916cd2Sjpk 			/*
338645916cd2Sjpk 			 * If the map file doesn't exist create a template
338745916cd2Sjpk 			 */
338845916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
338945916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
339045916cd2Sjpk 				int len;
339145916cd2Sjpk 				char map_rec[MAXPATHLEN];
339245916cd2Sjpk 
339345916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
339445916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
339545916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
339645916cd2Sjpk 				(void) write(fd, map_rec, len);
339745916cd2Sjpk 				(void) close(fd);
339845916cd2Sjpk 			}
339945916cd2Sjpk 
340045916cd2Sjpk 			/*
340145916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
340245916cd2Sjpk 			 * If it's already of type autofs, then
340345916cd2Sjpk 			 * don't mount it again.
340445916cd2Sjpk 			 */
340545916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
340645916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
340745916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
340845916cd2Sjpk 
340945916cd2Sjpk 				(void) make_one_dir(zlogp, "",
341027e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
341127e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
341245916cd2Sjpk 
341345916cd2Sjpk 				/*
341445916cd2Sjpk 				 * Mount will fail if automounter has already
341545916cd2Sjpk 				 * processed the auto_home_<zonename> map
341645916cd2Sjpk 				 */
341745916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
341845916cd2Sjpk 				    autofs_fstab.zone_fs_special,
341945916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
342045916cd2Sjpk 			}
342145916cd2Sjpk 			continue;
342245916cd2Sjpk 		}
342345916cd2Sjpk 
342445916cd2Sjpk 
342545916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
342648451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
342748451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
342845916cd2Sjpk 			/* Skip over zones without mounted filesystems */
342945916cd2Sjpk 			continue;
343045916cd2Sjpk 
343145916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
343245916cd2Sjpk 		    sizeof (m_label_t)) < 0)
343345916cd2Sjpk 			/* Skip over zones with unspecified label */
343445916cd2Sjpk 			continue;
343545916cd2Sjpk 
343645916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
343745916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
343845916cd2Sjpk 			/* Skip over zones with bad path */
343945916cd2Sjpk 			continue;
344045916cd2Sjpk 
344145916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
344245916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
344345916cd2Sjpk 			/* Skip over zones with bad privs */
344445916cd2Sjpk 			continue;
344545916cd2Sjpk 
344645916cd2Sjpk 		/*
344745916cd2Sjpk 		 * Reading down is valid according to our label model
344845916cd2Sjpk 		 * but some customers want to disable it because it
344945916cd2Sjpk 		 * allows execute down and other possible attacks.
345045916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
345145916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
345245916cd2Sjpk 		 * for NFS read-down semantics.
345345916cd2Sjpk 		 */
345445916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
345545916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
345645916cd2Sjpk 			/*
345745916cd2Sjpk 			 * Our zone dominates this one.
345845916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
345945916cd2Sjpk 			 */
346045916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
346145916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
346245916cd2Sjpk 
346345916cd2Sjpk 			/*
346445916cd2Sjpk 			 * If the target is already an LOFS mount
346545916cd2Sjpk 			 * then don't do it again.
346645916cd2Sjpk 			 */
346745916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
346845916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
346945916cd2Sjpk 
347045916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
347145916cd2Sjpk 				    MAXPATHLEN, "%s/export",
347245916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
347345916cd2Sjpk 					continue;
347445916cd2Sjpk 
347545916cd2Sjpk 				/*
347645916cd2Sjpk 				 * Make sure the lower-level home exists
347745916cd2Sjpk 				 */
347845916cd2Sjpk 				if (make_one_dir(zlogp,
347927e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
348027e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
348127e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
348245916cd2Sjpk 					continue;
348345916cd2Sjpk 
348445916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
348545916cd2Sjpk 				    "/home", MAXPATHLEN);
348645916cd2Sjpk 
348745916cd2Sjpk 				/*
348845916cd2Sjpk 				 * Mount can fail because the lower-level
348945916cd2Sjpk 				 * zone may have already done a mount up.
349045916cd2Sjpk 				 */
349145916cd2Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
349245916cd2Sjpk 			}
349345916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
349445916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
349545916cd2Sjpk 			/*
349645916cd2Sjpk 			 * This zone dominates our zone.
349745916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
349845916cd2Sjpk 			 */
349945916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
350045916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
350145916cd2Sjpk 			    zone_name) > MAXPATHLEN)
350245916cd2Sjpk 				continue;
350345916cd2Sjpk 
350445916cd2Sjpk 			/*
350545916cd2Sjpk 			 * If the target is already an LOFS mount
350645916cd2Sjpk 			 * then don't do it again.
350745916cd2Sjpk 			 */
350845916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
350945916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
351045916cd2Sjpk 
351145916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
351245916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
351345916cd2Sjpk 
351445916cd2Sjpk 				/*
351545916cd2Sjpk 				 * Mount can fail because the higher-level
351645916cd2Sjpk 				 * zone may have already done a mount down.
351745916cd2Sjpk 				 */
351845916cd2Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
351945916cd2Sjpk 			}
352045916cd2Sjpk 		}
352145916cd2Sjpk 	}
352245916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
352345916cd2Sjpk 	priv_freeset(zid_privs);
352445916cd2Sjpk 	free(zids);
352545916cd2Sjpk 
352645916cd2Sjpk 	/*
352745916cd2Sjpk 	 * Now share any exported directories from this zone.
352845916cd2Sjpk 	 * Each zone can have its own dfstab.
352945916cd2Sjpk 	 */
353045916cd2Sjpk 
353145916cd2Sjpk 	argv[0] = "zoneshare";
353245916cd2Sjpk 	argv[1] = "-z";
353345916cd2Sjpk 	argv[2] = zone_name;
353445916cd2Sjpk 	argv[3] = NULL;
353545916cd2Sjpk 
353645916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
353745916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
353845916cd2Sjpk 
353945916cd2Sjpk 	return (0);
354045916cd2Sjpk }
354145916cd2Sjpk 
354245916cd2Sjpk /*
354345916cd2Sjpk  * Unmount lofs mounts from higher level zones
354445916cd2Sjpk  * Unshare nfs exported directories
354545916cd2Sjpk  */
354645916cd2Sjpk static void
354745916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
354845916cd2Sjpk {
354945916cd2Sjpk 	zoneid_t *zids = NULL;
355045916cd2Sjpk 	uint_t nzents_saved;
355145916cd2Sjpk 	uint_t nzents;
355245916cd2Sjpk 	int i;
355345916cd2Sjpk 	char *argv[4];
355445916cd2Sjpk 	char path[MAXPATHLEN];
355545916cd2Sjpk 
355645916cd2Sjpk 	if (!is_system_labeled())
355745916cd2Sjpk 		return;
355845916cd2Sjpk 
355945916cd2Sjpk 	/*
356045916cd2Sjpk 	 * Get the list of zones from the kernel
356145916cd2Sjpk 	 */
356245916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
356345916cd2Sjpk 		return;
356445916cd2Sjpk 	}
356545916cd2Sjpk 
356645916cd2Sjpk 	if (zid_label == NULL) {
356745916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
356845916cd2Sjpk 		if (zid_label == NULL)
356945916cd2Sjpk 			return;
357045916cd2Sjpk 	}
357145916cd2Sjpk 
357245916cd2Sjpk again:
357345916cd2Sjpk 	if (nzents == 0)
357445916cd2Sjpk 		return;
357545916cd2Sjpk 
357645916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
357745916cd2Sjpk 	if (zids == NULL) {
35783f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
357945916cd2Sjpk 		return;
358045916cd2Sjpk 	}
358145916cd2Sjpk 	nzents_saved = nzents;
358245916cd2Sjpk 
358345916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
358445916cd2Sjpk 		free(zids);
358545916cd2Sjpk 		return;
358645916cd2Sjpk 	}
358745916cd2Sjpk 	if (nzents != nzents_saved) {
358845916cd2Sjpk 		/* list changed, try again */
358945916cd2Sjpk 		free(zids);
359045916cd2Sjpk 		goto again;
359145916cd2Sjpk 	}
359245916cd2Sjpk 
359345916cd2Sjpk 	for (i = 0; i < nzents; i++) {
359445916cd2Sjpk 		char zid_name[ZONENAME_MAX];
359545916cd2Sjpk 		zone_state_t zid_state;
359645916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
359745916cd2Sjpk 
359845916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
359945916cd2Sjpk 			continue;
360045916cd2Sjpk 
360145916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
360245916cd2Sjpk 			continue;
360345916cd2Sjpk 
360445916cd2Sjpk 		/*
360545916cd2Sjpk 		 * Skip the zone we are halting
360645916cd2Sjpk 		 */
360745916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
360845916cd2Sjpk 			continue;
360945916cd2Sjpk 
361045916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
361145916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
361245916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
361345916cd2Sjpk 			/* Skip over zones without mounted filesystems */
361445916cd2Sjpk 			continue;
361545916cd2Sjpk 
361645916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
361745916cd2Sjpk 		    sizeof (m_label_t)) < 0)
361845916cd2Sjpk 			/* Skip over zones with unspecified label */
361945916cd2Sjpk 			continue;
362045916cd2Sjpk 
362145916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
362245916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
362345916cd2Sjpk 			/* Skip over zones with bad path */
362445916cd2Sjpk 			continue;
362545916cd2Sjpk 
362645916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
362745916cd2Sjpk 			/*
362845916cd2Sjpk 			 * This zone dominates our zone.
362945916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
363045916cd2Sjpk 			 */
363145916cd2Sjpk 
363245916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
363345916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
363445916cd2Sjpk 			    zone_name) > MAXPATHLEN)
363545916cd2Sjpk 				continue;
363645916cd2Sjpk 
363745916cd2Sjpk 			/* Skip over mount failures */
363845916cd2Sjpk 			(void) umount(path);
363945916cd2Sjpk 		}
364045916cd2Sjpk 	}
364145916cd2Sjpk 	free(zids);
364245916cd2Sjpk 
364345916cd2Sjpk 	/*
364445916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
364545916cd2Sjpk 	 */
364645916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
364745916cd2Sjpk 	/* Skip over mount failures */
364845916cd2Sjpk 	(void) umount(path);
364945916cd2Sjpk 
365045916cd2Sjpk 	/*
365145916cd2Sjpk 	 * Next unshare any exported directories from this zone.
365245916cd2Sjpk 	 */
365345916cd2Sjpk 
365445916cd2Sjpk 	argv[0] = "zoneunshare";
365545916cd2Sjpk 	argv[1] = "-z";
365645916cd2Sjpk 	argv[2] = zone_name;
365745916cd2Sjpk 	argv[3] = NULL;
365845916cd2Sjpk 
365945916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
366045916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
366145916cd2Sjpk 
366245916cd2Sjpk 	/*
366345916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
366445916cd2Sjpk 	 */
366545916cd2Sjpk 
366645916cd2Sjpk 	argv[0] = "deallocate";
366745916cd2Sjpk 	argv[1] = "-Isz";
366845916cd2Sjpk 	argv[2] = zone_name;
366945916cd2Sjpk 	argv[3] = NULL;
367045916cd2Sjpk 
367145916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
367245916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
367345916cd2Sjpk }
367445916cd2Sjpk 
367545916cd2Sjpk /*
367645916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
367745916cd2Sjpk  * this zone.
367845916cd2Sjpk  */
367945916cd2Sjpk static tsol_zcent_t *
368045916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
368145916cd2Sjpk {
368245916cd2Sjpk 	FILE *fp;
368345916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
368445916cd2Sjpk 	char line[MAXTNZLEN];
368545916cd2Sjpk 
368645916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
368745916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
368845916cd2Sjpk 		return (NULL);
368945916cd2Sjpk 	}
369045916cd2Sjpk 
369145916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
369245916cd2Sjpk 		/*
369345916cd2Sjpk 		 * Check for malformed database
369445916cd2Sjpk 		 */
369545916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
369645916cd2Sjpk 			break;
369745916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
369845916cd2Sjpk 			continue;
369945916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
370045916cd2Sjpk 			break;
370145916cd2Sjpk 		tsol_freezcent(zcent);
370245916cd2Sjpk 		zcent = NULL;
370345916cd2Sjpk 	}
370445916cd2Sjpk 	(void) fclose(fp);
370545916cd2Sjpk 
370645916cd2Sjpk 	if (zcent == NULL) {
370745916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
370845916cd2Sjpk 		    "See tnzonecfg(4)");
370945916cd2Sjpk 	} else {
371045916cd2Sjpk 		if (zlabel == NULL)
371145916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
371245916cd2Sjpk 		/*
371345916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
371445916cd2Sjpk 		 */
371545916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
371645916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
371745916cd2Sjpk 			return (NULL);
371845916cd2Sjpk 		} else {
371945916cd2Sjpk 			priv_copyset(privs, zprivs);
372045916cd2Sjpk 		}
372145916cd2Sjpk 	}
372245916cd2Sjpk 	return (zcent);
372345916cd2Sjpk }
372445916cd2Sjpk 
372545916cd2Sjpk /*
372645916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
372745916cd2Sjpk  */
372845916cd2Sjpk static void
372945916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
373045916cd2Sjpk {
373145916cd2Sjpk 	tsol_mlp_t *mlp;
373245916cd2Sjpk 	tsol_mlpent_t tsme;
373345916cd2Sjpk 
373445916cd2Sjpk 	if (!is_system_labeled())
373545916cd2Sjpk 		return;
373645916cd2Sjpk 
373745916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
373845916cd2Sjpk 	tsme.tsme_flags = 0;
373945916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
374045916cd2Sjpk 		tsme.tsme_mlp = *mlp;
374145916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
374245916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
374345916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
374445916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
374545916cd2Sjpk 		}
374645916cd2Sjpk 	}
374745916cd2Sjpk 
374845916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
374945916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
375045916cd2Sjpk 		tsme.tsme_mlp = *mlp;
375145916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
375245916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
375345916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
375445916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
375545916cd2Sjpk 		}
375645916cd2Sjpk 	}
375745916cd2Sjpk }
375845916cd2Sjpk 
375945916cd2Sjpk static void
376045916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
376145916cd2Sjpk {
376245916cd2Sjpk 	tsol_mlpent_t tsme;
376345916cd2Sjpk 
376445916cd2Sjpk 	if (!is_system_labeled())
376545916cd2Sjpk 		return;
376645916cd2Sjpk 
376745916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
376845916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
376945916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
377045916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
377145916cd2Sjpk }
377245916cd2Sjpk 
37737c478bd9Sstevel@tonic-gate int
37747c478bd9Sstevel@tonic-gate prtmount(const char *fs, void *x) {
37757c478bd9Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
37767c478bd9Sstevel@tonic-gate 	return (0);
37777c478bd9Sstevel@tonic-gate }
37787c478bd9Sstevel@tonic-gate 
3779108322fbScarlsonj /*
3780108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
3781108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3782108322fbScarlsonj  * is found or if we can't tell.
3783108322fbScarlsonj  */
3784108322fbScarlsonj static boolean_t
3785108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
37867c478bd9Sstevel@tonic-gate {
3787108322fbScarlsonj 	zoneid_t *zids = NULL;
3788108322fbScarlsonj 	uint_t nzids = 0;
3789108322fbScarlsonj 	boolean_t retv;
3790108322fbScarlsonj 	int rlen, zlen;
3791108322fbScarlsonj 	char zroot[MAXPATHLEN];
3792108322fbScarlsonj 	char zonename[ZONENAME_MAX];
3793108322fbScarlsonj 
3794108322fbScarlsonj 	for (;;) {
3795108322fbScarlsonj 		nzids += 10;
3796108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
3797108322fbScarlsonj 		if (zids == NULL) {
37983f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3799108322fbScarlsonj 			return (B_TRUE);
3800108322fbScarlsonj 		}
3801108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
3802108322fbScarlsonj 			break;
3803108322fbScarlsonj 		free(zids);
3804108322fbScarlsonj 	}
3805108322fbScarlsonj 	retv = B_FALSE;
3806108322fbScarlsonj 	rlen = strlen(rootpath);
3807108322fbScarlsonj 	while (nzids > 0) {
3808108322fbScarlsonj 		/*
3809108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3810108322fbScarlsonj 		 * while we were busy.
3811108322fbScarlsonj 		 */
3812108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3813108322fbScarlsonj 		    sizeof (zroot)) == -1)
3814108322fbScarlsonj 			continue;
3815108322fbScarlsonj 		zlen = strlen(zroot);
3816108322fbScarlsonj 		if (zlen > rlen)
3817108322fbScarlsonj 			zlen = rlen;
3818108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3819108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3820108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3821108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3822108322fbScarlsonj 			    sizeof (zonename)) == -1)
3823108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3824108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
3825108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3826108322fbScarlsonj 			    "zone root %s already in use by zone %s",
3827108322fbScarlsonj 			    rootpath, zonename);
3828108322fbScarlsonj 			retv = B_TRUE;
3829108322fbScarlsonj 			break;
3830108322fbScarlsonj 		}
3831108322fbScarlsonj 	}
3832108322fbScarlsonj 	free(zids);
3833108322fbScarlsonj 	return (retv);
3834108322fbScarlsonj }
3835108322fbScarlsonj 
3836108322fbScarlsonj /*
3837108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
3838108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3839108322fbScarlsonj  */
3840108322fbScarlsonj static boolean_t
3841108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3842108322fbScarlsonj {
3843108322fbScarlsonj 	struct stat64 rst, zst;
3844108322fbScarlsonj 	struct mnttab *mnp;
3845108322fbScarlsonj 
3846108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
3847108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3848108322fbScarlsonj 		return (B_TRUE);
3849108322fbScarlsonj 	}
3850108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3851108322fbScarlsonj 		return (B_TRUE);
3852108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3853108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
3854108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3855108322fbScarlsonj 			continue;
3856108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3857108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
3858108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3859108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3860108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3861108322fbScarlsonj 			    "zone root %s is reachable through %s",
3862108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
3863108322fbScarlsonj 			return (B_TRUE);
3864108322fbScarlsonj 		}
3865108322fbScarlsonj 	}
3866108322fbScarlsonj 	return (B_FALSE);
3867108322fbScarlsonj }
3868108322fbScarlsonj 
38690209230bSgjelinek /*
38700209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
38710209230bSgjelinek  * configuration.
38720209230bSgjelinek  */
38730209230bSgjelinek static int
38740209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
38750209230bSgjelinek {
38760209230bSgjelinek 	int res;
38770209230bSgjelinek 	uint64_t tmp;
38780209230bSgjelinek 	struct zone_mcaptab mcap;
38790209230bSgjelinek 	char sched[MAXNAMELEN];
38800209230bSgjelinek 	zone_dochandle_t handle = NULL;
38810209230bSgjelinek 	char pool_err[128];
38820209230bSgjelinek 
38830209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
38840209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
38850209230bSgjelinek 		return (Z_BAD_HANDLE);
38860209230bSgjelinek 	}
38870209230bSgjelinek 
38880209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
38890209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
38900209230bSgjelinek 		zonecfg_fini_handle(handle);
38910209230bSgjelinek 		return (res);
38920209230bSgjelinek 	}
38930209230bSgjelinek 
38940209230bSgjelinek 	/*
38950209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
38960209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
38970209230bSgjelinek 	 */
38980209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
38990209230bSgjelinek 		uint64_t num;
39000209230bSgjelinek 		char smf_err[128];
39010209230bSgjelinek 
39020209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
39030209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
39040209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
39050209230bSgjelinek 			zonecfg_fini_handle(handle);
39060209230bSgjelinek 			return (Z_INVAL);
39070209230bSgjelinek 		}
39080209230bSgjelinek 
39090209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
39100209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
39110209230bSgjelinek 			    "failed: %s", smf_err);
39120209230bSgjelinek 			zonecfg_fini_handle(handle);
39130209230bSgjelinek 			return (Z_INVAL);
39140209230bSgjelinek 		}
39150209230bSgjelinek 	}
39160209230bSgjelinek 
39170209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
39180209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
39190209230bSgjelinek 	    strlen(sched) > 0) {
39200209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
39210209230bSgjelinek 		    strlen(sched)) == -1)
39220209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
39230209230bSgjelinek 			    "default scheduling class");
39240209230bSgjelinek 
39250209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
39260209230bSgjelinek 	    == Z_OK) {
39270209230bSgjelinek 		/*
39280209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
39290209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
39300209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
39310209230bSgjelinek 		 * in by default so we can print a warning and modify the class
39320209230bSgjelinek 		 * if we wouldn't be using FSS.
39330209230bSgjelinek 		 */
39340209230bSgjelinek 		char class_name[PC_CLNMSZ];
39350209230bSgjelinek 
39360209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
39370209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
39380209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
39390209230bSgjelinek 			    "the zone's scheduling class");
39400209230bSgjelinek 
39410209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
39420209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
39430209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
39440209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
39450209230bSgjelinek 			    "used for processes\nin the zone but to get the "
39460209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
39470209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
39480209230bSgjelinek 			    "details.");
39490209230bSgjelinek 
39500209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
39510209230bSgjelinek 			    strlen("FSS")) == -1)
39520209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
39530209230bSgjelinek 				    "zone scheduling class to FSS");
39540209230bSgjelinek 		}
39550209230bSgjelinek 	}
39560209230bSgjelinek 
39570209230bSgjelinek 	/*
39580209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
39590209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
39600209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
39610209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
39620209230bSgjelinek 	 * and just return if there is nothing to do.
39630209230bSgjelinek 	 *
39640209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
39650209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
39660209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
39670209230bSgjelinek 	 * zonecfg.
39680209230bSgjelinek 	 */
39690209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
39700209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
39710209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
39720209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
39730209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
39740209230bSgjelinek 			    pool_err);
39750209230bSgjelinek 		else
39760209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
39770209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
39780209230bSgjelinek 		zonecfg_fini_handle(handle);
39790209230bSgjelinek 		return (Z_POOL_BIND);
39800209230bSgjelinek 	}
39810209230bSgjelinek 
39820209230bSgjelinek 	/*
39830209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
39840209230bSgjelinek 	 */
39850209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
39860209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
39870209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
39880209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
39890209230bSgjelinek 		    "processor allocation within the specified range\n"
39900209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
39910209230bSgjelinek 		    "See poold(1M).");
39920209230bSgjelinek 	}
39930209230bSgjelinek 
39940209230bSgjelinek 	/* The following is a warning, not an error. */
39950209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
39960209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
39970209230bSgjelinek 		if (res == Z_POOL_BIND)
39980209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
39990209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
40000209230bSgjelinek 		else if (res == Z_POOL)
40010209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
40020209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
40030209230bSgjelinek 		else
40040209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
40050209230bSgjelinek 			    zonecfg_strerror(res));
40060209230bSgjelinek 	}
40070209230bSgjelinek 
40080209230bSgjelinek 	zonecfg_fini_handle(handle);
40090209230bSgjelinek 	return (Z_OK);
40100209230bSgjelinek }
40110209230bSgjelinek 
4012108322fbScarlsonj zoneid_t
4013108322fbScarlsonj vplat_create(zlog_t *zlogp, boolean_t mount_cmd)
4014108322fbScarlsonj {
4015108322fbScarlsonj 	zoneid_t rval = -1;
40167c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
40177c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
40189acbbeafSnn35248 	char modname[MAXPATHLEN];
40199acbbeafSnn35248 	struct brand_attr attr;
4020123807fbSedp 	brand_handle_t bh;
40217c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
4022108322fbScarlsonj 	size_t rctlbufsz = 0;
4023fa9e4066Sahrens 	char *zfsbuf = NULL;
4024fa9e4066Sahrens 	size_t zfsbufsz = 0;
4025108322fbScarlsonj 	zoneid_t zoneid = -1;
40267c478bd9Sstevel@tonic-gate 	int xerr;
4027108322fbScarlsonj 	char *kzone;
4028108322fbScarlsonj 	FILE *fp = NULL;
402945916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
403045916cd2Sjpk 	int match = 0;
403145916cd2Sjpk 	int doi = 0;
4032f4b3ec61Sdh155122 	int flags;
4033f4b3ec61Sdh155122 	zone_iptype_t iptype;
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
40367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
40377c478bd9Sstevel@tonic-gate 		return (-1);
40387c478bd9Sstevel@tonic-gate 	}
4039108322fbScarlsonj 	if (zonecfg_in_alt_root())
4040108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
40417c478bd9Sstevel@tonic-gate 
4042f4b3ec61Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
4043f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4044f4b3ec61Sdh155122 		return (-1);
4045f4b3ec61Sdh155122 	}
4046f4b3ec61Sdh155122 	switch (iptype) {
4047f4b3ec61Sdh155122 	case ZS_SHARED:
4048f4b3ec61Sdh155122 		flags = 0;
4049f4b3ec61Sdh155122 		break;
4050f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
4051f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
4052f4b3ec61Sdh155122 		break;
4053f4b3ec61Sdh155122 	}
4054f4b3ec61Sdh155122 
40557c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
40567c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
40577c478bd9Sstevel@tonic-gate 		return (-1);
40587c478bd9Sstevel@tonic-gate 	}
40597c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4060ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
40617c478bd9Sstevel@tonic-gate 		goto error;
4062ffbafc53Scomay 
4063108322fbScarlsonj 	if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
40647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
40657c478bd9Sstevel@tonic-gate 		goto error;
40667c478bd9Sstevel@tonic-gate 	}
4067ffbafc53Scomay 
4068fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4069fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4070fa9e4066Sahrens 		goto error;
4071fa9e4066Sahrens 	}
40727c478bd9Sstevel@tonic-gate 
407348451833Scarlsonj 	if (!mount_cmd && is_system_labeled()) {
407445916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
407548451833Scarlsonj 		if (zcent != NULL) {
407645916cd2Sjpk 			match = zcent->zc_match;
407745916cd2Sjpk 			doi = zcent->zc_doi;
407845916cd2Sjpk 			*zlabel = zcent->zc_label;
407945916cd2Sjpk 		} else {
408045916cd2Sjpk 			goto error;
408145916cd2Sjpk 		}
408245916cd2Sjpk 	}
408345916cd2Sjpk 
4084108322fbScarlsonj 	kzone = zone_name;
4085108322fbScarlsonj 
4086108322fbScarlsonj 	/*
4087108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4088108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4089108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4090108322fbScarlsonj 	 * that use this same source node (same device and inode).
4091108322fbScarlsonj 	 */
4092108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4093108322fbScarlsonj 		goto error;
4094108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4095108322fbScarlsonj 		goto error;
4096108322fbScarlsonj 
4097108322fbScarlsonj 	if (mount_cmd) {
409884561e8cStd153743 		assert(zone_isnative || zone_iscluster);
4099108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4100108322fbScarlsonj 
4101108322fbScarlsonj 		/*
4102108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4103108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4104108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4105108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4106108322fbScarlsonj 		 * executables.
4107108322fbScarlsonj 		 *
4108108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4109108322fbScarlsonj 		 */
4110108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4111108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4112108322fbScarlsonj 			goto error;
4113108322fbScarlsonj 		}
4114108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4115108322fbScarlsonj 			goto error;
4116108322fbScarlsonj 	}
4117108322fbScarlsonj 
4118108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4119108322fbScarlsonj 		/*
4120108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4121108322fbScarlsonj 		 * then we have some additional work to do before starting the
4122108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4123108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4124108322fbScarlsonj 		 * the zone.
4125108322fbScarlsonj 		 */
4126108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4127108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4128108322fbScarlsonj 			goto error;
4129108322fbScarlsonj 		}
4130108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4131108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4132108322fbScarlsonj 			goto error;
4133108322fbScarlsonj 		}
4134108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4135108322fbScarlsonj 		    NULL, 0) == 0) {
4136108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4137108322fbScarlsonj 			goto error;
4138108322fbScarlsonj 		}
4139108322fbScarlsonj 		/* This is the preferred name */
4140108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4141108322fbScarlsonj 		    zone_name);
4142108322fbScarlsonj 		srandom(getpid());
4143108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4144108322fbScarlsonj 		    0) == 0) {
4145108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4146108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4147108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4148108322fbScarlsonj 		}
4149108322fbScarlsonj 		kzone = kernzone;
4150108322fbScarlsonj 	}
4151108322fbScarlsonj 
41527c478bd9Sstevel@tonic-gate 	xerr = 0;
4153108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4154f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4155f4b3ec61Sdh155122 	    flags)) == -1) {
41567c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
41577c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
41587c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
41597c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
41607c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
41617c478bd9Sstevel@tonic-gate 			} else {
41627c478bd9Sstevel@tonic-gate 
41637c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
41647c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
41657c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
41667c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
41677c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
41687c478bd9Sstevel@tonic-gate 			}
41697c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
41707c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
41717c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
41727c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
41738c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
41748c55461bSton 			char zonename[ZONENAME_MAX];
41758c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
41768c55461bSton 			    zonename, ZONENAME_MAX);
41778c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
41788c55461bSton 			    "used by the zone '%s'.", zonename);
41797c478bd9Sstevel@tonic-gate 		} else {
41807c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
41817c478bd9Sstevel@tonic-gate 		}
41827c478bd9Sstevel@tonic-gate 		goto error;
41837c478bd9Sstevel@tonic-gate 	}
4184108322fbScarlsonj 
4185108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4186108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4187108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4188108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4189108322fbScarlsonj 		goto error;
4190108322fbScarlsonj 	}
4191108322fbScarlsonj 
41929acbbeafSnn35248 	if ((zone_get_brand(zone_name, attr.ba_brandname,
41939acbbeafSnn35248 	    MAXNAMELEN) != Z_OK) ||
4194123807fbSedp 	    (bh = brand_open(attr.ba_brandname)) == NULL) {
41959acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine brand name");
41969acbbeafSnn35248 		return (-1);
41979acbbeafSnn35248 	}
41989acbbeafSnn35248 
41999acbbeafSnn35248 	/*
42009acbbeafSnn35248 	 * If this brand requires any kernel support, now is the time to
42019acbbeafSnn35248 	 * get it loaded and initialized.
42029acbbeafSnn35248 	 */
4203123807fbSedp 	if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4204e767a340Sgjelinek 		brand_close(bh);
42059acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine brand kernel "
42069acbbeafSnn35248 		    "module");
42079acbbeafSnn35248 		return (-1);
42089acbbeafSnn35248 	}
4209e767a340Sgjelinek 	brand_close(bh);
42109acbbeafSnn35248 
42119acbbeafSnn35248 	if (strlen(modname) > 0) {
42129acbbeafSnn35248 		(void) strlcpy(attr.ba_modname, modname, MAXPATHLEN);
42139acbbeafSnn35248 		if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
42149acbbeafSnn35248 		    sizeof (attr) != 0)) {
42159acbbeafSnn35248 			zerror(zlogp, B_TRUE, "could not set zone brand "
42169acbbeafSnn35248 			    "attribute.");
42179acbbeafSnn35248 			goto error;
42189acbbeafSnn35248 		}
42199acbbeafSnn35248 	}
42209acbbeafSnn35248 
42217c478bd9Sstevel@tonic-gate 	/*
42220209230bSgjelinek 	 * The following actions are not performed when merely mounting a zone
42230209230bSgjelinek 	 * for administrative use.
42247c478bd9Sstevel@tonic-gate 	 */
42250209230bSgjelinek 	if (!mount_cmd) {
42260209230bSgjelinek 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) {
42270209230bSgjelinek 			(void) zone_shutdown(zoneid);
42280209230bSgjelinek 			goto error;
42290209230bSgjelinek 		}
42300209230bSgjelinek 
423145916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
42320209230bSgjelinek 	}
42330209230bSgjelinek 
4234108322fbScarlsonj 	rval = zoneid;
4235108322fbScarlsonj 	zoneid = -1;
4236108322fbScarlsonj 
42377c478bd9Sstevel@tonic-gate error:
4238108322fbScarlsonj 	if (zoneid != -1)
4239108322fbScarlsonj 		(void) zone_destroy(zoneid);
42407c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
42417c478bd9Sstevel@tonic-gate 		free(rctlbuf);
42427c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
4243108322fbScarlsonj 	if (fp != NULL)
4244108322fbScarlsonj 		zonecfg_close_scratch(fp);
4245108322fbScarlsonj 	lofs_discard_mnttab();
424645916cd2Sjpk 	if (zcent != NULL)
424745916cd2Sjpk 		tsol_freezcent(zcent);
42487c478bd9Sstevel@tonic-gate 	return (rval);
42497c478bd9Sstevel@tonic-gate }
42507c478bd9Sstevel@tonic-gate 
4251555afedfScarlsonj /*
4252555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
4253555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4254555afedfScarlsonj  * details from inside the zone.
4255555afedfScarlsonj  */
4256555afedfScarlsonj static void
4257555afedfScarlsonj write_index_file(zoneid_t zoneid)
4258555afedfScarlsonj {
4259555afedfScarlsonj 	FILE *zef;
4260555afedfScarlsonj 	FILE *zet;
4261555afedfScarlsonj 	struct zoneent *zep;
4262555afedfScarlsonj 	pid_t child;
4263555afedfScarlsonj 	int tmpl_fd;
4264555afedfScarlsonj 	ctid_t ct;
4265555afedfScarlsonj 	int fd;
4266555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4267555afedfScarlsonj 
4268555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
4269555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
4270555afedfScarlsonj 		return;
4271555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
4272555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
4273555afedfScarlsonj 			break;
4274555afedfScarlsonj 		free(zep);
4275555afedfScarlsonj 	}
4276555afedfScarlsonj 	endzoneent(zef);
4277555afedfScarlsonj 	if (zep == NULL)
4278555afedfScarlsonj 		return;
4279555afedfScarlsonj 
4280555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
4281555afedfScarlsonj 		free(zep);
4282555afedfScarlsonj 		return;
4283555afedfScarlsonj 	}
4284555afedfScarlsonj 
4285555afedfScarlsonj 	if ((child = fork()) == -1) {
4286555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4287555afedfScarlsonj 		(void) close(tmpl_fd);
4288555afedfScarlsonj 		free(zep);
4289555afedfScarlsonj 		return;
4290555afedfScarlsonj 	}
4291555afedfScarlsonj 
4292555afedfScarlsonj 	/* parent waits for child to finish */
4293555afedfScarlsonj 	if (child != 0) {
4294555afedfScarlsonj 		free(zep);
4295555afedfScarlsonj 		if (contract_latest(&ct) == -1)
4296555afedfScarlsonj 			ct = -1;
4297555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4298555afedfScarlsonj 		(void) close(tmpl_fd);
4299555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
4300555afedfScarlsonj 		(void) contract_abandon_id(ct);
4301555afedfScarlsonj 		return;
4302555afedfScarlsonj 	}
4303555afedfScarlsonj 
4304555afedfScarlsonj 	/* child enters zone and sets up index file */
4305555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
4306555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
4307555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4308555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4309555afedfScarlsonj 		    ZONE_CONFIG_GID);
4310555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4311555afedfScarlsonj 		    ZONE_INDEX_MODE);
4312555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4313555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4314555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
4315555afedfScarlsonj 				uuidstr[0] = '\0';
4316555afedfScarlsonj 			else
4317555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
4318555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4319555afedfScarlsonj 			    zone_state_str(zep->zone_state),
4320555afedfScarlsonj 			    uuidstr);
4321555afedfScarlsonj 			(void) fclose(zet);
4322555afedfScarlsonj 		}
4323555afedfScarlsonj 	}
4324555afedfScarlsonj 	_exit(0);
4325555afedfScarlsonj }
4326555afedfScarlsonj 
43277c478bd9Sstevel@tonic-gate int
4328555afedfScarlsonj vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd, zoneid_t zoneid)
43297c478bd9Sstevel@tonic-gate {
43309acbbeafSnn35248 	char zonepath[MAXPATHLEN];
43315749802bSdp 
4332fa9e4066Sahrens 	if (!mount_cmd && validate_datasets(zlogp) != 0) {
4333fa9e4066Sahrens 		lofs_discard_mnttab();
4334fa9e4066Sahrens 		return (-1);
4335fa9e4066Sahrens 	}
4336fa9e4066Sahrens 
43379acbbeafSnn35248 	/*
43389acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
43399acbbeafSnn35248 	 * attribute backing store for /dev
43409acbbeafSnn35248 	 */
43419acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
43429acbbeafSnn35248 		lofs_discard_mnttab();
43439acbbeafSnn35248 		return (-1);
43449acbbeafSnn35248 	}
434516bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
434627e6fb21Sdp 
434727e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
434827e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
434927e6fb21Sdp 	    0, 3) != 0) {
4350108322fbScarlsonj 		lofs_discard_mnttab();
43517c478bd9Sstevel@tonic-gate 		return (-1);
4352108322fbScarlsonj 	}
4353facf4a8dSllai1 
43549acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4355facf4a8dSllai1 		lofs_discard_mnttab();
4356facf4a8dSllai1 		return (-1);
4357facf4a8dSllai1 	}
4358facf4a8dSllai1 
4359f4b3ec61Sdh155122 	if (!mount_cmd) {
4360f4b3ec61Sdh155122 		zone_iptype_t iptype;
4361f4b3ec61Sdh155122 
4362f4b3ec61Sdh155122 		if (get_iptype(zlogp, &iptype) < 0) {
4363f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4364108322fbScarlsonj 			lofs_discard_mnttab();
43657c478bd9Sstevel@tonic-gate 			return (-1);
4366108322fbScarlsonj 		}
4367555afedfScarlsonj 
4368f4b3ec61Sdh155122 		switch (iptype) {
4369f4b3ec61Sdh155122 		case ZS_SHARED:
4370f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
4371f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
4372f4b3ec61Sdh155122 				lofs_discard_mnttab();
4373f4b3ec61Sdh155122 				return (-1);
4374f4b3ec61Sdh155122 			}
4375f4b3ec61Sdh155122 			break;
4376f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4377f4b3ec61Sdh155122 			if (configure_exclusive_network_interfaces(zlogp) !=
4378f4b3ec61Sdh155122 			    0) {
4379f4b3ec61Sdh155122 				lofs_discard_mnttab();
4380f4b3ec61Sdh155122 				return (-1);
4381f4b3ec61Sdh155122 			}
4382f4b3ec61Sdh155122 			break;
4383f4b3ec61Sdh155122 		}
4384f4b3ec61Sdh155122 	}
4385f4b3ec61Sdh155122 
4386555afedfScarlsonj 	write_index_file(zoneid);
4387555afedfScarlsonj 
4388108322fbScarlsonj 	lofs_discard_mnttab();
43897c478bd9Sstevel@tonic-gate 	return (0);
43907c478bd9Sstevel@tonic-gate }
43917c478bd9Sstevel@tonic-gate 
4392108322fbScarlsonj static int
4393108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
43947c478bd9Sstevel@tonic-gate {
4395108322fbScarlsonj 	char zroot[MAXPATHLEN];
4396108322fbScarlsonj 
439784561e8cStd153743 	assert(zone_isnative || zone_iscluster);
43989acbbeafSnn35248 
4399108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4400108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4401108322fbScarlsonj 		return (-1);
4402108322fbScarlsonj 	}
4403108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4404108322fbScarlsonj 
4405108322fbScarlsonj 	/*
4406108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
4407108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
4408108322fbScarlsonj 	 * still be creds floating about in the system that reference the
4409108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4410108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4411108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
4412108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4413108322fbScarlsonj 	 */
4414108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
4415108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4416108322fbScarlsonj 			goto unmounted;
4417108322fbScarlsonj 		if (errno == EBUSY) {
4418108322fbScarlsonj 			int tries = 10;
4419108322fbScarlsonj 
4420108322fbScarlsonj 			while (--tries >= 0) {
4421108322fbScarlsonj 				(void) sleep(1);
4422108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
4423108322fbScarlsonj 					goto unmounted;
4424108322fbScarlsonj 				if (errno != EBUSY)
4425108322fbScarlsonj 					break;
4426108322fbScarlsonj 			}
4427108322fbScarlsonj 		}
4428108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4429108322fbScarlsonj 		return (-1);
4430108322fbScarlsonj 	}
4431108322fbScarlsonj unmounted:
4432108322fbScarlsonj 
4433108322fbScarlsonj 	/*
4434108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
4435108322fbScarlsonj 	 * entries.
4436108322fbScarlsonj 	 */
4437108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4438108322fbScarlsonj 		FILE *fp;
4439108322fbScarlsonj 		int retv;
4440108322fbScarlsonj 
4441108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4442108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4443108322fbScarlsonj 			return (-1);
4444108322fbScarlsonj 		}
4445108322fbScarlsonj 		retv = -1;
4446108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
4447108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4448108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4449108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4450108322fbScarlsonj 		else
4451108322fbScarlsonj 			retv = 0;
4452108322fbScarlsonj 		zonecfg_close_scratch(fp);
4453108322fbScarlsonj 		return (retv);
4454108322fbScarlsonj 	} else {
4455108322fbScarlsonj 		return (0);
4456108322fbScarlsonj 	}
4457108322fbScarlsonj }
4458108322fbScarlsonj 
4459108322fbScarlsonj int
44600209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4461108322fbScarlsonj {
4462108322fbScarlsonj 	char *kzone;
44637c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
44640209230bSgjelinek 	int res;
44650209230bSgjelinek 	char pool_err[128];
44669acbbeafSnn35248 	char zroot[MAXPATHLEN];
44679acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
44689acbbeafSnn35248 	char brand[MAXNAMELEN];
4469123807fbSedp 	brand_handle_t bh = NULL;
4470f4b3ec61Sdh155122 	ushort_t flags;
44717c478bd9Sstevel@tonic-gate 
4472108322fbScarlsonj 	kzone = zone_name;
4473108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4474108322fbScarlsonj 		FILE *fp;
4475108322fbScarlsonj 
4476108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4477108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
4478108322fbScarlsonj 			goto error;
4479108322fbScarlsonj 		}
4480108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4481108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
4482108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4483108322fbScarlsonj 			zonecfg_close_scratch(fp);
4484108322fbScarlsonj 			goto error;
4485108322fbScarlsonj 		}
4486108322fbScarlsonj 		zonecfg_close_scratch(fp);
4487108322fbScarlsonj 		kzone = kernzone;
4488108322fbScarlsonj 	}
4489108322fbScarlsonj 
4490108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
44917c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
44927c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4493108322fbScarlsonj 		if (unmount_cmd)
4494108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
44957c478bd9Sstevel@tonic-gate 		goto error;
44967c478bd9Sstevel@tonic-gate 	}
44977c478bd9Sstevel@tonic-gate 
44987c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
44997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
45007c478bd9Sstevel@tonic-gate 		goto error;
45017c478bd9Sstevel@tonic-gate 	}
45027c478bd9Sstevel@tonic-gate 
45039acbbeafSnn35248 	/* Get the path to the root of this zone */
45049acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
45059acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone root");
45069acbbeafSnn35248 		goto error;
45079acbbeafSnn35248 	}
45089acbbeafSnn35248 
45099acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
45109acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
4511123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
45129acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
45139acbbeafSnn35248 		return (-1);
45149acbbeafSnn35248 	}
45159acbbeafSnn35248 	/*
45169acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
45179acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
45189acbbeafSnn35248 	 */
45199acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4520123807fbSedp 	if (brand_get_halt(bh, zone_name, zroot, cmdbuf + EXEC_LEN,
45219acbbeafSnn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) < 0) {
4522123807fbSedp 		brand_close(bh);
45239acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
45249acbbeafSnn35248 		    "halt callback.");
45259acbbeafSnn35248 		goto error;
45269acbbeafSnn35248 	}
4527123807fbSedp 	brand_close(bh);
45289acbbeafSnn35248 
45299acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
45309acbbeafSnn35248 	    (do_subproc(zlogp, cmdbuf) != Z_OK)) {
45319acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
45329acbbeafSnn35248 		goto error;
45339acbbeafSnn35248 	}
45349acbbeafSnn35248 
4535f4b3ec61Sdh155122 	if (!unmount_cmd) {
4536f4b3ec61Sdh155122 		zone_iptype_t iptype;
4537f4b3ec61Sdh155122 
4538f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4539f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
4540f4b3ec61Sdh155122 			if (get_iptype(zlogp, &iptype) < 0) {
4541f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
4542f4b3ec61Sdh155122 				    "ip-type");
45437c478bd9Sstevel@tonic-gate 				goto error;
45447c478bd9Sstevel@tonic-gate 			}
4545f4b3ec61Sdh155122 		} else {
4546f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
4547f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
4548f4b3ec61Sdh155122 			else
4549f4b3ec61Sdh155122 				iptype = ZS_SHARED;
4550f4b3ec61Sdh155122 		}
4551f4b3ec61Sdh155122 
4552f4b3ec61Sdh155122 		switch (iptype) {
4553f4b3ec61Sdh155122 		case ZS_SHARED:
4554f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
4555f4b3ec61Sdh155122 			    zoneid) != 0) {
4556f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4557f4b3ec61Sdh155122 				    "network interfaces in zone");
4558f4b3ec61Sdh155122 				goto error;
4559f4b3ec61Sdh155122 			}
4560f4b3ec61Sdh155122 			break;
4561f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4562f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
4563f4b3ec61Sdh155122 			    zoneid) != 0) {
4564f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4565f4b3ec61Sdh155122 				    "network interfaces in zone");
4566f4b3ec61Sdh155122 				goto error;
4567f4b3ec61Sdh155122 			}
4568f4b3ec61Sdh155122 			break;
4569f4b3ec61Sdh155122 		}
4570f4b3ec61Sdh155122 	}
45717c478bd9Sstevel@tonic-gate 
4572108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
45737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
45747c478bd9Sstevel@tonic-gate 		goto error;
45757c478bd9Sstevel@tonic-gate 	}
45767c478bd9Sstevel@tonic-gate 
4577facf4a8dSllai1 	/* destroy zconsole before umount /dev */
4578facf4a8dSllai1 	if (!unmount_cmd)
4579facf4a8dSllai1 		destroy_console_slave();
4580facf4a8dSllai1 
4581108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
45827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
45837c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
45847c478bd9Sstevel@tonic-gate 		goto error;
45857c478bd9Sstevel@tonic-gate 	}
45867c478bd9Sstevel@tonic-gate 
45870209230bSgjelinek 	/*
4588cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
4589cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
4590cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
4591cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
4592cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
4593cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
4594cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
4595cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
45960209230bSgjelinek 	 */
4597cf6b13d2Sgjelinek 	if (!unmount_cmd) {
4598cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
4599cf6b13d2Sgjelinek 
4600cf6b13d2Sgjelinek 		if (rebooting) {
4601cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
4602cf6b13d2Sgjelinek 			zone_dochandle_t handle;
4603cf6b13d2Sgjelinek 
4604cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
4605cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4606cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4607cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
4608e767a340Sgjelinek 
4609e767a340Sgjelinek 			zonecfg_fini_handle(handle);
4610cf6b13d2Sgjelinek 		}
4611cf6b13d2Sgjelinek 
4612cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
46130209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
46140209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
46150209230bSgjelinek 				if (res == Z_POOL)
46160209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
46170209230bSgjelinek 			}
46180209230bSgjelinek 		}
4619cf6b13d2Sgjelinek 	}
46200209230bSgjelinek 
462145916cd2Sjpk 	remove_mlps(zlogp, zoneid);
462245916cd2Sjpk 
46237c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
46247c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
46257c478bd9Sstevel@tonic-gate 		goto error;
46267c478bd9Sstevel@tonic-gate 	}
4627108322fbScarlsonj 
4628108322fbScarlsonj 	/*
4629108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
4630108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
4631108322fbScarlsonj 	 */
4632108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4633108322fbScarlsonj 		goto error;
4634108322fbScarlsonj 
4635108322fbScarlsonj 	lofs_discard_mnttab();
46367c478bd9Sstevel@tonic-gate 	return (0);
46377c478bd9Sstevel@tonic-gate 
46387c478bd9Sstevel@tonic-gate error:
4639108322fbScarlsonj 	lofs_discard_mnttab();
46407c478bd9Sstevel@tonic-gate 	return (-1);
46417c478bd9Sstevel@tonic-gate }
4642*9d48116cSdh155122 
4643*9d48116cSdh155122 /*
4644*9d48116cSdh155122  * Common routine for driver_hold_link and driver_rele_link.
4645*9d48116cSdh155122  * It invokes ioctl for a link like "ce*", for which the driver has been
4646*9d48116cSdh155122  * enhanced to support DLDIOC{HOLD,RELE}VLAN.
4647*9d48116cSdh155122  */
4648*9d48116cSdh155122 static int
4649*9d48116cSdh155122 driver_vlan_ioctl(const char *name, zoneid_t zoneid, int cmd)
4650*9d48116cSdh155122 {
4651*9d48116cSdh155122 	int		fd;
4652*9d48116cSdh155122 	uint_t		ppa;
4653*9d48116cSdh155122 	dld_hold_vlan_t	dhv;
4654*9d48116cSdh155122 	struct strioctl istr;
4655*9d48116cSdh155122 	char		providername[IFNAMSIZ];
4656*9d48116cSdh155122 	char		path[MAXPATHLEN];
4657*9d48116cSdh155122 
4658*9d48116cSdh155122 	if (strlen(name) >= IFNAMSIZ) {
4659*9d48116cSdh155122 		errno = EINVAL;
4660*9d48116cSdh155122 		return (-1);
4661*9d48116cSdh155122 	}
4662*9d48116cSdh155122 
4663*9d48116cSdh155122 	if (dlpi_parselink(name, providername, &ppa) != DLPI_SUCCESS) {
4664*9d48116cSdh155122 		errno = EINVAL;
4665*9d48116cSdh155122 		return (-1);
4666*9d48116cSdh155122 	}
4667*9d48116cSdh155122 
4668*9d48116cSdh155122 	(void) snprintf(path, sizeof (path), "/dev/%s", providername);
4669*9d48116cSdh155122 	fd = open(path, O_RDWR);
4670*9d48116cSdh155122 	if (fd < 0)
4671*9d48116cSdh155122 		return (-1);
4672*9d48116cSdh155122 	bzero(&dhv, sizeof (dld_hold_vlan_t));
4673*9d48116cSdh155122 	(void) strlcpy(dhv.dhv_name, name, IFNAMSIZ);
4674*9d48116cSdh155122 	dhv.dhv_zid = zoneid;
4675*9d48116cSdh155122 	dhv.dhv_docheck = B_FALSE;
4676*9d48116cSdh155122 
4677*9d48116cSdh155122 	istr.ic_cmd = cmd;
4678*9d48116cSdh155122 	istr.ic_len = sizeof (dhv);
4679*9d48116cSdh155122 	istr.ic_dp = (void *)&dhv;
4680*9d48116cSdh155122 	istr.ic_timout = 0;
4681*9d48116cSdh155122 
4682*9d48116cSdh155122 	if (ioctl(fd, I_STR, &istr) < 0) {
4683*9d48116cSdh155122 		int olderrno = errno;
4684*9d48116cSdh155122 		(void) close(fd);
4685*9d48116cSdh155122 		errno = olderrno;
4686*9d48116cSdh155122 		return (-1);
4687*9d48116cSdh155122 	}
4688*9d48116cSdh155122 	(void) close(fd);
4689*9d48116cSdh155122 	return (0);
4690*9d48116cSdh155122 }
4691*9d48116cSdh155122 
4692*9d48116cSdh155122 /*
4693*9d48116cSdh155122  * Hold a data-link where the style-2 datalink driver supports DLDIOCHOLDVLAN.
4694*9d48116cSdh155122  */
4695*9d48116cSdh155122 static int
4696*9d48116cSdh155122 driver_hold_link(const char *name, zoneid_t zoneid)
4697*9d48116cSdh155122 {
4698*9d48116cSdh155122 	return (driver_vlan_ioctl(name, zoneid, DLDIOCHOLDVLAN));
4699*9d48116cSdh155122 }
4700*9d48116cSdh155122 
4701*9d48116cSdh155122 /*
4702*9d48116cSdh155122  * Release a data-link where the style-2 datalink driver supports
4703*9d48116cSdh155122  * DLDIOC{HOLD,RELE}VLAN.
4704*9d48116cSdh155122  */
4705*9d48116cSdh155122 static int
4706*9d48116cSdh155122 driver_rele_link(const char *name, zoneid_t zoneid)
4707*9d48116cSdh155122 {
4708*9d48116cSdh155122 	return (driver_vlan_ioctl(name, zoneid, DLDIOCRELEVLAN));
4709*9d48116cSdh155122 }
4710