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