xref: /titanic_50/usr/src/cmd/zoneadmd/vplat.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
31*7c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
32*7c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
33*7c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
34*7c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
35*7c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
36*7c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/mount.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
51*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
52*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
53*7c478bd9Sstevel@tonic-gate #include <net/route.h>
54*7c478bd9Sstevel@tonic-gate #include <netdb.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include <stdio.h>
57*7c478bd9Sstevel@tonic-gate #include <errno.h>
58*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
59*7c478bd9Sstevel@tonic-gate #include <unistd.h>
60*7c478bd9Sstevel@tonic-gate #include <rctl.h>
61*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
62*7c478bd9Sstevel@tonic-gate #include <string.h>
63*7c478bd9Sstevel@tonic-gate #include <strings.h>
64*7c478bd9Sstevel@tonic-gate #include <wait.h>
65*7c478bd9Sstevel@tonic-gate #include <limits.h>
66*7c478bd9Sstevel@tonic-gate #include <libgen.h>
67*7c478bd9Sstevel@tonic-gate #include <zone.h>
68*7c478bd9Sstevel@tonic-gate #include <assert.h>
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
71*7c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
72*7c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
73*7c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate #include <pool.h>
76*7c478bd9Sstevel@tonic-gate #include <sys/pool.h>
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
79*7c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
82*7c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
85*7c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
86*7c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate #define	IPD_DEFAULT_OPTS \
89*7c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate  * A list of directories which should be created.
95*7c478bd9Sstevel@tonic-gate  */
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate struct dir_info {
98*7c478bd9Sstevel@tonic-gate 	char *dir_name;
99*7c478bd9Sstevel@tonic-gate 	mode_t dir_mode;
100*7c478bd9Sstevel@tonic-gate };
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /*
103*7c478bd9Sstevel@tonic-gate  * The pathnames below are relative to the zonepath
104*7c478bd9Sstevel@tonic-gate  */
105*7c478bd9Sstevel@tonic-gate static struct dir_info dev_dirs[] = {
106*7c478bd9Sstevel@tonic-gate 	{ "/dev",	0755 },
107*7c478bd9Sstevel@tonic-gate 	{ "/dev/dsk",	0755 },
108*7c478bd9Sstevel@tonic-gate 	{ "/dev/fd",	0555 },
109*7c478bd9Sstevel@tonic-gate 	{ "/dev/pts",	0755 },
110*7c478bd9Sstevel@tonic-gate 	{ "/dev/rdsk",	0755 },
111*7c478bd9Sstevel@tonic-gate 	{ "/dev/rmt",	0755 },
112*7c478bd9Sstevel@tonic-gate 	{ "/dev/sad",	0755 },
113*7c478bd9Sstevel@tonic-gate 	{ "/dev/swap",	0755 },
114*7c478bd9Sstevel@tonic-gate 	{ "/dev/term",	0755 },
115*7c478bd9Sstevel@tonic-gate };
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /*
118*7c478bd9Sstevel@tonic-gate  * A list of devices which should be symlinked to /dev/zconsole.
119*7c478bd9Sstevel@tonic-gate  */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate struct symlink_info {
122*7c478bd9Sstevel@tonic-gate 	char *sl_source;
123*7c478bd9Sstevel@tonic-gate 	char *sl_target;
124*7c478bd9Sstevel@tonic-gate };
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate /*
127*7c478bd9Sstevel@tonic-gate  * The "source" paths are relative to the zonepath
128*7c478bd9Sstevel@tonic-gate  */
129*7c478bd9Sstevel@tonic-gate static struct symlink_info dev_symlinks[] = {
130*7c478bd9Sstevel@tonic-gate 	{ "/dev/stderr",	"./fd/2" },
131*7c478bd9Sstevel@tonic-gate 	{ "/dev/stdin",		"./fd/0" },
132*7c478bd9Sstevel@tonic-gate 	{ "/dev/stdout",	"./fd/1" },
133*7c478bd9Sstevel@tonic-gate 	{ "/dev/dtremote",	"/dev/null" },
134*7c478bd9Sstevel@tonic-gate 	{ "/dev/console",	"zconsole" },
135*7c478bd9Sstevel@tonic-gate 	{ "/dev/syscon",	"zconsole" },
136*7c478bd9Sstevel@tonic-gate 	{ "/dev/sysmsg",	"zconsole" },
137*7c478bd9Sstevel@tonic-gate 	{ "/dev/systty",	"zconsole" },
138*7c478bd9Sstevel@tonic-gate 	{ "/dev/msglog",	"zconsole" },
139*7c478bd9Sstevel@tonic-gate };
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate /* for routing socket */
142*7c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
145*7c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate /*
148*7c478bd9Sstevel@tonic-gate  * Private autofs system call
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate static int
153*7c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	/*
156*7c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
157*7c478bd9Sstevel@tonic-gate 	 */
158*7c478bd9Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate static int
162*7c478bd9Sstevel@tonic-gate make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
165*7c478bd9Sstevel@tonic-gate 	struct stat st;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
168*7c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
169*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
170*7c478bd9Sstevel@tonic-gate 		    subdir);
171*7c478bd9Sstevel@tonic-gate 		return (-1);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
175*7c478bd9Sstevel@tonic-gate 		/*
176*7c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
177*7c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
178*7c478bd9Sstevel@tonic-gate 		 * and we don't need to second guess him.
179*7c478bd9Sstevel@tonic-gate 		 */
180*7c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
181*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s is not a directory", path);
182*7c478bd9Sstevel@tonic-gate 			return (-1);
183*7c478bd9Sstevel@tonic-gate 		}
184*7c478bd9Sstevel@tonic-gate 	} else if (mkdirp(path, mode) != 0) {
185*7c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
186*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
187*7c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
188*7c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
189*7c478bd9Sstevel@tonic-gate 		else
190*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
191*7c478bd9Sstevel@tonic-gate 		return (-1);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 	return (0);
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate /*
197*7c478bd9Sstevel@tonic-gate  * Make /dev and various directories underneath it.
198*7c478bd9Sstevel@tonic-gate  */
199*7c478bd9Sstevel@tonic-gate static int
200*7c478bd9Sstevel@tonic-gate make_dev_dirs(zlog_t *zlogp, const char *zonepath)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	int i;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (dev_dirs) / sizeof (struct dir_info); i++) {
205*7c478bd9Sstevel@tonic-gate 		if (make_one_dir(zlogp, zonepath, dev_dirs[i].dir_name,
206*7c478bd9Sstevel@tonic-gate 		    dev_dirs[i].dir_mode) != 0)
207*7c478bd9Sstevel@tonic-gate 			return (-1);
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 	return (0);
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate /*
213*7c478bd9Sstevel@tonic-gate  * Make various sym-links underneath /dev.
214*7c478bd9Sstevel@tonic-gate  */
215*7c478bd9Sstevel@tonic-gate static int
216*7c478bd9Sstevel@tonic-gate make_dev_links(zlog_t *zlogp, char *zonepath)
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	int i;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (dev_symlinks) / sizeof (struct symlink_info);
221*7c478bd9Sstevel@tonic-gate 	    i++) {
222*7c478bd9Sstevel@tonic-gate 		char dev[MAXPATHLEN];
223*7c478bd9Sstevel@tonic-gate 		struct stat st;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 		(void) snprintf(dev, sizeof (dev), "%s%s", zonepath,
226*7c478bd9Sstevel@tonic-gate 		    dev_symlinks[i].sl_source);
227*7c478bd9Sstevel@tonic-gate 		if (lstat(dev, &st) == 0) {
228*7c478bd9Sstevel@tonic-gate 			/*
229*7c478bd9Sstevel@tonic-gate 			 * Try not to call unlink(2) on directories, since that
230*7c478bd9Sstevel@tonic-gate 			 * makes UFS unhappy.
231*7c478bd9Sstevel@tonic-gate 			 */
232*7c478bd9Sstevel@tonic-gate 			if (S_ISDIR(st.st_mode)) {
233*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "symlink path %s is a "
234*7c478bd9Sstevel@tonic-gate 				    "directory", dev_symlinks[i].sl_source);
235*7c478bd9Sstevel@tonic-gate 				return (-1);
236*7c478bd9Sstevel@tonic-gate 			}
237*7c478bd9Sstevel@tonic-gate 			(void) unlink(dev);
238*7c478bd9Sstevel@tonic-gate 		}
239*7c478bd9Sstevel@tonic-gate 		if (symlink(dev_symlinks[i].sl_target, dev) != 0) {
240*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "could not setup %s symlink",
241*7c478bd9Sstevel@tonic-gate 			    dev_symlinks[i].sl_source);
242*7c478bd9Sstevel@tonic-gate 			return (-1);
243*7c478bd9Sstevel@tonic-gate 		}
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 	return (0);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate /*
249*7c478bd9Sstevel@tonic-gate  * Create various directories and sym-links under /dev.
250*7c478bd9Sstevel@tonic-gate  */
251*7c478bd9Sstevel@tonic-gate static int
252*7c478bd9Sstevel@tonic-gate create_dev_files(zlog_t *zlogp)
253*7c478bd9Sstevel@tonic-gate {
254*7c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
257*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
258*7c478bd9Sstevel@tonic-gate 		return (-1);
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	if (make_dev_dirs(zlogp, zonepath) != 0)
262*7c478bd9Sstevel@tonic-gate 		return (-1);
263*7c478bd9Sstevel@tonic-gate 	if (make_dev_links(zlogp, zonepath) != 0)
264*7c478bd9Sstevel@tonic-gate 		return (-1);
265*7c478bd9Sstevel@tonic-gate 	return (0);
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate static void
269*7c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate 	uint_t i;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	if (types == NULL)
274*7c478bd9Sstevel@tonic-gate 		return;
275*7c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
276*7c478bd9Sstevel@tonic-gate 		free(types[i]);
277*7c478bd9Sstevel@tonic-gate 	free(types);
278*7c478bd9Sstevel@tonic-gate }
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate static char **
281*7c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
282*7c478bd9Sstevel@tonic-gate {
283*7c478bd9Sstevel@tonic-gate 	char **types = NULL;
284*7c478bd9Sstevel@tonic-gate 	FILE *fp;
285*7c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
286*7c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
287*7c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
288*7c478bd9Sstevel@tonic-gate 	uint_t i;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
291*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
292*7c478bd9Sstevel@tonic-gate 		return (NULL);
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 	/*
295*7c478bd9Sstevel@tonic-gate 	 * Count the number of lines
296*7c478bd9Sstevel@tonic-gate 	 */
297*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
298*7c478bd9Sstevel@tonic-gate 		lines++;
299*7c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
300*7c478bd9Sstevel@tonic-gate 		goto out;
301*7c478bd9Sstevel@tonic-gate 	rewind(fp);
302*7c478bd9Sstevel@tonic-gate 	/*
303*7c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
304*7c478bd9Sstevel@tonic-gate 	 */
305*7c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
306*7c478bd9Sstevel@tonic-gate 	if (types == NULL) {
307*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
308*7c478bd9Sstevel@tonic-gate 		goto out;
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 	i = 0;
311*7c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
312*7c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
313*7c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
314*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
315*7c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
316*7c478bd9Sstevel@tonic-gate 			types = NULL;
317*7c478bd9Sstevel@tonic-gate 			goto out;
318*7c478bd9Sstevel@tonic-gate 		}
319*7c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
320*7c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
321*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
322*7c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
323*7c478bd9Sstevel@tonic-gate 			types = NULL;
324*7c478bd9Sstevel@tonic-gate 			goto out;
325*7c478bd9Sstevel@tonic-gate 		}
326*7c478bd9Sstevel@tonic-gate 		i++;
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate out:
329*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
330*7c478bd9Sstevel@tonic-gate 	return (types);
331*7c478bd9Sstevel@tonic-gate }
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate static boolean_t
334*7c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	uint_t i;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
339*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
340*7c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
341*7c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
342*7c478bd9Sstevel@tonic-gate 			return (B_TRUE);
343*7c478bd9Sstevel@tonic-gate 	}
344*7c478bd9Sstevel@tonic-gate 	return (B_FALSE);
345*7c478bd9Sstevel@tonic-gate }
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate static void
348*7c478bd9Sstevel@tonic-gate free_mnttable(struct mnttab *mnt_array, uint_t nelem)
349*7c478bd9Sstevel@tonic-gate {
350*7c478bd9Sstevel@tonic-gate 	uint_t i;
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	if (mnt_array == NULL)
353*7c478bd9Sstevel@tonic-gate 		return;
354*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++) {
355*7c478bd9Sstevel@tonic-gate 		free(mnt_array[i].mnt_mountp);
356*7c478bd9Sstevel@tonic-gate 		free(mnt_array[i].mnt_fstype);
357*7c478bd9Sstevel@tonic-gate 		assert(mnt_array[i].mnt_special == NULL);
358*7c478bd9Sstevel@tonic-gate 		assert(mnt_array[i].mnt_mntopts == NULL);
359*7c478bd9Sstevel@tonic-gate 		assert(mnt_array[i].mnt_time == NULL);
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 	free(mnt_array);
362*7c478bd9Sstevel@tonic-gate }
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate /*
365*7c478bd9Sstevel@tonic-gate  * Build the mount table for the zone rooted at "zroot", storing the resulting
366*7c478bd9Sstevel@tonic-gate  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
367*7c478bd9Sstevel@tonic-gate  * array in "nelemp".
368*7c478bd9Sstevel@tonic-gate  */
369*7c478bd9Sstevel@tonic-gate static int
370*7c478bd9Sstevel@tonic-gate build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
371*7c478bd9Sstevel@tonic-gate     struct mnttab **mnt_arrayp, uint_t *nelemp)
372*7c478bd9Sstevel@tonic-gate {
373*7c478bd9Sstevel@tonic-gate 	struct mnttab mnt;
374*7c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
375*7c478bd9Sstevel@tonic-gate 	struct mnttab *mnp;
376*7c478bd9Sstevel@tonic-gate 	uint_t nmnt;
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	rewind(mnttab);
379*7c478bd9Sstevel@tonic-gate 	resetmnttab(mnttab);
380*7c478bd9Sstevel@tonic-gate 	nmnt = 0;
381*7c478bd9Sstevel@tonic-gate 	mnts = NULL;
382*7c478bd9Sstevel@tonic-gate 	while (getmntent(mnttab, &mnt) == 0) {
383*7c478bd9Sstevel@tonic-gate 		struct mnttab *tmp_array;
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
386*7c478bd9Sstevel@tonic-gate 			continue;
387*7c478bd9Sstevel@tonic-gate 		nmnt++;
388*7c478bd9Sstevel@tonic-gate 		tmp_array = realloc(mnts, nmnt * sizeof (*mnts));
389*7c478bd9Sstevel@tonic-gate 		if (tmp_array == NULL) {
390*7c478bd9Sstevel@tonic-gate 			nmnt--;
391*7c478bd9Sstevel@tonic-gate 			free_mnttable(mnts, nmnt);
392*7c478bd9Sstevel@tonic-gate 			return (-1);
393*7c478bd9Sstevel@tonic-gate 		}
394*7c478bd9Sstevel@tonic-gate 		mnts = tmp_array;
395*7c478bd9Sstevel@tonic-gate 		mnp = &mnts[nmnt - 1];
396*7c478bd9Sstevel@tonic-gate 		/*
397*7c478bd9Sstevel@tonic-gate 		 * Zero out the fields we won't be using.
398*7c478bd9Sstevel@tonic-gate 		 */
399*7c478bd9Sstevel@tonic-gate 		mnp->mnt_special = NULL;
400*7c478bd9Sstevel@tonic-gate 		mnp->mnt_mntopts = NULL;
401*7c478bd9Sstevel@tonic-gate 		mnp->mnt_time = NULL;
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
404*7c478bd9Sstevel@tonic-gate 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
405*7c478bd9Sstevel@tonic-gate 		if (mnp->mnt_mountp == NULL ||
406*7c478bd9Sstevel@tonic-gate 		    mnp->mnt_fstype == NULL) {
407*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
408*7c478bd9Sstevel@tonic-gate 			free_mnttable(mnts, nmnt);
409*7c478bd9Sstevel@tonic-gate 			return (-1);
410*7c478bd9Sstevel@tonic-gate 		}
411*7c478bd9Sstevel@tonic-gate 	}
412*7c478bd9Sstevel@tonic-gate 	*mnt_arrayp = mnts;
413*7c478bd9Sstevel@tonic-gate 	*nelemp = nmnt;
414*7c478bd9Sstevel@tonic-gate 	return (0);
415*7c478bd9Sstevel@tonic-gate }
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate /*
418*7c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
419*7c478bd9Sstevel@tonic-gate  *
420*7c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
421*7c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
422*7c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
423*7c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
424*7c478bd9Sstevel@tonic-gate  *
425*7c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
426*7c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
427*7c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
428*7c478bd9Sstevel@tonic-gate  *
429*7c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
430*7c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
431*7c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
432*7c478bd9Sstevel@tonic-gate  *
433*7c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
434*7c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
435*7c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
436*7c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
437*7c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
438*7c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
439*7c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
440*7c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
441*7c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
442*7c478bd9Sstevel@tonic-gate  * graceful mounts first.
443*7c478bd9Sstevel@tonic-gate  *
444*7c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
445*7c478bd9Sstevel@tonic-gate  */
446*7c478bd9Sstevel@tonic-gate static int
447*7c478bd9Sstevel@tonic-gate unmount_filesystems(zlog_t *zlogp)
448*7c478bd9Sstevel@tonic-gate {
449*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
450*7c478bd9Sstevel@tonic-gate 	int error = 0;
451*7c478bd9Sstevel@tonic-gate 	FILE *mnttab;
452*7c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
453*7c478bd9Sstevel@tonic-gate 	uint_t nmnt;
454*7c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
455*7c478bd9Sstevel@tonic-gate 	size_t zrootlen;
456*7c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
457*7c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
458*7c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
461*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to find zoneid");
462*7c478bd9Sstevel@tonic-gate 		return (-1);
463*7c478bd9Sstevel@tonic-gate 	}
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
466*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
467*7c478bd9Sstevel@tonic-gate 		return (-1);
468*7c478bd9Sstevel@tonic-gate 	}
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
471*7c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
474*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
475*7c478bd9Sstevel@tonic-gate 		return (-1);
476*7c478bd9Sstevel@tonic-gate 	}
477*7c478bd9Sstevel@tonic-gate 	/*
478*7c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
479*7c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
480*7c478bd9Sstevel@tonic-gate 	 */
481*7c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
482*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
483*7c478bd9Sstevel@tonic-gate 		error++;
484*7c478bd9Sstevel@tonic-gate 		goto out;
485*7c478bd9Sstevel@tonic-gate 	}
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate 	/*
488*7c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
489*7c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
490*7c478bd9Sstevel@tonic-gate 	 */
491*7c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
492*7c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
493*7c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
494*7c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
495*7c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
496*7c478bd9Sstevel@tonic-gate 		char *path;
497*7c478bd9Sstevel@tonic-gate 		uint_t i;
498*7c478bd9Sstevel@tonic-gate 
499*7c478bd9Sstevel@tonic-gate 		mnts = NULL;
500*7c478bd9Sstevel@tonic-gate 		nmnt = 0;
501*7c478bd9Sstevel@tonic-gate 		/*
502*7c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
503*7c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
504*7c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
505*7c478bd9Sstevel@tonic-gate 		 * filesystems.
506*7c478bd9Sstevel@tonic-gate 		 */
507*7c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
508*7c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
509*7c478bd9Sstevel@tonic-gate 			error++;
510*7c478bd9Sstevel@tonic-gate 			goto out;
511*7c478bd9Sstevel@tonic-gate 		}
512*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
513*7c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
514*7c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
515*7c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
516*7c478bd9Sstevel@tonic-gate 			/*
517*7c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
518*7c478bd9Sstevel@tonic-gate 			 *
519*7c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
520*7c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
521*7c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
522*7c478bd9Sstevel@tonic-gate 			 */
523*7c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
524*7c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
525*7c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
526*7c478bd9Sstevel@tonic-gate 			}
527*7c478bd9Sstevel@tonic-gate 			/*
528*7c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
529*7c478bd9Sstevel@tonic-gate 			 */
530*7c478bd9Sstevel@tonic-gate 			if (stuck) {
531*7c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
532*7c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
533*7c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
534*7c478bd9Sstevel@tonic-gate 				} else {
535*7c478bd9Sstevel@tonic-gate 					/*
536*7c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
537*7c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
538*7c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
539*7c478bd9Sstevel@tonic-gate 					 * bail.
540*7c478bd9Sstevel@tonic-gate 					 */
541*7c478bd9Sstevel@tonic-gate 					error++;
542*7c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
543*7c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
544*7c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
545*7c478bd9Sstevel@tonic-gate 					goto out;
546*7c478bd9Sstevel@tonic-gate 				}
547*7c478bd9Sstevel@tonic-gate 			}
548*7c478bd9Sstevel@tonic-gate 			/*
549*7c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
550*7c478bd9Sstevel@tonic-gate 			 */
551*7c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
552*7c478bd9Sstevel@tonic-gate 				newcount++;
553*7c478bd9Sstevel@tonic-gate 		}
554*7c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 		if (newcount == 0)
557*7c478bd9Sstevel@tonic-gate 			break;
558*7c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
559*7c478bd9Sstevel@tonic-gate 			/*
560*7c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
561*7c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
562*7c478bd9Sstevel@tonic-gate 			 */
563*7c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
564*7c478bd9Sstevel@tonic-gate 		}
565*7c478bd9Sstevel@tonic-gate 		oldcount = newcount;
566*7c478bd9Sstevel@tonic-gate 
567*7c478bd9Sstevel@tonic-gate 		/*
568*7c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
569*7c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
570*7c478bd9Sstevel@tonic-gate 		 */
571*7c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
572*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
573*7c478bd9Sstevel@tonic-gate 			error++;
574*7c478bd9Sstevel@tonic-gate 			goto out;
575*7c478bd9Sstevel@tonic-gate 		}
576*7c478bd9Sstevel@tonic-gate 	}
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate out:
579*7c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
580*7c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
581*7c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
582*7c478bd9Sstevel@tonic-gate }
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate static int
585*7c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
586*7c478bd9Sstevel@tonic-gate {
587*7c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
588*7c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
591*7c478bd9Sstevel@tonic-gate }
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate /*
594*7c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
595*7c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
596*7c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
597*7c478bd9Sstevel@tonic-gate  *
598*7c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
599*7c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
600*7c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
601*7c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
602*7c478bd9Sstevel@tonic-gate  * remains the case.
603*7c478bd9Sstevel@tonic-gate  */
604*7c478bd9Sstevel@tonic-gate static int
605*7c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
606*7c478bd9Sstevel@tonic-gate {
607*7c478bd9Sstevel@tonic-gate 	pid_t child_pid;
608*7c478bd9Sstevel@tonic-gate 	int child_status = 0;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 	/*
611*7c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
612*7c478bd9Sstevel@tonic-gate 	 */
613*7c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
614*7c478bd9Sstevel@tonic-gate 	child_pid = fork();
615*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
616*7c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
617*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
618*7c478bd9Sstevel@tonic-gate 		return (-1);
619*7c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
620*7c478bd9Sstevel@tonic-gate 		closefrom(0);
621*7c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
622*7c478bd9Sstevel@tonic-gate 		/*
623*7c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
624*7c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
625*7c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
626*7c478bd9Sstevel@tonic-gate 		 * accordingly.
627*7c478bd9Sstevel@tonic-gate 		 */
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
630*7c478bd9Sstevel@tonic-gate 	} else {
631*7c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
632*7c478bd9Sstevel@tonic-gate 	}
633*7c478bd9Sstevel@tonic-gate 
634*7c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
635*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
636*7c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
637*7c478bd9Sstevel@tonic-gate 		return (-1);
638*7c478bd9Sstevel@tonic-gate 	}
639*7c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
640*7c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
641*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
642*7c478bd9Sstevel@tonic-gate 		return (-1);
643*7c478bd9Sstevel@tonic-gate 	}
644*7c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
645*7c478bd9Sstevel@tonic-gate }
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate static int
648*7c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
649*7c478bd9Sstevel@tonic-gate {
650*7c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
651*7c478bd9Sstevel@tonic-gate 	char *argv[4];
652*7c478bd9Sstevel@tonic-gate 	int status;
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	/*
655*7c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
656*7c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
657*7c478bd9Sstevel@tonic-gate 	 */
658*7c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
659*7c478bd9Sstevel@tonic-gate 	    > sizeof (cmdbuf)) {
660*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
661*7c478bd9Sstevel@tonic-gate 		return (-1);
662*7c478bd9Sstevel@tonic-gate 	}
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
665*7c478bd9Sstevel@tonic-gate 	argv[1] = "-m";
666*7c478bd9Sstevel@tonic-gate 	argv[2] = (char *)rawdev;
667*7c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
670*7c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
671*7c478bd9Sstevel@tonic-gate 		return (status);
672*7c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
673*7c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
674*7c478bd9Sstevel@tonic-gate 	return (-1);
675*7c478bd9Sstevel@tonic-gate }
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate static int
678*7c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
679*7c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
680*7c478bd9Sstevel@tonic-gate {
681*7c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
682*7c478bd9Sstevel@tonic-gate 	char *argv[6];
683*7c478bd9Sstevel@tonic-gate 	int status;
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	/*
686*7c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
687*7c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
688*7c478bd9Sstevel@tonic-gate 	 */
689*7c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
690*7c478bd9Sstevel@tonic-gate 	    > sizeof (cmdbuf)) {
691*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
692*7c478bd9Sstevel@tonic-gate 		return (-1);
693*7c478bd9Sstevel@tonic-gate 	}
694*7c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
695*7c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
696*7c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
697*7c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
698*7c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
699*7c478bd9Sstevel@tonic-gate 	} else {
700*7c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
701*7c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
702*7c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
703*7c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
704*7c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
705*7c478bd9Sstevel@tonic-gate 	}
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
708*7c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
709*7c478bd9Sstevel@tonic-gate 		return (status);
710*7c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
711*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
712*7c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
713*7c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
714*7c478bd9Sstevel@tonic-gate 	else
715*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
716*7c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
717*7c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
718*7c478bd9Sstevel@tonic-gate 	return (-1);
719*7c478bd9Sstevel@tonic-gate }
720*7c478bd9Sstevel@tonic-gate 
721*7c478bd9Sstevel@tonic-gate /*
722*7c478bd9Sstevel@tonic-gate  * Make sure if a given path exists, it is not a sym-link, and is a directory.
723*7c478bd9Sstevel@tonic-gate  */
724*7c478bd9Sstevel@tonic-gate static int
725*7c478bd9Sstevel@tonic-gate check_path(zlog_t *zlogp, const char *path)
726*7c478bd9Sstevel@tonic-gate {
727*7c478bd9Sstevel@tonic-gate 	struct stat statbuf;
728*7c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
729*7c478bd9Sstevel@tonic-gate 	int res;
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
732*7c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
733*7c478bd9Sstevel@tonic-gate 			return (0);
734*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
735*7c478bd9Sstevel@tonic-gate 		return (-1);
736*7c478bd9Sstevel@tonic-gate 	}
737*7c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
738*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
739*7c478bd9Sstevel@tonic-gate 		return (-1);
740*7c478bd9Sstevel@tonic-gate 	}
741*7c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(statbuf.st_mode)) {
742*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
743*7c478bd9Sstevel@tonic-gate 		return (-1);
744*7c478bd9Sstevel@tonic-gate 	}
745*7c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
746*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
747*7c478bd9Sstevel@tonic-gate 		return (-1);
748*7c478bd9Sstevel@tonic-gate 	}
749*7c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
750*7c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
751*7c478bd9Sstevel@tonic-gate 		/*
752*7c478bd9Sstevel@tonic-gate 		 * We don't like ".."s and "."s throwing us off
753*7c478bd9Sstevel@tonic-gate 		 */
754*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
755*7c478bd9Sstevel@tonic-gate 		return (-1);
756*7c478bd9Sstevel@tonic-gate 	}
757*7c478bd9Sstevel@tonic-gate 	return (0);
758*7c478bd9Sstevel@tonic-gate }
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate /*
761*7c478bd9Sstevel@tonic-gate  * Check every component of rootpath/relpath.  If any component fails (ie,
762*7c478bd9Sstevel@tonic-gate  * exists but isn't the canonical path to a directory), it is returned in
763*7c478bd9Sstevel@tonic-gate  * badpath, which is assumed to be at least of size MAXPATHLEN.
764*7c478bd9Sstevel@tonic-gate  *
765*7c478bd9Sstevel@tonic-gate  * Relpath must begin with '/'.
766*7c478bd9Sstevel@tonic-gate  */
767*7c478bd9Sstevel@tonic-gate static boolean_t
768*7c478bd9Sstevel@tonic-gate valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath)
769*7c478bd9Sstevel@tonic-gate {
770*7c478bd9Sstevel@tonic-gate 	char abspath[MAXPATHLEN], *slashp;
771*7c478bd9Sstevel@tonic-gate 
772*7c478bd9Sstevel@tonic-gate 	/*
773*7c478bd9Sstevel@tonic-gate 	 * Make sure abspath has at least one '/' after its rootpath
774*7c478bd9Sstevel@tonic-gate 	 * component, and ends with '/'.
775*7c478bd9Sstevel@tonic-gate 	 */
776*7c478bd9Sstevel@tonic-gate 	if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) >
777*7c478bd9Sstevel@tonic-gate 	    sizeof (abspath)) {
778*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath,
779*7c478bd9Sstevel@tonic-gate 		    relpath);
780*7c478bd9Sstevel@tonic-gate 		return (B_FALSE);
781*7c478bd9Sstevel@tonic-gate 	}
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
784*7c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
785*7c478bd9Sstevel@tonic-gate 	do {
786*7c478bd9Sstevel@tonic-gate 		*slashp = '\0';
787*7c478bd9Sstevel@tonic-gate 		if (check_path(zlogp, abspath) != 0)
788*7c478bd9Sstevel@tonic-gate 			return (B_FALSE);
789*7c478bd9Sstevel@tonic-gate 		*slashp = '/';
790*7c478bd9Sstevel@tonic-gate 		slashp++;
791*7c478bd9Sstevel@tonic-gate 	} while ((slashp = strchr(slashp, '/')) != NULL);
792*7c478bd9Sstevel@tonic-gate 	return (B_TRUE);
793*7c478bd9Sstevel@tonic-gate }
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate static int
796*7c478bd9Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath)
797*7c478bd9Sstevel@tonic-gate {
798*7c478bd9Sstevel@tonic-gate 	char    path[MAXPATHLEN];
799*7c478bd9Sstevel@tonic-gate 	char    optstr[MAX_MNTOPT_STR];
800*7c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate 	if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) {
803*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
804*7c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
805*7c478bd9Sstevel@tonic-gate 		return (-1);
806*7c478bd9Sstevel@tonic-gate 	}
807*7c478bd9Sstevel@tonic-gate 
808*7c478bd9Sstevel@tonic-gate 	if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
809*7c478bd9Sstevel@tonic-gate 	    DEFAULT_DIR_MODE) != 0)
810*7c478bd9Sstevel@tonic-gate 		return (-1);
811*7c478bd9Sstevel@tonic-gate 
812*7c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
813*7c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate 	if (strlen(fsptr->zone_fs_special) == 0) {
816*7c478bd9Sstevel@tonic-gate 		/*
817*7c478bd9Sstevel@tonic-gate 		 * A zero-length special is how we distinguish IPDs from
818*7c478bd9Sstevel@tonic-gate 		 * general-purpose FSs.
819*7c478bd9Sstevel@tonic-gate 		 */
820*7c478bd9Sstevel@tonic-gate 		if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS,
821*7c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, path) != 0) {
822*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to loopback mount %s",
823*7c478bd9Sstevel@tonic-gate 			    fsptr->zone_fs_dir);
824*7c478bd9Sstevel@tonic-gate 			return (-1);
825*7c478bd9Sstevel@tonic-gate 		}
826*7c478bd9Sstevel@tonic-gate 		return (0);
827*7c478bd9Sstevel@tonic-gate 	}
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate 	/*
830*7c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
831*7c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
832*7c478bd9Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
833*7c478bd9Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
834*7c478bd9Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
835*7c478bd9Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
836*7c478bd9Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
837*7c478bd9Sstevel@tonic-gate 	 */
838*7c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
839*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
840*7c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
841*7c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
842*7c478bd9Sstevel@tonic-gate 		return (-1);
843*7c478bd9Sstevel@tonic-gate 	}
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 	/*
846*7c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
847*7c478bd9Sstevel@tonic-gate 	 */
848*7c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
849*7c478bd9Sstevel@tonic-gate 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0)
850*7c478bd9Sstevel@tonic-gate 		return (-1);
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate 	/*
853*7c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
854*7c478bd9Sstevel@tonic-gate 	 */
855*7c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
856*7c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
857*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
858*7c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
859*7c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
860*7c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
861*7c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
862*7c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
863*7c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
864*7c478bd9Sstevel@tonic-gate 		}
865*7c478bd9Sstevel@tonic-gate 	}
866*7c478bd9Sstevel@tonic-gate 	return (domount(zlogp, fsptr->zone_fs_type, optstr,
867*7c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_special, path));
868*7c478bd9Sstevel@tonic-gate }
869*7c478bd9Sstevel@tonic-gate 
870*7c478bd9Sstevel@tonic-gate static void
871*7c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
872*7c478bd9Sstevel@tonic-gate {
873*7c478bd9Sstevel@tonic-gate 	uint_t i;
874*7c478bd9Sstevel@tonic-gate 
875*7c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
876*7c478bd9Sstevel@tonic-gate 		return;
877*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
878*7c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
879*7c478bd9Sstevel@tonic-gate 	free(fsarray);
880*7c478bd9Sstevel@tonic-gate }
881*7c478bd9Sstevel@tonic-gate 
882*7c478bd9Sstevel@tonic-gate static int
883*7c478bd9Sstevel@tonic-gate mount_filesystems(zlog_t *zlogp)
884*7c478bd9Sstevel@tonic-gate {
885*7c478bd9Sstevel@tonic-gate 	char	rootpath[MAXPATHLEN];
886*7c478bd9Sstevel@tonic-gate 	char	zonepath[MAXPATHLEN];
887*7c478bd9Sstevel@tonic-gate 	int	num_fs = 0, i;
888*7c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab, *fs_ptr = NULL, *tmp_ptr;
889*7c478bd9Sstevel@tonic-gate 	struct zone_fstab *fsp;
890*7c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
891*7c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
892*7c478bd9Sstevel@tonic-gate 
893*7c478bd9Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
894*7c478bd9Sstevel@tonic-gate 	    zstate != ZONE_STATE_READY) {
895*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
896*7c478bd9Sstevel@tonic-gate 		    "zone must be in '%s' state to mount file-systems",
897*7c478bd9Sstevel@tonic-gate 		    zone_state_str(ZONE_STATE_READY));
898*7c478bd9Sstevel@tonic-gate 		goto bad;
899*7c478bd9Sstevel@tonic-gate 	}
900*7c478bd9Sstevel@tonic-gate 
901*7c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
902*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
903*7c478bd9Sstevel@tonic-gate 		goto bad;
904*7c478bd9Sstevel@tonic-gate 	}
905*7c478bd9Sstevel@tonic-gate 
906*7c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
907*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
908*7c478bd9Sstevel@tonic-gate 		goto bad;
909*7c478bd9Sstevel@tonic-gate 	}
910*7c478bd9Sstevel@tonic-gate 
911*7c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
912*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
913*7c478bd9Sstevel@tonic-gate 		    "could not get zone configuration handle");
914*7c478bd9Sstevel@tonic-gate 		goto bad;
915*7c478bd9Sstevel@tonic-gate 	}
916*7c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
917*7c478bd9Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
918*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
919*7c478bd9Sstevel@tonic-gate 		goto bad;
920*7c478bd9Sstevel@tonic-gate 	}
921*7c478bd9Sstevel@tonic-gate 
922*7c478bd9Sstevel@tonic-gate 	/*
923*7c478bd9Sstevel@tonic-gate 	 * /dev in the zone is loopback'd from the external /dev repository,
924*7c478bd9Sstevel@tonic-gate 	 * in order to provide a largely read-only semantic.  But because
925*7c478bd9Sstevel@tonic-gate 	 * processes in the zone need to be able to chown, chmod, etc. zone
926*7c478bd9Sstevel@tonic-gate 	 * /dev files, we can't use a 'ro' lofs mount.  Instead we use a
927*7c478bd9Sstevel@tonic-gate 	 * special mode just for zones, "zonedevfs".
928*7c478bd9Sstevel@tonic-gate 	 *
929*7c478bd9Sstevel@tonic-gate 	 * In the future we should front /dev with a full-fledged filesystem.
930*7c478bd9Sstevel@tonic-gate 	 */
931*7c478bd9Sstevel@tonic-gate 	num_fs++;
932*7c478bd9Sstevel@tonic-gate 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
933*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
934*7c478bd9Sstevel@tonic-gate 		num_fs--;
935*7c478bd9Sstevel@tonic-gate 		goto bad;
936*7c478bd9Sstevel@tonic-gate 	}
937*7c478bd9Sstevel@tonic-gate 	fs_ptr = tmp_ptr;
938*7c478bd9Sstevel@tonic-gate 	fsp = &fs_ptr[num_fs - 1];
939*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(fsp->zone_fs_dir, "/dev", sizeof (fsp->zone_fs_dir));
940*7c478bd9Sstevel@tonic-gate 	(void) snprintf(fsp->zone_fs_special, sizeof (fsp->zone_fs_special),
941*7c478bd9Sstevel@tonic-gate 	    "%s/dev", zonepath);
942*7c478bd9Sstevel@tonic-gate 	fsp->zone_fs_raw[0] = '\0';
943*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(fsp->zone_fs_type, MNTTYPE_LOFS,
944*7c478bd9Sstevel@tonic-gate 	    sizeof (fsp->zone_fs_type));
945*7c478bd9Sstevel@tonic-gate 	fsp->zone_fs_options = NULL;
946*7c478bd9Sstevel@tonic-gate 	if (zonecfg_add_fs_option(fsp, MNTOPT_LOFS_ZONEDEVFS) != Z_OK) {
947*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "error adding property");
948*7c478bd9Sstevel@tonic-gate 		goto bad;
949*7c478bd9Sstevel@tonic-gate 	}
950*7c478bd9Sstevel@tonic-gate 
951*7c478bd9Sstevel@tonic-gate 	/*
952*7c478bd9Sstevel@tonic-gate 	 * Iterate through the rest of the filesystems, first the IPDs, then
953*7c478bd9Sstevel@tonic-gate 	 * the general FSs.  Sort them all, then mount them in sorted order.
954*7c478bd9Sstevel@tonic-gate 	 * This is to make sure the higher level directories (e.g., /usr)
955*7c478bd9Sstevel@tonic-gate 	 * get mounted before any beneath them (e.g., /usr/local).
956*7c478bd9Sstevel@tonic-gate 	 */
957*7c478bd9Sstevel@tonic-gate 	if (zonecfg_setipdent(handle) != Z_OK) {
958*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
959*7c478bd9Sstevel@tonic-gate 		goto bad;
960*7c478bd9Sstevel@tonic-gate 	}
961*7c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
962*7c478bd9Sstevel@tonic-gate 		num_fs++;
963*7c478bd9Sstevel@tonic-gate 		if ((tmp_ptr = realloc(fs_ptr,
964*7c478bd9Sstevel@tonic-gate 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
965*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
966*7c478bd9Sstevel@tonic-gate 			num_fs--;
967*7c478bd9Sstevel@tonic-gate 			(void) zonecfg_endipdent(handle);
968*7c478bd9Sstevel@tonic-gate 			goto bad;
969*7c478bd9Sstevel@tonic-gate 		}
970*7c478bd9Sstevel@tonic-gate 		fs_ptr = tmp_ptr;
971*7c478bd9Sstevel@tonic-gate 		fsp = &fs_ptr[num_fs - 1];
972*7c478bd9Sstevel@tonic-gate 		/*
973*7c478bd9Sstevel@tonic-gate 		 * IPDs logically only have a mount point; all other properties
974*7c478bd9Sstevel@tonic-gate 		 * are implied.
975*7c478bd9Sstevel@tonic-gate 		 */
976*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_dir,
977*7c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
978*7c478bd9Sstevel@tonic-gate 		fsp->zone_fs_special[0] = '\0';
979*7c478bd9Sstevel@tonic-gate 		fsp->zone_fs_raw[0] = '\0';
980*7c478bd9Sstevel@tonic-gate 		fsp->zone_fs_type[0] = '\0';
981*7c478bd9Sstevel@tonic-gate 		fsp->zone_fs_options = NULL;
982*7c478bd9Sstevel@tonic-gate 	}
983*7c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
984*7c478bd9Sstevel@tonic-gate 
985*7c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
986*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
987*7c478bd9Sstevel@tonic-gate 		goto bad;
988*7c478bd9Sstevel@tonic-gate 	}
989*7c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
990*7c478bd9Sstevel@tonic-gate 		num_fs++;
991*7c478bd9Sstevel@tonic-gate 		if ((tmp_ptr = realloc(fs_ptr,
992*7c478bd9Sstevel@tonic-gate 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
993*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
994*7c478bd9Sstevel@tonic-gate 			num_fs--;
995*7c478bd9Sstevel@tonic-gate 			(void) zonecfg_endfsent(handle);
996*7c478bd9Sstevel@tonic-gate 			goto bad;
997*7c478bd9Sstevel@tonic-gate 		}
998*7c478bd9Sstevel@tonic-gate 		fs_ptr = tmp_ptr;
999*7c478bd9Sstevel@tonic-gate 		fsp = &fs_ptr[num_fs - 1];
1000*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_dir,
1001*7c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
1002*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special,
1003*7c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_special));
1004*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
1005*7c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_raw));
1006*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
1007*7c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_type));
1008*7c478bd9Sstevel@tonic-gate 		fsp->zone_fs_options = fstab.zone_fs_options;
1009*7c478bd9Sstevel@tonic-gate 	}
1010*7c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
1011*7c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
1012*7c478bd9Sstevel@tonic-gate 	handle = NULL;
1013*7c478bd9Sstevel@tonic-gate 
1014*7c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
1015*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
1016*7c478bd9Sstevel@tonic-gate 		if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0)
1017*7c478bd9Sstevel@tonic-gate 			goto bad;
1018*7c478bd9Sstevel@tonic-gate 	}
1019*7c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
1020*7c478bd9Sstevel@tonic-gate 
1021*7c478bd9Sstevel@tonic-gate 	/*
1022*7c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
1023*7c478bd9Sstevel@tonic-gate 	 */
1024*7c478bd9Sstevel@tonic-gate 	return (0);
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate bad:
1027*7c478bd9Sstevel@tonic-gate 	if (handle != NULL)
1028*7c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
1029*7c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
1030*7c478bd9Sstevel@tonic-gate 	return (-1);
1031*7c478bd9Sstevel@tonic-gate }
1032*7c478bd9Sstevel@tonic-gate 
1033*7c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
1034*7c478bd9Sstevel@tonic-gate static int
1035*7c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
1036*7c478bd9Sstevel@tonic-gate {
1037*7c478bd9Sstevel@tonic-gate 	int prefixlen;
1038*7c478bd9Sstevel@tonic-gate 
1039*7c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
1040*7c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
1041*7c478bd9Sstevel@tonic-gate 		return (1);
1042*7c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
1043*7c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
1044*7c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
1045*7c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
1046*7c478bd9Sstevel@tonic-gate 			continue;
1047*7c478bd9Sstevel@tonic-gate 		}
1048*7c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
1049*7c478bd9Sstevel@tonic-gate 		prefixlen--;
1050*7c478bd9Sstevel@tonic-gate 	}
1051*7c478bd9Sstevel@tonic-gate 	return (0);
1052*7c478bd9Sstevel@tonic-gate }
1053*7c478bd9Sstevel@tonic-gate 
1054*7c478bd9Sstevel@tonic-gate /*
1055*7c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
1056*7c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
1057*7c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
1058*7c478bd9Sstevel@tonic-gate  *
1059*7c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
1060*7c478bd9Sstevel@tonic-gate  */
1061*7c478bd9Sstevel@tonic-gate static int
1062*7c478bd9Sstevel@tonic-gate unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
1063*7c478bd9Sstevel@tonic-gate {
1064*7c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
1065*7c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
1066*7c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
1067*7c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
1068*7c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
1069*7c478bd9Sstevel@tonic-gate 	uint_t bufsize;
1070*7c478bd9Sstevel@tonic-gate 	char *buf = NULL;
1071*7c478bd9Sstevel@tonic-gate 
1072*7c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1073*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
1074*7c478bd9Sstevel@tonic-gate 		ret_code = -1;
1075*7c478bd9Sstevel@tonic-gate 		goto bad;
1076*7c478bd9Sstevel@tonic-gate 	}
1077*7c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
1078*7c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
1079*7c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
1080*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1081*7c478bd9Sstevel@tonic-gate 		    "could not determine number of interfaces");
1082*7c478bd9Sstevel@tonic-gate 		ret_code = -1;
1083*7c478bd9Sstevel@tonic-gate 		goto bad;
1084*7c478bd9Sstevel@tonic-gate 	}
1085*7c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
1086*7c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
1087*7c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
1088*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
1089*7c478bd9Sstevel@tonic-gate 		ret_code = -1;
1090*7c478bd9Sstevel@tonic-gate 		goto bad;
1091*7c478bd9Sstevel@tonic-gate 	}
1092*7c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
1093*7c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
1094*7c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
1095*7c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
1096*7c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1097*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get configured interfaces");
1098*7c478bd9Sstevel@tonic-gate 		ret_code = -1;
1099*7c478bd9Sstevel@tonic-gate 		goto bad;
1100*7c478bd9Sstevel@tonic-gate 	}
1101*7c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
1102*7c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
1103*7c478bd9Sstevel@tonic-gate 		(void) close(s);
1104*7c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
1105*7c478bd9Sstevel@tonic-gate 		    0) {
1106*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
1107*7c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
1108*7c478bd9Sstevel@tonic-gate 			ret_code = -1;
1109*7c478bd9Sstevel@tonic-gate 			continue;
1110*7c478bd9Sstevel@tonic-gate 		}
1111*7c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
1112*7c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
1113*7c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
1114*7c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1115*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1116*7c478bd9Sstevel@tonic-gate 			    "%s: could not determine zone interface belongs to",
1117*7c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
1118*7c478bd9Sstevel@tonic-gate 			ret_code = -1;
1119*7c478bd9Sstevel@tonic-gate 			continue;
1120*7c478bd9Sstevel@tonic-gate 		}
1121*7c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
1122*7c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
1123*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1124*7c478bd9Sstevel@tonic-gate 				    "%s: could not remove interface",
1125*7c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
1126*7c478bd9Sstevel@tonic-gate 				ret_code = -1;
1127*7c478bd9Sstevel@tonic-gate 				continue;
1128*7c478bd9Sstevel@tonic-gate 			}
1129*7c478bd9Sstevel@tonic-gate 		}
1130*7c478bd9Sstevel@tonic-gate 	}
1131*7c478bd9Sstevel@tonic-gate bad:
1132*7c478bd9Sstevel@tonic-gate 	if (s > 0)
1133*7c478bd9Sstevel@tonic-gate 		(void) close(s);
1134*7c478bd9Sstevel@tonic-gate 	if (buf)
1135*7c478bd9Sstevel@tonic-gate 		free(buf);
1136*7c478bd9Sstevel@tonic-gate 	return (ret_code);
1137*7c478bd9Sstevel@tonic-gate }
1138*7c478bd9Sstevel@tonic-gate 
1139*7c478bd9Sstevel@tonic-gate static union	sockunion {
1140*7c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
1141*7c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
1142*7c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
1143*7c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
1144*7c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
1145*7c478bd9Sstevel@tonic-gate 
1146*7c478bd9Sstevel@tonic-gate static struct {
1147*7c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
1148*7c478bd9Sstevel@tonic-gate 	char	space[512];
1149*7c478bd9Sstevel@tonic-gate } rtmsg;
1150*7c478bd9Sstevel@tonic-gate 
1151*7c478bd9Sstevel@tonic-gate static int
1152*7c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
1153*7c478bd9Sstevel@tonic-gate {
1154*7c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
1155*7c478bd9Sstevel@tonic-gate 	case AF_INET:
1156*7c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
1157*7c478bd9Sstevel@tonic-gate 	case AF_LINK:
1158*7c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
1159*7c478bd9Sstevel@tonic-gate 	case AF_INET6:
1160*7c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
1161*7c478bd9Sstevel@tonic-gate 	default:
1162*7c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
1163*7c478bd9Sstevel@tonic-gate 	}
1164*7c478bd9Sstevel@tonic-gate }
1165*7c478bd9Sstevel@tonic-gate 
1166*7c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
1167*7c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
1168*7c478bd9Sstevel@tonic-gate 
1169*7c478bd9Sstevel@tonic-gate /*
1170*7c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
1171*7c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
1172*7c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
1173*7c478bd9Sstevel@tonic-gate  *
1174*7c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
1175*7c478bd9Sstevel@tonic-gate  *
1176*7c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
1177*7c478bd9Sstevel@tonic-gate  *
1178*7c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
1179*7c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
1180*7c478bd9Sstevel@tonic-gate  */
1181*7c478bd9Sstevel@tonic-gate 
1182*7c478bd9Sstevel@tonic-gate static char *
1183*7c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
1184*7c478bd9Sstevel@tonic-gate {
1185*7c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
1186*7c478bd9Sstevel@tonic-gate 	pid_t pid;
1187*7c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
1188*7c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
1189*7c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
1190*7c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
1191*7c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
1192*7c478bd9Sstevel@tonic-gate 
1193*7c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
1194*7c478bd9Sstevel@tonic-gate 
1195*7c478bd9Sstevel@tonic-gate 	pid = getpid();
1196*7c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1197*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
1198*7c478bd9Sstevel@tonic-gate 		return (NULL);
1199*7c478bd9Sstevel@tonic-gate 	}
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
1202*7c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
1203*7c478bd9Sstevel@tonic-gate 
1204*7c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
1205*7c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
1206*7c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
1207*7c478bd9Sstevel@tonic-gate 	} else {
1208*7c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
1209*7c478bd9Sstevel@tonic-gate 
1210*7c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
1211*7c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
1212*7c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
1213*7c478bd9Sstevel@tonic-gate 	}
1214*7c478bd9Sstevel@tonic-gate 
1215*7c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
1216*7c478bd9Sstevel@tonic-gate 
1217*7c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
1218*7c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
1219*7c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
1220*7c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
1221*7c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
1222*7c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
1223*7c478bd9Sstevel@tonic-gate 
1224*7c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
1225*7c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
1226*7c478bd9Sstevel@tonic-gate 	cp += l;
1227*7c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
1228*7c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
1229*7c478bd9Sstevel@tonic-gate 	cp += l;
1230*7c478bd9Sstevel@tonic-gate 
1231*7c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
1232*7c478bd9Sstevel@tonic-gate 
1233*7c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
1234*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
1235*7c478bd9Sstevel@tonic-gate 		return (NULL);
1236*7c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
1237*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1238*7c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
1239*7c478bd9Sstevel@tonic-gate 		return (NULL);
1240*7c478bd9Sstevel@tonic-gate 	}
1241*7c478bd9Sstevel@tonic-gate 	do {
1242*7c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
1243*7c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
1244*7c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
1245*7c478bd9Sstevel@tonic-gate 	if (l < 0) {
1246*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
1247*7c478bd9Sstevel@tonic-gate 		return (NULL);
1248*7c478bd9Sstevel@tonic-gate 	}
1249*7c478bd9Sstevel@tonic-gate 
1250*7c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
1251*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1252*7c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
1253*7c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
1254*7c478bd9Sstevel@tonic-gate 		return (NULL);
1255*7c478bd9Sstevel@tonic-gate 	}
1256*7c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
1257*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
1258*7c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
1259*7c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
1260*7c478bd9Sstevel@tonic-gate 		return (NULL);
1261*7c478bd9Sstevel@tonic-gate 	}
1262*7c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
1263*7c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
1264*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
1265*7c478bd9Sstevel@tonic-gate 		return (NULL);
1266*7c478bd9Sstevel@tonic-gate 	}
1267*7c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
1268*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "interface not found");
1269*7c478bd9Sstevel@tonic-gate 		return (NULL);
1270*7c478bd9Sstevel@tonic-gate 	}
1271*7c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
1272*7c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
1273*7c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1274*7c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
1275*7c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
1276*7c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
1277*7c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
1278*7c478bd9Sstevel@tonic-gate 			continue;
1279*7c478bd9Sstevel@tonic-gate 		}
1280*7c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
1281*7c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
1282*7c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
1283*7c478bd9Sstevel@tonic-gate 		break;
1284*7c478bd9Sstevel@tonic-gate 	}
1285*7c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
1286*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "interface could not be determined");
1287*7c478bd9Sstevel@tonic-gate 		return (NULL);
1288*7c478bd9Sstevel@tonic-gate 	}
1289*7c478bd9Sstevel@tonic-gate 
1290*7c478bd9Sstevel@tonic-gate 	/*
1291*7c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
1292*7c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
1293*7c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
1294*7c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
1295*7c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
1296*7c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
1297*7c478bd9Sstevel@tonic-gate 	 */
1298*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
1299*7c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
1300*7c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
1301*7c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
1302*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
1303*7c478bd9Sstevel@tonic-gate 	if (i < 0) {
1304*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1305*7c478bd9Sstevel@tonic-gate 		    "%s: could not determine the zone interface belongs to",
1306*7c478bd9Sstevel@tonic-gate 		    lifr->lifr_name);
1307*7c478bd9Sstevel@tonic-gate 		return (NULL);
1308*7c478bd9Sstevel@tonic-gate 	}
1309*7c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
1310*7c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
1311*7c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
1312*7c478bd9Sstevel@tonic-gate 
1313*7c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
1314*7c478bd9Sstevel@tonic-gate 		return (answer);
1315*7c478bd9Sstevel@tonic-gate 	return (NULL);
1316*7c478bd9Sstevel@tonic-gate }
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate typedef struct mcast_rtmsg_s {
1319*7c478bd9Sstevel@tonic-gate 	struct rt_msghdr	m_rtm;
1320*7c478bd9Sstevel@tonic-gate 	union {
1321*7c478bd9Sstevel@tonic-gate 		struct {
1322*7c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_dst;
1323*7c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_gw;
1324*7c478bd9Sstevel@tonic-gate 			struct sockaddr_in	m_netmask;
1325*7c478bd9Sstevel@tonic-gate 		} m_v4;
1326*7c478bd9Sstevel@tonic-gate 		struct {
1327*7c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_dst;
1328*7c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_gw;
1329*7c478bd9Sstevel@tonic-gate 			struct sockaddr_in6	m_netmask;
1330*7c478bd9Sstevel@tonic-gate 		} m_v6;
1331*7c478bd9Sstevel@tonic-gate 	} m_u;
1332*7c478bd9Sstevel@tonic-gate } mcast_rtmsg_t;
1333*7c478bd9Sstevel@tonic-gate #define	m_dst4		m_u.m_v4.m_dst
1334*7c478bd9Sstevel@tonic-gate #define	m_dst6		m_u.m_v6.m_dst
1335*7c478bd9Sstevel@tonic-gate #define	m_gw4		m_u.m_v4.m_gw
1336*7c478bd9Sstevel@tonic-gate #define	m_gw6		m_u.m_v6.m_gw
1337*7c478bd9Sstevel@tonic-gate #define	m_netmask4	m_u.m_v4.m_netmask
1338*7c478bd9Sstevel@tonic-gate #define	m_netmask6	m_u.m_v6.m_netmask
1339*7c478bd9Sstevel@tonic-gate 
1340*7c478bd9Sstevel@tonic-gate /*
1341*7c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
1342*7c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
1343*7c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
1344*7c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
1345*7c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
1346*7c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
1347*7c478bd9Sstevel@tonic-gate  *
1348*7c478bd9Sstevel@tonic-gate  * A default interface route for multicast is created on the first IPv4 and
1349*7c478bd9Sstevel@tonic-gate  * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively.
1350*7c478bd9Sstevel@tonic-gate  * This should really be done in the init scripts if we ever allow zones to
1351*7c478bd9Sstevel@tonic-gate  * modify the routing tables.
1352*7c478bd9Sstevel@tonic-gate  *
1353*7c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
1354*7c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
1355*7c478bd9Sstevel@tonic-gate  */
1356*7c478bd9Sstevel@tonic-gate static int
1357*7c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
1358*7c478bd9Sstevel@tonic-gate     struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp,
1359*7c478bd9Sstevel@tonic-gate     boolean_t *mcast_rt_v6_setp)
1360*7c478bd9Sstevel@tonic-gate {
1361*7c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
1362*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
1363*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
1364*7c478bd9Sstevel@tonic-gate 	struct in_addr in4;
1365*7c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
1366*7c478bd9Sstevel@tonic-gate 	sa_family_t af;
1367*7c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
1368*7c478bd9Sstevel@tonic-gate 	mcast_rtmsg_t mcast_rtmsg;
1369*7c478bd9Sstevel@tonic-gate 	int s;
1370*7c478bd9Sstevel@tonic-gate 	int rs;
1371*7c478bd9Sstevel@tonic-gate 	int rlen;
1372*7c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
1373*7c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
1374*7c478bd9Sstevel@tonic-gate 	int res;
1375*7c478bd9Sstevel@tonic-gate 
1376*7c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
1377*7c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
1378*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
1379*7c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
1380*7c478bd9Sstevel@tonic-gate 		return (-1);
1381*7c478bd9Sstevel@tonic-gate 	}
1382*7c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
1383*7c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
1384*7c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
1385*7c478bd9Sstevel@tonic-gate 	else
1386*7c478bd9Sstevel@tonic-gate 		in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr;
1387*7c478bd9Sstevel@tonic-gate 
1388*7c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
1389*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
1390*7c478bd9Sstevel@tonic-gate 		return (-1);
1391*7c478bd9Sstevel@tonic-gate 	}
1392*7c478bd9Sstevel@tonic-gate 
1393*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
1394*7c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
1395*7c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
1396*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not add interface",
1397*7c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
1398*7c478bd9Sstevel@tonic-gate 		(void) close(s);
1399*7c478bd9Sstevel@tonic-gate 		return (-1);
1400*7c478bd9Sstevel@tonic-gate 	}
1401*7c478bd9Sstevel@tonic-gate 
1402*7c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
1403*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1404*7c478bd9Sstevel@tonic-gate 		    "%s: could not set IP address to %s",
1405*7c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
1406*7c478bd9Sstevel@tonic-gate 		goto bad;
1407*7c478bd9Sstevel@tonic-gate 	}
1408*7c478bd9Sstevel@tonic-gate 
1409*7c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
1410*7c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
1411*7c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
1412*7c478bd9Sstevel@tonic-gate 
1413*7c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
1414*7c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
1415*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not place interface into zone",
1416*7c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
1417*7c478bd9Sstevel@tonic-gate 		goto bad;
1418*7c478bd9Sstevel@tonic-gate 	}
1419*7c478bd9Sstevel@tonic-gate 
1420*7c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
1421*7c478bd9Sstevel@tonic-gate 		got_netmask = B_TRUE;	/* default setting will be correct */
1422*7c478bd9Sstevel@tonic-gate 	} else {
1423*7c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
1424*7c478bd9Sstevel@tonic-gate 			/*
1425*7c478bd9Sstevel@tonic-gate 			 * The IPv4 netmask can be determined either
1426*7c478bd9Sstevel@tonic-gate 			 * directly if a prefix length was supplied with
1427*7c478bd9Sstevel@tonic-gate 			 * the address or via the netmasks database.  Not
1428*7c478bd9Sstevel@tonic-gate 			 * being able to determine it is a common failure,
1429*7c478bd9Sstevel@tonic-gate 			 * but it often is not fatal to operation of the
1430*7c478bd9Sstevel@tonic-gate 			 * interface.  In that case, a warning will be
1431*7c478bd9Sstevel@tonic-gate 			 * printed after the rest of the interface's
1432*7c478bd9Sstevel@tonic-gate 			 * parameters have been configured.
1433*7c478bd9Sstevel@tonic-gate 			 */
1434*7c478bd9Sstevel@tonic-gate 			(void) memset(&netmask4, 0, sizeof (netmask4));
1435*7c478bd9Sstevel@tonic-gate 			if (slashp != NULL) {
1436*7c478bd9Sstevel@tonic-gate 				if (addr2netmask(slashp + 1, V4_ADDR_LEN,
1437*7c478bd9Sstevel@tonic-gate 				    (uchar_t *)&netmask4.sin_addr) != 0) {
1438*7c478bd9Sstevel@tonic-gate 					*slashp = '/';
1439*7c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
1440*7c478bd9Sstevel@tonic-gate 					    "%s: invalid prefix length in %s",
1441*7c478bd9Sstevel@tonic-gate 					    lifr.lifr_name,
1442*7c478bd9Sstevel@tonic-gate 					    nwiftabptr->zone_nwif_address);
1443*7c478bd9Sstevel@tonic-gate 					goto bad;
1444*7c478bd9Sstevel@tonic-gate 				}
1445*7c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
1446*7c478bd9Sstevel@tonic-gate 			} else if (getnetmaskbyaddr(in4,
1447*7c478bd9Sstevel@tonic-gate 			    &netmask4.sin_addr) == 0) {
1448*7c478bd9Sstevel@tonic-gate 				got_netmask = B_TRUE;
1449*7c478bd9Sstevel@tonic-gate 			}
1450*7c478bd9Sstevel@tonic-gate 			if (got_netmask) {
1451*7c478bd9Sstevel@tonic-gate 				netmask4.sin_family = af;
1452*7c478bd9Sstevel@tonic-gate 				(void) memcpy(&lifr.lifr_addr, &netmask4,
1453*7c478bd9Sstevel@tonic-gate 				    sizeof (netmask4));
1454*7c478bd9Sstevel@tonic-gate 			}
1455*7c478bd9Sstevel@tonic-gate 		} else {
1456*7c478bd9Sstevel@tonic-gate 			(void) memset(&netmask6, 0, sizeof (netmask6));
1457*7c478bd9Sstevel@tonic-gate 			if (addr2netmask(slashp + 1, V6_ADDR_LEN,
1458*7c478bd9Sstevel@tonic-gate 			    (uchar_t *)&netmask6.sin6_addr) != 0) {
1459*7c478bd9Sstevel@tonic-gate 				*slashp = '/';
1460*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
1461*7c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
1462*7c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
1463*7c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
1464*7c478bd9Sstevel@tonic-gate 				goto bad;
1465*7c478bd9Sstevel@tonic-gate 			}
1466*7c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
1467*7c478bd9Sstevel@tonic-gate 			netmask6.sin6_family = af;
1468*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&lifr.lifr_addr, &netmask6,
1469*7c478bd9Sstevel@tonic-gate 			    sizeof (netmask6));
1470*7c478bd9Sstevel@tonic-gate 		}
1471*7c478bd9Sstevel@tonic-gate 		if (got_netmask &&
1472*7c478bd9Sstevel@tonic-gate 		    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
1473*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not set netmask",
1474*7c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
1475*7c478bd9Sstevel@tonic-gate 			goto bad;
1476*7c478bd9Sstevel@tonic-gate 		}
1477*7c478bd9Sstevel@tonic-gate 
1478*7c478bd9Sstevel@tonic-gate 		/*
1479*7c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
1480*7c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
1481*7c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
1482*7c478bd9Sstevel@tonic-gate 		 */
1483*7c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
1484*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
1485*7c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
1486*7c478bd9Sstevel@tonic-gate 			goto bad;
1487*7c478bd9Sstevel@tonic-gate 		}
1488*7c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
1489*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1490*7c478bd9Sstevel@tonic-gate 			    "%s: could not reset broadcast address",
1491*7c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
1492*7c478bd9Sstevel@tonic-gate 			goto bad;
1493*7c478bd9Sstevel@tonic-gate 		}
1494*7c478bd9Sstevel@tonic-gate 	}
1495*7c478bd9Sstevel@tonic-gate 
1496*7c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
1497*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
1498*7c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
1499*7c478bd9Sstevel@tonic-gate 		goto bad;
1500*7c478bd9Sstevel@tonic-gate 	}
1501*7c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
1502*7c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
1503*7c478bd9Sstevel@tonic-gate 		int save_errno = errno;
1504*7c478bd9Sstevel@tonic-gate 		char *zone_using;
1505*7c478bd9Sstevel@tonic-gate 
1506*7c478bd9Sstevel@tonic-gate 		/*
1507*7c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
1508*7c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
1509*7c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
1510*7c478bd9Sstevel@tonic-gate 		 * using that address.
1511*7c478bd9Sstevel@tonic-gate 		 */
1512*7c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
1513*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1514*7c478bd9Sstevel@tonic-gate 			    "%s: could not bring interface up", lifr.lifr_name);
1515*7c478bd9Sstevel@tonic-gate 			goto bad;
1516*7c478bd9Sstevel@tonic-gate 		}
1517*7c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
1518*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
1519*7c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
1520*7c478bd9Sstevel@tonic-gate 			goto bad;
1521*7c478bd9Sstevel@tonic-gate 		}
1522*7c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
1523*7c478bd9Sstevel@tonic-gate 		errno = save_errno;
1524*7c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
1525*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1526*7c478bd9Sstevel@tonic-gate 			    "%s: could not bring interface up", lifr.lifr_name);
1527*7c478bd9Sstevel@tonic-gate 		else
1528*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not bring interface "
1529*7c478bd9Sstevel@tonic-gate 			    "up: address in use by zone '%s'", lifr.lifr_name,
1530*7c478bd9Sstevel@tonic-gate 			    zone_using);
1531*7c478bd9Sstevel@tonic-gate 		goto bad;
1532*7c478bd9Sstevel@tonic-gate 	}
1533*7c478bd9Sstevel@tonic-gate 	if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET &&
1534*7c478bd9Sstevel@tonic-gate 	    mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) ||
1535*7c478bd9Sstevel@tonic-gate 	    (af == AF_INET6 &&
1536*7c478bd9Sstevel@tonic-gate 	    mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) {
1537*7c478bd9Sstevel@tonic-gate 		rs = socket(PF_ROUTE, SOCK_RAW, 0);
1538*7c478bd9Sstevel@tonic-gate 		if (rs < 0) {
1539*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not create "
1540*7c478bd9Sstevel@tonic-gate 			    "routing socket", lifr.lifr_name);
1541*7c478bd9Sstevel@tonic-gate 			goto bad;
1542*7c478bd9Sstevel@tonic-gate 		}
1543*7c478bd9Sstevel@tonic-gate 		(void) shutdown(rs, 0);
1544*7c478bd9Sstevel@tonic-gate 		(void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t));
1545*7c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_msglen =  sizeof (struct rt_msghdr) +
1546*7c478bd9Sstevel@tonic-gate 		    3 * (af == AF_INET ? sizeof (struct sockaddr_in) :
1547*7c478bd9Sstevel@tonic-gate 		    sizeof (struct sockaddr_in6));
1548*7c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION;
1549*7c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_type = RTM_ADD;
1550*7c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_flags = RTF_UP;
1551*7c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_addrs =
1552*7c478bd9Sstevel@tonic-gate 		    RTA_DST | RTA_GATEWAY | RTA_NETMASK;
1553*7c478bd9Sstevel@tonic-gate 		mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno;
1554*7c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
1555*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_family = AF_INET;
1556*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst4.sin_addr.s_addr =
1557*7c478bd9Sstevel@tonic-gate 			    htonl(INADDR_UNSPEC_GROUP);
1558*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_family = AF_INET;
1559*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw4.sin_addr = in4;
1560*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_family = AF_INET;
1561*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask4.sin_addr.s_addr =
1562*7c478bd9Sstevel@tonic-gate 			    htonl(IN_CLASSD_NET);
1563*7c478bd9Sstevel@tonic-gate 		} else {
1564*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_family = AF_INET6;
1565*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU;
1566*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_family = AF_INET6;
1567*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_gw6.sin6_addr = in6;
1568*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_family = AF_INET6;
1569*7c478bd9Sstevel@tonic-gate 			mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU;
1570*7c478bd9Sstevel@tonic-gate 		}
1571*7c478bd9Sstevel@tonic-gate 		rlen = write(rs, (char *)&mcast_rtmsg,
1572*7c478bd9Sstevel@tonic-gate 		    mcast_rtmsg.m_rtm.rtm_msglen);
1573*7c478bd9Sstevel@tonic-gate 		if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) {
1574*7c478bd9Sstevel@tonic-gate 			if (rlen < 0) {
1575*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s: could not set "
1576*7c478bd9Sstevel@tonic-gate 				    "default interface for multicast",
1577*7c478bd9Sstevel@tonic-gate 				    lifr.lifr_name);
1578*7c478bd9Sstevel@tonic-gate 			} else {
1579*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s: write to routing "
1580*7c478bd9Sstevel@tonic-gate 				    "socket returned %d", lifr.lifr_name, rlen);
1581*7c478bd9Sstevel@tonic-gate 			}
1582*7c478bd9Sstevel@tonic-gate 			(void) close(rs);
1583*7c478bd9Sstevel@tonic-gate 			goto bad;
1584*7c478bd9Sstevel@tonic-gate 		}
1585*7c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
1586*7c478bd9Sstevel@tonic-gate 			*mcast_rt_v4_setp = B_TRUE;
1587*7c478bd9Sstevel@tonic-gate 		} else {
1588*7c478bd9Sstevel@tonic-gate 			*mcast_rt_v6_setp = B_TRUE;
1589*7c478bd9Sstevel@tonic-gate 		}
1590*7c478bd9Sstevel@tonic-gate 		(void) close(rs);
1591*7c478bd9Sstevel@tonic-gate 	}
1592*7c478bd9Sstevel@tonic-gate 
1593*7c478bd9Sstevel@tonic-gate 	if (!got_netmask) {
1594*7c478bd9Sstevel@tonic-gate 		/*
1595*7c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
1596*7c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
1597*7c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
1598*7c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
1599*7c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
1600*7c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
1601*7c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
1602*7c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
1603*7c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
1604*7c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
1605*7c478bd9Sstevel@tonic-gate 		 */
1606*7c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
1607*7c478bd9Sstevel@tonic-gate 		void  *addr;
1608*7c478bd9Sstevel@tonic-gate 
1609*7c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
1610*7c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
1611*7c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
1612*7c478bd9Sstevel@tonic-gate 		else
1613*7c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
1614*7c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
1615*7c478bd9Sstevel@tonic-gate 
1616*7c478bd9Sstevel@tonic-gate 		/* Find out what netmask interface is going to be using */
1617*7c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
1618*7c478bd9Sstevel@tonic-gate 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL)
1619*7c478bd9Sstevel@tonic-gate 			goto bad;
1620*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1621*7c478bd9Sstevel@tonic-gate 		    "WARNING: %s: no matching subnet found in netmasks(4) for "
1622*7c478bd9Sstevel@tonic-gate 		    "%s; using default of %s.",
1623*7c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, addrstr4, buffer);
1624*7c478bd9Sstevel@tonic-gate 	}
1625*7c478bd9Sstevel@tonic-gate 
1626*7c478bd9Sstevel@tonic-gate 	(void) close(s);
1627*7c478bd9Sstevel@tonic-gate 	return (Z_OK);
1628*7c478bd9Sstevel@tonic-gate bad:
1629*7c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
1630*7c478bd9Sstevel@tonic-gate 	(void) close(s);
1631*7c478bd9Sstevel@tonic-gate 	return (-1);
1632*7c478bd9Sstevel@tonic-gate }
1633*7c478bd9Sstevel@tonic-gate 
1634*7c478bd9Sstevel@tonic-gate /*
1635*7c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
1636*7c478bd9Sstevel@tonic-gate  * An IPv4 loopback interface is set up "for free", modeling the global system.
1637*7c478bd9Sstevel@tonic-gate  * If any of the configuration interfaces were IPv6, then an IPv6 loopback
1638*7c478bd9Sstevel@tonic-gate  * address is set up as well.
1639*7c478bd9Sstevel@tonic-gate  *
1640*7c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
1641*7c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
1642*7c478bd9Sstevel@tonic-gate  */
1643*7c478bd9Sstevel@tonic-gate static int
1644*7c478bd9Sstevel@tonic-gate configure_network_interfaces(zlog_t *zlogp)
1645*7c478bd9Sstevel@tonic-gate {
1646*7c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
1647*7c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
1648*7c478bd9Sstevel@tonic-gate 	boolean_t saw_v6 = B_FALSE;
1649*7c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v4_set = B_FALSE;
1650*7c478bd9Sstevel@tonic-gate 	boolean_t mcast_rt_v6_set = B_FALSE;
1651*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
1652*7c478bd9Sstevel@tonic-gate 
1653*7c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
1654*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
1655*7c478bd9Sstevel@tonic-gate 		return (-1);
1656*7c478bd9Sstevel@tonic-gate 	}
1657*7c478bd9Sstevel@tonic-gate 
1658*7c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1659*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1660*7c478bd9Sstevel@tonic-gate 		return (-1);
1661*7c478bd9Sstevel@tonic-gate 	}
1662*7c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1663*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
1664*7c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
1665*7c478bd9Sstevel@tonic-gate 		return (-1);
1666*7c478bd9Sstevel@tonic-gate 	}
1667*7c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
1668*7c478bd9Sstevel@tonic-gate 		for (;;) {
1669*7c478bd9Sstevel@tonic-gate 			struct in6_addr in6;
1670*7c478bd9Sstevel@tonic-gate 
1671*7c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
1672*7c478bd9Sstevel@tonic-gate 				break;
1673*7c478bd9Sstevel@tonic-gate 			if (configure_one_interface(zlogp, zoneid,
1674*7c478bd9Sstevel@tonic-gate 			    &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) !=
1675*7c478bd9Sstevel@tonic-gate 			    Z_OK) {
1676*7c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
1677*7c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
1678*7c478bd9Sstevel@tonic-gate 				return (-1);
1679*7c478bd9Sstevel@tonic-gate 			}
1680*7c478bd9Sstevel@tonic-gate 			if (inet_pton(AF_INET6, nwiftab.zone_nwif_address,
1681*7c478bd9Sstevel@tonic-gate 			    &in6) == 1)
1682*7c478bd9Sstevel@tonic-gate 				saw_v6 = B_TRUE;
1683*7c478bd9Sstevel@tonic-gate 		}
1684*7c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
1685*7c478bd9Sstevel@tonic-gate 	}
1686*7c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
1687*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
1688*7c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
1689*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
1690*7c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
1691*7c478bd9Sstevel@tonic-gate 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL)
1692*7c478bd9Sstevel@tonic-gate 	    != Z_OK) {
1693*7c478bd9Sstevel@tonic-gate 		return (-1);
1694*7c478bd9Sstevel@tonic-gate 	}
1695*7c478bd9Sstevel@tonic-gate 	if (saw_v6) {
1696*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
1697*7c478bd9Sstevel@tonic-gate 		    sizeof (loopback_iftab.zone_nwif_address));
1698*7c478bd9Sstevel@tonic-gate 		if (configure_one_interface(zlogp, zoneid,
1699*7c478bd9Sstevel@tonic-gate 		    &loopback_iftab, NULL, NULL) != Z_OK) {
1700*7c478bd9Sstevel@tonic-gate 			return (-1);
1701*7c478bd9Sstevel@tonic-gate 		}
1702*7c478bd9Sstevel@tonic-gate 	}
1703*7c478bd9Sstevel@tonic-gate 	return (0);
1704*7c478bd9Sstevel@tonic-gate }
1705*7c478bd9Sstevel@tonic-gate 
1706*7c478bd9Sstevel@tonic-gate static int
1707*7c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
1708*7c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
1709*7c478bd9Sstevel@tonic-gate {
1710*7c478bd9Sstevel@tonic-gate 	int fd;
1711*7c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
1712*7c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
1713*7c478bd9Sstevel@tonic-gate 	int error;
1714*7c478bd9Sstevel@tonic-gate 
1715*7c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
1716*7c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
1717*7c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
1718*7c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
1719*7c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
1720*7c478bd9Sstevel@tonic-gate 
1721*7c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
1722*7c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
1723*7c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
1724*7c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
1727*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
1728*7c478bd9Sstevel@tonic-gate 		return (-1);
1729*7c478bd9Sstevel@tonic-gate 	}
1730*7c478bd9Sstevel@tonic-gate 
1731*7c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
1732*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
1733*7c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
1734*7c478bd9Sstevel@tonic-gate 		return (0);
1735*7c478bd9Sstevel@tonic-gate 	return (-1);
1736*7c478bd9Sstevel@tonic-gate }
1737*7c478bd9Sstevel@tonic-gate 
1738*7c478bd9Sstevel@tonic-gate static int
1739*7c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
1740*7c478bd9Sstevel@tonic-gate {
1741*7c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
1742*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
1743*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
1744*7c478bd9Sstevel@tonic-gate 	int error;
1745*7c478bd9Sstevel@tonic-gate 
1746*7c478bd9Sstevel@tonic-gate 	/*
1747*7c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
1748*7c478bd9Sstevel@tonic-gate 	 */
1749*7c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
1750*7c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
1751*7c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
1752*7c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
1753*7c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
1754*7c478bd9Sstevel@tonic-gate 
1755*7c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
1756*7c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
1757*7c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
1758*7c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
1759*7c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
1760*7c478bd9Sstevel@tonic-gate 
1761*7c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
1762*7c478bd9Sstevel@tonic-gate 		return (error);
1763*7c478bd9Sstevel@tonic-gate 
1764*7c478bd9Sstevel@tonic-gate 	/*
1765*7c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
1766*7c478bd9Sstevel@tonic-gate 	 */
1767*7c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
1768*7c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
1769*7c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
1770*7c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
1771*7c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
1772*7c478bd9Sstevel@tonic-gate 
1773*7c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
1774*7c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
1775*7c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
1776*7c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
1777*7c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
1778*7c478bd9Sstevel@tonic-gate 
1779*7c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
1780*7c478bd9Sstevel@tonic-gate 		return (error);
1781*7c478bd9Sstevel@tonic-gate 	return (0);
1782*7c478bd9Sstevel@tonic-gate }
1783*7c478bd9Sstevel@tonic-gate 
1784*7c478bd9Sstevel@tonic-gate static int
1785*7c478bd9Sstevel@tonic-gate devfsadm_call(zlog_t *zlogp, const char *arg)
1786*7c478bd9Sstevel@tonic-gate {
1787*7c478bd9Sstevel@tonic-gate 	char *argv[4];
1788*7c478bd9Sstevel@tonic-gate 	int status;
1789*7c478bd9Sstevel@tonic-gate 
1790*7c478bd9Sstevel@tonic-gate 	argv[0] = DEVFSADM;
1791*7c478bd9Sstevel@tonic-gate 	argv[1] = (char *)arg;
1792*7c478bd9Sstevel@tonic-gate 	argv[2] = zone_name;
1793*7c478bd9Sstevel@tonic-gate 	argv[3] = NULL;
1794*7c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, DEVFSADM_PATH, argv);
1795*7c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
1796*7c478bd9Sstevel@tonic-gate 		return (status);
1797*7c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "%s call (%s %s %s) unexpectedly returned %d",
1798*7c478bd9Sstevel@tonic-gate 	    DEVFSADM, DEVFSADM_PATH, arg, zone_name, status);
1799*7c478bd9Sstevel@tonic-gate 	return (-1);
1800*7c478bd9Sstevel@tonic-gate }
1801*7c478bd9Sstevel@tonic-gate 
1802*7c478bd9Sstevel@tonic-gate static int
1803*7c478bd9Sstevel@tonic-gate devfsadm_register(zlog_t *zlogp)
1804*7c478bd9Sstevel@tonic-gate {
1805*7c478bd9Sstevel@tonic-gate 	/*
1806*7c478bd9Sstevel@tonic-gate 	 * Ready the zone's devices.
1807*7c478bd9Sstevel@tonic-gate 	 */
1808*7c478bd9Sstevel@tonic-gate 	return (devfsadm_call(zlogp, "-z"));
1809*7c478bd9Sstevel@tonic-gate }
1810*7c478bd9Sstevel@tonic-gate 
1811*7c478bd9Sstevel@tonic-gate static int
1812*7c478bd9Sstevel@tonic-gate devfsadm_unregister(zlog_t *zlogp)
1813*7c478bd9Sstevel@tonic-gate {
1814*7c478bd9Sstevel@tonic-gate 	return (devfsadm_call(zlogp, "-Z"));
1815*7c478bd9Sstevel@tonic-gate }
1816*7c478bd9Sstevel@tonic-gate 
1817*7c478bd9Sstevel@tonic-gate static int
1818*7c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
1819*7c478bd9Sstevel@tonic-gate {
1820*7c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
1821*7c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
1822*7c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
1823*7c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
1824*7c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
1825*7c478bd9Sstevel@tonic-gate 	int error = -1;
1826*7c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
1827*7c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
1828*7c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
1829*7c478bd9Sstevel@tonic-gate 
1830*7c478bd9Sstevel@tonic-gate 	*bufp = NULL;
1831*7c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
1832*7c478bd9Sstevel@tonic-gate 
1833*7c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1834*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1835*7c478bd9Sstevel@tonic-gate 		return (-1);
1836*7c478bd9Sstevel@tonic-gate 	}
1837*7c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1838*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
1839*7c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
1840*7c478bd9Sstevel@tonic-gate 		return (-1);
1841*7c478bd9Sstevel@tonic-gate 	}
1842*7c478bd9Sstevel@tonic-gate 
1843*7c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
1844*7c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
1845*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
1846*7c478bd9Sstevel@tonic-gate 		goto out;
1847*7c478bd9Sstevel@tonic-gate 	}
1848*7c478bd9Sstevel@tonic-gate 
1849*7c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
1850*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
1851*7c478bd9Sstevel@tonic-gate 		goto out;
1852*7c478bd9Sstevel@tonic-gate 	}
1853*7c478bd9Sstevel@tonic-gate 
1854*7c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
1855*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
1856*7c478bd9Sstevel@tonic-gate 		goto out;
1857*7c478bd9Sstevel@tonic-gate 	}
1858*7c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
1859*7c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
1860*7c478bd9Sstevel@tonic-gate 		uint_t i, count;
1861*7c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
1862*7c478bd9Sstevel@tonic-gate 
1863*7c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
1864*7c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
1865*7c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
1866*7c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
1867*7c478bd9Sstevel@tonic-gate 			continue;
1868*7c478bd9Sstevel@tonic-gate 		}
1869*7c478bd9Sstevel@tonic-gate 		count = 0;
1870*7c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
1871*7c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
1872*7c478bd9Sstevel@tonic-gate 			count++;
1873*7c478bd9Sstevel@tonic-gate 		}
1874*7c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
1875*7c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
1876*7c478bd9Sstevel@tonic-gate 		}
1877*7c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
1878*7c478bd9Sstevel@tonic-gate 			goto out;
1879*7c478bd9Sstevel@tonic-gate 		i = 0;
1880*7c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
1881*7c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
1882*7c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
1883*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
1884*7c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
1885*7c478bd9Sstevel@tonic-gate 				goto out;
1886*7c478bd9Sstevel@tonic-gate 			}
1887*7c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
1888*7c478bd9Sstevel@tonic-gate 			    != Z_OK) {
1889*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
1890*7c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
1891*7c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
1892*7c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
1893*7c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
1894*7c478bd9Sstevel@tonic-gate 				goto out;
1895*7c478bd9Sstevel@tonic-gate 			}
1896*7c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
1897*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
1898*7c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
1899*7c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
1900*7c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
1901*7c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
1902*7c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
1903*7c478bd9Sstevel@tonic-gate 				    name);
1904*7c478bd9Sstevel@tonic-gate 				goto out;
1905*7c478bd9Sstevel@tonic-gate 			}
1906*7c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
1907*7c478bd9Sstevel@tonic-gate 				    rctlblk_get_privilege(rctlblk)) != 0) {
1908*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
1909*7c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
1910*7c478bd9Sstevel@tonic-gate 				goto out;
1911*7c478bd9Sstevel@tonic-gate 			}
1912*7c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
1913*7c478bd9Sstevel@tonic-gate 				    rctlblk_get_value(rctlblk)) != 0) {
1914*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
1915*7c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
1916*7c478bd9Sstevel@tonic-gate 				goto out;
1917*7c478bd9Sstevel@tonic-gate 			}
1918*7c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
1919*7c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
1920*7c478bd9Sstevel@tonic-gate 			    != 0) {
1921*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
1922*7c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
1923*7c478bd9Sstevel@tonic-gate 				goto out;
1924*7c478bd9Sstevel@tonic-gate 			}
1925*7c478bd9Sstevel@tonic-gate 		}
1926*7c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
1927*7c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
1928*7c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
1929*7c478bd9Sstevel@tonic-gate 		    != 0) {
1930*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
1931*7c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
1932*7c478bd9Sstevel@tonic-gate 			goto out;
1933*7c478bd9Sstevel@tonic-gate 		}
1934*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
1935*7c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
1936*7c478bd9Sstevel@tonic-gate 		free(nvlv);
1937*7c478bd9Sstevel@tonic-gate 		nvlv = NULL;
1938*7c478bd9Sstevel@tonic-gate 		rctlcount++;
1939*7c478bd9Sstevel@tonic-gate 	}
1940*7c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
1941*7c478bd9Sstevel@tonic-gate 
1942*7c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
1943*7c478bd9Sstevel@tonic-gate 		error = 0;
1944*7c478bd9Sstevel@tonic-gate 		goto out;
1945*7c478bd9Sstevel@tonic-gate 	}
1946*7c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
1947*7c478bd9Sstevel@tonic-gate 	    != 0) {
1948*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
1949*7c478bd9Sstevel@tonic-gate 		goto out;
1950*7c478bd9Sstevel@tonic-gate 	}
1951*7c478bd9Sstevel@tonic-gate 
1952*7c478bd9Sstevel@tonic-gate 	error = 0;
1953*7c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
1954*7c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
1955*7c478bd9Sstevel@tonic-gate 
1956*7c478bd9Sstevel@tonic-gate out:
1957*7c478bd9Sstevel@tonic-gate 	free(rctlblk);
1958*7c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
1959*7c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
1960*7c478bd9Sstevel@tonic-gate 		free(nvl_packed);
1961*7c478bd9Sstevel@tonic-gate 	if (nvl != NULL)
1962*7c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
1963*7c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
1964*7c478bd9Sstevel@tonic-gate 		free(nvlv);
1965*7c478bd9Sstevel@tonic-gate 	if (handle != NULL)
1966*7c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
1967*7c478bd9Sstevel@tonic-gate 	return (error);
1968*7c478bd9Sstevel@tonic-gate }
1969*7c478bd9Sstevel@tonic-gate 
1970*7c478bd9Sstevel@tonic-gate static int
1971*7c478bd9Sstevel@tonic-gate get_zone_pool(zlog_t *zlogp, char *poolbuf, size_t bufsz)
1972*7c478bd9Sstevel@tonic-gate {
1973*7c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
1974*7c478bd9Sstevel@tonic-gate 	int error;
1975*7c478bd9Sstevel@tonic-gate 
1976*7c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
1977*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1978*7c478bd9Sstevel@tonic-gate 		return (-1);
1979*7c478bd9Sstevel@tonic-gate 	}
1980*7c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1981*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
1982*7c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
1983*7c478bd9Sstevel@tonic-gate 		return (-1);
1984*7c478bd9Sstevel@tonic-gate 	}
1985*7c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolbuf, bufsz);
1986*7c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
1987*7c478bd9Sstevel@tonic-gate 	return (error);
1988*7c478bd9Sstevel@tonic-gate }
1989*7c478bd9Sstevel@tonic-gate 
1990*7c478bd9Sstevel@tonic-gate static int
1991*7c478bd9Sstevel@tonic-gate bind_to_pool(zlog_t *zlogp, zoneid_t zoneid)
1992*7c478bd9Sstevel@tonic-gate {
1993*7c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
1994*7c478bd9Sstevel@tonic-gate 	pool_t *pool;
1995*7c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
1996*7c478bd9Sstevel@tonic-gate 	int status;
1997*7c478bd9Sstevel@tonic-gate 	int error;
1998*7c478bd9Sstevel@tonic-gate 
1999*7c478bd9Sstevel@tonic-gate 	/*
2000*7c478bd9Sstevel@tonic-gate 	 * Find the pool mentioned in the zone configuration, and bind to it.
2001*7c478bd9Sstevel@tonic-gate 	 */
2002*7c478bd9Sstevel@tonic-gate 	error = get_zone_pool(zlogp, poolname, sizeof (poolname));
2003*7c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
2004*7c478bd9Sstevel@tonic-gate 		/*
2005*7c478bd9Sstevel@tonic-gate 		 * The property is not set on the zone, so the pool
2006*7c478bd9Sstevel@tonic-gate 		 * should be bound to the default pool.  But that's
2007*7c478bd9Sstevel@tonic-gate 		 * already done by the kernel, so we can just return.
2008*7c478bd9Sstevel@tonic-gate 		 */
2009*7c478bd9Sstevel@tonic-gate 		return (0);
2010*7c478bd9Sstevel@tonic-gate 	}
2011*7c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
2012*7c478bd9Sstevel@tonic-gate 		/*
2013*7c478bd9Sstevel@tonic-gate 		 * Not an error, even though it shouldn't be happening.
2014*7c478bd9Sstevel@tonic-gate 		 */
2015*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
2016*7c478bd9Sstevel@tonic-gate 		    "WARNING: unable to retrieve default pool.");
2017*7c478bd9Sstevel@tonic-gate 		return (0);
2018*7c478bd9Sstevel@tonic-gate 	}
2019*7c478bd9Sstevel@tonic-gate 	/*
2020*7c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
2021*7c478bd9Sstevel@tonic-gate 	 */
2022*7c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
2023*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: pools facility not active; "
2024*7c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'.", poolname);
2025*7c478bd9Sstevel@tonic-gate 		return (0);
2026*7c478bd9Sstevel@tonic-gate 	}
2027*7c478bd9Sstevel@tonic-gate 	/*
2028*7c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
2029*7c478bd9Sstevel@tonic-gate 	 * exist.
2030*7c478bd9Sstevel@tonic-gate 	 */
2031*7c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
2032*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "pool_conf_alloc");
2033*7c478bd9Sstevel@tonic-gate 		return (-1);
2034*7c478bd9Sstevel@tonic-gate 	}
2035*7c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
2036*7c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
2037*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "pool_conf_open");
2038*7c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
2039*7c478bd9Sstevel@tonic-gate 		return (-1);
2040*7c478bd9Sstevel@tonic-gate 	}
2041*7c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
2042*7c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
2043*7c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
2044*7c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
2045*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: pool '%s' not found; "
2046*7c478bd9Sstevel@tonic-gate 		    "using default pool.", poolname);
2047*7c478bd9Sstevel@tonic-gate 		return (0);
2048*7c478bd9Sstevel@tonic-gate 	}
2049*7c478bd9Sstevel@tonic-gate 	/*
2050*7c478bd9Sstevel@tonic-gate 	 * Bind the zone to the pool.
2051*7c478bd9Sstevel@tonic-gate 	 */
2052*7c478bd9Sstevel@tonic-gate 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
2053*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: unable to bind to pool '%s'; "
2054*7c478bd9Sstevel@tonic-gate 		    "using default pool.", poolname);
2055*7c478bd9Sstevel@tonic-gate 	}
2056*7c478bd9Sstevel@tonic-gate 	return (0);
2057*7c478bd9Sstevel@tonic-gate }
2058*7c478bd9Sstevel@tonic-gate 
2059*7c478bd9Sstevel@tonic-gate int
2060*7c478bd9Sstevel@tonic-gate prtmount(const char *fs, void *x) {
2061*7c478bd9Sstevel@tonic-gate 	zerror((zlog_t *)x, B_FALSE, "  %s", fs);
2062*7c478bd9Sstevel@tonic-gate 	return (0);
2063*7c478bd9Sstevel@tonic-gate }
2064*7c478bd9Sstevel@tonic-gate 
2065*7c478bd9Sstevel@tonic-gate int
2066*7c478bd9Sstevel@tonic-gate vplat_create(zlog_t *zlogp)
2067*7c478bd9Sstevel@tonic-gate {
2068*7c478bd9Sstevel@tonic-gate 	int rval = -1;
2069*7c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
2070*7c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
2071*7c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
2072*7c478bd9Sstevel@tonic-gate 	size_t rctlbufsz;
2073*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
2074*7c478bd9Sstevel@tonic-gate 	int xerr;
2075*7c478bd9Sstevel@tonic-gate 
2076*7c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
2077*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
2078*7c478bd9Sstevel@tonic-gate 		return (-1);
2079*7c478bd9Sstevel@tonic-gate 	}
2080*7c478bd9Sstevel@tonic-gate 
2081*7c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
2082*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
2083*7c478bd9Sstevel@tonic-gate 		return (-1);
2084*7c478bd9Sstevel@tonic-gate 	}
2085*7c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
2086*7c478bd9Sstevel@tonic-gate 	if (zonecfg_get_privset(privs) != Z_OK) {
2087*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "Failed to initialize privileges");
2088*7c478bd9Sstevel@tonic-gate 		goto error;
2089*7c478bd9Sstevel@tonic-gate 	}
2090*7c478bd9Sstevel@tonic-gate 	if (get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
2091*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
2092*7c478bd9Sstevel@tonic-gate 		goto error;
2093*7c478bd9Sstevel@tonic-gate 	}
2094*7c478bd9Sstevel@tonic-gate 
2095*7c478bd9Sstevel@tonic-gate 	xerr = 0;
2096*7c478bd9Sstevel@tonic-gate 	if ((zoneid = zone_create(zone_name, rootpath, privs, rctlbuf,
2097*7c478bd9Sstevel@tonic-gate 	    rctlbufsz, &xerr)) == -1) {
2098*7c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
2099*7c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
2100*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
2101*7c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
2102*7c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
2103*7c478bd9Sstevel@tonic-gate 			} else {
2104*7c478bd9Sstevel@tonic-gate 
2105*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
2106*7c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
2107*7c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
2108*7c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
2109*7c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
2110*7c478bd9Sstevel@tonic-gate 			}
2111*7c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
2112*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
2113*7c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
2114*7c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
2115*7c478bd9Sstevel@tonic-gate 		} else {
2116*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
2117*7c478bd9Sstevel@tonic-gate 		}
2118*7c478bd9Sstevel@tonic-gate 		goto error;
2119*7c478bd9Sstevel@tonic-gate 	}
2120*7c478bd9Sstevel@tonic-gate 	/*
2121*7c478bd9Sstevel@tonic-gate 	 * The following is a warning, not an error.
2122*7c478bd9Sstevel@tonic-gate 	 */
2123*7c478bd9Sstevel@tonic-gate 	if (bind_to_pool(zlogp, zoneid) != 0)
2124*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: unable to bind zone to "
2125*7c478bd9Sstevel@tonic-gate 		    "requested pool; using default pool.");
2126*7c478bd9Sstevel@tonic-gate 	rval = 0;
2127*7c478bd9Sstevel@tonic-gate error:
2128*7c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
2129*7c478bd9Sstevel@tonic-gate 		free(rctlbuf);
2130*7c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
2131*7c478bd9Sstevel@tonic-gate 	return (rval);
2132*7c478bd9Sstevel@tonic-gate }
2133*7c478bd9Sstevel@tonic-gate 
2134*7c478bd9Sstevel@tonic-gate int
2135*7c478bd9Sstevel@tonic-gate vplat_bringup(zlog_t *zlogp)
2136*7c478bd9Sstevel@tonic-gate {
2137*7c478bd9Sstevel@tonic-gate 	if (create_dev_files(zlogp) != 0)
2138*7c478bd9Sstevel@tonic-gate 		return (-1);
2139*7c478bd9Sstevel@tonic-gate 	if (mount_filesystems(zlogp) != 0)
2140*7c478bd9Sstevel@tonic-gate 		return (-1);
2141*7c478bd9Sstevel@tonic-gate 	if (devfsadm_register(zlogp) != 0)
2142*7c478bd9Sstevel@tonic-gate 		return (-1);
2143*7c478bd9Sstevel@tonic-gate 	if (configure_network_interfaces(zlogp) != 0)
2144*7c478bd9Sstevel@tonic-gate 		return (-1);
2145*7c478bd9Sstevel@tonic-gate 	return (0);
2146*7c478bd9Sstevel@tonic-gate }
2147*7c478bd9Sstevel@tonic-gate 
2148*7c478bd9Sstevel@tonic-gate int
2149*7c478bd9Sstevel@tonic-gate vplat_teardown(zlog_t *zlogp)
2150*7c478bd9Sstevel@tonic-gate {
2151*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
2152*7c478bd9Sstevel@tonic-gate 
2153*7c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
2154*7c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
2155*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
2156*7c478bd9Sstevel@tonic-gate 		goto error;
2157*7c478bd9Sstevel@tonic-gate 	}
2158*7c478bd9Sstevel@tonic-gate 
2159*7c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
2160*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
2161*7c478bd9Sstevel@tonic-gate 		goto error;
2162*7c478bd9Sstevel@tonic-gate 	}
2163*7c478bd9Sstevel@tonic-gate 
2164*7c478bd9Sstevel@tonic-gate 	if (devfsadm_unregister(zlogp) != 0)
2165*7c478bd9Sstevel@tonic-gate 		goto error;
2166*7c478bd9Sstevel@tonic-gate 
2167*7c478bd9Sstevel@tonic-gate 	if (unconfigure_network_interfaces(zlogp, zoneid) != 0) {
2168*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
2169*7c478bd9Sstevel@tonic-gate 		    "unable to unconfigure network interfaces in zone");
2170*7c478bd9Sstevel@tonic-gate 		goto error;
2171*7c478bd9Sstevel@tonic-gate 	}
2172*7c478bd9Sstevel@tonic-gate 
2173*7c478bd9Sstevel@tonic-gate 	if (tcp_abort_connections(zlogp, zoneid) != 0) {
2174*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
2175*7c478bd9Sstevel@tonic-gate 		goto error;
2176*7c478bd9Sstevel@tonic-gate 	}
2177*7c478bd9Sstevel@tonic-gate 
2178*7c478bd9Sstevel@tonic-gate 	if (unmount_filesystems(zlogp) != 0) {
2179*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
2180*7c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
2181*7c478bd9Sstevel@tonic-gate 		goto error;
2182*7c478bd9Sstevel@tonic-gate 	}
2183*7c478bd9Sstevel@tonic-gate 
2184*7c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
2185*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
2186*7c478bd9Sstevel@tonic-gate 		goto error;
2187*7c478bd9Sstevel@tonic-gate 	}
2188*7c478bd9Sstevel@tonic-gate 	destroy_console_slave();
2189*7c478bd9Sstevel@tonic-gate 
2190*7c478bd9Sstevel@tonic-gate 	return (0);
2191*7c478bd9Sstevel@tonic-gate 
2192*7c478bd9Sstevel@tonic-gate error:
2193*7c478bd9Sstevel@tonic-gate 	return (-1);
2194*7c478bd9Sstevel@tonic-gate }
2195