xref: /titanic_50/usr/src/cmd/zoneadmd/vplat.c (revision ff17c8bf86c3e567734be83f90267edee20f580f)
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 /*
236cfd72c6Sgjelinek  * Copyright 2008 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 <libdlpi.h>
78f595a68aSyz147064 #include <libdllink.h>
79d62bc4baSyz147064 #include <libdlvlan.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 
1257c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1267c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1297c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
13245916cd2Sjpk #define	MAXTNZLEN	2048
1337c478bd9Sstevel@tonic-gate 
1346cfd72c6Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1356cfd72c6Sgjelinek 
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 *);
1499d48116cSdh155122 
15045916cd2Sjpk static m_label_t *zlabel = NULL;
15145916cd2Sjpk static m_label_t *zid_label = NULL;
15245916cd2Sjpk static priv_set_t *zprivs = NULL;
15345916cd2Sjpk 
1547c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1557c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
158108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
159108322fbScarlsonj  * data) only once every N times through the loop.
160108322fbScarlsonj  */
161108322fbScarlsonj #define	MNTTAB_HUNK	32
162108322fbScarlsonj 
163108322fbScarlsonj /*
1647c478bd9Sstevel@tonic-gate  * Private autofs system call
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static int
1697c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
177108322fbScarlsonj static void
178108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
179108322fbScarlsonj {
180108322fbScarlsonj 	uint_t i;
181108322fbScarlsonj 
182108322fbScarlsonj 	if (mnt_array == NULL)
183108322fbScarlsonj 		return;
184108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
185108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
186108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
187108322fbScarlsonj 		free(mnt_array[i].mnt_special);
188108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
189108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
190108322fbScarlsonj 	}
191108322fbScarlsonj 	free(mnt_array);
192108322fbScarlsonj }
193108322fbScarlsonj 
194108322fbScarlsonj /*
195108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
196108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
197108322fbScarlsonj  * array in "nelemp".
198108322fbScarlsonj  */
199108322fbScarlsonj static int
200108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
201108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
202108322fbScarlsonj {
203108322fbScarlsonj 	struct mnttab mnt;
204108322fbScarlsonj 	struct mnttab *mnts;
205108322fbScarlsonj 	struct mnttab *mnp;
206108322fbScarlsonj 	uint_t nmnt;
207108322fbScarlsonj 
208108322fbScarlsonj 	rewind(mnttab);
209108322fbScarlsonj 	resetmnttab(mnttab);
210108322fbScarlsonj 	nmnt = 0;
211108322fbScarlsonj 	mnts = NULL;
212108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
213108322fbScarlsonj 		struct mnttab *tmp_array;
214108322fbScarlsonj 
215108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
216108322fbScarlsonj 			continue;
217108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
218108322fbScarlsonj 			tmp_array = realloc(mnts,
219108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
220108322fbScarlsonj 			if (tmp_array == NULL) {
221108322fbScarlsonj 				free_mnttable(mnts, nmnt);
222108322fbScarlsonj 				return (-1);
223108322fbScarlsonj 			}
224108322fbScarlsonj 			mnts = tmp_array;
225108322fbScarlsonj 		}
226108322fbScarlsonj 		mnp = &mnts[nmnt++];
227108322fbScarlsonj 
228108322fbScarlsonj 		/*
229108322fbScarlsonj 		 * Zero out any fields we're not using.
230108322fbScarlsonj 		 */
231108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
232108322fbScarlsonj 
233108322fbScarlsonj 		if (mnt.mnt_special != NULL)
234108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
235108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
236108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
237108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
238108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
239108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
240108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
241108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
242108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
243108322fbScarlsonj 			free_mnttable(mnts, nmnt);
244108322fbScarlsonj 			return (-1);
245108322fbScarlsonj 		}
246108322fbScarlsonj 	}
247108322fbScarlsonj 	*mnt_arrayp = mnts;
248108322fbScarlsonj 	*nelemp = nmnt;
249108322fbScarlsonj 	return (0);
250108322fbScarlsonj }
251108322fbScarlsonj 
252108322fbScarlsonj /*
253108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
254108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
255108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
256108322fbScarlsonj  * reread of the list of mount points
257108322fbScarlsonj  */
258108322fbScarlsonj static void
259108322fbScarlsonj lofs_discard_mnttab(void)
260108322fbScarlsonj {
261108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
262108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
263108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
264108322fbScarlsonj }
265108322fbScarlsonj 
266108322fbScarlsonj static int
267108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
268108322fbScarlsonj {
269108322fbScarlsonj 	FILE *mnttab;
270108322fbScarlsonj 	uint_t nmnts;
271108322fbScarlsonj 
272108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
273108322fbScarlsonj 		return (-1);
274108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
275108322fbScarlsonj 	    &nmnts) == -1) {
276108322fbScarlsonj 		(void) fclose(mnttab);
277108322fbScarlsonj 		return (-1);
278108322fbScarlsonj 	}
279108322fbScarlsonj 	(void) fclose(mnttab);
280108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
281108322fbScarlsonj 	return (0);
282108322fbScarlsonj }
283108322fbScarlsonj 
284108322fbScarlsonj /*
285108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
286108322fbScarlsonj  * path and resolves them all down to an absolute path.
287108322fbScarlsonj  */
288910f48daSedp void
289108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
290108322fbScarlsonj {
291108322fbScarlsonj 	int len, arlen;
292108322fbScarlsonj 	const char *altroot;
293108322fbScarlsonj 	char tmppath[MAXPATHLEN];
294108322fbScarlsonj 	boolean_t outside_altroot;
295108322fbScarlsonj 
296108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
297108322fbScarlsonj 		return;
298108322fbScarlsonj 	tmppath[len] = '\0';
299108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
300108322fbScarlsonj 
301108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
302108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
303108322fbScarlsonj 		return;
304108322fbScarlsonj 
305108322fbScarlsonj 	altroot = zonecfg_get_root();
306108322fbScarlsonj 	arlen = strlen(altroot);
307108322fbScarlsonj 	outside_altroot = B_FALSE;
308108322fbScarlsonj 	for (;;) {
309108322fbScarlsonj 		struct mnttab *mnp;
310108322fbScarlsonj 
3111e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3121e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3131e9d8f9fSdminer 		    mnp--) {
314108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
315108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3161e9d8f9fSdminer 			    mnp->mnt_special == NULL)
317108322fbScarlsonj 				continue;
318108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
319108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
320108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
321108322fbScarlsonj 				break;
322108322fbScarlsonj 		}
3231e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3241e9d8f9fSdminer 			break;
3251e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3261e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
327108322fbScarlsonj 			break;
328108322fbScarlsonj 		if (outside_altroot) {
329108322fbScarlsonj 			char *cp;
330108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
331108322fbScarlsonj 
332108322fbScarlsonj 			/*
333108322fbScarlsonj 			 * If we run into a read-only mount outside of the
334108322fbScarlsonj 			 * alternate root environment, then the user doesn't
335108322fbScarlsonj 			 * want this path to be made read-write.
336108322fbScarlsonj 			 */
337108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
338108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
339108322fbScarlsonj 			    NULL &&
340108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
341108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
342108322fbScarlsonj 				break;
343108322fbScarlsonj 			}
344108322fbScarlsonj 		} else if (arlen > 0 &&
345108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
346108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
347108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
348108322fbScarlsonj 			outside_altroot = B_TRUE;
349108322fbScarlsonj 		}
350108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
351108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
352108322fbScarlsonj 		    mnp->mnt_special, path + len);
353108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
354108322fbScarlsonj 			break;
355108322fbScarlsonj 		path[len] = '\0';
356108322fbScarlsonj 	}
357108322fbScarlsonj }
358108322fbScarlsonj 
359108322fbScarlsonj /*
360108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
361108322fbScarlsonj  * referenced device is already mounted somewhere.
362108322fbScarlsonj  */
363108322fbScarlsonj static int
364108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
365108322fbScarlsonj {
366108322fbScarlsonj 	struct mnttab *mnp;
367108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
368108322fbScarlsonj 
369108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
370108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
371108322fbScarlsonj 		return (-1);
372108322fbScarlsonj 
373108322fbScarlsonj 	/*
374108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
375108322fbScarlsonj 	 * no need to worry about conflicting mounts.
376108322fbScarlsonj 	 */
377108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
378108322fbScarlsonj 	    mnp++) {
379108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
380108322fbScarlsonj 			break;
381108322fbScarlsonj 	}
382108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
383108322fbScarlsonj 		return (0);
384108322fbScarlsonj 
385108322fbScarlsonj 	/*
386108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
387108322fbScarlsonj 	 */
388108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
389108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
390108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
391108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
392108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
393108322fbScarlsonj 
394108322fbScarlsonj 	/*
395108322fbScarlsonj 	 * Discard all but one of the original options and set that to be the
396108322fbScarlsonj 	 * same set of options used for inherit package directory resources.
397108322fbScarlsonj 	 */
398108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
399108322fbScarlsonj 	if (optptr == NULL) {
400108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
401108322fbScarlsonj 		if (optptr == NULL) {
402108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
403108322fbScarlsonj 			    fsptr->zone_fs_dir);
404108322fbScarlsonj 			return (-1);
405108322fbScarlsonj 		}
406108322fbScarlsonj 	} else {
407108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
408108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
409108322fbScarlsonj 			free(onext);
410108322fbScarlsonj 		}
411108322fbScarlsonj 	}
412108322fbScarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
413108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
414108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
415108322fbScarlsonj 	return (0);
416108322fbScarlsonj }
417108322fbScarlsonj 
418d314f035Sedp int
41927e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
42027e6fb21Sdp     uid_t userid, gid_t groupid)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4237c478bd9Sstevel@tonic-gate 	struct stat st;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4267c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4287c478bd9Sstevel@tonic-gate 		    subdir);
4297c478bd9Sstevel@tonic-gate 		return (-1);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4337c478bd9Sstevel@tonic-gate 		/*
4347c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4357c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4367c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4377c478bd9Sstevel@tonic-gate 		 */
4387c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
43945916cd2Sjpk 			if (is_system_labeled() &&
44045916cd2Sjpk 			    S_ISREG(st.st_mode)) {
44145916cd2Sjpk 				/*
44245916cd2Sjpk 				 * The need to mount readonly copies of
44345916cd2Sjpk 				 * global zone /etc/ files is unique to
44445916cd2Sjpk 				 * Trusted Extensions.
44545916cd2Sjpk 				 */
44645916cd2Sjpk 				if (strncmp(subdir, "/etc/",
44745916cd2Sjpk 				    strlen("/etc/")) != 0) {
44845916cd2Sjpk 					zerror(zlogp, B_FALSE,
44945916cd2Sjpk 					    "%s is not in /etc", path);
4507c478bd9Sstevel@tonic-gate 					return (-1);
4517c478bd9Sstevel@tonic-gate 				}
45245916cd2Sjpk 			} else {
45345916cd2Sjpk 				zerror(zlogp, B_FALSE,
45445916cd2Sjpk 				    "%s is not a directory", path);
45545916cd2Sjpk 				return (-1);
45645916cd2Sjpk 			}
45745916cd2Sjpk 		}
45827e6fb21Sdp 		return (0);
45927e6fb21Sdp 	}
46027e6fb21Sdp 
46127e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
4627c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4637c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4647c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4657c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4667c478bd9Sstevel@tonic-gate 		else
4677c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4687c478bd9Sstevel@tonic-gate 		return (-1);
4697c478bd9Sstevel@tonic-gate 	}
47027e6fb21Sdp 
47127e6fb21Sdp 	(void) chown(path, userid, groupid);
4727c478bd9Sstevel@tonic-gate 	return (0);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate static void
4767c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	uint_t i;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (types == NULL)
4817c478bd9Sstevel@tonic-gate 		return;
4827c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
4837c478bd9Sstevel@tonic-gate 		free(types[i]);
4847c478bd9Sstevel@tonic-gate 	free(types);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate static char **
4887c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	char **types = NULL;
4917c478bd9Sstevel@tonic-gate 	FILE *fp;
4927c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
4937c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
4947c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
4957c478bd9Sstevel@tonic-gate 	uint_t i;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
4987c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
4997c478bd9Sstevel@tonic-gate 		return (NULL);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	/*
5027c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5057c478bd9Sstevel@tonic-gate 		lines++;
5067c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5077c478bd9Sstevel@tonic-gate 		goto out;
5087c478bd9Sstevel@tonic-gate 	rewind(fp);
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5117c478bd9Sstevel@tonic-gate 	 */
5127c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5137c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5157c478bd9Sstevel@tonic-gate 		goto out;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	i = 0;
5187c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5197c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5207c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5217c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5227c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5237c478bd9Sstevel@tonic-gate 			types = NULL;
5247c478bd9Sstevel@tonic-gate 			goto out;
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5277c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5287c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5297c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5307c478bd9Sstevel@tonic-gate 			types = NULL;
5317c478bd9Sstevel@tonic-gate 			goto out;
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 		i++;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate out:
5367c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5377c478bd9Sstevel@tonic-gate 	return (types);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate static boolean_t
5417c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate 	uint_t i;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5467c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5477c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5487c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5497c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
554108322fbScarlsonj /*
555108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
556108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
557108322fbScarlsonj  */
5587c478bd9Sstevel@tonic-gate static void
559108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5607c478bd9Sstevel@tonic-gate {
56184561e8cStd153743 	assert(zone_isnative || zone_iscluster);
5629acbbeafSnn35248 
563108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
564108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
565108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5707c478bd9Sstevel@tonic-gate  *
5717c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5727c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5737c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5747c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5757c478bd9Sstevel@tonic-gate  *
5767c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5777c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5787c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5797c478bd9Sstevel@tonic-gate  *
5807c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5817c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5827c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5857c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5867c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5877c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5887c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5897c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5907c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5917c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5927c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5937c478bd9Sstevel@tonic-gate  * graceful mounts first.
5947c478bd9Sstevel@tonic-gate  *
5957c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5967c478bd9Sstevel@tonic-gate  */
5977c478bd9Sstevel@tonic-gate static int
598108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	int error = 0;
6017c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6027c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6037c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6047c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6057c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6067c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6077c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6087c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6117c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6127c478bd9Sstevel@tonic-gate 		return (-1);
6137c478bd9Sstevel@tonic-gate 	}
614108322fbScarlsonj 	if (unmount_cmd)
615108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6187c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6197c478bd9Sstevel@tonic-gate 
62045916cd2Sjpk 	/*
62145916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
62245916cd2Sjpk 	 * of our zone's /export/home
62345916cd2Sjpk 	 */
62448451833Scarlsonj 	if (!unmount_cmd)
62545916cd2Sjpk 		tsol_unmounts(zlogp, zone_name);
62645916cd2Sjpk 
6277c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6297c478bd9Sstevel@tonic-gate 		return (-1);
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6337c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6377c478bd9Sstevel@tonic-gate 		error++;
6387c478bd9Sstevel@tonic-gate 		goto out;
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6437c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6447c478bd9Sstevel@tonic-gate 	 */
6457c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6467c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
6477c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6487c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6497c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6507c478bd9Sstevel@tonic-gate 		char *path;
6517c478bd9Sstevel@tonic-gate 		uint_t i;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		mnts = NULL;
6547c478bd9Sstevel@tonic-gate 		nmnt = 0;
6557c478bd9Sstevel@tonic-gate 		/*
6567c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6577c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6587c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6597c478bd9Sstevel@tonic-gate 		 * filesystems.
6607c478bd9Sstevel@tonic-gate 		 */
6617c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6627c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
6637c478bd9Sstevel@tonic-gate 			error++;
6647c478bd9Sstevel@tonic-gate 			goto out;
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6677c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6687c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6697c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
6707c478bd9Sstevel@tonic-gate 			/*
6717c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6727c478bd9Sstevel@tonic-gate 			 *
6737c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6747c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6757c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
6767c478bd9Sstevel@tonic-gate 			 */
6777c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6787c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6797c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6807c478bd9Sstevel@tonic-gate 			}
6817c478bd9Sstevel@tonic-gate 			/*
6827c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6837c478bd9Sstevel@tonic-gate 			 */
6847c478bd9Sstevel@tonic-gate 			if (stuck) {
6857c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6867c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
6877c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
6887c478bd9Sstevel@tonic-gate 				} else {
6897c478bd9Sstevel@tonic-gate 					/*
6907c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
6917c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
6927c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
6937c478bd9Sstevel@tonic-gate 					 * bail.
6947c478bd9Sstevel@tonic-gate 					 */
6957c478bd9Sstevel@tonic-gate 					error++;
6967c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6977c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
6987c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
6997c478bd9Sstevel@tonic-gate 					goto out;
7007c478bd9Sstevel@tonic-gate 				}
7017c478bd9Sstevel@tonic-gate 			}
7027c478bd9Sstevel@tonic-gate 			/*
7037c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7047c478bd9Sstevel@tonic-gate 			 */
7057c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7067c478bd9Sstevel@tonic-gate 				newcount++;
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7137c478bd9Sstevel@tonic-gate 			/*
7147c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7157c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7167c478bd9Sstevel@tonic-gate 			 */
7177c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		/*
7227c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7237c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7247c478bd9Sstevel@tonic-gate 		 */
7257c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7267c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7277c478bd9Sstevel@tonic-gate 			error++;
7287c478bd9Sstevel@tonic-gate 			goto out;
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate out:
7337c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7347c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7357c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate static int
7397c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7427c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate /*
7487c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7497c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7507c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7517c478bd9Sstevel@tonic-gate  *
7527c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7537c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7547c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7557c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7567c478bd9Sstevel@tonic-gate  * remains the case.
7577c478bd9Sstevel@tonic-gate  */
7587c478bd9Sstevel@tonic-gate static int
7597c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	pid_t child_pid;
7627c478bd9Sstevel@tonic-gate 	int child_status = 0;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7667c478bd9Sstevel@tonic-gate 	 */
7677c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7687c478bd9Sstevel@tonic-gate 	child_pid = fork();
7697c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7707c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
7717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7727c478bd9Sstevel@tonic-gate 		return (-1);
7737c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
7747c478bd9Sstevel@tonic-gate 		closefrom(0);
7751390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7761390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7771390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7781390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7797c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
7807c478bd9Sstevel@tonic-gate 		/*
7817c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7827c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7837c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7847c478bd9Sstevel@tonic-gate 		 * accordingly.
7857c478bd9Sstevel@tonic-gate 		 */
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7887c478bd9Sstevel@tonic-gate 	} else {
7897c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7947c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7957c478bd9Sstevel@tonic-gate 		return (-1);
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
7987c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
7997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8007c478bd9Sstevel@tonic-gate 		return (-1);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate static int
80693239addSjohnlev isregfile(const char *path)
80793239addSjohnlev {
80893239addSjohnlev 	struct stat64 st;
80993239addSjohnlev 
81093239addSjohnlev 	if (stat64(path, &st) == -1)
81193239addSjohnlev 		return (-1);
81293239addSjohnlev 
81393239addSjohnlev 	return (S_ISREG(st.st_mode));
81493239addSjohnlev }
81593239addSjohnlev 
81693239addSjohnlev static int
8177c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8207c478bd9Sstevel@tonic-gate 	char *argv[4];
8217c478bd9Sstevel@tonic-gate 	int status;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	/*
8247c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8257c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8267c478bd9Sstevel@tonic-gate 	 */
8277c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8289acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8307c478bd9Sstevel@tonic-gate 		return (-1);
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 
83393239addSjohnlev 	/*
83493239addSjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
83593239addSjohnlev 	 * in zoneadm.
83693239addSjohnlev 	 */
83793239addSjohnlev 	if (isregfile(cmdbuf) == -1)
83893239addSjohnlev 		return (0);
83993239addSjohnlev 
8407c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
8417c478bd9Sstevel@tonic-gate 	argv[1] = "-m";
8427c478bd9Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
8437c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8467c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8477c478bd9Sstevel@tonic-gate 		return (status);
8487c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8497c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8507c478bd9Sstevel@tonic-gate 	return (-1);
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate static int
8547c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8557c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8587c478bd9Sstevel@tonic-gate 	char *argv[6];
8597c478bd9Sstevel@tonic-gate 	int status;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	/*
8627c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8637c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8647c478bd9Sstevel@tonic-gate 	 */
8657c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8669acbbeafSnn35248 	    >= sizeof (cmdbuf)) {
8677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8687c478bd9Sstevel@tonic-gate 		return (-1);
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
8717c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
8727c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
8737c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
8747c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
8757c478bd9Sstevel@tonic-gate 	} else {
8767c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
8777c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
8787c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
8797c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
8807c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8847c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8857c478bd9Sstevel@tonic-gate 		return (status);
8867c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
8877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8887c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8897c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8907c478bd9Sstevel@tonic-gate 	else
8917c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8927c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
8937c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8947c478bd9Sstevel@tonic-gate 	return (-1);
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*
898d314f035Sedp  * Check if a given mount point path exists.
899d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
900d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
901d314f035Sedp  * component of the mount point path, so it must be a directory.
902d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
903d314f035Sedp  * path, so the mount point itself can be anything aside from a
904d314f035Sedp  * symbolic link.
905d314f035Sedp  *
906d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
907d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
908d314f035Sedp  * If the path doesn't exist return a positive value.
9097c478bd9Sstevel@tonic-gate  */
9107c478bd9Sstevel@tonic-gate static int
911d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9127c478bd9Sstevel@tonic-gate {
9137c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9147c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9157c478bd9Sstevel@tonic-gate 	int res;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9187c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
919d314f035Sedp 			return (1);
9207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9217c478bd9Sstevel@tonic-gate 		return (-1);
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9247c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9257c478bd9Sstevel@tonic-gate 		return (-1);
9267c478bd9Sstevel@tonic-gate 	}
927d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9297c478bd9Sstevel@tonic-gate 		return (-1);
9307c478bd9Sstevel@tonic-gate 	}
9317c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9327c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9337c478bd9Sstevel@tonic-gate 		return (-1);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9367c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9377c478bd9Sstevel@tonic-gate 		/*
938d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9397c478bd9Sstevel@tonic-gate 		 */
9407c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9417c478bd9Sstevel@tonic-gate 		return (-1);
9427c478bd9Sstevel@tonic-gate 	}
9437c478bd9Sstevel@tonic-gate 	return (0);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
947d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
948d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
949d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
950d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
951d314f035Sedp  * object.  (Different filesystems can support different mount points,
952d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
953d314f035Sedp  * while "ufs" just supports directories.)
9547c478bd9Sstevel@tonic-gate  *
955d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
956d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
957d314f035Sedp  * If the path doesn't exist return a positive value.
9587c478bd9Sstevel@tonic-gate  */
959d314f035Sedp int
960d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
961d314f035Sedp     const char *dir, const char *fstype)
9627c478bd9Sstevel@tonic-gate {
963d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
964d314f035Sedp 	int rv;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	/*
967d314f035Sedp 	 * Sanity check the target mount point path.
968d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
9697c478bd9Sstevel@tonic-gate 	 */
970d314f035Sedp 	if (dir[0] != '/') {
971d314f035Sedp 		if (spec[0] == '\0') {
972d314f035Sedp 			/*
973d314f035Sedp 			 * This must be an invalid ipd entry (see comments
974d314f035Sedp 			 * in mount_filesystems_ipdent()).
975d314f035Sedp 			 */
976d314f035Sedp 			zerror(zlogp, B_FALSE,
977d314f035Sedp 			    "invalid inherit-pkg-dir entry: \"%s\"", dir);
978d314f035Sedp 		} else {
979d314f035Sedp 			/* Something went wrong. */
980d314f035Sedp 			zerror(zlogp, B_FALSE, "invalid mount directory, "
981d314f035Sedp 			    "type: \"%s\", special: \"%s\", dir: \"%s\"",
982d314f035Sedp 			    fstype, spec, dir);
983d314f035Sedp 		}
984d314f035Sedp 		return (-1);
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 
987d314f035Sedp 	/*
988d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
989d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
990d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
991d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
992d314f035Sedp 	 * cannonical path check in valid_mount_point()).
993d314f035Sedp 	 */
994d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
995d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
996d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
997d314f035Sedp 		    rootpath, dir);
998d314f035Sedp 		return (-1);
999d314f035Sedp 	}
1000d314f035Sedp 
1001d314f035Sedp 	/*
1002d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
1003d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
1004d314f035Sedp 	 */
10057c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10067c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10077c478bd9Sstevel@tonic-gate 	do {
1008d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
10097c478bd9Sstevel@tonic-gate 		*slashp = '\0';
1010d314f035Sedp 		if (slashp_next != NULL) {
1011d314f035Sedp 			/* This is an intermediary mount path component. */
1012d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1013d314f035Sedp 		} else {
1014d314f035Sedp 			/* This is the last component of the mount path. */
1015d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1016d314f035Sedp 		}
1017d314f035Sedp 		if (rv < 0)
1018d314f035Sedp 			return (rv);
10197c478bd9Sstevel@tonic-gate 		*slashp = '/';
1020d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1021d314f035Sedp 	return (rv);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate static int
10259acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10269acbbeafSnn35248 {
10279acbbeafSnn35248 	di_prof_t prof = arg;
10289acbbeafSnn35248 
10299acbbeafSnn35248 	if (name == NULL)
10309acbbeafSnn35248 		return (di_prof_add_dev(prof, match));
10319acbbeafSnn35248 	return (di_prof_add_map(prof, match, name));
10329acbbeafSnn35248 }
10339acbbeafSnn35248 
10349acbbeafSnn35248 static int
10359acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10369acbbeafSnn35248 {
10379acbbeafSnn35248 	di_prof_t prof = arg;
10389acbbeafSnn35248 
10399acbbeafSnn35248 	return (di_prof_add_symlink(prof, source, target));
10409acbbeafSnn35248 }
10419acbbeafSnn35248 
1042f4b3ec61Sdh155122 static int
1043f4b3ec61Sdh155122 get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1044f4b3ec61Sdh155122 {
1045f4b3ec61Sdh155122 	zone_dochandle_t handle;
1046f4b3ec61Sdh155122 
1047f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
1048f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1049f4b3ec61Sdh155122 		return (-1);
1050f4b3ec61Sdh155122 	}
1051f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1052f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
1053f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1054f4b3ec61Sdh155122 		return (-1);
1055f4b3ec61Sdh155122 	}
1056f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1057f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1058f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
1059f4b3ec61Sdh155122 		return (-1);
1060f4b3ec61Sdh155122 	}
1061f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
1062f4b3ec61Sdh155122 	return (0);
1063f4b3ec61Sdh155122 }
1064f4b3ec61Sdh155122 
10659acbbeafSnn35248 /*
10669acbbeafSnn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10679acbbeafSnn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10689acbbeafSnn35248  * use these as a profile/filter to determine what exists in /dev.
10699acbbeafSnn35248  */
10709acbbeafSnn35248 static int
10719acbbeafSnn35248 mount_one_dev(zlog_t *zlogp, char *devpath)
10729acbbeafSnn35248 {
10739acbbeafSnn35248 	char			brand[MAXNAMELEN];
10749acbbeafSnn35248 	zone_dochandle_t	handle = NULL;
1075123807fbSedp 	brand_handle_t		bh = NULL;
10769acbbeafSnn35248 	struct zone_devtab	ztab;
10779acbbeafSnn35248 	di_prof_t		prof = NULL;
10789acbbeafSnn35248 	int			err;
10799acbbeafSnn35248 	int			retval = -1;
1080f4b3ec61Sdh155122 	zone_iptype_t		iptype;
1081f4b3ec61Sdh155122 	const char 		*curr_iptype;
10829acbbeafSnn35248 
10839acbbeafSnn35248 	if (di_prof_init(devpath, &prof)) {
10849acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10859acbbeafSnn35248 		goto cleanup;
10869acbbeafSnn35248 	}
10879acbbeafSnn35248 
10889acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
10899acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
1090123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
10919acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
10929acbbeafSnn35248 		goto cleanup;
10939acbbeafSnn35248 	}
10949acbbeafSnn35248 
1095f4b3ec61Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
1096f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1097f4b3ec61Sdh155122 		goto cleanup;
1098f4b3ec61Sdh155122 	}
1099f4b3ec61Sdh155122 	switch (iptype) {
1100f4b3ec61Sdh155122 	case ZS_SHARED:
1101f4b3ec61Sdh155122 		curr_iptype = "shared";
1102f4b3ec61Sdh155122 		break;
1103f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1104f4b3ec61Sdh155122 		curr_iptype = "exclusive";
1105f4b3ec61Sdh155122 		break;
1106f4b3ec61Sdh155122 	}
1107f4b3ec61Sdh155122 
1108123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1109f4b3ec61Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11109acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11119acbbeafSnn35248 		goto cleanup;
11129acbbeafSnn35248 	}
11139acbbeafSnn35248 
1114123807fbSedp 	if (brand_platform_iter_link(bh,
11159acbbeafSnn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11169acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11179acbbeafSnn35248 		goto cleanup;
11189acbbeafSnn35248 	}
11199acbbeafSnn35248 
11209acbbeafSnn35248 	/* Add user-specified devices and directories */
11219acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11229acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11239acbbeafSnn35248 		goto cleanup;
11249acbbeafSnn35248 	}
11259acbbeafSnn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11269acbbeafSnn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11279acbbeafSnn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11289acbbeafSnn35248 		goto cleanup;
11299acbbeafSnn35248 	}
11309acbbeafSnn35248 	if (err = zonecfg_setdevent(handle)) {
11319acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11329acbbeafSnn35248 		    zonecfg_strerror(err));
11339acbbeafSnn35248 		goto cleanup;
11349acbbeafSnn35248 	}
11359acbbeafSnn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11369acbbeafSnn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11379acbbeafSnn35248 			zerror(zlogp, B_TRUE, "failed to add "
11389acbbeafSnn35248 			    "user-specified device");
11399acbbeafSnn35248 			goto cleanup;
11409acbbeafSnn35248 		}
11419acbbeafSnn35248 	}
11429acbbeafSnn35248 	(void) zonecfg_enddevent(handle);
11439acbbeafSnn35248 
11449acbbeafSnn35248 	/* Send profile to kernel */
11459acbbeafSnn35248 	if (di_prof_commit(prof)) {
11469acbbeafSnn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11479acbbeafSnn35248 		goto cleanup;
11489acbbeafSnn35248 	}
11499acbbeafSnn35248 
11509acbbeafSnn35248 	retval = 0;
11519acbbeafSnn35248 
11529acbbeafSnn35248 cleanup:
1153123807fbSedp 	if (bh != NULL)
1154123807fbSedp 		brand_close(bh);
1155e767a340Sgjelinek 	if (handle != NULL)
11569acbbeafSnn35248 		zonecfg_fini_handle(handle);
11579acbbeafSnn35248 	if (prof)
11589acbbeafSnn35248 		di_prof_fini(prof);
11599acbbeafSnn35248 	return (retval);
11609acbbeafSnn35248 }
11619acbbeafSnn35248 
11629acbbeafSnn35248 static int
11637c478bd9Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
11647c478bd9Sstevel@tonic-gate {
11657c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
1166108322fbScarlsonj 	char specpath[MAXPATHLEN];
11677c478bd9Sstevel@tonic-gate 	char optstr[MAX_MNTOPT_STR];
11687c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11699acbbeafSnn35248 	int rv;
11707c478bd9Sstevel@tonic-gate 
1171d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1172d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11747c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11757c478bd9Sstevel@tonic-gate 		return (-1);
1176d314f035Sedp 	} else if (rv > 0) {
1177d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1178d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1179d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1180d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1181d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1182d314f035Sedp 			return (-1);
11837c478bd9Sstevel@tonic-gate 		}
11847c478bd9Sstevel@tonic-gate 
1185d314f035Sedp 		/*
1186d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1187d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1188d314f035Sedp 		 * to make sure that the mount point path is canonical,
1189d314f035Sedp 		 * which it can only do if the path exists, so now that
1190d314f035Sedp 		 * we've created the path we have to verify it again.
1191d314f035Sedp 		 */
1192d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1193d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1194d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1195d314f035Sedp 			zerror(zlogp, B_FALSE,
1196d314f035Sedp 			    "%s%s is not a valid mount point",
1197d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
11987c478bd9Sstevel@tonic-gate 			return (-1);
1199d314f035Sedp 		}
1200d314f035Sedp 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12037c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
12067c478bd9Sstevel@tonic-gate 		/*
12077c478bd9Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1208108322fbScarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1209108322fbScarlsonj 		 * can be seen via the alternate zone's root.
12107c478bd9Sstevel@tonic-gate 		 */
1211108322fbScarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1212108322fbScarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1213108322fbScarlsonj 		    sizeof (specpath)) {
1214108322fbScarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1215108322fbScarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1216108322fbScarlsonj 			return (-1);
1217108322fbScarlsonj 		}
1218108322fbScarlsonj 		if (zonecfg_in_alt_root())
1219108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
12207c478bd9Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1221108322fbScarlsonj 		    specpath, path) != 0) {
12227c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1223108322fbScarlsonj 			    specpath);
12247c478bd9Sstevel@tonic-gate 			return (-1);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 		return (0);
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	/*
12307c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12317c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12327c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12337c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12347c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12357c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12367c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12377c478bd9Sstevel@tonic-gate 	 */
12387c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12397c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12407c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12417c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12427c478bd9Sstevel@tonic-gate 		return (-1);
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/*
1246108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1247fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1248fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1249fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1250fa3d7ae1Sedp 	 * time we get here.
1251108322fbScarlsonj 	 */
1252108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1253108322fbScarlsonj 		struct stat64 st;
1254108322fbScarlsonj 
1255108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
125668eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1257fa3d7ae1Sedp 			/*
1258fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1259fa3d7ae1Sedp 			 * to check if that device is already mounted
1260fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1261fa3d7ae1Sedp 			 * of the device instead of a direct mount
1262fa3d7ae1Sedp 			 */
126368eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
1264108322fbScarlsonj 				return (-1);
126568eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1266fa3d7ae1Sedp 			/*
1267fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1268fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1269fa3d7ae1Sedp 			 * this case in order to get at the underlying
1270fa3d7ae1Sedp 			 * read-write path.
1271fa3d7ae1Sedp 			 */
1272fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1273108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1274108322fbScarlsonj 		}
1275108322fbScarlsonj 	}
1276108322fbScarlsonj 
1277108322fbScarlsonj 	/*
12787c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12797c478bd9Sstevel@tonic-gate 	 */
12807c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
128193239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12827c478bd9Sstevel@tonic-gate 		return (-1);
128393239addSjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
128493239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
128593239addSjohnlev 		return (-1);
128693239addSjohnlev 	}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
12907c478bd9Sstevel@tonic-gate 	 */
12917c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
12927c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
12937c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12947c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
12957c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
12967c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
12977c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
12987c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
12997c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
13007c478bd9Sstevel@tonic-gate 		}
13017c478bd9Sstevel@tonic-gate 	}
13029acbbeafSnn35248 
13039acbbeafSnn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13049acbbeafSnn35248 	    fsptr->zone_fs_special, path)) != 0)
13059acbbeafSnn35248 		return (rv);
13069acbbeafSnn35248 
13079acbbeafSnn35248 	/*
13089acbbeafSnn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13099acbbeafSnn35248 	 * we're done.
13109acbbeafSnn35248 	 */
13119acbbeafSnn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13129acbbeafSnn35248 		return (0);
13139acbbeafSnn35248 
13149acbbeafSnn35248 	/*
13159acbbeafSnn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13169acbbeafSnn35248 	 * need to configure it.
13179acbbeafSnn35248 	 */
13189acbbeafSnn35248 	return (mount_one_dev(zlogp, path));
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate static void
13227c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13237c478bd9Sstevel@tonic-gate {
13247c478bd9Sstevel@tonic-gate 	uint_t i;
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13277c478bd9Sstevel@tonic-gate 		return;
13287c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13297c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13307c478bd9Sstevel@tonic-gate 	free(fsarray);
13317c478bd9Sstevel@tonic-gate }
13327c478bd9Sstevel@tonic-gate 
1333108322fbScarlsonj /*
1334f4368d3dSvp157776  * This function initiates the creation of a small Solaris Environment for
1335f4368d3dSvp157776  * scratch zone. The Environment creation process is split up into two
1336f4368d3dSvp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1337f4368d3dSvp157776  * is done this way because:
1338f4368d3dSvp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
1339f4368d3dSvp157776  * 	We loopback mount zone's own /etc and /var into the root of the
1340f4368d3dSvp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1341f4368d3dSvp157776  * 	need to delay the mount of /var till the zone's root gets populated.
1342f4368d3dSvp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
1343f4368d3dSvp157776  * 	build_mounted_post_var() which gets called only after the zone
1344f4368d3dSvp157776  * 	specific filesystems are mounted.
13456cfd72c6Sgjelinek  *
13466cfd72c6Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13476cfd72c6Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13486cfd72c6Sgjelinek  * scratch zone.
1349108322fbScarlsonj  */
1350108322fbScarlsonj static boolean_t
1351f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
135216bca938Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1353108322fbScarlsonj {
1354108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1355108322fbScarlsonj 	const char **cpp;
1356108322fbScarlsonj 	static const char *mkdirs[] = {
13573f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13583f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1359108322fbScarlsonj 	};
1360108322fbScarlsonj 	char *altstr;
1361f4368d3dSvp157776 	FILE *fp;
1362108322fbScarlsonj 	uuid_t uuid;
1363108322fbScarlsonj 
136484561e8cStd153743 	assert(zone_isnative || zone_iscluster);
13659acbbeafSnn35248 
1366108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
136716bca938Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
136816bca938Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1369108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1370108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1371108322fbScarlsonj 
1372108322fbScarlsonj 	/*
1373108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1374108322fbScarlsonj 	 * zone_mount_early.)
1375108322fbScarlsonj 	 */
1376108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1377108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1378108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1379108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1380108322fbScarlsonj 			return (B_FALSE);
1381108322fbScarlsonj 		}
1382108322fbScarlsonj 	}
1383f4368d3dSvp157776 	/*
1384f4368d3dSvp157776 	 * This is here to support lucopy.  If there's an instance of this same
1385f4368d3dSvp157776 	 * zone on the current running system, then we mount its root up as
1386f4368d3dSvp157776 	 * read-only inside the scratch zone.
1387f4368d3dSvp157776 	 */
1388f4368d3dSvp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
1389f4368d3dSvp157776 	altstr = strdup(zonecfg_get_root());
1390f4368d3dSvp157776 	if (altstr == NULL) {
1391f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
1392f4368d3dSvp157776 		return (B_FALSE);
1393f4368d3dSvp157776 	}
1394f4368d3dSvp157776 	zonecfg_set_root("");
1395f4368d3dSvp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1396f4368d3dSvp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1397f4368d3dSvp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1398f4368d3dSvp157776 	    strcmp(fromdir, rootpath) != 0) {
1399f4368d3dSvp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1400f4368d3dSvp157776 		if (mkdir(tmp, 0755) != 0) {
1401f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1402f4368d3dSvp157776 			return (B_FALSE);
1403f4368d3dSvp157776 		}
1404f4368d3dSvp157776 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1405f4368d3dSvp157776 		    tmp) != 0) {
1406f4368d3dSvp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1407f4368d3dSvp157776 			    fromdir);
1408f4368d3dSvp157776 			return (B_FALSE);
1409f4368d3dSvp157776 		}
1410f4368d3dSvp157776 	}
1411f4368d3dSvp157776 	zonecfg_set_root(altstr);
1412f4368d3dSvp157776 	free(altstr);
1413f4368d3dSvp157776 
1414f4368d3dSvp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1415f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1416f4368d3dSvp157776 		return (B_FALSE);
1417f4368d3dSvp157776 	}
1418f4368d3dSvp157776 	(void) ftruncate(fileno(fp), 0);
1419f4368d3dSvp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1420f4368d3dSvp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1421f4368d3dSvp157776 	}
1422f4368d3dSvp157776 	zonecfg_close_scratch(fp);
1423f4368d3dSvp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1424f4368d3dSvp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1425f4368d3dSvp157776 		return (B_FALSE);
1426f4368d3dSvp157776 	(void) strlcpy(rootpath, tmp, rootlen);
1427f4368d3dSvp157776 	return (B_TRUE);
1428f4368d3dSvp157776 }
1429f4368d3dSvp157776 
1430f4368d3dSvp157776 
1431f4368d3dSvp157776 static boolean_t
14326cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14336cfd72c6Sgjelinek     const char *luroot)
1434f4368d3dSvp157776 {
1435f4368d3dSvp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1436f4368d3dSvp157776 	const char **cpp;
14376cfd72c6Sgjelinek 	const char **loopdirs;
14386cfd72c6Sgjelinek 	const char **tmpdirs;
1439f4368d3dSvp157776 	static const char *localdirs[] = {
1440f4368d3dSvp157776 		"/etc", "/var", NULL
1441f4368d3dSvp157776 	};
14426cfd72c6Sgjelinek 	static const char *scr_loopdirs[] = {
1443f4368d3dSvp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1444f4368d3dSvp157776 		"/usr", NULL
1445f4368d3dSvp157776 	};
14466cfd72c6Sgjelinek 	static const char *upd_loopdirs[] = {
14476cfd72c6Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14486cfd72c6Sgjelinek 		"/usr", "/var", NULL
14496cfd72c6Sgjelinek 	};
14506cfd72c6Sgjelinek 	static const char *scr_tmpdirs[] = {
1451f4368d3dSvp157776 		"/tmp", "/var/run", NULL
1452f4368d3dSvp157776 	};
14536cfd72c6Sgjelinek 	static const char *upd_tmpdirs[] = {
14546cfd72c6Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14556cfd72c6Sgjelinek 	};
1456f4368d3dSvp157776 	struct stat st;
1457f4368d3dSvp157776 
14586cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
1459108322fbScarlsonj 		/*
14606cfd72c6Sgjelinek 		 * These are mounted read-write from the zone undergoing
14616cfd72c6Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14626cfd72c6Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
1463108322fbScarlsonj 		 */
1464108322fbScarlsonj 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14656cfd72c6Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1466108322fbScarlsonj 			    *cpp);
14676cfd72c6Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14686cfd72c6Sgjelinek 			    rootpath, *cpp);
1469108322fbScarlsonj 			if (mkdir(tmp, 0755) != 0) {
1470108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1471108322fbScarlsonj 				return (B_FALSE);
1472108322fbScarlsonj 			}
14736cfd72c6Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14746cfd72c6Sgjelinek 			    != 0) {
14756cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14766cfd72c6Sgjelinek 				    tmp, *cpp);
1477108322fbScarlsonj 				return (B_FALSE);
1478108322fbScarlsonj 			}
1479108322fbScarlsonj 		}
14806cfd72c6Sgjelinek 	}
14816cfd72c6Sgjelinek 
14826cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14836cfd72c6Sgjelinek 		loopdirs = upd_loopdirs;
14846cfd72c6Sgjelinek 	else
14856cfd72c6Sgjelinek 		loopdirs = scr_loopdirs;
1486108322fbScarlsonj 
1487108322fbScarlsonj 	/*
1488108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1489108322fbScarlsonj 	 * they contain binaries that must match system.
1490108322fbScarlsonj 	 */
1491108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1492108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1493108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1494108322fbScarlsonj 			if (errno != EEXIST) {
1495108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1496108322fbScarlsonj 				return (B_FALSE);
1497108322fbScarlsonj 			}
1498108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1499108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1500108322fbScarlsonj 				return (B_FALSE);
1501108322fbScarlsonj 			}
1502108322fbScarlsonj 			/*
1503108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1504108322fbScarlsonj 			 * things that have been converted into symlinks
1505108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1506108322fbScarlsonj 			 * fixup.
1507108322fbScarlsonj 			 */
1508108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1509108322fbScarlsonj 				continue;
1510108322fbScarlsonj 		}
1511108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1512108322fbScarlsonj 		    tmp) != 0) {
1513108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1514108322fbScarlsonj 			    *cpp);
1515108322fbScarlsonj 			return (B_FALSE);
1516108322fbScarlsonj 		}
1517108322fbScarlsonj 	}
1518108322fbScarlsonj 
15196cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15206cfd72c6Sgjelinek 		tmpdirs = upd_tmpdirs;
15216cfd72c6Sgjelinek 	else
15226cfd72c6Sgjelinek 		tmpdirs = scr_tmpdirs;
15236cfd72c6Sgjelinek 
1524108322fbScarlsonj 	/*
1525108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1526108322fbScarlsonj 	 */
1527108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1528108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15296cfd72c6Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15306cfd72c6Sgjelinek 		    errno != EEXIST) {
1531108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1532108322fbScarlsonj 			return (B_FALSE);
1533108322fbScarlsonj 		}
1534d30e996aSgjelinek 
1535d30e996aSgjelinek 		/*
1536d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1537d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1538d30e996aSgjelinek 		 * the correct mode for /tmp now.
1539d30e996aSgjelinek 		 */
1540d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1541d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1542d30e996aSgjelinek 			return (B_FALSE);
1543d30e996aSgjelinek 		}
1544d30e996aSgjelinek 
1545108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1546108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1547108322fbScarlsonj 			return (B_FALSE);
1548108322fbScarlsonj 		}
1549108322fbScarlsonj 	}
1550108322fbScarlsonj 	return (B_TRUE);
1551108322fbScarlsonj }
1552108322fbScarlsonj 
15539acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15549acbbeafSnn35248 	zlog_t			*pgcd_zlogp;
15559acbbeafSnn35248 	struct zone_fstab	**pgcd_fs_tab;
15569acbbeafSnn35248 	int			*pgcd_num_fs;
15579acbbeafSnn35248 } plat_gmount_cb_data_t;
15589acbbeafSnn35248 
15599acbbeafSnn35248 /*
15609acbbeafSnn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15619acbbeafSnn35248  * through all global brand platform mounts.
15629acbbeafSnn35248  */
15639acbbeafSnn35248 int
15649acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15659acbbeafSnn35248     const char *fstype, const char *opt)
15669acbbeafSnn35248 {
15679acbbeafSnn35248 	plat_gmount_cb_data_t	*cp = data;
15689acbbeafSnn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15699acbbeafSnn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15709acbbeafSnn35248 	int			num_fs = *cp->pgcd_num_fs;
15719acbbeafSnn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15729acbbeafSnn35248 
15739acbbeafSnn35248 	num_fs++;
15749acbbeafSnn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15759acbbeafSnn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15769acbbeafSnn35248 		return (-1);
15779acbbeafSnn35248 	}
15789acbbeafSnn35248 
15799acbbeafSnn35248 	fs_ptr = tmp_ptr;
15809acbbeafSnn35248 	fsp = &fs_ptr[num_fs - 1];
15819acbbeafSnn35248 
15829acbbeafSnn35248 	/* update the callback struct passed in */
15839acbbeafSnn35248 	*cp->pgcd_fs_tab = fs_ptr;
15849acbbeafSnn35248 	*cp->pgcd_num_fs = num_fs;
15859acbbeafSnn35248 
15869acbbeafSnn35248 	fsp->zone_fs_raw[0] = '\0';
15879acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15889acbbeafSnn35248 	    sizeof (fsp->zone_fs_special));
15899acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15909acbbeafSnn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15919acbbeafSnn35248 	fsp->zone_fs_options = NULL;
1592fa3d7ae1Sedp 	if ((opt != NULL) &&
1593fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15949acbbeafSnn35248 		zerror(zlogp, B_FALSE, "error adding property");
15959acbbeafSnn35248 		return (-1);
15969acbbeafSnn35248 	}
15979acbbeafSnn35248 
15989acbbeafSnn35248 	return (0);
15999acbbeafSnn35248 }
16009acbbeafSnn35248 
16019acbbeafSnn35248 static int
16029acbbeafSnn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
16039acbbeafSnn35248     struct zone_fstab **fs_tabp, int *num_fsp)
16049acbbeafSnn35248 {
16059acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16069acbbeafSnn35248 	int num_fs;
16079acbbeafSnn35248 
16089acbbeafSnn35248 	num_fs = *num_fsp;
16099acbbeafSnn35248 	fs_ptr = *fs_tabp;
16109acbbeafSnn35248 
16119acbbeafSnn35248 	if (zonecfg_setipdent(handle) != Z_OK) {
16129acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16139acbbeafSnn35248 		return (-1);
16149acbbeafSnn35248 	}
16159acbbeafSnn35248 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
16169acbbeafSnn35248 		num_fs++;
16179acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16189acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16199acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16209acbbeafSnn35248 			(void) zonecfg_endipdent(handle);
16219acbbeafSnn35248 			return (-1);
16229acbbeafSnn35248 		}
16239acbbeafSnn35248 
16249acbbeafSnn35248 		/* update the pointers passed in */
16259acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16269acbbeafSnn35248 		*num_fsp = num_fs;
16279acbbeafSnn35248 
16289acbbeafSnn35248 		/*
16299acbbeafSnn35248 		 * IPDs logically only have a mount point; all other properties
16309acbbeafSnn35248 		 * are implied.
16319acbbeafSnn35248 		 */
16329acbbeafSnn35248 		fs_ptr = tmp_ptr;
16339acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16349acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16359acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16369acbbeafSnn35248 		fsp->zone_fs_special[0] = '\0';
16379acbbeafSnn35248 		fsp->zone_fs_raw[0] = '\0';
16389acbbeafSnn35248 		fsp->zone_fs_type[0] = '\0';
16399acbbeafSnn35248 		fsp->zone_fs_options = NULL;
16409acbbeafSnn35248 	}
16419acbbeafSnn35248 	(void) zonecfg_endipdent(handle);
16429acbbeafSnn35248 	return (0);
16439acbbeafSnn35248 }
16449acbbeafSnn35248 
16459acbbeafSnn35248 static int
16469acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16476cfd72c6Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16489acbbeafSnn35248 {
16499acbbeafSnn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16509acbbeafSnn35248 	int num_fs;
16519acbbeafSnn35248 
16529acbbeafSnn35248 	num_fs = *num_fsp;
16539acbbeafSnn35248 	fs_ptr = *fs_tabp;
16549acbbeafSnn35248 
16559acbbeafSnn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16569acbbeafSnn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16579acbbeafSnn35248 		return (-1);
16589acbbeafSnn35248 	}
16599acbbeafSnn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16609acbbeafSnn35248 		/*
16619acbbeafSnn35248 		 * ZFS filesystems will not be accessible under an alternate
16629acbbeafSnn35248 		 * root, since the pool will not be known.  Ignore them in this
16639acbbeafSnn35248 		 * case.
16649acbbeafSnn35248 		 */
16656cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16666cfd72c6Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16679acbbeafSnn35248 			continue;
16689acbbeafSnn35248 
16699acbbeafSnn35248 		num_fs++;
16709acbbeafSnn35248 		if ((tmp_ptr = realloc(fs_ptr,
16719acbbeafSnn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16729acbbeafSnn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16739acbbeafSnn35248 			(void) zonecfg_endfsent(handle);
16749acbbeafSnn35248 			return (-1);
16759acbbeafSnn35248 		}
16769acbbeafSnn35248 		/* update the pointers passed in */
16779acbbeafSnn35248 		*fs_tabp = tmp_ptr;
16789acbbeafSnn35248 		*num_fsp = num_fs;
16799acbbeafSnn35248 
16809acbbeafSnn35248 		fs_ptr = tmp_ptr;
16819acbbeafSnn35248 		fsp = &fs_ptr[num_fs - 1];
16829acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_dir,
16839acbbeafSnn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16849acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16859acbbeafSnn35248 		    sizeof (fsp->zone_fs_raw));
16869acbbeafSnn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16879acbbeafSnn35248 		    sizeof (fsp->zone_fs_type));
16889acbbeafSnn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
1689fa3d7ae1Sedp 
1690fa3d7ae1Sedp 		/*
1691fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1692fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1693fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1694fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1695fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1696fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1697fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1698fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1699fa3d7ae1Sedp 		 */
1700fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1701fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1702fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1703fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1704fa3d7ae1Sedp 		}
1705fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1706fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
17079acbbeafSnn35248 	}
17089acbbeafSnn35248 	(void) zonecfg_endfsent(handle);
17099acbbeafSnn35248 	return (0);
17109acbbeafSnn35248 }
17119acbbeafSnn35248 
17127c478bd9Sstevel@tonic-gate static int
17136cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
17147c478bd9Sstevel@tonic-gate {
17157c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
17167c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
17179acbbeafSnn35248 	char brand[MAXNAMELEN];
171816bca938Svp157776 	char luroot[MAXPATHLEN];
17199acbbeafSnn35248 	int i, num_fs = 0;
17209acbbeafSnn35248 	struct zone_fstab *fs_ptr = NULL;
17217c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
17227c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
1723123807fbSedp 	brand_handle_t bh;
17249acbbeafSnn35248 	plat_gmount_cb_data_t cb;
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1727108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
17287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1729108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1730108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1731108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
17327c478bd9Sstevel@tonic-gate 		goto bad;
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
17367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
17377c478bd9Sstevel@tonic-gate 		goto bad;
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
17417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
17427c478bd9Sstevel@tonic-gate 		goto bad;
17437c478bd9Sstevel@tonic-gate 	}
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1746ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17477c478bd9Sstevel@tonic-gate 		goto bad;
17487c478bd9Sstevel@tonic-gate 	}
17497c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17507c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17517c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17527c478bd9Sstevel@tonic-gate 		goto bad;
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 
17559acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
17569acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
1757123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
17589acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1759e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17609acbbeafSnn35248 		return (-1);
17619acbbeafSnn35248 	}
17629acbbeafSnn35248 
17639acbbeafSnn35248 	/*
17649acbbeafSnn35248 	 * Get the list of global filesystems to mount from the brand
17659acbbeafSnn35248 	 * configuration.
17669acbbeafSnn35248 	 */
17679acbbeafSnn35248 	cb.pgcd_zlogp = zlogp;
17689acbbeafSnn35248 	cb.pgcd_fs_tab = &fs_ptr;
17699acbbeafSnn35248 	cb.pgcd_num_fs = &num_fs;
1770123807fbSedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17719acbbeafSnn35248 	    plat_gmount_cb, &cb) != 0) {
17729acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1773123807fbSedp 		brand_close(bh);
1774e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17759acbbeafSnn35248 		return (-1);
17769acbbeafSnn35248 	}
1777123807fbSedp 	brand_close(bh);
17789acbbeafSnn35248 
17797c478bd9Sstevel@tonic-gate 	/*
17807c478bd9Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
17817c478bd9Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
17827c478bd9Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
17837c478bd9Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
17847c478bd9Sstevel@tonic-gate 	 */
17859acbbeafSnn35248 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
17867c478bd9Sstevel@tonic-gate 		goto bad;
17877c478bd9Sstevel@tonic-gate 
17889acbbeafSnn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17899acbbeafSnn35248 	    mount_cmd) != 0)
17907c478bd9Sstevel@tonic-gate 		goto bad;
1791fa9e4066Sahrens 
17927c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17937c478bd9Sstevel@tonic-gate 	handle = NULL;
17947c478bd9Sstevel@tonic-gate 
1795108322fbScarlsonj 	/*
17969acbbeafSnn35248 	 * Normally when we mount a zone all the zone filesystems
17979acbbeafSnn35248 	 * get mounted relative to rootpath, which is usually
17989acbbeafSnn35248 	 * <zonepath>/root.  But when mounting a zone for administration
17999acbbeafSnn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
18009acbbeafSnn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
18019acbbeafSnn35248 	 * the zones filesystems there instead.
18029acbbeafSnn35248 	 *
18039acbbeafSnn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
18049acbbeafSnn35248 	 * also do some extra work to create directories and lofs mount
18059acbbeafSnn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
18069acbbeafSnn35248 	 *
18079acbbeafSnn35248 	 * This allows us to be able to enter the zone (now rooted at
18089acbbeafSnn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
18099acbbeafSnn35248 	 * global zone and have them upgrade the to-be-modified zone's
18109acbbeafSnn35248 	 * files mounted on /a.  (Which mirrors the existing standard
18119acbbeafSnn35248 	 * upgrade environment.)
18129acbbeafSnn35248 	 *
18139acbbeafSnn35248 	 * There is of course one catch.  When doing the upgrade
18149acbbeafSnn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
18159acbbeafSnn35248 	 * for the zone and we don't want to have any /dev filesystem
18169acbbeafSnn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
18179acbbeafSnn35248 	 * as a normal zone filesystem by default we'll try to mount
18189acbbeafSnn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
18199acbbeafSnn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
18209acbbeafSnn35248 	 *
18219acbbeafSnn35248 	 * All this work is done in three phases:
1822f4368d3dSvp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1823f4368d3dSvp157776 	 *   2) Mount the required filesystems as per the zone configuration.
1824f4368d3dSvp157776 	 *   3) Set up the rest of the scratch zone environment
1825f4368d3dSvp157776 	 *	(build_mounted_post_var()).
1826108322fbScarlsonj 	 */
18276cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
182816bca938Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1829108322fbScarlsonj 		goto bad;
1830108322fbScarlsonj 
18317c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
18329acbbeafSnn35248 
18337c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
18346cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
18359acbbeafSnn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
18369acbbeafSnn35248 			size_t slen = strlen(rootpath) - 2;
18379acbbeafSnn35248 
18389acbbeafSnn35248 			/*
18399acbbeafSnn35248 			 * By default we'll try to mount /dev as /a/dev
18409acbbeafSnn35248 			 * but /dev is special and always goes at the top
18419acbbeafSnn35248 			 * so strip the trailing '/a' from the rootpath.
18429acbbeafSnn35248 			 */
184384561e8cStd153743 			assert(zone_isnative || zone_iscluster);
18449acbbeafSnn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18459acbbeafSnn35248 			rootpath[slen] = '\0';
18469acbbeafSnn35248 			if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
18479acbbeafSnn35248 				goto bad;
18489acbbeafSnn35248 			rootpath[slen] = '/';
18499acbbeafSnn35248 			continue;
18509acbbeafSnn35248 		}
18517c478bd9Sstevel@tonic-gate 		if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
18527c478bd9Sstevel@tonic-gate 			goto bad;
18537c478bd9Sstevel@tonic-gate 	}
18546cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18556cfd72c6Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1856f4368d3dSvp157776 		goto bad;
185745916cd2Sjpk 
185845916cd2Sjpk 	/*
185945916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
186045916cd2Sjpk 	 */
18616cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18626cfd72c6Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
186345916cd2Sjpk 		goto bad;
186445916cd2Sjpk 
18657c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	/*
18687c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18697c478bd9Sstevel@tonic-gate 	 */
18707c478bd9Sstevel@tonic-gate 	return (0);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate bad:
18737c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18747c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18757c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18767c478bd9Sstevel@tonic-gate 	return (-1);
18777c478bd9Sstevel@tonic-gate }
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18807c478bd9Sstevel@tonic-gate static int
18817c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18827c478bd9Sstevel@tonic-gate {
18837c478bd9Sstevel@tonic-gate 	int prefixlen;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18867c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18877c478bd9Sstevel@tonic-gate 		return (1);
18887c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
18897c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
18907c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18917c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
18927c478bd9Sstevel@tonic-gate 			continue;
18937c478bd9Sstevel@tonic-gate 		}
18947c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18957c478bd9Sstevel@tonic-gate 		prefixlen--;
18967c478bd9Sstevel@tonic-gate 	}
18977c478bd9Sstevel@tonic-gate 	return (0);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate /*
19017c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
19027c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
19037c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
19047c478bd9Sstevel@tonic-gate  *
19057c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
19067c478bd9Sstevel@tonic-gate  */
19077c478bd9Sstevel@tonic-gate static int
1908f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
19097c478bd9Sstevel@tonic-gate {
19107c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
19117c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
19127c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
19137c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
19147c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
19157c478bd9Sstevel@tonic-gate 	uint_t bufsize;
19167c478bd9Sstevel@tonic-gate 	char *buf = NULL;
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
19197c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
19207c478bd9Sstevel@tonic-gate 		ret_code = -1;
19217c478bd9Sstevel@tonic-gate 		goto bad;
19227c478bd9Sstevel@tonic-gate 	}
19237c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
19247c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
19257c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
19267c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1927f4b3ec61Sdh155122 		    "could not determine number of network interfaces");
19287c478bd9Sstevel@tonic-gate 		ret_code = -1;
19297c478bd9Sstevel@tonic-gate 		goto bad;
19307c478bd9Sstevel@tonic-gate 	}
19317c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
19327c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
19337c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
19347c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
19357c478bd9Sstevel@tonic-gate 		ret_code = -1;
19367c478bd9Sstevel@tonic-gate 		goto bad;
19377c478bd9Sstevel@tonic-gate 	}
19387c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
19397c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19407c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19417c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19427c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1943f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
1944f4b3ec61Sdh155122 		    "interfaces");
19457c478bd9Sstevel@tonic-gate 		ret_code = -1;
19467c478bd9Sstevel@tonic-gate 		goto bad;
19477c478bd9Sstevel@tonic-gate 	}
19487c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19497c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19507c478bd9Sstevel@tonic-gate 		(void) close(s);
19517c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19527c478bd9Sstevel@tonic-gate 		    0) {
19537c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19547c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
19557c478bd9Sstevel@tonic-gate 			ret_code = -1;
19567c478bd9Sstevel@tonic-gate 			continue;
19577c478bd9Sstevel@tonic-gate 		}
19587c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19597c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19607c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19617c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1962c1c0ebd5Ssl108498 			if (errno == ENXIO)
1963c1c0ebd5Ssl108498 				/*
1964c1c0ebd5Ssl108498 				 * Interface may have been removed by admin or
1965c1c0ebd5Ssl108498 				 * another zone halting.
1966c1c0ebd5Ssl108498 				 */
1967c1c0ebd5Ssl108498 				continue;
19687c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1969c1c0ebd5Ssl108498 			    "%s: could not determine the zone to which this "
1970f4b3ec61Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19717c478bd9Sstevel@tonic-gate 			ret_code = -1;
19727c478bd9Sstevel@tonic-gate 			continue;
19737c478bd9Sstevel@tonic-gate 		}
19747c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19757c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19767c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1977f4b3ec61Sdh155122 				    "%s: could not remove network interface",
19787c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
19797c478bd9Sstevel@tonic-gate 				ret_code = -1;
19807c478bd9Sstevel@tonic-gate 				continue;
19817c478bd9Sstevel@tonic-gate 			}
19827c478bd9Sstevel@tonic-gate 		}
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate bad:
19857c478bd9Sstevel@tonic-gate 	if (s > 0)
19867c478bd9Sstevel@tonic-gate 		(void) close(s);
19877c478bd9Sstevel@tonic-gate 	if (buf)
19887c478bd9Sstevel@tonic-gate 		free(buf);
19897c478bd9Sstevel@tonic-gate 	return (ret_code);
19907c478bd9Sstevel@tonic-gate }
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate static union	sockunion {
19937c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
19947c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
19957c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19967c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19977c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate static struct {
20007c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
20017c478bd9Sstevel@tonic-gate 	char	space[512];
20027c478bd9Sstevel@tonic-gate } rtmsg;
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate static int
20057c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
20067c478bd9Sstevel@tonic-gate {
20077c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
20087c478bd9Sstevel@tonic-gate 	case AF_INET:
20097c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
20107c478bd9Sstevel@tonic-gate 	case AF_LINK:
20117c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
20127c478bd9Sstevel@tonic-gate 	case AF_INET6:
20137c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
20147c478bd9Sstevel@tonic-gate 	default:
20157c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate }
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
20207c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate /*
20237c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
20247c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
20257c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
20267c478bd9Sstevel@tonic-gate  *
20277c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
20287c478bd9Sstevel@tonic-gate  *
20297c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
20307c478bd9Sstevel@tonic-gate  *
20317c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
20327c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
20337c478bd9Sstevel@tonic-gate  */
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate static char *
20367c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20377c478bd9Sstevel@tonic-gate {
20387c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
20397c478bd9Sstevel@tonic-gate 	pid_t pid;
20407c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
20417c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
20427c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20437c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
20447c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	pid = getpid();
20497c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20517c478bd9Sstevel@tonic-gate 		return (NULL);
20527c478bd9Sstevel@tonic-gate 	}
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20557c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20587c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20597c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20607c478bd9Sstevel@tonic-gate 	} else {
20617c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20647c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20657c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20717c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20727c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20737c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20747c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20757c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20787c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20797c478bd9Sstevel@tonic-gate 	cp += l;
20807c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20817c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20827c478bd9Sstevel@tonic-gate 	cp += l;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20887c478bd9Sstevel@tonic-gate 		return (NULL);
20897c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20907c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20917c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20927c478bd9Sstevel@tonic-gate 		return (NULL);
20937c478bd9Sstevel@tonic-gate 	}
20947c478bd9Sstevel@tonic-gate 	do {
20957c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20967c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20977c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20987c478bd9Sstevel@tonic-gate 	if (l < 0) {
20997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
21007c478bd9Sstevel@tonic-gate 		return (NULL);
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
21047c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
21057c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
21067c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
21077c478bd9Sstevel@tonic-gate 		return (NULL);
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
21107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
21117c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
21127c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
21137c478bd9Sstevel@tonic-gate 		return (NULL);
21147c478bd9Sstevel@tonic-gate 	}
21157c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
21167c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
21177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
21187c478bd9Sstevel@tonic-gate 		return (NULL);
21197c478bd9Sstevel@tonic-gate 	}
21207c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2121f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
21227c478bd9Sstevel@tonic-gate 		return (NULL);
21237c478bd9Sstevel@tonic-gate 	}
21247c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
21257c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
21267c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
21277c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
21287c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
21297c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
21307c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
21317c478bd9Sstevel@tonic-gate 			continue;
21327c478bd9Sstevel@tonic-gate 		}
21337c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
21347c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
21357c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
21367c478bd9Sstevel@tonic-gate 		break;
21377c478bd9Sstevel@tonic-gate 	}
21387c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2139f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
2140f4b3ec61Sdh155122 		    "determined");
21417c478bd9Sstevel@tonic-gate 		return (NULL);
21427c478bd9Sstevel@tonic-gate 	}
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 	/*
21457c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21467c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21477c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21487c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21497c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21507c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21517c478bd9Sstevel@tonic-gate 	 */
21527c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21537c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21547c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21557c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21567c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21577c478bd9Sstevel@tonic-gate 	if (i < 0) {
21587c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2159f4b3ec61Sdh155122 		    "%s: could not determine the zone network interface "
2160f4b3ec61Sdh155122 		    "belongs to", lifr->lifr_name);
21617c478bd9Sstevel@tonic-gate 		return (NULL);
21627c478bd9Sstevel@tonic-gate 	}
21637c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21647c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21657c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21687c478bd9Sstevel@tonic-gate 		return (answer);
21697c478bd9Sstevel@tonic-gate 	return (NULL);
21707c478bd9Sstevel@tonic-gate }
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate typedef struct mcast_rtmsg_s {
21737c478bd9Sstevel@tonic-gate 	struct rt_msghdr	m_rtm;
21747c478bd9Sstevel@tonic-gate 	union {
21757c478bd9Sstevel@tonic-gate 		struct {
21767c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_dst;
21777c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_gw;
21787c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_netmask;
21797c478bd9Sstevel@tonic-gate 		} m_v4;
21807c478bd9Sstevel@tonic-gate 		struct {
21817c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_dst;
21827c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_gw;
21837c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_netmask;
21847c478bd9Sstevel@tonic-gate 		} m_v6;
21857c478bd9Sstevel@tonic-gate 	} m_u;
21867c478bd9Sstevel@tonic-gate } mcast_rtmsg_t;
21877c478bd9Sstevel@tonic-gate #define	m_dst4		m_u.m_v4.m_dst
21887c478bd9Sstevel@tonic-gate #define	m_dst6		m_u.m_v6.m_dst
21897c478bd9Sstevel@tonic-gate #define	m_gw4		m_u.m_v4.m_gw
21907c478bd9Sstevel@tonic-gate #define	m_gw6		m_u.m_v6.m_gw
21917c478bd9Sstevel@tonic-gate #define	m_netmask4	m_u.m_v4.m_netmask
21927c478bd9Sstevel@tonic-gate #define	m_netmask6	m_u.m_v6.m_netmask
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate /*
21957c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21967c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21977c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21987c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21997c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
22007c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
22017c478bd9Sstevel@tonic-gate  *
22027c478bd9Sstevel@tonic-gate  * A default interface route for multicast is created on the first IPv4 and
22037c478bd9Sstevel@tonic-gate  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
22047c478bd9Sstevel@tonic-gate  * This should really be done in the init scripts if we ever allow zones to
22057c478bd9Sstevel@tonic-gate  * modify the routing tables.
22067c478bd9Sstevel@tonic-gate  *
22077c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
22087c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
22097c478bd9Sstevel@tonic-gate  */
22107c478bd9Sstevel@tonic-gate static int
22117c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
22127c478bd9Sstevel@tonic-gate     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
22137c478bd9Sstevel@tonic-gate     boolean_t *mcast_rt_v6_setp)
22147c478bd9Sstevel@tonic-gate {
22157c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
22167c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
22177c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
22187c478bd9Sstevel@tonic-gate 	struct in_addr in4;
22197c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
22207c478bd9Sstevel@tonic-gate 	sa_family_t af;
22217c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
22227c478bd9Sstevel@tonic-gate 	mcast_rtmsg_t mcast_rtmsg;
22237c478bd9Sstevel@tonic-gate 	int s;
22247c478bd9Sstevel@tonic-gate 	int rs;
22257c478bd9Sstevel@tonic-gate 	int rlen;
22267c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
22277c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
22287c478bd9Sstevel@tonic-gate 	int res;
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
22317c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
22327c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
22337c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
22347c478bd9Sstevel@tonic-gate 		return (-1);
22357c478bd9Sstevel@tonic-gate 	}
22367c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
22377c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22387c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
22397c478bd9Sstevel@tonic-gate 	else
22407c478bd9Sstevel@tonic-gate 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
22437c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
22447c478bd9Sstevel@tonic-gate 		return (-1);
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
22487c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
22497c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
225022321485Svp157776 		/*
225122321485Svp157776 		 * Here, we know that the interface can't be brought up.
225222321485Svp157776 		 * A similar warning message was already printed out to
225322321485Svp157776 		 * the console by zoneadm(1M) so instead we log the
225422321485Svp157776 		 * message to syslog and continue.
225522321485Svp157776 		 */
2256f4b3ec61Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
225722321485Svp157776 		    "'%s' which may not be present/plumbed in the "
225822321485Svp157776 		    "global zone.", lifr.lifr_name);
22597c478bd9Sstevel@tonic-gate 		(void) close(s);
226022321485Svp157776 		return (Z_OK);
22617c478bd9Sstevel@tonic-gate 	}
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
22657c478bd9Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
22667c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22677c478bd9Sstevel@tonic-gate 		goto bad;
22687c478bd9Sstevel@tonic-gate 	}
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22717c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22727c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22757c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2276f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2277f4b3ec61Sdh155122 		    "into zone", lifr.lifr_name);
22787c478bd9Sstevel@tonic-gate 		goto bad;
22797c478bd9Sstevel@tonic-gate 	}
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
22827c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
22837c478bd9Sstevel@tonic-gate 	} else {
22847c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
22857c478bd9Sstevel@tonic-gate 			/*
22867c478bd9Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
22877c478bd9Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
22887c478bd9Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
22897c478bd9Sstevel@tonic-gate 			 * being able to determine it is a common failure,
22907c478bd9Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
22917c478bd9Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
22927c478bd9Sstevel@tonic-gate 			 * printed after the rest of the interface's
22937c478bd9Sstevel@tonic-gate 			 * parameters have been configured.
22947c478bd9Sstevel@tonic-gate 			 */
22957c478bd9Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
22967c478bd9Sstevel@tonic-gate 			if (slashp != NULL) {
22977c478bd9Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22987c478bd9Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
22997c478bd9Sstevel@tonic-gate 					*slashp = '/';
23007c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
23017c478bd9Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
23027c478bd9Sstevel@tonic-gate 					    lifr.lifr_name,
23037c478bd9Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
23047c478bd9Sstevel@tonic-gate 					goto bad;
23057c478bd9Sstevel@tonic-gate 				}
23067c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
23077c478bd9Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
23087c478bd9Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
23097c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
23107c478bd9Sstevel@tonic-gate 			}
23117c478bd9Sstevel@tonic-gate 			if (got_netmask) {
23127c478bd9Sstevel@tonic-gate 				netmask4.sin_family = af;
23137c478bd9Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
23147c478bd9Sstevel@tonic-gate 				    sizeof (netmask4));
23157c478bd9Sstevel@tonic-gate 			}
23167c478bd9Sstevel@tonic-gate 		} else {
23177c478bd9Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
23187c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
23197c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
23207c478bd9Sstevel@tonic-gate 				*slashp = '/';
23217c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
23227c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
23237c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
23247c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
23257c478bd9Sstevel@tonic-gate 				goto bad;
23267c478bd9Sstevel@tonic-gate 			}
23277c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
23287c478bd9Sstevel@tonic-gate 			netmask6.sin6_family = af;
23297c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
23307c478bd9Sstevel@tonic-gate 			    sizeof (netmask6));
23317c478bd9Sstevel@tonic-gate 		}
23327c478bd9Sstevel@tonic-gate 		if (got_netmask &&
23337c478bd9Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
23347c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
23357c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23367c478bd9Sstevel@tonic-gate 			goto bad;
23377c478bd9Sstevel@tonic-gate 		}
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 		/*
23407c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
23417c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
23427c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
23437c478bd9Sstevel@tonic-gate 		 */
23447c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23457c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23467c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23477c478bd9Sstevel@tonic-gate 			goto bad;
23487c478bd9Sstevel@tonic-gate 		}
23497c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
23507c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23517c478bd9Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
23527c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23537c478bd9Sstevel@tonic-gate 			goto bad;
23547c478bd9Sstevel@tonic-gate 		}
23557c478bd9Sstevel@tonic-gate 	}
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23587c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
23597c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
23607c478bd9Sstevel@tonic-gate 		goto bad;
23617c478bd9Sstevel@tonic-gate 	}
23627c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
23637c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
23647c478bd9Sstevel@tonic-gate 		int save_errno = errno;
23657c478bd9Sstevel@tonic-gate 		char *zone_using;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 		/*
23687c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
23697c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
23707c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23717c478bd9Sstevel@tonic-gate 		 * using that address.
23727c478bd9Sstevel@tonic-gate 		 */
23737c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23747c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2375f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2376f4b3ec61Sdh155122 			    lifr.lifr_name);
23777c478bd9Sstevel@tonic-gate 			goto bad;
23787c478bd9Sstevel@tonic-gate 		}
23797c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23807c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23817c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23827c478bd9Sstevel@tonic-gate 			goto bad;
23837c478bd9Sstevel@tonic-gate 		}
23847c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23857c478bd9Sstevel@tonic-gate 		errno = save_errno;
23867c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
23877c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2388f4b3ec61Sdh155122 			    "%s: could not bring network interface up",
2389f4b3ec61Sdh155122 			    lifr.lifr_name);
23907c478bd9Sstevel@tonic-gate 		else
2391f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2392f4b3ec61Sdh155122 			    "interface up: address in use by zone '%s'",
2393f4b3ec61Sdh155122 			    lifr.lifr_name, zone_using);
23947c478bd9Sstevel@tonic-gate 		goto bad;
23957c478bd9Sstevel@tonic-gate 	}
23967c478bd9Sstevel@tonic-gate 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
23977c478bd9Sstevel@tonic-gate 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
23987c478bd9Sstevel@tonic-gate 	    (af == AF_INET6 &&
23997c478bd9Sstevel@tonic-gate 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
24007c478bd9Sstevel@tonic-gate 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
24017c478bd9Sstevel@tonic-gate 		if (rs < 0) {
24027c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not create "
24037c478bd9Sstevel@tonic-gate 			    "routing socket", lifr.lifr_name);
24047c478bd9Sstevel@tonic-gate 			goto bad;
24057c478bd9Sstevel@tonic-gate 		}
24067c478bd9Sstevel@tonic-gate 		(void) shutdown(rs, 0);
24077c478bd9Sstevel@tonic-gate 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
24087c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
24097c478bd9Sstevel@tonic-gate 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
24107c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
24117c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
24127c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
24137c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
24147c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_addrs =
24157c478bd9Sstevel@tonic-gate 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
24167c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
24177c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
24187c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
24197c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
24207c478bd9Sstevel@tonic-gate 			    htonl(INADDR_UNSPEC_GROUP);
24217c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
24227c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_addr = in4;
24237c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
24247c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
24257c478bd9Sstevel@tonic-gate 			    htonl(IN_CLASSD_NET);
24267c478bd9Sstevel@tonic-gate 		} else {
24277c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
24287c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
24297c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
24307c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_addr = in6;
24317c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
24327c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
24337c478bd9Sstevel@tonic-gate 		}
24347c478bd9Sstevel@tonic-gate 		rlen = write(rs, (char *)&mcast_rtmsg,
24357c478bd9Sstevel@tonic-gate 		    mcast_rtmsg.m_rtm.rtm_msglen);
243622321485Svp157776 		/*
243722321485Svp157776 		 * The write to the multicast socket will fail if the
243822321485Svp157776 		 * interface belongs to a failed IPMP group. This is a
243922321485Svp157776 		 * non-fatal error and the zone will continue booting.
244022321485Svp157776 		 * While the zone is running, if any interface in the
244122321485Svp157776 		 * failed IPMP group recovers, the zone will fallback to
244222321485Svp157776 		 * using that interface.
244322321485Svp157776 		 */
24447c478bd9Sstevel@tonic-gate 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
24457c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
2446f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "WARNING: network "
2447f4b3ec61Sdh155122 				    "interface '%s' not available as default "
2448f4b3ec61Sdh155122 				    "for multicast.", lifr.lifr_name);
24497c478bd9Sstevel@tonic-gate 			} else {
2450f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "WARNING: network "
2451f4b3ec61Sdh155122 				    "interface '%s' not available as default "
2452f4b3ec61Sdh155122 				    "for multicast; routing socket returned "
245322321485Svp157776 				    "unexpected %d bytes.",
245422321485Svp157776 				    lifr.lifr_name, rlen);
24557c478bd9Sstevel@tonic-gate 			}
245622321485Svp157776 		} else {
245722321485Svp157776 
24587c478bd9Sstevel@tonic-gate 			if (af == AF_INET) {
24597c478bd9Sstevel@tonic-gate 				*mcast_rt_v4_setp = B_TRUE;
24607c478bd9Sstevel@tonic-gate 			} else {
24617c478bd9Sstevel@tonic-gate 				*mcast_rt_v6_setp = B_TRUE;
24627c478bd9Sstevel@tonic-gate 			}
246322321485Svp157776 		}
24647c478bd9Sstevel@tonic-gate 		(void) close(rs);
24657c478bd9Sstevel@tonic-gate 	}
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	if (!got_netmask) {
24687c478bd9Sstevel@tonic-gate 		/*
24697c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
24707c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
24717c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
24727c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
24737c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
24747c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
24757c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
24767c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
24777c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
24787c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
24797c478bd9Sstevel@tonic-gate 		 */
24807c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
24817c478bd9Sstevel@tonic-gate 		void  *addr;
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
24847c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
24857c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
24867c478bd9Sstevel@tonic-gate 		else
24877c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
24887c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
24917c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
24927c478bd9Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
24937c478bd9Sstevel@tonic-gate 			goto bad;
24947c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
24957c478bd9Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
24967c478bd9Sstevel@tonic-gate 		    "%s; using default of %s.",
24977c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
24987c478bd9Sstevel@tonic-gate 	}
24997c478bd9Sstevel@tonic-gate 
2500de860bd9Sgfaden 	/*
2501de860bd9Sgfaden 	 * If a default router was specified for this interface
2502de860bd9Sgfaden 	 * set the route now. Ignore if already set.
2503de860bd9Sgfaden 	 */
2504de860bd9Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2505de860bd9Sgfaden 		int status;
2506de860bd9Sgfaden 		char *argv[7];
2507de860bd9Sgfaden 
2508de860bd9Sgfaden 		argv[0] = "route";
2509de860bd9Sgfaden 		argv[1] = "add";
2510de860bd9Sgfaden 		argv[2] = "-ifp";
2511de860bd9Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
2512de860bd9Sgfaden 		argv[4] = "default";
2513de860bd9Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2514de860bd9Sgfaden 		argv[6] = NULL;
2515de860bd9Sgfaden 
2516de860bd9Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2517de860bd9Sgfaden 		if (status != 0 && status != EEXIST)
2518de860bd9Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
2519de860bd9Sgfaden 			    "interface %s to %s\n",
2520de860bd9Sgfaden 			    nwiftabptr->zone_nwif_physical,
2521de860bd9Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
2522de860bd9Sgfaden 	}
2523de860bd9Sgfaden 
25247c478bd9Sstevel@tonic-gate 	(void) close(s);
25257c478bd9Sstevel@tonic-gate 	return (Z_OK);
25267c478bd9Sstevel@tonic-gate bad:
25277c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
25287c478bd9Sstevel@tonic-gate 	(void) close(s);
25297c478bd9Sstevel@tonic-gate 	return (-1);
25307c478bd9Sstevel@tonic-gate }
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate /*
25337c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
25347c478bd9Sstevel@tonic-gate  * An IPv4 loopback interface is set up "for free", modeling the global system.
25357c478bd9Sstevel@tonic-gate  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
25367c478bd9Sstevel@tonic-gate  * address is set up as well.
25377c478bd9Sstevel@tonic-gate  *
25387c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
25397c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
25407c478bd9Sstevel@tonic-gate  */
25417c478bd9Sstevel@tonic-gate static int
2542f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
25437c478bd9Sstevel@tonic-gate {
25447c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
25457c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
25467c478bd9Sstevel@tonic-gate 	boolean_t saw_v6 = B_FALSE;
25477c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v4_set = B_FALSE;
25487c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v6_set = B_FALSE;
25497c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
25527c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
25537c478bd9Sstevel@tonic-gate 		return (-1);
25547c478bd9Sstevel@tonic-gate 	}
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
25577c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
25587c478bd9Sstevel@tonic-gate 		return (-1);
25597c478bd9Sstevel@tonic-gate 	}
25607c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
25617c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
25627c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
25637c478bd9Sstevel@tonic-gate 		return (-1);
25647c478bd9Sstevel@tonic-gate 	}
25657c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
25667c478bd9Sstevel@tonic-gate 		for (;;) {
25677c478bd9Sstevel@tonic-gate 			struct in6_addr in6;
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
25707c478bd9Sstevel@tonic-gate 				break;
25717c478bd9Sstevel@tonic-gate 			if (configure_one_interface(zlogp, zoneid,
25727c478bd9Sstevel@tonic-gate 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
25737c478bd9Sstevel@tonic-gate 			    Z_OK) {
25747c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
25757c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
25767c478bd9Sstevel@tonic-gate 				return (-1);
25777c478bd9Sstevel@tonic-gate 			}
25787c478bd9Sstevel@tonic-gate 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
25797c478bd9Sstevel@tonic-gate 			    &in6) == 1)
25807c478bd9Sstevel@tonic-gate 				saw_v6 = B_TRUE;
25817c478bd9Sstevel@tonic-gate 		}
25827c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
25837c478bd9Sstevel@tonic-gate 	}
25847c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
2585fdb7141fSgfaden 	if (is_system_labeled()) {
2586fdb7141fSgfaden 		/*
2587fdb7141fSgfaden 		 * Labeled zones share the loopback interface
2588fdb7141fSgfaden 		 * so it is not plumbed for shared stack instances.
2589fdb7141fSgfaden 		 */
2590fdb7141fSgfaden 		return (0);
2591fdb7141fSgfaden 	}
25927c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
25937c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
25947c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
25957c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2596442d3e9eSgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
25977c478bd9Sstevel@tonic-gate 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
25987c478bd9Sstevel@tonic-gate 	    != Z_OK) {
25997c478bd9Sstevel@tonic-gate 		return (-1);
26007c478bd9Sstevel@tonic-gate 	}
26017c478bd9Sstevel@tonic-gate 	if (saw_v6) {
26027c478bd9Sstevel@tonic-gate 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
26037c478bd9Sstevel@tonic-gate 		    sizeof (loopback_iftab.zone_nwif_address));
26047c478bd9Sstevel@tonic-gate 		if (configure_one_interface(zlogp, zoneid,
26057c478bd9Sstevel@tonic-gate 		    &loopback_iftab, NULL, NULL) != Z_OK) {
26067c478bd9Sstevel@tonic-gate 			return (-1);
26077c478bd9Sstevel@tonic-gate 		}
26087c478bd9Sstevel@tonic-gate 	}
26097c478bd9Sstevel@tonic-gate 	return (0);
26107c478bd9Sstevel@tonic-gate }
26117c478bd9Sstevel@tonic-gate 
2612f4b3ec61Sdh155122 static void
2613f4b3ec61Sdh155122 show_owner(zlog_t *zlogp, char *dlname)
2614f4b3ec61Sdh155122 {
2615f4b3ec61Sdh155122 	zoneid_t dl_owner_zid;
2616f4b3ec61Sdh155122 	char dl_owner_zname[ZONENAME_MAX];
2617f4b3ec61Sdh155122 
2618f4b3ec61Sdh155122 	dl_owner_zid = ALL_ZONES;
2619f4b3ec61Sdh155122 	if (zone_check_datalink(&dl_owner_zid, dlname) != 0)
2620f4b3ec61Sdh155122 		(void) snprintf(dl_owner_zname, ZONENAME_MAX, "<unknown>");
2621f4b3ec61Sdh155122 	else if (getzonenamebyid(dl_owner_zid, dl_owner_zname, ZONENAME_MAX)
2622f4b3ec61Sdh155122 	    < 0)
2623f4b3ec61Sdh155122 		(void) snprintf(dl_owner_zname, ZONENAME_MAX, "<%d>",
2624f4b3ec61Sdh155122 		    dl_owner_zid);
2625f4b3ec61Sdh155122 
2626f4b3ec61Sdh155122 	errno = EPERM;
2627f4b3ec61Sdh155122 	zerror(zlogp, B_TRUE, "WARNING: skipping network interface '%s' "
2628f4b3ec61Sdh155122 	    "which is used by the non-global zone '%s'.\n",
2629f4b3ec61Sdh155122 	    dlname, dl_owner_zname);
2630f4b3ec61Sdh155122 }
2631f4b3ec61Sdh155122 
2632f4b3ec61Sdh155122 static int
2633f4b3ec61Sdh155122 add_datalink(zlog_t *zlogp, zoneid_t zoneid, char *dlname)
2634f4b3ec61Sdh155122 {
2635f4b3ec61Sdh155122 	/* First check if it's in use by global zone. */
2636f4b3ec61Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2637f4b3ec61Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
2638f4b3ec61Sdh155122 		errno = EPERM;
2639f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "WARNING: skipping network interface "
2640f4b3ec61Sdh155122 		    "'%s' which is used in the global zone.", dlname);
2641f4b3ec61Sdh155122 		return (-1);
2642f4b3ec61Sdh155122 	}
2643f4b3ec61Sdh155122 
2644f4b3ec61Sdh155122 	/* Add access control information */
2645f4b3ec61Sdh155122 	if (zone_add_datalink(zoneid, dlname) != 0) {
2646f4b3ec61Sdh155122 		/* If someone got this link before us, show its name */
2647f4b3ec61Sdh155122 		if (errno == EPERM)
2648f4b3ec61Sdh155122 			show_owner(zlogp, dlname);
2649f4b3ec61Sdh155122 		else
2650f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "WARNING: unable to add network "
2651f4b3ec61Sdh155122 			    "interface '%s'.", dlname);
2652f4b3ec61Sdh155122 		return (-1);
2653f4b3ec61Sdh155122 	}
2654f4b3ec61Sdh155122 
2655d62bc4baSyz147064 	/* Set zoneid of this link. */
2656d62bc4baSyz147064 	if (dladm_setzid(dlname, zoneid) != DLADM_STATUS_OK) {
2657d62bc4baSyz147064 		zerror(zlogp, B_TRUE, "WARNING: unable to add network "
2658f4b3ec61Sdh155122 		    "interface '%s'.", dlname);
2659f4b3ec61Sdh155122 		(void) zone_remove_datalink(zoneid, dlname);
2660f4b3ec61Sdh155122 		return (-1);
2661f4b3ec61Sdh155122 	}
2662f4b3ec61Sdh155122 
2663f4b3ec61Sdh155122 	return (0);
2664f4b3ec61Sdh155122 }
2665f4b3ec61Sdh155122 
2666f4b3ec61Sdh155122 static int
2667f4b3ec61Sdh155122 remove_datalink(zlog_t *zlogp, zoneid_t zoneid, char *dlname)
2668f4b3ec61Sdh155122 {
2669f4b3ec61Sdh155122 	/*
2670f4b3ec61Sdh155122 	 * Remove access control information.
2671f4b3ec61Sdh155122 	 * If the errno is ENXIO, the interface is not added yet,
2672f4b3ec61Sdh155122 	 * nothing to report then.
2673f4b3ec61Sdh155122 	 */
2674f4b3ec61Sdh155122 	if (zone_remove_datalink(zoneid, dlname) != 0) {
2675f4b3ec61Sdh155122 		if (errno == ENXIO)
2676f4b3ec61Sdh155122 			return (0);
2677f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to remove network interface '%s'",
2678f4b3ec61Sdh155122 		    dlname);
2679f4b3ec61Sdh155122 		return (-1);
2680f4b3ec61Sdh155122 	}
2681f4b3ec61Sdh155122 
2682d62bc4baSyz147064 	if (dladm_setzid(dlname, GLOBAL_ZONEID) != DLADM_STATUS_OK) {
2683f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to release network "
2684f4b3ec61Sdh155122 		    "interface '%s'", dlname);
2685f4b3ec61Sdh155122 		return (-1);
2686f4b3ec61Sdh155122 	}
2687f4b3ec61Sdh155122 	return (0);
2688f4b3ec61Sdh155122 }
2689f4b3ec61Sdh155122 
2690f4b3ec61Sdh155122 /*
2691f4b3ec61Sdh155122  * Add the kernel access control information for the interface names.
2692f4b3ec61Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
2693f4b3ec61Sdh155122  * whatever we set up, and return an error.
2694f4b3ec61Sdh155122  */
2695f4b3ec61Sdh155122 static int
2696f4b3ec61Sdh155122 configure_exclusive_network_interfaces(zlog_t *zlogp)
2697f4b3ec61Sdh155122 {
2698f4b3ec61Sdh155122 	zone_dochandle_t handle;
2699f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2700f4b3ec61Sdh155122 	zoneid_t zoneid;
2701f4b3ec61Sdh155122 	char rootpath[MAXPATHLEN];
2702f4b3ec61Sdh155122 	char path[MAXPATHLEN];
2703f4b3ec61Sdh155122 	di_prof_t prof = NULL;
2704f4b3ec61Sdh155122 	boolean_t added = B_FALSE;
2705f4b3ec61Sdh155122 
2706f4b3ec61Sdh155122 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
2707f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to get zoneid");
2708f4b3ec61Sdh155122 		return (-1);
2709f4b3ec61Sdh155122 	}
2710f4b3ec61Sdh155122 
2711f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2712f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2713f4b3ec61Sdh155122 		return (-1);
2714f4b3ec61Sdh155122 	}
2715f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2716f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2717f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2718f4b3ec61Sdh155122 		return (-1);
2719f4b3ec61Sdh155122 	}
2720f4b3ec61Sdh155122 
2721f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2722f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2723f4b3ec61Sdh155122 		return (0);
2724f4b3ec61Sdh155122 	}
2725f4b3ec61Sdh155122 
2726f4b3ec61Sdh155122 	for (;;) {
2727f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2728f4b3ec61Sdh155122 			break;
2729f4b3ec61Sdh155122 
2730f4b3ec61Sdh155122 		if (prof == NULL) {
2731f4b3ec61Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
2732f4b3ec61Sdh155122 			    sizeof (rootpath)) != Z_OK) {
2733f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2734f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2735f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2736f4b3ec61Sdh155122 				    "unable to determine dev root");
2737f4b3ec61Sdh155122 				return (-1);
2738f4b3ec61Sdh155122 			}
2739f4b3ec61Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2740f4b3ec61Sdh155122 			    "/dev");
2741f4b3ec61Sdh155122 			if (di_prof_init(path, &prof) != 0) {
2742f4b3ec61Sdh155122 				(void) zonecfg_endnwifent(handle);
2743f4b3ec61Sdh155122 				zonecfg_fini_handle(handle);
2744f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE,
2745f4b3ec61Sdh155122 				    "failed to initialize profile");
2746f4b3ec61Sdh155122 				return (-1);
2747f4b3ec61Sdh155122 			}
2748f4b3ec61Sdh155122 		}
2749f4b3ec61Sdh155122 
2750f4b3ec61Sdh155122 		/*
2751d62bc4baSyz147064 		 * Create the /dev entry for backward compatibility.
2752f4b3ec61Sdh155122 		 * Only create the /dev entry if it's not in use.
2753d62bc4baSyz147064 		 * Note that the zone still boots when the assigned
2754d62bc4baSyz147064 		 * interface is inaccessible, used by others, etc.
2755d62bc4baSyz147064 		 * Also, when vanity naming is used, some interface do
2756d62bc4baSyz147064 		 * do not have corresponding /dev node names (for example,
2757d62bc4baSyz147064 		 * vanity named aggregations).  The /dev entry is not
2758d62bc4baSyz147064 		 * created in that case.  The /dev/net entry is always
2759d62bc4baSyz147064 		 * accessible.
2760f4b3ec61Sdh155122 		 */
2761f4b3ec61Sdh155122 		if (add_datalink(zlogp, zoneid, nwiftab.zone_nwif_physical)
2762f4b3ec61Sdh155122 		    == 0) {
2763d62bc4baSyz147064 			char		name[DLPI_LINKNAME_MAX];
2764d62bc4baSyz147064 			datalink_id_t	linkid;
2765d62bc4baSyz147064 
2766d62bc4baSyz147064 			if (dladm_name2info(nwiftab.zone_nwif_physical,
2767d62bc4baSyz147064 			    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
2768d62bc4baSyz147064 			    dladm_linkid2legacyname(linkid, name,
2769d62bc4baSyz147064 			    sizeof (name)) == DLADM_STATUS_OK) {
2770d62bc4baSyz147064 				if (di_prof_add_dev(prof, name) != 0) {
2771f4b3ec61Sdh155122 					(void) zonecfg_endnwifent(handle);
2772f4b3ec61Sdh155122 					zonecfg_fini_handle(handle);
2773f4b3ec61Sdh155122 					zerror(zlogp, B_TRUE,
2774f4b3ec61Sdh155122 					    "failed to add network device");
2775f4b3ec61Sdh155122 					return (-1);
2776f4b3ec61Sdh155122 				}
2777f4b3ec61Sdh155122 				added = B_TRUE;
2778f4b3ec61Sdh155122 			}
2779f4b3ec61Sdh155122 		}
2780d62bc4baSyz147064 	}
2781f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2782f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2783f4b3ec61Sdh155122 
2784f4b3ec61Sdh155122 	if (prof != NULL && added) {
2785f4b3ec61Sdh155122 		if (di_prof_commit(prof) != 0) {
2786f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
2787f4b3ec61Sdh155122 			return (-1);
2788f4b3ec61Sdh155122 		}
2789f4b3ec61Sdh155122 	}
2790f4b3ec61Sdh155122 	if (prof != NULL)
2791f4b3ec61Sdh155122 		di_prof_fini(prof);
2792f4b3ec61Sdh155122 
2793f4b3ec61Sdh155122 	return (0);
2794f4b3ec61Sdh155122 }
2795f4b3ec61Sdh155122 
2796f4b3ec61Sdh155122 /*
2797f4b3ec61Sdh155122  * Get the list of the data-links from kernel, and try to remove it
2798f4b3ec61Sdh155122  */
2799f4b3ec61Sdh155122 static int
2800f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces_run(zlog_t *zlogp, zoneid_t zoneid)
2801f4b3ec61Sdh155122 {
2802f4b3ec61Sdh155122 	char *dlnames, *ptr;
2803f4b3ec61Sdh155122 	int dlnum, dlnum_saved, i;
2804f4b3ec61Sdh155122 
2805f4b3ec61Sdh155122 	dlnum = 0;
2806f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
2807f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2808f4b3ec61Sdh155122 		return (-1);
2809f4b3ec61Sdh155122 	}
2810f4b3ec61Sdh155122 again:
2811f4b3ec61Sdh155122 	/* this zone doesn't have any data-links */
2812f4b3ec61Sdh155122 	if (dlnum == 0)
2813f4b3ec61Sdh155122 		return (0);
2814f4b3ec61Sdh155122 
2815f4b3ec61Sdh155122 	dlnames = malloc(dlnum * LIFNAMSIZ);
2816f4b3ec61Sdh155122 	if (dlnames == NULL) {
2817f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "memory allocation failed");
2818f4b3ec61Sdh155122 		return (-1);
2819f4b3ec61Sdh155122 	}
2820f4b3ec61Sdh155122 	dlnum_saved = dlnum;
2821f4b3ec61Sdh155122 
2822f4b3ec61Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, dlnames) != 0) {
2823f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
2824f4b3ec61Sdh155122 		free(dlnames);
2825f4b3ec61Sdh155122 		return (-1);
2826f4b3ec61Sdh155122 	}
2827f4b3ec61Sdh155122 	if (dlnum_saved < dlnum) {
2828f4b3ec61Sdh155122 		/* list increased, try again */
2829f4b3ec61Sdh155122 		free(dlnames);
2830f4b3ec61Sdh155122 		goto again;
2831f4b3ec61Sdh155122 	}
2832f4b3ec61Sdh155122 	ptr = dlnames;
2833f4b3ec61Sdh155122 	for (i = 0; i < dlnum; i++) {
2834f4b3ec61Sdh155122 		/* Remove access control information */
2835f4b3ec61Sdh155122 		if (remove_datalink(zlogp, zoneid, ptr) != 0) {
2836f4b3ec61Sdh155122 			free(dlnames);
2837f4b3ec61Sdh155122 			return (-1);
2838f4b3ec61Sdh155122 		}
2839f4b3ec61Sdh155122 		ptr += LIFNAMSIZ;
2840f4b3ec61Sdh155122 	}
2841f4b3ec61Sdh155122 	free(dlnames);
2842f4b3ec61Sdh155122 	return (0);
2843f4b3ec61Sdh155122 }
2844f4b3ec61Sdh155122 
2845f4b3ec61Sdh155122 /*
2846f4b3ec61Sdh155122  * Get the list of the data-links from configuration, and try to remove it
2847f4b3ec61Sdh155122  */
2848f4b3ec61Sdh155122 static int
2849f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces_static(zlog_t *zlogp, zoneid_t zoneid)
2850f4b3ec61Sdh155122 {
2851f4b3ec61Sdh155122 	zone_dochandle_t handle;
2852f4b3ec61Sdh155122 	struct zone_nwiftab nwiftab;
2853f4b3ec61Sdh155122 
2854f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
2855f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2856f4b3ec61Sdh155122 		return (-1);
2857f4b3ec61Sdh155122 	}
2858f4b3ec61Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2859f4b3ec61Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
2860f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2861f4b3ec61Sdh155122 		return (-1);
2862f4b3ec61Sdh155122 	}
2863f4b3ec61Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
2864f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2865f4b3ec61Sdh155122 		return (0);
2866f4b3ec61Sdh155122 	}
2867f4b3ec61Sdh155122 	for (;;) {
2868f4b3ec61Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2869f4b3ec61Sdh155122 			break;
2870f4b3ec61Sdh155122 		/* Remove access control information */
2871f4b3ec61Sdh155122 		if (remove_datalink(zlogp, zoneid, nwiftab.zone_nwif_physical)
2872f4b3ec61Sdh155122 		    != 0) {
2873f4b3ec61Sdh155122 			(void) zonecfg_endnwifent(handle);
2874f4b3ec61Sdh155122 			zonecfg_fini_handle(handle);
2875f4b3ec61Sdh155122 			return (-1);
2876f4b3ec61Sdh155122 		}
2877f4b3ec61Sdh155122 	}
2878f4b3ec61Sdh155122 	(void) zonecfg_endnwifent(handle);
2879f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
2880f4b3ec61Sdh155122 	return (0);
2881f4b3ec61Sdh155122 }
2882f4b3ec61Sdh155122 
2883f4b3ec61Sdh155122 /*
2884f4b3ec61Sdh155122  * Remove the access control information from the kernel for the exclusive
2885f4b3ec61Sdh155122  * network interfaces.
2886f4b3ec61Sdh155122  */
2887f4b3ec61Sdh155122 static int
2888f4b3ec61Sdh155122 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2889f4b3ec61Sdh155122 {
2890f4b3ec61Sdh155122 	if (unconfigure_exclusive_network_interfaces_run(zlogp, zoneid) != 0) {
2891f4b3ec61Sdh155122 		return (unconfigure_exclusive_network_interfaces_static(zlogp,
2892f4b3ec61Sdh155122 		    zoneid));
2893f4b3ec61Sdh155122 	}
2894f4b3ec61Sdh155122 
2895f4b3ec61Sdh155122 	return (0);
2896f4b3ec61Sdh155122 }
2897f4b3ec61Sdh155122 
28987c478bd9Sstevel@tonic-gate static int
28997c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
29007c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
29017c478bd9Sstevel@tonic-gate {
29027c478bd9Sstevel@tonic-gate 	int fd;
29037c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
29047c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
29057c478bd9Sstevel@tonic-gate 	int error;
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
29087c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
29097c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
29107c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
29117c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
29147c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
29157c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
29167c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
29197c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
29207c478bd9Sstevel@tonic-gate 		return (-1);
29217c478bd9Sstevel@tonic-gate 	}
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
29247c478bd9Sstevel@tonic-gate 	(void) close(fd);
29257c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
29267c478bd9Sstevel@tonic-gate 		return (0);
29277c478bd9Sstevel@tonic-gate 	return (-1);
29287c478bd9Sstevel@tonic-gate }
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate static int
29317c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
29327c478bd9Sstevel@tonic-gate {
29337c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
29347c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
29357c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
29367c478bd9Sstevel@tonic-gate 	int error;
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	/*
29397c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
29407c478bd9Sstevel@tonic-gate 	 */
29417c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
29427c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
29437c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
29447c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
29457c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
29487c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
29497c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
29507c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
29517c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
29547c478bd9Sstevel@tonic-gate 		return (error);
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate 	/*
29577c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
29587c478bd9Sstevel@tonic-gate 	 */
29597c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
29607c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
29617c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
29627c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
29637c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
29667c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
29677c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
29687c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
29697c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
29727c478bd9Sstevel@tonic-gate 		return (error);
29737c478bd9Sstevel@tonic-gate 	return (0);
29747c478bd9Sstevel@tonic-gate }
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate static int
29776cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
2978ffbafc53Scomay {
2979ffbafc53Scomay 	int error = -1;
2980ffbafc53Scomay 	zone_dochandle_t handle;
2981ffbafc53Scomay 	char *privname = NULL;
2982ffbafc53Scomay 
2983ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
2984ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2985ffbafc53Scomay 		return (-1);
2986ffbafc53Scomay 	}
2987ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2988ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
2989ffbafc53Scomay 		zonecfg_fini_handle(handle);
2990ffbafc53Scomay 		return (-1);
2991ffbafc53Scomay 	}
2992ffbafc53Scomay 
29936cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
2994bf1d7e28Sdh155122 		zone_iptype_t	iptype;
2995bf1d7e28Sdh155122 		const char	*curr_iptype;
2996bf1d7e28Sdh155122 
2997bf1d7e28Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
2998bf1d7e28Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
2999bf1d7e28Sdh155122 			zonecfg_fini_handle(handle);
3000bf1d7e28Sdh155122 			return (-1);
3001bf1d7e28Sdh155122 		}
3002bf1d7e28Sdh155122 
3003bf1d7e28Sdh155122 		switch (iptype) {
3004bf1d7e28Sdh155122 		case ZS_SHARED:
3005bf1d7e28Sdh155122 			curr_iptype = "shared";
3006bf1d7e28Sdh155122 			break;
3007bf1d7e28Sdh155122 		case ZS_EXCLUSIVE:
3008bf1d7e28Sdh155122 			curr_iptype = "exclusive";
3009bf1d7e28Sdh155122 			break;
3010bf1d7e28Sdh155122 		}
3011bf1d7e28Sdh155122 
3012e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
3013e767a340Sgjelinek 			zonecfg_fini_handle(handle);
30149acbbeafSnn35248 			return (0);
3015e767a340Sgjelinek 		}
30169acbbeafSnn35248 		zerror(zlogp, B_FALSE,
30179acbbeafSnn35248 		    "failed to determine the zone's default privilege set");
30189acbbeafSnn35248 		zonecfg_fini_handle(handle);
30199acbbeafSnn35248 		return (-1);
30209acbbeafSnn35248 	}
30219acbbeafSnn35248 
3022ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
3023ffbafc53Scomay 	case Z_OK:
3024ffbafc53Scomay 		error = 0;
3025ffbafc53Scomay 		break;
3026ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3027ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3028ffbafc53Scomay 		    "within the zone's privilege set", privname);
3029ffbafc53Scomay 		break;
3030ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3031ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3032ffbafc53Scomay 		    "from the zone's privilege set", privname);
3033ffbafc53Scomay 		break;
3034ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3035ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3036ffbafc53Scomay 		    "in the zone's privilege set", privname);
3037ffbafc53Scomay 		break;
3038ffbafc53Scomay 	default:
3039ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
3040ffbafc53Scomay 		    "privilege set");
3041ffbafc53Scomay 		break;
3042ffbafc53Scomay 	}
3043ffbafc53Scomay 
3044ffbafc53Scomay 	free(privname);
3045ffbafc53Scomay 	zonecfg_fini_handle(handle);
3046ffbafc53Scomay 	return (error);
3047ffbafc53Scomay }
3048ffbafc53Scomay 
3049ffbafc53Scomay static int
30507c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
30517c478bd9Sstevel@tonic-gate {
30527c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
30537c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
30547c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
30557c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
30567c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
30577c478bd9Sstevel@tonic-gate 	int error = -1;
30587c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
30597c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
30607c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 	*bufp = NULL;
30637c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
30667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
30677c478bd9Sstevel@tonic-gate 		return (-1);
30687c478bd9Sstevel@tonic-gate 	}
30697c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
30707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
30717c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
30727c478bd9Sstevel@tonic-gate 		return (-1);
30737c478bd9Sstevel@tonic-gate 	}
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
30767c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
30777c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
30787c478bd9Sstevel@tonic-gate 		goto out;
30797c478bd9Sstevel@tonic-gate 	}
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
30827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
30837c478bd9Sstevel@tonic-gate 		goto out;
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
30877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
30887c478bd9Sstevel@tonic-gate 		goto out;
30897c478bd9Sstevel@tonic-gate 	}
30907c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
30917c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
30927c478bd9Sstevel@tonic-gate 		uint_t i, count;
30937c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
30967c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
30977c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30987c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
30997c478bd9Sstevel@tonic-gate 			continue;
31007c478bd9Sstevel@tonic-gate 		}
31017c478bd9Sstevel@tonic-gate 		count = 0;
31027c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
31037c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
31047c478bd9Sstevel@tonic-gate 			count++;
31057c478bd9Sstevel@tonic-gate 		}
31067c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
31077c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
31087c478bd9Sstevel@tonic-gate 		}
31097c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
31107c478bd9Sstevel@tonic-gate 			goto out;
31117c478bd9Sstevel@tonic-gate 		i = 0;
31127c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
31137c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
31147c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
31157c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
31167c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
31177c478bd9Sstevel@tonic-gate 				goto out;
31187c478bd9Sstevel@tonic-gate 			}
31197c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
31207c478bd9Sstevel@tonic-gate 			    != Z_OK) {
31217c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
31227c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
31237c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
31247c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
31257c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
31267c478bd9Sstevel@tonic-gate 				goto out;
31277c478bd9Sstevel@tonic-gate 			}
31287c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
31297c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
31307c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
31317c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
31327c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
31337c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
31347c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
31357c478bd9Sstevel@tonic-gate 				    name);
31367c478bd9Sstevel@tonic-gate 				goto out;
31377c478bd9Sstevel@tonic-gate 			}
31387c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
31397c478bd9Sstevel@tonic-gate 			    rctlblk_get_privilege(rctlblk)) != 0) {
31407c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
31417c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
31427c478bd9Sstevel@tonic-gate 				goto out;
31437c478bd9Sstevel@tonic-gate 			}
31447c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
31457c478bd9Sstevel@tonic-gate 			    rctlblk_get_value(rctlblk)) != 0) {
31467c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
31477c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
31487c478bd9Sstevel@tonic-gate 				goto out;
31497c478bd9Sstevel@tonic-gate 			}
31507c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
31517c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
31527c478bd9Sstevel@tonic-gate 			    != 0) {
31537c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
31547c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
31557c478bd9Sstevel@tonic-gate 				goto out;
31567c478bd9Sstevel@tonic-gate 			}
31577c478bd9Sstevel@tonic-gate 		}
31587c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
31597c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
31607c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
31617c478bd9Sstevel@tonic-gate 		    != 0) {
31627c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
31637c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
31647c478bd9Sstevel@tonic-gate 			goto out;
31657c478bd9Sstevel@tonic-gate 		}
31667c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
31677c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
31687c478bd9Sstevel@tonic-gate 		free(nvlv);
31697c478bd9Sstevel@tonic-gate 		nvlv = NULL;
31707c478bd9Sstevel@tonic-gate 		rctlcount++;
31717c478bd9Sstevel@tonic-gate 	}
31727c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
31757c478bd9Sstevel@tonic-gate 		error = 0;
31767c478bd9Sstevel@tonic-gate 		goto out;
31777c478bd9Sstevel@tonic-gate 	}
31787c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
31797c478bd9Sstevel@tonic-gate 	    != 0) {
31807c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
31817c478bd9Sstevel@tonic-gate 		goto out;
31827c478bd9Sstevel@tonic-gate 	}
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	error = 0;
31857c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
31867c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate out:
31897c478bd9Sstevel@tonic-gate 	free(rctlblk);
31907c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
31917c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
31927c478bd9Sstevel@tonic-gate 		free(nvl_packed);
31937c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
31947c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
31957c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
31967c478bd9Sstevel@tonic-gate 		free(nvlv);
31977c478bd9Sstevel@tonic-gate 	if (handle != NULL)
31987c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
31997c478bd9Sstevel@tonic-gate 	return (error);
32007c478bd9Sstevel@tonic-gate }
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate static int
3203fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3204fa9e4066Sahrens {
3205fa9e4066Sahrens 	zone_dochandle_t handle;
3206fa9e4066Sahrens 	struct zone_dstab dstab;
3207fa9e4066Sahrens 	size_t total, offset, len;
3208fa9e4066Sahrens 	int error = -1;
3209e5a17f6aSgjelinek 	char *str = NULL;
3210fa9e4066Sahrens 
3211fa9e4066Sahrens 	*bufp = NULL;
3212fa9e4066Sahrens 	*bufsizep = 0;
3213fa9e4066Sahrens 
3214fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3215fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3216fa9e4066Sahrens 		return (-1);
3217fa9e4066Sahrens 	}
3218fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3219fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3220fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3221fa9e4066Sahrens 		return (-1);
3222fa9e4066Sahrens 	}
3223fa9e4066Sahrens 
3224fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3225fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3226fa9e4066Sahrens 		goto out;
3227fa9e4066Sahrens 	}
3228fa9e4066Sahrens 
3229fa9e4066Sahrens 	total = 0;
3230fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3231fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3232fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3233fa9e4066Sahrens 
3234fa9e4066Sahrens 	if (total == 0) {
3235fa9e4066Sahrens 		error = 0;
3236fa9e4066Sahrens 		goto out;
3237fa9e4066Sahrens 	}
3238fa9e4066Sahrens 
3239fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3240fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3241fa9e4066Sahrens 		goto out;
3242fa9e4066Sahrens 	}
3243fa9e4066Sahrens 
3244fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3245fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3246fa9e4066Sahrens 		goto out;
3247fa9e4066Sahrens 	}
3248fa9e4066Sahrens 	offset = 0;
3249fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3250fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3251fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3252e5a17f6aSgjelinek 		    total - offset);
3253fa9e4066Sahrens 		offset += len;
3254e5a17f6aSgjelinek 		if (offset < total - 1)
3255fa9e4066Sahrens 			str[offset++] = ',';
3256fa9e4066Sahrens 	}
3257fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3258fa9e4066Sahrens 
3259fa9e4066Sahrens 	error = 0;
3260fa9e4066Sahrens 	*bufp = str;
3261fa9e4066Sahrens 	*bufsizep = total;
3262fa9e4066Sahrens 
3263fa9e4066Sahrens out:
3264fa9e4066Sahrens 	if (error != 0 && str != NULL)
3265fa9e4066Sahrens 		free(str);
3266fa9e4066Sahrens 	if (handle != NULL)
3267fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3268fa9e4066Sahrens 
3269fa9e4066Sahrens 	return (error);
3270fa9e4066Sahrens }
3271fa9e4066Sahrens 
3272fa9e4066Sahrens static int
3273fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3274fa9e4066Sahrens {
3275fa9e4066Sahrens 	zone_dochandle_t handle;
3276fa9e4066Sahrens 	struct zone_dstab dstab;
3277fa9e4066Sahrens 	zfs_handle_t *zhp;
327899653d4eSeschrock 	libzfs_handle_t *hdl;
3279fa9e4066Sahrens 
3280fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3281fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3282fa9e4066Sahrens 		return (-1);
3283fa9e4066Sahrens 	}
3284fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3285fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3286fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3287fa9e4066Sahrens 		return (-1);
3288fa9e4066Sahrens 	}
3289fa9e4066Sahrens 
3290fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3291fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3292fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3293fa9e4066Sahrens 		return (-1);
3294fa9e4066Sahrens 	}
3295fa9e4066Sahrens 
329699653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
329799653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
329899653d4eSeschrock 		zonecfg_fini_handle(handle);
329999653d4eSeschrock 		return (-1);
330099653d4eSeschrock 	}
3301fa9e4066Sahrens 
3302fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3303fa9e4066Sahrens 
330499653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3305fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3306fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3307fa9e4066Sahrens 			    dstab.zone_dataset_name);
3308fa9e4066Sahrens 			zonecfg_fini_handle(handle);
330999653d4eSeschrock 			libzfs_fini(hdl);
3310fa9e4066Sahrens 			return (-1);
3311fa9e4066Sahrens 		}
3312fa9e4066Sahrens 
3313fa9e4066Sahrens 		/*
3314fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3315fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3316fa9e4066Sahrens 		 */
3317fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3318e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3319e9dbad6fSeschrock 		    "on") != 0) {
3320fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3321fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3322fa9e4066Sahrens 			    dstab.zone_dataset_name);
3323fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3324fa9e4066Sahrens 			zfs_close(zhp);
332599653d4eSeschrock 			libzfs_fini(hdl);
3326fa9e4066Sahrens 			return (-1);
3327fa9e4066Sahrens 		}
3328fa9e4066Sahrens 
3329fa9e4066Sahrens 		zfs_close(zhp);
3330fa9e4066Sahrens 	}
3331fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3332fa9e4066Sahrens 
3333fa9e4066Sahrens 	zonecfg_fini_handle(handle);
333499653d4eSeschrock 	libzfs_fini(hdl);
3335fa9e4066Sahrens 
3336fa9e4066Sahrens 	return (0);
3337fa9e4066Sahrens }
3338fa9e4066Sahrens 
333945916cd2Sjpk /*
334045916cd2Sjpk  * Mount lower level home directories into/from current zone
334145916cd2Sjpk  * Share exported directories specified in dfstab for zone
334245916cd2Sjpk  */
334345916cd2Sjpk static int
334445916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
334545916cd2Sjpk {
334645916cd2Sjpk 	zoneid_t *zids = NULL;
334745916cd2Sjpk 	priv_set_t *zid_privs;
334845916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
334945916cd2Sjpk 	uint_t nzents_saved;
335045916cd2Sjpk 	uint_t nzents;
335145916cd2Sjpk 	int i;
335245916cd2Sjpk 	char readonly[] = "ro";
335345916cd2Sjpk 	struct zone_fstab lower_fstab;
335445916cd2Sjpk 	char *argv[4];
335545916cd2Sjpk 
335645916cd2Sjpk 	if (!is_system_labeled())
335745916cd2Sjpk 		return (0);
335845916cd2Sjpk 
335945916cd2Sjpk 	if (zid_label == NULL) {
336045916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
336145916cd2Sjpk 		if (zid_label == NULL)
336245916cd2Sjpk 			return (-1);
336345916cd2Sjpk 	}
336445916cd2Sjpk 
336545916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
336645916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
336727e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
336845916cd2Sjpk 
336945916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
337045916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
337145916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
337245916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
337345916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
337445916cd2Sjpk 
337545916cd2Sjpk 	/*
337645916cd2Sjpk 	 * Get the list of zones from the kernel
337745916cd2Sjpk 	 */
337845916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
337945916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
338045916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
338145916cd2Sjpk 		return (-1);
338245916cd2Sjpk 	}
338345916cd2Sjpk again:
338445916cd2Sjpk 	if (nzents == 0) {
338545916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
338645916cd2Sjpk 		return (-1);
338745916cd2Sjpk 	}
338845916cd2Sjpk 
338945916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
339045916cd2Sjpk 	if (zids == NULL) {
33913f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
339245916cd2Sjpk 		return (-1);
339345916cd2Sjpk 	}
339445916cd2Sjpk 	nzents_saved = nzents;
339545916cd2Sjpk 
339645916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
339745916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
339845916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
339945916cd2Sjpk 		free(zids);
340045916cd2Sjpk 		return (-1);
340145916cd2Sjpk 	}
340245916cd2Sjpk 	if (nzents != nzents_saved) {
340345916cd2Sjpk 		/* list changed, try again */
340445916cd2Sjpk 		free(zids);
340545916cd2Sjpk 		goto again;
340645916cd2Sjpk 	}
340745916cd2Sjpk 
340845916cd2Sjpk 	ip = getprivimplinfo();
340945916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
341045916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
341145916cd2Sjpk 		zonecfg_free_fs_option_list(
341245916cd2Sjpk 		    lower_fstab.zone_fs_options);
341345916cd2Sjpk 		free(zids);
341445916cd2Sjpk 		return (-1);
341545916cd2Sjpk 	}
341645916cd2Sjpk 
341745916cd2Sjpk 	for (i = 0; i < nzents; i++) {
341845916cd2Sjpk 		char zid_name[ZONENAME_MAX];
341945916cd2Sjpk 		zone_state_t zid_state;
342045916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
342145916cd2Sjpk 		struct stat stat_buf;
342245916cd2Sjpk 
342345916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
342445916cd2Sjpk 			continue;
342545916cd2Sjpk 
342645916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
342745916cd2Sjpk 			continue;
342845916cd2Sjpk 
342945916cd2Sjpk 		/*
343045916cd2Sjpk 		 * Do special setup for the zone we are booting
343145916cd2Sjpk 		 */
343245916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
343345916cd2Sjpk 			struct zone_fstab autofs_fstab;
343445916cd2Sjpk 			char map_path[MAXPATHLEN];
343545916cd2Sjpk 			int fd;
343645916cd2Sjpk 
343745916cd2Sjpk 			/*
343845916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
34399acbbeafSnn35248 			 * in the global zone. The non-global zone entry
344045916cd2Sjpk 			 * will be created by automount when the zone
344145916cd2Sjpk 			 * is booted.
344245916cd2Sjpk 			 */
344345916cd2Sjpk 
344445916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
344545916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
344645916cd2Sjpk 
344745916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
344845916cd2Sjpk 			    "/zone/%s/home", zid_name);
344945916cd2Sjpk 
345045916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
345145916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
345245916cd2Sjpk 			/*
345345916cd2Sjpk 			 * If the map file doesn't exist create a template
345445916cd2Sjpk 			 */
345545916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
345645916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
345745916cd2Sjpk 				int len;
345845916cd2Sjpk 				char map_rec[MAXPATHLEN];
345945916cd2Sjpk 
346045916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
346145916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
346245916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
346345916cd2Sjpk 				(void) write(fd, map_rec, len);
346445916cd2Sjpk 				(void) close(fd);
346545916cd2Sjpk 			}
346645916cd2Sjpk 
346745916cd2Sjpk 			/*
346845916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
346945916cd2Sjpk 			 * If it's already of type autofs, then
347045916cd2Sjpk 			 * don't mount it again.
347145916cd2Sjpk 			 */
347245916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
347345916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
347445916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
347545916cd2Sjpk 
347645916cd2Sjpk 				(void) make_one_dir(zlogp, "",
347727e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
347827e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
347945916cd2Sjpk 
348045916cd2Sjpk 				/*
348145916cd2Sjpk 				 * Mount will fail if automounter has already
348245916cd2Sjpk 				 * processed the auto_home_<zonename> map
348345916cd2Sjpk 				 */
348445916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
348545916cd2Sjpk 				    autofs_fstab.zone_fs_special,
348645916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
348745916cd2Sjpk 			}
348845916cd2Sjpk 			continue;
348945916cd2Sjpk 		}
349045916cd2Sjpk 
349145916cd2Sjpk 
349245916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
349348451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
349448451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
349545916cd2Sjpk 			/* Skip over zones without mounted filesystems */
349645916cd2Sjpk 			continue;
349745916cd2Sjpk 
349845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
349945916cd2Sjpk 		    sizeof (m_label_t)) < 0)
350045916cd2Sjpk 			/* Skip over zones with unspecified label */
350145916cd2Sjpk 			continue;
350245916cd2Sjpk 
350345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
350445916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
350545916cd2Sjpk 			/* Skip over zones with bad path */
350645916cd2Sjpk 			continue;
350745916cd2Sjpk 
350845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
350945916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
351045916cd2Sjpk 			/* Skip over zones with bad privs */
351145916cd2Sjpk 			continue;
351245916cd2Sjpk 
351345916cd2Sjpk 		/*
351445916cd2Sjpk 		 * Reading down is valid according to our label model
351545916cd2Sjpk 		 * but some customers want to disable it because it
351645916cd2Sjpk 		 * allows execute down and other possible attacks.
351745916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
351845916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
351945916cd2Sjpk 		 * for NFS read-down semantics.
352045916cd2Sjpk 		 */
352145916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
352245916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
352345916cd2Sjpk 			/*
352445916cd2Sjpk 			 * Our zone dominates this one.
352545916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
352645916cd2Sjpk 			 */
352745916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
352845916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
352945916cd2Sjpk 
353045916cd2Sjpk 			/*
353145916cd2Sjpk 			 * If the target is already an LOFS mount
353245916cd2Sjpk 			 * then don't do it again.
353345916cd2Sjpk 			 */
353445916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
353545916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
353645916cd2Sjpk 
353745916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
353845916cd2Sjpk 				    MAXPATHLEN, "%s/export",
353945916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
354045916cd2Sjpk 					continue;
354145916cd2Sjpk 
354245916cd2Sjpk 				/*
354345916cd2Sjpk 				 * Make sure the lower-level home exists
354445916cd2Sjpk 				 */
354545916cd2Sjpk 				if (make_one_dir(zlogp,
354627e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
354727e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
354827e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
354945916cd2Sjpk 					continue;
355045916cd2Sjpk 
355145916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
355245916cd2Sjpk 				    "/home", MAXPATHLEN);
355345916cd2Sjpk 
355445916cd2Sjpk 				/*
355545916cd2Sjpk 				 * Mount can fail because the lower-level
355645916cd2Sjpk 				 * zone may have already done a mount up.
355745916cd2Sjpk 				 */
355845916cd2Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
355945916cd2Sjpk 			}
356045916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
356145916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
356245916cd2Sjpk 			/*
356345916cd2Sjpk 			 * This zone dominates our zone.
356445916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
356545916cd2Sjpk 			 */
356645916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
356745916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
356845916cd2Sjpk 			    zone_name) > MAXPATHLEN)
356945916cd2Sjpk 				continue;
357045916cd2Sjpk 
357145916cd2Sjpk 			/*
357245916cd2Sjpk 			 * If the target is already an LOFS mount
357345916cd2Sjpk 			 * then don't do it again.
357445916cd2Sjpk 			 */
357545916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
357645916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
357745916cd2Sjpk 
357845916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
357945916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
358045916cd2Sjpk 
358145916cd2Sjpk 				/*
358245916cd2Sjpk 				 * Mount can fail because the higher-level
358345916cd2Sjpk 				 * zone may have already done a mount down.
358445916cd2Sjpk 				 */
358545916cd2Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
358645916cd2Sjpk 			}
358745916cd2Sjpk 		}
358845916cd2Sjpk 	}
358945916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
359045916cd2Sjpk 	priv_freeset(zid_privs);
359145916cd2Sjpk 	free(zids);
359245916cd2Sjpk 
359345916cd2Sjpk 	/*
359445916cd2Sjpk 	 * Now share any exported directories from this zone.
359545916cd2Sjpk 	 * Each zone can have its own dfstab.
359645916cd2Sjpk 	 */
359745916cd2Sjpk 
359845916cd2Sjpk 	argv[0] = "zoneshare";
359945916cd2Sjpk 	argv[1] = "-z";
360045916cd2Sjpk 	argv[2] = zone_name;
360145916cd2Sjpk 	argv[3] = NULL;
360245916cd2Sjpk 
360345916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
360445916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
360545916cd2Sjpk 
360645916cd2Sjpk 	return (0);
360745916cd2Sjpk }
360845916cd2Sjpk 
360945916cd2Sjpk /*
361045916cd2Sjpk  * Unmount lofs mounts from higher level zones
361145916cd2Sjpk  * Unshare nfs exported directories
361245916cd2Sjpk  */
361345916cd2Sjpk static void
361445916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
361545916cd2Sjpk {
361645916cd2Sjpk 	zoneid_t *zids = NULL;
361745916cd2Sjpk 	uint_t nzents_saved;
361845916cd2Sjpk 	uint_t nzents;
361945916cd2Sjpk 	int i;
362045916cd2Sjpk 	char *argv[4];
362145916cd2Sjpk 	char path[MAXPATHLEN];
362245916cd2Sjpk 
362345916cd2Sjpk 	if (!is_system_labeled())
362445916cd2Sjpk 		return;
362545916cd2Sjpk 
362645916cd2Sjpk 	/*
362745916cd2Sjpk 	 * Get the list of zones from the kernel
362845916cd2Sjpk 	 */
362945916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
363045916cd2Sjpk 		return;
363145916cd2Sjpk 	}
363245916cd2Sjpk 
363345916cd2Sjpk 	if (zid_label == NULL) {
363445916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
363545916cd2Sjpk 		if (zid_label == NULL)
363645916cd2Sjpk 			return;
363745916cd2Sjpk 	}
363845916cd2Sjpk 
363945916cd2Sjpk again:
364045916cd2Sjpk 	if (nzents == 0)
364145916cd2Sjpk 		return;
364245916cd2Sjpk 
364345916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
364445916cd2Sjpk 	if (zids == NULL) {
36453f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
364645916cd2Sjpk 		return;
364745916cd2Sjpk 	}
364845916cd2Sjpk 	nzents_saved = nzents;
364945916cd2Sjpk 
365045916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
365145916cd2Sjpk 		free(zids);
365245916cd2Sjpk 		return;
365345916cd2Sjpk 	}
365445916cd2Sjpk 	if (nzents != nzents_saved) {
365545916cd2Sjpk 		/* list changed, try again */
365645916cd2Sjpk 		free(zids);
365745916cd2Sjpk 		goto again;
365845916cd2Sjpk 	}
365945916cd2Sjpk 
366045916cd2Sjpk 	for (i = 0; i < nzents; i++) {
366145916cd2Sjpk 		char zid_name[ZONENAME_MAX];
366245916cd2Sjpk 		zone_state_t zid_state;
366345916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
366445916cd2Sjpk 
366545916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
366645916cd2Sjpk 			continue;
366745916cd2Sjpk 
366845916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
366945916cd2Sjpk 			continue;
367045916cd2Sjpk 
367145916cd2Sjpk 		/*
367245916cd2Sjpk 		 * Skip the zone we are halting
367345916cd2Sjpk 		 */
367445916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
367545916cd2Sjpk 			continue;
367645916cd2Sjpk 
367745916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
367845916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
367945916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
368045916cd2Sjpk 			/* Skip over zones without mounted filesystems */
368145916cd2Sjpk 			continue;
368245916cd2Sjpk 
368345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
368445916cd2Sjpk 		    sizeof (m_label_t)) < 0)
368545916cd2Sjpk 			/* Skip over zones with unspecified label */
368645916cd2Sjpk 			continue;
368745916cd2Sjpk 
368845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
368945916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
369045916cd2Sjpk 			/* Skip over zones with bad path */
369145916cd2Sjpk 			continue;
369245916cd2Sjpk 
369345916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
369445916cd2Sjpk 			/*
369545916cd2Sjpk 			 * This zone dominates our zone.
369645916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
369745916cd2Sjpk 			 */
369845916cd2Sjpk 
369945916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
370045916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
370145916cd2Sjpk 			    zone_name) > MAXPATHLEN)
370245916cd2Sjpk 				continue;
370345916cd2Sjpk 
370445916cd2Sjpk 			/* Skip over mount failures */
370545916cd2Sjpk 			(void) umount(path);
370645916cd2Sjpk 		}
370745916cd2Sjpk 	}
370845916cd2Sjpk 	free(zids);
370945916cd2Sjpk 
371045916cd2Sjpk 	/*
371145916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
371245916cd2Sjpk 	 */
371345916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
371445916cd2Sjpk 	/* Skip over mount failures */
371545916cd2Sjpk 	(void) umount(path);
371645916cd2Sjpk 
371745916cd2Sjpk 	/*
371845916cd2Sjpk 	 * Next unshare any exported directories from this zone.
371945916cd2Sjpk 	 */
372045916cd2Sjpk 
372145916cd2Sjpk 	argv[0] = "zoneunshare";
372245916cd2Sjpk 	argv[1] = "-z";
372345916cd2Sjpk 	argv[2] = zone_name;
372445916cd2Sjpk 	argv[3] = NULL;
372545916cd2Sjpk 
372645916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
372745916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
372845916cd2Sjpk 
372945916cd2Sjpk 	/*
373045916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
373145916cd2Sjpk 	 */
373245916cd2Sjpk 
373345916cd2Sjpk 	argv[0] = "deallocate";
373445916cd2Sjpk 	argv[1] = "-Isz";
373545916cd2Sjpk 	argv[2] = zone_name;
373645916cd2Sjpk 	argv[3] = NULL;
373745916cd2Sjpk 
373845916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
373945916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
374045916cd2Sjpk }
374145916cd2Sjpk 
374245916cd2Sjpk /*
374345916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
374445916cd2Sjpk  * this zone.
374545916cd2Sjpk  */
374645916cd2Sjpk static tsol_zcent_t *
374745916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
374845916cd2Sjpk {
374945916cd2Sjpk 	FILE *fp;
375045916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
375145916cd2Sjpk 	char line[MAXTNZLEN];
375245916cd2Sjpk 
375345916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
375445916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
375545916cd2Sjpk 		return (NULL);
375645916cd2Sjpk 	}
375745916cd2Sjpk 
375845916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
375945916cd2Sjpk 		/*
376045916cd2Sjpk 		 * Check for malformed database
376145916cd2Sjpk 		 */
376245916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
376345916cd2Sjpk 			break;
376445916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
376545916cd2Sjpk 			continue;
376645916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
376745916cd2Sjpk 			break;
376845916cd2Sjpk 		tsol_freezcent(zcent);
376945916cd2Sjpk 		zcent = NULL;
377045916cd2Sjpk 	}
377145916cd2Sjpk 	(void) fclose(fp);
377245916cd2Sjpk 
377345916cd2Sjpk 	if (zcent == NULL) {
377445916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
377545916cd2Sjpk 		    "See tnzonecfg(4)");
377645916cd2Sjpk 	} else {
377745916cd2Sjpk 		if (zlabel == NULL)
377845916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
377945916cd2Sjpk 		/*
378045916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
378145916cd2Sjpk 		 */
378245916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
378345916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
378445916cd2Sjpk 			return (NULL);
378545916cd2Sjpk 		} else {
378645916cd2Sjpk 			priv_copyset(privs, zprivs);
378745916cd2Sjpk 		}
378845916cd2Sjpk 	}
378945916cd2Sjpk 	return (zcent);
379045916cd2Sjpk }
379145916cd2Sjpk 
379245916cd2Sjpk /*
379345916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
379445916cd2Sjpk  */
379545916cd2Sjpk static void
379645916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
379745916cd2Sjpk {
379845916cd2Sjpk 	tsol_mlp_t *mlp;
379945916cd2Sjpk 	tsol_mlpent_t tsme;
380045916cd2Sjpk 
380145916cd2Sjpk 	if (!is_system_labeled())
380245916cd2Sjpk 		return;
380345916cd2Sjpk 
380445916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
380545916cd2Sjpk 	tsme.tsme_flags = 0;
380645916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
380745916cd2Sjpk 		tsme.tsme_mlp = *mlp;
380845916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
380945916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
381045916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
381145916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
381245916cd2Sjpk 		}
381345916cd2Sjpk 	}
381445916cd2Sjpk 
381545916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
381645916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
381745916cd2Sjpk 		tsme.tsme_mlp = *mlp;
381845916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
381945916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
382045916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
382145916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
382245916cd2Sjpk 		}
382345916cd2Sjpk 	}
382445916cd2Sjpk }
382545916cd2Sjpk 
382645916cd2Sjpk static void
382745916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
382845916cd2Sjpk {
382945916cd2Sjpk 	tsol_mlpent_t tsme;
383045916cd2Sjpk 
383145916cd2Sjpk 	if (!is_system_labeled())
383245916cd2Sjpk 		return;
383345916cd2Sjpk 
383445916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
383545916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
383645916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
383745916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
383845916cd2Sjpk }
383945916cd2Sjpk 
38407c478bd9Sstevel@tonic-gate int
38417c478bd9Sstevel@tonic-gate prtmount(const char *fs, void *x) {
38427c478bd9Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
38437c478bd9Sstevel@tonic-gate 	return (0);
38447c478bd9Sstevel@tonic-gate }
38457c478bd9Sstevel@tonic-gate 
3846108322fbScarlsonj /*
3847108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
3848108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3849108322fbScarlsonj  * is found or if we can't tell.
3850108322fbScarlsonj  */
3851108322fbScarlsonj static boolean_t
3852108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
38537c478bd9Sstevel@tonic-gate {
3854108322fbScarlsonj 	zoneid_t *zids = NULL;
3855108322fbScarlsonj 	uint_t nzids = 0;
3856108322fbScarlsonj 	boolean_t retv;
3857108322fbScarlsonj 	int rlen, zlen;
3858108322fbScarlsonj 	char zroot[MAXPATHLEN];
3859108322fbScarlsonj 	char zonename[ZONENAME_MAX];
3860108322fbScarlsonj 
3861108322fbScarlsonj 	for (;;) {
3862108322fbScarlsonj 		nzids += 10;
3863108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
3864108322fbScarlsonj 		if (zids == NULL) {
38653f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3866108322fbScarlsonj 			return (B_TRUE);
3867108322fbScarlsonj 		}
3868108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
3869108322fbScarlsonj 			break;
3870108322fbScarlsonj 		free(zids);
3871108322fbScarlsonj 	}
3872108322fbScarlsonj 	retv = B_FALSE;
3873108322fbScarlsonj 	rlen = strlen(rootpath);
3874108322fbScarlsonj 	while (nzids > 0) {
3875108322fbScarlsonj 		/*
3876108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3877108322fbScarlsonj 		 * while we were busy.
3878108322fbScarlsonj 		 */
3879108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3880108322fbScarlsonj 		    sizeof (zroot)) == -1)
3881108322fbScarlsonj 			continue;
3882108322fbScarlsonj 		zlen = strlen(zroot);
3883108322fbScarlsonj 		if (zlen > rlen)
3884108322fbScarlsonj 			zlen = rlen;
3885108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3886108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3887108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3888108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3889108322fbScarlsonj 			    sizeof (zonename)) == -1)
3890108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3891108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
3892108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3893108322fbScarlsonj 			    "zone root %s already in use by zone %s",
3894108322fbScarlsonj 			    rootpath, zonename);
3895108322fbScarlsonj 			retv = B_TRUE;
3896108322fbScarlsonj 			break;
3897108322fbScarlsonj 		}
3898108322fbScarlsonj 	}
3899108322fbScarlsonj 	free(zids);
3900108322fbScarlsonj 	return (retv);
3901108322fbScarlsonj }
3902108322fbScarlsonj 
3903108322fbScarlsonj /*
3904108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
3905108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3906108322fbScarlsonj  */
3907108322fbScarlsonj static boolean_t
3908108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3909108322fbScarlsonj {
3910108322fbScarlsonj 	struct stat64 rst, zst;
3911108322fbScarlsonj 	struct mnttab *mnp;
3912108322fbScarlsonj 
3913108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
3914108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3915108322fbScarlsonj 		return (B_TRUE);
3916108322fbScarlsonj 	}
3917108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3918108322fbScarlsonj 		return (B_TRUE);
3919108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3920108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
3921108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3922108322fbScarlsonj 			continue;
3923108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3924108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
3925108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3926108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3927108322fbScarlsonj 			zerror(zlogp, B_FALSE,
3928108322fbScarlsonj 			    "zone root %s is reachable through %s",
3929108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
3930108322fbScarlsonj 			return (B_TRUE);
3931108322fbScarlsonj 		}
3932108322fbScarlsonj 	}
3933108322fbScarlsonj 	return (B_FALSE);
3934108322fbScarlsonj }
3935108322fbScarlsonj 
39360209230bSgjelinek /*
39370209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
39380209230bSgjelinek  * configuration.
39390209230bSgjelinek  */
39400209230bSgjelinek static int
39410209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
39420209230bSgjelinek {
39430209230bSgjelinek 	int res;
39440209230bSgjelinek 	uint64_t tmp;
39450209230bSgjelinek 	struct zone_mcaptab mcap;
39460209230bSgjelinek 	char sched[MAXNAMELEN];
39470209230bSgjelinek 	zone_dochandle_t handle = NULL;
39480209230bSgjelinek 	char pool_err[128];
39490209230bSgjelinek 
39500209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
39510209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
39520209230bSgjelinek 		return (Z_BAD_HANDLE);
39530209230bSgjelinek 	}
39540209230bSgjelinek 
39550209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
39560209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
39570209230bSgjelinek 		zonecfg_fini_handle(handle);
39580209230bSgjelinek 		return (res);
39590209230bSgjelinek 	}
39600209230bSgjelinek 
39610209230bSgjelinek 	/*
39620209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
39630209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
39640209230bSgjelinek 	 */
39650209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
39660209230bSgjelinek 		uint64_t num;
39670209230bSgjelinek 		char smf_err[128];
39680209230bSgjelinek 
39690209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
39700209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
39710209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
39720209230bSgjelinek 			zonecfg_fini_handle(handle);
39730209230bSgjelinek 			return (Z_INVAL);
39740209230bSgjelinek 		}
39750209230bSgjelinek 
39760209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
39770209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
39780209230bSgjelinek 			    "failed: %s", smf_err);
39790209230bSgjelinek 			zonecfg_fini_handle(handle);
39800209230bSgjelinek 			return (Z_INVAL);
39810209230bSgjelinek 		}
39820209230bSgjelinek 	}
39830209230bSgjelinek 
39840209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
39850209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
39860209230bSgjelinek 	    strlen(sched) > 0) {
39870209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
39880209230bSgjelinek 		    strlen(sched)) == -1)
39890209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
39900209230bSgjelinek 			    "default scheduling class");
39910209230bSgjelinek 
39920209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
39930209230bSgjelinek 	    == Z_OK) {
39940209230bSgjelinek 		/*
39950209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
39960209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
39970209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
39980209230bSgjelinek 		 * in by default so we can print a warning and modify the class
39990209230bSgjelinek 		 * if we wouldn't be using FSS.
40000209230bSgjelinek 		 */
40010209230bSgjelinek 		char class_name[PC_CLNMSZ];
40020209230bSgjelinek 
40030209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
40040209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
40050209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
40060209230bSgjelinek 			    "the zone's scheduling class");
40070209230bSgjelinek 
40080209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
40090209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
40100209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
40110209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
40120209230bSgjelinek 			    "used for processes\nin the zone but to get the "
40130209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
40140209230bSgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
40150209230bSgjelinek 			    "details.");
40160209230bSgjelinek 
40170209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
40180209230bSgjelinek 			    strlen("FSS")) == -1)
40190209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
40200209230bSgjelinek 				    "zone scheduling class to FSS");
40210209230bSgjelinek 		}
40220209230bSgjelinek 	}
40230209230bSgjelinek 
40240209230bSgjelinek 	/*
40250209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
40260209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
40270209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
40280209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
40290209230bSgjelinek 	 * and just return if there is nothing to do.
40300209230bSgjelinek 	 *
40310209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
40320209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
40330209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
40340209230bSgjelinek 	 * zonecfg.
40350209230bSgjelinek 	 */
40360209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
40370209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
40380209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
40390209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
40400209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
40410209230bSgjelinek 			    pool_err);
40420209230bSgjelinek 		else
40430209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
40440209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
40450209230bSgjelinek 		zonecfg_fini_handle(handle);
40460209230bSgjelinek 		return (Z_POOL_BIND);
40470209230bSgjelinek 	}
40480209230bSgjelinek 
40490209230bSgjelinek 	/*
40500209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
40510209230bSgjelinek 	 */
40520209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
40530209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
40540209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
40550209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
40560209230bSgjelinek 		    "processor allocation within the specified range\n"
40570209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
40580209230bSgjelinek 		    "See poold(1M).");
40590209230bSgjelinek 	}
40600209230bSgjelinek 
40610209230bSgjelinek 	/* The following is a warning, not an error. */
40620209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
40630209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
40640209230bSgjelinek 		if (res == Z_POOL_BIND)
40650209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
40660209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
40670209230bSgjelinek 		else if (res == Z_POOL)
40680209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
40690209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
40700209230bSgjelinek 		else
40710209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
40720209230bSgjelinek 			    zonecfg_strerror(res));
40730209230bSgjelinek 	}
40740209230bSgjelinek 
40750209230bSgjelinek 	zonecfg_fini_handle(handle);
40760209230bSgjelinek 	return (Z_OK);
40770209230bSgjelinek }
40780209230bSgjelinek 
4079108322fbScarlsonj zoneid_t
40806cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4081108322fbScarlsonj {
4082108322fbScarlsonj 	zoneid_t rval = -1;
40837c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
40847c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
40859acbbeafSnn35248 	char modname[MAXPATHLEN];
40869acbbeafSnn35248 	struct brand_attr attr;
4087123807fbSedp 	brand_handle_t bh;
40887c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
4089108322fbScarlsonj 	size_t rctlbufsz = 0;
4090fa9e4066Sahrens 	char *zfsbuf = NULL;
4091fa9e4066Sahrens 	size_t zfsbufsz = 0;
4092108322fbScarlsonj 	zoneid_t zoneid = -1;
40937c478bd9Sstevel@tonic-gate 	int xerr;
4094108322fbScarlsonj 	char *kzone;
4095108322fbScarlsonj 	FILE *fp = NULL;
409645916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
409745916cd2Sjpk 	int match = 0;
409845916cd2Sjpk 	int doi = 0;
4099f4b3ec61Sdh155122 	int flags;
4100f4b3ec61Sdh155122 	zone_iptype_t iptype;
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
41037c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
41047c478bd9Sstevel@tonic-gate 		return (-1);
41057c478bd9Sstevel@tonic-gate 	}
4106108322fbScarlsonj 	if (zonecfg_in_alt_root())
4107108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
41087c478bd9Sstevel@tonic-gate 
4109f4b3ec61Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
4110f4b3ec61Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4111f4b3ec61Sdh155122 		return (-1);
4112f4b3ec61Sdh155122 	}
4113f4b3ec61Sdh155122 	switch (iptype) {
4114f4b3ec61Sdh155122 	case ZS_SHARED:
4115f4b3ec61Sdh155122 		flags = 0;
4116f4b3ec61Sdh155122 		break;
4117f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
4118f4b3ec61Sdh155122 		flags = ZCF_NET_EXCL;
4119f4b3ec61Sdh155122 		break;
4120f4b3ec61Sdh155122 	}
4121f4b3ec61Sdh155122 
41227c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
41237c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
41247c478bd9Sstevel@tonic-gate 		return (-1);
41257c478bd9Sstevel@tonic-gate 	}
41267c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4127ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
41287c478bd9Sstevel@tonic-gate 		goto error;
4129ffbafc53Scomay 
41306cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
41316cfd72c6Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
41327c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
41337c478bd9Sstevel@tonic-gate 		goto error;
41347c478bd9Sstevel@tonic-gate 	}
4135ffbafc53Scomay 
4136fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4137fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4138fa9e4066Sahrens 		goto error;
4139fa9e4066Sahrens 	}
41407c478bd9Sstevel@tonic-gate 
41416cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
414245916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
414348451833Scarlsonj 		if (zcent != NULL) {
414445916cd2Sjpk 			match = zcent->zc_match;
414545916cd2Sjpk 			doi = zcent->zc_doi;
414645916cd2Sjpk 			*zlabel = zcent->zc_label;
414745916cd2Sjpk 		} else {
414845916cd2Sjpk 			goto error;
414945916cd2Sjpk 		}
415045916cd2Sjpk 	}
415145916cd2Sjpk 
4152108322fbScarlsonj 	kzone = zone_name;
4153108322fbScarlsonj 
4154108322fbScarlsonj 	/*
4155108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4156108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4157108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4158108322fbScarlsonj 	 * that use this same source node (same device and inode).
4159108322fbScarlsonj 	 */
4160108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4161108322fbScarlsonj 		goto error;
4162108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4163108322fbScarlsonj 		goto error;
4164108322fbScarlsonj 
41656cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
416684561e8cStd153743 		assert(zone_isnative || zone_iscluster);
4167108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4168108322fbScarlsonj 
4169108322fbScarlsonj 		/*
4170108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4171108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4172108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4173108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4174108322fbScarlsonj 		 * executables.
4175108322fbScarlsonj 		 *
4176108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4177108322fbScarlsonj 		 */
4178108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4179108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4180108322fbScarlsonj 			goto error;
4181108322fbScarlsonj 		}
4182108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4183108322fbScarlsonj 			goto error;
4184108322fbScarlsonj 	}
4185108322fbScarlsonj 
4186108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4187108322fbScarlsonj 		/*
4188108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4189108322fbScarlsonj 		 * then we have some additional work to do before starting the
4190108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
4191108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4192108322fbScarlsonj 		 * the zone.
4193108322fbScarlsonj 		 */
4194108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4195108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4196108322fbScarlsonj 			goto error;
4197108322fbScarlsonj 		}
4198108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4199108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4200108322fbScarlsonj 			goto error;
4201108322fbScarlsonj 		}
4202108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4203108322fbScarlsonj 		    NULL, 0) == 0) {
4204108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4205108322fbScarlsonj 			goto error;
4206108322fbScarlsonj 		}
4207108322fbScarlsonj 		/* This is the preferred name */
4208108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4209108322fbScarlsonj 		    zone_name);
4210108322fbScarlsonj 		srandom(getpid());
4211108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4212108322fbScarlsonj 		    0) == 0) {
4213108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
4214108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4215108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4216108322fbScarlsonj 		}
4217108322fbScarlsonj 		kzone = kernzone;
4218108322fbScarlsonj 	}
4219108322fbScarlsonj 
42207c478bd9Sstevel@tonic-gate 	xerr = 0;
4221108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4222f4b3ec61Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4223f4b3ec61Sdh155122 	    flags)) == -1) {
42247c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
42257c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
42267c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
42277c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
42287c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
42297c478bd9Sstevel@tonic-gate 			} else {
42307c478bd9Sstevel@tonic-gate 
42317c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
42327c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
42337c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
42347c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
42357c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
42367c478bd9Sstevel@tonic-gate 			}
42377c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
42387c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
42397c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
42407c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
42418c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
42428c55461bSton 			char zonename[ZONENAME_MAX];
42438c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
42448c55461bSton 			    zonename, ZONENAME_MAX);
42458c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
42468c55461bSton 			    "used by the zone '%s'.", zonename);
42477c478bd9Sstevel@tonic-gate 		} else {
42487c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
42497c478bd9Sstevel@tonic-gate 		}
42507c478bd9Sstevel@tonic-gate 		goto error;
42517c478bd9Sstevel@tonic-gate 	}
4252108322fbScarlsonj 
4253108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
4254108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4255108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
4256108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4257108322fbScarlsonj 		goto error;
4258108322fbScarlsonj 	}
4259108322fbScarlsonj 
42609acbbeafSnn35248 	if ((zone_get_brand(zone_name, attr.ba_brandname,
42619acbbeafSnn35248 	    MAXNAMELEN) != Z_OK) ||
4262123807fbSedp 	    (bh = brand_open(attr.ba_brandname)) == NULL) {
42639acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine brand name");
42649acbbeafSnn35248 		return (-1);
42659acbbeafSnn35248 	}
42669acbbeafSnn35248 
42679acbbeafSnn35248 	/*
42689acbbeafSnn35248 	 * If this brand requires any kernel support, now is the time to
42699acbbeafSnn35248 	 * get it loaded and initialized.
42709acbbeafSnn35248 	 */
4271123807fbSedp 	if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4272e767a340Sgjelinek 		brand_close(bh);
42739acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine brand kernel "
42749acbbeafSnn35248 		    "module");
42759acbbeafSnn35248 		return (-1);
42769acbbeafSnn35248 	}
4277e767a340Sgjelinek 	brand_close(bh);
42789acbbeafSnn35248 
42799acbbeafSnn35248 	if (strlen(modname) > 0) {
42809acbbeafSnn35248 		(void) strlcpy(attr.ba_modname, modname, MAXPATHLEN);
42819acbbeafSnn35248 		if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
42829acbbeafSnn35248 		    sizeof (attr) != 0)) {
42839acbbeafSnn35248 			zerror(zlogp, B_TRUE, "could not set zone brand "
42849acbbeafSnn35248 			    "attribute.");
42859acbbeafSnn35248 			goto error;
42869acbbeafSnn35248 		}
42879acbbeafSnn35248 	}
42889acbbeafSnn35248 
42897c478bd9Sstevel@tonic-gate 	/*
42900209230bSgjelinek 	 * The following actions are not performed when merely mounting a zone
42910209230bSgjelinek 	 * for administrative use.
42927c478bd9Sstevel@tonic-gate 	 */
42936cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
42940209230bSgjelinek 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) {
42950209230bSgjelinek 			(void) zone_shutdown(zoneid);
42960209230bSgjelinek 			goto error;
42970209230bSgjelinek 		}
42980209230bSgjelinek 
429945916cd2Sjpk 		set_mlps(zlogp, zoneid, zcent);
43000209230bSgjelinek 	}
43010209230bSgjelinek 
4302108322fbScarlsonj 	rval = zoneid;
4303108322fbScarlsonj 	zoneid = -1;
4304108322fbScarlsonj 
43057c478bd9Sstevel@tonic-gate error:
4306108322fbScarlsonj 	if (zoneid != -1)
4307108322fbScarlsonj 		(void) zone_destroy(zoneid);
43087c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
43097c478bd9Sstevel@tonic-gate 		free(rctlbuf);
43107c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
4311108322fbScarlsonj 	if (fp != NULL)
4312108322fbScarlsonj 		zonecfg_close_scratch(fp);
4313108322fbScarlsonj 	lofs_discard_mnttab();
431445916cd2Sjpk 	if (zcent != NULL)
431545916cd2Sjpk 		tsol_freezcent(zcent);
43167c478bd9Sstevel@tonic-gate 	return (rval);
43177c478bd9Sstevel@tonic-gate }
43187c478bd9Sstevel@tonic-gate 
4319555afedfScarlsonj /*
4320555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
4321555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4322555afedfScarlsonj  * details from inside the zone.
4323555afedfScarlsonj  */
4324555afedfScarlsonj static void
4325555afedfScarlsonj write_index_file(zoneid_t zoneid)
4326555afedfScarlsonj {
4327555afedfScarlsonj 	FILE *zef;
4328555afedfScarlsonj 	FILE *zet;
4329555afedfScarlsonj 	struct zoneent *zep;
4330555afedfScarlsonj 	pid_t child;
4331555afedfScarlsonj 	int tmpl_fd;
4332555afedfScarlsonj 	ctid_t ct;
4333555afedfScarlsonj 	int fd;
4334555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4335555afedfScarlsonj 
4336555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
4337555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
4338555afedfScarlsonj 		return;
4339555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
4340555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
4341555afedfScarlsonj 			break;
4342555afedfScarlsonj 		free(zep);
4343555afedfScarlsonj 	}
4344555afedfScarlsonj 	endzoneent(zef);
4345555afedfScarlsonj 	if (zep == NULL)
4346555afedfScarlsonj 		return;
4347555afedfScarlsonj 
4348555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
4349555afedfScarlsonj 		free(zep);
4350555afedfScarlsonj 		return;
4351555afedfScarlsonj 	}
4352555afedfScarlsonj 
4353555afedfScarlsonj 	if ((child = fork()) == -1) {
4354555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4355555afedfScarlsonj 		(void) close(tmpl_fd);
4356555afedfScarlsonj 		free(zep);
4357555afedfScarlsonj 		return;
4358555afedfScarlsonj 	}
4359555afedfScarlsonj 
4360555afedfScarlsonj 	/* parent waits for child to finish */
4361555afedfScarlsonj 	if (child != 0) {
4362555afedfScarlsonj 		free(zep);
4363555afedfScarlsonj 		if (contract_latest(&ct) == -1)
4364555afedfScarlsonj 			ct = -1;
4365555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
4366555afedfScarlsonj 		(void) close(tmpl_fd);
4367555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
4368555afedfScarlsonj 		(void) contract_abandon_id(ct);
4369555afedfScarlsonj 		return;
4370555afedfScarlsonj 	}
4371555afedfScarlsonj 
4372555afedfScarlsonj 	/* child enters zone and sets up index file */
4373555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
4374555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
4375555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4376555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4377555afedfScarlsonj 		    ZONE_CONFIG_GID);
4378555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4379555afedfScarlsonj 		    ZONE_INDEX_MODE);
4380555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4381555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4382555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
4383555afedfScarlsonj 				uuidstr[0] = '\0';
4384555afedfScarlsonj 			else
4385555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
4386555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4387555afedfScarlsonj 			    zone_state_str(zep->zone_state),
4388555afedfScarlsonj 			    uuidstr);
4389555afedfScarlsonj 			(void) fclose(zet);
4390555afedfScarlsonj 		}
4391555afedfScarlsonj 	}
4392555afedfScarlsonj 	_exit(0);
4393555afedfScarlsonj }
4394555afedfScarlsonj 
43957c478bd9Sstevel@tonic-gate int
43966cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
43977c478bd9Sstevel@tonic-gate {
43989acbbeafSnn35248 	char zonepath[MAXPATHLEN];
43995749802bSdp 
44006cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4401fa9e4066Sahrens 		lofs_discard_mnttab();
4402fa9e4066Sahrens 		return (-1);
4403fa9e4066Sahrens 	}
4404fa9e4066Sahrens 
44059acbbeafSnn35248 	/*
44069acbbeafSnn35248 	 * Before we try to mount filesystems we need to create the
44079acbbeafSnn35248 	 * attribute backing store for /dev
44089acbbeafSnn35248 	 */
44099acbbeafSnn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
44109acbbeafSnn35248 		lofs_discard_mnttab();
44119acbbeafSnn35248 		return (-1);
44129acbbeafSnn35248 	}
441316bca938Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
441427e6fb21Sdp 
441527e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
441627e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
441727e6fb21Sdp 	    0, 3) != 0) {
4418108322fbScarlsonj 		lofs_discard_mnttab();
44197c478bd9Sstevel@tonic-gate 		return (-1);
4420108322fbScarlsonj 	}
4421facf4a8dSllai1 
44229acbbeafSnn35248 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4423facf4a8dSllai1 		lofs_discard_mnttab();
4424facf4a8dSllai1 		return (-1);
4425facf4a8dSllai1 	}
4426facf4a8dSllai1 
44276cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
4428f4b3ec61Sdh155122 		zone_iptype_t iptype;
4429f4b3ec61Sdh155122 
4430f4b3ec61Sdh155122 		if (get_iptype(zlogp, &iptype) < 0) {
4431f4b3ec61Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
4432108322fbScarlsonj 			lofs_discard_mnttab();
44337c478bd9Sstevel@tonic-gate 			return (-1);
4434108322fbScarlsonj 		}
4435555afedfScarlsonj 
4436f4b3ec61Sdh155122 		switch (iptype) {
4437f4b3ec61Sdh155122 		case ZS_SHARED:
4438f4b3ec61Sdh155122 			/* Always do this to make lo0 get configured */
4439f4b3ec61Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
4440f4b3ec61Sdh155122 				lofs_discard_mnttab();
4441f4b3ec61Sdh155122 				return (-1);
4442f4b3ec61Sdh155122 			}
4443f4b3ec61Sdh155122 			break;
4444f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4445f4b3ec61Sdh155122 			if (configure_exclusive_network_interfaces(zlogp) !=
4446f4b3ec61Sdh155122 			    0) {
4447f4b3ec61Sdh155122 				lofs_discard_mnttab();
4448f4b3ec61Sdh155122 				return (-1);
4449f4b3ec61Sdh155122 			}
4450f4b3ec61Sdh155122 			break;
4451f4b3ec61Sdh155122 		}
4452f4b3ec61Sdh155122 	}
4453f4b3ec61Sdh155122 
4454555afedfScarlsonj 	write_index_file(zoneid);
4455555afedfScarlsonj 
4456108322fbScarlsonj 	lofs_discard_mnttab();
44577c478bd9Sstevel@tonic-gate 	return (0);
44587c478bd9Sstevel@tonic-gate }
44597c478bd9Sstevel@tonic-gate 
4460108322fbScarlsonj static int
4461108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
44627c478bd9Sstevel@tonic-gate {
4463108322fbScarlsonj 	char zroot[MAXPATHLEN];
4464108322fbScarlsonj 
446584561e8cStd153743 	assert(zone_isnative || zone_iscluster);
44669acbbeafSnn35248 
4467108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4468108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4469108322fbScarlsonj 		return (-1);
4470108322fbScarlsonj 	}
4471108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4472108322fbScarlsonj 
4473108322fbScarlsonj 	/*
4474108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
4475108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
4476108322fbScarlsonj 	 * still be creds floating about in the system that reference the
4477108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4478108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4479108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
4480108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4481108322fbScarlsonj 	 */
4482108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
4483108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4484108322fbScarlsonj 			goto unmounted;
4485108322fbScarlsonj 		if (errno == EBUSY) {
4486108322fbScarlsonj 			int tries = 10;
4487108322fbScarlsonj 
4488108322fbScarlsonj 			while (--tries >= 0) {
4489108322fbScarlsonj 				(void) sleep(1);
4490108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
4491108322fbScarlsonj 					goto unmounted;
4492108322fbScarlsonj 				if (errno != EBUSY)
4493108322fbScarlsonj 					break;
4494108322fbScarlsonj 			}
4495108322fbScarlsonj 		}
4496108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4497108322fbScarlsonj 		return (-1);
4498108322fbScarlsonj 	}
4499108322fbScarlsonj unmounted:
4500108322fbScarlsonj 
4501108322fbScarlsonj 	/*
4502108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
4503108322fbScarlsonj 	 * entries.
4504108322fbScarlsonj 	 */
4505108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4506108322fbScarlsonj 		FILE *fp;
4507108322fbScarlsonj 		int retv;
4508108322fbScarlsonj 
4509108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4510108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4511108322fbScarlsonj 			return (-1);
4512108322fbScarlsonj 		}
4513108322fbScarlsonj 		retv = -1;
4514108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
4515108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4516108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4517108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4518108322fbScarlsonj 		else
4519108322fbScarlsonj 			retv = 0;
4520108322fbScarlsonj 		zonecfg_close_scratch(fp);
4521108322fbScarlsonj 		return (retv);
4522108322fbScarlsonj 	} else {
4523108322fbScarlsonj 		return (0);
4524108322fbScarlsonj 	}
4525108322fbScarlsonj }
4526108322fbScarlsonj 
4527108322fbScarlsonj int
45280209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
4529108322fbScarlsonj {
4530108322fbScarlsonj 	char *kzone;
45317c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
45320209230bSgjelinek 	int res;
45330209230bSgjelinek 	char pool_err[128];
4534*ff17c8bfSgjelinek 	char zpath[MAXPATHLEN];
45359acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
45369acbbeafSnn35248 	char brand[MAXNAMELEN];
4537123807fbSedp 	brand_handle_t bh = NULL;
4538f4b3ec61Sdh155122 	ushort_t flags;
45397c478bd9Sstevel@tonic-gate 
4540108322fbScarlsonj 	kzone = zone_name;
4541108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4542108322fbScarlsonj 		FILE *fp;
4543108322fbScarlsonj 
4544108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4545108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
4546108322fbScarlsonj 			goto error;
4547108322fbScarlsonj 		}
4548108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4549108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
4550108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4551108322fbScarlsonj 			zonecfg_close_scratch(fp);
4552108322fbScarlsonj 			goto error;
4553108322fbScarlsonj 		}
4554108322fbScarlsonj 		zonecfg_close_scratch(fp);
4555108322fbScarlsonj 		kzone = kernzone;
4556108322fbScarlsonj 	}
4557108322fbScarlsonj 
4558108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
45597c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
45607c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4561108322fbScarlsonj 		if (unmount_cmd)
4562108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
45637c478bd9Sstevel@tonic-gate 		goto error;
45647c478bd9Sstevel@tonic-gate 	}
45657c478bd9Sstevel@tonic-gate 
45667c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
45677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
45687c478bd9Sstevel@tonic-gate 		goto error;
45697c478bd9Sstevel@tonic-gate 	}
45707c478bd9Sstevel@tonic-gate 
4571*ff17c8bfSgjelinek 	/* Get the zonepath of this zone */
4572*ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
4573*ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
45749acbbeafSnn35248 		goto error;
45759acbbeafSnn35248 	}
45769acbbeafSnn35248 
45779acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
45789acbbeafSnn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
4579123807fbSedp 	    (bh = brand_open(brand)) == NULL) {
45809acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
45819acbbeafSnn35248 		return (-1);
45829acbbeafSnn35248 	}
45839acbbeafSnn35248 	/*
45849acbbeafSnn35248 	 * If there is a brand 'halt' callback, execute it now to give the
45859acbbeafSnn35248 	 * brand a chance to cleanup any custom configuration.
45869acbbeafSnn35248 	 */
45879acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4588*ff17c8bfSgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
4589*ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
4590123807fbSedp 		brand_close(bh);
45919acbbeafSnn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
45929acbbeafSnn35248 		    "halt callback.");
45939acbbeafSnn35248 		goto error;
45949acbbeafSnn35248 	}
4595123807fbSedp 	brand_close(bh);
45969acbbeafSnn35248 
45979acbbeafSnn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
45989acbbeafSnn35248 	    (do_subproc(zlogp, cmdbuf) != Z_OK)) {
45999acbbeafSnn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
46009acbbeafSnn35248 		goto error;
46019acbbeafSnn35248 	}
46029acbbeafSnn35248 
4603f4b3ec61Sdh155122 	if (!unmount_cmd) {
4604f4b3ec61Sdh155122 		zone_iptype_t iptype;
4605f4b3ec61Sdh155122 
4606f4b3ec61Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
4607f4b3ec61Sdh155122 		    sizeof (flags)) < 0) {
4608f4b3ec61Sdh155122 			if (get_iptype(zlogp, &iptype) < 0) {
4609f4b3ec61Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
4610f4b3ec61Sdh155122 				    "ip-type");
46117c478bd9Sstevel@tonic-gate 				goto error;
46127c478bd9Sstevel@tonic-gate 			}
4613f4b3ec61Sdh155122 		} else {
4614f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
4615f4b3ec61Sdh155122 				iptype = ZS_EXCLUSIVE;
4616f4b3ec61Sdh155122 			else
4617f4b3ec61Sdh155122 				iptype = ZS_SHARED;
4618f4b3ec61Sdh155122 		}
4619f4b3ec61Sdh155122 
4620f4b3ec61Sdh155122 		switch (iptype) {
4621f4b3ec61Sdh155122 		case ZS_SHARED:
4622f4b3ec61Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
4623f4b3ec61Sdh155122 			    zoneid) != 0) {
4624f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4625f4b3ec61Sdh155122 				    "network interfaces in zone");
4626f4b3ec61Sdh155122 				goto error;
4627f4b3ec61Sdh155122 			}
4628f4b3ec61Sdh155122 			break;
4629f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4630f4b3ec61Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
4631f4b3ec61Sdh155122 			    zoneid) != 0) {
4632f4b3ec61Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
4633f4b3ec61Sdh155122 				    "network interfaces in zone");
4634f4b3ec61Sdh155122 				goto error;
4635f4b3ec61Sdh155122 			}
4636f4b3ec61Sdh155122 			break;
4637f4b3ec61Sdh155122 		}
4638f4b3ec61Sdh155122 	}
46397c478bd9Sstevel@tonic-gate 
4640108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
46417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
46427c478bd9Sstevel@tonic-gate 		goto error;
46437c478bd9Sstevel@tonic-gate 	}
46447c478bd9Sstevel@tonic-gate 
4645facf4a8dSllai1 	/* destroy zconsole before umount /dev */
4646facf4a8dSllai1 	if (!unmount_cmd)
4647facf4a8dSllai1 		destroy_console_slave();
4648facf4a8dSllai1 
4649108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
46507c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
46517c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
46527c478bd9Sstevel@tonic-gate 		goto error;
46537c478bd9Sstevel@tonic-gate 	}
46547c478bd9Sstevel@tonic-gate 
46550209230bSgjelinek 	/*
4656cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
4657cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
4658cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
4659cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
4660cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
4661cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
4662cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
4663cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
46640209230bSgjelinek 	 */
4665cf6b13d2Sgjelinek 	if (!unmount_cmd) {
4666cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
4667cf6b13d2Sgjelinek 
4668cf6b13d2Sgjelinek 		if (rebooting) {
4669cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
4670cf6b13d2Sgjelinek 			zone_dochandle_t handle;
4671cf6b13d2Sgjelinek 
4672cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
4673cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
4674cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
4675cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
4676e767a340Sgjelinek 
4677e767a340Sgjelinek 			zonecfg_fini_handle(handle);
4678cf6b13d2Sgjelinek 		}
4679cf6b13d2Sgjelinek 
4680cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
46810209230bSgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
46820209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
46830209230bSgjelinek 				if (res == Z_POOL)
46840209230bSgjelinek 					zerror(zlogp, B_FALSE, pool_err);
46850209230bSgjelinek 			}
46860209230bSgjelinek 		}
4687cf6b13d2Sgjelinek 	}
46880209230bSgjelinek 
468945916cd2Sjpk 	remove_mlps(zlogp, zoneid);
469045916cd2Sjpk 
46917c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
46927c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
46937c478bd9Sstevel@tonic-gate 		goto error;
46947c478bd9Sstevel@tonic-gate 	}
4695108322fbScarlsonj 
4696108322fbScarlsonj 	/*
4697108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
4698108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
4699108322fbScarlsonj 	 */
4700108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4701108322fbScarlsonj 		goto error;
4702108322fbScarlsonj 
4703108322fbScarlsonj 	lofs_discard_mnttab();
47047c478bd9Sstevel@tonic-gate 	return (0);
47057c478bd9Sstevel@tonic-gate 
47067c478bd9Sstevel@tonic-gate error:
4707108322fbScarlsonj 	lofs_discard_mnttab();
47087c478bd9Sstevel@tonic-gate 	return (-1);
47097c478bd9Sstevel@tonic-gate }
4710