xref: /onnv-gate/usr/src/cmd/zoneadmd/vplat.c (revision 10943:b45d62b629cd)
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 /*
238485SPeter.Memishian@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
290Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
300Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
310Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
320Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
330Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
340Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
35766Scarlsonj  *
36766Scarlsonj  * This module also has primary responsibility for the layout of "scratch
37766Scarlsonj  * zones."  These are mounted, but inactive, zones that are used during
38766Scarlsonj  * operating system upgrade and potentially other administrative action.  The
39766Scarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
40766Scarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
41766Scarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
42766Scarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
43766Scarlsonj  * without relying on any binaries in the zone itself.
44766Scarlsonj  *
45766Scarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
46766Scarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
47766Scarlsonj  * writable (unshared) resources.  Shared resources, though, are always
48766Scarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
49766Scarlsonj  * currently running, then "/b" inside the zone points to the running zone's
50766Scarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
51766Scarlsonj  * process.
52766Scarlsonj  *
53766Scarlsonj  * To construct this environment, this module creates a tmpfs mount on
54766Scarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
55766Scarlsonj  * described above is constructed on the fly.  The zone is then created using
56766Scarlsonj  * $ZONEPATH/lu as the root.
57766Scarlsonj  *
58766Scarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
59766Scarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
60766Scarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
61766Scarlsonj  * is not a part of the usual halt/ready/boot state machine.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <sys/param.h>
650Sstevel@tonic-gate #include <sys/mount.h>
660Sstevel@tonic-gate #include <sys/mntent.h>
670Sstevel@tonic-gate #include <sys/socket.h>
680Sstevel@tonic-gate #include <sys/utsname.h>
690Sstevel@tonic-gate #include <sys/types.h>
700Sstevel@tonic-gate #include <sys/stat.h>
710Sstevel@tonic-gate #include <sys/sockio.h>
720Sstevel@tonic-gate #include <sys/stropts.h>
730Sstevel@tonic-gate #include <sys/conf.h>
748662SJordan.Vaughan@Sun.com #include <sys/systeminfo.h>
750Sstevel@tonic-gate 
763448Sdh155122 #include <libdlpi.h>
773871Syz147064 #include <libdllink.h>
785895Syz147064 #include <libdlvlan.h>
793448Sdh155122 
800Sstevel@tonic-gate #include <inet/tcp.h>
810Sstevel@tonic-gate #include <arpa/inet.h>
820Sstevel@tonic-gate #include <netinet/in.h>
830Sstevel@tonic-gate #include <net/route.h>
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #include <stdio.h>
860Sstevel@tonic-gate #include <errno.h>
870Sstevel@tonic-gate #include <fcntl.h>
880Sstevel@tonic-gate #include <unistd.h>
890Sstevel@tonic-gate #include <rctl.h>
900Sstevel@tonic-gate #include <stdlib.h>
910Sstevel@tonic-gate #include <string.h>
920Sstevel@tonic-gate #include <strings.h>
930Sstevel@tonic-gate #include <wait.h>
940Sstevel@tonic-gate #include <limits.h>
950Sstevel@tonic-gate #include <libgen.h>
96789Sahrens #include <libzfs.h>
972621Sllai1 #include <libdevinfo.h>
980Sstevel@tonic-gate #include <zone.h>
990Sstevel@tonic-gate #include <assert.h>
1002303Scarlsonj #include <libcontract.h>
1012303Scarlsonj #include <libcontract_priv.h>
1022303Scarlsonj #include <uuid/uuid.h>
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #include <sys/mntio.h>
1050Sstevel@tonic-gate #include <sys/mnttab.h>
1060Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1070Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
108789Sahrens #include <sys/fs/zfs.h>
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #include <pool.h>
1110Sstevel@tonic-gate #include <sys/pool.h>
1123247Sgjelinek #include <sys/priocntl.h>
1130Sstevel@tonic-gate 
1142712Snn35248 #include <libbrand.h>
1152712Snn35248 #include <sys/brand.h>
1160Sstevel@tonic-gate #include <libzonecfg.h>
1172170Sevanl #include <synch.h>
1182611Svp157776 
1190Sstevel@tonic-gate #include "zoneadmd.h"
1201676Sjpk #include <tsol/label.h>
1211676Sjpk #include <libtsnet.h>
1221676Sjpk #include <sys/priv.h>
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1250Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1280Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
1311676Sjpk #define	MAXTNZLEN	2048
1320Sstevel@tonic-gate 
1335829Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1345829Sgjelinek 
1350Sstevel@tonic-gate /* for routing socket */
1360Sstevel@tonic-gate static int rts_seqno = 0;
1370Sstevel@tonic-gate 
138766Scarlsonj /* mangled zone name when mounting in an alternate root environment */
139766Scarlsonj static char kernzone[ZONENAME_MAX];
140766Scarlsonj 
141766Scarlsonj /* array of cached mount entries for resolve_lofs */
142766Scarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
143766Scarlsonj 
1441676Sjpk /* for Trusted Extensions */
1451676Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
1461676Sjpk static int tsol_mounts(zlog_t *, char *, char *);
1471676Sjpk static void tsol_unmounts(zlog_t *, char *);
1485596Sdh155122 
1491676Sjpk static m_label_t *zlabel = NULL;
1501676Sjpk static m_label_t *zid_label = NULL;
1511676Sjpk static priv_set_t *zprivs = NULL;
1521676Sjpk 
1530Sstevel@tonic-gate /* from libsocket, not in any header file */
1540Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1550Sstevel@tonic-gate 
1567370S<Gerald Jelinek> /* from zoneadmd */
1577370S<Gerald Jelinek> extern char query_hook[];
1587370S<Gerald Jelinek> 
1590Sstevel@tonic-gate /*
160766Scarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
161766Scarlsonj  * data) only once every N times through the loop.
162766Scarlsonj  */
163766Scarlsonj #define	MNTTAB_HUNK	32
164766Scarlsonj 
165766Scarlsonj /*
1660Sstevel@tonic-gate  * Private autofs system call
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate extern int _autofssys(int, void *);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static int
1710Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
179766Scarlsonj static void
180766Scarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
181766Scarlsonj {
182766Scarlsonj 	uint_t i;
183766Scarlsonj 
184766Scarlsonj 	if (mnt_array == NULL)
185766Scarlsonj 		return;
186766Scarlsonj 	for (i = 0; i < nelem; i++) {
187766Scarlsonj 		free(mnt_array[i].mnt_mountp);
188766Scarlsonj 		free(mnt_array[i].mnt_fstype);
189766Scarlsonj 		free(mnt_array[i].mnt_special);
190766Scarlsonj 		free(mnt_array[i].mnt_mntopts);
191766Scarlsonj 		assert(mnt_array[i].mnt_time == NULL);
192766Scarlsonj 	}
193766Scarlsonj 	free(mnt_array);
194766Scarlsonj }
195766Scarlsonj 
196766Scarlsonj /*
197766Scarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
198766Scarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
199766Scarlsonj  * array in "nelemp".
200766Scarlsonj  */
201766Scarlsonj static int
202766Scarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
203766Scarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
204766Scarlsonj {
205766Scarlsonj 	struct mnttab mnt;
206766Scarlsonj 	struct mnttab *mnts;
207766Scarlsonj 	struct mnttab *mnp;
208766Scarlsonj 	uint_t nmnt;
209766Scarlsonj 
210766Scarlsonj 	rewind(mnttab);
211766Scarlsonj 	resetmnttab(mnttab);
212766Scarlsonj 	nmnt = 0;
213766Scarlsonj 	mnts = NULL;
214766Scarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
215766Scarlsonj 		struct mnttab *tmp_array;
216766Scarlsonj 
217766Scarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
218766Scarlsonj 			continue;
219766Scarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
220766Scarlsonj 			tmp_array = realloc(mnts,
221766Scarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
222766Scarlsonj 			if (tmp_array == NULL) {
223766Scarlsonj 				free_mnttable(mnts, nmnt);
224766Scarlsonj 				return (-1);
225766Scarlsonj 			}
226766Scarlsonj 			mnts = tmp_array;
227766Scarlsonj 		}
228766Scarlsonj 		mnp = &mnts[nmnt++];
229766Scarlsonj 
230766Scarlsonj 		/*
231766Scarlsonj 		 * Zero out any fields we're not using.
232766Scarlsonj 		 */
233766Scarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
234766Scarlsonj 
235766Scarlsonj 		if (mnt.mnt_special != NULL)
236766Scarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
237766Scarlsonj 		if (mnt.mnt_mntopts != NULL)
238766Scarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
239766Scarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
240766Scarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
241766Scarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
242766Scarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
243766Scarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
244766Scarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
245766Scarlsonj 			free_mnttable(mnts, nmnt);
246766Scarlsonj 			return (-1);
247766Scarlsonj 		}
248766Scarlsonj 	}
249766Scarlsonj 	*mnt_arrayp = mnts;
250766Scarlsonj 	*nelemp = nmnt;
251766Scarlsonj 	return (0);
252766Scarlsonj }
253766Scarlsonj 
254766Scarlsonj /*
255766Scarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
256766Scarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
257766Scarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
258766Scarlsonj  * reread of the list of mount points
259766Scarlsonj  */
260766Scarlsonj static void
261766Scarlsonj lofs_discard_mnttab(void)
262766Scarlsonj {
263766Scarlsonj 	free_mnttable(resolve_lofs_mnts,
264766Scarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
265766Scarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
266766Scarlsonj }
267766Scarlsonj 
268766Scarlsonj static int
269766Scarlsonj lofs_read_mnttab(zlog_t *zlogp)
270766Scarlsonj {
271766Scarlsonj 	FILE *mnttab;
272766Scarlsonj 	uint_t nmnts;
273766Scarlsonj 
274766Scarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
275766Scarlsonj 		return (-1);
276766Scarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
277766Scarlsonj 	    &nmnts) == -1) {
278766Scarlsonj 		(void) fclose(mnttab);
279766Scarlsonj 		return (-1);
280766Scarlsonj 	}
281766Scarlsonj 	(void) fclose(mnttab);
282766Scarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
283766Scarlsonj 	return (0);
284766Scarlsonj }
285766Scarlsonj 
286766Scarlsonj /*
287766Scarlsonj  * This function loops over potential loopback mounts and symlinks in a given
288766Scarlsonj  * path and resolves them all down to an absolute path.
289766Scarlsonj  */
2905576Sedp void
291766Scarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
292766Scarlsonj {
293766Scarlsonj 	int len, arlen;
294766Scarlsonj 	const char *altroot;
295766Scarlsonj 	char tmppath[MAXPATHLEN];
296766Scarlsonj 	boolean_t outside_altroot;
297766Scarlsonj 
298766Scarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
299766Scarlsonj 		return;
300766Scarlsonj 	tmppath[len] = '\0';
301766Scarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
302766Scarlsonj 
303766Scarlsonj 	/* This happens once per zoneadmd operation. */
304766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
305766Scarlsonj 		return;
306766Scarlsonj 
307766Scarlsonj 	altroot = zonecfg_get_root();
308766Scarlsonj 	arlen = strlen(altroot);
309766Scarlsonj 	outside_altroot = B_FALSE;
310766Scarlsonj 	for (;;) {
311766Scarlsonj 		struct mnttab *mnp;
312766Scarlsonj 
3133079Sdminer 		/* Search in reverse order to find longest match */
3143079Sdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3153079Sdminer 		    mnp--) {
316766Scarlsonj 			if (mnp->mnt_fstype == NULL ||
317766Scarlsonj 			    mnp->mnt_mountp == NULL ||
3183079Sdminer 			    mnp->mnt_special == NULL)
319766Scarlsonj 				continue;
320766Scarlsonj 			len = strlen(mnp->mnt_mountp);
321766Scarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
322766Scarlsonj 			    (path[len] == '/' || path[len] == '\0'))
323766Scarlsonj 				break;
324766Scarlsonj 		}
3253079Sdminer 		if (mnp < resolve_lofs_mnts)
3263079Sdminer 			break;
3273079Sdminer 		/* If it's not a lofs then we're done */
3283079Sdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
329766Scarlsonj 			break;
330766Scarlsonj 		if (outside_altroot) {
331766Scarlsonj 			char *cp;
332766Scarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
333766Scarlsonj 
334766Scarlsonj 			/*
335766Scarlsonj 			 * If we run into a read-only mount outside of the
336766Scarlsonj 			 * alternate root environment, then the user doesn't
337766Scarlsonj 			 * want this path to be made read-write.
338766Scarlsonj 			 */
339766Scarlsonj 			if (mnp->mnt_mntopts != NULL &&
340766Scarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
341766Scarlsonj 			    NULL &&
342766Scarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
343766Scarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
344766Scarlsonj 				break;
345766Scarlsonj 			}
346766Scarlsonj 		} else if (arlen > 0 &&
347766Scarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
348766Scarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
349766Scarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
350766Scarlsonj 			outside_altroot = B_TRUE;
351766Scarlsonj 		}
352766Scarlsonj 		/* use temporary buffer because new path might be longer */
353766Scarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
354766Scarlsonj 		    mnp->mnt_special, path + len);
355766Scarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
356766Scarlsonj 			break;
357766Scarlsonj 		path[len] = '\0';
358766Scarlsonj 	}
359766Scarlsonj }
360766Scarlsonj 
361766Scarlsonj /*
362766Scarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
363766Scarlsonj  * referenced device is already mounted somewhere.
364766Scarlsonj  */
365766Scarlsonj static int
366766Scarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
367766Scarlsonj {
368766Scarlsonj 	struct mnttab *mnp;
369766Scarlsonj 	zone_fsopt_t *optptr, *onext;
370766Scarlsonj 
371766Scarlsonj 	/* This happens once per zoneadmd operation. */
372766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
373766Scarlsonj 		return (-1);
374766Scarlsonj 
375766Scarlsonj 	/*
376766Scarlsonj 	 * If this special node isn't already in use, then it's ours alone;
377766Scarlsonj 	 * no need to worry about conflicting mounts.
378766Scarlsonj 	 */
379766Scarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
380766Scarlsonj 	    mnp++) {
381766Scarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
382766Scarlsonj 			break;
383766Scarlsonj 	}
384766Scarlsonj 	if (mnp >= resolve_lofs_mnt_max)
385766Scarlsonj 		return (0);
386766Scarlsonj 
387766Scarlsonj 	/*
388766Scarlsonj 	 * Convert this duplicate mount into a lofs mount.
389766Scarlsonj 	 */
390766Scarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
391766Scarlsonj 	    sizeof (fsptr->zone_fs_special));
392766Scarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
393766Scarlsonj 	    sizeof (fsptr->zone_fs_type));
394766Scarlsonj 	fsptr->zone_fs_raw[0] = '\0';
395766Scarlsonj 
396766Scarlsonj 	/*
397766Scarlsonj 	 * Discard all but one of the original options and set that to be the
398766Scarlsonj 	 * same set of options used for inherit package directory resources.
399766Scarlsonj 	 */
400766Scarlsonj 	optptr = fsptr->zone_fs_options;
401766Scarlsonj 	if (optptr == NULL) {
402766Scarlsonj 		optptr = malloc(sizeof (*optptr));
403766Scarlsonj 		if (optptr == NULL) {
404766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
405766Scarlsonj 			    fsptr->zone_fs_dir);
406766Scarlsonj 			return (-1);
407766Scarlsonj 		}
408766Scarlsonj 	} else {
409766Scarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
410766Scarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
411766Scarlsonj 			free(onext);
412766Scarlsonj 		}
413766Scarlsonj 	}
414766Scarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
415766Scarlsonj 	optptr->zone_fsopt_next = NULL;
416766Scarlsonj 	fsptr->zone_fs_options = optptr;
417766Scarlsonj 	return (0);
418766Scarlsonj }
419766Scarlsonj 
4205182Sedp int
4213813Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
4223813Sdp     uid_t userid, gid_t groupid)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	char path[MAXPATHLEN];
4250Sstevel@tonic-gate 	struct stat st;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4280Sstevel@tonic-gate 	    sizeof (path)) {
4290Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4300Sstevel@tonic-gate 		    subdir);
4310Sstevel@tonic-gate 		return (-1);
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4350Sstevel@tonic-gate 		/*
4360Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4370Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4380Sstevel@tonic-gate 		 * and we don't need to second guess him.
4390Sstevel@tonic-gate 		 */
4400Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
4417714SRic.Aleshire@Sun.COM 			if (S_ISREG(st.st_mode)) {
4421676Sjpk 				/*
4437714SRic.Aleshire@Sun.COM 				 * Allow readonly mounts of /etc/ files; this
4447714SRic.Aleshire@Sun.COM 				 * is needed most by 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 {
561766Scarlsonj 	if (!isresolved && zonecfg_in_alt_root())
562766Scarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
563766Scarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5680Sstevel@tonic-gate  *
5690Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5700Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5710Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5720Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5730Sstevel@tonic-gate  *
5740Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5750Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5760Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5770Sstevel@tonic-gate  *
5780Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
5790Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
5800Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
5810Sstevel@tonic-gate  *
5820Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
5830Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
5840Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
5850Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
5860Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
5870Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
5880Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
5890Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
5900Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
5910Sstevel@tonic-gate  * graceful mounts first.
5920Sstevel@tonic-gate  *
5930Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate static int
596766Scarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	int error = 0;
5990Sstevel@tonic-gate 	FILE *mnttab;
6000Sstevel@tonic-gate 	struct mnttab *mnts;
6010Sstevel@tonic-gate 	uint_t nmnt;
6020Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6030Sstevel@tonic-gate 	size_t zrootlen;
6040Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6050Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6060Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6090Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6100Sstevel@tonic-gate 		return (-1);
6110Sstevel@tonic-gate 	}
612766Scarlsonj 	if (unmount_cmd)
613766Scarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6160Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6170Sstevel@tonic-gate 
6181676Sjpk 	/*
6191676Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
6201676Sjpk 	 * of our zone's /export/home
6211676Sjpk 	 */
6221769Scarlsonj 	if (!unmount_cmd)
6231769Scarlsonj 		tsol_unmounts(zlogp, zone_name);
6241676Sjpk 
6250Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6260Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6270Sstevel@tonic-gate 		return (-1);
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 	/*
6300Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6310Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6320Sstevel@tonic-gate 	 */
6330Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6340Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6350Sstevel@tonic-gate 		error++;
6360Sstevel@tonic-gate 		goto out;
6370Sstevel@tonic-gate 	}
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	/*
6400Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6410Sstevel@tonic-gate 	 * should forcibly unmount.
6420Sstevel@tonic-gate 	 */
6430Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6440Sstevel@tonic-gate 	for (; /* ever */; ) {
6450Sstevel@tonic-gate 		uint_t newcount = 0;
6460Sstevel@tonic-gate 		boolean_t unmounted;
6470Sstevel@tonic-gate 		struct mnttab *mnp;
6480Sstevel@tonic-gate 		char *path;
6490Sstevel@tonic-gate 		uint_t i;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		mnts = NULL;
6520Sstevel@tonic-gate 		nmnt = 0;
6530Sstevel@tonic-gate 		/*
6540Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6550Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6560Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6570Sstevel@tonic-gate 		 * filesystems.
6580Sstevel@tonic-gate 		 */
6590Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6600Sstevel@tonic-gate 		    &nmnt) != 0) {
6610Sstevel@tonic-gate 			error++;
6620Sstevel@tonic-gate 			goto out;
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6650Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6660Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6670Sstevel@tonic-gate 			unmounted = B_FALSE;
6680Sstevel@tonic-gate 			/*
6690Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6700Sstevel@tonic-gate 			 *
6710Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6720Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6730Sstevel@tonic-gate 			 * and try a regular unmount.
6740Sstevel@tonic-gate 			 */
6750Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6760Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6770Sstevel@tonic-gate 					unmounted = B_TRUE;
6780Sstevel@tonic-gate 			}
6790Sstevel@tonic-gate 			/*
6800Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
6810Sstevel@tonic-gate 			 */
6820Sstevel@tonic-gate 			if (stuck) {
6830Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
6840Sstevel@tonic-gate 					unmounted = B_TRUE;
6850Sstevel@tonic-gate 					stuck = B_FALSE;
6860Sstevel@tonic-gate 				} else {
6870Sstevel@tonic-gate 					/*
6880Sstevel@tonic-gate 					 * The first failure indicates a
6890Sstevel@tonic-gate 					 * mount we won't be able to get
6900Sstevel@tonic-gate 					 * rid of automatically, so we
6910Sstevel@tonic-gate 					 * bail.
6920Sstevel@tonic-gate 					 */
6930Sstevel@tonic-gate 					error++;
6940Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
6950Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
6960Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
6970Sstevel@tonic-gate 					goto out;
6980Sstevel@tonic-gate 				}
6990Sstevel@tonic-gate 			}
7000Sstevel@tonic-gate 			/*
7010Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7020Sstevel@tonic-gate 			 */
7030Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7040Sstevel@tonic-gate 				newcount++;
7050Sstevel@tonic-gate 		}
7060Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		if (newcount == 0)
7090Sstevel@tonic-gate 			break;
7100Sstevel@tonic-gate 		if (newcount >= oldcount) {
7110Sstevel@tonic-gate 			/*
7120Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7130Sstevel@tonic-gate 			 * should start trying forced unmounts.
7140Sstevel@tonic-gate 			 */
7150Sstevel@tonic-gate 			stuck = B_TRUE;
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 		oldcount = newcount;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		/*
7200Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7210Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7220Sstevel@tonic-gate 		 */
7230Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7240Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7250Sstevel@tonic-gate 			error++;
7260Sstevel@tonic-gate 			goto out;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate out:
7310Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7320Sstevel@tonic-gate 	(void) fclose(mnttab);
7330Sstevel@tonic-gate 	return (error ? -1 : 0);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static int
7370Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7400Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7470Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7480Sstevel@tonic-gate  * returns the exit status otherwise.
7490Sstevel@tonic-gate  *
7500Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7510Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7520Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7530Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7540Sstevel@tonic-gate  * remains the case.
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate static int
7570Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	pid_t child_pid;
7600Sstevel@tonic-gate 	int child_status = 0;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	/*
7630Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7640Sstevel@tonic-gate 	 */
7650Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7660Sstevel@tonic-gate 	child_pid = fork();
7670Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7680Sstevel@tonic-gate 	if (child_pid == -1) {
7690Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7700Sstevel@tonic-gate 		return (-1);
7710Sstevel@tonic-gate 	} else if (child_pid == 0) {
7720Sstevel@tonic-gate 		closefrom(0);
7731915Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7741915Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7751915Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7761915Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7770Sstevel@tonic-gate 		(void) execv(path, argv);
7780Sstevel@tonic-gate 		/*
7790Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
7800Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
7810Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
7820Sstevel@tonic-gate 		 * accordingly.
7830Sstevel@tonic-gate 		 */
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
7860Sstevel@tonic-gate 	} else {
7870Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
7910Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
7920Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
7930Sstevel@tonic-gate 		return (-1);
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
7960Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
7970Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
7980Sstevel@tonic-gate 		return (-1);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate static int
8046734Sjohnlev isregfile(const char *path)
8056734Sjohnlev {
8066734Sjohnlev 	struct stat64 st;
8076734Sjohnlev 
8086734Sjohnlev 	if (stat64(path, &st) == -1)
8096734Sjohnlev 		return (-1);
8106734Sjohnlev 
8116734Sjohnlev 	return (S_ISREG(st.st_mode));
8126734Sjohnlev }
8136734Sjohnlev 
8146734Sjohnlev static int
8150Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8180Sstevel@tonic-gate 	char *argv[4];
8190Sstevel@tonic-gate 	int status;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	/*
8220Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8230Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8240Sstevel@tonic-gate 	 */
8250Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8262712Snn35248 	    >= sizeof (cmdbuf)) {
8270Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8280Sstevel@tonic-gate 		return (-1);
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8316734Sjohnlev 	/*
8326734Sjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
8336734Sjohnlev 	 * in zoneadm.
8346734Sjohnlev 	 */
8356734Sjohnlev 	if (isregfile(cmdbuf) == -1)
8366734Sjohnlev 		return (0);
8376734Sjohnlev 
8380Sstevel@tonic-gate 	argv[0] = "fsck";
8390Sstevel@tonic-gate 	argv[1] = "-m";
8400Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
8410Sstevel@tonic-gate 	argv[3] = NULL;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8440Sstevel@tonic-gate 	if (status == 0 || status == -1)
8450Sstevel@tonic-gate 		return (status);
8460Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8470Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8480Sstevel@tonic-gate 	return (-1);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate static int
8520Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8530Sstevel@tonic-gate     const char *special, const char *directory)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8560Sstevel@tonic-gate 	char *argv[6];
8570Sstevel@tonic-gate 	int status;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/*
8600Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8610Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8642712Snn35248 	    >= sizeof (cmdbuf)) {
8650Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8660Sstevel@tonic-gate 		return (-1);
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 	argv[0] = "mount";
8690Sstevel@tonic-gate 	if (opts[0] == '\0') {
8700Sstevel@tonic-gate 		argv[1] = (char *)special;
8710Sstevel@tonic-gate 		argv[2] = (char *)directory;
8720Sstevel@tonic-gate 		argv[3] = NULL;
8730Sstevel@tonic-gate 	} else {
8740Sstevel@tonic-gate 		argv[1] = "-o";
8750Sstevel@tonic-gate 		argv[2] = (char *)opts;
8760Sstevel@tonic-gate 		argv[3] = (char *)special;
8770Sstevel@tonic-gate 		argv[4] = (char *)directory;
8780Sstevel@tonic-gate 		argv[5] = NULL;
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8820Sstevel@tonic-gate 	if (status == 0 || status == -1)
8830Sstevel@tonic-gate 		return (status);
8840Sstevel@tonic-gate 	if (opts[0] == '\0')
8850Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
8860Sstevel@tonic-gate 		    "failed with exit code %d",
8870Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
8880Sstevel@tonic-gate 	else
8890Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
8900Sstevel@tonic-gate 		    "failed with exit code %d",
8910Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
8920Sstevel@tonic-gate 	return (-1);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate /*
8965182Sedp  * Check if a given mount point path exists.
8975182Sedp  * If it does, make sure it doesn't contain any symlinks.
8985182Sedp  * Note that if "leaf" is false we're checking an intermediate
8995182Sedp  * component of the mount point path, so it must be a directory.
9005182Sedp  * If "leaf" is true, then we're checking the entire mount point
9015182Sedp  * path, so the mount point itself can be anything aside from a
9025182Sedp  * symbolic link.
9035182Sedp  *
9045182Sedp  * If the path is invalid then a negative value is returned.  If the
9055182Sedp  * path exists and is a valid mount point path then 0 is returned.
9065182Sedp  * If the path doesn't exist return a positive value.
9070Sstevel@tonic-gate  */
9080Sstevel@tonic-gate static int
9095182Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate 	struct stat statbuf;
9120Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9130Sstevel@tonic-gate 	int res;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9160Sstevel@tonic-gate 		if (errno == ENOENT)
9175182Sedp 			return (1);
9180Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9190Sstevel@tonic-gate 		return (-1);
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9220Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9230Sstevel@tonic-gate 		return (-1);
9240Sstevel@tonic-gate 	}
9255182Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9265182Sedp 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9275182Sedp 		return (-1);
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9300Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9310Sstevel@tonic-gate 		return (-1);
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 	respath[res] = '\0';
9340Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9350Sstevel@tonic-gate 		/*
9365182Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9370Sstevel@tonic-gate 		 */
9380Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9390Sstevel@tonic-gate 		return (-1);
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate 	return (0);
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate /*
9455182Sedp  * Validate a mount point path.  A valid mount point path is an
9465182Sedp  * absolute path that either doesn't exist, or, if it does exists it
9475182Sedp  * must be an absolute canonical path that doesn't have any symbolic
9485182Sedp  * links in it.  The target of a mount point path can be any filesystem
9495182Sedp  * object.  (Different filesystems can support different mount points,
9505182Sedp  * for example "lofs" and "mntfs" both support files and directories
9515182Sedp  * while "ufs" just supports directories.)
9520Sstevel@tonic-gate  *
9535182Sedp  * If the path is invalid then a negative value is returned.  If the
9545182Sedp  * path exists and is a valid mount point path then 0 is returned.
9555182Sedp  * If the path doesn't exist return a positive value.
9560Sstevel@tonic-gate  */
9575182Sedp int
9585182Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
9595182Sedp     const char *dir, const char *fstype)
9600Sstevel@tonic-gate {
9615182Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
9625182Sedp 	int rv;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/*
9655182Sedp 	 * Sanity check the target mount point path.
9665182Sedp 	 * It must be a non-null string that starts with a '/'.
9670Sstevel@tonic-gate 	 */
9685182Sedp 	if (dir[0] != '/') {
9695182Sedp 		if (spec[0] == '\0') {
9705182Sedp 			/*
9715182Sedp 			 * This must be an invalid ipd entry (see comments
9725182Sedp 			 * in mount_filesystems_ipdent()).
9735182Sedp 			 */
9745182Sedp 			zerror(zlogp, B_FALSE,
9755182Sedp 			    "invalid inherit-pkg-dir entry: \"%s\"", dir);
9765182Sedp 		} else {
9775182Sedp 			/* Something went wrong. */
9785182Sedp 			zerror(zlogp, B_FALSE, "invalid mount directory, "
9795182Sedp 			    "type: \"%s\", special: \"%s\", dir: \"%s\"",
9805182Sedp 			    fstype, spec, dir);
9815182Sedp 		}
9825182Sedp 		return (-1);
9835182Sedp 	}
9845182Sedp 
9855182Sedp 	/*
9865182Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
9875182Sedp 	 * is added to all paths (even non-directory paths) to allow us
9885182Sedp 	 * to detect the end of paths below.  If the path already ends
9895182Sedp 	 * in a '/', then that's ok too (although we'll fail the
9905182Sedp 	 * cannonical path check in valid_mount_point()).
9915182Sedp 	 */
9925182Sedp 	if (snprintf(abspath, sizeof (abspath),
9935182Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
9945182Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
9955182Sedp 		    rootpath, dir);
9965182Sedp 		return (-1);
9975182Sedp 	}
9985182Sedp 
9995182Sedp 	/*
10005182Sedp 	 * Starting with rootpath, verify the mount path one component
10015182Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
10025182Sedp 	 */
10030Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10040Sstevel@tonic-gate 	assert(*slashp == '/');
10050Sstevel@tonic-gate 	do {
10065182Sedp 		slashp_next = strchr(slashp + 1, '/');
10070Sstevel@tonic-gate 		*slashp = '\0';
10085182Sedp 		if (slashp_next != NULL) {
10095182Sedp 			/* This is an intermediary mount path component. */
10105182Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
10115182Sedp 		} else {
10125182Sedp 			/* This is the last component of the mount path. */
10135182Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
10145182Sedp 		}
10155182Sedp 		if (rv < 0)
10165182Sedp 			return (rv);
10170Sstevel@tonic-gate 		*slashp = '/';
10185182Sedp 	} while ((slashp = slashp_next) != NULL);
10195182Sedp 	return (rv);
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate static int
10232712Snn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10242712Snn35248 {
10252712Snn35248 	di_prof_t prof = arg;
10262712Snn35248 
10272712Snn35248 	if (name == NULL)
10282712Snn35248 		return (di_prof_add_dev(prof, match));
10292712Snn35248 	return (di_prof_add_map(prof, match, name));
10302712Snn35248 }
10312712Snn35248 
10322712Snn35248 static int
10332712Snn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10342712Snn35248 {
10352712Snn35248 	di_prof_t prof = arg;
10362712Snn35248 
10372712Snn35248 	return (di_prof_add_symlink(prof, source, target));
10382712Snn35248 }
10392712Snn35248 
104010616SSebastien.Roy@Sun.COM int
104110616SSebastien.Roy@Sun.COM vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
10423448Sdh155122 {
10433448Sdh155122 	zone_dochandle_t handle;
10443448Sdh155122 
10453448Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
10463448Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
10473448Sdh155122 		return (-1);
10483448Sdh155122 	}
10493448Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
10503448Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
10513448Sdh155122 		zonecfg_fini_handle(handle);
10523448Sdh155122 		return (-1);
10533448Sdh155122 	}
10543448Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
10553448Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
10563448Sdh155122 		zonecfg_fini_handle(handle);
10573448Sdh155122 		return (-1);
10583448Sdh155122 	}
10593448Sdh155122 	zonecfg_fini_handle(handle);
10603448Sdh155122 	return (0);
10613448Sdh155122 }
10623448Sdh155122 
10632712Snn35248 /*
10642712Snn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10652712Snn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10662712Snn35248  * use these as a profile/filter to determine what exists in /dev.
10672712Snn35248  */
10682712Snn35248 static int
10697655Sgerald.jelinek@sun.com mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10702712Snn35248 {
10712712Snn35248 	char			brand[MAXNAMELEN];
10722712Snn35248 	zone_dochandle_t	handle = NULL;
10732727Sedp 	brand_handle_t		bh = NULL;
10742712Snn35248 	struct zone_devtab	ztab;
10752712Snn35248 	di_prof_t		prof = NULL;
10762712Snn35248 	int			err;
10772712Snn35248 	int			retval = -1;
10783448Sdh155122 	zone_iptype_t		iptype;
10793448Sdh155122 	const char 		*curr_iptype;
10802712Snn35248 
10812712Snn35248 	if (di_prof_init(devpath, &prof)) {
10822712Snn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10832712Snn35248 		goto cleanup;
10842712Snn35248 	}
10852712Snn35248 
10867655Sgerald.jelinek@sun.com 	/*
10877655Sgerald.jelinek@sun.com 	 * Get a handle to the brand info for this zone.
1088*10943SEdward.Pilatowicz@Sun.COM 	 * If we are mounting the zone, then we must always use the default
10897655Sgerald.jelinek@sun.com 	 * brand device mounts.
10907655Sgerald.jelinek@sun.com 	 */
10917655Sgerald.jelinek@sun.com 	if (ALT_MOUNT(mount_cmd)) {
1092*10943SEdward.Pilatowicz@Sun.COM 		(void) strlcpy(brand, default_brand, sizeof (brand));
10937655Sgerald.jelinek@sun.com 	} else {
109410796SStephen.Lawrence@Sun.COM 		(void) strlcpy(brand, brand_name, sizeof (brand));
10957655Sgerald.jelinek@sun.com 	}
10967655Sgerald.jelinek@sun.com 
10977655Sgerald.jelinek@sun.com 	if ((bh = brand_open(brand)) == NULL) {
10982712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
10992712Snn35248 		goto cleanup;
11002712Snn35248 	}
11012712Snn35248 
110210616SSebastien.Roy@Sun.COM 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
11033448Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
11043448Sdh155122 		goto cleanup;
11053448Sdh155122 	}
11063448Sdh155122 	switch (iptype) {
11073448Sdh155122 	case ZS_SHARED:
11083448Sdh155122 		curr_iptype = "shared";
11093448Sdh155122 		break;
11103448Sdh155122 	case ZS_EXCLUSIVE:
11113448Sdh155122 		curr_iptype = "exclusive";
11123448Sdh155122 		break;
11133448Sdh155122 	}
11143448Sdh155122 
11152727Sedp 	if (brand_platform_iter_devices(bh, zone_name,
11163448Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11172712Snn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11182712Snn35248 		goto cleanup;
11192712Snn35248 	}
11202712Snn35248 
11212727Sedp 	if (brand_platform_iter_link(bh,
11222712Snn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11232712Snn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11242712Snn35248 		goto cleanup;
11252712Snn35248 	}
11262712Snn35248 
11272712Snn35248 	/* Add user-specified devices and directories */
11282712Snn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11292712Snn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11302712Snn35248 		goto cleanup;
11312712Snn35248 	}
11322712Snn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11332712Snn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11342712Snn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11352712Snn35248 		goto cleanup;
11362712Snn35248 	}
11372712Snn35248 	if (err = zonecfg_setdevent(handle)) {
11382712Snn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11392712Snn35248 		    zonecfg_strerror(err));
11402712Snn35248 		goto cleanup;
11412712Snn35248 	}
11422712Snn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11432712Snn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11442712Snn35248 			zerror(zlogp, B_TRUE, "failed to add "
11452712Snn35248 			    "user-specified device");
11462712Snn35248 			goto cleanup;
11472712Snn35248 		}
11482712Snn35248 	}
11492712Snn35248 	(void) zonecfg_enddevent(handle);
11502712Snn35248 
11512712Snn35248 	/* Send profile to kernel */
11522712Snn35248 	if (di_prof_commit(prof)) {
11532712Snn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11542712Snn35248 		goto cleanup;
11552712Snn35248 	}
11562712Snn35248 
11572712Snn35248 	retval = 0;
11582712Snn35248 
11592712Snn35248 cleanup:
11602727Sedp 	if (bh != NULL)
11612727Sedp 		brand_close(bh);
11623716Sgjelinek 	if (handle != NULL)
11632712Snn35248 		zonecfg_fini_handle(handle);
11642712Snn35248 	if (prof)
11652712Snn35248 		di_prof_fini(prof);
11662712Snn35248 	return (retval);
11672712Snn35248 }
11682712Snn35248 
11692712Snn35248 static int
11707655Sgerald.jelinek@sun.com mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11717655Sgerald.jelinek@sun.com     zone_mnt_t mount_cmd)
11720Sstevel@tonic-gate {
11732712Snn35248 	char path[MAXPATHLEN];
11742712Snn35248 	char specpath[MAXPATHLEN];
11752712Snn35248 	char optstr[MAX_MNTOPT_STR];
11760Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11772712Snn35248 	int rv;
11780Sstevel@tonic-gate 
11795182Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
11805182Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11810Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11820Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11830Sstevel@tonic-gate 		return (-1);
11845182Sedp 	} else if (rv > 0) {
11855182Sedp 		/* The mount point path doesn't exist, create it now. */
11865182Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
11875182Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
11885182Sedp 		    DEFAULT_DIR_GROUP) != 0) {
11895182Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
11905182Sedp 			return (-1);
11915182Sedp 		}
11925182Sedp 
11935182Sedp 		/*
11945182Sedp 		 * Now this might seem weird, but we need to invoke
11955182Sedp 		 * valid_mount_path() again.  Why?  Because it checks
11965182Sedp 		 * to make sure that the mount point path is canonical,
11975182Sedp 		 * which it can only do if the path exists, so now that
11985182Sedp 		 * we've created the path we have to verify it again.
11995182Sedp 		 */
12005182Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
12015182Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
12025182Sedp 		    fsptr->zone_fs_type)) < 0) {
12035182Sedp 			zerror(zlogp, B_FALSE,
12045182Sedp 			    "%s%s is not a valid mount point",
12055182Sedp 			    rootpath, fsptr->zone_fs_dir);
12065182Sedp 			return (-1);
12075182Sedp 		}
12085182Sedp 	}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12110Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
12140Sstevel@tonic-gate 		/*
12150Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1216766Scarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1217766Scarlsonj 		 * can be seen via the alternate zone's root.
12180Sstevel@tonic-gate 		 */
1219766Scarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1220766Scarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1221766Scarlsonj 		    sizeof (specpath)) {
1222766Scarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1223766Scarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1224766Scarlsonj 			return (-1);
1225766Scarlsonj 		}
1226766Scarlsonj 		if (zonecfg_in_alt_root())
1227766Scarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
12280Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1229766Scarlsonj 		    specpath, path) != 0) {
12300Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1231766Scarlsonj 			    specpath);
12320Sstevel@tonic-gate 			return (-1);
12330Sstevel@tonic-gate 		}
12340Sstevel@tonic-gate 		return (0);
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	/*
12380Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12390Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12400Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12410Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12420Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12430Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12440Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12450Sstevel@tonic-gate 	 */
12460Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12470Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12480Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12490Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12500Sstevel@tonic-gate 		return (-1);
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	/*
1254766Scarlsonj 	 * If we're looking at an alternate root environment, then construct
12553688Sedp 	 * read-only loopback mounts as necessary.  Note that any special
12563688Sedp 	 * paths for lofs zone mounts in an alternate root must have
12573688Sedp 	 * already been pre-pended with any alternate root path by the
12583688Sedp 	 * time we get here.
1259766Scarlsonj 	 */
1260766Scarlsonj 	if (zonecfg_in_alt_root()) {
1261766Scarlsonj 		struct stat64 st;
1262766Scarlsonj 
1263766Scarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
12642772Scarlsonj 		    S_ISBLK(st.st_mode)) {
12653688Sedp 			/*
12663688Sedp 			 * If we're going to mount a block device we need
12673688Sedp 			 * to check if that device is already mounted
12683688Sedp 			 * somewhere else, and if so, do a lofs mount
12693688Sedp 			 * of the device instead of a direct mount
12703688Sedp 			 */
12712772Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
12722772Scarlsonj 				return (-1);
12732772Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
12743688Sedp 			/*
12753688Sedp 			 * For lofs mounts, the special node is inside the
12763688Sedp 			 * alternate root.  We need lofs resolution for
12773688Sedp 			 * this case in order to get at the underlying
12783688Sedp 			 * read-write path.
12793688Sedp 			 */
12803688Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1281766Scarlsonj 			    sizeof (fsptr->zone_fs_special));
1282766Scarlsonj 		}
1283766Scarlsonj 	}
1284766Scarlsonj 
1285766Scarlsonj 	/*
12860Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12870Sstevel@tonic-gate 	 */
12880Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
12896734Sjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12900Sstevel@tonic-gate 		return (-1);
12916734Sjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
12926734Sjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
12936734Sjohnlev 		return (-1);
12946734Sjohnlev 	}
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	/*
12970Sstevel@tonic-gate 	 * Build up mount option string.
12980Sstevel@tonic-gate 	 */
12990Sstevel@tonic-gate 	optstr[0] = '\0';
13000Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
13010Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
13020Sstevel@tonic-gate 		    sizeof (optstr));
13030Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
13040Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
13050Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
13060Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
13070Sstevel@tonic-gate 			    sizeof (optstr));
13080Sstevel@tonic-gate 		}
13090Sstevel@tonic-gate 	}
13102712Snn35248 
13112712Snn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13122712Snn35248 	    fsptr->zone_fs_special, path)) != 0)
13132712Snn35248 		return (rv);
13142712Snn35248 
13152712Snn35248 	/*
13162712Snn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13172712Snn35248 	 * we're done.
13182712Snn35248 	 */
13192712Snn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13202712Snn35248 		return (0);
13212712Snn35248 
13222712Snn35248 	/*
13232712Snn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13242712Snn35248 	 * need to configure it.
13252712Snn35248 	 */
13267655Sgerald.jelinek@sun.com 	return (mount_one_dev(zlogp, path, mount_cmd));
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate static void
13300Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13310Sstevel@tonic-gate {
13320Sstevel@tonic-gate 	uint_t i;
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	if (fsarray == NULL)
13350Sstevel@tonic-gate 		return;
13360Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13370Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13380Sstevel@tonic-gate 	free(fsarray);
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate 
1341766Scarlsonj /*
13422653Svp157776  * This function initiates the creation of a small Solaris Environment for
13432653Svp157776  * scratch zone. The Environment creation process is split up into two
13442653Svp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
13452653Svp157776  * is done this way because:
13462653Svp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
13472653Svp157776  * 	We loopback mount zone's own /etc and /var into the root of the
13482653Svp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
13492653Svp157776  * 	need to delay the mount of /var till the zone's root gets populated.
13502653Svp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
13512653Svp157776  * 	build_mounted_post_var() which gets called only after the zone
13522653Svp157776  * 	specific filesystems are mounted.
13535829Sgjelinek  *
13545829Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13555829Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13565829Sgjelinek  * scratch zone.
1357766Scarlsonj  */
1358766Scarlsonj static boolean_t
13592653Svp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
13603071Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1361766Scarlsonj {
1362766Scarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1363766Scarlsonj 	const char **cpp;
1364766Scarlsonj 	static const char *mkdirs[] = {
13652592Sdp 		"/system", "/system/contract", "/system/object", "/proc",
13662592Sdp 		"/dev", "/tmp", "/a", NULL
1367766Scarlsonj 	};
13682653Svp157776 	char *altstr;
1369766Scarlsonj 	FILE *fp;
1370766Scarlsonj 	uuid_t uuid;
1371766Scarlsonj 
1372766Scarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
13733071Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
13743071Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1375766Scarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1376766Scarlsonj 	(void) symlink("./usr/bin", tmp);
1377766Scarlsonj 
1378766Scarlsonj 	/*
1379766Scarlsonj 	 * These are mostly special mount points; not handled here.  (See
1380766Scarlsonj 	 * zone_mount_early.)
1381766Scarlsonj 	 */
1382766Scarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1383766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1384766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1385766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1386766Scarlsonj 			return (B_FALSE);
1387766Scarlsonj 		}
1388766Scarlsonj 	}
13892653Svp157776 	/*
13902653Svp157776 	 * This is here to support lucopy.  If there's an instance of this same
13912653Svp157776 	 * zone on the current running system, then we mount its root up as
13922653Svp157776 	 * read-only inside the scratch zone.
13932653Svp157776 	 */
13942653Svp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
13952653Svp157776 	altstr = strdup(zonecfg_get_root());
13962653Svp157776 	if (altstr == NULL) {
13972653Svp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
13982653Svp157776 		return (B_FALSE);
13992653Svp157776 	}
14002653Svp157776 	zonecfg_set_root("");
14012653Svp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
14022653Svp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
14032653Svp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
14042653Svp157776 	    strcmp(fromdir, rootpath) != 0) {
14052653Svp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
14062653Svp157776 		if (mkdir(tmp, 0755) != 0) {
14072653Svp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
14082653Svp157776 			return (B_FALSE);
14092653Svp157776 		}
14102653Svp157776 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
14112653Svp157776 		    tmp) != 0) {
14122653Svp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
14132653Svp157776 			    fromdir);
14142653Svp157776 			return (B_FALSE);
14152653Svp157776 		}
14162653Svp157776 	}
14172653Svp157776 	zonecfg_set_root(altstr);
14182653Svp157776 	free(altstr);
14192653Svp157776 
14202653Svp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
14212653Svp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
14222653Svp157776 		return (B_FALSE);
14232653Svp157776 	}
14242653Svp157776 	(void) ftruncate(fileno(fp), 0);
14252653Svp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
14262653Svp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
14272653Svp157776 	}
14282653Svp157776 	zonecfg_close_scratch(fp);
14292653Svp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
14302653Svp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
14312653Svp157776 		return (B_FALSE);
14322653Svp157776 	(void) strlcpy(rootpath, tmp, rootlen);
14332653Svp157776 	return (B_TRUE);
14342653Svp157776 }
14352653Svp157776 
14362653Svp157776 
14372653Svp157776 static boolean_t
14385829Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14395829Sgjelinek     const char *luroot)
14402653Svp157776 {
14412653Svp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
14422653Svp157776 	const char **cpp;
14435829Sgjelinek 	const char **loopdirs;
14445829Sgjelinek 	const char **tmpdirs;
14452653Svp157776 	static const char *localdirs[] = {
14462653Svp157776 		"/etc", "/var", NULL
14472653Svp157776 	};
14485829Sgjelinek 	static const char *scr_loopdirs[] = {
14492653Svp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
14502653Svp157776 		"/usr", NULL
14512653Svp157776 	};
14525829Sgjelinek 	static const char *upd_loopdirs[] = {
14535829Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14545829Sgjelinek 		"/usr", "/var", NULL
14555829Sgjelinek 	};
14565829Sgjelinek 	static const char *scr_tmpdirs[] = {
14572653Svp157776 		"/tmp", "/var/run", NULL
14582653Svp157776 	};
14595829Sgjelinek 	static const char *upd_tmpdirs[] = {
14605829Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14615829Sgjelinek 	};
14622653Svp157776 	struct stat st;
14632653Svp157776 
14645829Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
14655829Sgjelinek 		/*
14665829Sgjelinek 		 * These are mounted read-write from the zone undergoing
14675829Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14685829Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
14695829Sgjelinek 		 */
14705829Sgjelinek 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14715829Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
14725829Sgjelinek 			    *cpp);
14735829Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14745829Sgjelinek 			    rootpath, *cpp);
14755829Sgjelinek 			if (mkdir(tmp, 0755) != 0) {
14765829Sgjelinek 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
14775829Sgjelinek 				return (B_FALSE);
14785829Sgjelinek 			}
14795829Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14805829Sgjelinek 			    != 0) {
14815829Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14825829Sgjelinek 				    tmp, *cpp);
14835829Sgjelinek 				return (B_FALSE);
14845829Sgjelinek 			}
1485766Scarlsonj 		}
14865829Sgjelinek 	}
14875829Sgjelinek 
14885829Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14895829Sgjelinek 		loopdirs = upd_loopdirs;
14905829Sgjelinek 	else
14915829Sgjelinek 		loopdirs = scr_loopdirs;
1492766Scarlsonj 
1493766Scarlsonj 	/*
1494766Scarlsonj 	 * These are things mounted read-only from the running system because
1495766Scarlsonj 	 * they contain binaries that must match system.
1496766Scarlsonj 	 */
1497766Scarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1498766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1499766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1500766Scarlsonj 			if (errno != EEXIST) {
1501766Scarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1502766Scarlsonj 				return (B_FALSE);
1503766Scarlsonj 			}
1504766Scarlsonj 			if (lstat(tmp, &st) != 0) {
1505766Scarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1506766Scarlsonj 				return (B_FALSE);
1507766Scarlsonj 			}
1508766Scarlsonj 			/*
1509766Scarlsonj 			 * Ignore any non-directories encountered.  These are
1510766Scarlsonj 			 * things that have been converted into symlinks
1511766Scarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1512766Scarlsonj 			 * fixup.
1513766Scarlsonj 			 */
1514766Scarlsonj 			if (!S_ISDIR(st.st_mode))
1515766Scarlsonj 				continue;
1516766Scarlsonj 		}
1517766Scarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1518766Scarlsonj 		    tmp) != 0) {
1519766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1520766Scarlsonj 			    *cpp);
1521766Scarlsonj 			return (B_FALSE);
1522766Scarlsonj 		}
1523766Scarlsonj 	}
1524766Scarlsonj 
15255829Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15265829Sgjelinek 		tmpdirs = upd_tmpdirs;
15275829Sgjelinek 	else
15285829Sgjelinek 		tmpdirs = scr_tmpdirs;
15295829Sgjelinek 
1530766Scarlsonj 	/*
1531766Scarlsonj 	 * These are things with tmpfs mounted inside.
1532766Scarlsonj 	 */
1533766Scarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1534766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15355829Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15365829Sgjelinek 		    errno != EEXIST) {
1537766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1538766Scarlsonj 			return (B_FALSE);
1539766Scarlsonj 		}
15403514Sgjelinek 
15413514Sgjelinek 		/*
15423514Sgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
15433514Sgjelinek 		 * since that can be modified by the umask we will just set
15443514Sgjelinek 		 * the correct mode for /tmp now.
15453514Sgjelinek 		 */
15463514Sgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
15473514Sgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
15483514Sgjelinek 			return (B_FALSE);
15493514Sgjelinek 		}
15503514Sgjelinek 
1551766Scarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1552766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1553766Scarlsonj 			return (B_FALSE);
1554766Scarlsonj 		}
1555766Scarlsonj 	}
1556766Scarlsonj 	return (B_TRUE);
1557766Scarlsonj }
1558766Scarlsonj 
15592712Snn35248 typedef struct plat_gmount_cb_data {
15602712Snn35248 	zlog_t			*pgcd_zlogp;
15612712Snn35248 	struct zone_fstab	**pgcd_fs_tab;
15622712Snn35248 	int			*pgcd_num_fs;
15632712Snn35248 } plat_gmount_cb_data_t;
15642712Snn35248 
15652712Snn35248 /*
15662712Snn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15672712Snn35248  * through all global brand platform mounts.
15682712Snn35248  */
15692712Snn35248 int
15702712Snn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15712712Snn35248     const char *fstype, const char *opt)
15722712Snn35248 {
15732712Snn35248 	plat_gmount_cb_data_t	*cp = data;
15742712Snn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15752712Snn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15762712Snn35248 	int			num_fs = *cp->pgcd_num_fs;
15772712Snn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15782712Snn35248 
15792712Snn35248 	num_fs++;
15802712Snn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15812712Snn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15822712Snn35248 		return (-1);
15832712Snn35248 	}
15842712Snn35248 
15852712Snn35248 	fs_ptr = tmp_ptr;
15862712Snn35248 	fsp = &fs_ptr[num_fs - 1];
15872712Snn35248 
15882712Snn35248 	/* update the callback struct passed in */
15892712Snn35248 	*cp->pgcd_fs_tab = fs_ptr;
15902712Snn35248 	*cp->pgcd_num_fs = num_fs;
15912712Snn35248 
15922712Snn35248 	fsp->zone_fs_raw[0] = '\0';
15932712Snn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15942712Snn35248 	    sizeof (fsp->zone_fs_special));
15952712Snn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15962712Snn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15972712Snn35248 	fsp->zone_fs_options = NULL;
15983688Sedp 	if ((opt != NULL) &&
15993688Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
16002712Snn35248 		zerror(zlogp, B_FALSE, "error adding property");
16012712Snn35248 		return (-1);
16022712Snn35248 	}
16032712Snn35248 
16042712Snn35248 	return (0);
16052712Snn35248 }
16062712Snn35248 
16072712Snn35248 static int
16082712Snn35248 mount_filesystems_ipdent(zone_dochandle_t handle, zlog_t *zlogp,
16092712Snn35248     struct zone_fstab **fs_tabp, int *num_fsp)
16102712Snn35248 {
16112712Snn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16122712Snn35248 	int num_fs;
16132712Snn35248 
16142712Snn35248 	num_fs = *num_fsp;
16152712Snn35248 	fs_ptr = *fs_tabp;
16162712Snn35248 
16172712Snn35248 	if (zonecfg_setipdent(handle) != Z_OK) {
16182712Snn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16192712Snn35248 		return (-1);
16202712Snn35248 	}
16212712Snn35248 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
16222712Snn35248 		num_fs++;
16232712Snn35248 		if ((tmp_ptr = realloc(fs_ptr,
16242712Snn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16252712Snn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16262712Snn35248 			(void) zonecfg_endipdent(handle);
16272712Snn35248 			return (-1);
16282712Snn35248 		}
16292712Snn35248 
16302712Snn35248 		/* update the pointers passed in */
16312712Snn35248 		*fs_tabp = tmp_ptr;
16322712Snn35248 		*num_fsp = num_fs;
16332712Snn35248 
16342712Snn35248 		/*
16352712Snn35248 		 * IPDs logically only have a mount point; all other properties
16362712Snn35248 		 * are implied.
16372712Snn35248 		 */
16382712Snn35248 		fs_ptr = tmp_ptr;
16392712Snn35248 		fsp = &fs_ptr[num_fs - 1];
16402712Snn35248 		(void) strlcpy(fsp->zone_fs_dir,
16412712Snn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16422712Snn35248 		fsp->zone_fs_special[0] = '\0';
16432712Snn35248 		fsp->zone_fs_raw[0] = '\0';
16442712Snn35248 		fsp->zone_fs_type[0] = '\0';
16452712Snn35248 		fsp->zone_fs_options = NULL;
16462712Snn35248 	}
16472712Snn35248 	(void) zonecfg_endipdent(handle);
16482712Snn35248 	return (0);
16492712Snn35248 }
16502712Snn35248 
16512712Snn35248 static int
16522712Snn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16535829Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16542712Snn35248 {
16552712Snn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16562712Snn35248 	int num_fs;
16572712Snn35248 
16582712Snn35248 	num_fs = *num_fsp;
16592712Snn35248 	fs_ptr = *fs_tabp;
16602712Snn35248 
16612712Snn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16622712Snn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16632712Snn35248 		return (-1);
16642712Snn35248 	}
16652712Snn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16662712Snn35248 		/*
16672712Snn35248 		 * ZFS filesystems will not be accessible under an alternate
16682712Snn35248 		 * root, since the pool will not be known.  Ignore them in this
16692712Snn35248 		 * case.
16702712Snn35248 		 */
16715829Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16725829Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16732712Snn35248 			continue;
16742712Snn35248 
16752712Snn35248 		num_fs++;
16762712Snn35248 		if ((tmp_ptr = realloc(fs_ptr,
16772712Snn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16782712Snn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16792712Snn35248 			(void) zonecfg_endfsent(handle);
16802712Snn35248 			return (-1);
16812712Snn35248 		}
16822712Snn35248 		/* update the pointers passed in */
16832712Snn35248 		*fs_tabp = tmp_ptr;
16842712Snn35248 		*num_fsp = num_fs;
16852712Snn35248 
16862712Snn35248 		fs_ptr = tmp_ptr;
16872712Snn35248 		fsp = &fs_ptr[num_fs - 1];
16882712Snn35248 		(void) strlcpy(fsp->zone_fs_dir,
16892712Snn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16902712Snn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16912712Snn35248 		    sizeof (fsp->zone_fs_raw));
16922712Snn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16932712Snn35248 		    sizeof (fsp->zone_fs_type));
16942712Snn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
16953688Sedp 
16963688Sedp 		/*
16973688Sedp 		 * For all lofs mounts, make sure that the 'special'
16983688Sedp 		 * entry points inside the alternate root.  The
16993688Sedp 		 * source path for a lofs mount in a given zone needs
17003688Sedp 		 * to be relative to the root of the boot environment
17013688Sedp 		 * that contains the zone.  Note that we don't do this
17023688Sedp 		 * for non-lofs mounts since they will have a device
17033688Sedp 		 * as a backing store and device paths must always be
17043688Sedp 		 * specified relative to the current boot environment.
17053688Sedp 		 */
17063688Sedp 		fsp->zone_fs_special[0] = '\0';
17073688Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
17083688Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
17093688Sedp 			    sizeof (fsp->zone_fs_special));
17103688Sedp 		}
17113688Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
17123688Sedp 		    sizeof (fsp->zone_fs_special));
17132712Snn35248 	}
17142712Snn35248 	(void) zonecfg_endfsent(handle);
17152712Snn35248 	return (0);
17162712Snn35248 }
17172712Snn35248 
17180Sstevel@tonic-gate static int
17195829Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
17200Sstevel@tonic-gate {
17212712Snn35248 	char rootpath[MAXPATHLEN];
17222712Snn35248 	char zonepath[MAXPATHLEN];
17232712Snn35248 	char brand[MAXNAMELEN];
17243071Svp157776 	char luroot[MAXPATHLEN];
17252712Snn35248 	int i, num_fs = 0;
17262712Snn35248 	struct zone_fstab *fs_ptr = NULL;
17270Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
17280Sstevel@tonic-gate 	zone_state_t zstate;
17292727Sedp 	brand_handle_t bh;
17302712Snn35248 	plat_gmount_cb_data_t cb;
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1733766Scarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
17340Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1735766Scarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1736766Scarlsonj 		    zone_state_str(ZONE_STATE_READY),
1737766Scarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
17380Sstevel@tonic-gate 		goto bad;
17390Sstevel@tonic-gate 	}
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
17420Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
17430Sstevel@tonic-gate 		goto bad;
17440Sstevel@tonic-gate 	}
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
17470Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
17480Sstevel@tonic-gate 		goto bad;
17490Sstevel@tonic-gate 	}
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
17521645Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17530Sstevel@tonic-gate 		goto bad;
17540Sstevel@tonic-gate 	}
17550Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17560Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17570Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17580Sstevel@tonic-gate 		goto bad;
17590Sstevel@tonic-gate 	}
17600Sstevel@tonic-gate 
17617655Sgerald.jelinek@sun.com 	/*
1762*10943SEdward.Pilatowicz@Sun.COM 	 * If we are mounting the zone, then we must always use the default
17637655Sgerald.jelinek@sun.com 	 * brand global mounts.
17647655Sgerald.jelinek@sun.com 	 */
17657655Sgerald.jelinek@sun.com 	if (ALT_MOUNT(mount_cmd)) {
1766*10943SEdward.Pilatowicz@Sun.COM 		(void) strlcpy(brand, default_brand, sizeof (brand));
17677655Sgerald.jelinek@sun.com 	} else {
176810796SStephen.Lawrence@Sun.COM 		(void) strlcpy(brand, brand_name, sizeof (brand));
17697655Sgerald.jelinek@sun.com 	}
17707655Sgerald.jelinek@sun.com 
17712712Snn35248 	/* Get a handle to the brand info for this zone */
17727655Sgerald.jelinek@sun.com 	if ((bh = brand_open(brand)) == NULL) {
17732712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
17743716Sgjelinek 		zonecfg_fini_handle(handle);
17752712Snn35248 		return (-1);
17762712Snn35248 	}
17772712Snn35248 
17782712Snn35248 	/*
17792712Snn35248 	 * Get the list of global filesystems to mount from the brand
17802712Snn35248 	 * configuration.
17812712Snn35248 	 */
17822712Snn35248 	cb.pgcd_zlogp = zlogp;
17832712Snn35248 	cb.pgcd_fs_tab = &fs_ptr;
17842712Snn35248 	cb.pgcd_num_fs = &num_fs;
17852727Sedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17862712Snn35248 	    plat_gmount_cb, &cb) != 0) {
17872712Snn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
17882727Sedp 		brand_close(bh);
17893716Sgjelinek 		zonecfg_fini_handle(handle);
17902712Snn35248 		return (-1);
17912712Snn35248 	}
17922727Sedp 	brand_close(bh);
17932712Snn35248 
17940Sstevel@tonic-gate 	/*
17950Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
17960Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
17970Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
17980Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
17990Sstevel@tonic-gate 	 */
18002712Snn35248 	if (mount_filesystems_ipdent(handle, zlogp, &fs_ptr, &num_fs) != 0)
18010Sstevel@tonic-gate 		goto bad;
18022712Snn35248 
18032712Snn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
18042712Snn35248 	    mount_cmd) != 0)
18052712Snn35248 		goto bad;
18062712Snn35248 
18070Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
18080Sstevel@tonic-gate 	handle = NULL;
18090Sstevel@tonic-gate 
1810766Scarlsonj 	/*
18112712Snn35248 	 * Normally when we mount a zone all the zone filesystems
18122712Snn35248 	 * get mounted relative to rootpath, which is usually
18132712Snn35248 	 * <zonepath>/root.  But when mounting a zone for administration
18142712Snn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
18152712Snn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
18162712Snn35248 	 * the zones filesystems there instead.
18172712Snn35248 	 *
18182712Snn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
18192712Snn35248 	 * also do some extra work to create directories and lofs mount
18202712Snn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
18212712Snn35248 	 *
18222712Snn35248 	 * This allows us to be able to enter the zone (now rooted at
18232712Snn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
18242712Snn35248 	 * global zone and have them upgrade the to-be-modified zone's
18252712Snn35248 	 * files mounted on /a.  (Which mirrors the existing standard
18262712Snn35248 	 * upgrade environment.)
18272712Snn35248 	 *
18282712Snn35248 	 * There is of course one catch.  When doing the upgrade
18292712Snn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
18302712Snn35248 	 * for the zone and we don't want to have any /dev filesystem
18312712Snn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
18322712Snn35248 	 * as a normal zone filesystem by default we'll try to mount
18332712Snn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
18342712Snn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
18352712Snn35248 	 *
18362712Snn35248 	 * All this work is done in three phases:
18372653Svp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
18382653Svp157776 	 *   2) Mount the required filesystems as per the zone configuration.
18392653Svp157776 	 *   3) Set up the rest of the scratch zone environment
18402653Svp157776 	 *	(build_mounted_post_var()).
1841766Scarlsonj 	 */
18425829Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
18433071Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1844766Scarlsonj 		goto bad;
1845766Scarlsonj 
18460Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
18472712Snn35248 
18480Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
18495829Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
18502712Snn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
18512712Snn35248 			size_t slen = strlen(rootpath) - 2;
18522712Snn35248 
18532712Snn35248 			/*
18542712Snn35248 			 * By default we'll try to mount /dev as /a/dev
18552712Snn35248 			 * but /dev is special and always goes at the top
18562712Snn35248 			 * so strip the trailing '/a' from the rootpath.
18572712Snn35248 			 */
18582712Snn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18592712Snn35248 			rootpath[slen] = '\0';
18607655Sgerald.jelinek@sun.com 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18617655Sgerald.jelinek@sun.com 			    != 0)
18622712Snn35248 				goto bad;
18632712Snn35248 			rootpath[slen] = '/';
18642712Snn35248 			continue;
18652712Snn35248 		}
18667655Sgerald.jelinek@sun.com 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18670Sstevel@tonic-gate 			goto bad;
18680Sstevel@tonic-gate 	}
18695829Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18705829Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
18712653Svp157776 		goto bad;
18721676Sjpk 
18731676Sjpk 	/*
18741676Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
18751676Sjpk 	 */
18765829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18775829Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
18781676Sjpk 		goto bad;
18791676Sjpk 
18800Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 	/*
18830Sstevel@tonic-gate 	 * Everything looks fine.
18840Sstevel@tonic-gate 	 */
18850Sstevel@tonic-gate 	return (0);
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate bad:
18880Sstevel@tonic-gate 	if (handle != NULL)
18890Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18900Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18910Sstevel@tonic-gate 	return (-1);
18920Sstevel@tonic-gate }
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18950Sstevel@tonic-gate static int
18960Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18970Sstevel@tonic-gate {
18980Sstevel@tonic-gate 	int prefixlen;
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
19010Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
19020Sstevel@tonic-gate 		return (1);
19030Sstevel@tonic-gate 	while (prefixlen > 0) {
19040Sstevel@tonic-gate 		if (prefixlen >= 8) {
19050Sstevel@tonic-gate 			*maskstr++ = 0xFF;
19060Sstevel@tonic-gate 			prefixlen -= 8;
19070Sstevel@tonic-gate 			continue;
19080Sstevel@tonic-gate 		}
19090Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
19100Sstevel@tonic-gate 		prefixlen--;
19110Sstevel@tonic-gate 	}
19120Sstevel@tonic-gate 	return (0);
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate /*
19160Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
19170Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
19180Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
19190Sstevel@tonic-gate  *
19200Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
19210Sstevel@tonic-gate  */
19220Sstevel@tonic-gate static int
19233448Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
19240Sstevel@tonic-gate {
19250Sstevel@tonic-gate 	struct lifnum lifn;
19260Sstevel@tonic-gate 	struct lifconf lifc;
19270Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
19280Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
19290Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
19300Sstevel@tonic-gate 	uint_t bufsize;
19310Sstevel@tonic-gate 	char *buf = NULL;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
19340Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
19350Sstevel@tonic-gate 		ret_code = -1;
19360Sstevel@tonic-gate 		goto bad;
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
19390Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
19400Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
19410Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
19423448Sdh155122 		    "could not determine number of network interfaces");
19430Sstevel@tonic-gate 		ret_code = -1;
19440Sstevel@tonic-gate 		goto bad;
19450Sstevel@tonic-gate 	}
19460Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
19470Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
19480Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
19490Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
19500Sstevel@tonic-gate 		ret_code = -1;
19510Sstevel@tonic-gate 		goto bad;
19520Sstevel@tonic-gate 	}
19530Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
19540Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19550Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19560Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19570Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
19583448Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
19593448Sdh155122 		    "interfaces");
19600Sstevel@tonic-gate 		ret_code = -1;
19610Sstevel@tonic-gate 		goto bad;
19620Sstevel@tonic-gate 	}
19630Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19640Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19650Sstevel@tonic-gate 		(void) close(s);
19660Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19670Sstevel@tonic-gate 		    0) {
19680Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19690Sstevel@tonic-gate 			    lifrl.lifr_name);
19700Sstevel@tonic-gate 			ret_code = -1;
19710Sstevel@tonic-gate 			continue;
19720Sstevel@tonic-gate 		}
19730Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19740Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19750Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19760Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
19773251Ssl108498 			if (errno == ENXIO)
19783251Ssl108498 				/*
19793251Ssl108498 				 * Interface may have been removed by admin or
19803251Ssl108498 				 * another zone halting.
19813251Ssl108498 				 */
19823251Ssl108498 				continue;
19830Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
19843251Ssl108498 			    "%s: could not determine the zone to which this "
19853448Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19860Sstevel@tonic-gate 			ret_code = -1;
19870Sstevel@tonic-gate 			continue;
19880Sstevel@tonic-gate 		}
19890Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19900Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19910Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
19923448Sdh155122 				    "%s: could not remove network interface",
19930Sstevel@tonic-gate 				    lifrl.lifr_name);
19940Sstevel@tonic-gate 				ret_code = -1;
19950Sstevel@tonic-gate 				continue;
19960Sstevel@tonic-gate 			}
19970Sstevel@tonic-gate 		}
19980Sstevel@tonic-gate 	}
19990Sstevel@tonic-gate bad:
20000Sstevel@tonic-gate 	if (s > 0)
20010Sstevel@tonic-gate 		(void) close(s);
20020Sstevel@tonic-gate 	if (buf)
20030Sstevel@tonic-gate 		free(buf);
20040Sstevel@tonic-gate 	return (ret_code);
20050Sstevel@tonic-gate }
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate static union	sockunion {
20080Sstevel@tonic-gate 	struct	sockaddr sa;
20090Sstevel@tonic-gate 	struct	sockaddr_in sin;
20100Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
20110Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
20120Sstevel@tonic-gate } so_dst, so_ifp;
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate static struct {
20150Sstevel@tonic-gate 	struct	rt_msghdr hdr;
20160Sstevel@tonic-gate 	char	space[512];
20170Sstevel@tonic-gate } rtmsg;
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate static int
20200Sstevel@tonic-gate salen(struct sockaddr *sa)
20210Sstevel@tonic-gate {
20220Sstevel@tonic-gate 	switch (sa->sa_family) {
20230Sstevel@tonic-gate 	case AF_INET:
20240Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
20250Sstevel@tonic-gate 	case AF_LINK:
20260Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
20270Sstevel@tonic-gate 	case AF_INET6:
20280Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
20290Sstevel@tonic-gate 	default:
20300Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
20310Sstevel@tonic-gate 	}
20320Sstevel@tonic-gate }
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
20350Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate /*
20380Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
20390Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
20400Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
20410Sstevel@tonic-gate  *
20420Sstevel@tonic-gate  * This is done using black router socket magic.
20430Sstevel@tonic-gate  *
20440Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
20450Sstevel@tonic-gate  *
20460Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
20470Sstevel@tonic-gate  * of this might be a useful RFE.
20480Sstevel@tonic-gate  */
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate static char *
20510Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20520Sstevel@tonic-gate {
20530Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
20540Sstevel@tonic-gate 	pid_t pid;
20550Sstevel@tonic-gate 	int s, rlen, l, i;
20560Sstevel@tonic-gate 	char *cp = rtmsg.space;
20570Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20580Sstevel@tonic-gate 	struct sockaddr *sa;
20590Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	answer[0] = '\0';
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	pid = getpid();
20640Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20650Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20660Sstevel@tonic-gate 		return (NULL);
20670Sstevel@tonic-gate 	}
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20700Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20730Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20740Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20750Sstevel@tonic-gate 	} else {
20760Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20790Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20800Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20810Sstevel@tonic-gate 	}
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20860Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20870Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20880Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20890Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20900Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20930Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20940Sstevel@tonic-gate 	cp += l;
20950Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20960Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20970Sstevel@tonic-gate 	cp += l;
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
21020Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
21030Sstevel@tonic-gate 		return (NULL);
21040Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
21050Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
21060Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
21070Sstevel@tonic-gate 		return (NULL);
21080Sstevel@tonic-gate 	}
21090Sstevel@tonic-gate 	do {
21100Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
21110Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
21120Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
21130Sstevel@tonic-gate 	if (l < 0) {
21140Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
21150Sstevel@tonic-gate 		return (NULL);
21160Sstevel@tonic-gate 	}
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
21190Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
21200Sstevel@tonic-gate 		    "routing message version %d not understood",
21210Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
21220Sstevel@tonic-gate 		return (NULL);
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
21250Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
21260Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
21270Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
21280Sstevel@tonic-gate 		return (NULL);
21290Sstevel@tonic-gate 	}
21300Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
21310Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
21320Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
21330Sstevel@tonic-gate 		return (NULL);
21340Sstevel@tonic-gate 	}
21350Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
21363448Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
21370Sstevel@tonic-gate 		return (NULL);
21380Sstevel@tonic-gate 	}
21390Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
21400Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
21410Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
21420Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
21430Sstevel@tonic-gate 		if (i != RTA_IFP) {
21440Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
21450Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
21460Sstevel@tonic-gate 			continue;
21470Sstevel@tonic-gate 		}
21480Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
21490Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
21500Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
21510Sstevel@tonic-gate 		break;
21520Sstevel@tonic-gate 	}
21530Sstevel@tonic-gate 	if (ifp == NULL) {
21543448Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
21553448Sdh155122 		    "determined");
21560Sstevel@tonic-gate 		return (NULL);
21570Sstevel@tonic-gate 	}
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	/*
21600Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21610Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21620Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21630Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21640Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21650Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21660Sstevel@tonic-gate 	 */
21670Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21680Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21690Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21700Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21710Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21720Sstevel@tonic-gate 	if (i < 0) {
21730Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
21743448Sdh155122 		    "%s: could not determine the zone network interface "
21753448Sdh155122 		    "belongs to", lifr->lifr_name);
21760Sstevel@tonic-gate 		return (NULL);
21770Sstevel@tonic-gate 	}
21780Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21790Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21800Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	if (strlen(answer) > 0)
21830Sstevel@tonic-gate 		return (answer);
21840Sstevel@tonic-gate 	return (NULL);
21850Sstevel@tonic-gate }
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate /*
21880Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21890Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21900Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21910Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21920Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21930Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21940Sstevel@tonic-gate  *
21950Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21960Sstevel@tonic-gate  * down whatever we set up and return an error.
21970Sstevel@tonic-gate  */
21980Sstevel@tonic-gate static int
21990Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
22008058SJordan.Vaughan@Sun.com     struct zone_nwiftab *nwiftabptr)
22010Sstevel@tonic-gate {
22020Sstevel@tonic-gate 	struct lifreq lifr;
22030Sstevel@tonic-gate 	struct sockaddr_in netmask4;
22040Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
220510067SVamsi.Krishna@Sun.COM 	struct sockaddr_storage laddr;
22060Sstevel@tonic-gate 	struct in_addr in4;
22070Sstevel@tonic-gate 	sa_family_t af;
22080Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
22090Sstevel@tonic-gate 	int s;
22100Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
22119720SSaurabh.Vyas@Sun.COM 	boolean_t is_loopback = B_FALSE;
22120Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
22130Sstevel@tonic-gate 	int res;
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
22160Sstevel@tonic-gate 	if (res != Z_OK) {
22170Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
22180Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
22190Sstevel@tonic-gate 		return (-1);
22200Sstevel@tonic-gate 	}
22210Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
22220Sstevel@tonic-gate 	if (af == AF_INET)
22230Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
22240Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
22250Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
22260Sstevel@tonic-gate 		return (-1);
22270Sstevel@tonic-gate 	}
22280Sstevel@tonic-gate 
222910067SVamsi.Krishna@Sun.COM 	/*
223010067SVamsi.Krishna@Sun.COM 	 * This is a similar kind of "hack" like in addif() to get around
223110067SVamsi.Krishna@Sun.COM 	 * the problem of SIOCLIFADDIF.  The problem is that this ioctl
223210067SVamsi.Krishna@Sun.COM 	 * does not include the netmask when adding a logical interface.
223310067SVamsi.Krishna@Sun.COM 	 * To get around this problem, we first add the logical interface
223410067SVamsi.Krishna@Sun.COM 	 * with a 0 address.  After that, we set the netmask if provided.
223510067SVamsi.Krishna@Sun.COM 	 * Finally we set the interface address.
223610067SVamsi.Krishna@Sun.COM 	 */
223710067SVamsi.Krishna@Sun.COM 	laddr = lifr.lifr_addr;
22380Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
22390Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
224010067SVamsi.Krishna@Sun.COM 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
224110067SVamsi.Krishna@Sun.COM 
22420Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
22432611Svp157776 		/*
22442611Svp157776 		 * Here, we know that the interface can't be brought up.
22452611Svp157776 		 * A similar warning message was already printed out to
22462611Svp157776 		 * the console by zoneadm(1M) so instead we log the
22472611Svp157776 		 * message to syslog and continue.
22482611Svp157776 		 */
22493448Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
22502611Svp157776 		    "'%s' which may not be present/plumbed in the "
22512611Svp157776 		    "global zone.", lifr.lifr_name);
22520Sstevel@tonic-gate 		(void) close(s);
22532611Svp157776 		return (Z_OK);
22540Sstevel@tonic-gate 	}
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22570Sstevel@tonic-gate 	if (af == AF_INET)
22580Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22610Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
22623448Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
22633448Sdh155122 		    "into zone", lifr.lifr_name);
22640Sstevel@tonic-gate 		goto bad;
22650Sstevel@tonic-gate 	}
22660Sstevel@tonic-gate 
22679720SSaurabh.Vyas@Sun.COM 	/*
22689720SSaurabh.Vyas@Sun.COM 	 * Loopback interface will use the default netmask assigned, if no
22699720SSaurabh.Vyas@Sun.COM 	 * netmask is found.
22709720SSaurabh.Vyas@Sun.COM 	 */
22710Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
22729720SSaurabh.Vyas@Sun.COM 		is_loopback = B_TRUE;
22739720SSaurabh.Vyas@Sun.COM 	}
22749720SSaurabh.Vyas@Sun.COM 	if (af == AF_INET) {
22759720SSaurabh.Vyas@Sun.COM 		/*
22769720SSaurabh.Vyas@Sun.COM 		 * The IPv4 netmask can be determined either
22779720SSaurabh.Vyas@Sun.COM 		 * directly if a prefix length was supplied with
22789720SSaurabh.Vyas@Sun.COM 		 * the address or via the netmasks database.  Not
22799720SSaurabh.Vyas@Sun.COM 		 * being able to determine it is a common failure,
22809720SSaurabh.Vyas@Sun.COM 		 * but it often is not fatal to operation of the
22819720SSaurabh.Vyas@Sun.COM 		 * interface.  In that case, a warning will be
22829720SSaurabh.Vyas@Sun.COM 		 * printed after the rest of the interface's
22839720SSaurabh.Vyas@Sun.COM 		 * parameters have been configured.
22849720SSaurabh.Vyas@Sun.COM 		 */
22859720SSaurabh.Vyas@Sun.COM 		(void) memset(&netmask4, 0, sizeof (netmask4));
22869720SSaurabh.Vyas@Sun.COM 		if (slashp != NULL) {
22879720SSaurabh.Vyas@Sun.COM 			if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22889720SSaurabh.Vyas@Sun.COM 			    (uchar_t *)&netmask4.sin_addr) != 0) {
22890Sstevel@tonic-gate 				*slashp = '/';
22900Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22910Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22920Sstevel@tonic-gate 				    lifr.lifr_name,
22930Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22940Sstevel@tonic-gate 				goto bad;
22950Sstevel@tonic-gate 			}
22960Sstevel@tonic-gate 			got_netmask = B_TRUE;
22979720SSaurabh.Vyas@Sun.COM 		} else if (getnetmaskbyaddr(in4,
22989720SSaurabh.Vyas@Sun.COM 		    &netmask4.sin_addr) == 0) {
22999720SSaurabh.Vyas@Sun.COM 			got_netmask = B_TRUE;
23009720SSaurabh.Vyas@Sun.COM 		}
23019720SSaurabh.Vyas@Sun.COM 		if (got_netmask) {
23029720SSaurabh.Vyas@Sun.COM 			netmask4.sin_family = af;
23039720SSaurabh.Vyas@Sun.COM 			(void) memcpy(&lifr.lifr_addr, &netmask4,
23049720SSaurabh.Vyas@Sun.COM 			    sizeof (netmask4));
23050Sstevel@tonic-gate 		}
23069720SSaurabh.Vyas@Sun.COM 	} else {
23079720SSaurabh.Vyas@Sun.COM 		(void) memset(&netmask6, 0, sizeof (netmask6));
23089720SSaurabh.Vyas@Sun.COM 		if (addr2netmask(slashp + 1, V6_ADDR_LEN,
23099720SSaurabh.Vyas@Sun.COM 		    (uchar_t *)&netmask6.sin6_addr) != 0) {
23109720SSaurabh.Vyas@Sun.COM 			*slashp = '/';
23119720SSaurabh.Vyas@Sun.COM 			zerror(zlogp, B_FALSE,
23129720SSaurabh.Vyas@Sun.COM 			    "%s: invalid prefix length in %s",
23139720SSaurabh.Vyas@Sun.COM 			    lifr.lifr_name,
23149720SSaurabh.Vyas@Sun.COM 			    nwiftabptr->zone_nwif_address);
23150Sstevel@tonic-gate 			goto bad;
23160Sstevel@tonic-gate 		}
23179720SSaurabh.Vyas@Sun.COM 		got_netmask = B_TRUE;
23189720SSaurabh.Vyas@Sun.COM 		netmask6.sin6_family = af;
23199720SSaurabh.Vyas@Sun.COM 		(void) memcpy(&lifr.lifr_addr, &netmask6,
23209720SSaurabh.Vyas@Sun.COM 		    sizeof (netmask6));
23219720SSaurabh.Vyas@Sun.COM 	}
23229720SSaurabh.Vyas@Sun.COM 	if (got_netmask &&
23239720SSaurabh.Vyas@Sun.COM 	    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
23249720SSaurabh.Vyas@Sun.COM 		zerror(zlogp, B_TRUE, "%s: could not set netmask",
23259720SSaurabh.Vyas@Sun.COM 		    lifr.lifr_name);
23269720SSaurabh.Vyas@Sun.COM 		goto bad;
23279720SSaurabh.Vyas@Sun.COM 	}
23289720SSaurabh.Vyas@Sun.COM 
232910067SVamsi.Krishna@Sun.COM 	/* Set the interface address */
233010067SVamsi.Krishna@Sun.COM 	lifr.lifr_addr = laddr;
23319720SSaurabh.Vyas@Sun.COM 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
23329720SSaurabh.Vyas@Sun.COM 		zerror(zlogp, B_TRUE,
233310067SVamsi.Krishna@Sun.COM 		    "%s: could not set IP address to %s",
233410067SVamsi.Krishna@Sun.COM 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
23359720SSaurabh.Vyas@Sun.COM 		goto bad;
23360Sstevel@tonic-gate 	}
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23390Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
23400Sstevel@tonic-gate 		    lifr.lifr_name);
23410Sstevel@tonic-gate 		goto bad;
23420Sstevel@tonic-gate 	}
23430Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
23440Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
23450Sstevel@tonic-gate 		int save_errno = errno;
23460Sstevel@tonic-gate 		char *zone_using;
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 		/*
23490Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
23500Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
23510Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23520Sstevel@tonic-gate 		 * using that address.
23530Sstevel@tonic-gate 		 */
23540Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23550Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23563448Sdh155122 			    "%s: could not bring network interface up",
23573448Sdh155122 			    lifr.lifr_name);
23580Sstevel@tonic-gate 			goto bad;
23590Sstevel@tonic-gate 		}
23600Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23610Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23620Sstevel@tonic-gate 			    lifr.lifr_name);
23630Sstevel@tonic-gate 			goto bad;
23640Sstevel@tonic-gate 		}
23650Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23660Sstevel@tonic-gate 		errno = save_errno;
23670Sstevel@tonic-gate 		if (zone_using == NULL)
23680Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23693448Sdh155122 			    "%s: could not bring network interface up",
23703448Sdh155122 			    lifr.lifr_name);
23710Sstevel@tonic-gate 		else
23723448Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
23733448Sdh155122 			    "interface up: address in use by zone '%s'",
23743448Sdh155122 			    lifr.lifr_name, zone_using);
23750Sstevel@tonic-gate 		goto bad;
23760Sstevel@tonic-gate 	}
23770Sstevel@tonic-gate 
23789720SSaurabh.Vyas@Sun.COM 	if (!got_netmask && !is_loopback) {
23790Sstevel@tonic-gate 		/*
23800Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23810Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23820Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23830Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23840Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23850Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23860Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23870Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23880Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23890Sstevel@tonic-gate 		 * possible incorrect netmask.
23900Sstevel@tonic-gate 		 */
23910Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23920Sstevel@tonic-gate 		void  *addr;
23938485SPeter.Memishian@Sun.COM 		const char *nomatch = "no matching subnet found in netmasks(4)";
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 		if (af == AF_INET)
23960Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23970Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23980Sstevel@tonic-gate 		else
23990Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
24000Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
24010Sstevel@tonic-gate 
24028485SPeter.Memishian@Sun.COM 		/*
24038485SPeter.Memishian@Sun.COM 		 * Find out what netmask the interface is going to be using.
24048485SPeter.Memishian@Sun.COM 		 * If we just brought up an IPMP data address on an underlying
24058485SPeter.Memishian@Sun.COM 		 * interface above, the address will have already migrated, so
24068485SPeter.Memishian@Sun.COM 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
24078485SPeter.Memishian@Sun.COM 		 * to bring the address up to get the actual netmask).  Just
24088485SPeter.Memishian@Sun.COM 		 * omit printing the actual netmask in this corner-case.
24098485SPeter.Memishian@Sun.COM 		 */
24100Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
24118485SPeter.Memishian@Sun.COM 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
24128485SPeter.Memishian@Sun.COM 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
24138485SPeter.Memishian@Sun.COM 			    nomatch);
24148485SPeter.Memishian@Sun.COM 		} else {
24158485SPeter.Memishian@Sun.COM 			zerror(zlogp, B_FALSE,
24168485SPeter.Memishian@Sun.COM 			    "WARNING: %s: %s: %s; using default of %s.",
24178485SPeter.Memishian@Sun.COM 			    lifr.lifr_name, nomatch, addrstr4, buffer);
24188485SPeter.Memishian@Sun.COM 		}
24190Sstevel@tonic-gate 	}
24200Sstevel@tonic-gate 
24216076Sgfaden 	/*
24226076Sgfaden 	 * If a default router was specified for this interface
24236076Sgfaden 	 * set the route now. Ignore if already set.
24246076Sgfaden 	 */
24256076Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
24266076Sgfaden 		int status;
24276076Sgfaden 		char *argv[7];
24286076Sgfaden 
24296076Sgfaden 		argv[0] = "route";
24306076Sgfaden 		argv[1] = "add";
24316076Sgfaden 		argv[2] = "-ifp";
24326076Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
24336076Sgfaden 		argv[4] = "default";
24346076Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
24356076Sgfaden 		argv[6] = NULL;
24366076Sgfaden 
24376076Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
24386076Sgfaden 		if (status != 0 && status != EEXIST)
24396076Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
24406076Sgfaden 			    "interface %s to %s\n",
24416076Sgfaden 			    nwiftabptr->zone_nwif_physical,
24426076Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
24436076Sgfaden 	}
24446076Sgfaden 
24450Sstevel@tonic-gate 	(void) close(s);
24460Sstevel@tonic-gate 	return (Z_OK);
24470Sstevel@tonic-gate bad:
24480Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
24490Sstevel@tonic-gate 	(void) close(s);
24500Sstevel@tonic-gate 	return (-1);
24510Sstevel@tonic-gate }
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate /*
24540Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
24558058SJordan.Vaughan@Sun.com  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
24568058SJordan.Vaughan@Sun.com  * system.
24570Sstevel@tonic-gate  *
24580Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24590Sstevel@tonic-gate  * whatever we set up, and return an error.
24600Sstevel@tonic-gate  */
24610Sstevel@tonic-gate static int
24623448Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24630Sstevel@tonic-gate {
24640Sstevel@tonic-gate 	zone_dochandle_t handle;
24650Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24660Sstevel@tonic-gate 	zoneid_t zoneid;
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24690Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24700Sstevel@tonic-gate 		return (-1);
24710Sstevel@tonic-gate 	}
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24740Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24750Sstevel@tonic-gate 		return (-1);
24760Sstevel@tonic-gate 	}
24770Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24780Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24790Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24800Sstevel@tonic-gate 		return (-1);
24810Sstevel@tonic-gate 	}
24820Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24830Sstevel@tonic-gate 		for (;;) {
24840Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24850Sstevel@tonic-gate 				break;
24868058SJordan.Vaughan@Sun.com 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24870Sstevel@tonic-gate 			    Z_OK) {
24880Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24890Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24900Sstevel@tonic-gate 				return (-1);
24910Sstevel@tonic-gate 			}
24920Sstevel@tonic-gate 		}
24930Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24940Sstevel@tonic-gate 	}
24950Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
24965863Sgfaden 	if (is_system_labeled()) {
24975863Sgfaden 		/*
24985863Sgfaden 		 * Labeled zones share the loopback interface
24995863Sgfaden 		 * so it is not plumbed for shared stack instances.
25005863Sgfaden 		 */
25015863Sgfaden 		return (0);
25025863Sgfaden 	}
25030Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
25040Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
25050Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
25060Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
25076378Sgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
25088058SJordan.Vaughan@Sun.com 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
25090Sstevel@tonic-gate 		return (-1);
25108058SJordan.Vaughan@Sun.com 
25118058SJordan.Vaughan@Sun.com 	/* Always plumb up the IPv6 loopback interface. */
25128058SJordan.Vaughan@Sun.com 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
25138058SJordan.Vaughan@Sun.com 	    sizeof (loopback_iftab.zone_nwif_address));
25148058SJordan.Vaughan@Sun.com 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
25158058SJordan.Vaughan@Sun.com 		return (-1);
25160Sstevel@tonic-gate 	return (0);
25170Sstevel@tonic-gate }
25180Sstevel@tonic-gate 
25198878SPeter.Memishian@Sun.COM static void
25208878SPeter.Memishian@Sun.COM zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
25218878SPeter.Memishian@Sun.COM {
25228878SPeter.Memishian@Sun.COM 	char errmsg[DLADM_STRSIZE];
25238878SPeter.Memishian@Sun.COM 
25248878SPeter.Memishian@Sun.COM 	(void) dladm_status2str(err, errmsg);
25258878SPeter.Memishian@Sun.COM 	zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
25268878SPeter.Memishian@Sun.COM }
25278878SPeter.Memishian@Sun.COM 
25283448Sdh155122 static int
252910616SSebastien.Roy@Sun.COM add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
25303448Sdh155122 {
25318878SPeter.Memishian@Sun.COM 	dladm_status_t err;
25328878SPeter.Memishian@Sun.COM 
25333448Sdh155122 	/* First check if it's in use by global zone. */
25343448Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
25353448Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
25368878SPeter.Memishian@Sun.COM 		zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
25378878SPeter.Memishian@Sun.COM 		    "'%s' which is used in the global zone", dlname);
25383448Sdh155122 		return (-1);
25393448Sdh155122 	}
25403448Sdh155122 
25415895Syz147064 	/* Set zoneid of this link. */
254210616SSebastien.Roy@Sun.COM 	err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
254310616SSebastien.Roy@Sun.COM 	    DLADM_OPT_ACTIVE);
25448878SPeter.Memishian@Sun.COM 	if (err != DLADM_STATUS_OK) {
25458878SPeter.Memishian@Sun.COM 		zdlerror(zlogp, err, dlname,
25468878SPeter.Memishian@Sun.COM 		    "WARNING: unable to add network interface");
25473448Sdh155122 		return (-1);
25483448Sdh155122 	}
25493448Sdh155122 	return (0);
25503448Sdh155122 }
25513448Sdh155122 
25523448Sdh155122 /*
25533448Sdh155122  * Add the kernel access control information for the interface names.
25543448Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
25553448Sdh155122  * whatever we set up, and return an error.
25563448Sdh155122  */
25573448Sdh155122 static int
25583448Sdh155122 configure_exclusive_network_interfaces(zlog_t *zlogp)
25593448Sdh155122 {
25603448Sdh155122 	zone_dochandle_t handle;
25613448Sdh155122 	struct zone_nwiftab nwiftab;
25623448Sdh155122 	char rootpath[MAXPATHLEN];
25633448Sdh155122 	char path[MAXPATHLEN];
256410616SSebastien.Roy@Sun.COM 	datalink_id_t linkid;
25653448Sdh155122 	di_prof_t prof = NULL;
25663448Sdh155122 	boolean_t added = B_FALSE;
25673448Sdh155122 
25683448Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
25693448Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
25703448Sdh155122 		return (-1);
25713448Sdh155122 	}
25723448Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
25733448Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
25743448Sdh155122 		zonecfg_fini_handle(handle);
25753448Sdh155122 		return (-1);
25763448Sdh155122 	}
25773448Sdh155122 
25783448Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
25793448Sdh155122 		zonecfg_fini_handle(handle);
25803448Sdh155122 		return (0);
25813448Sdh155122 	}
25823448Sdh155122 
25833448Sdh155122 	for (;;) {
25843448Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
25853448Sdh155122 			break;
25863448Sdh155122 
25873448Sdh155122 		if (prof == NULL) {
25883448Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
25893448Sdh155122 			    sizeof (rootpath)) != Z_OK) {
25903448Sdh155122 				(void) zonecfg_endnwifent(handle);
25913448Sdh155122 				zonecfg_fini_handle(handle);
25923448Sdh155122 				zerror(zlogp, B_TRUE,
25933448Sdh155122 				    "unable to determine dev root");
25943448Sdh155122 				return (-1);
25953448Sdh155122 			}
25963448Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
25973448Sdh155122 			    "/dev");
25983448Sdh155122 			if (di_prof_init(path, &prof) != 0) {
25993448Sdh155122 				(void) zonecfg_endnwifent(handle);
26003448Sdh155122 				zonecfg_fini_handle(handle);
26013448Sdh155122 				zerror(zlogp, B_TRUE,
26023448Sdh155122 				    "failed to initialize profile");
26033448Sdh155122 				return (-1);
26043448Sdh155122 			}
26053448Sdh155122 		}
26063448Sdh155122 
26073448Sdh155122 		/*
26085895Syz147064 		 * Create the /dev entry for backward compatibility.
26093448Sdh155122 		 * Only create the /dev entry if it's not in use.
26105895Syz147064 		 * Note that the zone still boots when the assigned
26115895Syz147064 		 * interface is inaccessible, used by others, etc.
26125895Syz147064 		 * Also, when vanity naming is used, some interface do
26135895Syz147064 		 * do not have corresponding /dev node names (for example,
26145895Syz147064 		 * vanity named aggregations).  The /dev entry is not
26155895Syz147064 		 * created in that case.  The /dev/net entry is always
26165895Syz147064 		 * accessible.
26173448Sdh155122 		 */
261810616SSebastien.Roy@Sun.COM 		if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
261910616SSebastien.Roy@Sun.COM 		    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
262010616SSebastien.Roy@Sun.COM 		    add_datalink(zlogp, zone_name, linkid,
262110616SSebastien.Roy@Sun.COM 		    nwiftab.zone_nwif_physical) == 0) {
26227342SAruna.Ramakrishna@Sun.COM 			added = B_TRUE;
26237342SAruna.Ramakrishna@Sun.COM 		} else {
26247342SAruna.Ramakrishna@Sun.COM 			(void) zonecfg_endnwifent(handle);
26257342SAruna.Ramakrishna@Sun.COM 			zonecfg_fini_handle(handle);
26267342SAruna.Ramakrishna@Sun.COM 			zerror(zlogp, B_TRUE, "failed to add network device");
26277342SAruna.Ramakrishna@Sun.COM 			return (-1);
26283448Sdh155122 		}
26293448Sdh155122 	}
26303448Sdh155122 	(void) zonecfg_endnwifent(handle);
26313448Sdh155122 	zonecfg_fini_handle(handle);
26323448Sdh155122 
26333448Sdh155122 	if (prof != NULL && added) {
26343448Sdh155122 		if (di_prof_commit(prof) != 0) {
26353448Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
26363448Sdh155122 			return (-1);
26373448Sdh155122 		}
26383448Sdh155122 	}
26393448Sdh155122 	if (prof != NULL)
26403448Sdh155122 		di_prof_fini(prof);
26413448Sdh155122 
26423448Sdh155122 	return (0);
26433448Sdh155122 }
26443448Sdh155122 
26453448Sdh155122 static int
264610616SSebastien.Roy@Sun.COM unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
26473448Sdh155122 {
264810616SSebastien.Roy@Sun.COM 	int dlnum = 0;
264910616SSebastien.Roy@Sun.COM 
265010616SSebastien.Roy@Sun.COM 	/*
265110616SSebastien.Roy@Sun.COM 	 * The kernel shutdown callback for the dls module should have removed
265210616SSebastien.Roy@Sun.COM 	 * all datalinks from this zone.  If any remain, then there's a
265310616SSebastien.Roy@Sun.COM 	 * problem.
265410616SSebastien.Roy@Sun.COM 	 */
26553448Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
26563448Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
26573448Sdh155122 		return (-1);
26583448Sdh155122 	}
265910616SSebastien.Roy@Sun.COM 	if (dlnum != 0) {
266010616SSebastien.Roy@Sun.COM 		zerror(zlogp, B_FALSE,
266110616SSebastien.Roy@Sun.COM 		    "datalinks remain in zone after shutdown");
26623448Sdh155122 		return (-1);
26633448Sdh155122 	}
26643448Sdh155122 	return (0);
26653448Sdh155122 }
26663448Sdh155122 
26670Sstevel@tonic-gate static int
26680Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
26690Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
26700Sstevel@tonic-gate {
26710Sstevel@tonic-gate 	int fd;
26720Sstevel@tonic-gate 	struct strioctl ioc;
26730Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
26740Sstevel@tonic-gate 	int error;
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	conn.ac_local = *local;
26770Sstevel@tonic-gate 	conn.ac_remote = *remote;
26780Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
26790Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
26800Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
26830Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
26840Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
26850Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
26880Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
26890Sstevel@tonic-gate 		return (-1);
26900Sstevel@tonic-gate 	}
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
26930Sstevel@tonic-gate 	(void) close(fd);
26940Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
26950Sstevel@tonic-gate 		return (0);
26960Sstevel@tonic-gate 	return (-1);
26970Sstevel@tonic-gate }
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate static int
27000Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
27010Sstevel@tonic-gate {
27020Sstevel@tonic-gate 	struct sockaddr_storage l, r;
27030Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
27040Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
27050Sstevel@tonic-gate 	int error;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	/*
27080Sstevel@tonic-gate 	 * Abort IPv4 connections.
27090Sstevel@tonic-gate 	 */
27100Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
27110Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
27120Sstevel@tonic-gate 	local->sin_family = AF_INET;
27130Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
27140Sstevel@tonic-gate 	local->sin_port = 0;
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
27170Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
27180Sstevel@tonic-gate 	remote->sin_family = AF_INET;
27190Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
27200Sstevel@tonic-gate 	remote->sin_port = 0;
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
27230Sstevel@tonic-gate 		return (error);
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	/*
27260Sstevel@tonic-gate 	 * Abort IPv6 connections.
27270Sstevel@tonic-gate 	 */
27280Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
27290Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
27300Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
27310Sstevel@tonic-gate 	local6->sin6_port = 0;
27320Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
27350Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
27360Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
27370Sstevel@tonic-gate 	remote6->sin6_port = 0;
27380Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
27410Sstevel@tonic-gate 		return (error);
27420Sstevel@tonic-gate 	return (0);
27430Sstevel@tonic-gate }
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate static int
27465829Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
27471645Scomay {
27481645Scomay 	int error = -1;
27491645Scomay 	zone_dochandle_t handle;
27501645Scomay 	char *privname = NULL;
27511645Scomay 
27521645Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
27531645Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
27541645Scomay 		return (-1);
27551645Scomay 	}
27561645Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
27571645Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
27581645Scomay 		zonecfg_fini_handle(handle);
27591645Scomay 		return (-1);
27601645Scomay 	}
27611645Scomay 
27625829Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
27633673Sdh155122 		zone_iptype_t	iptype;
27643673Sdh155122 		const char	*curr_iptype;
27653673Sdh155122 
27663673Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
27673673Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
27683673Sdh155122 			zonecfg_fini_handle(handle);
27693673Sdh155122 			return (-1);
27703673Sdh155122 		}
27713673Sdh155122 
27723673Sdh155122 		switch (iptype) {
27733673Sdh155122 		case ZS_SHARED:
27743673Sdh155122 			curr_iptype = "shared";
27753673Sdh155122 			break;
27763673Sdh155122 		case ZS_EXCLUSIVE:
27773673Sdh155122 			curr_iptype = "exclusive";
27783673Sdh155122 			break;
27793673Sdh155122 		}
27803673Sdh155122 
27813716Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
27823716Sgjelinek 			zonecfg_fini_handle(handle);
27832712Snn35248 			return (0);
27843716Sgjelinek 		}
27852712Snn35248 		zerror(zlogp, B_FALSE,
27862712Snn35248 		    "failed to determine the zone's default privilege set");
27872712Snn35248 		zonecfg_fini_handle(handle);
27882712Snn35248 		return (-1);
27892712Snn35248 	}
27902712Snn35248 
27911645Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
27921645Scomay 	case Z_OK:
27931645Scomay 		error = 0;
27941645Scomay 		break;
27951645Scomay 	case Z_PRIV_PROHIBITED:
27961645Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
27971645Scomay 		    "within the zone's privilege set", privname);
27981645Scomay 		break;
27991645Scomay 	case Z_PRIV_REQUIRED:
28001645Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
28011645Scomay 		    "from the zone's privilege set", privname);
28021645Scomay 		break;
28031645Scomay 	case Z_PRIV_UNKNOWN:
28041645Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
28051645Scomay 		    "in the zone's privilege set", privname);
28061645Scomay 		break;
28071645Scomay 	default:
28081645Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
28091645Scomay 		    "privilege set");
28101645Scomay 		break;
28111645Scomay 	}
28121645Scomay 
28131645Scomay 	free(privname);
28141645Scomay 	zonecfg_fini_handle(handle);
28151645Scomay 	return (error);
28161645Scomay }
28171645Scomay 
28181645Scomay static int
28190Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
28200Sstevel@tonic-gate {
28210Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
28220Sstevel@tonic-gate 	char *nvl_packed = NULL;
28230Sstevel@tonic-gate 	size_t nvl_size = 0;
28240Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
28250Sstevel@tonic-gate 	int rctlcount = 0;
28260Sstevel@tonic-gate 	int error = -1;
28270Sstevel@tonic-gate 	zone_dochandle_t handle;
28280Sstevel@tonic-gate 	struct zone_rctltab rctltab;
28290Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 	*bufp = NULL;
28320Sstevel@tonic-gate 	*bufsizep = 0;
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
28350Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
28360Sstevel@tonic-gate 		return (-1);
28370Sstevel@tonic-gate 	}
28380Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
28390Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
28400Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
28410Sstevel@tonic-gate 		return (-1);
28420Sstevel@tonic-gate 	}
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
28450Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
28460Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
28470Sstevel@tonic-gate 		goto out;
28480Sstevel@tonic-gate 	}
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
28510Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
28520Sstevel@tonic-gate 		goto out;
28530Sstevel@tonic-gate 	}
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
28560Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
28570Sstevel@tonic-gate 		goto out;
28580Sstevel@tonic-gate 	}
28590Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
28600Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
28610Sstevel@tonic-gate 		uint_t i, count;
28620Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
28650Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
28660Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
28670Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
28680Sstevel@tonic-gate 			continue;
28690Sstevel@tonic-gate 		}
28700Sstevel@tonic-gate 		count = 0;
28710Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
28720Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
28730Sstevel@tonic-gate 			count++;
28740Sstevel@tonic-gate 		}
28750Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
28760Sstevel@tonic-gate 			continue;	/* Nothing to free */
28770Sstevel@tonic-gate 		}
28780Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
28790Sstevel@tonic-gate 			goto out;
28800Sstevel@tonic-gate 		i = 0;
28810Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
28820Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
28830Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
28840Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
28850Sstevel@tonic-gate 				    "nvlist_alloc");
28860Sstevel@tonic-gate 				goto out;
28870Sstevel@tonic-gate 			}
28880Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
28890Sstevel@tonic-gate 			    != Z_OK) {
28900Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
28910Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
28920Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
28930Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
28940Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
28950Sstevel@tonic-gate 				goto out;
28960Sstevel@tonic-gate 			}
28970Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
28980Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
28990Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
29000Sstevel@tonic-gate 				    "valid value for rctl '%s'",
29010Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
29020Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
29030Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
29040Sstevel@tonic-gate 				    name);
29050Sstevel@tonic-gate 				goto out;
29060Sstevel@tonic-gate 			}
29070Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
29081645Scomay 			    rctlblk_get_privilege(rctlblk)) != 0) {
29090Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29100Sstevel@tonic-gate 				    "nvlist_add_uint64");
29110Sstevel@tonic-gate 				goto out;
29120Sstevel@tonic-gate 			}
29130Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
29141645Scomay 			    rctlblk_get_value(rctlblk)) != 0) {
29150Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29160Sstevel@tonic-gate 				    "nvlist_add_uint64");
29170Sstevel@tonic-gate 				goto out;
29180Sstevel@tonic-gate 			}
29190Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
29200Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
29210Sstevel@tonic-gate 			    != 0) {
29220Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
29230Sstevel@tonic-gate 				    "nvlist_add_uint64");
29240Sstevel@tonic-gate 				goto out;
29250Sstevel@tonic-gate 			}
29260Sstevel@tonic-gate 		}
29270Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29280Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
29290Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
29300Sstevel@tonic-gate 		    != 0) {
29310Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
29320Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
29330Sstevel@tonic-gate 			goto out;
29340Sstevel@tonic-gate 		}
29350Sstevel@tonic-gate 		for (i = 0; i < count; i++)
29360Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
29370Sstevel@tonic-gate 		free(nvlv);
29380Sstevel@tonic-gate 		nvlv = NULL;
29390Sstevel@tonic-gate 		rctlcount++;
29400Sstevel@tonic-gate 	}
29410Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	if (rctlcount == 0) {
29440Sstevel@tonic-gate 		error = 0;
29450Sstevel@tonic-gate 		goto out;
29460Sstevel@tonic-gate 	}
29470Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
29480Sstevel@tonic-gate 	    != 0) {
29490Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
29500Sstevel@tonic-gate 		goto out;
29510Sstevel@tonic-gate 	}
29520Sstevel@tonic-gate 
29530Sstevel@tonic-gate 	error = 0;
29540Sstevel@tonic-gate 	*bufp = nvl_packed;
29550Sstevel@tonic-gate 	*bufsizep = nvl_size;
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate out:
29580Sstevel@tonic-gate 	free(rctlblk);
29590Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
29600Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
29610Sstevel@tonic-gate 		free(nvl_packed);
29620Sstevel@tonic-gate 	if (nvl != NULL)
29630Sstevel@tonic-gate 		nvlist_free(nvl);
29640Sstevel@tonic-gate 	if (nvlv != NULL)
29650Sstevel@tonic-gate 		free(nvlv);
29660Sstevel@tonic-gate 	if (handle != NULL)
29670Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
29680Sstevel@tonic-gate 	return (error);
29690Sstevel@tonic-gate }
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate static int
29727370S<Gerald Jelinek> get_implicit_datasets(zlog_t *zlogp, char **retstr)
29737370S<Gerald Jelinek> {
29747370S<Gerald Jelinek> 	char cmdbuf[2 * MAXPATHLEN];
29757370S<Gerald Jelinek> 
29767370S<Gerald Jelinek> 	if (query_hook[0] == '\0')
29777370S<Gerald Jelinek> 		return (0);
29787370S<Gerald Jelinek> 
29797370S<Gerald Jelinek> 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
29807370S<Gerald Jelinek> 	    > sizeof (cmdbuf))
29817370S<Gerald Jelinek> 		return (-1);
29827370S<Gerald Jelinek> 
29837370S<Gerald Jelinek> 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
29847370S<Gerald Jelinek> 		return (-1);
29857370S<Gerald Jelinek> 
29867370S<Gerald Jelinek> 	return (0);
29877370S<Gerald Jelinek> }
29887370S<Gerald Jelinek> 
29897370S<Gerald Jelinek> static int
2990789Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
2991789Sahrens {
2992789Sahrens 	zone_dochandle_t handle;
2993789Sahrens 	struct zone_dstab dstab;
2994789Sahrens 	size_t total, offset, len;
2995789Sahrens 	int error = -1;
29965185Sgjelinek 	char *str = NULL;
29977370S<Gerald Jelinek> 	char *implicit_datasets = NULL;
29987370S<Gerald Jelinek> 	int implicit_len = 0;
2999789Sahrens 
3000789Sahrens 	*bufp = NULL;
3001789Sahrens 	*bufsizep = 0;
3002789Sahrens 
3003789Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3004789Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3005789Sahrens 		return (-1);
3006789Sahrens 	}
3007789Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3008789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3009789Sahrens 		zonecfg_fini_handle(handle);
3010789Sahrens 		return (-1);
3011789Sahrens 	}
3012789Sahrens 
30137370S<Gerald Jelinek> 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
30147370S<Gerald Jelinek> 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
30157370S<Gerald Jelinek> 		goto out;
30167370S<Gerald Jelinek> 	}
30177370S<Gerald Jelinek> 
3018789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3019789Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3020789Sahrens 		goto out;
3021789Sahrens 	}
3022789Sahrens 
3023789Sahrens 	total = 0;
3024789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3025789Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3026789Sahrens 	(void) zonecfg_enddsent(handle);
3027789Sahrens 
30287370S<Gerald Jelinek> 	if (implicit_datasets != NULL)
30297370S<Gerald Jelinek> 		implicit_len = strlen(implicit_datasets);
30307370S<Gerald Jelinek> 	if (implicit_len > 0)
30317370S<Gerald Jelinek> 		total += implicit_len + 1;
30327370S<Gerald Jelinek> 
3033789Sahrens 	if (total == 0) {
3034789Sahrens 		error = 0;
3035789Sahrens 		goto out;
3036789Sahrens 	}
3037789Sahrens 
3038789Sahrens 	if ((str = malloc(total)) == NULL) {
3039789Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3040789Sahrens 		goto out;
3041789Sahrens 	}
3042789Sahrens 
3043789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3044789Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3045789Sahrens 		goto out;
3046789Sahrens 	}
3047789Sahrens 	offset = 0;
3048789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3049789Sahrens 		len = strlen(dstab.zone_dataset_name);
3050789Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
30515185Sgjelinek 		    total - offset);
3052789Sahrens 		offset += len;
30535185Sgjelinek 		if (offset < total - 1)
3054789Sahrens 			str[offset++] = ',';
3055789Sahrens 	}
3056789Sahrens 	(void) zonecfg_enddsent(handle);
3057789Sahrens 
30587370S<Gerald Jelinek> 	if (implicit_len > 0)
30597370S<Gerald Jelinek> 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
30607370S<Gerald Jelinek> 
3061789Sahrens 	error = 0;
3062789Sahrens 	*bufp = str;
3063789Sahrens 	*bufsizep = total;
3064789Sahrens 
3065789Sahrens out:
3066789Sahrens 	if (error != 0 && str != NULL)
3067789Sahrens 		free(str);
3068789Sahrens 	if (handle != NULL)
3069789Sahrens 		zonecfg_fini_handle(handle);
30707370S<Gerald Jelinek> 	if (implicit_datasets != NULL)
30717370S<Gerald Jelinek> 		free(implicit_datasets);
3072789Sahrens 
3073789Sahrens 	return (error);
3074789Sahrens }
3075789Sahrens 
3076789Sahrens static int
3077789Sahrens validate_datasets(zlog_t *zlogp)
3078789Sahrens {
3079789Sahrens 	zone_dochandle_t handle;
3080789Sahrens 	struct zone_dstab dstab;
3081789Sahrens 	zfs_handle_t *zhp;
30822082Seschrock 	libzfs_handle_t *hdl;
3083789Sahrens 
3084789Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3085789Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3086789Sahrens 		return (-1);
3087789Sahrens 	}
3088789Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3089789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3090789Sahrens 		zonecfg_fini_handle(handle);
3091789Sahrens 		return (-1);
3092789Sahrens 	}
3093789Sahrens 
3094789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3095789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3096789Sahrens 		zonecfg_fini_handle(handle);
3097789Sahrens 		return (-1);
3098789Sahrens 	}
3099789Sahrens 
31002082Seschrock 	if ((hdl = libzfs_init()) == NULL) {
31012082Seschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
31022082Seschrock 		zonecfg_fini_handle(handle);
31032082Seschrock 		return (-1);
31042082Seschrock 	}
3105789Sahrens 
3106789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3107789Sahrens 
31082082Seschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3109789Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3110789Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3111789Sahrens 			    dstab.zone_dataset_name);
3112789Sahrens 			zonecfg_fini_handle(handle);
31132082Seschrock 			libzfs_fini(hdl);
3114789Sahrens 			return (-1);
3115789Sahrens 		}
3116789Sahrens 
3117789Sahrens 		/*
3118789Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3119789Sahrens 		 * first because we'll get EPERM if it is already set.
3120789Sahrens 		 */
3121789Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
31222676Seschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
31232676Seschrock 		    "on") != 0) {
3124789Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3125789Sahrens 			    "property for ZFS dataset '%s'\n",
3126789Sahrens 			    dstab.zone_dataset_name);
3127789Sahrens 			zonecfg_fini_handle(handle);
3128789Sahrens 			zfs_close(zhp);
31292082Seschrock 			libzfs_fini(hdl);
3130789Sahrens 			return (-1);
3131789Sahrens 		}
3132789Sahrens 
3133789Sahrens 		zfs_close(zhp);
3134789Sahrens 	}
3135789Sahrens 	(void) zonecfg_enddsent(handle);
3136789Sahrens 
3137789Sahrens 	zonecfg_fini_handle(handle);
31382082Seschrock 	libzfs_fini(hdl);
3139789Sahrens 
3140789Sahrens 	return (0);
3141789Sahrens }
3142789Sahrens 
31431676Sjpk /*
31441676Sjpk  * Mount lower level home directories into/from current zone
31451676Sjpk  * Share exported directories specified in dfstab for zone
31461676Sjpk  */
31471676Sjpk static int
31481676Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
31491676Sjpk {
31501676Sjpk 	zoneid_t *zids = NULL;
31511676Sjpk 	priv_set_t *zid_privs;
31521676Sjpk 	const priv_impl_info_t *ip = NULL;
31531676Sjpk 	uint_t nzents_saved;
31541676Sjpk 	uint_t nzents;
31551676Sjpk 	int i;
31561676Sjpk 	char readonly[] = "ro";
31571676Sjpk 	struct zone_fstab lower_fstab;
31581676Sjpk 	char *argv[4];
31591676Sjpk 
31601676Sjpk 	if (!is_system_labeled())
31611676Sjpk 		return (0);
31621676Sjpk 
31631676Sjpk 	if (zid_label == NULL) {
31641676Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
31651676Sjpk 		if (zid_label == NULL)
31661676Sjpk 			return (-1);
31671676Sjpk 	}
31681676Sjpk 
31691676Sjpk 	/* Make sure our zone has an /export/home dir */
31701676Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
31713813Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
31721676Sjpk 
31731676Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
31741676Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
31751676Sjpk 	    sizeof (lower_fstab.zone_fs_type));
31761676Sjpk 	lower_fstab.zone_fs_options = NULL;
31771676Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
31781676Sjpk 
31791676Sjpk 	/*
31801676Sjpk 	 * Get the list of zones from the kernel
31811676Sjpk 	 */
31821676Sjpk 	if (zone_list(NULL, &nzents) != 0) {
31831676Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
31841676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
31851676Sjpk 		return (-1);
31861676Sjpk 	}
31871676Sjpk again:
31881676Sjpk 	if (nzents == 0) {
31891676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
31901676Sjpk 		return (-1);
31911676Sjpk 	}
31921676Sjpk 
31931676Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
31941676Sjpk 	if (zids == NULL) {
31952267Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
31961676Sjpk 		return (-1);
31971676Sjpk 	}
31981676Sjpk 	nzents_saved = nzents;
31991676Sjpk 
32001676Sjpk 	if (zone_list(zids, &nzents) != 0) {
32011676Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
32021676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
32031676Sjpk 		free(zids);
32041676Sjpk 		return (-1);
32051676Sjpk 	}
32061676Sjpk 	if (nzents != nzents_saved) {
32071676Sjpk 		/* list changed, try again */
32081676Sjpk 		free(zids);
32091676Sjpk 		goto again;
32101676Sjpk 	}
32111676Sjpk 
32121676Sjpk 	ip = getprivimplinfo();
32131676Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
32141676Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
32151676Sjpk 		zonecfg_free_fs_option_list(
32161676Sjpk 		    lower_fstab.zone_fs_options);
32171676Sjpk 		free(zids);
32181676Sjpk 		return (-1);
32191676Sjpk 	}
32201676Sjpk 
32211676Sjpk 	for (i = 0; i < nzents; i++) {
32221676Sjpk 		char zid_name[ZONENAME_MAX];
32231676Sjpk 		zone_state_t zid_state;
32241676Sjpk 		char zid_rpath[MAXPATHLEN];
32251676Sjpk 		struct stat stat_buf;
32261676Sjpk 
32271676Sjpk 		if (zids[i] == GLOBAL_ZONEID)
32281676Sjpk 			continue;
32291676Sjpk 
32301676Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
32311676Sjpk 			continue;
32321676Sjpk 
32331676Sjpk 		/*
32341676Sjpk 		 * Do special setup for the zone we are booting
32351676Sjpk 		 */
32361676Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
32371676Sjpk 			struct zone_fstab autofs_fstab;
32381676Sjpk 			char map_path[MAXPATHLEN];
32391676Sjpk 			int fd;
32401676Sjpk 
32411676Sjpk 			/*
32421676Sjpk 			 * Create auto_home_<zone> map for this zone
32432712Snn35248 			 * in the global zone. The non-global zone entry
32441676Sjpk 			 * will be created by automount when the zone
32451676Sjpk 			 * is booted.
32461676Sjpk 			 */
32471676Sjpk 
32481676Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
32491676Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
32501676Sjpk 
32511676Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
32521676Sjpk 			    "/zone/%s/home", zid_name);
32531676Sjpk 
32541676Sjpk 			(void) snprintf(map_path, sizeof (map_path),
32551676Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
32561676Sjpk 			/*
32571676Sjpk 			 * If the map file doesn't exist create a template
32581676Sjpk 			 */
32591676Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
32601676Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
32611676Sjpk 				int len;
32621676Sjpk 				char map_rec[MAXPATHLEN];
32631676Sjpk 
32641676Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
32651676Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
32661676Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
32671676Sjpk 				(void) write(fd, map_rec, len);
32681676Sjpk 				(void) close(fd);
32691676Sjpk 			}
32701676Sjpk 
32711676Sjpk 			/*
32721676Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
32731676Sjpk 			 * If it's already of type autofs, then
32741676Sjpk 			 * don't mount it again.
32751676Sjpk 			 */
32761676Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
32771676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
32781676Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
32791676Sjpk 
32801676Sjpk 				(void) make_one_dir(zlogp, "",
32813813Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
32823813Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
32831676Sjpk 
32841676Sjpk 				/*
32851676Sjpk 				 * Mount will fail if automounter has already
32861676Sjpk 				 * processed the auto_home_<zonename> map
32871676Sjpk 				 */
32881676Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
32891676Sjpk 				    autofs_fstab.zone_fs_special,
32901676Sjpk 				    autofs_fstab.zone_fs_dir);
32911676Sjpk 			}
32921676Sjpk 			continue;
32931676Sjpk 		}
32941676Sjpk 
32951676Sjpk 
32961676Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
32971769Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
32981769Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
32991676Sjpk 			/* Skip over zones without mounted filesystems */
33001676Sjpk 			continue;
33011676Sjpk 
33021676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
33031676Sjpk 		    sizeof (m_label_t)) < 0)
33041676Sjpk 			/* Skip over zones with unspecified label */
33051676Sjpk 			continue;
33061676Sjpk 
33071676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
33081676Sjpk 		    sizeof (zid_rpath)) == -1)
33091676Sjpk 			/* Skip over zones with bad path */
33101676Sjpk 			continue;
33111676Sjpk 
33121676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
33131676Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
33141676Sjpk 			/* Skip over zones with bad privs */
33151676Sjpk 			continue;
33161676Sjpk 
33171676Sjpk 		/*
33181676Sjpk 		 * Reading down is valid according to our label model
33191676Sjpk 		 * but some customers want to disable it because it
33201676Sjpk 		 * allows execute down and other possible attacks.
33211676Sjpk 		 * Therefore, we restrict this feature to zones that
33221676Sjpk 		 * have the NET_MAC_AWARE privilege which is required
33231676Sjpk 		 * for NFS read-down semantics.
33241676Sjpk 		 */
33251676Sjpk 		if ((bldominates(zlabel, zid_label)) &&
33261676Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
33271676Sjpk 			/*
33281676Sjpk 			 * Our zone dominates this one.
33291676Sjpk 			 * Create a lofs mount from lower zone's /export/home
33301676Sjpk 			 */
33311676Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
33321676Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
33331676Sjpk 
33341676Sjpk 			/*
33351676Sjpk 			 * If the target is already an LOFS mount
33361676Sjpk 			 * then don't do it again.
33371676Sjpk 			 */
33381676Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
33391676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
33401676Sjpk 
33411676Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
33421676Sjpk 				    MAXPATHLEN, "%s/export",
33431676Sjpk 				    zid_rpath) > MAXPATHLEN)
33441676Sjpk 					continue;
33451676Sjpk 
33461676Sjpk 				/*
33471676Sjpk 				 * Make sure the lower-level home exists
33481676Sjpk 				 */
33491676Sjpk 				if (make_one_dir(zlogp,
33503813Sdp 				    lower_fstab.zone_fs_special, "/home",
33513813Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
33523813Sdp 				    DEFAULT_DIR_GROUP) != 0)
33531676Sjpk 					continue;
33541676Sjpk 
33551676Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
33561676Sjpk 				    "/home", MAXPATHLEN);
33571676Sjpk 
33581676Sjpk 				/*
33591676Sjpk 				 * Mount can fail because the lower-level
33601676Sjpk 				 * zone may have already done a mount up.
33611676Sjpk 				 */
33627655Sgerald.jelinek@sun.com 				(void) mount_one(zlogp, &lower_fstab, "",
33637655Sgerald.jelinek@sun.com 				    Z_MNT_BOOT);
33641676Sjpk 			}
33651676Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
33661676Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
33671676Sjpk 			/*
33681676Sjpk 			 * This zone dominates our zone.
33691676Sjpk 			 * Create a lofs mount from our zone's /export/home
33701676Sjpk 			 */
33711676Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
33721676Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
33731676Sjpk 			    zone_name) > MAXPATHLEN)
33741676Sjpk 				continue;
33751676Sjpk 
33761676Sjpk 			/*
33771676Sjpk 			 * If the target is already an LOFS mount
33781676Sjpk 			 * then don't do it again.
33791676Sjpk 			 */
33801676Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
33811676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
33821676Sjpk 
33831676Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
33841676Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
33851676Sjpk 
33861676Sjpk 				/*
33871676Sjpk 				 * Mount can fail because the higher-level
33881676Sjpk 				 * zone may have already done a mount down.
33891676Sjpk 				 */
33907655Sgerald.jelinek@sun.com 				(void) mount_one(zlogp, &lower_fstab, "",
33917655Sgerald.jelinek@sun.com 				    Z_MNT_BOOT);
33921676Sjpk 			}
33931676Sjpk 		}
33941676Sjpk 	}
33951676Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
33961676Sjpk 	priv_freeset(zid_privs);
33971676Sjpk 	free(zids);
33981676Sjpk 
33991676Sjpk 	/*
34001676Sjpk 	 * Now share any exported directories from this zone.
34011676Sjpk 	 * Each zone can have its own dfstab.
34021676Sjpk 	 */
34031676Sjpk 
34041676Sjpk 	argv[0] = "zoneshare";
34051676Sjpk 	argv[1] = "-z";
34061676Sjpk 	argv[2] = zone_name;
34071676Sjpk 	argv[3] = NULL;
34081676Sjpk 
34091676Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
34101676Sjpk 	/* Don't check for errors since they don't affect the zone */
34111676Sjpk 
34121676Sjpk 	return (0);
34131676Sjpk }
34141676Sjpk 
34151676Sjpk /*
34161676Sjpk  * Unmount lofs mounts from higher level zones
34171676Sjpk  * Unshare nfs exported directories
34181676Sjpk  */
34191676Sjpk static void
34201676Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
34211676Sjpk {
34221676Sjpk 	zoneid_t *zids = NULL;
34231676Sjpk 	uint_t nzents_saved;
34241676Sjpk 	uint_t nzents;
34251676Sjpk 	int i;
34261676Sjpk 	char *argv[4];
34271676Sjpk 	char path[MAXPATHLEN];
34281676Sjpk 
34291676Sjpk 	if (!is_system_labeled())
34301676Sjpk 		return;
34311676Sjpk 
34321676Sjpk 	/*
34331676Sjpk 	 * Get the list of zones from the kernel
34341676Sjpk 	 */
34351676Sjpk 	if (zone_list(NULL, &nzents) != 0) {
34361676Sjpk 		return;
34371676Sjpk 	}
34381676Sjpk 
34391676Sjpk 	if (zid_label == NULL) {
34401676Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
34411676Sjpk 		if (zid_label == NULL)
34421676Sjpk 			return;
34431676Sjpk 	}
34441676Sjpk 
34451676Sjpk again:
34461676Sjpk 	if (nzents == 0)
34471676Sjpk 		return;
34481676Sjpk 
34491676Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
34501676Sjpk 	if (zids == NULL) {
34512267Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
34521676Sjpk 		return;
34531676Sjpk 	}
34541676Sjpk 	nzents_saved = nzents;
34551676Sjpk 
34561676Sjpk 	if (zone_list(zids, &nzents) != 0) {
34571676Sjpk 		free(zids);
34581676Sjpk 		return;
34591676Sjpk 	}
34601676Sjpk 	if (nzents != nzents_saved) {
34611676Sjpk 		/* list changed, try again */
34621676Sjpk 		free(zids);
34631676Sjpk 		goto again;
34641676Sjpk 	}
34651676Sjpk 
34661676Sjpk 	for (i = 0; i < nzents; i++) {
34671676Sjpk 		char zid_name[ZONENAME_MAX];
34681676Sjpk 		zone_state_t zid_state;
34691676Sjpk 		char zid_rpath[MAXPATHLEN];
34701676Sjpk 
34711676Sjpk 		if (zids[i] == GLOBAL_ZONEID)
34721676Sjpk 			continue;
34731676Sjpk 
34741676Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
34751676Sjpk 			continue;
34761676Sjpk 
34771676Sjpk 		/*
34781676Sjpk 		 * Skip the zone we are halting
34791676Sjpk 		 */
34801676Sjpk 		if (strcmp(zid_name, zone_name) == 0)
34811676Sjpk 			continue;
34821676Sjpk 
34831676Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
34841676Sjpk 		    sizeof (zid_state)) < 0) ||
34851676Sjpk 		    (zid_state < ZONE_IS_READY))
34861676Sjpk 			/* Skip over zones without mounted filesystems */
34871676Sjpk 			continue;
34881676Sjpk 
34891676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
34901676Sjpk 		    sizeof (m_label_t)) < 0)
34911676Sjpk 			/* Skip over zones with unspecified label */
34921676Sjpk 			continue;
34931676Sjpk 
34941676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
34951676Sjpk 		    sizeof (zid_rpath)) == -1)
34961676Sjpk 			/* Skip over zones with bad path */
34971676Sjpk 			continue;
34981676Sjpk 
34991676Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
35001676Sjpk 			/*
35011676Sjpk 			 * This zone dominates our zone.
35021676Sjpk 			 * Unmount the lofs mount of our zone's /export/home
35031676Sjpk 			 */
35041676Sjpk 
35051676Sjpk 			if (snprintf(path, MAXPATHLEN,
35061676Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
35071676Sjpk 			    zone_name) > MAXPATHLEN)
35081676Sjpk 				continue;
35091676Sjpk 
35101676Sjpk 			/* Skip over mount failures */
35111676Sjpk 			(void) umount(path);
35121676Sjpk 		}
35131676Sjpk 	}
35141676Sjpk 	free(zids);
35151676Sjpk 
35161676Sjpk 	/*
35171676Sjpk 	 * Unmount global zone autofs trigger for this zone
35181676Sjpk 	 */
35191676Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
35201676Sjpk 	/* Skip over mount failures */
35211676Sjpk 	(void) umount(path);
35221676Sjpk 
35231676Sjpk 	/*
35241676Sjpk 	 * Next unshare any exported directories from this zone.
35251676Sjpk 	 */
35261676Sjpk 
35271676Sjpk 	argv[0] = "zoneunshare";
35281676Sjpk 	argv[1] = "-z";
35291676Sjpk 	argv[2] = zone_name;
35301676Sjpk 	argv[3] = NULL;
35311676Sjpk 
35321676Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
35331676Sjpk 	/* Don't check for errors since they don't affect the zone */
35341676Sjpk 
35351676Sjpk 	/*
35361676Sjpk 	 * Finally, deallocate any devices in the zone.
35371676Sjpk 	 */
35381676Sjpk 
35391676Sjpk 	argv[0] = "deallocate";
35401676Sjpk 	argv[1] = "-Isz";
35411676Sjpk 	argv[2] = zone_name;
35421676Sjpk 	argv[3] = NULL;
35431676Sjpk 
35441676Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
35451676Sjpk 	/* Don't check for errors since they don't affect the zone */
35461676Sjpk }
35471676Sjpk 
35481676Sjpk /*
35491676Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
35501676Sjpk  * this zone.
35511676Sjpk  */
35521676Sjpk static tsol_zcent_t *
35531676Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
35541676Sjpk {
35551676Sjpk 	FILE *fp;
35561676Sjpk 	tsol_zcent_t *zcent = NULL;
35571676Sjpk 	char line[MAXTNZLEN];
35581676Sjpk 
35591676Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
35601676Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
35611676Sjpk 		return (NULL);
35621676Sjpk 	}
35631676Sjpk 
35641676Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
35651676Sjpk 		/*
35661676Sjpk 		 * Check for malformed database
35671676Sjpk 		 */
35681676Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
35691676Sjpk 			break;
35701676Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
35711676Sjpk 			continue;
35721676Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
35731676Sjpk 			break;
35741676Sjpk 		tsol_freezcent(zcent);
35751676Sjpk 		zcent = NULL;
35761676Sjpk 	}
35771676Sjpk 	(void) fclose(fp);
35781676Sjpk 
35791676Sjpk 	if (zcent == NULL) {
35801676Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
35811676Sjpk 		    "See tnzonecfg(4)");
35821676Sjpk 	} else {
35831676Sjpk 		if (zlabel == NULL)
35841676Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
35851676Sjpk 		/*
35861676Sjpk 		 * Save this zone's privileges for later read-down processing
35871676Sjpk 		 */
35881676Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
35891676Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
35901676Sjpk 			return (NULL);
35911676Sjpk 		} else {
35921676Sjpk 			priv_copyset(privs, zprivs);
35931676Sjpk 		}
35941676Sjpk 	}
35951676Sjpk 	return (zcent);
35961676Sjpk }
35971676Sjpk 
35981676Sjpk /*
35991676Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
36001676Sjpk  */
36011676Sjpk static void
36021676Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
36031676Sjpk {
36041676Sjpk 	tsol_mlp_t *mlp;
36051676Sjpk 	tsol_mlpent_t tsme;
36061676Sjpk 
36071676Sjpk 	if (!is_system_labeled())
36081676Sjpk 		return;
36091676Sjpk 
36101676Sjpk 	tsme.tsme_zoneid = zoneid;
36111676Sjpk 	tsme.tsme_flags = 0;
36121676Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
36131676Sjpk 		tsme.tsme_mlp = *mlp;
36141676Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
36151676Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
36161676Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
36171676Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
36181676Sjpk 		}
36191676Sjpk 	}
36201676Sjpk 
36211676Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
36221676Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
36231676Sjpk 		tsme.tsme_mlp = *mlp;
36241676Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
36251676Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
36261676Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
36271676Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
36281676Sjpk 		}
36291676Sjpk 	}
36301676Sjpk }
36311676Sjpk 
36321676Sjpk static void
36331676Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
36341676Sjpk {
36351676Sjpk 	tsol_mlpent_t tsme;
36361676Sjpk 
36371676Sjpk 	if (!is_system_labeled())
36381676Sjpk 		return;
36391676Sjpk 
36401676Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
36411676Sjpk 	tsme.tsme_zoneid = zoneid;
36421676Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
36431676Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
36441676Sjpk }
36451676Sjpk 
36460Sstevel@tonic-gate int
36470Sstevel@tonic-gate prtmount(const char *fs, void *x) {
36480Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
36490Sstevel@tonic-gate 	return (0);
36500Sstevel@tonic-gate }
36510Sstevel@tonic-gate 
3652766Scarlsonj /*
3653766Scarlsonj  * Look for zones running on the main system that are using this root (or any
3654766Scarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
3655766Scarlsonj  * is found or if we can't tell.
3656766Scarlsonj  */
3657766Scarlsonj static boolean_t
3658766Scarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
36590Sstevel@tonic-gate {
3660766Scarlsonj 	zoneid_t *zids = NULL;
3661766Scarlsonj 	uint_t nzids = 0;
3662766Scarlsonj 	boolean_t retv;
3663766Scarlsonj 	int rlen, zlen;
3664766Scarlsonj 	char zroot[MAXPATHLEN];
3665766Scarlsonj 	char zonename[ZONENAME_MAX];
3666766Scarlsonj 
3667766Scarlsonj 	for (;;) {
3668766Scarlsonj 		nzids += 10;
3669766Scarlsonj 		zids = malloc(nzids * sizeof (*zids));
3670766Scarlsonj 		if (zids == NULL) {
36712267Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
3672766Scarlsonj 			return (B_TRUE);
3673766Scarlsonj 		}
3674766Scarlsonj 		if (zone_list(zids, &nzids) == 0)
3675766Scarlsonj 			break;
3676766Scarlsonj 		free(zids);
3677766Scarlsonj 	}
3678766Scarlsonj 	retv = B_FALSE;
3679766Scarlsonj 	rlen = strlen(rootpath);
3680766Scarlsonj 	while (nzids > 0) {
3681766Scarlsonj 		/*
3682766Scarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
3683766Scarlsonj 		 * while we were busy.
3684766Scarlsonj 		 */
3685766Scarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
3686766Scarlsonj 		    sizeof (zroot)) == -1)
3687766Scarlsonj 			continue;
3688766Scarlsonj 		zlen = strlen(zroot);
3689766Scarlsonj 		if (zlen > rlen)
3690766Scarlsonj 			zlen = rlen;
3691766Scarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
3692766Scarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
3693766Scarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
3694766Scarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
3695766Scarlsonj 			    sizeof (zonename)) == -1)
3696766Scarlsonj 				(void) snprintf(zonename, sizeof (zonename),
3697766Scarlsonj 				    "id %d", (int)zids[nzids]);
3698766Scarlsonj 			zerror(zlogp, B_FALSE,
3699766Scarlsonj 			    "zone root %s already in use by zone %s",
3700766Scarlsonj 			    rootpath, zonename);
3701766Scarlsonj 			retv = B_TRUE;
3702766Scarlsonj 			break;
3703766Scarlsonj 		}
3704766Scarlsonj 	}
3705766Scarlsonj 	free(zids);
3706766Scarlsonj 	return (retv);
3707766Scarlsonj }
3708766Scarlsonj 
3709766Scarlsonj /*
3710766Scarlsonj  * Search for loopback mounts that use this same source node (same device and
3711766Scarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
3712766Scarlsonj  */
3713766Scarlsonj static boolean_t
3714766Scarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
3715766Scarlsonj {
3716766Scarlsonj 	struct stat64 rst, zst;
3717766Scarlsonj 	struct mnttab *mnp;
3718766Scarlsonj 
3719766Scarlsonj 	if (stat64(rootpath, &rst) == -1) {
3720766Scarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
3721766Scarlsonj 		return (B_TRUE);
3722766Scarlsonj 	}
3723766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
3724766Scarlsonj 		return (B_TRUE);
3725766Scarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
3726766Scarlsonj 		if (mnp->mnt_fstype == NULL ||
3727766Scarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
3728766Scarlsonj 			continue;
3729766Scarlsonj 		/* We're looking at a loopback mount.  Stat it. */
3730766Scarlsonj 		if (mnp->mnt_special != NULL &&
3731766Scarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
3732766Scarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
3733766Scarlsonj 			zerror(zlogp, B_FALSE,
3734766Scarlsonj 			    "zone root %s is reachable through %s",
3735766Scarlsonj 			    rootpath, mnp->mnt_mountp);
3736766Scarlsonj 			return (B_TRUE);
3737766Scarlsonj 		}
3738766Scarlsonj 	}
3739766Scarlsonj 	return (B_FALSE);
3740766Scarlsonj }
3741766Scarlsonj 
37423247Sgjelinek /*
37433247Sgjelinek  * Set memory cap and pool info for the zone's resource management
37443247Sgjelinek  * configuration.
37453247Sgjelinek  */
37463247Sgjelinek static int
37473247Sgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
37483247Sgjelinek {
37493247Sgjelinek 	int res;
37503247Sgjelinek 	uint64_t tmp;
37513247Sgjelinek 	struct zone_mcaptab mcap;
37523247Sgjelinek 	char sched[MAXNAMELEN];
37533247Sgjelinek 	zone_dochandle_t handle = NULL;
37543247Sgjelinek 	char pool_err[128];
37553247Sgjelinek 
37563247Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
37573247Sgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
37583247Sgjelinek 		return (Z_BAD_HANDLE);
37593247Sgjelinek 	}
37603247Sgjelinek 
37613247Sgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
37623247Sgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
37633247Sgjelinek 		zonecfg_fini_handle(handle);
37643247Sgjelinek 		return (res);
37653247Sgjelinek 	}
37663247Sgjelinek 
37673247Sgjelinek 	/*
37683247Sgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
37693247Sgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
37703247Sgjelinek 	 */
37713247Sgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
37723247Sgjelinek 		uint64_t num;
37733247Sgjelinek 		char smf_err[128];
37743247Sgjelinek 
37753247Sgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
37763247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
37773247Sgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
37783247Sgjelinek 			zonecfg_fini_handle(handle);
37793247Sgjelinek 			return (Z_INVAL);
37803247Sgjelinek 		}
37813247Sgjelinek 
37823247Sgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
37833247Sgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
37843247Sgjelinek 			    "failed: %s", smf_err);
37853247Sgjelinek 			zonecfg_fini_handle(handle);
37863247Sgjelinek 			return (Z_INVAL);
37873247Sgjelinek 		}
37883247Sgjelinek 	}
37893247Sgjelinek 
37903247Sgjelinek 	/* Get the scheduling class set in the zone configuration. */
37913247Sgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
37923247Sgjelinek 	    strlen(sched) > 0) {
37933247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
37943247Sgjelinek 		    strlen(sched)) == -1)
37953247Sgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
37963247Sgjelinek 			    "default scheduling class");
37973247Sgjelinek 
37983247Sgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
37993247Sgjelinek 	    == Z_OK) {
38003247Sgjelinek 		/*
38013247Sgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
38023247Sgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
38033247Sgjelinek 		 * zone.  Check what scheduling class the zone would be running
38043247Sgjelinek 		 * in by default so we can print a warning and modify the class
38053247Sgjelinek 		 * if we wouldn't be using FSS.
38063247Sgjelinek 		 */
38073247Sgjelinek 		char class_name[PC_CLNMSZ];
38083247Sgjelinek 
38093247Sgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
38103247Sgjelinek 		    sizeof (class_name)) != Z_OK) {
38113247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
38123247Sgjelinek 			    "the zone's scheduling class");
38133247Sgjelinek 
38143247Sgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
38153247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
38163247Sgjelinek 			    "rctl is set but\nFSS is not the default "
38173247Sgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
38183247Sgjelinek 			    "used for processes\nin the zone but to get the "
38193247Sgjelinek 			    "full benefit of FSS,\nit should be the default "
38203247Sgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
38213247Sgjelinek 			    "details.");
38223247Sgjelinek 
38233247Sgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
38243247Sgjelinek 			    strlen("FSS")) == -1)
38253247Sgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
38263247Sgjelinek 				    "zone scheduling class to FSS");
38273247Sgjelinek 		}
38283247Sgjelinek 	}
38293247Sgjelinek 
38303247Sgjelinek 	/*
38313247Sgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
38323247Sgjelinek 	 * well as persistent pools.  In all cases we call the functions
38333247Sgjelinek 	 * unconditionally.  Within each funtion the code will check if the
38343247Sgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
38353247Sgjelinek 	 * and just return if there is nothing to do.
38363247Sgjelinek 	 *
38373247Sgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
38383247Sgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
38393247Sgjelinek 	 * right thing in all cases (reuse or create) based on the current
38403247Sgjelinek 	 * zonecfg.
38413247Sgjelinek 	 */
38423247Sgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
38433247Sgjelinek 	    sizeof (pool_err))) != Z_OK) {
38443247Sgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
38453247Sgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
38463247Sgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
38473247Sgjelinek 			    pool_err);
38483247Sgjelinek 		else
38493247Sgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
38503247Sgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
38513247Sgjelinek 		zonecfg_fini_handle(handle);
38523247Sgjelinek 		return (Z_POOL_BIND);
38533247Sgjelinek 	}
38543247Sgjelinek 
38553247Sgjelinek 	/*
38563247Sgjelinek 	 * Check if we need to warn about poold not being enabled.
38573247Sgjelinek 	 */
38583247Sgjelinek 	if (zonecfg_warn_poold(handle)) {
38593247Sgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
38603247Sgjelinek 		    "been specified\nbut the dynamic pool service is not "
38613247Sgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
38623247Sgjelinek 		    "processor allocation within the specified range\n"
38633247Sgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
38643247Sgjelinek 		    "See poold(1M).");
38653247Sgjelinek 	}
38663247Sgjelinek 
38673247Sgjelinek 	/* The following is a warning, not an error. */
38683247Sgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
38693247Sgjelinek 	    sizeof (pool_err))) != Z_OK) {
38703247Sgjelinek 		if (res == Z_POOL_BIND)
38713247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
38723247Sgjelinek 			    "pool '%s'; using default pool.", pool_err);
38733247Sgjelinek 		else if (res == Z_POOL)
38743247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
38753247Sgjelinek 			    zonecfg_strerror(res), pool_err);
38763247Sgjelinek 		else
38773247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
38783247Sgjelinek 			    zonecfg_strerror(res));
38793247Sgjelinek 	}
38803247Sgjelinek 
38813247Sgjelinek 	zonecfg_fini_handle(handle);
38823247Sgjelinek 	return (Z_OK);
38833247Sgjelinek }
38843247Sgjelinek 
38858662SJordan.Vaughan@Sun.com /*
38868662SJordan.Vaughan@Sun.com  * Sets the hostid of the new zone based on its configured value.  The zone's
38878662SJordan.Vaughan@Sun.com  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
38888662SJordan.Vaughan@Sun.com  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
38898662SJordan.Vaughan@Sun.com  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
38908662SJordan.Vaughan@Sun.com  * ID of the new zone.
38918662SJordan.Vaughan@Sun.com  *
38928662SJordan.Vaughan@Sun.com  * This function returns zero on success and a nonzero error code on failure.
38938662SJordan.Vaughan@Sun.com  */
38948662SJordan.Vaughan@Sun.com static int
38958662SJordan.Vaughan@Sun.com setup_zone_hostid(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
38968662SJordan.Vaughan@Sun.com {
38978662SJordan.Vaughan@Sun.com 	int res;
38988662SJordan.Vaughan@Sun.com 	zone_dochandle_t handle;
38998662SJordan.Vaughan@Sun.com 	char hostidp[HW_HOSTID_LEN];
39008662SJordan.Vaughan@Sun.com 	unsigned int hostid;
39018662SJordan.Vaughan@Sun.com 
39028662SJordan.Vaughan@Sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
39038662SJordan.Vaughan@Sun.com 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
39048662SJordan.Vaughan@Sun.com 		return (Z_BAD_HANDLE);
39058662SJordan.Vaughan@Sun.com 	}
39068662SJordan.Vaughan@Sun.com 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
39078662SJordan.Vaughan@Sun.com 		zerror(zlogp, B_FALSE, "invalid configuration");
39088662SJordan.Vaughan@Sun.com 		zonecfg_fini_handle(handle);
39098662SJordan.Vaughan@Sun.com 		return (res);
39108662SJordan.Vaughan@Sun.com 	}
39118662SJordan.Vaughan@Sun.com 
39128662SJordan.Vaughan@Sun.com 	if ((res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp))) ==
39138662SJordan.Vaughan@Sun.com 	    Z_OK) {
39148662SJordan.Vaughan@Sun.com 		if (zonecfg_valid_hostid(hostidp) != Z_OK) {
39158662SJordan.Vaughan@Sun.com 			zerror(zlogp, B_FALSE,
39168662SJordan.Vaughan@Sun.com 			    "zone hostid is not valid: %s", hostidp);
39178662SJordan.Vaughan@Sun.com 			zonecfg_fini_handle(handle);
39188662SJordan.Vaughan@Sun.com 			return (Z_HOSTID_FUBAR);
39198662SJordan.Vaughan@Sun.com 		}
39208662SJordan.Vaughan@Sun.com 		hostid = (unsigned int)strtoul(hostidp, NULL, 16);
39218662SJordan.Vaughan@Sun.com 		if (zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
39228662SJordan.Vaughan@Sun.com 		    sizeof (hostid)) != 0) {
39238662SJordan.Vaughan@Sun.com 			zerror(zlogp, B_TRUE,
39248662SJordan.Vaughan@Sun.com 			    "zone hostid is not valid: %s", hostidp);
39258662SJordan.Vaughan@Sun.com 			zonecfg_fini_handle(handle);
39268662SJordan.Vaughan@Sun.com 			return (Z_SYSTEM);
39278662SJordan.Vaughan@Sun.com 		}
39288662SJordan.Vaughan@Sun.com 	} else if (res != Z_BAD_PROPERTY) {
39298662SJordan.Vaughan@Sun.com 		/*
39308662SJordan.Vaughan@Sun.com 		 * Z_BAD_PROPERTY is an acceptable error value (from
39318662SJordan.Vaughan@Sun.com 		 * zonecfg_get_hostid()) because it indicates that the zone
39328662SJordan.Vaughan@Sun.com 		 * doesn't have a hostid.
39338662SJordan.Vaughan@Sun.com 		 */
39348662SJordan.Vaughan@Sun.com 		if (res == Z_TOO_BIG)
39358662SJordan.Vaughan@Sun.com 			zerror(zlogp, B_FALSE, "hostid string in zone "
39368662SJordan.Vaughan@Sun.com 			    "configuration is too large.");
39378662SJordan.Vaughan@Sun.com 		else
39388662SJordan.Vaughan@Sun.com 			zerror(zlogp, B_TRUE, "fetching zone hostid from "
39398662SJordan.Vaughan@Sun.com 			    "configuration");
39408662SJordan.Vaughan@Sun.com 		zonecfg_fini_handle(handle);
39418662SJordan.Vaughan@Sun.com 		return (res);
39428662SJordan.Vaughan@Sun.com 	}
39438662SJordan.Vaughan@Sun.com 
39448662SJordan.Vaughan@Sun.com 	zonecfg_fini_handle(handle);
39458662SJordan.Vaughan@Sun.com 	return (Z_OK);
39468662SJordan.Vaughan@Sun.com }
39478662SJordan.Vaughan@Sun.com 
3948766Scarlsonj zoneid_t
39495829Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
3950766Scarlsonj {
3951766Scarlsonj 	zoneid_t rval = -1;
39520Sstevel@tonic-gate 	priv_set_t *privs;
39530Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
39540Sstevel@tonic-gate 	char *rctlbuf = NULL;
3955766Scarlsonj 	size_t rctlbufsz = 0;
3956789Sahrens 	char *zfsbuf = NULL;
3957789Sahrens 	size_t zfsbufsz = 0;
3958766Scarlsonj 	zoneid_t zoneid = -1;
39590Sstevel@tonic-gate 	int xerr;
3960766Scarlsonj 	char *kzone;
3961766Scarlsonj 	FILE *fp = NULL;
39621676Sjpk 	tsol_zcent_t *zcent = NULL;
39631676Sjpk 	int match = 0;
39641676Sjpk 	int doi = 0;
39653448Sdh155122 	int flags;
39663448Sdh155122 	zone_iptype_t iptype;
39670Sstevel@tonic-gate 
39680Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
39690Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
39700Sstevel@tonic-gate 		return (-1);
39710Sstevel@tonic-gate 	}
3972766Scarlsonj 	if (zonecfg_in_alt_root())
3973766Scarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
39740Sstevel@tonic-gate 
397510616SSebastien.Roy@Sun.COM 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
39763448Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
39773448Sdh155122 		return (-1);
39783448Sdh155122 	}
39793448Sdh155122 	switch (iptype) {
39803448Sdh155122 	case ZS_SHARED:
39813448Sdh155122 		flags = 0;
39823448Sdh155122 		break;
39833448Sdh155122 	case ZS_EXCLUSIVE:
39843448Sdh155122 		flags = ZCF_NET_EXCL;
39853448Sdh155122 		break;
39863448Sdh155122 	}
39873448Sdh155122 
39880Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
39890Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
39900Sstevel@tonic-gate 		return (-1);
39910Sstevel@tonic-gate 	}
39920Sstevel@tonic-gate 	priv_emptyset(privs);
39931645Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
39940Sstevel@tonic-gate 		goto error;
39951645Scomay 
39965829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
39975829Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
39980Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
39990Sstevel@tonic-gate 		goto error;
40000Sstevel@tonic-gate 	}
40011645Scomay 
4002789Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4003789Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4004789Sahrens 		goto error;
4005789Sahrens 	}
40060Sstevel@tonic-gate 
40075829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
40081676Sjpk 		zcent = get_zone_label(zlogp, privs);
40091769Scarlsonj 		if (zcent != NULL) {
40101676Sjpk 			match = zcent->zc_match;
40111676Sjpk 			doi = zcent->zc_doi;
40121676Sjpk 			*zlabel = zcent->zc_label;
40131676Sjpk 		} else {
40141676Sjpk 			goto error;
40151676Sjpk 		}
40161676Sjpk 	}
40171676Sjpk 
4018766Scarlsonj 	kzone = zone_name;
4019766Scarlsonj 
4020766Scarlsonj 	/*
4021766Scarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4022766Scarlsonj 	 * main system that are using this root (or any subdirectory of it).
4023766Scarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4024766Scarlsonj 	 * that use this same source node (same device and inode).
4025766Scarlsonj 	 */
4026766Scarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4027766Scarlsonj 		goto error;
4028766Scarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4029766Scarlsonj 		goto error;
4030766Scarlsonj 
40315829Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4032766Scarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4033766Scarlsonj 
4034766Scarlsonj 		/*
4035766Scarlsonj 		 * Forge up a special root for this zone.  When a zone is
4036766Scarlsonj 		 * mounted, we can't let the zone have its own root because the
4037766Scarlsonj 		 * tools that will be used in this "scratch zone" need access
4038766Scarlsonj 		 * to both the zone's resources and the running machine's
4039766Scarlsonj 		 * executables.
4040766Scarlsonj 		 *
4041766Scarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4042766Scarlsonj 		 */
4043766Scarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4044766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4045766Scarlsonj 			goto error;
4046766Scarlsonj 		}
4047766Scarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4048766Scarlsonj 			goto error;
4049766Scarlsonj 	}
4050766Scarlsonj 
4051766Scarlsonj 	if (zonecfg_in_alt_root()) {
4052766Scarlsonj 		/*
4053766Scarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4054766Scarlsonj 		 * then we have some additional work to do before starting the
4055766Scarlsonj 		 * zone.  First, resolve the root path down so that we're not
4056766Scarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4057766Scarlsonj 		 * the zone.
4058766Scarlsonj 		 */
4059766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4060766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4061766Scarlsonj 			goto error;
4062766Scarlsonj 		}
4063766Scarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4064766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4065766Scarlsonj 			goto error;
4066766Scarlsonj 		}
4067766Scarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4068766Scarlsonj 		    NULL, 0) == 0) {
4069766Scarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4070766Scarlsonj 			goto error;
4071766Scarlsonj 		}
4072766Scarlsonj 		/* This is the preferred name */
4073766Scarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4074766Scarlsonj 		    zone_name);
4075766Scarlsonj 		srandom(getpid());
4076766Scarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4077766Scarlsonj 		    0) == 0) {
4078766Scarlsonj 			/* This is just an arbitrary name; note "." usage */
4079766Scarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4080766Scarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4081766Scarlsonj 		}
4082766Scarlsonj 		kzone = kernzone;
4083766Scarlsonj 	}
4084766Scarlsonj 
40850Sstevel@tonic-gate 	xerr = 0;
4086766Scarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
40873448Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
40883448Sdh155122 	    flags)) == -1) {
40890Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
40900Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
40910Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
40920Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
40930Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
40940Sstevel@tonic-gate 			} else {
40950Sstevel@tonic-gate 
40960Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
40970Sstevel@tonic-gate 				    "These file-systems are mounted on "
40980Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
40990Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
41000Sstevel@tonic-gate 				    prtmount, zlogp);
41010Sstevel@tonic-gate 			}
41020Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
41030Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
41040Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
41050Sstevel@tonic-gate 			    "environment", "zone_create");
41064791Ston 		} else if (xerr == ZE_LABELINUSE) {
41074791Ston 			char zonename[ZONENAME_MAX];
41084791Ston 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
41094791Ston 			    zonename, ZONENAME_MAX);
41104791Ston 			zerror(zlogp, B_FALSE, "The zone label is already "
41114791Ston 			    "used by the zone '%s'.", zonename);
41120Sstevel@tonic-gate 		} else {
41130Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
41140Sstevel@tonic-gate 		}
41150Sstevel@tonic-gate 		goto error;
41160Sstevel@tonic-gate 	}
4117766Scarlsonj 
4118766Scarlsonj 	if (zonecfg_in_alt_root() &&
4119766Scarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4120766Scarlsonj 	    zonecfg_get_root()) == -1) {
4121766Scarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4122766Scarlsonj 		goto error;
4123766Scarlsonj 	}
4124766Scarlsonj 
41250Sstevel@tonic-gate 	/*
41263247Sgjelinek 	 * The following actions are not performed when merely mounting a zone
41273247Sgjelinek 	 * for administrative use.
41280Sstevel@tonic-gate 	 */
41295829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
41307655Sgerald.jelinek@sun.com 		brand_handle_t bh;
41317655Sgerald.jelinek@sun.com 		struct brand_attr attr;
41327655Sgerald.jelinek@sun.com 		char modname[MAXPATHLEN];
41337655Sgerald.jelinek@sun.com 
41348662SJordan.Vaughan@Sun.com 		if (setup_zone_hostid(zlogp, zone_name, zoneid) != Z_OK)
41358662SJordan.Vaughan@Sun.com 			goto error;
41368662SJordan.Vaughan@Sun.com 
413710796SStephen.Lawrence@Sun.COM 		if ((bh = brand_open(brand_name)) == NULL) {
41387655Sgerald.jelinek@sun.com 			zerror(zlogp, B_FALSE,
41397655Sgerald.jelinek@sun.com 			    "unable to determine brand name");
41408662SJordan.Vaughan@Sun.com 			goto error;
41417655Sgerald.jelinek@sun.com 		}
41427655Sgerald.jelinek@sun.com 
41438905SRic.Aleshire@Sun.COM 		if (!is_system_labeled() &&
414410796SStephen.Lawrence@Sun.COM 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
41458905SRic.Aleshire@Sun.COM 			brand_close(bh);
41468905SRic.Aleshire@Sun.COM 			zerror(zlogp, B_FALSE,
41478905SRic.Aleshire@Sun.COM 			    "cannot boot labeled zone on unlabeled system");
41488905SRic.Aleshire@Sun.COM 			goto error;
41498905SRic.Aleshire@Sun.COM 		}
41508905SRic.Aleshire@Sun.COM 
41517655Sgerald.jelinek@sun.com 		/*
41527655Sgerald.jelinek@sun.com 		 * If this brand requires any kernel support, now is the time to
41537655Sgerald.jelinek@sun.com 		 * get it loaded and initialized.
41547655Sgerald.jelinek@sun.com 		 */
41557655Sgerald.jelinek@sun.com 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
41567655Sgerald.jelinek@sun.com 			brand_close(bh);
41577655Sgerald.jelinek@sun.com 			zerror(zlogp, B_FALSE,
41587655Sgerald.jelinek@sun.com 			    "unable to determine brand kernel module");
41598662SJordan.Vaughan@Sun.com 			goto error;
41607655Sgerald.jelinek@sun.com 		}
41617655Sgerald.jelinek@sun.com 		brand_close(bh);
41627655Sgerald.jelinek@sun.com 
41637655Sgerald.jelinek@sun.com 		if (strlen(modname) > 0) {
416410796SStephen.Lawrence@Sun.COM 			(void) strlcpy(attr.ba_brandname, brand_name,
416510796SStephen.Lawrence@Sun.COM 			    sizeof (attr.ba_brandname));
416610796SStephen.Lawrence@Sun.COM 			(void) strlcpy(attr.ba_modname, modname,
416710796SStephen.Lawrence@Sun.COM 			    sizeof (attr.ba_modname));
41687655Sgerald.jelinek@sun.com 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
41697655Sgerald.jelinek@sun.com 			    sizeof (attr) != 0)) {
41707655Sgerald.jelinek@sun.com 				zerror(zlogp, B_TRUE,
41717655Sgerald.jelinek@sun.com 				    "could not set zone brand attribute.");
41727655Sgerald.jelinek@sun.com 				goto error;
41737655Sgerald.jelinek@sun.com 			}
41747655Sgerald.jelinek@sun.com 		}
41757655Sgerald.jelinek@sun.com 
41768662SJordan.Vaughan@Sun.com 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
41773247Sgjelinek 			goto error;
41783247Sgjelinek 
41791769Scarlsonj 		set_mlps(zlogp, zoneid, zcent);
41803247Sgjelinek 	}
41813247Sgjelinek 
4182766Scarlsonj 	rval = zoneid;
4183766Scarlsonj 	zoneid = -1;
4184766Scarlsonj 
41850Sstevel@tonic-gate error:
41868662SJordan.Vaughan@Sun.com 	if (zoneid != -1) {
41878662SJordan.Vaughan@Sun.com 		(void) zone_shutdown(zoneid);
4188766Scarlsonj 		(void) zone_destroy(zoneid);
41898662SJordan.Vaughan@Sun.com 	}
41900Sstevel@tonic-gate 	if (rctlbuf != NULL)
41910Sstevel@tonic-gate 		free(rctlbuf);
41920Sstevel@tonic-gate 	priv_freeset(privs);
4193766Scarlsonj 	if (fp != NULL)
4194766Scarlsonj 		zonecfg_close_scratch(fp);
4195766Scarlsonj 	lofs_discard_mnttab();
41961676Sjpk 	if (zcent != NULL)
41971676Sjpk 		tsol_freezcent(zcent);
41980Sstevel@tonic-gate 	return (rval);
41990Sstevel@tonic-gate }
42000Sstevel@tonic-gate 
42012303Scarlsonj /*
42022303Scarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
42032303Scarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
42042303Scarlsonj  * details from inside the zone.
42052303Scarlsonj  */
42062303Scarlsonj static void
42072303Scarlsonj write_index_file(zoneid_t zoneid)
42082303Scarlsonj {
42092303Scarlsonj 	FILE *zef;
42102303Scarlsonj 	FILE *zet;
42112303Scarlsonj 	struct zoneent *zep;
42122303Scarlsonj 	pid_t child;
42132303Scarlsonj 	int tmpl_fd;
42142303Scarlsonj 	ctid_t ct;
42152303Scarlsonj 	int fd;
42162303Scarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
42172303Scarlsonj 
42182303Scarlsonj 	/* Locate the zone entry in the global zone's index file */
42192303Scarlsonj 	if ((zef = setzoneent()) == NULL)
42202303Scarlsonj 		return;
42212303Scarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
42222303Scarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
42232303Scarlsonj 			break;
42242303Scarlsonj 		free(zep);
42252303Scarlsonj 	}
42262303Scarlsonj 	endzoneent(zef);
42272303Scarlsonj 	if (zep == NULL)
42282303Scarlsonj 		return;
42292303Scarlsonj 
42302303Scarlsonj 	if ((tmpl_fd = init_template()) == -1) {
42312303Scarlsonj 		free(zep);
42322303Scarlsonj 		return;
42332303Scarlsonj 	}
42342303Scarlsonj 
42352303Scarlsonj 	if ((child = fork()) == -1) {
42362303Scarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
42372303Scarlsonj 		(void) close(tmpl_fd);
42382303Scarlsonj 		free(zep);
42392303Scarlsonj 		return;
42402303Scarlsonj 	}
42412303Scarlsonj 
42422303Scarlsonj 	/* parent waits for child to finish */
42432303Scarlsonj 	if (child != 0) {
42442303Scarlsonj 		free(zep);
42452303Scarlsonj 		if (contract_latest(&ct) == -1)
42462303Scarlsonj 			ct = -1;
42472303Scarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
42482303Scarlsonj 		(void) close(tmpl_fd);
42492303Scarlsonj 		(void) waitpid(child, NULL, 0);
42502303Scarlsonj 		(void) contract_abandon_id(ct);
42512303Scarlsonj 		return;
42522303Scarlsonj 	}
42532303Scarlsonj 
42542303Scarlsonj 	/* child enters zone and sets up index file */
42552303Scarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
42562303Scarlsonj 	if (zone_enter(zoneid) != -1) {
42572303Scarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
42582303Scarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
42592303Scarlsonj 		    ZONE_CONFIG_GID);
42602303Scarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
42612303Scarlsonj 		    ZONE_INDEX_MODE);
42622303Scarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
42632303Scarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
42642303Scarlsonj 			if (uuid_is_null(zep->zone_uuid))
42652303Scarlsonj 				uuidstr[0] = '\0';
42662303Scarlsonj 			else
42672303Scarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
42682303Scarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
42692303Scarlsonj 			    zone_state_str(zep->zone_state),
42702303Scarlsonj 			    uuidstr);
42712303Scarlsonj 			(void) fclose(zet);
42722303Scarlsonj 		}
42732303Scarlsonj 	}
42742303Scarlsonj 	_exit(0);
42752303Scarlsonj }
42762303Scarlsonj 
42770Sstevel@tonic-gate int
42785829Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
42790Sstevel@tonic-gate {
42802712Snn35248 	char zonepath[MAXPATHLEN];
42812503Sdp 
42825829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4283789Sahrens 		lofs_discard_mnttab();
4284789Sahrens 		return (-1);
4285789Sahrens 	}
4286789Sahrens 
42872712Snn35248 	/*
42882712Snn35248 	 * Before we try to mount filesystems we need to create the
42892712Snn35248 	 * attribute backing store for /dev
42902712Snn35248 	 */
42912712Snn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
42922712Snn35248 		lofs_discard_mnttab();
42932712Snn35248 		return (-1);
42942712Snn35248 	}
42953071Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
42963813Sdp 
42973813Sdp 	/* Make /dev directory owned by root, grouped sys */
42983813Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
42993813Sdp 	    0, 3) != 0) {
43002712Snn35248 		lofs_discard_mnttab();
43012712Snn35248 		return (-1);
43022712Snn35248 	}
43032712Snn35248 
43042621Sllai1 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4305766Scarlsonj 		lofs_discard_mnttab();
43060Sstevel@tonic-gate 		return (-1);
4307766Scarlsonj 	}
43082621Sllai1 
43095829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
43103448Sdh155122 		zone_iptype_t iptype;
43113448Sdh155122 
431210616SSebastien.Roy@Sun.COM 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
43133448Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
43143448Sdh155122 			lofs_discard_mnttab();
43153448Sdh155122 			return (-1);
43163448Sdh155122 		}
43173448Sdh155122 
43183448Sdh155122 		switch (iptype) {
43193448Sdh155122 		case ZS_SHARED:
43203448Sdh155122 			/* Always do this to make lo0 get configured */
43213448Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
43223448Sdh155122 				lofs_discard_mnttab();
43233448Sdh155122 				return (-1);
43243448Sdh155122 			}
43253448Sdh155122 			break;
43263448Sdh155122 		case ZS_EXCLUSIVE:
43273448Sdh155122 			if (configure_exclusive_network_interfaces(zlogp) !=
43283448Sdh155122 			    0) {
43293448Sdh155122 				lofs_discard_mnttab();
43303448Sdh155122 				return (-1);
43313448Sdh155122 			}
43323448Sdh155122 			break;
43333448Sdh155122 		}
4334766Scarlsonj 	}
43352303Scarlsonj 
43362303Scarlsonj 	write_index_file(zoneid);
43372303Scarlsonj 
4338766Scarlsonj 	lofs_discard_mnttab();
43390Sstevel@tonic-gate 	return (0);
43400Sstevel@tonic-gate }
43410Sstevel@tonic-gate 
4342766Scarlsonj static int
4343766Scarlsonj lu_root_teardown(zlog_t *zlogp)
4344766Scarlsonj {
4345766Scarlsonj 	char zroot[MAXPATHLEN];
4346766Scarlsonj 
4347766Scarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
4348766Scarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
4349766Scarlsonj 		return (-1);
4350766Scarlsonj 	}
4351766Scarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
4352766Scarlsonj 
4353766Scarlsonj 	/*
4354766Scarlsonj 	 * At this point, the processes are gone, the filesystems (save the
4355766Scarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
4356766Scarlsonj 	 * still be creds floating about in the system that reference the
4357766Scarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
4358766Scarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
4359766Scarlsonj 	 * (How I wish this were not true, and umount2 just did the right
4360766Scarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
4361766Scarlsonj 	 */
4362766Scarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
4363766Scarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
4364766Scarlsonj 			goto unmounted;
4365766Scarlsonj 		if (errno == EBUSY) {
4366766Scarlsonj 			int tries = 10;
4367766Scarlsonj 
4368766Scarlsonj 			while (--tries >= 0) {
4369766Scarlsonj 				(void) sleep(1);
4370766Scarlsonj 				if (umount2(zroot, 0) == 0)
4371766Scarlsonj 					goto unmounted;
4372766Scarlsonj 				if (errno != EBUSY)
4373766Scarlsonj 					break;
4374766Scarlsonj 			}
4375766Scarlsonj 		}
4376766Scarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
4377766Scarlsonj 		return (-1);
4378766Scarlsonj 	}
4379766Scarlsonj unmounted:
4380766Scarlsonj 
4381766Scarlsonj 	/*
4382766Scarlsonj 	 * Only zones in an alternate root environment have scratch zone
4383766Scarlsonj 	 * entries.
4384766Scarlsonj 	 */
4385766Scarlsonj 	if (zonecfg_in_alt_root()) {
4386766Scarlsonj 		FILE *fp;
4387766Scarlsonj 		int retv;
4388766Scarlsonj 
4389766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4390766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4391766Scarlsonj 			return (-1);
4392766Scarlsonj 		}
4393766Scarlsonj 		retv = -1;
4394766Scarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
4395766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4396766Scarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
4397766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
4398766Scarlsonj 		else
4399766Scarlsonj 			retv = 0;
4400766Scarlsonj 		zonecfg_close_scratch(fp);
4401766Scarlsonj 		return (retv);
4402766Scarlsonj 	} else {
4403766Scarlsonj 		return (0);
4404766Scarlsonj 	}
4405766Scarlsonj }
4406766Scarlsonj 
44070Sstevel@tonic-gate int
44083247Sgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
44090Sstevel@tonic-gate {
4410766Scarlsonj 	char *kzone;
44110Sstevel@tonic-gate 	zoneid_t zoneid;
44123247Sgjelinek 	int res;
44133247Sgjelinek 	char pool_err[128];
44147089Sgjelinek 	char zpath[MAXPATHLEN];
44152712Snn35248 	char cmdbuf[MAXPATHLEN];
44162727Sedp 	brand_handle_t bh = NULL;
441710616SSebastien.Roy@Sun.COM 	dladm_status_t status;
441810616SSebastien.Roy@Sun.COM 	char errmsg[DLADM_STRSIZE];
44193448Sdh155122 	ushort_t flags;
44200Sstevel@tonic-gate 
4421766Scarlsonj 	kzone = zone_name;
4422766Scarlsonj 	if (zonecfg_in_alt_root()) {
4423766Scarlsonj 		FILE *fp;
4424766Scarlsonj 
4425766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
4426766Scarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
4427766Scarlsonj 			goto error;
4428766Scarlsonj 		}
4429766Scarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4430766Scarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
4431766Scarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
4432766Scarlsonj 			zonecfg_close_scratch(fp);
4433766Scarlsonj 			goto error;
4434766Scarlsonj 		}
4435766Scarlsonj 		zonecfg_close_scratch(fp);
4436766Scarlsonj 		kzone = kernzone;
4437766Scarlsonj 	}
4438766Scarlsonj 
4439766Scarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
44400Sstevel@tonic-gate 		if (!bringup_failure_recovery)
44410Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
4442766Scarlsonj 		if (unmount_cmd)
4443766Scarlsonj 			(void) lu_root_teardown(zlogp);
44440Sstevel@tonic-gate 		goto error;
44450Sstevel@tonic-gate 	}
44460Sstevel@tonic-gate 
44470Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
44480Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
44490Sstevel@tonic-gate 		goto error;
44500Sstevel@tonic-gate 	}
44510Sstevel@tonic-gate 
44527089Sgjelinek 	/* Get the zonepath of this zone */
44537089Sgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
44547089Sgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
44552712Snn35248 		goto error;
44562712Snn35248 	}
44572712Snn35248 
44582712Snn35248 	/* Get a handle to the brand info for this zone */
445910796SStephen.Lawrence@Sun.COM 	if ((bh = brand_open(brand_name)) == NULL) {
44602712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
44612712Snn35248 		return (-1);
44622712Snn35248 	}
44632712Snn35248 	/*
44642712Snn35248 	 * If there is a brand 'halt' callback, execute it now to give the
44652712Snn35248 	 * brand a chance to cleanup any custom configuration.
44662712Snn35248 	 */
44672712Snn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
44687089Sgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
44697089Sgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
44702727Sedp 		brand_close(bh);
44712712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
44722712Snn35248 		    "halt callback.");
44732712Snn35248 		goto error;
44742712Snn35248 	}
44752727Sedp 	brand_close(bh);
44762712Snn35248 
44772712Snn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
44787370S<Gerald Jelinek> 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
44792712Snn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
44802712Snn35248 		goto error;
44812712Snn35248 	}
44822712Snn35248 
44833448Sdh155122 	if (!unmount_cmd) {
44843448Sdh155122 		zone_iptype_t iptype;
44853448Sdh155122 
44863448Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
44873448Sdh155122 		    sizeof (flags)) < 0) {
448810616SSebastien.Roy@Sun.COM 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
44893448Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
44903448Sdh155122 				    "ip-type");
44913448Sdh155122 				goto error;
44923448Sdh155122 			}
44933448Sdh155122 		} else {
44943448Sdh155122 			if (flags & ZF_NET_EXCL)
44953448Sdh155122 				iptype = ZS_EXCLUSIVE;
44963448Sdh155122 			else
44973448Sdh155122 				iptype = ZS_SHARED;
44983448Sdh155122 		}
44993448Sdh155122 
45003448Sdh155122 		switch (iptype) {
45013448Sdh155122 		case ZS_SHARED:
45023448Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
45033448Sdh155122 			    zoneid) != 0) {
45043448Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
45053448Sdh155122 				    "network interfaces in zone");
45063448Sdh155122 				goto error;
45073448Sdh155122 			}
45083448Sdh155122 			break;
45093448Sdh155122 		case ZS_EXCLUSIVE:
45103448Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
45113448Sdh155122 			    zoneid) != 0) {
45123448Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
45133448Sdh155122 				    "network interfaces in zone");
45143448Sdh155122 				goto error;
45153448Sdh155122 			}
451610616SSebastien.Roy@Sun.COM 			status = dladm_zone_halt(dld_handle, zoneid);
451710616SSebastien.Roy@Sun.COM 			if (status != DLADM_STATUS_OK) {
451810616SSebastien.Roy@Sun.COM 				zerror(zlogp, B_FALSE, "unable to notify "
451910616SSebastien.Roy@Sun.COM 				    "dlmgmtd of zone halt: %s",
452010616SSebastien.Roy@Sun.COM 				    dladm_status2str(status, errmsg));
452110616SSebastien.Roy@Sun.COM 			}
45223448Sdh155122 			break;
45233448Sdh155122 		}
45240Sstevel@tonic-gate 	}
45250Sstevel@tonic-gate 
4526766Scarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
45270Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
45280Sstevel@tonic-gate 		goto error;
45290Sstevel@tonic-gate 	}
45300Sstevel@tonic-gate 
4531766Scarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
45320Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
45330Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
45340Sstevel@tonic-gate 		goto error;
45350Sstevel@tonic-gate 	}
45360Sstevel@tonic-gate 
45373247Sgjelinek 	/*
45383356Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
45393356Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
45403356Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
45413356Sgjelinek 	 * running with a temporary pool and the zone configuration has been
45423356Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
45433356Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
45443356Sgjelinek 	 * where we never had a temporary pool configured but
45453356Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
45463247Sgjelinek 	 */
45473356Sgjelinek 	if (!unmount_cmd) {
45483356Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
45493356Sgjelinek 
45503356Sgjelinek 		if (rebooting) {
45513356Sgjelinek 			struct zone_psettab pset_tab;
45523356Sgjelinek 			zone_dochandle_t handle;
45533356Sgjelinek 
45543356Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
45553356Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
45563356Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
45573356Sgjelinek 				destroy_tmp_pool = B_FALSE;
45583716Sgjelinek 
45593716Sgjelinek 			zonecfg_fini_handle(handle);
45603356Sgjelinek 		}
45613356Sgjelinek 
45623356Sgjelinek 		if (destroy_tmp_pool) {
45633356Sgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
45643356Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
45653356Sgjelinek 				if (res == Z_POOL)
45663356Sgjelinek 					zerror(zlogp, B_FALSE, pool_err);
45673356Sgjelinek 			}
45683247Sgjelinek 		}
45693247Sgjelinek 	}
45703247Sgjelinek 
45711676Sjpk 	remove_mlps(zlogp, zoneid);
45721676Sjpk 
45730Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
45740Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
45750Sstevel@tonic-gate 		goto error;
45760Sstevel@tonic-gate 	}
45770Sstevel@tonic-gate 
4578766Scarlsonj 	/*
4579766Scarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
4580766Scarlsonj 	 * root for the zone and then remove it from the map file.
4581766Scarlsonj 	 */
4582766Scarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
4583766Scarlsonj 		goto error;
4584766Scarlsonj 
4585766Scarlsonj 	lofs_discard_mnttab();
45860Sstevel@tonic-gate 	return (0);
45870Sstevel@tonic-gate 
45880Sstevel@tonic-gate error:
4589766Scarlsonj 	lofs_discard_mnttab();
45900Sstevel@tonic-gate 	return (-1);
45910Sstevel@tonic-gate }
4592