xref: /onnv-gate/usr/src/cmd/zoneadmd/vplat.c (revision 7089:0461a2d76570)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211645Scomay 
220Sstevel@tonic-gate /*
235829Sgjelinek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
310Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
320Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
330Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
340Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
350Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
360Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
37766Scarlsonj  *
38766Scarlsonj  * This module also has primary responsibility for the layout of "scratch
39766Scarlsonj  * zones."  These are mounted, but inactive, zones that are used during
40766Scarlsonj  * operating system upgrade and potentially other administrative action.  The
41766Scarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
42766Scarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
43766Scarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
44766Scarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
45766Scarlsonj  * without relying on any binaries in the zone itself.
46766Scarlsonj  *
47766Scarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
48766Scarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
49766Scarlsonj  * writable (unshared) resources.  Shared resources, though, are always
50766Scarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
51766Scarlsonj  * currently running, then "/b" inside the zone points to the running zone's
52766Scarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
53766Scarlsonj  * process.
54766Scarlsonj  *
55766Scarlsonj  * To construct this environment, this module creates a tmpfs mount on
56766Scarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
57766Scarlsonj  * described above is constructed on the fly.  The zone is then created using
58766Scarlsonj  * $ZONEPATH/lu as the root.
59766Scarlsonj  *
60766Scarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
61766Scarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
62766Scarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
63766Scarlsonj  * is not a part of the usual halt/ready/boot state machine.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/param.h>
670Sstevel@tonic-gate #include <sys/mount.h>
680Sstevel@tonic-gate #include <sys/mntent.h>
690Sstevel@tonic-gate #include <sys/socket.h>
700Sstevel@tonic-gate #include <sys/utsname.h>
710Sstevel@tonic-gate #include <sys/types.h>
720Sstevel@tonic-gate #include <sys/stat.h>
730Sstevel@tonic-gate #include <sys/sockio.h>
740Sstevel@tonic-gate #include <sys/stropts.h>
750Sstevel@tonic-gate #include <sys/conf.h>
760Sstevel@tonic-gate 
773448Sdh155122 #include <libdlpi.h>
783871Syz147064 #include <libdllink.h>
795895Syz147064 #include <libdlvlan.h>
803448Sdh155122 
810Sstevel@tonic-gate #include <inet/tcp.h>
820Sstevel@tonic-gate #include <arpa/inet.h>
830Sstevel@tonic-gate #include <netinet/in.h>
840Sstevel@tonic-gate #include <net/route.h>
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #include <stdio.h>
870Sstevel@tonic-gate #include <errno.h>
880Sstevel@tonic-gate #include <fcntl.h>
890Sstevel@tonic-gate #include <unistd.h>
900Sstevel@tonic-gate #include <rctl.h>
910Sstevel@tonic-gate #include <stdlib.h>
920Sstevel@tonic-gate #include <string.h>
930Sstevel@tonic-gate #include <strings.h>
940Sstevel@tonic-gate #include <wait.h>
950Sstevel@tonic-gate #include <limits.h>
960Sstevel@tonic-gate #include <libgen.h>
97789Sahrens #include <libzfs.h>
982621Sllai1 #include <libdevinfo.h>
990Sstevel@tonic-gate #include <zone.h>
1000Sstevel@tonic-gate #include <assert.h>
1012303Scarlsonj #include <libcontract.h>
1022303Scarlsonj #include <libcontract_priv.h>
1032303Scarlsonj #include <uuid/uuid.h>
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #include <sys/mntio.h>
1060Sstevel@tonic-gate #include <sys/mnttab.h>
1070Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1080Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
109789Sahrens #include <sys/fs/zfs.h>
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #include <pool.h>
1120Sstevel@tonic-gate #include <sys/pool.h>
1133247Sgjelinek #include <sys/priocntl.h>
1140Sstevel@tonic-gate 
1152712Snn35248 #include <libbrand.h>
1162712Snn35248 #include <sys/brand.h>
1170Sstevel@tonic-gate #include <libzonecfg.h>
1182170Sevanl #include <synch.h>
1192611Svp157776 
1200Sstevel@tonic-gate #include "zoneadmd.h"
1211676Sjpk #include <tsol/label.h>
1221676Sjpk #include <libtsnet.h>
1231676Sjpk #include <sys/priv.h>
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1260Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1290Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
1321676Sjpk #define	MAXTNZLEN	2048
1330Sstevel@tonic-gate 
1345829Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1355829Sgjelinek 
1360Sstevel@tonic-gate /* for routing socket */
1370Sstevel@tonic-gate static int rts_seqno = 0;
1380Sstevel@tonic-gate 
139766Scarlsonj /* mangled zone name when mounting in an alternate root environment */
140766Scarlsonj static char kernzone[ZONENAME_MAX];
141766Scarlsonj 
142766Scarlsonj /* array of cached mount entries for resolve_lofs */
143766Scarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
144766Scarlsonj 
1451676Sjpk /* for Trusted Extensions */
1461676Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
1471676Sjpk static int tsol_mounts(zlog_t *, char *, char *);
1481676Sjpk static void tsol_unmounts(zlog_t *, char *);
1495596Sdh155122 
1501676Sjpk static m_label_t *zlabel = NULL;
1511676Sjpk static m_label_t *zid_label = NULL;
1521676Sjpk static priv_set_t *zprivs = NULL;
1531676Sjpk 
1540Sstevel@tonic-gate /* from libsocket, not in any header file */
1550Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
158766Scarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
159766Scarlsonj  * data) only once every N times through the loop.
160766Scarlsonj  */
161766Scarlsonj #define	MNTTAB_HUNK	32
162766Scarlsonj 
163766Scarlsonj /*
1640Sstevel@tonic-gate  * Private autofs system call
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate extern int _autofssys(int, void *);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static int
1690Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	/*
1720Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
177766Scarlsonj static void
178766Scarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
179766Scarlsonj {
180766Scarlsonj 	uint_t i;
181766Scarlsonj 
182766Scarlsonj 	if (mnt_array == NULL)
183766Scarlsonj 		return;
184766Scarlsonj 	for (i = 0; i < nelem; i++) {
185766Scarlsonj 		free(mnt_array[i].mnt_mountp);
186766Scarlsonj 		free(mnt_array[i].mnt_fstype);
187766Scarlsonj 		free(mnt_array[i].mnt_special);
188766Scarlsonj 		free(mnt_array[i].mnt_mntopts);
189766Scarlsonj 		assert(mnt_array[i].mnt_time == NULL);
190766Scarlsonj 	}
191766Scarlsonj 	free(mnt_array);
192766Scarlsonj }
193766Scarlsonj 
194766Scarlsonj /*
195766Scarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
196766Scarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
197766Scarlsonj  * array in "nelemp".
198766Scarlsonj  */
199766Scarlsonj static int
200766Scarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
201766Scarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
202766Scarlsonj {
203766Scarlsonj 	struct mnttab mnt;
204766Scarlsonj 	struct mnttab *mnts;
205766Scarlsonj 	struct mnttab *mnp;
206766Scarlsonj 	uint_t nmnt;
207766Scarlsonj 
208766Scarlsonj 	rewind(mnttab);
209766Scarlsonj 	resetmnttab(mnttab);
210766Scarlsonj 	nmnt = 0;
211766Scarlsonj 	mnts = NULL;
212766Scarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
213766Scarlsonj 		struct mnttab *tmp_array;
214766Scarlsonj 
215766Scarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
216766Scarlsonj 			continue;
217766Scarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
218766Scarlsonj 			tmp_array = realloc(mnts,
219766Scarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
220766Scarlsonj 			if (tmp_array == NULL) {
221766Scarlsonj 				free_mnttable(mnts, nmnt);
222766Scarlsonj 				return (-1);
223766Scarlsonj 			}
224766Scarlsonj 			mnts = tmp_array;
225766Scarlsonj 		}
226766Scarlsonj 		mnp = &mnts[nmnt++];
227766Scarlsonj 
228766Scarlsonj 		/*
229766Scarlsonj 		 * Zero out any fields we're not using.
230766Scarlsonj 		 */
231766Scarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
232766Scarlsonj 
233766Scarlsonj 		if (mnt.mnt_special != NULL)
234766Scarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
235766Scarlsonj 		if (mnt.mnt_mntopts != NULL)
236766Scarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
237766Scarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
238766Scarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
239766Scarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
240766Scarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
241766Scarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
242766Scarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
243766Scarlsonj 			free_mnttable(mnts, nmnt);
244766Scarlsonj 			return (-1);
245766Scarlsonj 		}
246766Scarlsonj 	}
247766Scarlsonj 	*mnt_arrayp = mnts;
248766Scarlsonj 	*nelemp = nmnt;
249766Scarlsonj 	return (0);
250766Scarlsonj }
251766Scarlsonj 
252766Scarlsonj /*
253766Scarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
254766Scarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
255766Scarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
256766Scarlsonj  * reread of the list of mount points
257766Scarlsonj  */
258766Scarlsonj static void
259766Scarlsonj lofs_discard_mnttab(void)
260766Scarlsonj {
261766Scarlsonj 	free_mnttable(resolve_lofs_mnts,
262766Scarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
263766Scarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
264766Scarlsonj }
265766Scarlsonj 
266766Scarlsonj static int
267766Scarlsonj lofs_read_mnttab(zlog_t *zlogp)
268766Scarlsonj {
269766Scarlsonj 	FILE *mnttab;
270766Scarlsonj 	uint_t nmnts;
271766Scarlsonj 
272766Scarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
273766Scarlsonj 		return (-1);
274766Scarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
275766Scarlsonj 	    &nmnts) == -1) {
276766Scarlsonj 		(void) fclose(mnttab);
277766Scarlsonj 		return (-1);
278766Scarlsonj 	}
279766Scarlsonj 	(void) fclose(mnttab);
280766Scarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
281766Scarlsonj 	return (0);
282766Scarlsonj }
283766Scarlsonj 
284766Scarlsonj /*
285766Scarlsonj  * This function loops over potential loopback mounts and symlinks in a given
286766Scarlsonj  * path and resolves them all down to an absolute path.
287766Scarlsonj  */
2885576Sedp void
289766Scarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
290766Scarlsonj {
291766Scarlsonj 	int len, arlen;
292766Scarlsonj 	const char *altroot;
293766Scarlsonj 	char tmppath[MAXPATHLEN];
294766Scarlsonj 	boolean_t outside_altroot;
295766Scarlsonj 
296766Scarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
297766Scarlsonj 		return;
298766Scarlsonj 	tmppath[len] = '\0';
299766Scarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
300766Scarlsonj 
301766Scarlsonj 	/* This happens once per zoneadmd operation. */
302766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
303766Scarlsonj 		return;
304766Scarlsonj 
305766Scarlsonj 	altroot = zonecfg_get_root();
306766Scarlsonj 	arlen = strlen(altroot);
307766Scarlsonj 	outside_altroot = B_FALSE;
308766Scarlsonj 	for (;;) {
309766Scarlsonj 		struct mnttab *mnp;
310766Scarlsonj 
3113079Sdminer 		/* Search in reverse order to find longest match */
3123079Sdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3133079Sdminer 		    mnp--) {
314766Scarlsonj 			if (mnp->mnt_fstype == NULL ||
315766Scarlsonj 			    mnp->mnt_mountp == NULL ||
3163079Sdminer 			    mnp->mnt_special == NULL)
317766Scarlsonj 				continue;
318766Scarlsonj 			len = strlen(mnp->mnt_mountp);
319766Scarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
320766Scarlsonj 			    (path[len] == '/' || path[len] == '\0'))
321766Scarlsonj 				break;
322766Scarlsonj 		}
3233079Sdminer 		if (mnp < resolve_lofs_mnts)
3243079Sdminer 			break;
3253079Sdminer 		/* If it's not a lofs then we're done */
3263079Sdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
327766Scarlsonj 			break;
328766Scarlsonj 		if (outside_altroot) {
329766Scarlsonj 			char *cp;
330766Scarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
331766Scarlsonj 
332766Scarlsonj 			/*
333766Scarlsonj 			 * If we run into a read-only mount outside of the
334766Scarlsonj 			 * alternate root environment, then the user doesn't
335766Scarlsonj 			 * want this path to be made read-write.
336766Scarlsonj 			 */
337766Scarlsonj 			if (mnp->mnt_mntopts != NULL &&
338766Scarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
339766Scarlsonj 			    NULL &&
340766Scarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
341766Scarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
342766Scarlsonj 				break;
343766Scarlsonj 			}
344766Scarlsonj 		} else if (arlen > 0 &&
345766Scarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
346766Scarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
347766Scarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
348766Scarlsonj 			outside_altroot = B_TRUE;
349766Scarlsonj 		}
350766Scarlsonj 		/* use temporary buffer because new path might be longer */
351766Scarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
352766Scarlsonj 		    mnp->mnt_special, path + len);
353766Scarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
354766Scarlsonj 			break;
355766Scarlsonj 		path[len] = '\0';
356766Scarlsonj 	}
357766Scarlsonj }
358766Scarlsonj 
359766Scarlsonj /*
360766Scarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
361766Scarlsonj  * referenced device is already mounted somewhere.
362766Scarlsonj  */
363766Scarlsonj static int
364766Scarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
365766Scarlsonj {
366766Scarlsonj 	struct mnttab *mnp;
367766Scarlsonj 	zone_fsopt_t *optptr, *onext;
368766Scarlsonj 
369766Scarlsonj 	/* This happens once per zoneadmd operation. */
370766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
371766Scarlsonj 		return (-1);
372766Scarlsonj 
373766Scarlsonj 	/*
374766Scarlsonj 	 * If this special node isn't already in use, then it's ours alone;
375766Scarlsonj 	 * no need to worry about conflicting mounts.
376766Scarlsonj 	 */
377766Scarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
378766Scarlsonj 	    mnp++) {
379766Scarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
380766Scarlsonj 			break;
381766Scarlsonj 	}
382766Scarlsonj 	if (mnp >= resolve_lofs_mnt_max)
383766Scarlsonj 		return (0);
384766Scarlsonj 
385766Scarlsonj 	/*
386766Scarlsonj 	 * Convert this duplicate mount into a lofs mount.
387766Scarlsonj 	 */
388766Scarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
389766Scarlsonj 	    sizeof (fsptr->zone_fs_special));
390766Scarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
391766Scarlsonj 	    sizeof (fsptr->zone_fs_type));
392766Scarlsonj 	fsptr->zone_fs_raw[0] = '\0';
393766Scarlsonj 
394766Scarlsonj 	/*
395766Scarlsonj 	 * Discard all but one of the original options and set that to be the
396766Scarlsonj 	 * same set of options used for inherit package directory resources.
397766Scarlsonj 	 */
398766Scarlsonj 	optptr = fsptr->zone_fs_options;
399766Scarlsonj 	if (optptr == NULL) {
400766Scarlsonj 		optptr = malloc(sizeof (*optptr));
401766Scarlsonj 		if (optptr == NULL) {
402766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
403766Scarlsonj 			    fsptr->zone_fs_dir);
404766Scarlsonj 			return (-1);
405766Scarlsonj 		}
406766Scarlsonj 	} else {
407766Scarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
408766Scarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
409766Scarlsonj 			free(onext);
410766Scarlsonj 		}
411766Scarlsonj 	}
412766Scarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
413766Scarlsonj 	optptr->zone_fsopt_next = NULL;
414766Scarlsonj 	fsptr->zone_fs_options = optptr;
415766Scarlsonj 	return (0);
416766Scarlsonj }
417766Scarlsonj 
4185182Sedp int
4193813Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
4203813Sdp     uid_t userid, gid_t groupid)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	char path[MAXPATHLEN];
4230Sstevel@tonic-gate 	struct stat st;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4260Sstevel@tonic-gate 	    sizeof (path)) {
4270Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4280Sstevel@tonic-gate 		    subdir);
4290Sstevel@tonic-gate 		return (-1);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4330Sstevel@tonic-gate 		/*
4340Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4350Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4360Sstevel@tonic-gate 		 * and we don't need to second guess him.
4370Sstevel@tonic-gate 		 */
4380Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
4391676Sjpk 			if (is_system_labeled() &&
4401676Sjpk 			    S_ISREG(st.st_mode)) {
4411676Sjpk 				/*
4421676Sjpk 				 * The need to mount readonly copies of
4431676Sjpk 				 * global zone /etc/ files is unique to
4441676Sjpk 				 * Trusted Extensions.
4451676Sjpk 				 */
4461676Sjpk 				if (strncmp(subdir, "/etc/",
4471676Sjpk 				    strlen("/etc/")) != 0) {
4481676Sjpk 					zerror(zlogp, B_FALSE,
4491676Sjpk 					    "%s is not in /etc", path);
4501676Sjpk 					return (-1);
4511676Sjpk 				}
4521676Sjpk 			} else {
4531676Sjpk 				zerror(zlogp, B_FALSE,
4541676Sjpk 				    "%s is not a directory", path);
4551676Sjpk 				return (-1);
4561676Sjpk 			}
4570Sstevel@tonic-gate 		}
4583813Sdp 		return (0);
4593813Sdp 	}
4603813Sdp 
4613813Sdp 	if (mkdirp(path, mode) != 0) {
4620Sstevel@tonic-gate 		if (errno == EROFS)
4630Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4640Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4650Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4660Sstevel@tonic-gate 		else
4670Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4680Sstevel@tonic-gate 		return (-1);
4690Sstevel@tonic-gate 	}
4703813Sdp 
4713813Sdp 	(void) chown(path, userid, groupid);
4720Sstevel@tonic-gate 	return (0);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate static void
4760Sstevel@tonic-gate free_remote_fstypes(char **types)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate 	uint_t i;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if (types == NULL)
4810Sstevel@tonic-gate 		return;
4820Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
4830Sstevel@tonic-gate 		free(types[i]);
4840Sstevel@tonic-gate 	free(types);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate static char **
4880Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	char **types = NULL;
4910Sstevel@tonic-gate 	FILE *fp;
4920Sstevel@tonic-gate 	char buf[MAXPATHLEN];
4930Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
4940Sstevel@tonic-gate 	uint_t lines = 0;
4950Sstevel@tonic-gate 	uint_t i;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
4980Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
4990Sstevel@tonic-gate 		return (NULL);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 	/*
5020Sstevel@tonic-gate 	 * Count the number of lines
5030Sstevel@tonic-gate 	 */
5040Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5050Sstevel@tonic-gate 		lines++;
5060Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5070Sstevel@tonic-gate 		goto out;
5080Sstevel@tonic-gate 	rewind(fp);
5090Sstevel@tonic-gate 	/*
5100Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5110Sstevel@tonic-gate 	 */
5120Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5130Sstevel@tonic-gate 	if (types == NULL) {
5140Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5150Sstevel@tonic-gate 		goto out;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 	i = 0;
5180Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5190Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5200Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5210Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5220Sstevel@tonic-gate 			free_remote_fstypes(types);
5230Sstevel@tonic-gate 			types = NULL;
5240Sstevel@tonic-gate 			goto out;
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 		types[i] = strdup(fstype);
5270Sstevel@tonic-gate 		if (types[i] == NULL) {
5280Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5290Sstevel@tonic-gate 			free_remote_fstypes(types);
5300Sstevel@tonic-gate 			types = NULL;
5310Sstevel@tonic-gate 			goto out;
5320Sstevel@tonic-gate 		}
5330Sstevel@tonic-gate 		i++;
5340Sstevel@tonic-gate 	}
5350Sstevel@tonic-gate out:
5360Sstevel@tonic-gate 	(void) fclose(fp);
5370Sstevel@tonic-gate 	return (types);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate static boolean_t
5410Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	uint_t i;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5460Sstevel@tonic-gate 		return (B_FALSE);
5470Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5480Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5490Sstevel@tonic-gate 			return (B_TRUE);
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 	return (B_FALSE);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate 
554766Scarlsonj /*
555766Scarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
556766Scarlsonj  * Upgrade scratch zone root (of the form .../lu).
557766Scarlsonj  */
5580Sstevel@tonic-gate static void
559766Scarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5600Sstevel@tonic-gate {
5614350Std153743 	assert(zone_isnative || zone_iscluster);
5622712Snn35248 
563766Scarlsonj 	if (!isresolved && zonecfg_in_alt_root())
564766Scarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
565766Scarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5700Sstevel@tonic-gate  *
5710Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5720Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5730Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5740Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5750Sstevel@tonic-gate  *
5760Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5770Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5780Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5790Sstevel@tonic-gate  *
5800Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5810Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5820Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5830Sstevel@tonic-gate  *
5840Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5850Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5860Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5870Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5880Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5890Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5900Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5910Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5920Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5930Sstevel@tonic-gate  * graceful mounts first.
5940Sstevel@tonic-gate  *
5950Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5960Sstevel@tonic-gate  */
5970Sstevel@tonic-gate static int
598766Scarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate 	int error = 0;
6010Sstevel@tonic-gate 	FILE *mnttab;
6020Sstevel@tonic-gate 	struct mnttab *mnts;
6030Sstevel@tonic-gate 	uint_t nmnt;
6040Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6050Sstevel@tonic-gate 	size_t zrootlen;
6060Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6070Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6080Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6110Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6120Sstevel@tonic-gate 		return (-1);
6130Sstevel@tonic-gate 	}
614766Scarlsonj 	if (unmount_cmd)
615766Scarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6180Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6190Sstevel@tonic-gate 
6201676Sjpk 	/*
6211676Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
6221676Sjpk 	 * of our zone's /export/home
6231676Sjpk 	 */
6241769Scarlsonj 	if (!unmount_cmd)
6251769Scarlsonj 		tsol_unmounts(zlogp, zone_name);
6261676Sjpk 
6270Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6280Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6290Sstevel@tonic-gate 		return (-1);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 	/*
6320Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6330Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6360Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6370Sstevel@tonic-gate 		error++;
6380Sstevel@tonic-gate 		goto out;
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/*
6420Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6430Sstevel@tonic-gate 	 * should forcibly unmount.
6440Sstevel@tonic-gate 	 */
6450Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6460Sstevel@tonic-gate 	for (; /* ever */; ) {
6470Sstevel@tonic-gate 		uint_t newcount = 0;
6480Sstevel@tonic-gate 		boolean_t unmounted;
6490Sstevel@tonic-gate 		struct mnttab *mnp;
6500Sstevel@tonic-gate 		char *path;
6510Sstevel@tonic-gate 		uint_t i;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		mnts = NULL;
6540Sstevel@tonic-gate 		nmnt = 0;
6550Sstevel@tonic-gate 		/*
6560Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6570Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6580Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6590Sstevel@tonic-gate 		 * filesystems.
6600Sstevel@tonic-gate 		 */
6610Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6620Sstevel@tonic-gate 		    &nmnt) != 0) {
6630Sstevel@tonic-gate 			error++;
6640Sstevel@tonic-gate 			goto out;
6650Sstevel@tonic-gate 		}
6660Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6670Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6680Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6690Sstevel@tonic-gate 			unmounted = B_FALSE;
6700Sstevel@tonic-gate 			/*
6710Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6720Sstevel@tonic-gate 			 *
6730Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6740Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6750Sstevel@tonic-gate 			 * and try a regular unmount.
6760Sstevel@tonic-gate 			 */
6770Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6780Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6790Sstevel@tonic-gate 					unmounted = B_TRUE;
6800Sstevel@tonic-gate 			}
6810Sstevel@tonic-gate 			/*
6820Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6830Sstevel@tonic-gate 			 */
6840Sstevel@tonic-gate 			if (stuck) {
6850Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6860Sstevel@tonic-gate 					unmounted = B_TRUE;
6870Sstevel@tonic-gate 					stuck = B_FALSE;
6880Sstevel@tonic-gate 				} else {
6890Sstevel@tonic-gate 					/*
6900Sstevel@tonic-gate 					 * The first failure indicates a
6910Sstevel@tonic-gate 					 * mount we won't be able to get
6920Sstevel@tonic-gate 					 * rid of automatically, so we
6930Sstevel@tonic-gate 					 * bail.
6940Sstevel@tonic-gate 					 */
6950Sstevel@tonic-gate 					error++;
6960Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6970Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
6980Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
6990Sstevel@tonic-gate 					goto out;
7000Sstevel@tonic-gate 				}
7010Sstevel@tonic-gate 			}
7020Sstevel@tonic-gate 			/*
7030Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7040Sstevel@tonic-gate 			 */
7050Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7060Sstevel@tonic-gate 				newcount++;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		if (newcount == 0)
7110Sstevel@tonic-gate 			break;
7120Sstevel@tonic-gate 		if (newcount >= oldcount) {
7130Sstevel@tonic-gate 			/*
7140Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7150Sstevel@tonic-gate 			 * should start trying forced unmounts.
7160Sstevel@tonic-gate 			 */
7170Sstevel@tonic-gate 			stuck = B_TRUE;
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 		oldcount = newcount;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		/*
7220Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7230Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7240Sstevel@tonic-gate 		 */
7250Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7260Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7270Sstevel@tonic-gate 			error++;
7280Sstevel@tonic-gate 			goto out;
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate out:
7330Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7340Sstevel@tonic-gate 	(void) fclose(mnttab);
7350Sstevel@tonic-gate 	return (error ? -1 : 0);
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate static int
7390Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7420Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7490Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7500Sstevel@tonic-gate  * returns the exit status otherwise.
7510Sstevel@tonic-gate  *
7520Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7530Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7540Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7550Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7560Sstevel@tonic-gate  * remains the case.
7570Sstevel@tonic-gate  */
7580Sstevel@tonic-gate static int
7590Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate 	pid_t child_pid;
7620Sstevel@tonic-gate 	int child_status = 0;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7650Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7660Sstevel@tonic-gate 	 */
7670Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7680Sstevel@tonic-gate 	child_pid = fork();
7690Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7700Sstevel@tonic-gate 	if (child_pid == -1) {
7710Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7720Sstevel@tonic-gate 		return (-1);
7730Sstevel@tonic-gate 	} else if (child_pid == 0) {
7740Sstevel@tonic-gate 		closefrom(0);
7751915Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7761915Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7771915Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7781915Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7790Sstevel@tonic-gate 		(void) execv(path, argv);
7800Sstevel@tonic-gate 		/*
7810Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7820Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7830Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7840Sstevel@tonic-gate 		 * accordingly.
7850Sstevel@tonic-gate 		 */
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7880Sstevel@tonic-gate 	} else {
7890Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7930Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7940Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7950Sstevel@tonic-gate 		return (-1);
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
7980Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
7990Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8000Sstevel@tonic-gate 		return (-1);
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate static int
8066734Sjohnlev isregfile(const char *path)
8076734Sjohnlev {
8086734Sjohnlev 	struct stat64 st;
8096734Sjohnlev 
8106734Sjohnlev 	if (stat64(path, &st) == -1)
8116734Sjohnlev 		return (-1);
8126734Sjohnlev 
8136734Sjohnlev 	return (S_ISREG(st.st_mode));
8146734Sjohnlev }
8156734Sjohnlev 
8166734Sjohnlev static int
8170Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8200Sstevel@tonic-gate 	char *argv[4];
8210Sstevel@tonic-gate 	int status;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	/*
8240Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8250Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8260Sstevel@tonic-gate 	 */
8270Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8282712Snn35248 	    >= sizeof (cmdbuf)) {
8290Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8300Sstevel@tonic-gate 		return (-1);
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8336734Sjohnlev 	/*
8346734Sjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
8356734Sjohnlev 	 * in zoneadm.
8366734Sjohnlev 	 */
8376734Sjohnlev 	if (isregfile(cmdbuf) == -1)
8386734Sjohnlev 		return (0);
8396734Sjohnlev 
8400Sstevel@tonic-gate 	argv[0] = "fsck";
8410Sstevel@tonic-gate 	argv[1] = "-m";
8420Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
8430Sstevel@tonic-gate 	argv[3] = NULL;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8460Sstevel@tonic-gate 	if (status == 0 || status == -1)
8470Sstevel@tonic-gate 		return (status);
8480Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8490Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8500Sstevel@tonic-gate 	return (-1);
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate static int
8540Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8550Sstevel@tonic-gate     const char *special, const char *directory)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8580Sstevel@tonic-gate 	char *argv[6];
8590Sstevel@tonic-gate 	int status;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/*
8620Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8630Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8640Sstevel@tonic-gate 	 */
8650Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8662712Snn35248 	    >= sizeof (cmdbuf)) {
8670Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8680Sstevel@tonic-gate 		return (-1);
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 	argv[0] = "mount";
8710Sstevel@tonic-gate 	if (opts[0] == '\0') {
8720Sstevel@tonic-gate 		argv[1] = (char *)special;
8730Sstevel@tonic-gate 		argv[2] = (char *)directory;
8740Sstevel@tonic-gate 		argv[3] = NULL;
8750Sstevel@tonic-gate 	} else {
8760Sstevel@tonic-gate 		argv[1] = "-o";
8770Sstevel@tonic-gate 		argv[2] = (char *)opts;
8780Sstevel@tonic-gate 		argv[3] = (char *)special;
8790Sstevel@tonic-gate 		argv[4] = (char *)directory;
8800Sstevel@tonic-gate 		argv[5] = NULL;
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8840Sstevel@tonic-gate 	if (status == 0 || status == -1)
8850Sstevel@tonic-gate 		return (status);
8860Sstevel@tonic-gate 	if (opts[0] == '\0')
8870Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8880Sstevel@tonic-gate 		    "failed with exit code %d",
8890Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8900Sstevel@tonic-gate 	else
8910Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8920Sstevel@tonic-gate 		    "failed with exit code %d",
8930Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8940Sstevel@tonic-gate 	return (-1);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8985182Sedp  * Check if a given mount point path exists.
8995182Sedp  * If it does, make sure it doesn't contain any symlinks.
9005182Sedp  * Note that if "leaf" is false we're checking an intermediate
9015182Sedp  * component of the mount point path, so it must be a directory.
9025182Sedp  * If "leaf" is true, then we're checking the entire mount point
9035182Sedp  * path, so the mount point itself can be anything aside from a
9045182Sedp  * symbolic link.
9055182Sedp  *
9065182Sedp  * If the path is invalid then a negative value is returned.  If the
9075182Sedp  * path exists and is a valid mount point path then 0 is returned.
9085182Sedp  * If the path doesn't exist return a positive value.
9090Sstevel@tonic-gate  */
9100Sstevel@tonic-gate static int
9115182Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9120Sstevel@tonic-gate {
9130Sstevel@tonic-gate 	struct stat statbuf;
9140Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9150Sstevel@tonic-gate 	int res;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9180Sstevel@tonic-gate 		if (errno == ENOENT)
9195182Sedp 			return (1);
9200Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9210Sstevel@tonic-gate 		return (-1);
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9240Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9250Sstevel@tonic-gate 		return (-1);
9260Sstevel@tonic-gate 	}
9275182Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9285182Sedp 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9295182Sedp 		return (-1);
9300Sstevel@tonic-gate 	}
9310Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9320Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9330Sstevel@tonic-gate 		return (-1);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 	respath[res] = '\0';
9360Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9370Sstevel@tonic-gate 		/*
9385182Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9390Sstevel@tonic-gate 		 */
9400Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9410Sstevel@tonic-gate 		return (-1);
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 	return (0);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate /*
9475182Sedp  * Validate a mount point path.  A valid mount point path is an
9485182Sedp  * absolute path that either doesn't exist, or, if it does exists it
9495182Sedp  * must be an absolute canonical path that doesn't have any symbolic
9505182Sedp  * links in it.  The target of a mount point path can be any filesystem
9515182Sedp  * object.  (Different filesystems can support different mount points,
9525182Sedp  * for example "lofs" and "mntfs" both support files and directories
9535182Sedp  * while "ufs" just supports directories.)
9540Sstevel@tonic-gate  *
9555182Sedp  * If the path is invalid then a negative value is returned.  If the
9565182Sedp  * path exists and is a valid mount point path then 0 is returned.
9575182Sedp  * If the path doesn't exist return a positive value.
9580Sstevel@tonic-gate  */
9595182Sedp int
9605182Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
9615182Sedp     const char *dir, const char *fstype)
9620Sstevel@tonic-gate {
9635182Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
9645182Sedp 	int rv;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/*
9675182Sedp 	 * Sanity check the target mount point path.
9685182Sedp 	 * It must be a non-null string that starts with a '/'.
9690Sstevel@tonic-gate 	 */
9705182Sedp 	if (dir[0] != '/') {
9715182Sedp 		if (spec[0] == '\0') {
9725182Sedp 			/*
9735182Sedp 			 * This must be an invalid ipd entry (see comments
9745182Sedp 			 * in mount_filesystems_ipdent()).
9755182Sedp 			 */
9765182Sedp 			zerror(zlogp, B_FALSE,
9775182Sedp 			    "invalid inherit-pkg-dir entry: \"%s\"", dir);
9785182Sedp 		} else {
9795182Sedp 			/* Something went wrong. */
9805182Sedp 			zerror(zlogp, B_FALSE, "invalid mount directory, "
9815182Sedp 			    "type: \"%s\", special: \"%s\", dir: \"%s\"",
9825182Sedp 			    fstype, spec, dir);
9835182Sedp 		}
9845182Sedp 		return (-1);
9855182Sedp 	}
9865182Sedp 
9875182Sedp 	/*
9885182Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
9895182Sedp 	 * is added to all paths (even non-directory paths) to allow us
9905182Sedp 	 * to detect the end of paths below.  If the path already ends
9915182Sedp 	 * in a '/', then that's ok too (although we'll fail the
9925182Sedp 	 * cannonical path check in valid_mount_point()).
9935182Sedp 	 */
9945182Sedp 	if (snprintf(abspath, sizeof (abspath),
9955182Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
9965182Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
9975182Sedp 		    rootpath, dir);
9985182Sedp 		return (-1);
9995182Sedp 	}
10005182Sedp 
10015182Sedp 	/*
10025182Sedp 	 * Starting with rootpath, verify the mount path one component
10035182Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
10045182Sedp 	 */
10050Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10060Sstevel@tonic-gate 	assert(*slashp == '/');
10070Sstevel@tonic-gate 	do {
10085182Sedp 		slashp_next = strchr(slashp + 1, '/');
10090Sstevel@tonic-gate 		*slashp = '\0';
10105182Sedp 		if (slashp_next != NULL) {
10115182Sedp 			/* This is an intermediary mount path component. */
10125182Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
10135182Sedp 		} else {
10145182Sedp 			/* This is the last component of the mount path. */
10155182Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
10165182Sedp 		}
10175182Sedp 		if (rv < 0)
10185182Sedp 			return (rv);
10190Sstevel@tonic-gate 		*slashp = '/';
10205182Sedp 	} while ((slashp = slashp_next) != NULL);
10215182Sedp 	return (rv);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate static int
10252712Snn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10262712Snn35248 {
10272712Snn35248 	di_prof_t prof = arg;
10282712Snn35248 
10292712Snn35248 	if (name == NULL)
10302712Snn35248 		return (di_prof_add_dev(prof, match));
10312712Snn35248 	return (di_prof_add_map(prof, match, name));
10322712Snn35248 }
10332712Snn35248 
10342712Snn35248 static int
10352712Snn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10362712Snn35248 {
10372712Snn35248 	di_prof_t prof = arg;
10382712Snn35248 
10392712Snn35248 	return (di_prof_add_symlink(prof, source, target));
10402712Snn35248 }
10412712Snn35248 
10423448Sdh155122 static int
10433448Sdh155122 get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
10443448Sdh155122 {
10453448Sdh155122 	zone_dochandle_t handle;
10463448Sdh155122 
10473448Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
10483448Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
10493448Sdh155122 		return (-1);
10503448Sdh155122 	}
10513448Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
10523448Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
10533448Sdh155122 		zonecfg_fini_handle(handle);
10543448Sdh155122 		return (-1);
10553448Sdh155122 	}
10563448Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
10573448Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
10583448Sdh155122 		zonecfg_fini_handle(handle);
10593448Sdh155122 		return (-1);
10603448Sdh155122 	}
10613448Sdh155122 	zonecfg_fini_handle(handle);
10623448Sdh155122 	return (0);
10633448Sdh155122 }
10643448Sdh155122 
10652712Snn35248 /*
10662712Snn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10672712Snn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10682712Snn35248  * use these as a profile/filter to determine what exists in /dev.
10692712Snn35248  */
10702712Snn35248 static int
10712712Snn35248 mount_one_dev(zlog_t *zlogp, char *devpath)
10722712Snn35248 {
10732712Snn35248 	char			brand[MAXNAMELEN];
10742712Snn35248 	zone_dochandle_t	handle = NULL;
10752727Sedp 	brand_handle_t		bh = NULL;
10762712Snn35248 	struct zone_devtab	ztab;
10772712Snn35248 	di_prof_t		prof = NULL;
10782712Snn35248 	int			err;
10792712Snn35248 	int			retval = -1;
10803448Sdh155122 	zone_iptype_t		iptype;
10813448Sdh155122 	const char 		*curr_iptype;
10822712Snn35248 
10832712Snn35248 	if (di_prof_init(devpath, &prof)) {
10842712Snn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10852712Snn35248 		goto cleanup;
10862712Snn35248 	}
10872712Snn35248 
10882712Snn35248 	/* Get a handle to the brand info for this zone */
10892712Snn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
10902727Sedp 	    (bh = brand_open(brand)) == NULL) {
10912712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
10922712Snn35248 		goto cleanup;
10932712Snn35248 	}
10942712Snn35248 
10953448Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
10963448Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
10973448Sdh155122 		goto cleanup;
10983448Sdh155122 	}
10993448Sdh155122 	switch (iptype) {
11003448Sdh155122 	case ZS_SHARED:
11013448Sdh155122 		curr_iptype = "shared";
11023448Sdh155122 		break;
11033448Sdh155122 	case ZS_EXCLUSIVE:
11043448Sdh155122 		curr_iptype = "exclusive";
11053448Sdh155122 		break;
11063448Sdh155122 	}
11073448Sdh155122 
11082727Sedp 	if (brand_platform_iter_devices(bh, zone_name,
11093448Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11102712Snn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11112712Snn35248 		goto cleanup;
11122712Snn35248 	}
11132712Snn35248 
11142727Sedp 	if (brand_platform_iter_link(bh,
11152712Snn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11162712Snn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11172712Snn35248 		goto cleanup;
11182712Snn35248 	}
11192712Snn35248 
11202712Snn35248 	/* Add user-specified devices and directories */
11212712Snn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11222712Snn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11232712Snn35248 		goto cleanup;
11242712Snn35248 	}
11252712Snn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11262712Snn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11272712Snn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11282712Snn35248 		goto cleanup;
11292712Snn35248 	}
11302712Snn35248 	if (err = zonecfg_setdevent(handle)) {
11312712Snn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11322712Snn35248 		    zonecfg_strerror(err));
11332712Snn35248 		goto cleanup;
11342712Snn35248 	}
11352712Snn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11362712Snn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11372712Snn35248 			zerror(zlogp, B_TRUE, "failed to add "
11382712Snn35248 			    "user-specified device");
11392712Snn35248 			goto cleanup;
11402712Snn35248 		}
11412712Snn35248 	}
11422712Snn35248 	(void) zonecfg_enddevent(handle);
11432712Snn35248 
11442712Snn35248 	/* Send profile to kernel */
11452712Snn35248 	if (di_prof_commit(prof)) {
11462712Snn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11472712Snn35248 		goto cleanup;
11482712Snn35248 	}
11492712Snn35248 
11502712Snn35248 	retval = 0;
11512712Snn35248 
11522712Snn35248 cleanup:
11532727Sedp 	if (bh != NULL)
11542727Sedp 		brand_close(bh);
11553716Sgjelinek 	if (handle != NULL)
11562712Snn35248 		zonecfg_fini_handle(handle);
11572712Snn35248 	if (prof)
11582712Snn35248 		di_prof_fini(prof);
11592712Snn35248 	return (retval);
11602712Snn35248 }
11612712Snn35248 
11622712Snn35248 static int
11630Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
11640Sstevel@tonic-gate {
11652712Snn35248 	char path[MAXPATHLEN];
11662712Snn35248 	char specpath[MAXPATHLEN];
11672712Snn35248 	char optstr[MAX_MNTOPT_STR];
11680Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11692712Snn35248 	int rv;
11700Sstevel@tonic-gate 
11715182Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
11725182Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11730Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11740Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11750Sstevel@tonic-gate 		return (-1);
11765182Sedp 	} else if (rv > 0) {
11775182Sedp 		/* The mount point path doesn't exist, create it now. */
11785182Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
11795182Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
11805182Sedp 		    DEFAULT_DIR_GROUP) != 0) {
11815182Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
11825182Sedp 			return (-1);
11835182Sedp 		}
11845182Sedp 
11855182Sedp 		/*
11865182Sedp 		 * Now this might seem weird, but we need to invoke
11875182Sedp 		 * valid_mount_path() again.  Why?  Because it checks
11885182Sedp 		 * to make sure that the mount point path is canonical,
11895182Sedp 		 * which it can only do if the path exists, so now that
11905182Sedp 		 * we've created the path we have to verify it again.
11915182Sedp 		 */
11925182Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
11935182Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
11945182Sedp 		    fsptr->zone_fs_type)) < 0) {
11955182Sedp 			zerror(zlogp, B_FALSE,
11965182Sedp 			    "%s%s is not a valid mount point",
11975182Sedp 			    rootpath, fsptr->zone_fs_dir);
11985182Sedp 			return (-1);
11995182Sedp 		}
12005182Sedp 	}
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12030Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
12060Sstevel@tonic-gate 		/*
12070Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1208766Scarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1209766Scarlsonj 		 * can be seen via the alternate zone's root.
12100Sstevel@tonic-gate 		 */
1211766Scarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1212766Scarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1213766Scarlsonj 		    sizeof (specpath)) {
1214766Scarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1215766Scarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1216766Scarlsonj 			return (-1);
1217766Scarlsonj 		}
1218766Scarlsonj 		if (zonecfg_in_alt_root())
1219766Scarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
12200Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1221766Scarlsonj 		    specpath, path) != 0) {
12220Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1223766Scarlsonj 			    specpath);
12240Sstevel@tonic-gate 			return (-1);
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 		return (0);
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	/*
12300Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12310Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12320Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12330Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12340Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12350Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12360Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12370Sstevel@tonic-gate 	 */
12380Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12390Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12400Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12410Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12420Sstevel@tonic-gate 		return (-1);
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	/*
1246766Scarlsonj 	 * If we're looking at an alternate root environment, then construct
12473688Sedp 	 * read-only loopback mounts as necessary.  Note that any special
12483688Sedp 	 * paths for lofs zone mounts in an alternate root must have
12493688Sedp 	 * already been pre-pended with any alternate root path by the
12503688Sedp 	 * time we get here.
1251766Scarlsonj 	 */
1252766Scarlsonj 	if (zonecfg_in_alt_root()) {
1253766Scarlsonj 		struct stat64 st;
1254766Scarlsonj 
1255766Scarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
12562772Scarlsonj 		    S_ISBLK(st.st_mode)) {
12573688Sedp 			/*
12583688Sedp 			 * If we're going to mount a block device we need
12593688Sedp 			 * to check if that device is already mounted
12603688Sedp 			 * somewhere else, and if so, do a lofs mount
12613688Sedp 			 * of the device instead of a direct mount
12623688Sedp 			 */
12632772Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
12642772Scarlsonj 				return (-1);
12652772Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
12663688Sedp 			/*
12673688Sedp 			 * For lofs mounts, the special node is inside the
12683688Sedp 			 * alternate root.  We need lofs resolution for
12693688Sedp 			 * this case in order to get at the underlying
12703688Sedp 			 * read-write path.
12713688Sedp 			 */
12723688Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1273766Scarlsonj 			    sizeof (fsptr->zone_fs_special));
1274766Scarlsonj 		}
1275766Scarlsonj 	}
1276766Scarlsonj 
1277766Scarlsonj 	/*
12780Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12790Sstevel@tonic-gate 	 */
12800Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
12816734Sjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12820Sstevel@tonic-gate 		return (-1);
12836734Sjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
12846734Sjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
12856734Sjohnlev 		return (-1);
12866734Sjohnlev 	}
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	/*
12890Sstevel@tonic-gate 	 * Build up mount option string.
12900Sstevel@tonic-gate 	 */
12910Sstevel@tonic-gate 	optstr[0] = '\0';
12920Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
12930Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12940Sstevel@tonic-gate 		    sizeof (optstr));
12950Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
12960Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
12970Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
12980Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
12990Sstevel@tonic-gate 			    sizeof (optstr));
13000Sstevel@tonic-gate 		}
13010Sstevel@tonic-gate 	}
13022712Snn35248 
13032712Snn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13042712Snn35248 	    fsptr->zone_fs_special, path)) != 0)
13052712Snn35248 		return (rv);
13062712Snn35248 
13072712Snn35248 	/*
13082712Snn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13092712Snn35248 	 * we're done.
13102712Snn35248 	 */
13112712Snn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13122712Snn35248 		return (0);
13132712Snn35248 
13142712Snn35248 	/*
13152712Snn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13162712Snn35248 	 * need to configure it.
13172712Snn35248 	 */
13182712Snn35248 	return (mount_one_dev(zlogp, path));
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate static void
13220Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate 	uint_t i;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	if (fsarray == NULL)
13270Sstevel@tonic-gate 		return;
13280Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13290Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13300Sstevel@tonic-gate 	free(fsarray);
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate 
1333766Scarlsonj /*
13342653Svp157776  * This function initiates the creation of a small Solaris Environment for
13352653Svp157776  * scratch zone. The Environment creation process is split up into two
13362653Svp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
13372653Svp157776  * is done this way because:
13382653Svp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
13392653Svp157776  * 	We loopback mount zone's own /etc and /var into the root of the
13402653Svp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
13412653Svp157776  * 	need to delay the mount of /var till the zone's root gets populated.
13422653Svp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
13432653Svp157776  * 	build_mounted_post_var() which gets called only after the zone
13442653Svp157776  * 	specific filesystems are mounted.
13455829Sgjelinek  *
13465829Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13475829Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13485829Sgjelinek  * scratch zone.
1349766Scarlsonj  */
1350766Scarlsonj static boolean_t
13512653Svp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
13523071Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1353766Scarlsonj {
1354766Scarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1355766Scarlsonj 	const char **cpp;
1356766Scarlsonj 	static const char *mkdirs[] = {
13572592Sdp 		"/system", "/system/contract", "/system/object", "/proc",
13582592Sdp 		"/dev", "/tmp", "/a", NULL
1359766Scarlsonj 	};
13602653Svp157776 	char *altstr;
1361766Scarlsonj 	FILE *fp;
1362766Scarlsonj 	uuid_t uuid;
1363766Scarlsonj 
13644350Std153743 	assert(zone_isnative || zone_iscluster);
13652712Snn35248 
1366766Scarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
13673071Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
13683071Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1369766Scarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1370766Scarlsonj 	(void) symlink("./usr/bin", tmp);
1371766Scarlsonj 
1372766Scarlsonj 	/*
1373766Scarlsonj 	 * These are mostly special mount points; not handled here.  (See
1374766Scarlsonj 	 * zone_mount_early.)
1375766Scarlsonj 	 */
1376766Scarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1377766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1378766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1379766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1380766Scarlsonj 			return (B_FALSE);
1381766Scarlsonj 		}
1382766Scarlsonj 	}
13832653Svp157776 	/*
13842653Svp157776 	 * This is here to support lucopy.  If there's an instance of this same
13852653Svp157776 	 * zone on the current running system, then we mount its root up as
13862653Svp157776 	 * read-only inside the scratch zone.
13872653Svp157776 	 */
13882653Svp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
13892653Svp157776 	altstr = strdup(zonecfg_get_root());
13902653Svp157776 	if (altstr == NULL) {
13912653Svp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
13922653Svp157776 		return (B_FALSE);
13932653Svp157776 	}
13942653Svp157776 	zonecfg_set_root("");
13952653Svp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
13962653Svp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
13972653Svp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
13982653Svp157776 	    strcmp(fromdir, rootpath) != 0) {
13992653Svp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
14002653Svp157776 		if (mkdir(tmp, 0755) != 0) {
14012653Svp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
14022653Svp157776 			return (B_FALSE);
14032653Svp157776 		}
14042653Svp157776 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
14052653Svp157776 		    tmp) != 0) {
14062653Svp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
14072653Svp157776 			    fromdir);
14082653Svp157776 			return (B_FALSE);
14092653Svp157776 		}
14102653Svp157776 	}
14112653Svp157776 	zonecfg_set_root(altstr);
14122653Svp157776 	free(altstr);
14132653Svp157776 
14142653Svp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
14152653Svp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
14162653Svp157776 		return (B_FALSE);
14172653Svp157776 	}
14182653Svp157776 	(void) ftruncate(fileno(fp), 0);
14192653Svp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
14202653Svp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
14212653Svp157776 	}
14222653Svp157776 	zonecfg_close_scratch(fp);
14232653Svp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
14242653Svp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
14252653Svp157776 		return (B_FALSE);
14262653Svp157776 	(void) strlcpy(rootpath, tmp, rootlen);
14272653Svp157776 	return (B_TRUE);
14282653Svp157776 }
14292653Svp157776 
14302653Svp157776 
14312653Svp157776 static boolean_t
14325829Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14335829Sgjelinek     const char *luroot)
14342653Svp157776 {
14352653Svp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
14362653Svp157776 	const char **cpp;
14375829Sgjelinek 	const char **loopdirs;
14385829Sgjelinek 	const char **tmpdirs;
14392653Svp157776 	static const char *localdirs[] = {
14402653Svp157776 		"/etc", "/var", NULL
14412653Svp157776 	};
14425829Sgjelinek 	static const char *scr_loopdirs[] = {
14432653Svp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
14442653Svp157776 		"/usr", NULL
14452653Svp157776 	};
14465829Sgjelinek 	static const char *upd_loopdirs[] = {
14475829Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14485829Sgjelinek 		"/usr", "/var", NULL
14495829Sgjelinek 	};
14505829Sgjelinek 	static const char *scr_tmpdirs[] = {
14512653Svp157776 		"/tmp", "/var/run", NULL
14522653Svp157776 	};
14535829Sgjelinek 	static const char *upd_tmpdirs[] = {
14545829Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14555829Sgjelinek 	};
14562653Svp157776 	struct stat st;
14572653Svp157776 
14585829Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
14595829Sgjelinek 		/*
14605829Sgjelinek 		 * These are mounted read-write from the zone undergoing
14615829Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14625829Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
14635829Sgjelinek 		 */
14645829Sgjelinek 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14655829Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
14665829Sgjelinek 			    *cpp);
14675829Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14685829Sgjelinek 			    rootpath, *cpp);
14695829Sgjelinek 			if (mkdir(tmp, 0755) != 0) {
14705829Sgjelinek 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
14715829Sgjelinek 				return (B_FALSE);
14725829Sgjelinek 			}
14735829Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14745829Sgjelinek 			    != 0) {
14755829Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14765829Sgjelinek 				    tmp, *cpp);
14775829Sgjelinek 				return (B_FALSE);
14785829Sgjelinek 			}
1479766Scarlsonj 		}
14805829Sgjelinek 	}
14815829Sgjelinek 
14825829Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14835829Sgjelinek 		loopdirs = upd_loopdirs;
14845829Sgjelinek 	else
14855829Sgjelinek 		loopdirs = scr_loopdirs;
1486766Scarlsonj 
1487766Scarlsonj 	/*
1488766Scarlsonj 	 * These are things mounted read-only from the running system because
1489766Scarlsonj 	 * they contain binaries that must match system.
1490766Scarlsonj 	 */
1491766Scarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1492766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1493766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1494766Scarlsonj 			if (errno != EEXIST) {
1495766Scarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1496766Scarlsonj 				return (B_FALSE);
1497766Scarlsonj 			}
1498766Scarlsonj 			if (lstat(tmp, &st) != 0) {
1499766Scarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1500766Scarlsonj 				return (B_FALSE);
1501766Scarlsonj 			}
1502766Scarlsonj 			/*
1503766Scarlsonj 			 * Ignore any non-directories encountered.  These are
1504766Scarlsonj 			 * things that have been converted into symlinks
1505766Scarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1506766Scarlsonj 			 * fixup.
1507766Scarlsonj 			 */
1508766Scarlsonj 			if (!S_ISDIR(st.st_mode))
1509766Scarlsonj 				continue;
1510766Scarlsonj 		}
1511766Scarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1512766Scarlsonj 		    tmp) != 0) {
1513766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1514766Scarlsonj 			    *cpp);
1515766Scarlsonj 			return (B_FALSE);
1516766Scarlsonj 		}
1517766Scarlsonj 	}
1518766Scarlsonj 
15195829Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15205829Sgjelinek 		tmpdirs = upd_tmpdirs;
15215829Sgjelinek 	else
15225829Sgjelinek 		tmpdirs = scr_tmpdirs;
15235829Sgjelinek 
1524766Scarlsonj 	/*
1525766Scarlsonj 	 * These are things with tmpfs mounted inside.
1526766Scarlsonj 	 */
1527766Scarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1528766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15295829Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15305829Sgjelinek 		    errno != EEXIST) {
1531766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1532766Scarlsonj 			return (B_FALSE);
1533766Scarlsonj 		}
15343514Sgjelinek 
15353514Sgjelinek 		/*
15363514Sgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
15373514Sgjelinek 		 * since that can be modified by the umask we will just set
15383514Sgjelinek 		 * the correct mode for /tmp now.
15393514Sgjelinek 		 */
15403514Sgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
15413514Sgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
15423514Sgjelinek 			return (B_FALSE);
15433514Sgjelinek 		}
15443514Sgjelinek 
1545766Scarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1546766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1547766Scarlsonj 			return (B_FALSE);
1548766Scarlsonj 		}
1549766Scarlsonj 	}
1550766Scarlsonj 	return (B_TRUE);
1551766Scarlsonj }
1552766Scarlsonj 
15532712Snn35248 typedef struct plat_gmount_cb_data {
15542712Snn35248 	zlog_t			*pgcd_zlogp;
15552712Snn35248 	struct zone_fstab	**pgcd_fs_tab;
15562712Snn35248 	int			*pgcd_num_fs;
15572712Snn35248 } plat_gmount_cb_data_t;
15582712Snn35248 
15592712Snn35248 /*
15602712Snn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15612712Snn35248  * through all global brand platform mounts.
15622712Snn35248  */
15632712Snn35248 int
15642712Snn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15652712Snn35248     const char *fstype, const char *opt)
15662712Snn35248 {
15672712Snn35248 	plat_gmount_cb_data_t	*cp = data;
15682712Snn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15692712Snn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15702712Snn35248 	int			num_fs = *cp->pgcd_num_fs;
15712712Snn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15722712Snn35248 
15732712Snn35248 	num_fs++;
15742712Snn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15752712Snn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15762712Snn35248 		return (-1);
15772712Snn35248 	}
15782712Snn35248 
15792712Snn35248 	fs_ptr = tmp_ptr;
15802712Snn35248 	fsp = &fs_ptr[num_fs - 1];
15812712Snn35248 
15822712Snn35248 	/* update the callback struct passed in */
15832712Snn35248 	*cp->pgcd_fs_tab = fs_ptr;
15842712Snn35248 	*cp->pgcd_num_fs = num_fs;
15852712Snn35248 
15862712Snn35248 	fsp->zone_fs_raw[0] = '\0';
15872712Snn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15882712Snn35248 	    sizeof (fsp->zone_fs_special));
15892712Snn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15902712Snn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15912712Snn35248 	fsp->zone_fs_options = NULL;
15923688Sedp 	if ((opt != NULL) &&
15933688Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15942712Snn35248 		zerror(zlogp, B_FALSE, "error adding property");
15952712Snn35248 		return (-1);
15962712Snn35248 	}
15972712Snn35248 
15982712Snn35248 	return (0);
15992712Snn35248 }
16002712Snn35248 
16012712Snn35248 static int
16022712Snn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
16032712Snn35248     struct zone_fstab **fs_tabp, int *num_fsp)
16042712Snn35248 {
16052712Snn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16062712Snn35248 	int num_fs;
16072712Snn35248 
16082712Snn35248 	num_fs = *num_fsp;
16092712Snn35248 	fs_ptr = *fs_tabp;
16102712Snn35248 
16112712Snn35248 	if (zonecfg_setipdent(handle) != Z_OK) {
16122712Snn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16132712Snn35248 		return (-1);
16142712Snn35248 	}
16152712Snn35248 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
16162712Snn35248 		num_fs++;
16172712Snn35248 		if ((tmp_ptr = realloc(fs_ptr,
16182712Snn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16192712Snn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16202712Snn35248 			(void) zonecfg_endipdent(handle);
16212712Snn35248 			return (-1);
16222712Snn35248 		}
16232712Snn35248 
16242712Snn35248 		/* update the pointers passed in */
16252712Snn35248 		*fs_tabp = tmp_ptr;
16262712Snn35248 		*num_fsp = num_fs;
16272712Snn35248 
16282712Snn35248 		/*
16292712Snn35248 		 * IPDs logically only have a mount point; all other properties
16302712Snn35248 		 * are implied.
16312712Snn35248 		 */
16322712Snn35248 		fs_ptr = tmp_ptr;
16332712Snn35248 		fsp = &fs_ptr[num_fs - 1];
16342712Snn35248 		(void) strlcpy(fsp->zone_fs_dir,
16352712Snn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16362712Snn35248 		fsp->zone_fs_special[0] = '\0';
16372712Snn35248 		fsp->zone_fs_raw[0] = '\0';
16382712Snn35248 		fsp->zone_fs_type[0] = '\0';
16392712Snn35248 		fsp->zone_fs_options = NULL;
16402712Snn35248 	}
16412712Snn35248 	(void) zonecfg_endipdent(handle);
16422712Snn35248 	return (0);
16432712Snn35248 }
16442712Snn35248 
16452712Snn35248 static int
16462712Snn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16475829Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16482712Snn35248 {
16492712Snn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16502712Snn35248 	int num_fs;
16512712Snn35248 
16522712Snn35248 	num_fs = *num_fsp;
16532712Snn35248 	fs_ptr = *fs_tabp;
16542712Snn35248 
16552712Snn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16562712Snn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16572712Snn35248 		return (-1);
16582712Snn35248 	}
16592712Snn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16602712Snn35248 		/*
16612712Snn35248 		 * ZFS filesystems will not be accessible under an alternate
16622712Snn35248 		 * root, since the pool will not be known.  Ignore them in this
16632712Snn35248 		 * case.
16642712Snn35248 		 */
16655829Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16665829Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16672712Snn35248 			continue;
16682712Snn35248 
16692712Snn35248 		num_fs++;
16702712Snn35248 		if ((tmp_ptr = realloc(fs_ptr,
16712712Snn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16722712Snn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16732712Snn35248 			(void) zonecfg_endfsent(handle);
16742712Snn35248 			return (-1);
16752712Snn35248 		}
16762712Snn35248 		/* update the pointers passed in */
16772712Snn35248 		*fs_tabp = tmp_ptr;
16782712Snn35248 		*num_fsp = num_fs;
16792712Snn35248 
16802712Snn35248 		fs_ptr = tmp_ptr;
16812712Snn35248 		fsp = &fs_ptr[num_fs - 1];
16822712Snn35248 		(void) strlcpy(fsp->zone_fs_dir,
16832712Snn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16842712Snn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16852712Snn35248 		    sizeof (fsp->zone_fs_raw));
16862712Snn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16872712Snn35248 		    sizeof (fsp->zone_fs_type));
16882712Snn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
16893688Sedp 
16903688Sedp 		/*
16913688Sedp 		 * For all lofs mounts, make sure that the 'special'
16923688Sedp 		 * entry points inside the alternate root.  The
16933688Sedp 		 * source path for a lofs mount in a given zone needs
16943688Sedp 		 * to be relative to the root of the boot environment
16953688Sedp 		 * that contains the zone.  Note that we don't do this
16963688Sedp 		 * for non-lofs mounts since they will have a device
16973688Sedp 		 * as a backing store and device paths must always be
16983688Sedp 		 * specified relative to the current boot environment.
16993688Sedp 		 */
17003688Sedp 		fsp->zone_fs_special[0] = '\0';
17013688Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
17023688Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
17033688Sedp 			    sizeof (fsp->zone_fs_special));
17043688Sedp 		}
17053688Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
17063688Sedp 		    sizeof (fsp->zone_fs_special));
17072712Snn35248 	}
17082712Snn35248 	(void) zonecfg_endfsent(handle);
17092712Snn35248 	return (0);
17102712Snn35248 }
17112712Snn35248 
17120Sstevel@tonic-gate static int
17135829Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
17140Sstevel@tonic-gate {
17152712Snn35248 	char rootpath[MAXPATHLEN];
17162712Snn35248 	char zonepath[MAXPATHLEN];
17172712Snn35248 	char brand[MAXNAMELEN];
17183071Svp157776 	char luroot[MAXPATHLEN];
17192712Snn35248 	int i, num_fs = 0;
17202712Snn35248 	struct zone_fstab *fs_ptr = NULL;
17210Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
17220Sstevel@tonic-gate 	zone_state_t zstate;
17232727Sedp 	brand_handle_t bh;
17242712Snn35248 	plat_gmount_cb_data_t cb;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1727766Scarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
17280Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1729766Scarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1730766Scarlsonj 		    zone_state_str(ZONE_STATE_READY),
1731766Scarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
17320Sstevel@tonic-gate 		goto bad;
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
17360Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
17370Sstevel@tonic-gate 		goto bad;
17380Sstevel@tonic-gate 	}
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
17410Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
17420Sstevel@tonic-gate 		goto bad;
17430Sstevel@tonic-gate 	}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
17461645Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17470Sstevel@tonic-gate 		goto bad;
17480Sstevel@tonic-gate 	}
17490Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17500Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17510Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17520Sstevel@tonic-gate 		goto bad;
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 
17552712Snn35248 	/* Get a handle to the brand info for this zone */
17562712Snn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
17572727Sedp 	    (bh = brand_open(brand)) == NULL) {
17582712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
17593716Sgjelinek 		zonecfg_fini_handle(handle);
17602712Snn35248 		return (-1);
17612712Snn35248 	}
17622712Snn35248 
17632712Snn35248 	/*
17642712Snn35248 	 * Get the list of global filesystems to mount from the brand
17652712Snn35248 	 * configuration.
17662712Snn35248 	 */
17672712Snn35248 	cb.pgcd_zlogp = zlogp;
17682712Snn35248 	cb.pgcd_fs_tab = &fs_ptr;
17692712Snn35248 	cb.pgcd_num_fs = &num_fs;
17702727Sedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17712712Snn35248 	    plat_gmount_cb, &cb) != 0) {
17722712Snn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
17732727Sedp 		brand_close(bh);
17743716Sgjelinek 		zonecfg_fini_handle(handle);
17752712Snn35248 		return (-1);
17762712Snn35248 	}
17772727Sedp 	brand_close(bh);
17782712Snn35248 
17790Sstevel@tonic-gate 	/*
17800Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
17810Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
17820Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
17830Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
17840Sstevel@tonic-gate 	 */
17852712Snn35248 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
17860Sstevel@tonic-gate 		goto bad;
17872712Snn35248 
17882712Snn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17892712Snn35248 	    mount_cmd) != 0)
17902712Snn35248 		goto bad;
17912712Snn35248 
17920Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17930Sstevel@tonic-gate 	handle = NULL;
17940Sstevel@tonic-gate 
1795766Scarlsonj 	/*
17962712Snn35248 	 * Normally when we mount a zone all the zone filesystems
17972712Snn35248 	 * get mounted relative to rootpath, which is usually
17982712Snn35248 	 * <zonepath>/root.  But when mounting a zone for administration
17992712Snn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
18002712Snn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
18012712Snn35248 	 * the zones filesystems there instead.
18022712Snn35248 	 *
18032712Snn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
18042712Snn35248 	 * also do some extra work to create directories and lofs mount
18052712Snn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
18062712Snn35248 	 *
18072712Snn35248 	 * This allows us to be able to enter the zone (now rooted at
18082712Snn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
18092712Snn35248 	 * global zone and have them upgrade the to-be-modified zone's
18102712Snn35248 	 * files mounted on /a.  (Which mirrors the existing standard
18112712Snn35248 	 * upgrade environment.)
18122712Snn35248 	 *
18132712Snn35248 	 * There is of course one catch.  When doing the upgrade
18142712Snn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
18152712Snn35248 	 * for the zone and we don't want to have any /dev filesystem
18162712Snn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
18172712Snn35248 	 * as a normal zone filesystem by default we'll try to mount
18182712Snn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
18192712Snn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
18202712Snn35248 	 *
18212712Snn35248 	 * All this work is done in three phases:
18222653Svp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
18232653Svp157776 	 *   2) Mount the required filesystems as per the zone configuration.
18242653Svp157776 	 *   3) Set up the rest of the scratch zone environment
18252653Svp157776 	 *	(build_mounted_post_var()).
1826766Scarlsonj 	 */
18275829Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
18283071Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1829766Scarlsonj 		goto bad;
1830766Scarlsonj 
18310Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
18322712Snn35248 
18330Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
18345829Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
18352712Snn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
18362712Snn35248 			size_t slen = strlen(rootpath) - 2;
18372712Snn35248 
18382712Snn35248 			/*
18392712Snn35248 			 * By default we'll try to mount /dev as /a/dev
18402712Snn35248 			 * but /dev is special and always goes at the top
18412712Snn35248 			 * so strip the trailing '/a' from the rootpath.
18422712Snn35248 			 */
18434350Std153743 			assert(zone_isnative || zone_iscluster);
18442712Snn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18452712Snn35248 			rootpath[slen] = '\0';
18462712Snn35248 			if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
18472712Snn35248 				goto bad;
18482712Snn35248 			rootpath[slen] = '/';
18492712Snn35248 			continue;
18502712Snn35248 		}
18510Sstevel@tonic-gate 		if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
18520Sstevel@tonic-gate 			goto bad;
18530Sstevel@tonic-gate 	}
18545829Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18555829Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
18562653Svp157776 		goto bad;
18571676Sjpk 
18581676Sjpk 	/*
18591676Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
18601676Sjpk 	 */
18615829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18625829Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
18631676Sjpk 		goto bad;
18641676Sjpk 
18650Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	/*
18680Sstevel@tonic-gate 	 * Everything looks fine.
18690Sstevel@tonic-gate 	 */
18700Sstevel@tonic-gate 	return (0);
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate bad:
18730Sstevel@tonic-gate 	if (handle != NULL)
18740Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18750Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18760Sstevel@tonic-gate 	return (-1);
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18800Sstevel@tonic-gate static int
18810Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18820Sstevel@tonic-gate {
18830Sstevel@tonic-gate 	int prefixlen;
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18860Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18870Sstevel@tonic-gate 		return (1);
18880Sstevel@tonic-gate 	while (prefixlen > 0) {
18890Sstevel@tonic-gate 		if (prefixlen >= 8) {
18900Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18910Sstevel@tonic-gate 			prefixlen -= 8;
18920Sstevel@tonic-gate 			continue;
18930Sstevel@tonic-gate 		}
18940Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18950Sstevel@tonic-gate 		prefixlen--;
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 	return (0);
18980Sstevel@tonic-gate }
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate /*
19010Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
19020Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
19030Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
19040Sstevel@tonic-gate  *
19050Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
19060Sstevel@tonic-gate  */
19070Sstevel@tonic-gate static int
19083448Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
19090Sstevel@tonic-gate {
19100Sstevel@tonic-gate 	struct lifnum lifn;
19110Sstevel@tonic-gate 	struct lifconf lifc;
19120Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
19130Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
19140Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
19150Sstevel@tonic-gate 	uint_t bufsize;
19160Sstevel@tonic-gate 	char *buf = NULL;
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
19190Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
19200Sstevel@tonic-gate 		ret_code = -1;
19210Sstevel@tonic-gate 		goto bad;
19220Sstevel@tonic-gate 	}
19230Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
19240Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
19250Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
19260Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
19273448Sdh155122 		    "could not determine number of network interfaces");
19280Sstevel@tonic-gate 		ret_code = -1;
19290Sstevel@tonic-gate 		goto bad;
19300Sstevel@tonic-gate 	}
19310Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
19320Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
19330Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
19340Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
19350Sstevel@tonic-gate 		ret_code = -1;
19360Sstevel@tonic-gate 		goto bad;
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
19390Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19400Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19410Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19420Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
19433448Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
19443448Sdh155122 		    "interfaces");
19450Sstevel@tonic-gate 		ret_code = -1;
19460Sstevel@tonic-gate 		goto bad;
19470Sstevel@tonic-gate 	}
19480Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19490Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19500Sstevel@tonic-gate 		(void) close(s);
19510Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19520Sstevel@tonic-gate 		    0) {
19530Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19540Sstevel@tonic-gate 			    lifrl.lifr_name);
19550Sstevel@tonic-gate 			ret_code = -1;
19560Sstevel@tonic-gate 			continue;
19570Sstevel@tonic-gate 		}
19580Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19590Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19600Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19610Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
19623251Ssl108498 			if (errno == ENXIO)
19633251Ssl108498 				/*
19643251Ssl108498 				 * Interface may have been removed by admin or
19653251Ssl108498 				 * another zone halting.
19663251Ssl108498 				 */
19673251Ssl108498 				continue;
19680Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
19693251Ssl108498 			    "%s: could not determine the zone to which this "
19703448Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19710Sstevel@tonic-gate 			ret_code = -1;
19720Sstevel@tonic-gate 			continue;
19730Sstevel@tonic-gate 		}
19740Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19750Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19760Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
19773448Sdh155122 				    "%s: could not remove network interface",
19780Sstevel@tonic-gate 				    lifrl.lifr_name);
19790Sstevel@tonic-gate 				ret_code = -1;
19800Sstevel@tonic-gate 				continue;
19810Sstevel@tonic-gate 			}
19820Sstevel@tonic-gate 		}
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate bad:
19850Sstevel@tonic-gate 	if (s > 0)
19860Sstevel@tonic-gate 		(void) close(s);
19870Sstevel@tonic-gate 	if (buf)
19880Sstevel@tonic-gate 		free(buf);
19890Sstevel@tonic-gate 	return (ret_code);
19900Sstevel@tonic-gate }
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate static union	sockunion {
19930Sstevel@tonic-gate 	struct	sockaddr sa;
19940Sstevel@tonic-gate 	struct	sockaddr_in sin;
19950Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19960Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19970Sstevel@tonic-gate } so_dst, so_ifp;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate static struct {
20000Sstevel@tonic-gate 	struct	rt_msghdr hdr;
20010Sstevel@tonic-gate 	char	space[512];
20020Sstevel@tonic-gate } rtmsg;
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate static int
20050Sstevel@tonic-gate salen(struct sockaddr *sa)
20060Sstevel@tonic-gate {
20070Sstevel@tonic-gate 	switch (sa->sa_family) {
20080Sstevel@tonic-gate 	case AF_INET:
20090Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
20100Sstevel@tonic-gate 	case AF_LINK:
20110Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
20120Sstevel@tonic-gate 	case AF_INET6:
20130Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
20140Sstevel@tonic-gate 	default:
20150Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
20160Sstevel@tonic-gate 	}
20170Sstevel@tonic-gate }
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
20200Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate /*
20230Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
20240Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
20250Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
20260Sstevel@tonic-gate  *
20270Sstevel@tonic-gate  * This is done using black router socket magic.
20280Sstevel@tonic-gate  *
20290Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
20300Sstevel@tonic-gate  *
20310Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
20320Sstevel@tonic-gate  * of this might be a useful RFE.
20330Sstevel@tonic-gate  */
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate static char *
20360Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20370Sstevel@tonic-gate {
20380Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
20390Sstevel@tonic-gate 	pid_t pid;
20400Sstevel@tonic-gate 	int s, rlen, l, i;
20410Sstevel@tonic-gate 	char *cp = rtmsg.space;
20420Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20430Sstevel@tonic-gate 	struct sockaddr *sa;
20440Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	answer[0] = '\0';
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	pid = getpid();
20490Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20500Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20510Sstevel@tonic-gate 		return (NULL);
20520Sstevel@tonic-gate 	}
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20550Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20580Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20590Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20600Sstevel@tonic-gate 	} else {
20610Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20640Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20650Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20660Sstevel@tonic-gate 	}
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20710Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20720Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20730Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20740Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20750Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20780Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20790Sstevel@tonic-gate 	cp += l;
20800Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20810Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20820Sstevel@tonic-gate 	cp += l;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20870Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20880Sstevel@tonic-gate 		return (NULL);
20890Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20900Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20910Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20920Sstevel@tonic-gate 		return (NULL);
20930Sstevel@tonic-gate 	}
20940Sstevel@tonic-gate 	do {
20950Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20960Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20970Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20980Sstevel@tonic-gate 	if (l < 0) {
20990Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
21000Sstevel@tonic-gate 		return (NULL);
21010Sstevel@tonic-gate 	}
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
21040Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
21050Sstevel@tonic-gate 		    "routing message version %d not understood",
21060Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
21070Sstevel@tonic-gate 		return (NULL);
21080Sstevel@tonic-gate 	}
21090Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
21100Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
21110Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
21120Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
21130Sstevel@tonic-gate 		return (NULL);
21140Sstevel@tonic-gate 	}
21150Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
21160Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
21170Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
21180Sstevel@tonic-gate 		return (NULL);
21190Sstevel@tonic-gate 	}
21200Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
21213448Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
21220Sstevel@tonic-gate 		return (NULL);
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
21250Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
21260Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
21270Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
21280Sstevel@tonic-gate 		if (i != RTA_IFP) {
21290Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
21300Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
21310Sstevel@tonic-gate 			continue;
21320Sstevel@tonic-gate 		}
21330Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
21340Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
21350Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
21360Sstevel@tonic-gate 		break;
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 	if (ifp == NULL) {
21393448Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
21403448Sdh155122 		    "determined");
21410Sstevel@tonic-gate 		return (NULL);
21420Sstevel@tonic-gate 	}
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 	/*
21450Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21460Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21470Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21480Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21490Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21500Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21510Sstevel@tonic-gate 	 */
21520Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21530Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21540Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21550Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21560Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21570Sstevel@tonic-gate 	if (i < 0) {
21580Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
21593448Sdh155122 		    "%s: could not determine the zone network interface "
21603448Sdh155122 		    "belongs to", lifr->lifr_name);
21610Sstevel@tonic-gate 		return (NULL);
21620Sstevel@tonic-gate 	}
21630Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21640Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21650Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	if (strlen(answer) > 0)
21680Sstevel@tonic-gate 		return (answer);
21690Sstevel@tonic-gate 	return (NULL);
21700Sstevel@tonic-gate }
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate typedef struct mcast_rtmsg_s {
21730Sstevel@tonic-gate 	struct rt_msghdr	m_rtm;
21740Sstevel@tonic-gate 	union {
21750Sstevel@tonic-gate 		struct {
21760Sstevel@tonic-gate 			struct sockaddr_in	m_dst;
21770Sstevel@tonic-gate 			struct sockaddr_in	m_gw;
21780Sstevel@tonic-gate 			struct sockaddr_in	m_netmask;
21790Sstevel@tonic-gate 		} m_v4;
21800Sstevel@tonic-gate 		struct {
21810Sstevel@tonic-gate 			struct sockaddr_in6	m_dst;
21820Sstevel@tonic-gate 			struct sockaddr_in6	m_gw;
21830Sstevel@tonic-gate 			struct sockaddr_in6	m_netmask;
21840Sstevel@tonic-gate 		} m_v6;
21850Sstevel@tonic-gate 	} m_u;
21860Sstevel@tonic-gate } mcast_rtmsg_t;
21870Sstevel@tonic-gate #define	m_dst4		m_u.m_v4.m_dst
21880Sstevel@tonic-gate #define	m_dst6		m_u.m_v6.m_dst
21890Sstevel@tonic-gate #define	m_gw4		m_u.m_v4.m_gw
21900Sstevel@tonic-gate #define	m_gw6		m_u.m_v6.m_gw
21910Sstevel@tonic-gate #define	m_netmask4	m_u.m_v4.m_netmask
21920Sstevel@tonic-gate #define	m_netmask6	m_u.m_v6.m_netmask
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate /*
21950Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21960Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21970Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21980Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21990Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
22000Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
22010Sstevel@tonic-gate  *
22020Sstevel@tonic-gate  * A default interface route for multicast is created on the first IPv4 and
22030Sstevel@tonic-gate  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
22040Sstevel@tonic-gate  * This should really be done in the init scripts if we ever allow zones to
22050Sstevel@tonic-gate  * modify the routing tables.
22060Sstevel@tonic-gate  *
22070Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
22080Sstevel@tonic-gate  * down whatever we set up and return an error.
22090Sstevel@tonic-gate  */
22100Sstevel@tonic-gate static int
22110Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
22120Sstevel@tonic-gate     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
22130Sstevel@tonic-gate     boolean_t *mcast_rt_v6_setp)
22140Sstevel@tonic-gate {
22150Sstevel@tonic-gate 	struct lifreq lifr;
22160Sstevel@tonic-gate 	struct sockaddr_in netmask4;
22170Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
22180Sstevel@tonic-gate 	struct in_addr in4;
22190Sstevel@tonic-gate 	struct in6_addr in6;
22200Sstevel@tonic-gate 	sa_family_t af;
22210Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
22220Sstevel@tonic-gate 	mcast_rtmsg_t mcast_rtmsg;
22230Sstevel@tonic-gate 	int s;
22240Sstevel@tonic-gate 	int rs;
22250Sstevel@tonic-gate 	int rlen;
22260Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
22270Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
22280Sstevel@tonic-gate 	int res;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
22310Sstevel@tonic-gate 	if (res != Z_OK) {
22320Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
22330Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
22340Sstevel@tonic-gate 		return (-1);
22350Sstevel@tonic-gate 	}
22360Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
22370Sstevel@tonic-gate 	if (af == AF_INET)
22380Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
22390Sstevel@tonic-gate 	else
22400Sstevel@tonic-gate 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
22430Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
22440Sstevel@tonic-gate 		return (-1);
22450Sstevel@tonic-gate 	}
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
22480Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
22490Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
22502611Svp157776 		/*
22512611Svp157776 		 * Here, we know that the interface can't be brought up.
22522611Svp157776 		 * A similar warning message was already printed out to
22532611Svp157776 		 * the console by zoneadm(1M) so instead we log the
22542611Svp157776 		 * message to syslog and continue.
22552611Svp157776 		 */
22563448Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
22572611Svp157776 		    "'%s' which may not be present/plumbed in the "
22582611Svp157776 		    "global zone.", lifr.lifr_name);
22590Sstevel@tonic-gate 		(void) close(s);
22602611Svp157776 		return (Z_OK);
22610Sstevel@tonic-gate 	}
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22640Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
22650Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
22660Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22670Sstevel@tonic-gate 		goto bad;
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22710Sstevel@tonic-gate 	if (af == AF_INET)
22720Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22750Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
22763448Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
22773448Sdh155122 		    "into zone", lifr.lifr_name);
22780Sstevel@tonic-gate 		goto bad;
22790Sstevel@tonic-gate 	}
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
22820Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
22830Sstevel@tonic-gate 	} else {
22840Sstevel@tonic-gate 		if (af == AF_INET) {
22850Sstevel@tonic-gate 			/*
22860Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
22870Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
22880Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
22890Sstevel@tonic-gate 			 * being able to determine it is a common failure,
22900Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
22910Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
22920Sstevel@tonic-gate 			 * printed after the rest of the interface's
22930Sstevel@tonic-gate 			 * parameters have been configured.
22940Sstevel@tonic-gate 			 */
22950Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
22960Sstevel@tonic-gate 			if (slashp != NULL) {
22970Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22980Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
22990Sstevel@tonic-gate 					*slashp = '/';
23000Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
23010Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
23020Sstevel@tonic-gate 					    lifr.lifr_name,
23030Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
23040Sstevel@tonic-gate 					goto bad;
23050Sstevel@tonic-gate 				}
23060Sstevel@tonic-gate 				got_netmask = B_TRUE;
23070Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
23080Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
23090Sstevel@tonic-gate 				got_netmask = B_TRUE;
23100Sstevel@tonic-gate 			}
23110Sstevel@tonic-gate 			if (got_netmask) {
23120Sstevel@tonic-gate 				netmask4.sin_family = af;
23130Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
23140Sstevel@tonic-gate 				    sizeof (netmask4));
23150Sstevel@tonic-gate 			}
23160Sstevel@tonic-gate 		} else {
23170Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
23180Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
23190Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
23200Sstevel@tonic-gate 				*slashp = '/';
23210Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
23220Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
23230Sstevel@tonic-gate 				    lifr.lifr_name,
23240Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
23250Sstevel@tonic-gate 				goto bad;
23260Sstevel@tonic-gate 			}
23270Sstevel@tonic-gate 			got_netmask = B_TRUE;
23280Sstevel@tonic-gate 			netmask6.sin6_family = af;
23290Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
23300Sstevel@tonic-gate 			    sizeof (netmask6));
23310Sstevel@tonic-gate 		}
23320Sstevel@tonic-gate 		if (got_netmask &&
23330Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
23340Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
23350Sstevel@tonic-gate 			    lifr.lifr_name);
23360Sstevel@tonic-gate 			goto bad;
23370Sstevel@tonic-gate 		}
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 		/*
23400Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
23410Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
23420Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
23430Sstevel@tonic-gate 		 */
23440Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23450Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23460Sstevel@tonic-gate 			    lifr.lifr_name);
23470Sstevel@tonic-gate 			goto bad;
23480Sstevel@tonic-gate 		}
23490Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
23500Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23510Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
23520Sstevel@tonic-gate 			    lifr.lifr_name);
23530Sstevel@tonic-gate 			goto bad;
23540Sstevel@tonic-gate 		}
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23580Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
23590Sstevel@tonic-gate 		    lifr.lifr_name);
23600Sstevel@tonic-gate 		goto bad;
23610Sstevel@tonic-gate 	}
23620Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
23630Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
23640Sstevel@tonic-gate 		int save_errno = errno;
23650Sstevel@tonic-gate 		char *zone_using;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 		/*
23680Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
23690Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
23700Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23710Sstevel@tonic-gate 		 * using that address.
23720Sstevel@tonic-gate 		 */
23730Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23740Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23753448Sdh155122 			    "%s: could not bring network interface up",
23763448Sdh155122 			    lifr.lifr_name);
23770Sstevel@tonic-gate 			goto bad;
23780Sstevel@tonic-gate 		}
23790Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23800Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23810Sstevel@tonic-gate 			    lifr.lifr_name);
23820Sstevel@tonic-gate 			goto bad;
23830Sstevel@tonic-gate 		}
23840Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23850Sstevel@tonic-gate 		errno = save_errno;
23860Sstevel@tonic-gate 		if (zone_using == NULL)
23870Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23883448Sdh155122 			    "%s: could not bring network interface up",
23893448Sdh155122 			    lifr.lifr_name);
23900Sstevel@tonic-gate 		else
23913448Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
23923448Sdh155122 			    "interface up: address in use by zone '%s'",
23933448Sdh155122 			    lifr.lifr_name, zone_using);
23940Sstevel@tonic-gate 		goto bad;
23950Sstevel@tonic-gate 	}
23960Sstevel@tonic-gate 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
23970Sstevel@tonic-gate 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
23980Sstevel@tonic-gate 	    (af == AF_INET6 &&
23990Sstevel@tonic-gate 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
24000Sstevel@tonic-gate 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
24010Sstevel@tonic-gate 		if (rs < 0) {
24020Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not create "
24030Sstevel@tonic-gate 			    "routing socket", lifr.lifr_name);
24040Sstevel@tonic-gate 			goto bad;
24050Sstevel@tonic-gate 		}
24060Sstevel@tonic-gate 		(void) shutdown(rs, 0);
24070Sstevel@tonic-gate 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
24080Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
24090Sstevel@tonic-gate 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
24100Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
24110Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
24120Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
24130Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
24140Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_addrs =
24150Sstevel@tonic-gate 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
24160Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
24170Sstevel@tonic-gate 		if (af == AF_INET) {
24180Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
24190Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
24200Sstevel@tonic-gate 			    htonl(INADDR_UNSPEC_GROUP);
24210Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
24220Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_addr = in4;
24230Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
24240Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
24250Sstevel@tonic-gate 			    htonl(IN_CLASSD_NET);
24260Sstevel@tonic-gate 		} else {
24270Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
24280Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
24290Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
24300Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_addr = in6;
24310Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
24320Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
24330Sstevel@tonic-gate 		}
24340Sstevel@tonic-gate 		rlen = write(rs, (char *)&mcast_rtmsg,
24350Sstevel@tonic-gate 		    mcast_rtmsg.m_rtm.rtm_msglen);
24362611Svp157776 		/*
24372611Svp157776 		 * The write to the multicast socket will fail if the
24382611Svp157776 		 * interface belongs to a failed IPMP group. This is a
24392611Svp157776 		 * non-fatal error and the zone will continue booting.
24402611Svp157776 		 * While the zone is running, if any interface in the
24412611Svp157776 		 * failed IPMP group recovers, the zone will fallback to
24422611Svp157776 		 * using that interface.
24432611Svp157776 		 */
24440Sstevel@tonic-gate 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
24450Sstevel@tonic-gate 			if (rlen < 0) {
24463448Sdh155122 				zerror(zlogp, B_TRUE, "WARNING: network "
24473448Sdh155122 				    "interface '%s' not available as default "
24483448Sdh155122 				    "for multicast.", lifr.lifr_name);
24490Sstevel@tonic-gate 			} else {
24503448Sdh155122 				zerror(zlogp, B_FALSE, "WARNING: network "
24513448Sdh155122 				    "interface '%s' not available as default "
24523448Sdh155122 				    "for multicast; routing socket returned "
24532611Svp157776 				    "unexpected %d bytes.",
24542611Svp157776 				    lifr.lifr_name, rlen);
24550Sstevel@tonic-gate 			}
24560Sstevel@tonic-gate 		} else {
24572611Svp157776 
24582611Svp157776 			if (af == AF_INET) {
24592611Svp157776 				*mcast_rt_v4_setp = B_TRUE;
24602611Svp157776 			} else {
24612611Svp157776 				*mcast_rt_v6_setp = B_TRUE;
24622611Svp157776 			}
24630Sstevel@tonic-gate 		}
24640Sstevel@tonic-gate 		(void) close(rs);
24650Sstevel@tonic-gate 	}
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	if (!got_netmask) {
24680Sstevel@tonic-gate 		/*
24690Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
24700Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
24710Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
24720Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
24730Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
24740Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
24750Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
24760Sstevel@tonic-gate 		 * available. We warn the user here that something like this
24770Sstevel@tonic-gate 		 * has happened and we're just running with a default and
24780Sstevel@tonic-gate 		 * possible incorrect netmask.
24790Sstevel@tonic-gate 		 */
24800Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
24810Sstevel@tonic-gate 		void  *addr;
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 		if (af == AF_INET)
24840Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
24850Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
24860Sstevel@tonic-gate 		else
24870Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
24880Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
24910Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
24920Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
24930Sstevel@tonic-gate 			goto bad;
24940Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
24950Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
24960Sstevel@tonic-gate 		    "%s; using default of %s.",
24970Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
24980Sstevel@tonic-gate 	}
24990Sstevel@tonic-gate 
25006076Sgfaden 	/*
25016076Sgfaden 	 * If a default router was specified for this interface
25026076Sgfaden 	 * set the route now. Ignore if already set.
25036076Sgfaden 	 */
25046076Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
25056076Sgfaden 		int status;
25066076Sgfaden 		char *argv[7];
25076076Sgfaden 
25086076Sgfaden 		argv[0] = "route";
25096076Sgfaden 		argv[1] = "add";
25106076Sgfaden 		argv[2] = "-ifp";
25116076Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
25126076Sgfaden 		argv[4] = "default";
25136076Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
25146076Sgfaden 		argv[6] = NULL;
25156076Sgfaden 
25166076Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
25176076Sgfaden 		if (status != 0 && status != EEXIST)
25186076Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
25196076Sgfaden 			    "interface %s to %s\n",
25206076Sgfaden 			    nwiftabptr->zone_nwif_physical,
25216076Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
25226076Sgfaden 	}
25236076Sgfaden 
25240Sstevel@tonic-gate 	(void) close(s);
25250Sstevel@tonic-gate 	return (Z_OK);
25260Sstevel@tonic-gate bad:
25270Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
25280Sstevel@tonic-gate 	(void) close(s);
25290Sstevel@tonic-gate 	return (-1);
25300Sstevel@tonic-gate }
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate /*
25330Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
25340Sstevel@tonic-gate  * An IPv4 loopback interface is set up "for free", modeling the global system.
25350Sstevel@tonic-gate  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
25360Sstevel@tonic-gate  * address is set up as well.
25370Sstevel@tonic-gate  *
25380Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
25390Sstevel@tonic-gate  * whatever we set up, and return an error.
25400Sstevel@tonic-gate  */
25410Sstevel@tonic-gate static int
25423448Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
25430Sstevel@tonic-gate {
25440Sstevel@tonic-gate 	zone_dochandle_t handle;
25450Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
25460Sstevel@tonic-gate 	boolean_t saw_v6 = B_FALSE;
25470Sstevel@tonic-gate 	boolean_t mcast_rt_v4_set = B_FALSE;
25480Sstevel@tonic-gate 	boolean_t mcast_rt_v6_set = B_FALSE;
25490Sstevel@tonic-gate 	zoneid_t zoneid;
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
25520Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
25530Sstevel@tonic-gate 		return (-1);
25540Sstevel@tonic-gate 	}
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
25570Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
25580Sstevel@tonic-gate 		return (-1);
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
25610Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
25620Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
25630Sstevel@tonic-gate 		return (-1);
25640Sstevel@tonic-gate 	}
25650Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
25660Sstevel@tonic-gate 		for (;;) {
25670Sstevel@tonic-gate 			struct in6_addr in6;
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
25700Sstevel@tonic-gate 				break;
25710Sstevel@tonic-gate 			if (configure_one_interface(zlogp, zoneid,
25720Sstevel@tonic-gate 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
25730Sstevel@tonic-gate 			    Z_OK) {
25740Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
25750Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
25760Sstevel@tonic-gate 				return (-1);
25770Sstevel@tonic-gate 			}
25780Sstevel@tonic-gate 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
25790Sstevel@tonic-gate 			    &in6) == 1)
25800Sstevel@tonic-gate 				saw_v6 = B_TRUE;
25810Sstevel@tonic-gate 		}
25820Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
25830Sstevel@tonic-gate 	}
25840Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
25855863Sgfaden 	if (is_system_labeled()) {
25865863Sgfaden 		/*
25875863Sgfaden 		 * Labeled zones share the loopback interface
25885863Sgfaden 		 * so it is not plumbed for shared stack instances.
25895863Sgfaden 		 */
25905863Sgfaden 		return (0);
25915863Sgfaden 	}
25920Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
25930Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
25940Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
25950Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
25966378Sgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
25970Sstevel@tonic-gate 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
25980Sstevel@tonic-gate 	    != Z_OK) {
25990Sstevel@tonic-gate 		return (-1);
26000Sstevel@tonic-gate 	}
26010Sstevel@tonic-gate 	if (saw_v6) {
26020Sstevel@tonic-gate 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
26030Sstevel@tonic-gate 		    sizeof (loopback_iftab.zone_nwif_address));
26040Sstevel@tonic-gate 		if (configure_one_interface(zlogp, zoneid,
26050Sstevel@tonic-gate 		    &loopback_iftab, NULL, NULL) != Z_OK) {
26060Sstevel@tonic-gate 			return (-1);
26070Sstevel@tonic-gate 		}
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate 	return (0);
26100Sstevel@tonic-gate }
26110Sstevel@tonic-gate 
26123448Sdh155122 static void
26133448Sdh155122 show_owner(zlog_t *zlogp, char *dlname)
26143448Sdh155122 {
26153448Sdh155122 	zoneid_t dl_owner_zid;
26163448Sdh155122 	char dl_owner_zname[ZONENAME_MAX];
26173448Sdh155122 
26183448Sdh155122 	dl_owner_zid = ALL_ZONES;
26193448Sdh155122 	if (zone_check_datalink(&dl_owner_zid, dlname) != 0)
26203448Sdh155122 		(void) snprintf(dl_owner_zname, ZONENAME_MAX, "<unknown>");
26213448Sdh155122 	else if (getzonenamebyid(dl_owner_zid, dl_owner_zname, ZONENAME_MAX)
26223448Sdh155122 	    < 0)
26233448Sdh155122 		(void) snprintf(dl_owner_zname, ZONENAME_MAX, "<%d>",
26243448Sdh155122 		    dl_owner_zid);
26253448Sdh155122 
26263448Sdh155122 	errno = EPERM;
26273448Sdh155122 	zerror(zlogp, B_TRUE, "WARNING: skipping network interface '%s' "
26283448Sdh155122 	    "which is used by the non-global zone '%s'.\n",
26293448Sdh155122 	    dlname, dl_owner_zname);
26303448Sdh155122 }
26313448Sdh155122 
26323448Sdh155122 static int
26333448Sdh155122 add_datalink(zlog_t *zlogp, zoneid_t zoneid, char *dlname)
26343448Sdh155122 {
26353448Sdh155122 	/* First check if it's in use by global zone. */
26363448Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
26373448Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
26383448Sdh155122 		errno = EPERM;
26393448Sdh155122 		zerror(zlogp, B_TRUE, "WARNING: skipping network interface "
26403448Sdh155122 		    "'%s' which is used in the global zone.", dlname);
26413448Sdh155122 		return (-1);
26423448Sdh155122 	}
26433448Sdh155122 
26443448Sdh155122 	/* Add access control information */
26453448Sdh155122 	if (zone_add_datalink(zoneid, dlname) != 0) {
26463448Sdh155122 		/* If someone got this link before us, show its name */
26473448Sdh155122 		if (errno == EPERM)
26483448Sdh155122 			show_owner(zlogp, dlname);
26493448Sdh155122 		else
26503448Sdh155122 			zerror(zlogp, B_TRUE, "WARNING: unable to add network "
26513448Sdh155122 			    "interface '%s'.", dlname);
26523448Sdh155122 		return (-1);
26533448Sdh155122 	}
26543448Sdh155122 
26555895Syz147064 	/* Set zoneid of this link. */
26565895Syz147064 	if (dladm_setzid(dlname, zoneid) != DLADM_STATUS_OK) {
26575895Syz147064 		zerror(zlogp, B_TRUE, "WARNING: unable to add network "
26585895Syz147064 		    "interface '%s'.", dlname);
26593448Sdh155122 		(void) zone_remove_datalink(zoneid, dlname);
26603448Sdh155122 		return (-1);
26613448Sdh155122 	}
26623448Sdh155122 
26633448Sdh155122 	return (0);
26643448Sdh155122 }
26653448Sdh155122 
26663448Sdh155122 static int
26673448Sdh155122 remove_datalink(zlog_t *zlogp, zoneid_t zoneid, char *dlname)
26683448Sdh155122 {
26693448Sdh155122 	/*
26703448Sdh155122 	 * Remove access control information.
26713448Sdh155122 	 * If the errno is ENXIO, the interface is not added yet,
26723448Sdh155122 	 * nothing to report then.
26733448Sdh155122 	 */
26743448Sdh155122 	if (zone_remove_datalink(zoneid, dlname) != 0) {
26753448Sdh155122 		if (errno == ENXIO)
26763448Sdh155122 			return (0);
26773448Sdh155122 		zerror(zlogp, B_TRUE, "unable to remove network interface '%s'",
26783448Sdh155122 		    dlname);
26793448Sdh155122 		return (-1);
26803448Sdh155122 	}
26813448Sdh155122 
26825895Syz147064 	if (dladm_setzid(dlname, GLOBAL_ZONEID) != DLADM_STATUS_OK) {
26835895Syz147064 		zerror(zlogp, B_TRUE, "unable to release network "
26845895Syz147064 		    "interface '%s'", dlname);
26855895Syz147064 		return (-1);
26863448Sdh155122 	}
26873448Sdh155122 	return (0);
26883448Sdh155122 }
26893448Sdh155122 
26903448Sdh155122 /*
26913448Sdh155122  * Add the kernel access control information for the interface names.
26923448Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
26933448Sdh155122  * whatever we set up, and return an error.
26943448Sdh155122  */
26953448Sdh155122 static int
26963448Sdh155122 configure_exclusive_network_interfaces(zlog_t *zlogp)
26973448Sdh155122 {
26983448Sdh155122 	zone_dochandle_t handle;
26993448Sdh155122 	struct zone_nwiftab nwiftab;
27003448Sdh155122 	zoneid_t zoneid;
27013448Sdh155122 	char rootpath[MAXPATHLEN];
27023448Sdh155122 	char path[MAXPATHLEN];
27033448Sdh155122 	di_prof_t prof = NULL;
27043448Sdh155122 	boolean_t added = B_FALSE;
27053448Sdh155122 
27063448Sdh155122 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
27073448Sdh155122 		zerror(zlogp, B_TRUE, "unable to get zoneid");
27083448Sdh155122 		return (-1);
27093448Sdh155122 	}
27103448Sdh155122 
27113448Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
27123448Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
27133448Sdh155122 		return (-1);
27143448Sdh155122 	}
27153448Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
27163448Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
27173448Sdh155122 		zonecfg_fini_handle(handle);
27183448Sdh155122 		return (-1);
27193448Sdh155122 	}
27203448Sdh155122 
27213448Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
27223448Sdh155122 		zonecfg_fini_handle(handle);
27233448Sdh155122 		return (0);
27243448Sdh155122 	}
27253448Sdh155122 
27263448Sdh155122 	for (;;) {
27273448Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
27283448Sdh155122 			break;
27293448Sdh155122 
27303448Sdh155122 		if (prof == NULL) {
27313448Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
27323448Sdh155122 			    sizeof (rootpath)) != Z_OK) {
27333448Sdh155122 				(void) zonecfg_endnwifent(handle);
27343448Sdh155122 				zonecfg_fini_handle(handle);
27353448Sdh155122 				zerror(zlogp, B_TRUE,
27363448Sdh155122 				    "unable to determine dev root");
27373448Sdh155122 				return (-1);
27383448Sdh155122 			}
27393448Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
27403448Sdh155122 			    "/dev");
27413448Sdh155122 			if (di_prof_init(path, &prof) != 0) {
27423448Sdh155122 				(void) zonecfg_endnwifent(handle);
27433448Sdh155122 				zonecfg_fini_handle(handle);
27443448Sdh155122 				zerror(zlogp, B_TRUE,
27453448Sdh155122 				    "failed to initialize profile");
27463448Sdh155122 				return (-1);
27473448Sdh155122 			}
27483448Sdh155122 		}
27493448Sdh155122 
27503448Sdh155122 		/*
27515895Syz147064 		 * Create the /dev entry for backward compatibility.
27523448Sdh155122 		 * Only create the /dev entry if it's not in use.
27535895Syz147064 		 * Note that the zone still boots when the assigned
27545895Syz147064 		 * interface is inaccessible, used by others, etc.
27555895Syz147064 		 * Also, when vanity naming is used, some interface do
27565895Syz147064 		 * do not have corresponding /dev node names (for example,
27575895Syz147064 		 * vanity named aggregations).  The /dev entry is not
27585895Syz147064 		 * created in that case.  The /dev/net entry is always
27595895Syz147064 		 * accessible.
27603448Sdh155122 		 */
27613448Sdh155122 		if (add_datalink(zlogp, zoneid, nwiftab.zone_nwif_physical)
27623448Sdh155122 		    == 0) {
27635895Syz147064 			char		name[DLPI_LINKNAME_MAX];
27645895Syz147064 			datalink_id_t	linkid;
27655895Syz147064 
27665895Syz147064 			if (dladm_name2info(nwiftab.zone_nwif_physical,
27675895Syz147064 			    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
27685895Syz147064 			    dladm_linkid2legacyname(linkid, name,
27695895Syz147064 			    sizeof (name)) == DLADM_STATUS_OK) {
27705895Syz147064 				if (di_prof_add_dev(prof, name) != 0) {
27715895Syz147064 					(void) zonecfg_endnwifent(handle);
27725895Syz147064 					zonecfg_fini_handle(handle);
27735895Syz147064 					zerror(zlogp, B_TRUE,
27745895Syz147064 					    "failed to add network device");
27755895Syz147064 					return (-1);
27765895Syz147064 				}
27775895Syz147064 				added = B_TRUE;
27783448Sdh155122 			}
27793448Sdh155122 		}
27803448Sdh155122 	}
27813448Sdh155122 	(void) zonecfg_endnwifent(handle);
27823448Sdh155122 	zonecfg_fini_handle(handle);
27833448Sdh155122 
27843448Sdh155122 	if (prof != NULL && added) {
27853448Sdh155122 		if (di_prof_commit(prof) != 0) {
27863448Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
27873448Sdh155122 			return (-1);
27883448Sdh155122 		}
27893448Sdh155122 	}
27903448Sdh155122 	if (prof != NULL)
27913448Sdh155122 		di_prof_fini(prof);
27923448Sdh155122 
27933448Sdh155122 	return (0);
27943448Sdh155122 }
27953448Sdh155122 
27963448Sdh155122 /*
27973448Sdh155122  * Get the list of the data-links from kernel, and try to remove it
27983448Sdh155122  */
27993448Sdh155122 static int
28003448Sdh155122 unconfigure_exclusive_network_interfaces_run(zlog_t *zlogp, zoneid_t zoneid)
28013448Sdh155122 {
28023448Sdh155122 	char *dlnames, *ptr;
28033448Sdh155122 	int dlnum, dlnum_saved, i;
28043448Sdh155122 
28053448Sdh155122 	dlnum = 0;
28063448Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
28073448Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
28083448Sdh155122 		return (-1);
28093448Sdh155122 	}
28103448Sdh155122 again:
28113448Sdh155122 	/* this zone doesn't have any data-links */
28123448Sdh155122 	if (dlnum == 0)
28133448Sdh155122 		return (0);
28143448Sdh155122 
28153448Sdh155122 	dlnames = malloc(dlnum * LIFNAMSIZ);
28163448Sdh155122 	if (dlnames == NULL) {
28173448Sdh155122 		zerror(zlogp, B_TRUE, "memory allocation failed");
28183448Sdh155122 		return (-1);
28193448Sdh155122 	}
28203448Sdh155122 	dlnum_saved = dlnum;
28213448Sdh155122 
28223448Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, dlnames) != 0) {
28233448Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
28243448Sdh155122 		free(dlnames);
28253448Sdh155122 		return (-1);
28263448Sdh155122 	}
28273448Sdh155122 	if (dlnum_saved < dlnum) {
28283448Sdh155122 		/* list increased, try again */
28293448Sdh155122 		free(dlnames);
28303448Sdh155122 		goto again;
28313448Sdh155122 	}
28323448Sdh155122 	ptr = dlnames;
28333448Sdh155122 	for (i = 0; i < dlnum; i++) {
28343448Sdh155122 		/* Remove access control information */
28353448Sdh155122 		if (remove_datalink(zlogp, zoneid, ptr) != 0) {
28363448Sdh155122 			free(dlnames);
28373448Sdh155122 			return (-1);
28383448Sdh155122 		}
28393448Sdh155122 		ptr += LIFNAMSIZ;
28403448Sdh155122 	}
28413448Sdh155122 	free(dlnames);
28423448Sdh155122 	return (0);
28433448Sdh155122 }
28443448Sdh155122 
28453448Sdh155122 /*
28463448Sdh155122  * Get the list of the data-links from configuration, and try to remove it
28473448Sdh155122  */
28483448Sdh155122 static int
28493448Sdh155122 unconfigure_exclusive_network_interfaces_static(zlog_t *zlogp, zoneid_t zoneid)
28503448Sdh155122 {
28513448Sdh155122 	zone_dochandle_t handle;
28523448Sdh155122 	struct zone_nwiftab nwiftab;
28533448Sdh155122 
28543448Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
28553448Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
28563448Sdh155122 		return (-1);
28573448Sdh155122 	}
28583448Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
28593448Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
28603448Sdh155122 		zonecfg_fini_handle(handle);
28613448Sdh155122 		return (-1);
28623448Sdh155122 	}
28633448Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
28643448Sdh155122 		zonecfg_fini_handle(handle);
28653448Sdh155122 		return (0);
28663448Sdh155122 	}
28673448Sdh155122 	for (;;) {
28683448Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
28693448Sdh155122 			break;
28703448Sdh155122 		/* Remove access control information */
28713448Sdh155122 		if (remove_datalink(zlogp, zoneid, nwiftab.zone_nwif_physical)
28723448Sdh155122 		    != 0) {
28733448Sdh155122 			(void) zonecfg_endnwifent(handle);
28743448Sdh155122 			zonecfg_fini_handle(handle);
28753448Sdh155122 			return (-1);
28763448Sdh155122 		}
28773448Sdh155122 	}
28783448Sdh155122 	(void) zonecfg_endnwifent(handle);
28793448Sdh155122 	zonecfg_fini_handle(handle);
28803448Sdh155122 	return (0);
28813448Sdh155122 }
28823448Sdh155122 
28833448Sdh155122 /*
28843448Sdh155122  * Remove the access control information from the kernel for the exclusive
28853448Sdh155122  * network interfaces.
28863448Sdh155122  */
28873448Sdh155122 static int
28883448Sdh155122 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
28893448Sdh155122 {
28903448Sdh155122 	if (unconfigure_exclusive_network_interfaces_run(zlogp, zoneid) != 0) {
28913448Sdh155122 		return (unconfigure_exclusive_network_interfaces_static(zlogp,
28923448Sdh155122 		    zoneid));
28933448Sdh155122 	}
28943448Sdh155122 
28953448Sdh155122 	return (0);
28963448Sdh155122 }
28973448Sdh155122 
28980Sstevel@tonic-gate static int
28990Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
29000Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
29010Sstevel@tonic-gate {
29020Sstevel@tonic-gate 	int fd;
29030Sstevel@tonic-gate 	struct strioctl ioc;
29040Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
29050Sstevel@tonic-gate 	int error;
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate 	conn.ac_local = *local;
29080Sstevel@tonic-gate 	conn.ac_remote = *remote;
29090Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
29100Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
29110Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
29140Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
29150Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
29160Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
29190Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
29200Sstevel@tonic-gate 		return (-1);
29210Sstevel@tonic-gate 	}
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
29240Sstevel@tonic-gate 	(void) close(fd);
29250Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
29260Sstevel@tonic-gate 		return (0);
29270Sstevel@tonic-gate 	return (-1);
29280Sstevel@tonic-gate }
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate static int
29310Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
29320Sstevel@tonic-gate {
29330Sstevel@tonic-gate 	struct sockaddr_storage l, r;
29340Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
29350Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
29360Sstevel@tonic-gate 	int error;
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 	/*
29390Sstevel@tonic-gate 	 * Abort IPv4 connections.
29400Sstevel@tonic-gate 	 */
29410Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
29420Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
29430Sstevel@tonic-gate 	local->sin_family = AF_INET;
29440Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
29450Sstevel@tonic-gate 	local->sin_port = 0;
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
29480Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
29490Sstevel@tonic-gate 	remote->sin_family = AF_INET;
29500Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
29510Sstevel@tonic-gate 	remote->sin_port = 0;
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
29540Sstevel@tonic-gate 		return (error);
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate 	/*
29570Sstevel@tonic-gate 	 * Abort IPv6 connections.
29580Sstevel@tonic-gate 	 */
29590Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
29600Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
29610Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
29620Sstevel@tonic-gate 	local6->sin6_port = 0;
29630Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
29660Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
29670Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
29680Sstevel@tonic-gate 	remote6->sin6_port = 0;
29690Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
29720Sstevel@tonic-gate 		return (error);
29730Sstevel@tonic-gate 	return (0);
29740Sstevel@tonic-gate }
29750Sstevel@tonic-gate 
29760Sstevel@tonic-gate static int
29775829Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
29781645Scomay {
29791645Scomay 	int error = -1;
29801645Scomay 	zone_dochandle_t handle;
29811645Scomay 	char *privname = NULL;
29821645Scomay 
29831645Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
29841645Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
29851645Scomay 		return (-1);
29861645Scomay 	}
29871645Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
29881645Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
29891645Scomay 		zonecfg_fini_handle(handle);
29901645Scomay 		return (-1);
29911645Scomay 	}
29921645Scomay 
29935829Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
29943673Sdh155122 		zone_iptype_t	iptype;
29953673Sdh155122 		const char	*curr_iptype;
29963673Sdh155122 
29973673Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
29983673Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
29993673Sdh155122 			zonecfg_fini_handle(handle);
30003673Sdh155122 			return (-1);
30013673Sdh155122 		}
30023673Sdh155122 
30033673Sdh155122 		switch (iptype) {
30043673Sdh155122 		case ZS_SHARED:
30053673Sdh155122 			curr_iptype = "shared";
30063673Sdh155122 			break;
30073673Sdh155122 		case ZS_EXCLUSIVE:
30083673Sdh155122 			curr_iptype = "exclusive";
30093673Sdh155122 			break;
30103673Sdh155122 		}
30113673Sdh155122 
30123716Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
30133716Sgjelinek 			zonecfg_fini_handle(handle);
30142712Snn35248 			return (0);
30153716Sgjelinek 		}
30162712Snn35248 		zerror(zlogp, B_FALSE,
30172712Snn35248 		    "failed to determine the zone's default privilege set");
30182712Snn35248 		zonecfg_fini_handle(handle);
30192712Snn35248 		return (-1);
30202712Snn35248 	}
30212712Snn35248 
30221645Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
30231645Scomay 	case Z_OK:
30241645Scomay 		error = 0;
30251645Scomay 		break;
30261645Scomay 	case Z_PRIV_PROHIBITED:
30271645Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
30281645Scomay 		    "within the zone's privilege set", privname);
30291645Scomay 		break;
30301645Scomay 	case Z_PRIV_REQUIRED:
30311645Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
30321645Scomay 		    "from the zone's privilege set", privname);
30331645Scomay 		break;
30341645Scomay 	case Z_PRIV_UNKNOWN:
30351645Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
30361645Scomay 		    "in the zone's privilege set", privname);
30371645Scomay 		break;
30381645Scomay 	default:
30391645Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
30401645Scomay 		    "privilege set");
30411645Scomay 		break;
30421645Scomay 	}
30431645Scomay 
30441645Scomay 	free(privname);
30451645Scomay 	zonecfg_fini_handle(handle);
30461645Scomay 	return (error);
30471645Scomay }
30481645Scomay 
30491645Scomay static int
30500Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
30510Sstevel@tonic-gate {
30520Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
30530Sstevel@tonic-gate 	char *nvl_packed = NULL;
30540Sstevel@tonic-gate 	size_t nvl_size = 0;
30550Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
30560Sstevel@tonic-gate 	int rctlcount = 0;
30570Sstevel@tonic-gate 	int error = -1;
30580Sstevel@tonic-gate 	zone_dochandle_t handle;
30590Sstevel@tonic-gate 	struct zone_rctltab rctltab;
30600Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	*bufp = NULL;
30630Sstevel@tonic-gate 	*bufsizep = 0;
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
30660Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
30670Sstevel@tonic-gate 		return (-1);
30680Sstevel@tonic-gate 	}
30690Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
30700Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
30710Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
30720Sstevel@tonic-gate 		return (-1);
30730Sstevel@tonic-gate 	}
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
30760Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
30770Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
30780Sstevel@tonic-gate 		goto out;
30790Sstevel@tonic-gate 	}
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
30820Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
30830Sstevel@tonic-gate 		goto out;
30840Sstevel@tonic-gate 	}
30850Sstevel@tonic-gate 
30860Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
30870Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
30880Sstevel@tonic-gate 		goto out;
30890Sstevel@tonic-gate 	}
30900Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
30910Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
30920Sstevel@tonic-gate 		uint_t i, count;
30930Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
30960Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
30970Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30980Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
30990Sstevel@tonic-gate 			continue;
31000Sstevel@tonic-gate 		}
31010Sstevel@tonic-gate 		count = 0;
31020Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
31030Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
31040Sstevel@tonic-gate 			count++;
31050Sstevel@tonic-gate 		}
31060Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
31070Sstevel@tonic-gate 			continue;	/* Nothing to free */
31080Sstevel@tonic-gate 		}
31090Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
31100Sstevel@tonic-gate 			goto out;
31110Sstevel@tonic-gate 		i = 0;
31120Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
31130Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
31140Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
31150Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
31160Sstevel@tonic-gate 				    "nvlist_alloc");
31170Sstevel@tonic-gate 				goto out;
31180Sstevel@tonic-gate 			}
31190Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
31200Sstevel@tonic-gate 			    != Z_OK) {
31210Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
31220Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
31230Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
31240Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
31250Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
31260Sstevel@tonic-gate 				goto out;
31270Sstevel@tonic-gate 			}
31280Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
31290Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
31300Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
31310Sstevel@tonic-gate 				    "valid value for rctl '%s'",
31320Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
31330Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
31340Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
31350Sstevel@tonic-gate 				    name);
31360Sstevel@tonic-gate 				goto out;
31370Sstevel@tonic-gate 			}
31380Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
31391645Scomay 			    rctlblk_get_privilege(rctlblk)) != 0) {
31400Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
31410Sstevel@tonic-gate 				    "nvlist_add_uint64");
31420Sstevel@tonic-gate 				goto out;
31430Sstevel@tonic-gate 			}
31440Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
31451645Scomay 			    rctlblk_get_value(rctlblk)) != 0) {
31460Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
31470Sstevel@tonic-gate 				    "nvlist_add_uint64");
31480Sstevel@tonic-gate 				goto out;
31490Sstevel@tonic-gate 			}
31500Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
31510Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
31520Sstevel@tonic-gate 			    != 0) {
31530Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
31540Sstevel@tonic-gate 				    "nvlist_add_uint64");
31550Sstevel@tonic-gate 				goto out;
31560Sstevel@tonic-gate 			}
31570Sstevel@tonic-gate 		}
31580Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
31590Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
31600Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
31610Sstevel@tonic-gate 		    != 0) {
31620Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
31630Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
31640Sstevel@tonic-gate 			goto out;
31650Sstevel@tonic-gate 		}
31660Sstevel@tonic-gate 		for (i = 0; i < count; i++)
31670Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
31680Sstevel@tonic-gate 		free(nvlv);
31690Sstevel@tonic-gate 		nvlv = NULL;
31700Sstevel@tonic-gate 		rctlcount++;
31710Sstevel@tonic-gate 	}
31720Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
31730Sstevel@tonic-gate 
31740Sstevel@tonic-gate 	if (rctlcount == 0) {
31750Sstevel@tonic-gate 		error = 0;
31760Sstevel@tonic-gate 		goto out;
31770Sstevel@tonic-gate 	}
31780Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
31790Sstevel@tonic-gate 	    != 0) {
31800Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
31810Sstevel@tonic-gate 		goto out;
31820Sstevel@tonic-gate 	}
31830Sstevel@tonic-gate 
31840Sstevel@tonic-gate 	error = 0;
31850Sstevel@tonic-gate 	*bufp = nvl_packed;
31860Sstevel@tonic-gate 	*bufsizep = nvl_size;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate out:
31890Sstevel@tonic-gate 	free(rctlblk);
31900Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
31910Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
31920Sstevel@tonic-gate 		free(nvl_packed);
31930Sstevel@tonic-gate 	if (nvl != NULL)
31940Sstevel@tonic-gate 		nvlist_free(nvl);
31950Sstevel@tonic-gate 	if (nvlv != NULL)
31960Sstevel@tonic-gate 		free(nvlv);
31970Sstevel@tonic-gate 	if (handle != NULL)
31980Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
31990Sstevel@tonic-gate 	return (error);
32000Sstevel@tonic-gate }
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate static int
3203789Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3204789Sahrens {
3205789Sahrens 	zone_dochandle_t handle;
3206789Sahrens 	struct zone_dstab dstab;
3207789Sahrens 	size_t total, offset, len;
3208789Sahrens 	int error = -1;
32095185Sgjelinek 	char *str = NULL;
3210789Sahrens 
3211789Sahrens 	*bufp = NULL;
3212789Sahrens 	*bufsizep = 0;
3213789Sahrens 
3214789Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3215789Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3216789Sahrens 		return (-1);
3217789Sahrens 	}
3218789Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3219789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3220789Sahrens 		zonecfg_fini_handle(handle);
3221789Sahrens 		return (-1);
3222789Sahrens 	}
3223789Sahrens 
3224789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3225789Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3226789Sahrens 		goto out;
3227789Sahrens 	}
3228789Sahrens 
3229789Sahrens 	total = 0;
3230789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3231789Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3232789Sahrens 	(void) zonecfg_enddsent(handle);
3233789Sahrens 
3234789Sahrens 	if (total == 0) {
3235789Sahrens 		error = 0;
3236789Sahrens 		goto out;
3237789Sahrens 	}
3238789Sahrens 
3239789Sahrens 	if ((str = malloc(total)) == NULL) {
3240789Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3241789Sahrens 		goto out;
3242789Sahrens 	}
3243789Sahrens 
3244789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3245789Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3246789Sahrens 		goto out;
3247789Sahrens 	}
3248789Sahrens 	offset = 0;
3249789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3250789Sahrens 		len = strlen(dstab.zone_dataset_name);
3251789Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
32525185Sgjelinek 		    total - offset);
3253789Sahrens 		offset += len;
32545185Sgjelinek 		if (offset < total - 1)
3255789Sahrens 			str[offset++] = ',';
3256789Sahrens 	}
3257789Sahrens 	(void) zonecfg_enddsent(handle);
3258789Sahrens 
3259789Sahrens 	error = 0;
3260789Sahrens 	*bufp = str;
3261789Sahrens 	*bufsizep = total;
3262789Sahrens 
3263789Sahrens out:
3264789Sahrens 	if (error != 0 && str != NULL)
3265789Sahrens 		free(str);
3266789Sahrens 	if (handle != NULL)
3267789Sahrens 		zonecfg_fini_handle(handle);
3268789Sahrens 
3269789Sahrens 	return (error);
3270789Sahrens }
3271789Sahrens 
3272789Sahrens static int
3273789Sahrens validate_datasets(zlog_t *zlogp)
3274789Sahrens {
3275789Sahrens 	zone_dochandle_t handle;
3276789Sahrens 	struct zone_dstab dstab;
3277789Sahrens 	zfs_handle_t *zhp;
32782082Seschrock 	libzfs_handle_t *hdl;
3279789Sahrens 
3280789Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3281789Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3282789Sahrens 		return (-1);
3283789Sahrens 	}
3284789Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3285789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3286789Sahrens 		zonecfg_fini_handle(handle);
3287789Sahrens 		return (-1);
3288789Sahrens 	}
3289789Sahrens 
3290789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3291789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3292789Sahrens 		zonecfg_fini_handle(handle);
3293789Sahrens 		return (-1);
3294789Sahrens 	}
3295789Sahrens 
32962082Seschrock 	if ((hdl = libzfs_init()) == NULL) {
32972082Seschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
32982082Seschrock 		zonecfg_fini_handle(handle);
32992082Seschrock 		return (-1);
33002082Seschrock 	}
3301789Sahrens 
3302789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3303789Sahrens 
33042082Seschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3305789Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3306789Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3307789Sahrens 			    dstab.zone_dataset_name);
3308789Sahrens 			zonecfg_fini_handle(handle);
33092082Seschrock 			libzfs_fini(hdl);
3310789Sahrens 			return (-1);
3311789Sahrens 		}
3312789Sahrens 
3313789Sahrens 		/*
3314789Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3315789Sahrens 		 * first because we'll get EPERM if it is already set.
3316789Sahrens 		 */
3317789Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
33182676Seschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
33192676Seschrock 		    "on") != 0) {
3320789Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3321789Sahrens 			    "property for ZFS dataset '%s'\n",
3322789Sahrens 			    dstab.zone_dataset_name);
3323789Sahrens 			zonecfg_fini_handle(handle);
3324789Sahrens 			zfs_close(zhp);
33252082Seschrock 			libzfs_fini(hdl);
3326789Sahrens 			return (-1);
3327789Sahrens 		}
3328789Sahrens 
3329789Sahrens 		zfs_close(zhp);
3330789Sahrens 	}
3331789Sahrens 	(void) zonecfg_enddsent(handle);
3332789Sahrens 
3333789Sahrens 	zonecfg_fini_handle(handle);
33342082Seschrock 	libzfs_fini(hdl);
3335789Sahrens 
3336789Sahrens 	return (0);
3337789Sahrens }
3338789Sahrens 
33391676Sjpk /*
33401676Sjpk  * Mount lower level home directories into/from current zone
33411676Sjpk  * Share exported directories specified in dfstab for zone
33421676Sjpk  */
33431676Sjpk static int
33441676Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
33451676Sjpk {
33461676Sjpk 	zoneid_t *zids = NULL;
33471676Sjpk 	priv_set_t *zid_privs;
33481676Sjpk 	const priv_impl_info_t *ip = NULL;
33491676Sjpk 	uint_t nzents_saved;
33501676Sjpk 	uint_t nzents;
33511676Sjpk 	int i;
33521676Sjpk 	char readonly[] = "ro";
33531676Sjpk 	struct zone_fstab lower_fstab;
33541676Sjpk 	char *argv[4];
33551676Sjpk 
33561676Sjpk 	if (!is_system_labeled())
33571676Sjpk 		return (0);
33581676Sjpk 
33591676Sjpk 	if (zid_label == NULL) {
33601676Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
33611676Sjpk 		if (zid_label == NULL)
33621676Sjpk 			return (-1);
33631676Sjpk 	}
33641676Sjpk 
33651676Sjpk 	/* Make sure our zone has an /export/home dir */
33661676Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
33673813Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
33681676Sjpk 
33691676Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
33701676Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
33711676Sjpk 	    sizeof (lower_fstab.zone_fs_type));
33721676Sjpk 	lower_fstab.zone_fs_options = NULL;
33731676Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
33741676Sjpk 
33751676Sjpk 	/*
33761676Sjpk 	 * Get the list of zones from the kernel
33771676Sjpk 	 */
33781676Sjpk 	if (zone_list(NULL, &nzents) != 0) {
33791676Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
33801676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
33811676Sjpk 		return (-1);
33821676Sjpk 	}
33831676Sjpk again:
33841676Sjpk 	if (nzents == 0) {
33851676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
33861676Sjpk 		return (-1);
33871676Sjpk 	}
33881676Sjpk 
33891676Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
33901676Sjpk 	if (zids == NULL) {
33912267Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
33921676Sjpk 		return (-1);
33931676Sjpk 	}
33941676Sjpk 	nzents_saved = nzents;
33951676Sjpk 
33961676Sjpk 	if (zone_list(zids, &nzents) != 0) {
33971676Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
33981676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
33991676Sjpk 		free(zids);
34001676Sjpk 		return (-1);
34011676Sjpk 	}
34021676Sjpk 	if (nzents != nzents_saved) {
34031676Sjpk 		/* list changed, try again */
34041676Sjpk 		free(zids);
34051676Sjpk 		goto again;
34061676Sjpk 	}
34071676Sjpk 
34081676Sjpk 	ip = getprivimplinfo();
34091676Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
34101676Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
34111676Sjpk 		zonecfg_free_fs_option_list(
34121676Sjpk 		    lower_fstab.zone_fs_options);
34131676Sjpk 		free(zids);
34141676Sjpk 		return (-1);
34151676Sjpk 	}
34161676Sjpk 
34171676Sjpk 	for (i = 0; i < nzents; i++) {
34181676Sjpk 		char zid_name[ZONENAME_MAX];
34191676Sjpk 		zone_state_t zid_state;
34201676Sjpk 		char zid_rpath[MAXPATHLEN];
34211676Sjpk 		struct stat stat_buf;
34221676Sjpk 
34231676Sjpk 		if (zids[i] == GLOBAL_ZONEID)
34241676Sjpk 			continue;
34251676Sjpk 
34261676Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
34271676Sjpk 			continue;
34281676Sjpk 
34291676Sjpk 		/*
34301676Sjpk 		 * Do special setup for the zone we are booting
34311676Sjpk 		 */
34321676Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
34331676Sjpk 			struct zone_fstab autofs_fstab;
34341676Sjpk 			char map_path[MAXPATHLEN];
34351676Sjpk 			int fd;
34361676Sjpk 
34371676Sjpk 			/*
34381676Sjpk 			 * Create auto_home_<zone> map for this zone
34392712Snn35248 			 * in the global zone. The non-global zone entry
34401676Sjpk 			 * will be created by automount when the zone
34411676Sjpk 			 * is booted.
34421676Sjpk 			 */
34431676Sjpk 
34441676Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
34451676Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
34461676Sjpk 
34471676Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
34481676Sjpk 			    "/zone/%s/home", zid_name);
34491676Sjpk 
34501676Sjpk 			(void) snprintf(map_path, sizeof (map_path),
34511676Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
34521676Sjpk 			/*
34531676Sjpk 			 * If the map file doesn't exist create a template
34541676Sjpk 			 */
34551676Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
34561676Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
34571676Sjpk 				int len;
34581676Sjpk 				char map_rec[MAXPATHLEN];
34591676Sjpk 
34601676Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
34611676Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
34621676Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
34631676Sjpk 				(void) write(fd, map_rec, len);
34641676Sjpk 				(void) close(fd);
34651676Sjpk 			}
34661676Sjpk 
34671676Sjpk 			/*
34681676Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
34691676Sjpk 			 * If it's already of type autofs, then
34701676Sjpk 			 * don't mount it again.
34711676Sjpk 			 */
34721676Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
34731676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
34741676Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
34751676Sjpk 
34761676Sjpk 				(void) make_one_dir(zlogp, "",
34773813Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
34783813Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
34791676Sjpk 
34801676Sjpk 				/*
34811676Sjpk 				 * Mount will fail if automounter has already
34821676Sjpk 				 * processed the auto_home_<zonename> map
34831676Sjpk 				 */
34841676Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
34851676Sjpk 				    autofs_fstab.zone_fs_special,
34861676Sjpk 				    autofs_fstab.zone_fs_dir);
34871676Sjpk 			}
34881676Sjpk 			continue;
34891676Sjpk 		}
34901676Sjpk 
34911676Sjpk 
34921676Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
34931769Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
34941769Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
34951676Sjpk 			/* Skip over zones without mounted filesystems */
34961676Sjpk 			continue;
34971676Sjpk 
34981676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
34991676Sjpk 		    sizeof (m_label_t)) < 0)
35001676Sjpk 			/* Skip over zones with unspecified label */
35011676Sjpk 			continue;
35021676Sjpk 
35031676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
35041676Sjpk 		    sizeof (zid_rpath)) == -1)
35051676Sjpk 			/* Skip over zones with bad path */
35061676Sjpk 			continue;
35071676Sjpk 
35081676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
35091676Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
35101676Sjpk 			/* Skip over zones with bad privs */
35111676Sjpk 			continue;
35121676Sjpk 
35131676Sjpk 		/*
35141676Sjpk 		 * Reading down is valid according to our label model
35151676Sjpk 		 * but some customers want to disable it because it
35161676Sjpk 		 * allows execute down and other possible attacks.
35171676Sjpk 		 * Therefore, we restrict this feature to zones that
35181676Sjpk 		 * have the NET_MAC_AWARE privilege which is required
35191676Sjpk 		 * for NFS read-down semantics.
35201676Sjpk 		 */
35211676Sjpk 		if ((bldominates(zlabel, zid_label)) &&
35221676Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
35231676Sjpk 			/*
35241676Sjpk 			 * Our zone dominates this one.
35251676Sjpk 			 * Create a lofs mount from lower zone's /export/home
35261676Sjpk 			 */
35271676Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
35281676Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
35291676Sjpk 
35301676Sjpk 			/*
35311676Sjpk 			 * If the target is already an LOFS mount
35321676Sjpk 			 * then don't do it again.
35331676Sjpk 			 */
35341676Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
35351676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
35361676Sjpk 
35371676Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
35381676Sjpk 				    MAXPATHLEN, "%s/export",
35391676Sjpk 				    zid_rpath) > MAXPATHLEN)
35401676Sjpk 					continue;
35411676Sjpk 
35421676Sjpk 				/*
35431676Sjpk 				 * Make sure the lower-level home exists
35441676Sjpk 				 */
35451676Sjpk 				if (make_one_dir(zlogp,
35463813Sdp 				    lower_fstab.zone_fs_special, "/home",
35473813Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
35483813Sdp 				    DEFAULT_DIR_GROUP) != 0)
35491676Sjpk 					continue;
35501676Sjpk 
35511676Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
35521676Sjpk 				    "/home", MAXPATHLEN);
35531676Sjpk 
35541676Sjpk 				/*
35551676Sjpk 				 * Mount can fail because the lower-level
35561676Sjpk 				 * zone may have already done a mount up.
35571676Sjpk 				 */
35581676Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
35591676Sjpk 			}
35601676Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
35611676Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
35621676Sjpk 			/*
35631676Sjpk 			 * This zone dominates our zone.
35641676Sjpk 			 * Create a lofs mount from our zone's /export/home
35651676Sjpk 			 */
35661676Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
35671676Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
35681676Sjpk 			    zone_name) > MAXPATHLEN)
35691676Sjpk 				continue;
35701676Sjpk 
35711676Sjpk 			/*
35721676Sjpk 			 * If the target is already an LOFS mount
35731676Sjpk 			 * then don't do it again.
35741676Sjpk 			 */
35751676Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
35761676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
35771676Sjpk 
35781676Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
35791676Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
35801676Sjpk 
35811676Sjpk 				/*
35821676Sjpk 				 * Mount can fail because the higher-level
35831676Sjpk 				 * zone may have already done a mount down.
35841676Sjpk 				 */
35851676Sjpk 				(void) mount_one(zlogp, &lower_fstab, "");
35861676Sjpk 			}
35871676Sjpk 		}
35881676Sjpk 	}
35891676Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
35901676Sjpk 	priv_freeset(zid_privs);
35911676Sjpk 	free(zids);
35921676Sjpk 
35931676Sjpk 	/*
35941676Sjpk 	 * Now share any exported directories from this zone.
35951676Sjpk 	 * Each zone can have its own dfstab.
35961676Sjpk 	 */
35971676Sjpk 
35981676Sjpk 	argv[0] = "zoneshare";
35991676Sjpk 	argv[1] = "-z";
36001676Sjpk 	argv[2] = zone_name;
36011676Sjpk 	argv[3] = NULL;
36021676Sjpk 
36031676Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
36041676Sjpk 	/* Don't check for errors since they don't affect the zone */
36051676Sjpk 
36061676Sjpk 	return (0);
36071676Sjpk }
36081676Sjpk 
36091676Sjpk /*
36101676Sjpk  * Unmount lofs mounts from higher level zones
36111676Sjpk  * Unshare nfs exported directories
36121676Sjpk  */
36131676Sjpk static void
36141676Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
36151676Sjpk {
36161676Sjpk 	zoneid_t *zids = NULL;
36171676Sjpk 	uint_t nzents_saved;
36181676Sjpk 	uint_t nzents;
36191676Sjpk 	int i;
36201676Sjpk 	char *argv[4];
36211676Sjpk 	char path[MAXPATHLEN];
36221676Sjpk 
36231676Sjpk 	if (!is_system_labeled())
36241676Sjpk 		return;
36251676Sjpk 
36261676Sjpk 	/*
36271676Sjpk 	 * Get the list of zones from the kernel
36281676Sjpk 	 */
36291676Sjpk 	if (zone_list(NULL, &nzents) != 0) {
36301676Sjpk 		return;
36311676Sjpk 	}
36321676Sjpk 
36331676Sjpk 	if (zid_label == NULL) {
36341676Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
36351676Sjpk 		if (zid_label == NULL)
36361676Sjpk 			return;
36371676Sjpk 	}
36381676Sjpk 
36391676Sjpk again:
36401676Sjpk 	if (nzents == 0)
36411676Sjpk 		return;
36421676Sjpk 
36431676Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
36441676Sjpk 	if (zids == NULL) {
36452267Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
36461676Sjpk 		return;
36471676Sjpk 	}
36481676Sjpk 	nzents_saved = nzents;
36491676Sjpk 
36501676Sjpk 	if (zone_list(zids, &nzents) != 0) {
36511676Sjpk 		free(zids);
36521676Sjpk 		return;
36531676Sjpk 	}
36541676Sjpk 	if (nzents != nzents_saved) {
36551676Sjpk 		/* list changed, try again */
36561676Sjpk 		free(zids);
36571676Sjpk 		goto again;
36581676Sjpk 	}
36591676Sjpk 
36601676Sjpk 	for (i = 0; i < nzents; i++) {
36611676Sjpk 		char zid_name[ZONENAME_MAX];
36621676Sjpk 		zone_state_t zid_state;
36631676Sjpk 		char zid_rpath[MAXPATHLEN];
36641676Sjpk 
36651676Sjpk 		if (zids[i] == GLOBAL_ZONEID)
36661676Sjpk 			continue;
36671676Sjpk 
36681676Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
36691676Sjpk 			continue;
36701676Sjpk 
36711676Sjpk 		/*
36721676Sjpk 		 * Skip the zone we are halting
36731676Sjpk 		 */
36741676Sjpk 		if (strcmp(zid_name, zone_name) == 0)
36751676Sjpk 			continue;
36761676Sjpk 
36771676Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
36781676Sjpk 		    sizeof (zid_state)) < 0) ||
36791676Sjpk 		    (zid_state < ZONE_IS_READY))
36801676Sjpk 			/* Skip over zones without mounted filesystems */
36811676Sjpk 			continue;
36821676Sjpk 
36831676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
36841676Sjpk 		    sizeof (m_label_t)) < 0)
36851676Sjpk 			/* Skip over zones with unspecified label */
36861676Sjpk 			continue;
36871676Sjpk 
36881676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
36891676Sjpk 		    sizeof (zid_rpath)) == -1)
36901676Sjpk 			/* Skip over zones with bad path */
36911676Sjpk 			continue;
36921676Sjpk 
36931676Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
36941676Sjpk 			/*
36951676Sjpk 			 * This zone dominates our zone.
36961676Sjpk 			 * Unmount the lofs mount of our zone's /export/home
36971676Sjpk 			 */
36981676Sjpk 
36991676Sjpk 			if (snprintf(path, MAXPATHLEN,
37001676Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
37011676Sjpk 			    zone_name) > MAXPATHLEN)
37021676Sjpk 				continue;
37031676Sjpk 
37041676Sjpk 			/* Skip over mount failures */
37051676Sjpk 			(void) umount(path);
37061676Sjpk 		}
37071676Sjpk 	}
37081676Sjpk 	free(zids);
37091676Sjpk 
37101676Sjpk 	/*
37111676Sjpk 	 * Unmount global zone autofs trigger for this zone
37121676Sjpk 	 */
37131676Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
37141676Sjpk 	/* Skip over mount failures */
37151676Sjpk 	(void) umount(path);
37161676Sjpk 
37171676Sjpk 	/*
37181676Sjpk 	 * Next unshare any exported directories from this zone.
37191676Sjpk 	 */
37201676Sjpk 
37211676Sjpk 	argv[0] = "zoneunshare";
37221676Sjpk 	argv[1] = "-z";
37231676Sjpk 	argv[2] = zone_name;
37241676Sjpk 	argv[3] = NULL;
37251676Sjpk 
37261676Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
37271676Sjpk 	/* Don't check for errors since they don't affect the zone */
37281676Sjpk 
37291676Sjpk 	/*
37301676Sjpk 	 * Finally, deallocate any devices in the zone.
37311676Sjpk 	 */
37321676Sjpk 
37331676Sjpk 	argv[0] = "deallocate";
37341676Sjpk 	argv[1] = "-Isz";
37351676Sjpk 	argv[2] = zone_name;
37361676Sjpk 	argv[3] = NULL;
37371676Sjpk 
37381676Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
37391676Sjpk 	/* Don't check for errors since they don't affect the zone */
37401676Sjpk }
37411676Sjpk 
37421676Sjpk /*
37431676Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
37441676Sjpk  * this zone.
37451676Sjpk  */
37461676Sjpk static tsol_zcent_t *
37471676Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
37481676Sjpk {
37491676Sjpk 	FILE *fp;
37501676Sjpk 	tsol_zcent_t *zcent = NULL;
37511676Sjpk 	char line[MAXTNZLEN];
37521676Sjpk 
37531676Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
37541676Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
37551676Sjpk 		return (NULL);
37561676Sjpk 	}
37571676Sjpk 
37581676Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
37591676Sjpk 		/*
37601676Sjpk 		 * Check for malformed database
37611676Sjpk 		 */
37621676Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
37631676Sjpk 			break;
37641676Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
37651676Sjpk 			continue;
37661676Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
37671676Sjpk 			break;
37681676Sjpk 		tsol_freezcent(zcent);
37691676Sjpk 		zcent = NULL;
37701676Sjpk 	}
37711676Sjpk 	(void) fclose(fp);
37721676Sjpk 
37731676Sjpk 	if (zcent == NULL) {
37741676Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
37751676Sjpk 		    "See tnzonecfg(4)");
37761676Sjpk 	} else {
37771676Sjpk 		if (zlabel == NULL)
37781676Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
37791676Sjpk 		/*
37801676Sjpk 		 * Save this zone's privileges for later read-down processing
37811676Sjpk 		 */
37821676Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
37831676Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
37841676Sjpk 			return (NULL);
37851676Sjpk 		} else {
37861676Sjpk 			priv_copyset(privs, zprivs);
37871676Sjpk 		}
37881676Sjpk 	}
37891676Sjpk 	return (zcent);
37901676Sjpk }
37911676Sjpk 
37921676Sjpk /*
37931676Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
37941676Sjpk  */
37951676Sjpk static void
37961676Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
37971676Sjpk {
37981676Sjpk 	tsol_mlp_t *mlp;
37991676Sjpk 	tsol_mlpent_t tsme;
38001676Sjpk 
38011676Sjpk 	if (!is_system_labeled())
38021676Sjpk 		return;
38031676Sjpk 
38041676Sjpk 	tsme.tsme_zoneid = zoneid;
38051676Sjpk 	tsme.tsme_flags = 0;
38061676Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
38071676Sjpk 		tsme.tsme_mlp = *mlp;
38081676Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
38091676Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
38101676Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
38111676Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
38121676Sjpk 		}
38131676Sjpk 	}
38141676Sjpk 
38151676Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
38161676Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
38171676Sjpk 		tsme.tsme_mlp = *mlp;
38181676Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
38191676Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
38201676Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
38211676Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
38221676Sjpk 		}
38231676Sjpk 	}
38241676Sjpk }
38251676Sjpk 
38261676Sjpk static void
38271676Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
38281676Sjpk {
38291676Sjpk 	tsol_mlpent_t tsme;
38301676Sjpk 
38311676Sjpk 	if (!is_system_labeled())
38321676Sjpk 		return;
38331676Sjpk 
38341676Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
38351676Sjpk 	tsme.tsme_zoneid = zoneid;
38361676Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
38371676Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
38381676Sjpk }
38391676Sjpk 
38400Sstevel@tonic-gate int
38410Sstevel@tonic-gate prtmount(const char *fs, void *x) {
38420Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
38430Sstevel@tonic-gate 	return (0);
38440Sstevel@tonic-gate }
38450Sstevel@tonic-gate 
3846766Scarlsonj /*
3847766Scarlsonj  * Look for zones running on the main system that are using this root (or any
3848766Scarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3849766Scarlsonj  * is found or if we can't tell.
3850766Scarlsonj  */
3851766Scarlsonj static boolean_t
3852766Scarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
38530Sstevel@tonic-gate {
3854766Scarlsonj 	zoneid_t *zids = NULL;
3855766Scarlsonj 	uint_t nzids = 0;
3856766Scarlsonj 	boolean_t retv;
3857766Scarlsonj 	int rlen, zlen;
3858766Scarlsonj 	char zroot[MAXPATHLEN];
3859766Scarlsonj 	char zonename[ZONENAME_MAX];
3860766Scarlsonj 
3861766Scarlsonj 	for (;;) {
3862766Scarlsonj 		nzids += 10;
3863766Scarlsonj 		zids = malloc(nzids * sizeof (*zids));
3864766Scarlsonj 		if (zids == NULL) {
38652267Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3866766Scarlsonj 			return (B_TRUE);
3867766Scarlsonj 		}
3868766Scarlsonj 		if (zone_list(zids, &nzids) == 0)
3869766Scarlsonj 			break;
3870766Scarlsonj 		free(zids);
3871766Scarlsonj 	}
3872766Scarlsonj 	retv = B_FALSE;
3873766Scarlsonj 	rlen = strlen(rootpath);
3874766Scarlsonj 	while (nzids > 0) {
3875766Scarlsonj 		/*
3876766Scarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3877766Scarlsonj 		 * while we were busy.
3878766Scarlsonj 		 */
3879766Scarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3880766Scarlsonj 		    sizeof (zroot)) == -1)
3881766Scarlsonj 			continue;
3882766Scarlsonj 		zlen = strlen(zroot);
3883766Scarlsonj 		if (zlen > rlen)
3884766Scarlsonj 			zlen = rlen;
3885766Scarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3886766Scarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3887766Scarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3888766Scarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3889766Scarlsonj 			    sizeof (zonename)) == -1)
3890766Scarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3891766Scarlsonj 				    "id %d", (int)zids[nzids]);
3892766Scarlsonj 			zerror(zlogp, B_FALSE,
3893766Scarlsonj 			    "zone root %s already in use by zone %s",
3894766Scarlsonj 			    rootpath, zonename);
3895766Scarlsonj 			retv = B_TRUE;
3896766Scarlsonj 			break;
3897766Scarlsonj 		}
3898766Scarlsonj 	}
3899766Scarlsonj 	free(zids);
3900766Scarlsonj 	return (retv);
3901766Scarlsonj }
3902766Scarlsonj 
3903766Scarlsonj /*
3904766Scarlsonj  * Search for loopback mounts that use this same source node (same device and
3905766Scarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3906766Scarlsonj  */
3907766Scarlsonj static boolean_t
3908766Scarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3909766Scarlsonj {
3910766Scarlsonj 	struct stat64 rst, zst;
3911766Scarlsonj 	struct mnttab *mnp;
3912766Scarlsonj 
3913766Scarlsonj 	if (stat64(rootpath, &rst) == -1) {
3914766Scarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3915766Scarlsonj 		return (B_TRUE);
3916766Scarlsonj 	}
3917766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3918766Scarlsonj 		return (B_TRUE);
3919766Scarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3920766Scarlsonj 		if (mnp->mnt_fstype == NULL ||
3921766Scarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3922766Scarlsonj 			continue;
3923766Scarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3924766Scarlsonj 		if (mnp->mnt_special != NULL &&
3925766Scarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3926766Scarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3927766Scarlsonj 			zerror(zlogp, B_FALSE,
3928766Scarlsonj 			    "zone root %s is reachable through %s",
3929766Scarlsonj 			    rootpath, mnp->mnt_mountp);
3930766Scarlsonj 			return (B_TRUE);
3931766Scarlsonj 		}
3932766Scarlsonj 	}
3933766Scarlsonj 	return (B_FALSE);
3934766Scarlsonj }
3935766Scarlsonj 
39363247Sgjelinek /*
39373247Sgjelinek  * Set memory cap and pool info for the zone's resource management
39383247Sgjelinek  * configuration.
39393247Sgjelinek  */
39403247Sgjelinek static int
39413247Sgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
39423247Sgjelinek {
39433247Sgjelinek 	int res;
39443247Sgjelinek 	uint64_t tmp;
39453247Sgjelinek 	struct zone_mcaptab mcap;
39463247Sgjelinek 	char sched[MAXNAMELEN];
39473247Sgjelinek 	zone_dochandle_t handle = NULL;
39483247Sgjelinek 	char pool_err[128];
39493247Sgjelinek 
39503247Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
39513247Sgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
39523247Sgjelinek 		return (Z_BAD_HANDLE);
39533247Sgjelinek 	}
39543247Sgjelinek 
39553247Sgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
39563247Sgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
39573247Sgjelinek 		zonecfg_fini_handle(handle);
39583247Sgjelinek 		return (res);
39593247Sgjelinek 	}
39603247Sgjelinek 
39613247Sgjelinek 	/*
39623247Sgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
39633247Sgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
39643247Sgjelinek 	 */
39653247Sgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
39663247Sgjelinek 		uint64_t num;
39673247Sgjelinek 		char smf_err[128];
39683247Sgjelinek 
39693247Sgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
39703247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
39713247Sgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
39723247Sgjelinek 			zonecfg_fini_handle(handle);
39733247Sgjelinek 			return (Z_INVAL);
39743247Sgjelinek 		}
39753247Sgjelinek 
39763247Sgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
39773247Sgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
39783247Sgjelinek 			    "failed: %s", smf_err);
39793247Sgjelinek 			zonecfg_fini_handle(handle);
39803247Sgjelinek 			return (Z_INVAL);
39813247Sgjelinek 		}
39823247Sgjelinek 	}
39833247Sgjelinek 
39843247Sgjelinek 	/* Get the scheduling class set in the zone configuration. */
39853247Sgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
39863247Sgjelinek 	    strlen(sched) > 0) {
39873247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
39883247Sgjelinek 		    strlen(sched)) == -1)
39893247Sgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
39903247Sgjelinek 			    "default scheduling class");
39913247Sgjelinek 
39923247Sgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
39933247Sgjelinek 	    == Z_OK) {
39943247Sgjelinek 		/*
39953247Sgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
39963247Sgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
39973247Sgjelinek 		 * zone.  Check what scheduling class the zone would be running
39983247Sgjelinek 		 * in by default so we can print a warning and modify the class
39993247Sgjelinek 		 * if we wouldn't be using FSS.
40003247Sgjelinek 		 */
40013247Sgjelinek 		char class_name[PC_CLNMSZ];
40023247Sgjelinek 
40033247Sgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
40043247Sgjelinek 		    sizeof (class_name)) != Z_OK) {
40053247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
40063247Sgjelinek 			    "the zone's scheduling class");
40073247Sgjelinek 
40083247Sgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
40093247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
40103247Sgjelinek 			    "rctl is set but\nFSS is not the default "
40113247Sgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
40123247Sgjelinek 			    "used for processes\nin the zone but to get the "
40133247Sgjelinek 			    "full benefit of FSS,\nit should be the default "
40143247Sgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
40153247Sgjelinek 			    "details.");
40163247Sgjelinek 
40173247Sgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
40183247Sgjelinek 			    strlen("FSS")) == -1)
40193247Sgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
40203247Sgjelinek 				    "zone scheduling class to FSS");
40213247Sgjelinek 		}
40223247Sgjelinek 	}
40233247Sgjelinek 
40243247Sgjelinek 	/*
40253247Sgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
40263247Sgjelinek 	 * well as persistent pools.  In all cases we call the functions
40273247Sgjelinek 	 * unconditionally.  Within each funtion the code will check if the
40283247Sgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
40293247Sgjelinek 	 * and just return if there is nothing to do.
40303247Sgjelinek 	 *
40313247Sgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
40323247Sgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
40333247Sgjelinek 	 * right thing in all cases (reuse or create) based on the current
40343247Sgjelinek 	 * zonecfg.
40353247Sgjelinek 	 */
40363247Sgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
40373247Sgjelinek 	    sizeof (pool_err))) != Z_OK) {
40383247Sgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
40393247Sgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
40403247Sgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
40413247Sgjelinek 			    pool_err);
40423247Sgjelinek 		else
40433247Sgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
40443247Sgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
40453247Sgjelinek 		zonecfg_fini_handle(handle);
40463247Sgjelinek 		return (Z_POOL_BIND);
40473247Sgjelinek 	}
40483247Sgjelinek 
40493247Sgjelinek 	/*
40503247Sgjelinek 	 * Check if we need to warn about poold not being enabled.
40513247Sgjelinek 	 */
40523247Sgjelinek 	if (zonecfg_warn_poold(handle)) {
40533247Sgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
40543247Sgjelinek 		    "been specified\nbut the dynamic pool service is not "
40553247Sgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
40563247Sgjelinek 		    "processor allocation within the specified range\n"
40573247Sgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
40583247Sgjelinek 		    "See poold(1M).");
40593247Sgjelinek 	}
40603247Sgjelinek 
40613247Sgjelinek 	/* The following is a warning, not an error. */
40623247Sgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
40633247Sgjelinek 	    sizeof (pool_err))) != Z_OK) {
40643247Sgjelinek 		if (res == Z_POOL_BIND)
40653247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
40663247Sgjelinek 			    "pool '%s'; using default pool.", pool_err);
40673247Sgjelinek 		else if (res == Z_POOL)
40683247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
40693247Sgjelinek 			    zonecfg_strerror(res), pool_err);
40703247Sgjelinek 		else
40713247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
40723247Sgjelinek 			    zonecfg_strerror(res));
40733247Sgjelinek 	}
40743247Sgjelinek 
40753247Sgjelinek 	zonecfg_fini_handle(handle);
40763247Sgjelinek 	return (Z_OK);
40773247Sgjelinek }
40783247Sgjelinek 
4079766Scarlsonj zoneid_t
40805829Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4081766Scarlsonj {
4082766Scarlsonj 	zoneid_t rval = -1;
40830Sstevel@tonic-gate 	priv_set_t *privs;
40840Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
40852712Snn35248 	char modname[MAXPATHLEN];
40862712Snn35248 	struct brand_attr attr;
40872727Sedp 	brand_handle_t bh;
40880Sstevel@tonic-gate 	char *rctlbuf = NULL;
4089766Scarlsonj 	size_t rctlbufsz = 0;
4090789Sahrens 	char *zfsbuf = NULL;
4091789Sahrens 	size_t zfsbufsz = 0;
4092766Scarlsonj 	zoneid_t zoneid = -1;
40930Sstevel@tonic-gate 	int xerr;
4094766Scarlsonj 	char *kzone;
4095766Scarlsonj 	FILE *fp = NULL;
40961676Sjpk 	tsol_zcent_t *zcent = NULL;
40971676Sjpk 	int match = 0;
40981676Sjpk 	int doi = 0;
40993448Sdh155122 	int flags;
41003448Sdh155122 	zone_iptype_t iptype;
41010Sstevel@tonic-gate 
41020Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
41030Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
41040Sstevel@tonic-gate 		return (-1);
41050Sstevel@tonic-gate 	}
4106766Scarlsonj 	if (zonecfg_in_alt_root())
4107766Scarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
41080Sstevel@tonic-gate 
41093448Sdh155122 	if (get_iptype(zlogp, &iptype) < 0) {
41103448Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
41113448Sdh155122 		return (-1);
41123448Sdh155122 	}
41133448Sdh155122 	switch (iptype) {
41143448Sdh155122 	case ZS_SHARED:
41153448Sdh155122 		flags = 0;
41163448Sdh155122 		break;
41173448Sdh155122 	case ZS_EXCLUSIVE:
41183448Sdh155122 		flags = ZCF_NET_EXCL;
41193448Sdh155122 		break;
41203448Sdh155122 	}
41213448Sdh155122 
41220Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
41230Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
41240Sstevel@tonic-gate 		return (-1);
41250Sstevel@tonic-gate 	}
41260Sstevel@tonic-gate 	priv_emptyset(privs);
41271645Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
41280Sstevel@tonic-gate 		goto error;
41291645Scomay 
41305829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
41315829Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
41320Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
41330Sstevel@tonic-gate 		goto error;
41340Sstevel@tonic-gate 	}
41351645Scomay 
4136789Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4137789Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4138789Sahrens 		goto error;
4139789Sahrens 	}
41400Sstevel@tonic-gate 
41415829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
41421676Sjpk 		zcent = get_zone_label(zlogp, privs);
41431769Scarlsonj 		if (zcent != NULL) {
41441676Sjpk 			match = zcent->zc_match;
41451676Sjpk 			doi = zcent->zc_doi;
41461676Sjpk 			*zlabel = zcent->zc_label;
41471676Sjpk 		} else {
41481676Sjpk 			goto error;
41491676Sjpk 		}
41501676Sjpk 	}
41511676Sjpk 
4152766Scarlsonj 	kzone = zone_name;
4153766Scarlsonj 
4154766Scarlsonj 	/*
4155766Scarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4156766Scarlsonj 	 * main system that are using this root (or any subdirectory of it).
4157766Scarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4158766Scarlsonj 	 * that use this same source node (same device and inode).
4159766Scarlsonj 	 */
4160766Scarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4161766Scarlsonj 		goto error;
4162766Scarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4163766Scarlsonj 		goto error;
4164766Scarlsonj 
41655829Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
41664350Std153743 		assert(zone_isnative || zone_iscluster);
4167766Scarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4168766Scarlsonj 
4169766Scarlsonj 		/*
4170766Scarlsonj 		 * Forge up a special root for this zone.  When a zone is
4171766Scarlsonj 		 * mounted, we can't let the zone have its own root because the
4172766Scarlsonj 		 * tools that will be used in this "scratch zone" need access
4173766Scarlsonj 		 * to both the zone's resources and the running machine's
4174766Scarlsonj 		 * executables.
4175766Scarlsonj 		 *
4176766Scarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4177766Scarlsonj 		 */
4178766Scarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4179766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4180766Scarlsonj 			goto error;
4181766Scarlsonj 		}
4182766Scarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4183766Scarlsonj 			goto error;
4184766Scarlsonj 	}
4185766Scarlsonj 
4186766Scarlsonj 	if (zonecfg_in_alt_root()) {
4187766Scarlsonj 		/*
4188766Scarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4189766Scarlsonj 		 * then we have some additional work to do before starting the
4190766Scarlsonj 		 * zone.  First, resolve the root path down so that we're not
4191766Scarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4192766Scarlsonj 		 * the zone.
4193766Scarlsonj 		 */
4194766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4195766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4196766Scarlsonj 			goto error;
4197766Scarlsonj 		}
4198766Scarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4199766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4200766Scarlsonj 			goto error;
4201766Scarlsonj 		}
4202766Scarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4203766Scarlsonj 		    NULL, 0) == 0) {
4204766Scarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4205766Scarlsonj 			goto error;
4206766Scarlsonj 		}
4207766Scarlsonj 		/* This is the preferred name */
4208766Scarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4209766Scarlsonj 		    zone_name);
4210766Scarlsonj 		srandom(getpid());
4211766Scarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4212766Scarlsonj 		    0) == 0) {
4213766Scarlsonj 			/* This is just an arbitrary name; note "." usage */
4214766Scarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4215766Scarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4216766Scarlsonj 		}
4217766Scarlsonj 		kzone = kernzone;
4218766Scarlsonj 	}
4219766Scarlsonj 
42200Sstevel@tonic-gate 	xerr = 0;
4221766Scarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
42223448Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
42233448Sdh155122 	    flags)) == -1) {
42240Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
42250Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
42260Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
42270Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
42280Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
42290Sstevel@tonic-gate 			} else {
42300Sstevel@tonic-gate 
42310Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
42320Sstevel@tonic-gate 				    "These file-systems are mounted on "
42330Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
42340Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
42350Sstevel@tonic-gate 				    prtmount, zlogp);
42360Sstevel@tonic-gate 			}
42370Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
42380Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
42390Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
42400Sstevel@tonic-gate 			    "environment", "zone_create");
42414791Ston 		} else if (xerr == ZE_LABELINUSE) {
42424791Ston 			char zonename[ZONENAME_MAX];
42434791Ston 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
42444791Ston 			    zonename, ZONENAME_MAX);
42454791Ston 			zerror(zlogp, B_FALSE, "The zone label is already "
42464791Ston 			    "used by the zone '%s'.", zonename);
42470Sstevel@tonic-gate 		} else {
42480Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
42490Sstevel@tonic-gate 		}
42500Sstevel@tonic-gate 		goto error;
42510Sstevel@tonic-gate 	}
4252766Scarlsonj 
4253766Scarlsonj 	if (zonecfg_in_alt_root() &&
4254766Scarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4255766Scarlsonj 	    zonecfg_get_root()) == -1) {
4256766Scarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4257766Scarlsonj 		goto error;
4258766Scarlsonj 	}
4259766Scarlsonj 
42602712Snn35248 	if ((zone_get_brand(zone_name, attr.ba_brandname,
42612712Snn35248 	    MAXNAMELEN) != Z_OK) ||
42622727Sedp 	    (bh = brand_open(attr.ba_brandname)) == NULL) {
42632712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine brand name");
42642712Snn35248 		return (-1);
42652712Snn35248 	}
42662712Snn35248 
42672712Snn35248 	/*
42682712Snn35248 	 * If this brand requires any kernel support, now is the time to
42692712Snn35248 	 * get it loaded and initialized.
42702712Snn35248 	 */
42712727Sedp 	if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
42723716Sgjelinek 		brand_close(bh);
42732712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine brand kernel "
42742712Snn35248 		    "module");
42752712Snn35248 		return (-1);
42762712Snn35248 	}
42773716Sgjelinek 	brand_close(bh);
42782712Snn35248 
42792712Snn35248 	if (strlen(modname) > 0) {
42802712Snn35248 		(void) strlcpy(attr.ba_modname, modname, MAXPATHLEN);
42812712Snn35248 		if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
42822712Snn35248 		    sizeof (attr) != 0)) {
42832712Snn35248 			zerror(zlogp, B_TRUE, "could not set zone brand "
42842712Snn35248 			    "attribute.");
42852712Snn35248 			goto error;
42862712Snn35248 		}
42872712Snn35248 	}
42882712Snn35248 
42890Sstevel@tonic-gate 	/*
42903247Sgjelinek 	 * The following actions are not performed when merely mounting a zone
42913247Sgjelinek 	 * for administrative use.
42920Sstevel@tonic-gate 	 */
42935829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
42943247Sgjelinek 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) {
42953247Sgjelinek 			(void) zone_shutdown(zoneid);
42963247Sgjelinek 			goto error;
42973247Sgjelinek 		}
42983247Sgjelinek 
42991769Scarlsonj 		set_mlps(zlogp, zoneid, zcent);
43003247Sgjelinek 	}
43013247Sgjelinek 
4302766Scarlsonj 	rval = zoneid;
4303766Scarlsonj 	zoneid = -1;
4304766Scarlsonj 
43050Sstevel@tonic-gate error:
4306766Scarlsonj 	if (zoneid != -1)
4307766Scarlsonj 		(void) zone_destroy(zoneid);
43080Sstevel@tonic-gate 	if (rctlbuf != NULL)
43090Sstevel@tonic-gate 		free(rctlbuf);
43100Sstevel@tonic-gate 	priv_freeset(privs);
4311766Scarlsonj 	if (fp != NULL)
4312766Scarlsonj 		zonecfg_close_scratch(fp);
4313766Scarlsonj 	lofs_discard_mnttab();
43141676Sjpk 	if (zcent != NULL)
43151676Sjpk 		tsol_freezcent(zcent);
43160Sstevel@tonic-gate 	return (rval);
43170Sstevel@tonic-gate }
43180Sstevel@tonic-gate 
43192303Scarlsonj /*
43202303Scarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
43212303Scarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
43222303Scarlsonj  * details from inside the zone.
43232303Scarlsonj  */
43242303Scarlsonj static void
43252303Scarlsonj write_index_file(zoneid_t zoneid)
43262303Scarlsonj {
43272303Scarlsonj 	FILE *zef;
43282303Scarlsonj 	FILE *zet;
43292303Scarlsonj 	struct zoneent *zep;
43302303Scarlsonj 	pid_t child;
43312303Scarlsonj 	int tmpl_fd;
43322303Scarlsonj 	ctid_t ct;
43332303Scarlsonj 	int fd;
43342303Scarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
43352303Scarlsonj 
43362303Scarlsonj 	/* Locate the zone entry in the global zone's index file */
43372303Scarlsonj 	if ((zef = setzoneent()) == NULL)
43382303Scarlsonj 		return;
43392303Scarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
43402303Scarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
43412303Scarlsonj 			break;
43422303Scarlsonj 		free(zep);
43432303Scarlsonj 	}
43442303Scarlsonj 	endzoneent(zef);
43452303Scarlsonj 	if (zep == NULL)
43462303Scarlsonj 		return;
43472303Scarlsonj 
43482303Scarlsonj 	if ((tmpl_fd = init_template()) == -1) {
43492303Scarlsonj 		free(zep);
43502303Scarlsonj 		return;
43512303Scarlsonj 	}
43522303Scarlsonj 
43532303Scarlsonj 	if ((child = fork()) == -1) {
43542303Scarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
43552303Scarlsonj 		(void) close(tmpl_fd);
43562303Scarlsonj 		free(zep);
43572303Scarlsonj 		return;
43582303Scarlsonj 	}
43592303Scarlsonj 
43602303Scarlsonj 	/* parent waits for child to finish */
43612303Scarlsonj 	if (child != 0) {
43622303Scarlsonj 		free(zep);
43632303Scarlsonj 		if (contract_latest(&ct) == -1)
43642303Scarlsonj 			ct = -1;
43652303Scarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
43662303Scarlsonj 		(void) close(tmpl_fd);
43672303Scarlsonj 		(void) waitpid(child, NULL, 0);
43682303Scarlsonj 		(void) contract_abandon_id(ct);
43692303Scarlsonj 		return;
43702303Scarlsonj 	}
43712303Scarlsonj 
43722303Scarlsonj 	/* child enters zone and sets up index file */
43732303Scarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
43742303Scarlsonj 	if (zone_enter(zoneid) != -1) {
43752303Scarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
43762303Scarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
43772303Scarlsonj 		    ZONE_CONFIG_GID);
43782303Scarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
43792303Scarlsonj 		    ZONE_INDEX_MODE);
43802303Scarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
43812303Scarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
43822303Scarlsonj 			if (uuid_is_null(zep->zone_uuid))
43832303Scarlsonj 				uuidstr[0] = '\0';
43842303Scarlsonj 			else
43852303Scarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
43862303Scarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
43872303Scarlsonj 			    zone_state_str(zep->zone_state),
43882303Scarlsonj 			    uuidstr);
43892303Scarlsonj 			(void) fclose(zet);
43902303Scarlsonj 		}
43912303Scarlsonj 	}
43922303Scarlsonj 	_exit(0);
43932303Scarlsonj }
43942303Scarlsonj 
43950Sstevel@tonic-gate int
43965829Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
43970Sstevel@tonic-gate {
43982712Snn35248 	char zonepath[MAXPATHLEN];
43992503Sdp 
44005829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4401789Sahrens 		lofs_discard_mnttab();
4402789Sahrens 		return (-1);
4403789Sahrens 	}
4404789Sahrens 
44052712Snn35248 	/*
44062712Snn35248 	 * Before we try to mount filesystems we need to create the
44072712Snn35248 	 * attribute backing store for /dev
44082712Snn35248 	 */
44092712Snn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
44102712Snn35248 		lofs_discard_mnttab();
44112712Snn35248 		return (-1);
44122712Snn35248 	}
44133071Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
44143813Sdp 
44153813Sdp 	/* Make /dev directory owned by root, grouped sys */
44163813Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
44173813Sdp 	    0, 3) != 0) {
44182712Snn35248 		lofs_discard_mnttab();
44192712Snn35248 		return (-1);
44202712Snn35248 	}
44212712Snn35248 
44222621Sllai1 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4423766Scarlsonj 		lofs_discard_mnttab();
44240Sstevel@tonic-gate 		return (-1);
4425766Scarlsonj 	}
44262621Sllai1 
44275829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
44283448Sdh155122 		zone_iptype_t iptype;
44293448Sdh155122 
44303448Sdh155122 		if (get_iptype(zlogp, &iptype) < 0) {
44313448Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
44323448Sdh155122 			lofs_discard_mnttab();
44333448Sdh155122 			return (-1);
44343448Sdh155122 		}
44353448Sdh155122 
44363448Sdh155122 		switch (iptype) {
44373448Sdh155122 		case ZS_SHARED:
44383448Sdh155122 			/* Always do this to make lo0 get configured */
44393448Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
44403448Sdh155122 				lofs_discard_mnttab();
44413448Sdh155122 				return (-1);
44423448Sdh155122 			}
44433448Sdh155122 			break;
44443448Sdh155122 		case ZS_EXCLUSIVE:
44453448Sdh155122 			if (configure_exclusive_network_interfaces(zlogp) !=
44463448Sdh155122 			    0) {
44473448Sdh155122 				lofs_discard_mnttab();
44483448Sdh155122 				return (-1);
44493448Sdh155122 			}
44503448Sdh155122 			break;
44513448Sdh155122 		}
4452766Scarlsonj 	}
44532303Scarlsonj 
44542303Scarlsonj 	write_index_file(zoneid);
44552303Scarlsonj 
4456766Scarlsonj 	lofs_discard_mnttab();
44570Sstevel@tonic-gate 	return (0);
44580Sstevel@tonic-gate }
44590Sstevel@tonic-gate 
4460766Scarlsonj static int
4461766Scarlsonj lu_root_teardown(zlog_t *zlogp)
4462766Scarlsonj {
4463766Scarlsonj 	char zroot[MAXPATHLEN];
4464766Scarlsonj 
44654350Std153743 	assert(zone_isnative || zone_iscluster);
44662712Snn35248 
4467766Scarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4468766Scarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4469766Scarlsonj 		return (-1);
4470766Scarlsonj 	}
4471766Scarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4472766Scarlsonj 
4473766Scarlsonj 	/*
4474766Scarlsonj 	 * At this point, the processes are gone, the filesystems (save the
4475766Scarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
4476766Scarlsonj 	 * still be creds floating about in the system that reference the
4477766Scarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4478766Scarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4479766Scarlsonj 	 * (How I wish this were not true, and umount2 just did the right
4480766Scarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4481766Scarlsonj 	 */
4482766Scarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
4483766Scarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4484766Scarlsonj 			goto unmounted;
4485766Scarlsonj 		if (errno == EBUSY) {
4486766Scarlsonj 			int tries = 10;
4487766Scarlsonj 
4488766Scarlsonj 			while (--tries >= 0) {
4489766Scarlsonj 				(void) sleep(1);
4490766Scarlsonj 				if (umount2(zroot, 0) == 0)
4491766Scarlsonj 					goto unmounted;
4492766Scarlsonj 				if (errno != EBUSY)
4493766Scarlsonj 					break;
4494766Scarlsonj 			}
4495766Scarlsonj 		}
4496766Scarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4497766Scarlsonj 		return (-1);
4498766Scarlsonj 	}
4499766Scarlsonj unmounted:
4500766Scarlsonj 
4501766Scarlsonj 	/*
4502766Scarlsonj 	 * Only zones in an alternate root environment have scratch zone
4503766Scarlsonj 	 * entries.
4504766Scarlsonj 	 */
4505766Scarlsonj 	if (zonecfg_in_alt_root()) {
4506766Scarlsonj 		FILE *fp;
4507766Scarlsonj 		int retv;
4508766Scarlsonj 
4509766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4510766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4511766Scarlsonj 			return (-1);
4512766Scarlsonj 		}
4513766Scarlsonj 		retv = -1;
4514766Scarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
4515766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4516766Scarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4517766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4518766Scarlsonj 		else
4519766Scarlsonj 			retv = 0;
4520766Scarlsonj 		zonecfg_close_scratch(fp);
4521766Scarlsonj 		return (retv);
4522766Scarlsonj 	} else {
4523766Scarlsonj 		return (0);
4524766Scarlsonj 	}
4525766Scarlsonj }
4526766Scarlsonj 
45270Sstevel@tonic-gate int
45283247Sgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
45290Sstevel@tonic-gate {
4530766Scarlsonj 	char *kzone;
45310Sstevel@tonic-gate 	zoneid_t zoneid;
45323247Sgjelinek 	int res;
45333247Sgjelinek 	char pool_err[128];
4534*7089Sgjelinek 	char zpath[MAXPATHLEN];
45352712Snn35248 	char cmdbuf[MAXPATHLEN];
45362712Snn35248 	char brand[MAXNAMELEN];
45372727Sedp 	brand_handle_t bh = NULL;
45383448Sdh155122 	ushort_t flags;
45390Sstevel@tonic-gate 
4540766Scarlsonj 	kzone = zone_name;
4541766Scarlsonj 	if (zonecfg_in_alt_root()) {
4542766Scarlsonj 		FILE *fp;
4543766Scarlsonj 
4544766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4545766Scarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
4546766Scarlsonj 			goto error;
4547766Scarlsonj 		}
4548766Scarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4549766Scarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
4550766Scarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4551766Scarlsonj 			zonecfg_close_scratch(fp);
4552766Scarlsonj 			goto error;
4553766Scarlsonj 		}
4554766Scarlsonj 		zonecfg_close_scratch(fp);
4555766Scarlsonj 		kzone = kernzone;
4556766Scarlsonj 	}
4557766Scarlsonj 
4558766Scarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
45590Sstevel@tonic-gate 		if (!bringup_failure_recovery)
45600Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4561766Scarlsonj 		if (unmount_cmd)
4562766Scarlsonj 			(void) lu_root_teardown(zlogp);
45630Sstevel@tonic-gate 		goto error;
45640Sstevel@tonic-gate 	}
45650Sstevel@tonic-gate 
45660Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
45670Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
45680Sstevel@tonic-gate 		goto error;
45690Sstevel@tonic-gate 	}
45700Sstevel@tonic-gate 
4571*7089Sgjelinek 	/* Get the zonepath of this zone */
4572*7089Sgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
4573*7089Sgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
45742712Snn35248 		goto error;
45752712Snn35248 	}
45762712Snn35248 
45772712Snn35248 	/* Get a handle to the brand info for this zone */
45782712Snn35248 	if ((zone_get_brand(zone_name, brand, sizeof (brand)) != Z_OK) ||
45792727Sedp 	    (bh = brand_open(brand)) == NULL) {
45802712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
45812712Snn35248 		return (-1);
45822712Snn35248 	}
45832712Snn35248 	/*
45842712Snn35248 	 * If there is a brand 'halt' callback, execute it now to give the
45852712Snn35248 	 * brand a chance to cleanup any custom configuration.
45862712Snn35248 	 */
45872712Snn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
4588*7089Sgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
4589*7089Sgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
45902727Sedp 		brand_close(bh);
45912712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
45922712Snn35248 		    "halt callback.");
45932712Snn35248 		goto error;
45942712Snn35248 	}
45952727Sedp 	brand_close(bh);
45962712Snn35248 
45972712Snn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
45982712Snn35248 	    (do_subproc(zlogp, cmdbuf) != Z_OK)) {
45992712Snn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
46002712Snn35248 		goto error;
46012712Snn35248 	}
46022712Snn35248 
46033448Sdh155122 	if (!unmount_cmd) {
46043448Sdh155122 		zone_iptype_t iptype;
46053448Sdh155122 
46063448Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
46073448Sdh155122 		    sizeof (flags)) < 0) {
46083448Sdh155122 			if (get_iptype(zlogp, &iptype) < 0) {
46093448Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
46103448Sdh155122 				    "ip-type");
46113448Sdh155122 				goto error;
46123448Sdh155122 			}
46133448Sdh155122 		} else {
46143448Sdh155122 			if (flags & ZF_NET_EXCL)
46153448Sdh155122 				iptype = ZS_EXCLUSIVE;
46163448Sdh155122 			else
46173448Sdh155122 				iptype = ZS_SHARED;
46183448Sdh155122 		}
46193448Sdh155122 
46203448Sdh155122 		switch (iptype) {
46213448Sdh155122 		case ZS_SHARED:
46223448Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
46233448Sdh155122 			    zoneid) != 0) {
46243448Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
46253448Sdh155122 				    "network interfaces in zone");
46263448Sdh155122 				goto error;
46273448Sdh155122 			}
46283448Sdh155122 			break;
46293448Sdh155122 		case ZS_EXCLUSIVE:
46303448Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
46313448Sdh155122 			    zoneid) != 0) {
46323448Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
46333448Sdh155122 				    "network interfaces in zone");
46343448Sdh155122 				goto error;
46353448Sdh155122 			}
46363448Sdh155122 			break;
46373448Sdh155122 		}
46380Sstevel@tonic-gate 	}
46390Sstevel@tonic-gate 
4640766Scarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
46410Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
46420Sstevel@tonic-gate 		goto error;
46430Sstevel@tonic-gate 	}
46440Sstevel@tonic-gate 
46452621Sllai1 	/* destroy zconsole before umount /dev */
46462621Sllai1 	if (!unmount_cmd)
46472621Sllai1 		destroy_console_slave();
46482621Sllai1 
4649766Scarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
46500Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
46510Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
46520Sstevel@tonic-gate 		goto error;
46530Sstevel@tonic-gate 	}
46540Sstevel@tonic-gate 
46553247Sgjelinek 	/*
46563356Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
46573356Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
46583356Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
46593356Sgjelinek 	 * running with a temporary pool and the zone configuration has been
46603356Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
46613356Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
46623356Sgjelinek 	 * where we never had a temporary pool configured but
46633356Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
46643247Sgjelinek 	 */
46653356Sgjelinek 	if (!unmount_cmd) {
46663356Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
46673356Sgjelinek 
46683356Sgjelinek 		if (rebooting) {
46693356Sgjelinek 			struct zone_psettab pset_tab;
46703356Sgjelinek 			zone_dochandle_t handle;
46713356Sgjelinek 
46723356Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
46733356Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
46743356Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
46753356Sgjelinek 				destroy_tmp_pool = B_FALSE;
46763716Sgjelinek 
46773716Sgjelinek 			zonecfg_fini_handle(handle);
46783356Sgjelinek 		}
46793356Sgjelinek 
46803356Sgjelinek 		if (destroy_tmp_pool) {
46813356Sgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
46823356Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
46833356Sgjelinek 				if (res == Z_POOL)
46843356Sgjelinek 					zerror(zlogp, B_FALSE, pool_err);
46853356Sgjelinek 			}
46863247Sgjelinek 		}
46873247Sgjelinek 	}
46883247Sgjelinek 
46891676Sjpk 	remove_mlps(zlogp, zoneid);
46901676Sjpk 
46910Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
46920Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
46930Sstevel@tonic-gate 		goto error;
46940Sstevel@tonic-gate 	}
46950Sstevel@tonic-gate 
4696766Scarlsonj 	/*
4697766Scarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
4698766Scarlsonj 	 * root for the zone and then remove it from the map file.
4699766Scarlsonj 	 */
4700766Scarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4701766Scarlsonj 		goto error;
4702766Scarlsonj 
4703766Scarlsonj 	lofs_discard_mnttab();
47040Sstevel@tonic-gate 	return (0);
47050Sstevel@tonic-gate 
47060Sstevel@tonic-gate error:
4707766Scarlsonj 	lofs_discard_mnttab();
47080Sstevel@tonic-gate 	return (-1);
47090Sstevel@tonic-gate }
4710