xref: /onnv-gate/usr/src/cmd/zoneadmd/vplat.c (revision 13122:61fe7fb74c94)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211645Scomay 
220Sstevel@tonic-gate /*
2312633Sjohn.levon@sun.com  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * This module contains functions used to bring up and tear down the
280Sstevel@tonic-gate  * Virtual Platform: [un]mounting file-systems, [un]plumbing network
290Sstevel@tonic-gate  * interfaces, [un]configuring devices, establishing resource controls,
300Sstevel@tonic-gate  * and creating/destroying the zone in the kernel.  These actions, on
310Sstevel@tonic-gate  * the way up, ready the zone; on the way down, they halt the zone.
320Sstevel@tonic-gate  * See the much longer block comment at the beginning of zoneadmd.c
330Sstevel@tonic-gate  * for a bigger picture of how the whole program functions.
34766Scarlsonj  *
35766Scarlsonj  * This module also has primary responsibility for the layout of "scratch
36766Scarlsonj  * zones."  These are mounted, but inactive, zones that are used during
37766Scarlsonj  * operating system upgrade and potentially other administrative action.  The
38766Scarlsonj  * scratch zone environment is similar to the miniroot environment.  The zone's
39766Scarlsonj  * actual root is mounted read-write on /a, and the standard paths (/usr,
40766Scarlsonj  * /sbin, /lib) all lead to read-only copies of the running system's binaries.
41766Scarlsonj  * This allows the administrative tools to manipulate the zone using "-R /a"
42766Scarlsonj  * without relying on any binaries in the zone itself.
43766Scarlsonj  *
44766Scarlsonj  * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
45766Scarlsonj  * environment), then we must resolve the lofs mounts used there to uncover
46766Scarlsonj  * writable (unshared) resources.  Shared resources, though, are always
47766Scarlsonj  * read-only.  In addition, if the "same" zone with a different root path is
48766Scarlsonj  * currently running, then "/b" inside the zone points to the running zone's
49766Scarlsonj  * root.  This allows LU to synchronize configuration files during the upgrade
50766Scarlsonj  * process.
51766Scarlsonj  *
52766Scarlsonj  * To construct this environment, this module creates a tmpfs mount on
53766Scarlsonj  * $ZONEPATH/lu.  Inside this scratch area, the miniroot-like environment as
54766Scarlsonj  * described above is constructed on the fly.  The zone is then created using
55766Scarlsonj  * $ZONEPATH/lu as the root.
56766Scarlsonj  *
57766Scarlsonj  * Note that scratch zones are inactive.  The zone's bits are not running and
58766Scarlsonj  * likely cannot be run correctly until upgrade is done.  Init is not running
59766Scarlsonj  * there, nor is SMF.  Because of this, the "mounted" state of a scratch zone
60766Scarlsonj  * is not a part of the usual halt/ready/boot state machine.
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <sys/param.h>
640Sstevel@tonic-gate #include <sys/mount.h>
650Sstevel@tonic-gate #include <sys/mntent.h>
660Sstevel@tonic-gate #include <sys/socket.h>
670Sstevel@tonic-gate #include <sys/utsname.h>
680Sstevel@tonic-gate #include <sys/types.h>
690Sstevel@tonic-gate #include <sys/stat.h>
700Sstevel@tonic-gate #include <sys/sockio.h>
710Sstevel@tonic-gate #include <sys/stropts.h>
720Sstevel@tonic-gate #include <sys/conf.h>
738662SJordan.Vaughan@Sun.com #include <sys/systeminfo.h>
740Sstevel@tonic-gate 
753448Sdh155122 #include <libdlpi.h>
763871Syz147064 #include <libdllink.h>
775895Syz147064 #include <libdlvlan.h>
783448Sdh155122 
790Sstevel@tonic-gate #include <inet/tcp.h>
800Sstevel@tonic-gate #include <arpa/inet.h>
810Sstevel@tonic-gate #include <netinet/in.h>
820Sstevel@tonic-gate #include <net/route.h>
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #include <stdio.h>
850Sstevel@tonic-gate #include <errno.h>
860Sstevel@tonic-gate #include <fcntl.h>
870Sstevel@tonic-gate #include <unistd.h>
880Sstevel@tonic-gate #include <rctl.h>
890Sstevel@tonic-gate #include <stdlib.h>
900Sstevel@tonic-gate #include <string.h>
910Sstevel@tonic-gate #include <strings.h>
920Sstevel@tonic-gate #include <wait.h>
930Sstevel@tonic-gate #include <limits.h>
940Sstevel@tonic-gate #include <libgen.h>
95789Sahrens #include <libzfs.h>
962621Sllai1 #include <libdevinfo.h>
970Sstevel@tonic-gate #include <zone.h>
980Sstevel@tonic-gate #include <assert.h>
992303Scarlsonj #include <libcontract.h>
1002303Scarlsonj #include <libcontract_priv.h>
1012303Scarlsonj #include <uuid/uuid.h>
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #include <sys/mntio.h>
1040Sstevel@tonic-gate #include <sys/mnttab.h>
1050Sstevel@tonic-gate #include <sys/fs/autofs.h>	/* for _autofssys() */
1060Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
107789Sahrens #include <sys/fs/zfs.h>
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #include <pool.h>
1100Sstevel@tonic-gate #include <sys/pool.h>
1113247Sgjelinek #include <sys/priocntl.h>
1120Sstevel@tonic-gate 
1132712Snn35248 #include <libbrand.h>
1142712Snn35248 #include <sys/brand.h>
1150Sstevel@tonic-gate #include <libzonecfg.h>
1162170Sevanl #include <synch.h>
1172611Svp157776 
1180Sstevel@tonic-gate #include "zoneadmd.h"
1191676Sjpk #include <tsol/label.h>
1201676Sjpk #include <libtsnet.h>
1211676Sjpk #include <sys/priv.h>
12212748SSowmini.Varadhan@oracle.COM #include <libinetutil.h>
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #define	V4_ADDR_LEN	32
1250Sstevel@tonic-gate #define	V6_ADDR_LEN	128
1260Sstevel@tonic-gate 
12712734Sgary.pennington@oracle.com #define	RESOURCE_DEFAULT_OPTS \
1280Sstevel@tonic-gate 	MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	DFSTYPES	"/etc/dfs/fstypes"
1311676Sjpk #define	MAXTNZLEN	2048
1320Sstevel@tonic-gate 
1335829Sgjelinek #define	ALT_MOUNT(mount_cmd) 	((mount_cmd) != Z_MNT_BOOT)
1345829Sgjelinek 
13512725SMenno.Lageman@Sun.COM /* a reasonable estimate for the number of lwps per process */
13612725SMenno.Lageman@Sun.COM #define	LWPS_PER_PROCESS	10
13712725SMenno.Lageman@Sun.COM 
1380Sstevel@tonic-gate /* for routing socket */
1390Sstevel@tonic-gate static int rts_seqno = 0;
1400Sstevel@tonic-gate 
141766Scarlsonj /* mangled zone name when mounting in an alternate root environment */
142766Scarlsonj static char kernzone[ZONENAME_MAX];
143766Scarlsonj 
144766Scarlsonj /* array of cached mount entries for resolve_lofs */
145766Scarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
146766Scarlsonj 
1471676Sjpk /* for Trusted Extensions */
1481676Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
1491676Sjpk static int tsol_mounts(zlog_t *, char *, char *);
1501676Sjpk static void tsol_unmounts(zlog_t *, char *);
1515596Sdh155122 
1521676Sjpk static m_label_t *zlabel = NULL;
1531676Sjpk static m_label_t *zid_label = NULL;
1541676Sjpk static priv_set_t *zprivs = NULL;
1551676Sjpk 
1560Sstevel@tonic-gate /* from libsocket, not in any header file */
1570Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1580Sstevel@tonic-gate 
1597370S<Gerald Jelinek> /* from zoneadmd */
1607370S<Gerald Jelinek> extern char query_hook[];
1617370S<Gerald Jelinek> 
1620Sstevel@tonic-gate /*
16312748SSowmini.Varadhan@oracle.COM  * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
16412748SSowmini.Varadhan@oracle.COM  * node in a linked list that is sorted by linkid.  The list is constructed as
16512748SSowmini.Varadhan@oracle.COM  * the xml configuration file is parsed, and the information
16612748SSowmini.Varadhan@oracle.COM  * contained in each node is added to the kernel before the zone is
16712748SSowmini.Varadhan@oracle.COM  * booted, to be retrieved and applied from within the exclusive-IP NGZ
16812748SSowmini.Varadhan@oracle.COM  * on boot.
16912748SSowmini.Varadhan@oracle.COM  */
17012748SSowmini.Varadhan@oracle.COM typedef struct zone_addr_list {
17112748SSowmini.Varadhan@oracle.COM 	struct zone_addr_list *za_next;
17212748SSowmini.Varadhan@oracle.COM 	datalink_id_t za_linkid;	/* datalink_id_t of interface */
17312748SSowmini.Varadhan@oracle.COM 	struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
17412748SSowmini.Varadhan@oracle.COM } zone_addr_list_t;
17512748SSowmini.Varadhan@oracle.COM 
17612748SSowmini.Varadhan@oracle.COM /*
177766Scarlsonj  * An optimization for build_mnttable: reallocate (and potentially copy the
178766Scarlsonj  * data) only once every N times through the loop.
179766Scarlsonj  */
180766Scarlsonj #define	MNTTAB_HUNK	32
181766Scarlsonj 
18212748SSowmini.Varadhan@oracle.COM /* some handy macros */
18312748SSowmini.Varadhan@oracle.COM #define	SIN(s)	((struct sockaddr_in *)s)
18412748SSowmini.Varadhan@oracle.COM #define	SIN6(s)	((struct sockaddr_in6 *)s)
18512748SSowmini.Varadhan@oracle.COM 
186766Scarlsonj /*
1870Sstevel@tonic-gate  * Private autofs system call
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate extern int _autofssys(int, void *);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static int
autofs_cleanup(zoneid_t zoneid)1920Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	/*
1950Sstevel@tonic-gate 	 * Ask autofs to unmount all trigger nodes in the given zone.
1960Sstevel@tonic-gate 	 */
1970Sstevel@tonic-gate 	return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
200766Scarlsonj static void
free_mnttable(struct mnttab * mnt_array,uint_t nelem)201766Scarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
202766Scarlsonj {
203766Scarlsonj 	uint_t i;
204766Scarlsonj 
205766Scarlsonj 	if (mnt_array == NULL)
206766Scarlsonj 		return;
207766Scarlsonj 	for (i = 0; i < nelem; i++) {
208766Scarlsonj 		free(mnt_array[i].mnt_mountp);
209766Scarlsonj 		free(mnt_array[i].mnt_fstype);
210766Scarlsonj 		free(mnt_array[i].mnt_special);
211766Scarlsonj 		free(mnt_array[i].mnt_mntopts);
212766Scarlsonj 		assert(mnt_array[i].mnt_time == NULL);
213766Scarlsonj 	}
214766Scarlsonj 	free(mnt_array);
215766Scarlsonj }
216766Scarlsonj 
217766Scarlsonj /*
218766Scarlsonj  * Build the mount table for the zone rooted at "zroot", storing the resulting
219766Scarlsonj  * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
220766Scarlsonj  * array in "nelemp".
221766Scarlsonj  */
222766Scarlsonj static int
build_mnttable(zlog_t * zlogp,const char * zroot,size_t zrootlen,FILE * mnttab,struct mnttab ** mnt_arrayp,uint_t * nelemp)223766Scarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
224766Scarlsonj     struct mnttab **mnt_arrayp, uint_t *nelemp)
225766Scarlsonj {
226766Scarlsonj 	struct mnttab mnt;
227766Scarlsonj 	struct mnttab *mnts;
228766Scarlsonj 	struct mnttab *mnp;
229766Scarlsonj 	uint_t nmnt;
230766Scarlsonj 
231766Scarlsonj 	rewind(mnttab);
232766Scarlsonj 	resetmnttab(mnttab);
233766Scarlsonj 	nmnt = 0;
234766Scarlsonj 	mnts = NULL;
235766Scarlsonj 	while (getmntent(mnttab, &mnt) == 0) {
236766Scarlsonj 		struct mnttab *tmp_array;
237766Scarlsonj 
238766Scarlsonj 		if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
239766Scarlsonj 			continue;
240766Scarlsonj 		if (nmnt % MNTTAB_HUNK == 0) {
241766Scarlsonj 			tmp_array = realloc(mnts,
242766Scarlsonj 			    (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
243766Scarlsonj 			if (tmp_array == NULL) {
244766Scarlsonj 				free_mnttable(mnts, nmnt);
245766Scarlsonj 				return (-1);
246766Scarlsonj 			}
247766Scarlsonj 			mnts = tmp_array;
248766Scarlsonj 		}
249766Scarlsonj 		mnp = &mnts[nmnt++];
250766Scarlsonj 
251766Scarlsonj 		/*
252766Scarlsonj 		 * Zero out any fields we're not using.
253766Scarlsonj 		 */
254766Scarlsonj 		(void) memset(mnp, 0, sizeof (*mnp));
255766Scarlsonj 
256766Scarlsonj 		if (mnt.mnt_special != NULL)
257766Scarlsonj 			mnp->mnt_special = strdup(mnt.mnt_special);
258766Scarlsonj 		if (mnt.mnt_mntopts != NULL)
259766Scarlsonj 			mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
260766Scarlsonj 		mnp->mnt_mountp = strdup(mnt.mnt_mountp);
261766Scarlsonj 		mnp->mnt_fstype = strdup(mnt.mnt_fstype);
262766Scarlsonj 		if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
263766Scarlsonj 		    (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
264766Scarlsonj 		    mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
265766Scarlsonj 			zerror(zlogp, B_TRUE, "memory allocation failed");
266766Scarlsonj 			free_mnttable(mnts, nmnt);
267766Scarlsonj 			return (-1);
268766Scarlsonj 		}
269766Scarlsonj 	}
270766Scarlsonj 	*mnt_arrayp = mnts;
271766Scarlsonj 	*nelemp = nmnt;
272766Scarlsonj 	return (0);
273766Scarlsonj }
274766Scarlsonj 
275766Scarlsonj /*
276766Scarlsonj  * This is an optimization.  The resolve_lofs function is used quite frequently
277766Scarlsonj  * to manipulate file paths, and on a machine with a large number of zones,
278766Scarlsonj  * there will be a huge number of mounted file systems.  Thus, we trigger a
279766Scarlsonj  * reread of the list of mount points
280766Scarlsonj  */
281766Scarlsonj static void
lofs_discard_mnttab(void)282766Scarlsonj lofs_discard_mnttab(void)
283766Scarlsonj {
284766Scarlsonj 	free_mnttable(resolve_lofs_mnts,
285766Scarlsonj 	    resolve_lofs_mnt_max - resolve_lofs_mnts);
286766Scarlsonj 	resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
287766Scarlsonj }
288766Scarlsonj 
289766Scarlsonj static int
lofs_read_mnttab(zlog_t * zlogp)290766Scarlsonj lofs_read_mnttab(zlog_t *zlogp)
291766Scarlsonj {
292766Scarlsonj 	FILE *mnttab;
293766Scarlsonj 	uint_t nmnts;
294766Scarlsonj 
295766Scarlsonj 	if ((mnttab = fopen(MNTTAB, "r")) == NULL)
296766Scarlsonj 		return (-1);
297766Scarlsonj 	if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
298766Scarlsonj 	    &nmnts) == -1) {
299766Scarlsonj 		(void) fclose(mnttab);
300766Scarlsonj 		return (-1);
301766Scarlsonj 	}
302766Scarlsonj 	(void) fclose(mnttab);
303766Scarlsonj 	resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
304766Scarlsonj 	return (0);
305766Scarlsonj }
306766Scarlsonj 
307766Scarlsonj /*
308766Scarlsonj  * This function loops over potential loopback mounts and symlinks in a given
309766Scarlsonj  * path and resolves them all down to an absolute path.
310766Scarlsonj  */
3115576Sedp void
resolve_lofs(zlog_t * zlogp,char * path,size_t pathlen)312766Scarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
313766Scarlsonj {
314766Scarlsonj 	int len, arlen;
315766Scarlsonj 	const char *altroot;
316766Scarlsonj 	char tmppath[MAXPATHLEN];
317766Scarlsonj 	boolean_t outside_altroot;
318766Scarlsonj 
319766Scarlsonj 	if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
320766Scarlsonj 		return;
321766Scarlsonj 	tmppath[len] = '\0';
322766Scarlsonj 	(void) strlcpy(path, tmppath, sizeof (tmppath));
323766Scarlsonj 
324766Scarlsonj 	/* This happens once per zoneadmd operation. */
325766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
326766Scarlsonj 		return;
327766Scarlsonj 
328766Scarlsonj 	altroot = zonecfg_get_root();
329766Scarlsonj 	arlen = strlen(altroot);
330766Scarlsonj 	outside_altroot = B_FALSE;
331766Scarlsonj 	for (;;) {
332766Scarlsonj 		struct mnttab *mnp;
333766Scarlsonj 
3343079Sdminer 		/* Search in reverse order to find longest match */
3353079Sdminer 		for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3363079Sdminer 		    mnp--) {
337766Scarlsonj 			if (mnp->mnt_fstype == NULL ||
338766Scarlsonj 			    mnp->mnt_mountp == NULL ||
3393079Sdminer 			    mnp->mnt_special == NULL)
340766Scarlsonj 				continue;
341766Scarlsonj 			len = strlen(mnp->mnt_mountp);
342766Scarlsonj 			if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
343766Scarlsonj 			    (path[len] == '/' || path[len] == '\0'))
344766Scarlsonj 				break;
345766Scarlsonj 		}
3463079Sdminer 		if (mnp < resolve_lofs_mnts)
3473079Sdminer 			break;
3483079Sdminer 		/* If it's not a lofs then we're done */
3493079Sdminer 		if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
350766Scarlsonj 			break;
351766Scarlsonj 		if (outside_altroot) {
352766Scarlsonj 			char *cp;
353766Scarlsonj 			int olen = sizeof (MNTOPT_RO) - 1;
354766Scarlsonj 
355766Scarlsonj 			/*
356766Scarlsonj 			 * If we run into a read-only mount outside of the
357766Scarlsonj 			 * alternate root environment, then the user doesn't
358766Scarlsonj 			 * want this path to be made read-write.
359766Scarlsonj 			 */
360766Scarlsonj 			if (mnp->mnt_mntopts != NULL &&
361766Scarlsonj 			    (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
362766Scarlsonj 			    NULL &&
363766Scarlsonj 			    (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
364766Scarlsonj 			    (cp[olen] == '\0' || cp[olen] == ',')) {
365766Scarlsonj 				break;
366766Scarlsonj 			}
367766Scarlsonj 		} else if (arlen > 0 &&
368766Scarlsonj 		    (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
369766Scarlsonj 		    (mnp->mnt_special[arlen] != '\0' &&
370766Scarlsonj 		    mnp->mnt_special[arlen] != '/'))) {
371766Scarlsonj 			outside_altroot = B_TRUE;
372766Scarlsonj 		}
373766Scarlsonj 		/* use temporary buffer because new path might be longer */
374766Scarlsonj 		(void) snprintf(tmppath, sizeof (tmppath), "%s%s",
375766Scarlsonj 		    mnp->mnt_special, path + len);
376766Scarlsonj 		if ((len = resolvepath(tmppath, path, pathlen)) == -1)
377766Scarlsonj 			break;
378766Scarlsonj 		path[len] = '\0';
379766Scarlsonj 	}
380766Scarlsonj }
381766Scarlsonj 
382766Scarlsonj /*
383766Scarlsonj  * For a regular mount, check if a replacement lofs mount is needed because the
384766Scarlsonj  * referenced device is already mounted somewhere.
385766Scarlsonj  */
386766Scarlsonj static int
check_lofs_needed(zlog_t * zlogp,struct zone_fstab * fsptr)387766Scarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
388766Scarlsonj {
389766Scarlsonj 	struct mnttab *mnp;
390766Scarlsonj 	zone_fsopt_t *optptr, *onext;
391766Scarlsonj 
392766Scarlsonj 	/* This happens once per zoneadmd operation. */
393766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
394766Scarlsonj 		return (-1);
395766Scarlsonj 
396766Scarlsonj 	/*
397766Scarlsonj 	 * If this special node isn't already in use, then it's ours alone;
398766Scarlsonj 	 * no need to worry about conflicting mounts.
399766Scarlsonj 	 */
400766Scarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
401766Scarlsonj 	    mnp++) {
402766Scarlsonj 		if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
403766Scarlsonj 			break;
404766Scarlsonj 	}
405766Scarlsonj 	if (mnp >= resolve_lofs_mnt_max)
406766Scarlsonj 		return (0);
407766Scarlsonj 
408766Scarlsonj 	/*
409766Scarlsonj 	 * Convert this duplicate mount into a lofs mount.
410766Scarlsonj 	 */
411766Scarlsonj 	(void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
412766Scarlsonj 	    sizeof (fsptr->zone_fs_special));
413766Scarlsonj 	(void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
414766Scarlsonj 	    sizeof (fsptr->zone_fs_type));
415766Scarlsonj 	fsptr->zone_fs_raw[0] = '\0';
416766Scarlsonj 
417766Scarlsonj 	/*
41812734Sgary.pennington@oracle.com 	 * Discard all but one of the original options and set that to our
41912734Sgary.pennington@oracle.com 	 * default set of options used for resources.
420766Scarlsonj 	 */
421766Scarlsonj 	optptr = fsptr->zone_fs_options;
422766Scarlsonj 	if (optptr == NULL) {
423766Scarlsonj 		optptr = malloc(sizeof (*optptr));
424766Scarlsonj 		if (optptr == NULL) {
425766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s",
426766Scarlsonj 			    fsptr->zone_fs_dir);
427766Scarlsonj 			return (-1);
428766Scarlsonj 		}
429766Scarlsonj 	} else {
430766Scarlsonj 		while ((onext = optptr->zone_fsopt_next) != NULL) {
431766Scarlsonj 			optptr->zone_fsopt_next = onext->zone_fsopt_next;
432766Scarlsonj 			free(onext);
433766Scarlsonj 		}
434766Scarlsonj 	}
43512734Sgary.pennington@oracle.com 	(void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
436766Scarlsonj 	optptr->zone_fsopt_next = NULL;
437766Scarlsonj 	fsptr->zone_fs_options = optptr;
438766Scarlsonj 	return (0);
439766Scarlsonj }
440766Scarlsonj 
4415182Sedp int
make_one_dir(zlog_t * zlogp,const char * prefix,const char * subdir,mode_t mode,uid_t userid,gid_t groupid)4423813Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
4433813Sdp     uid_t userid, gid_t groupid)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	char path[MAXPATHLEN];
4460Sstevel@tonic-gate 	struct stat st;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4490Sstevel@tonic-gate 	    sizeof (path)) {
4500Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4510Sstevel@tonic-gate 		    subdir);
4520Sstevel@tonic-gate 		return (-1);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if (lstat(path, &st) == 0) {
4560Sstevel@tonic-gate 		/*
4570Sstevel@tonic-gate 		 * We don't check the file mode since presumably the zone
4580Sstevel@tonic-gate 		 * administrator may have had good reason to change the mode,
4590Sstevel@tonic-gate 		 * and we don't need to second guess him.
4600Sstevel@tonic-gate 		 */
4610Sstevel@tonic-gate 		if (!S_ISDIR(st.st_mode)) {
4627714SRic.Aleshire@Sun.COM 			if (S_ISREG(st.st_mode)) {
4631676Sjpk 				/*
4647714SRic.Aleshire@Sun.COM 				 * Allow readonly mounts of /etc/ files; this
4657714SRic.Aleshire@Sun.COM 				 * is needed most by Trusted Extensions.
4661676Sjpk 				 */
4671676Sjpk 				if (strncmp(subdir, "/etc/",
4681676Sjpk 				    strlen("/etc/")) != 0) {
4691676Sjpk 					zerror(zlogp, B_FALSE,
4701676Sjpk 					    "%s is not in /etc", path);
4711676Sjpk 					return (-1);
4721676Sjpk 				}
4731676Sjpk 			} else {
4741676Sjpk 				zerror(zlogp, B_FALSE,
4751676Sjpk 				    "%s is not a directory", path);
4761676Sjpk 				return (-1);
4771676Sjpk 			}
4780Sstevel@tonic-gate 		}
4793813Sdp 		return (0);
4803813Sdp 	}
4813813Sdp 
4823813Sdp 	if (mkdirp(path, mode) != 0) {
4830Sstevel@tonic-gate 		if (errno == EROFS)
4840Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4850Sstevel@tonic-gate 			    "a read-only file system in this local zone.\nMake "
4860Sstevel@tonic-gate 			    "sure %s exists in the global zone.", path, subdir);
4870Sstevel@tonic-gate 		else
4880Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4890Sstevel@tonic-gate 		return (-1);
4900Sstevel@tonic-gate 	}
4913813Sdp 
4923813Sdp 	(void) chown(path, userid, groupid);
4930Sstevel@tonic-gate 	return (0);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate static void
free_remote_fstypes(char ** types)4970Sstevel@tonic-gate free_remote_fstypes(char **types)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate 	uint_t i;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	if (types == NULL)
5020Sstevel@tonic-gate 		return;
5030Sstevel@tonic-gate 	for (i = 0; types[i] != NULL; i++)
5040Sstevel@tonic-gate 		free(types[i]);
5050Sstevel@tonic-gate 	free(types);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate static char **
get_remote_fstypes(zlog_t * zlogp)5090Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	char **types = NULL;
5120Sstevel@tonic-gate 	FILE *fp;
5130Sstevel@tonic-gate 	char buf[MAXPATHLEN];
5140Sstevel@tonic-gate 	char fstype[MAXPATHLEN];
5150Sstevel@tonic-gate 	uint_t lines = 0;
5160Sstevel@tonic-gate 	uint_t i;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5190Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5200Sstevel@tonic-gate 		return (NULL);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * Count the number of lines
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL)
5260Sstevel@tonic-gate 		lines++;
5270Sstevel@tonic-gate 	if (lines == 0)	/* didn't read anything; empty file */
5280Sstevel@tonic-gate 		goto out;
5290Sstevel@tonic-gate 	rewind(fp);
5300Sstevel@tonic-gate 	/*
5310Sstevel@tonic-gate 	 * Allocate enough space for a NULL-terminated array.
5320Sstevel@tonic-gate 	 */
5330Sstevel@tonic-gate 	types = calloc(lines + 1, sizeof (char *));
5340Sstevel@tonic-gate 	if (types == NULL) {
5350Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
5360Sstevel@tonic-gate 		goto out;
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 	i = 0;
5390Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
5400Sstevel@tonic-gate 		/* LINTED - fstype is big enough to hold buf */
5410Sstevel@tonic-gate 		if (sscanf(buf, "%s", fstype) == 0) {
5420Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5430Sstevel@tonic-gate 			free_remote_fstypes(types);
5440Sstevel@tonic-gate 			types = NULL;
5450Sstevel@tonic-gate 			goto out;
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 		types[i] = strdup(fstype);
5480Sstevel@tonic-gate 		if (types[i] == NULL) {
5490Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "memory allocation failed");
5500Sstevel@tonic-gate 			free_remote_fstypes(types);
5510Sstevel@tonic-gate 			types = NULL;
5520Sstevel@tonic-gate 			goto out;
5530Sstevel@tonic-gate 		}
5540Sstevel@tonic-gate 		i++;
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate out:
5570Sstevel@tonic-gate 	(void) fclose(fp);
5580Sstevel@tonic-gate 	return (types);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate static boolean_t
is_remote_fstype(const char * fstype,char * const * remote_fstypes)5620Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	uint_t i;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if (remote_fstypes == NULL)
5670Sstevel@tonic-gate 		return (B_FALSE);
5680Sstevel@tonic-gate 	for (i = 0; remote_fstypes[i] != NULL; i++) {
5690Sstevel@tonic-gate 		if (strcmp(remote_fstypes[i], fstype) == 0)
5700Sstevel@tonic-gate 			return (B_TRUE);
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 	return (B_FALSE);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
575766Scarlsonj /*
576766Scarlsonj  * This converts a zone root path (normally of the form .../root) to a Live
577766Scarlsonj  * Upgrade scratch zone root (of the form .../lu).
578766Scarlsonj  */
5790Sstevel@tonic-gate static void
root_to_lu(zlog_t * zlogp,char * zroot,size_t zrootlen,boolean_t isresolved)580766Scarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5810Sstevel@tonic-gate {
582766Scarlsonj 	if (!isresolved && zonecfg_in_alt_root())
583766Scarlsonj 		resolve_lofs(zlogp, zroot, zrootlen);
584766Scarlsonj 	(void) strcpy(strrchr(zroot, '/') + 1, "lu");
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate /*
5880Sstevel@tonic-gate  * The general strategy for unmounting filesystems is as follows:
5890Sstevel@tonic-gate  *
5900Sstevel@tonic-gate  * - Remote filesystems may be dead, and attempting to contact them as
5910Sstevel@tonic-gate  * part of a regular unmount may hang forever; we want to always try to
5920Sstevel@tonic-gate  * forcibly unmount such filesystems and only fall back to regular
5930Sstevel@tonic-gate  * unmounts if the filesystem doesn't support forced unmounts.
5940Sstevel@tonic-gate  *
5950Sstevel@tonic-gate  * - We don't want to unnecessarily corrupt metadata on local
5960Sstevel@tonic-gate  * filesystems (ie UFS), so we want to start off with graceful unmounts,
5970Sstevel@tonic-gate  * and only escalate to doing forced unmounts if we get stuck.
5980Sstevel@tonic-gate  *
5990Sstevel@tonic-gate  * We start off walking backwards through the mount table.  This doesn't
6000Sstevel@tonic-gate  * give us strict ordering but ensures that we try to unmount submounts
6010Sstevel@tonic-gate  * first.  We thus limit the number of failed umount2(2) calls.
6020Sstevel@tonic-gate  *
6030Sstevel@tonic-gate  * The mechanism for determining if we're stuck is to count the number
6040Sstevel@tonic-gate  * of failed unmounts each iteration through the mount table.  This
6050Sstevel@tonic-gate  * gives us an upper bound on the number of filesystems which remain
6060Sstevel@tonic-gate  * mounted (autofs trigger nodes are dealt with separately).  If at the
6070Sstevel@tonic-gate  * end of one unmount+autofs_cleanup cycle we still have the same number
6080Sstevel@tonic-gate  * of mounts that we started out with, we're stuck and try a forced
6090Sstevel@tonic-gate  * unmount.  If that fails (filesystem doesn't support forced unmounts)
6100Sstevel@tonic-gate  * then we bail and are unable to teardown the zone.  If it succeeds,
6110Sstevel@tonic-gate  * we're no longer stuck so we continue with our policy of trying
6120Sstevel@tonic-gate  * graceful mounts first.
6130Sstevel@tonic-gate  *
6140Sstevel@tonic-gate  * Zone must be down (ie, no processes or threads active).
6150Sstevel@tonic-gate  */
6160Sstevel@tonic-gate static int
unmount_filesystems(zlog_t * zlogp,zoneid_t zoneid,boolean_t unmount_cmd)617766Scarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	int error = 0;
6200Sstevel@tonic-gate 	FILE *mnttab;
6210Sstevel@tonic-gate 	struct mnttab *mnts;
6220Sstevel@tonic-gate 	uint_t nmnt;
6230Sstevel@tonic-gate 	char zroot[MAXPATHLEN + 1];
6240Sstevel@tonic-gate 	size_t zrootlen;
6250Sstevel@tonic-gate 	uint_t oldcount = UINT_MAX;
6260Sstevel@tonic-gate 	boolean_t stuck = B_FALSE;
6270Sstevel@tonic-gate 	char **remote_fstypes = NULL;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6300Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "unable to determine zone root");
6310Sstevel@tonic-gate 		return (-1);
6320Sstevel@tonic-gate 	}
633766Scarlsonj 	if (unmount_cmd)
634766Scarlsonj 		root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	(void) strcat(zroot, "/");
6370Sstevel@tonic-gate 	zrootlen = strlen(zroot);
6380Sstevel@tonic-gate 
6391676Sjpk 	/*
6401676Sjpk 	 * For Trusted Extensions unmount each higher level zone's mount
6411676Sjpk 	 * of our zone's /export/home
6421676Sjpk 	 */
6431769Scarlsonj 	if (!unmount_cmd)
6441769Scarlsonj 		tsol_unmounts(zlogp, zone_name);
6451676Sjpk 
6460Sstevel@tonic-gate 	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6470Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6480Sstevel@tonic-gate 		return (-1);
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 	/*
6510Sstevel@tonic-gate 	 * Use our hacky mntfs ioctl so we see everything, even mounts with
6520Sstevel@tonic-gate 	 * MS_NOMNTTAB.
6530Sstevel@tonic-gate 	 */
6540Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6550Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6560Sstevel@tonic-gate 		error++;
6570Sstevel@tonic-gate 		goto out;
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/*
6610Sstevel@tonic-gate 	 * Build the list of remote fstypes so we know which ones we
6620Sstevel@tonic-gate 	 * should forcibly unmount.
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	remote_fstypes = get_remote_fstypes(zlogp);
6650Sstevel@tonic-gate 	for (; /* ever */; ) {
6660Sstevel@tonic-gate 		uint_t newcount = 0;
6670Sstevel@tonic-gate 		boolean_t unmounted;
6680Sstevel@tonic-gate 		struct mnttab *mnp;
6690Sstevel@tonic-gate 		char *path;
6700Sstevel@tonic-gate 		uint_t i;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 		mnts = NULL;
6730Sstevel@tonic-gate 		nmnt = 0;
6740Sstevel@tonic-gate 		/*
6750Sstevel@tonic-gate 		 * MNTTAB gives us a way to walk through mounted
6760Sstevel@tonic-gate 		 * filesystems; we need to be able to walk them in
6770Sstevel@tonic-gate 		 * reverse order, so we build a list of all mounted
6780Sstevel@tonic-gate 		 * filesystems.
6790Sstevel@tonic-gate 		 */
6800Sstevel@tonic-gate 		if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6810Sstevel@tonic-gate 		    &nmnt) != 0) {
6820Sstevel@tonic-gate 			error++;
6830Sstevel@tonic-gate 			goto out;
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 		for (i = 0; i < nmnt; i++) {
6860Sstevel@tonic-gate 			mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6870Sstevel@tonic-gate 			path = mnp->mnt_mountp;
6880Sstevel@tonic-gate 			unmounted = B_FALSE;
6890Sstevel@tonic-gate 			/*
6900Sstevel@tonic-gate 			 * Try forced unmount first for remote filesystems.
6910Sstevel@tonic-gate 			 *
6920Sstevel@tonic-gate 			 * Not all remote filesystems support forced unmounts,
6930Sstevel@tonic-gate 			 * so if this fails (ENOTSUP) we'll continue on
6940Sstevel@tonic-gate 			 * and try a regular unmount.
6950Sstevel@tonic-gate 			 */
6960Sstevel@tonic-gate 			if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
6970Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0)
6980Sstevel@tonic-gate 					unmounted = B_TRUE;
6990Sstevel@tonic-gate 			}
7000Sstevel@tonic-gate 			/*
7010Sstevel@tonic-gate 			 * Try forced unmount if we're stuck.
7020Sstevel@tonic-gate 			 */
7030Sstevel@tonic-gate 			if (stuck) {
7040Sstevel@tonic-gate 				if (umount2(path, MS_FORCE) == 0) {
7050Sstevel@tonic-gate 					unmounted = B_TRUE;
7060Sstevel@tonic-gate 					stuck = B_FALSE;
7070Sstevel@tonic-gate 				} else {
7080Sstevel@tonic-gate 					/*
7090Sstevel@tonic-gate 					 * The first failure indicates a
7100Sstevel@tonic-gate 					 * mount we won't be able to get
7110Sstevel@tonic-gate 					 * rid of automatically, so we
7120Sstevel@tonic-gate 					 * bail.
7130Sstevel@tonic-gate 					 */
7140Sstevel@tonic-gate 					error++;
7150Sstevel@tonic-gate 					zerror(zlogp, B_FALSE,
7160Sstevel@tonic-gate 					    "unable to unmount '%s'", path);
7170Sstevel@tonic-gate 					free_mnttable(mnts, nmnt);
7180Sstevel@tonic-gate 					goto out;
7190Sstevel@tonic-gate 				}
7200Sstevel@tonic-gate 			}
7210Sstevel@tonic-gate 			/*
7220Sstevel@tonic-gate 			 * Try regular unmounts for everything else.
7230Sstevel@tonic-gate 			 */
7240Sstevel@tonic-gate 			if (!unmounted && umount2(path, 0) != 0)
7250Sstevel@tonic-gate 				newcount++;
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 		free_mnttable(mnts, nmnt);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 		if (newcount == 0)
7300Sstevel@tonic-gate 			break;
7310Sstevel@tonic-gate 		if (newcount >= oldcount) {
7320Sstevel@tonic-gate 			/*
7330Sstevel@tonic-gate 			 * Last round didn't unmount anything; we're stuck and
7340Sstevel@tonic-gate 			 * should start trying forced unmounts.
7350Sstevel@tonic-gate 			 */
7360Sstevel@tonic-gate 			stuck = B_TRUE;
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 		oldcount = newcount;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		/*
7410Sstevel@tonic-gate 		 * Autofs doesn't let you unmount its trigger nodes from
7420Sstevel@tonic-gate 		 * userland so we have to tell the kernel to cleanup for us.
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		if (autofs_cleanup(zoneid) != 0) {
7450Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7460Sstevel@tonic-gate 			error++;
7470Sstevel@tonic-gate 			goto out;
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate out:
7520Sstevel@tonic-gate 	free_remote_fstypes(remote_fstypes);
7530Sstevel@tonic-gate 	(void) fclose(mnttab);
7540Sstevel@tonic-gate 	return (error ? -1 : 0);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate static int
fs_compare(const void * m1,const void * m2)7580Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	struct zone_fstab *i = (struct zone_fstab *)m1;
7610Sstevel@tonic-gate 	struct zone_fstab *j = (struct zone_fstab *)m2;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate /*
7670Sstevel@tonic-gate  * Fork and exec (and wait for) the mentioned binary with the provided
7680Sstevel@tonic-gate  * arguments.  Returns (-1) if something went wrong with fork(2) or exec(2),
7690Sstevel@tonic-gate  * returns the exit status otherwise.
7700Sstevel@tonic-gate  *
7710Sstevel@tonic-gate  * If we were unable to exec the provided pathname (for whatever
7720Sstevel@tonic-gate  * reason), we return the special token ZEXIT_EXEC.  The current value
7730Sstevel@tonic-gate  * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7740Sstevel@tonic-gate  * consumers of this function; any future consumers must make sure this
7750Sstevel@tonic-gate  * remains the case.
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate static int
forkexec(zlog_t * zlogp,const char * path,char * const argv[])7780Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	pid_t child_pid;
7810Sstevel@tonic-gate 	int child_status = 0;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * Do not let another thread localize a message while we are forking.
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	(void) mutex_lock(&msglock);
7870Sstevel@tonic-gate 	child_pid = fork();
7880Sstevel@tonic-gate 	(void) mutex_unlock(&msglock);
7890Sstevel@tonic-gate 	if (child_pid == -1) {
7900Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7910Sstevel@tonic-gate 		return (-1);
7920Sstevel@tonic-gate 	} else if (child_pid == 0) {
7930Sstevel@tonic-gate 		closefrom(0);
7941915Sgjelinek 		/* redirect stdin, stdout & stderr to /dev/null */
7951915Sgjelinek 		(void) open("/dev/null", O_RDONLY);	/* stdin */
7961915Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stdout */
7971915Sgjelinek 		(void) open("/dev/null", O_WRONLY);	/* stderr */
7980Sstevel@tonic-gate 		(void) execv(path, argv);
7990Sstevel@tonic-gate 		/*
8000Sstevel@tonic-gate 		 * Since we are in the child, there is no point calling zerror()
8010Sstevel@tonic-gate 		 * since there is nobody waiting to consume it.  So exit with a
8020Sstevel@tonic-gate 		 * special code that the parent will recognize and call zerror()
8030Sstevel@tonic-gate 		 * accordingly.
8040Sstevel@tonic-gate 		 */
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 		_exit(ZEXIT_EXEC);
8070Sstevel@tonic-gate 	} else {
8080Sstevel@tonic-gate 		(void) waitpid(child_pid, &child_status, 0);
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	if (WIFSIGNALED(child_status)) {
8120Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8130Sstevel@tonic-gate 		    "signal %d", path, WTERMSIG(child_status));
8140Sstevel@tonic-gate 		return (-1);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 	assert(WIFEXITED(child_status));
8170Sstevel@tonic-gate 	if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
8180Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "failed to exec %s", path);
8190Sstevel@tonic-gate 		return (-1);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate static int
isregfile(const char * path)8256734Sjohnlev isregfile(const char *path)
8266734Sjohnlev {
8276734Sjohnlev 	struct stat64 st;
8286734Sjohnlev 
8296734Sjohnlev 	if (stat64(path, &st) == -1)
8306734Sjohnlev 		return (-1);
8316734Sjohnlev 
8326734Sjohnlev 	return (S_ISREG(st.st_mode));
8336734Sjohnlev }
8346734Sjohnlev 
8356734Sjohnlev static int
dofsck(zlog_t * zlogp,const char * fstype,const char * rawdev)8360Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
83911581SFrank.Batschulat@Sun.COM 	char *argv[5];
8400Sstevel@tonic-gate 	int status;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	/*
8430Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8440Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8450Sstevel@tonic-gate 	 */
8460Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8472712Snn35248 	    >= sizeof (cmdbuf)) {
8480Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8490Sstevel@tonic-gate 		return (-1);
8500Sstevel@tonic-gate 	}
8510Sstevel@tonic-gate 
8526734Sjohnlev 	/*
8536734Sjohnlev 	 * If it doesn't exist, that's OK: we verified this previously
8546734Sjohnlev 	 * in zoneadm.
8556734Sjohnlev 	 */
8566734Sjohnlev 	if (isregfile(cmdbuf) == -1)
8576734Sjohnlev 		return (0);
8586734Sjohnlev 
8590Sstevel@tonic-gate 	argv[0] = "fsck";
86011581SFrank.Batschulat@Sun.COM 	argv[1] = "-o";
86111581SFrank.Batschulat@Sun.COM 	argv[2] = "p";
86211581SFrank.Batschulat@Sun.COM 	argv[3] = (char *)rawdev;
86311581SFrank.Batschulat@Sun.COM 	argv[4] = NULL;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
8660Sstevel@tonic-gate 	if (status == 0 || status == -1)
8670Sstevel@tonic-gate 		return (status);
8680Sstevel@tonic-gate 	zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8690Sstevel@tonic-gate 	    "run fsck manually", rawdev, status);
8700Sstevel@tonic-gate 	return (-1);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate static int
domount(zlog_t * zlogp,const char * fstype,const char * opts,const char * special,const char * directory)8740Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8750Sstevel@tonic-gate     const char *special, const char *directory)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
8780Sstevel@tonic-gate 	char *argv[6];
8790Sstevel@tonic-gate 	int status;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/*
8820Sstevel@tonic-gate 	 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8830Sstevel@tonic-gate 	 * that would cost us an extra fork/exec without buying us anything.
8840Sstevel@tonic-gate 	 */
8850Sstevel@tonic-gate 	if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8862712Snn35248 	    >= sizeof (cmdbuf)) {
8870Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8880Sstevel@tonic-gate 		return (-1);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	argv[0] = "mount";
8910Sstevel@tonic-gate 	if (opts[0] == '\0') {
8920Sstevel@tonic-gate 		argv[1] = (char *)special;
8930Sstevel@tonic-gate 		argv[2] = (char *)directory;
8940Sstevel@tonic-gate 		argv[3] = NULL;
8950Sstevel@tonic-gate 	} else {
8960Sstevel@tonic-gate 		argv[1] = "-o";
8970Sstevel@tonic-gate 		argv[2] = (char *)opts;
8980Sstevel@tonic-gate 		argv[3] = (char *)special;
8990Sstevel@tonic-gate 		argv[4] = (char *)directory;
9000Sstevel@tonic-gate 		argv[5] = NULL;
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	status = forkexec(zlogp, cmdbuf, argv);
9040Sstevel@tonic-gate 	if (status == 0 || status == -1)
9050Sstevel@tonic-gate 		return (status);
9060Sstevel@tonic-gate 	if (opts[0] == '\0')
9070Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s %s %s\" "
9080Sstevel@tonic-gate 		    "failed with exit code %d",
9090Sstevel@tonic-gate 		    cmdbuf, special, directory, status);
9100Sstevel@tonic-gate 	else
9110Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
9120Sstevel@tonic-gate 		    "failed with exit code %d",
9130Sstevel@tonic-gate 		    cmdbuf, opts, special, directory, status);
9140Sstevel@tonic-gate 	return (-1);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate /*
9185182Sedp  * Check if a given mount point path exists.
9195182Sedp  * If it does, make sure it doesn't contain any symlinks.
9205182Sedp  * Note that if "leaf" is false we're checking an intermediate
9215182Sedp  * component of the mount point path, so it must be a directory.
9225182Sedp  * If "leaf" is true, then we're checking the entire mount point
9235182Sedp  * path, so the mount point itself can be anything aside from a
9245182Sedp  * symbolic link.
9255182Sedp  *
9265182Sedp  * If the path is invalid then a negative value is returned.  If the
9275182Sedp  * path exists and is a valid mount point path then 0 is returned.
9285182Sedp  * If the path doesn't exist return a positive value.
9290Sstevel@tonic-gate  */
9300Sstevel@tonic-gate static int
valid_mount_point(zlog_t * zlogp,const char * path,const boolean_t leaf)9315182Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	struct stat statbuf;
9340Sstevel@tonic-gate 	char respath[MAXPATHLEN];
9350Sstevel@tonic-gate 	int res;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (lstat(path, &statbuf) != 0) {
9380Sstevel@tonic-gate 		if (errno == ENOENT)
9395182Sedp 			return (1);
9400Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "can't stat %s", path);
9410Sstevel@tonic-gate 		return (-1);
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 	if (S_ISLNK(statbuf.st_mode)) {
9440Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is a symlink", path);
9450Sstevel@tonic-gate 		return (-1);
9460Sstevel@tonic-gate 	}
9475182Sedp 	if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9485182Sedp 		zerror(zlogp, B_FALSE, "%s is not a directory", path);
9495182Sedp 		return (-1);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 	if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9520Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9530Sstevel@tonic-gate 		return (-1);
9540Sstevel@tonic-gate 	}
9550Sstevel@tonic-gate 	respath[res] = '\0';
9560Sstevel@tonic-gate 	if (strcmp(path, respath) != 0) {
9570Sstevel@tonic-gate 		/*
9585182Sedp 		 * We don't like ".."s, "."s, or "//"s throwing us off
9590Sstevel@tonic-gate 		 */
9600Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9610Sstevel@tonic-gate 		return (-1);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 	return (0);
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate /*
9675182Sedp  * Validate a mount point path.  A valid mount point path is an
9685182Sedp  * absolute path that either doesn't exist, or, if it does exists it
9695182Sedp  * must be an absolute canonical path that doesn't have any symbolic
9705182Sedp  * links in it.  The target of a mount point path can be any filesystem
9715182Sedp  * object.  (Different filesystems can support different mount points,
9725182Sedp  * for example "lofs" and "mntfs" both support files and directories
9735182Sedp  * while "ufs" just supports directories.)
9740Sstevel@tonic-gate  *
9755182Sedp  * If the path is invalid then a negative value is returned.  If the
9765182Sedp  * path exists and is a valid mount point path then 0 is returned.
9775182Sedp  * If the path doesn't exist return a positive value.
9780Sstevel@tonic-gate  */
9795182Sedp int
valid_mount_path(zlog_t * zlogp,const char * rootpath,const char * spec,const char * dir,const char * fstype)9805182Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
9815182Sedp     const char *dir, const char *fstype)
9820Sstevel@tonic-gate {
9835182Sedp 	char abspath[MAXPATHLEN], *slashp, *slashp_next;
9845182Sedp 	int rv;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	/*
9875182Sedp 	 * Sanity check the target mount point path.
9885182Sedp 	 * It must be a non-null string that starts with a '/'.
9890Sstevel@tonic-gate 	 */
9905182Sedp 	if (dir[0] != '/') {
99112734Sgary.pennington@oracle.com 		/* Something went wrong. */
99212734Sgary.pennington@oracle.com 		zerror(zlogp, B_FALSE, "invalid mount directory, "
99312734Sgary.pennington@oracle.com 		    "type: \"%s\", special: \"%s\", dir: \"%s\"",
99412734Sgary.pennington@oracle.com 		    fstype, spec, dir);
9955182Sedp 		return (-1);
9965182Sedp 	}
9975182Sedp 
9985182Sedp 	/*
9995182Sedp 	 * Join rootpath and dir.  Make sure abspath ends with '/', this
10005182Sedp 	 * is added to all paths (even non-directory paths) to allow us
10015182Sedp 	 * to detect the end of paths below.  If the path already ends
10025182Sedp 	 * in a '/', then that's ok too (although we'll fail the
10035182Sedp 	 * cannonical path check in valid_mount_point()).
10045182Sedp 	 */
10055182Sedp 	if (snprintf(abspath, sizeof (abspath),
10065182Sedp 	    "%s%s/", rootpath, dir) >= sizeof (abspath)) {
10075182Sedp 		zerror(zlogp, B_FALSE, "pathname %s%s is too long",
10085182Sedp 		    rootpath, dir);
10095182Sedp 		return (-1);
10105182Sedp 	}
10115182Sedp 
10125182Sedp 	/*
10135182Sedp 	 * Starting with rootpath, verify the mount path one component
10145182Sedp 	 * at a time.  Continue until we've evaluated all of abspath.
10155182Sedp 	 */
10160Sstevel@tonic-gate 	slashp = &abspath[strlen(rootpath)];
10170Sstevel@tonic-gate 	assert(*slashp == '/');
10180Sstevel@tonic-gate 	do {
10195182Sedp 		slashp_next = strchr(slashp + 1, '/');
10200Sstevel@tonic-gate 		*slashp = '\0';
10215182Sedp 		if (slashp_next != NULL) {
10225182Sedp 			/* This is an intermediary mount path component. */
10235182Sedp 			rv = valid_mount_point(zlogp, abspath, B_FALSE);
10245182Sedp 		} else {
10255182Sedp 			/* This is the last component of the mount path. */
10265182Sedp 			rv = valid_mount_point(zlogp, abspath, B_TRUE);
10275182Sedp 		}
10285182Sedp 		if (rv < 0)
10295182Sedp 			return (rv);
10300Sstevel@tonic-gate 		*slashp = '/';
10315182Sedp 	} while ((slashp = slashp_next) != NULL);
10325182Sedp 	return (rv);
10330Sstevel@tonic-gate }
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate static int
mount_one_dev_device_cb(void * arg,const char * match,const char * name)10362712Snn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10372712Snn35248 {
10382712Snn35248 	di_prof_t prof = arg;
10392712Snn35248 
10402712Snn35248 	if (name == NULL)
10412712Snn35248 		return (di_prof_add_dev(prof, match));
10422712Snn35248 	return (di_prof_add_map(prof, match, name));
10432712Snn35248 }
10442712Snn35248 
10452712Snn35248 static int
mount_one_dev_symlink_cb(void * arg,const char * source,const char * target)10462712Snn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10472712Snn35248 {
10482712Snn35248 	di_prof_t prof = arg;
10492712Snn35248 
10502712Snn35248 	return (di_prof_add_symlink(prof, source, target));
10512712Snn35248 }
10522712Snn35248 
105310616SSebastien.Roy@Sun.COM int
vplat_get_iptype(zlog_t * zlogp,zone_iptype_t * iptypep)105410616SSebastien.Roy@Sun.COM vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
10553448Sdh155122 {
10563448Sdh155122 	zone_dochandle_t handle;
10573448Sdh155122 
10583448Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
10593448Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
10603448Sdh155122 		return (-1);
10613448Sdh155122 	}
10623448Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
10633448Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
10643448Sdh155122 		zonecfg_fini_handle(handle);
10653448Sdh155122 		return (-1);
10663448Sdh155122 	}
10673448Sdh155122 	if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
10683448Sdh155122 		zerror(zlogp, B_FALSE, "invalid ip-type configuration");
10693448Sdh155122 		zonecfg_fini_handle(handle);
10703448Sdh155122 		return (-1);
10713448Sdh155122 	}
10723448Sdh155122 	zonecfg_fini_handle(handle);
10733448Sdh155122 	return (0);
10743448Sdh155122 }
10753448Sdh155122 
10762712Snn35248 /*
10772712Snn35248  * Apply the standard lists of devices/symlinks/mappings and the user-specified
10782712Snn35248  * list of devices (via zonecfg) to the /dev filesystem.  The filesystem will
10792712Snn35248  * use these as a profile/filter to determine what exists in /dev.
10802712Snn35248  */
10812712Snn35248 static int
mount_one_dev(zlog_t * zlogp,char * devpath,zone_mnt_t mount_cmd)10827655Sgerald.jelinek@sun.com mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10832712Snn35248 {
10842712Snn35248 	char			brand[MAXNAMELEN];
10852712Snn35248 	zone_dochandle_t	handle = NULL;
10862727Sedp 	brand_handle_t		bh = NULL;
10872712Snn35248 	struct zone_devtab	ztab;
10882712Snn35248 	di_prof_t		prof = NULL;
10892712Snn35248 	int			err;
10902712Snn35248 	int			retval = -1;
10913448Sdh155122 	zone_iptype_t		iptype;
10923448Sdh155122 	const char 		*curr_iptype;
10932712Snn35248 
10942712Snn35248 	if (di_prof_init(devpath, &prof)) {
10952712Snn35248 		zerror(zlogp, B_TRUE, "failed to initialize profile");
10962712Snn35248 		goto cleanup;
10972712Snn35248 	}
10982712Snn35248 
10997655Sgerald.jelinek@sun.com 	/*
11007655Sgerald.jelinek@sun.com 	 * Get a handle to the brand info for this zone.
110110943SEdward.Pilatowicz@Sun.COM 	 * If we are mounting the zone, then we must always use the default
11027655Sgerald.jelinek@sun.com 	 * brand device mounts.
11037655Sgerald.jelinek@sun.com 	 */
11047655Sgerald.jelinek@sun.com 	if (ALT_MOUNT(mount_cmd)) {
110510943SEdward.Pilatowicz@Sun.COM 		(void) strlcpy(brand, default_brand, sizeof (brand));
11067655Sgerald.jelinek@sun.com 	} else {
110710796SStephen.Lawrence@Sun.COM 		(void) strlcpy(brand, brand_name, sizeof (brand));
11087655Sgerald.jelinek@sun.com 	}
11097655Sgerald.jelinek@sun.com 
11107655Sgerald.jelinek@sun.com 	if ((bh = brand_open(brand)) == NULL) {
11112712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
11122712Snn35248 		goto cleanup;
11132712Snn35248 	}
11142712Snn35248 
111510616SSebastien.Roy@Sun.COM 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
11163448Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
11173448Sdh155122 		goto cleanup;
11183448Sdh155122 	}
11193448Sdh155122 	switch (iptype) {
11203448Sdh155122 	case ZS_SHARED:
11213448Sdh155122 		curr_iptype = "shared";
11223448Sdh155122 		break;
11233448Sdh155122 	case ZS_EXCLUSIVE:
11243448Sdh155122 		curr_iptype = "exclusive";
11253448Sdh155122 		break;
11263448Sdh155122 	}
11273448Sdh155122 
11282727Sedp 	if (brand_platform_iter_devices(bh, zone_name,
11293448Sdh155122 	    mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11302712Snn35248 		zerror(zlogp, B_TRUE, "failed to add standard device");
11312712Snn35248 		goto cleanup;
11322712Snn35248 	}
11332712Snn35248 
11342727Sedp 	if (brand_platform_iter_link(bh,
11352712Snn35248 	    mount_one_dev_symlink_cb, prof) != 0) {
11362712Snn35248 		zerror(zlogp, B_TRUE, "failed to add standard symlink");
11372712Snn35248 		goto cleanup;
11382712Snn35248 	}
11392712Snn35248 
11402712Snn35248 	/* Add user-specified devices and directories */
11412712Snn35248 	if ((handle = zonecfg_init_handle()) == NULL) {
11422712Snn35248 		zerror(zlogp, B_FALSE, "can't initialize zone handle");
11432712Snn35248 		goto cleanup;
11442712Snn35248 	}
11452712Snn35248 	if (err = zonecfg_get_handle(zone_name, handle)) {
11462712Snn35248 		zerror(zlogp, B_FALSE, "can't get handle for zone "
11472712Snn35248 		    "%s: %s", zone_name, zonecfg_strerror(err));
11482712Snn35248 		goto cleanup;
11492712Snn35248 	}
11502712Snn35248 	if (err = zonecfg_setdevent(handle)) {
11512712Snn35248 		zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11522712Snn35248 		    zonecfg_strerror(err));
11532712Snn35248 		goto cleanup;
11542712Snn35248 	}
11552712Snn35248 	while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11562712Snn35248 		if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11572712Snn35248 			zerror(zlogp, B_TRUE, "failed to add "
11582712Snn35248 			    "user-specified device");
11592712Snn35248 			goto cleanup;
11602712Snn35248 		}
11612712Snn35248 	}
11622712Snn35248 	(void) zonecfg_enddevent(handle);
11632712Snn35248 
11642712Snn35248 	/* Send profile to kernel */
11652712Snn35248 	if (di_prof_commit(prof)) {
11662712Snn35248 		zerror(zlogp, B_TRUE, "failed to commit profile");
11672712Snn35248 		goto cleanup;
11682712Snn35248 	}
11692712Snn35248 
11702712Snn35248 	retval = 0;
11712712Snn35248 
11722712Snn35248 cleanup:
11732727Sedp 	if (bh != NULL)
11742727Sedp 		brand_close(bh);
11753716Sgjelinek 	if (handle != NULL)
11762712Snn35248 		zonecfg_fini_handle(handle);
11772712Snn35248 	if (prof)
11782712Snn35248 		di_prof_fini(prof);
11792712Snn35248 	return (retval);
11802712Snn35248 }
11812712Snn35248 
11822712Snn35248 static int
mount_one(zlog_t * zlogp,struct zone_fstab * fsptr,const char * rootpath,zone_mnt_t mount_cmd)11837655Sgerald.jelinek@sun.com mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11847655Sgerald.jelinek@sun.com     zone_mnt_t mount_cmd)
11850Sstevel@tonic-gate {
11862712Snn35248 	char path[MAXPATHLEN];
11872712Snn35248 	char optstr[MAX_MNTOPT_STR];
11880Sstevel@tonic-gate 	zone_fsopt_t *optptr;
11892712Snn35248 	int rv;
11900Sstevel@tonic-gate 
11915182Sedp 	if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
11925182Sedp 	    fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
11930Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
11940Sstevel@tonic-gate 		    rootpath, fsptr->zone_fs_dir);
11950Sstevel@tonic-gate 		return (-1);
11965182Sedp 	} else if (rv > 0) {
11975182Sedp 		/* The mount point path doesn't exist, create it now. */
11985182Sedp 		if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
11995182Sedp 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
12005182Sedp 		    DEFAULT_DIR_GROUP) != 0) {
12015182Sedp 			zerror(zlogp, B_FALSE, "failed to create mount point");
12025182Sedp 			return (-1);
12035182Sedp 		}
12045182Sedp 
12055182Sedp 		/*
12065182Sedp 		 * Now this might seem weird, but we need to invoke
12075182Sedp 		 * valid_mount_path() again.  Why?  Because it checks
12085182Sedp 		 * to make sure that the mount point path is canonical,
12095182Sedp 		 * which it can only do if the path exists, so now that
12105182Sedp 		 * we've created the path we have to verify it again.
12115182Sedp 		 */
12125182Sedp 		if ((rv = valid_mount_path(zlogp, rootpath,
12135182Sedp 		    fsptr->zone_fs_special, fsptr->zone_fs_dir,
12145182Sedp 		    fsptr->zone_fs_type)) < 0) {
12155182Sedp 			zerror(zlogp, B_FALSE,
12165182Sedp 			    "%s%s is not a valid mount point",
12175182Sedp 			    rootpath, fsptr->zone_fs_dir);
12185182Sedp 			return (-1);
12195182Sedp 		}
12205182Sedp 	}
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s", rootpath,
12230Sstevel@tonic-gate 	    fsptr->zone_fs_dir);
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	/*
12260Sstevel@tonic-gate 	 * In general the strategy here is to do just as much verification as
12270Sstevel@tonic-gate 	 * necessary to avoid crashing or otherwise doing something bad; if the
12280Sstevel@tonic-gate 	 * administrator initiated the operation via zoneadm(1m), he'll get
12290Sstevel@tonic-gate 	 * auto-verification which will let him know what's wrong.  If he
12300Sstevel@tonic-gate 	 * modifies the zone configuration of a running zone and doesn't attempt
12310Sstevel@tonic-gate 	 * to verify that it's OK we won't crash but won't bother trying to be
12320Sstevel@tonic-gate 	 * too helpful either.  zoneadm verify is only a couple keystrokes away.
12330Sstevel@tonic-gate 	 */
12340Sstevel@tonic-gate 	if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12350Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12360Sstevel@tonic-gate 		    "invalid file-system type %s", fsptr->zone_fs_special,
12370Sstevel@tonic-gate 		    fsptr->zone_fs_dir, fsptr->zone_fs_type);
12380Sstevel@tonic-gate 		return (-1);
12390Sstevel@tonic-gate 	}
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	/*
1242766Scarlsonj 	 * If we're looking at an alternate root environment, then construct
12433688Sedp 	 * read-only loopback mounts as necessary.  Note that any special
12443688Sedp 	 * paths for lofs zone mounts in an alternate root must have
12453688Sedp 	 * already been pre-pended with any alternate root path by the
12463688Sedp 	 * time we get here.
1247766Scarlsonj 	 */
1248766Scarlsonj 	if (zonecfg_in_alt_root()) {
1249766Scarlsonj 		struct stat64 st;
1250766Scarlsonj 
1251766Scarlsonj 		if (stat64(fsptr->zone_fs_special, &st) != -1 &&
12522772Scarlsonj 		    S_ISBLK(st.st_mode)) {
12533688Sedp 			/*
12543688Sedp 			 * If we're going to mount a block device we need
12553688Sedp 			 * to check if that device is already mounted
12563688Sedp 			 * somewhere else, and if so, do a lofs mount
12573688Sedp 			 * of the device instead of a direct mount
12583688Sedp 			 */
12592772Scarlsonj 			if (check_lofs_needed(zlogp, fsptr) == -1)
12602772Scarlsonj 				return (-1);
12612772Scarlsonj 		} else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
12623688Sedp 			/*
12633688Sedp 			 * For lofs mounts, the special node is inside the
12643688Sedp 			 * alternate root.  We need lofs resolution for
12653688Sedp 			 * this case in order to get at the underlying
12663688Sedp 			 * read-write path.
12673688Sedp 			 */
12683688Sedp 			resolve_lofs(zlogp, fsptr->zone_fs_special,
1269766Scarlsonj 			    sizeof (fsptr->zone_fs_special));
1270766Scarlsonj 		}
1271766Scarlsonj 	}
1272766Scarlsonj 
1273766Scarlsonj 	/*
12740Sstevel@tonic-gate 	 * Run 'fsck -m' if there's a device to fsck.
12750Sstevel@tonic-gate 	 */
12760Sstevel@tonic-gate 	if (fsptr->zone_fs_raw[0] != '\0' &&
12776734Sjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12780Sstevel@tonic-gate 		return (-1);
12796734Sjohnlev 	} else if (isregfile(fsptr->zone_fs_special) == 1 &&
12806734Sjohnlev 	    dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
12816734Sjohnlev 		return (-1);
12826734Sjohnlev 	}
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	/*
12850Sstevel@tonic-gate 	 * Build up mount option string.
12860Sstevel@tonic-gate 	 */
12870Sstevel@tonic-gate 	optstr[0] = '\0';
12880Sstevel@tonic-gate 	if (fsptr->zone_fs_options != NULL) {
12890Sstevel@tonic-gate 		(void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12900Sstevel@tonic-gate 		    sizeof (optstr));
12910Sstevel@tonic-gate 		for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
12920Sstevel@tonic-gate 		    optptr != NULL; optptr = optptr->zone_fsopt_next) {
12930Sstevel@tonic-gate 			(void) strlcat(optstr, ",", sizeof (optstr));
12940Sstevel@tonic-gate 			(void) strlcat(optstr, optptr->zone_fsopt_opt,
12950Sstevel@tonic-gate 			    sizeof (optstr));
12960Sstevel@tonic-gate 		}
12970Sstevel@tonic-gate 	}
12982712Snn35248 
12992712Snn35248 	if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13002712Snn35248 	    fsptr->zone_fs_special, path)) != 0)
13012712Snn35248 		return (rv);
13022712Snn35248 
13032712Snn35248 	/*
13042712Snn35248 	 * The mount succeeded.  If this was not a mount of /dev then
13052712Snn35248 	 * we're done.
13062712Snn35248 	 */
13072712Snn35248 	if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13082712Snn35248 		return (0);
13092712Snn35248 
13102712Snn35248 	/*
13112712Snn35248 	 * We just mounted an instance of a /dev filesystem, so now we
13122712Snn35248 	 * need to configure it.
13132712Snn35248 	 */
13147655Sgerald.jelinek@sun.com 	return (mount_one_dev(zlogp, path, mount_cmd));
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate static void
free_fs_data(struct zone_fstab * fsarray,uint_t nelem)13180Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate 	uint_t i;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	if (fsarray == NULL)
13230Sstevel@tonic-gate 		return;
13240Sstevel@tonic-gate 	for (i = 0; i < nelem; i++)
13250Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13260Sstevel@tonic-gate 	free(fsarray);
13270Sstevel@tonic-gate }
13280Sstevel@tonic-gate 
1329766Scarlsonj /*
13302653Svp157776  * This function initiates the creation of a small Solaris Environment for
13312653Svp157776  * scratch zone. The Environment creation process is split up into two
13322653Svp157776  * functions(build_mounted_pre_var() and build_mounted_post_var()). It
13332653Svp157776  * is done this way because:
13342653Svp157776  * 	We need to have both /etc and /var in the root of the scratchzone.
13352653Svp157776  * 	We loopback mount zone's own /etc and /var into the root of the
13362653Svp157776  * 	scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
13372653Svp157776  * 	need to delay the mount of /var till the zone's root gets populated.
13382653Svp157776  *	So mounting of localdirs[](/etc and /var) have been moved to the
13392653Svp157776  * 	build_mounted_post_var() which gets called only after the zone
13402653Svp157776  * 	specific filesystems are mounted.
13415829Sgjelinek  *
13425829Sgjelinek  * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13435829Sgjelinek  * does not loopback mount the zone's own /etc and /var into the root of the
13445829Sgjelinek  * scratch zone.
1345766Scarlsonj  */
1346766Scarlsonj static boolean_t
build_mounted_pre_var(zlog_t * zlogp,char * rootpath,size_t rootlen,const char * zonepath,char * luroot,size_t lurootlen)13472653Svp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
13483071Svp157776     size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1349766Scarlsonj {
1350766Scarlsonj 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1351766Scarlsonj 	const char **cpp;
1352766Scarlsonj 	static const char *mkdirs[] = {
13532592Sdp 		"/system", "/system/contract", "/system/object", "/proc",
13542592Sdp 		"/dev", "/tmp", "/a", NULL
1355766Scarlsonj 	};
13562653Svp157776 	char *altstr;
1357766Scarlsonj 	FILE *fp;
1358766Scarlsonj 	uuid_t uuid;
1359766Scarlsonj 
1360766Scarlsonj 	resolve_lofs(zlogp, rootpath, rootlen);
13613071Svp157776 	(void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
13623071Svp157776 	resolve_lofs(zlogp, luroot, lurootlen);
1363766Scarlsonj 	(void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1364766Scarlsonj 	(void) symlink("./usr/bin", tmp);
1365766Scarlsonj 
1366766Scarlsonj 	/*
1367766Scarlsonj 	 * These are mostly special mount points; not handled here.  (See
1368766Scarlsonj 	 * zone_mount_early.)
1369766Scarlsonj 	 */
1370766Scarlsonj 	for (cpp = mkdirs; *cpp != NULL; cpp++) {
1371766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1372766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1373766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1374766Scarlsonj 			return (B_FALSE);
1375766Scarlsonj 		}
1376766Scarlsonj 	}
13772653Svp157776 	/*
13782653Svp157776 	 * This is here to support lucopy.  If there's an instance of this same
13792653Svp157776 	 * zone on the current running system, then we mount its root up as
13802653Svp157776 	 * read-only inside the scratch zone.
13812653Svp157776 	 */
13822653Svp157776 	(void) zonecfg_get_uuid(zone_name, uuid);
13832653Svp157776 	altstr = strdup(zonecfg_get_root());
13842653Svp157776 	if (altstr == NULL) {
13852653Svp157776 		zerror(zlogp, B_TRUE, "memory allocation failed");
13862653Svp157776 		return (B_FALSE);
13872653Svp157776 	}
13882653Svp157776 	zonecfg_set_root("");
13892653Svp157776 	(void) strlcpy(tmp, zone_name, sizeof (tmp));
13902653Svp157776 	(void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
13912653Svp157776 	if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
13922653Svp157776 	    strcmp(fromdir, rootpath) != 0) {
13932653Svp157776 		(void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
13942653Svp157776 		if (mkdir(tmp, 0755) != 0) {
13952653Svp157776 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
13962653Svp157776 			return (B_FALSE);
13972653Svp157776 		}
139812734Sgary.pennington@oracle.com 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
13992653Svp157776 		    tmp) != 0) {
14002653Svp157776 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
14012653Svp157776 			    fromdir);
14022653Svp157776 			return (B_FALSE);
14032653Svp157776 		}
14042653Svp157776 	}
14052653Svp157776 	zonecfg_set_root(altstr);
14062653Svp157776 	free(altstr);
14072653Svp157776 
14082653Svp157776 	if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
14092653Svp157776 		zerror(zlogp, B_TRUE, "cannot open zone mapfile");
14102653Svp157776 		return (B_FALSE);
14112653Svp157776 	}
14122653Svp157776 	(void) ftruncate(fileno(fp), 0);
14132653Svp157776 	if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
14142653Svp157776 		zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
14152653Svp157776 	}
14162653Svp157776 	zonecfg_close_scratch(fp);
14172653Svp157776 	(void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
14182653Svp157776 	if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
14192653Svp157776 		return (B_FALSE);
14202653Svp157776 	(void) strlcpy(rootpath, tmp, rootlen);
14212653Svp157776 	return (B_TRUE);
14222653Svp157776 }
14232653Svp157776 
14242653Svp157776 
14252653Svp157776 static boolean_t
build_mounted_post_var(zlog_t * zlogp,zone_mnt_t mount_cmd,char * rootpath,const char * luroot)14265829Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14275829Sgjelinek     const char *luroot)
14282653Svp157776 {
14292653Svp157776 	char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
14302653Svp157776 	const char **cpp;
14315829Sgjelinek 	const char **loopdirs;
14325829Sgjelinek 	const char **tmpdirs;
14332653Svp157776 	static const char *localdirs[] = {
14342653Svp157776 		"/etc", "/var", NULL
14352653Svp157776 	};
14365829Sgjelinek 	static const char *scr_loopdirs[] = {
14372653Svp157776 		"/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
14382653Svp157776 		"/usr", NULL
14392653Svp157776 	};
14405829Sgjelinek 	static const char *upd_loopdirs[] = {
14415829Sgjelinek 		"/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14425829Sgjelinek 		"/usr", "/var", NULL
14435829Sgjelinek 	};
14445829Sgjelinek 	static const char *scr_tmpdirs[] = {
14452653Svp157776 		"/tmp", "/var/run", NULL
14462653Svp157776 	};
14475829Sgjelinek 	static const char *upd_tmpdirs[] = {
14485829Sgjelinek 		"/tmp", "/var/run", "/var/tmp", NULL
14495829Sgjelinek 	};
14502653Svp157776 	struct stat st;
14512653Svp157776 
14525829Sgjelinek 	if (mount_cmd == Z_MNT_SCRATCH) {
14535829Sgjelinek 		/*
14545829Sgjelinek 		 * These are mounted read-write from the zone undergoing
14555829Sgjelinek 		 * upgrade.  We must be careful not to 'leak' things from the
14565829Sgjelinek 		 * main system into the zone, and this accomplishes that goal.
14575829Sgjelinek 		 */
14585829Sgjelinek 		for (cpp = localdirs; *cpp != NULL; cpp++) {
14595829Sgjelinek 			(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
14605829Sgjelinek 			    *cpp);
14615829Sgjelinek 			(void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14625829Sgjelinek 			    rootpath, *cpp);
14635829Sgjelinek 			if (mkdir(tmp, 0755) != 0) {
14645829Sgjelinek 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
14655829Sgjelinek 				return (B_FALSE);
14665829Sgjelinek 			}
14675829Sgjelinek 			if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14685829Sgjelinek 			    != 0) {
14695829Sgjelinek 				zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14705829Sgjelinek 				    tmp, *cpp);
14715829Sgjelinek 				return (B_FALSE);
14725829Sgjelinek 			}
1473766Scarlsonj 		}
14745829Sgjelinek 	}
14755829Sgjelinek 
14765829Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
14775829Sgjelinek 		loopdirs = upd_loopdirs;
14785829Sgjelinek 	else
14795829Sgjelinek 		loopdirs = scr_loopdirs;
1480766Scarlsonj 
1481766Scarlsonj 	/*
1482766Scarlsonj 	 * These are things mounted read-only from the running system because
1483766Scarlsonj 	 * they contain binaries that must match system.
1484766Scarlsonj 	 */
1485766Scarlsonj 	for (cpp = loopdirs; *cpp != NULL; cpp++) {
1486766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1487766Scarlsonj 		if (mkdir(tmp, 0755) != 0) {
1488766Scarlsonj 			if (errno != EEXIST) {
1489766Scarlsonj 				zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1490766Scarlsonj 				return (B_FALSE);
1491766Scarlsonj 			}
1492766Scarlsonj 			if (lstat(tmp, &st) != 0) {
1493766Scarlsonj 				zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1494766Scarlsonj 				return (B_FALSE);
1495766Scarlsonj 			}
1496766Scarlsonj 			/*
1497766Scarlsonj 			 * Ignore any non-directories encountered.  These are
1498766Scarlsonj 			 * things that have been converted into symlinks
1499766Scarlsonj 			 * (/etc/fs and /etc/lib) and no longer need a lofs
1500766Scarlsonj 			 * fixup.
1501766Scarlsonj 			 */
1502766Scarlsonj 			if (!S_ISDIR(st.st_mode))
1503766Scarlsonj 				continue;
1504766Scarlsonj 		}
150512734Sgary.pennington@oracle.com 		if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1506766Scarlsonj 		    tmp) != 0) {
1507766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1508766Scarlsonj 			    *cpp);
1509766Scarlsonj 			return (B_FALSE);
1510766Scarlsonj 		}
1511766Scarlsonj 	}
1512766Scarlsonj 
15135829Sgjelinek 	if (mount_cmd == Z_MNT_UPDATE)
15145829Sgjelinek 		tmpdirs = upd_tmpdirs;
15155829Sgjelinek 	else
15165829Sgjelinek 		tmpdirs = scr_tmpdirs;
15175829Sgjelinek 
1518766Scarlsonj 	/*
1519766Scarlsonj 	 * These are things with tmpfs mounted inside.
1520766Scarlsonj 	 */
1521766Scarlsonj 	for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1522766Scarlsonj 		(void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15235829Sgjelinek 		if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15245829Sgjelinek 		    errno != EEXIST) {
1525766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1526766Scarlsonj 			return (B_FALSE);
1527766Scarlsonj 		}
15283514Sgjelinek 
15293514Sgjelinek 		/*
15303514Sgjelinek 		 * We could set the mode for /tmp when we do the mkdir but
15313514Sgjelinek 		 * since that can be modified by the umask we will just set
15323514Sgjelinek 		 * the correct mode for /tmp now.
15333514Sgjelinek 		 */
15343514Sgjelinek 		if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
15353514Sgjelinek 			zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
15363514Sgjelinek 			return (B_FALSE);
15373514Sgjelinek 		}
15383514Sgjelinek 
1539766Scarlsonj 		if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1540766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1541766Scarlsonj 			return (B_FALSE);
1542766Scarlsonj 		}
1543766Scarlsonj 	}
1544766Scarlsonj 	return (B_TRUE);
1545766Scarlsonj }
1546766Scarlsonj 
15472712Snn35248 typedef struct plat_gmount_cb_data {
15482712Snn35248 	zlog_t			*pgcd_zlogp;
15492712Snn35248 	struct zone_fstab	**pgcd_fs_tab;
15502712Snn35248 	int			*pgcd_num_fs;
15512712Snn35248 } plat_gmount_cb_data_t;
15522712Snn35248 
15532712Snn35248 /*
15542712Snn35248  * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15552712Snn35248  * through all global brand platform mounts.
15562712Snn35248  */
15572712Snn35248 int
plat_gmount_cb(void * data,const char * spec,const char * dir,const char * fstype,const char * opt)15582712Snn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15592712Snn35248     const char *fstype, const char *opt)
15602712Snn35248 {
15612712Snn35248 	plat_gmount_cb_data_t	*cp = data;
15622712Snn35248 	zlog_t			*zlogp = cp->pgcd_zlogp;
15632712Snn35248 	struct zone_fstab	*fs_ptr = *cp->pgcd_fs_tab;
15642712Snn35248 	int			num_fs = *cp->pgcd_num_fs;
15652712Snn35248 	struct zone_fstab	*fsp, *tmp_ptr;
15662712Snn35248 
15672712Snn35248 	num_fs++;
15682712Snn35248 	if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15692712Snn35248 		zerror(zlogp, B_TRUE, "memory allocation failed");
15702712Snn35248 		return (-1);
15712712Snn35248 	}
15722712Snn35248 
15732712Snn35248 	fs_ptr = tmp_ptr;
15742712Snn35248 	fsp = &fs_ptr[num_fs - 1];
15752712Snn35248 
15762712Snn35248 	/* update the callback struct passed in */
15772712Snn35248 	*cp->pgcd_fs_tab = fs_ptr;
15782712Snn35248 	*cp->pgcd_num_fs = num_fs;
15792712Snn35248 
15802712Snn35248 	fsp->zone_fs_raw[0] = '\0';
15812712Snn35248 	(void) strlcpy(fsp->zone_fs_special, spec,
15822712Snn35248 	    sizeof (fsp->zone_fs_special));
15832712Snn35248 	(void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15842712Snn35248 	(void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15852712Snn35248 	fsp->zone_fs_options = NULL;
15863688Sedp 	if ((opt != NULL) &&
15873688Sedp 	    (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15882712Snn35248 		zerror(zlogp, B_FALSE, "error adding property");
15892712Snn35248 		return (-1);
15902712Snn35248 	}
15912712Snn35248 
15922712Snn35248 	return (0);
15932712Snn35248 }
15942712Snn35248 
15952712Snn35248 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)15962712Snn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
15975829Sgjelinek     struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
15982712Snn35248 {
15992712Snn35248 	struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16002712Snn35248 	int num_fs;
16012712Snn35248 
16022712Snn35248 	num_fs = *num_fsp;
16032712Snn35248 	fs_ptr = *fs_tabp;
16042712Snn35248 
16052712Snn35248 	if (zonecfg_setfsent(handle) != Z_OK) {
16062712Snn35248 		zerror(zlogp, B_FALSE, "invalid configuration");
16072712Snn35248 		return (-1);
16082712Snn35248 	}
16092712Snn35248 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16102712Snn35248 		/*
16112712Snn35248 		 * ZFS filesystems will not be accessible under an alternate
16122712Snn35248 		 * root, since the pool will not be known.  Ignore them in this
16132712Snn35248 		 * case.
16142712Snn35248 		 */
16155829Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
16165829Sgjelinek 		    strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16172712Snn35248 			continue;
16182712Snn35248 
16192712Snn35248 		num_fs++;
16202712Snn35248 		if ((tmp_ptr = realloc(fs_ptr,
16212712Snn35248 		    num_fs * sizeof (*tmp_ptr))) == NULL) {
16222712Snn35248 			zerror(zlogp, B_TRUE, "memory allocation failed");
16232712Snn35248 			(void) zonecfg_endfsent(handle);
16242712Snn35248 			return (-1);
16252712Snn35248 		}
16262712Snn35248 		/* update the pointers passed in */
16272712Snn35248 		*fs_tabp = tmp_ptr;
16282712Snn35248 		*num_fsp = num_fs;
16292712Snn35248 
16302712Snn35248 		fs_ptr = tmp_ptr;
16312712Snn35248 		fsp = &fs_ptr[num_fs - 1];
16322712Snn35248 		(void) strlcpy(fsp->zone_fs_dir,
16332712Snn35248 		    fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16342712Snn35248 		(void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16352712Snn35248 		    sizeof (fsp->zone_fs_raw));
16362712Snn35248 		(void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16372712Snn35248 		    sizeof (fsp->zone_fs_type));
16382712Snn35248 		fsp->zone_fs_options = fstab.zone_fs_options;
16393688Sedp 
16403688Sedp 		/*
16413688Sedp 		 * For all lofs mounts, make sure that the 'special'
16423688Sedp 		 * entry points inside the alternate root.  The
16433688Sedp 		 * source path for a lofs mount in a given zone needs
16443688Sedp 		 * to be relative to the root of the boot environment
16453688Sedp 		 * that contains the zone.  Note that we don't do this
16463688Sedp 		 * for non-lofs mounts since they will have a device
16473688Sedp 		 * as a backing store and device paths must always be
16483688Sedp 		 * specified relative to the current boot environment.
16493688Sedp 		 */
16503688Sedp 		fsp->zone_fs_special[0] = '\0';
16513688Sedp 		if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
16523688Sedp 			(void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
16533688Sedp 			    sizeof (fsp->zone_fs_special));
16543688Sedp 		}
16553688Sedp 		(void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
16563688Sedp 		    sizeof (fsp->zone_fs_special));
16572712Snn35248 	}
16582712Snn35248 	(void) zonecfg_endfsent(handle);
16592712Snn35248 	return (0);
16602712Snn35248 }
16612712Snn35248 
16620Sstevel@tonic-gate static int
mount_filesystems(zlog_t * zlogp,zone_mnt_t mount_cmd)16635829Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
16640Sstevel@tonic-gate {
16652712Snn35248 	char rootpath[MAXPATHLEN];
16662712Snn35248 	char zonepath[MAXPATHLEN];
16672712Snn35248 	char brand[MAXNAMELEN];
16683071Svp157776 	char luroot[MAXPATHLEN];
16692712Snn35248 	int i, num_fs = 0;
16702712Snn35248 	struct zone_fstab *fs_ptr = NULL;
16710Sstevel@tonic-gate 	zone_dochandle_t handle = NULL;
16720Sstevel@tonic-gate 	zone_state_t zstate;
16732727Sedp 	brand_handle_t bh;
16742712Snn35248 	plat_gmount_cb_data_t cb;
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	if (zone_get_state(zone_name, &zstate) != Z_OK ||
1677766Scarlsonj 	    (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
16780Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
1679766Scarlsonj 		    "zone must be in '%s' or '%s' state to mount file-systems",
1680766Scarlsonj 		    zone_state_str(ZONE_STATE_READY),
1681766Scarlsonj 		    zone_state_str(ZONE_STATE_MOUNTED));
16820Sstevel@tonic-gate 		goto bad;
16830Sstevel@tonic-gate 	}
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
16860Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone path");
16870Sstevel@tonic-gate 		goto bad;
16880Sstevel@tonic-gate 	}
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
16910Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
16920Sstevel@tonic-gate 		goto bad;
16930Sstevel@tonic-gate 	}
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
16961645Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
16970Sstevel@tonic-gate 		goto bad;
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17000Sstevel@tonic-gate 	    zonecfg_setfsent(handle) != Z_OK) {
17010Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
17020Sstevel@tonic-gate 		goto bad;
17030Sstevel@tonic-gate 	}
17040Sstevel@tonic-gate 
17057655Sgerald.jelinek@sun.com 	/*
170610943SEdward.Pilatowicz@Sun.COM 	 * If we are mounting the zone, then we must always use the default
17077655Sgerald.jelinek@sun.com 	 * brand global mounts.
17087655Sgerald.jelinek@sun.com 	 */
17097655Sgerald.jelinek@sun.com 	if (ALT_MOUNT(mount_cmd)) {
171010943SEdward.Pilatowicz@Sun.COM 		(void) strlcpy(brand, default_brand, sizeof (brand));
17117655Sgerald.jelinek@sun.com 	} else {
171210796SStephen.Lawrence@Sun.COM 		(void) strlcpy(brand, brand_name, sizeof (brand));
17137655Sgerald.jelinek@sun.com 	}
17147655Sgerald.jelinek@sun.com 
17152712Snn35248 	/* Get a handle to the brand info for this zone */
17167655Sgerald.jelinek@sun.com 	if ((bh = brand_open(brand)) == NULL) {
17172712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
17183716Sgjelinek 		zonecfg_fini_handle(handle);
17192712Snn35248 		return (-1);
17202712Snn35248 	}
17212712Snn35248 
17222712Snn35248 	/*
17232712Snn35248 	 * Get the list of global filesystems to mount from the brand
17242712Snn35248 	 * configuration.
17252712Snn35248 	 */
17262712Snn35248 	cb.pgcd_zlogp = zlogp;
17272712Snn35248 	cb.pgcd_fs_tab = &fs_ptr;
17282712Snn35248 	cb.pgcd_num_fs = &num_fs;
17292727Sedp 	if (brand_platform_iter_gmounts(bh, zonepath,
17302712Snn35248 	    plat_gmount_cb, &cb) != 0) {
17312712Snn35248 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
17322727Sedp 		brand_close(bh);
17333716Sgjelinek 		zonecfg_fini_handle(handle);
17342712Snn35248 		return (-1);
17352712Snn35248 	}
17362727Sedp 	brand_close(bh);
17372712Snn35248 
17380Sstevel@tonic-gate 	/*
173912734Sgary.pennington@oracle.com 	 * Iterate through the rest of the filesystems. Sort them all,
174012734Sgary.pennington@oracle.com 	 * then mount them in sorted order. This is to make sure the
174112734Sgary.pennington@oracle.com 	 * higher level directories (e.g., /usr) get mounted before
174212734Sgary.pennington@oracle.com 	 * any beneath them (e.g., /usr/local).
17430Sstevel@tonic-gate 	 */
17442712Snn35248 	if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17452712Snn35248 	    mount_cmd) != 0)
17462712Snn35248 		goto bad;
17472712Snn35248 
17480Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
17490Sstevel@tonic-gate 	handle = NULL;
17500Sstevel@tonic-gate 
1751766Scarlsonj 	/*
17522712Snn35248 	 * Normally when we mount a zone all the zone filesystems
17532712Snn35248 	 * get mounted relative to rootpath, which is usually
17542712Snn35248 	 * <zonepath>/root.  But when mounting a zone for administration
17552712Snn35248 	 * purposes via the zone "mount" state, build_mounted_pre_var()
17562712Snn35248 	 * updates rootpath to be <zonepath>/lu/a so we'll mount all
17572712Snn35248 	 * the zones filesystems there instead.
17582712Snn35248 	 *
17592712Snn35248 	 * build_mounted_pre_var() and build_mounted_post_var() will
17602712Snn35248 	 * also do some extra work to create directories and lofs mount
17612712Snn35248 	 * a bunch of global zone file system paths into <zonepath>/lu.
17622712Snn35248 	 *
17632712Snn35248 	 * This allows us to be able to enter the zone (now rooted at
17642712Snn35248 	 * <zonepath>/lu) and run the upgrade/patch tools that are in the
17652712Snn35248 	 * global zone and have them upgrade the to-be-modified zone's
17662712Snn35248 	 * files mounted on /a.  (Which mirrors the existing standard
17672712Snn35248 	 * upgrade environment.)
17682712Snn35248 	 *
17692712Snn35248 	 * There is of course one catch.  When doing the upgrade
17702712Snn35248 	 * we need <zoneroot>/lu/dev to be the /dev filesystem
17712712Snn35248 	 * for the zone and we don't want to have any /dev filesystem
17722712Snn35248 	 * mounted at <zoneroot>/lu/a/dev.  Since /dev is specified
17732712Snn35248 	 * as a normal zone filesystem by default we'll try to mount
17742712Snn35248 	 * it at <zoneroot>/lu/a/dev, so we have to detect this
17752712Snn35248 	 * case and instead mount it at <zoneroot>/lu/dev.
17762712Snn35248 	 *
17772712Snn35248 	 * All this work is done in three phases:
17782653Svp157776 	 *   1) Create and populate lu directory (build_mounted_pre_var()).
17792653Svp157776 	 *   2) Mount the required filesystems as per the zone configuration.
17802653Svp157776 	 *   3) Set up the rest of the scratch zone environment
17812653Svp157776 	 *	(build_mounted_post_var()).
1782766Scarlsonj 	 */
17835829Sgjelinek 	if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
17843071Svp157776 	    rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1785766Scarlsonj 		goto bad;
1786766Scarlsonj 
17870Sstevel@tonic-gate 	qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
17882712Snn35248 
17890Sstevel@tonic-gate 	for (i = 0; i < num_fs; i++) {
17905829Sgjelinek 		if (ALT_MOUNT(mount_cmd) &&
17912712Snn35248 		    strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
17922712Snn35248 			size_t slen = strlen(rootpath) - 2;
17932712Snn35248 
17942712Snn35248 			/*
17952712Snn35248 			 * By default we'll try to mount /dev as /a/dev
17962712Snn35248 			 * but /dev is special and always goes at the top
17972712Snn35248 			 * so strip the trailing '/a' from the rootpath.
17982712Snn35248 			 */
17992712Snn35248 			assert(strcmp(&rootpath[slen], "/a") == 0);
18002712Snn35248 			rootpath[slen] = '\0';
18017655Sgerald.jelinek@sun.com 			if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18027655Sgerald.jelinek@sun.com 			    != 0)
18032712Snn35248 				goto bad;
18042712Snn35248 			rootpath[slen] = '/';
18052712Snn35248 			continue;
18062712Snn35248 		}
18077655Sgerald.jelinek@sun.com 		if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18080Sstevel@tonic-gate 			goto bad;
18090Sstevel@tonic-gate 	}
18105829Sgjelinek 	if (ALT_MOUNT(mount_cmd) &&
18115829Sgjelinek 	    !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
18122653Svp157776 		goto bad;
18131676Sjpk 
18141676Sjpk 	/*
18151676Sjpk 	 * For Trusted Extensions cross-mount each lower level /export/home
18161676Sjpk 	 */
18175829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
18185829Sgjelinek 	    tsol_mounts(zlogp, zone_name, rootpath) != 0)
18191676Sjpk 		goto bad;
18201676Sjpk 
18210Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	/*
18240Sstevel@tonic-gate 	 * Everything looks fine.
18250Sstevel@tonic-gate 	 */
18260Sstevel@tonic-gate 	return (0);
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate bad:
18290Sstevel@tonic-gate 	if (handle != NULL)
18300Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
18310Sstevel@tonic-gate 	free_fs_data(fs_ptr, num_fs);
18320Sstevel@tonic-gate 	return (-1);
18330Sstevel@tonic-gate }
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18360Sstevel@tonic-gate static int
addr2netmask(char * prefixstr,int maxprefixlen,uchar_t * maskstr)18370Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18380Sstevel@tonic-gate {
18390Sstevel@tonic-gate 	int prefixlen;
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 	prefixlen = atoi(prefixstr);
18420Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxprefixlen)
18430Sstevel@tonic-gate 		return (1);
18440Sstevel@tonic-gate 	while (prefixlen > 0) {
18450Sstevel@tonic-gate 		if (prefixlen >= 8) {
18460Sstevel@tonic-gate 			*maskstr++ = 0xFF;
18470Sstevel@tonic-gate 			prefixlen -= 8;
18480Sstevel@tonic-gate 			continue;
18490Sstevel@tonic-gate 		}
18500Sstevel@tonic-gate 		*maskstr |= 1 << (8 - prefixlen);
18510Sstevel@tonic-gate 		prefixlen--;
18520Sstevel@tonic-gate 	}
18530Sstevel@tonic-gate 	return (0);
18540Sstevel@tonic-gate }
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate /*
18570Sstevel@tonic-gate  * Tear down all interfaces belonging to the given zone.  This should
18580Sstevel@tonic-gate  * be called with the zone in a state other than "running", so that
18590Sstevel@tonic-gate  * interfaces can't be assigned to the zone after this returns.
18600Sstevel@tonic-gate  *
18610Sstevel@tonic-gate  * If anything goes wrong, log an error message and return an error.
18620Sstevel@tonic-gate  */
18630Sstevel@tonic-gate static int
unconfigure_shared_network_interfaces(zlog_t * zlogp,zoneid_t zone_id)18643448Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
18650Sstevel@tonic-gate {
18660Sstevel@tonic-gate 	struct lifnum lifn;
18670Sstevel@tonic-gate 	struct lifconf lifc;
18680Sstevel@tonic-gate 	struct lifreq *lifrp, lifrl;
18690Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
18700Sstevel@tonic-gate 	int num_ifs, s, i, ret_code = 0;
18710Sstevel@tonic-gate 	uint_t bufsize;
18720Sstevel@tonic-gate 	char *buf = NULL;
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
18750Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
18760Sstevel@tonic-gate 		ret_code = -1;
18770Sstevel@tonic-gate 		goto bad;
18780Sstevel@tonic-gate 	}
18790Sstevel@tonic-gate 	lifn.lifn_family = AF_UNSPEC;
18800Sstevel@tonic-gate 	lifn.lifn_flags = (int)lifc_flags;
18810Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
18820Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
18833448Sdh155122 		    "could not determine number of network interfaces");
18840Sstevel@tonic-gate 		ret_code = -1;
18850Sstevel@tonic-gate 		goto bad;
18860Sstevel@tonic-gate 	}
18870Sstevel@tonic-gate 	num_ifs = lifn.lifn_count;
18880Sstevel@tonic-gate 	bufsize = num_ifs * sizeof (struct lifreq);
18890Sstevel@tonic-gate 	if ((buf = malloc(bufsize)) == NULL) {
18900Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
18910Sstevel@tonic-gate 		ret_code = -1;
18920Sstevel@tonic-gate 		goto bad;
18930Sstevel@tonic-gate 	}
18940Sstevel@tonic-gate 	lifc.lifc_family = AF_UNSPEC;
18950Sstevel@tonic-gate 	lifc.lifc_flags = (int)lifc_flags;
18960Sstevel@tonic-gate 	lifc.lifc_len = bufsize;
18970Sstevel@tonic-gate 	lifc.lifc_buf = buf;
18980Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
18993448Sdh155122 		zerror(zlogp, B_TRUE, "could not get configured network "
19003448Sdh155122 		    "interfaces");
19010Sstevel@tonic-gate 		ret_code = -1;
19020Sstevel@tonic-gate 		goto bad;
19030Sstevel@tonic-gate 	}
19040Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
19050Sstevel@tonic-gate 	for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19060Sstevel@tonic-gate 		(void) close(s);
19070Sstevel@tonic-gate 		if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19080Sstevel@tonic-gate 		    0) {
19090Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get socket",
19100Sstevel@tonic-gate 			    lifrl.lifr_name);
19110Sstevel@tonic-gate 			ret_code = -1;
19120Sstevel@tonic-gate 			continue;
19130Sstevel@tonic-gate 		}
19140Sstevel@tonic-gate 		(void) memset(&lifrl, 0, sizeof (lifrl));
19150Sstevel@tonic-gate 		(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19160Sstevel@tonic-gate 		    sizeof (lifrl.lifr_name));
19170Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
19183251Ssl108498 			if (errno == ENXIO)
19193251Ssl108498 				/*
19203251Ssl108498 				 * Interface may have been removed by admin or
19213251Ssl108498 				 * another zone halting.
19223251Ssl108498 				 */
19233251Ssl108498 				continue;
19240Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
19253251Ssl108498 			    "%s: could not determine the zone to which this "
19263448Sdh155122 			    "network interface is bound", lifrl.lifr_name);
19270Sstevel@tonic-gate 			ret_code = -1;
19280Sstevel@tonic-gate 			continue;
19290Sstevel@tonic-gate 		}
19300Sstevel@tonic-gate 		if (lifrl.lifr_zoneid == zone_id) {
19310Sstevel@tonic-gate 			if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19320Sstevel@tonic-gate 				zerror(zlogp, B_TRUE,
19333448Sdh155122 				    "%s: could not remove network interface",
19340Sstevel@tonic-gate 				    lifrl.lifr_name);
19350Sstevel@tonic-gate 				ret_code = -1;
19360Sstevel@tonic-gate 				continue;
19370Sstevel@tonic-gate 			}
19380Sstevel@tonic-gate 		}
19390Sstevel@tonic-gate 	}
19400Sstevel@tonic-gate bad:
19410Sstevel@tonic-gate 	if (s > 0)
19420Sstevel@tonic-gate 		(void) close(s);
19430Sstevel@tonic-gate 	if (buf)
19440Sstevel@tonic-gate 		free(buf);
19450Sstevel@tonic-gate 	return (ret_code);
19460Sstevel@tonic-gate }
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate static union	sockunion {
19490Sstevel@tonic-gate 	struct	sockaddr sa;
19500Sstevel@tonic-gate 	struct	sockaddr_in sin;
19510Sstevel@tonic-gate 	struct	sockaddr_dl sdl;
19520Sstevel@tonic-gate 	struct	sockaddr_in6 sin6;
19530Sstevel@tonic-gate } so_dst, so_ifp;
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate static struct {
19560Sstevel@tonic-gate 	struct	rt_msghdr hdr;
19570Sstevel@tonic-gate 	char	space[512];
19580Sstevel@tonic-gate } rtmsg;
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate static int
salen(struct sockaddr * sa)19610Sstevel@tonic-gate salen(struct sockaddr *sa)
19620Sstevel@tonic-gate {
19630Sstevel@tonic-gate 	switch (sa->sa_family) {
19640Sstevel@tonic-gate 	case AF_INET:
19650Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in));
19660Sstevel@tonic-gate 	case AF_LINK:
19670Sstevel@tonic-gate 		return (sizeof (struct sockaddr_dl));
19680Sstevel@tonic-gate 	case AF_INET6:
19690Sstevel@tonic-gate 		return (sizeof (struct sockaddr_in6));
19700Sstevel@tonic-gate 	default:
19710Sstevel@tonic-gate 		return (sizeof (struct sockaddr));
19720Sstevel@tonic-gate 	}
19730Sstevel@tonic-gate }
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate #define	ROUNDUP_LONG(a) \
19760Sstevel@tonic-gate 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate /*
19790Sstevel@tonic-gate  * Look up which zone is using a given IP address.  The address in question
19800Sstevel@tonic-gate  * is expected to have been stuffed into the structure to which lifr points
19810Sstevel@tonic-gate  * via a previous SIOCGLIFADDR ioctl().
19820Sstevel@tonic-gate  *
19830Sstevel@tonic-gate  * This is done using black router socket magic.
19840Sstevel@tonic-gate  *
19850Sstevel@tonic-gate  * Return the name of the zone on success or NULL on failure.
19860Sstevel@tonic-gate  *
19870Sstevel@tonic-gate  * This is a lot of code for a simple task; a new ioctl request to take care
19880Sstevel@tonic-gate  * of this might be a useful RFE.
19890Sstevel@tonic-gate  */
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate static char *
who_is_using(zlog_t * zlogp,struct lifreq * lifr)19920Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
19930Sstevel@tonic-gate {
19940Sstevel@tonic-gate 	static char answer[ZONENAME_MAX];
19950Sstevel@tonic-gate 	pid_t pid;
19960Sstevel@tonic-gate 	int s, rlen, l, i;
19970Sstevel@tonic-gate 	char *cp = rtmsg.space;
19980Sstevel@tonic-gate 	struct sockaddr_dl *ifp = NULL;
19990Sstevel@tonic-gate 	struct sockaddr *sa;
20000Sstevel@tonic-gate 	char save_if_name[LIFNAMSIZ];
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	answer[0] = '\0';
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	pid = getpid();
20050Sstevel@tonic-gate 	if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20060Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get routing socket");
20070Sstevel@tonic-gate 		return (NULL);
20080Sstevel@tonic-gate 	}
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	if (lifr->lifr_addr.ss_family == AF_INET) {
20110Sstevel@tonic-gate 		struct sockaddr_in *sin4;
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET;
20140Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20150Sstevel@tonic-gate 		so_dst.sin.sin_addr = sin4->sin_addr;
20160Sstevel@tonic-gate 	} else {
20170Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 		so_dst.sa.sa_family = AF_INET6;
20200Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20210Sstevel@tonic-gate 		so_dst.sin6.sin6_addr = sin6->sin6_addr;
20220Sstevel@tonic-gate 	}
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	so_ifp.sa.sa_family = AF_LINK;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	(void) memset(&rtmsg, 0, sizeof (rtmsg));
20270Sstevel@tonic-gate 	rtmsg.hdr.rtm_type = RTM_GET;
20280Sstevel@tonic-gate 	rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20290Sstevel@tonic-gate 	rtmsg.hdr.rtm_version = RTM_VERSION;
20300Sstevel@tonic-gate 	rtmsg.hdr.rtm_seq = ++rts_seqno;
20310Sstevel@tonic-gate 	rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_dst.sa));
20340Sstevel@tonic-gate 	(void) memmove(cp, &(so_dst), l);
20350Sstevel@tonic-gate 	cp += l;
20360Sstevel@tonic-gate 	l = ROUNDUP_LONG(salen(&so_ifp.sa));
20370Sstevel@tonic-gate 	(void) memmove(cp, &(so_ifp), l);
20380Sstevel@tonic-gate 	cp += l;
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	if ((rlen = write(s, &rtmsg, l)) < 0) {
20430Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "writing to routing socket");
20440Sstevel@tonic-gate 		return (NULL);
20450Sstevel@tonic-gate 	} else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20460Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
20470Sstevel@tonic-gate 		    "write to routing socket got only %d for len\n", rlen);
20480Sstevel@tonic-gate 		return (NULL);
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate 	do {
20510Sstevel@tonic-gate 		l = read(s, &rtmsg, sizeof (rtmsg));
20520Sstevel@tonic-gate 	} while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20530Sstevel@tonic-gate 	    rtmsg.hdr.rtm_pid != pid));
20540Sstevel@tonic-gate 	if (l < 0) {
20550Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "reading from routing socket");
20560Sstevel@tonic-gate 		return (NULL);
20570Sstevel@tonic-gate 	}
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_version != RTM_VERSION) {
20600Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
20610Sstevel@tonic-gate 		    "routing message version %d not understood",
20620Sstevel@tonic-gate 		    rtmsg.hdr.rtm_version);
20630Sstevel@tonic-gate 		return (NULL);
20640Sstevel@tonic-gate 	}
20650Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
20660Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "message length mismatch, "
20670Sstevel@tonic-gate 		    "expected %d bytes, returned %d bytes",
20680Sstevel@tonic-gate 		    rtmsg.hdr.rtm_msglen, l);
20690Sstevel@tonic-gate 		return (NULL);
20700Sstevel@tonic-gate 	}
20710Sstevel@tonic-gate 	if (rtmsg.hdr.rtm_errno != 0)  {
20720Sstevel@tonic-gate 		errno = rtmsg.hdr.rtm_errno;
20730Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
20740Sstevel@tonic-gate 		return (NULL);
20750Sstevel@tonic-gate 	}
20760Sstevel@tonic-gate 	if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
20773448Sdh155122 		zerror(zlogp, B_FALSE, "network interface not found");
20780Sstevel@tonic-gate 		return (NULL);
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 	cp = ((char *)(&rtmsg.hdr + 1));
20810Sstevel@tonic-gate 	for (i = 1; i != 0; i <<= 1) {
20820Sstevel@tonic-gate 		/* LINTED E_BAD_PTR_CAST_ALIGN */
20830Sstevel@tonic-gate 		sa = (struct sockaddr *)cp;
20840Sstevel@tonic-gate 		if (i != RTA_IFP) {
20850Sstevel@tonic-gate 			if ((i & rtmsg.hdr.rtm_addrs) != 0)
20860Sstevel@tonic-gate 				cp += ROUNDUP_LONG(salen(sa));
20870Sstevel@tonic-gate 			continue;
20880Sstevel@tonic-gate 		}
20890Sstevel@tonic-gate 		if (sa->sa_family == AF_LINK &&
20900Sstevel@tonic-gate 		    ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
20910Sstevel@tonic-gate 			ifp = (struct sockaddr_dl *)sa;
20920Sstevel@tonic-gate 		break;
20930Sstevel@tonic-gate 	}
20940Sstevel@tonic-gate 	if (ifp == NULL) {
20953448Sdh155122 		zerror(zlogp, B_FALSE, "network interface could not be "
20963448Sdh155122 		    "determined");
20970Sstevel@tonic-gate 		return (NULL);
20980Sstevel@tonic-gate 	}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	/*
21010Sstevel@tonic-gate 	 * We need to set the I/F name to what we got above, then do the
21020Sstevel@tonic-gate 	 * appropriate ioctl to get its zone name.  But lifr->lifr_name is
21030Sstevel@tonic-gate 	 * used by the calling function to do a REMOVEIF, so if we leave the
21040Sstevel@tonic-gate 	 * "good" zone's I/F name in place, *that* I/F will be removed instead
21050Sstevel@tonic-gate 	 * of the bad one.  So we save the old (bad) I/F name before over-
21060Sstevel@tonic-gate 	 * writing it and doing the ioctl, then restore it after the ioctl.
21070Sstevel@tonic-gate 	 */
21080Sstevel@tonic-gate 	(void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21090Sstevel@tonic-gate 	(void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21100Sstevel@tonic-gate 	lifr->lifr_name[ifp->sdl_nlen] = '\0';
21110Sstevel@tonic-gate 	i = ioctl(s, SIOCGLIFZONE, lifr);
21120Sstevel@tonic-gate 	(void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21130Sstevel@tonic-gate 	if (i < 0) {
21140Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
21153448Sdh155122 		    "%s: could not determine the zone network interface "
21163448Sdh155122 		    "belongs to", lifr->lifr_name);
21170Sstevel@tonic-gate 		return (NULL);
21180Sstevel@tonic-gate 	}
21190Sstevel@tonic-gate 	if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21200Sstevel@tonic-gate 		(void) snprintf(answer, sizeof (answer), "%d",
21210Sstevel@tonic-gate 		    lifr->lifr_zoneid);
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	if (strlen(answer) > 0)
21240Sstevel@tonic-gate 		return (answer);
21250Sstevel@tonic-gate 	return (NULL);
21260Sstevel@tonic-gate }
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate /*
21290Sstevel@tonic-gate  * Configures a single interface: a new virtual interface is added, based on
21300Sstevel@tonic-gate  * the physical interface nwiftabptr->zone_nwif_physical, with the address
21310Sstevel@tonic-gate  * specified in nwiftabptr->zone_nwif_address, for zone zone_id.  Note that
21320Sstevel@tonic-gate  * the "address" can be an IPv6 address (with a /prefixlength required), an
21330Sstevel@tonic-gate  * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21340Sstevel@tonic-gate  * an IPv4 name-to-address resolution will be attempted.
21350Sstevel@tonic-gate  *
21360Sstevel@tonic-gate  * If anything goes wrong, we log an detailed error message, attempt to tear
21370Sstevel@tonic-gate  * down whatever we set up and return an error.
21380Sstevel@tonic-gate  */
21390Sstevel@tonic-gate static int
configure_one_interface(zlog_t * zlogp,zoneid_t zone_id,struct zone_nwiftab * nwiftabptr)21400Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
21418058SJordan.Vaughan@Sun.com     struct zone_nwiftab *nwiftabptr)
21420Sstevel@tonic-gate {
21430Sstevel@tonic-gate 	struct lifreq lifr;
21440Sstevel@tonic-gate 	struct sockaddr_in netmask4;
21450Sstevel@tonic-gate 	struct sockaddr_in6 netmask6;
214610067SVamsi.Krishna@Sun.COM 	struct sockaddr_storage laddr;
21470Sstevel@tonic-gate 	struct in_addr in4;
21480Sstevel@tonic-gate 	sa_family_t af;
21490Sstevel@tonic-gate 	char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
21500Sstevel@tonic-gate 	int s;
21510Sstevel@tonic-gate 	boolean_t got_netmask = B_FALSE;
21529720SSaurabh.Vyas@Sun.COM 	boolean_t is_loopback = B_FALSE;
21530Sstevel@tonic-gate 	char addrstr4[INET_ADDRSTRLEN];
21540Sstevel@tonic-gate 	int res;
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
21570Sstevel@tonic-gate 	if (res != Z_OK) {
21580Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
21590Sstevel@tonic-gate 		    nwiftabptr->zone_nwif_address);
21600Sstevel@tonic-gate 		return (-1);
21610Sstevel@tonic-gate 	}
21620Sstevel@tonic-gate 	af = lifr.lifr_addr.ss_family;
21630Sstevel@tonic-gate 	if (af == AF_INET)
21640Sstevel@tonic-gate 		in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21650Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21660Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "could not get socket");
21670Sstevel@tonic-gate 		return (-1);
21680Sstevel@tonic-gate 	}
21690Sstevel@tonic-gate 
217010067SVamsi.Krishna@Sun.COM 	/*
217110067SVamsi.Krishna@Sun.COM 	 * This is a similar kind of "hack" like in addif() to get around
217210067SVamsi.Krishna@Sun.COM 	 * the problem of SIOCLIFADDIF.  The problem is that this ioctl
217310067SVamsi.Krishna@Sun.COM 	 * does not include the netmask when adding a logical interface.
217410067SVamsi.Krishna@Sun.COM 	 * To get around this problem, we first add the logical interface
217510067SVamsi.Krishna@Sun.COM 	 * with a 0 address.  After that, we set the netmask if provided.
217610067SVamsi.Krishna@Sun.COM 	 * Finally we set the interface address.
217710067SVamsi.Krishna@Sun.COM 	 */
217810067SVamsi.Krishna@Sun.COM 	laddr = lifr.lifr_addr;
21790Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
21800Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
218110067SVamsi.Krishna@Sun.COM 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
218210067SVamsi.Krishna@Sun.COM 
21830Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
21842611Svp157776 		/*
21852611Svp157776 		 * Here, we know that the interface can't be brought up.
21862611Svp157776 		 * A similar warning message was already printed out to
21872611Svp157776 		 * the console by zoneadm(1M) so instead we log the
21882611Svp157776 		 * message to syslog and continue.
21892611Svp157776 		 */
21903448Sdh155122 		zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
21912611Svp157776 		    "'%s' which may not be present/plumbed in the "
21922611Svp157776 		    "global zone.", lifr.lifr_name);
21930Sstevel@tonic-gate 		(void) close(s);
21942611Svp157776 		return (Z_OK);
21950Sstevel@tonic-gate 	}
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate 	/* Preserve literal IPv4 address for later potential printing. */
21980Sstevel@tonic-gate 	if (af == AF_INET)
21990Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	lifr.lifr_zoneid = zone_id;
22020Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
22033448Sdh155122 		zerror(zlogp, B_TRUE, "%s: could not place network interface "
22043448Sdh155122 		    "into zone", lifr.lifr_name);
22050Sstevel@tonic-gate 		goto bad;
22060Sstevel@tonic-gate 	}
22070Sstevel@tonic-gate 
22089720SSaurabh.Vyas@Sun.COM 	/*
22099720SSaurabh.Vyas@Sun.COM 	 * Loopback interface will use the default netmask assigned, if no
22109720SSaurabh.Vyas@Sun.COM 	 * netmask is found.
22119720SSaurabh.Vyas@Sun.COM 	 */
22120Sstevel@tonic-gate 	if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
22139720SSaurabh.Vyas@Sun.COM 		is_loopback = B_TRUE;
22149720SSaurabh.Vyas@Sun.COM 	}
22159720SSaurabh.Vyas@Sun.COM 	if (af == AF_INET) {
22169720SSaurabh.Vyas@Sun.COM 		/*
22179720SSaurabh.Vyas@Sun.COM 		 * The IPv4 netmask can be determined either
22189720SSaurabh.Vyas@Sun.COM 		 * directly if a prefix length was supplied with
22199720SSaurabh.Vyas@Sun.COM 		 * the address or via the netmasks database.  Not
22209720SSaurabh.Vyas@Sun.COM 		 * being able to determine it is a common failure,
22219720SSaurabh.Vyas@Sun.COM 		 * but it often is not fatal to operation of the
22229720SSaurabh.Vyas@Sun.COM 		 * interface.  In that case, a warning will be
22239720SSaurabh.Vyas@Sun.COM 		 * printed after the rest of the interface's
22249720SSaurabh.Vyas@Sun.COM 		 * parameters have been configured.
22259720SSaurabh.Vyas@Sun.COM 		 */
22269720SSaurabh.Vyas@Sun.COM 		(void) memset(&netmask4, 0, sizeof (netmask4));
22279720SSaurabh.Vyas@Sun.COM 		if (slashp != NULL) {
22289720SSaurabh.Vyas@Sun.COM 			if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22299720SSaurabh.Vyas@Sun.COM 			    (uchar_t *)&netmask4.sin_addr) != 0) {
22300Sstevel@tonic-gate 				*slashp = '/';
22310Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
22320Sstevel@tonic-gate 				    "%s: invalid prefix length in %s",
22330Sstevel@tonic-gate 				    lifr.lifr_name,
22340Sstevel@tonic-gate 				    nwiftabptr->zone_nwif_address);
22350Sstevel@tonic-gate 				goto bad;
22360Sstevel@tonic-gate 			}
22370Sstevel@tonic-gate 			got_netmask = B_TRUE;
22389720SSaurabh.Vyas@Sun.COM 		} else if (getnetmaskbyaddr(in4,
22399720SSaurabh.Vyas@Sun.COM 		    &netmask4.sin_addr) == 0) {
22409720SSaurabh.Vyas@Sun.COM 			got_netmask = B_TRUE;
22419720SSaurabh.Vyas@Sun.COM 		}
22429720SSaurabh.Vyas@Sun.COM 		if (got_netmask) {
22439720SSaurabh.Vyas@Sun.COM 			netmask4.sin_family = af;
22449720SSaurabh.Vyas@Sun.COM 			(void) memcpy(&lifr.lifr_addr, &netmask4,
22459720SSaurabh.Vyas@Sun.COM 			    sizeof (netmask4));
22460Sstevel@tonic-gate 		}
22479720SSaurabh.Vyas@Sun.COM 	} else {
22489720SSaurabh.Vyas@Sun.COM 		(void) memset(&netmask6, 0, sizeof (netmask6));
22499720SSaurabh.Vyas@Sun.COM 		if (addr2netmask(slashp + 1, V6_ADDR_LEN,
22509720SSaurabh.Vyas@Sun.COM 		    (uchar_t *)&netmask6.sin6_addr) != 0) {
22519720SSaurabh.Vyas@Sun.COM 			*slashp = '/';
22529720SSaurabh.Vyas@Sun.COM 			zerror(zlogp, B_FALSE,
22539720SSaurabh.Vyas@Sun.COM 			    "%s: invalid prefix length in %s",
22549720SSaurabh.Vyas@Sun.COM 			    lifr.lifr_name,
22559720SSaurabh.Vyas@Sun.COM 			    nwiftabptr->zone_nwif_address);
22560Sstevel@tonic-gate 			goto bad;
22570Sstevel@tonic-gate 		}
22589720SSaurabh.Vyas@Sun.COM 		got_netmask = B_TRUE;
22599720SSaurabh.Vyas@Sun.COM 		netmask6.sin6_family = af;
22609720SSaurabh.Vyas@Sun.COM 		(void) memcpy(&lifr.lifr_addr, &netmask6,
22619720SSaurabh.Vyas@Sun.COM 		    sizeof (netmask6));
22629720SSaurabh.Vyas@Sun.COM 	}
22639720SSaurabh.Vyas@Sun.COM 	if (got_netmask &&
22649720SSaurabh.Vyas@Sun.COM 	    ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
22659720SSaurabh.Vyas@Sun.COM 		zerror(zlogp, B_TRUE, "%s: could not set netmask",
22669720SSaurabh.Vyas@Sun.COM 		    lifr.lifr_name);
22679720SSaurabh.Vyas@Sun.COM 		goto bad;
22689720SSaurabh.Vyas@Sun.COM 	}
22699720SSaurabh.Vyas@Sun.COM 
227010067SVamsi.Krishna@Sun.COM 	/* Set the interface address */
227110067SVamsi.Krishna@Sun.COM 	lifr.lifr_addr = laddr;
22729720SSaurabh.Vyas@Sun.COM 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22739720SSaurabh.Vyas@Sun.COM 		zerror(zlogp, B_TRUE,
227410067SVamsi.Krishna@Sun.COM 		    "%s: could not set IP address to %s",
227510067SVamsi.Krishna@Sun.COM 		    lifr.lifr_name, nwiftabptr->zone_nwif_address);
22769720SSaurabh.Vyas@Sun.COM 		goto bad;
22770Sstevel@tonic-gate 	}
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22800Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s: could not get flags",
22810Sstevel@tonic-gate 		    lifr.lifr_name);
22820Sstevel@tonic-gate 		goto bad;
22830Sstevel@tonic-gate 	}
22840Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_UP;
22850Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
22860Sstevel@tonic-gate 		int save_errno = errno;
22870Sstevel@tonic-gate 		char *zone_using;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 		/*
22900Sstevel@tonic-gate 		 * If we failed with something other than EADDRNOTAVAIL,
22910Sstevel@tonic-gate 		 * then skip to the end.  Otherwise, look up our address,
22920Sstevel@tonic-gate 		 * then call a function to determine which zone is already
22930Sstevel@tonic-gate 		 * using that address.
22940Sstevel@tonic-gate 		 */
22950Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL) {
22960Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
22973448Sdh155122 			    "%s: could not bring network interface up",
22983448Sdh155122 			    lifr.lifr_name);
22990Sstevel@tonic-gate 			goto bad;
23000Sstevel@tonic-gate 		}
23010Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23020Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s: could not get address",
23030Sstevel@tonic-gate 			    lifr.lifr_name);
23040Sstevel@tonic-gate 			goto bad;
23050Sstevel@tonic-gate 		}
23060Sstevel@tonic-gate 		zone_using = who_is_using(zlogp, &lifr);
23070Sstevel@tonic-gate 		errno = save_errno;
23080Sstevel@tonic-gate 		if (zone_using == NULL)
23090Sstevel@tonic-gate 			zerror(zlogp, B_TRUE,
23103448Sdh155122 			    "%s: could not bring network interface up",
23113448Sdh155122 			    lifr.lifr_name);
23120Sstevel@tonic-gate 		else
23133448Sdh155122 			zerror(zlogp, B_TRUE, "%s: could not bring network "
23143448Sdh155122 			    "interface up: address in use by zone '%s'",
23153448Sdh155122 			    lifr.lifr_name, zone_using);
23160Sstevel@tonic-gate 		goto bad;
23170Sstevel@tonic-gate 	}
23180Sstevel@tonic-gate 
23199720SSaurabh.Vyas@Sun.COM 	if (!got_netmask && !is_loopback) {
23200Sstevel@tonic-gate 		/*
23210Sstevel@tonic-gate 		 * A common, but often non-fatal problem, is that the system
23220Sstevel@tonic-gate 		 * cannot find the netmask for an interface address. This is
23230Sstevel@tonic-gate 		 * often caused by it being only in /etc/inet/netmasks, but
23240Sstevel@tonic-gate 		 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23250Sstevel@tonic-gate 		 * in that. This doesn't show up at boot because the netmask
23260Sstevel@tonic-gate 		 * is obtained from /etc/inet/netmasks when no network
23270Sstevel@tonic-gate 		 * interfaces are up, but isn't consulted when NIS/NIS+ is
23280Sstevel@tonic-gate 		 * available. We warn the user here that something like this
23290Sstevel@tonic-gate 		 * has happened and we're just running with a default and
23300Sstevel@tonic-gate 		 * possible incorrect netmask.
23310Sstevel@tonic-gate 		 */
23320Sstevel@tonic-gate 		char buffer[INET6_ADDRSTRLEN];
23330Sstevel@tonic-gate 		void  *addr;
23348485SPeter.Memishian@Sun.COM 		const char *nomatch = "no matching subnet found in netmasks(4)";
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 		if (af == AF_INET)
23370Sstevel@tonic-gate 			addr = &((struct sockaddr_in *)
23380Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin_addr;
23390Sstevel@tonic-gate 		else
23400Sstevel@tonic-gate 			addr = &((struct sockaddr_in6 *)
23410Sstevel@tonic-gate 			    (&lifr.lifr_addr))->sin6_addr;
23420Sstevel@tonic-gate 
23438485SPeter.Memishian@Sun.COM 		/*
23448485SPeter.Memishian@Sun.COM 		 * Find out what netmask the interface is going to be using.
23458485SPeter.Memishian@Sun.COM 		 * If we just brought up an IPMP data address on an underlying
23468485SPeter.Memishian@Sun.COM 		 * interface above, the address will have already migrated, so
23478485SPeter.Memishian@Sun.COM 		 * the SIOCGLIFNETMASK won't be able to find it (but we need
23488485SPeter.Memishian@Sun.COM 		 * to bring the address up to get the actual netmask).  Just
23498485SPeter.Memishian@Sun.COM 		 * omit printing the actual netmask in this corner-case.
23508485SPeter.Memishian@Sun.COM 		 */
23510Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
23528485SPeter.Memishian@Sun.COM 		    inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
23538485SPeter.Memishian@Sun.COM 			zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
23548485SPeter.Memishian@Sun.COM 			    nomatch);
23558485SPeter.Memishian@Sun.COM 		} else {
23568485SPeter.Memishian@Sun.COM 			zerror(zlogp, B_FALSE,
23578485SPeter.Memishian@Sun.COM 			    "WARNING: %s: %s: %s; using default of %s.",
23588485SPeter.Memishian@Sun.COM 			    lifr.lifr_name, nomatch, addrstr4, buffer);
23598485SPeter.Memishian@Sun.COM 		}
23600Sstevel@tonic-gate 	}
23610Sstevel@tonic-gate 
23626076Sgfaden 	/*
23636076Sgfaden 	 * If a default router was specified for this interface
23646076Sgfaden 	 * set the route now. Ignore if already set.
23656076Sgfaden 	 */
23666076Sgfaden 	if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
23676076Sgfaden 		int status;
23686076Sgfaden 		char *argv[7];
23696076Sgfaden 
23706076Sgfaden 		argv[0] = "route";
23716076Sgfaden 		argv[1] = "add";
23726076Sgfaden 		argv[2] = "-ifp";
23736076Sgfaden 		argv[3] = nwiftabptr->zone_nwif_physical;
23746076Sgfaden 		argv[4] = "default";
23756076Sgfaden 		argv[5] = nwiftabptr->zone_nwif_defrouter;
23766076Sgfaden 		argv[6] = NULL;
23776076Sgfaden 
23786076Sgfaden 		status = forkexec(zlogp, "/usr/sbin/route", argv);
23796076Sgfaden 		if (status != 0 && status != EEXIST)
23806076Sgfaden 			zerror(zlogp, B_FALSE, "Unable to set route for "
23816076Sgfaden 			    "interface %s to %s\n",
23826076Sgfaden 			    nwiftabptr->zone_nwif_physical,
23836076Sgfaden 			    nwiftabptr->zone_nwif_defrouter);
23846076Sgfaden 	}
23856076Sgfaden 
23860Sstevel@tonic-gate 	(void) close(s);
23870Sstevel@tonic-gate 	return (Z_OK);
23880Sstevel@tonic-gate bad:
23890Sstevel@tonic-gate 	(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
23900Sstevel@tonic-gate 	(void) close(s);
23910Sstevel@tonic-gate 	return (-1);
23920Sstevel@tonic-gate }
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate /*
23950Sstevel@tonic-gate  * Sets up network interfaces based on information from the zone configuration.
23968058SJordan.Vaughan@Sun.com  * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
23978058SJordan.Vaughan@Sun.com  * system.
23980Sstevel@tonic-gate  *
23990Sstevel@tonic-gate  * If anything goes wrong, we log a general error message, attempt to tear down
24000Sstevel@tonic-gate  * whatever we set up, and return an error.
24010Sstevel@tonic-gate  */
24020Sstevel@tonic-gate static int
configure_shared_network_interfaces(zlog_t * zlogp)24033448Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24040Sstevel@tonic-gate {
24050Sstevel@tonic-gate 	zone_dochandle_t handle;
24060Sstevel@tonic-gate 	struct zone_nwiftab nwiftab, loopback_iftab;
24070Sstevel@tonic-gate 	zoneid_t zoneid;
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24100Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to get zoneid");
24110Sstevel@tonic-gate 		return (-1);
24120Sstevel@tonic-gate 	}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
24150Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
24160Sstevel@tonic-gate 		return (-1);
24170Sstevel@tonic-gate 	}
24180Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24190Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
24200Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24210Sstevel@tonic-gate 		return (-1);
24220Sstevel@tonic-gate 	}
24230Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) == Z_OK) {
24240Sstevel@tonic-gate 		for (;;) {
24250Sstevel@tonic-gate 			if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24260Sstevel@tonic-gate 				break;
24278058SJordan.Vaughan@Sun.com 			if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24280Sstevel@tonic-gate 			    Z_OK) {
24290Sstevel@tonic-gate 				(void) zonecfg_endnwifent(handle);
24300Sstevel@tonic-gate 				zonecfg_fini_handle(handle);
24310Sstevel@tonic-gate 				return (-1);
24320Sstevel@tonic-gate 			}
24330Sstevel@tonic-gate 		}
24340Sstevel@tonic-gate 		(void) zonecfg_endnwifent(handle);
24350Sstevel@tonic-gate 	}
24360Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
24375863Sgfaden 	if (is_system_labeled()) {
24385863Sgfaden 		/*
24395863Sgfaden 		 * Labeled zones share the loopback interface
24405863Sgfaden 		 * so it is not plumbed for shared stack instances.
24415863Sgfaden 		 */
24425863Sgfaden 		return (0);
24435863Sgfaden 	}
24440Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
24450Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_physical));
24460Sstevel@tonic-gate 	(void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
24470Sstevel@tonic-gate 	    sizeof (loopback_iftab.zone_nwif_address));
24486378Sgfaden 	loopback_iftab.zone_nwif_defrouter[0] = '\0';
24498058SJordan.Vaughan@Sun.com 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24500Sstevel@tonic-gate 		return (-1);
24518058SJordan.Vaughan@Sun.com 
24528058SJordan.Vaughan@Sun.com 	/* Always plumb up the IPv6 loopback interface. */
24538058SJordan.Vaughan@Sun.com 	(void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
24548058SJordan.Vaughan@Sun.com 	    sizeof (loopback_iftab.zone_nwif_address));
24558058SJordan.Vaughan@Sun.com 	if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24568058SJordan.Vaughan@Sun.com 		return (-1);
24570Sstevel@tonic-gate 	return (0);
24580Sstevel@tonic-gate }
24590Sstevel@tonic-gate 
24608878SPeter.Memishian@Sun.COM static void
zdlerror(zlog_t * zlogp,dladm_status_t err,const char * dlname,const char * str)24618878SPeter.Memishian@Sun.COM zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
24628878SPeter.Memishian@Sun.COM {
24638878SPeter.Memishian@Sun.COM 	char errmsg[DLADM_STRSIZE];
24648878SPeter.Memishian@Sun.COM 
24658878SPeter.Memishian@Sun.COM 	(void) dladm_status2str(err, errmsg);
24668878SPeter.Memishian@Sun.COM 	zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
24678878SPeter.Memishian@Sun.COM }
24688878SPeter.Memishian@Sun.COM 
24693448Sdh155122 static int
add_datalink(zlog_t * zlogp,char * zone_name,datalink_id_t linkid,char * dlname)247010616SSebastien.Roy@Sun.COM add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
24713448Sdh155122 {
24728878SPeter.Memishian@Sun.COM 	dladm_status_t err;
247311878SVenu.Iyer@Sun.COM 	boolean_t cpuset, poolset;
2474*13122SStephen.Lawrence@oracle.COM 	char *poolp;
24758878SPeter.Memishian@Sun.COM 
24763448Sdh155122 	/* First check if it's in use by global zone. */
24773448Sdh155122 	if (zonecfg_ifname_exists(AF_INET, dlname) ||
24783448Sdh155122 	    zonecfg_ifname_exists(AF_INET6, dlname)) {
24798878SPeter.Memishian@Sun.COM 		zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
24808878SPeter.Memishian@Sun.COM 		    "'%s' which is used in the global zone", dlname);
24813448Sdh155122 		return (-1);
24823448Sdh155122 	}
24833448Sdh155122 
24845895Syz147064 	/* Set zoneid of this link. */
248510616SSebastien.Roy@Sun.COM 	err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
248610616SSebastien.Roy@Sun.COM 	    DLADM_OPT_ACTIVE);
24878878SPeter.Memishian@Sun.COM 	if (err != DLADM_STATUS_OK) {
24888878SPeter.Memishian@Sun.COM 		zdlerror(zlogp, err, dlname,
24898878SPeter.Memishian@Sun.COM 		    "WARNING: unable to add network interface");
24903448Sdh155122 		return (-1);
24913448Sdh155122 	}
249211878SVenu.Iyer@Sun.COM 
249311878SVenu.Iyer@Sun.COM 	/*
249411878SVenu.Iyer@Sun.COM 	 * Set the pool of this link if the zone has a pool and
249511878SVenu.Iyer@Sun.COM 	 * neither the cpus nor the pool datalink property is
249611878SVenu.Iyer@Sun.COM 	 * already set.
249711878SVenu.Iyer@Sun.COM 	 */
249811878SVenu.Iyer@Sun.COM 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
249911878SVenu.Iyer@Sun.COM 	    "cpus", &cpuset);
250011878SVenu.Iyer@Sun.COM 	if (err != DLADM_STATUS_OK) {
250111878SVenu.Iyer@Sun.COM 		zdlerror(zlogp, err, dlname,
250211878SVenu.Iyer@Sun.COM 		    "WARNING: unable to check if cpus link property is set");
250311878SVenu.Iyer@Sun.COM 	}
250411878SVenu.Iyer@Sun.COM 	err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
250511878SVenu.Iyer@Sun.COM 	    "pool", &poolset);
250611878SVenu.Iyer@Sun.COM 	if (err != DLADM_STATUS_OK) {
250711878SVenu.Iyer@Sun.COM 		zdlerror(zlogp, err, dlname,
250811878SVenu.Iyer@Sun.COM 		    "WARNING: unable to check if pool link property is set");
250911878SVenu.Iyer@Sun.COM 	}
251011878SVenu.Iyer@Sun.COM 
251111878SVenu.Iyer@Sun.COM 	if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2512*13122SStephen.Lawrence@oracle.COM 		poolp = pool_name;
251311878SVenu.Iyer@Sun.COM 		err = dladm_set_linkprop(dld_handle, linkid, "pool",
2514*13122SStephen.Lawrence@oracle.COM 		    &poolp, 1, DLADM_OPT_ACTIVE);
251511878SVenu.Iyer@Sun.COM 		if (err != DLADM_STATUS_OK) {
251611878SVenu.Iyer@Sun.COM 			zerror(zlogp, B_FALSE, "WARNING: unable to set "
251711878SVenu.Iyer@Sun.COM 			    "pool %s to datalink %s", pool_name, dlname);
251811878SVenu.Iyer@Sun.COM 			bzero(pool_name, MAXPATHLEN);
251911878SVenu.Iyer@Sun.COM 		}
252011878SVenu.Iyer@Sun.COM 	} else {
252111878SVenu.Iyer@Sun.COM 		bzero(pool_name, MAXPATHLEN);
252211878SVenu.Iyer@Sun.COM 	}
25233448Sdh155122 	return (0);
25243448Sdh155122 }
25253448Sdh155122 
252612748SSowmini.Varadhan@oracle.COM static boolean_t
sockaddr_to_str(sa_family_t af,const struct sockaddr * sockaddr,char * straddr,size_t len)252712748SSowmini.Varadhan@oracle.COM sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
252812748SSowmini.Varadhan@oracle.COM     char *straddr, size_t len)
252912748SSowmini.Varadhan@oracle.COM {
253012748SSowmini.Varadhan@oracle.COM 	struct sockaddr_in *sin;
253112748SSowmini.Varadhan@oracle.COM 	struct sockaddr_in6 *sin6;
253212748SSowmini.Varadhan@oracle.COM 	const char *str = NULL;
253312748SSowmini.Varadhan@oracle.COM 
253412748SSowmini.Varadhan@oracle.COM 	if (af == AF_INET) {
253512748SSowmini.Varadhan@oracle.COM 		/* LINTED E_BAD_PTR_CAST_ALIGN */
253612748SSowmini.Varadhan@oracle.COM 		sin = SIN(sockaddr);
253712748SSowmini.Varadhan@oracle.COM 		str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
253812748SSowmini.Varadhan@oracle.COM 	} else if (af == AF_INET6) {
253912748SSowmini.Varadhan@oracle.COM 		/* LINTED E_BAD_PTR_CAST_ALIGN */
254012748SSowmini.Varadhan@oracle.COM 		sin6 = SIN6(sockaddr);
254112748SSowmini.Varadhan@oracle.COM 		str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
254212748SSowmini.Varadhan@oracle.COM 		    len);
254312748SSowmini.Varadhan@oracle.COM 	}
254412748SSowmini.Varadhan@oracle.COM 
254512748SSowmini.Varadhan@oracle.COM 	return (str != NULL);
254612748SSowmini.Varadhan@oracle.COM }
254712748SSowmini.Varadhan@oracle.COM 
254812748SSowmini.Varadhan@oracle.COM static int
ipv4_prefixlen(struct sockaddr_in * sin)254912748SSowmini.Varadhan@oracle.COM ipv4_prefixlen(struct sockaddr_in *sin)
255012748SSowmini.Varadhan@oracle.COM {
255112748SSowmini.Varadhan@oracle.COM 	struct sockaddr_in *m;
255212748SSowmini.Varadhan@oracle.COM 	struct sockaddr_storage mask;
255312748SSowmini.Varadhan@oracle.COM 
255412748SSowmini.Varadhan@oracle.COM 	m = SIN(&mask);
255512748SSowmini.Varadhan@oracle.COM 	m->sin_family = AF_INET;
255612748SSowmini.Varadhan@oracle.COM 	if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
255712805SDarren.Reed@Oracle.COM 		return (mask2plen((struct sockaddr *)&mask));
255812748SSowmini.Varadhan@oracle.COM 	} else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
255912748SSowmini.Varadhan@oracle.COM 		return (8);
256012748SSowmini.Varadhan@oracle.COM 	} else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
256112748SSowmini.Varadhan@oracle.COM 		return (16);
256212748SSowmini.Varadhan@oracle.COM 	} else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
256312748SSowmini.Varadhan@oracle.COM 		return (24);
256412748SSowmini.Varadhan@oracle.COM 	}
256512748SSowmini.Varadhan@oracle.COM 	return (0);
256612748SSowmini.Varadhan@oracle.COM }
256712748SSowmini.Varadhan@oracle.COM 
256812748SSowmini.Varadhan@oracle.COM static int
zone_setattr_network(int type,zoneid_t zoneid,datalink_id_t linkid,void * buf,size_t bufsize)256912748SSowmini.Varadhan@oracle.COM zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
257012748SSowmini.Varadhan@oracle.COM     void *buf, size_t bufsize)
257112748SSowmini.Varadhan@oracle.COM {
257212748SSowmini.Varadhan@oracle.COM 	zone_net_data_t *zndata;
257312748SSowmini.Varadhan@oracle.COM 	size_t znsize;
257412748SSowmini.Varadhan@oracle.COM 	int err;
257512748SSowmini.Varadhan@oracle.COM 
257612748SSowmini.Varadhan@oracle.COM 	znsize = sizeof (*zndata) + bufsize;
257712748SSowmini.Varadhan@oracle.COM 	zndata = calloc(1, znsize);
257812748SSowmini.Varadhan@oracle.COM 	if (zndata == NULL)
257912748SSowmini.Varadhan@oracle.COM 		return (ENOMEM);
258012748SSowmini.Varadhan@oracle.COM 	zndata->zn_type = type;
258112748SSowmini.Varadhan@oracle.COM 	zndata->zn_len = bufsize;
258212748SSowmini.Varadhan@oracle.COM 	zndata->zn_linkid = linkid;
258312748SSowmini.Varadhan@oracle.COM 	bcopy(buf, zndata->zn_val, zndata->zn_len);
258412748SSowmini.Varadhan@oracle.COM 	err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
258512748SSowmini.Varadhan@oracle.COM 	free(zndata);
258612748SSowmini.Varadhan@oracle.COM 	return (err);
258712748SSowmini.Varadhan@oracle.COM }
258812748SSowmini.Varadhan@oracle.COM 
258912748SSowmini.Varadhan@oracle.COM static int
add_net_for_linkid(zlog_t * zlogp,zoneid_t zoneid,zone_addr_list_t * start)259012748SSowmini.Varadhan@oracle.COM add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
259112748SSowmini.Varadhan@oracle.COM {
259212748SSowmini.Varadhan@oracle.COM 	struct lifreq lifr;
259312748SSowmini.Varadhan@oracle.COM 	char **astr, *address;
259412748SSowmini.Varadhan@oracle.COM 	dladm_status_t dlstatus;
259512748SSowmini.Varadhan@oracle.COM 	char *ip_nospoof = "ip-nospoof";
259612748SSowmini.Varadhan@oracle.COM 	int nnet, naddr, err = 0, j;
259712748SSowmini.Varadhan@oracle.COM 	size_t zlen, cpleft;
259812748SSowmini.Varadhan@oracle.COM 	zone_addr_list_t *ptr, *end;
259912748SSowmini.Varadhan@oracle.COM 	char  tmp[INET6_ADDRSTRLEN], *maskstr;
260012748SSowmini.Varadhan@oracle.COM 	char *zaddr, *cp;
260112748SSowmini.Varadhan@oracle.COM 	struct in6_addr *routes = NULL;
260212748SSowmini.Varadhan@oracle.COM 	boolean_t is_set;
260312748SSowmini.Varadhan@oracle.COM 	datalink_id_t linkid;
260412748SSowmini.Varadhan@oracle.COM 
260512748SSowmini.Varadhan@oracle.COM 	assert(start != NULL);
260612748SSowmini.Varadhan@oracle.COM 	naddr = 0; /* number of addresses */
260712748SSowmini.Varadhan@oracle.COM 	nnet = 0; /* number of net resources */
260812748SSowmini.Varadhan@oracle.COM 	linkid = start->za_linkid;
260912748SSowmini.Varadhan@oracle.COM 	for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
261012748SSowmini.Varadhan@oracle.COM 	    ptr = ptr->za_next) {
261112748SSowmini.Varadhan@oracle.COM 		nnet++;
261212748SSowmini.Varadhan@oracle.COM 	}
261312748SSowmini.Varadhan@oracle.COM 	end = ptr;
261412748SSowmini.Varadhan@oracle.COM 	zlen = nnet * (INET6_ADDRSTRLEN + 1);
261512748SSowmini.Varadhan@oracle.COM 	astr = calloc(1, nnet * sizeof (uintptr_t));
261612748SSowmini.Varadhan@oracle.COM 	zaddr = calloc(1, zlen);
261712748SSowmini.Varadhan@oracle.COM 	if (astr == NULL || zaddr == NULL) {
261812748SSowmini.Varadhan@oracle.COM 		err = ENOMEM;
261912748SSowmini.Varadhan@oracle.COM 		goto done;
262012748SSowmini.Varadhan@oracle.COM 	}
262112748SSowmini.Varadhan@oracle.COM 	cp = zaddr;
262212748SSowmini.Varadhan@oracle.COM 	cpleft = zlen;
262312748SSowmini.Varadhan@oracle.COM 	j = 0;
262412748SSowmini.Varadhan@oracle.COM 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
262512748SSowmini.Varadhan@oracle.COM 		address = ptr->za_nwiftab.zone_nwif_allowed_address;
262612748SSowmini.Varadhan@oracle.COM 		if (address[0] == '\0')
262712748SSowmini.Varadhan@oracle.COM 			continue;
262812748SSowmini.Varadhan@oracle.COM 		(void) snprintf(tmp, sizeof (tmp), "%s", address);
262912748SSowmini.Varadhan@oracle.COM 		/*
263012748SSowmini.Varadhan@oracle.COM 		 * Validate the data. zonecfg_valid_net_address() clobbers
263112748SSowmini.Varadhan@oracle.COM 		 * the /<mask> in the address string.
263212748SSowmini.Varadhan@oracle.COM 		 */
263312748SSowmini.Varadhan@oracle.COM 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
263412748SSowmini.Varadhan@oracle.COM 			zerror(zlogp, B_FALSE, "invalid address [%s]\n",
263512748SSowmini.Varadhan@oracle.COM 			    address);
263612748SSowmini.Varadhan@oracle.COM 			err = EINVAL;
263712748SSowmini.Varadhan@oracle.COM 			goto done;
263812748SSowmini.Varadhan@oracle.COM 		}
263912748SSowmini.Varadhan@oracle.COM 		/*
264012748SSowmini.Varadhan@oracle.COM 		 * convert any hostnames to numeric address strings.
264112748SSowmini.Varadhan@oracle.COM 		 */
264212748SSowmini.Varadhan@oracle.COM 		if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
264312748SSowmini.Varadhan@oracle.COM 		    (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
264412748SSowmini.Varadhan@oracle.COM 			err = EINVAL;
264512748SSowmini.Varadhan@oracle.COM 			goto done;
264612748SSowmini.Varadhan@oracle.COM 		}
264712748SSowmini.Varadhan@oracle.COM 		/*
264812748SSowmini.Varadhan@oracle.COM 		 * make a copy of the numeric string for the data needed
264912748SSowmini.Varadhan@oracle.COM 		 * by the "allowed-ips" datalink property.
265012748SSowmini.Varadhan@oracle.COM 		 */
265112748SSowmini.Varadhan@oracle.COM 		astr[j] = strdup(cp);
265212748SSowmini.Varadhan@oracle.COM 		if (astr[j] == NULL) {
265312748SSowmini.Varadhan@oracle.COM 			err = ENOMEM;
265412748SSowmini.Varadhan@oracle.COM 			goto done;
265512748SSowmini.Varadhan@oracle.COM 		}
265612748SSowmini.Varadhan@oracle.COM 		j++;
265712748SSowmini.Varadhan@oracle.COM 		/*
265812748SSowmini.Varadhan@oracle.COM 		 * compute the default netmask from the address, if necessary
265912748SSowmini.Varadhan@oracle.COM 		 */
266012748SSowmini.Varadhan@oracle.COM 		if ((maskstr = strchr(tmp, '/')) == NULL) {
266112748SSowmini.Varadhan@oracle.COM 			int prefixlen;
266212748SSowmini.Varadhan@oracle.COM 
266312748SSowmini.Varadhan@oracle.COM 			if (lifr.lifr_addr.ss_family == AF_INET) {
266412748SSowmini.Varadhan@oracle.COM 				prefixlen = ipv4_prefixlen(
266512748SSowmini.Varadhan@oracle.COM 				    SIN(&lifr.lifr_addr));
266612748SSowmini.Varadhan@oracle.COM 			} else {
266712748SSowmini.Varadhan@oracle.COM 				struct sockaddr_in6 *sin6;
266812748SSowmini.Varadhan@oracle.COM 
266912748SSowmini.Varadhan@oracle.COM 				sin6 = SIN6(&lifr.lifr_addr);
267012748SSowmini.Varadhan@oracle.COM 				if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
267112748SSowmini.Varadhan@oracle.COM 					prefixlen = 10;
267212748SSowmini.Varadhan@oracle.COM 				else
267312748SSowmini.Varadhan@oracle.COM 					prefixlen = 64;
267412748SSowmini.Varadhan@oracle.COM 			}
267512748SSowmini.Varadhan@oracle.COM 			(void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
267612748SSowmini.Varadhan@oracle.COM 			maskstr = tmp;
267712748SSowmini.Varadhan@oracle.COM 		} else {
267812748SSowmini.Varadhan@oracle.COM 			maskstr++;
267912748SSowmini.Varadhan@oracle.COM 		}
268012748SSowmini.Varadhan@oracle.COM 		/* append the "/<netmask>" */
268112748SSowmini.Varadhan@oracle.COM 		(void) strlcat(cp, "/", cpleft);
268212748SSowmini.Varadhan@oracle.COM 		(void) strlcat(cp, maskstr, cpleft);
268312748SSowmini.Varadhan@oracle.COM 		(void) strlcat(cp, ",", cpleft);
268412748SSowmini.Varadhan@oracle.COM 		cp += strnlen(cp, zlen);
268512748SSowmini.Varadhan@oracle.COM 		cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
268612748SSowmini.Varadhan@oracle.COM 	}
268712748SSowmini.Varadhan@oracle.COM 	naddr = j; /* the actual number of addresses in the net resource */
268812748SSowmini.Varadhan@oracle.COM 	assert(naddr <= nnet);
268912748SSowmini.Varadhan@oracle.COM 
269012748SSowmini.Varadhan@oracle.COM 	/*
269112748SSowmini.Varadhan@oracle.COM 	 * zonecfg has already verified that the defrouter property can only
269212748SSowmini.Varadhan@oracle.COM 	 * be set if there is at least one address defined for the net resource.
269312748SSowmini.Varadhan@oracle.COM 	 * If j is 0, there are no addresses defined, and therefore no routers
269412748SSowmini.Varadhan@oracle.COM 	 * to configure, and we are done at that point.
269512748SSowmini.Varadhan@oracle.COM 	 */
269612748SSowmini.Varadhan@oracle.COM 	if (j == 0)
269712748SSowmini.Varadhan@oracle.COM 		goto done;
269812748SSowmini.Varadhan@oracle.COM 
269912748SSowmini.Varadhan@oracle.COM 	/* over-write last ',' with '\0' */
270012748SSowmini.Varadhan@oracle.COM 	zaddr[strnlen(zaddr, zlen) + 1] = '\0';
270112748SSowmini.Varadhan@oracle.COM 
270212748SSowmini.Varadhan@oracle.COM 	/*
270312748SSowmini.Varadhan@oracle.COM 	 * First make sure L3 protection is not already set on the link.
270412748SSowmini.Varadhan@oracle.COM 	 */
270512748SSowmini.Varadhan@oracle.COM 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
270612748SSowmini.Varadhan@oracle.COM 	    "protection", &is_set);
270712748SSowmini.Varadhan@oracle.COM 	if (dlstatus != DLADM_STATUS_OK) {
270812748SSowmini.Varadhan@oracle.COM 		err = EINVAL;
270912748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_FALSE, "unable to check if protection is set");
271012748SSowmini.Varadhan@oracle.COM 		goto done;
271112748SSowmini.Varadhan@oracle.COM 	}
271212748SSowmini.Varadhan@oracle.COM 	if (is_set) {
271312748SSowmini.Varadhan@oracle.COM 		err = EINVAL;
271412748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_FALSE, "Protection is already set");
271512748SSowmini.Varadhan@oracle.COM 		goto done;
271612748SSowmini.Varadhan@oracle.COM 	}
271712748SSowmini.Varadhan@oracle.COM 	dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
271812748SSowmini.Varadhan@oracle.COM 	    "allowed-ips", &is_set);
271912748SSowmini.Varadhan@oracle.COM 	if (dlstatus != DLADM_STATUS_OK) {
272012748SSowmini.Varadhan@oracle.COM 		err = EINVAL;
272112748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
272212748SSowmini.Varadhan@oracle.COM 		goto done;
272312748SSowmini.Varadhan@oracle.COM 	}
272412748SSowmini.Varadhan@oracle.COM 	if (is_set) {
272512748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_FALSE, "allowed-ips is already set");
272612748SSowmini.Varadhan@oracle.COM 		err = EINVAL;
272712748SSowmini.Varadhan@oracle.COM 		goto done;
272812748SSowmini.Varadhan@oracle.COM 	}
272912748SSowmini.Varadhan@oracle.COM 
273012748SSowmini.Varadhan@oracle.COM 	/*
273112748SSowmini.Varadhan@oracle.COM 	 * Enable ip-nospoof for the link, and add address to the allowed-ips
273212748SSowmini.Varadhan@oracle.COM 	 * list.
273312748SSowmini.Varadhan@oracle.COM 	 */
273412748SSowmini.Varadhan@oracle.COM 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
273512748SSowmini.Varadhan@oracle.COM 	    &ip_nospoof, 1, DLADM_OPT_ACTIVE);
273612748SSowmini.Varadhan@oracle.COM 	if (dlstatus != DLADM_STATUS_OK) {
273712748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_FALSE, "could not set protection\n");
273812748SSowmini.Varadhan@oracle.COM 		err = EINVAL;
273912748SSowmini.Varadhan@oracle.COM 		goto done;
274012748SSowmini.Varadhan@oracle.COM 	}
274112748SSowmini.Varadhan@oracle.COM 	dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
274212748SSowmini.Varadhan@oracle.COM 	    astr, naddr, DLADM_OPT_ACTIVE);
274312748SSowmini.Varadhan@oracle.COM 	if (dlstatus != DLADM_STATUS_OK) {
274412748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
274512748SSowmini.Varadhan@oracle.COM 		err = EINVAL;
274612748SSowmini.Varadhan@oracle.COM 		goto done;
274712748SSowmini.Varadhan@oracle.COM 	}
274812748SSowmini.Varadhan@oracle.COM 
274912748SSowmini.Varadhan@oracle.COM 	/* now set the address in the data-store */
275012748SSowmini.Varadhan@oracle.COM 	err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
275112748SSowmini.Varadhan@oracle.COM 	    zaddr, strnlen(zaddr, zlen) + 1);
275212748SSowmini.Varadhan@oracle.COM 	if (err != 0)
275312748SSowmini.Varadhan@oracle.COM 		goto done;
275412748SSowmini.Varadhan@oracle.COM 
275512748SSowmini.Varadhan@oracle.COM 	/*
275612748SSowmini.Varadhan@oracle.COM 	 * add the defaultrouters
275712748SSowmini.Varadhan@oracle.COM 	 */
275812748SSowmini.Varadhan@oracle.COM 	routes = calloc(1, nnet * sizeof (*routes));
275912748SSowmini.Varadhan@oracle.COM 	j = 0;
276012748SSowmini.Varadhan@oracle.COM 	for (ptr = start; ptr != end; ptr = ptr->za_next) {
276112748SSowmini.Varadhan@oracle.COM 		address = ptr->za_nwiftab.zone_nwif_defrouter;
276212748SSowmini.Varadhan@oracle.COM 		if (address[0] == '\0')
276312748SSowmini.Varadhan@oracle.COM 			continue;
276412748SSowmini.Varadhan@oracle.COM 		if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
276512748SSowmini.Varadhan@oracle.COM 			/*
276612748SSowmini.Varadhan@oracle.COM 			 * zonecfg_valid_net_address() expects numeric IPv6
276712748SSowmini.Varadhan@oracle.COM 			 * addresses to have a CIDR format netmask.
276812748SSowmini.Varadhan@oracle.COM 			 */
276912748SSowmini.Varadhan@oracle.COM 			(void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
277012748SSowmini.Varadhan@oracle.COM 			(void) strlcat(address, tmp, INET6_ADDRSTRLEN);
277112748SSowmini.Varadhan@oracle.COM 		}
277212748SSowmini.Varadhan@oracle.COM 		if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
277312748SSowmini.Varadhan@oracle.COM 			zerror(zlogp, B_FALSE,
277412748SSowmini.Varadhan@oracle.COM 			    "invalid router [%s]\n", address);
277512748SSowmini.Varadhan@oracle.COM 			err = EINVAL;
277612748SSowmini.Varadhan@oracle.COM 			goto done;
277712748SSowmini.Varadhan@oracle.COM 		}
277812748SSowmini.Varadhan@oracle.COM 		if (lifr.lifr_addr.ss_family == AF_INET6) {
277912748SSowmini.Varadhan@oracle.COM 			routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
278012748SSowmini.Varadhan@oracle.COM 		} else {
278112748SSowmini.Varadhan@oracle.COM 			IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
278212748SSowmini.Varadhan@oracle.COM 			    &routes[j]);
278312748SSowmini.Varadhan@oracle.COM 		}
278412748SSowmini.Varadhan@oracle.COM 		j++;
278512748SSowmini.Varadhan@oracle.COM 	}
278612748SSowmini.Varadhan@oracle.COM 	assert(j <= nnet);
278712748SSowmini.Varadhan@oracle.COM 	if (j > 0) {
278812748SSowmini.Varadhan@oracle.COM 		err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
278912748SSowmini.Varadhan@oracle.COM 		    linkid, routes, j * sizeof (*routes));
279012748SSowmini.Varadhan@oracle.COM 	}
279112748SSowmini.Varadhan@oracle.COM done:
279212748SSowmini.Varadhan@oracle.COM 	free(routes);
279312748SSowmini.Varadhan@oracle.COM 	for (j = 0; j < naddr; j++)
279412748SSowmini.Varadhan@oracle.COM 		free(astr[j]);
279512748SSowmini.Varadhan@oracle.COM 	free(astr);
279612748SSowmini.Varadhan@oracle.COM 	free(zaddr);
279712748SSowmini.Varadhan@oracle.COM 	return (err);
279812748SSowmini.Varadhan@oracle.COM 
279912748SSowmini.Varadhan@oracle.COM }
280012748SSowmini.Varadhan@oracle.COM 
280112748SSowmini.Varadhan@oracle.COM static int
add_net(zlog_t * zlogp,zoneid_t zoneid,zone_addr_list_t * zalist)280212748SSowmini.Varadhan@oracle.COM add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
280312748SSowmini.Varadhan@oracle.COM {
280412748SSowmini.Varadhan@oracle.COM 	zone_addr_list_t *ptr;
280512748SSowmini.Varadhan@oracle.COM 	datalink_id_t linkid;
280612748SSowmini.Varadhan@oracle.COM 	int err;
280712748SSowmini.Varadhan@oracle.COM 
280812748SSowmini.Varadhan@oracle.COM 	if (zalist == NULL)
280912748SSowmini.Varadhan@oracle.COM 		return (0);
281012748SSowmini.Varadhan@oracle.COM 
281112748SSowmini.Varadhan@oracle.COM 	linkid = zalist->za_linkid;
281212748SSowmini.Varadhan@oracle.COM 
281312748SSowmini.Varadhan@oracle.COM 	err = add_net_for_linkid(zlogp, zoneid, zalist);
281412748SSowmini.Varadhan@oracle.COM 	if (err != 0)
281512748SSowmini.Varadhan@oracle.COM 		return (err);
281612748SSowmini.Varadhan@oracle.COM 
281712748SSowmini.Varadhan@oracle.COM 	for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
281812748SSowmini.Varadhan@oracle.COM 		if (ptr->za_linkid == linkid)
281912748SSowmini.Varadhan@oracle.COM 			continue;
282012748SSowmini.Varadhan@oracle.COM 		linkid = ptr->za_linkid;
282112748SSowmini.Varadhan@oracle.COM 		err = add_net_for_linkid(zlogp, zoneid, ptr);
282212748SSowmini.Varadhan@oracle.COM 		if (err != 0)
282312748SSowmini.Varadhan@oracle.COM 			return (err);
282412748SSowmini.Varadhan@oracle.COM 	}
282512748SSowmini.Varadhan@oracle.COM 	return (0);
282612748SSowmini.Varadhan@oracle.COM }
282712748SSowmini.Varadhan@oracle.COM 
282812748SSowmini.Varadhan@oracle.COM /*
282912748SSowmini.Varadhan@oracle.COM  * Add "new" to the list of network interfaces to be configured  by
283012748SSowmini.Varadhan@oracle.COM  * add_net on zone boot in "old". The list of interfaces in "old" is
283112748SSowmini.Varadhan@oracle.COM  * sorted by datalink_id_t, with interfaces sorted FIFO for a given
283212748SSowmini.Varadhan@oracle.COM  * datalink_id_t.
283312748SSowmini.Varadhan@oracle.COM  *
283412748SSowmini.Varadhan@oracle.COM  * Returns the merged list of IP interfaces containing "old" and "new"
283512748SSowmini.Varadhan@oracle.COM  */
283612748SSowmini.Varadhan@oracle.COM static zone_addr_list_t *
add_ip_interface(zone_addr_list_t * old,zone_addr_list_t * new)283712748SSowmini.Varadhan@oracle.COM add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
283812748SSowmini.Varadhan@oracle.COM {
283912748SSowmini.Varadhan@oracle.COM 	zone_addr_list_t *ptr, *next;
284012748SSowmini.Varadhan@oracle.COM 	datalink_id_t linkid = new->za_linkid;
284112748SSowmini.Varadhan@oracle.COM 
284212748SSowmini.Varadhan@oracle.COM 	assert(old != new);
284312748SSowmini.Varadhan@oracle.COM 
284412748SSowmini.Varadhan@oracle.COM 	if (old == NULL)
284512748SSowmini.Varadhan@oracle.COM 		return (new);
284612748SSowmini.Varadhan@oracle.COM 	for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
284712748SSowmini.Varadhan@oracle.COM 		if (ptr->za_linkid == linkid)
284812748SSowmini.Varadhan@oracle.COM 			break;
284912748SSowmini.Varadhan@oracle.COM 	}
285012748SSowmini.Varadhan@oracle.COM 	if (ptr == NULL) {
285112748SSowmini.Varadhan@oracle.COM 		/* linkid does not already exist, add to the beginning */
285212748SSowmini.Varadhan@oracle.COM 		new->za_next = old;
285312748SSowmini.Varadhan@oracle.COM 		return (new);
285412748SSowmini.Varadhan@oracle.COM 	}
285512748SSowmini.Varadhan@oracle.COM 	/*
285612748SSowmini.Varadhan@oracle.COM 	 * adding to the middle of the list; ptr points at the first
285712748SSowmini.Varadhan@oracle.COM 	 * occurrence of linkid. Find the last occurrence.
285812748SSowmini.Varadhan@oracle.COM 	 */
285912748SSowmini.Varadhan@oracle.COM 	while ((next = ptr->za_next) != NULL) {
286012748SSowmini.Varadhan@oracle.COM 		if (next->za_linkid != linkid)
286112748SSowmini.Varadhan@oracle.COM 			break;
286212748SSowmini.Varadhan@oracle.COM 		ptr = next;
286312748SSowmini.Varadhan@oracle.COM 	}
286412748SSowmini.Varadhan@oracle.COM 	/* insert new after ptr */
286512748SSowmini.Varadhan@oracle.COM 	new->za_next = next;
286612748SSowmini.Varadhan@oracle.COM 	ptr->za_next = new;
286712748SSowmini.Varadhan@oracle.COM 	return (old);
286812748SSowmini.Varadhan@oracle.COM }
286912748SSowmini.Varadhan@oracle.COM 
287012748SSowmini.Varadhan@oracle.COM void
free_ip_interface(zone_addr_list_t * zalist)287112748SSowmini.Varadhan@oracle.COM free_ip_interface(zone_addr_list_t *zalist)
287212748SSowmini.Varadhan@oracle.COM {
287312748SSowmini.Varadhan@oracle.COM 	zone_addr_list_t *ptr, *new;
287412748SSowmini.Varadhan@oracle.COM 
287512748SSowmini.Varadhan@oracle.COM 	for (ptr = zalist; ptr != NULL; ) {
287612748SSowmini.Varadhan@oracle.COM 		new = ptr;
287712748SSowmini.Varadhan@oracle.COM 		ptr = ptr->za_next;
287812748SSowmini.Varadhan@oracle.COM 		free(new);
287912748SSowmini.Varadhan@oracle.COM 	}
288012748SSowmini.Varadhan@oracle.COM }
288112748SSowmini.Varadhan@oracle.COM 
28823448Sdh155122 /*
28833448Sdh155122  * Add the kernel access control information for the interface names.
28843448Sdh155122  * If anything goes wrong, we log a general error message, attempt to tear down
28853448Sdh155122  * whatever we set up, and return an error.
28863448Sdh155122  */
28873448Sdh155122 static int
configure_exclusive_network_interfaces(zlog_t * zlogp,zoneid_t zoneid)288812748SSowmini.Varadhan@oracle.COM configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
28893448Sdh155122 {
28903448Sdh155122 	zone_dochandle_t handle;
28913448Sdh155122 	struct zone_nwiftab nwiftab;
28923448Sdh155122 	char rootpath[MAXPATHLEN];
28933448Sdh155122 	char path[MAXPATHLEN];
289410616SSebastien.Roy@Sun.COM 	datalink_id_t linkid;
28953448Sdh155122 	di_prof_t prof = NULL;
28963448Sdh155122 	boolean_t added = B_FALSE;
289712748SSowmini.Varadhan@oracle.COM 	zone_addr_list_t *zalist = NULL, *new;
28983448Sdh155122 
28993448Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
29003448Sdh155122 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
29013448Sdh155122 		return (-1);
29023448Sdh155122 	}
29033448Sdh155122 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
29043448Sdh155122 		zerror(zlogp, B_FALSE, "invalid configuration");
29053448Sdh155122 		zonecfg_fini_handle(handle);
29063448Sdh155122 		return (-1);
29073448Sdh155122 	}
29083448Sdh155122 
29093448Sdh155122 	if (zonecfg_setnwifent(handle) != Z_OK) {
29103448Sdh155122 		zonecfg_fini_handle(handle);
29113448Sdh155122 		return (0);
29123448Sdh155122 	}
29133448Sdh155122 
29143448Sdh155122 	for (;;) {
29153448Sdh155122 		if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
29163448Sdh155122 			break;
29173448Sdh155122 
29183448Sdh155122 		if (prof == NULL) {
29193448Sdh155122 			if (zone_get_devroot(zone_name, rootpath,
29203448Sdh155122 			    sizeof (rootpath)) != Z_OK) {
29213448Sdh155122 				(void) zonecfg_endnwifent(handle);
29223448Sdh155122 				zonecfg_fini_handle(handle);
29233448Sdh155122 				zerror(zlogp, B_TRUE,
29243448Sdh155122 				    "unable to determine dev root");
29253448Sdh155122 				return (-1);
29263448Sdh155122 			}
29273448Sdh155122 			(void) snprintf(path, sizeof (path), "%s%s", rootpath,
29283448Sdh155122 			    "/dev");
29293448Sdh155122 			if (di_prof_init(path, &prof) != 0) {
29303448Sdh155122 				(void) zonecfg_endnwifent(handle);
29313448Sdh155122 				zonecfg_fini_handle(handle);
29323448Sdh155122 				zerror(zlogp, B_TRUE,
29333448Sdh155122 				    "failed to initialize profile");
29343448Sdh155122 				return (-1);
29353448Sdh155122 			}
29363448Sdh155122 		}
29373448Sdh155122 
29383448Sdh155122 		/*
29395895Syz147064 		 * Create the /dev entry for backward compatibility.
29403448Sdh155122 		 * Only create the /dev entry if it's not in use.
29415895Syz147064 		 * Note that the zone still boots when the assigned
29425895Syz147064 		 * interface is inaccessible, used by others, etc.
29435895Syz147064 		 * Also, when vanity naming is used, some interface do
29445895Syz147064 		 * do not have corresponding /dev node names (for example,
29455895Syz147064 		 * vanity named aggregations).  The /dev entry is not
29465895Syz147064 		 * created in that case.  The /dev/net entry is always
29475895Syz147064 		 * accessible.
29483448Sdh155122 		 */
294910616SSebastien.Roy@Sun.COM 		if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
295010616SSebastien.Roy@Sun.COM 		    &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
295110616SSebastien.Roy@Sun.COM 		    add_datalink(zlogp, zone_name, linkid,
295210616SSebastien.Roy@Sun.COM 		    nwiftab.zone_nwif_physical) == 0) {
29537342SAruna.Ramakrishna@Sun.COM 			added = B_TRUE;
29547342SAruna.Ramakrishna@Sun.COM 		} else {
29557342SAruna.Ramakrishna@Sun.COM 			(void) zonecfg_endnwifent(handle);
29567342SAruna.Ramakrishna@Sun.COM 			zonecfg_fini_handle(handle);
29577342SAruna.Ramakrishna@Sun.COM 			zerror(zlogp, B_TRUE, "failed to add network device");
29587342SAruna.Ramakrishna@Sun.COM 			return (-1);
29593448Sdh155122 		}
296012748SSowmini.Varadhan@oracle.COM 		/* set up the new IP interface, and add them all later */
296112748SSowmini.Varadhan@oracle.COM 		new = malloc(sizeof (*new));
296212748SSowmini.Varadhan@oracle.COM 		if (new == NULL) {
296312748SSowmini.Varadhan@oracle.COM 			zerror(zlogp, B_TRUE, "no memory for %s",
296412748SSowmini.Varadhan@oracle.COM 			    nwiftab.zone_nwif_physical);
296512748SSowmini.Varadhan@oracle.COM 			zonecfg_fini_handle(handle);
296612748SSowmini.Varadhan@oracle.COM 			free_ip_interface(zalist);
296712748SSowmini.Varadhan@oracle.COM 		}
296812748SSowmini.Varadhan@oracle.COM 		bzero(new, sizeof (*new));
296912748SSowmini.Varadhan@oracle.COM 		new->za_nwiftab = nwiftab;
297012748SSowmini.Varadhan@oracle.COM 		new->za_linkid = linkid;
297112748SSowmini.Varadhan@oracle.COM 		zalist = add_ip_interface(zalist, new);
297212748SSowmini.Varadhan@oracle.COM 	}
297312748SSowmini.Varadhan@oracle.COM 	if (zalist != NULL) {
297412748SSowmini.Varadhan@oracle.COM 		if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
297512748SSowmini.Varadhan@oracle.COM 			(void) zonecfg_endnwifent(handle);
297612748SSowmini.Varadhan@oracle.COM 			zonecfg_fini_handle(handle);
297712748SSowmini.Varadhan@oracle.COM 			zerror(zlogp, B_TRUE, "failed to add address");
297812748SSowmini.Varadhan@oracle.COM 			free_ip_interface(zalist);
297912748SSowmini.Varadhan@oracle.COM 			return (-1);
298012748SSowmini.Varadhan@oracle.COM 		}
298112748SSowmini.Varadhan@oracle.COM 		free_ip_interface(zalist);
29823448Sdh155122 	}
29833448Sdh155122 	(void) zonecfg_endnwifent(handle);
29843448Sdh155122 	zonecfg_fini_handle(handle);
29853448Sdh155122 
29863448Sdh155122 	if (prof != NULL && added) {
29873448Sdh155122 		if (di_prof_commit(prof) != 0) {
29883448Sdh155122 			zerror(zlogp, B_TRUE, "failed to commit profile");
29893448Sdh155122 			return (-1);
29903448Sdh155122 		}
29913448Sdh155122 	}
29923448Sdh155122 	if (prof != NULL)
29933448Sdh155122 		di_prof_fini(prof);
29943448Sdh155122 
29953448Sdh155122 	return (0);
29963448Sdh155122 }
29973448Sdh155122 
29983448Sdh155122 static int
remove_datalink_pool(zlog_t * zlogp,zoneid_t zoneid)299911878SVenu.Iyer@Sun.COM remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
300011878SVenu.Iyer@Sun.COM {
300111878SVenu.Iyer@Sun.COM 	ushort_t flags;
300211878SVenu.Iyer@Sun.COM 	zone_iptype_t iptype;
300311878SVenu.Iyer@Sun.COM 	int i, dlnum = 0;
300411878SVenu.Iyer@Sun.COM 	datalink_id_t *dllink, *dllinks = NULL;
300511878SVenu.Iyer@Sun.COM 	dladm_status_t err;
300611878SVenu.Iyer@Sun.COM 
300711878SVenu.Iyer@Sun.COM 	if (strlen(pool_name) == 0)
300811878SVenu.Iyer@Sun.COM 		return (0);
300911878SVenu.Iyer@Sun.COM 
301011878SVenu.Iyer@Sun.COM 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
301111878SVenu.Iyer@Sun.COM 	    sizeof (flags)) < 0) {
301211878SVenu.Iyer@Sun.COM 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
301312748SSowmini.Varadhan@oracle.COM 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
301411878SVenu.Iyer@Sun.COM 			return (-1);
301511878SVenu.Iyer@Sun.COM 		}
301611878SVenu.Iyer@Sun.COM 	} else {
301711878SVenu.Iyer@Sun.COM 		if (flags & ZF_NET_EXCL)
301811878SVenu.Iyer@Sun.COM 			iptype = ZS_EXCLUSIVE;
301911878SVenu.Iyer@Sun.COM 		else
302011878SVenu.Iyer@Sun.COM 			iptype = ZS_SHARED;
302111878SVenu.Iyer@Sun.COM 	}
302211878SVenu.Iyer@Sun.COM 
302311878SVenu.Iyer@Sun.COM 	if (iptype == ZS_EXCLUSIVE) {
302411878SVenu.Iyer@Sun.COM 		/*
302511878SVenu.Iyer@Sun.COM 		 * Get the datalink count and for each datalink,
302611878SVenu.Iyer@Sun.COM 		 * attempt to clear the pool property and clear
302711878SVenu.Iyer@Sun.COM 		 * the pool_name.
302811878SVenu.Iyer@Sun.COM 		 */
302911878SVenu.Iyer@Sun.COM 		if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
303011878SVenu.Iyer@Sun.COM 			zerror(zlogp, B_TRUE, "unable to count network "
303111878SVenu.Iyer@Sun.COM 			    "interfaces");
303211878SVenu.Iyer@Sun.COM 			return (-1);
303311878SVenu.Iyer@Sun.COM 		}
303411878SVenu.Iyer@Sun.COM 
303511878SVenu.Iyer@Sun.COM 		if (dlnum == 0)
303611878SVenu.Iyer@Sun.COM 			return (0);
303711878SVenu.Iyer@Sun.COM 
303811878SVenu.Iyer@Sun.COM 		if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
303911878SVenu.Iyer@Sun.COM 		    == NULL) {
304011878SVenu.Iyer@Sun.COM 			zerror(zlogp, B_TRUE, "memory allocation failed");
304111878SVenu.Iyer@Sun.COM 			return (-1);
304211878SVenu.Iyer@Sun.COM 		}
304311878SVenu.Iyer@Sun.COM 		if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
304411878SVenu.Iyer@Sun.COM 			zerror(zlogp, B_TRUE, "unable to list network "
304511878SVenu.Iyer@Sun.COM 			    "interfaces");
304611878SVenu.Iyer@Sun.COM 			return (-1);
304711878SVenu.Iyer@Sun.COM 		}
304811878SVenu.Iyer@Sun.COM 
304911878SVenu.Iyer@Sun.COM 		bzero(pool_name, MAXPATHLEN);
305011878SVenu.Iyer@Sun.COM 		for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
305111878SVenu.Iyer@Sun.COM 			err = dladm_set_linkprop(dld_handle, *dllink, "pool",
305211878SVenu.Iyer@Sun.COM 			    NULL, 0, DLADM_OPT_ACTIVE);
305311878SVenu.Iyer@Sun.COM 			if (err != DLADM_STATUS_OK) {
305411878SVenu.Iyer@Sun.COM 				zerror(zlogp, B_TRUE,
305511878SVenu.Iyer@Sun.COM 				    "WARNING: unable to clear pool");
305611878SVenu.Iyer@Sun.COM 			}
305711878SVenu.Iyer@Sun.COM 		}
305811878SVenu.Iyer@Sun.COM 		free(dllinks);
305911878SVenu.Iyer@Sun.COM 	}
306011878SVenu.Iyer@Sun.COM 	return (0);
306111878SVenu.Iyer@Sun.COM }
306211878SVenu.Iyer@Sun.COM 
306311878SVenu.Iyer@Sun.COM static int
remove_datalink_protect(zlog_t * zlogp,zoneid_t zoneid)306412748SSowmini.Varadhan@oracle.COM remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
306512748SSowmini.Varadhan@oracle.COM {
306612748SSowmini.Varadhan@oracle.COM 	ushort_t flags;
306712748SSowmini.Varadhan@oracle.COM 	zone_iptype_t iptype;
306812748SSowmini.Varadhan@oracle.COM 	int i, dlnum = 0;
306912748SSowmini.Varadhan@oracle.COM 	dladm_status_t dlstatus;
307012748SSowmini.Varadhan@oracle.COM 	datalink_id_t *dllink, *dllinks = NULL;
307112748SSowmini.Varadhan@oracle.COM 
307212748SSowmini.Varadhan@oracle.COM 	if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
307312748SSowmini.Varadhan@oracle.COM 	    sizeof (flags)) < 0) {
307412748SSowmini.Varadhan@oracle.COM 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
307512748SSowmini.Varadhan@oracle.COM 			zerror(zlogp, B_FALSE, "unable to determine ip-type");
307612748SSowmini.Varadhan@oracle.COM 			return (-1);
307712748SSowmini.Varadhan@oracle.COM 		}
307812748SSowmini.Varadhan@oracle.COM 	} else {
307912748SSowmini.Varadhan@oracle.COM 		if (flags & ZF_NET_EXCL)
308012748SSowmini.Varadhan@oracle.COM 			iptype = ZS_EXCLUSIVE;
308112748SSowmini.Varadhan@oracle.COM 		else
308212748SSowmini.Varadhan@oracle.COM 			iptype = ZS_SHARED;
308312748SSowmini.Varadhan@oracle.COM 	}
308412748SSowmini.Varadhan@oracle.COM 
308512748SSowmini.Varadhan@oracle.COM 	if (iptype != ZS_EXCLUSIVE)
308612748SSowmini.Varadhan@oracle.COM 		return (0);
308712748SSowmini.Varadhan@oracle.COM 
308812748SSowmini.Varadhan@oracle.COM 	/*
308912748SSowmini.Varadhan@oracle.COM 	 * Get the datalink count and for each datalink,
309012748SSowmini.Varadhan@oracle.COM 	 * attempt to clear the pool property and clear
309112748SSowmini.Varadhan@oracle.COM 	 * the pool_name.
309212748SSowmini.Varadhan@oracle.COM 	 */
309312748SSowmini.Varadhan@oracle.COM 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
309412748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_TRUE, "unable to count network interfaces");
309512748SSowmini.Varadhan@oracle.COM 		return (-1);
309612748SSowmini.Varadhan@oracle.COM 	}
309712748SSowmini.Varadhan@oracle.COM 
309812748SSowmini.Varadhan@oracle.COM 	if (dlnum == 0)
309912748SSowmini.Varadhan@oracle.COM 		return (0);
310012748SSowmini.Varadhan@oracle.COM 
310112748SSowmini.Varadhan@oracle.COM 	if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
310212748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_TRUE, "memory allocation failed");
310312748SSowmini.Varadhan@oracle.COM 		return (-1);
310412748SSowmini.Varadhan@oracle.COM 	}
310512748SSowmini.Varadhan@oracle.COM 	if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
310612748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
310712748SSowmini.Varadhan@oracle.COM 		free(dllinks);
310812748SSowmini.Varadhan@oracle.COM 		return (-1);
310912748SSowmini.Varadhan@oracle.COM 	}
311012748SSowmini.Varadhan@oracle.COM 
311112748SSowmini.Varadhan@oracle.COM 	for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
311212985SSowmini.Varadhan@oracle.COM 		char dlerr[DLADM_STRSIZE];
311312985SSowmini.Varadhan@oracle.COM 
311412748SSowmini.Varadhan@oracle.COM 		dlstatus = dladm_set_linkprop(dld_handle, *dllink,
311512748SSowmini.Varadhan@oracle.COM 		    "protection", NULL, 0, DLADM_OPT_ACTIVE);
311612985SSowmini.Varadhan@oracle.COM 		if (dlstatus == DLADM_STATUS_NOTFOUND) {
311712985SSowmini.Varadhan@oracle.COM 			/* datalink does not belong to the GZ */
311812985SSowmini.Varadhan@oracle.COM 			continue;
311912985SSowmini.Varadhan@oracle.COM 		}
312012748SSowmini.Varadhan@oracle.COM 		if (dlstatus != DLADM_STATUS_OK) {
312112985SSowmini.Varadhan@oracle.COM 			zerror(zlogp, B_FALSE,
312212985SSowmini.Varadhan@oracle.COM 			    dladm_status2str(dlstatus, dlerr));
312312748SSowmini.Varadhan@oracle.COM 			free(dllinks);
312412748SSowmini.Varadhan@oracle.COM 			return (-1);
312512748SSowmini.Varadhan@oracle.COM 		}
312612748SSowmini.Varadhan@oracle.COM 		dlstatus = dladm_set_linkprop(dld_handle, *dllink,
312712748SSowmini.Varadhan@oracle.COM 		    "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
312812748SSowmini.Varadhan@oracle.COM 		if (dlstatus != DLADM_STATUS_OK) {
312912985SSowmini.Varadhan@oracle.COM 			zerror(zlogp, B_FALSE,
313012985SSowmini.Varadhan@oracle.COM 			    dladm_status2str(dlstatus, dlerr));
313112748SSowmini.Varadhan@oracle.COM 			free(dllinks);
313212748SSowmini.Varadhan@oracle.COM 			return (-1);
313312748SSowmini.Varadhan@oracle.COM 		}
313412748SSowmini.Varadhan@oracle.COM 	}
313512748SSowmini.Varadhan@oracle.COM 	free(dllinks);
313612748SSowmini.Varadhan@oracle.COM 	return (0);
313712748SSowmini.Varadhan@oracle.COM }
313812748SSowmini.Varadhan@oracle.COM 
313912748SSowmini.Varadhan@oracle.COM static int
unconfigure_exclusive_network_interfaces(zlog_t * zlogp,zoneid_t zoneid)314010616SSebastien.Roy@Sun.COM unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
31413448Sdh155122 {
314210616SSebastien.Roy@Sun.COM 	int dlnum = 0;
314310616SSebastien.Roy@Sun.COM 
314410616SSebastien.Roy@Sun.COM 	/*
314510616SSebastien.Roy@Sun.COM 	 * The kernel shutdown callback for the dls module should have removed
314610616SSebastien.Roy@Sun.COM 	 * all datalinks from this zone.  If any remain, then there's a
314710616SSebastien.Roy@Sun.COM 	 * problem.
314810616SSebastien.Roy@Sun.COM 	 */
31493448Sdh155122 	if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
31503448Sdh155122 		zerror(zlogp, B_TRUE, "unable to list network interfaces");
31513448Sdh155122 		return (-1);
31523448Sdh155122 	}
315310616SSebastien.Roy@Sun.COM 	if (dlnum != 0) {
315410616SSebastien.Roy@Sun.COM 		zerror(zlogp, B_FALSE,
315510616SSebastien.Roy@Sun.COM 		    "datalinks remain in zone after shutdown");
31563448Sdh155122 		return (-1);
31573448Sdh155122 	}
31583448Sdh155122 	return (0);
31593448Sdh155122 }
31603448Sdh155122 
31610Sstevel@tonic-gate static int
tcp_abort_conn(zlog_t * zlogp,zoneid_t zoneid,const struct sockaddr_storage * local,const struct sockaddr_storage * remote)31620Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
31630Sstevel@tonic-gate     const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
31640Sstevel@tonic-gate {
31650Sstevel@tonic-gate 	int fd;
31660Sstevel@tonic-gate 	struct strioctl ioc;
31670Sstevel@tonic-gate 	tcp_ioc_abort_conn_t conn;
31680Sstevel@tonic-gate 	int error;
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 	conn.ac_local = *local;
31710Sstevel@tonic-gate 	conn.ac_remote = *remote;
31720Sstevel@tonic-gate 	conn.ac_start = TCPS_SYN_SENT;
31730Sstevel@tonic-gate 	conn.ac_end = TCPS_TIME_WAIT;
31740Sstevel@tonic-gate 	conn.ac_zoneid = zoneid;
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 	ioc.ic_cmd = TCP_IOC_ABORT_CONN;
31770Sstevel@tonic-gate 	ioc.ic_timout = -1; /* infinite timeout */
31780Sstevel@tonic-gate 	ioc.ic_len = sizeof (conn);
31790Sstevel@tonic-gate 	ioc.ic_dp = (char *)&conn;
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 	if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
31820Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
31830Sstevel@tonic-gate 		return (-1);
31840Sstevel@tonic-gate 	}
31850Sstevel@tonic-gate 
31860Sstevel@tonic-gate 	error = ioctl(fd, I_STR, &ioc);
31870Sstevel@tonic-gate 	(void) close(fd);
31880Sstevel@tonic-gate 	if (error == 0 || errno == ENOENT)	/* ENOENT is not an error */
31890Sstevel@tonic-gate 		return (0);
31900Sstevel@tonic-gate 	return (-1);
31910Sstevel@tonic-gate }
31920Sstevel@tonic-gate 
31930Sstevel@tonic-gate static int
tcp_abort_connections(zlog_t * zlogp,zoneid_t zoneid)31940Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
31950Sstevel@tonic-gate {
31960Sstevel@tonic-gate 	struct sockaddr_storage l, r;
31970Sstevel@tonic-gate 	struct sockaddr_in *local, *remote;
31980Sstevel@tonic-gate 	struct sockaddr_in6 *local6, *remote6;
31990Sstevel@tonic-gate 	int error;
32000Sstevel@tonic-gate 
32010Sstevel@tonic-gate 	/*
32020Sstevel@tonic-gate 	 * Abort IPv4 connections.
32030Sstevel@tonic-gate 	 */
32040Sstevel@tonic-gate 	bzero(&l, sizeof (*local));
32050Sstevel@tonic-gate 	local = (struct sockaddr_in *)&l;
32060Sstevel@tonic-gate 	local->sin_family = AF_INET;
32070Sstevel@tonic-gate 	local->sin_addr.s_addr = INADDR_ANY;
32080Sstevel@tonic-gate 	local->sin_port = 0;
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 	bzero(&r, sizeof (*remote));
32110Sstevel@tonic-gate 	remote = (struct sockaddr_in *)&r;
32120Sstevel@tonic-gate 	remote->sin_family = AF_INET;
32130Sstevel@tonic-gate 	remote->sin_addr.s_addr = INADDR_ANY;
32140Sstevel@tonic-gate 	remote->sin_port = 0;
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32170Sstevel@tonic-gate 		return (error);
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate 	/*
32200Sstevel@tonic-gate 	 * Abort IPv6 connections.
32210Sstevel@tonic-gate 	 */
32220Sstevel@tonic-gate 	bzero(&l, sizeof (*local6));
32230Sstevel@tonic-gate 	local6 = (struct sockaddr_in6 *)&l;
32240Sstevel@tonic-gate 	local6->sin6_family = AF_INET6;
32250Sstevel@tonic-gate 	local6->sin6_port = 0;
32260Sstevel@tonic-gate 	local6->sin6_addr = in6addr_any;
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	bzero(&r, sizeof (*remote6));
32290Sstevel@tonic-gate 	remote6 = (struct sockaddr_in6 *)&r;
32300Sstevel@tonic-gate 	remote6->sin6_family = AF_INET6;
32310Sstevel@tonic-gate 	remote6->sin6_port = 0;
32320Sstevel@tonic-gate 	remote6->sin6_addr = in6addr_any;
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 	if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32350Sstevel@tonic-gate 		return (error);
32360Sstevel@tonic-gate 	return (0);
32370Sstevel@tonic-gate }
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate static int
get_privset(zlog_t * zlogp,priv_set_t * privs,zone_mnt_t mount_cmd)32405829Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
32411645Scomay {
32421645Scomay 	int error = -1;
32431645Scomay 	zone_dochandle_t handle;
32441645Scomay 	char *privname = NULL;
32451645Scomay 
32461645Scomay 	if ((handle = zonecfg_init_handle()) == NULL) {
32471645Scomay 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
32481645Scomay 		return (-1);
32491645Scomay 	}
32501645Scomay 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
32511645Scomay 		zerror(zlogp, B_FALSE, "invalid configuration");
32521645Scomay 		zonecfg_fini_handle(handle);
32531645Scomay 		return (-1);
32541645Scomay 	}
32551645Scomay 
32565829Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
32573673Sdh155122 		zone_iptype_t	iptype;
32583673Sdh155122 		const char	*curr_iptype;
32593673Sdh155122 
32603673Sdh155122 		if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
32613673Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
32623673Sdh155122 			zonecfg_fini_handle(handle);
32633673Sdh155122 			return (-1);
32643673Sdh155122 		}
32653673Sdh155122 
32663673Sdh155122 		switch (iptype) {
32673673Sdh155122 		case ZS_SHARED:
32683673Sdh155122 			curr_iptype = "shared";
32693673Sdh155122 			break;
32703673Sdh155122 		case ZS_EXCLUSIVE:
32713673Sdh155122 			curr_iptype = "exclusive";
32723673Sdh155122 			break;
32733673Sdh155122 		}
32743673Sdh155122 
32753716Sgjelinek 		if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
32763716Sgjelinek 			zonecfg_fini_handle(handle);
32772712Snn35248 			return (0);
32783716Sgjelinek 		}
32792712Snn35248 		zerror(zlogp, B_FALSE,
32802712Snn35248 		    "failed to determine the zone's default privilege set");
32812712Snn35248 		zonecfg_fini_handle(handle);
32822712Snn35248 		return (-1);
32832712Snn35248 	}
32842712Snn35248 
32851645Scomay 	switch (zonecfg_get_privset(handle, privs, &privname)) {
32861645Scomay 	case Z_OK:
32871645Scomay 		error = 0;
32881645Scomay 		break;
32891645Scomay 	case Z_PRIV_PROHIBITED:
32901645Scomay 		zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
32911645Scomay 		    "within the zone's privilege set", privname);
32921645Scomay 		break;
32931645Scomay 	case Z_PRIV_REQUIRED:
32941645Scomay 		zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
32951645Scomay 		    "from the zone's privilege set", privname);
32961645Scomay 		break;
32971645Scomay 	case Z_PRIV_UNKNOWN:
32981645Scomay 		zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
32991645Scomay 		    "in the zone's privilege set", privname);
33001645Scomay 		break;
33011645Scomay 	default:
33021645Scomay 		zerror(zlogp, B_FALSE, "failed to determine the zone's "
33031645Scomay 		    "privilege set");
33041645Scomay 		break;
33051645Scomay 	}
33061645Scomay 
33071645Scomay 	free(privname);
33081645Scomay 	zonecfg_fini_handle(handle);
33091645Scomay 	return (error);
33101645Scomay }
33111645Scomay 
33121645Scomay static int
get_rctls(zlog_t * zlogp,char ** bufp,size_t * bufsizep)33130Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
33140Sstevel@tonic-gate {
33150Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
33160Sstevel@tonic-gate 	char *nvl_packed = NULL;
33170Sstevel@tonic-gate 	size_t nvl_size = 0;
33180Sstevel@tonic-gate 	nvlist_t **nvlv = NULL;
33190Sstevel@tonic-gate 	int rctlcount = 0;
33200Sstevel@tonic-gate 	int error = -1;
33210Sstevel@tonic-gate 	zone_dochandle_t handle;
33220Sstevel@tonic-gate 	struct zone_rctltab rctltab;
33230Sstevel@tonic-gate 	rctlblk_t *rctlblk = NULL;
332412725SMenno.Lageman@Sun.COM 	uint64_t maxlwps;
332512725SMenno.Lageman@Sun.COM 	uint64_t maxprocs;
33260Sstevel@tonic-gate 
33270Sstevel@tonic-gate 	*bufp = NULL;
33280Sstevel@tonic-gate 	*bufsizep = 0;
33290Sstevel@tonic-gate 
33300Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
33310Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
33320Sstevel@tonic-gate 		return (-1);
33330Sstevel@tonic-gate 	}
33340Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
33350Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "invalid configuration");
33360Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
33370Sstevel@tonic-gate 		return (-1);
33380Sstevel@tonic-gate 	}
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
33410Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
33420Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
33430Sstevel@tonic-gate 		goto out;
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 
334612725SMenno.Lageman@Sun.COM 	/*
334712725SMenno.Lageman@Sun.COM 	 * Allow the administrator to control both the maximum number of
334812725SMenno.Lageman@Sun.COM 	 * process table slots and the maximum number of lwps with just the
334912725SMenno.Lageman@Sun.COM 	 * max-processes property.  If only the max-processes property is set,
335012725SMenno.Lageman@Sun.COM 	 * we add a max-lwps property with a limit derived from max-processes.
335112725SMenno.Lageman@Sun.COM 	 */
335212725SMenno.Lageman@Sun.COM 	if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs)
335312725SMenno.Lageman@Sun.COM 	    == Z_OK &&
335412725SMenno.Lageman@Sun.COM 	    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps)
335512725SMenno.Lageman@Sun.COM 	    == Z_NO_ENTRY) {
335612725SMenno.Lageman@Sun.COM 		if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
335712725SMenno.Lageman@Sun.COM 		    maxprocs * LWPS_PER_PROCESS) != Z_OK) {
335812725SMenno.Lageman@Sun.COM 			zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
335912725SMenno.Lageman@Sun.COM 			goto out;
336012725SMenno.Lageman@Sun.COM 		}
336112725SMenno.Lageman@Sun.COM 	}
336212725SMenno.Lageman@Sun.COM 
33630Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
33640Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
33650Sstevel@tonic-gate 		goto out;
33660Sstevel@tonic-gate 	}
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 	if ((rctlblk = malloc(rctlblk_size())) == NULL) {
33690Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "memory allocation failed");
33700Sstevel@tonic-gate 		goto out;
33710Sstevel@tonic-gate 	}
33720Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
33730Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
33740Sstevel@tonic-gate 		uint_t i, count;
33750Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate 		/* zoneadm should have already warned about unknown rctls. */
33780Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
33790Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
33800Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
33810Sstevel@tonic-gate 			continue;
33820Sstevel@tonic-gate 		}
33830Sstevel@tonic-gate 		count = 0;
33840Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
33850Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
33860Sstevel@tonic-gate 			count++;
33870Sstevel@tonic-gate 		}
33880Sstevel@tonic-gate 		if (count == 0) {	/* ignore */
33890Sstevel@tonic-gate 			continue;	/* Nothing to free */
33900Sstevel@tonic-gate 		}
33910Sstevel@tonic-gate 		if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
33920Sstevel@tonic-gate 			goto out;
33930Sstevel@tonic-gate 		i = 0;
33940Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
33950Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next, i++) {
33960Sstevel@tonic-gate 			if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
33970Sstevel@tonic-gate 				zerror(zlogp, B_TRUE, "%s failed",
33980Sstevel@tonic-gate 				    "nvlist_alloc");
33990Sstevel@tonic-gate 				goto out;
34000Sstevel@tonic-gate 			}
34010Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
34020Sstevel@tonic-gate 			    != Z_OK) {
34030Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "invalid rctl value: "
34040Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s)",
34050Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
34060Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
34070Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
34080Sstevel@tonic-gate 				goto out;
34090Sstevel@tonic-gate 			}
34100Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
34110Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
34120Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action=%s) is not a "
34130Sstevel@tonic-gate 				    "valid value for rctl '%s'",
34140Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
34150Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
34160Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
34170Sstevel@tonic-gate 				    name);
34180Sstevel@tonic-gate 				goto out;
34190Sstevel@tonic-gate 			}
34200Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "privilege",
34211645Scomay 			    rctlblk_get_privilege(rctlblk)) != 0) {
34220Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34230Sstevel@tonic-gate 				    "nvlist_add_uint64");
34240Sstevel@tonic-gate 				goto out;
34250Sstevel@tonic-gate 			}
34260Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "limit",
34271645Scomay 			    rctlblk_get_value(rctlblk)) != 0) {
34280Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34290Sstevel@tonic-gate 				    "nvlist_add_uint64");
34300Sstevel@tonic-gate 				goto out;
34310Sstevel@tonic-gate 			}
34320Sstevel@tonic-gate 			if (nvlist_add_uint64(nvlv[i], "action",
34330Sstevel@tonic-gate 			    (uint_t)rctlblk_get_local_action(rctlblk, NULL))
34340Sstevel@tonic-gate 			    != 0) {
34350Sstevel@tonic-gate 				zerror(zlogp, B_FALSE, "%s failed",
34360Sstevel@tonic-gate 				    "nvlist_add_uint64");
34370Sstevel@tonic-gate 				goto out;
34380Sstevel@tonic-gate 			}
34390Sstevel@tonic-gate 		}
34400Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34410Sstevel@tonic-gate 		rctltab.zone_rctl_valptr = NULL;
34420Sstevel@tonic-gate 		if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
34430Sstevel@tonic-gate 		    != 0) {
34440Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s failed",
34450Sstevel@tonic-gate 			    "nvlist_add_nvlist_array");
34460Sstevel@tonic-gate 			goto out;
34470Sstevel@tonic-gate 		}
34480Sstevel@tonic-gate 		for (i = 0; i < count; i++)
34490Sstevel@tonic-gate 			nvlist_free(nvlv[i]);
34500Sstevel@tonic-gate 		free(nvlv);
34510Sstevel@tonic-gate 		nvlv = NULL;
34520Sstevel@tonic-gate 		rctlcount++;
34530Sstevel@tonic-gate 	}
34540Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
34550Sstevel@tonic-gate 
34560Sstevel@tonic-gate 	if (rctlcount == 0) {
34570Sstevel@tonic-gate 		error = 0;
34580Sstevel@tonic-gate 		goto out;
34590Sstevel@tonic-gate 	}
34600Sstevel@tonic-gate 	if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
34610Sstevel@tonic-gate 	    != 0) {
34620Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
34630Sstevel@tonic-gate 		goto out;
34640Sstevel@tonic-gate 	}
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 	error = 0;
34670Sstevel@tonic-gate 	*bufp = nvl_packed;
34680Sstevel@tonic-gate 	*bufsizep = nvl_size;
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate out:
34710Sstevel@tonic-gate 	free(rctlblk);
34720Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34730Sstevel@tonic-gate 	if (error && nvl_packed != NULL)
34740Sstevel@tonic-gate 		free(nvl_packed);
34750Sstevel@tonic-gate 	if (nvl != NULL)
34760Sstevel@tonic-gate 		nvlist_free(nvl);
34770Sstevel@tonic-gate 	if (nvlv != NULL)
34780Sstevel@tonic-gate 		free(nvlv);
34790Sstevel@tonic-gate 	if (handle != NULL)
34800Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
34810Sstevel@tonic-gate 	return (error);
34820Sstevel@tonic-gate }
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate static int
get_implicit_datasets(zlog_t * zlogp,char ** retstr)34857370S<Gerald Jelinek> get_implicit_datasets(zlog_t *zlogp, char **retstr)
34867370S<Gerald Jelinek> {
34877370S<Gerald Jelinek> 	char cmdbuf[2 * MAXPATHLEN];
34887370S<Gerald Jelinek> 
34897370S<Gerald Jelinek> 	if (query_hook[0] == '\0')
34907370S<Gerald Jelinek> 		return (0);
34917370S<Gerald Jelinek> 
34927370S<Gerald Jelinek> 	if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
34937370S<Gerald Jelinek> 	    > sizeof (cmdbuf))
34947370S<Gerald Jelinek> 		return (-1);
34957370S<Gerald Jelinek> 
34967370S<Gerald Jelinek> 	if (do_subproc(zlogp, cmdbuf, retstr) != 0)
34977370S<Gerald Jelinek> 		return (-1);
34987370S<Gerald Jelinek> 
34997370S<Gerald Jelinek> 	return (0);
35007370S<Gerald Jelinek> }
35017370S<Gerald Jelinek> 
35027370S<Gerald Jelinek> static int
get_datasets(zlog_t * zlogp,char ** bufp,size_t * bufsizep)3503789Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3504789Sahrens {
3505789Sahrens 	zone_dochandle_t handle;
3506789Sahrens 	struct zone_dstab dstab;
3507789Sahrens 	size_t total, offset, len;
3508789Sahrens 	int error = -1;
35095185Sgjelinek 	char *str = NULL;
35107370S<Gerald Jelinek> 	char *implicit_datasets = NULL;
35117370S<Gerald Jelinek> 	int implicit_len = 0;
3512789Sahrens 
3513789Sahrens 	*bufp = NULL;
3514789Sahrens 	*bufsizep = 0;
3515789Sahrens 
3516789Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3517789Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3518789Sahrens 		return (-1);
3519789Sahrens 	}
3520789Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3521789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3522789Sahrens 		zonecfg_fini_handle(handle);
3523789Sahrens 		return (-1);
3524789Sahrens 	}
3525789Sahrens 
35267370S<Gerald Jelinek> 	if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
35277370S<Gerald Jelinek> 		zerror(zlogp, B_FALSE, "getting implicit datasets failed");
35287370S<Gerald Jelinek> 		goto out;
35297370S<Gerald Jelinek> 	}
35307370S<Gerald Jelinek> 
3531789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3532789Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3533789Sahrens 		goto out;
3534789Sahrens 	}
3535789Sahrens 
3536789Sahrens 	total = 0;
3537789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3538789Sahrens 		total += strlen(dstab.zone_dataset_name) + 1;
3539789Sahrens 	(void) zonecfg_enddsent(handle);
3540789Sahrens 
35417370S<Gerald Jelinek> 	if (implicit_datasets != NULL)
35427370S<Gerald Jelinek> 		implicit_len = strlen(implicit_datasets);
35437370S<Gerald Jelinek> 	if (implicit_len > 0)
35447370S<Gerald Jelinek> 		total += implicit_len + 1;
35457370S<Gerald Jelinek> 
3546789Sahrens 	if (total == 0) {
3547789Sahrens 		error = 0;
3548789Sahrens 		goto out;
3549789Sahrens 	}
3550789Sahrens 
3551789Sahrens 	if ((str = malloc(total)) == NULL) {
3552789Sahrens 		zerror(zlogp, B_TRUE, "memory allocation failed");
3553789Sahrens 		goto out;
3554789Sahrens 	}
3555789Sahrens 
3556789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3557789Sahrens 		zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3558789Sahrens 		goto out;
3559789Sahrens 	}
3560789Sahrens 	offset = 0;
3561789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3562789Sahrens 		len = strlen(dstab.zone_dataset_name);
3563789Sahrens 		(void) strlcpy(str + offset, dstab.zone_dataset_name,
35645185Sgjelinek 		    total - offset);
3565789Sahrens 		offset += len;
35665185Sgjelinek 		if (offset < total - 1)
3567789Sahrens 			str[offset++] = ',';
3568789Sahrens 	}
3569789Sahrens 	(void) zonecfg_enddsent(handle);
3570789Sahrens 
35717370S<Gerald Jelinek> 	if (implicit_len > 0)
35727370S<Gerald Jelinek> 		(void) strlcpy(str + offset, implicit_datasets, total - offset);
35737370S<Gerald Jelinek> 
3574789Sahrens 	error = 0;
3575789Sahrens 	*bufp = str;
3576789Sahrens 	*bufsizep = total;
3577789Sahrens 
3578789Sahrens out:
3579789Sahrens 	if (error != 0 && str != NULL)
3580789Sahrens 		free(str);
3581789Sahrens 	if (handle != NULL)
3582789Sahrens 		zonecfg_fini_handle(handle);
35837370S<Gerald Jelinek> 	if (implicit_datasets != NULL)
35847370S<Gerald Jelinek> 		free(implicit_datasets);
3585789Sahrens 
3586789Sahrens 	return (error);
3587789Sahrens }
3588789Sahrens 
3589789Sahrens static int
validate_datasets(zlog_t * zlogp)3590789Sahrens validate_datasets(zlog_t *zlogp)
3591789Sahrens {
3592789Sahrens 	zone_dochandle_t handle;
3593789Sahrens 	struct zone_dstab dstab;
3594789Sahrens 	zfs_handle_t *zhp;
35952082Seschrock 	libzfs_handle_t *hdl;
3596789Sahrens 
3597789Sahrens 	if ((handle = zonecfg_init_handle()) == NULL) {
3598789Sahrens 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
3599789Sahrens 		return (-1);
3600789Sahrens 	}
3601789Sahrens 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3602789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3603789Sahrens 		zonecfg_fini_handle(handle);
3604789Sahrens 		return (-1);
3605789Sahrens 	}
3606789Sahrens 
3607789Sahrens 	if (zonecfg_setdsent(handle) != Z_OK) {
3608789Sahrens 		zerror(zlogp, B_FALSE, "invalid configuration");
3609789Sahrens 		zonecfg_fini_handle(handle);
3610789Sahrens 		return (-1);
3611789Sahrens 	}
3612789Sahrens 
36132082Seschrock 	if ((hdl = libzfs_init()) == NULL) {
36142082Seschrock 		zerror(zlogp, B_FALSE, "opening ZFS library");
36152082Seschrock 		zonecfg_fini_handle(handle);
36162082Seschrock 		return (-1);
36172082Seschrock 	}
3618789Sahrens 
3619789Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3620789Sahrens 
36212082Seschrock 		if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3622789Sahrens 		    ZFS_TYPE_FILESYSTEM)) == NULL) {
3623789Sahrens 			zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3624789Sahrens 			    dstab.zone_dataset_name);
3625789Sahrens 			zonecfg_fini_handle(handle);
36262082Seschrock 			libzfs_fini(hdl);
3627789Sahrens 			return (-1);
3628789Sahrens 		}
3629789Sahrens 
3630789Sahrens 		/*
3631789Sahrens 		 * Automatically set the 'zoned' property.  We check the value
3632789Sahrens 		 * first because we'll get EPERM if it is already set.
3633789Sahrens 		 */
3634789Sahrens 		if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
36352676Seschrock 		    zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
36362676Seschrock 		    "on") != 0) {
3637789Sahrens 			zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3638789Sahrens 			    "property for ZFS dataset '%s'\n",
3639789Sahrens 			    dstab.zone_dataset_name);
3640789Sahrens 			zonecfg_fini_handle(handle);
3641789Sahrens 			zfs_close(zhp);
36422082Seschrock 			libzfs_fini(hdl);
3643789Sahrens 			return (-1);
3644789Sahrens 		}
3645789Sahrens 
3646789Sahrens 		zfs_close(zhp);
3647789Sahrens 	}
3648789Sahrens 	(void) zonecfg_enddsent(handle);
3649789Sahrens 
3650789Sahrens 	zonecfg_fini_handle(handle);
36512082Seschrock 	libzfs_fini(hdl);
3652789Sahrens 
3653789Sahrens 	return (0);
3654789Sahrens }
3655789Sahrens 
36561676Sjpk /*
365710972SRic.Aleshire@Sun.COM  * Return true if the path is its own zfs file system.  We determine this
365810972SRic.Aleshire@Sun.COM  * by stat-ing the path to see if it is zfs and stat-ing the parent to see
365910972SRic.Aleshire@Sun.COM  * if it is a different fs.
366010972SRic.Aleshire@Sun.COM  */
366110972SRic.Aleshire@Sun.COM boolean_t
is_zonepath_zfs(char * zonepath)366210972SRic.Aleshire@Sun.COM is_zonepath_zfs(char *zonepath)
366310972SRic.Aleshire@Sun.COM {
366410972SRic.Aleshire@Sun.COM 	int res;
366510972SRic.Aleshire@Sun.COM 	char *path;
366610972SRic.Aleshire@Sun.COM 	char *parent;
366710972SRic.Aleshire@Sun.COM 	struct statvfs64 buf1, buf2;
366810972SRic.Aleshire@Sun.COM 
366910972SRic.Aleshire@Sun.COM 	if (statvfs64(zonepath, &buf1) != 0)
367010972SRic.Aleshire@Sun.COM 		return (B_FALSE);
367110972SRic.Aleshire@Sun.COM 
367210972SRic.Aleshire@Sun.COM 	if (strcmp(buf1.f_basetype, "zfs") != 0)
367310972SRic.Aleshire@Sun.COM 		return (B_FALSE);
367410972SRic.Aleshire@Sun.COM 
367510972SRic.Aleshire@Sun.COM 	if ((path = strdup(zonepath)) == NULL)
367610972SRic.Aleshire@Sun.COM 		return (B_FALSE);
367710972SRic.Aleshire@Sun.COM 
367810972SRic.Aleshire@Sun.COM 	parent = dirname(path);
367910972SRic.Aleshire@Sun.COM 	res = statvfs64(parent, &buf2);
368010972SRic.Aleshire@Sun.COM 	free(path);
368110972SRic.Aleshire@Sun.COM 
368210972SRic.Aleshire@Sun.COM 	if (res != 0)
368310972SRic.Aleshire@Sun.COM 		return (B_FALSE);
368410972SRic.Aleshire@Sun.COM 
368510972SRic.Aleshire@Sun.COM 	if (buf1.f_fsid == buf2.f_fsid)
368610972SRic.Aleshire@Sun.COM 		return (B_FALSE);
368710972SRic.Aleshire@Sun.COM 
368810972SRic.Aleshire@Sun.COM 	return (B_TRUE);
368910972SRic.Aleshire@Sun.COM }
369010972SRic.Aleshire@Sun.COM 
369110972SRic.Aleshire@Sun.COM /*
369210972SRic.Aleshire@Sun.COM  * Verify the MAC label in the root dataset for the zone.
369310972SRic.Aleshire@Sun.COM  * If the label exists, it must match the label configured for the zone.
369410972SRic.Aleshire@Sun.COM  * Otherwise if there's no label on the dataset, create one here.
369510972SRic.Aleshire@Sun.COM  */
369610972SRic.Aleshire@Sun.COM 
369710972SRic.Aleshire@Sun.COM static int
validate_rootds_label(zlog_t * zlogp,char * rootpath,m_label_t * zone_sl)369810972SRic.Aleshire@Sun.COM validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
369910972SRic.Aleshire@Sun.COM {
370010972SRic.Aleshire@Sun.COM 	int		error = -1;
370110972SRic.Aleshire@Sun.COM 	zfs_handle_t	*zhp;
370210972SRic.Aleshire@Sun.COM 	libzfs_handle_t	*hdl;
370310972SRic.Aleshire@Sun.COM 	m_label_t	ds_sl;
370410972SRic.Aleshire@Sun.COM 	char		zonepath[MAXPATHLEN];
370510972SRic.Aleshire@Sun.COM 	char		ds_hexsl[MAXNAMELEN];
370610972SRic.Aleshire@Sun.COM 
370710972SRic.Aleshire@Sun.COM 	if (!is_system_labeled())
370810972SRic.Aleshire@Sun.COM 		return (0);
370910972SRic.Aleshire@Sun.COM 
371010972SRic.Aleshire@Sun.COM 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
371110972SRic.Aleshire@Sun.COM 		zerror(zlogp, B_TRUE, "unable to determine zone path");
371210972SRic.Aleshire@Sun.COM 		return (-1);
371310972SRic.Aleshire@Sun.COM 	}
371410972SRic.Aleshire@Sun.COM 
371510972SRic.Aleshire@Sun.COM 	if (!is_zonepath_zfs(zonepath))
371610972SRic.Aleshire@Sun.COM 		return (0);
371710972SRic.Aleshire@Sun.COM 
371810972SRic.Aleshire@Sun.COM 	if ((hdl = libzfs_init()) == NULL) {
371910972SRic.Aleshire@Sun.COM 		zerror(zlogp, B_FALSE, "opening ZFS library");
372010972SRic.Aleshire@Sun.COM 		return (-1);
372110972SRic.Aleshire@Sun.COM 	}
372210972SRic.Aleshire@Sun.COM 
372310972SRic.Aleshire@Sun.COM 	if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
372410972SRic.Aleshire@Sun.COM 	    ZFS_TYPE_FILESYSTEM)) == NULL) {
372510972SRic.Aleshire@Sun.COM 		zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
372610972SRic.Aleshire@Sun.COM 		    rootpath);
372710972SRic.Aleshire@Sun.COM 		libzfs_fini(hdl);
372810972SRic.Aleshire@Sun.COM 		return (-1);
372910972SRic.Aleshire@Sun.COM 	}
373010972SRic.Aleshire@Sun.COM 
373110972SRic.Aleshire@Sun.COM 	/* Get the mlslabel property if it exists. */
373210972SRic.Aleshire@Sun.COM 	if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
373310972SRic.Aleshire@Sun.COM 	    NULL, NULL, 0, B_TRUE) != 0) ||
373410972SRic.Aleshire@Sun.COM 	    (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
373510972SRic.Aleshire@Sun.COM 		char		*str2 = NULL;
373610972SRic.Aleshire@Sun.COM 
373710972SRic.Aleshire@Sun.COM 		/*
373810972SRic.Aleshire@Sun.COM 		 * No label on the dataset (or default only); create one.
373910972SRic.Aleshire@Sun.COM 		 * (Only do this automatic labeling for the labeled brand.)
374010972SRic.Aleshire@Sun.COM 		 */
374110972SRic.Aleshire@Sun.COM 		if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
374210972SRic.Aleshire@Sun.COM 			error = 0;
374310972SRic.Aleshire@Sun.COM 			goto out;
374410972SRic.Aleshire@Sun.COM 		}
374510972SRic.Aleshire@Sun.COM 
374610972SRic.Aleshire@Sun.COM 		error = l_to_str_internal(zone_sl, &str2);
374710972SRic.Aleshire@Sun.COM 		if (error)
374810972SRic.Aleshire@Sun.COM 			goto out;
374910972SRic.Aleshire@Sun.COM 		if (str2 == NULL) {
375010972SRic.Aleshire@Sun.COM 			error = -1;
375110972SRic.Aleshire@Sun.COM 			goto out;
375210972SRic.Aleshire@Sun.COM 		}
375310972SRic.Aleshire@Sun.COM 		if ((error = zfs_prop_set(zhp,
375410972SRic.Aleshire@Sun.COM 		    zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
375510972SRic.Aleshire@Sun.COM 			zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
375610972SRic.Aleshire@Sun.COM 			    "property for root dataset at '%s'\n", rootpath);
375710972SRic.Aleshire@Sun.COM 		}
375810972SRic.Aleshire@Sun.COM 		free(str2);
375910972SRic.Aleshire@Sun.COM 		goto out;
376010972SRic.Aleshire@Sun.COM 	}
376110972SRic.Aleshire@Sun.COM 
376210972SRic.Aleshire@Sun.COM 	/* Convert the retrieved dataset label to binary form. */
376310972SRic.Aleshire@Sun.COM 	error = hexstr_to_label(ds_hexsl, &ds_sl);
376410972SRic.Aleshire@Sun.COM 	if (error) {
376510972SRic.Aleshire@Sun.COM 		zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
376610972SRic.Aleshire@Sun.COM 		    "property on root dataset at '%s'\n", rootpath);
376710972SRic.Aleshire@Sun.COM 		goto out;			/* exit with error */
376810972SRic.Aleshire@Sun.COM 	}
376910972SRic.Aleshire@Sun.COM 
377010972SRic.Aleshire@Sun.COM 	/*
377110972SRic.Aleshire@Sun.COM 	 * Perform a MAC check by comparing the zone label with the
377210972SRic.Aleshire@Sun.COM 	 * dataset label.
377310972SRic.Aleshire@Sun.COM 	 */
377410972SRic.Aleshire@Sun.COM 	error = (!blequal(zone_sl, &ds_sl));
377510972SRic.Aleshire@Sun.COM 	if (error)
377610972SRic.Aleshire@Sun.COM 		zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
377710972SRic.Aleshire@Sun.COM out:
377810972SRic.Aleshire@Sun.COM 	zfs_close(zhp);
377910972SRic.Aleshire@Sun.COM 	libzfs_fini(hdl);
378010972SRic.Aleshire@Sun.COM 
378110972SRic.Aleshire@Sun.COM 	return (error);
378210972SRic.Aleshire@Sun.COM }
378310972SRic.Aleshire@Sun.COM 
378410972SRic.Aleshire@Sun.COM /*
37851676Sjpk  * Mount lower level home directories into/from current zone
37861676Sjpk  * Share exported directories specified in dfstab for zone
37871676Sjpk  */
37881676Sjpk static int
tsol_mounts(zlog_t * zlogp,char * zone_name,char * rootpath)37891676Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
37901676Sjpk {
37911676Sjpk 	zoneid_t *zids = NULL;
37921676Sjpk 	priv_set_t *zid_privs;
37931676Sjpk 	const priv_impl_info_t *ip = NULL;
37941676Sjpk 	uint_t nzents_saved;
37951676Sjpk 	uint_t nzents;
37961676Sjpk 	int i;
37971676Sjpk 	char readonly[] = "ro";
37981676Sjpk 	struct zone_fstab lower_fstab;
37991676Sjpk 	char *argv[4];
38001676Sjpk 
38011676Sjpk 	if (!is_system_labeled())
38021676Sjpk 		return (0);
38031676Sjpk 
38041676Sjpk 	if (zid_label == NULL) {
38051676Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
38061676Sjpk 		if (zid_label == NULL)
38071676Sjpk 			return (-1);
38081676Sjpk 	}
38091676Sjpk 
38101676Sjpk 	/* Make sure our zone has an /export/home dir */
38111676Sjpk 	(void) make_one_dir(zlogp, rootpath, "/export/home",
38123813Sdp 	    DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
38131676Sjpk 
38141676Sjpk 	lower_fstab.zone_fs_raw[0] = '\0';
38151676Sjpk 	(void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
38161676Sjpk 	    sizeof (lower_fstab.zone_fs_type));
38171676Sjpk 	lower_fstab.zone_fs_options = NULL;
38181676Sjpk 	(void) zonecfg_add_fs_option(&lower_fstab, readonly);
38191676Sjpk 
38201676Sjpk 	/*
38211676Sjpk 	 * Get the list of zones from the kernel
38221676Sjpk 	 */
38231676Sjpk 	if (zone_list(NULL, &nzents) != 0) {
38241676Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
38251676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
38261676Sjpk 		return (-1);
38271676Sjpk 	}
38281676Sjpk again:
38291676Sjpk 	if (nzents == 0) {
38301676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
38311676Sjpk 		return (-1);
38321676Sjpk 	}
38331676Sjpk 
38341676Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
38351676Sjpk 	if (zids == NULL) {
38362267Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
38371676Sjpk 		return (-1);
38381676Sjpk 	}
38391676Sjpk 	nzents_saved = nzents;
38401676Sjpk 
38411676Sjpk 	if (zone_list(zids, &nzents) != 0) {
38421676Sjpk 		zerror(zlogp, B_TRUE, "unable to list zones");
38431676Sjpk 		zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
38441676Sjpk 		free(zids);
38451676Sjpk 		return (-1);
38461676Sjpk 	}
38471676Sjpk 	if (nzents != nzents_saved) {
38481676Sjpk 		/* list changed, try again */
38491676Sjpk 		free(zids);
38501676Sjpk 		goto again;
38511676Sjpk 	}
38521676Sjpk 
38531676Sjpk 	ip = getprivimplinfo();
38541676Sjpk 	if ((zid_privs = priv_allocset()) == NULL) {
38551676Sjpk 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
38561676Sjpk 		zonecfg_free_fs_option_list(
38571676Sjpk 		    lower_fstab.zone_fs_options);
38581676Sjpk 		free(zids);
38591676Sjpk 		return (-1);
38601676Sjpk 	}
38611676Sjpk 
38621676Sjpk 	for (i = 0; i < nzents; i++) {
38631676Sjpk 		char zid_name[ZONENAME_MAX];
38641676Sjpk 		zone_state_t zid_state;
38651676Sjpk 		char zid_rpath[MAXPATHLEN];
38661676Sjpk 		struct stat stat_buf;
38671676Sjpk 
38681676Sjpk 		if (zids[i] == GLOBAL_ZONEID)
38691676Sjpk 			continue;
38701676Sjpk 
38711676Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
38721676Sjpk 			continue;
38731676Sjpk 
38741676Sjpk 		/*
38751676Sjpk 		 * Do special setup for the zone we are booting
38761676Sjpk 		 */
38771676Sjpk 		if (strcmp(zid_name, zone_name) == 0) {
38781676Sjpk 			struct zone_fstab autofs_fstab;
38791676Sjpk 			char map_path[MAXPATHLEN];
38801676Sjpk 			int fd;
38811676Sjpk 
38821676Sjpk 			/*
38831676Sjpk 			 * Create auto_home_<zone> map for this zone
38842712Snn35248 			 * in the global zone. The non-global zone entry
38851676Sjpk 			 * will be created by automount when the zone
38861676Sjpk 			 * is booted.
38871676Sjpk 			 */
38881676Sjpk 
38891676Sjpk 			(void) snprintf(autofs_fstab.zone_fs_special,
38901676Sjpk 			    MAXPATHLEN, "auto_home_%s", zid_name);
38911676Sjpk 
38921676Sjpk 			(void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
38931676Sjpk 			    "/zone/%s/home", zid_name);
38941676Sjpk 
38951676Sjpk 			(void) snprintf(map_path, sizeof (map_path),
38961676Sjpk 			    "/etc/%s", autofs_fstab.zone_fs_special);
38971676Sjpk 			/*
38981676Sjpk 			 * If the map file doesn't exist create a template
38991676Sjpk 			 */
39001676Sjpk 			if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
39011676Sjpk 			    S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
39021676Sjpk 				int len;
39031676Sjpk 				char map_rec[MAXPATHLEN];
39041676Sjpk 
39051676Sjpk 				len = snprintf(map_rec, sizeof (map_rec),
39061676Sjpk 				    "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
39071676Sjpk 				    autofs_fstab.zone_fs_special, rootpath);
39081676Sjpk 				(void) write(fd, map_rec, len);
39091676Sjpk 				(void) close(fd);
39101676Sjpk 			}
39111676Sjpk 
39121676Sjpk 			/*
39131676Sjpk 			 * Mount auto_home_<zone> in the global zone if absent.
39141676Sjpk 			 * If it's already of type autofs, then
39151676Sjpk 			 * don't mount it again.
39161676Sjpk 			 */
39171676Sjpk 			if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
39181676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
39191676Sjpk 				char optstr[] = "indirect,ignore,nobrowse";
39201676Sjpk 
39211676Sjpk 				(void) make_one_dir(zlogp, "",
39223813Sdp 				    autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
39233813Sdp 				    DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
39241676Sjpk 
39251676Sjpk 				/*
39261676Sjpk 				 * Mount will fail if automounter has already
39271676Sjpk 				 * processed the auto_home_<zonename> map
39281676Sjpk 				 */
39291676Sjpk 				(void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
39301676Sjpk 				    autofs_fstab.zone_fs_special,
39311676Sjpk 				    autofs_fstab.zone_fs_dir);
39321676Sjpk 			}
39331676Sjpk 			continue;
39341676Sjpk 		}
39351676Sjpk 
39361676Sjpk 
39371676Sjpk 		if (zone_get_state(zid_name, &zid_state) != Z_OK ||
39381769Scarlsonj 		    (zid_state != ZONE_STATE_READY &&
39391769Scarlsonj 		    zid_state != ZONE_STATE_RUNNING))
39401676Sjpk 			/* Skip over zones without mounted filesystems */
39411676Sjpk 			continue;
39421676Sjpk 
39431676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
39441676Sjpk 		    sizeof (m_label_t)) < 0)
39451676Sjpk 			/* Skip over zones with unspecified label */
39461676Sjpk 			continue;
39471676Sjpk 
39481676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
39491676Sjpk 		    sizeof (zid_rpath)) == -1)
39501676Sjpk 			/* Skip over zones with bad path */
39511676Sjpk 			continue;
39521676Sjpk 
39531676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
39541676Sjpk 		    sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
39551676Sjpk 			/* Skip over zones with bad privs */
39561676Sjpk 			continue;
39571676Sjpk 
39581676Sjpk 		/*
39591676Sjpk 		 * Reading down is valid according to our label model
39601676Sjpk 		 * but some customers want to disable it because it
39611676Sjpk 		 * allows execute down and other possible attacks.
39621676Sjpk 		 * Therefore, we restrict this feature to zones that
39631676Sjpk 		 * have the NET_MAC_AWARE privilege which is required
39641676Sjpk 		 * for NFS read-down semantics.
39651676Sjpk 		 */
39661676Sjpk 		if ((bldominates(zlabel, zid_label)) &&
39671676Sjpk 		    (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
39681676Sjpk 			/*
39691676Sjpk 			 * Our zone dominates this one.
39701676Sjpk 			 * Create a lofs mount from lower zone's /export/home
39711676Sjpk 			 */
39721676Sjpk 			(void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
39731676Sjpk 			    "%s/zone/%s/export/home", rootpath, zid_name);
39741676Sjpk 
39751676Sjpk 			/*
39761676Sjpk 			 * If the target is already an LOFS mount
39771676Sjpk 			 * then don't do it again.
39781676Sjpk 			 */
39791676Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
39801676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
39811676Sjpk 
39821676Sjpk 				if (snprintf(lower_fstab.zone_fs_special,
39831676Sjpk 				    MAXPATHLEN, "%s/export",
39841676Sjpk 				    zid_rpath) > MAXPATHLEN)
39851676Sjpk 					continue;
39861676Sjpk 
39871676Sjpk 				/*
39881676Sjpk 				 * Make sure the lower-level home exists
39891676Sjpk 				 */
39901676Sjpk 				if (make_one_dir(zlogp,
39913813Sdp 				    lower_fstab.zone_fs_special, "/home",
39923813Sdp 				    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
39933813Sdp 				    DEFAULT_DIR_GROUP) != 0)
39941676Sjpk 					continue;
39951676Sjpk 
39961676Sjpk 				(void) strlcat(lower_fstab.zone_fs_special,
39971676Sjpk 				    "/home", MAXPATHLEN);
39981676Sjpk 
39991676Sjpk 				/*
40001676Sjpk 				 * Mount can fail because the lower-level
40011676Sjpk 				 * zone may have already done a mount up.
40021676Sjpk 				 */
40037655Sgerald.jelinek@sun.com 				(void) mount_one(zlogp, &lower_fstab, "",
40047655Sgerald.jelinek@sun.com 				    Z_MNT_BOOT);
40051676Sjpk 			}
40061676Sjpk 		} else if ((bldominates(zid_label, zlabel)) &&
40071676Sjpk 		    (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
40081676Sjpk 			/*
40091676Sjpk 			 * This zone dominates our zone.
40101676Sjpk 			 * Create a lofs mount from our zone's /export/home
40111676Sjpk 			 */
40121676Sjpk 			if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
40131676Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
40141676Sjpk 			    zone_name) > MAXPATHLEN)
40151676Sjpk 				continue;
40161676Sjpk 
40171676Sjpk 			/*
40181676Sjpk 			 * If the target is already an LOFS mount
40191676Sjpk 			 * then don't do it again.
40201676Sjpk 			 */
40211676Sjpk 			if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
40221676Sjpk 			    strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
40231676Sjpk 
40241676Sjpk 				(void) snprintf(lower_fstab.zone_fs_special,
40251676Sjpk 				    MAXPATHLEN, "%s/export/home", rootpath);
40261676Sjpk 
40271676Sjpk 				/*
40281676Sjpk 				 * Mount can fail because the higher-level
40291676Sjpk 				 * zone may have already done a mount down.
40301676Sjpk 				 */
40317655Sgerald.jelinek@sun.com 				(void) mount_one(zlogp, &lower_fstab, "",
40327655Sgerald.jelinek@sun.com 				    Z_MNT_BOOT);
40331676Sjpk 			}
40341676Sjpk 		}
40351676Sjpk 	}
40361676Sjpk 	zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
40371676Sjpk 	priv_freeset(zid_privs);
40381676Sjpk 	free(zids);
40391676Sjpk 
40401676Sjpk 	/*
40411676Sjpk 	 * Now share any exported directories from this zone.
40421676Sjpk 	 * Each zone can have its own dfstab.
40431676Sjpk 	 */
40441676Sjpk 
40451676Sjpk 	argv[0] = "zoneshare";
40461676Sjpk 	argv[1] = "-z";
40471676Sjpk 	argv[2] = zone_name;
40481676Sjpk 	argv[3] = NULL;
40491676Sjpk 
40501676Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
40511676Sjpk 	/* Don't check for errors since they don't affect the zone */
40521676Sjpk 
40531676Sjpk 	return (0);
40541676Sjpk }
40551676Sjpk 
40561676Sjpk /*
40571676Sjpk  * Unmount lofs mounts from higher level zones
40581676Sjpk  * Unshare nfs exported directories
40591676Sjpk  */
40601676Sjpk static void
tsol_unmounts(zlog_t * zlogp,char * zone_name)40611676Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
40621676Sjpk {
40631676Sjpk 	zoneid_t *zids = NULL;
40641676Sjpk 	uint_t nzents_saved;
40651676Sjpk 	uint_t nzents;
40661676Sjpk 	int i;
40671676Sjpk 	char *argv[4];
40681676Sjpk 	char path[MAXPATHLEN];
40691676Sjpk 
40701676Sjpk 	if (!is_system_labeled())
40711676Sjpk 		return;
40721676Sjpk 
40731676Sjpk 	/*
40741676Sjpk 	 * Get the list of zones from the kernel
40751676Sjpk 	 */
40761676Sjpk 	if (zone_list(NULL, &nzents) != 0) {
40771676Sjpk 		return;
40781676Sjpk 	}
40791676Sjpk 
40801676Sjpk 	if (zid_label == NULL) {
40811676Sjpk 		zid_label = m_label_alloc(MAC_LABEL);
40821676Sjpk 		if (zid_label == NULL)
40831676Sjpk 			return;
40841676Sjpk 	}
40851676Sjpk 
40861676Sjpk again:
40871676Sjpk 	if (nzents == 0)
40881676Sjpk 		return;
40891676Sjpk 
40901676Sjpk 	zids = malloc(nzents * sizeof (zoneid_t));
40911676Sjpk 	if (zids == NULL) {
40922267Sdp 		zerror(zlogp, B_TRUE, "memory allocation failed");
40931676Sjpk 		return;
40941676Sjpk 	}
40951676Sjpk 	nzents_saved = nzents;
40961676Sjpk 
40971676Sjpk 	if (zone_list(zids, &nzents) != 0) {
40981676Sjpk 		free(zids);
40991676Sjpk 		return;
41001676Sjpk 	}
41011676Sjpk 	if (nzents != nzents_saved) {
41021676Sjpk 		/* list changed, try again */
41031676Sjpk 		free(zids);
41041676Sjpk 		goto again;
41051676Sjpk 	}
41061676Sjpk 
41071676Sjpk 	for (i = 0; i < nzents; i++) {
41081676Sjpk 		char zid_name[ZONENAME_MAX];
41091676Sjpk 		zone_state_t zid_state;
41101676Sjpk 		char zid_rpath[MAXPATHLEN];
41111676Sjpk 
41121676Sjpk 		if (zids[i] == GLOBAL_ZONEID)
41131676Sjpk 			continue;
41141676Sjpk 
41151676Sjpk 		if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
41161676Sjpk 			continue;
41171676Sjpk 
41181676Sjpk 		/*
41191676Sjpk 		 * Skip the zone we are halting
41201676Sjpk 		 */
41211676Sjpk 		if (strcmp(zid_name, zone_name) == 0)
41221676Sjpk 			continue;
41231676Sjpk 
41241676Sjpk 		if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
41251676Sjpk 		    sizeof (zid_state)) < 0) ||
41261676Sjpk 		    (zid_state < ZONE_IS_READY))
41271676Sjpk 			/* Skip over zones without mounted filesystems */
41281676Sjpk 			continue;
41291676Sjpk 
41301676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
41311676Sjpk 		    sizeof (m_label_t)) < 0)
41321676Sjpk 			/* Skip over zones with unspecified label */
41331676Sjpk 			continue;
41341676Sjpk 
41351676Sjpk 		if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
41361676Sjpk 		    sizeof (zid_rpath)) == -1)
41371676Sjpk 			/* Skip over zones with bad path */
41381676Sjpk 			continue;
41391676Sjpk 
41401676Sjpk 		if (zlabel != NULL && bldominates(zid_label, zlabel)) {
41411676Sjpk 			/*
41421676Sjpk 			 * This zone dominates our zone.
41431676Sjpk 			 * Unmount the lofs mount of our zone's /export/home
41441676Sjpk 			 */
41451676Sjpk 
41461676Sjpk 			if (snprintf(path, MAXPATHLEN,
41471676Sjpk 			    "%s/zone/%s/export/home", zid_rpath,
41481676Sjpk 			    zone_name) > MAXPATHLEN)
41491676Sjpk 				continue;
41501676Sjpk 
41511676Sjpk 			/* Skip over mount failures */
41521676Sjpk 			(void) umount(path);
41531676Sjpk 		}
41541676Sjpk 	}
41551676Sjpk 	free(zids);
41561676Sjpk 
41571676Sjpk 	/*
41581676Sjpk 	 * Unmount global zone autofs trigger for this zone
41591676Sjpk 	 */
41601676Sjpk 	(void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
41611676Sjpk 	/* Skip over mount failures */
41621676Sjpk 	(void) umount(path);
41631676Sjpk 
41641676Sjpk 	/*
41651676Sjpk 	 * Next unshare any exported directories from this zone.
41661676Sjpk 	 */
41671676Sjpk 
41681676Sjpk 	argv[0] = "zoneunshare";
41691676Sjpk 	argv[1] = "-z";
41701676Sjpk 	argv[2] = zone_name;
41711676Sjpk 	argv[3] = NULL;
41721676Sjpk 
41731676Sjpk 	(void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
41741676Sjpk 	/* Don't check for errors since they don't affect the zone */
41751676Sjpk 
41761676Sjpk 	/*
41771676Sjpk 	 * Finally, deallocate any devices in the zone.
41781676Sjpk 	 */
41791676Sjpk 
41801676Sjpk 	argv[0] = "deallocate";
41811676Sjpk 	argv[1] = "-Isz";
41821676Sjpk 	argv[2] = zone_name;
41831676Sjpk 	argv[3] = NULL;
41841676Sjpk 
41851676Sjpk 	(void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
41861676Sjpk 	/* Don't check for errors since they don't affect the zone */
41871676Sjpk }
41881676Sjpk 
41891676Sjpk /*
41901676Sjpk  * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
41911676Sjpk  * this zone.
41921676Sjpk  */
41931676Sjpk static tsol_zcent_t *
get_zone_label(zlog_t * zlogp,priv_set_t * privs)41941676Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
41951676Sjpk {
41961676Sjpk 	FILE *fp;
41971676Sjpk 	tsol_zcent_t *zcent = NULL;
41981676Sjpk 	char line[MAXTNZLEN];
41991676Sjpk 
42001676Sjpk 	if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
42011676Sjpk 		zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
42021676Sjpk 		return (NULL);
42031676Sjpk 	}
42041676Sjpk 
42051676Sjpk 	while (fgets(line, sizeof (line), fp) != NULL) {
42061676Sjpk 		/*
42071676Sjpk 		 * Check for malformed database
42081676Sjpk 		 */
42091676Sjpk 		if (strlen(line) == MAXTNZLEN - 1)
42101676Sjpk 			break;
42111676Sjpk 		if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
42121676Sjpk 			continue;
42131676Sjpk 		if (strcmp(zcent->zc_name, zone_name) == 0)
42141676Sjpk 			break;
42151676Sjpk 		tsol_freezcent(zcent);
42161676Sjpk 		zcent = NULL;
42171676Sjpk 	}
42181676Sjpk 	(void) fclose(fp);
42191676Sjpk 
42201676Sjpk 	if (zcent == NULL) {
42211676Sjpk 		zerror(zlogp, B_FALSE, "zone requires a label assignment. "
42221676Sjpk 		    "See tnzonecfg(4)");
42231676Sjpk 	} else {
42241676Sjpk 		if (zlabel == NULL)
42251676Sjpk 			zlabel = m_label_alloc(MAC_LABEL);
42261676Sjpk 		/*
42271676Sjpk 		 * Save this zone's privileges for later read-down processing
42281676Sjpk 		 */
42291676Sjpk 		if ((zprivs = priv_allocset()) == NULL) {
42301676Sjpk 			zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
42311676Sjpk 			return (NULL);
42321676Sjpk 		} else {
42331676Sjpk 			priv_copyset(privs, zprivs);
42341676Sjpk 		}
42351676Sjpk 	}
42361676Sjpk 	return (zcent);
42371676Sjpk }
42381676Sjpk 
42391676Sjpk /*
42401676Sjpk  * Add the Trusted Extensions multi-level ports for this zone.
42411676Sjpk  */
42421676Sjpk static void
set_mlps(zlog_t * zlogp,zoneid_t zoneid,tsol_zcent_t * zcent)42431676Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
42441676Sjpk {
42451676Sjpk 	tsol_mlp_t *mlp;
42461676Sjpk 	tsol_mlpent_t tsme;
42471676Sjpk 
42481676Sjpk 	if (!is_system_labeled())
42491676Sjpk 		return;
42501676Sjpk 
42511676Sjpk 	tsme.tsme_zoneid = zoneid;
42521676Sjpk 	tsme.tsme_flags = 0;
42531676Sjpk 	for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
42541676Sjpk 		tsme.tsme_mlp = *mlp;
42551676Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
42561676Sjpk 			zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
42571676Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
42581676Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
42591676Sjpk 		}
42601676Sjpk 	}
42611676Sjpk 
42621676Sjpk 	tsme.tsme_flags = TSOL_MEF_SHARED;
42631676Sjpk 	for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
42641676Sjpk 		tsme.tsme_mlp = *mlp;
42651676Sjpk 		if (tnmlp(TNDB_LOAD, &tsme) != 0) {
42661676Sjpk 			zerror(zlogp, B_TRUE, "cannot set shared MLP "
42671676Sjpk 			    "on %d-%d/%d", mlp->mlp_port,
42681676Sjpk 			    mlp->mlp_port_upper, mlp->mlp_ipp);
42691676Sjpk 		}
42701676Sjpk 	}
42711676Sjpk }
42721676Sjpk 
42731676Sjpk static void
remove_mlps(zlog_t * zlogp,zoneid_t zoneid)42741676Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
42751676Sjpk {
42761676Sjpk 	tsol_mlpent_t tsme;
42771676Sjpk 
42781676Sjpk 	if (!is_system_labeled())
42791676Sjpk 		return;
42801676Sjpk 
42811676Sjpk 	(void) memset(&tsme, 0, sizeof (tsme));
42821676Sjpk 	tsme.tsme_zoneid = zoneid;
42831676Sjpk 	if (tnmlp(TNDB_FLUSH, &tsme) != 0)
42841676Sjpk 		zerror(zlogp, B_TRUE, "cannot flush MLPs");
42851676Sjpk }
42861676Sjpk 
42870Sstevel@tonic-gate int
prtmount(const struct mnttab * fs,void * x)428811276SJordan.Vaughan@Sun.com prtmount(const struct mnttab *fs, void *x) {
428911276SJordan.Vaughan@Sun.com 	zerror((zlog_t *)x, B_FALSE, "  %s", fs->mnt_mountp);
42900Sstevel@tonic-gate 	return (0);
42910Sstevel@tonic-gate }
42920Sstevel@tonic-gate 
4293766Scarlsonj /*
4294766Scarlsonj  * Look for zones running on the main system that are using this root (or any
4295766Scarlsonj  * subdirectory of it).  Return B_TRUE and print an error if a conflicting zone
4296766Scarlsonj  * is found or if we can't tell.
4297766Scarlsonj  */
4298766Scarlsonj static boolean_t
duplicate_zone_root(zlog_t * zlogp,const char * rootpath)4299766Scarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
43000Sstevel@tonic-gate {
4301766Scarlsonj 	zoneid_t *zids = NULL;
4302766Scarlsonj 	uint_t nzids = 0;
4303766Scarlsonj 	boolean_t retv;
4304766Scarlsonj 	int rlen, zlen;
4305766Scarlsonj 	char zroot[MAXPATHLEN];
4306766Scarlsonj 	char zonename[ZONENAME_MAX];
4307766Scarlsonj 
4308766Scarlsonj 	for (;;) {
4309766Scarlsonj 		nzids += 10;
4310766Scarlsonj 		zids = malloc(nzids * sizeof (*zids));
4311766Scarlsonj 		if (zids == NULL) {
43122267Sdp 			zerror(zlogp, B_TRUE, "memory allocation failed");
4313766Scarlsonj 			return (B_TRUE);
4314766Scarlsonj 		}
4315766Scarlsonj 		if (zone_list(zids, &nzids) == 0)
4316766Scarlsonj 			break;
4317766Scarlsonj 		free(zids);
4318766Scarlsonj 	}
4319766Scarlsonj 	retv = B_FALSE;
4320766Scarlsonj 	rlen = strlen(rootpath);
4321766Scarlsonj 	while (nzids > 0) {
4322766Scarlsonj 		/*
4323766Scarlsonj 		 * Ignore errors; they just mean that the zone has disappeared
4324766Scarlsonj 		 * while we were busy.
4325766Scarlsonj 		 */
4326766Scarlsonj 		if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4327766Scarlsonj 		    sizeof (zroot)) == -1)
4328766Scarlsonj 			continue;
4329766Scarlsonj 		zlen = strlen(zroot);
4330766Scarlsonj 		if (zlen > rlen)
4331766Scarlsonj 			zlen = rlen;
4332766Scarlsonj 		if (strncmp(rootpath, zroot, zlen) == 0 &&
4333766Scarlsonj 		    (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4334766Scarlsonj 		    (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4335766Scarlsonj 			if (getzonenamebyid(zids[nzids], zonename,
4336766Scarlsonj 			    sizeof (zonename)) == -1)
4337766Scarlsonj 				(void) snprintf(zonename, sizeof (zonename),
4338766Scarlsonj 				    "id %d", (int)zids[nzids]);
4339766Scarlsonj 			zerror(zlogp, B_FALSE,
4340766Scarlsonj 			    "zone root %s already in use by zone %s",
4341766Scarlsonj 			    rootpath, zonename);
4342766Scarlsonj 			retv = B_TRUE;
4343766Scarlsonj 			break;
4344766Scarlsonj 		}
4345766Scarlsonj 	}
4346766Scarlsonj 	free(zids);
4347766Scarlsonj 	return (retv);
4348766Scarlsonj }
4349766Scarlsonj 
4350766Scarlsonj /*
4351766Scarlsonj  * Search for loopback mounts that use this same source node (same device and
4352766Scarlsonj  * inode).  Return B_TRUE if there is one or if we can't tell.
4353766Scarlsonj  */
4354766Scarlsonj static boolean_t
duplicate_reachable_path(zlog_t * zlogp,const char * rootpath)4355766Scarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4356766Scarlsonj {
4357766Scarlsonj 	struct stat64 rst, zst;
4358766Scarlsonj 	struct mnttab *mnp;
4359766Scarlsonj 
4360766Scarlsonj 	if (stat64(rootpath, &rst) == -1) {
4361766Scarlsonj 		zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4362766Scarlsonj 		return (B_TRUE);
4363766Scarlsonj 	}
4364766Scarlsonj 	if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4365766Scarlsonj 		return (B_TRUE);
4366766Scarlsonj 	for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4367766Scarlsonj 		if (mnp->mnt_fstype == NULL ||
4368766Scarlsonj 		    strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4369766Scarlsonj 			continue;
4370766Scarlsonj 		/* We're looking at a loopback mount.  Stat it. */
4371766Scarlsonj 		if (mnp->mnt_special != NULL &&
4372766Scarlsonj 		    stat64(mnp->mnt_special, &zst) != -1 &&
4373766Scarlsonj 		    rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4374766Scarlsonj 			zerror(zlogp, B_FALSE,
4375766Scarlsonj 			    "zone root %s is reachable through %s",
4376766Scarlsonj 			    rootpath, mnp->mnt_mountp);
4377766Scarlsonj 			return (B_TRUE);
4378766Scarlsonj 		}
4379766Scarlsonj 	}
4380766Scarlsonj 	return (B_FALSE);
4381766Scarlsonj }
4382766Scarlsonj 
43833247Sgjelinek /*
43843247Sgjelinek  * Set memory cap and pool info for the zone's resource management
43853247Sgjelinek  * configuration.
43863247Sgjelinek  */
43873247Sgjelinek static int
setup_zone_rm(zlog_t * zlogp,char * zone_name,zoneid_t zoneid)43883247Sgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
43893247Sgjelinek {
43903247Sgjelinek 	int res;
43913247Sgjelinek 	uint64_t tmp;
43923247Sgjelinek 	struct zone_mcaptab mcap;
43933247Sgjelinek 	char sched[MAXNAMELEN];
43943247Sgjelinek 	zone_dochandle_t handle = NULL;
43953247Sgjelinek 	char pool_err[128];
43963247Sgjelinek 
43973247Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
43983247Sgjelinek 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
43993247Sgjelinek 		return (Z_BAD_HANDLE);
44003247Sgjelinek 	}
44013247Sgjelinek 
44023247Sgjelinek 	if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
44033247Sgjelinek 		zerror(zlogp, B_FALSE, "invalid configuration");
44043247Sgjelinek 		zonecfg_fini_handle(handle);
44053247Sgjelinek 		return (res);
44063247Sgjelinek 	}
44073247Sgjelinek 
44083247Sgjelinek 	/*
44093247Sgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
44103247Sgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
44113247Sgjelinek 	 */
44123247Sgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
44133247Sgjelinek 		uint64_t num;
44143247Sgjelinek 		char smf_err[128];
44153247Sgjelinek 
44163247Sgjelinek 		num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
44173247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
44183247Sgjelinek 			zerror(zlogp, B_TRUE, "could not set zone memory cap");
44193247Sgjelinek 			zonecfg_fini_handle(handle);
44203247Sgjelinek 			return (Z_INVAL);
44213247Sgjelinek 		}
44223247Sgjelinek 
44233247Sgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
44243247Sgjelinek 			zerror(zlogp, B_FALSE, "enabling system/rcap service "
44253247Sgjelinek 			    "failed: %s", smf_err);
44263247Sgjelinek 			zonecfg_fini_handle(handle);
44273247Sgjelinek 			return (Z_INVAL);
44283247Sgjelinek 		}
44293247Sgjelinek 	}
44303247Sgjelinek 
44313247Sgjelinek 	/* Get the scheduling class set in the zone configuration. */
44323247Sgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
44333247Sgjelinek 	    strlen(sched) > 0) {
44343247Sgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
44353247Sgjelinek 		    strlen(sched)) == -1)
44363247Sgjelinek 			zerror(zlogp, B_TRUE, "WARNING: unable to set the "
44373247Sgjelinek 			    "default scheduling class");
44383247Sgjelinek 
44393247Sgjelinek 	} else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
44403247Sgjelinek 	    == Z_OK) {
44413247Sgjelinek 		/*
44423247Sgjelinek 		 * If the zone has the zone.cpu-shares rctl set then we want to
44433247Sgjelinek 		 * use the Fair Share Scheduler (FSS) for processes in the
44443247Sgjelinek 		 * zone.  Check what scheduling class the zone would be running
44453247Sgjelinek 		 * in by default so we can print a warning and modify the class
44463247Sgjelinek 		 * if we wouldn't be using FSS.
44473247Sgjelinek 		 */
44483247Sgjelinek 		char class_name[PC_CLNMSZ];
44493247Sgjelinek 
44503247Sgjelinek 		if (zonecfg_get_dflt_sched_class(handle, class_name,
44513247Sgjelinek 		    sizeof (class_name)) != Z_OK) {
44523247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to determine "
44533247Sgjelinek 			    "the zone's scheduling class");
44543247Sgjelinek 
44553247Sgjelinek 		} else if (strcmp("FSS", class_name) != 0) {
44563247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
44573247Sgjelinek 			    "rctl is set but\nFSS is not the default "
44583247Sgjelinek 			    "scheduling class for\nthis zone.  FSS will be "
44593247Sgjelinek 			    "used for processes\nin the zone but to get the "
44603247Sgjelinek 			    "full benefit of FSS,\nit should be the default "
44613247Sgjelinek 			    "scheduling class.\nSee dispadmin(1M) for more "
44623247Sgjelinek 			    "details.");
44633247Sgjelinek 
44643247Sgjelinek 			if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
44653247Sgjelinek 			    strlen("FSS")) == -1)
44663247Sgjelinek 				zerror(zlogp, B_TRUE, "WARNING: unable to set "
44673247Sgjelinek 				    "zone scheduling class to FSS");
44683247Sgjelinek 		}
44693247Sgjelinek 	}
44703247Sgjelinek 
44713247Sgjelinek 	/*
44723247Sgjelinek 	 * The next few blocks of code attempt to set up temporary pools as
44733247Sgjelinek 	 * well as persistent pools.  In all cases we call the functions
44743247Sgjelinek 	 * unconditionally.  Within each funtion the code will check if the
44753247Sgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
44763247Sgjelinek 	 * and just return if there is nothing to do.
44773247Sgjelinek 	 *
44783247Sgjelinek 	 * If we are rebooting we want to attempt to reuse any temporary pool
44793247Sgjelinek 	 * that was previously set up.  zonecfg_bind_tmp_pool() will do the
44803247Sgjelinek 	 * right thing in all cases (reuse or create) based on the current
44813247Sgjelinek 	 * zonecfg.
44823247Sgjelinek 	 */
44833247Sgjelinek 	if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
44843247Sgjelinek 	    sizeof (pool_err))) != Z_OK) {
44853247Sgjelinek 		if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
44863247Sgjelinek 			zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
44873247Sgjelinek 			    "cannot be instantiated", zonecfg_strerror(res),
44883247Sgjelinek 			    pool_err);
44893247Sgjelinek 		else
44903247Sgjelinek 			zerror(zlogp, B_FALSE, "could not bind zone to "
44913247Sgjelinek 			    "temporary pool: %s", zonecfg_strerror(res));
44923247Sgjelinek 		zonecfg_fini_handle(handle);
44933247Sgjelinek 		return (Z_POOL_BIND);
44943247Sgjelinek 	}
44953247Sgjelinek 
44963247Sgjelinek 	/*
44973247Sgjelinek 	 * Check if we need to warn about poold not being enabled.
44983247Sgjelinek 	 */
44993247Sgjelinek 	if (zonecfg_warn_poold(handle)) {
45003247Sgjelinek 		zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
45013247Sgjelinek 		    "been specified\nbut the dynamic pool service is not "
45023247Sgjelinek 		    "enabled.\nThe system will not dynamically adjust the\n"
45033247Sgjelinek 		    "processor allocation within the specified range\n"
45043247Sgjelinek 		    "until svc:/system/pools/dynamic is enabled.\n"
45053247Sgjelinek 		    "See poold(1M).");
45063247Sgjelinek 	}
45073247Sgjelinek 
45083247Sgjelinek 	/* The following is a warning, not an error. */
45093247Sgjelinek 	if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
45103247Sgjelinek 	    sizeof (pool_err))) != Z_OK) {
45113247Sgjelinek 		if (res == Z_POOL_BIND)
45123247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
45133247Sgjelinek 			    "pool '%s'; using default pool.", pool_err);
45143247Sgjelinek 		else if (res == Z_POOL)
45153247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s: %s",
45163247Sgjelinek 			    zonecfg_strerror(res), pool_err);
45173247Sgjelinek 		else
45183247Sgjelinek 			zerror(zlogp, B_FALSE, "WARNING: %s",
45193247Sgjelinek 			    zonecfg_strerror(res));
45203247Sgjelinek 	}
4521*13122SStephen.Lawrence@oracle.COM 
4522*13122SStephen.Lawrence@oracle.COM 	/* Update saved pool name in case it has changed */
452311878SVenu.Iyer@Sun.COM 	(void) zonecfg_get_poolname(handle, zone_name, pool_name, MAXPATHLEN);
45243247Sgjelinek 
45253247Sgjelinek 	zonecfg_fini_handle(handle);
45263247Sgjelinek 	return (Z_OK);
45273247Sgjelinek }
45283247Sgjelinek 
452912633Sjohn.levon@sun.com static void
report_prop_err(zlog_t * zlogp,const char * name,const char * value,int res)453012633Sjohn.levon@sun.com report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
453112633Sjohn.levon@sun.com {
453212633Sjohn.levon@sun.com 	switch (res) {
453312633Sjohn.levon@sun.com 	case Z_TOO_BIG:
453412633Sjohn.levon@sun.com 		zerror(zlogp, B_FALSE, "%s property value is too large.", name);
453512633Sjohn.levon@sun.com 		break;
453612633Sjohn.levon@sun.com 
453712633Sjohn.levon@sun.com 	case Z_INVALID_PROPERTY:
453812633Sjohn.levon@sun.com 		zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
453912633Sjohn.levon@sun.com 		    name, value);
454012633Sjohn.levon@sun.com 		break;
454112633Sjohn.levon@sun.com 
454212633Sjohn.levon@sun.com 	default:
454312633Sjohn.levon@sun.com 		zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
454412633Sjohn.levon@sun.com 		break;
454512633Sjohn.levon@sun.com 	}
454612633Sjohn.levon@sun.com }
454712633Sjohn.levon@sun.com 
45488662SJordan.Vaughan@Sun.com /*
45498662SJordan.Vaughan@Sun.com  * Sets the hostid of the new zone based on its configured value.  The zone's
45508662SJordan.Vaughan@Sun.com  * zone_t structure must already exist in kernel memory.  'zlogp' refers to the
45518662SJordan.Vaughan@Sun.com  * log used to report errors and warnings and must be non-NULL.  'zone_namep'
45528662SJordan.Vaughan@Sun.com  * is the name of the new zone and must be non-NULL.  'zoneid' is the numeric
45538662SJordan.Vaughan@Sun.com  * ID of the new zone.
45548662SJordan.Vaughan@Sun.com  *
45558662SJordan.Vaughan@Sun.com  * This function returns zero on success and a nonzero error code on failure.
45568662SJordan.Vaughan@Sun.com  */
45578662SJordan.Vaughan@Sun.com static int
setup_zone_hostid(zone_dochandle_t handle,zlog_t * zlogp,zoneid_t zoneid)455812633Sjohn.levon@sun.com setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
45598662SJordan.Vaughan@Sun.com {
45608662SJordan.Vaughan@Sun.com 	int res;
45618662SJordan.Vaughan@Sun.com 	char hostidp[HW_HOSTID_LEN];
45628662SJordan.Vaughan@Sun.com 	unsigned int hostid;
45638662SJordan.Vaughan@Sun.com 
456412633Sjohn.levon@sun.com 	res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
456512633Sjohn.levon@sun.com 
456612633Sjohn.levon@sun.com 	if (res == Z_BAD_PROPERTY) {
456712633Sjohn.levon@sun.com 		return (Z_OK);
456812633Sjohn.levon@sun.com 	} else if (res != Z_OK) {
456912633Sjohn.levon@sun.com 		report_prop_err(zlogp, "hostid", hostidp, res);
457012633Sjohn.levon@sun.com 		return (res);
457112633Sjohn.levon@sun.com 	}
457212633Sjohn.levon@sun.com 
457312633Sjohn.levon@sun.com 	hostid = (unsigned int)strtoul(hostidp, NULL, 16);
457412633Sjohn.levon@sun.com 	if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
457512633Sjohn.levon@sun.com 	    sizeof (hostid))) != 0) {
457612633Sjohn.levon@sun.com 		zerror(zlogp, B_TRUE,
457712633Sjohn.levon@sun.com 		    "zone hostid is not valid: %s: %d", hostidp, res);
457812633Sjohn.levon@sun.com 		return (Z_SYSTEM);
457912633Sjohn.levon@sun.com 	}
458012633Sjohn.levon@sun.com 
458112633Sjohn.levon@sun.com 	return (res);
458212633Sjohn.levon@sun.com }
458312633Sjohn.levon@sun.com 
458412633Sjohn.levon@sun.com static int
setup_zone_fs_allowed(zone_dochandle_t handle,zlog_t * zlogp,zoneid_t zoneid)458512633Sjohn.levon@sun.com setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
458612633Sjohn.levon@sun.com {
458712633Sjohn.levon@sun.com 	char fsallowedp[ZONE_FS_ALLOWED_MAX];
458812633Sjohn.levon@sun.com 	int res;
458912633Sjohn.levon@sun.com 
459012633Sjohn.levon@sun.com 	res = zonecfg_get_fs_allowed(handle, fsallowedp, sizeof (fsallowedp));
459112633Sjohn.levon@sun.com 
459212633Sjohn.levon@sun.com 	if (res == Z_BAD_PROPERTY) {
459312633Sjohn.levon@sun.com 		return (Z_OK);
459412633Sjohn.levon@sun.com 	} else if (res != Z_OK) {
459512633Sjohn.levon@sun.com 		report_prop_err(zlogp, "fs-allowed", fsallowedp, res);
459612633Sjohn.levon@sun.com 		return (res);
459712633Sjohn.levon@sun.com 	}
459812633Sjohn.levon@sun.com 
459912633Sjohn.levon@sun.com 	if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, &fsallowedp,
460012633Sjohn.levon@sun.com 	    sizeof (fsallowedp)) != 0) {
460112633Sjohn.levon@sun.com 		zerror(zlogp, B_TRUE,
460212633Sjohn.levon@sun.com 		    "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
460312633Sjohn.levon@sun.com 		return (Z_SYSTEM);
460412633Sjohn.levon@sun.com 	}
460512633Sjohn.levon@sun.com 
460612633Sjohn.levon@sun.com 	return (res);
460712633Sjohn.levon@sun.com }
460812633Sjohn.levon@sun.com 
460912633Sjohn.levon@sun.com static int
setup_zone_attrs(zlog_t * zlogp,char * zone_namep,zoneid_t zoneid)461012633Sjohn.levon@sun.com setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
461112633Sjohn.levon@sun.com {
461212633Sjohn.levon@sun.com 	zone_dochandle_t handle;
461312633Sjohn.levon@sun.com 	int res = Z_OK;
461412633Sjohn.levon@sun.com 
46158662SJordan.Vaughan@Sun.com 	if ((handle = zonecfg_init_handle()) == NULL) {
46168662SJordan.Vaughan@Sun.com 		zerror(zlogp, B_TRUE, "getting zone configuration handle");
46178662SJordan.Vaughan@Sun.com 		return (Z_BAD_HANDLE);
46188662SJordan.Vaughan@Sun.com 	}
46198662SJordan.Vaughan@Sun.com 	if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
46208662SJordan.Vaughan@Sun.com 		zerror(zlogp, B_FALSE, "invalid configuration");
462112633Sjohn.levon@sun.com 		goto out;
462212633Sjohn.levon@sun.com 	}
462312633Sjohn.levon@sun.com 
462412633Sjohn.levon@sun.com 	if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
462512633Sjohn.levon@sun.com 		goto out;
462612633Sjohn.levon@sun.com 
462712633Sjohn.levon@sun.com 	if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
462812633Sjohn.levon@sun.com 		goto out;
462912633Sjohn.levon@sun.com 
463012633Sjohn.levon@sun.com out:
46318662SJordan.Vaughan@Sun.com 	zonecfg_fini_handle(handle);
463212633Sjohn.levon@sun.com 	return (res);
46338662SJordan.Vaughan@Sun.com }
46348662SJordan.Vaughan@Sun.com 
4635766Scarlsonj zoneid_t
vplat_create(zlog_t * zlogp,zone_mnt_t mount_cmd)46365829Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4637766Scarlsonj {
4638766Scarlsonj 	zoneid_t rval = -1;
46390Sstevel@tonic-gate 	priv_set_t *privs;
46400Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];
46410Sstevel@tonic-gate 	char *rctlbuf = NULL;
4642766Scarlsonj 	size_t rctlbufsz = 0;
4643789Sahrens 	char *zfsbuf = NULL;
4644789Sahrens 	size_t zfsbufsz = 0;
4645766Scarlsonj 	zoneid_t zoneid = -1;
46460Sstevel@tonic-gate 	int xerr;
4647766Scarlsonj 	char *kzone;
4648766Scarlsonj 	FILE *fp = NULL;
46491676Sjpk 	tsol_zcent_t *zcent = NULL;
46501676Sjpk 	int match = 0;
46511676Sjpk 	int doi = 0;
46523448Sdh155122 	int flags;
46533448Sdh155122 	zone_iptype_t iptype;
46540Sstevel@tonic-gate 
46550Sstevel@tonic-gate 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
46560Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
46570Sstevel@tonic-gate 		return (-1);
46580Sstevel@tonic-gate 	}
4659766Scarlsonj 	if (zonecfg_in_alt_root())
4660766Scarlsonj 		resolve_lofs(zlogp, rootpath, sizeof (rootpath));
46610Sstevel@tonic-gate 
466210616SSebastien.Roy@Sun.COM 	if (vplat_get_iptype(zlogp, &iptype) < 0) {
46633448Sdh155122 		zerror(zlogp, B_TRUE, "unable to determine ip-type");
46643448Sdh155122 		return (-1);
46653448Sdh155122 	}
46663448Sdh155122 	switch (iptype) {
46673448Sdh155122 	case ZS_SHARED:
46683448Sdh155122 		flags = 0;
46693448Sdh155122 		break;
46703448Sdh155122 	case ZS_EXCLUSIVE:
46713448Sdh155122 		flags = ZCF_NET_EXCL;
46723448Sdh155122 		break;
46733448Sdh155122 	}
46743448Sdh155122 
46750Sstevel@tonic-gate 	if ((privs = priv_allocset()) == NULL) {
46760Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
46770Sstevel@tonic-gate 		return (-1);
46780Sstevel@tonic-gate 	}
46790Sstevel@tonic-gate 	priv_emptyset(privs);
46801645Scomay 	if (get_privset(zlogp, privs, mount_cmd) != 0)
46810Sstevel@tonic-gate 		goto error;
46821645Scomay 
46835829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT &&
46845829Sgjelinek 	    get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
46850Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "Unable to get list of rctls");
46860Sstevel@tonic-gate 		goto error;
46870Sstevel@tonic-gate 	}
46881645Scomay 
4689789Sahrens 	if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4690789Sahrens 		zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4691789Sahrens 		goto error;
4692789Sahrens 	}
46930Sstevel@tonic-gate 
46945829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
46951676Sjpk 		zcent = get_zone_label(zlogp, privs);
46961769Scarlsonj 		if (zcent != NULL) {
46971676Sjpk 			match = zcent->zc_match;
46981676Sjpk 			doi = zcent->zc_doi;
46991676Sjpk 			*zlabel = zcent->zc_label;
47001676Sjpk 		} else {
47011676Sjpk 			goto error;
47021676Sjpk 		}
470310972SRic.Aleshire@Sun.COM 		if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
470410972SRic.Aleshire@Sun.COM 			goto error;
47051676Sjpk 	}
47061676Sjpk 
4707766Scarlsonj 	kzone = zone_name;
4708766Scarlsonj 
4709766Scarlsonj 	/*
4710766Scarlsonj 	 * We must do this scan twice.  First, we look for zones running on the
4711766Scarlsonj 	 * main system that are using this root (or any subdirectory of it).
4712766Scarlsonj 	 * Next, we reduce to the shortest path and search for loopback mounts
4713766Scarlsonj 	 * that use this same source node (same device and inode).
4714766Scarlsonj 	 */
4715766Scarlsonj 	if (duplicate_zone_root(zlogp, rootpath))
4716766Scarlsonj 		goto error;
4717766Scarlsonj 	if (duplicate_reachable_path(zlogp, rootpath))
4718766Scarlsonj 		goto error;
4719766Scarlsonj 
47205829Sgjelinek 	if (ALT_MOUNT(mount_cmd)) {
4721766Scarlsonj 		root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4722766Scarlsonj 
4723766Scarlsonj 		/*
4724766Scarlsonj 		 * Forge up a special root for this zone.  When a zone is
4725766Scarlsonj 		 * mounted, we can't let the zone have its own root because the
4726766Scarlsonj 		 * tools that will be used in this "scratch zone" need access
4727766Scarlsonj 		 * to both the zone's resources and the running machine's
4728766Scarlsonj 		 * executables.
4729766Scarlsonj 		 *
4730766Scarlsonj 		 * Note that the mkdir here also catches read-only filesystems.
4731766Scarlsonj 		 */
4732766Scarlsonj 		if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4733766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4734766Scarlsonj 			goto error;
4735766Scarlsonj 		}
4736766Scarlsonj 		if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4737766Scarlsonj 			goto error;
4738766Scarlsonj 	}
4739766Scarlsonj 
4740766Scarlsonj 	if (zonecfg_in_alt_root()) {
4741766Scarlsonj 		/*
4742766Scarlsonj 		 * If we are mounting up a zone in an alternate root partition,
4743766Scarlsonj 		 * then we have some additional work to do before starting the
4744766Scarlsonj 		 * zone.  First, resolve the root path down so that we're not
4745766Scarlsonj 		 * fooled by duplicates.  Then forge up an internal name for
4746766Scarlsonj 		 * the zone.
4747766Scarlsonj 		 */
4748766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4749766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
4750766Scarlsonj 			goto error;
4751766Scarlsonj 		}
4752766Scarlsonj 		if (zonecfg_lock_scratch(fp) != 0) {
4753766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
4754766Scarlsonj 			goto error;
4755766Scarlsonj 		}
4756766Scarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4757766Scarlsonj 		    NULL, 0) == 0) {
4758766Scarlsonj 			zerror(zlogp, B_FALSE, "scratch zone already running");
4759766Scarlsonj 			goto error;
4760766Scarlsonj 		}
4761766Scarlsonj 		/* This is the preferred name */
4762766Scarlsonj 		(void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4763766Scarlsonj 		    zone_name);
4764766Scarlsonj 		srandom(getpid());
4765766Scarlsonj 		while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4766766Scarlsonj 		    0) == 0) {
4767766Scarlsonj 			/* This is just an arbitrary name; note "." usage */
4768766Scarlsonj 			(void) snprintf(kernzone, sizeof (kernzone),
4769766Scarlsonj 			    "SUNWlu.%08lX%08lX", random(), random());
4770766Scarlsonj 		}
4771766Scarlsonj 		kzone = kernzone;
4772766Scarlsonj 	}
4773766Scarlsonj 
47740Sstevel@tonic-gate 	xerr = 0;
4775766Scarlsonj 	if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
47763448Sdh155122 	    rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
47773448Sdh155122 	    flags)) == -1) {
47780Sstevel@tonic-gate 		if (xerr == ZE_AREMOUNTS) {
47790Sstevel@tonic-gate 			if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
47800Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
47810Sstevel@tonic-gate 				    "An unknown file-system is mounted on "
47820Sstevel@tonic-gate 				    "a subdirectory of %s", rootpath);
47830Sstevel@tonic-gate 			} else {
47840Sstevel@tonic-gate 
47850Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
47860Sstevel@tonic-gate 				    "These file-systems are mounted on "
47870Sstevel@tonic-gate 				    "subdirectories of %s:", rootpath);
47880Sstevel@tonic-gate 				(void) zonecfg_find_mounts(rootpath,
47890Sstevel@tonic-gate 				    prtmount, zlogp);
47900Sstevel@tonic-gate 			}
47910Sstevel@tonic-gate 		} else if (xerr == ZE_CHROOTED) {
47920Sstevel@tonic-gate 			zerror(zlogp, B_FALSE, "%s: "
47930Sstevel@tonic-gate 			    "cannot create a zone from a chrooted "
47940Sstevel@tonic-gate 			    "environment", "zone_create");
47954791Ston 		} else if (xerr == ZE_LABELINUSE) {
47964791Ston 			char zonename[ZONENAME_MAX];
47974791Ston 			(void) getzonenamebyid(getzoneidbylabel(zlabel),
47984791Ston 			    zonename, ZONENAME_MAX);
47994791Ston 			zerror(zlogp, B_FALSE, "The zone label is already "
48004791Ston 			    "used by the zone '%s'.", zonename);
48010Sstevel@tonic-gate 		} else {
48020Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "%s failed", "zone_create");
48030Sstevel@tonic-gate 		}
48040Sstevel@tonic-gate 		goto error;
48050Sstevel@tonic-gate 	}
4806766Scarlsonj 
4807766Scarlsonj 	if (zonecfg_in_alt_root() &&
4808766Scarlsonj 	    zonecfg_add_scratch(fp, zone_name, kernzone,
4809766Scarlsonj 	    zonecfg_get_root()) == -1) {
4810766Scarlsonj 		zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4811766Scarlsonj 		goto error;
4812766Scarlsonj 	}
4813766Scarlsonj 
48140Sstevel@tonic-gate 	/*
48153247Sgjelinek 	 * The following actions are not performed when merely mounting a zone
48163247Sgjelinek 	 * for administrative use.
48170Sstevel@tonic-gate 	 */
48185829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
48197655Sgerald.jelinek@sun.com 		brand_handle_t bh;
48207655Sgerald.jelinek@sun.com 		struct brand_attr attr;
48217655Sgerald.jelinek@sun.com 		char modname[MAXPATHLEN];
48227655Sgerald.jelinek@sun.com 
482312633Sjohn.levon@sun.com 		if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
48248662SJordan.Vaughan@Sun.com 			goto error;
48258662SJordan.Vaughan@Sun.com 
482610796SStephen.Lawrence@Sun.COM 		if ((bh = brand_open(brand_name)) == NULL) {
48277655Sgerald.jelinek@sun.com 			zerror(zlogp, B_FALSE,
48287655Sgerald.jelinek@sun.com 			    "unable to determine brand name");
48298662SJordan.Vaughan@Sun.com 			goto error;
48307655Sgerald.jelinek@sun.com 		}
48317655Sgerald.jelinek@sun.com 
48328905SRic.Aleshire@Sun.COM 		if (!is_system_labeled() &&
483310796SStephen.Lawrence@Sun.COM 		    (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
48348905SRic.Aleshire@Sun.COM 			brand_close(bh);
48358905SRic.Aleshire@Sun.COM 			zerror(zlogp, B_FALSE,
48368905SRic.Aleshire@Sun.COM 			    "cannot boot labeled zone on unlabeled system");
48378905SRic.Aleshire@Sun.COM 			goto error;
48388905SRic.Aleshire@Sun.COM 		}
48398905SRic.Aleshire@Sun.COM 
48407655Sgerald.jelinek@sun.com 		/*
48417655Sgerald.jelinek@sun.com 		 * If this brand requires any kernel support, now is the time to
48427655Sgerald.jelinek@sun.com 		 * get it loaded and initialized.
48437655Sgerald.jelinek@sun.com 		 */
48447655Sgerald.jelinek@sun.com 		if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
48457655Sgerald.jelinek@sun.com 			brand_close(bh);
48467655Sgerald.jelinek@sun.com 			zerror(zlogp, B_FALSE,
48477655Sgerald.jelinek@sun.com 			    "unable to determine brand kernel module");
48488662SJordan.Vaughan@Sun.com 			goto error;
48497655Sgerald.jelinek@sun.com 		}
48507655Sgerald.jelinek@sun.com 		brand_close(bh);
48517655Sgerald.jelinek@sun.com 
48527655Sgerald.jelinek@sun.com 		if (strlen(modname) > 0) {
485310796SStephen.Lawrence@Sun.COM 			(void) strlcpy(attr.ba_brandname, brand_name,
485410796SStephen.Lawrence@Sun.COM 			    sizeof (attr.ba_brandname));
485510796SStephen.Lawrence@Sun.COM 			(void) strlcpy(attr.ba_modname, modname,
485610796SStephen.Lawrence@Sun.COM 			    sizeof (attr.ba_modname));
48577655Sgerald.jelinek@sun.com 			if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
48587655Sgerald.jelinek@sun.com 			    sizeof (attr) != 0)) {
48597655Sgerald.jelinek@sun.com 				zerror(zlogp, B_TRUE,
48607655Sgerald.jelinek@sun.com 				    "could not set zone brand attribute.");
48617655Sgerald.jelinek@sun.com 				goto error;
48627655Sgerald.jelinek@sun.com 			}
48637655Sgerald.jelinek@sun.com 		}
48647655Sgerald.jelinek@sun.com 
48658662SJordan.Vaughan@Sun.com 		if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
48663247Sgjelinek 			goto error;
48673247Sgjelinek 
48681769Scarlsonj 		set_mlps(zlogp, zoneid, zcent);
48693247Sgjelinek 	}
48703247Sgjelinek 
4871766Scarlsonj 	rval = zoneid;
4872766Scarlsonj 	zoneid = -1;
4873766Scarlsonj 
48740Sstevel@tonic-gate error:
48758662SJordan.Vaughan@Sun.com 	if (zoneid != -1) {
48768662SJordan.Vaughan@Sun.com 		(void) zone_shutdown(zoneid);
4877766Scarlsonj 		(void) zone_destroy(zoneid);
48788662SJordan.Vaughan@Sun.com 	}
48790Sstevel@tonic-gate 	if (rctlbuf != NULL)
48800Sstevel@tonic-gate 		free(rctlbuf);
48810Sstevel@tonic-gate 	priv_freeset(privs);
4882766Scarlsonj 	if (fp != NULL)
4883766Scarlsonj 		zonecfg_close_scratch(fp);
4884766Scarlsonj 	lofs_discard_mnttab();
48851676Sjpk 	if (zcent != NULL)
48861676Sjpk 		tsol_freezcent(zcent);
48870Sstevel@tonic-gate 	return (rval);
48880Sstevel@tonic-gate }
48890Sstevel@tonic-gate 
48902303Scarlsonj /*
48912303Scarlsonj  * Enter the zone and write a /etc/zones/index file there.  This allows
48922303Scarlsonj  * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
48932303Scarlsonj  * details from inside the zone.
48942303Scarlsonj  */
48952303Scarlsonj static void
write_index_file(zoneid_t zoneid)48962303Scarlsonj write_index_file(zoneid_t zoneid)
48972303Scarlsonj {
48982303Scarlsonj 	FILE *zef;
48992303Scarlsonj 	FILE *zet;
49002303Scarlsonj 	struct zoneent *zep;
49012303Scarlsonj 	pid_t child;
49022303Scarlsonj 	int tmpl_fd;
49032303Scarlsonj 	ctid_t ct;
49042303Scarlsonj 	int fd;
49052303Scarlsonj 	char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
49062303Scarlsonj 
49072303Scarlsonj 	/* Locate the zone entry in the global zone's index file */
49082303Scarlsonj 	if ((zef = setzoneent()) == NULL)
49092303Scarlsonj 		return;
49102303Scarlsonj 	while ((zep = getzoneent_private(zef)) != NULL) {
49112303Scarlsonj 		if (strcmp(zep->zone_name, zone_name) == 0)
49122303Scarlsonj 			break;
49132303Scarlsonj 		free(zep);
49142303Scarlsonj 	}
49152303Scarlsonj 	endzoneent(zef);
49162303Scarlsonj 	if (zep == NULL)
49172303Scarlsonj 		return;
49182303Scarlsonj 
49192303Scarlsonj 	if ((tmpl_fd = init_template()) == -1) {
49202303Scarlsonj 		free(zep);
49212303Scarlsonj 		return;
49222303Scarlsonj 	}
49232303Scarlsonj 
49242303Scarlsonj 	if ((child = fork()) == -1) {
49252303Scarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
49262303Scarlsonj 		(void) close(tmpl_fd);
49272303Scarlsonj 		free(zep);
49282303Scarlsonj 		return;
49292303Scarlsonj 	}
49302303Scarlsonj 
49312303Scarlsonj 	/* parent waits for child to finish */
49322303Scarlsonj 	if (child != 0) {
49332303Scarlsonj 		free(zep);
49342303Scarlsonj 		if (contract_latest(&ct) == -1)
49352303Scarlsonj 			ct = -1;
49362303Scarlsonj 		(void) ct_tmpl_clear(tmpl_fd);
49372303Scarlsonj 		(void) close(tmpl_fd);
49382303Scarlsonj 		(void) waitpid(child, NULL, 0);
49392303Scarlsonj 		(void) contract_abandon_id(ct);
49402303Scarlsonj 		return;
49412303Scarlsonj 	}
49422303Scarlsonj 
49432303Scarlsonj 	/* child enters zone and sets up index file */
49442303Scarlsonj 	(void) ct_tmpl_clear(tmpl_fd);
49452303Scarlsonj 	if (zone_enter(zoneid) != -1) {
49462303Scarlsonj 		(void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
49472303Scarlsonj 		(void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
49482303Scarlsonj 		    ZONE_CONFIG_GID);
49492303Scarlsonj 		fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
49502303Scarlsonj 		    ZONE_INDEX_MODE);
49512303Scarlsonj 		if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
49522303Scarlsonj 			(void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
49532303Scarlsonj 			if (uuid_is_null(zep->zone_uuid))
49542303Scarlsonj 				uuidstr[0] = '\0';
49552303Scarlsonj 			else
49562303Scarlsonj 				uuid_unparse(zep->zone_uuid, uuidstr);
49572303Scarlsonj 			(void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
49582303Scarlsonj 			    zone_state_str(zep->zone_state),
49592303Scarlsonj 			    uuidstr);
49602303Scarlsonj 			(void) fclose(zet);
49612303Scarlsonj 		}
49622303Scarlsonj 	}
49632303Scarlsonj 	_exit(0);
49642303Scarlsonj }
49652303Scarlsonj 
49660Sstevel@tonic-gate int
vplat_bringup(zlog_t * zlogp,zone_mnt_t mount_cmd,zoneid_t zoneid)49675829Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
49680Sstevel@tonic-gate {
49692712Snn35248 	char zonepath[MAXPATHLEN];
49702503Sdp 
49715829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4972789Sahrens 		lofs_discard_mnttab();
4973789Sahrens 		return (-1);
4974789Sahrens 	}
4975789Sahrens 
49762712Snn35248 	/*
49772712Snn35248 	 * Before we try to mount filesystems we need to create the
49782712Snn35248 	 * attribute backing store for /dev
49792712Snn35248 	 */
49802712Snn35248 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
49812712Snn35248 		lofs_discard_mnttab();
49822712Snn35248 		return (-1);
49832712Snn35248 	}
49843071Svp157776 	resolve_lofs(zlogp, zonepath, sizeof (zonepath));
49853813Sdp 
49863813Sdp 	/* Make /dev directory owned by root, grouped sys */
49873813Sdp 	if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
49883813Sdp 	    0, 3) != 0) {
49892712Snn35248 		lofs_discard_mnttab();
49902712Snn35248 		return (-1);
49912712Snn35248 	}
49922712Snn35248 
49932621Sllai1 	if (mount_filesystems(zlogp, mount_cmd) != 0) {
4994766Scarlsonj 		lofs_discard_mnttab();
49950Sstevel@tonic-gate 		return (-1);
4996766Scarlsonj 	}
49972621Sllai1 
49985829Sgjelinek 	if (mount_cmd == Z_MNT_BOOT) {
49993448Sdh155122 		zone_iptype_t iptype;
50003448Sdh155122 
500110616SSebastien.Roy@Sun.COM 		if (vplat_get_iptype(zlogp, &iptype) < 0) {
50023448Sdh155122 			zerror(zlogp, B_TRUE, "unable to determine ip-type");
50033448Sdh155122 			lofs_discard_mnttab();
50043448Sdh155122 			return (-1);
50053448Sdh155122 		}
50063448Sdh155122 
50073448Sdh155122 		switch (iptype) {
50083448Sdh155122 		case ZS_SHARED:
50093448Sdh155122 			/* Always do this to make lo0 get configured */
50103448Sdh155122 			if (configure_shared_network_interfaces(zlogp) != 0) {
50113448Sdh155122 				lofs_discard_mnttab();
50123448Sdh155122 				return (-1);
50133448Sdh155122 			}
50143448Sdh155122 			break;
50153448Sdh155122 		case ZS_EXCLUSIVE:
501612748SSowmini.Varadhan@oracle.COM 			if (configure_exclusive_network_interfaces(zlogp,
501712748SSowmini.Varadhan@oracle.COM 			    zoneid) !=
50183448Sdh155122 			    0) {
50193448Sdh155122 				lofs_discard_mnttab();
50203448Sdh155122 				return (-1);
50213448Sdh155122 			}
50223448Sdh155122 			break;
50233448Sdh155122 		}
5024766Scarlsonj 	}
50252303Scarlsonj 
50262303Scarlsonj 	write_index_file(zoneid);
50272303Scarlsonj 
5028766Scarlsonj 	lofs_discard_mnttab();
50290Sstevel@tonic-gate 	return (0);
50300Sstevel@tonic-gate }
50310Sstevel@tonic-gate 
5032766Scarlsonj static int
lu_root_teardown(zlog_t * zlogp)5033766Scarlsonj lu_root_teardown(zlog_t *zlogp)
5034766Scarlsonj {
5035766Scarlsonj 	char zroot[MAXPATHLEN];
5036766Scarlsonj 
5037766Scarlsonj 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5038766Scarlsonj 		zerror(zlogp, B_FALSE, "unable to determine zone root");
5039766Scarlsonj 		return (-1);
5040766Scarlsonj 	}
5041766Scarlsonj 	root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
5042766Scarlsonj 
5043766Scarlsonj 	/*
5044766Scarlsonj 	 * At this point, the processes are gone, the filesystems (save the
5045766Scarlsonj 	 * root) are unmounted, and the zone is on death row.  But there may
5046766Scarlsonj 	 * still be creds floating about in the system that reference the
5047766Scarlsonj 	 * zone_t, and which pin down zone_rootvp causing this call to fail
5048766Scarlsonj 	 * with EBUSY.  Thus, we try for a little while before just giving up.
5049766Scarlsonj 	 * (How I wish this were not true, and umount2 just did the right
5050766Scarlsonj 	 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
5051766Scarlsonj 	 */
5052766Scarlsonj 	if (umount2(zroot, MS_FORCE) != 0) {
5053766Scarlsonj 		if (errno == ENOTSUP && umount2(zroot, 0) == 0)
5054766Scarlsonj 			goto unmounted;
5055766Scarlsonj 		if (errno == EBUSY) {
5056766Scarlsonj 			int tries = 10;
5057766Scarlsonj 
5058766Scarlsonj 			while (--tries >= 0) {
5059766Scarlsonj 				(void) sleep(1);
5060766Scarlsonj 				if (umount2(zroot, 0) == 0)
5061766Scarlsonj 					goto unmounted;
5062766Scarlsonj 				if (errno != EBUSY)
5063766Scarlsonj 					break;
5064766Scarlsonj 			}
5065766Scarlsonj 		}
5066766Scarlsonj 		zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
5067766Scarlsonj 		return (-1);
5068766Scarlsonj 	}
5069766Scarlsonj unmounted:
5070766Scarlsonj 
5071766Scarlsonj 	/*
5072766Scarlsonj 	 * Only zones in an alternate root environment have scratch zone
5073766Scarlsonj 	 * entries.
5074766Scarlsonj 	 */
5075766Scarlsonj 	if (zonecfg_in_alt_root()) {
5076766Scarlsonj 		FILE *fp;
5077766Scarlsonj 		int retv;
5078766Scarlsonj 
5079766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5080766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot open mapfile");
5081766Scarlsonj 			return (-1);
5082766Scarlsonj 		}
5083766Scarlsonj 		retv = -1;
5084766Scarlsonj 		if (zonecfg_lock_scratch(fp) != 0)
5085766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot lock mapfile");
5086766Scarlsonj 		else if (zonecfg_delete_scratch(fp, kernzone) != 0)
5087766Scarlsonj 			zerror(zlogp, B_TRUE, "cannot delete map entry");
5088766Scarlsonj 		else
5089766Scarlsonj 			retv = 0;
5090766Scarlsonj 		zonecfg_close_scratch(fp);
5091766Scarlsonj 		return (retv);
5092766Scarlsonj 	} else {
5093766Scarlsonj 		return (0);
5094766Scarlsonj 	}
5095766Scarlsonj }
5096766Scarlsonj 
50970Sstevel@tonic-gate int
vplat_teardown(zlog_t * zlogp,boolean_t unmount_cmd,boolean_t rebooting)50983247Sgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
50990Sstevel@tonic-gate {
5100766Scarlsonj 	char *kzone;
51010Sstevel@tonic-gate 	zoneid_t zoneid;
51023247Sgjelinek 	int res;
51033247Sgjelinek 	char pool_err[128];
51047089Sgjelinek 	char zpath[MAXPATHLEN];
51052712Snn35248 	char cmdbuf[MAXPATHLEN];
51062727Sedp 	brand_handle_t bh = NULL;
510710616SSebastien.Roy@Sun.COM 	dladm_status_t status;
510810616SSebastien.Roy@Sun.COM 	char errmsg[DLADM_STRSIZE];
51093448Sdh155122 	ushort_t flags;
51100Sstevel@tonic-gate 
5111766Scarlsonj 	kzone = zone_name;
5112766Scarlsonj 	if (zonecfg_in_alt_root()) {
5113766Scarlsonj 		FILE *fp;
5114766Scarlsonj 
5115766Scarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5116766Scarlsonj 			zerror(zlogp, B_TRUE, "unable to open map file");
5117766Scarlsonj 			goto error;
5118766Scarlsonj 		}
5119766Scarlsonj 		if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5120766Scarlsonj 		    kernzone, sizeof (kernzone)) != 0) {
5121766Scarlsonj 			zerror(zlogp, B_FALSE, "unable to find scratch zone");
5122766Scarlsonj 			zonecfg_close_scratch(fp);
5123766Scarlsonj 			goto error;
5124766Scarlsonj 		}
5125766Scarlsonj 		zonecfg_close_scratch(fp);
5126766Scarlsonj 		kzone = kernzone;
5127766Scarlsonj 	}
5128766Scarlsonj 
5129766Scarlsonj 	if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
51300Sstevel@tonic-gate 		if (!bringup_failure_recovery)
51310Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "unable to get zoneid");
5132766Scarlsonj 		if (unmount_cmd)
5133766Scarlsonj 			(void) lu_root_teardown(zlogp);
51340Sstevel@tonic-gate 		goto error;
51350Sstevel@tonic-gate 	}
51360Sstevel@tonic-gate 
513711878SVenu.Iyer@Sun.COM 	if (remove_datalink_pool(zlogp, zoneid) != 0) {
513811878SVenu.Iyer@Sun.COM 		zerror(zlogp, B_FALSE, "unable clear datalink pool property");
513911878SVenu.Iyer@Sun.COM 		goto error;
514011878SVenu.Iyer@Sun.COM 	}
514111878SVenu.Iyer@Sun.COM 
514212748SSowmini.Varadhan@oracle.COM 	if (remove_datalink_protect(zlogp, zoneid) != 0) {
514312748SSowmini.Varadhan@oracle.COM 		zerror(zlogp, B_FALSE,
514412748SSowmini.Varadhan@oracle.COM 		    "unable clear datalink protect property");
514512748SSowmini.Varadhan@oracle.COM 		goto error;
514612748SSowmini.Varadhan@oracle.COM 	}
514712748SSowmini.Varadhan@oracle.COM 
514812748SSowmini.Varadhan@oracle.COM 	/*
514912748SSowmini.Varadhan@oracle.COM 	 * The datalinks assigned to the zone will be removed from the NGZ as
515012748SSowmini.Varadhan@oracle.COM 	 * part of zone_shutdown() so that we need to remove protect/pool etc.
515112748SSowmini.Varadhan@oracle.COM 	 * before zone_shutdown(). Even if the shutdown itself fails, the zone
515212748SSowmini.Varadhan@oracle.COM 	 * will not be able to violate any constraints applied because the
515312748SSowmini.Varadhan@oracle.COM 	 * datalinks are no longer available to the zone.
515412748SSowmini.Varadhan@oracle.COM 	 */
51550Sstevel@tonic-gate 	if (zone_shutdown(zoneid) != 0) {
51560Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to shutdown zone");
51570Sstevel@tonic-gate 		goto error;
51580Sstevel@tonic-gate 	}
51590Sstevel@tonic-gate 
51607089Sgjelinek 	/* Get the zonepath of this zone */
51617089Sgjelinek 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
51627089Sgjelinek 		zerror(zlogp, B_FALSE, "unable to determine zone path");
51632712Snn35248 		goto error;
51642712Snn35248 	}
51652712Snn35248 
51662712Snn35248 	/* Get a handle to the brand info for this zone */
516710796SStephen.Lawrence@Sun.COM 	if ((bh = brand_open(brand_name)) == NULL) {
51682712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
51692712Snn35248 		return (-1);
51702712Snn35248 	}
51712712Snn35248 	/*
51722712Snn35248 	 * If there is a brand 'halt' callback, execute it now to give the
51732712Snn35248 	 * brand a chance to cleanup any custom configuration.
51742712Snn35248 	 */
51752712Snn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
51767089Sgjelinek 	if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
51777089Sgjelinek 	    sizeof (cmdbuf) - EXEC_LEN) < 0) {
51782727Sedp 		brand_close(bh);
51792712Snn35248 		zerror(zlogp, B_FALSE, "unable to determine branded zone's "
51802712Snn35248 		    "halt callback.");
51812712Snn35248 		goto error;
51822712Snn35248 	}
51832727Sedp 	brand_close(bh);
51842712Snn35248 
51852712Snn35248 	if ((strlen(cmdbuf) > EXEC_LEN) &&
51867370S<Gerald Jelinek> 	    (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
51872712Snn35248 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
51882712Snn35248 		goto error;
51892712Snn35248 	}
51902712Snn35248 
51913448Sdh155122 	if (!unmount_cmd) {
51923448Sdh155122 		zone_iptype_t iptype;
51933448Sdh155122 
51943448Sdh155122 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
51953448Sdh155122 		    sizeof (flags)) < 0) {
519610616SSebastien.Roy@Sun.COM 			if (vplat_get_iptype(zlogp, &iptype) < 0) {
51973448Sdh155122 				zerror(zlogp, B_TRUE, "unable to determine "
51983448Sdh155122 				    "ip-type");
51993448Sdh155122 				goto error;
52003448Sdh155122 			}
52013448Sdh155122 		} else {
52023448Sdh155122 			if (flags & ZF_NET_EXCL)
52033448Sdh155122 				iptype = ZS_EXCLUSIVE;
52043448Sdh155122 			else
52053448Sdh155122 				iptype = ZS_SHARED;
52063448Sdh155122 		}
52073448Sdh155122 
52083448Sdh155122 		switch (iptype) {
52093448Sdh155122 		case ZS_SHARED:
52103448Sdh155122 			if (unconfigure_shared_network_interfaces(zlogp,
52113448Sdh155122 			    zoneid) != 0) {
52123448Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
52133448Sdh155122 				    "network interfaces in zone");
52143448Sdh155122 				goto error;
52153448Sdh155122 			}
52163448Sdh155122 			break;
52173448Sdh155122 		case ZS_EXCLUSIVE:
52183448Sdh155122 			if (unconfigure_exclusive_network_interfaces(zlogp,
52193448Sdh155122 			    zoneid) != 0) {
52203448Sdh155122 				zerror(zlogp, B_FALSE, "unable to unconfigure "
52213448Sdh155122 				    "network interfaces in zone");
52223448Sdh155122 				goto error;
52233448Sdh155122 			}
522410616SSebastien.Roy@Sun.COM 			status = dladm_zone_halt(dld_handle, zoneid);
522510616SSebastien.Roy@Sun.COM 			if (status != DLADM_STATUS_OK) {
522610616SSebastien.Roy@Sun.COM 				zerror(zlogp, B_FALSE, "unable to notify "
522710616SSebastien.Roy@Sun.COM 				    "dlmgmtd of zone halt: %s",
522810616SSebastien.Roy@Sun.COM 				    dladm_status2str(status, errmsg));
522910616SSebastien.Roy@Sun.COM 			}
52303448Sdh155122 			break;
52313448Sdh155122 		}
52320Sstevel@tonic-gate 	}
52330Sstevel@tonic-gate 
5234766Scarlsonj 	if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
52350Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to abort TCP connections");
52360Sstevel@tonic-gate 		goto error;
52370Sstevel@tonic-gate 	}
52380Sstevel@tonic-gate 
5239766Scarlsonj 	if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
52400Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
52410Sstevel@tonic-gate 		    "unable to unmount file systems in zone");
52420Sstevel@tonic-gate 		goto error;
52430Sstevel@tonic-gate 	}
52440Sstevel@tonic-gate 
52453247Sgjelinek 	/*
52463356Sgjelinek 	 * If we are rebooting then we normally don't want to destroy an
52473356Sgjelinek 	 * existing temporary pool at this point so that we can just reuse it
52483356Sgjelinek 	 * when the zone boots back up.  However, it is also possible we were
52493356Sgjelinek 	 * running with a temporary pool and the zone configuration has been
52503356Sgjelinek 	 * modified to no longer use a temporary pool.  In that case we need
52513356Sgjelinek 	 * to destroy the temporary pool now.  This case looks like the case
52523356Sgjelinek 	 * where we never had a temporary pool configured but
52533356Sgjelinek 	 * zonecfg_destroy_tmp_pool will do the right thing either way.
52543247Sgjelinek 	 */
52553356Sgjelinek 	if (!unmount_cmd) {
52563356Sgjelinek 		boolean_t destroy_tmp_pool = B_TRUE;
52573356Sgjelinek 
52583356Sgjelinek 		if (rebooting) {
52593356Sgjelinek 			struct zone_psettab pset_tab;
52603356Sgjelinek 			zone_dochandle_t handle;
52613356Sgjelinek 
52623356Sgjelinek 			if ((handle = zonecfg_init_handle()) != NULL &&
52633356Sgjelinek 			    zonecfg_get_handle(zone_name, handle) == Z_OK &&
52643356Sgjelinek 			    zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
52653356Sgjelinek 				destroy_tmp_pool = B_FALSE;
52663716Sgjelinek 
52673716Sgjelinek 			zonecfg_fini_handle(handle);
52683356Sgjelinek 		}
52693356Sgjelinek 
52703356Sgjelinek 		if (destroy_tmp_pool) {
52713356Sgjelinek 			if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
52723356Sgjelinek 			    sizeof (pool_err))) != Z_OK) {
52733356Sgjelinek 				if (res == Z_POOL)
52743356Sgjelinek 					zerror(zlogp, B_FALSE, pool_err);
52753356Sgjelinek 			}
52763247Sgjelinek 		}
52773247Sgjelinek 	}
52783247Sgjelinek 
52791676Sjpk 	remove_mlps(zlogp, zoneid);
52801676Sjpk 
52810Sstevel@tonic-gate 	if (zone_destroy(zoneid) != 0) {
52820Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to destroy zone");
52830Sstevel@tonic-gate 		goto error;
52840Sstevel@tonic-gate 	}
52850Sstevel@tonic-gate 
5286766Scarlsonj 	/*
5287766Scarlsonj 	 * Special teardown for alternate boot environments: remove the tmpfs
5288766Scarlsonj 	 * root for the zone and then remove it from the map file.
5289766Scarlsonj 	 */
5290766Scarlsonj 	if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5291766Scarlsonj 		goto error;
5292766Scarlsonj 
5293766Scarlsonj 	lofs_discard_mnttab();
52940Sstevel@tonic-gate 	return (0);
52950Sstevel@tonic-gate 
52960Sstevel@tonic-gate error:
5297766Scarlsonj 	lofs_discard_mnttab();
52980Sstevel@tonic-gate 	return (-1);
52990Sstevel@tonic-gate }
5300