xref: /illumos-gate/usr/src/cmd/zoneadmd/vplat.c (revision fcfad32e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
230fbb751dSJohn Levon  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2485dff7a0SAndy Fiddaman  * Copyright 2018, Joyent Inc.
2548bbca81SDaniel Hoffman  * Copyright (c) 2015, 2016 by Delphix. All rights reserved.
26af34582fSAndy Fiddaman  * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
278af575c0SJerry Jelinek  * Copyright 2020 RackTop Systems Inc.
2885dff7a0SAndy Fiddaman  * Copyright 2023 Oxide Computer Company
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
31a3002e0aSGarrett D'Amore /*
32a3002e0aSGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
33a3002e0aSGarrett D'Amore  */
34a3002e0aSGarrett D'Amore 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
377c478bd9Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
387c478bd9Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
397c478bd9Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
407c478bd9Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
417c478bd9Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
427c478bd9Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
43108322fbScarlsonj  *
44108322fbScarlsonj  * This module also has primary responsibility for the layout of "scratch
45108322fbScarlsonj  * zones."  These are mounted, but inactive, zones that are used during
46108322fbScarlsonj  * operating system upgrade and potentially other administrative action.  The
47108322fbScarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
48108322fbScarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
49108322fbScarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
50108322fbScarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
51108322fbScarlsonj  * without relying on any binaries in the zone itself.
52108322fbScarlsonj  *
53108322fbScarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
54108322fbScarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
55108322fbScarlsonj  * writable (unshared) resources.  Shared resources, though, are always
56108322fbScarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
57108322fbScarlsonj  * currently running, then "/b" inside the zone points to the running zone's
58108322fbScarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
59108322fbScarlsonj  * process.
60108322fbScarlsonj  *
61108322fbScarlsonj  * To construct this environment, this module creates a tmpfs mount on
62108322fbScarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
63108322fbScarlsonj  * described above is constructed on the fly.  The zone is then created using
64108322fbScarlsonj  * $ZONEPATH/lu as the root.
65108322fbScarlsonj  *
66108322fbScarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
67108322fbScarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
68108322fbScarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
69108322fbScarlsonj  * is not a part of the usual halt/ready/boot state machine.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #include <sys/param.h>
737c478bd9Sstevel@tonic-gate #include <sys/mount.h>
747c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
757c478bd9Sstevel@tonic-gate #include <sys/socket.h>
767c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
777c478bd9Sstevel@tonic-gate #include <sys/types.h>
787c478bd9Sstevel@tonic-gate #include <sys/stat.h>
797c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
807c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
817c478bd9Sstevel@tonic-gate #include <sys/conf.h>
825679c89fSjv #include <sys/systeminfo.h>
83d2a70789SRichard Lowe #include <sys/secflags.h>
8485dff7a0SAndy Fiddaman #include <sys/vnic.h>
857c478bd9Sstevel@tonic-gate 
86f4b3ec61Sdh #include <libdlpi.h>
87f595a68aSyz #include <libdllink.h>
88d62bc4baSyz #include <libdlvlan.h>
8985dff7a0SAndy Fiddaman #include <libdlvnic.h>
9085dff7a0SAndy Fiddaman #include <libdlaggr.h>
91f4b3ec61Sdh 
927c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
937c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
947c478bd9Sstevel@tonic-gate #include <netinet/in.h>
957c478bd9Sstevel@tonic-gate #include <net/route.h>
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #include <stdio.h>
987c478bd9Sstevel@tonic-gate #include <errno.h>
997c478bd9Sstevel@tonic-gate #include <fcntl.h>
1007c478bd9Sstevel@tonic-gate #include <unistd.h>
1017c478bd9Sstevel@tonic-gate #include <rctl.h>
1027c478bd9Sstevel@tonic-gate #include <stdlib.h>
1037c478bd9Sstevel@tonic-gate #include <string.h>
1047c478bd9Sstevel@tonic-gate #include <strings.h>
1057c478bd9Sstevel@tonic-gate #include <wait.h>
1067c478bd9Sstevel@tonic-gate #include <limits.h>
1077c478bd9Sstevel@tonic-gate #include <libgen.h>
108fa9e4066Sahrens #include <libzfs.h>
109facf4a8dSllai #include <libdevinfo.h>
1107c478bd9Sstevel@tonic-gate #include <zone.h>
1117c478bd9Sstevel@tonic-gate #include <assert.h>
112555afedfScarlsonj #include <libcontract.h>
113555afedfScarlsonj #include <libcontract_priv.h>
114555afedfScarlsonj #include <uuid/uuid.h>
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1177c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1187c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1197c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
120fa9e4066Sahrens #include <sys/fs/zfs.h>
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #include <pool.h>
1237c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1240209230bSgjelinek #include <sys/priocntl.h>
1257c478bd9Sstevel@tonic-gate 
1269acbbeafSnn #include <libbrand.h>
1279acbbeafSnn #include <sys/brand.h>
1287c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
12939d3e169Sevanl #include <synch.h>
13022321485Svp 
1317c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
13245916cd2Sjpk #include <tsol/label.h>
13345916cd2Sjpk #include <libtsnet.h>
13445916cd2Sjpk #include <sys/priv.h>
135550b6e40SSowmini Varadhan #include <libinetutil.h>
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1387c478bd9Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1397c478bd9Sstevel@tonic-gate 
1406e1ae2a3SGary Pennington #define	RESOURCE_DEFAULT_OPTS \
1417c478bd9Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
14445916cd2Sjpk #define	MAXTNZLEN	2048
1457c478bd9Sstevel@tonic-gate 
146af34582fSAndy Fiddaman #define	ALT_MOUNT(mount_cmd)	((mount_cmd) != Z_MNT_BOOT)
1476cfd72c6Sgjelinek 
148ff19e029SMenno Lageman /* a reasonable estimate for the number of lwps per process */
149ff19e029SMenno Lageman #define	LWPS_PER_PROCESS	10
150ff19e029SMenno Lageman 
1517c478bd9Sstevel@tonic-gate /* for routing socket */
1527c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1537c478bd9Sstevel@tonic-gate 
154108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
155108322fbScarlsonj static char kernzone[ZONENAME_MAX];
156108322fbScarlsonj 
157108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
158108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
159108322fbScarlsonj 
16045916cd2Sjpk /* for Trusted Extensions */
16145916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
16245916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
16345916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
1649d48116cSdh 
16545916cd2Sjpk static m_label_t *zlabel = NULL;
16645916cd2Sjpk static m_label_t *zid_label = NULL;
16745916cd2Sjpk static priv_set_t *zprivs = NULL;
16845916cd2Sjpk 
1698cd81a20SJerry Jelinek static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn";
1708cd81a20SJerry Jelinek 
1717c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1727c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1737c478bd9Sstevel@tonic-gate 
174c5cd6260S /* from zoneadmd */
175c5cd6260S extern char query_hook[];
176c5cd6260S 
177550b6e40SSowmini Varadhan /*
178550b6e40SSowmini Varadhan  * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
179550b6e40SSowmini Varadhan  * node in a linked list that is sorted by linkid.  The list is constructed as
180550b6e40SSowmini Varadhan  * the xml configuration file is parsed, and the information
181550b6e40SSowmini Varadhan  * contained in each node is added to the kernel before the zone is
182550b6e40SSowmini Varadhan  * booted, to be retrieved and applied from within the exclusive-IP NGZ
183550b6e40SSowmini Varadhan  * on boot.
184550b6e40SSowmini Varadhan  */
185550b6e40SSowmini Varadhan typedef struct zone_addr_list {
186550b6e40SSowmini Varadhan 	struct zone_addr_list *za_next;
187550b6e40SSowmini Varadhan 	datalink_id_t za_linkid;	/* datalink_id_t of interface */
188550b6e40SSowmini Varadhan 	struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
189550b6e40SSowmini Varadhan } zone_addr_list_t;
190550b6e40SSowmini Varadhan 
191108322fbScarlsonj /*
192108322fbScarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
193108322fbScarlsonj  * data) only once every N times through the loop.
194108322fbScarlsonj  */
195108322fbScarlsonj #define	MNTTAB_HUNK	32
196108322fbScarlsonj 
197550b6e40SSowmini Varadhan /* some handy macros */
198550b6e40SSowmini Varadhan #define	SIN(s)	((struct sockaddr_in *)s)
199550b6e40SSowmini Varadhan #define	SIN6(s)	((struct sockaddr_in6 *)s)
200550b6e40SSowmini Varadhan 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Private autofs system call
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static int
autofs_cleanup(zoneid_t zoneid)2077c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
2087c478bd9Sstevel@tonic-gate {
2098af575c0SJerry Jelinek 	int r;
2108af575c0SJerry Jelinek 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
2138af575c0SJerry Jelinek 	 * Handle ENOSYS in the case that the autofs kernel module is not
2148af575c0SJerry Jelinek 	 * installed.
2157c478bd9Sstevel@tonic-gate 	 */
2168af575c0SJerry Jelinek 	r = _autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid);
2178af575c0SJerry Jelinek 	if (r != 0 && errno == ENOSYS) {
2188af575c0SJerry Jelinek 		return (0);
2198af575c0SJerry Jelinek 	}
2208af575c0SJerry Jelinek 	return (r);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
223108322fbScarlsonj static void
free_mnttable(struct mnttab * mnt_array,uint_t nelem)224108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
225108322fbScarlsonj {
226108322fbScarlsonj 	uint_t i;
227108322fbScarlsonj 
228108322fbScarlsonj 	if (mnt_array == NULL)
229108322fbScarlsonj 		return;
230108322fbScarlsonj 	for (i = 0; i < nelem; i++) {
231108322fbScarlsonj 		free(mnt_array[i].mnt_mountp);
232108322fbScarlsonj 		free(mnt_array[i].mnt_fstype);
233108322fbScarlsonj 		free(mnt_array[i].mnt_special);
234108322fbScarlsonj 		free(mnt_array[i].mnt_mntopts);
235108322fbScarlsonj 		assert(mnt_array[i].mnt_time == NULL);
236108322fbScarlsonj 	}
237108322fbScarlsonj 	free(mnt_array);
238108322fbScarlsonj }
239108322fbScarlsonj 
240108322fbScarlsonj /*
241108322fbScarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
242108322fbScarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
243108322fbScarlsonj  * array in "nelemp".
244108322fbScarlsonj  */
245108322fbScarlsonj static int
build_mnttable(zlog_t * zlogp,const char * zroot,size_t zrootlen,FILE * mnttab,struct mnttab ** mnt_arrayp,uint_t * nelemp)246108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
247108322fbScarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
248108322fbScarlsonj {
249108322fbScarlsonj 	struct mnttab mnt;
250108322fbScarlsonj 	struct mnttab *mnts;
251108322fbScarlsonj 	struct mnttab *mnp;
252108322fbScarlsonj 	uint_t nmnt;
253108322fbScarlsonj 
254108322fbScarlsonj 	rewind(mnttab);
255108322fbScarlsonj 	resetmnttab(mnttab);
256108322fbScarlsonj 	nmnt = 0;
257108322fbScarlsonj 	mnts = NULL;
258108322fbScarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
259108322fbScarlsonj 		struct mnttab *tmp_array;
260108322fbScarlsonj 
261108322fbScarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
262108322fbScarlsonj 			continue;
263108322fbScarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
264108322fbScarlsonj 			tmp_array = realloc(mnts,
265108322fbScarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
266108322fbScarlsonj 			if (tmp_array == NULL) {
267108322fbScarlsonj 				free_mnttable(mnts, nmnt);
268108322fbScarlsonj 				return (-1);
269108322fbScarlsonj 			}
270108322fbScarlsonj 			mnts = tmp_array;
271108322fbScarlsonj 		}
272108322fbScarlsonj 		mnp = &mnts[nmnt++];
273108322fbScarlsonj 
274108322fbScarlsonj 		/*
275108322fbScarlsonj 		 * Zero out any fields we're not using.
276108322fbScarlsonj 		 */
277108322fbScarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
278108322fbScarlsonj 
279108322fbScarlsonj 		if (mnt.mnt_special != NULL)
280108322fbScarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
281108322fbScarlsonj 		if (mnt.mnt_mntopts != NULL)
282108322fbScarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
283108322fbScarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
284108322fbScarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
285108322fbScarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
286108322fbScarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
287108322fbScarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
288108322fbScarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
289108322fbScarlsonj 			free_mnttable(mnts, nmnt);
290108322fbScarlsonj 			return (-1);
291108322fbScarlsonj 		}
292108322fbScarlsonj 	}
293108322fbScarlsonj 	*mnt_arrayp = mnts;
294108322fbScarlsonj 	*nelemp = nmnt;
295108322fbScarlsonj 	return (0);
296108322fbScarlsonj }
297108322fbScarlsonj 
298108322fbScarlsonj /*
299108322fbScarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
300108322fbScarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
301108322fbScarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
302108322fbScarlsonj  * reread of the list of mount points
303108322fbScarlsonj  */
304108322fbScarlsonj static void
lofs_discard_mnttab(void)305108322fbScarlsonj lofs_discard_mnttab(void)
306108322fbScarlsonj {
307108322fbScarlsonj 	free_mnttable(resolve_lofs_mnts,
308108322fbScarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
309108322fbScarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
310108322fbScarlsonj }
311108322fbScarlsonj 
312108322fbScarlsonj static int
lofs_read_mnttab(zlog_t * zlogp)313108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
314108322fbScarlsonj {
315108322fbScarlsonj 	FILE *mnttab;
316108322fbScarlsonj 	uint_t nmnts;
317108322fbScarlsonj 
318108322fbScarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
319108322fbScarlsonj 		return (-1);
320108322fbScarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
321108322fbScarlsonj 	    &nmnts) == -1) {
322108322fbScarlsonj 		(void) fclose(mnttab);
323108322fbScarlsonj 		return (-1);
324108322fbScarlsonj 	}
325108322fbScarlsonj 	(void) fclose(mnttab);
326108322fbScarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
327108322fbScarlsonj 	return (0);
328108322fbScarlsonj }
329108322fbScarlsonj 
330108322fbScarlsonj /*
331108322fbScarlsonj  * This function loops over potential loopback mounts and symlinks in a given
332108322fbScarlsonj  * path and resolves them all down to an absolute path.
333108322fbScarlsonj  */
334910f48daSedp void
resolve_lofs(zlog_t * zlogp,char * path,size_t pathlen)335108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
336108322fbScarlsonj {
337108322fbScarlsonj 	int len, arlen;
338108322fbScarlsonj 	const char *altroot;
339108322fbScarlsonj 	char tmppath[MAXPATHLEN];
340108322fbScarlsonj 	boolean_t outside_altroot;
341108322fbScarlsonj 
342108322fbScarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
343108322fbScarlsonj 		return;
344108322fbScarlsonj 	tmppath[len] = '\0';
345108322fbScarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
346108322fbScarlsonj 
347108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
348108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
349108322fbScarlsonj 		return;
350108322fbScarlsonj 
351108322fbScarlsonj 	altroot = zonecfg_get_root();
352108322fbScarlsonj 	arlen = strlen(altroot);
353108322fbScarlsonj 	outside_altroot = B_FALSE;
354108322fbScarlsonj 	for (;;) {
355108322fbScarlsonj 		struct mnttab *mnp;
356108322fbScarlsonj 
3571e9d8f9fSdminer 		/* Search in reverse order to find longest match */
3581e9d8f9fSdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3591e9d8f9fSdminer 		    mnp--) {
360108322fbScarlsonj 			if (mnp->mnt_fstype == NULL ||
361108322fbScarlsonj 			    mnp->mnt_mountp == NULL ||
3621e9d8f9fSdminer 			    mnp->mnt_special == NULL)
363108322fbScarlsonj 				continue;
364108322fbScarlsonj 			len = strlen(mnp->mnt_mountp);
365108322fbScarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
366108322fbScarlsonj 			    (path[len] == '/' || path[len] == '\0'))
367108322fbScarlsonj 				break;
368108322fbScarlsonj 		}
3691e9d8f9fSdminer 		if (mnp < resolve_lofs_mnts)
3701e9d8f9fSdminer 			break;
3711e9d8f9fSdminer 		/* If it's not a lofs then we're done */
3721e9d8f9fSdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
373108322fbScarlsonj 			break;
374108322fbScarlsonj 		if (outside_altroot) {
375108322fbScarlsonj 			char *cp;
376108322fbScarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
377108322fbScarlsonj 
378108322fbScarlsonj 			/*
379108322fbScarlsonj 			 * If we run into a read-only mount outside of the
380108322fbScarlsonj 			 * alternate root environment, then the user doesn't
381108322fbScarlsonj 			 * want this path to be made read-write.
382108322fbScarlsonj 			 */
383108322fbScarlsonj 			if (mnp->mnt_mntopts != NULL &&
384108322fbScarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
385108322fbScarlsonj 			    NULL &&
386108322fbScarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
387108322fbScarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
388108322fbScarlsonj 				break;
389108322fbScarlsonj 			}
390108322fbScarlsonj 		} else if (arlen > 0 &&
391108322fbScarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
392108322fbScarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
393108322fbScarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
394108322fbScarlsonj 			outside_altroot = B_TRUE;
395108322fbScarlsonj 		}
396108322fbScarlsonj 		/* use temporary buffer because new path might be longer */
397108322fbScarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
398108322fbScarlsonj 		    mnp->mnt_special, path + len);
399108322fbScarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
400108322fbScarlsonj 			break;
401108322fbScarlsonj 		path[len] = '\0';
402108322fbScarlsonj 	}
403108322fbScarlsonj }
404108322fbScarlsonj 
405108322fbScarlsonj /*
406108322fbScarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
407108322fbScarlsonj  * referenced device is already mounted somewhere.
408108322fbScarlsonj  */
409108322fbScarlsonj static int
check_lofs_needed(zlog_t * zlogp,struct zone_fstab * fsptr)410108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
411108322fbScarlsonj {
412108322fbScarlsonj 	struct mnttab *mnp;
413108322fbScarlsonj 	zone_fsopt_t *optptr, *onext;
414108322fbScarlsonj 
415108322fbScarlsonj 	/* This happens once per zoneadmd operation. */
416108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
417108322fbScarlsonj 		return (-1);
418108322fbScarlsonj 
419108322fbScarlsonj 	/*
420108322fbScarlsonj 	 * If this special node isn't already in use, then it's ours alone;
421108322fbScarlsonj 	 * no need to worry about conflicting mounts.
422108322fbScarlsonj 	 */
423108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
424108322fbScarlsonj 	    mnp++) {
425108322fbScarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
426108322fbScarlsonj 			break;
427108322fbScarlsonj 	}
428108322fbScarlsonj 	if (mnp >= resolve_lofs_mnt_max)
429108322fbScarlsonj 		return (0);
430108322fbScarlsonj 
431108322fbScarlsonj 	/*
432108322fbScarlsonj 	 * Convert this duplicate mount into a lofs mount.
433108322fbScarlsonj 	 */
434108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
435108322fbScarlsonj 	    sizeof (fsptr->zone_fs_special));
436108322fbScarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
437108322fbScarlsonj 	    sizeof (fsptr->zone_fs_type));
438108322fbScarlsonj 	fsptr->zone_fs_raw[0] = '\0';
439108322fbScarlsonj 
440108322fbScarlsonj 	/*
4416e1ae2a3SGary Pennington 	 * Discard all but one of the original options and set that to our
4426e1ae2a3SGary Pennington 	 * default set of options used for resources.
443108322fbScarlsonj 	 */
444108322fbScarlsonj 	optptr = fsptr->zone_fs_options;
445108322fbScarlsonj 	if (optptr == NULL) {
446108322fbScarlsonj 		optptr = malloc(sizeof (*optptr));
447108322fbScarlsonj 		if (optptr == NULL) {
448108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
449108322fbScarlsonj 			    fsptr->zone_fs_dir);
450108322fbScarlsonj 			return (-1);
451108322fbScarlsonj 		}
452108322fbScarlsonj 	} else {
453108322fbScarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
454108322fbScarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
455108322fbScarlsonj 			free(onext);
456108322fbScarlsonj 		}
457108322fbScarlsonj 	}
4586e1ae2a3SGary Pennington 	(void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
459108322fbScarlsonj 	optptr->zone_fsopt_next = NULL;
460108322fbScarlsonj 	fsptr->zone_fs_options = optptr;
461108322fbScarlsonj 	return (0);
462108322fbScarlsonj }
463108322fbScarlsonj 
464d314f035Sedp int
make_one_dir(zlog_t * zlogp,const char * prefix,const char * subdir,mode_t mode,uid_t userid,gid_t groupid)46527e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
46627e6fb21Sdp     uid_t userid, gid_t groupid)
4677c478bd9Sstevel@tonic-gate {
4687c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4697c478bd9Sstevel@tonic-gate 	struct stat st;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4727c478bd9Sstevel@tonic-gate 	    sizeof (path)) {
4737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4747c478bd9Sstevel@tonic-gate 		    subdir);
4757c478bd9Sstevel@tonic-gate 		return (-1);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4797c478bd9Sstevel@tonic-gate 		/*
4807c478bd9Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4817c478bd9Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
48248bbca81SDaniel Hoffman 		 * and we don't need to second guess them.
4837c478bd9Sstevel@tonic-gate 		 */
4847c478bd9Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
485756cf395SRic Aleshire 			if (S_ISREG(st.st_mode)) {
48645916cd2Sjpk 				/*
487756cf395SRic Aleshire 				 * Allow readonly mounts of /etc/ files; this
488756cf395SRic Aleshire 				 * is needed most by Trusted Extensions.
48945916cd2Sjpk 				 */
49045916cd2Sjpk 				if (strncmp(subdir, "/etc/",
49145916cd2Sjpk 				    strlen("/etc/")) != 0) {
49245916cd2Sjpk 					zerror(zlogp, B_FALSE,
49345916cd2Sjpk 					    "%s is not in /etc", path);
49445916cd2Sjpk 					return (-1);
49545916cd2Sjpk 				}
49645916cd2Sjpk 			} else {
49745916cd2Sjpk 				zerror(zlogp, B_FALSE,
49845916cd2Sjpk 				    "%s is not a directory", path);
49945916cd2Sjpk 				return (-1);
50045916cd2Sjpk 			}
5017c478bd9Sstevel@tonic-gate 		}
50227e6fb21Sdp 		return (0);
50327e6fb21Sdp 	}
50427e6fb21Sdp 
50527e6fb21Sdp 	if (mkdirp(path, mode) != 0) {
5067c478bd9Sstevel@tonic-gate 		if (errno == EROFS)
5077c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
5087c478bd9Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
5097c478bd9Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
5107c478bd9Sstevel@tonic-gate 		else
5117c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
5127c478bd9Sstevel@tonic-gate 		return (-1);
5137c478bd9Sstevel@tonic-gate 	}
51427e6fb21Sdp 
51527e6fb21Sdp 	(void) chown(path, userid, groupid);
5167c478bd9Sstevel@tonic-gate 	return (0);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate static void
free_remote_fstypes(char ** types)5207c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	uint_t i;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if (types == NULL)
5257c478bd9Sstevel@tonic-gate 		return;
5267c478bd9Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
5277c478bd9Sstevel@tonic-gate 		free(types[i]);
5287c478bd9Sstevel@tonic-gate 	free(types);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate static char **
get_remote_fstypes(zlog_t * zlogp)5327c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	char **types = NULL;
5357c478bd9Sstevel@tonic-gate 	FILE *fp;
5367c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
5377c478bd9Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
5387c478bd9Sstevel@tonic-gate 	uint_t lines = 0;
5397c478bd9Sstevel@tonic-gate 	uint_t i;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5437c478bd9Sstevel@tonic-gate 		return (NULL);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * Count the number of lines
5477c478bd9Sstevel@tonic-gate 	 */
5487c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5497c478bd9Sstevel@tonic-gate 		lines++;
5507c478bd9Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5517c478bd9Sstevel@tonic-gate 		goto out;
5527c478bd9Sstevel@tonic-gate 	rewind(fp);
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5557c478bd9Sstevel@tonic-gate 	 */
5567c478bd9Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5577c478bd9Sstevel@tonic-gate 	if (types == NULL) {
5587c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5597c478bd9Sstevel@tonic-gate 		goto out;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	i = 0;
5627c478bd9Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5637c478bd9Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5647c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5657c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5667c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5677c478bd9Sstevel@tonic-gate 			types = NULL;
5687c478bd9Sstevel@tonic-gate 			goto out;
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 		types[i] = strdup(fstype);
5717c478bd9Sstevel@tonic-gate 		if (types[i] == NULL) {
5727c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5737c478bd9Sstevel@tonic-gate 			free_remote_fstypes(types);
5747c478bd9Sstevel@tonic-gate 			types = NULL;
5757c478bd9Sstevel@tonic-gate 			goto out;
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		i++;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate out:
5807c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5817c478bd9Sstevel@tonic-gate 	return (types);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate static boolean_t
is_remote_fstype(const char * fstype,char * const * remote_fstypes)5857c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	uint_t i;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5907c478bd9Sstevel@tonic-gate 		return (B_FALSE);
5917c478bd9Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5927c478bd9Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5937c478bd9Sstevel@tonic-gate 			return (B_TRUE);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 	return (B_FALSE);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
599108322fbScarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
600108322fbScarlsonj  * Upgrade scratch zone root (of the form .../lu).
6017c478bd9Sstevel@tonic-gate  */
602108322fbScarlsonj static void
root_to_lu(zlog_t * zlogp,char * zroot,size_t zrootlen,boolean_t isresolved)603108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
6047c478bd9Sstevel@tonic-gate {
605108322fbScarlsonj 	if (!isresolved && zonecfg_in_alt_root())
606108322fbScarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
607108322fbScarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
6127c478bd9Sstevel@tonic-gate  *
6137c478bd9Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
6147c478bd9Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
6157c478bd9Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
6167c478bd9Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
6177c478bd9Sstevel@tonic-gate  *
6187c478bd9Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
6197c478bd9Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
6207c478bd9Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
6217c478bd9Sstevel@tonic-gate  *
6227c478bd9Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
6237c478bd9Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
6247c478bd9Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
6257c478bd9Sstevel@tonic-gate  *
6267c478bd9Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
6277c478bd9Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
6287c478bd9Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
6297c478bd9Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
6307c478bd9Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
6317c478bd9Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
6327c478bd9Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
6337c478bd9Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
6347c478bd9Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
6357c478bd9Sstevel@tonic-gate  * graceful mounts first.
6367c478bd9Sstevel@tonic-gate  *
6377c478bd9Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@tonic-gate static int
unmount_filesystems(zlog_t * zlogp,zoneid_t zoneid,boolean_t unmount_cmd)640108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate 	int error = 0;
6437c478bd9Sstevel@tonic-gate 	FILE *mnttab;
6447c478bd9Sstevel@tonic-gate 	struct mnttab *mnts;
6457c478bd9Sstevel@tonic-gate 	uint_t nmnt;
6467c478bd9Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6477c478bd9Sstevel@tonic-gate 	size_t zrootlen;
6487c478bd9Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6497c478bd9Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6507c478bd9Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6537c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6547c478bd9Sstevel@tonic-gate 		return (-1);
6557c478bd9Sstevel@tonic-gate 	}
656108322fbScarlsonj 	if (unmount_cmd)
657108322fbScarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6607c478bd9Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6617c478bd9Sstevel@tonic-gate 
66245916cd2Sjpk 	/*
66345916cd2Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
66445916cd2Sjpk 	 * of our zone's /export/home
66545916cd2Sjpk 	 */
66648451833Scarlsonj 	if (!unmount_cmd)
66748451833Scarlsonj 		tsol_unmounts(zlogp, zone_name);
66845916cd2Sjpk 
6697c478bd9Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6717c478bd9Sstevel@tonic-gate 		return (-1);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6757c478bd9Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6767c478bd9Sstevel@tonic-gate 	 */
6777c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6787c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6797c478bd9Sstevel@tonic-gate 		error++;
6807c478bd9Sstevel@tonic-gate 		goto out;
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/*
6847c478bd9Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6857c478bd9Sstevel@tonic-gate 	 * should forcibly unmount.
6867c478bd9Sstevel@tonic-gate 	 */
6877c478bd9Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
688*fcfad32eSPatrick Mooney 	for (; /* ever */; ) {
6897c478bd9Sstevel@tonic-gate 		uint_t newcount = 0;
6907c478bd9Sstevel@tonic-gate 		boolean_t unmounted;
6917c478bd9Sstevel@tonic-gate 		struct mnttab *mnp;
6927c478bd9Sstevel@tonic-gate 		char *path;
6937c478bd9Sstevel@tonic-gate 		uint_t i;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		mnts = NULL;
6967c478bd9Sstevel@tonic-gate 		nmnt = 0;
6977c478bd9Sstevel@tonic-gate 		/*
6987c478bd9Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6997c478bd9Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
7007c478bd9Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
7017c478bd9Sstevel@tonic-gate 		 * filesystems.
7027c478bd9Sstevel@tonic-gate 		 */
7037c478bd9Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
7047c478bd9Sstevel@tonic-gate 		    &nmnt) != 0) {
7057c478bd9Sstevel@tonic-gate 			error++;
7067c478bd9Sstevel@tonic-gate 			goto out;
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
7097c478bd9Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
7107c478bd9Sstevel@tonic-gate 			path = mnp->mnt_mountp;
7117c478bd9Sstevel@tonic-gate 			unmounted = B_FALSE;
7127c478bd9Sstevel@tonic-gate 			/*
7137c478bd9Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
7147c478bd9Sstevel@tonic-gate 			 *
7157c478bd9Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
7167c478bd9Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
7177c478bd9Sstevel@tonic-gate 			 * and try a regular unmount.
7187c478bd9Sstevel@tonic-gate 			 */
7197c478bd9Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
7207c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
7217c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7227c478bd9Sstevel@tonic-gate 			}
7237c478bd9Sstevel@tonic-gate 			/*
7247c478bd9Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
7257c478bd9Sstevel@tonic-gate 			 */
7267c478bd9Sstevel@tonic-gate 			if (stuck) {
7277c478bd9Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
7287c478bd9Sstevel@tonic-gate 					unmounted = B_TRUE;
7297c478bd9Sstevel@tonic-gate 					stuck = B_FALSE;
7307c478bd9Sstevel@tonic-gate 				} else {
7317c478bd9Sstevel@tonic-gate 					/*
7327c478bd9Sstevel@tonic-gate 					 * The first failure indicates a
7337c478bd9Sstevel@tonic-gate 					 * mount we won't be able to get
7347c478bd9Sstevel@tonic-gate 					 * rid of automatically, so we
7357c478bd9Sstevel@tonic-gate 					 * bail.
7367c478bd9Sstevel@tonic-gate 					 */
7377c478bd9Sstevel@tonic-gate 					error++;
7387c478bd9Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
7397c478bd9Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
7407c478bd9Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
7417c478bd9Sstevel@tonic-gate 					goto out;
7427c478bd9Sstevel@tonic-gate 				}
7437c478bd9Sstevel@tonic-gate 			}
7447c478bd9Sstevel@tonic-gate 			/*
7457c478bd9Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7467c478bd9Sstevel@tonic-gate 			 */
7477c478bd9Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7487c478bd9Sstevel@tonic-gate 				newcount++;
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		if (newcount == 0)
7537c478bd9Sstevel@tonic-gate 			break;
7547c478bd9Sstevel@tonic-gate 		if (newcount >= oldcount) {
7557c478bd9Sstevel@tonic-gate 			/*
7567c478bd9Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7577c478bd9Sstevel@tonic-gate 			 * should start trying forced unmounts.
7587c478bd9Sstevel@tonic-gate 			 */
7597c478bd9Sstevel@tonic-gate 			stuck = B_TRUE;
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 		oldcount = newcount;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		/*
7647c478bd9Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7657c478bd9Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7667c478bd9Sstevel@tonic-gate 		 */
7677c478bd9Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7687c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7697c478bd9Sstevel@tonic-gate 			error++;
7707c478bd9Sstevel@tonic-gate 			goto out;
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate out:
7757c478bd9Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7767c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
7777c478bd9Sstevel@tonic-gate 	return (error ? -1 : 0);
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate static int
fs_compare(const void * m1,const void * m2)7817c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7847c478bd9Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7917c478bd9Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7927c478bd9Sstevel@tonic-gate  * returns the exit status otherwise.
7937c478bd9Sstevel@tonic-gate  *
7947c478bd9Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7957c478bd9Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7967c478bd9Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7977c478bd9Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7987c478bd9Sstevel@tonic-gate  * remains the case.
7997c478bd9Sstevel@tonic-gate  */
8007c478bd9Sstevel@tonic-gate static int
forkexec(zlog_t * zlogp,const char * path,char * const argv[])8017c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate 	pid_t child_pid;
8047c478bd9Sstevel@tonic-gate 	int child_status = 0;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/*
8077c478bd9Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
8087c478bd9Sstevel@tonic-gate 	 */
8097c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
8107c478bd9Sstevel@tonic-gate 	child_pid = fork();
8117c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
8127c478bd9Sstevel@tonic-gate 	if (child_pid == -1) {
8137c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
8147c478bd9Sstevel@tonic-gate 		return (-1);
8157c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
8167c478bd9Sstevel@tonic-gate 		closefrom(0);
8171390a385Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
8181390a385Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
8191390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
8201390a385Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
8217c478bd9Sstevel@tonic-gate 		(void) execv(path, argv);
8227c478bd9Sstevel@tonic-gate 		/*
8237c478bd9Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
8247c478bd9Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
8257c478bd9Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
8267c478bd9Sstevel@tonic-gate 		 * accordingly.
8277c478bd9Sstevel@tonic-gate 		 */
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
8307c478bd9Sstevel@tonic-gate 	} else {
8317c478bd9Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
8357c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8367c478bd9Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
8377c478bd9Sstevel@tonic-gate 		return (-1);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
8407c478bd9Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
8417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8427c478bd9Sstevel@tonic-gate 		return (-1);
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
84793239addSjohnlev static int
isregfile(const char * path)84893239addSjohnlev isregfile(const char *path)
84993239addSjohnlev {
85093239addSjohnlev 	struct stat64 st;
85193239addSjohnlev 
85293239addSjohnlev 	if (stat64(path, &st) == -1)
85393239addSjohnlev 		return (-1);
85493239addSjohnlev 
85593239addSjohnlev 	return (S_ISREG(st.st_mode));
85693239addSjohnlev }
85793239addSjohnlev 
8587c478bd9Sstevel@tonic-gate static int
dofsck(zlog_t * zlogp,const char * fstype,const char * rawdev)8597c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
86257a250fbSbatschul 	char *argv[5];
8637c478bd9Sstevel@tonic-gate 	int status;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/*
8667c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8677c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8687c478bd9Sstevel@tonic-gate 	 */
8697c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8709acbbeafSnn 	    >= sizeof (cmdbuf)) {
8717c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8727c478bd9Sstevel@tonic-gate 		return (-1);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
87593239addSjohnlev 	/*
87693239addSjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
87793239addSjohnlev 	 * in zoneadm.
87893239addSjohnlev 	 */
87993239addSjohnlev 	if (isregfile(cmdbuf) == -1)
88093239addSjohnlev 		return (0);
88193239addSjohnlev 
8827c478bd9Sstevel@tonic-gate 	argv[0] = "fsck";
88357a250fbSbatschul 	argv[1] = "-o";
88457a250fbSbatschul 	argv[2] = "p";
88557a250fbSbatschul 	argv[3] = (char *)rawdev;
88657a250fbSbatschul 	argv[4] = NULL;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8897c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
8907c478bd9Sstevel@tonic-gate 		return (status);
8917c478bd9Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8927c478bd9Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8937c478bd9Sstevel@tonic-gate 	return (-1);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate static int
domount(zlog_t * zlogp,const char * fstype,const char * opts,const char * special,const char * directory)8977c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8987c478bd9Sstevel@tonic-gate     const char *special, const char *directory)
8997c478bd9Sstevel@tonic-gate {
9007c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
9017c478bd9Sstevel@tonic-gate 	char *argv[6];
9027c478bd9Sstevel@tonic-gate 	int status;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/*
9057c478bd9Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
9067c478bd9Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
9099acbbeafSnn 	    >= sizeof (cmdbuf)) {
9107c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
9117c478bd9Sstevel@tonic-gate 		return (-1);
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 	argv[0] = "mount";
9147c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
9157c478bd9Sstevel@tonic-gate 		argv[1] = (char *)special;
9167c478bd9Sstevel@tonic-gate 		argv[2] = (char *)directory;
9177c478bd9Sstevel@tonic-gate 		argv[3] = NULL;
9187c478bd9Sstevel@tonic-gate 	} else {
9197c478bd9Sstevel@tonic-gate 		argv[1] = "-o";
9207c478bd9Sstevel@tonic-gate 		argv[2] = (char *)opts;
9217c478bd9Sstevel@tonic-gate 		argv[3] = (char *)special;
9227c478bd9Sstevel@tonic-gate 		argv[4] = (char *)directory;
9237c478bd9Sstevel@tonic-gate 		argv[5] = NULL;
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
9277c478bd9Sstevel@tonic-gate 	if (status == 0 || status == -1)
9287c478bd9Sstevel@tonic-gate 		return (status);
9297c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0')
9307c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
9317c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9327c478bd9Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
9337c478bd9Sstevel@tonic-gate 	else
9347c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
9357c478bd9Sstevel@tonic-gate 		    "failed with exit code %d",
9367c478bd9Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
9377c478bd9Sstevel@tonic-gate 	return (-1);
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate /*
941d314f035Sedp  * Check if a given mount point path exists.
942d314f035Sedp  * If it does, make sure it doesn't contain any symlinks.
943d314f035Sedp  * Note that if "leaf" is false we're checking an intermediate
944d314f035Sedp  * component of the mount point path, so it must be a directory.
945d314f035Sedp  * If "leaf" is true, then we're checking the entire mount point
946d314f035Sedp  * path, so the mount point itself can be anything aside from a
947d314f035Sedp  * symbolic link.
948d314f035Sedp  *
949d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
950d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
951d314f035Sedp  * If the path doesn't exist return a positive value.
9527c478bd9Sstevel@tonic-gate  */
9537c478bd9Sstevel@tonic-gate static int
valid_mount_point(zlog_t * zlogp,const char * path,const boolean_t leaf)954d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	struct stat statbuf;
9577c478bd9Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9587c478bd9Sstevel@tonic-gate 	int res;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9617c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
962d314f035Sedp 			return (1);
9637c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9647c478bd9Sstevel@tonic-gate 		return (-1);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9677c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9687c478bd9Sstevel@tonic-gate 		return (-1);
9697c478bd9Sstevel@tonic-gate 	}
970d314f035Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
971d314f035Sedp 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
972d314f035Sedp 		return (-1);
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9757c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9767c478bd9Sstevel@tonic-gate 		return (-1);
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 	respath[res] = '\0';
9797c478bd9Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9807c478bd9Sstevel@tonic-gate 		/*
981d314f035Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9827c478bd9Sstevel@tonic-gate 		 */
9837c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9847c478bd9Sstevel@tonic-gate 		return (-1);
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 	return (0);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate /*
990d314f035Sedp  * Validate a mount point path.  A valid mount point path is an
991d314f035Sedp  * absolute path that either doesn't exist, or, if it does exists it
992d314f035Sedp  * must be an absolute canonical path that doesn't have any symbolic
993d314f035Sedp  * links in it.  The target of a mount point path can be any filesystem
994d314f035Sedp  * object.  (Different filesystems can support different mount points,
995d314f035Sedp  * for example "lofs" and "mntfs" both support files and directories
996d314f035Sedp  * while "ufs" just supports directories.)
9977c478bd9Sstevel@tonic-gate  *
998d314f035Sedp  * If the path is invalid then a negative value is returned.  If the
999d314f035Sedp  * path exists and is a valid mount point path then 0 is returned.
1000d314f035Sedp  * If the path doesn't exist return a positive value.
10017c478bd9Sstevel@tonic-gate  */
1002d314f035Sedp int
valid_mount_path(zlog_t * zlogp,const char * rootpath,const char * spec,const char * dir,const char * fstype)1003d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
1004d314f035Sedp     const char *dir, const char *fstype)
10057c478bd9Sstevel@tonic-gate {
1006d314f035Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
1007d314f035Sedp 	int rv;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	/*
1010d314f035Sedp 	 * Sanity check the target mount point path.
1011d314f035Sedp 	 * It must be a non-null string that starts with a '/'.
10127c478bd9Sstevel@tonic-gate 	 */
1013d314f035Sedp 	if (dir[0] != '/') {
10146e1ae2a3SGary Pennington 		/* Something went wrong. */
10156e1ae2a3SGary Pennington 		zerror(zlogp, B_FALSE, "invalid mount directory, "
10166e1ae2a3SGary Pennington 		    "type: \"%s\", special: \"%s\", dir: \"%s\"",
10176e1ae2a3SGary Pennington 		    fstype, spec, dir);
1018d314f035Sedp 		return (-1);
1019d314f035Sedp 	}
1020d314f035Sedp 
1021d314f035Sedp 	/*
1022d314f035Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
1023d314f035Sedp 	 * is added to all paths (even non-directory paths) to allow us
1024d314f035Sedp 	 * to detect the end of paths below.  If the path already ends
1025d314f035Sedp 	 * in a '/', then that's ok too (although we'll fail the
1026d314f035Sedp 	 * cannonical path check in valid_mount_point()).
1027d314f035Sedp 	 */
1028d314f035Sedp 	if (snprintf(abspath, sizeof (abspath),
1029d314f035Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
1030d314f035Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
1031d314f035Sedp 		    rootpath, dir);
1032d314f035Sedp 		return (-1);
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 
1035d314f035Sedp 	/*
1036d314f035Sedp 	 * Starting with rootpath, verify the mount path one component
1037d314f035Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
1038d314f035Sedp 	 */
10397c478bd9Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10407c478bd9Sstevel@tonic-gate 	assert(*slashp == '/');
10417c478bd9Sstevel@tonic-gate 	do {
1042d314f035Sedp 		slashp_next = strchr(slashp + 1, '/');
10437c478bd9Sstevel@tonic-gate 		*slashp = '\0';
1044d314f035Sedp 		if (slashp_next != NULL) {
1045d314f035Sedp 			/* This is an intermediary mount path component. */
1046d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
1047d314f035Sedp 		} else {
1048d314f035Sedp 			/* This is the last component of the mount path. */
1049d314f035Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
1050d314f035Sedp 		}
1051d314f035Sedp 		if (rv < 0)
1052d314f035Sedp 			return (rv);
10537c478bd9Sstevel@tonic-gate 		*slashp = '/';
1054d314f035Sedp 	} while ((slashp = slashp_next) != NULL);
1055d314f035Sedp 	return (rv);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10589acbbeafSnn static int
mount_one_dev_device_cb(void * arg,const char * match,const char * name)10599acbbeafSnn mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10609acbbeafSnn {
10619acbbeafSnn 	di_prof_t prof = arg;
10629acbbeafSnn 
10639acbbeafSnn 	if (name == NULL)
10649acbbeafSnn 		return (di_prof_add_dev(prof, match));
10659acbbeafSnn 	return (di_prof_add_map(prof, match, name));
10669acbbeafSnn }
10679acbbeafSnn 
10689acbbeafSnn static int
mount_one_dev_symlink_cb(void * arg,const char * source,const char * target)10699acbbeafSnn mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10709acbbeafSnn {
10719acbbeafSnn 	di_prof_t prof = arg;
10729acbbeafSnn 
10739acbbeafSnn 	return (di_prof_add_symlink(prof, source, target));
10749acbbeafSnn }
10759acbbeafSnn 
10762b24ab6bSSebastien Roy int
vplat_get_iptype(zlog_t * zlogp,zone_iptype_t * iptypep)10772b24ab6bSSebastien Roy vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1078f4b3ec61Sdh {
1079f4b3ec61Sdh 	zone_dochandle_t handle;
1080f4b3ec61Sdh 
1081f4b3ec61Sdh 	if ((handle = zonecfg_init_handle()) == NULL) {
1082f4b3ec61Sdh 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
1083f4b3ec61Sdh 		return (-1);
1084f4b3ec61Sdh 	}
1085f4b3ec61Sdh 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1086f4b3ec61Sdh 		zerror(zlogp, B_FALSE, "invalid configuration");
1087f4b3ec61Sdh 		zonecfg_fini_handle(handle);
1088f4b3ec61Sdh 		return (-1);
1089f4b3ec61Sdh 	}
1090f4b3ec61Sdh 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1091f4b3ec61Sdh 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1092f4b3ec61Sdh 		zonecfg_fini_handle(handle);
1093f4b3ec61Sdh 		return (-1);
1094f4b3ec61Sdh 	}
1095f4b3ec61Sdh 	zonecfg_fini_handle(handle);
1096f4b3ec61Sdh 	return (0);
1097f4b3ec61Sdh }
1098f4b3ec61Sdh 
10999acbbeafSnn /*
11009acbbeafSnn  * Apply the standard lists of devices/symlinks/mappings and the user-specified
11019acbbeafSnn  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
11029acbbeafSnn  * use these as a profile/filter to determine what exists in /dev.
11039acbbeafSnn  */
11049acbbeafSnn static int
mount_one_dev(zlog_t * zlogp,char * devpath,zone_mnt_t mount_cmd)11050679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
11069acbbeafSnn {
11079acbbeafSnn 	char			brand[MAXNAMELEN];
11089acbbeafSnn 	zone_dochandle_t	handle = NULL;
1109123807fbSedp 	brand_handle_t		bh = NULL;
11109acbbeafSnn 	struct zone_devtab	ztab;
11119acbbeafSnn 	di_prof_t		prof = NULL;
11129acbbeafSnn 	int			err;
11139acbbeafSnn 	int			retval = -1;
1114f4b3ec61Sdh 	zone_iptype_t		iptype;
1115af34582fSAndy Fiddaman 	const char		*curr_iptype;
11169acbbeafSnn 
11179acbbeafSnn 	if (di_prof_init(devpath, &prof)) {
11189acbbeafSnn 		zerror(zlogp, B_TRUE, "failed to initialize profile");
11199acbbeafSnn 		goto cleanup;
11209acbbeafSnn 	}
11219acbbeafSnn 
11220679f794S 	/*
11230679f794S 	 * Get a handle to the brand info for this zone.
1124e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
11250679f794S 	 * brand device mounts.
11260679f794S 	 */
11270679f794S 	if (ALT_MOUNT(mount_cmd)) {
1128e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
11290679f794S 	} else {
113062868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
11310679f794S 	}
11320679f794S 
11330679f794S 	if ((bh = brand_open(brand)) == NULL) {
11349acbbeafSnn 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
11359acbbeafSnn 		goto cleanup;
11369acbbeafSnn 	}
11379acbbeafSnn 
11382b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
1139f4b3ec61Sdh 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
1140f4b3ec61Sdh 		goto cleanup;
1141f4b3ec61Sdh 	}
1142f4b3ec61Sdh 	switch (iptype) {
1143f4b3ec61Sdh 	case ZS_SHARED:
1144f4b3ec61Sdh 		curr_iptype = "shared";
1145f4b3ec61Sdh 		break;
1146f4b3ec61Sdh 	case ZS_EXCLUSIVE:
1147f4b3ec61Sdh 		curr_iptype = "exclusive";
1148f4b3ec61Sdh 		break;
11492a22bccaSToomas Soome 	default:
11502a22bccaSToomas Soome 		zerror(zlogp, B_FALSE, "bad ip-type");
11512a22bccaSToomas Soome 		goto cleanup;
1152f4b3ec61Sdh 	}
1153f4b3ec61Sdh 
1154123807fbSedp 	if (brand_platform_iter_devices(bh, zone_name,
1155f4b3ec61Sdh 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11569acbbeafSnn 		zerror(zlogp, B_TRUE, "failed to add standard device");
11579acbbeafSnn 		goto cleanup;
11589acbbeafSnn 	}
11599acbbeafSnn 
1160123807fbSedp 	if (brand_platform_iter_link(bh,
11619acbbeafSnn 	    mount_one_dev_symlink_cb, prof) != 0) {
11629acbbeafSnn 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11639acbbeafSnn 		goto cleanup;
11649acbbeafSnn 	}
11659acbbeafSnn 
11669acbbeafSnn 	/* Add user-specified devices and directories */
11679acbbeafSnn 	if ((handle = zonecfg_init_handle()) == NULL) {
11689acbbeafSnn 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11699acbbeafSnn 		goto cleanup;
11709acbbeafSnn 	}
11712a22bccaSToomas Soome 	if ((err = zonecfg_get_handle(zone_name, handle)) != 0) {
11729acbbeafSnn 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11739acbbeafSnn 		    "%s: %s", zone_name, zonecfg_strerror(err));
11749acbbeafSnn 		goto cleanup;
11759acbbeafSnn 	}
11762a22bccaSToomas Soome 	if ((err = zonecfg_setdevent(handle)) != 0) {
11779acbbeafSnn 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11789acbbeafSnn 		    zonecfg_strerror(err));
11799acbbeafSnn 		goto cleanup;
11809acbbeafSnn 	}
11819acbbeafSnn 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11829acbbeafSnn 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11839acbbeafSnn 			zerror(zlogp, B_TRUE, "failed to add "
11849acbbeafSnn 			    "user-specified device");
11859acbbeafSnn 			goto cleanup;
11869acbbeafSnn 		}
11879acbbeafSnn 	}
11889acbbeafSnn 	(void) zonecfg_enddevent(handle);
11899acbbeafSnn 
11909acbbeafSnn 	/* Send profile to kernel */
11919acbbeafSnn 	if (di_prof_commit(prof)) {
11929acbbeafSnn 		zerror(zlogp, B_TRUE, "failed to commit profile");
11939acbbeafSnn 		goto cleanup;
11949acbbeafSnn 	}
11959acbbeafSnn 
11969acbbeafSnn 	retval = 0;
11979acbbeafSnn 
11989acbbeafSnn cleanup:
1199123807fbSedp 	if (bh != NULL)
1200123807fbSedp 		brand_close(bh);
1201e767a340Sgjelinek 	if (handle != NULL)
12029acbbeafSnn 		zonecfg_fini_handle(handle);
12039acbbeafSnn 	if (prof)
12049acbbeafSnn 		di_prof_fini(prof);
12059acbbeafSnn 	return (retval);
12069acbbeafSnn }
12079acbbeafSnn 
12087c478bd9Sstevel@tonic-gate static int
mount_one(zlog_t * zlogp,struct zone_fstab * fsptr,const char * rootpath,zone_mnt_t mount_cmd)12090679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
12100679f794S     zone_mnt_t mount_cmd)
12117c478bd9Sstevel@tonic-gate {
12129acbbeafSnn 	char path[MAXPATHLEN];
12139acbbeafSnn 	char optstr[MAX_MNTOPT_STR];
12147c478bd9Sstevel@tonic-gate 	zone_fsopt_t *optptr;
12159acbbeafSnn 	int rv;
12167c478bd9Sstevel@tonic-gate 
1217d314f035Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1218d314f035Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
12197c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
12207c478bd9Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
12217c478bd9Sstevel@tonic-gate 		return (-1);
1222d314f035Sedp 	} else if (rv > 0) {
1223d314f035Sedp 		/* The mount point path doesn't exist, create it now. */
1224d314f035Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1225d314f035Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1226d314f035Sedp 		    DEFAULT_DIR_GROUP) != 0) {
1227d314f035Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
1228d314f035Sedp 			return (-1);
1229d314f035Sedp 		}
12307c478bd9Sstevel@tonic-gate 
1231d314f035Sedp 		/*
1232d314f035Sedp 		 * Now this might seem weird, but we need to invoke
1233d314f035Sedp 		 * valid_mount_path() again.  Why?  Because it checks
1234d314f035Sedp 		 * to make sure that the mount point path is canonical,
1235d314f035Sedp 		 * which it can only do if the path exists, so now that
1236d314f035Sedp 		 * we've created the path we have to verify it again.
1237d314f035Sedp 		 */
1238d314f035Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
1239d314f035Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
1240d314f035Sedp 		    fsptr->zone_fs_type)) < 0) {
1241d314f035Sedp 			zerror(zlogp, B_FALSE,
1242d314f035Sedp 			    "%s%s is not a valid mount point",
1243d314f035Sedp 			    rootpath, fsptr->zone_fs_dir);
1244d314f035Sedp 			return (-1);
1245d314f035Sedp 		}
1246d314f035Sedp 	}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12497c478bd9Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	/*
12527c478bd9Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12537c478bd9Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
1254bbf21555SRichard Lowe 	 * administrator initiated the operation via zoneadm(8), they'll get
125548bbca81SDaniel Hoffman 	 * auto-verification which will let them know what's wrong.  If they
125648bbca81SDaniel Hoffman 	 * modify the zone configuration of a running zone, and don't attempt
125748bbca81SDaniel Hoffman 	 * to verify that it's OK, then we won't crash but won't bother trying
125848bbca81SDaniel Hoffman 	 * to be too helpful either. zoneadm verify is only a couple keystrokes
125948bbca81SDaniel Hoffman 	 * away.
12607c478bd9Sstevel@tonic-gate 	 */
12617c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12627c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12637c478bd9Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12647c478bd9Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12657c478bd9Sstevel@tonic-gate 		return (-1);
12667c478bd9Sstevel@tonic-gate 	}
12677c478bd9Sstevel@tonic-gate 
1268108322fbScarlsonj 	/*
1269108322fbScarlsonj 	 * If we're looking at an alternate root environment, then construct
1270fa3d7ae1Sedp 	 * read-only loopback mounts as necessary.  Note that any special
1271fa3d7ae1Sedp 	 * paths for lofs zone mounts in an alternate root must have
1272fa3d7ae1Sedp 	 * already been pre-pended with any alternate root path by the
1273fa3d7ae1Sedp 	 * time we get here.
1274108322fbScarlsonj 	 */
1275108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1276108322fbScarlsonj 		struct stat64 st;
1277108322fbScarlsonj 
1278108322fbScarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
127968eeb376Scarlsonj 		    S_ISBLK(st.st_mode)) {
1280fa3d7ae1Sedp 			/*
1281fa3d7ae1Sedp 			 * If we're going to mount a block device we need
1282fa3d7ae1Sedp 			 * to check if that device is already mounted
1283fa3d7ae1Sedp 			 * somewhere else, and if so, do a lofs mount
1284fa3d7ae1Sedp 			 * of the device instead of a direct mount
1285fa3d7ae1Sedp 			 */
128668eeb376Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
128768eeb376Scarlsonj 				return (-1);
128868eeb376Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1289fa3d7ae1Sedp 			/*
1290fa3d7ae1Sedp 			 * For lofs mounts, the special node is inside the
1291fa3d7ae1Sedp 			 * alternate root.  We need lofs resolution for
1292fa3d7ae1Sedp 			 * this case in order to get at the underlying
1293fa3d7ae1Sedp 			 * read-write path.
1294fa3d7ae1Sedp 			 */
1295fa3d7ae1Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1296108322fbScarlsonj 			    sizeof (fsptr->zone_fs_special));
1297108322fbScarlsonj 		}
1298108322fbScarlsonj 	}
1299108322fbScarlsonj 
13007c478bd9Sstevel@tonic-gate 	/*
13017c478bd9Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
13027c478bd9Sstevel@tonic-gate 	 */
13037c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
130493239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
130593239addSjohnlev 		return (-1);
130693239addSjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
130793239addSjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
13087c478bd9Sstevel@tonic-gate 		return (-1);
130993239addSjohnlev 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	/*
13127c478bd9Sstevel@tonic-gate 	 * Build up mount option string.
13137c478bd9Sstevel@tonic-gate 	 */
13147c478bd9Sstevel@tonic-gate 	optstr[0] = '\0';
13157c478bd9Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
13167c478bd9Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
13177c478bd9Sstevel@tonic-gate 		    sizeof (optstr));
13187c478bd9Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
13197c478bd9Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
13207c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
13217c478bd9Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
13227c478bd9Sstevel@tonic-gate 			    sizeof (optstr));
13237c478bd9Sstevel@tonic-gate 		}
13247c478bd9Sstevel@tonic-gate 	}
13259acbbeafSnn 
13269acbbeafSnn 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13279acbbeafSnn 	    fsptr->zone_fs_special, path)) != 0)
13289acbbeafSnn 		return (rv);
13299acbbeafSnn 
13309acbbeafSnn 	/*
13319acbbeafSnn 	 * The mount succeeded.  If this was not a mount of /dev then
13329acbbeafSnn 	 * we're done.
13339acbbeafSnn 	 */
13349acbbeafSnn 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13359acbbeafSnn 		return (0);
13369acbbeafSnn 
13379acbbeafSnn 	/*
13389acbbeafSnn 	 * We just mounted an instance of a /dev filesystem, so now we
13399acbbeafSnn 	 * need to configure it.
13409acbbeafSnn 	 */
13410679f794S 	return (mount_one_dev(zlogp, path, mount_cmd));
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate static void
free_fs_data(struct zone_fstab * fsarray,uint_t nelem)13457c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13467c478bd9Sstevel@tonic-gate {
13477c478bd9Sstevel@tonic-gate 	uint_t i;
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	if (fsarray == NULL)
13507c478bd9Sstevel@tonic-gate 		return;
13517c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13527c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13537c478bd9Sstevel@tonic-gate 	free(fsarray);
13547c478bd9Sstevel@tonic-gate }
13557c478bd9Sstevel@tonic-gate 
1356108322fbScarlsonj /*
1357f4368d3dSvp  * This function initiates the creation of a small Solaris Environment for
1358f4368d3dSvp  * scratch zone. The Environment creation process is split up into two
1359f4368d3dSvp  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1360f4368d3dSvp  * is done this way because:
1361af34582fSAndy Fiddaman  *	We need to have both /etc and /var in the root of the scratchzone.
1362af34582fSAndy Fiddaman  *	We loopback mount zone's own /etc and /var into the root of the
1363af34582fSAndy Fiddaman  *	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1364af34582fSAndy Fiddaman  *	need to delay the mount of /var till the zone's root gets populated.
1365f4368d3dSvp  *	So mounting of localdirs[](/etc and /var) have been moved to the
1366af34582fSAndy Fiddaman  *	build_mounted_post_var() which gets called only after the zone
1367af34582fSAndy Fiddaman  *	specific filesystems are mounted.
13686cfd72c6Sgjelinek  *
13696cfd72c6Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13706cfd72c6Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13716cfd72c6Sgjelinek  * scratch zone.
1372108322fbScarlsonj  */
1373108322fbScarlsonj static boolean_t
build_mounted_pre_var(zlog_t * zlogp,char * rootpath,size_t rootlen,const char * zonepath,char * luroot,size_t lurootlen)1374f4368d3dSvp build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
137516bca938Svp     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1376108322fbScarlsonj {
1377108322fbScarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1378108322fbScarlsonj 	const char **cpp;
1379108322fbScarlsonj 	static const char *mkdirs[] = {
13803f604e0fSdp 		"/system", "/system/contract", "/system/object", "/proc",
13813f604e0fSdp 		"/dev", "/tmp", "/a", NULL
1382108322fbScarlsonj 	};
1383108322fbScarlsonj 	char *altstr;
1384f4368d3dSvp 	FILE *fp;
1385108322fbScarlsonj 	uuid_t uuid;
1386108322fbScarlsonj 
1387108322fbScarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
138816bca938Svp 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
138916bca938Svp 	resolve_lofs(zlogp, luroot, lurootlen);
1390108322fbScarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1391108322fbScarlsonj 	(void) symlink("./usr/bin", tmp);
1392108322fbScarlsonj 
1393108322fbScarlsonj 	/*
1394108322fbScarlsonj 	 * These are mostly special mount points; not handled here.  (See
1395108322fbScarlsonj 	 * zone_mount_early.)
1396108322fbScarlsonj 	 */
1397108322fbScarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1398108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1399108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1400108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1401108322fbScarlsonj 			return (B_FALSE);
1402108322fbScarlsonj 		}
1403108322fbScarlsonj 	}
1404f4368d3dSvp 	/*
1405f4368d3dSvp 	 * This is here to support lucopy.  If there's an instance of this same
1406f4368d3dSvp 	 * zone on the current running system, then we mount its root up as
1407f4368d3dSvp 	 * read-only inside the scratch zone.
1408f4368d3dSvp 	 */
1409f4368d3dSvp 	(void) zonecfg_get_uuid(zone_name, uuid);
1410f4368d3dSvp 	altstr = strdup(zonecfg_get_root());
1411f4368d3dSvp 	if (altstr == NULL) {
1412f4368d3dSvp 		zerror(zlogp, B_TRUE, "memory allocation failed");
1413f4368d3dSvp 		return (B_FALSE);
1414f4368d3dSvp 	}
1415f4368d3dSvp 	zonecfg_set_root("");
1416f4368d3dSvp 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
1417f4368d3dSvp 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1418f4368d3dSvp 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1419f4368d3dSvp 	    strcmp(fromdir, rootpath) != 0) {
1420f4368d3dSvp 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1421f4368d3dSvp 		if (mkdir(tmp, 0755) != 0) {
1422f4368d3dSvp 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1423f4368d3dSvp 			return (B_FALSE);
1424f4368d3dSvp 		}
14256e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1426f4368d3dSvp 		    tmp) != 0) {
1427f4368d3dSvp 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1428f4368d3dSvp 			    fromdir);
1429f4368d3dSvp 			return (B_FALSE);
1430f4368d3dSvp 		}
1431f4368d3dSvp 	}
1432f4368d3dSvp 	zonecfg_set_root(altstr);
1433f4368d3dSvp 	free(altstr);
1434f4368d3dSvp 
1435f4368d3dSvp 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1436f4368d3dSvp 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1437f4368d3dSvp 		return (B_FALSE);
1438f4368d3dSvp 	}
1439f4368d3dSvp 	(void) ftruncate(fileno(fp), 0);
1440f4368d3dSvp 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1441f4368d3dSvp 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1442f4368d3dSvp 	}
1443f4368d3dSvp 	zonecfg_close_scratch(fp);
1444f4368d3dSvp 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1445f4368d3dSvp 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1446f4368d3dSvp 		return (B_FALSE);
1447f4368d3dSvp 	(void) strlcpy(rootpath, tmp, rootlen);
1448f4368d3dSvp 	return (B_TRUE);
1449f4368d3dSvp }
1450f4368d3dSvp 
1451f4368d3dSvp 
1452f4368d3dSvp static boolean_t
build_mounted_post_var(zlog_t * zlogp,zone_mnt_t mount_cmd,char * rootpath,const char * luroot)14536cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14546cfd72c6Sgjelinek     const char *luroot)
1455f4368d3dSvp {
1456f4368d3dSvp 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1457f4368d3dSvp 	const char **cpp;
14586cfd72c6Sgjelinek 	const char **loopdirs;
14596cfd72c6Sgjelinek 	const char **tmpdirs;
1460f4368d3dSvp 	static const char *localdirs[] = {
1461f4368d3dSvp 		"/etc", "/var", NULL
1462f4368d3dSvp 	};
14636cfd72c6Sgjelinek 	static const char *scr_loopdirs[] = {
1464f4368d3dSvp 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1465f4368d3dSvp 		"/usr", NULL
1466f4368d3dSvp 	};
14676cfd72c6Sgjelinek 	static const char *upd_loopdirs[] = {
14686cfd72c6Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14696cfd72c6Sgjelinek 		"/usr", "/var", NULL
14706cfd72c6Sgjelinek 	};
14716cfd72c6Sgjelinek 	static const char *scr_tmpdirs[] = {
1472f4368d3dSvp 		"/tmp", "/var/run", NULL
1473f4368d3dSvp 	};
14746cfd72c6Sgjelinek 	static const char *upd_tmpdirs[] = {
14756cfd72c6Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14766cfd72c6Sgjelinek 	};
1477f4368d3dSvp 	struct stat st;
1478f4368d3dSvp 
14796cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
14806cfd72c6Sgjelinek 		/*
14816cfd72c6Sgjelinek 		 * These are mounted read-write from the zone undergoing
14826cfd72c6Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14836cfd72c6Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
14846cfd72c6Sgjelinek 		 */
14856cfd72c6Sgjelinek 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14866cfd72c6Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1487108322fbScarlsonj 			    *cpp);
14886cfd72c6Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14896cfd72c6Sgjelinek 			    rootpath, *cpp);
14906cfd72c6Sgjelinek 			if (mkdir(tmp, 0755) != 0) {
14916cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
14926cfd72c6Sgjelinek 				return (B_FALSE);
14936cfd72c6Sgjelinek 			}
14946cfd72c6Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14956cfd72c6Sgjelinek 			    != 0) {
14966cfd72c6Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14976cfd72c6Sgjelinek 				    tmp, *cpp);
14986cfd72c6Sgjelinek 				return (B_FALSE);
14996cfd72c6Sgjelinek 			}
1500108322fbScarlsonj 		}
1501108322fbScarlsonj 	}
1502108322fbScarlsonj 
15036cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15046cfd72c6Sgjelinek 		loopdirs = upd_loopdirs;
15056cfd72c6Sgjelinek 	else
15066cfd72c6Sgjelinek 		loopdirs = scr_loopdirs;
15076cfd72c6Sgjelinek 
1508108322fbScarlsonj 	/*
1509108322fbScarlsonj 	 * These are things mounted read-only from the running system because
1510108322fbScarlsonj 	 * they contain binaries that must match system.
1511108322fbScarlsonj 	 */
1512108322fbScarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1513108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1514108322fbScarlsonj 		if (mkdir(tmp, 0755) != 0) {
1515108322fbScarlsonj 			if (errno != EEXIST) {
1516108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1517108322fbScarlsonj 				return (B_FALSE);
1518108322fbScarlsonj 			}
1519108322fbScarlsonj 			if (lstat(tmp, &st) != 0) {
1520108322fbScarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1521108322fbScarlsonj 				return (B_FALSE);
1522108322fbScarlsonj 			}
1523108322fbScarlsonj 			/*
1524108322fbScarlsonj 			 * Ignore any non-directories encountered.  These are
1525108322fbScarlsonj 			 * things that have been converted into symlinks
1526108322fbScarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1527108322fbScarlsonj 			 * fixup.
1528108322fbScarlsonj 			 */
1529108322fbScarlsonj 			if (!S_ISDIR(st.st_mode))
1530108322fbScarlsonj 				continue;
1531108322fbScarlsonj 		}
15326e1ae2a3SGary Pennington 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1533108322fbScarlsonj 		    tmp) != 0) {
1534108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1535108322fbScarlsonj 			    *cpp);
1536108322fbScarlsonj 			return (B_FALSE);
1537108322fbScarlsonj 		}
1538108322fbScarlsonj 	}
1539108322fbScarlsonj 
15406cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15416cfd72c6Sgjelinek 		tmpdirs = upd_tmpdirs;
15426cfd72c6Sgjelinek 	else
15436cfd72c6Sgjelinek 		tmpdirs = scr_tmpdirs;
15446cfd72c6Sgjelinek 
1545108322fbScarlsonj 	/*
1546108322fbScarlsonj 	 * These are things with tmpfs mounted inside.
1547108322fbScarlsonj 	 */
1548108322fbScarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1549108322fbScarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15506cfd72c6Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15516cfd72c6Sgjelinek 		    errno != EEXIST) {
1552108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1553108322fbScarlsonj 			return (B_FALSE);
1554108322fbScarlsonj 		}
1555d30e996aSgjelinek 
1556d30e996aSgjelinek 		/*
1557d30e996aSgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
1558d30e996aSgjelinek 		 * since that can be modified by the umask we will just set
1559d30e996aSgjelinek 		 * the correct mode for /tmp now.
1560d30e996aSgjelinek 		 */
1561d30e996aSgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1562d30e996aSgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1563d30e996aSgjelinek 			return (B_FALSE);
1564d30e996aSgjelinek 		}
1565d30e996aSgjelinek 
1566108322fbScarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1567108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1568108322fbScarlsonj 			return (B_FALSE);
1569108322fbScarlsonj 		}
1570108322fbScarlsonj 	}
1571108322fbScarlsonj 	return (B_TRUE);
1572108322fbScarlsonj }
1573108322fbScarlsonj 
15749acbbeafSnn typedef struct plat_gmount_cb_data {
15759acbbeafSnn 	zlog_t			*pgcd_zlogp;
15769acbbeafSnn 	struct zone_fstab	**pgcd_fs_tab;
15779acbbeafSnn 	int			*pgcd_num_fs;
15789acbbeafSnn } plat_gmount_cb_data_t;
15797c478bd9Sstevel@tonic-gate 
15809acbbeafSnn /*
15819acbbeafSnn  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15829acbbeafSnn  * through all global brand platform mounts.
15839acbbeafSnn  */
15849acbbeafSnn int
plat_gmount_cb(void * data,const char * spec,const char * dir,const char * fstype,const char * opt)15859acbbeafSnn plat_gmount_cb(void *data, const char *spec, const char *dir,
15869acbbeafSnn     const char *fstype, const char *opt)
15879acbbeafSnn {
15889acbbeafSnn 	plat_gmount_cb_data_t	*cp = data;
15899acbbeafSnn 	zlog_t			*zlogp = cp->pgcd_zlogp;
15909acbbeafSnn 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15919acbbeafSnn 	int			num_fs = *cp->pgcd_num_fs;
15929acbbeafSnn 	struct zone_fstab	*fsp, *tmp_ptr;
15939acbbeafSnn 
15949acbbeafSnn 	num_fs++;
15959acbbeafSnn 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15969acbbeafSnn 		zerror(zlogp, B_TRUE, "memory allocation failed");
15979acbbeafSnn 		return (-1);
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 
16009acbbeafSnn 	fs_ptr = tmp_ptr;
16019acbbeafSnn 	fsp = &fs_ptr[num_fs - 1];
16027c478bd9Sstevel@tonic-gate 
16039acbbeafSnn 	/* update the callback struct passed in */
16049acbbeafSnn 	*cp->pgcd_fs_tab = fs_ptr;
16059acbbeafSnn 	*cp->pgcd_num_fs = num_fs;
16067c478bd9Sstevel@tonic-gate 
16079acbbeafSnn 	fsp->zone_fs_raw[0] = '\0';
16089acbbeafSnn 	(void) strlcpy(fsp->zone_fs_special, spec,
16099acbbeafSnn 	    sizeof (fsp->zone_fs_special));
16109acbbeafSnn 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
16119acbbeafSnn 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
16129acbbeafSnn 	fsp->zone_fs_options = NULL;
1613fa3d7ae1Sedp 	if ((opt != NULL) &&
1614fa3d7ae1Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
16159acbbeafSnn 		zerror(zlogp, B_FALSE, "error adding property");
16169acbbeafSnn 		return (-1);
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16199acbbeafSnn 	return (0);
16209acbbeafSnn }
16219acbbeafSnn 
16229acbbeafSnn static int
mount_filesystems_fsent(zone_dochandle_t handle,zlog_t * zlogp,struct zone_fstab ** fs_tabp,int * num_fsp,zone_mnt_t mount_cmd)16239acbbeafSnn mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16246cfd72c6Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16259acbbeafSnn {
16269acbbeafSnn 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16279acbbeafSnn 	int num_fs;
16289acbbeafSnn 
16299acbbeafSnn 	num_fs = *num_fsp;
16309acbbeafSnn 	fs_ptr = *fs_tabp;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
16337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
16349acbbeafSnn 		return (-1);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
1637fa9e4066Sahrens 		/*
1638fa9e4066Sahrens 		 * ZFS filesystems will not be accessible under an alternate
1639fa9e4066Sahrens 		 * root, since the pool will not be known.  Ignore them in this
1640fa9e4066Sahrens 		 * case.
1641fa9e4066Sahrens 		 */
16426cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16436cfd72c6Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
1644fa9e4066Sahrens 			continue;
1645fa9e4066Sahrens 
16467c478bd9Sstevel@tonic-gate 		num_fs++;
16477c478bd9Sstevel@tonic-gate 		if ((tmp_ptr = realloc(fs_ptr,
16487c478bd9Sstevel@tonic-gate 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16497c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
16507c478bd9Sstevel@tonic-gate 			(void) zonecfg_endfsent(handle);
16519acbbeafSnn 			return (-1);
16527c478bd9Sstevel@tonic-gate 		}
16539acbbeafSnn 		/* update the pointers passed in */
16549acbbeafSnn 		*fs_tabp = tmp_ptr;
16559acbbeafSnn 		*num_fsp = num_fs;
16569acbbeafSnn 
16577c478bd9Sstevel@tonic-gate 		fs_ptr = tmp_ptr;
16587c478bd9Sstevel@tonic-gate 		fsp = &fs_ptr[num_fs - 1];
16597c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_dir,
16607c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16617c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16627c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_raw));
16637c478bd9Sstevel@tonic-gate 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16647c478bd9Sstevel@tonic-gate 		    sizeof (fsp->zone_fs_type));
16657c478bd9Sstevel@tonic-gate 		fsp->zone_fs_options = fstab.zone_fs_options;
1666fa3d7ae1Sedp 
1667fa3d7ae1Sedp 		/*
1668fa3d7ae1Sedp 		 * For all lofs mounts, make sure that the 'special'
1669fa3d7ae1Sedp 		 * entry points inside the alternate root.  The
1670fa3d7ae1Sedp 		 * source path for a lofs mount in a given zone needs
1671fa3d7ae1Sedp 		 * to be relative to the root of the boot environment
1672fa3d7ae1Sedp 		 * that contains the zone.  Note that we don't do this
1673fa3d7ae1Sedp 		 * for non-lofs mounts since they will have a device
1674fa3d7ae1Sedp 		 * as a backing store and device paths must always be
1675fa3d7ae1Sedp 		 * specified relative to the current boot environment.
1676fa3d7ae1Sedp 		 */
1677fa3d7ae1Sedp 		fsp->zone_fs_special[0] = '\0';
1678fa3d7ae1Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1679fa3d7ae1Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1680fa3d7ae1Sedp 			    sizeof (fsp->zone_fs_special));
1681fa3d7ae1Sedp 		}
1682fa3d7ae1Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1683fa3d7ae1Sedp 		    sizeof (fsp->zone_fs_special));
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
16869acbbeafSnn 	return (0);
16879acbbeafSnn }
16889acbbeafSnn 
16899acbbeafSnn static int
mount_filesystems(zlog_t * zlogp,zone_mnt_t mount_cmd)16906cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
16919acbbeafSnn {
16929acbbeafSnn 	char rootpath[MAXPATHLEN];
16939acbbeafSnn 	char zonepath[MAXPATHLEN];
16949acbbeafSnn 	char brand[MAXNAMELEN];
169516bca938Svp 	char luroot[MAXPATHLEN];
16969acbbeafSnn 	int i, num_fs = 0;
16979acbbeafSnn 	struct zone_fstab *fs_ptr = NULL;
16989acbbeafSnn 	zone_dochandle_t handle = NULL;
16999acbbeafSnn 	zone_state_t zstate;
1700123807fbSedp 	brand_handle_t bh;
17019acbbeafSnn 	plat_gmount_cb_data_t cb;
17029acbbeafSnn 
17039acbbeafSnn 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
17049acbbeafSnn 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
17059acbbeafSnn 		zerror(zlogp, B_FALSE,
17069acbbeafSnn 		    "zone must be in '%s' or '%s' state to mount file-systems",
17079acbbeafSnn 		    zone_state_str(ZONE_STATE_READY),
17089acbbeafSnn 		    zone_state_str(ZONE_STATE_MOUNTED));
17099acbbeafSnn 		goto bad;
17109acbbeafSnn 	}
17119acbbeafSnn 
17129acbbeafSnn 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
17139acbbeafSnn 		zerror(zlogp, B_TRUE, "unable to determine zone path");
17149acbbeafSnn 		goto bad;
17159acbbeafSnn 	}
17169acbbeafSnn 
17179acbbeafSnn 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
17189acbbeafSnn 		zerror(zlogp, B_TRUE, "unable to determine zone root");
17199acbbeafSnn 		goto bad;
17209acbbeafSnn 	}
17219acbbeafSnn 
17229acbbeafSnn 	if ((handle = zonecfg_init_handle()) == NULL) {
17239acbbeafSnn 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
17249acbbeafSnn 		goto bad;
17259acbbeafSnn 	}
17269acbbeafSnn 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17279acbbeafSnn 	    zonecfg_setfsent(handle) != Z_OK) {
17289acbbeafSnn 		zerror(zlogp, B_FALSE, "invalid configuration");
17299acbbeafSnn 		goto bad;
17309acbbeafSnn 	}
17319acbbeafSnn 
17320679f794S 	/*
1733e5816e35SEdward Pilatowicz 	 * If we are mounting the zone, then we must always use the default
17340679f794S 	 * brand global mounts.
17350679f794S 	 */
17360679f794S 	if (ALT_MOUNT(mount_cmd)) {
1737e5816e35SEdward Pilatowicz 		(void) strlcpy(brand, default_brand, sizeof (brand));
17380679f794S 	} else {
173962868012SSteve Lawrence 		(void) strlcpy(brand, brand_name, sizeof (brand));
17400679f794S 	}
17410679f794S 
17429acbbeafSnn 	/* Get a handle to the brand info for this zone */
17430679f794S 	if ((bh = brand_open(brand)) == NULL) {
17449acbbeafSnn 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1745e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17469acbbeafSnn 		return (-1);
17479acbbeafSnn 	}
17489acbbeafSnn 
17499acbbeafSnn 	/*
17509acbbeafSnn 	 * Get the list of global filesystems to mount from the brand
17519acbbeafSnn 	 * configuration.
17529acbbeafSnn 	 */
17539acbbeafSnn 	cb.pgcd_zlogp = zlogp;
17549acbbeafSnn 	cb.pgcd_fs_tab = &fs_ptr;
17559acbbeafSnn 	cb.pgcd_num_fs = &num_fs;
1756c6596baeSPatrick Mooney 	if (brand_platform_iter_gmounts(bh, zone_name, zonepath,
17579acbbeafSnn 	    plat_gmount_cb, &cb) != 0) {
17589acbbeafSnn 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
1759123807fbSedp 		brand_close(bh);
1760e767a340Sgjelinek 		zonecfg_fini_handle(handle);
17619acbbeafSnn 		return (-1);
17629acbbeafSnn 	}
1763123807fbSedp 	brand_close(bh);
17649acbbeafSnn 
17659acbbeafSnn 	/*
17666e1ae2a3SGary Pennington 	 * Iterate through the rest of the filesystems. Sort them all,
17676e1ae2a3SGary Pennington 	 * then mount them in sorted order. This is to make sure the
17686e1ae2a3SGary Pennington 	 * higher level directories (e.g., /usr) get mounted before
17696e1ae2a3SGary Pennington 	 * any beneath them (e.g., /usr/local).
17709acbbeafSnn 	 */
17719acbbeafSnn 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17729acbbeafSnn 	    mount_cmd) != 0)
17739acbbeafSnn 		goto bad;
17749acbbeafSnn 
17757c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17767c478bd9Sstevel@tonic-gate 	handle = NULL;
17777c478bd9Sstevel@tonic-gate 
1778108322fbScarlsonj 	/*
17799acbbeafSnn 	 * Normally when we mount a zone all the zone filesystems
17809acbbeafSnn 	 * get mounted relative to rootpath, which is usually
17819acbbeafSnn 	 * <zonepath>/root.  But when mounting a zone for administration
17829acbbeafSnn 	 * purposes via the zone "mount" state, build_mounted_pre_var()
17839acbbeafSnn 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
17849acbbeafSnn 	 * the zones filesystems there instead.
17859acbbeafSnn 	 *
17869acbbeafSnn 	 * build_mounted_pre_var() and build_mounted_post_var() will
17879acbbeafSnn 	 * also do some extra work to create directories and lofs mount
17889acbbeafSnn 	 * a bunch of global zone file system paths into <zonepath>/lu.
17899acbbeafSnn 	 *
17909acbbeafSnn 	 * This allows us to be able to enter the zone (now rooted at
17919acbbeafSnn 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
17929acbbeafSnn 	 * global zone and have them upgrade the to-be-modified zone's
17939acbbeafSnn 	 * files mounted on /a.  (Which mirrors the existing standard
17949acbbeafSnn 	 * upgrade environment.)
17959acbbeafSnn 	 *
17969acbbeafSnn 	 * There is of course one catch.  When doing the upgrade
17979acbbeafSnn 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
17989acbbeafSnn 	 * for the zone and we don't want to have any /dev filesystem
17999acbbeafSnn 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
18009acbbeafSnn 	 * as a normal zone filesystem by default we'll try to mount
18019acbbeafSnn 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
18029acbbeafSnn 	 * case and instead mount it at <zoneroot>/lu/dev.
18039acbbeafSnn 	 *
18049acbbeafSnn 	 * All this work is done in three phases:
1805f4368d3dSvp 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
1806f4368d3dSvp 	 *   2) Mount the required filesystems as per the zone configuration.
1807f4368d3dSvp 	 *   3) Set up the rest of the scratch zone environment
1808f4368d3dSvp 	 *	(build_mounted_post_var()).
1809108322fbScarlsonj 	 */
18106cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
181116bca938Svp 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1812108322fbScarlsonj 		goto bad;
1813108322fbScarlsonj 
18147c478bd9Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
18159acbbeafSnn 
18167c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
18176cfd72c6Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
18189acbbeafSnn 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
18199acbbeafSnn 			size_t slen = strlen(rootpath) - 2;
18209acbbeafSnn 
18219acbbeafSnn 			/*
18229acbbeafSnn 			 * By default we'll try to mount /dev as /a/dev
18239acbbeafSnn 			 * but /dev is special and always goes at the top
18249acbbeafSnn 			 * so strip the trailing '/a' from the rootpath.
18259acbbeafSnn 			 */
18269acbbeafSnn 			assert(strcmp(&rootpath[slen], "/a") == 0);
18279acbbeafSnn 			rootpath[slen] = '\0';
18280679f794S 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18290679f794S 			    != 0)
18309acbbeafSnn 				goto bad;
18319acbbeafSnn 			rootpath[slen] = '/';
18329acbbeafSnn 			continue;
18339acbbeafSnn 		}
18340679f794S 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18357c478bd9Sstevel@tonic-gate 			goto bad;
18367c478bd9Sstevel@tonic-gate 	}
18376cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18386cfd72c6Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1839f4368d3dSvp 		goto bad;
184045916cd2Sjpk 
184145916cd2Sjpk 	/*
184245916cd2Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
184345916cd2Sjpk 	 */
18446cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18456cfd72c6Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
184645916cd2Sjpk 		goto bad;
184745916cd2Sjpk 
18487c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/*
18517c478bd9Sstevel@tonic-gate 	 * Everything looks fine.
18527c478bd9Sstevel@tonic-gate 	 */
18537c478bd9Sstevel@tonic-gate 	return (0);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate bad:
18567c478bd9Sstevel@tonic-gate 	if (handle != NULL)
18577c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18587c478bd9Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18597c478bd9Sstevel@tonic-gate 	return (-1);
18607c478bd9Sstevel@tonic-gate }
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18637c478bd9Sstevel@tonic-gate static int
addr2netmask(char * prefixstr,int maxprefixlen,uchar_t * maskstr)18647c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18657c478bd9Sstevel@tonic-gate {
18667c478bd9Sstevel@tonic-gate 	int prefixlen;
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18697c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18707c478bd9Sstevel@tonic-gate 		return (1);
18717c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
18727c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
18737c478bd9Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18747c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
18757c478bd9Sstevel@tonic-gate 			continue;
18767c478bd9Sstevel@tonic-gate 		}
18777c478bd9Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18787c478bd9Sstevel@tonic-gate 		prefixlen--;
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 	return (0);
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate /*
18847c478bd9Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
18857c478bd9Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
18867c478bd9Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
18877c478bd9Sstevel@tonic-gate  *
18887c478bd9Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
18897c478bd9Sstevel@tonic-gate  */
18907c478bd9Sstevel@tonic-gate static int
unconfigure_shared_network_interfaces(zlog_t * zlogp,zoneid_t zone_id)1891f4b3ec61Sdh unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
18927c478bd9Sstevel@tonic-gate {
18937c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
18947c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
18957c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
18967c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
18977c478bd9Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
18987c478bd9Sstevel@tonic-gate 	uint_t bufsize;
18997c478bd9Sstevel@tonic-gate 	char *buf = NULL;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
19027c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
19037c478bd9Sstevel@tonic-gate 		ret_code = -1;
19047c478bd9Sstevel@tonic-gate 		goto bad;
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
19077c478bd9Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
19087c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
19097c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
1910f4b3ec61Sdh 		    "could not determine number of network interfaces");
19117c478bd9Sstevel@tonic-gate 		ret_code = -1;
19127c478bd9Sstevel@tonic-gate 		goto bad;
19137c478bd9Sstevel@tonic-gate 	}
19147c478bd9Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
19157c478bd9Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
19167c478bd9Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
19177c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
19187c478bd9Sstevel@tonic-gate 		ret_code = -1;
19197c478bd9Sstevel@tonic-gate 		goto bad;
19207c478bd9Sstevel@tonic-gate 	}
19217c478bd9Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
19227c478bd9Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
19237c478bd9Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
19247c478bd9Sstevel@tonic-gate 	lifc.lifc_buf = buf;
19257c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1926f4b3ec61Sdh 		zerror(zlogp, B_TRUE, "could not get configured network "
1927f4b3ec61Sdh 		    "interfaces");
19287c478bd9Sstevel@tonic-gate 		ret_code = -1;
19297c478bd9Sstevel@tonic-gate 		goto bad;
19307c478bd9Sstevel@tonic-gate 	}
19317c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19327c478bd9Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19337c478bd9Sstevel@tonic-gate 		(void) close(s);
19347c478bd9Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19357c478bd9Sstevel@tonic-gate 		    0) {
19367c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19377c478bd9Sstevel@tonic-gate 			    lifrl.lifr_name);
19387c478bd9Sstevel@tonic-gate 			ret_code = -1;
19397c478bd9Sstevel@tonic-gate 			continue;
19407c478bd9Sstevel@tonic-gate 		}
19417c478bd9Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19427c478bd9Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19437c478bd9Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19447c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1945c1c0ebd5Ssl 			if (errno == ENXIO)
1946c1c0ebd5Ssl 				/*
1947c1c0ebd5Ssl 				 * Interface may have been removed by admin or
1948c1c0ebd5Ssl 				 * another zone halting.
1949c1c0ebd5Ssl 				 */
1950c1c0ebd5Ssl 				continue;
19517c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
1952c1c0ebd5Ssl 			    "%s: could not determine the zone to which this "
1953f4b3ec61Sdh 			    "network interface is bound", lifrl.lifr_name);
19547c478bd9Sstevel@tonic-gate 			ret_code = -1;
19557c478bd9Sstevel@tonic-gate 			continue;
19567c478bd9Sstevel@tonic-gate 		}
19577c478bd9Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19587c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19597c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
1960f4b3ec61Sdh 				    "%s: could not remove network interface",
19617c478bd9Sstevel@tonic-gate 				    lifrl.lifr_name);
19627c478bd9Sstevel@tonic-gate 				ret_code = -1;
19637c478bd9Sstevel@tonic-gate 				continue;
19647c478bd9Sstevel@tonic-gate 			}
19657c478bd9Sstevel@tonic-gate 		}
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate bad:
19687c478bd9Sstevel@tonic-gate 	if (s > 0)
19697c478bd9Sstevel@tonic-gate 		(void) close(s);
19707c478bd9Sstevel@tonic-gate 	if (buf)
19717c478bd9Sstevel@tonic-gate 		free(buf);
19727c478bd9Sstevel@tonic-gate 	return (ret_code);
19737c478bd9Sstevel@tonic-gate }
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate static union	sockunion {
19767c478bd9Sstevel@tonic-gate 	struct	sockaddr sa;
19777c478bd9Sstevel@tonic-gate 	struct	sockaddr_in sin;
19787c478bd9Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19797c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19807c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate static struct {
19837c478bd9Sstevel@tonic-gate 	struct	rt_msghdr hdr;
19847c478bd9Sstevel@tonic-gate 	char	space[512];
19857c478bd9Sstevel@tonic-gate } rtmsg;
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate static int
salen(struct sockaddr * sa)19887c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
19897c478bd9Sstevel@tonic-gate {
19907c478bd9Sstevel@tonic-gate 	switch (sa->sa_family) {
19917c478bd9Sstevel@tonic-gate 	case AF_INET:
19927c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
19937c478bd9Sstevel@tonic-gate 	case AF_LINK:
19947c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
19957c478bd9Sstevel@tonic-gate 	case AF_INET6:
19967c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
19977c478bd9Sstevel@tonic-gate 	default:
19987c478bd9Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
20037c478bd9Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate /*
20067c478bd9Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
20077c478bd9Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
20087c478bd9Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
20097c478bd9Sstevel@tonic-gate  *
20107c478bd9Sstevel@tonic-gate  * This is done using black router socket magic.
20117c478bd9Sstevel@tonic-gate  *
20127c478bd9Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
20137c478bd9Sstevel@tonic-gate  *
20147c478bd9Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
20157c478bd9Sstevel@tonic-gate  * of this might be a useful RFE.
20167c478bd9Sstevel@tonic-gate  */
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate static char *
who_is_using(zlog_t * zlogp,struct lifreq * lifr)20197c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20207c478bd9Sstevel@tonic-gate {
20217c478bd9Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
20227c478bd9Sstevel@tonic-gate 	pid_t pid;
20237c478bd9Sstevel@tonic-gate 	int s, rlen, l, i;
20247c478bd9Sstevel@tonic-gate 	char *cp = rtmsg.space;
20257c478bd9Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
20267c478bd9Sstevel@tonic-gate 	struct sockaddr *sa;
20277c478bd9Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	answer[0] = '\0';
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	pid = getpid();
20327c478bd9Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20337c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20347c478bd9Sstevel@tonic-gate 		return (NULL);
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20387c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20417c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20427c478bd9Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20437c478bd9Sstevel@tonic-gate 	} else {
20447c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20477c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20487c478bd9Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20547c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20557c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20567c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20577c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20587c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20617c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20627c478bd9Sstevel@tonic-gate 	cp += l;
20637c478bd9Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20647c478bd9Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20657c478bd9Sstevel@tonic-gate 	cp += l;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20707c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20717c478bd9Sstevel@tonic-gate 		return (NULL);
20727c478bd9Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20737c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20747c478bd9Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20757c478bd9Sstevel@tonic-gate 		return (NULL);
20767c478bd9Sstevel@tonic-gate 	}
20777c478bd9Sstevel@tonic-gate 	do {
20787c478bd9Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20797c478bd9Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20807c478bd9Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20817c478bd9Sstevel@tonic-gate 	if (l < 0) {
20827c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
20837c478bd9Sstevel@tonic-gate 		return (NULL);
20847c478bd9Sstevel@tonic-gate 	}
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
20877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
20887c478bd9Sstevel@tonic-gate 		    "routing message version %d not understood",
20897c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
20907c478bd9Sstevel@tonic-gate 		return (NULL);
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
20937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
20947c478bd9Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
20957c478bd9Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
20967c478bd9Sstevel@tonic-gate 		return (NULL);
20977c478bd9Sstevel@tonic-gate 	}
20987c478bd9Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
20997c478bd9Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
21007c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
21017c478bd9Sstevel@tonic-gate 		return (NULL);
21027c478bd9Sstevel@tonic-gate 	}
21037c478bd9Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2104f4b3ec61Sdh 		zerror(zlogp, B_FALSE, "network interface not found");
21057c478bd9Sstevel@tonic-gate 		return (NULL);
21067c478bd9Sstevel@tonic-gate 	}
21077c478bd9Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
21087c478bd9Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
21097c478bd9Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
21107c478bd9Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
21117c478bd9Sstevel@tonic-gate 		if (i != RTA_IFP) {
21127c478bd9Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
21137c478bd9Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
21147c478bd9Sstevel@tonic-gate 			continue;
21157c478bd9Sstevel@tonic-gate 		}
21167c478bd9Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
21177c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
21187c478bd9Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
21197c478bd9Sstevel@tonic-gate 		break;
21207c478bd9Sstevel@tonic-gate 	}
21217c478bd9Sstevel@tonic-gate 	if (ifp == NULL) {
2122f4b3ec61Sdh 		zerror(zlogp, B_FALSE, "network interface could not be "
2123f4b3ec61Sdh 		    "determined");
21247c478bd9Sstevel@tonic-gate 		return (NULL);
21257c478bd9Sstevel@tonic-gate 	}
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	/*
21287c478bd9Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21297c478bd9Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21307c478bd9Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21317c478bd9Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21327c478bd9Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21337c478bd9Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21347c478bd9Sstevel@tonic-gate 	 */
21357c478bd9Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21367c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21377c478bd9Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21387c478bd9Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21397c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21407c478bd9Sstevel@tonic-gate 	if (i < 0) {
21417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
2142f4b3ec61Sdh 		    "%s: could not determine the zone network interface "
2143f4b3ec61Sdh 		    "belongs to", lifr->lifr_name);
21447c478bd9Sstevel@tonic-gate 		return (NULL);
21457c478bd9Sstevel@tonic-gate 	}
21467c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21477c478bd9Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21487c478bd9Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	if (strlen(answer) > 0)
21517c478bd9Sstevel@tonic-gate 		return (answer);
21527c478bd9Sstevel@tonic-gate 	return (NULL);
21537c478bd9Sstevel@tonic-gate }
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate /*
21567c478bd9Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21577c478bd9Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21587c478bd9Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21597c478bd9Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21607c478bd9Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21617c478bd9Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21627c478bd9Sstevel@tonic-gate  *
21637c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21647c478bd9Sstevel@tonic-gate  * down whatever we set up and return an error.
21657c478bd9Sstevel@tonic-gate  */
21667c478bd9Sstevel@tonic-gate static int
configure_one_interface(zlog_t * zlogp,zoneid_t zone_id,struct zone_nwiftab * nwiftabptr)21677c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2168f14f3ae7Sjv     struct zone_nwiftab *nwiftabptr)
21697c478bd9Sstevel@tonic-gate {
21707c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
21717c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask4;
21727c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
21732e481403SVamsi Nagineni 	struct sockaddr_storage laddr;
21747c478bd9Sstevel@tonic-gate 	struct in_addr in4;
21757c478bd9Sstevel@tonic-gate 	sa_family_t af;
21767c478bd9Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
21777c478bd9Sstevel@tonic-gate 	int s;
21787c478bd9Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
2179f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	boolean_t is_loopback = B_FALSE;
21807c478bd9Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
21817c478bd9Sstevel@tonic-gate 	int res;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
21847c478bd9Sstevel@tonic-gate 	if (res != Z_OK) {
21857c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
21867c478bd9Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
21877c478bd9Sstevel@tonic-gate 		return (-1);
21887c478bd9Sstevel@tonic-gate 	}
21897c478bd9Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
21907c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
21917c478bd9Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21927c478bd9Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21937c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
21947c478bd9Sstevel@tonic-gate 		return (-1);
21957c478bd9Sstevel@tonic-gate 	}
21967c478bd9Sstevel@tonic-gate 
21972e481403SVamsi Nagineni 	/*
21982e481403SVamsi Nagineni 	 * This is a similar kind of "hack" like in addif() to get around
21992e481403SVamsi Nagineni 	 * the problem of SIOCLIFADDIF.  The problem is that this ioctl
22002e481403SVamsi Nagineni 	 * does not include the netmask when adding a logical interface.
22012e481403SVamsi Nagineni 	 * To get around this problem, we first add the logical interface
22022e481403SVamsi Nagineni 	 * with a 0 address.  After that, we set the netmask if provided.
22032e481403SVamsi Nagineni 	 * Finally we set the interface address.
22042e481403SVamsi Nagineni 	 */
22052e481403SVamsi Nagineni 	laddr = lifr.lifr_addr;
22067c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
22077c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
22082e481403SVamsi Nagineni 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
22092e481403SVamsi Nagineni 
22107c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
221122321485Svp 		/*
221222321485Svp 		 * Here, we know that the interface can't be brought up.
221322321485Svp 		 * A similar warning message was already printed out to
2214bbf21555SRichard Lowe 		 * the console by zoneadm(8) so instead we log the
221522321485Svp 		 * message to syslog and continue.
221622321485Svp 		 */
2217f4b3ec61Sdh 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
221822321485Svp 		    "'%s' which may not be present/plumbed in the "
221922321485Svp 		    "global zone.", lifr.lifr_name);
22207c478bd9Sstevel@tonic-gate 		(void) close(s);
222122321485Svp 		return (Z_OK);
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
22257c478bd9Sstevel@tonic-gate 	if (af == AF_INET)
22267c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22297c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2230f4b3ec61Sdh 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
2231f4b3ec61Sdh 		    "into zone", lifr.lifr_name);
22327c478bd9Sstevel@tonic-gate 		goto bad;
22337c478bd9Sstevel@tonic-gate 	}
22347c478bd9Sstevel@tonic-gate 
2235f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	/*
2236f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * Loopback interface will use the default netmask assigned, if no
2237f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 * netmask is found.
2238f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	 */
22397c478bd9Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2240f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		is_loopback = B_TRUE;
2241f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	}
2242f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (af == AF_INET) {
2243f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		/*
2244f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 * The IPv4 netmask can be determined either
2245f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 * directly if a prefix length was supplied with
2246f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 * the address or via the netmasks database.  Not
2247f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 * being able to determine it is a common failure,
2248f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 * but it often is not fatal to operation of the
2249f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 * interface.  In that case, a warning will be
2250f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 * printed after the rest of the interface's
2251f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 * parameters have been configured.
2252f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		 */
2253f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		(void) memset(&netmask4, 0, sizeof (netmask4));
2254f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		if (slashp != NULL) {
2255f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			if (addr2netmask(slashp + 1, V4_ADDR_LEN,
2256f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			    (uchar_t *)&netmask4.sin_addr) != 0) {
22577c478bd9Sstevel@tonic-gate 				*slashp = '/';
22587c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22597c478bd9Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22607c478bd9Sstevel@tonic-gate 				    lifr.lifr_name,
22617c478bd9Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22627c478bd9Sstevel@tonic-gate 				goto bad;
22637c478bd9Sstevel@tonic-gate 			}
22647c478bd9Sstevel@tonic-gate 			got_netmask = B_TRUE;
2265f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		} else if (getnetmaskbyaddr(in4,
2266f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		    &netmask4.sin_addr) == 0) {
2267f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			got_netmask = B_TRUE;
22687c478bd9Sstevel@tonic-gate 		}
2269f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		if (got_netmask) {
2270f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			netmask4.sin_family = af;
2271f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			(void) memcpy(&lifr.lifr_addr, &netmask4,
2272f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			    sizeof (netmask4));
22737c478bd9Sstevel@tonic-gate 		}
2274f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	} else {
2275f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		(void) memset(&netmask6, 0, sizeof (netmask6));
2276f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		if (addr2netmask(slashp + 1, V6_ADDR_LEN,
2277f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		    (uchar_t *)&netmask6.sin6_addr) != 0) {
2278f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			*slashp = '/';
2279f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			zerror(zlogp, B_FALSE,
2280f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			    "%s: invalid prefix length in %s",
2281f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			    lifr.lifr_name,
2282f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 			    nwiftabptr->zone_nwif_address);
22837c478bd9Sstevel@tonic-gate 			goto bad;
22847c478bd9Sstevel@tonic-gate 		}
2285f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		got_netmask = B_TRUE;
2286f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		netmask6.sin6_family = af;
2287f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		(void) memcpy(&lifr.lifr_addr, &netmask6,
2288f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		    sizeof (netmask6));
2289f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	}
2290f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (got_netmask &&
2291f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
2292f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		zerror(zlogp, B_TRUE, "%s: could not set netmask",
2293f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		    lifr.lifr_name);
2294f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		goto bad;
2295f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	}
2296f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 
22972e481403SVamsi Nagineni 	/* Set the interface address */
22982e481403SVamsi Nagineni 	lifr.lifr_addr = laddr;
2299f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2300f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		zerror(zlogp, B_TRUE,
23012e481403SVamsi Nagineni 		    "%s: could not set IP address to %s",
23022e481403SVamsi Nagineni 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
2303f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 		goto bad;
23047c478bd9Sstevel@tonic-gate 	}
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23077c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
23087c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
23097c478bd9Sstevel@tonic-gate 		goto bad;
23107c478bd9Sstevel@tonic-gate 	}
23117c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
23127c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
23137c478bd9Sstevel@tonic-gate 		int save_errno = errno;
23147c478bd9Sstevel@tonic-gate 		char *zone_using;
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 		/*
23177c478bd9Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
23187c478bd9Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
23197c478bd9Sstevel@tonic-gate 		 * then call a function to determine which zone is already
23207c478bd9Sstevel@tonic-gate 		 * using that address.
23217c478bd9Sstevel@tonic-gate 		 */
23227c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
23237c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2324f4b3ec61Sdh 			    "%s: could not bring network interface up",
2325f4b3ec61Sdh 			    lifr.lifr_name);
23267c478bd9Sstevel@tonic-gate 			goto bad;
23277c478bd9Sstevel@tonic-gate 		}
23287c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23297c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23307c478bd9Sstevel@tonic-gate 			    lifr.lifr_name);
23317c478bd9Sstevel@tonic-gate 			goto bad;
23327c478bd9Sstevel@tonic-gate 		}
23337c478bd9Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23347c478bd9Sstevel@tonic-gate 		errno = save_errno;
23357c478bd9Sstevel@tonic-gate 		if (zone_using == NULL)
23367c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
2337f4b3ec61Sdh 			    "%s: could not bring network interface up",
2338f4b3ec61Sdh 			    lifr.lifr_name);
23397c478bd9Sstevel@tonic-gate 		else
2340f4b3ec61Sdh 			zerror(zlogp, B_TRUE, "%s: could not bring network "
2341f4b3ec61Sdh 			    "interface up: address in use by zone '%s'",
2342f4b3ec61Sdh 			    lifr.lifr_name, zone_using);
23437c478bd9Sstevel@tonic-gate 		goto bad;
23447c478bd9Sstevel@tonic-gate 	}
23457c478bd9Sstevel@tonic-gate 
2346f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India 	if (!got_netmask && !is_loopback) {
23477c478bd9Sstevel@tonic-gate 		/*
23487c478bd9Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23497c478bd9Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23507c478bd9Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23517c478bd9Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23527c478bd9Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23537c478bd9Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23547c478bd9Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23557c478bd9Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23567c478bd9Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23577c478bd9Sstevel@tonic-gate 		 * possible incorrect netmask.
23587c478bd9Sstevel@tonic-gate 		 */
23597c478bd9Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23607c478bd9Sstevel@tonic-gate 		void  *addr;
2361bbf21555SRichard Lowe 		const char *nomatch = "no matching subnet found in netmasks(5)";
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 		if (af == AF_INET)
23647c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23657c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23667c478bd9Sstevel@tonic-gate 		else
23677c478bd9Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
23687c478bd9Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
23697c478bd9Sstevel@tonic-gate 
2370e11c3f44Smeem 		/*
2371e11c3f44Smeem 		 * Find out what netmask the interface is going to be using.
2372e11c3f44Smeem 		 * If we just brought up an IPMP data address on an underlying
2373e11c3f44Smeem 		 * interface above, the address will have already migrated, so
2374e11c3f44Smeem 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
2375e11c3f44Smeem 		 * to bring the address up to get the actual netmask).  Just
2376e11c3f44Smeem 		 * omit printing the actual netmask in this corner-case.
2377e11c3f44Smeem 		 */
23787c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2379e11c3f44Smeem 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2380e11c3f44Smeem 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2381e11c3f44Smeem 			    nomatch);
2382e11c3f44Smeem 		} else {
2383e11c3f44Smeem 			zerror(zlogp, B_FALSE,
2384e11c3f44Smeem 			    "WARNING: %s: %s: %s; using default of %s.",
2385e11c3f44Smeem 			    lifr.lifr_name, nomatch, addrstr4, buffer);
2386e11c3f44Smeem 		}
23877c478bd9Sstevel@tonic-gate 	}
23887c478bd9Sstevel@tonic-gate 
2389de860bd9Sgfaden 	/*
2390de860bd9Sgfaden 	 * If a default router was specified for this interface
2391de860bd9Sgfaden 	 * set the route now. Ignore if already set.
2392de860bd9Sgfaden 	 */
2393de860bd9Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2394de860bd9Sgfaden 		int status;
2395de860bd9Sgfaden 		char *argv[7];
2396de860bd9Sgfaden 
2397de860bd9Sgfaden 		argv[0] = "route";
2398de860bd9Sgfaden 		argv[1] = "add";
2399de860bd9Sgfaden 		argv[2] = "-ifp";
2400de860bd9Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
2401de860bd9Sgfaden 		argv[4] = "default";
2402de860bd9Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
2403de860bd9Sgfaden 		argv[6] = NULL;
2404de860bd9Sgfaden 
2405de860bd9Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
2406de860bd9Sgfaden 		if (status != 0 && status != EEXIST)
2407de860bd9Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
2408de860bd9Sgfaden 			    "interface %s to %s\n",
2409de860bd9Sgfaden 			    nwiftabptr->zone_nwif_physical,
2410de860bd9Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
2411de860bd9Sgfaden 	}
2412de860bd9Sgfaden 
24137c478bd9Sstevel@tonic-gate 	(void) close(s);
24147c478bd9Sstevel@tonic-gate 	return (Z_OK);
24157c478bd9Sstevel@tonic-gate bad:
24167c478bd9Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
24177c478bd9Sstevel@tonic-gate 	(void) close(s);
24187c478bd9Sstevel@tonic-gate 	return (-1);
24197c478bd9Sstevel@tonic-gate }
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate /*
24227c478bd9Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
2423f14f3ae7Sjv  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2424f14f3ae7Sjv  * system.
24257c478bd9Sstevel@tonic-gate  *
24267c478bd9Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24277c478bd9Sstevel@tonic-gate  * whatever we set up, and return an error.
24287c478bd9Sstevel@tonic-gate  */
24297c478bd9Sstevel@tonic-gate static int
configure_shared_network_interfaces(zlog_t * zlogp)2430f4b3ec61Sdh configure_shared_network_interfaces(zlog_t *zlogp)
24317c478bd9Sstevel@tonic-gate {
24327c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
24337c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24347c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24377c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24387c478bd9Sstevel@tonic-gate 		return (-1);
24397c478bd9Sstevel@tonic-gate 	}
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24427c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24437c478bd9Sstevel@tonic-gate 		return (-1);
24447c478bd9Sstevel@tonic-gate 	}
24457c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24467c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24477c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24487c478bd9Sstevel@tonic-gate 		return (-1);
24497c478bd9Sstevel@tonic-gate 	}
24507c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24517c478bd9Sstevel@tonic-gate 		for (;;) {
24527c478bd9Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24537c478bd9Sstevel@tonic-gate 				break;
2454f14f3ae7Sjv 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24557c478bd9Sstevel@tonic-gate 			    Z_OK) {
24567c478bd9Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24577c478bd9Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24587c478bd9Sstevel@tonic-gate 				return (-1);
24597c478bd9Sstevel@tonic-gate 			}
24607c478bd9Sstevel@tonic-gate 		}
24617c478bd9Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24627c478bd9Sstevel@tonic-gate 	}
24637c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
2464fdb7141fSgfaden 	if (is_system_labeled()) {
2465fdb7141fSgfaden 		/*
2466fdb7141fSgfaden 		 * Labeled zones share the loopback interface
2467fdb7141fSgfaden 		 * so it is not plumbed for shared stack instances.
2468fdb7141fSgfaden 		 */
2469fdb7141fSgfaden 		return (0);
2470fdb7141fSgfaden 	}
24717c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
24727c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
24737c478bd9Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
24747c478bd9Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
2475442d3e9eSgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
2476f14f3ae7Sjv 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2477f14f3ae7Sjv 		return (-1);
2478f14f3ae7Sjv 
2479f14f3ae7Sjv 	/* Always plumb up the IPv6 loopback interface. */
2480f14f3ae7Sjv 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
2481f14f3ae7Sjv 	    sizeof (loopback_iftab.zone_nwif_address));
2482f14f3ae7Sjv 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24837c478bd9Sstevel@tonic-gate 		return (-1);
24847c478bd9Sstevel@tonic-gate 	return (0);
24857c478bd9Sstevel@tonic-gate }
24867c478bd9Sstevel@tonic-gate 
248789b1c373Smeem static void
zdlerror(zlog_t * zlogp,dladm_status_t err,const char * dlname,const char * str)248889b1c373Smeem zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
248989b1c373Smeem {
249089b1c373Smeem 	char errmsg[DLADM_STRSIZE];
249189b1c373Smeem 
249289b1c373Smeem 	(void) dladm_status2str(err, errmsg);
249389b1c373Smeem 	zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
249489b1c373Smeem }
249589b1c373Smeem 
2496f4b3ec61Sdh static int
add_datalink(zlog_t * zlogp,char * zone_name,datalink_id_t linkid,char * dlname)24972b24ab6bSSebastien Roy add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2498f4b3ec61Sdh {
249989b1c373Smeem 	dladm_status_t err;
25000dc2366fSVenugopal Iyer 	boolean_t cpuset, poolset;
2501efd4c9b6SSteve Lawrence 	char *poolp;
250289b1c373Smeem 
2503f4b3ec61Sdh 	/* First check if it's in use by global zone. */
2504f4b3ec61Sdh 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
2505f4b3ec61Sdh 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
250689b1c373Smeem 		zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
250789b1c373Smeem 		    "'%s' which is used in the global zone", dlname);
2508f4b3ec61Sdh 		return (-1);
2509f4b3ec61Sdh 	}
2510f4b3ec61Sdh 
2511d62bc4baSyz 	/* Set zoneid of this link. */
25122b24ab6bSSebastien Roy 	err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
25132b24ab6bSSebastien Roy 	    DLADM_OPT_ACTIVE);
251489b1c373Smeem 	if (err != DLADM_STATUS_OK) {
251589b1c373Smeem 		zdlerror(zlogp, err, dlname,
251689b1c373Smeem 		    "WARNING: unable to add network interface");
2517f4b3ec61Sdh 		return (-1);
2518f4b3ec61Sdh 	}
25190dc2366fSVenugopal Iyer 
25200dc2366fSVenugopal Iyer 	/*
25210dc2366fSVenugopal Iyer 	 * Set the pool of this link if the zone has a pool and
25220dc2366fSVenugopal Iyer 	 * neither the cpus nor the pool datalink property is
25230dc2366fSVenugopal Iyer 	 * already set.
25240dc2366fSVenugopal Iyer 	 */
25250dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25260dc2366fSVenugopal Iyer 	    "cpus", &cpuset);
25270dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
25280dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
25290dc2366fSVenugopal Iyer 		    "WARNING: unable to check if cpus link property is set");
25300dc2366fSVenugopal Iyer 	}
25310dc2366fSVenugopal Iyer 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25320dc2366fSVenugopal Iyer 	    "pool", &poolset);
25330dc2366fSVenugopal Iyer 	if (err != DLADM_STATUS_OK) {
25340dc2366fSVenugopal Iyer 		zdlerror(zlogp, err, dlname,
25350dc2366fSVenugopal Iyer 		    "WARNING: unable to check if pool link property is set");
25360dc2366fSVenugopal Iyer 	}
25370dc2366fSVenugopal Iyer 
25380dc2366fSVenugopal Iyer 	if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2539efd4c9b6SSteve Lawrence 		poolp = pool_name;
25400dc2366fSVenugopal Iyer 		err = dladm_set_linkprop(dld_handle, linkid, "pool",
2541efd4c9b6SSteve Lawrence 		    &poolp, 1, DLADM_OPT_ACTIVE);
25420dc2366fSVenugopal Iyer 		if (err != DLADM_STATUS_OK) {
25430dc2366fSVenugopal Iyer 			zerror(zlogp, B_FALSE, "WARNING: unable to set "
25440dc2366fSVenugopal Iyer 			    "pool %s to datalink %s", pool_name, dlname);
2545a3002e0aSGarrett D'Amore 			bzero(pool_name, sizeof (pool_name));
25460dc2366fSVenugopal Iyer 		}
25470dc2366fSVenugopal Iyer 	} else {
2548a3002e0aSGarrett D'Amore 		bzero(pool_name, sizeof (pool_name));
25490dc2366fSVenugopal Iyer 	}
2550f4b3ec61Sdh 	return (0);
2551f4b3ec61Sdh }
2552f4b3ec61Sdh 
2553550b6e40SSowmini Varadhan static boolean_t
sockaddr_to_str(sa_family_t af,const struct sockaddr * sockaddr,char * straddr,size_t len)2554550b6e40SSowmini Varadhan sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
2555550b6e40SSowmini Varadhan     char *straddr, size_t len)
2556550b6e40SSowmini Varadhan {
2557550b6e40SSowmini Varadhan 	struct sockaddr_in *sin;
2558550b6e40SSowmini Varadhan 	struct sockaddr_in6 *sin6;
2559550b6e40SSowmini Varadhan 	const char *str = NULL;
2560550b6e40SSowmini Varadhan 
2561550b6e40SSowmini Varadhan 	if (af == AF_INET) {
2562550b6e40SSowmini Varadhan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2563550b6e40SSowmini Varadhan 		sin = SIN(sockaddr);
2564550b6e40SSowmini Varadhan 		str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
2565550b6e40SSowmini Varadhan 	} else if (af == AF_INET6) {
2566550b6e40SSowmini Varadhan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
2567550b6e40SSowmini Varadhan 		sin6 = SIN6(sockaddr);
2568550b6e40SSowmini Varadhan 		str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
2569550b6e40SSowmini Varadhan 		    len);
2570550b6e40SSowmini Varadhan 	}
2571550b6e40SSowmini Varadhan 
2572550b6e40SSowmini Varadhan 	return (str != NULL);
2573550b6e40SSowmini Varadhan }
2574550b6e40SSowmini Varadhan 
2575550b6e40SSowmini Varadhan static int
ipv4_prefixlen(struct sockaddr_in * sin)2576550b6e40SSowmini Varadhan ipv4_prefixlen(struct sockaddr_in *sin)
2577550b6e40SSowmini Varadhan {
2578550b6e40SSowmini Varadhan 	struct sockaddr_in *m;
2579550b6e40SSowmini Varadhan 	struct sockaddr_storage mask;
2580550b6e40SSowmini Varadhan 
2581550b6e40SSowmini Varadhan 	m = SIN(&mask);
2582550b6e40SSowmini Varadhan 	m->sin_family = AF_INET;
2583550b6e40SSowmini Varadhan 	if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
258464639aafSDarren Reed 		return (mask2plen((struct sockaddr *)&mask));
2585550b6e40SSowmini Varadhan 	} else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
2586550b6e40SSowmini Varadhan 		return (8);
2587550b6e40SSowmini Varadhan 	} else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
2588550b6e40SSowmini Varadhan 		return (16);
2589550b6e40SSowmini Varadhan 	} else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
2590550b6e40SSowmini Varadhan 		return (24);
2591550b6e40SSowmini Varadhan 	}
2592550b6e40SSowmini Varadhan 	return (0);
2593550b6e40SSowmini Varadhan }
2594550b6e40SSowmini Varadhan 
2595550b6e40SSowmini Varadhan static int
zone_setattr_network(int type,zoneid_t zoneid,datalink_id_t linkid,void * buf,size_t bufsize)2596550b6e40SSowmini Varadhan zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
2597550b6e40SSowmini Varadhan     void *buf, size_t bufsize)
2598550b6e40SSowmini Varadhan {
2599550b6e40SSowmini Varadhan 	zone_net_data_t *zndata;
2600550b6e40SSowmini Varadhan 	size_t znsize;
2601550b6e40SSowmini Varadhan 	int err;
2602550b6e40SSowmini Varadhan 
2603550b6e40SSowmini Varadhan 	znsize = sizeof (*zndata) + bufsize;
2604550b6e40SSowmini Varadhan 	zndata = calloc(1, znsize);
2605550b6e40SSowmini Varadhan 	if (zndata == NULL)
2606550b6e40SSowmini Varadhan 		return (ENOMEM);
2607550b6e40SSowmini Varadhan 	zndata->zn_type = type;
2608550b6e40SSowmini Varadhan 	zndata->zn_len = bufsize;
2609550b6e40SSowmini Varadhan 	zndata->zn_linkid = linkid;
2610550b6e40SSowmini Varadhan 	bcopy(buf, zndata->zn_val, zndata->zn_len);
2611550b6e40SSowmini Varadhan 	err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
2612550b6e40SSowmini Varadhan 	free(zndata);
2613550b6e40SSowmini Varadhan 	return (err);
2614550b6e40SSowmini Varadhan }
2615550b6e40SSowmini Varadhan 
2616550b6e40SSowmini Varadhan static int
add_net_for_linkid(zlog_t * zlogp,zoneid_t zoneid,zone_addr_list_t * start)2617550b6e40SSowmini Varadhan add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
2618550b6e40SSowmini Varadhan {
2619550b6e40SSowmini Varadhan 	struct lifreq lifr;
2620550b6e40SSowmini Varadhan 	char **astr, *address;
2621550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
2622550b6e40SSowmini Varadhan 	char *ip_nospoof = "ip-nospoof";
2623550b6e40SSowmini Varadhan 	int nnet, naddr, err = 0, j;
2624550b6e40SSowmini Varadhan 	size_t zlen, cpleft;
2625550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *end;
2626550b6e40SSowmini Varadhan 	char  tmp[INET6_ADDRSTRLEN], *maskstr;
2627550b6e40SSowmini Varadhan 	char *zaddr, *cp;
2628550b6e40SSowmini Varadhan 	struct in6_addr *routes = NULL;
2629550b6e40SSowmini Varadhan 	boolean_t is_set;
2630550b6e40SSowmini Varadhan 	datalink_id_t linkid;
2631550b6e40SSowmini Varadhan 
2632550b6e40SSowmini Varadhan 	assert(start != NULL);
2633550b6e40SSowmini Varadhan 	naddr = 0; /* number of addresses */
2634550b6e40SSowmini Varadhan 	nnet = 0; /* number of net resources */
2635550b6e40SSowmini Varadhan 	linkid = start->za_linkid;
2636550b6e40SSowmini Varadhan 	for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
2637550b6e40SSowmini Varadhan 	    ptr = ptr->za_next) {
2638550b6e40SSowmini Varadhan 		nnet++;
2639550b6e40SSowmini Varadhan 	}
2640550b6e40SSowmini Varadhan 	end = ptr;
2641550b6e40SSowmini Varadhan 	zlen = nnet * (INET6_ADDRSTRLEN + 1);
2642550b6e40SSowmini Varadhan 	astr = calloc(1, nnet * sizeof (uintptr_t));
2643550b6e40SSowmini Varadhan 	zaddr = calloc(1, zlen);
2644550b6e40SSowmini Varadhan 	if (astr == NULL || zaddr == NULL) {
2645550b6e40SSowmini Varadhan 		err = ENOMEM;
2646550b6e40SSowmini Varadhan 		goto done;
2647550b6e40SSowmini Varadhan 	}
2648550b6e40SSowmini Varadhan 	cp = zaddr;
2649550b6e40SSowmini Varadhan 	cpleft = zlen;
2650550b6e40SSowmini Varadhan 	j = 0;
2651550b6e40SSowmini Varadhan 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
2652550b6e40SSowmini Varadhan 		address = ptr->za_nwiftab.zone_nwif_allowed_address;
2653550b6e40SSowmini Varadhan 		if (address[0] == '\0')
2654550b6e40SSowmini Varadhan 			continue;
2655550b6e40SSowmini Varadhan 		(void) snprintf(tmp, sizeof (tmp), "%s", address);
2656550b6e40SSowmini Varadhan 		/*
2657550b6e40SSowmini Varadhan 		 * Validate the data. zonecfg_valid_net_address() clobbers
2658550b6e40SSowmini Varadhan 		 * the /<mask> in the address string.
2659550b6e40SSowmini Varadhan 		 */
2660550b6e40SSowmini Varadhan 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2661550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "invalid address [%s]\n",
2662550b6e40SSowmini Varadhan 			    address);
2663550b6e40SSowmini Varadhan 			err = EINVAL;
2664550b6e40SSowmini Varadhan 			goto done;
2665550b6e40SSowmini Varadhan 		}
2666550b6e40SSowmini Varadhan 		/*
2667550b6e40SSowmini Varadhan 		 * convert any hostnames to numeric address strings.
2668550b6e40SSowmini Varadhan 		 */
2669550b6e40SSowmini Varadhan 		if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
2670550b6e40SSowmini Varadhan 		    (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
2671550b6e40SSowmini Varadhan 			err = EINVAL;
2672550b6e40SSowmini Varadhan 			goto done;
2673550b6e40SSowmini Varadhan 		}
2674550b6e40SSowmini Varadhan 		/*
2675550b6e40SSowmini Varadhan 		 * make a copy of the numeric string for the data needed
2676550b6e40SSowmini Varadhan 		 * by the "allowed-ips" datalink property.
2677550b6e40SSowmini Varadhan 		 */
2678550b6e40SSowmini Varadhan 		astr[j] = strdup(cp);
2679550b6e40SSowmini Varadhan 		if (astr[j] == NULL) {
2680550b6e40SSowmini Varadhan 			err = ENOMEM;
2681550b6e40SSowmini Varadhan 			goto done;
2682550b6e40SSowmini Varadhan 		}
2683550b6e40SSowmini Varadhan 		j++;
2684550b6e40SSowmini Varadhan 		/*
2685550b6e40SSowmini Varadhan 		 * compute the default netmask from the address, if necessary
2686550b6e40SSowmini Varadhan 		 */
2687550b6e40SSowmini Varadhan 		if ((maskstr = strchr(tmp, '/')) == NULL) {
2688550b6e40SSowmini Varadhan 			int prefixlen;
2689550b6e40SSowmini Varadhan 
2690550b6e40SSowmini Varadhan 			if (lifr.lifr_addr.ss_family == AF_INET) {
2691550b6e40SSowmini Varadhan 				prefixlen = ipv4_prefixlen(
2692550b6e40SSowmini Varadhan 				    SIN(&lifr.lifr_addr));
2693550b6e40SSowmini Varadhan 			} else {
2694550b6e40SSowmini Varadhan 				struct sockaddr_in6 *sin6;
2695550b6e40SSowmini Varadhan 
2696550b6e40SSowmini Varadhan 				sin6 = SIN6(&lifr.lifr_addr);
2697550b6e40SSowmini Varadhan 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2698550b6e40SSowmini Varadhan 					prefixlen = 10;
2699550b6e40SSowmini Varadhan 				else
2700550b6e40SSowmini Varadhan 					prefixlen = 64;
2701550b6e40SSowmini Varadhan 			}
2702550b6e40SSowmini Varadhan 			(void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
2703550b6e40SSowmini Varadhan 			maskstr = tmp;
2704550b6e40SSowmini Varadhan 		} else {
2705550b6e40SSowmini Varadhan 			maskstr++;
2706550b6e40SSowmini Varadhan 		}
2707550b6e40SSowmini Varadhan 		/* append the "/<netmask>" */
2708550b6e40SSowmini Varadhan 		(void) strlcat(cp, "/", cpleft);
2709550b6e40SSowmini Varadhan 		(void) strlcat(cp, maskstr, cpleft);
2710550b6e40SSowmini Varadhan 		(void) strlcat(cp, ",", cpleft);
2711550b6e40SSowmini Varadhan 		cp += strnlen(cp, zlen);
2712550b6e40SSowmini Varadhan 		cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
2713550b6e40SSowmini Varadhan 	}
2714550b6e40SSowmini Varadhan 	naddr = j; /* the actual number of addresses in the net resource */
2715550b6e40SSowmini Varadhan 	assert(naddr <= nnet);
2716550b6e40SSowmini Varadhan 
2717550b6e40SSowmini Varadhan 	/*
2718550b6e40SSowmini Varadhan 	 * zonecfg has already verified that the defrouter property can only
2719550b6e40SSowmini Varadhan 	 * be set if there is at least one address defined for the net resource.
2720550b6e40SSowmini Varadhan 	 * If j is 0, there are no addresses defined, and therefore no routers
2721550b6e40SSowmini Varadhan 	 * to configure, and we are done at that point.
2722550b6e40SSowmini Varadhan 	 */
2723550b6e40SSowmini Varadhan 	if (j == 0)
2724550b6e40SSowmini Varadhan 		goto done;
2725550b6e40SSowmini Varadhan 
2726550b6e40SSowmini Varadhan 	/* over-write last ',' with '\0' */
2727af34582fSAndy Fiddaman 	zaddr[strnlen(zaddr, zlen) - 1] = '\0';
2728550b6e40SSowmini Varadhan 
2729550b6e40SSowmini Varadhan 	/*
2730550b6e40SSowmini Varadhan 	 * First make sure L3 protection is not already set on the link.
2731550b6e40SSowmini Varadhan 	 */
2732550b6e40SSowmini Varadhan 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2733550b6e40SSowmini Varadhan 	    "protection", &is_set);
2734550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2735550b6e40SSowmini Varadhan 		err = EINVAL;
2736550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "unable to check if protection is set");
2737550b6e40SSowmini Varadhan 		goto done;
2738550b6e40SSowmini Varadhan 	}
2739550b6e40SSowmini Varadhan 	if (is_set) {
2740550b6e40SSowmini Varadhan 		err = EINVAL;
2741550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "Protection is already set");
2742550b6e40SSowmini Varadhan 		goto done;
2743550b6e40SSowmini Varadhan 	}
2744550b6e40SSowmini Varadhan 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2745550b6e40SSowmini Varadhan 	    "allowed-ips", &is_set);
2746550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2747550b6e40SSowmini Varadhan 		err = EINVAL;
2748550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
2749550b6e40SSowmini Varadhan 		goto done;
2750550b6e40SSowmini Varadhan 	}
2751550b6e40SSowmini Varadhan 	if (is_set) {
2752550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "allowed-ips is already set");
2753550b6e40SSowmini Varadhan 		err = EINVAL;
2754550b6e40SSowmini Varadhan 		goto done;
2755550b6e40SSowmini Varadhan 	}
2756550b6e40SSowmini Varadhan 
2757550b6e40SSowmini Varadhan 	/*
2758550b6e40SSowmini Varadhan 	 * Enable ip-nospoof for the link, and add address to the allowed-ips
2759550b6e40SSowmini Varadhan 	 * list.
2760550b6e40SSowmini Varadhan 	 */
2761550b6e40SSowmini Varadhan 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
2762550b6e40SSowmini Varadhan 	    &ip_nospoof, 1, DLADM_OPT_ACTIVE);
2763550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2764550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "could not set protection\n");
2765550b6e40SSowmini Varadhan 		err = EINVAL;
2766550b6e40SSowmini Varadhan 		goto done;
2767550b6e40SSowmini Varadhan 	}
2768550b6e40SSowmini Varadhan 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
2769550b6e40SSowmini Varadhan 	    astr, naddr, DLADM_OPT_ACTIVE);
2770550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK) {
2771550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
2772550b6e40SSowmini Varadhan 		err = EINVAL;
2773550b6e40SSowmini Varadhan 		goto done;
2774550b6e40SSowmini Varadhan 	}
2775550b6e40SSowmini Varadhan 
2776550b6e40SSowmini Varadhan 	/* now set the address in the data-store */
2777550b6e40SSowmini Varadhan 	err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
2778550b6e40SSowmini Varadhan 	    zaddr, strnlen(zaddr, zlen) + 1);
2779550b6e40SSowmini Varadhan 	if (err != 0)
2780550b6e40SSowmini Varadhan 		goto done;
2781550b6e40SSowmini Varadhan 
2782550b6e40SSowmini Varadhan 	/*
2783550b6e40SSowmini Varadhan 	 * add the defaultrouters
2784550b6e40SSowmini Varadhan 	 */
2785550b6e40SSowmini Varadhan 	routes = calloc(1, nnet * sizeof (*routes));
2786550b6e40SSowmini Varadhan 	j = 0;
2787550b6e40SSowmini Varadhan 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
2788550b6e40SSowmini Varadhan 		address = ptr->za_nwiftab.zone_nwif_defrouter;
2789550b6e40SSowmini Varadhan 		if (address[0] == '\0')
2790550b6e40SSowmini Varadhan 			continue;
2791550b6e40SSowmini Varadhan 		if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
2792550b6e40SSowmini Varadhan 			/*
2793550b6e40SSowmini Varadhan 			 * zonecfg_valid_net_address() expects numeric IPv6
2794550b6e40SSowmini Varadhan 			 * addresses to have a CIDR format netmask.
2795550b6e40SSowmini Varadhan 			 */
2796550b6e40SSowmini Varadhan 			(void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
2797550b6e40SSowmini Varadhan 			(void) strlcat(address, tmp, INET6_ADDRSTRLEN);
2798550b6e40SSowmini Varadhan 		}
2799550b6e40SSowmini Varadhan 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2800550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE,
2801550b6e40SSowmini Varadhan 			    "invalid router [%s]\n", address);
2802550b6e40SSowmini Varadhan 			err = EINVAL;
2803550b6e40SSowmini Varadhan 			goto done;
2804550b6e40SSowmini Varadhan 		}
2805550b6e40SSowmini Varadhan 		if (lifr.lifr_addr.ss_family == AF_INET6) {
2806550b6e40SSowmini Varadhan 			routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
2807550b6e40SSowmini Varadhan 		} else {
2808550b6e40SSowmini Varadhan 			IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
2809550b6e40SSowmini Varadhan 			    &routes[j]);
2810550b6e40SSowmini Varadhan 		}
2811550b6e40SSowmini Varadhan 		j++;
2812550b6e40SSowmini Varadhan 	}
2813550b6e40SSowmini Varadhan 	assert(j <= nnet);
2814550b6e40SSowmini Varadhan 	if (j > 0) {
2815550b6e40SSowmini Varadhan 		err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
2816550b6e40SSowmini Varadhan 		    linkid, routes, j * sizeof (*routes));
2817550b6e40SSowmini Varadhan 	}
2818550b6e40SSowmini Varadhan done:
2819550b6e40SSowmini Varadhan 	free(routes);
2820550b6e40SSowmini Varadhan 	for (j = 0; j < naddr; j++)
2821550b6e40SSowmini Varadhan 		free(astr[j]);
2822550b6e40SSowmini Varadhan 	free(astr);
2823550b6e40SSowmini Varadhan 	free(zaddr);
2824550b6e40SSowmini Varadhan 	return (err);
2825550b6e40SSowmini Varadhan 
2826550b6e40SSowmini Varadhan }
2827550b6e40SSowmini Varadhan 
2828550b6e40SSowmini Varadhan static int
add_net(zlog_t * zlogp,zoneid_t zoneid,zone_addr_list_t * zalist)2829550b6e40SSowmini Varadhan add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
2830550b6e40SSowmini Varadhan {
2831550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr;
2832550b6e40SSowmini Varadhan 	datalink_id_t linkid;
2833550b6e40SSowmini Varadhan 	int err;
2834550b6e40SSowmini Varadhan 
2835550b6e40SSowmini Varadhan 	if (zalist == NULL)
2836550b6e40SSowmini Varadhan 		return (0);
2837550b6e40SSowmini Varadhan 
2838550b6e40SSowmini Varadhan 	linkid = zalist->za_linkid;
2839550b6e40SSowmini Varadhan 
2840550b6e40SSowmini Varadhan 	err = add_net_for_linkid(zlogp, zoneid, zalist);
2841550b6e40SSowmini Varadhan 	if (err != 0)
2842550b6e40SSowmini Varadhan 		return (err);
2843550b6e40SSowmini Varadhan 
2844550b6e40SSowmini Varadhan 	for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
2845550b6e40SSowmini Varadhan 		if (ptr->za_linkid == linkid)
2846550b6e40SSowmini Varadhan 			continue;
2847550b6e40SSowmini Varadhan 		linkid = ptr->za_linkid;
2848550b6e40SSowmini Varadhan 		err = add_net_for_linkid(zlogp, zoneid, ptr);
2849550b6e40SSowmini Varadhan 		if (err != 0)
2850550b6e40SSowmini Varadhan 			return (err);
2851550b6e40SSowmini Varadhan 	}
2852550b6e40SSowmini Varadhan 	return (0);
2853550b6e40SSowmini Varadhan }
2854550b6e40SSowmini Varadhan 
2855550b6e40SSowmini Varadhan /*
2856550b6e40SSowmini Varadhan  * Add "new" to the list of network interfaces to be configured  by
2857550b6e40SSowmini Varadhan  * add_net on zone boot in "old". The list of interfaces in "old" is
2858550b6e40SSowmini Varadhan  * sorted by datalink_id_t, with interfaces sorted FIFO for a given
2859550b6e40SSowmini Varadhan  * datalink_id_t.
2860550b6e40SSowmini Varadhan  *
2861550b6e40SSowmini Varadhan  * Returns the merged list of IP interfaces containing "old" and "new"
2862550b6e40SSowmini Varadhan  */
2863550b6e40SSowmini Varadhan static zone_addr_list_t *
add_ip_interface(zone_addr_list_t * old,zone_addr_list_t * new)2864550b6e40SSowmini Varadhan add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
2865550b6e40SSowmini Varadhan {
2866550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *next;
2867550b6e40SSowmini Varadhan 	datalink_id_t linkid = new->za_linkid;
2868550b6e40SSowmini Varadhan 
2869550b6e40SSowmini Varadhan 	assert(old != new);
2870550b6e40SSowmini Varadhan 
2871550b6e40SSowmini Varadhan 	if (old == NULL)
2872550b6e40SSowmini Varadhan 		return (new);
2873550b6e40SSowmini Varadhan 	for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
2874550b6e40SSowmini Varadhan 		if (ptr->za_linkid == linkid)
2875550b6e40SSowmini Varadhan 			break;
2876550b6e40SSowmini Varadhan 	}
2877550b6e40SSowmini Varadhan 	if (ptr == NULL) {
2878550b6e40SSowmini Varadhan 		/* linkid does not already exist, add to the beginning */
2879550b6e40SSowmini Varadhan 		new->za_next = old;
2880550b6e40SSowmini Varadhan 		return (new);
2881550b6e40SSowmini Varadhan 	}
2882550b6e40SSowmini Varadhan 	/*
2883550b6e40SSowmini Varadhan 	 * adding to the middle of the list; ptr points at the first
2884550b6e40SSowmini Varadhan 	 * occurrence of linkid. Find the last occurrence.
2885550b6e40SSowmini Varadhan 	 */
2886550b6e40SSowmini Varadhan 	while ((next = ptr->za_next) != NULL) {
2887550b6e40SSowmini Varadhan 		if (next->za_linkid != linkid)
2888550b6e40SSowmini Varadhan 			break;
2889550b6e40SSowmini Varadhan 		ptr = next;
2890550b6e40SSowmini Varadhan 	}
2891550b6e40SSowmini Varadhan 	/* insert new after ptr */
2892550b6e40SSowmini Varadhan 	new->za_next = next;
2893550b6e40SSowmini Varadhan 	ptr->za_next = new;
2894550b6e40SSowmini Varadhan 	return (old);
2895550b6e40SSowmini Varadhan }
2896550b6e40SSowmini Varadhan 
2897550b6e40SSowmini Varadhan void
free_ip_interface(zone_addr_list_t * zalist)2898550b6e40SSowmini Varadhan free_ip_interface(zone_addr_list_t *zalist)
2899550b6e40SSowmini Varadhan {
2900550b6e40SSowmini Varadhan 	zone_addr_list_t *ptr, *new;
2901550b6e40SSowmini Varadhan 
2902*fcfad32eSPatrick Mooney 	for (ptr = zalist; ptr != NULL; ) {
2903550b6e40SSowmini Varadhan 		new = ptr;
2904550b6e40SSowmini Varadhan 		ptr = ptr->za_next;
2905550b6e40SSowmini Varadhan 		free(new);
2906550b6e40SSowmini Varadhan 	}
2907550b6e40SSowmini Varadhan }
2908550b6e40SSowmini Varadhan 
2909f4b3ec61Sdh /*
2910f4b3ec61Sdh  * Add the kernel access control information for the interface names.
2911f4b3ec61Sdh  * If anything goes wrong, we log a general error message, attempt to tear down
2912f4b3ec61Sdh  * whatever we set up, and return an error.
2913f4b3ec61Sdh  */
2914f4b3ec61Sdh static int
configure_exclusive_network_interfaces(zlog_t * zlogp,zoneid_t zoneid)2915550b6e40SSowmini Varadhan configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2916f4b3ec61Sdh {
2917f4b3ec61Sdh 	zone_dochandle_t handle;
2918f4b3ec61Sdh 	struct zone_nwiftab nwiftab;
2919f4b3ec61Sdh 	char rootpath[MAXPATHLEN];
2920f4b3ec61Sdh 	char path[MAXPATHLEN];
29212b24ab6bSSebastien Roy 	datalink_id_t linkid;
2922f4b3ec61Sdh 	di_prof_t prof = NULL;
2923f4b3ec61Sdh 	boolean_t added = B_FALSE;
2924550b6e40SSowmini Varadhan 	zone_addr_list_t *zalist = NULL, *new;
2925f4b3ec61Sdh 
2926f4b3ec61Sdh 	if ((handle = zonecfg_init_handle()) == NULL) {
2927f4b3ec61Sdh 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
2928f4b3ec61Sdh 		return (-1);
2929f4b3ec61Sdh 	}
2930f4b3ec61Sdh 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2931f4b3ec61Sdh 		zerror(zlogp, B_FALSE, "invalid configuration");
2932f4b3ec61Sdh 		zonecfg_fini_handle(handle);
2933f4b3ec61Sdh 		return (-1);
2934f4b3ec61Sdh 	}
2935f4b3ec61Sdh 
2936f4b3ec61Sdh 	if (zonecfg_setnwifent(handle) != Z_OK) {
2937f4b3ec61Sdh 		zonecfg_fini_handle(handle);
2938f4b3ec61Sdh 		return (0);
2939f4b3ec61Sdh 	}
2940f4b3ec61Sdh 
2941f4b3ec61Sdh 	for (;;) {
2942f4b3ec61Sdh 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2943f4b3ec61Sdh 			break;
2944f4b3ec61Sdh 
2945f4b3ec61Sdh 		if (prof == NULL) {
2946f4b3ec61Sdh 			if (zone_get_devroot(zone_name, rootpath,
2947f4b3ec61Sdh 			    sizeof (rootpath)) != Z_OK) {
2948f4b3ec61Sdh 				(void) zonecfg_endnwifent(handle);
2949f4b3ec61Sdh 				zonecfg_fini_handle(handle);
2950f4b3ec61Sdh 				zerror(zlogp, B_TRUE,
2951f4b3ec61Sdh 				    "unable to determine dev root");
2952f4b3ec61Sdh 				return (-1);
2953f4b3ec61Sdh 			}
2954f4b3ec61Sdh 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
2955f4b3ec61Sdh 			    "/dev");
2956f4b3ec61Sdh 			if (di_prof_init(path, &prof) != 0) {
2957f4b3ec61Sdh 				(void) zonecfg_endnwifent(handle);
2958f4b3ec61Sdh 				zonecfg_fini_handle(handle);
2959f4b3ec61Sdh 				zerror(zlogp, B_TRUE,
2960f4b3ec61Sdh 				    "failed to initialize profile");
2961f4b3ec61Sdh 				return (-1);
2962f4b3ec61Sdh 			}
2963f4b3ec61Sdh 		}
2964f4b3ec61Sdh 
2965f4b3ec61Sdh 		/*
2966d62bc4baSyz 		 * Create the /dev entry for backward compatibility.
2967f4b3ec61Sdh 		 * Only create the /dev entry if it's not in use.
2968d62bc4baSyz 		 * Note that the zone still boots when the assigned
2969d62bc4baSyz 		 * interface is inaccessible, used by others, etc.
2970d62bc4baSyz 		 * Also, when vanity naming is used, some interface do
2971d62bc4baSyz 		 * do not have corresponding /dev node names (for example,
2972d62bc4baSyz 		 * vanity named aggregations).  The /dev entry is not
2973d62bc4baSyz 		 * created in that case.  The /dev/net entry is always
2974d62bc4baSyz 		 * accessible.
2975f4b3ec61Sdh 		 */
29762b24ab6bSSebastien Roy 		if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
29772b24ab6bSSebastien Roy 		    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
29782b24ab6bSSebastien Roy 		    add_datalink(zlogp, zone_name, linkid,
29792b24ab6bSSebastien Roy 		    nwiftab.zone_nwif_physical) == 0) {
29803bc21d0aSAruna Ramakrishna - Sun Microsystems 			added = B_TRUE;
29813bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
29823bc21d0aSAruna Ramakrishna - Sun Microsystems 			(void) zonecfg_endnwifent(handle);
29833bc21d0aSAruna Ramakrishna - Sun Microsystems 			zonecfg_fini_handle(handle);
29843bc21d0aSAruna Ramakrishna - Sun Microsystems 			zerror(zlogp, B_TRUE, "failed to add network device");
29853bc21d0aSAruna Ramakrishna - Sun Microsystems 			return (-1);
2986f4b3ec61Sdh 		}
2987550b6e40SSowmini Varadhan 		/* set up the new IP interface, and add them all later */
2988550b6e40SSowmini Varadhan 		new = malloc(sizeof (*new));
2989550b6e40SSowmini Varadhan 		if (new == NULL) {
2990550b6e40SSowmini Varadhan 			zerror(zlogp, B_TRUE, "no memory for %s",
2991550b6e40SSowmini Varadhan 			    nwiftab.zone_nwif_physical);
2992550b6e40SSowmini Varadhan 			zonecfg_fini_handle(handle);
2993550b6e40SSowmini Varadhan 			free_ip_interface(zalist);
2994550b6e40SSowmini Varadhan 		}
2995550b6e40SSowmini Varadhan 		bzero(new, sizeof (*new));
2996550b6e40SSowmini Varadhan 		new->za_nwiftab = nwiftab;
2997550b6e40SSowmini Varadhan 		new->za_linkid = linkid;
2998550b6e40SSowmini Varadhan 		zalist = add_ip_interface(zalist, new);
2999550b6e40SSowmini Varadhan 	}
3000550b6e40SSowmini Varadhan 	if (zalist != NULL) {
3001550b6e40SSowmini Varadhan 		if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
3002550b6e40SSowmini Varadhan 			(void) zonecfg_endnwifent(handle);
3003550b6e40SSowmini Varadhan 			zonecfg_fini_handle(handle);
3004550b6e40SSowmini Varadhan 			zerror(zlogp, B_TRUE, "failed to add address");
3005550b6e40SSowmini Varadhan 			free_ip_interface(zalist);
3006550b6e40SSowmini Varadhan 			return (-1);
3007550b6e40SSowmini Varadhan 		}
3008550b6e40SSowmini Varadhan 		free_ip_interface(zalist);
3009f4b3ec61Sdh 	}
3010f4b3ec61Sdh 	(void) zonecfg_endnwifent(handle);
3011f4b3ec61Sdh 	zonecfg_fini_handle(handle);
3012f4b3ec61Sdh 
3013f4b3ec61Sdh 	if (prof != NULL && added) {
3014f4b3ec61Sdh 		if (di_prof_commit(prof) != 0) {
3015f4b3ec61Sdh 			zerror(zlogp, B_TRUE, "failed to commit profile");
3016f4b3ec61Sdh 			return (-1);
3017f4b3ec61Sdh 		}
3018f4b3ec61Sdh 	}
3019f4b3ec61Sdh 	if (prof != NULL)
3020f4b3ec61Sdh 		di_prof_fini(prof);
3021f4b3ec61Sdh 
3022f4b3ec61Sdh 	return (0);
3023f4b3ec61Sdh }
3024f4b3ec61Sdh 
302585dff7a0SAndy Fiddaman /*
302685dff7a0SAndy Fiddaman  * Retrieve the list of datalink IDs assigned to a zone.
302785dff7a0SAndy Fiddaman  *
302885dff7a0SAndy Fiddaman  * On return, *count will be updated with the total number of links and, if it
302985dff7a0SAndy Fiddaman  * is not NULL, **linksp will be updated to point to allocated memory
303085dff7a0SAndy Fiddaman  * containing the link IDs. This should be passed to free() when the caller is
303185dff7a0SAndy Fiddaman  * finished with it.
303285dff7a0SAndy Fiddaman  */
303385dff7a0SAndy Fiddaman static int
fetch_zone_datalinks(zlog_t * zlogp,zoneid_t zoneid,int * countp,datalink_id_t ** linksp)303485dff7a0SAndy Fiddaman fetch_zone_datalinks(zlog_t *zlogp, zoneid_t zoneid, int *countp,
303585dff7a0SAndy Fiddaman     datalink_id_t **linksp)
303685dff7a0SAndy Fiddaman {
303785dff7a0SAndy Fiddaman 	datalink_id_t *links = NULL;
303885dff7a0SAndy Fiddaman 	int links_size = 0;
303985dff7a0SAndy Fiddaman 	int num_links;
304085dff7a0SAndy Fiddaman 
304185dff7a0SAndy Fiddaman 	if (linksp != NULL)
304285dff7a0SAndy Fiddaman 		*linksp = NULL;
304385dff7a0SAndy Fiddaman 	*countp = 0;
304485dff7a0SAndy Fiddaman 
304585dff7a0SAndy Fiddaman 	num_links = 0;
304685dff7a0SAndy Fiddaman 	if (zone_list_datalink(zoneid, &num_links, NULL) != 0) {
304785dff7a0SAndy Fiddaman 		zerror(zlogp, B_TRUE,
304885dff7a0SAndy Fiddaman 		    "unable to determine number of network interfaces");
304985dff7a0SAndy Fiddaman 		return (-1);
305085dff7a0SAndy Fiddaman 	}
305185dff7a0SAndy Fiddaman 
305285dff7a0SAndy Fiddaman 	if (num_links == 0)
305385dff7a0SAndy Fiddaman 		return (0);
305485dff7a0SAndy Fiddaman 
305585dff7a0SAndy Fiddaman 	/* If linkp is NULL, the caller only wants the count. */
305685dff7a0SAndy Fiddaman 	if (linksp == NULL) {
305785dff7a0SAndy Fiddaman 		*countp = num_links;
305885dff7a0SAndy Fiddaman 		return (0);
305985dff7a0SAndy Fiddaman 	}
306085dff7a0SAndy Fiddaman 
306185dff7a0SAndy Fiddaman 	do {
306285dff7a0SAndy Fiddaman 		datalink_id_t *p;
306385dff7a0SAndy Fiddaman 
306485dff7a0SAndy Fiddaman 		links_size = num_links;
306585dff7a0SAndy Fiddaman 		p = reallocarray(links, links_size, sizeof (datalink_id_t));
306685dff7a0SAndy Fiddaman 
306785dff7a0SAndy Fiddaman 		if (p == NULL) {
306885dff7a0SAndy Fiddaman 			zerror(zlogp, B_TRUE,
306985dff7a0SAndy Fiddaman 			    "failed to allocate memory for zone links");
307085dff7a0SAndy Fiddaman 			free(links);
307185dff7a0SAndy Fiddaman 			return (-1);
307285dff7a0SAndy Fiddaman 		}
307385dff7a0SAndy Fiddaman 		links = p;
307485dff7a0SAndy Fiddaman 
307585dff7a0SAndy Fiddaman 		if (zone_list_datalink(zoneid, &num_links, links) != 0) {
307685dff7a0SAndy Fiddaman 			zerror(zlogp, B_TRUE, "failed to list zone links");
307785dff7a0SAndy Fiddaman 			free(links);
307885dff7a0SAndy Fiddaman 			return (-1);
307985dff7a0SAndy Fiddaman 		}
308085dff7a0SAndy Fiddaman 	} while (links_size < num_links);
308185dff7a0SAndy Fiddaman 
308285dff7a0SAndy Fiddaman 	*countp = num_links;
308385dff7a0SAndy Fiddaman 	*linksp = links;
308485dff7a0SAndy Fiddaman 
308585dff7a0SAndy Fiddaman 	return (0);
308685dff7a0SAndy Fiddaman }
308785dff7a0SAndy Fiddaman 
30880dc2366fSVenugopal Iyer static int
remove_datalink_pool(zlog_t * zlogp,zoneid_t zoneid)30890dc2366fSVenugopal Iyer remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
30900dc2366fSVenugopal Iyer {
30910dc2366fSVenugopal Iyer 	ushort_t flags;
30920dc2366fSVenugopal Iyer 	zone_iptype_t iptype;
309385dff7a0SAndy Fiddaman 	int i;
30940dc2366fSVenugopal Iyer 	dladm_status_t err;
30950dc2366fSVenugopal Iyer 
30960dc2366fSVenugopal Iyer 	if (strlen(pool_name) == 0)
30970dc2366fSVenugopal Iyer 		return (0);
30980dc2366fSVenugopal Iyer 
30990dc2366fSVenugopal Iyer 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
31000dc2366fSVenugopal Iyer 	    sizeof (flags)) < 0) {
31010dc2366fSVenugopal Iyer 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
3102550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
31030dc2366fSVenugopal Iyer 			return (-1);
31040dc2366fSVenugopal Iyer 		}
31050dc2366fSVenugopal Iyer 	} else {
31060dc2366fSVenugopal Iyer 		if (flags & ZF_NET_EXCL)
31070dc2366fSVenugopal Iyer 			iptype = ZS_EXCLUSIVE;
31080dc2366fSVenugopal Iyer 		else
31090dc2366fSVenugopal Iyer 			iptype = ZS_SHARED;
31100dc2366fSVenugopal Iyer 	}
31110dc2366fSVenugopal Iyer 
31120dc2366fSVenugopal Iyer 	if (iptype == ZS_EXCLUSIVE) {
311385dff7a0SAndy Fiddaman 		datalink_id_t *dllinks = NULL;
311485dff7a0SAndy Fiddaman 		int dlnum = 0;
31150dc2366fSVenugopal Iyer 
311685dff7a0SAndy Fiddaman 		if (fetch_zone_datalinks(zlogp, zoneid, &dlnum, &dllinks) != 0)
31170dc2366fSVenugopal Iyer 			return (-1);
31180dc2366fSVenugopal Iyer 
3119a3002e0aSGarrett D'Amore 		bzero(pool_name, sizeof (pool_name));
312085dff7a0SAndy Fiddaman 		for (i = 0; i < dlnum; i++) {
312185dff7a0SAndy Fiddaman 			err = dladm_set_linkprop(dld_handle, dllinks[i], "pool",
31220dc2366fSVenugopal Iyer 			    NULL, 0, DLADM_OPT_ACTIVE);
31230dc2366fSVenugopal Iyer 			if (err != DLADM_STATUS_OK) {
31240dc2366fSVenugopal Iyer 				zerror(zlogp, B_TRUE,
31250dc2366fSVenugopal Iyer 				    "WARNING: unable to clear pool");
31260dc2366fSVenugopal Iyer 			}
31270dc2366fSVenugopal Iyer 		}
31280dc2366fSVenugopal Iyer 		free(dllinks);
31290dc2366fSVenugopal Iyer 	}
31300dc2366fSVenugopal Iyer 	return (0);
31310dc2366fSVenugopal Iyer }
31320dc2366fSVenugopal Iyer 
3133550b6e40SSowmini Varadhan static int
remove_datalink_protect(zlog_t * zlogp,zoneid_t zoneid)3134550b6e40SSowmini Varadhan remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
3135550b6e40SSowmini Varadhan {
3136550b6e40SSowmini Varadhan 	ushort_t flags;
3137550b6e40SSowmini Varadhan 	zone_iptype_t iptype;
3138550b6e40SSowmini Varadhan 	int i, dlnum = 0;
3139550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
314085dff7a0SAndy Fiddaman 	datalink_id_t *dllinks = NULL;
3141550b6e40SSowmini Varadhan 
3142550b6e40SSowmini Varadhan 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3143550b6e40SSowmini Varadhan 	    sizeof (flags)) < 0) {
3144550b6e40SSowmini Varadhan 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
3145550b6e40SSowmini Varadhan 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
3146550b6e40SSowmini Varadhan 			return (-1);
3147550b6e40SSowmini Varadhan 		}
3148550b6e40SSowmini Varadhan 	} else {
3149550b6e40SSowmini Varadhan 		if (flags & ZF_NET_EXCL)
3150550b6e40SSowmini Varadhan 			iptype = ZS_EXCLUSIVE;
3151550b6e40SSowmini Varadhan 		else
3152550b6e40SSowmini Varadhan 			iptype = ZS_SHARED;
3153550b6e40SSowmini Varadhan 	}
3154550b6e40SSowmini Varadhan 
3155550b6e40SSowmini Varadhan 	if (iptype != ZS_EXCLUSIVE)
3156550b6e40SSowmini Varadhan 		return (0);
3157550b6e40SSowmini Varadhan 
3158550b6e40SSowmini Varadhan 	/*
315985dff7a0SAndy Fiddaman 	 * Get the datalink count and for each datalink, attempt to clear the
316085dff7a0SAndy Fiddaman 	 * protection and allowed_ips properties.
3161550b6e40SSowmini Varadhan 	 */
3162550b6e40SSowmini Varadhan 
316385dff7a0SAndy Fiddaman 	if (fetch_zone_datalinks(zlogp, zoneid, &dlnum, &dllinks) != 0)
3164550b6e40SSowmini Varadhan 		return (-1);
3165550b6e40SSowmini Varadhan 
316685dff7a0SAndy Fiddaman 	for (i = 0; i < dlnum; i++) {
3167fff7ec1dSSowmini Varadhan 		char dlerr[DLADM_STRSIZE];
3168fff7ec1dSSowmini Varadhan 
316985dff7a0SAndy Fiddaman 		dlstatus = dladm_set_linkprop(dld_handle, dllinks[i],
3170550b6e40SSowmini Varadhan 		    "protection", NULL, 0, DLADM_OPT_ACTIVE);
3171fff7ec1dSSowmini Varadhan 		if (dlstatus == DLADM_STATUS_NOTFOUND) {
3172fff7ec1dSSowmini Varadhan 			/* datalink does not belong to the GZ */
3173fff7ec1dSSowmini Varadhan 			continue;
3174fff7ec1dSSowmini Varadhan 		}
3175550b6e40SSowmini Varadhan 		if (dlstatus != DLADM_STATUS_OK) {
3176fff7ec1dSSowmini Varadhan 			zerror(zlogp, B_FALSE,
317785dff7a0SAndy Fiddaman 			    "clear link %d 'protection' link property: %s",
317885dff7a0SAndy Fiddaman 			    dllinks[i], dladm_status2str(dlstatus, dlerr));
3179550b6e40SSowmini Varadhan 		}
318085dff7a0SAndy Fiddaman 
318185dff7a0SAndy Fiddaman 		dlstatus = dladm_set_linkprop(dld_handle, dllinks[i],
3182550b6e40SSowmini Varadhan 		    "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
3183550b6e40SSowmini Varadhan 		if (dlstatus != DLADM_STATUS_OK) {
3184fff7ec1dSSowmini Varadhan 			zerror(zlogp, B_FALSE,
318585dff7a0SAndy Fiddaman 			    "clear link %d 'allowed-ips' link property: %s",
318685dff7a0SAndy Fiddaman 			    dllinks[i], dladm_status2str(dlstatus, dlerr));
3187550b6e40SSowmini Varadhan 		}
3188550b6e40SSowmini Varadhan 	}
3189550b6e40SSowmini Varadhan 	free(dllinks);
3190550b6e40SSowmini Varadhan 	return (0);
3191550b6e40SSowmini Varadhan }
3192550b6e40SSowmini Varadhan 
3193f4b3ec61Sdh static int
unconfigure_exclusive_network_interfaces(zlog_t * zlogp,zoneid_t zoneid)31942b24ab6bSSebastien Roy unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
3195f4b3ec61Sdh {
319685dff7a0SAndy Fiddaman 	datalink_id_t *dllinks;
31972b24ab6bSSebastien Roy 	int dlnum = 0;
319885dff7a0SAndy Fiddaman 	uint_t i;
3199f4b3ec61Sdh 
32002b24ab6bSSebastien Roy 	/*
32012b24ab6bSSebastien Roy 	 * The kernel shutdown callback for the dls module should have removed
32022b24ab6bSSebastien Roy 	 * all datalinks from this zone.  If any remain, then there's a
32032b24ab6bSSebastien Roy 	 * problem.
32042b24ab6bSSebastien Roy 	 */
320585dff7a0SAndy Fiddaman 
320685dff7a0SAndy Fiddaman 	if (fetch_zone_datalinks(zlogp, zoneid, &dlnum, &dllinks) != 0)
3207f4b3ec61Sdh 		return (-1);
320885dff7a0SAndy Fiddaman 
320985dff7a0SAndy Fiddaman 	if (dlnum == 0)
321085dff7a0SAndy Fiddaman 		return (0);
321185dff7a0SAndy Fiddaman 
321285dff7a0SAndy Fiddaman 	/*
321385dff7a0SAndy Fiddaman 	 * There are some datalinks left in the zone. The most likely cause of
321485dff7a0SAndy Fiddaman 	 * this is that the datalink-management daemon (dlmgmtd) was not
321585dff7a0SAndy Fiddaman 	 * running when the zone was shut down. That prevented the kernel from
321685dff7a0SAndy Fiddaman 	 * doing the required upcall to move the links back to the GZ. To
321785dff7a0SAndy Fiddaman 	 * attempt recovery, do that now.
321885dff7a0SAndy Fiddaman 	 */
321985dff7a0SAndy Fiddaman 
322085dff7a0SAndy Fiddaman 	for (i = 0; i < dlnum; i++) {
322185dff7a0SAndy Fiddaman 		char dlerr[DLADM_STRSIZE];
322285dff7a0SAndy Fiddaman 		dladm_status_t status;
322385dff7a0SAndy Fiddaman 		uint32_t link_flags;
322485dff7a0SAndy Fiddaman 		datalink_id_t link = dllinks[i];
322585dff7a0SAndy Fiddaman 		char *prop_vals[] = { GLOBAL_ZONENAME };
322685dff7a0SAndy Fiddaman 
322785dff7a0SAndy Fiddaman 		status = dladm_datalink_id2info(dld_handle, link,
322885dff7a0SAndy Fiddaman 		    &link_flags, NULL, NULL, NULL, 0);
322985dff7a0SAndy Fiddaman 
323085dff7a0SAndy Fiddaman 		if (status != DLADM_STATUS_OK) {
323185dff7a0SAndy Fiddaman 			zerror(zlogp, B_FALSE,
323285dff7a0SAndy Fiddaman 			    "failed to get link info for %u: %s",
323385dff7a0SAndy Fiddaman 			    link, dladm_status2str(status, dlerr));
323485dff7a0SAndy Fiddaman 			continue;
323585dff7a0SAndy Fiddaman 		}
323685dff7a0SAndy Fiddaman 
323785dff7a0SAndy Fiddaman 		if (link_flags & DLADM_OPT_TRANSIENT)
323885dff7a0SAndy Fiddaman 			continue;
323985dff7a0SAndy Fiddaman 
324085dff7a0SAndy Fiddaman 		status = dladm_set_linkprop(dld_handle, link, "zone",
324185dff7a0SAndy Fiddaman 		    prop_vals, 1, DLADM_OPT_ACTIVE);
324285dff7a0SAndy Fiddaman 
324385dff7a0SAndy Fiddaman 		if (status != DLADM_STATUS_OK) {
324485dff7a0SAndy Fiddaman 			zerror(zlogp, B_FALSE,
324585dff7a0SAndy Fiddaman 			    "failed to move link %u to GZ: %s",
324685dff7a0SAndy Fiddaman 			    link, dladm_status2str(status, dlerr));
324785dff7a0SAndy Fiddaman 		}
3248f4b3ec61Sdh 	}
324985dff7a0SAndy Fiddaman 
325085dff7a0SAndy Fiddaman 	free(dllinks);
325185dff7a0SAndy Fiddaman 
325285dff7a0SAndy Fiddaman 	/* Check again and log a message if links remain */
325385dff7a0SAndy Fiddaman 
325485dff7a0SAndy Fiddaman 	if (fetch_zone_datalinks(zlogp, zoneid, &dlnum, NULL) != 0)
3255f4b3ec61Sdh 		return (-1);
325685dff7a0SAndy Fiddaman 
325785dff7a0SAndy Fiddaman 	if (dlnum == 0)
325885dff7a0SAndy Fiddaman 		return (0);
325985dff7a0SAndy Fiddaman 
326085dff7a0SAndy Fiddaman 	zerror(zlogp, B_FALSE, "%d datalink(s) remain in zone after shutdown",
326185dff7a0SAndy Fiddaman 	    dlnum);
326285dff7a0SAndy Fiddaman 
326385dff7a0SAndy Fiddaman 	return (-1);
3264f4b3ec61Sdh }
3265f4b3ec61Sdh 
32667c478bd9Sstevel@tonic-gate static int
tcp_abort_conn(zlog_t * zlogp,zoneid_t zoneid,const struct sockaddr_storage * local,const struct sockaddr_storage * remote)32677c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
32687c478bd9Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
32697c478bd9Sstevel@tonic-gate {
32707c478bd9Sstevel@tonic-gate 	int fd;
32717c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
32727c478bd9Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
32737c478bd9Sstevel@tonic-gate 	int error;
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 	conn.ac_local = *local;
32767c478bd9Sstevel@tonic-gate 	conn.ac_remote = *remote;
32777c478bd9Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
32787c478bd9Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
32797c478bd9Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
32827c478bd9Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
32837c478bd9Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
32847c478bd9Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
32877c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
32887c478bd9Sstevel@tonic-gate 		return (-1);
32897c478bd9Sstevel@tonic-gate 	}
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
32927c478bd9Sstevel@tonic-gate 	(void) close(fd);
32937c478bd9Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
32947c478bd9Sstevel@tonic-gate 		return (0);
32957c478bd9Sstevel@tonic-gate 	return (-1);
32967c478bd9Sstevel@tonic-gate }
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate static int
tcp_abort_connections(zlog_t * zlogp,zoneid_t zoneid)32997c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
33007c478bd9Sstevel@tonic-gate {
33017c478bd9Sstevel@tonic-gate 	struct sockaddr_storage l, r;
33027c478bd9Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
33037c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
33047c478bd9Sstevel@tonic-gate 	int error;
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	/*
33077c478bd9Sstevel@tonic-gate 	 * Abort IPv4 connections.
33087c478bd9Sstevel@tonic-gate 	 */
33097c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
33107c478bd9Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
33117c478bd9Sstevel@tonic-gate 	local->sin_family = AF_INET;
33127c478bd9Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
33137c478bd9Sstevel@tonic-gate 	local->sin_port = 0;
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
33167c478bd9Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
33177c478bd9Sstevel@tonic-gate 	remote->sin_family = AF_INET;
33187c478bd9Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
33197c478bd9Sstevel@tonic-gate 	remote->sin_port = 0;
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
33227c478bd9Sstevel@tonic-gate 		return (error);
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 	/*
33257c478bd9Sstevel@tonic-gate 	 * Abort IPv6 connections.
33267c478bd9Sstevel@tonic-gate 	 */
33277c478bd9Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
33287c478bd9Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
33297c478bd9Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
33307c478bd9Sstevel@tonic-gate 	local6->sin6_port = 0;
33317c478bd9Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
33347c478bd9Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
33357c478bd9Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
33367c478bd9Sstevel@tonic-gate 	remote6->sin6_port = 0;
33377c478bd9Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
33407c478bd9Sstevel@tonic-gate 		return (error);
33417c478bd9Sstevel@tonic-gate 	return (0);
33427c478bd9Sstevel@tonic-gate }
33437c478bd9Sstevel@tonic-gate 
3344ffbafc53Scomay static int
get_privset(zlog_t * zlogp,priv_set_t * privs,zone_mnt_t mount_cmd)33456cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
3346ffbafc53Scomay {
3347ffbafc53Scomay 	int error = -1;
3348ffbafc53Scomay 	zone_dochandle_t handle;
3349ffbafc53Scomay 	char *privname = NULL;
3350ffbafc53Scomay 
3351ffbafc53Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
3352ffbafc53Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3353ffbafc53Scomay 		return (-1);
3354ffbafc53Scomay 	}
3355ffbafc53Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3356ffbafc53Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
3357ffbafc53Scomay 		zonecfg_fini_handle(handle);
3358ffbafc53Scomay 		return (-1);
3359ffbafc53Scomay 	}
3360ffbafc53Scomay 
33616cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
3362bf1d7e28Sdh 		zone_iptype_t	iptype;
3363bf1d7e28Sdh 		const char	*curr_iptype;
3364bf1d7e28Sdh 
3365bf1d7e28Sdh 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
3366bf1d7e28Sdh 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
3367bf1d7e28Sdh 			zonecfg_fini_handle(handle);
3368bf1d7e28Sdh 			return (-1);
3369bf1d7e28Sdh 		}
3370bf1d7e28Sdh 
3371bf1d7e28Sdh 		switch (iptype) {
3372bf1d7e28Sdh 		case ZS_SHARED:
3373bf1d7e28Sdh 			curr_iptype = "shared";
3374bf1d7e28Sdh 			break;
3375bf1d7e28Sdh 		case ZS_EXCLUSIVE:
3376bf1d7e28Sdh 			curr_iptype = "exclusive";
3377bf1d7e28Sdh 			break;
33782a22bccaSToomas Soome 		default:
33792a22bccaSToomas Soome 			zerror(zlogp, B_FALSE, "bad ip-type");
33802a22bccaSToomas Soome 			zonecfg_fini_handle(handle);
33812a22bccaSToomas Soome 			return (-1);
3382bf1d7e28Sdh 		}
3383bf1d7e28Sdh 
3384e767a340Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
3385e767a340Sgjelinek 			zonecfg_fini_handle(handle);
33869acbbeafSnn 			return (0);
3387e767a340Sgjelinek 		}
33889acbbeafSnn 		zerror(zlogp, B_FALSE,
33899acbbeafSnn 		    "failed to determine the zone's default privilege set");
33909acbbeafSnn 		zonecfg_fini_handle(handle);
33919acbbeafSnn 		return (-1);
33929acbbeafSnn 	}
33939acbbeafSnn 
3394ffbafc53Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
3395ffbafc53Scomay 	case Z_OK:
3396ffbafc53Scomay 		error = 0;
3397ffbafc53Scomay 		break;
3398ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3399ffbafc53Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3400ffbafc53Scomay 		    "within the zone's privilege set", privname);
3401ffbafc53Scomay 		break;
3402ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3403ffbafc53Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3404ffbafc53Scomay 		    "from the zone's privilege set", privname);
3405ffbafc53Scomay 		break;
3406ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3407ffbafc53Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3408ffbafc53Scomay 		    "in the zone's privilege set", privname);
3409ffbafc53Scomay 		break;
3410ffbafc53Scomay 	default:
3411ffbafc53Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
3412ffbafc53Scomay 		    "privilege set");
3413ffbafc53Scomay 		break;
3414ffbafc53Scomay 	}
3415ffbafc53Scomay 
3416ffbafc53Scomay 	free(privname);
3417ffbafc53Scomay 	zonecfg_fini_handle(handle);
3418ffbafc53Scomay 	return (error);
3419ffbafc53Scomay }
3420ffbafc53Scomay 
34217c478bd9Sstevel@tonic-gate static int
get_rctls(zlog_t * zlogp,char ** bufp,size_t * bufsizep)34227c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
34237c478bd9Sstevel@tonic-gate {
34247c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
34257c478bd9Sstevel@tonic-gate 	char *nvl_packed = NULL;
34267c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
34277c478bd9Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
34287c478bd9Sstevel@tonic-gate 	int rctlcount = 0;
34297c478bd9Sstevel@tonic-gate 	int error = -1;
34307c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
34317c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
34327c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
3433ff19e029SMenno Lageman 	uint64_t maxlwps;
3434ff19e029SMenno Lageman 	uint64_t maxprocs;
34353c2328bfSJerry Jelinek 	int rproc, rlwp;
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	*bufp = NULL;
34387c478bd9Sstevel@tonic-gate 	*bufsizep = 0;
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
34417c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
34427c478bd9Sstevel@tonic-gate 		return (-1);
34437c478bd9Sstevel@tonic-gate 	}
34447c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
34457c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
34467c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
34477c478bd9Sstevel@tonic-gate 		return (-1);
34487c478bd9Sstevel@tonic-gate 	}
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
34517c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
34527c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
34537c478bd9Sstevel@tonic-gate 		goto out;
34547c478bd9Sstevel@tonic-gate 	}
34557c478bd9Sstevel@tonic-gate 
3456ff19e029SMenno Lageman 	/*
3457ff19e029SMenno Lageman 	 * Allow the administrator to control both the maximum number of
34583c2328bfSJerry Jelinek 	 * process table slots, and the maximum number of lwps, with a single
34593c2328bfSJerry Jelinek 	 * max-processes or max-lwps property. If only the max-processes
34603c2328bfSJerry Jelinek 	 * property is set, we add a max-lwps property with a limit derived
34613c2328bfSJerry Jelinek 	 * from max-processes. If only the max-lwps property is set, we add a
34623c2328bfSJerry Jelinek 	 * max-processes property with the same limit as max-lwps.
3463ff19e029SMenno Lageman 	 */
34643c2328bfSJerry Jelinek 	rproc = zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs);
34653c2328bfSJerry Jelinek 	rlwp = zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps);
34663c2328bfSJerry Jelinek 	if (rproc == Z_OK && rlwp == Z_NO_ENTRY) {
3467ff19e029SMenno Lageman 		if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
3468ff19e029SMenno Lageman 		    maxprocs * LWPS_PER_PROCESS) != Z_OK) {
3469ff19e029SMenno Lageman 			zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
3470ff19e029SMenno Lageman 			goto out;
3471ff19e029SMenno Lageman 		}
34723c2328bfSJerry Jelinek 	} else if (rlwp == Z_OK && rproc == Z_NO_ENTRY) {
34733c2328bfSJerry Jelinek 		/* no scaling for max-proc value */
34743c2328bfSJerry Jelinek 		if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXPROCS,
34753c2328bfSJerry Jelinek 		    maxlwps) != Z_OK) {
34763c2328bfSJerry Jelinek 			zerror(zlogp, B_FALSE,
34773c2328bfSJerry Jelinek 			    "unable to set max-processes alias");
34783c2328bfSJerry Jelinek 			goto out;
34793c2328bfSJerry Jelinek 		}
3480ff19e029SMenno Lageman 	}
3481ff19e029SMenno Lageman 
34827c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
34837c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
34847c478bd9Sstevel@tonic-gate 		goto out;
34857c478bd9Sstevel@tonic-gate 	}
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
34887c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
34897c478bd9Sstevel@tonic-gate 		goto out;
34907c478bd9Sstevel@tonic-gate 	}
34917c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
34927c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
34937c478bd9Sstevel@tonic-gate 		uint_t i, count;
34947c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
34977c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
34987c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34997c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
35007c478bd9Sstevel@tonic-gate 			continue;
35017c478bd9Sstevel@tonic-gate 		}
35027c478bd9Sstevel@tonic-gate 		count = 0;
35037c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
35047c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
35057c478bd9Sstevel@tonic-gate 			count++;
35067c478bd9Sstevel@tonic-gate 		}
35077c478bd9Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
35087c478bd9Sstevel@tonic-gate 			continue;	/* Nothing to free */
35097c478bd9Sstevel@tonic-gate 		}
35107c478bd9Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
35117c478bd9Sstevel@tonic-gate 			goto out;
35127c478bd9Sstevel@tonic-gate 		i = 0;
35137c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
35147c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
35157c478bd9Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
35167c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
35177c478bd9Sstevel@tonic-gate 				    "nvlist_alloc");
35187c478bd9Sstevel@tonic-gate 				goto out;
35197c478bd9Sstevel@tonic-gate 			}
35207c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
35217c478bd9Sstevel@tonic-gate 			    != Z_OK) {
35227c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
35237c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
35247c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
35257c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
35267c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
35277c478bd9Sstevel@tonic-gate 				goto out;
35287c478bd9Sstevel@tonic-gate 			}
35297c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
35307c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
35317c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
35327c478bd9Sstevel@tonic-gate 				    "valid value for rctl '%s'",
35337c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
35347c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
35357c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
35367c478bd9Sstevel@tonic-gate 				    name);
35377c478bd9Sstevel@tonic-gate 				goto out;
35387c478bd9Sstevel@tonic-gate 			}
35397c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
3540ffbafc53Scomay 			    rctlblk_get_privilege(rctlblk)) != 0) {
35417c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
35427c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
35437c478bd9Sstevel@tonic-gate 				goto out;
35447c478bd9Sstevel@tonic-gate 			}
35457c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
3546ffbafc53Scomay 			    rctlblk_get_value(rctlblk)) != 0) {
35477c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
35487c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
35497c478bd9Sstevel@tonic-gate 				goto out;
35507c478bd9Sstevel@tonic-gate 			}
35517c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
35527c478bd9Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
35537c478bd9Sstevel@tonic-gate 			    != 0) {
35547c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
35557c478bd9Sstevel@tonic-gate 				    "nvlist_add_uint64");
35567c478bd9Sstevel@tonic-gate 				goto out;
35577c478bd9Sstevel@tonic-gate 			}
35587c478bd9Sstevel@tonic-gate 		}
35597c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
35607c478bd9Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
35617c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
35627c478bd9Sstevel@tonic-gate 		    != 0) {
35637c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
35647c478bd9Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
35657c478bd9Sstevel@tonic-gate 			goto out;
35667c478bd9Sstevel@tonic-gate 		}
35677c478bd9Sstevel@tonic-gate 		for (i = 0; i < count; i++)
35687c478bd9Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
35697c478bd9Sstevel@tonic-gate 		free(nvlv);
35707c478bd9Sstevel@tonic-gate 		nvlv = NULL;
35717c478bd9Sstevel@tonic-gate 		rctlcount++;
35727c478bd9Sstevel@tonic-gate 	}
35737c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 	if (rctlcount == 0) {
35767c478bd9Sstevel@tonic-gate 		error = 0;
35777c478bd9Sstevel@tonic-gate 		goto out;
35787c478bd9Sstevel@tonic-gate 	}
35797c478bd9Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
35807c478bd9Sstevel@tonic-gate 	    != 0) {
35817c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
35827c478bd9Sstevel@tonic-gate 		goto out;
35837c478bd9Sstevel@tonic-gate 	}
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 	error = 0;
35867c478bd9Sstevel@tonic-gate 	*bufp = nvl_packed;
35877c478bd9Sstevel@tonic-gate 	*bufsizep = nvl_size;
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate out:
35907c478bd9Sstevel@tonic-gate 	free(rctlblk);
35917c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
35927c478bd9Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
35937c478bd9Sstevel@tonic-gate 		free(nvl_packed);
3594aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvl);
35957c478bd9Sstevel@tonic-gate 	if (nvlv != NULL)
35967c478bd9Sstevel@tonic-gate 		free(nvlv);
35977c478bd9Sstevel@tonic-gate 	if (handle != NULL)
35987c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
35997c478bd9Sstevel@tonic-gate 	return (error);
36007c478bd9Sstevel@tonic-gate }
36017c478bd9Sstevel@tonic-gate 
3602c5cd6260S static int
get_implicit_datasets(zlog_t * zlogp,char ** retstr)3603c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr)
3604c5cd6260S {
3605c5cd6260S 	char cmdbuf[2 * MAXPATHLEN];
3606c5cd6260S 
3607c5cd6260S 	if (query_hook[0] == '\0')
3608c5cd6260S 		return (0);
3609c5cd6260S 
3610c5cd6260S 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3611c5cd6260S 	    > sizeof (cmdbuf))
3612c5cd6260S 		return (-1);
3613c5cd6260S 
3614c5cd6260S 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3615c5cd6260S 		return (-1);
3616c5cd6260S 
3617c5cd6260S 	return (0);
3618c5cd6260S }
3619c5cd6260S 
3620fa9e4066Sahrens static int
get_datasets(zlog_t * zlogp,char ** bufp,size_t * bufsizep)3621fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3622fa9e4066Sahrens {
3623fa9e4066Sahrens 	zone_dochandle_t handle;
3624fa9e4066Sahrens 	struct zone_dstab dstab;
3625fa9e4066Sahrens 	size_t total, offset, len;
3626fa9e4066Sahrens 	int error = -1;
3627e5a17f6aSgjelinek 	char *str = NULL;
3628c5cd6260S 	char *implicit_datasets = NULL;
3629c5cd6260S 	int implicit_len = 0;
3630fa9e4066Sahrens 
3631fa9e4066Sahrens 	*bufp = NULL;
3632fa9e4066Sahrens 	*bufsizep = 0;
3633fa9e4066Sahrens 
3634fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3635fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3636fa9e4066Sahrens 		return (-1);
3637fa9e4066Sahrens 	}
3638fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3639fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3640fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3641fa9e4066Sahrens 		return (-1);
3642fa9e4066Sahrens 	}
3643fa9e4066Sahrens 
3644c5cd6260S 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3645c5cd6260S 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3646c5cd6260S 		goto out;
3647c5cd6260S 	}
3648c5cd6260S 
3649fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3650fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3651fa9e4066Sahrens 		goto out;
3652fa9e4066Sahrens 	}
3653fa9e4066Sahrens 
3654fa9e4066Sahrens 	total = 0;
3655fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3656fa9e4066Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3657fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3658fa9e4066Sahrens 
3659c5cd6260S 	if (implicit_datasets != NULL)
3660c5cd6260S 		implicit_len = strlen(implicit_datasets);
3661c5cd6260S 	if (implicit_len > 0)
3662c5cd6260S 		total += implicit_len + 1;
3663c5cd6260S 
3664fa9e4066Sahrens 	if (total == 0) {
3665fa9e4066Sahrens 		error = 0;
3666fa9e4066Sahrens 		goto out;
3667fa9e4066Sahrens 	}
3668fa9e4066Sahrens 
3669fa9e4066Sahrens 	if ((str = malloc(total)) == NULL) {
3670fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3671fa9e4066Sahrens 		goto out;
3672fa9e4066Sahrens 	}
3673fa9e4066Sahrens 
3674fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3675fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3676fa9e4066Sahrens 		goto out;
3677fa9e4066Sahrens 	}
3678fa9e4066Sahrens 	offset = 0;
3679fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3680fa9e4066Sahrens 		len = strlen(dstab.zone_dataset_name);
3681fa9e4066Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
3682e5a17f6aSgjelinek 		    total - offset);
3683fa9e4066Sahrens 		offset += len;
3684e5a17f6aSgjelinek 		if (offset < total - 1)
3685fa9e4066Sahrens 			str[offset++] = ',';
3686fa9e4066Sahrens 	}
3687fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3688fa9e4066Sahrens 
3689c5cd6260S 	if (implicit_len > 0)
3690c5cd6260S 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
3691c5cd6260S 
3692fa9e4066Sahrens 	error = 0;
3693fa9e4066Sahrens 	*bufp = str;
3694fa9e4066Sahrens 	*bufsizep = total;
3695fa9e4066Sahrens 
3696fa9e4066Sahrens out:
3697fa9e4066Sahrens 	if (error != 0 && str != NULL)
3698fa9e4066Sahrens 		free(str);
3699fa9e4066Sahrens 	if (handle != NULL)
3700fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3701c5cd6260S 	if (implicit_datasets != NULL)
3702c5cd6260S 		free(implicit_datasets);
3703fa9e4066Sahrens 
3704fa9e4066Sahrens 	return (error);
3705fa9e4066Sahrens }
3706fa9e4066Sahrens 
3707fa9e4066Sahrens static int
validate_datasets(zlog_t * zlogp)3708fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3709fa9e4066Sahrens {
3710fa9e4066Sahrens 	zone_dochandle_t handle;
3711fa9e4066Sahrens 	struct zone_dstab dstab;
3712fa9e4066Sahrens 	zfs_handle_t *zhp;
371399653d4eSeschrock 	libzfs_handle_t *hdl;
3714fa9e4066Sahrens 
3715fa9e4066Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3716fa9e4066Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3717fa9e4066Sahrens 		return (-1);
3718fa9e4066Sahrens 	}
3719fa9e4066Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3720fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3721fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3722fa9e4066Sahrens 		return (-1);
3723fa9e4066Sahrens 	}
3724fa9e4066Sahrens 
3725fa9e4066Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3726fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3727fa9e4066Sahrens 		zonecfg_fini_handle(handle);
3728fa9e4066Sahrens 		return (-1);
3729fa9e4066Sahrens 	}
3730fa9e4066Sahrens 
373199653d4eSeschrock 	if ((hdl = libzfs_init()) == NULL) {
373299653d4eSeschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
373399653d4eSeschrock 		zonecfg_fini_handle(handle);
373499653d4eSeschrock 		return (-1);
373599653d4eSeschrock 	}
3736fa9e4066Sahrens 
3737fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3738fa9e4066Sahrens 
373999653d4eSeschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3740fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3741fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3742fa9e4066Sahrens 			    dstab.zone_dataset_name);
3743fa9e4066Sahrens 			zonecfg_fini_handle(handle);
374499653d4eSeschrock 			libzfs_fini(hdl);
3745fa9e4066Sahrens 			return (-1);
3746fa9e4066Sahrens 		}
3747fa9e4066Sahrens 
3748fa9e4066Sahrens 		/*
3749fa9e4066Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3750fa9e4066Sahrens 		 * first because we'll get EPERM if it is already set.
3751fa9e4066Sahrens 		 */
3752fa9e4066Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3753e9dbad6fSeschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3754e9dbad6fSeschrock 		    "on") != 0) {
3755fa9e4066Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3756fa9e4066Sahrens 			    "property for ZFS dataset '%s'\n",
3757fa9e4066Sahrens 			    dstab.zone_dataset_name);
3758fa9e4066Sahrens 			zonecfg_fini_handle(handle);
3759fa9e4066Sahrens 			zfs_close(zhp);
376099653d4eSeschrock 			libzfs_fini(hdl);
3761fa9e4066Sahrens 			return (-1);
3762fa9e4066Sahrens 		}
3763fa9e4066Sahrens 
3764fa9e4066Sahrens 		zfs_close(zhp);
3765fa9e4066Sahrens 	}
3766fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
3767fa9e4066Sahrens 
3768fa9e4066Sahrens 	zonecfg_fini_handle(handle);
376999653d4eSeschrock 	libzfs_fini(hdl);
3770fa9e4066Sahrens 
3771fa9e4066Sahrens 	return (0);
3772fa9e4066Sahrens }
3773fa9e4066Sahrens 
37744201a95eSRic Aleshire /*
37754201a95eSRic Aleshire  * Return true if the path is its own zfs file system.  We determine this
37764201a95eSRic Aleshire  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
37774201a95eSRic Aleshire  * if it is a different fs.
37784201a95eSRic Aleshire  */
37794201a95eSRic Aleshire boolean_t
is_zonepath_zfs(char * zonepath)37804201a95eSRic Aleshire is_zonepath_zfs(char *zonepath)
37814201a95eSRic Aleshire {
37824201a95eSRic Aleshire 	int res;
37834201a95eSRic Aleshire 	char *path;
37844201a95eSRic Aleshire 	char *parent;
37854201a95eSRic Aleshire 	struct statvfs64 buf1, buf2;
37864201a95eSRic Aleshire 
37874201a95eSRic Aleshire 	if (statvfs64(zonepath, &buf1) != 0)
37884201a95eSRic Aleshire 		return (B_FALSE);
37894201a95eSRic Aleshire 
37904201a95eSRic Aleshire 	if (strcmp(buf1.f_basetype, "zfs") != 0)
37914201a95eSRic Aleshire 		return (B_FALSE);
37924201a95eSRic Aleshire 
37934201a95eSRic Aleshire 	if ((path = strdup(zonepath)) == NULL)
37944201a95eSRic Aleshire 		return (B_FALSE);
37954201a95eSRic Aleshire 
37964201a95eSRic Aleshire 	parent = dirname(path);
37974201a95eSRic Aleshire 	res = statvfs64(parent, &buf2);
37984201a95eSRic Aleshire 	free(path);
37994201a95eSRic Aleshire 
38004201a95eSRic Aleshire 	if (res != 0)
38014201a95eSRic Aleshire 		return (B_FALSE);
38024201a95eSRic Aleshire 
38034201a95eSRic Aleshire 	if (buf1.f_fsid == buf2.f_fsid)
38044201a95eSRic Aleshire 		return (B_FALSE);
38054201a95eSRic Aleshire 
38064201a95eSRic Aleshire 	return (B_TRUE);
38074201a95eSRic Aleshire }
38084201a95eSRic Aleshire 
38094201a95eSRic Aleshire /*
38104201a95eSRic Aleshire  * Verify the MAC label in the root dataset for the zone.
38114201a95eSRic Aleshire  * If the label exists, it must match the label configured for the zone.
38124201a95eSRic Aleshire  * Otherwise if there's no label on the dataset, create one here.
38134201a95eSRic Aleshire  */
38144201a95eSRic Aleshire 
38154201a95eSRic Aleshire static int
validate_rootds_label(zlog_t * zlogp,char * rootpath,m_label_t * zone_sl)38164201a95eSRic Aleshire validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
38174201a95eSRic Aleshire {
38184201a95eSRic Aleshire 	int		error = -1;
38194201a95eSRic Aleshire 	zfs_handle_t	*zhp;
38204201a95eSRic Aleshire 	libzfs_handle_t	*hdl;
38214201a95eSRic Aleshire 	m_label_t	ds_sl;
38224201a95eSRic Aleshire 	char		zonepath[MAXPATHLEN];
38234201a95eSRic Aleshire 	char		ds_hexsl[MAXNAMELEN];
38244201a95eSRic Aleshire 
38254201a95eSRic Aleshire 	if (!is_system_labeled())
38264201a95eSRic Aleshire 		return (0);
38274201a95eSRic Aleshire 
38284201a95eSRic Aleshire 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
38294201a95eSRic Aleshire 		zerror(zlogp, B_TRUE, "unable to determine zone path");
38304201a95eSRic Aleshire 		return (-1);
38314201a95eSRic Aleshire 	}
38324201a95eSRic Aleshire 
38334201a95eSRic Aleshire 	if (!is_zonepath_zfs(zonepath))
38344201a95eSRic Aleshire 		return (0);
38354201a95eSRic Aleshire 
38364201a95eSRic Aleshire 	if ((hdl = libzfs_init()) == NULL) {
38374201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "opening ZFS library");
38384201a95eSRic Aleshire 		return (-1);
38394201a95eSRic Aleshire 	}
38404201a95eSRic Aleshire 
38414201a95eSRic Aleshire 	if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
38424201a95eSRic Aleshire 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
38434201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
38444201a95eSRic Aleshire 		    rootpath);
38454201a95eSRic Aleshire 		libzfs_fini(hdl);
38464201a95eSRic Aleshire 		return (-1);
38474201a95eSRic Aleshire 	}
38484201a95eSRic Aleshire 
38494201a95eSRic Aleshire 	/* Get the mlslabel property if it exists. */
38504201a95eSRic Aleshire 	if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
38514201a95eSRic Aleshire 	    NULL, NULL, 0, B_TRUE) != 0) ||
38524201a95eSRic Aleshire 	    (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
38534201a95eSRic Aleshire 		char		*str2 = NULL;
38544201a95eSRic Aleshire 
38554201a95eSRic Aleshire 		/*
38564201a95eSRic Aleshire 		 * No label on the dataset (or default only); create one.
38574201a95eSRic Aleshire 		 * (Only do this automatic labeling for the labeled brand.)
38584201a95eSRic Aleshire 		 */
38594201a95eSRic Aleshire 		if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
38604201a95eSRic Aleshire 			error = 0;
38614201a95eSRic Aleshire 			goto out;
38624201a95eSRic Aleshire 		}
38634201a95eSRic Aleshire 
38644201a95eSRic Aleshire 		error = l_to_str_internal(zone_sl, &str2);
38654201a95eSRic Aleshire 		if (error)
38664201a95eSRic Aleshire 			goto out;
38674201a95eSRic Aleshire 		if (str2 == NULL) {
38684201a95eSRic Aleshire 			error = -1;
38694201a95eSRic Aleshire 			goto out;
38704201a95eSRic Aleshire 		}
38714201a95eSRic Aleshire 		if ((error = zfs_prop_set(zhp,
38724201a95eSRic Aleshire 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
38734201a95eSRic Aleshire 			zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
38744201a95eSRic Aleshire 			    "property for root dataset at '%s'\n", rootpath);
38754201a95eSRic Aleshire 		}
38764201a95eSRic Aleshire 		free(str2);
38774201a95eSRic Aleshire 		goto out;
38784201a95eSRic Aleshire 	}
38794201a95eSRic Aleshire 
38804201a95eSRic Aleshire 	/* Convert the retrieved dataset label to binary form. */
38814201a95eSRic Aleshire 	error = hexstr_to_label(ds_hexsl, &ds_sl);
38824201a95eSRic Aleshire 	if (error) {
38834201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
38844201a95eSRic Aleshire 		    "property on root dataset at '%s'\n", rootpath);
38854201a95eSRic Aleshire 		goto out;			/* exit with error */
38864201a95eSRic Aleshire 	}
38874201a95eSRic Aleshire 
38884201a95eSRic Aleshire 	/*
38894201a95eSRic Aleshire 	 * Perform a MAC check by comparing the zone label with the
38904201a95eSRic Aleshire 	 * dataset label.
38914201a95eSRic Aleshire 	 */
38924201a95eSRic Aleshire 	error = (!blequal(zone_sl, &ds_sl));
38934201a95eSRic Aleshire 	if (error)
38944201a95eSRic Aleshire 		zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
38954201a95eSRic Aleshire out:
38964201a95eSRic Aleshire 	zfs_close(zhp);
38974201a95eSRic Aleshire 	libzfs_fini(hdl);
38984201a95eSRic Aleshire 
38994201a95eSRic Aleshire 	return (error);
39004201a95eSRic Aleshire }
39014201a95eSRic Aleshire 
390245916cd2Sjpk /*
390345916cd2Sjpk  * Mount lower level home directories into/from current zone
390445916cd2Sjpk  * Share exported directories specified in dfstab for zone
390545916cd2Sjpk  */
390645916cd2Sjpk static int
tsol_mounts(zlog_t * zlogp,char * zone_name,char * rootpath)390745916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
390845916cd2Sjpk {
390945916cd2Sjpk 	zoneid_t *zids = NULL;
391045916cd2Sjpk 	priv_set_t *zid_privs;
391145916cd2Sjpk 	const priv_impl_info_t *ip = NULL;
391245916cd2Sjpk 	uint_t nzents_saved;
391345916cd2Sjpk 	uint_t nzents;
391445916cd2Sjpk 	int i;
391545916cd2Sjpk 	char readonly[] = "ro";
391645916cd2Sjpk 	struct zone_fstab lower_fstab;
391745916cd2Sjpk 	char *argv[4];
391845916cd2Sjpk 
391945916cd2Sjpk 	if (!is_system_labeled())
392045916cd2Sjpk 		return (0);
392145916cd2Sjpk 
392245916cd2Sjpk 	if (zid_label == NULL) {
392345916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
392445916cd2Sjpk 		if (zid_label == NULL)
392545916cd2Sjpk 			return (-1);
392645916cd2Sjpk 	}
392745916cd2Sjpk 
392845916cd2Sjpk 	/* Make sure our zone has an /export/home dir */
392945916cd2Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
393027e6fb21Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
393145916cd2Sjpk 
393245916cd2Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
393345916cd2Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
393445916cd2Sjpk 	    sizeof (lower_fstab.zone_fs_type));
393545916cd2Sjpk 	lower_fstab.zone_fs_options = NULL;
393645916cd2Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
393745916cd2Sjpk 
393845916cd2Sjpk 	/*
393945916cd2Sjpk 	 * Get the list of zones from the kernel
394045916cd2Sjpk 	 */
394145916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
394245916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
394345916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
394445916cd2Sjpk 		return (-1);
394545916cd2Sjpk 	}
394645916cd2Sjpk again:
394745916cd2Sjpk 	if (nzents == 0) {
394845916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
394945916cd2Sjpk 		return (-1);
395045916cd2Sjpk 	}
395145916cd2Sjpk 
395245916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
395345916cd2Sjpk 	if (zids == NULL) {
39543f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
395545916cd2Sjpk 		return (-1);
395645916cd2Sjpk 	}
395745916cd2Sjpk 	nzents_saved = nzents;
395845916cd2Sjpk 
395945916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
396045916cd2Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
396145916cd2Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
396245916cd2Sjpk 		free(zids);
396345916cd2Sjpk 		return (-1);
396445916cd2Sjpk 	}
396545916cd2Sjpk 	if (nzents != nzents_saved) {
396645916cd2Sjpk 		/* list changed, try again */
396745916cd2Sjpk 		free(zids);
396845916cd2Sjpk 		goto again;
396945916cd2Sjpk 	}
397045916cd2Sjpk 
397145916cd2Sjpk 	ip = getprivimplinfo();
397245916cd2Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
397345916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
397445916cd2Sjpk 		zonecfg_free_fs_option_list(
397545916cd2Sjpk 		    lower_fstab.zone_fs_options);
397645916cd2Sjpk 		free(zids);
397745916cd2Sjpk 		return (-1);
397845916cd2Sjpk 	}
397945916cd2Sjpk 
398045916cd2Sjpk 	for (i = 0; i < nzents; i++) {
398145916cd2Sjpk 		char zid_name[ZONENAME_MAX];
398245916cd2Sjpk 		zone_state_t zid_state;
398345916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
398445916cd2Sjpk 		struct stat stat_buf;
398545916cd2Sjpk 
398645916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
398745916cd2Sjpk 			continue;
398845916cd2Sjpk 
398945916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
399045916cd2Sjpk 			continue;
399145916cd2Sjpk 
399245916cd2Sjpk 		/*
399345916cd2Sjpk 		 * Do special setup for the zone we are booting
399445916cd2Sjpk 		 */
399545916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
399645916cd2Sjpk 			struct zone_fstab autofs_fstab;
399745916cd2Sjpk 			char map_path[MAXPATHLEN];
399845916cd2Sjpk 			int fd;
399945916cd2Sjpk 
400045916cd2Sjpk 			/*
400145916cd2Sjpk 			 * Create auto_home_<zone> map for this zone
40029acbbeafSnn 			 * in the global zone. The non-global zone entry
400345916cd2Sjpk 			 * will be created by automount when the zone
400445916cd2Sjpk 			 * is booted.
400545916cd2Sjpk 			 */
400645916cd2Sjpk 
400745916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
400845916cd2Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
400945916cd2Sjpk 
401045916cd2Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
401145916cd2Sjpk 			    "/zone/%s/home", zid_name);
401245916cd2Sjpk 
401345916cd2Sjpk 			(void) snprintf(map_path, sizeof (map_path),
401445916cd2Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
401545916cd2Sjpk 			/*
401645916cd2Sjpk 			 * If the map file doesn't exist create a template
401745916cd2Sjpk 			 */
401845916cd2Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
401945916cd2Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
402045916cd2Sjpk 				int len;
402145916cd2Sjpk 				char map_rec[MAXPATHLEN];
402245916cd2Sjpk 
402345916cd2Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
402445916cd2Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
402545916cd2Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
402645916cd2Sjpk 				(void) write(fd, map_rec, len);
402745916cd2Sjpk 				(void) close(fd);
402845916cd2Sjpk 			}
402945916cd2Sjpk 
403045916cd2Sjpk 			/*
403145916cd2Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
403245916cd2Sjpk 			 * If it's already of type autofs, then
403345916cd2Sjpk 			 * don't mount it again.
403445916cd2Sjpk 			 */
403545916cd2Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
403645916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
403745916cd2Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
403845916cd2Sjpk 
403945916cd2Sjpk 				(void) make_one_dir(zlogp, "",
404027e6fb21Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
404127e6fb21Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
404245916cd2Sjpk 
404345916cd2Sjpk 				/*
404445916cd2Sjpk 				 * Mount will fail if automounter has already
404545916cd2Sjpk 				 * processed the auto_home_<zonename> map
404645916cd2Sjpk 				 */
404745916cd2Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
404845916cd2Sjpk 				    autofs_fstab.zone_fs_special,
404945916cd2Sjpk 				    autofs_fstab.zone_fs_dir);
405045916cd2Sjpk 			}
405145916cd2Sjpk 			continue;
405245916cd2Sjpk 		}
405345916cd2Sjpk 
405445916cd2Sjpk 
405545916cd2Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
405648451833Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
405748451833Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
405845916cd2Sjpk 			/* Skip over zones without mounted filesystems */
405945916cd2Sjpk 			continue;
406045916cd2Sjpk 
406145916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
406245916cd2Sjpk 		    sizeof (m_label_t)) < 0)
406345916cd2Sjpk 			/* Skip over zones with unspecified label */
406445916cd2Sjpk 			continue;
406545916cd2Sjpk 
406645916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
406745916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
406845916cd2Sjpk 			/* Skip over zones with bad path */
406945916cd2Sjpk 			continue;
407045916cd2Sjpk 
407145916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
407245916cd2Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
407345916cd2Sjpk 			/* Skip over zones with bad privs */
407445916cd2Sjpk 			continue;
407545916cd2Sjpk 
407645916cd2Sjpk 		/*
407745916cd2Sjpk 		 * Reading down is valid according to our label model
407845916cd2Sjpk 		 * but some customers want to disable it because it
407945916cd2Sjpk 		 * allows execute down and other possible attacks.
408045916cd2Sjpk 		 * Therefore, we restrict this feature to zones that
408145916cd2Sjpk 		 * have the NET_MAC_AWARE privilege which is required
408245916cd2Sjpk 		 * for NFS read-down semantics.
408345916cd2Sjpk 		 */
408445916cd2Sjpk 		if ((bldominates(zlabel, zid_label)) &&
408545916cd2Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
408645916cd2Sjpk 			/*
408745916cd2Sjpk 			 * Our zone dominates this one.
408845916cd2Sjpk 			 * Create a lofs mount from lower zone's /export/home
408945916cd2Sjpk 			 */
409045916cd2Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
409145916cd2Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
409245916cd2Sjpk 
409345916cd2Sjpk 			/*
409445916cd2Sjpk 			 * If the target is already an LOFS mount
409545916cd2Sjpk 			 * then don't do it again.
409645916cd2Sjpk 			 */
409745916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
409845916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
409945916cd2Sjpk 
410045916cd2Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
410145916cd2Sjpk 				    MAXPATHLEN, "%s/export",
410245916cd2Sjpk 				    zid_rpath) > MAXPATHLEN)
410345916cd2Sjpk 					continue;
410445916cd2Sjpk 
410545916cd2Sjpk 				/*
410645916cd2Sjpk 				 * Make sure the lower-level home exists
410745916cd2Sjpk 				 */
410845916cd2Sjpk 				if (make_one_dir(zlogp,
410927e6fb21Sdp 				    lower_fstab.zone_fs_special, "/home",
411027e6fb21Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
411127e6fb21Sdp 				    DEFAULT_DIR_GROUP) != 0)
411245916cd2Sjpk 					continue;
411345916cd2Sjpk 
411445916cd2Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
411545916cd2Sjpk 				    "/home", MAXPATHLEN);
411645916cd2Sjpk 
411745916cd2Sjpk 				/*
411845916cd2Sjpk 				 * Mount can fail because the lower-level
411945916cd2Sjpk 				 * zone may have already done a mount up.
412045916cd2Sjpk 				 */
41210679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
41220679f794S 				    Z_MNT_BOOT);
412345916cd2Sjpk 			}
412445916cd2Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
412545916cd2Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
412645916cd2Sjpk 			/*
412745916cd2Sjpk 			 * This zone dominates our zone.
412845916cd2Sjpk 			 * Create a lofs mount from our zone's /export/home
412945916cd2Sjpk 			 */
413045916cd2Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
413145916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
413245916cd2Sjpk 			    zone_name) > MAXPATHLEN)
413345916cd2Sjpk 				continue;
413445916cd2Sjpk 
413545916cd2Sjpk 			/*
413645916cd2Sjpk 			 * If the target is already an LOFS mount
413745916cd2Sjpk 			 * then don't do it again.
413845916cd2Sjpk 			 */
413945916cd2Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
414045916cd2Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
414145916cd2Sjpk 
414245916cd2Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
414345916cd2Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
414445916cd2Sjpk 
414545916cd2Sjpk 				/*
414645916cd2Sjpk 				 * Mount can fail because the higher-level
414745916cd2Sjpk 				 * zone may have already done a mount down.
414845916cd2Sjpk 				 */
41490679f794S 				(void) mount_one(zlogp, &lower_fstab, "",
41500679f794S 				    Z_MNT_BOOT);
415145916cd2Sjpk 			}
415245916cd2Sjpk 		}
415345916cd2Sjpk 	}
415445916cd2Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
415545916cd2Sjpk 	priv_freeset(zid_privs);
415645916cd2Sjpk 	free(zids);
415745916cd2Sjpk 
415845916cd2Sjpk 	/*
415945916cd2Sjpk 	 * Now share any exported directories from this zone.
416045916cd2Sjpk 	 * Each zone can have its own dfstab.
416145916cd2Sjpk 	 */
416245916cd2Sjpk 
416345916cd2Sjpk 	argv[0] = "zoneshare";
416445916cd2Sjpk 	argv[1] = "-z";
416545916cd2Sjpk 	argv[2] = zone_name;
416645916cd2Sjpk 	argv[3] = NULL;
416745916cd2Sjpk 
416845916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
416945916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
417045916cd2Sjpk 
417145916cd2Sjpk 	return (0);
417245916cd2Sjpk }
417345916cd2Sjpk 
417445916cd2Sjpk /*
417545916cd2Sjpk  * Unmount lofs mounts from higher level zones
417645916cd2Sjpk  * Unshare nfs exported directories
417745916cd2Sjpk  */
417845916cd2Sjpk static void
tsol_unmounts(zlog_t * zlogp,char * zone_name)417945916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
418045916cd2Sjpk {
418145916cd2Sjpk 	zoneid_t *zids = NULL;
418245916cd2Sjpk 	uint_t nzents_saved;
418345916cd2Sjpk 	uint_t nzents;
418445916cd2Sjpk 	int i;
418545916cd2Sjpk 	char *argv[4];
418645916cd2Sjpk 	char path[MAXPATHLEN];
418745916cd2Sjpk 
418845916cd2Sjpk 	if (!is_system_labeled())
418945916cd2Sjpk 		return;
419045916cd2Sjpk 
419145916cd2Sjpk 	/*
419245916cd2Sjpk 	 * Get the list of zones from the kernel
419345916cd2Sjpk 	 */
419445916cd2Sjpk 	if (zone_list(NULL, &nzents) != 0) {
419545916cd2Sjpk 		return;
419645916cd2Sjpk 	}
419745916cd2Sjpk 
419845916cd2Sjpk 	if (zid_label == NULL) {
419945916cd2Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
420045916cd2Sjpk 		if (zid_label == NULL)
420145916cd2Sjpk 			return;
420245916cd2Sjpk 	}
420345916cd2Sjpk 
420445916cd2Sjpk again:
420545916cd2Sjpk 	if (nzents == 0)
420645916cd2Sjpk 		return;
420745916cd2Sjpk 
420845916cd2Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
420945916cd2Sjpk 	if (zids == NULL) {
42103f2f09c1Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
421145916cd2Sjpk 		return;
421245916cd2Sjpk 	}
421345916cd2Sjpk 	nzents_saved = nzents;
421445916cd2Sjpk 
421545916cd2Sjpk 	if (zone_list(zids, &nzents) != 0) {
421645916cd2Sjpk 		free(zids);
421745916cd2Sjpk 		return;
421845916cd2Sjpk 	}
421945916cd2Sjpk 	if (nzents != nzents_saved) {
422045916cd2Sjpk 		/* list changed, try again */
422145916cd2Sjpk 		free(zids);
422245916cd2Sjpk 		goto again;
422345916cd2Sjpk 	}
422445916cd2Sjpk 
422545916cd2Sjpk 	for (i = 0; i < nzents; i++) {
422645916cd2Sjpk 		char zid_name[ZONENAME_MAX];
422745916cd2Sjpk 		zone_state_t zid_state;
422845916cd2Sjpk 		char zid_rpath[MAXPATHLEN];
422945916cd2Sjpk 
423045916cd2Sjpk 		if (zids[i] == GLOBAL_ZONEID)
423145916cd2Sjpk 			continue;
423245916cd2Sjpk 
423345916cd2Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
423445916cd2Sjpk 			continue;
423545916cd2Sjpk 
423645916cd2Sjpk 		/*
423745916cd2Sjpk 		 * Skip the zone we are halting
423845916cd2Sjpk 		 */
423945916cd2Sjpk 		if (strcmp(zid_name, zone_name) == 0)
424045916cd2Sjpk 			continue;
424145916cd2Sjpk 
424245916cd2Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
424345916cd2Sjpk 		    sizeof (zid_state)) < 0) ||
424445916cd2Sjpk 		    (zid_state < ZONE_IS_READY))
424545916cd2Sjpk 			/* Skip over zones without mounted filesystems */
424645916cd2Sjpk 			continue;
424745916cd2Sjpk 
424845916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
424945916cd2Sjpk 		    sizeof (m_label_t)) < 0)
425045916cd2Sjpk 			/* Skip over zones with unspecified label */
425145916cd2Sjpk 			continue;
425245916cd2Sjpk 
425345916cd2Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
425445916cd2Sjpk 		    sizeof (zid_rpath)) == -1)
425545916cd2Sjpk 			/* Skip over zones with bad path */
425645916cd2Sjpk 			continue;
425745916cd2Sjpk 
425845916cd2Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
425945916cd2Sjpk 			/*
426045916cd2Sjpk 			 * This zone dominates our zone.
426145916cd2Sjpk 			 * Unmount the lofs mount of our zone's /export/home
426245916cd2Sjpk 			 */
426345916cd2Sjpk 
426445916cd2Sjpk 			if (snprintf(path, MAXPATHLEN,
426545916cd2Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
426645916cd2Sjpk 			    zone_name) > MAXPATHLEN)
426745916cd2Sjpk 				continue;
426845916cd2Sjpk 
426945916cd2Sjpk 			/* Skip over mount failures */
427045916cd2Sjpk 			(void) umount(path);
427145916cd2Sjpk 		}
427245916cd2Sjpk 	}
427345916cd2Sjpk 	free(zids);
427445916cd2Sjpk 
427545916cd2Sjpk 	/*
427645916cd2Sjpk 	 * Unmount global zone autofs trigger for this zone
427745916cd2Sjpk 	 */
427845916cd2Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
427945916cd2Sjpk 	/* Skip over mount failures */
428045916cd2Sjpk 	(void) umount(path);
428145916cd2Sjpk 
428245916cd2Sjpk 	/*
428345916cd2Sjpk 	 * Next unshare any exported directories from this zone.
428445916cd2Sjpk 	 */
428545916cd2Sjpk 
428645916cd2Sjpk 	argv[0] = "zoneunshare";
428745916cd2Sjpk 	argv[1] = "-z";
428845916cd2Sjpk 	argv[2] = zone_name;
428945916cd2Sjpk 	argv[3] = NULL;
429045916cd2Sjpk 
429145916cd2Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
429245916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
429345916cd2Sjpk 
429445916cd2Sjpk 	/*
429545916cd2Sjpk 	 * Finally, deallocate any devices in the zone.
429645916cd2Sjpk 	 */
429745916cd2Sjpk 
429845916cd2Sjpk 	argv[0] = "deallocate";
429945916cd2Sjpk 	argv[1] = "-Isz";
430045916cd2Sjpk 	argv[2] = zone_name;
430145916cd2Sjpk 	argv[3] = NULL;
430245916cd2Sjpk 
430345916cd2Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
430445916cd2Sjpk 	/* Don't check for errors since they don't affect the zone */
430545916cd2Sjpk }
430645916cd2Sjpk 
430745916cd2Sjpk /*
430845916cd2Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
430945916cd2Sjpk  * this zone.
431045916cd2Sjpk  */
431145916cd2Sjpk static tsol_zcent_t *
get_zone_label(zlog_t * zlogp,priv_set_t * privs)431245916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
431345916cd2Sjpk {
431445916cd2Sjpk 	FILE *fp;
431545916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
431645916cd2Sjpk 	char line[MAXTNZLEN];
431745916cd2Sjpk 
431845916cd2Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
431945916cd2Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
432045916cd2Sjpk 		return (NULL);
432145916cd2Sjpk 	}
432245916cd2Sjpk 
432345916cd2Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
432445916cd2Sjpk 		/*
432545916cd2Sjpk 		 * Check for malformed database
432645916cd2Sjpk 		 */
432745916cd2Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
432845916cd2Sjpk 			break;
432945916cd2Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
433045916cd2Sjpk 			continue;
433145916cd2Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
433245916cd2Sjpk 			break;
433345916cd2Sjpk 		tsol_freezcent(zcent);
433445916cd2Sjpk 		zcent = NULL;
433545916cd2Sjpk 	}
433645916cd2Sjpk 	(void) fclose(fp);
433745916cd2Sjpk 
433845916cd2Sjpk 	if (zcent == NULL) {
433945916cd2Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
4340bbf21555SRichard Lowe 		    "See tnzonecfg(5)");
434145916cd2Sjpk 	} else {
434245916cd2Sjpk 		if (zlabel == NULL)
434345916cd2Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
434445916cd2Sjpk 		/*
434545916cd2Sjpk 		 * Save this zone's privileges for later read-down processing
434645916cd2Sjpk 		 */
434745916cd2Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
434845916cd2Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
434945916cd2Sjpk 			return (NULL);
435045916cd2Sjpk 		} else {
435145916cd2Sjpk 			priv_copyset(privs, zprivs);
435245916cd2Sjpk 		}
435345916cd2Sjpk 	}
435445916cd2Sjpk 	return (zcent);
435545916cd2Sjpk }
435645916cd2Sjpk 
435745916cd2Sjpk /*
435845916cd2Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
435945916cd2Sjpk  */
436045916cd2Sjpk static void
set_mlps(zlog_t * zlogp,zoneid_t zoneid,tsol_zcent_t * zcent)436145916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
436245916cd2Sjpk {
436345916cd2Sjpk 	tsol_mlp_t *mlp;
436445916cd2Sjpk 	tsol_mlpent_t tsme;
436545916cd2Sjpk 
436645916cd2Sjpk 	if (!is_system_labeled())
436745916cd2Sjpk 		return;
436845916cd2Sjpk 
436945916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
437045916cd2Sjpk 	tsme.tsme_flags = 0;
437145916cd2Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
437245916cd2Sjpk 		tsme.tsme_mlp = *mlp;
437345916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
437445916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
437545916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
437645916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
437745916cd2Sjpk 		}
437845916cd2Sjpk 	}
437945916cd2Sjpk 
438045916cd2Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
438145916cd2Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
438245916cd2Sjpk 		tsme.tsme_mlp = *mlp;
438345916cd2Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
438445916cd2Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
438545916cd2Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
438645916cd2Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
438745916cd2Sjpk 		}
438845916cd2Sjpk 	}
438945916cd2Sjpk }
439045916cd2Sjpk 
439145916cd2Sjpk static void
remove_mlps(zlog_t * zlogp,zoneid_t zoneid)439245916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
439345916cd2Sjpk {
439445916cd2Sjpk 	tsol_mlpent_t tsme;
439545916cd2Sjpk 
439645916cd2Sjpk 	if (!is_system_labeled())
439745916cd2Sjpk 		return;
439845916cd2Sjpk 
439945916cd2Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
440045916cd2Sjpk 	tsme.tsme_zoneid = zoneid;
440145916cd2Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
440245916cd2Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
440345916cd2Sjpk }
440445916cd2Sjpk 
44057c478bd9Sstevel@tonic-gate int
prtmount(const struct mnttab * fs,void * x)44069a686fbcSPaul Dagnelie prtmount(const struct mnttab *fs, void *x)
44079a686fbcSPaul Dagnelie {
44080094b373Sjv 	zerror((zlog_t *)x, B_FALSE, "  %s", fs->mnt_mountp);
44097c478bd9Sstevel@tonic-gate 	return (0);
44107c478bd9Sstevel@tonic-gate }
44117c478bd9Sstevel@tonic-gate 
4412108322fbScarlsonj /*
4413108322fbScarlsonj  * Look for zones running on the main system that are using this root (or any
4414108322fbScarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
4415108322fbScarlsonj  * is found or if we can't tell.
4416108322fbScarlsonj  */
4417108322fbScarlsonj static boolean_t
duplicate_zone_root(zlog_t * zlogp,const char * rootpath)4418108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
44197c478bd9Sstevel@tonic-gate {
4420108322fbScarlsonj 	zoneid_t *zids = NULL;
4421108322fbScarlsonj 	uint_t nzids = 0;
4422108322fbScarlsonj 	boolean_t retv;
4423108322fbScarlsonj 	int rlen, zlen;
4424108322fbScarlsonj 	char zroot[MAXPATHLEN];
4425108322fbScarlsonj 	char zonename[ZONENAME_MAX];
4426108322fbScarlsonj 
4427108322fbScarlsonj 	for (;;) {
4428108322fbScarlsonj 		nzids += 10;
4429108322fbScarlsonj 		zids = malloc(nzids * sizeof (*zids));
4430108322fbScarlsonj 		if (zids == NULL) {
44313f2f09c1Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
4432108322fbScarlsonj 			return (B_TRUE);
4433108322fbScarlsonj 		}
4434108322fbScarlsonj 		if (zone_list(zids, &nzids) == 0)
4435108322fbScarlsonj 			break;
4436108322fbScarlsonj 		free(zids);
4437108322fbScarlsonj 	}
4438108322fbScarlsonj 	retv = B_FALSE;
4439108322fbScarlsonj 	rlen = strlen(rootpath);
4440108322fbScarlsonj 	while (nzids > 0) {
4441108322fbScarlsonj 		/*
4442108322fbScarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
4443108322fbScarlsonj 		 * while we were busy.
4444108322fbScarlsonj 		 */
4445108322fbScarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4446108322fbScarlsonj 		    sizeof (zroot)) == -1)
4447108322fbScarlsonj 			continue;
4448108322fbScarlsonj 		zlen = strlen(zroot);
4449108322fbScarlsonj 		if (zlen > rlen)
4450108322fbScarlsonj 			zlen = rlen;
4451108322fbScarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
4452108322fbScarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4453108322fbScarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4454108322fbScarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
4455108322fbScarlsonj 			    sizeof (zonename)) == -1)
4456108322fbScarlsonj 				(void) snprintf(zonename, sizeof (zonename),
4457108322fbScarlsonj 				    "id %d", (int)zids[nzids]);
4458108322fbScarlsonj 			zerror(zlogp, B_FALSE,
4459108322fbScarlsonj 			    "zone root %s already in use by zone %s",
4460108322fbScarlsonj 			    rootpath, zonename);
4461108322fbScarlsonj 			retv = B_TRUE;
4462108322fbScarlsonj 			break;
4463108322fbScarlsonj 		}
4464108322fbScarlsonj 	}
4465108322fbScarlsonj 	free(zids);
4466108322fbScarlsonj 	return (retv);
4467108322fbScarlsonj }
4468108322fbScarlsonj 
4469108322fbScarlsonj /*
4470108322fbScarlsonj  * Search for loopback mounts that use this same source node (same device and
4471108322fbScarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
4472108322fbScarlsonj  */
4473108322fbScarlsonj static boolean_t
duplicate_reachable_path(zlog_t * zlogp,const char * rootpath)4474108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4475108322fbScarlsonj {
4476108322fbScarlsonj 	struct stat64 rst, zst;
4477108322fbScarlsonj 	struct mnttab *mnp;
4478108322fbScarlsonj 
4479108322fbScarlsonj 	if (stat64(rootpath, &rst) == -1) {
4480108322fbScarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4481108322fbScarlsonj 		return (B_TRUE);
4482108322fbScarlsonj 	}
4483108322fbScarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4484108322fbScarlsonj 		return (B_TRUE);
4485108322fbScarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4486108322fbScarlsonj 		if (mnp->mnt_fstype == NULL ||
4487108322fbScarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4488108322fbScarlsonj 			continue;
4489108322fbScarlsonj 		/* We're looking at a loopback mount.  Stat it. */
4490108322fbScarlsonj 		if (mnp->mnt_special != NULL &&
4491108322fbScarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
4492108322fbScarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4493108322fbScarlsonj 			zerror(zlogp, B_FALSE,
4494108322fbScarlsonj 			    "zone root %s is reachable through %s",
4495108322fbScarlsonj 			    rootpath, mnp->mnt_mountp);
4496108322fbScarlsonj 			return (B_TRUE);
4497108322fbScarlsonj 		}
4498108322fbScarlsonj 	}
4499108322fbScarlsonj 	return (B_FALSE);
4500108322fbScarlsonj }
4501108322fbScarlsonj 
45020209230bSgjelinek /*
45030209230bSgjelinek  * Set memory cap and pool info for the zone's resource management
45040209230bSgjelinek  * configuration.
45050209230bSgjelinek  */
45060209230bSgjelinek static int
setup_zone_rm(zlog_t * zlogp,char * zone_name,zoneid_t zoneid)45070209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
45080209230bSgjelinek {
45090209230bSgjelinek 	int res;
45100209230bSgjelinek 	uint64_t tmp;
45110209230bSgjelinek 	struct zone_mcaptab mcap;
45120209230bSgjelinek 	char sched[MAXNAMELEN];
45130209230bSgjelinek 	zone_dochandle_t handle = NULL;
45140209230bSgjelinek 	char pool_err[128];
45150209230bSgjelinek 
45160209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
45170209230bSgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
45180209230bSgjelinek 		return (Z_BAD_HANDLE);
45190209230bSgjelinek 	}
45200209230bSgjelinek 
45210209230bSgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
45220209230bSgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
45230209230bSgjelinek 		zonecfg_fini_handle(handle);
45240209230bSgjelinek 		return (res);
45250209230bSgjelinek 	}
45260209230bSgjelinek 
45270209230bSgjelinek 	/*
45280209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
45290209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
45300209230bSgjelinek 	 */
45310209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
45320209230bSgjelinek 		uint64_t num;
45330209230bSgjelinek 		char smf_err[128];
45340209230bSgjelinek 
45350209230bSgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
45360209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
45370209230bSgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
45380209230bSgjelinek 			zonecfg_fini_handle(handle);
45390209230bSgjelinek 			return (Z_INVAL);
45400209230bSgjelinek 		}
45410209230bSgjelinek 
45420209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
45430209230bSgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
45440209230bSgjelinek 			    "failed: %s", smf_err);
45450209230bSgjelinek 			zonecfg_fini_handle(handle);
45460209230bSgjelinek 			return (Z_INVAL);
45470209230bSgjelinek 		}
45480209230bSgjelinek 	}
45490209230bSgjelinek 
45500209230bSgjelinek 	/* Get the scheduling class set in the zone configuration. */
45510209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
45520209230bSgjelinek 	    strlen(sched) > 0) {
45530209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
45540209230bSgjelinek 		    strlen(sched)) == -1)
45550209230bSgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
45560209230bSgjelinek 			    "default scheduling class");
45570209230bSgjelinek 
45580209230bSgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
45590209230bSgjelinek 	    == Z_OK) {
45600209230bSgjelinek 		/*
45610209230bSgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
45620209230bSgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
45630209230bSgjelinek 		 * zone.  Check what scheduling class the zone would be running
45640209230bSgjelinek 		 * in by default so we can print a warning and modify the class
45650209230bSgjelinek 		 * if we wouldn't be using FSS.
45660209230bSgjelinek 		 */
45670209230bSgjelinek 		char class_name[PC_CLNMSZ];
45680209230bSgjelinek 
45690209230bSgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
45700209230bSgjelinek 		    sizeof (class_name)) != Z_OK) {
45710209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
45720209230bSgjelinek 			    "the zone's scheduling class");
45730209230bSgjelinek 
45740209230bSgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
45750209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
45760209230bSgjelinek 			    "rctl is set but\nFSS is not the default "
45770209230bSgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
45780209230bSgjelinek 			    "used for processes\nin the zone but to get the "
45790209230bSgjelinek 			    "full benefit of FSS,\nit should be the default "
4580bbf21555SRichard Lowe 			    "scheduling class.\nSee dispadmin(8) for more "
45810209230bSgjelinek 			    "details.");
45820209230bSgjelinek 
45830209230bSgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
45840209230bSgjelinek 			    strlen("FSS")) == -1)
45850209230bSgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
45860209230bSgjelinek 				    "zone scheduling class to FSS");
45870209230bSgjelinek 		}
45880209230bSgjelinek 	}
45890209230bSgjelinek 
45900209230bSgjelinek 	/*
45910209230bSgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
45920209230bSgjelinek 	 * well as persistent pools.  In all cases we call the functions
45930209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
45940209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
45950209230bSgjelinek 	 * and just return if there is nothing to do.
45960209230bSgjelinek 	 *
45970209230bSgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
45980209230bSgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
45990209230bSgjelinek 	 * right thing in all cases (reuse or create) based on the current
46000209230bSgjelinek 	 * zonecfg.
46010209230bSgjelinek 	 */
46020209230bSgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
46030209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
46040209230bSgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
46050209230bSgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
46060209230bSgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
46070209230bSgjelinek 			    pool_err);
46080209230bSgjelinek 		else
46090209230bSgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
46100209230bSgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
46110209230bSgjelinek 		zonecfg_fini_handle(handle);
46120209230bSgjelinek 		return (Z_POOL_BIND);
46130209230bSgjelinek 	}
46140209230bSgjelinek 
46150209230bSgjelinek 	/*
46160209230bSgjelinek 	 * Check if we need to warn about poold not being enabled.
46170209230bSgjelinek 	 */
46180209230bSgjelinek 	if (zonecfg_warn_poold(handle)) {
46190209230bSgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
46200209230bSgjelinek 		    "been specified\nbut the dynamic pool service is not "
46210209230bSgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
46220209230bSgjelinek 		    "processor allocation within the specified range\n"
46230209230bSgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
4624bbf21555SRichard Lowe 		    "See poold(8).");
46250209230bSgjelinek 	}
46260209230bSgjelinek 
46270209230bSgjelinek 	/* The following is a warning, not an error. */
46280209230bSgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
46290209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
46300209230bSgjelinek 		if (res == Z_POOL_BIND)
46310209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
46320209230bSgjelinek 			    "pool '%s'; using default pool.", pool_err);
46330209230bSgjelinek 		else if (res == Z_POOL)
46340209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
46350209230bSgjelinek 			    zonecfg_strerror(res), pool_err);
46360209230bSgjelinek 		else
46370209230bSgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
46380209230bSgjelinek 			    zonecfg_strerror(res));
46390209230bSgjelinek 	}
4640efd4c9b6SSteve Lawrence 
4641efd4c9b6SSteve Lawrence 	/* Update saved pool name in case it has changed */
4642a3002e0aSGarrett D'Amore 	(void) zonecfg_get_poolname(handle, zone_name, pool_name,
4643a3002e0aSGarrett D'Amore 	    sizeof (pool_name));
46440209230bSgjelinek 
46450209230bSgjelinek 	zonecfg_fini_handle(handle);
46460209230bSgjelinek 	return (Z_OK);
46470209230bSgjelinek }
46480209230bSgjelinek 
46490fbb751dSJohn Levon static void
report_prop_err(zlog_t * zlogp,const char * name,const char * value,int res)46500fbb751dSJohn Levon report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
46510fbb751dSJohn Levon {
46520fbb751dSJohn Levon 	switch (res) {
46530fbb751dSJohn Levon 	case Z_TOO_BIG:
46540fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value is too large.", name);
46550fbb751dSJohn Levon 		break;
46560fbb751dSJohn Levon 
46570fbb751dSJohn Levon 	case Z_INVALID_PROPERTY:
46580fbb751dSJohn Levon 		zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
46590fbb751dSJohn Levon 		    name, value);
46600fbb751dSJohn Levon 		break;
46610fbb751dSJohn Levon 
46620fbb751dSJohn Levon 	default:
46630fbb751dSJohn Levon 		zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
46640fbb751dSJohn Levon 		break;
46650fbb751dSJohn Levon 	}
46660fbb751dSJohn Levon }
46670fbb751dSJohn Levon 
46685679c89fSjv /*
46695679c89fSjv  * Sets the hostid of the new zone based on its configured value.  The zone's
46705679c89fSjv  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
46715679c89fSjv  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
46725679c89fSjv  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
46735679c89fSjv  * ID of the new zone.
46745679c89fSjv  *
46755679c89fSjv  * This function returns zero on success and a nonzero error code on failure.
46765679c89fSjv  */
46775679c89fSjv static int
setup_zone_hostid(zone_dochandle_t handle,zlog_t * zlogp,zoneid_t zoneid)46780fbb751dSJohn Levon setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
46795679c89fSjv {
46805679c89fSjv 	int res;
46815679c89fSjv 	char hostidp[HW_HOSTID_LEN];
46825679c89fSjv 	unsigned int hostid;
46835679c89fSjv 
46840fbb751dSJohn Levon 	res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
46850fbb751dSJohn Levon 
46860fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
46870fbb751dSJohn Levon 		return (Z_OK);
46880fbb751dSJohn Levon 	} else if (res != Z_OK) {
46890fbb751dSJohn Levon 		report_prop_err(zlogp, "hostid", hostidp, res);
46900fbb751dSJohn Levon 		return (res);
46910fbb751dSJohn Levon 	}
46920fbb751dSJohn Levon 
46930fbb751dSJohn Levon 	hostid = (unsigned int)strtoul(hostidp, NULL, 16);
46940fbb751dSJohn Levon 	if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
46950fbb751dSJohn Levon 	    sizeof (hostid))) != 0) {
46960fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
46970fbb751dSJohn Levon 		    "zone hostid is not valid: %s: %d", hostidp, res);
46980fbb751dSJohn Levon 		return (Z_SYSTEM);
46990fbb751dSJohn Levon 	}
47000fbb751dSJohn Levon 
47010fbb751dSJohn Levon 	return (res);
47020fbb751dSJohn Levon }
47030fbb751dSJohn Levon 
47045d08dfa0SAndy Fiddaman static int
secflags_parse_check(secflagset_t * flagset,const char * flagstr,char * descr,zlog_t * zlogp)47055d08dfa0SAndy Fiddaman secflags_parse_check(secflagset_t *flagset, const char *flagstr, char *descr,
47065d08dfa0SAndy Fiddaman     zlog_t *zlogp)
47075d08dfa0SAndy Fiddaman {
47085d08dfa0SAndy Fiddaman 	secflagdelta_t delt;
47095d08dfa0SAndy Fiddaman 
47105d08dfa0SAndy Fiddaman 	if (secflags_parse(NULL, flagstr, &delt) == -1) {
47115d08dfa0SAndy Fiddaman 		zerror(zlogp, B_FALSE,
47125d08dfa0SAndy Fiddaman 		    "failed to parse %s security-flags '%s': %s",
47135d08dfa0SAndy Fiddaman 		    descr, flagstr, strerror(errno));
47145d08dfa0SAndy Fiddaman 		return (Z_BAD_PROPERTY);
47155d08dfa0SAndy Fiddaman 	}
47165d08dfa0SAndy Fiddaman 
47175d08dfa0SAndy Fiddaman 	if (delt.psd_ass_active != B_TRUE) {
47185d08dfa0SAndy Fiddaman 		zerror(zlogp, B_FALSE,
47195d08dfa0SAndy Fiddaman 		    "relative security-flags are not allowed "
47205d08dfa0SAndy Fiddaman 		    "(%s security-flags: '%s')", descr, flagstr);
47215d08dfa0SAndy Fiddaman 		return (Z_BAD_PROPERTY);
47225d08dfa0SAndy Fiddaman 	}
47235d08dfa0SAndy Fiddaman 
47245d08dfa0SAndy Fiddaman 	secflags_copy(flagset, &delt.psd_assign);
47255d08dfa0SAndy Fiddaman 
47265d08dfa0SAndy Fiddaman 	return (Z_OK);
47275d08dfa0SAndy Fiddaman }
47285d08dfa0SAndy Fiddaman 
4729d2a70789SRichard Lowe static int
setup_zone_secflags(zone_dochandle_t handle,zlog_t * zlogp,zoneid_t zoneid)4730d2a70789SRichard Lowe setup_zone_secflags(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4731d2a70789SRichard Lowe {
4732d2a70789SRichard Lowe 	psecflags_t secflags;
4733d2a70789SRichard Lowe 	struct zone_secflagstab tab = {0};
47345d08dfa0SAndy Fiddaman 	secflagset_t flagset;
4735d2a70789SRichard Lowe 	int res;
4736d2a70789SRichard Lowe 
4737d2a70789SRichard Lowe 	res = zonecfg_lookup_secflags(handle, &tab);
4738d2a70789SRichard Lowe 
47395d08dfa0SAndy Fiddaman 	/*
47405d08dfa0SAndy Fiddaman 	 * If the zone configuration does not define any security flag sets,
47415d08dfa0SAndy Fiddaman 	 * then check to see if there are any default flags configured for
47425d08dfa0SAndy Fiddaman 	 * the brand. If so, set these as the default set for this zone and
47435d08dfa0SAndy Fiddaman 	 * the lower/upper sets will become none/all as per the defaults.
47445d08dfa0SAndy Fiddaman 	 *
47455d08dfa0SAndy Fiddaman 	 * If there is no brand default either, then the flags will be
47465d08dfa0SAndy Fiddaman 	 * defaulted below.
47475d08dfa0SAndy Fiddaman 	 */
47485d08dfa0SAndy Fiddaman 	if (res == Z_NO_ENTRY) {
47495d08dfa0SAndy Fiddaman 		char flagstr[ZONECFG_SECFLAGS_MAX];
47505d08dfa0SAndy Fiddaman 		brand_handle_t bh = NULL;
47515d08dfa0SAndy Fiddaman 
47525d08dfa0SAndy Fiddaman 		if ((bh = brand_open(brand_name)) == NULL) {
47535d08dfa0SAndy Fiddaman 			zerror(zlogp, B_FALSE,
47545d08dfa0SAndy Fiddaman 			    "unable to find brand named %s", brand_name);
47555d08dfa0SAndy Fiddaman 			return (Z_BAD_PROPERTY);
47565d08dfa0SAndy Fiddaman 		}
47575d08dfa0SAndy Fiddaman 		if (brand_get_secflags(bh, flagstr, sizeof (flagstr)) != 0) {
47585d08dfa0SAndy Fiddaman 			brand_close(bh);
47595d08dfa0SAndy Fiddaman 			zerror(zlogp, B_FALSE,
47605d08dfa0SAndy Fiddaman 			    "unable to retrieve brand default security flags");
47615d08dfa0SAndy Fiddaman 			return (Z_BAD_PROPERTY);
47625d08dfa0SAndy Fiddaman 		}
47635d08dfa0SAndy Fiddaman 		brand_close(bh);
47645d08dfa0SAndy Fiddaman 
47655d08dfa0SAndy Fiddaman 		if (*flagstr != '\0' &&
47665d08dfa0SAndy Fiddaman 		    strlcpy(tab.zone_secflags_default, flagstr,
47675d08dfa0SAndy Fiddaman 		    sizeof (tab.zone_secflags_default)) >=
47685d08dfa0SAndy Fiddaman 		    sizeof (tab.zone_secflags_default)) {
47695d08dfa0SAndy Fiddaman 			zerror(zlogp, B_FALSE,
47705d08dfa0SAndy Fiddaman 			    "brand default security-flags is too long");
47715d08dfa0SAndy Fiddaman 			return (Z_BAD_PROPERTY);
47725d08dfa0SAndy Fiddaman 		}
47735d08dfa0SAndy Fiddaman 	} else if (res != Z_OK) {
47745d08dfa0SAndy Fiddaman 		zerror(zlogp, B_FALSE,
47755d08dfa0SAndy Fiddaman 		    "security-flags property is invalid: %d", res);
4776d2a70789SRichard Lowe 		return (res);
4777d2a70789SRichard Lowe 	}
4778d2a70789SRichard Lowe 
47795d08dfa0SAndy Fiddaman 	if (strlen(tab.zone_secflags_lower) == 0) {
4780d2a70789SRichard Lowe 		(void) strlcpy(tab.zone_secflags_lower, "none",
4781d2a70789SRichard Lowe 		    sizeof (tab.zone_secflags_lower));
47825d08dfa0SAndy Fiddaman 	}
47835d08dfa0SAndy Fiddaman 	if (strlen(tab.zone_secflags_default) == 0) {
4784d2a70789SRichard Lowe 		(void) strlcpy(tab.zone_secflags_default,
4785d2a70789SRichard Lowe 		    tab.zone_secflags_lower,
4786d2a70789SRichard Lowe 		    sizeof (tab.zone_secflags_default));
47875d08dfa0SAndy Fiddaman 	}
47885d08dfa0SAndy Fiddaman 	if (strlen(tab.zone_secflags_upper) == 0) {
4789d2a70789SRichard Lowe 		(void) strlcpy(tab.zone_secflags_upper, "all",
4790d2a70789SRichard Lowe 		    sizeof (tab.zone_secflags_upper));
47915d08dfa0SAndy Fiddaman 	}
4792d2a70789SRichard Lowe 
47935d08dfa0SAndy Fiddaman 	if ((res = secflags_parse_check(&flagset, tab.zone_secflags_default,
47945d08dfa0SAndy Fiddaman 	    "default", zlogp)) != Z_OK) {
47955d08dfa0SAndy Fiddaman 		return (res);
4796d2a70789SRichard Lowe 	} else {
47975d08dfa0SAndy Fiddaman 		secflags_copy(&secflags.psf_inherit, &flagset);
47985d08dfa0SAndy Fiddaman 		secflags_copy(&secflags.psf_effective, &flagset);
4799d2a70789SRichard Lowe 	}
4800d2a70789SRichard Lowe 
48015d08dfa0SAndy Fiddaman 	if ((res = secflags_parse_check(&flagset, tab.zone_secflags_lower,
48025d08dfa0SAndy Fiddaman 	    "lower", zlogp)) != Z_OK) {
48035d08dfa0SAndy Fiddaman 		return (res);
4804d2a70789SRichard Lowe 	} else {
48055d08dfa0SAndy Fiddaman 		secflags_copy(&secflags.psf_lower, &flagset);
4806d2a70789SRichard Lowe 	}
4807d2a70789SRichard Lowe 
48085d08dfa0SAndy Fiddaman 	if ((res = secflags_parse_check(&flagset, tab.zone_secflags_upper,
48095d08dfa0SAndy Fiddaman 	    "upper", zlogp)) != Z_OK) {
48105d08dfa0SAndy Fiddaman 		return (res);
4811d2a70789SRichard Lowe 	} else {
48125d08dfa0SAndy Fiddaman 		secflags_copy(&secflags.psf_upper, &flagset);
4813d2a70789SRichard Lowe 	}
4814d2a70789SRichard Lowe 
4815d2a70789SRichard Lowe 	if (!psecflags_validate(&secflags)) {
4816d2a70789SRichard Lowe 		zerror(zlogp, B_TRUE, "security-flags violate invariants");
4817d2a70789SRichard Lowe 		return (Z_BAD_PROPERTY);
4818d2a70789SRichard Lowe 	}
4819d2a70789SRichard Lowe 
4820d2a70789SRichard Lowe 	if ((res = zone_setattr(zoneid, ZONE_ATTR_SECFLAGS, &secflags,
4821d2a70789SRichard Lowe 	    sizeof (secflags))) != 0) {
4822d2a70789SRichard Lowe 		zerror(zlogp, B_TRUE,
4823d2a70789SRichard Lowe 		    "security-flags couldn't be set: %d", res);
4824d2a70789SRichard Lowe 		return (Z_SYSTEM);
4825d2a70789SRichard Lowe 	}
4826d2a70789SRichard Lowe 
4827d2a70789SRichard Lowe 	return (Z_OK);
4828d2a70789SRichard Lowe }
4829d2a70789SRichard Lowe 
48300fbb751dSJohn Levon static int
setup_zone_fs_allowed(zone_dochandle_t handle,zlog_t * zlogp,zoneid_t zoneid)48310fbb751dSJohn Levon setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
48320fbb751dSJohn Levon {
48338cd81a20SJerry Jelinek 	char fsallowed[ZONE_FS_ALLOWED_MAX];
48348cd81a20SJerry Jelinek 	char *fsallowedp = fsallowed;
48358cd81a20SJerry Jelinek 	int len = sizeof (fsallowed);
48360fbb751dSJohn Levon 	int res;
48370fbb751dSJohn Levon 
48388cd81a20SJerry Jelinek 	res = zonecfg_get_fs_allowed(handle, fsallowed, len);
48390fbb751dSJohn Levon 
48400fbb751dSJohn Levon 	if (res == Z_BAD_PROPERTY) {
48418cd81a20SJerry Jelinek 		/* No value, set the defaults */
48428cd81a20SJerry Jelinek 		(void) strlcpy(fsallowed, DFLT_FS_ALLOWED, len);
48430fbb751dSJohn Levon 	} else if (res != Z_OK) {
48448cd81a20SJerry Jelinek 		report_prop_err(zlogp, "fs-allowed", fsallowed, res);
48450fbb751dSJohn Levon 		return (res);
48468cd81a20SJerry Jelinek 	} else if (fsallowed[0] == '-') {
4847d2a70789SRichard Lowe 		/* dropping default filesystems - use remaining list */
48488cd81a20SJerry Jelinek 		if (fsallowed[1] != ',')
48498cd81a20SJerry Jelinek 			return (Z_OK);
48508cd81a20SJerry Jelinek 		fsallowedp += 2;
48518cd81a20SJerry Jelinek 		len -= 2;
48528cd81a20SJerry Jelinek 	} else {
48538cd81a20SJerry Jelinek 		/* Has a value, append the defaults */
48548cd81a20SJerry Jelinek 		if (strlcat(fsallowed, ",", len) >= len ||
48558cd81a20SJerry Jelinek 		    strlcat(fsallowed, DFLT_FS_ALLOWED, len) >= len) {
48568cd81a20SJerry Jelinek 			report_prop_err(zlogp, "fs-allowed", fsallowed,
48578cd81a20SJerry Jelinek 			    Z_TOO_BIG);
48588cd81a20SJerry Jelinek 			return (Z_TOO_BIG);
48598cd81a20SJerry Jelinek 		}
48600fbb751dSJohn Levon 	}
48610fbb751dSJohn Levon 
48628cd81a20SJerry Jelinek 	if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, fsallowedp, len) != 0) {
48630fbb751dSJohn Levon 		zerror(zlogp, B_TRUE,
48640fbb751dSJohn Levon 		    "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
48650fbb751dSJohn Levon 		return (Z_SYSTEM);
48660fbb751dSJohn Levon 	}
48670fbb751dSJohn Levon 
48688cd81a20SJerry Jelinek 	return (Z_OK);
48690fbb751dSJohn Levon }
48700fbb751dSJohn Levon 
48710fbb751dSJohn Levon static int
setup_zone_attrs(zlog_t * zlogp,char * zone_namep,zoneid_t zoneid)48720fbb751dSJohn Levon setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
48730fbb751dSJohn Levon {
48740fbb751dSJohn Levon 	zone_dochandle_t handle;
48750fbb751dSJohn Levon 	int res = Z_OK;
48760fbb751dSJohn Levon 
48775679c89fSjv 	if ((handle = zonecfg_init_handle()) == NULL) {
48785679c89fSjv 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
48795679c89fSjv 		return (Z_BAD_HANDLE);
48805679c89fSjv 	}
48815679c89fSjv 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
48825679c89fSjv 		zerror(zlogp, B_FALSE, "invalid configuration");
48830fbb751dSJohn Levon 		goto out;
48845679c89fSjv 	}
48855679c89fSjv 
48860fbb751dSJohn Levon 	if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
48870fbb751dSJohn Levon 		goto out;
48885679c89fSjv 
48890fbb751dSJohn Levon 	if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
48900fbb751dSJohn Levon 		goto out;
48910fbb751dSJohn Levon 
4892d2a70789SRichard Lowe 	if ((res = setup_zone_secflags(handle, zlogp, zoneid)) != Z_OK)
4893d2a70789SRichard Lowe 		goto out;
4894d2a70789SRichard Lowe 
48950fbb751dSJohn Levon out:
48965679c89fSjv 	zonecfg_fini_handle(handle);
48970fbb751dSJohn Levon 	return (res);
48985679c89fSjv }
48995679c89fSjv 
4900108322fbScarlsonj zoneid_t
vplat_create(zlog_t * zlogp,zone_mnt_t mount_cmd)49016cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4902108322fbScarlsonj {
4903108322fbScarlsonj 	zoneid_t rval = -1;
49047c478bd9Sstevel@tonic-gate 	priv_set_t *privs;
49057c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
49067c478bd9Sstevel@tonic-gate 	char *rctlbuf = NULL;
4907108322fbScarlsonj 	size_t rctlbufsz = 0;
4908fa9e4066Sahrens 	char *zfsbuf = NULL;
4909fa9e4066Sahrens 	size_t zfsbufsz = 0;
4910108322fbScarlsonj 	zoneid_t zoneid = -1;
49117c478bd9Sstevel@tonic-gate 	int xerr;
4912108322fbScarlsonj 	char *kzone;
4913108322fbScarlsonj 	FILE *fp = NULL;
491445916cd2Sjpk 	tsol_zcent_t *zcent = NULL;
491545916cd2Sjpk 	int match = 0;
491645916cd2Sjpk 	int doi = 0;
4917f4b3ec61Sdh 	int flags;
4918f4b3ec61Sdh 	zone_iptype_t iptype;
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
49217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
49227c478bd9Sstevel@tonic-gate 		return (-1);
49237c478bd9Sstevel@tonic-gate 	}
4924108322fbScarlsonj 	if (zonecfg_in_alt_root())
4925108322fbScarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
49267c478bd9Sstevel@tonic-gate 
49272b24ab6bSSebastien Roy 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
4928f4b3ec61Sdh 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
4929f4b3ec61Sdh 		return (-1);
4930f4b3ec61Sdh 	}
49312a22bccaSToomas Soome 	if (iptype == ZS_EXCLUSIVE) {
4932f4b3ec61Sdh 		flags = ZCF_NET_EXCL;
49332a22bccaSToomas Soome 	} else {
49342a22bccaSToomas Soome 		flags = 0;
4935f4b3ec61Sdh 	}
4936f4b3ec61Sdh 
49377c478bd9Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
49387c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
49397c478bd9Sstevel@tonic-gate 		return (-1);
49407c478bd9Sstevel@tonic-gate 	}
49417c478bd9Sstevel@tonic-gate 	priv_emptyset(privs);
4942ffbafc53Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
49437c478bd9Sstevel@tonic-gate 		goto error;
4944ffbafc53Scomay 
49456cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
49466cfd72c6Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
49477c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
49487c478bd9Sstevel@tonic-gate 		goto error;
49497c478bd9Sstevel@tonic-gate 	}
4950ffbafc53Scomay 
4951fa9e4066Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4952fa9e4066Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4953fa9e4066Sahrens 		goto error;
4954fa9e4066Sahrens 	}
49557c478bd9Sstevel@tonic-gate 
49566cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
495745916cd2Sjpk 		zcent = get_zone_label(zlogp, privs);
495848451833Scarlsonj 		if (zcent != NULL) {
495945916cd2Sjpk 			match = zcent->zc_match;
496045916cd2Sjpk 			doi = zcent->zc_doi;
496145916cd2Sjpk 			*zlabel = zcent->zc_label;
496245916cd2Sjpk 		} else {
496345916cd2Sjpk 			goto error;
496445916cd2Sjpk 		}
49654201a95eSRic Aleshire 		if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
49664201a95eSRic Aleshire 			goto error;
496745916cd2Sjpk 	}
496845916cd2Sjpk 
4969108322fbScarlsonj 	kzone = zone_name;
4970108322fbScarlsonj 
4971108322fbScarlsonj 	/*
4972108322fbScarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4973108322fbScarlsonj 	 * main system that are using this root (or any subdirectory of it).
4974108322fbScarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4975108322fbScarlsonj 	 * that use this same source node (same device and inode).
4976108322fbScarlsonj 	 */
4977108322fbScarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4978108322fbScarlsonj 		goto error;
4979108322fbScarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4980108322fbScarlsonj 		goto error;
4981108322fbScarlsonj 
49826cfd72c6Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4983108322fbScarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4984108322fbScarlsonj 
4985108322fbScarlsonj 		/*
4986108322fbScarlsonj 		 * Forge up a special root for this zone.  When a zone is
4987108322fbScarlsonj 		 * mounted, we can't let the zone have its own root because the
4988108322fbScarlsonj 		 * tools that will be used in this "scratch zone" need access
4989108322fbScarlsonj 		 * to both the zone's resources and the running machine's
4990108322fbScarlsonj 		 * executables.
4991108322fbScarlsonj 		 *
4992108322fbScarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4993108322fbScarlsonj 		 */
4994108322fbScarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4995108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4996108322fbScarlsonj 			goto error;
4997108322fbScarlsonj 		}
4998108322fbScarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4999108322fbScarlsonj 			goto error;
5000108322fbScarlsonj 	}
5001108322fbScarlsonj 
5002108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5003108322fbScarlsonj 		/*
5004108322fbScarlsonj 		 * If we are mounting up a zone in an alternate root partition,
5005108322fbScarlsonj 		 * then we have some additional work to do before starting the
5006108322fbScarlsonj 		 * zone.  First, resolve the root path down so that we're not
5007108322fbScarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
5008108322fbScarlsonj 		 * the zone.
5009108322fbScarlsonj 		 */
5010108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
5011108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
5012108322fbScarlsonj 			goto error;
5013108322fbScarlsonj 		}
5014108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
5015108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
5016108322fbScarlsonj 			goto error;
5017108322fbScarlsonj 		}
5018108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5019108322fbScarlsonj 		    NULL, 0) == 0) {
5020108322fbScarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
5021108322fbScarlsonj 			goto error;
5022108322fbScarlsonj 		}
5023108322fbScarlsonj 		/* This is the preferred name */
5024108322fbScarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
5025108322fbScarlsonj 		    zone_name);
5026108322fbScarlsonj 		srandom(getpid());
5027108322fbScarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
5028108322fbScarlsonj 		    0) == 0) {
5029108322fbScarlsonj 			/* This is just an arbitrary name; note "." usage */
5030108322fbScarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
5031108322fbScarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
5032108322fbScarlsonj 		}
5033108322fbScarlsonj 		kzone = kernzone;
5034108322fbScarlsonj 	}
5035108322fbScarlsonj 
50367c478bd9Sstevel@tonic-gate 	xerr = 0;
5037108322fbScarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
5038f4b3ec61Sdh 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
5039f4b3ec61Sdh 	    flags)) == -1) {
50407c478bd9Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
50417c478bd9Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
50427c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
50437c478bd9Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
50447c478bd9Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
50457c478bd9Sstevel@tonic-gate 			} else {
50467c478bd9Sstevel@tonic-gate 
50477c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
50487c478bd9Sstevel@tonic-gate 				    "These file-systems are mounted on "
50497c478bd9Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
50507c478bd9Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
50517c478bd9Sstevel@tonic-gate 				    prtmount, zlogp);
50527c478bd9Sstevel@tonic-gate 			}
50537c478bd9Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
50547c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
50557c478bd9Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
50567c478bd9Sstevel@tonic-gate 			    "environment", "zone_create");
50578c55461bSton 		} else if (xerr == ZE_LABELINUSE) {
50588c55461bSton 			char zonename[ZONENAME_MAX];
50598c55461bSton 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
50608c55461bSton 			    zonename, ZONENAME_MAX);
50618c55461bSton 			zerror(zlogp, B_FALSE, "The zone label is already "
50628c55461bSton 			    "used by the zone '%s'.", zonename);
50637c478bd9Sstevel@tonic-gate 		} else {
50647c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
50657c478bd9Sstevel@tonic-gate 		}
50667c478bd9Sstevel@tonic-gate 		goto error;
50677c478bd9Sstevel@tonic-gate 	}
5068108322fbScarlsonj 
5069108322fbScarlsonj 	if (zonecfg_in_alt_root() &&
5070108322fbScarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
5071108322fbScarlsonj 	    zonecfg_get_root()) == -1) {
5072108322fbScarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
5073108322fbScarlsonj 		goto error;
5074108322fbScarlsonj 	}
5075108322fbScarlsonj 
50769acbbeafSnn 	/*
50770679f794S 	 * The following actions are not performed when merely mounting a zone
50780679f794S 	 * for administrative use.
50799acbbeafSnn 	 */
50800679f794S 	if (mount_cmd == Z_MNT_BOOT) {
50810679f794S 		brand_handle_t bh;
50820679f794S 		struct brand_attr attr;
50830679f794S 		char modname[MAXPATHLEN];
50840679f794S 
50850fbb751dSJohn Levon 		if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
50865679c89fSjv 			goto error;
50875679c89fSjv 
508862868012SSteve Lawrence 		if ((bh = brand_open(brand_name)) == NULL) {
50890679f794S 			zerror(zlogp, B_FALSE,
50900679f794S 			    "unable to determine brand name");
50915679c89fSjv 			goto error;
50920679f794S 		}
50930679f794S 
50949a5d73e0SRic Aleshire 		if (!is_system_labeled() &&
509562868012SSteve Lawrence 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
50969a5d73e0SRic Aleshire 			brand_close(bh);
50979a5d73e0SRic Aleshire 			zerror(zlogp, B_FALSE,
50989a5d73e0SRic Aleshire 			    "cannot boot labeled zone on unlabeled system");
50999a5d73e0SRic Aleshire 			goto error;
51009a5d73e0SRic Aleshire 		}
51019a5d73e0SRic Aleshire 
51020679f794S 		/*
51030679f794S 		 * If this brand requires any kernel support, now is the time to
51040679f794S 		 * get it loaded and initialized.
51050679f794S 		 */
51060679f794S 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
51070679f794S 			brand_close(bh);
51080679f794S 			zerror(zlogp, B_FALSE,
51090679f794S 			    "unable to determine brand kernel module");
51105679c89fSjv 			goto error;
51110679f794S 		}
5112e767a340Sgjelinek 		brand_close(bh);
51139acbbeafSnn 
51140679f794S 		if (strlen(modname) > 0) {
511562868012SSteve Lawrence 			(void) strlcpy(attr.ba_brandname, brand_name,
511662868012SSteve Lawrence 			    sizeof (attr.ba_brandname));
511762868012SSteve Lawrence 			(void) strlcpy(attr.ba_modname, modname,
511862868012SSteve Lawrence 			    sizeof (attr.ba_modname));
51190679f794S 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
51200679f794S 			    sizeof (attr) != 0)) {
51210679f794S 				zerror(zlogp, B_TRUE,
51220679f794S 				    "could not set zone brand attribute.");
51230679f794S 				goto error;
51240679f794S 			}
51259acbbeafSnn 		}
51269acbbeafSnn 
51275679c89fSjv 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
51280209230bSgjelinek 			goto error;
51290209230bSgjelinek 
513048451833Scarlsonj 		set_mlps(zlogp, zoneid, zcent);
51310209230bSgjelinek 	}
51320209230bSgjelinek 
5133108322fbScarlsonj 	rval = zoneid;
5134108322fbScarlsonj 	zoneid = -1;
5135108322fbScarlsonj 
51367c478bd9Sstevel@tonic-gate error:
51375679c89fSjv 	if (zoneid != -1) {
51385679c89fSjv 		(void) zone_shutdown(zoneid);
5139108322fbScarlsonj 		(void) zone_destroy(zoneid);
51405679c89fSjv 	}
51417c478bd9Sstevel@tonic-gate 	if (rctlbuf != NULL)
51427c478bd9Sstevel@tonic-gate 		free(rctlbuf);
51437c478bd9Sstevel@tonic-gate 	priv_freeset(privs);
5144108322fbScarlsonj 	if (fp != NULL)
5145108322fbScarlsonj 		zonecfg_close_scratch(fp);
5146108322fbScarlsonj 	lofs_discard_mnttab();
514745916cd2Sjpk 	if (zcent != NULL)
514845916cd2Sjpk 		tsol_freezcent(zcent);
51497c478bd9Sstevel@tonic-gate 	return (rval);
51507c478bd9Sstevel@tonic-gate }
51517c478bd9Sstevel@tonic-gate 
5152555afedfScarlsonj /*
5153555afedfScarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
5154555afedfScarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
5155555afedfScarlsonj  * details from inside the zone.
5156555afedfScarlsonj  */
5157555afedfScarlsonj static void
write_index_file(zoneid_t zoneid)5158555afedfScarlsonj write_index_file(zoneid_t zoneid)
5159555afedfScarlsonj {
5160555afedfScarlsonj 	FILE *zef;
5161555afedfScarlsonj 	FILE *zet;
5162555afedfScarlsonj 	struct zoneent *zep;
5163555afedfScarlsonj 	pid_t child;
5164555afedfScarlsonj 	int tmpl_fd;
5165555afedfScarlsonj 	ctid_t ct;
5166555afedfScarlsonj 	int fd;
5167555afedfScarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
5168555afedfScarlsonj 
5169555afedfScarlsonj 	/* Locate the zone entry in the global zone's index file */
5170555afedfScarlsonj 	if ((zef = setzoneent()) == NULL)
5171555afedfScarlsonj 		return;
5172555afedfScarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
5173555afedfScarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
5174555afedfScarlsonj 			break;
5175555afedfScarlsonj 		free(zep);
5176555afedfScarlsonj 	}
5177555afedfScarlsonj 	endzoneent(zef);
5178555afedfScarlsonj 	if (zep == NULL)
5179555afedfScarlsonj 		return;
5180555afedfScarlsonj 
5181555afedfScarlsonj 	if ((tmpl_fd = init_template()) == -1) {
5182555afedfScarlsonj 		free(zep);
5183555afedfScarlsonj 		return;
5184555afedfScarlsonj 	}
5185555afedfScarlsonj 
5186555afedfScarlsonj 	if ((child = fork()) == -1) {
5187555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
5188555afedfScarlsonj 		(void) close(tmpl_fd);
5189555afedfScarlsonj 		free(zep);
5190555afedfScarlsonj 		return;
5191555afedfScarlsonj 	}
5192555afedfScarlsonj 
5193555afedfScarlsonj 	/* parent waits for child to finish */
5194555afedfScarlsonj 	if (child != 0) {
5195555afedfScarlsonj 		free(zep);
5196555afedfScarlsonj 		if (contract_latest(&ct) == -1)
5197555afedfScarlsonj 			ct = -1;
5198555afedfScarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
5199555afedfScarlsonj 		(void) close(tmpl_fd);
5200555afedfScarlsonj 		(void) waitpid(child, NULL, 0);
5201555afedfScarlsonj 		(void) contract_abandon_id(ct);
5202555afedfScarlsonj 		return;
5203555afedfScarlsonj 	}
5204555afedfScarlsonj 
5205555afedfScarlsonj 	/* child enters zone and sets up index file */
5206555afedfScarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
5207555afedfScarlsonj 	if (zone_enter(zoneid) != -1) {
5208555afedfScarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
5209555afedfScarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
5210555afedfScarlsonj 		    ZONE_CONFIG_GID);
5211555afedfScarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
5212555afedfScarlsonj 		    ZONE_INDEX_MODE);
5213555afedfScarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
5214555afedfScarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
5215555afedfScarlsonj 			if (uuid_is_null(zep->zone_uuid))
5216555afedfScarlsonj 				uuidstr[0] = '\0';
5217555afedfScarlsonj 			else
5218555afedfScarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
5219555afedfScarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
5220555afedfScarlsonj 			    zone_state_str(zep->zone_state),
5221555afedfScarlsonj 			    uuidstr);
5222555afedfScarlsonj 			(void) fclose(zet);
5223555afedfScarlsonj 		}
5224555afedfScarlsonj 	}
5225555afedfScarlsonj 	_exit(0);
5226555afedfScarlsonj }
5227555afedfScarlsonj 
52287c478bd9Sstevel@tonic-gate int
vplat_bringup(zlog_t * zlogp,zone_mnt_t mount_cmd,zoneid_t zoneid)52296cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
52307c478bd9Sstevel@tonic-gate {
52319acbbeafSnn 	char zonepath[MAXPATHLEN];
52325749802bSdp 
52336cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
5234fa9e4066Sahrens 		lofs_discard_mnttab();
5235fa9e4066Sahrens 		return (-1);
5236fa9e4066Sahrens 	}
5237fa9e4066Sahrens 
52389acbbeafSnn 	/*
52399acbbeafSnn 	 * Before we try to mount filesystems we need to create the
52409acbbeafSnn 	 * attribute backing store for /dev
52419acbbeafSnn 	 */
52429acbbeafSnn 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
52439acbbeafSnn 		lofs_discard_mnttab();
52449acbbeafSnn 		return (-1);
52459acbbeafSnn 	}
524616bca938Svp 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
524727e6fb21Sdp 
524827e6fb21Sdp 	/* Make /dev directory owned by root, grouped sys */
524927e6fb21Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
525027e6fb21Sdp 	    0, 3) != 0) {
5251facf4a8dSllai 		lofs_discard_mnttab();
52525749802bSdp 		return (-1);
52535749802bSdp 	}
52545749802bSdp 
52559acbbeafSnn 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
5256108322fbScarlsonj 		lofs_discard_mnttab();
52577c478bd9Sstevel@tonic-gate 		return (-1);
5258108322fbScarlsonj 	}
5259facf4a8dSllai 
52606cfd72c6Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
5261f4b3ec61Sdh 		zone_iptype_t iptype;
5262f4b3ec61Sdh 
52632b24ab6bSSebastien Roy 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
5264f4b3ec61Sdh 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
5265f4b3ec61Sdh 			lofs_discard_mnttab();
5266f4b3ec61Sdh 			return (-1);
5267f4b3ec61Sdh 		}
5268f4b3ec61Sdh 
5269f4b3ec61Sdh 		switch (iptype) {
5270f4b3ec61Sdh 		case ZS_SHARED:
5271f4b3ec61Sdh 			/* Always do this to make lo0 get configured */
5272f4b3ec61Sdh 			if (configure_shared_network_interfaces(zlogp) != 0) {
5273f4b3ec61Sdh 				lofs_discard_mnttab();
5274f4b3ec61Sdh 				return (-1);
5275f4b3ec61Sdh 			}
5276f4b3ec61Sdh 			break;
5277f4b3ec61Sdh 		case ZS_EXCLUSIVE:
5278550b6e40SSowmini Varadhan 			if (configure_exclusive_network_interfaces(zlogp,
5279550b6e40SSowmini Varadhan 			    zoneid) !=
5280f4b3ec61Sdh 			    0) {
5281f4b3ec61Sdh 				lofs_discard_mnttab();
5282f4b3ec61Sdh 				return (-1);
5283f4b3ec61Sdh 			}
5284f4b3ec61Sdh 			break;
5285f4b3ec61Sdh 		}
5286108322fbScarlsonj 	}
5287555afedfScarlsonj 
5288555afedfScarlsonj 	write_index_file(zoneid);
5289555afedfScarlsonj 
5290108322fbScarlsonj 	lofs_discard_mnttab();
5291108322fbScarlsonj 	return (0);
5292108322fbScarlsonj }
5293108322fbScarlsonj 
5294108322fbScarlsonj static int
lu_root_teardown(zlog_t * zlogp)5295108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
5296108322fbScarlsonj {
5297108322fbScarlsonj 	char zroot[MAXPATHLEN];
5298108322fbScarlsonj 
5299108322fbScarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5300108322fbScarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
53017c478bd9Sstevel@tonic-gate 		return (-1);
5302108322fbScarlsonj 	}
5303108322fbScarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
5304108322fbScarlsonj 
5305108322fbScarlsonj 	/*
5306108322fbScarlsonj 	 * At this point, the processes are gone, the filesystems (save the
5307108322fbScarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
5308108322fbScarlsonj 	 * still be creds floating about in the system that reference the
5309108322fbScarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
5310108322fbScarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
5311108322fbScarlsonj 	 * (How I wish this were not true, and umount2 just did the right
5312108322fbScarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
5313108322fbScarlsonj 	 */
5314108322fbScarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
5315108322fbScarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
5316108322fbScarlsonj 			goto unmounted;
5317108322fbScarlsonj 		if (errno == EBUSY) {
5318108322fbScarlsonj 			int tries = 10;
5319108322fbScarlsonj 
5320108322fbScarlsonj 			while (--tries >= 0) {
5321108322fbScarlsonj 				(void) sleep(1);
5322108322fbScarlsonj 				if (umount2(zroot, 0) == 0)
5323108322fbScarlsonj 					goto unmounted;
5324108322fbScarlsonj 				if (errno != EBUSY)
5325108322fbScarlsonj 					break;
5326108322fbScarlsonj 			}
5327108322fbScarlsonj 		}
5328108322fbScarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
53297c478bd9Sstevel@tonic-gate 		return (-1);
5330108322fbScarlsonj 	}
5331108322fbScarlsonj unmounted:
5332108322fbScarlsonj 
5333108322fbScarlsonj 	/*
5334108322fbScarlsonj 	 * Only zones in an alternate root environment have scratch zone
5335108322fbScarlsonj 	 * entries.
5336108322fbScarlsonj 	 */
5337108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5338108322fbScarlsonj 		FILE *fp;
5339108322fbScarlsonj 		int retv;
5340108322fbScarlsonj 
5341108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5342108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
5343108322fbScarlsonj 			return (-1);
5344108322fbScarlsonj 		}
5345108322fbScarlsonj 		retv = -1;
5346108322fbScarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
5347108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
5348108322fbScarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
5349108322fbScarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
5350108322fbScarlsonj 		else
5351108322fbScarlsonj 			retv = 0;
5352108322fbScarlsonj 		zonecfg_close_scratch(fp);
5353108322fbScarlsonj 		return (retv);
5354108322fbScarlsonj 	} else {
5355108322fbScarlsonj 		return (0);
5356108322fbScarlsonj 	}
53577c478bd9Sstevel@tonic-gate }
53587c478bd9Sstevel@tonic-gate 
535985dff7a0SAndy Fiddaman /*
536085dff7a0SAndy Fiddaman  * Delete all transient links belonging to this zone. A transient link
536185dff7a0SAndy Fiddaman  * is one that is created and destroyed along with the lifetime of the
536285dff7a0SAndy Fiddaman  * zone. Non-transient links, ones that are assigned from the GZ to a
536385dff7a0SAndy Fiddaman  * NGZ, are reassigned to the GZ in zone_shutdown() via the
536485dff7a0SAndy Fiddaman  * zone-specific data (zsd) callbacks.
536585dff7a0SAndy Fiddaman  */
536685dff7a0SAndy Fiddaman static int
delete_transient_links(zlog_t * zlogp,zoneid_t zoneid)536785dff7a0SAndy Fiddaman delete_transient_links(zlog_t *zlogp, zoneid_t zoneid)
536885dff7a0SAndy Fiddaman {
536985dff7a0SAndy Fiddaman 	datalink_id_t *dllinks = NULL;
537085dff7a0SAndy Fiddaman 	int dlnum = 0;
537185dff7a0SAndy Fiddaman 	uint_t i;
537285dff7a0SAndy Fiddaman 
537385dff7a0SAndy Fiddaman 	if (fetch_zone_datalinks(zlogp, zoneid, &dlnum, &dllinks) != 0)
537485dff7a0SAndy Fiddaman 		return (-1);
537585dff7a0SAndy Fiddaman 
537685dff7a0SAndy Fiddaman 	if (dlnum == 0)
537785dff7a0SAndy Fiddaman 		return (0);
537885dff7a0SAndy Fiddaman 
537985dff7a0SAndy Fiddaman 	for (i = 0; i < dlnum; i++) {
538085dff7a0SAndy Fiddaman 		char link_name[MAXLINKNAMELEN];
538185dff7a0SAndy Fiddaman 		char dlerr[DLADM_STRSIZE];
538285dff7a0SAndy Fiddaman 		datalink_id_t link = dllinks[i];
538385dff7a0SAndy Fiddaman 		datalink_class_t link_class;
538485dff7a0SAndy Fiddaman 		dladm_status_t status;
538585dff7a0SAndy Fiddaman 		uint32_t link_flags;
538685dff7a0SAndy Fiddaman 
538785dff7a0SAndy Fiddaman 		status = dladm_datalink_id2info(dld_handle, link, &link_flags,
538885dff7a0SAndy Fiddaman 		    &link_class, NULL, link_name, sizeof (link_name));
538985dff7a0SAndy Fiddaman 
539085dff7a0SAndy Fiddaman 		if (status != DLADM_STATUS_OK) {
539185dff7a0SAndy Fiddaman 			zerror(zlogp, B_FALSE,
539285dff7a0SAndy Fiddaman 			    "failed to get link info for %u: %s",
539385dff7a0SAndy Fiddaman 			    link, dladm_status2str(status, dlerr));
539485dff7a0SAndy Fiddaman 			continue;
539585dff7a0SAndy Fiddaman 		}
539685dff7a0SAndy Fiddaman 
539785dff7a0SAndy Fiddaman 		if (!(link_flags & DLADM_OPT_TRANSIENT))
539885dff7a0SAndy Fiddaman 			continue;
539985dff7a0SAndy Fiddaman 
540085dff7a0SAndy Fiddaman 		switch (link_class) {
540185dff7a0SAndy Fiddaman 		case DATALINK_CLASS_VNIC:
540285dff7a0SAndy Fiddaman 		case DATALINK_CLASS_ETHERSTUB:
540385dff7a0SAndy Fiddaman 			status = dladm_vnic_delete(dld_handle, link,
540485dff7a0SAndy Fiddaman 			    DLADM_OPT_ACTIVE);
540585dff7a0SAndy Fiddaman 			break;
540685dff7a0SAndy Fiddaman 		case DATALINK_CLASS_VLAN:
540785dff7a0SAndy Fiddaman 			status = dladm_vlan_delete(dld_handle, link,
540885dff7a0SAndy Fiddaman 			    DLADM_OPT_ACTIVE);
540985dff7a0SAndy Fiddaman 			break;
541085dff7a0SAndy Fiddaman 		case DATALINK_CLASS_AGGR:
541185dff7a0SAndy Fiddaman 			status = dladm_aggr_delete(dld_handle, link,
541285dff7a0SAndy Fiddaman 			    DLADM_OPT_ACTIVE);
541385dff7a0SAndy Fiddaman 			break;
541485dff7a0SAndy Fiddaman 		default:
541585dff7a0SAndy Fiddaman 			zerror(zlogp, B_FALSE,
541685dff7a0SAndy Fiddaman 			    "unhandled class for transient link %s (%u)",
541785dff7a0SAndy Fiddaman 			    link_name, link);
541885dff7a0SAndy Fiddaman 			continue;
541985dff7a0SAndy Fiddaman 		}
542085dff7a0SAndy Fiddaman 
542185dff7a0SAndy Fiddaman 		if (status != DLADM_STATUS_OK) {
542285dff7a0SAndy Fiddaman 			zerror(zlogp, B_TRUE,
542385dff7a0SAndy Fiddaman 			    "failed to delete transient link %s (%u): %s",
542485dff7a0SAndy Fiddaman 			    link_name, link, dladm_status2str(status, dlerr));
542585dff7a0SAndy Fiddaman 		}
542685dff7a0SAndy Fiddaman 	}
542785dff7a0SAndy Fiddaman 
542885dff7a0SAndy Fiddaman 	free(dllinks);
542985dff7a0SAndy Fiddaman 	return (0);
543085dff7a0SAndy Fiddaman }
543185dff7a0SAndy Fiddaman 
54327c478bd9Sstevel@tonic-gate int
vplat_teardown(zlog_t * zlogp,boolean_t unmount_cmd,boolean_t rebooting)54330209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
54347c478bd9Sstevel@tonic-gate {
5435108322fbScarlsonj 	char *kzone;
54367c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
54370209230bSgjelinek 	int res;
54380209230bSgjelinek 	char pool_err[128];
5439ff17c8bfSgjelinek 	char zpath[MAXPATHLEN];
54409acbbeafSnn 	char cmdbuf[MAXPATHLEN];
5441123807fbSedp 	brand_handle_t bh = NULL;
54422b24ab6bSSebastien Roy 	dladm_status_t status;
54432b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
5444f4b3ec61Sdh 	ushort_t flags;
54457c478bd9Sstevel@tonic-gate 
5446108322fbScarlsonj 	kzone = zone_name;
5447108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
5448108322fbScarlsonj 		FILE *fp;
5449108322fbScarlsonj 
5450108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5451108322fbScarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
5452108322fbScarlsonj 			goto error;
5453108322fbScarlsonj 		}
5454108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5455108322fbScarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
5456108322fbScarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
5457108322fbScarlsonj 			zonecfg_close_scratch(fp);
5458108322fbScarlsonj 			goto error;
5459108322fbScarlsonj 		}
5460108322fbScarlsonj 		zonecfg_close_scratch(fp);
5461108322fbScarlsonj 		kzone = kernzone;
5462108322fbScarlsonj 	}
5463108322fbScarlsonj 
5464108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
54657c478bd9Sstevel@tonic-gate 		if (!bringup_failure_recovery)
54667c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
5467108322fbScarlsonj 		if (unmount_cmd)
5468108322fbScarlsonj 			(void) lu_root_teardown(zlogp);
54697c478bd9Sstevel@tonic-gate 		goto error;
54707c478bd9Sstevel@tonic-gate 	}
54717c478bd9Sstevel@tonic-gate 
54720dc2366fSVenugopal Iyer 	if (remove_datalink_pool(zlogp, zoneid) != 0) {
547385dff7a0SAndy Fiddaman 		zerror(zlogp, B_FALSE,
547485dff7a0SAndy Fiddaman 		    "unable to clear datalink pool property");
54750dc2366fSVenugopal Iyer 	}
54760dc2366fSVenugopal Iyer 
5477550b6e40SSowmini Varadhan 	if (remove_datalink_protect(zlogp, zoneid) != 0) {
5478550b6e40SSowmini Varadhan 		zerror(zlogp, B_FALSE,
547985dff7a0SAndy Fiddaman 		    "unable to clear datalink protect property");
5480550b6e40SSowmini Varadhan 	}
5481550b6e40SSowmini Varadhan 
5482550b6e40SSowmini Varadhan 	/*
5483550b6e40SSowmini Varadhan 	 * The datalinks assigned to the zone will be removed from the NGZ as
5484550b6e40SSowmini Varadhan 	 * part of zone_shutdown() so that we need to remove protect/pool etc.
5485550b6e40SSowmini Varadhan 	 * before zone_shutdown(). Even if the shutdown itself fails, the zone
5486550b6e40SSowmini Varadhan 	 * will not be able to violate any constraints applied because the
5487550b6e40SSowmini Varadhan 	 * datalinks are no longer available to the zone.
5488550b6e40SSowmini Varadhan 	 */
54897c478bd9Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
54907c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
54917c478bd9Sstevel@tonic-gate 		goto error;
54927c478bd9Sstevel@tonic-gate 	}
54937c478bd9Sstevel@tonic-gate 
5494ff17c8bfSgjelinek 	/* Get the zonepath of this zone */
5495ff17c8bfSgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
5496ff17c8bfSgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
54979acbbeafSnn 		goto error;
54989acbbeafSnn 	}
54999acbbeafSnn 
55009acbbeafSnn 	/* Get a handle to the brand info for this zone */
550162868012SSteve Lawrence 	if ((bh = brand_open(brand_name)) == NULL) {
55029acbbeafSnn 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
55039acbbeafSnn 		return (-1);
55049acbbeafSnn 	}
55059acbbeafSnn 	/*
55069acbbeafSnn 	 * If there is a brand 'halt' callback, execute it now to give the
55079acbbeafSnn 	 * brand a chance to cleanup any custom configuration.
55089acbbeafSnn 	 */
55099acbbeafSnn 	(void) strcpy(cmdbuf, EXEC_PREFIX);
5510ff17c8bfSgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
5511ff17c8bfSgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
5512123807fbSedp 		brand_close(bh);
55139acbbeafSnn 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
55149acbbeafSnn 		    "halt callback.");
55159acbbeafSnn 		goto error;
55169acbbeafSnn 	}
5517123807fbSedp 	brand_close(bh);
55189acbbeafSnn 
55199acbbeafSnn 	if ((strlen(cmdbuf) > EXEC_LEN) &&
5520c5cd6260S 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
55219acbbeafSnn 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
55229acbbeafSnn 		goto error;
55239acbbeafSnn 	}
55249acbbeafSnn 
5525f4b3ec61Sdh 	if (!unmount_cmd) {
5526f4b3ec61Sdh 		zone_iptype_t iptype;
5527f4b3ec61Sdh 
5528f4b3ec61Sdh 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
5529f4b3ec61Sdh 		    sizeof (flags)) < 0) {
55302b24ab6bSSebastien Roy 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
5531f4b3ec61Sdh 				zerror(zlogp, B_TRUE, "unable to determine "
5532f4b3ec61Sdh 				    "ip-type");
5533f4b3ec61Sdh 				goto error;
5534f4b3ec61Sdh 			}
5535f4b3ec61Sdh 		} else {
5536f4b3ec61Sdh 			if (flags & ZF_NET_EXCL)
5537f4b3ec61Sdh 				iptype = ZS_EXCLUSIVE;
5538f4b3ec61Sdh 			else
5539f4b3ec61Sdh 				iptype = ZS_SHARED;
5540f4b3ec61Sdh 		}
5541f4b3ec61Sdh 
5542f4b3ec61Sdh 		switch (iptype) {
5543f4b3ec61Sdh 		case ZS_SHARED:
5544f4b3ec61Sdh 			if (unconfigure_shared_network_interfaces(zlogp,
5545f4b3ec61Sdh 			    zoneid) != 0) {
5546f4b3ec61Sdh 				zerror(zlogp, B_FALSE, "unable to unconfigure "
5547f4b3ec61Sdh 				    "network interfaces in zone");
5548f4b3ec61Sdh 				goto error;
5549f4b3ec61Sdh 			}
5550f4b3ec61Sdh 			break;
5551f4b3ec61Sdh 		case ZS_EXCLUSIVE:
555285dff7a0SAndy Fiddaman 			if (delete_transient_links(zlogp, zoneid) != 0) {
555385dff7a0SAndy Fiddaman 				zerror(zlogp, B_FALSE, "unable to delete "
555485dff7a0SAndy Fiddaman 				    "transient links in zone");
555585dff7a0SAndy Fiddaman 				goto error;
555685dff7a0SAndy Fiddaman 			}
5557f4b3ec61Sdh 			if (unconfigure_exclusive_network_interfaces(zlogp,
5558f4b3ec61Sdh 			    zoneid) != 0) {
5559f4b3ec61Sdh 				zerror(zlogp, B_FALSE, "unable to unconfigure "
5560f4b3ec61Sdh 				    "network interfaces in zone");
5561f4b3ec61Sdh 				goto error;
5562f4b3ec61Sdh 			}
55632b24ab6bSSebastien Roy 			status = dladm_zone_halt(dld_handle, zoneid);
55642b24ab6bSSebastien Roy 			if (status != DLADM_STATUS_OK) {
55652b24ab6bSSebastien Roy 				zerror(zlogp, B_FALSE, "unable to notify "
55662b24ab6bSSebastien Roy 				    "dlmgmtd of zone halt: %s",
55672b24ab6bSSebastien Roy 				    dladm_status2str(status, errmsg));
55682b24ab6bSSebastien Roy 			}
5569f4b3ec61Sdh 			break;
5570f4b3ec61Sdh 		}
55717c478bd9Sstevel@tonic-gate 	}
55727c478bd9Sstevel@tonic-gate 
5573108322fbScarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
55747c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
55757c478bd9Sstevel@tonic-gate 		goto error;
55767c478bd9Sstevel@tonic-gate 	}
55777c478bd9Sstevel@tonic-gate 
5578108322fbScarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
55797c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
55807c478bd9Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
55817c478bd9Sstevel@tonic-gate 		goto error;
55827c478bd9Sstevel@tonic-gate 	}
55837c478bd9Sstevel@tonic-gate 
55840209230bSgjelinek 	/*
5585cf6b13d2Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
5586cf6b13d2Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
5587cf6b13d2Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
5588cf6b13d2Sgjelinek 	 * running with a temporary pool and the zone configuration has been
5589cf6b13d2Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
5590cf6b13d2Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
5591cf6b13d2Sgjelinek 	 * where we never had a temporary pool configured but
5592cf6b13d2Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
55930209230bSgjelinek 	 */
5594cf6b13d2Sgjelinek 	if (!unmount_cmd) {
5595cf6b13d2Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
5596cf6b13d2Sgjelinek 
5597cf6b13d2Sgjelinek 		if (rebooting) {
5598cf6b13d2Sgjelinek 			struct zone_psettab pset_tab;
5599cf6b13d2Sgjelinek 			zone_dochandle_t handle;
5600cf6b13d2Sgjelinek 
5601cf6b13d2Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
5602cf6b13d2Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
5603cf6b13d2Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
5604cf6b13d2Sgjelinek 				destroy_tmp_pool = B_FALSE;
5605e767a340Sgjelinek 
5606e767a340Sgjelinek 			zonecfg_fini_handle(handle);
5607cf6b13d2Sgjelinek 		}
5608cf6b13d2Sgjelinek 
5609cf6b13d2Sgjelinek 		if (destroy_tmp_pool) {
5610cf6b13d2Sgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
5611cf6b13d2Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
5612cf6b13d2Sgjelinek 				if (res == Z_POOL)
5613cf6b13d2Sgjelinek 					zerror(zlogp, B_FALSE, pool_err);
5614cf6b13d2Sgjelinek 			}
56150209230bSgjelinek 		}
56160209230bSgjelinek 	}
56170209230bSgjelinek 
561845916cd2Sjpk 	remove_mlps(zlogp, zoneid);
561945916cd2Sjpk 
56207c478bd9Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
56217c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
56227c478bd9Sstevel@tonic-gate 		goto error;
56237c478bd9Sstevel@tonic-gate 	}
56247c478bd9Sstevel@tonic-gate 
5625108322fbScarlsonj 	/*
5626108322fbScarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
5627108322fbScarlsonj 	 * root for the zone and then remove it from the map file.
5628108322fbScarlsonj 	 */
5629108322fbScarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5630108322fbScarlsonj 		goto error;
5631108322fbScarlsonj 
5632108322fbScarlsonj 	lofs_discard_mnttab();
56337c478bd9Sstevel@tonic-gate 	return (0);
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate error:
5636108322fbScarlsonj 	lofs_discard_mnttab();
56377c478bd9Sstevel@tonic-gate 	return (-1);
56387c478bd9Sstevel@tonic-gate }
5639