xref: /titanic_54/usr/src/cmd/zoneadmd/vplat.c (revision 108322fb1c3ed341aba9c80c9774df0ed9e35768)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
317c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
327c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
337c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
347c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
357c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
367c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
37*108322fbScarlsonj  *
38*108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
39*108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
40*108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
41*108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
42*108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
43*108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
44*108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
45*108322fbScarlsonj  * without relying on any binaries in the zone itself.
46*108322fbScarlsonj  *
47*108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
48*108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
49*108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
50*108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
51*108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
52*108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
53*108322fbScarlsonj  * process.
54*108322fbScarlsonj  *
55*108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
56*108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
57*108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
58*108322fbScarlsonj  * $ZONEPATH/lu as the root.
59*108322fbScarlsonj  *
60*108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
61*108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
62*108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
63*108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/param.h>
677c478bd9Sstevel@tonic-gate #include <sys/mount.h>
687c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
697c478bd9Sstevel@tonic-gate #include <sys/socket.h>
707c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
717c478bd9Sstevel@tonic-gate #include <sys/types.h>
727c478bd9Sstevel@tonic-gate #include <sys/stat.h>
737c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
747c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
757c478bd9Sstevel@tonic-gate #include <sys/conf.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
787c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
797c478bd9Sstevel@tonic-gate #include <netinet/in.h>
807c478bd9Sstevel@tonic-gate #include <net/route.h>
817c478bd9Sstevel@tonic-gate #include <netdb.h>
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #include <stdio.h>
847c478bd9Sstevel@tonic-gate #include <errno.h>
857c478bd9Sstevel@tonic-gate #include <fcntl.h>
867c478bd9Sstevel@tonic-gate #include <unistd.h>
877c478bd9Sstevel@tonic-gate #include <rctl.h>
887c478bd9Sstevel@tonic-gate #include <stdlib.h>
897c478bd9Sstevel@tonic-gate #include <string.h>
907c478bd9Sstevel@tonic-gate #include <strings.h>
917c478bd9Sstevel@tonic-gate #include <wait.h>
927c478bd9Sstevel@tonic-gate #include <limits.h>
937c478bd9Sstevel@tonic-gate #include <libgen.h>
947c478bd9Sstevel@tonic-gate #include <zone.h>
957c478bd9Sstevel@tonic-gate #include <assert.h>
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
987c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
997c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1007c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #include <pool.h>
1037c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
1067c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1097c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1127c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1137c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
1167c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * A list of directories which should be created.
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate struct dir_info {
1257c478bd9Sstevel@tonic-gate 	char *dir_name;
1267c478bd9Sstevel@tonic-gate 	mode_t dir_mode;
1277c478bd9Sstevel@tonic-gate };
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * The pathnames below are relative to the zonepath
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate static struct dir_info dev_dirs[] = {
1337c478bd9Sstevel@tonic-gate 	{ "/dev",	0755 },
1347c478bd9Sstevel@tonic-gate 	{ "/dev/dsk",	0755 },
1357c478bd9Sstevel@tonic-gate 	{ "/dev/fd",	0555 },
1367c478bd9Sstevel@tonic-gate 	{ "/dev/pts",	0755 },
1377c478bd9Sstevel@tonic-gate 	{ "/dev/rdsk",	0755 },
1387c478bd9Sstevel@tonic-gate 	{ "/dev/rmt",	0755 },
1397c478bd9Sstevel@tonic-gate 	{ "/dev/sad",	0755 },
1407c478bd9Sstevel@tonic-gate 	{ "/dev/swap",	0755 },
1417c478bd9Sstevel@tonic-gate 	{ "/dev/term",	0755 },
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * A list of devices which should be symlinked to /dev/zconsole.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate struct symlink_info {
1497c478bd9Sstevel@tonic-gate 	char *sl_source;
1507c478bd9Sstevel@tonic-gate 	char *sl_target;
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * The "source" paths are relative to the zonepath
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate static struct symlink_info dev_symlinks[] = {
1577c478bd9Sstevel@tonic-gate 	{ "/dev/stderr",	"./fd/2" },
1587c478bd9Sstevel@tonic-gate 	{ "/dev/stdin",		"./fd/0" },
1597c478bd9Sstevel@tonic-gate 	{ "/dev/stdout",	"./fd/1" },
1607c478bd9Sstevel@tonic-gate 	{ "/dev/dtremote",	"/dev/null" },
1617c478bd9Sstevel@tonic-gate 	{ "/dev/console",	"zconsole" },
1627c478bd9Sstevel@tonic-gate 	{ "/dev/syscon",	"zconsole" },
1637c478bd9Sstevel@tonic-gate 	{ "/dev/sysmsg",	"zconsole" },
1647c478bd9Sstevel@tonic-gate 	{ "/dev/systty",	"zconsole" },
1657c478bd9Sstevel@tonic-gate 	{ "/dev/msglog",	"zconsole" },
1667c478bd9Sstevel@tonic-gate };
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* for routing socket */
1697c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1707c478bd9Sstevel@tonic-gate 
171*108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
172*108322fbScarlsonj static char kernzone[ZONENAME_MAX];
173*108322fbScarlsonj 
174*108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
175*108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
176*108322fbScarlsonj 
1777c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1787c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
181*108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
182*108322fbScarlsonj  * data) only once every N times through the loop.
183*108322fbScarlsonj  */
184*108322fbScarlsonj #define	MNTTAB_HUNK	32
185*108322fbScarlsonj 
186*108322fbScarlsonj /*
1877c478bd9Sstevel@tonic-gate  * Private autofs system call
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static int
1927c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
200*108322fbScarlsonj static void
201*108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
202*108322fbScarlsonj {
203*108322fbScarlsonj 	uint_t i;
204*108322fbScarlsonj 
205*108322fbScarlsonj 	if (mnt_array == NULL)
206*108322fbScarlsonj 		return;
207*108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
208*108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
209*108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
210*108322fbScarlsonj 		free(mnt_array[i].mnt_special);
211*108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
212*108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
213*108322fbScarlsonj 	}
214*108322fbScarlsonj 	free(mnt_array);
215*108322fbScarlsonj }
216*108322fbScarlsonj 
217*108322fbScarlsonj /*
218*108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
219*108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
220*108322fbScarlsonj  * array in "nelemp".
221*108322fbScarlsonj  */
222*108322fbScarlsonj static int
223*108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
224*108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
225*108322fbScarlsonj {
226*108322fbScarlsonj 	struct mnttab mnt;
227*108322fbScarlsonj 	struct mnttab *mnts;
228*108322fbScarlsonj 	struct mnttab *mnp;
229*108322fbScarlsonj 	uint_t nmnt;
230*108322fbScarlsonj 
231*108322fbScarlsonj 	rewind(mnttab);
232*108322fbScarlsonj 	resetmnttab(mnttab);
233*108322fbScarlsonj 	nmnt = 0;
234*108322fbScarlsonj 	mnts = NULL;
235*108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
236*108322fbScarlsonj 		struct mnttab *tmp_array;
237*108322fbScarlsonj 
238*108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
239*108322fbScarlsonj 			continue;
240*108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
241*108322fbScarlsonj 			tmp_array = realloc(mnts,
242*108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
243*108322fbScarlsonj 			if (tmp_array == NULL) {
244*108322fbScarlsonj 				free_mnttable(mnts, nmnt);
245*108322fbScarlsonj 				return (-1);
246*108322fbScarlsonj 			}
247*108322fbScarlsonj 			mnts = tmp_array;
248*108322fbScarlsonj 		}
249*108322fbScarlsonj 		mnp = &mnts[nmnt++];
250*108322fbScarlsonj 
251*108322fbScarlsonj 		/*
252*108322fbScarlsonj 		 * Zero out any fields we're not using.
253*108322fbScarlsonj 		 */
254*108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
255*108322fbScarlsonj 
256*108322fbScarlsonj 		if (mnt.mnt_special != NULL)
257*108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
258*108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
259*108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
260*108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
261*108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
262*108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
263*108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
264*108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
265*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
266*108322fbScarlsonj 			free_mnttable(mnts, nmnt);
267*108322fbScarlsonj 			return (-1);
268*108322fbScarlsonj 		}
269*108322fbScarlsonj 	}
270*108322fbScarlsonj 	*mnt_arrayp = mnts;
271*108322fbScarlsonj 	*nelemp = nmnt;
272*108322fbScarlsonj 	return (0);
273*108322fbScarlsonj }
274*108322fbScarlsonj 
275*108322fbScarlsonj /*
276*108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
277*108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
278*108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
279*108322fbScarlsonj  * reread of the list of mount points
280*108322fbScarlsonj  */
281*108322fbScarlsonj static void
282*108322fbScarlsonj lofs_discard_mnttab(void)
283*108322fbScarlsonj {
284*108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
285*108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
286*108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
287*108322fbScarlsonj }
288*108322fbScarlsonj 
289*108322fbScarlsonj static int
290*108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
291*108322fbScarlsonj {
292*108322fbScarlsonj 	FILE *mnttab;
293*108322fbScarlsonj 	uint_t nmnts;
294*108322fbScarlsonj 
295*108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
296*108322fbScarlsonj 		return (-1);
297*108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
298*108322fbScarlsonj 	    &nmnts) == -1) {
299*108322fbScarlsonj 		(void) fclose(mnttab);
300*108322fbScarlsonj 		return (-1);
301*108322fbScarlsonj 	}
302*108322fbScarlsonj 	(void) fclose(mnttab);
303*108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
304*108322fbScarlsonj 	return (0);
305*108322fbScarlsonj }
306*108322fbScarlsonj 
307*108322fbScarlsonj /*
308*108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
309*108322fbScarlsonj  * path and resolves them all down to an absolute path.
310*108322fbScarlsonj  */
311*108322fbScarlsonj static void
312*108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
313*108322fbScarlsonj {
314*108322fbScarlsonj 	int len, arlen;
315*108322fbScarlsonj 	const char *altroot;
316*108322fbScarlsonj 	char tmppath[MAXPATHLEN];
317*108322fbScarlsonj 	boolean_t outside_altroot;
318*108322fbScarlsonj 
319*108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
320*108322fbScarlsonj 		return;
321*108322fbScarlsonj 	tmppath[len] = '\0';
322*108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
323*108322fbScarlsonj 
324*108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
325*108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
326*108322fbScarlsonj 		return;
327*108322fbScarlsonj 
328*108322fbScarlsonj 	altroot = zonecfg_get_root();
329*108322fbScarlsonj 	arlen = strlen(altroot);
330*108322fbScarlsonj 	outside_altroot = B_FALSE;
331*108322fbScarlsonj 	for (;;) {
332*108322fbScarlsonj 		struct mnttab *mnp;
333*108322fbScarlsonj 
334*108322fbScarlsonj 		for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
335*108322fbScarlsonj 		    mnp++) {
336*108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
337*108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
338*108322fbScarlsonj 			    mnp->mnt_special == NULL ||
339*108322fbScarlsonj 			    strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
340*108322fbScarlsonj 				continue;
341*108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
342*108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
343*108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
344*108322fbScarlsonj 				break;
345*108322fbScarlsonj 		}
346*108322fbScarlsonj 		if (mnp >= resolve_lofs_mnt_max)
347*108322fbScarlsonj 			break;
348*108322fbScarlsonj 		if (outside_altroot) {
349*108322fbScarlsonj 			char *cp;
350*108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
351*108322fbScarlsonj 
352*108322fbScarlsonj 			/*
353*108322fbScarlsonj 			 * If we run into a read-only mount outside of the
354*108322fbScarlsonj 			 * alternate root environment, then the user doesn't
355*108322fbScarlsonj 			 * want this path to be made read-write.
356*108322fbScarlsonj 			 */
357*108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
358*108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
359*108322fbScarlsonj 			    NULL &&
360*108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
361*108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
362*108322fbScarlsonj 				break;
363*108322fbScarlsonj 			}
364*108322fbScarlsonj 		} else if (arlen > 0 &&
365*108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
366*108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
367*108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
368*108322fbScarlsonj 			outside_altroot = B_TRUE;
369*108322fbScarlsonj 		}
370*108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
371*108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
372*108322fbScarlsonj 		    mnp->mnt_special, path + len);
373*108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
374*108322fbScarlsonj 			break;
375*108322fbScarlsonj 		path[len] = '\0';
376*108322fbScarlsonj 	}
377*108322fbScarlsonj }
378*108322fbScarlsonj 
379*108322fbScarlsonj /*
380*108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
381*108322fbScarlsonj  * referenced device is already mounted somewhere.
382*108322fbScarlsonj  */
383*108322fbScarlsonj static int
384*108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
385*108322fbScarlsonj {
386*108322fbScarlsonj 	struct mnttab *mnp;
387*108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
388*108322fbScarlsonj 
389*108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
390*108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
391*108322fbScarlsonj 		return (-1);
392*108322fbScarlsonj 
393*108322fbScarlsonj 	/*
394*108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
395*108322fbScarlsonj 	 * no need to worry about conflicting mounts.
396*108322fbScarlsonj 	 */
397*108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
398*108322fbScarlsonj 	    mnp++) {
399*108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
400*108322fbScarlsonj 			break;
401*108322fbScarlsonj 	}
402*108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
403*108322fbScarlsonj 		return (0);
404*108322fbScarlsonj 
405*108322fbScarlsonj 	/*
406*108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
407*108322fbScarlsonj 	 */
408*108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
409*108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
410*108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
411*108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
412*108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
413*108322fbScarlsonj 
414*108322fbScarlsonj 	/*
415*108322fbScarlsonj 	 * Discard all but one of the original options and set that to be the
416*108322fbScarlsonj 	 * same set of options used for inherit package directory resources.
417*108322fbScarlsonj 	 */
418*108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
419*108322fbScarlsonj 	if (optptr == NULL) {
420*108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
421*108322fbScarlsonj 		if (optptr == NULL) {
422*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
423*108322fbScarlsonj 			    fsptr->zone_fs_dir);
424*108322fbScarlsonj 			return (-1);
425*108322fbScarlsonj 		}
426*108322fbScarlsonj 	} else {
427*108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
428*108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
429*108322fbScarlsonj 			free(onext);
430*108322fbScarlsonj 		}
431*108322fbScarlsonj 	}
432*108322fbScarlsonj 	(void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS);
433*108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
434*108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
435*108322fbScarlsonj 	return (0);
436*108322fbScarlsonj }
437*108322fbScarlsonj 
4387c478bd9Sstevel@tonic-gate static int
4397c478bd9Sstevel@tonic-gate make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4427c478bd9Sstevel@tonic-gate 	struct stat st;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4457c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4477c478bd9Sstevel@tonic-gate 		    subdir);
4487c478bd9Sstevel@tonic-gate 		return (-1);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4527c478bd9Sstevel@tonic-gate 		/*
4537c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4547c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4557c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
4567c478bd9Sstevel@tonic-gate 		 */
4577c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
4587c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s is not a directory", path);
4597c478bd9Sstevel@tonic-gate 			return (-1);
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 	} else if (mkdirp(path, mode) != 0) {
4627c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
4637c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4647c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4657c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4667c478bd9Sstevel@tonic-gate 		else
4677c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4687c478bd9Sstevel@tonic-gate 		return (-1);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	return (0);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * Make /dev and various directories underneath it.
4757c478bd9Sstevel@tonic-gate  */
4767c478bd9Sstevel@tonic-gate static int
4777c478bd9Sstevel@tonic-gate make_dev_dirs(zlog_t *zlogp, const char *zonepath)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	int i;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (dev_dirs) / sizeof (struct dir_info); i++) {
4827c478bd9Sstevel@tonic-gate 		if (make_one_dir(zlogp, zonepath, dev_dirs[i].dir_name,
4837c478bd9Sstevel@tonic-gate 		    dev_dirs[i].dir_mode) != 0)
4847c478bd9Sstevel@tonic-gate 			return (-1);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	return (0);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * Make various sym-links underneath /dev.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate static int
4937c478bd9Sstevel@tonic-gate make_dev_links(zlog_t *zlogp, char *zonepath)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	int i;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (dev_symlinks) / sizeof (struct symlink_info);
4987c478bd9Sstevel@tonic-gate 	    i++) {
4997c478bd9Sstevel@tonic-gate 		char dev[MAXPATHLEN];
5007c478bd9Sstevel@tonic-gate 		struct stat st;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		(void) snprintf(dev, sizeof (dev), "%s%s", zonepath,
5037c478bd9Sstevel@tonic-gate 		    dev_symlinks[i].sl_source);
5047c478bd9Sstevel@tonic-gate 		if (lstat(dev, &st) == 0) {
5057c478bd9Sstevel@tonic-gate 			/*
5067c478bd9Sstevel@tonic-gate 			 * Try not to call unlink(2) on directories, since that
5077c478bd9Sstevel@tonic-gate 			 * makes UFS unhappy.
5087c478bd9Sstevel@tonic-gate 			 */
5097c478bd9Sstevel@tonic-gate 			if (S_ISDIR(st.st_mode)) {
5107c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "symlink path %s is a "
5117c478bd9Sstevel@tonic-gate 				    "directory", dev_symlinks[i].sl_source);
5127c478bd9Sstevel@tonic-gate 				return (-1);
5137c478bd9Sstevel@tonic-gate 			}
5147c478bd9Sstevel@tonic-gate 			(void) unlink(dev);
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 		if (symlink(dev_symlinks[i].sl_target, dev) != 0) {
517*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "could not setup %s->%s symlink",
518*108322fbScarlsonj 			    dev_symlinks[i].sl_source,
519*108322fbScarlsonj 			    dev_symlinks[i].sl_target);
5207c478bd9Sstevel@tonic-gate 			return (-1);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 	return (0);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * Create various directories and sym-links under /dev.
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate static int
5307c478bd9Sstevel@tonic-gate create_dev_files(zlog_t *zlogp)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
5357c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
5367c478bd9Sstevel@tonic-gate 		return (-1);
5377c478bd9Sstevel@tonic-gate 	}
538*108322fbScarlsonj 	if (zonecfg_in_alt_root())
539*108322fbScarlsonj 		resolve_lofs(zlogp, zonepath, sizeof (zonepath));
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (make_dev_dirs(zlogp, zonepath) != 0)
5427c478bd9Sstevel@tonic-gate 		return (-1);
5437c478bd9Sstevel@tonic-gate 	if (make_dev_links(zlogp, zonepath) != 0)
5447c478bd9Sstevel@tonic-gate 		return (-1);
5457c478bd9Sstevel@tonic-gate 	return (0);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate static void
5497c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	uint_t i;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (types == NULL)
5547c478bd9Sstevel@tonic-gate 		return;
5557c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
5567c478bd9Sstevel@tonic-gate 		free(types[i]);
5577c478bd9Sstevel@tonic-gate 	free(types);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate static char **
5617c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	char **types = NULL;
5647c478bd9Sstevel@tonic-gate 	FILE *fp;
5657c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
5667c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
5677c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
5687c478bd9Sstevel@tonic-gate 	uint_t i;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5727c478bd9Sstevel@tonic-gate 		return (NULL);
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5787c478bd9Sstevel@tonic-gate 		lines++;
5797c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5807c478bd9Sstevel@tonic-gate 		goto out;
5817c478bd9Sstevel@tonic-gate 	rewind(fp);
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5847c478bd9Sstevel@tonic-gate 	 */
5857c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5867c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5887c478bd9Sstevel@tonic-gate 		goto out;
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	i = 0;
5917c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5927c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5937c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5947c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5957c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5967c478bd9Sstevel@tonic-gate 			types = NULL;
5977c478bd9Sstevel@tonic-gate 			goto out;
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
6007c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
6017c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
6027c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
6037c478bd9Sstevel@tonic-gate 			types = NULL;
6047c478bd9Sstevel@tonic-gate 			goto out;
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 		i++;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate out:
6097c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
6107c478bd9Sstevel@tonic-gate 	return (types);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate static boolean_t
6147c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	uint_t i;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
6197c478bd9Sstevel@tonic-gate 		return (B_FALSE);
6207c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
6217c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
6227c478bd9Sstevel@tonic-gate 			return (B_TRUE);
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 	return (B_FALSE);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
627*108322fbScarlsonj /*
628*108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
629*108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
630*108322fbScarlsonj  */
6317c478bd9Sstevel@tonic-gate static void
632*108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
6337c478bd9Sstevel@tonic-gate {
634*108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
635*108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
636*108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
6417c478bd9Sstevel@tonic-gate  *
6427c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
6437c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
6447c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
6457c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
6467c478bd9Sstevel@tonic-gate  *
6477c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
6487c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
6497c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
6507c478bd9Sstevel@tonic-gate  *
6517c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
6527c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
6537c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
6547c478bd9Sstevel@tonic-gate  *
6557c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
6567c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
6577c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
6587c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
6597c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
6607c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
6617c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
6627c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
6637c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
6647c478bd9Sstevel@tonic-gate  * graceful mounts first.
6657c478bd9Sstevel@tonic-gate  *
6667c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
6677c478bd9Sstevel@tonic-gate  */
6687c478bd9Sstevel@tonic-gate static int
669*108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	int error = 0;
6727c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6737c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6747c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6757c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6767c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6777c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6787c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6797c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6837c478bd9Sstevel@tonic-gate 		return (-1);
6847c478bd9Sstevel@tonic-gate 	}
685*108322fbScarlsonj 	if (unmount_cmd)
686*108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6897c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6927c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6937c478bd9Sstevel@tonic-gate 		return (-1);
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 	/*
6967c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6977c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6987c478bd9Sstevel@tonic-gate 	 */
6997c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
7007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
7017c478bd9Sstevel@tonic-gate 		error++;
7027c478bd9Sstevel@tonic-gate 		goto out;
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
7077c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
7087c478bd9Sstevel@tonic-gate 	 */
7097c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
7107c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
7117c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
7127c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
7137c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
7147c478bd9Sstevel@tonic-gate 		char *path;
7157c478bd9Sstevel@tonic-gate 		uint_t i;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 		mnts = NULL;
7187c478bd9Sstevel@tonic-gate 		nmnt = 0;
7197c478bd9Sstevel@tonic-gate 		/*
7207c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
7217c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
7227c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
7237c478bd9Sstevel@tonic-gate 		 * filesystems.
7247c478bd9Sstevel@tonic-gate 		 */
7257c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
7267c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
7277c478bd9Sstevel@tonic-gate 			error++;
7287c478bd9Sstevel@tonic-gate 			goto out;
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
7317c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
7327c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
7337c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
7347c478bd9Sstevel@tonic-gate 			/*
7357c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
7367c478bd9Sstevel@tonic-gate 			 *
7377c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
7387c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
7397c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
7407c478bd9Sstevel@tonic-gate 			 */
7417c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
7427c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
7437c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7447c478bd9Sstevel@tonic-gate 			}
7457c478bd9Sstevel@tonic-gate 			/*
7467c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
7477c478bd9Sstevel@tonic-gate 			 */
7487c478bd9Sstevel@tonic-gate 			if (stuck) {
7497c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
7507c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7517c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
7527c478bd9Sstevel@tonic-gate 				} else {
7537c478bd9Sstevel@tonic-gate 					/*
7547c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
7557c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
7567c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
7577c478bd9Sstevel@tonic-gate 					 * bail.
7587c478bd9Sstevel@tonic-gate 					 */
7597c478bd9Sstevel@tonic-gate 					error++;
7607c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
7617c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
7627c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
7637c478bd9Sstevel@tonic-gate 					goto out;
7647c478bd9Sstevel@tonic-gate 				}
7657c478bd9Sstevel@tonic-gate 			}
7667c478bd9Sstevel@tonic-gate 			/*
7677c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7687c478bd9Sstevel@tonic-gate 			 */
7697c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7707c478bd9Sstevel@tonic-gate 				newcount++;
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7757c478bd9Sstevel@tonic-gate 			break;
7767c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7777c478bd9Sstevel@tonic-gate 			/*
7787c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7797c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7807c478bd9Sstevel@tonic-gate 			 */
7817c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		/*
7867c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7877c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7887c478bd9Sstevel@tonic-gate 		 */
7897c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7907c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7917c478bd9Sstevel@tonic-gate 			error++;
7927c478bd9Sstevel@tonic-gate 			goto out;
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate out:
7977c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7987c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7997c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate static int
8037c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
8047c478bd9Sstevel@tonic-gate {
8057c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
8067c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
8137c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
8147c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
8157c478bd9Sstevel@tonic-gate  *
8167c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
8177c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
8187c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
8197c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
8207c478bd9Sstevel@tonic-gate  * remains the case.
8217c478bd9Sstevel@tonic-gate  */
8227c478bd9Sstevel@tonic-gate static int
8237c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	pid_t child_pid;
8267c478bd9Sstevel@tonic-gate 	int child_status = 0;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/*
8297c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
8307c478bd9Sstevel@tonic-gate 	 */
8317c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
8327c478bd9Sstevel@tonic-gate 	child_pid = fork();
8337c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
8347c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
8357c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
8367c478bd9Sstevel@tonic-gate 		return (-1);
8377c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
8387c478bd9Sstevel@tonic-gate 		closefrom(0);
8397c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
8407c478bd9Sstevel@tonic-gate 		/*
8417c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
8427c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
8437c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
8447c478bd9Sstevel@tonic-gate 		 * accordingly.
8457c478bd9Sstevel@tonic-gate 		 */
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
8487c478bd9Sstevel@tonic-gate 	} else {
8497c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
8537c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8547c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
8557c478bd9Sstevel@tonic-gate 		return (-1);
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
8587c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
8597c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8607c478bd9Sstevel@tonic-gate 		return (-1);
8617c478bd9Sstevel@tonic-gate 	}
8627c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate static int
8667c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8697c478bd9Sstevel@tonic-gate 	char *argv[4];
8707c478bd9Sstevel@tonic-gate 	int status;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/*
8737c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8747c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8757c478bd9Sstevel@tonic-gate 	 */
8767c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8777c478bd9Sstevel@tonic-gate 	    > sizeof (cmdbuf)) {
8787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8797c478bd9Sstevel@tonic-gate 		return (-1);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
8837c478bd9Sstevel@tonic-gate 	argv[1] = "-m";
8847c478bd9Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
8857c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8887c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8897c478bd9Sstevel@tonic-gate 		return (status);
8907c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8917c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8927c478bd9Sstevel@tonic-gate 	return (-1);
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate static int
8967c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8977c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8987c478bd9Sstevel@tonic-gate {
8997c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
9007c478bd9Sstevel@tonic-gate 	char *argv[6];
9017c478bd9Sstevel@tonic-gate 	int status;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/*
9047c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
9057c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
9087c478bd9Sstevel@tonic-gate 	    > sizeof (cmdbuf)) {
9097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
9107c478bd9Sstevel@tonic-gate 		return (-1);
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
9137c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
9147c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
9157c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
9167c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
9177c478bd9Sstevel@tonic-gate 	} else {
9187c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
9197c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
9207c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
9217c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
9227c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
9267c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
9277c478bd9Sstevel@tonic-gate 		return (status);
9287c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
9297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
9307c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9317c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
9327c478bd9Sstevel@tonic-gate 	else
9337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
9347c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9357c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
9367c478bd9Sstevel@tonic-gate 	return (-1);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate  * Make sure if a given path exists, it is not a sym-link, and is a directory.
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate static int
9437c478bd9Sstevel@tonic-gate check_path(zlog_t *zlogp, const char *path)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9467c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9477c478bd9Sstevel@tonic-gate 	int res;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9507c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
9517c478bd9Sstevel@tonic-gate 			return (0);
9527c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9537c478bd9Sstevel@tonic-gate 		return (-1);
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9567c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9577c478bd9Sstevel@tonic-gate 		return (-1);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(statbuf.st_mode)) {
9607c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9617c478bd9Sstevel@tonic-gate 		return (-1);
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9647c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9657c478bd9Sstevel@tonic-gate 		return (-1);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9687c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9697c478bd9Sstevel@tonic-gate 		/*
9707c478bd9Sstevel@tonic-gate 		 * We don't like ".."s and "."s throwing us off
9717c478bd9Sstevel@tonic-gate 		 */
9727c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9737c478bd9Sstevel@tonic-gate 		return (-1);
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 	return (0);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate /*
9797c478bd9Sstevel@tonic-gate  * Check every component of rootpath/relpath.  If any component fails (ie,
9807c478bd9Sstevel@tonic-gate  * exists but isn't the canonical path to a directory), it is returned in
9817c478bd9Sstevel@tonic-gate  * badpath, which is assumed to be at least of size MAXPATHLEN.
9827c478bd9Sstevel@tonic-gate  *
9837c478bd9Sstevel@tonic-gate  * Relpath must begin with '/'.
9847c478bd9Sstevel@tonic-gate  */
9857c478bd9Sstevel@tonic-gate static boolean_t
9867c478bd9Sstevel@tonic-gate valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath)
9877c478bd9Sstevel@tonic-gate {
9887c478bd9Sstevel@tonic-gate 	char abspath[MAXPATHLEN], *slashp;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/*
9917c478bd9Sstevel@tonic-gate 	 * Make sure abspath has at least one '/' after its rootpath
9927c478bd9Sstevel@tonic-gate 	 * component, and ends with '/'.
9937c478bd9Sstevel@tonic-gate 	 */
9947c478bd9Sstevel@tonic-gate 	if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) >
9957c478bd9Sstevel@tonic-gate 	    sizeof (abspath)) {
9967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath,
9977c478bd9Sstevel@tonic-gate 		    relpath);
9987c478bd9Sstevel@tonic-gate 		return (B_FALSE);
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10027c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10037c478bd9Sstevel@tonic-gate 	do {
10047c478bd9Sstevel@tonic-gate 		*slashp = '\0';
10057c478bd9Sstevel@tonic-gate 		if (check_path(zlogp, abspath) != 0)
10067c478bd9Sstevel@tonic-gate 			return (B_FALSE);
10077c478bd9Sstevel@tonic-gate 		*slashp = '/';
10087c478bd9Sstevel@tonic-gate 		slashp++;
10097c478bd9Sstevel@tonic-gate 	} while ((slashp = strchr(slashp, '/')) != NULL);
10107c478bd9Sstevel@tonic-gate 	return (B_TRUE);
10117c478bd9Sstevel@tonic-gate }
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate static int
10147c478bd9Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	char    path[MAXPATHLEN];
1017*108322fbScarlsonj 	char	specpath[MAXPATHLEN];
10187c478bd9Sstevel@tonic-gate 	char    optstr[MAX_MNTOPT_STR];
10197c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) {
10227c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
10237c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
10247c478bd9Sstevel@tonic-gate 		return (-1);
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
10287c478bd9Sstevel@tonic-gate 	    DEFAULT_DIR_MODE) != 0)
10297c478bd9Sstevel@tonic-gate 		return (-1);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
10327c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
10357c478bd9Sstevel@tonic-gate 		/*
10367c478bd9Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
1037*108322fbScarlsonj 		 * general-purpose FSs.  Make sure it mounts from a place that
1038*108322fbScarlsonj 		 * can be seen via the alternate zone's root.
10397c478bd9Sstevel@tonic-gate 		 */
1040*108322fbScarlsonj 		if (snprintf(specpath, sizeof (specpath), "%s%s",
1041*108322fbScarlsonj 		    zonecfg_get_root(), fsptr->zone_fs_dir) >=
1042*108322fbScarlsonj 		    sizeof (specpath)) {
1043*108322fbScarlsonj 			zerror(zlogp, B_FALSE, "cannot mount %s: path too "
1044*108322fbScarlsonj 			    "long in alternate root", fsptr->zone_fs_dir);
1045*108322fbScarlsonj 			return (-1);
1046*108322fbScarlsonj 		}
1047*108322fbScarlsonj 		if (zonecfg_in_alt_root())
1048*108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
10497c478bd9Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
1050*108322fbScarlsonj 		    specpath, path) != 0) {
10517c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
1052*108322fbScarlsonj 			    specpath);
10537c478bd9Sstevel@tonic-gate 			return (-1);
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 		return (0);
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	/*
10597c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
10607c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
10617c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
10627c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
10637c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
10647c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
10657c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
10667c478bd9Sstevel@tonic-gate 	 */
10677c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
10687c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
10697c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
10707c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
10717c478bd9Sstevel@tonic-gate 		return (-1);
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	/*
1075*108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1076*108322fbScarlsonj 	 * read-only loopback mounts as necessary.  For all lofs mounts, make
1077*108322fbScarlsonj 	 * sure that the 'special' entry points inside the alternate root.  (We
1078*108322fbScarlsonj 	 * don't do this with other mounts, as devfs isn't in the alternate
1079*108322fbScarlsonj 	 * root, and we need to assume the device environment is roughly the
1080*108322fbScarlsonj 	 * same.)
1081*108322fbScarlsonj 	 */
1082*108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1083*108322fbScarlsonj 		struct stat64 st;
1084*108322fbScarlsonj 
1085*108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
1086*108322fbScarlsonj 		    S_ISBLK(st.st_mode) &&
1087*108322fbScarlsonj 		    check_lofs_needed(zlogp, fsptr) == -1)
1088*108322fbScarlsonj 			return (-1);
1089*108322fbScarlsonj 		if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1090*108322fbScarlsonj 			if (snprintf(specpath, sizeof (specpath), "%s%s",
1091*108322fbScarlsonj 			    zonecfg_get_root(), fsptr->zone_fs_special) >=
1092*108322fbScarlsonj 			    sizeof (specpath)) {
1093*108322fbScarlsonj 				zerror(zlogp, B_FALSE, "cannot mount %s: path "
1094*108322fbScarlsonj 				    "too long in alternate root",
1095*108322fbScarlsonj 				    fsptr->zone_fs_special);
1096*108322fbScarlsonj 				return (-1);
1097*108322fbScarlsonj 			}
1098*108322fbScarlsonj 			resolve_lofs(zlogp, specpath, sizeof (specpath));
1099*108322fbScarlsonj 			(void) strlcpy(fsptr->zone_fs_special, specpath,
1100*108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1101*108322fbScarlsonj 		}
1102*108322fbScarlsonj 	}
1103*108322fbScarlsonj 
1104*108322fbScarlsonj 	/*
11057c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
11067c478bd9Sstevel@tonic-gate 	 */
11077c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
11087c478bd9Sstevel@tonic-gate 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0)
11097c478bd9Sstevel@tonic-gate 		return (-1);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
11137c478bd9Sstevel@tonic-gate 	 */
11147c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
11157c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
11167c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
11177c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
11187c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
11197c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
11207c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
11217c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
11227c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
11237c478bd9Sstevel@tonic-gate 		}
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 	return (domount(zlogp, fsptr->zone_fs_type, optstr,
11267c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_special, path));
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate static void
11307c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
11317c478bd9Sstevel@tonic-gate {
11327c478bd9Sstevel@tonic-gate 	uint_t i;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
11357c478bd9Sstevel@tonic-gate 		return;
11367c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
11377c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
11387c478bd9Sstevel@tonic-gate 	free(fsarray);
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
1141*108322fbScarlsonj /*
1142*108322fbScarlsonj  * This function constructs the miniroot-like "scratch zone" environment.  If
1143*108322fbScarlsonj  * it returns B_FALSE, then the error has already been logged.
1144*108322fbScarlsonj  */
1145*108322fbScarlsonj static boolean_t
1146*108322fbScarlsonj build_mounted(zlog_t *zlogp, char *rootpath, size_t rootlen,
1147*108322fbScarlsonj     const char *zonepath)
1148*108322fbScarlsonj {
1149*108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1150*108322fbScarlsonj 	char luroot[MAXPATHLEN];
1151*108322fbScarlsonj 	const char **cpp;
1152*108322fbScarlsonj 	static const char *mkdirs[] = {
1153*108322fbScarlsonj 		"/system", "/system/contract", "/proc", "/dev", "/tmp",
1154*108322fbScarlsonj 		"/a", NULL
1155*108322fbScarlsonj 	};
1156*108322fbScarlsonj 	static const char *localdirs[] = {
1157*108322fbScarlsonj 		"/etc", "/var", NULL
1158*108322fbScarlsonj 	};
1159*108322fbScarlsonj 	static const char *loopdirs[] = {
1160*108322fbScarlsonj 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1161*108322fbScarlsonj 		"/usr", NULL
1162*108322fbScarlsonj 	};
1163*108322fbScarlsonj 	static const char *tmpdirs[] = {
1164*108322fbScarlsonj 		"/tmp", "/var/run", NULL
1165*108322fbScarlsonj 	};
1166*108322fbScarlsonj 	FILE *fp;
1167*108322fbScarlsonj 	struct stat st;
1168*108322fbScarlsonj 	char *altstr;
1169*108322fbScarlsonj 	uuid_t uuid;
1170*108322fbScarlsonj 
1171*108322fbScarlsonj 	/*
1172*108322fbScarlsonj 	 * Construct a small Solaris environment, including the zone root
1173*108322fbScarlsonj 	 * mounted on '/a' inside that environment.
1174*108322fbScarlsonj 	 */
1175*108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
1176*108322fbScarlsonj 	(void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath);
1177*108322fbScarlsonj 	resolve_lofs(zlogp, luroot, sizeof (luroot));
1178*108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1179*108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1180*108322fbScarlsonj 
1181*108322fbScarlsonj 	/*
1182*108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1183*108322fbScarlsonj 	 * zone_mount_early.)
1184*108322fbScarlsonj 	 */
1185*108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1186*108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1187*108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1188*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1189*108322fbScarlsonj 			return (B_FALSE);
1190*108322fbScarlsonj 		}
1191*108322fbScarlsonj 	}
1192*108322fbScarlsonj 
1193*108322fbScarlsonj 	/*
1194*108322fbScarlsonj 	 * These are mounted read-write from the zone undergoing upgrade.  We
1195*108322fbScarlsonj 	 * must be careful not to 'leak' things from the main system into the
1196*108322fbScarlsonj 	 * zone, and this accomplishes that goal.
1197*108322fbScarlsonj 	 */
1198*108322fbScarlsonj 	for (cpp = localdirs; *cpp != NULL; cpp++) {
1199*108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1200*108322fbScarlsonj 		(void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath,
1201*108322fbScarlsonj 		    *cpp);
1202*108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1203*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1204*108322fbScarlsonj 			return (B_FALSE);
1205*108322fbScarlsonj 		}
1206*108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) {
1207*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1208*108322fbScarlsonj 			    *cpp);
1209*108322fbScarlsonj 			return (B_FALSE);
1210*108322fbScarlsonj 		}
1211*108322fbScarlsonj 	}
1212*108322fbScarlsonj 
1213*108322fbScarlsonj 	/*
1214*108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1215*108322fbScarlsonj 	 * they contain binaries that must match system.
1216*108322fbScarlsonj 	 */
1217*108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1218*108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1219*108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1220*108322fbScarlsonj 			if (errno != EEXIST) {
1221*108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1222*108322fbScarlsonj 				return (B_FALSE);
1223*108322fbScarlsonj 			}
1224*108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1225*108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1226*108322fbScarlsonj 				return (B_FALSE);
1227*108322fbScarlsonj 			}
1228*108322fbScarlsonj 			/*
1229*108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1230*108322fbScarlsonj 			 * things that have been converted into symlinks
1231*108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1232*108322fbScarlsonj 			 * fixup.
1233*108322fbScarlsonj 			 */
1234*108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1235*108322fbScarlsonj 				continue;
1236*108322fbScarlsonj 		}
1237*108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp,
1238*108322fbScarlsonj 		    tmp) != 0) {
1239*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1240*108322fbScarlsonj 			    *cpp);
1241*108322fbScarlsonj 			return (B_FALSE);
1242*108322fbScarlsonj 		}
1243*108322fbScarlsonj 	}
1244*108322fbScarlsonj 
1245*108322fbScarlsonj 	/*
1246*108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1247*108322fbScarlsonj 	 */
1248*108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1249*108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1250*108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
1251*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1252*108322fbScarlsonj 			return (B_FALSE);
1253*108322fbScarlsonj 		}
1254*108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1255*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1256*108322fbScarlsonj 			return (B_FALSE);
1257*108322fbScarlsonj 		}
1258*108322fbScarlsonj 	}
1259*108322fbScarlsonj 
1260*108322fbScarlsonj 	/*
1261*108322fbScarlsonj 	 * This is here to support lucopy.  If there's an instance of this same
1262*108322fbScarlsonj 	 * zone on the current running system, then we mount its root up as
1263*108322fbScarlsonj 	 * read-only inside the scratch zone.
1264*108322fbScarlsonj 	 */
1265*108322fbScarlsonj 	(void) zonecfg_get_uuid(zone_name, uuid);
1266*108322fbScarlsonj 	altstr = strdup(zonecfg_get_root());
1267*108322fbScarlsonj 	if (altstr == NULL) {
1268*108322fbScarlsonj 		zerror(zlogp, B_TRUE, "out of memory");
1269*108322fbScarlsonj 		return (B_FALSE);
1270*108322fbScarlsonj 	}
1271*108322fbScarlsonj 	zonecfg_set_root("");
1272*108322fbScarlsonj 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1273*108322fbScarlsonj 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1274*108322fbScarlsonj 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1275*108322fbScarlsonj 	    strcmp(fromdir, rootpath) != 0) {
1276*108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1277*108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1278*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1279*108322fbScarlsonj 			return (B_FALSE);
1280*108322fbScarlsonj 		}
1281*108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir,
1282*108322fbScarlsonj 		    tmp) != 0) {
1283*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1284*108322fbScarlsonj 			    fromdir);
1285*108322fbScarlsonj 			return (B_FALSE);
1286*108322fbScarlsonj 		}
1287*108322fbScarlsonj 	}
1288*108322fbScarlsonj 	zonecfg_set_root(altstr);
1289*108322fbScarlsonj 	free(altstr);
1290*108322fbScarlsonj 
1291*108322fbScarlsonj 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1292*108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1293*108322fbScarlsonj 		return (B_FALSE);
1294*108322fbScarlsonj 	}
1295*108322fbScarlsonj 	(void) ftruncate(fileno(fp), 0);
1296*108322fbScarlsonj 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1297*108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1298*108322fbScarlsonj 	}
1299*108322fbScarlsonj 	zonecfg_close_scratch(fp);
1300*108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1301*108322fbScarlsonj 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1302*108322fbScarlsonj 		return (B_FALSE);
1303*108322fbScarlsonj 	(void) strlcpy(rootpath, tmp, rootlen);
1304*108322fbScarlsonj 	return (B_TRUE);
1305*108322fbScarlsonj }
1306*108322fbScarlsonj 
13077c478bd9Sstevel@tonic-gate static int
1308*108322fbScarlsonj mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	char	rootpath[MAXPATHLEN];
13117c478bd9Sstevel@tonic-gate 	char	zonepath[MAXPATHLEN];
13127c478bd9Sstevel@tonic-gate 	int	num_fs = 0, i;
13137c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab, *fs_ptr = NULL, *tmp_ptr;
13147c478bd9Sstevel@tonic-gate 	struct zone_fstab *fsp;
13157c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
13167c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1319*108322fbScarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
13207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1321*108322fbScarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1322*108322fbScarlsonj 		    zone_state_str(ZONE_STATE_READY),
1323*108322fbScarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
13247c478bd9Sstevel@tonic-gate 		goto bad;
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
13287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
13297c478bd9Sstevel@tonic-gate 		goto bad;
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
13337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
13347c478bd9Sstevel@tonic-gate 		goto bad;
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
13387c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
13397c478bd9Sstevel@tonic-gate 		    "could not get zone configuration handle");
13407c478bd9Sstevel@tonic-gate 		goto bad;
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
13437c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
13447c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
13457c478bd9Sstevel@tonic-gate 		goto bad;
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	/*
13497c478bd9Sstevel@tonic-gate 	 * /dev in the zone is loopback'd from the external /dev repository,
13507c478bd9Sstevel@tonic-gate 	 * in order to provide a largely read-only semantic.  But because
13517c478bd9Sstevel@tonic-gate 	 * processes in the zone need to be able to chown, chmod, etc. zone
13527c478bd9Sstevel@tonic-gate 	 * /dev files, we can't use a 'ro' lofs mount.  Instead we use a
13537c478bd9Sstevel@tonic-gate 	 * special mode just for zones, "zonedevfs".
13547c478bd9Sstevel@tonic-gate 	 *
13557c478bd9Sstevel@tonic-gate 	 * In the future we should front /dev with a full-fledged filesystem.
13567c478bd9Sstevel@tonic-gate 	 */
13577c478bd9Sstevel@tonic-gate 	num_fs++;
13587c478bd9Sstevel@tonic-gate 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
13597c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
13607c478bd9Sstevel@tonic-gate 		num_fs--;
13617c478bd9Sstevel@tonic-gate 		goto bad;
13627c478bd9Sstevel@tonic-gate 	}
13637c478bd9Sstevel@tonic-gate 	fs_ptr = tmp_ptr;
13647c478bd9Sstevel@tonic-gate 	fsp = &fs_ptr[num_fs - 1];
1365*108322fbScarlsonj 	/*
1366*108322fbScarlsonj 	 * Note that mount_one will prepend the alternate root to
1367*108322fbScarlsonj 	 * zone_fs_special and do the necessary resolution, so all that is
1368*108322fbScarlsonj 	 * needed here is to strip the root added by zone_get_zonepath.
1369*108322fbScarlsonj 	 */
13707c478bd9Sstevel@tonic-gate 	(void) strlcpy(fsp->zone_fs_dir, "/dev", sizeof (fsp->zone_fs_dir));
13717c478bd9Sstevel@tonic-gate 	(void) snprintf(fsp->zone_fs_special, sizeof (fsp->zone_fs_special),
1372*108322fbScarlsonj 	    "%s/dev", zonepath + strlen(zonecfg_get_root()));
13737c478bd9Sstevel@tonic-gate 	fsp->zone_fs_raw[0] = '\0';
13747c478bd9Sstevel@tonic-gate 	(void) strlcpy(fsp->zone_fs_type, MNTTYPE_LOFS,
13757c478bd9Sstevel@tonic-gate 	    sizeof (fsp->zone_fs_type));
13767c478bd9Sstevel@tonic-gate 	fsp->zone_fs_options = NULL;
13777c478bd9Sstevel@tonic-gate 	if (zonecfg_add_fs_option(fsp, MNTOPT_LOFS_ZONEDEVFS) != Z_OK) {
13787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "error adding property");
13797c478bd9Sstevel@tonic-gate 		goto bad;
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	/*
13837c478bd9Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
13847c478bd9Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
13857c478bd9Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
13867c478bd9Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
13877c478bd9Sstevel@tonic-gate 	 */
13887c478bd9Sstevel@tonic-gate 	if (zonecfg_setipdent(handle) != Z_OK) {
13897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
13907c478bd9Sstevel@tonic-gate 		goto bad;
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
13937c478bd9Sstevel@tonic-gate 		num_fs++;
13947c478bd9Sstevel@tonic-gate 		if ((tmp_ptr = realloc(fs_ptr,
13957c478bd9Sstevel@tonic-gate 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
13967c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
13977c478bd9Sstevel@tonic-gate 			num_fs--;
13987c478bd9Sstevel@tonic-gate 			(void) zonecfg_endipdent(handle);
13997c478bd9Sstevel@tonic-gate 			goto bad;
14007c478bd9Sstevel@tonic-gate 		}
14017c478bd9Sstevel@tonic-gate 		fs_ptr = tmp_ptr;
14027c478bd9Sstevel@tonic-gate 		fsp = &fs_ptr[num_fs - 1];
14037c478bd9Sstevel@tonic-gate 		/*
14047c478bd9Sstevel@tonic-gate 		 * IPDs logically only have a mount point; all other properties
14057c478bd9Sstevel@tonic-gate 		 * are implied.
14067c478bd9Sstevel@tonic-gate 		 */
14077c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_dir,
14087c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
14097c478bd9Sstevel@tonic-gate 		fsp->zone_fs_special[0] = '\0';
14107c478bd9Sstevel@tonic-gate 		fsp->zone_fs_raw[0] = '\0';
14117c478bd9Sstevel@tonic-gate 		fsp->zone_fs_type[0] = '\0';
14127c478bd9Sstevel@tonic-gate 		fsp->zone_fs_options = NULL;
14137c478bd9Sstevel@tonic-gate 	}
14147c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
14177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
14187c478bd9Sstevel@tonic-gate 		goto bad;
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
14217c478bd9Sstevel@tonic-gate 		num_fs++;
14227c478bd9Sstevel@tonic-gate 		if ((tmp_ptr = realloc(fs_ptr,
14237c478bd9Sstevel@tonic-gate 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
14247c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
14257c478bd9Sstevel@tonic-gate 			num_fs--;
14267c478bd9Sstevel@tonic-gate 			(void) zonecfg_endfsent(handle);
14277c478bd9Sstevel@tonic-gate 			goto bad;
14287c478bd9Sstevel@tonic-gate 		}
14297c478bd9Sstevel@tonic-gate 		fs_ptr = tmp_ptr;
14307c478bd9Sstevel@tonic-gate 		fsp = &fs_ptr[num_fs - 1];
14317c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_dir,
14327c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
14337c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special,
14347c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_special));
14357c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
14367c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_raw));
14377c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
14387c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_type));
14397c478bd9Sstevel@tonic-gate 		fsp->zone_fs_options = fstab.zone_fs_options;
14407c478bd9Sstevel@tonic-gate 	}
14417c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
14427c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
14437c478bd9Sstevel@tonic-gate 	handle = NULL;
14447c478bd9Sstevel@tonic-gate 
1445*108322fbScarlsonj 	/*
1446*108322fbScarlsonj 	 * If we're mounting a zone for administration, then we need to set up
1447*108322fbScarlsonj 	 * the "/a" environment inside the zone so that the commands that run
1448*108322fbScarlsonj 	 * in there have access to both the running system's utilities and the
1449*108322fbScarlsonj 	 * to-be-modified zone's files.
1450*108322fbScarlsonj 	 */
1451*108322fbScarlsonj 	if (mount_cmd &&
1452*108322fbScarlsonj 	    !build_mounted(zlogp, rootpath, sizeof (rootpath), zonepath))
1453*108322fbScarlsonj 		goto bad;
1454*108322fbScarlsonj 
14557c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
14567c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
1457*108322fbScarlsonj 		if (mount_cmd && strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
1458*108322fbScarlsonj 			size_t slen = strlen(rootpath) - 2;
1459*108322fbScarlsonj 
1460*108322fbScarlsonj 			/* /dev is special and always goes at the top */
1461*108322fbScarlsonj 			rootpath[slen] = '\0';
1462*108322fbScarlsonj 			if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
1463*108322fbScarlsonj 				goto bad;
1464*108322fbScarlsonj 			rootpath[slen] = '/';
1465*108322fbScarlsonj 			continue;
1466*108322fbScarlsonj 		}
14677c478bd9Sstevel@tonic-gate 		if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
14687c478bd9Sstevel@tonic-gate 			goto bad;
14697c478bd9Sstevel@tonic-gate 	}
14707c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	/*
14737c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
14747c478bd9Sstevel@tonic-gate 	 */
14757c478bd9Sstevel@tonic-gate 	return (0);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate bad:
14787c478bd9Sstevel@tonic-gate 	if (handle != NULL)
14797c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
14807c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
14817c478bd9Sstevel@tonic-gate 	return (-1);
14827c478bd9Sstevel@tonic-gate }
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
14857c478bd9Sstevel@tonic-gate static int
14867c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	int prefixlen;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
14917c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
14927c478bd9Sstevel@tonic-gate 		return (1);
14937c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
14947c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
14957c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
14967c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
14977c478bd9Sstevel@tonic-gate 			continue;
14987c478bd9Sstevel@tonic-gate 		}
14997c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
15007c478bd9Sstevel@tonic-gate 		prefixlen--;
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 	return (0);
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate /*
15067c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
15077c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
15087c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
15097c478bd9Sstevel@tonic-gate  *
15107c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
15117c478bd9Sstevel@tonic-gate  */
15127c478bd9Sstevel@tonic-gate static int
15137c478bd9Sstevel@tonic-gate unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
15147c478bd9Sstevel@tonic-gate {
15157c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
15167c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
15177c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
15187c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
15197c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
15207c478bd9Sstevel@tonic-gate 	uint_t bufsize;
15217c478bd9Sstevel@tonic-gate 	char *buf = NULL;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
15247c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
15257c478bd9Sstevel@tonic-gate 		ret_code = -1;
15267c478bd9Sstevel@tonic-gate 		goto bad;
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
15297c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
15307c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
15317c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
15327c478bd9Sstevel@tonic-gate 		    "could not determine number of interfaces");
15337c478bd9Sstevel@tonic-gate 		ret_code = -1;
15347c478bd9Sstevel@tonic-gate 		goto bad;
15357c478bd9Sstevel@tonic-gate 	}
15367c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
15377c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
15387c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
15397c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
15407c478bd9Sstevel@tonic-gate 		ret_code = -1;
15417c478bd9Sstevel@tonic-gate 		goto bad;
15427c478bd9Sstevel@tonic-gate 	}
15437c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
15447c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
15457c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
15467c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
15477c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
15487c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get configured interfaces");
15497c478bd9Sstevel@tonic-gate 		ret_code = -1;
15507c478bd9Sstevel@tonic-gate 		goto bad;
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
15537c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
15547c478bd9Sstevel@tonic-gate 		(void) close(s);
15557c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
15567c478bd9Sstevel@tonic-gate 		    0) {
15577c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
15587c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
15597c478bd9Sstevel@tonic-gate 			ret_code = -1;
15607c478bd9Sstevel@tonic-gate 			continue;
15617c478bd9Sstevel@tonic-gate 		}
15627c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
15637c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
15647c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
15657c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
15667c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
15677c478bd9Sstevel@tonic-gate 			    "%s: could not determine zone interface belongs to",
15687c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
15697c478bd9Sstevel@tonic-gate 			ret_code = -1;
15707c478bd9Sstevel@tonic-gate 			continue;
15717c478bd9Sstevel@tonic-gate 		}
15727c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
15737c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
15747c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
15757c478bd9Sstevel@tonic-gate 				    "%s: could not remove interface",
15767c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
15777c478bd9Sstevel@tonic-gate 				ret_code = -1;
15787c478bd9Sstevel@tonic-gate 				continue;
15797c478bd9Sstevel@tonic-gate 			}
15807c478bd9Sstevel@tonic-gate 		}
15817c478bd9Sstevel@tonic-gate 	}
15827c478bd9Sstevel@tonic-gate bad:
15837c478bd9Sstevel@tonic-gate 	if (s > 0)
15847c478bd9Sstevel@tonic-gate 		(void) close(s);
15857c478bd9Sstevel@tonic-gate 	if (buf)
15867c478bd9Sstevel@tonic-gate 		free(buf);
15877c478bd9Sstevel@tonic-gate 	return (ret_code);
15887c478bd9Sstevel@tonic-gate }
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate static union	sockunion {
15917c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
15927c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
15937c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
15947c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
15957c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate static struct {
15987c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
15997c478bd9Sstevel@tonic-gate 	char	space[512];
16007c478bd9Sstevel@tonic-gate } rtmsg;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate static int
16037c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
16067c478bd9Sstevel@tonic-gate 	case AF_INET:
16077c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
16087c478bd9Sstevel@tonic-gate 	case AF_LINK:
16097c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
16107c478bd9Sstevel@tonic-gate 	case AF_INET6:
16117c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
16127c478bd9Sstevel@tonic-gate 	default:
16137c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
16147c478bd9Sstevel@tonic-gate 	}
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
16187c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate /*
16217c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
16227c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
16237c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
16247c478bd9Sstevel@tonic-gate  *
16257c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
16267c478bd9Sstevel@tonic-gate  *
16277c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
16287c478bd9Sstevel@tonic-gate  *
16297c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
16307c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
16317c478bd9Sstevel@tonic-gate  */
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate static char *
16347c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
16357c478bd9Sstevel@tonic-gate {
16367c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
16377c478bd9Sstevel@tonic-gate 	pid_t pid;
16387c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
16397c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
16407c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
16417c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
16427c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	pid = getpid();
16477c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
16487c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
16497c478bd9Sstevel@tonic-gate 		return (NULL);
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
16537c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
16567c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
16577c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
16587c478bd9Sstevel@tonic-gate 	} else {
16597c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
16627c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
16637c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
16647c478bd9Sstevel@tonic-gate 	}
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
16697c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
16707c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
16717c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
16727c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
16737c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
16767c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
16777c478bd9Sstevel@tonic-gate 	cp += l;
16787c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
16797c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
16807c478bd9Sstevel@tonic-gate 	cp += l;
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
16857c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
16867c478bd9Sstevel@tonic-gate 		return (NULL);
16877c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
16887c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
16897c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
16907c478bd9Sstevel@tonic-gate 		return (NULL);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 	do {
16937c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
16947c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
16957c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
16967c478bd9Sstevel@tonic-gate 	if (l < 0) {
16977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
16987c478bd9Sstevel@tonic-gate 		return (NULL);
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
17027c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
17037c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
17047c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
17057c478bd9Sstevel@tonic-gate 		return (NULL);
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
17087c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
17097c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
17107c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
17117c478bd9Sstevel@tonic-gate 		return (NULL);
17127c478bd9Sstevel@tonic-gate 	}
17137c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
17147c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
17157c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
17167c478bd9Sstevel@tonic-gate 		return (NULL);
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
17197c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "interface not found");
17207c478bd9Sstevel@tonic-gate 		return (NULL);
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
17237c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
17247c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
17257c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
17267c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
17277c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
17287c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
17297c478bd9Sstevel@tonic-gate 			continue;
17307c478bd9Sstevel@tonic-gate 		}
17317c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
17327c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
17337c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
17347c478bd9Sstevel@tonic-gate 		break;
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
17377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "interface could not be determined");
17387c478bd9Sstevel@tonic-gate 		return (NULL);
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	/*
17427c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
17437c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
17447c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
17457c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
17467c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
17477c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
17487c478bd9Sstevel@tonic-gate 	 */
17497c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
17507c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
17517c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
17527c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
17537c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
17547c478bd9Sstevel@tonic-gate 	if (i < 0) {
17557c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
17567c478bd9Sstevel@tonic-gate 		    "%s: could not determine the zone interface belongs to",
17577c478bd9Sstevel@tonic-gate 		    lifr->lifr_name);
17587c478bd9Sstevel@tonic-gate 		return (NULL);
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
17617c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
17627c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
17657c478bd9Sstevel@tonic-gate 		return (answer);
17667c478bd9Sstevel@tonic-gate 	return (NULL);
17677c478bd9Sstevel@tonic-gate }
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate typedef struct mcast_rtmsg_s {
17707c478bd9Sstevel@tonic-gate 	struct rt_msghdr	m_rtm;
17717c478bd9Sstevel@tonic-gate 	union {
17727c478bd9Sstevel@tonic-gate 		struct {
17737c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_dst;
17747c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_gw;
17757c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_netmask;
17767c478bd9Sstevel@tonic-gate 		} m_v4;
17777c478bd9Sstevel@tonic-gate 		struct {
17787c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_dst;
17797c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_gw;
17807c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_netmask;
17817c478bd9Sstevel@tonic-gate 		} m_v6;
17827c478bd9Sstevel@tonic-gate 	} m_u;
17837c478bd9Sstevel@tonic-gate } mcast_rtmsg_t;
17847c478bd9Sstevel@tonic-gate #define	m_dst4		m_u.m_v4.m_dst
17857c478bd9Sstevel@tonic-gate #define	m_dst6		m_u.m_v6.m_dst
17867c478bd9Sstevel@tonic-gate #define	m_gw4		m_u.m_v4.m_gw
17877c478bd9Sstevel@tonic-gate #define	m_gw6		m_u.m_v6.m_gw
17887c478bd9Sstevel@tonic-gate #define	m_netmask4	m_u.m_v4.m_netmask
17897c478bd9Sstevel@tonic-gate #define	m_netmask6	m_u.m_v6.m_netmask
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate /*
17927c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
17937c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
17947c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
17957c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
17967c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
17977c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
17987c478bd9Sstevel@tonic-gate  *
17997c478bd9Sstevel@tonic-gate  * A default interface route for multicast is created on the first IPv4 and
18007c478bd9Sstevel@tonic-gate  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
18017c478bd9Sstevel@tonic-gate  * This should really be done in the init scripts if we ever allow zones to
18027c478bd9Sstevel@tonic-gate  * modify the routing tables.
18037c478bd9Sstevel@tonic-gate  *
18047c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
18057c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
18067c478bd9Sstevel@tonic-gate  */
18077c478bd9Sstevel@tonic-gate static int
18087c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
18097c478bd9Sstevel@tonic-gate     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
18107c478bd9Sstevel@tonic-gate     boolean_t *mcast_rt_v6_setp)
18117c478bd9Sstevel@tonic-gate {
18127c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
18137c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
18147c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
18157c478bd9Sstevel@tonic-gate 	struct in_addr in4;
18167c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
18177c478bd9Sstevel@tonic-gate 	sa_family_t af;
18187c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
18197c478bd9Sstevel@tonic-gate 	mcast_rtmsg_t mcast_rtmsg;
18207c478bd9Sstevel@tonic-gate 	int s;
18217c478bd9Sstevel@tonic-gate 	int rs;
18227c478bd9Sstevel@tonic-gate 	int rlen;
18237c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
18247c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
18257c478bd9Sstevel@tonic-gate 	int res;
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
18287c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
18297c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
18307c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
18317c478bd9Sstevel@tonic-gate 		return (-1);
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
18347c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
18357c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
18367c478bd9Sstevel@tonic-gate 	else
18377c478bd9Sstevel@tonic-gate 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
18407c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
18417c478bd9Sstevel@tonic-gate 		return (-1);
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
18457c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
18467c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
18477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not add interface",
18487c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
18497c478bd9Sstevel@tonic-gate 		(void) close(s);
18507c478bd9Sstevel@tonic-gate 		return (-1);
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
18547c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
18557c478bd9Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
18567c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
18577c478bd9Sstevel@tonic-gate 		goto bad;
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
18617c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
18627c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
18657c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
18667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not place interface into zone",
18677c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
18687c478bd9Sstevel@tonic-gate 		goto bad;
18697c478bd9Sstevel@tonic-gate 	}
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
18727c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
18737c478bd9Sstevel@tonic-gate 	} else {
18747c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
18757c478bd9Sstevel@tonic-gate 			/*
18767c478bd9Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
18777c478bd9Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
18787c478bd9Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
18797c478bd9Sstevel@tonic-gate 			 * being able to determine it is a common failure,
18807c478bd9Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
18817c478bd9Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
18827c478bd9Sstevel@tonic-gate 			 * printed after the rest of the interface's
18837c478bd9Sstevel@tonic-gate 			 * parameters have been configured.
18847c478bd9Sstevel@tonic-gate 			 */
18857c478bd9Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
18867c478bd9Sstevel@tonic-gate 			if (slashp != NULL) {
18877c478bd9Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
18887c478bd9Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
18897c478bd9Sstevel@tonic-gate 					*slashp = '/';
18907c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
18917c478bd9Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
18927c478bd9Sstevel@tonic-gate 					    lifr.lifr_name,
18937c478bd9Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
18947c478bd9Sstevel@tonic-gate 					goto bad;
18957c478bd9Sstevel@tonic-gate 				}
18967c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
18977c478bd9Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
18987c478bd9Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
18997c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
19007c478bd9Sstevel@tonic-gate 			}
19017c478bd9Sstevel@tonic-gate 			if (got_netmask) {
19027c478bd9Sstevel@tonic-gate 				netmask4.sin_family = af;
19037c478bd9Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
19047c478bd9Sstevel@tonic-gate 				    sizeof (netmask4));
19057c478bd9Sstevel@tonic-gate 			}
19067c478bd9Sstevel@tonic-gate 		} else {
19077c478bd9Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
19087c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
19097c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
19107c478bd9Sstevel@tonic-gate 				*slashp = '/';
19117c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
19127c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
19137c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
19147c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
19157c478bd9Sstevel@tonic-gate 				goto bad;
19167c478bd9Sstevel@tonic-gate 			}
19177c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
19187c478bd9Sstevel@tonic-gate 			netmask6.sin6_family = af;
19197c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
19207c478bd9Sstevel@tonic-gate 			    sizeof (netmask6));
19217c478bd9Sstevel@tonic-gate 		}
19227c478bd9Sstevel@tonic-gate 		if (got_netmask &&
19237c478bd9Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
19247c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
19257c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
19267c478bd9Sstevel@tonic-gate 			goto bad;
19277c478bd9Sstevel@tonic-gate 		}
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 		/*
19307c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
19317c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
19327c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
19337c478bd9Sstevel@tonic-gate 		 */
19347c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
19357c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
19367c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
19377c478bd9Sstevel@tonic-gate 			goto bad;
19387c478bd9Sstevel@tonic-gate 		}
19397c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
19407c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
19417c478bd9Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
19427c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
19437c478bd9Sstevel@tonic-gate 			goto bad;
19447c478bd9Sstevel@tonic-gate 		}
19457c478bd9Sstevel@tonic-gate 	}
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
19487c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
19497c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
19507c478bd9Sstevel@tonic-gate 		goto bad;
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
19537c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
19547c478bd9Sstevel@tonic-gate 		int save_errno = errno;
19557c478bd9Sstevel@tonic-gate 		char *zone_using;
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 		/*
19587c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
19597c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
19607c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
19617c478bd9Sstevel@tonic-gate 		 * using that address.
19627c478bd9Sstevel@tonic-gate 		 */
19637c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
19647c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
19657c478bd9Sstevel@tonic-gate 			    "%s: could not bring interface up", lifr.lifr_name);
19667c478bd9Sstevel@tonic-gate 			goto bad;
19677c478bd9Sstevel@tonic-gate 		}
19687c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
19697c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
19707c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
19717c478bd9Sstevel@tonic-gate 			goto bad;
19727c478bd9Sstevel@tonic-gate 		}
19737c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
19747c478bd9Sstevel@tonic-gate 		errno = save_errno;
19757c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
19767c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
19777c478bd9Sstevel@tonic-gate 			    "%s: could not bring interface up", lifr.lifr_name);
19787c478bd9Sstevel@tonic-gate 		else
19797c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not bring interface "
19807c478bd9Sstevel@tonic-gate 			    "up: address in use by zone '%s'", lifr.lifr_name,
19817c478bd9Sstevel@tonic-gate 			    zone_using);
19827c478bd9Sstevel@tonic-gate 		goto bad;
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
19857c478bd9Sstevel@tonic-gate 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
19867c478bd9Sstevel@tonic-gate 	    (af == AF_INET6 &&
19877c478bd9Sstevel@tonic-gate 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
19887c478bd9Sstevel@tonic-gate 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
19897c478bd9Sstevel@tonic-gate 		if (rs < 0) {
19907c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not create "
19917c478bd9Sstevel@tonic-gate 			    "routing socket", lifr.lifr_name);
19927c478bd9Sstevel@tonic-gate 			goto bad;
19937c478bd9Sstevel@tonic-gate 		}
19947c478bd9Sstevel@tonic-gate 		(void) shutdown(rs, 0);
19957c478bd9Sstevel@tonic-gate 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
19967c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
19977c478bd9Sstevel@tonic-gate 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
19987c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
19997c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
20007c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
20017c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
20027c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_addrs =
20037c478bd9Sstevel@tonic-gate 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
20047c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
20057c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
20067c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
20077c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
20087c478bd9Sstevel@tonic-gate 			    htonl(INADDR_UNSPEC_GROUP);
20097c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
20107c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_addr = in4;
20117c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
20127c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
20137c478bd9Sstevel@tonic-gate 			    htonl(IN_CLASSD_NET);
20147c478bd9Sstevel@tonic-gate 		} else {
20157c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
20167c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
20177c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
20187c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_addr = in6;
20197c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
20207c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
20217c478bd9Sstevel@tonic-gate 		}
20227c478bd9Sstevel@tonic-gate 		rlen = write(rs, (char *)&mcast_rtmsg,
20237c478bd9Sstevel@tonic-gate 		    mcast_rtmsg.m_rtm.rtm_msglen);
20247c478bd9Sstevel@tonic-gate 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
20257c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
20267c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s: could not set "
20277c478bd9Sstevel@tonic-gate 				    "default interface for multicast",
20287c478bd9Sstevel@tonic-gate 				    lifr.lifr_name);
20297c478bd9Sstevel@tonic-gate 			} else {
20307c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s: write to routing "
20317c478bd9Sstevel@tonic-gate 				    "socket returned %d", lifr.lifr_name, rlen);
20327c478bd9Sstevel@tonic-gate 			}
20337c478bd9Sstevel@tonic-gate 			(void) close(rs);
20347c478bd9Sstevel@tonic-gate 			goto bad;
20357c478bd9Sstevel@tonic-gate 		}
20367c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
20377c478bd9Sstevel@tonic-gate 			*mcast_rt_v4_setp = B_TRUE;
20387c478bd9Sstevel@tonic-gate 		} else {
20397c478bd9Sstevel@tonic-gate 			*mcast_rt_v6_setp = B_TRUE;
20407c478bd9Sstevel@tonic-gate 		}
20417c478bd9Sstevel@tonic-gate 		(void) close(rs);
20427c478bd9Sstevel@tonic-gate 	}
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	if (!got_netmask) {
20457c478bd9Sstevel@tonic-gate 		/*
20467c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
20477c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
20487c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
20497c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
20507c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
20517c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
20527c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
20537c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
20547c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
20557c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
20567c478bd9Sstevel@tonic-gate 		 */
20577c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
20587c478bd9Sstevel@tonic-gate 		void  *addr;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
20617c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
20627c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
20637c478bd9Sstevel@tonic-gate 		else
20647c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
20657c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
20687c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
20697c478bd9Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
20707c478bd9Sstevel@tonic-gate 			goto bad;
20717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
20727c478bd9Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
20737c478bd9Sstevel@tonic-gate 		    "%s; using default of %s.",
20747c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	(void) close(s);
20787c478bd9Sstevel@tonic-gate 	return (Z_OK);
20797c478bd9Sstevel@tonic-gate bad:
20807c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
20817c478bd9Sstevel@tonic-gate 	(void) close(s);
20827c478bd9Sstevel@tonic-gate 	return (-1);
20837c478bd9Sstevel@tonic-gate }
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate /*
20867c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
20877c478bd9Sstevel@tonic-gate  * An IPv4 loopback interface is set up "for free", modeling the global system.
20887c478bd9Sstevel@tonic-gate  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
20897c478bd9Sstevel@tonic-gate  * address is set up as well.
20907c478bd9Sstevel@tonic-gate  *
20917c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
20927c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
20937c478bd9Sstevel@tonic-gate  */
20947c478bd9Sstevel@tonic-gate static int
20957c478bd9Sstevel@tonic-gate configure_network_interfaces(zlog_t *zlogp)
20967c478bd9Sstevel@tonic-gate {
20977c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
20987c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
20997c478bd9Sstevel@tonic-gate 	boolean_t saw_v6 = B_FALSE;
21007c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v4_set = B_FALSE;
21017c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v6_set = B_FALSE;
21027c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
21057c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
21067c478bd9Sstevel@tonic-gate 		return (-1);
21077c478bd9Sstevel@tonic-gate 	}
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
21107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
21117c478bd9Sstevel@tonic-gate 		return (-1);
21127c478bd9Sstevel@tonic-gate 	}
21137c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
21147c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
21157c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
21167c478bd9Sstevel@tonic-gate 		return (-1);
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
21197c478bd9Sstevel@tonic-gate 		for (;;) {
21207c478bd9Sstevel@tonic-gate 			struct in6_addr in6;
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
21237c478bd9Sstevel@tonic-gate 				break;
21247c478bd9Sstevel@tonic-gate 			if (configure_one_interface(zlogp, zoneid,
21257c478bd9Sstevel@tonic-gate 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
21267c478bd9Sstevel@tonic-gate 			    Z_OK) {
21277c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
21287c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
21297c478bd9Sstevel@tonic-gate 				return (-1);
21307c478bd9Sstevel@tonic-gate 			}
21317c478bd9Sstevel@tonic-gate 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
21327c478bd9Sstevel@tonic-gate 			    &in6) == 1)
21337c478bd9Sstevel@tonic-gate 				saw_v6 = B_TRUE;
21347c478bd9Sstevel@tonic-gate 		}
21357c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
21387c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
21397c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
21407c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
21417c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
21427c478bd9Sstevel@tonic-gate 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
21437c478bd9Sstevel@tonic-gate 	    != Z_OK) {
21447c478bd9Sstevel@tonic-gate 		return (-1);
21457c478bd9Sstevel@tonic-gate 	}
21467c478bd9Sstevel@tonic-gate 	if (saw_v6) {
21477c478bd9Sstevel@tonic-gate 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
21487c478bd9Sstevel@tonic-gate 		    sizeof (loopback_iftab.zone_nwif_address));
21497c478bd9Sstevel@tonic-gate 		if (configure_one_interface(zlogp, zoneid,
21507c478bd9Sstevel@tonic-gate 		    &loopback_iftab, NULL, NULL) != Z_OK) {
21517c478bd9Sstevel@tonic-gate 			return (-1);
21527c478bd9Sstevel@tonic-gate 		}
21537c478bd9Sstevel@tonic-gate 	}
21547c478bd9Sstevel@tonic-gate 	return (0);
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate static int
21587c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
21597c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
21607c478bd9Sstevel@tonic-gate {
21617c478bd9Sstevel@tonic-gate 	int fd;
21627c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
21637c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
21647c478bd9Sstevel@tonic-gate 	int error;
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
21677c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
21687c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
21697c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
21707c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
21737c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
21747c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
21757c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
21787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
21797c478bd9Sstevel@tonic-gate 		return (-1);
21807c478bd9Sstevel@tonic-gate 	}
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
21837c478bd9Sstevel@tonic-gate 	(void) close(fd);
21847c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
21857c478bd9Sstevel@tonic-gate 		return (0);
21867c478bd9Sstevel@tonic-gate 	return (-1);
21877c478bd9Sstevel@tonic-gate }
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate static int
21907c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
21917c478bd9Sstevel@tonic-gate {
21927c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
21937c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
21947c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
21957c478bd9Sstevel@tonic-gate 	int error;
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	/*
21987c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
21997c478bd9Sstevel@tonic-gate 	 */
22007c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
22017c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
22027c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
22037c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
22047c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
22077c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
22087c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
22097c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
22107c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
22137c478bd9Sstevel@tonic-gate 		return (error);
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	/*
22167c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
22177c478bd9Sstevel@tonic-gate 	 */
22187c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
22197c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
22207c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
22217c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
22227c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
22257c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
22267c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
22277c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
22287c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
22317c478bd9Sstevel@tonic-gate 		return (error);
22327c478bd9Sstevel@tonic-gate 	return (0);
22337c478bd9Sstevel@tonic-gate }
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate static int
22367c478bd9Sstevel@tonic-gate devfsadm_call(zlog_t *zlogp, const char *arg)
22377c478bd9Sstevel@tonic-gate {
22387c478bd9Sstevel@tonic-gate 	char *argv[4];
22397c478bd9Sstevel@tonic-gate 	int status;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	argv[0] = DEVFSADM;
22427c478bd9Sstevel@tonic-gate 	argv[1] = (char *)arg;
22437c478bd9Sstevel@tonic-gate 	argv[2] = zone_name;
22447c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
22457c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, DEVFSADM_PATH, argv);
22467c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
22477c478bd9Sstevel@tonic-gate 		return (status);
22487c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "%s call (%s %s %s) unexpectedly returned %d",
22497c478bd9Sstevel@tonic-gate 		    DEVFSADM, DEVFSADM_PATH, arg, zone_name, status);
22507c478bd9Sstevel@tonic-gate 	return (-1);
22517c478bd9Sstevel@tonic-gate }
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate static int
22547c478bd9Sstevel@tonic-gate devfsadm_register(zlog_t *zlogp)
22557c478bd9Sstevel@tonic-gate {
22567c478bd9Sstevel@tonic-gate 	/*
22577c478bd9Sstevel@tonic-gate 	 * Ready the zone's devices.
22587c478bd9Sstevel@tonic-gate 	 */
22597c478bd9Sstevel@tonic-gate 	return (devfsadm_call(zlogp, "-z"));
22607c478bd9Sstevel@tonic-gate }
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate static int
22637c478bd9Sstevel@tonic-gate devfsadm_unregister(zlog_t *zlogp)
22647c478bd9Sstevel@tonic-gate {
22657c478bd9Sstevel@tonic-gate 	return (devfsadm_call(zlogp, "-Z"));
22667c478bd9Sstevel@tonic-gate }
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate static int
22697c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
22707c478bd9Sstevel@tonic-gate {
22717c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
22727c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
22737c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
22747c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
22757c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
22767c478bd9Sstevel@tonic-gate 	int error = -1;
22777c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
22787c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
22797c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	*bufp = NULL;
22827c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
22857c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
22867c478bd9Sstevel@tonic-gate 		return (-1);
22877c478bd9Sstevel@tonic-gate 	}
22887c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
22897c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
22907c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
22917c478bd9Sstevel@tonic-gate 		return (-1);
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
22957c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
22967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
22977c478bd9Sstevel@tonic-gate 		goto out;
22987c478bd9Sstevel@tonic-gate 	}
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
23017c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
23027c478bd9Sstevel@tonic-gate 		goto out;
23037c478bd9Sstevel@tonic-gate 	}
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
23067c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
23077c478bd9Sstevel@tonic-gate 		goto out;
23087c478bd9Sstevel@tonic-gate 	}
23097c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
23107c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
23117c478bd9Sstevel@tonic-gate 		uint_t i, count;
23127c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
23157c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
23167c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
23177c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
23187c478bd9Sstevel@tonic-gate 			continue;
23197c478bd9Sstevel@tonic-gate 		}
23207c478bd9Sstevel@tonic-gate 		count = 0;
23217c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
23227c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
23237c478bd9Sstevel@tonic-gate 			count++;
23247c478bd9Sstevel@tonic-gate 		}
23257c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
23267c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
23277c478bd9Sstevel@tonic-gate 		}
23287c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
23297c478bd9Sstevel@tonic-gate 			goto out;
23307c478bd9Sstevel@tonic-gate 		i = 0;
23317c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
23327c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
23337c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
23347c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
23357c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
23367c478bd9Sstevel@tonic-gate 				goto out;
23377c478bd9Sstevel@tonic-gate 			}
23387c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
23397c478bd9Sstevel@tonic-gate 			    != Z_OK) {
23407c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
23417c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
23427c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
23437c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
23447c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
23457c478bd9Sstevel@tonic-gate 				goto out;
23467c478bd9Sstevel@tonic-gate 			}
23477c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
23487c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
23497c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
23507c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
23517c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
23527c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
23537c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
23547c478bd9Sstevel@tonic-gate 				    name);
23557c478bd9Sstevel@tonic-gate 				goto out;
23567c478bd9Sstevel@tonic-gate 			}
23577c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
23587c478bd9Sstevel@tonic-gate 				    rctlblk_get_privilege(rctlblk)) != 0) {
23597c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
23607c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
23617c478bd9Sstevel@tonic-gate 				goto out;
23627c478bd9Sstevel@tonic-gate 			}
23637c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
23647c478bd9Sstevel@tonic-gate 				    rctlblk_get_value(rctlblk)) != 0) {
23657c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
23667c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
23677c478bd9Sstevel@tonic-gate 				goto out;
23687c478bd9Sstevel@tonic-gate 			}
23697c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
23707c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
23717c478bd9Sstevel@tonic-gate 			    != 0) {
23727c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
23737c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
23747c478bd9Sstevel@tonic-gate 				goto out;
23757c478bd9Sstevel@tonic-gate 			}
23767c478bd9Sstevel@tonic-gate 		}
23777c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
23787c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
23797c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
23807c478bd9Sstevel@tonic-gate 		    != 0) {
23817c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
23827c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
23837c478bd9Sstevel@tonic-gate 			goto out;
23847c478bd9Sstevel@tonic-gate 		}
23857c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
23867c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
23877c478bd9Sstevel@tonic-gate 		free(nvlv);
23887c478bd9Sstevel@tonic-gate 		nvlv = NULL;
23897c478bd9Sstevel@tonic-gate 		rctlcount++;
23907c478bd9Sstevel@tonic-gate 	}
23917c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
23947c478bd9Sstevel@tonic-gate 		error = 0;
23957c478bd9Sstevel@tonic-gate 		goto out;
23967c478bd9Sstevel@tonic-gate 	}
23977c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
23987c478bd9Sstevel@tonic-gate 	    != 0) {
23997c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
24007c478bd9Sstevel@tonic-gate 		goto out;
24017c478bd9Sstevel@tonic-gate 	}
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	error = 0;
24047c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
24057c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate out:
24087c478bd9Sstevel@tonic-gate 	free(rctlblk);
24097c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
24107c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
24117c478bd9Sstevel@tonic-gate 		free(nvl_packed);
24127c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
24137c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
24147c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
24157c478bd9Sstevel@tonic-gate 		free(nvlv);
24167c478bd9Sstevel@tonic-gate 	if (handle != NULL)
24177c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24187c478bd9Sstevel@tonic-gate 	return (error);
24197c478bd9Sstevel@tonic-gate }
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate static int
24227c478bd9Sstevel@tonic-gate get_zone_pool(zlog_t *zlogp, char *poolbuf, size_t bufsz)
24237c478bd9Sstevel@tonic-gate {
24247c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24257c478bd9Sstevel@tonic-gate 	int error;
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24287c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24297c478bd9Sstevel@tonic-gate 		return (-1);
24307c478bd9Sstevel@tonic-gate 	}
24317c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24327c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24337c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24347c478bd9Sstevel@tonic-gate 		return (-1);
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolbuf, bufsz);
24377c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
24387c478bd9Sstevel@tonic-gate 	return (error);
24397c478bd9Sstevel@tonic-gate }
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate static int
24427c478bd9Sstevel@tonic-gate bind_to_pool(zlog_t *zlogp, zoneid_t zoneid)
24437c478bd9Sstevel@tonic-gate {
24447c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
24457c478bd9Sstevel@tonic-gate 	pool_t *pool;
24467c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
24477c478bd9Sstevel@tonic-gate 	int status;
24487c478bd9Sstevel@tonic-gate 	int error;
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 	/*
24517c478bd9Sstevel@tonic-gate 	 * Find the pool mentioned in the zone configuration, and bind to it.
24527c478bd9Sstevel@tonic-gate 	 */
24537c478bd9Sstevel@tonic-gate 	error = get_zone_pool(zlogp, poolname, sizeof (poolname));
24547c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
24557c478bd9Sstevel@tonic-gate 		/*
24567c478bd9Sstevel@tonic-gate 		 * The property is not set on the zone, so the pool
24577c478bd9Sstevel@tonic-gate 		 * should be bound to the default pool.  But that's
24587c478bd9Sstevel@tonic-gate 		 * already done by the kernel, so we can just return.
24597c478bd9Sstevel@tonic-gate 		 */
24607c478bd9Sstevel@tonic-gate 		return (0);
24617c478bd9Sstevel@tonic-gate 	}
24627c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
24637c478bd9Sstevel@tonic-gate 		/*
24647c478bd9Sstevel@tonic-gate 		 * Not an error, even though it shouldn't be happening.
24657c478bd9Sstevel@tonic-gate 		 */
24667c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
24677c478bd9Sstevel@tonic-gate 		    "WARNING: unable to retrieve default pool.");
24687c478bd9Sstevel@tonic-gate 		return (0);
24697c478bd9Sstevel@tonic-gate 	}
24707c478bd9Sstevel@tonic-gate 	/*
24717c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
24727c478bd9Sstevel@tonic-gate 	 */
24737c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
24747c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: pools facility not active; "
24757c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'.", poolname);
24767c478bd9Sstevel@tonic-gate 		return (0);
24777c478bd9Sstevel@tonic-gate 	}
24787c478bd9Sstevel@tonic-gate 	/*
24797c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
24807c478bd9Sstevel@tonic-gate 	 * exist.
24817c478bd9Sstevel@tonic-gate 	 */
24827c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
24837c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "pool_conf_alloc");
24847c478bd9Sstevel@tonic-gate 		return (-1);
24857c478bd9Sstevel@tonic-gate 	}
24867c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
24877c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
24887c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "pool_conf_open");
24897c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
24907c478bd9Sstevel@tonic-gate 		return (-1);
24917c478bd9Sstevel@tonic-gate 	}
24927c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
24937c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
24947c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
24957c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
24967c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: pool '%s' not found; "
24977c478bd9Sstevel@tonic-gate 		    "using default pool.", poolname);
24987c478bd9Sstevel@tonic-gate 		return (0);
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate 	/*
25017c478bd9Sstevel@tonic-gate 	 * Bind the zone to the pool.
25027c478bd9Sstevel@tonic-gate 	 */
25037c478bd9Sstevel@tonic-gate 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
25047c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: unable to bind to pool '%s'; "
25057c478bd9Sstevel@tonic-gate 		    "using default pool.", poolname);
25067c478bd9Sstevel@tonic-gate 	}
25077c478bd9Sstevel@tonic-gate 	return (0);
25087c478bd9Sstevel@tonic-gate }
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate int
25117c478bd9Sstevel@tonic-gate prtmount(const char *fs, void *x) {
25127c478bd9Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
25137c478bd9Sstevel@tonic-gate 	return (0);
25147c478bd9Sstevel@tonic-gate }
25157c478bd9Sstevel@tonic-gate 
2516*108322fbScarlsonj /*
2517*108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
2518*108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
2519*108322fbScarlsonj  * is found or if we can't tell.
2520*108322fbScarlsonj  */
2521*108322fbScarlsonj static boolean_t
2522*108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
25237c478bd9Sstevel@tonic-gate {
2524*108322fbScarlsonj 	zoneid_t *zids = NULL;
2525*108322fbScarlsonj 	uint_t nzids = 0;
2526*108322fbScarlsonj 	boolean_t retv;
2527*108322fbScarlsonj 	int rlen, zlen;
2528*108322fbScarlsonj 	char zroot[MAXPATHLEN];
2529*108322fbScarlsonj 	char zonename[ZONENAME_MAX];
2530*108322fbScarlsonj 
2531*108322fbScarlsonj 	for (;;) {
2532*108322fbScarlsonj 		nzids += 10;
2533*108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
2534*108322fbScarlsonj 		if (zids == NULL) {
2535*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to allocate memory");
2536*108322fbScarlsonj 			return (B_TRUE);
2537*108322fbScarlsonj 		}
2538*108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
2539*108322fbScarlsonj 			break;
2540*108322fbScarlsonj 		free(zids);
2541*108322fbScarlsonj 	}
2542*108322fbScarlsonj 	retv = B_FALSE;
2543*108322fbScarlsonj 	rlen = strlen(rootpath);
2544*108322fbScarlsonj 	while (nzids > 0) {
2545*108322fbScarlsonj 		/*
2546*108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
2547*108322fbScarlsonj 		 * while we were busy.
2548*108322fbScarlsonj 		 */
2549*108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
2550*108322fbScarlsonj 		    sizeof (zroot)) == -1)
2551*108322fbScarlsonj 			continue;
2552*108322fbScarlsonj 		zlen = strlen(zroot);
2553*108322fbScarlsonj 		if (zlen > rlen)
2554*108322fbScarlsonj 			zlen = rlen;
2555*108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
2556*108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
2557*108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
2558*108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
2559*108322fbScarlsonj 			    sizeof (zonename)) == -1)
2560*108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
2561*108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
2562*108322fbScarlsonj 			zerror(zlogp, B_FALSE,
2563*108322fbScarlsonj 			    "zone root %s already in use by zone %s",
2564*108322fbScarlsonj 			    rootpath, zonename);
2565*108322fbScarlsonj 			retv = B_TRUE;
2566*108322fbScarlsonj 			break;
2567*108322fbScarlsonj 		}
2568*108322fbScarlsonj 	}
2569*108322fbScarlsonj 	free(zids);
2570*108322fbScarlsonj 	return (retv);
2571*108322fbScarlsonj }
2572*108322fbScarlsonj 
2573*108322fbScarlsonj /*
2574*108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
2575*108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
2576*108322fbScarlsonj  */
2577*108322fbScarlsonj static boolean_t
2578*108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
2579*108322fbScarlsonj {
2580*108322fbScarlsonj 	struct stat64 rst, zst;
2581*108322fbScarlsonj 	struct mnttab *mnp;
2582*108322fbScarlsonj 
2583*108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
2584*108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
2585*108322fbScarlsonj 		return (B_TRUE);
2586*108322fbScarlsonj 	}
2587*108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
2588*108322fbScarlsonj 		return (B_TRUE);
2589*108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
2590*108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
2591*108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
2592*108322fbScarlsonj 			continue;
2593*108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
2594*108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
2595*108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
2596*108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
2597*108322fbScarlsonj 			zerror(zlogp, B_FALSE,
2598*108322fbScarlsonj 			    "zone root %s is reachable through %s",
2599*108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
2600*108322fbScarlsonj 			return (B_TRUE);
2601*108322fbScarlsonj 		}
2602*108322fbScarlsonj 	}
2603*108322fbScarlsonj 	return (B_FALSE);
2604*108322fbScarlsonj }
2605*108322fbScarlsonj 
2606*108322fbScarlsonj zoneid_t
2607*108322fbScarlsonj vplat_create(zlog_t *zlogp, boolean_t mount_cmd)
2608*108322fbScarlsonj {
2609*108322fbScarlsonj 	zoneid_t rval = -1;
26107c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
26117c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
26127c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
2613*108322fbScarlsonj 	size_t rctlbufsz = 0;
2614*108322fbScarlsonj 	zoneid_t zoneid = -1;
26157c478bd9Sstevel@tonic-gate 	int xerr;
2616*108322fbScarlsonj 	char *kzone;
2617*108322fbScarlsonj 	FILE *fp = NULL;
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
26207c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
26217c478bd9Sstevel@tonic-gate 		return (-1);
26227c478bd9Sstevel@tonic-gate 	}
2623*108322fbScarlsonj 	if (zonecfg_in_alt_root())
2624*108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
26277c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
26287c478bd9Sstevel@tonic-gate 		return (-1);
26297c478bd9Sstevel@tonic-gate 	}
26307c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
26317c478bd9Sstevel@tonic-gate 	if (zonecfg_get_privset(privs) != Z_OK) {
26327c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "Failed to initialize privileges");
26337c478bd9Sstevel@tonic-gate 		goto error;
26347c478bd9Sstevel@tonic-gate 	}
2635*108322fbScarlsonj 	if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
26367c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
26377c478bd9Sstevel@tonic-gate 		goto error;
26387c478bd9Sstevel@tonic-gate 	}
26397c478bd9Sstevel@tonic-gate 
2640*108322fbScarlsonj 	kzone = zone_name;
2641*108322fbScarlsonj 
2642*108322fbScarlsonj 	/*
2643*108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
2644*108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
2645*108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
2646*108322fbScarlsonj 	 * that use this same source node (same device and inode).
2647*108322fbScarlsonj 	 */
2648*108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
2649*108322fbScarlsonj 		goto error;
2650*108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
2651*108322fbScarlsonj 		goto error;
2652*108322fbScarlsonj 
2653*108322fbScarlsonj 	if (mount_cmd) {
2654*108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
2655*108322fbScarlsonj 
2656*108322fbScarlsonj 		/*
2657*108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
2658*108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
2659*108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
2660*108322fbScarlsonj 		 * to both the zone's resources and the running machine's
2661*108322fbScarlsonj 		 * executables.
2662*108322fbScarlsonj 		 *
2663*108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
2664*108322fbScarlsonj 		 */
2665*108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
2666*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
2667*108322fbScarlsonj 			goto error;
2668*108322fbScarlsonj 		}
2669*108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
2670*108322fbScarlsonj 			goto error;
2671*108322fbScarlsonj 	}
2672*108322fbScarlsonj 
2673*108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2674*108322fbScarlsonj 		/*
2675*108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
2676*108322fbScarlsonj 		 * then we have some additional work to do before starting the
2677*108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
2678*108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
2679*108322fbScarlsonj 		 * the zone.
2680*108322fbScarlsonj 		 */
2681*108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
2682*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
2683*108322fbScarlsonj 			goto error;
2684*108322fbScarlsonj 		}
2685*108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
2686*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
2687*108322fbScarlsonj 			goto error;
2688*108322fbScarlsonj 		}
2689*108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
2690*108322fbScarlsonj 		    NULL, 0) == 0) {
2691*108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
2692*108322fbScarlsonj 			goto error;
2693*108322fbScarlsonj 		}
2694*108322fbScarlsonj 		/* This is the preferred name */
2695*108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
2696*108322fbScarlsonj 		    zone_name);
2697*108322fbScarlsonj 		srandom(getpid());
2698*108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
2699*108322fbScarlsonj 		    0) == 0) {
2700*108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
2701*108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
2702*108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
2703*108322fbScarlsonj 		}
2704*108322fbScarlsonj 		kzone = kernzone;
2705*108322fbScarlsonj 	}
2706*108322fbScarlsonj 
27077c478bd9Sstevel@tonic-gate 	xerr = 0;
2708*108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
27097c478bd9Sstevel@tonic-gate 	    rctlbufsz, &xerr)) == -1) {
27107c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
27117c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
27127c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
27137c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
27147c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
27157c478bd9Sstevel@tonic-gate 			} else {
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
27187c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
27197c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
27207c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
27217c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
27227c478bd9Sstevel@tonic-gate 			}
27237c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
27247c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
27257c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
27267c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
27277c478bd9Sstevel@tonic-gate 		} else {
27287c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
27297c478bd9Sstevel@tonic-gate 		}
27307c478bd9Sstevel@tonic-gate 		goto error;
27317c478bd9Sstevel@tonic-gate 	}
2732*108322fbScarlsonj 
2733*108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
2734*108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
2735*108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
2736*108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
2737*108322fbScarlsonj 		goto error;
2738*108322fbScarlsonj 	}
2739*108322fbScarlsonj 
27407c478bd9Sstevel@tonic-gate 	/*
2741*108322fbScarlsonj 	 * The following is a warning, not an error, and is not performed when
2742*108322fbScarlsonj 	 * merely mounting a zone for administrative use.
27437c478bd9Sstevel@tonic-gate 	 */
2744*108322fbScarlsonj 	if (!mount_cmd && bind_to_pool(zlogp, zoneid) != 0)
27457c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: unable to bind zone to "
27467c478bd9Sstevel@tonic-gate 		    "requested pool; using default pool.");
2747*108322fbScarlsonj 	rval = zoneid;
2748*108322fbScarlsonj 	zoneid = -1;
2749*108322fbScarlsonj 
27507c478bd9Sstevel@tonic-gate error:
2751*108322fbScarlsonj 	if (zoneid != -1)
2752*108322fbScarlsonj 		(void) zone_destroy(zoneid);
27537c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
27547c478bd9Sstevel@tonic-gate 		free(rctlbuf);
27557c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
2756*108322fbScarlsonj 	if (fp != NULL)
2757*108322fbScarlsonj 		zonecfg_close_scratch(fp);
2758*108322fbScarlsonj 	lofs_discard_mnttab();
27597c478bd9Sstevel@tonic-gate 	return (rval);
27607c478bd9Sstevel@tonic-gate }
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate int
2763*108322fbScarlsonj vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd)
27647c478bd9Sstevel@tonic-gate {
2765*108322fbScarlsonj 	if (create_dev_files(zlogp) != 0 ||
2766*108322fbScarlsonj 	    mount_filesystems(zlogp, mount_cmd) != 0) {
2767*108322fbScarlsonj 		lofs_discard_mnttab();
27687c478bd9Sstevel@tonic-gate 		return (-1);
2769*108322fbScarlsonj 	}
2770*108322fbScarlsonj 	if (!mount_cmd && (devfsadm_register(zlogp) != 0 ||
2771*108322fbScarlsonj 	    configure_network_interfaces(zlogp) != 0)) {
2772*108322fbScarlsonj 		lofs_discard_mnttab();
27737c478bd9Sstevel@tonic-gate 		return (-1);
2774*108322fbScarlsonj 	}
2775*108322fbScarlsonj 	lofs_discard_mnttab();
27767c478bd9Sstevel@tonic-gate 	return (0);
27777c478bd9Sstevel@tonic-gate }
27787c478bd9Sstevel@tonic-gate 
2779*108322fbScarlsonj static int
2780*108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
27817c478bd9Sstevel@tonic-gate {
2782*108322fbScarlsonj 	char zroot[MAXPATHLEN];
2783*108322fbScarlsonj 
2784*108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
2785*108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
2786*108322fbScarlsonj 		return (-1);
2787*108322fbScarlsonj 	}
2788*108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
2789*108322fbScarlsonj 
2790*108322fbScarlsonj 	/*
2791*108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
2792*108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
2793*108322fbScarlsonj 	 * still be creds floating about in the system that reference the
2794*108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
2795*108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
2796*108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
2797*108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
2798*108322fbScarlsonj 	 */
2799*108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
2800*108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
2801*108322fbScarlsonj 			goto unmounted;
2802*108322fbScarlsonj 		if (errno == EBUSY) {
2803*108322fbScarlsonj 			int tries = 10;
2804*108322fbScarlsonj 
2805*108322fbScarlsonj 			while (--tries >= 0) {
2806*108322fbScarlsonj 				(void) sleep(1);
2807*108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
2808*108322fbScarlsonj 					goto unmounted;
2809*108322fbScarlsonj 				if (errno != EBUSY)
2810*108322fbScarlsonj 					break;
2811*108322fbScarlsonj 			}
2812*108322fbScarlsonj 		}
2813*108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
2814*108322fbScarlsonj 		return (-1);
2815*108322fbScarlsonj 	}
2816*108322fbScarlsonj unmounted:
2817*108322fbScarlsonj 
2818*108322fbScarlsonj 	/*
2819*108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
2820*108322fbScarlsonj 	 * entries.
2821*108322fbScarlsonj 	 */
2822*108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2823*108322fbScarlsonj 		FILE *fp;
2824*108322fbScarlsonj 		int retv;
2825*108322fbScarlsonj 
2826*108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
2827*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
2828*108322fbScarlsonj 			return (-1);
2829*108322fbScarlsonj 		}
2830*108322fbScarlsonj 		retv = -1;
2831*108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
2832*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
2833*108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
2834*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
2835*108322fbScarlsonj 		else
2836*108322fbScarlsonj 			retv = 0;
2837*108322fbScarlsonj 		zonecfg_close_scratch(fp);
2838*108322fbScarlsonj 		return (retv);
2839*108322fbScarlsonj 	} else {
2840*108322fbScarlsonj 		return (0);
2841*108322fbScarlsonj 	}
2842*108322fbScarlsonj }
2843*108322fbScarlsonj 
2844*108322fbScarlsonj int
2845*108322fbScarlsonj vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd)
2846*108322fbScarlsonj {
2847*108322fbScarlsonj 	char *kzone;
28487c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
28497c478bd9Sstevel@tonic-gate 
2850*108322fbScarlsonj 	kzone = zone_name;
2851*108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2852*108322fbScarlsonj 		FILE *fp;
2853*108322fbScarlsonj 
2854*108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
2855*108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
2856*108322fbScarlsonj 			goto error;
2857*108322fbScarlsonj 		}
2858*108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
2859*108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
2860*108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
2861*108322fbScarlsonj 			zonecfg_close_scratch(fp);
2862*108322fbScarlsonj 			goto error;
2863*108322fbScarlsonj 		}
2864*108322fbScarlsonj 		zonecfg_close_scratch(fp);
2865*108322fbScarlsonj 		kzone = kernzone;
2866*108322fbScarlsonj 	}
2867*108322fbScarlsonj 
2868*108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
28697c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
28707c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
2871*108322fbScarlsonj 		if (unmount_cmd)
2872*108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
28737c478bd9Sstevel@tonic-gate 		goto error;
28747c478bd9Sstevel@tonic-gate 	}
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
28777c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
28787c478bd9Sstevel@tonic-gate 		goto error;
28797c478bd9Sstevel@tonic-gate 	}
28807c478bd9Sstevel@tonic-gate 
2881*108322fbScarlsonj 	if (!unmount_cmd && devfsadm_unregister(zlogp) != 0)
28827c478bd9Sstevel@tonic-gate 		goto error;
28837c478bd9Sstevel@tonic-gate 
2884*108322fbScarlsonj 	if (!unmount_cmd &&
2885*108322fbScarlsonj 	    unconfigure_network_interfaces(zlogp, zoneid) != 0) {
28867c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
28877c478bd9Sstevel@tonic-gate 		    "unable to unconfigure network interfaces in zone");
28887c478bd9Sstevel@tonic-gate 		goto error;
28897c478bd9Sstevel@tonic-gate 	}
28907c478bd9Sstevel@tonic-gate 
2891*108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
28927c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
28937c478bd9Sstevel@tonic-gate 		goto error;
28947c478bd9Sstevel@tonic-gate 	}
28957c478bd9Sstevel@tonic-gate 
2896*108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
28977c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
28987c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
28997c478bd9Sstevel@tonic-gate 		goto error;
29007c478bd9Sstevel@tonic-gate 	}
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
29037c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
29047c478bd9Sstevel@tonic-gate 		goto error;
29057c478bd9Sstevel@tonic-gate 	}
2906*108322fbScarlsonj 
2907*108322fbScarlsonj 	/*
2908*108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
2909*108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
2910*108322fbScarlsonj 	 */
2911*108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
2912*108322fbScarlsonj 		goto error;
2913*108322fbScarlsonj 
2914*108322fbScarlsonj 	if (!unmount_cmd)
29157c478bd9Sstevel@tonic-gate 		destroy_console_slave();
29167c478bd9Sstevel@tonic-gate 
2917*108322fbScarlsonj 	lofs_discard_mnttab();
29187c478bd9Sstevel@tonic-gate 	return (0);
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate error:
2921*108322fbScarlsonj 	lofs_discard_mnttab();
29227c478bd9Sstevel@tonic-gate 	return (-1);
29237c478bd9Sstevel@tonic-gate }
2924