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 /* 231544Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * This module contains functions used to bring up and tear down the 310Sstevel@tonic-gate * Virtual Platform: [un]mounting file-systems, [un]plumbing network 320Sstevel@tonic-gate * interfaces, [un]configuring devices, establishing resource controls, 330Sstevel@tonic-gate * and creating/destroying the zone in the kernel. These actions, on 340Sstevel@tonic-gate * the way up, ready the zone; on the way down, they halt the zone. 350Sstevel@tonic-gate * See the much longer block comment at the beginning of zoneadmd.c 360Sstevel@tonic-gate * for a bigger picture of how the whole program functions. 37766Scarlsonj * 38766Scarlsonj * This module also has primary responsibility for the layout of "scratch 39766Scarlsonj * zones." These are mounted, but inactive, zones that are used during 40766Scarlsonj * operating system upgrade and potentially other administrative action. The 41766Scarlsonj * scratch zone environment is similar to the miniroot environment. The zone's 42766Scarlsonj * actual root is mounted read-write on /a, and the standard paths (/usr, 43766Scarlsonj * /sbin, /lib) all lead to read-only copies of the running system's binaries. 44766Scarlsonj * This allows the administrative tools to manipulate the zone using "-R /a" 45766Scarlsonj * without relying on any binaries in the zone itself. 46766Scarlsonj * 47766Scarlsonj * If the scratch zone is on an alternate root (Live Upgrade [LU] boot 48766Scarlsonj * environment), then we must resolve the lofs mounts used there to uncover 49766Scarlsonj * writable (unshared) resources. Shared resources, though, are always 50766Scarlsonj * read-only. In addition, if the "same" zone with a different root path is 51766Scarlsonj * currently running, then "/b" inside the zone points to the running zone's 52766Scarlsonj * root. This allows LU to synchronize configuration files during the upgrade 53766Scarlsonj * process. 54766Scarlsonj * 55766Scarlsonj * To construct this environment, this module creates a tmpfs mount on 56766Scarlsonj * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as 57766Scarlsonj * described above is constructed on the fly. The zone is then created using 58766Scarlsonj * $ZONEPATH/lu as the root. 59766Scarlsonj * 60766Scarlsonj * Note that scratch zones are inactive. The zone's bits are not running and 61766Scarlsonj * likely cannot be run correctly until upgrade is done. Init is not running 62766Scarlsonj * there, nor is SMF. Because of this, the "mounted" state of a scratch zone 63766Scarlsonj * is not a part of the usual halt/ready/boot state machine. 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate 660Sstevel@tonic-gate #include <sys/param.h> 670Sstevel@tonic-gate #include <sys/mount.h> 680Sstevel@tonic-gate #include <sys/mntent.h> 690Sstevel@tonic-gate #include <sys/socket.h> 700Sstevel@tonic-gate #include <sys/utsname.h> 710Sstevel@tonic-gate #include <sys/types.h> 720Sstevel@tonic-gate #include <sys/stat.h> 730Sstevel@tonic-gate #include <sys/sockio.h> 740Sstevel@tonic-gate #include <sys/stropts.h> 750Sstevel@tonic-gate #include <sys/conf.h> 760Sstevel@tonic-gate 770Sstevel@tonic-gate #include <inet/tcp.h> 780Sstevel@tonic-gate #include <arpa/inet.h> 790Sstevel@tonic-gate #include <netinet/in.h> 800Sstevel@tonic-gate #include <net/route.h> 810Sstevel@tonic-gate 820Sstevel@tonic-gate #include <stdio.h> 830Sstevel@tonic-gate #include <errno.h> 840Sstevel@tonic-gate #include <fcntl.h> 850Sstevel@tonic-gate #include <unistd.h> 860Sstevel@tonic-gate #include <rctl.h> 870Sstevel@tonic-gate #include <stdlib.h> 880Sstevel@tonic-gate #include <string.h> 890Sstevel@tonic-gate #include <strings.h> 900Sstevel@tonic-gate #include <wait.h> 910Sstevel@tonic-gate #include <limits.h> 920Sstevel@tonic-gate #include <libgen.h> 93789Sahrens #include <libzfs.h> 940Sstevel@tonic-gate #include <zone.h> 950Sstevel@tonic-gate #include <assert.h> 962303Scarlsonj #include <libcontract.h> 972303Scarlsonj #include <libcontract_priv.h> 982303Scarlsonj #include <uuid/uuid.h> 990Sstevel@tonic-gate 1000Sstevel@tonic-gate #include <sys/mntio.h> 1010Sstevel@tonic-gate #include <sys/mnttab.h> 1020Sstevel@tonic-gate #include <sys/fs/autofs.h> /* for _autofssys() */ 1030Sstevel@tonic-gate #include <sys/fs/lofs_info.h> 104789Sahrens #include <sys/fs/zfs.h> 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate #include <pool.h> 1070Sstevel@tonic-gate #include <sys/pool.h> 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate #include <libzonecfg.h> 1102170Sevanl #include <synch.h> 111*2611Svp157776 1120Sstevel@tonic-gate #include "zoneadmd.h" 1131676Sjpk #include <tsol/label.h> 1141676Sjpk #include <libtsnet.h> 1151676Sjpk #include <sys/priv.h> 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate #define V4_ADDR_LEN 32 1180Sstevel@tonic-gate #define V6_ADDR_LEN 128 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 0755 is the default directory mode. */ 1210Sstevel@tonic-gate #define DEFAULT_DIR_MODE \ 1220Sstevel@tonic-gate (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate #define IPD_DEFAULT_OPTS \ 1250Sstevel@tonic-gate MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate #define DFSTYPES "/etc/dfs/fstypes" 1281676Sjpk #define MAXTNZLEN 2048 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* 1310Sstevel@tonic-gate * A list of directories which should be created. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate struct dir_info { 1350Sstevel@tonic-gate char *dir_name; 1360Sstevel@tonic-gate mode_t dir_mode; 1370Sstevel@tonic-gate }; 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* 1400Sstevel@tonic-gate * The pathnames below are relative to the zonepath 1410Sstevel@tonic-gate */ 1420Sstevel@tonic-gate static struct dir_info dev_dirs[] = { 1430Sstevel@tonic-gate { "/dev", 0755 }, 1440Sstevel@tonic-gate { "/dev/dsk", 0755 }, 1450Sstevel@tonic-gate { "/dev/fd", 0555 }, 1460Sstevel@tonic-gate { "/dev/pts", 0755 }, 1470Sstevel@tonic-gate { "/dev/rdsk", 0755 }, 1480Sstevel@tonic-gate { "/dev/rmt", 0755 }, 1490Sstevel@tonic-gate { "/dev/sad", 0755 }, 1500Sstevel@tonic-gate { "/dev/swap", 0755 }, 1510Sstevel@tonic-gate { "/dev/term", 0755 }, 1520Sstevel@tonic-gate }; 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * A list of devices which should be symlinked to /dev/zconsole. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate struct symlink_info { 1590Sstevel@tonic-gate char *sl_source; 1600Sstevel@tonic-gate char *sl_target; 1610Sstevel@tonic-gate }; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * The "source" paths are relative to the zonepath 1650Sstevel@tonic-gate */ 1660Sstevel@tonic-gate static struct symlink_info dev_symlinks[] = { 1670Sstevel@tonic-gate { "/dev/stderr", "./fd/2" }, 1680Sstevel@tonic-gate { "/dev/stdin", "./fd/0" }, 1690Sstevel@tonic-gate { "/dev/stdout", "./fd/1" }, 1700Sstevel@tonic-gate { "/dev/dtremote", "/dev/null" }, 1710Sstevel@tonic-gate { "/dev/console", "zconsole" }, 1720Sstevel@tonic-gate { "/dev/syscon", "zconsole" }, 1730Sstevel@tonic-gate { "/dev/sysmsg", "zconsole" }, 1740Sstevel@tonic-gate { "/dev/systty", "zconsole" }, 1750Sstevel@tonic-gate { "/dev/msglog", "zconsole" }, 1760Sstevel@tonic-gate }; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate /* for routing socket */ 1790Sstevel@tonic-gate static int rts_seqno = 0; 1800Sstevel@tonic-gate 181766Scarlsonj /* mangled zone name when mounting in an alternate root environment */ 182766Scarlsonj static char kernzone[ZONENAME_MAX]; 183766Scarlsonj 184766Scarlsonj /* array of cached mount entries for resolve_lofs */ 185766Scarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max; 186766Scarlsonj 1871676Sjpk /* for Trusted Extensions */ 1881676Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *); 1891676Sjpk static int tsol_mounts(zlog_t *, char *, char *); 1901676Sjpk static void tsol_unmounts(zlog_t *, char *); 1911676Sjpk static m_label_t *zlabel = NULL; 1921676Sjpk static m_label_t *zid_label = NULL; 1931676Sjpk static priv_set_t *zprivs = NULL; 1941676Sjpk 1950Sstevel@tonic-gate /* from libsocket, not in any header file */ 1960Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* 199766Scarlsonj * An optimization for build_mnttable: reallocate (and potentially copy the 200766Scarlsonj * data) only once every N times through the loop. 201766Scarlsonj */ 202766Scarlsonj #define MNTTAB_HUNK 32 203766Scarlsonj 204766Scarlsonj /* 2050Sstevel@tonic-gate * Private autofs system call 2060Sstevel@tonic-gate */ 2070Sstevel@tonic-gate extern int _autofssys(int, void *); 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate static int 2100Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid) 2110Sstevel@tonic-gate { 2120Sstevel@tonic-gate /* 2130Sstevel@tonic-gate * Ask autofs to unmount all trigger nodes in the given zone. 2140Sstevel@tonic-gate */ 2150Sstevel@tonic-gate return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid)); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 218766Scarlsonj static void 219766Scarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem) 220766Scarlsonj { 221766Scarlsonj uint_t i; 222766Scarlsonj 223766Scarlsonj if (mnt_array == NULL) 224766Scarlsonj return; 225766Scarlsonj for (i = 0; i < nelem; i++) { 226766Scarlsonj free(mnt_array[i].mnt_mountp); 227766Scarlsonj free(mnt_array[i].mnt_fstype); 228766Scarlsonj free(mnt_array[i].mnt_special); 229766Scarlsonj free(mnt_array[i].mnt_mntopts); 230766Scarlsonj assert(mnt_array[i].mnt_time == NULL); 231766Scarlsonj } 232766Scarlsonj free(mnt_array); 233766Scarlsonj } 234766Scarlsonj 235766Scarlsonj /* 236766Scarlsonj * Build the mount table for the zone rooted at "zroot", storing the resulting 237766Scarlsonj * array of struct mnttabs in "mnt_arrayp" and the number of elements in the 238766Scarlsonj * array in "nelemp". 239766Scarlsonj */ 240766Scarlsonj static int 241766Scarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab, 242766Scarlsonj struct mnttab **mnt_arrayp, uint_t *nelemp) 243766Scarlsonj { 244766Scarlsonj struct mnttab mnt; 245766Scarlsonj struct mnttab *mnts; 246766Scarlsonj struct mnttab *mnp; 247766Scarlsonj uint_t nmnt; 248766Scarlsonj 249766Scarlsonj rewind(mnttab); 250766Scarlsonj resetmnttab(mnttab); 251766Scarlsonj nmnt = 0; 252766Scarlsonj mnts = NULL; 253766Scarlsonj while (getmntent(mnttab, &mnt) == 0) { 254766Scarlsonj struct mnttab *tmp_array; 255766Scarlsonj 256766Scarlsonj if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0) 257766Scarlsonj continue; 258766Scarlsonj if (nmnt % MNTTAB_HUNK == 0) { 259766Scarlsonj tmp_array = realloc(mnts, 260766Scarlsonj (nmnt + MNTTAB_HUNK) * sizeof (*mnts)); 261766Scarlsonj if (tmp_array == NULL) { 262766Scarlsonj free_mnttable(mnts, nmnt); 263766Scarlsonj return (-1); 264766Scarlsonj } 265766Scarlsonj mnts = tmp_array; 266766Scarlsonj } 267766Scarlsonj mnp = &mnts[nmnt++]; 268766Scarlsonj 269766Scarlsonj /* 270766Scarlsonj * Zero out any fields we're not using. 271766Scarlsonj */ 272766Scarlsonj (void) memset(mnp, 0, sizeof (*mnp)); 273766Scarlsonj 274766Scarlsonj if (mnt.mnt_special != NULL) 275766Scarlsonj mnp->mnt_special = strdup(mnt.mnt_special); 276766Scarlsonj if (mnt.mnt_mntopts != NULL) 277766Scarlsonj mnp->mnt_mntopts = strdup(mnt.mnt_mntopts); 278766Scarlsonj mnp->mnt_mountp = strdup(mnt.mnt_mountp); 279766Scarlsonj mnp->mnt_fstype = strdup(mnt.mnt_fstype); 280766Scarlsonj if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) || 281766Scarlsonj (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) || 282766Scarlsonj mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) { 283766Scarlsonj zerror(zlogp, B_TRUE, "memory allocation failed"); 284766Scarlsonj free_mnttable(mnts, nmnt); 285766Scarlsonj return (-1); 286766Scarlsonj } 287766Scarlsonj } 288766Scarlsonj *mnt_arrayp = mnts; 289766Scarlsonj *nelemp = nmnt; 290766Scarlsonj return (0); 291766Scarlsonj } 292766Scarlsonj 293766Scarlsonj /* 294766Scarlsonj * This is an optimization. The resolve_lofs function is used quite frequently 295766Scarlsonj * to manipulate file paths, and on a machine with a large number of zones, 296766Scarlsonj * there will be a huge number of mounted file systems. Thus, we trigger a 297766Scarlsonj * reread of the list of mount points 298766Scarlsonj */ 299766Scarlsonj static void 300766Scarlsonj lofs_discard_mnttab(void) 301766Scarlsonj { 302766Scarlsonj free_mnttable(resolve_lofs_mnts, 303766Scarlsonj resolve_lofs_mnt_max - resolve_lofs_mnts); 304766Scarlsonj resolve_lofs_mnts = resolve_lofs_mnt_max = NULL; 305766Scarlsonj } 306766Scarlsonj 307766Scarlsonj static int 308766Scarlsonj lofs_read_mnttab(zlog_t *zlogp) 309766Scarlsonj { 310766Scarlsonj FILE *mnttab; 311766Scarlsonj uint_t nmnts; 312766Scarlsonj 313766Scarlsonj if ((mnttab = fopen(MNTTAB, "r")) == NULL) 314766Scarlsonj return (-1); 315766Scarlsonj if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts, 316766Scarlsonj &nmnts) == -1) { 317766Scarlsonj (void) fclose(mnttab); 318766Scarlsonj return (-1); 319766Scarlsonj } 320766Scarlsonj (void) fclose(mnttab); 321766Scarlsonj resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts; 322766Scarlsonj return (0); 323766Scarlsonj } 324766Scarlsonj 325766Scarlsonj /* 326766Scarlsonj * This function loops over potential loopback mounts and symlinks in a given 327766Scarlsonj * path and resolves them all down to an absolute path. 328766Scarlsonj */ 329766Scarlsonj static void 330766Scarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen) 331766Scarlsonj { 332766Scarlsonj int len, arlen; 333766Scarlsonj const char *altroot; 334766Scarlsonj char tmppath[MAXPATHLEN]; 335766Scarlsonj boolean_t outside_altroot; 336766Scarlsonj 337766Scarlsonj if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1) 338766Scarlsonj return; 339766Scarlsonj tmppath[len] = '\0'; 340766Scarlsonj (void) strlcpy(path, tmppath, sizeof (tmppath)); 341766Scarlsonj 342766Scarlsonj /* This happens once per zoneadmd operation. */ 343766Scarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 344766Scarlsonj return; 345766Scarlsonj 346766Scarlsonj altroot = zonecfg_get_root(); 347766Scarlsonj arlen = strlen(altroot); 348766Scarlsonj outside_altroot = B_FALSE; 349766Scarlsonj for (;;) { 350766Scarlsonj struct mnttab *mnp; 351766Scarlsonj 352766Scarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 353766Scarlsonj mnp++) { 354766Scarlsonj if (mnp->mnt_fstype == NULL || 355766Scarlsonj mnp->mnt_mountp == NULL || 356766Scarlsonj mnp->mnt_special == NULL || 357766Scarlsonj strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0) 358766Scarlsonj continue; 359766Scarlsonj len = strlen(mnp->mnt_mountp); 360766Scarlsonj if (strncmp(mnp->mnt_mountp, path, len) == 0 && 361766Scarlsonj (path[len] == '/' || path[len] == '\0')) 362766Scarlsonj break; 363766Scarlsonj } 364766Scarlsonj if (mnp >= resolve_lofs_mnt_max) 365766Scarlsonj break; 366766Scarlsonj if (outside_altroot) { 367766Scarlsonj char *cp; 368766Scarlsonj int olen = sizeof (MNTOPT_RO) - 1; 369766Scarlsonj 370766Scarlsonj /* 371766Scarlsonj * If we run into a read-only mount outside of the 372766Scarlsonj * alternate root environment, then the user doesn't 373766Scarlsonj * want this path to be made read-write. 374766Scarlsonj */ 375766Scarlsonj if (mnp->mnt_mntopts != NULL && 376766Scarlsonj (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) != 377766Scarlsonj NULL && 378766Scarlsonj (cp == mnp->mnt_mntopts || cp[-1] == ',') && 379766Scarlsonj (cp[olen] == '\0' || cp[olen] == ',')) { 380766Scarlsonj break; 381766Scarlsonj } 382766Scarlsonj } else if (arlen > 0 && 383766Scarlsonj (strncmp(mnp->mnt_special, altroot, arlen) != 0 || 384766Scarlsonj (mnp->mnt_special[arlen] != '\0' && 385766Scarlsonj mnp->mnt_special[arlen] != '/'))) { 386766Scarlsonj outside_altroot = B_TRUE; 387766Scarlsonj } 388766Scarlsonj /* use temporary buffer because new path might be longer */ 389766Scarlsonj (void) snprintf(tmppath, sizeof (tmppath), "%s%s", 390766Scarlsonj mnp->mnt_special, path + len); 391766Scarlsonj if ((len = resolvepath(tmppath, path, pathlen)) == -1) 392766Scarlsonj break; 393766Scarlsonj path[len] = '\0'; 394766Scarlsonj } 395766Scarlsonj } 396766Scarlsonj 397766Scarlsonj /* 398766Scarlsonj * For a regular mount, check if a replacement lofs mount is needed because the 399766Scarlsonj * referenced device is already mounted somewhere. 400766Scarlsonj */ 401766Scarlsonj static int 402766Scarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr) 403766Scarlsonj { 404766Scarlsonj struct mnttab *mnp; 405766Scarlsonj zone_fsopt_t *optptr, *onext; 406766Scarlsonj 407766Scarlsonj /* This happens once per zoneadmd operation. */ 408766Scarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 409766Scarlsonj return (-1); 410766Scarlsonj 411766Scarlsonj /* 412766Scarlsonj * If this special node isn't already in use, then it's ours alone; 413766Scarlsonj * no need to worry about conflicting mounts. 414766Scarlsonj */ 415766Scarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 416766Scarlsonj mnp++) { 417766Scarlsonj if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0) 418766Scarlsonj break; 419766Scarlsonj } 420766Scarlsonj if (mnp >= resolve_lofs_mnt_max) 421766Scarlsonj return (0); 422766Scarlsonj 423766Scarlsonj /* 424766Scarlsonj * Convert this duplicate mount into a lofs mount. 425766Scarlsonj */ 426766Scarlsonj (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp, 427766Scarlsonj sizeof (fsptr->zone_fs_special)); 428766Scarlsonj (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS, 429766Scarlsonj sizeof (fsptr->zone_fs_type)); 430766Scarlsonj fsptr->zone_fs_raw[0] = '\0'; 431766Scarlsonj 432766Scarlsonj /* 433766Scarlsonj * Discard all but one of the original options and set that to be the 434766Scarlsonj * same set of options used for inherit package directory resources. 435766Scarlsonj */ 436766Scarlsonj optptr = fsptr->zone_fs_options; 437766Scarlsonj if (optptr == NULL) { 438766Scarlsonj optptr = malloc(sizeof (*optptr)); 439766Scarlsonj if (optptr == NULL) { 440766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount %s", 441766Scarlsonj fsptr->zone_fs_dir); 442766Scarlsonj return (-1); 443766Scarlsonj } 444766Scarlsonj } else { 445766Scarlsonj while ((onext = optptr->zone_fsopt_next) != NULL) { 446766Scarlsonj optptr->zone_fsopt_next = onext->zone_fsopt_next; 447766Scarlsonj free(onext); 448766Scarlsonj } 449766Scarlsonj } 450766Scarlsonj (void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS); 451766Scarlsonj optptr->zone_fsopt_next = NULL; 452766Scarlsonj fsptr->zone_fs_options = optptr; 453766Scarlsonj return (0); 454766Scarlsonj } 455766Scarlsonj 4560Sstevel@tonic-gate static int 4570Sstevel@tonic-gate make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode) 4580Sstevel@tonic-gate { 4590Sstevel@tonic-gate char path[MAXPATHLEN]; 4600Sstevel@tonic-gate struct stat st; 4610Sstevel@tonic-gate 4620Sstevel@tonic-gate if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) > 4630Sstevel@tonic-gate sizeof (path)) { 4640Sstevel@tonic-gate zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix, 4650Sstevel@tonic-gate subdir); 4660Sstevel@tonic-gate return (-1); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate if (lstat(path, &st) == 0) { 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * We don't check the file mode since presumably the zone 4720Sstevel@tonic-gate * administrator may have had good reason to change the mode, 4730Sstevel@tonic-gate * and we don't need to second guess him. 4740Sstevel@tonic-gate */ 4750Sstevel@tonic-gate if (!S_ISDIR(st.st_mode)) { 4761676Sjpk if (is_system_labeled() && 4771676Sjpk S_ISREG(st.st_mode)) { 4781676Sjpk /* 4791676Sjpk * The need to mount readonly copies of 4801676Sjpk * global zone /etc/ files is unique to 4811676Sjpk * Trusted Extensions. 4821676Sjpk */ 4831676Sjpk if (strncmp(subdir, "/etc/", 4841676Sjpk strlen("/etc/")) != 0) { 4851676Sjpk zerror(zlogp, B_FALSE, 4861676Sjpk "%s is not in /etc", path); 4871676Sjpk return (-1); 4881676Sjpk } 4891676Sjpk } else { 4901676Sjpk zerror(zlogp, B_FALSE, 4911676Sjpk "%s is not a directory", path); 4921676Sjpk return (-1); 4931676Sjpk } 4940Sstevel@tonic-gate } 4950Sstevel@tonic-gate } else if (mkdirp(path, mode) != 0) { 4960Sstevel@tonic-gate if (errno == EROFS) 4970Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on " 4980Sstevel@tonic-gate "a read-only file system in this local zone.\nMake " 4990Sstevel@tonic-gate "sure %s exists in the global zone.", path, subdir); 5000Sstevel@tonic-gate else 5010Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mkdirp of %s failed", path); 5020Sstevel@tonic-gate return (-1); 5030Sstevel@tonic-gate } 5040Sstevel@tonic-gate return (0); 5050Sstevel@tonic-gate } 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate /* 5080Sstevel@tonic-gate * Make /dev and various directories underneath it. 5090Sstevel@tonic-gate */ 5100Sstevel@tonic-gate static int 5110Sstevel@tonic-gate make_dev_dirs(zlog_t *zlogp, const char *zonepath) 5120Sstevel@tonic-gate { 5130Sstevel@tonic-gate int i; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate for (i = 0; i < sizeof (dev_dirs) / sizeof (struct dir_info); i++) { 5160Sstevel@tonic-gate if (make_one_dir(zlogp, zonepath, dev_dirs[i].dir_name, 5170Sstevel@tonic-gate dev_dirs[i].dir_mode) != 0) 5180Sstevel@tonic-gate return (-1); 5190Sstevel@tonic-gate } 5200Sstevel@tonic-gate return (0); 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate /* 5240Sstevel@tonic-gate * Make various sym-links underneath /dev. 5250Sstevel@tonic-gate */ 5260Sstevel@tonic-gate static int 5270Sstevel@tonic-gate make_dev_links(zlog_t *zlogp, char *zonepath) 5280Sstevel@tonic-gate { 5290Sstevel@tonic-gate int i; 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate for (i = 0; i < sizeof (dev_symlinks) / sizeof (struct symlink_info); 5320Sstevel@tonic-gate i++) { 5330Sstevel@tonic-gate char dev[MAXPATHLEN]; 5340Sstevel@tonic-gate struct stat st; 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate (void) snprintf(dev, sizeof (dev), "%s%s", zonepath, 5370Sstevel@tonic-gate dev_symlinks[i].sl_source); 5380Sstevel@tonic-gate if (lstat(dev, &st) == 0) { 5390Sstevel@tonic-gate /* 5400Sstevel@tonic-gate * Try not to call unlink(2) on directories, since that 5410Sstevel@tonic-gate * makes UFS unhappy. 5420Sstevel@tonic-gate */ 5430Sstevel@tonic-gate if (S_ISDIR(st.st_mode)) { 5440Sstevel@tonic-gate zerror(zlogp, B_FALSE, "symlink path %s is a " 5450Sstevel@tonic-gate "directory", dev_symlinks[i].sl_source); 5460Sstevel@tonic-gate return (-1); 5470Sstevel@tonic-gate } 5480Sstevel@tonic-gate (void) unlink(dev); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate if (symlink(dev_symlinks[i].sl_target, dev) != 0) { 551766Scarlsonj zerror(zlogp, B_TRUE, "could not setup %s->%s symlink", 552766Scarlsonj dev_symlinks[i].sl_source, 553766Scarlsonj dev_symlinks[i].sl_target); 5540Sstevel@tonic-gate return (-1); 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate return (0); 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate /* 5610Sstevel@tonic-gate * Create various directories and sym-links under /dev. 5620Sstevel@tonic-gate */ 5630Sstevel@tonic-gate static int 5640Sstevel@tonic-gate create_dev_files(zlog_t *zlogp) 5650Sstevel@tonic-gate { 5660Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 5690Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 5700Sstevel@tonic-gate return (-1); 5710Sstevel@tonic-gate } 572766Scarlsonj if (zonecfg_in_alt_root()) 573766Scarlsonj resolve_lofs(zlogp, zonepath, sizeof (zonepath)); 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate if (make_dev_dirs(zlogp, zonepath) != 0) 5760Sstevel@tonic-gate return (-1); 5770Sstevel@tonic-gate if (make_dev_links(zlogp, zonepath) != 0) 5780Sstevel@tonic-gate return (-1); 5790Sstevel@tonic-gate return (0); 5800Sstevel@tonic-gate } 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate static void 5830Sstevel@tonic-gate free_remote_fstypes(char **types) 5840Sstevel@tonic-gate { 5850Sstevel@tonic-gate uint_t i; 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate if (types == NULL) 5880Sstevel@tonic-gate return; 5890Sstevel@tonic-gate for (i = 0; types[i] != NULL; i++) 5900Sstevel@tonic-gate free(types[i]); 5910Sstevel@tonic-gate free(types); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate static char ** 5950Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp) 5960Sstevel@tonic-gate { 5970Sstevel@tonic-gate char **types = NULL; 5980Sstevel@tonic-gate FILE *fp; 5990Sstevel@tonic-gate char buf[MAXPATHLEN]; 6000Sstevel@tonic-gate char fstype[MAXPATHLEN]; 6010Sstevel@tonic-gate uint_t lines = 0; 6020Sstevel@tonic-gate uint_t i; 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate if ((fp = fopen(DFSTYPES, "r")) == NULL) { 6050Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES); 6060Sstevel@tonic-gate return (NULL); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate /* 6090Sstevel@tonic-gate * Count the number of lines 6100Sstevel@tonic-gate */ 6110Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) 6120Sstevel@tonic-gate lines++; 6130Sstevel@tonic-gate if (lines == 0) /* didn't read anything; empty file */ 6140Sstevel@tonic-gate goto out; 6150Sstevel@tonic-gate rewind(fp); 6160Sstevel@tonic-gate /* 6170Sstevel@tonic-gate * Allocate enough space for a NULL-terminated array. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate types = calloc(lines + 1, sizeof (char *)); 6200Sstevel@tonic-gate if (types == NULL) { 6210Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 6220Sstevel@tonic-gate goto out; 6230Sstevel@tonic-gate } 6240Sstevel@tonic-gate i = 0; 6250Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) { 6260Sstevel@tonic-gate /* LINTED - fstype is big enough to hold buf */ 6270Sstevel@tonic-gate if (sscanf(buf, "%s", fstype) == 0) { 6280Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES); 6290Sstevel@tonic-gate free_remote_fstypes(types); 6300Sstevel@tonic-gate types = NULL; 6310Sstevel@tonic-gate goto out; 6320Sstevel@tonic-gate } 6330Sstevel@tonic-gate types[i] = strdup(fstype); 6340Sstevel@tonic-gate if (types[i] == NULL) { 6350Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 6360Sstevel@tonic-gate free_remote_fstypes(types); 6370Sstevel@tonic-gate types = NULL; 6380Sstevel@tonic-gate goto out; 6390Sstevel@tonic-gate } 6400Sstevel@tonic-gate i++; 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate out: 6430Sstevel@tonic-gate (void) fclose(fp); 6440Sstevel@tonic-gate return (types); 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate static boolean_t 6480Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes) 6490Sstevel@tonic-gate { 6500Sstevel@tonic-gate uint_t i; 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate if (remote_fstypes == NULL) 6530Sstevel@tonic-gate return (B_FALSE); 6540Sstevel@tonic-gate for (i = 0; remote_fstypes[i] != NULL; i++) { 6550Sstevel@tonic-gate if (strcmp(remote_fstypes[i], fstype) == 0) 6560Sstevel@tonic-gate return (B_TRUE); 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate return (B_FALSE); 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate 661766Scarlsonj /* 662766Scarlsonj * This converts a zone root path (normally of the form .../root) to a Live 663766Scarlsonj * Upgrade scratch zone root (of the form .../lu). 664766Scarlsonj */ 6650Sstevel@tonic-gate static void 666766Scarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved) 6670Sstevel@tonic-gate { 668766Scarlsonj if (!isresolved && zonecfg_in_alt_root()) 669766Scarlsonj resolve_lofs(zlogp, zroot, zrootlen); 670766Scarlsonj (void) strcpy(strrchr(zroot, '/') + 1, "lu"); 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * The general strategy for unmounting filesystems is as follows: 6750Sstevel@tonic-gate * 6760Sstevel@tonic-gate * - Remote filesystems may be dead, and attempting to contact them as 6770Sstevel@tonic-gate * part of a regular unmount may hang forever; we want to always try to 6780Sstevel@tonic-gate * forcibly unmount such filesystems and only fall back to regular 6790Sstevel@tonic-gate * unmounts if the filesystem doesn't support forced unmounts. 6800Sstevel@tonic-gate * 6810Sstevel@tonic-gate * - We don't want to unnecessarily corrupt metadata on local 6820Sstevel@tonic-gate * filesystems (ie UFS), so we want to start off with graceful unmounts, 6830Sstevel@tonic-gate * and only escalate to doing forced unmounts if we get stuck. 6840Sstevel@tonic-gate * 6850Sstevel@tonic-gate * We start off walking backwards through the mount table. This doesn't 6860Sstevel@tonic-gate * give us strict ordering but ensures that we try to unmount submounts 6870Sstevel@tonic-gate * first. We thus limit the number of failed umount2(2) calls. 6880Sstevel@tonic-gate * 6890Sstevel@tonic-gate * The mechanism for determining if we're stuck is to count the number 6900Sstevel@tonic-gate * of failed unmounts each iteration through the mount table. This 6910Sstevel@tonic-gate * gives us an upper bound on the number of filesystems which remain 6920Sstevel@tonic-gate * mounted (autofs trigger nodes are dealt with separately). If at the 6930Sstevel@tonic-gate * end of one unmount+autofs_cleanup cycle we still have the same number 6940Sstevel@tonic-gate * of mounts that we started out with, we're stuck and try a forced 6950Sstevel@tonic-gate * unmount. If that fails (filesystem doesn't support forced unmounts) 6960Sstevel@tonic-gate * then we bail and are unable to teardown the zone. If it succeeds, 6970Sstevel@tonic-gate * we're no longer stuck so we continue with our policy of trying 6980Sstevel@tonic-gate * graceful mounts first. 6990Sstevel@tonic-gate * 7000Sstevel@tonic-gate * Zone must be down (ie, no processes or threads active). 7010Sstevel@tonic-gate */ 7020Sstevel@tonic-gate static int 703766Scarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd) 7040Sstevel@tonic-gate { 7050Sstevel@tonic-gate int error = 0; 7060Sstevel@tonic-gate FILE *mnttab; 7070Sstevel@tonic-gate struct mnttab *mnts; 7080Sstevel@tonic-gate uint_t nmnt; 7090Sstevel@tonic-gate char zroot[MAXPATHLEN + 1]; 7100Sstevel@tonic-gate size_t zrootlen; 7110Sstevel@tonic-gate uint_t oldcount = UINT_MAX; 7120Sstevel@tonic-gate boolean_t stuck = B_FALSE; 7130Sstevel@tonic-gate char **remote_fstypes = NULL; 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 7160Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 7170Sstevel@tonic-gate return (-1); 7180Sstevel@tonic-gate } 719766Scarlsonj if (unmount_cmd) 720766Scarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate (void) strcat(zroot, "/"); 7230Sstevel@tonic-gate zrootlen = strlen(zroot); 7240Sstevel@tonic-gate 7251676Sjpk /* 7261676Sjpk * For Trusted Extensions unmount each higher level zone's mount 7271676Sjpk * of our zone's /export/home 7281676Sjpk */ 7291769Scarlsonj if (!unmount_cmd) 7301769Scarlsonj tsol_unmounts(zlogp, zone_name); 7311676Sjpk 7320Sstevel@tonic-gate if ((mnttab = fopen(MNTTAB, "r")) == NULL) { 7330Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB); 7340Sstevel@tonic-gate return (-1); 7350Sstevel@tonic-gate } 7360Sstevel@tonic-gate /* 7370Sstevel@tonic-gate * Use our hacky mntfs ioctl so we see everything, even mounts with 7380Sstevel@tonic-gate * MS_NOMNTTAB. 7390Sstevel@tonic-gate */ 7400Sstevel@tonic-gate if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 7410Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB); 7420Sstevel@tonic-gate error++; 7430Sstevel@tonic-gate goto out; 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate /* 7470Sstevel@tonic-gate * Build the list of remote fstypes so we know which ones we 7480Sstevel@tonic-gate * should forcibly unmount. 7490Sstevel@tonic-gate */ 7500Sstevel@tonic-gate remote_fstypes = get_remote_fstypes(zlogp); 7510Sstevel@tonic-gate for (; /* ever */; ) { 7520Sstevel@tonic-gate uint_t newcount = 0; 7530Sstevel@tonic-gate boolean_t unmounted; 7540Sstevel@tonic-gate struct mnttab *mnp; 7550Sstevel@tonic-gate char *path; 7560Sstevel@tonic-gate uint_t i; 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate mnts = NULL; 7590Sstevel@tonic-gate nmnt = 0; 7600Sstevel@tonic-gate /* 7610Sstevel@tonic-gate * MNTTAB gives us a way to walk through mounted 7620Sstevel@tonic-gate * filesystems; we need to be able to walk them in 7630Sstevel@tonic-gate * reverse order, so we build a list of all mounted 7640Sstevel@tonic-gate * filesystems. 7650Sstevel@tonic-gate */ 7660Sstevel@tonic-gate if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts, 7670Sstevel@tonic-gate &nmnt) != 0) { 7680Sstevel@tonic-gate error++; 7690Sstevel@tonic-gate goto out; 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate for (i = 0; i < nmnt; i++) { 7720Sstevel@tonic-gate mnp = &mnts[nmnt - i - 1]; /* access in reverse order */ 7730Sstevel@tonic-gate path = mnp->mnt_mountp; 7740Sstevel@tonic-gate unmounted = B_FALSE; 7750Sstevel@tonic-gate /* 7760Sstevel@tonic-gate * Try forced unmount first for remote filesystems. 7770Sstevel@tonic-gate * 7780Sstevel@tonic-gate * Not all remote filesystems support forced unmounts, 7790Sstevel@tonic-gate * so if this fails (ENOTSUP) we'll continue on 7800Sstevel@tonic-gate * and try a regular unmount. 7810Sstevel@tonic-gate */ 7820Sstevel@tonic-gate if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) { 7830Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) 7840Sstevel@tonic-gate unmounted = B_TRUE; 7850Sstevel@tonic-gate } 7860Sstevel@tonic-gate /* 7870Sstevel@tonic-gate * Try forced unmount if we're stuck. 7880Sstevel@tonic-gate */ 7890Sstevel@tonic-gate if (stuck) { 7900Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) { 7910Sstevel@tonic-gate unmounted = B_TRUE; 7920Sstevel@tonic-gate stuck = B_FALSE; 7930Sstevel@tonic-gate } else { 7940Sstevel@tonic-gate /* 7950Sstevel@tonic-gate * The first failure indicates a 7960Sstevel@tonic-gate * mount we won't be able to get 7970Sstevel@tonic-gate * rid of automatically, so we 7980Sstevel@tonic-gate * bail. 7990Sstevel@tonic-gate */ 8000Sstevel@tonic-gate error++; 8010Sstevel@tonic-gate zerror(zlogp, B_FALSE, 8020Sstevel@tonic-gate "unable to unmount '%s'", path); 8030Sstevel@tonic-gate free_mnttable(mnts, nmnt); 8040Sstevel@tonic-gate goto out; 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate /* 8080Sstevel@tonic-gate * Try regular unmounts for everything else. 8090Sstevel@tonic-gate */ 8100Sstevel@tonic-gate if (!unmounted && umount2(path, 0) != 0) 8110Sstevel@tonic-gate newcount++; 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate free_mnttable(mnts, nmnt); 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate if (newcount == 0) 8160Sstevel@tonic-gate break; 8170Sstevel@tonic-gate if (newcount >= oldcount) { 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * Last round didn't unmount anything; we're stuck and 8200Sstevel@tonic-gate * should start trying forced unmounts. 8210Sstevel@tonic-gate */ 8220Sstevel@tonic-gate stuck = B_TRUE; 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate oldcount = newcount; 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate /* 8270Sstevel@tonic-gate * Autofs doesn't let you unmount its trigger nodes from 8280Sstevel@tonic-gate * userland so we have to tell the kernel to cleanup for us. 8290Sstevel@tonic-gate */ 8300Sstevel@tonic-gate if (autofs_cleanup(zoneid) != 0) { 8310Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to remove autofs nodes"); 8320Sstevel@tonic-gate error++; 8330Sstevel@tonic-gate goto out; 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate } 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate out: 8380Sstevel@tonic-gate free_remote_fstypes(remote_fstypes); 8390Sstevel@tonic-gate (void) fclose(mnttab); 8400Sstevel@tonic-gate return (error ? -1 : 0); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate static int 8440Sstevel@tonic-gate fs_compare(const void *m1, const void *m2) 8450Sstevel@tonic-gate { 8460Sstevel@tonic-gate struct zone_fstab *i = (struct zone_fstab *)m1; 8470Sstevel@tonic-gate struct zone_fstab *j = (struct zone_fstab *)m2; 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate return (strcmp(i->zone_fs_dir, j->zone_fs_dir)); 8500Sstevel@tonic-gate } 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate /* 8530Sstevel@tonic-gate * Fork and exec (and wait for) the mentioned binary with the provided 8540Sstevel@tonic-gate * arguments. Returns (-1) if something went wrong with fork(2) or exec(2), 8550Sstevel@tonic-gate * returns the exit status otherwise. 8560Sstevel@tonic-gate * 8570Sstevel@tonic-gate * If we were unable to exec the provided pathname (for whatever 8580Sstevel@tonic-gate * reason), we return the special token ZEXIT_EXEC. The current value 8590Sstevel@tonic-gate * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the 8600Sstevel@tonic-gate * consumers of this function; any future consumers must make sure this 8610Sstevel@tonic-gate * remains the case. 8620Sstevel@tonic-gate */ 8630Sstevel@tonic-gate static int 8640Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[]) 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate pid_t child_pid; 8670Sstevel@tonic-gate int child_status = 0; 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate /* 8700Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 8710Sstevel@tonic-gate */ 8720Sstevel@tonic-gate (void) mutex_lock(&msglock); 8730Sstevel@tonic-gate child_pid = fork(); 8740Sstevel@tonic-gate (void) mutex_unlock(&msglock); 8750Sstevel@tonic-gate if (child_pid == -1) { 8760Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]); 8770Sstevel@tonic-gate return (-1); 8780Sstevel@tonic-gate } else if (child_pid == 0) { 8790Sstevel@tonic-gate closefrom(0); 8801915Sgjelinek /* redirect stdin, stdout & stderr to /dev/null */ 8811915Sgjelinek (void) open("/dev/null", O_RDONLY); /* stdin */ 8821915Sgjelinek (void) open("/dev/null", O_WRONLY); /* stdout */ 8831915Sgjelinek (void) open("/dev/null", O_WRONLY); /* stderr */ 8840Sstevel@tonic-gate (void) execv(path, argv); 8850Sstevel@tonic-gate /* 8860Sstevel@tonic-gate * Since we are in the child, there is no point calling zerror() 8870Sstevel@tonic-gate * since there is nobody waiting to consume it. So exit with a 8880Sstevel@tonic-gate * special code that the parent will recognize and call zerror() 8890Sstevel@tonic-gate * accordingly. 8900Sstevel@tonic-gate */ 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate _exit(ZEXIT_EXEC); 8930Sstevel@tonic-gate } else { 8940Sstevel@tonic-gate (void) waitpid(child_pid, &child_status, 0); 8950Sstevel@tonic-gate } 8960Sstevel@tonic-gate 8970Sstevel@tonic-gate if (WIFSIGNALED(child_status)) { 8980Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 8990Sstevel@tonic-gate "signal %d", path, WTERMSIG(child_status)); 9000Sstevel@tonic-gate return (-1); 9010Sstevel@tonic-gate } 9020Sstevel@tonic-gate assert(WIFEXITED(child_status)); 9030Sstevel@tonic-gate if (WEXITSTATUS(child_status) == ZEXIT_EXEC) { 9040Sstevel@tonic-gate zerror(zlogp, B_FALSE, "failed to exec %s", path); 9050Sstevel@tonic-gate return (-1); 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate return (WEXITSTATUS(child_status)); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate static int 9110Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev) 9120Sstevel@tonic-gate { 9130Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 9140Sstevel@tonic-gate char *argv[4]; 9150Sstevel@tonic-gate int status; 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * We could alternatively have called /usr/sbin/fsck -F <fstype>, but 9190Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything. 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype) 9220Sstevel@tonic-gate > sizeof (cmdbuf)) { 9230Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 9240Sstevel@tonic-gate return (-1); 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate argv[0] = "fsck"; 9280Sstevel@tonic-gate argv[1] = "-m"; 9290Sstevel@tonic-gate argv[2] = (char *)rawdev; 9300Sstevel@tonic-gate argv[3] = NULL; 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv); 9330Sstevel@tonic-gate if (status == 0 || status == -1) 9340Sstevel@tonic-gate return (status); 9350Sstevel@tonic-gate zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; " 9360Sstevel@tonic-gate "run fsck manually", rawdev, status); 9370Sstevel@tonic-gate return (-1); 9380Sstevel@tonic-gate } 9390Sstevel@tonic-gate 9400Sstevel@tonic-gate static int 9410Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts, 9420Sstevel@tonic-gate const char *special, const char *directory) 9430Sstevel@tonic-gate { 9440Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 9450Sstevel@tonic-gate char *argv[6]; 9460Sstevel@tonic-gate int status; 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate /* 9490Sstevel@tonic-gate * We could alternatively have called /usr/sbin/mount -F <fstype>, but 9500Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything. 9510Sstevel@tonic-gate */ 9520Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype) 9530Sstevel@tonic-gate > sizeof (cmdbuf)) { 9540Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 9550Sstevel@tonic-gate return (-1); 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate argv[0] = "mount"; 9580Sstevel@tonic-gate if (opts[0] == '\0') { 9590Sstevel@tonic-gate argv[1] = (char *)special; 9600Sstevel@tonic-gate argv[2] = (char *)directory; 9610Sstevel@tonic-gate argv[3] = NULL; 9620Sstevel@tonic-gate } else { 9630Sstevel@tonic-gate argv[1] = "-o"; 9640Sstevel@tonic-gate argv[2] = (char *)opts; 9650Sstevel@tonic-gate argv[3] = (char *)special; 9660Sstevel@tonic-gate argv[4] = (char *)directory; 9670Sstevel@tonic-gate argv[5] = NULL; 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv); 9710Sstevel@tonic-gate if (status == 0 || status == -1) 9720Sstevel@tonic-gate return (status); 9730Sstevel@tonic-gate if (opts[0] == '\0') 9740Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s %s %s\" " 9750Sstevel@tonic-gate "failed with exit code %d", 9760Sstevel@tonic-gate cmdbuf, special, directory, status); 9770Sstevel@tonic-gate else 9780Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" " 9790Sstevel@tonic-gate "failed with exit code %d", 9800Sstevel@tonic-gate cmdbuf, opts, special, directory, status); 9810Sstevel@tonic-gate return (-1); 9820Sstevel@tonic-gate } 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate /* 9850Sstevel@tonic-gate * Make sure if a given path exists, it is not a sym-link, and is a directory. 9860Sstevel@tonic-gate */ 9870Sstevel@tonic-gate static int 9880Sstevel@tonic-gate check_path(zlog_t *zlogp, const char *path) 9890Sstevel@tonic-gate { 9900Sstevel@tonic-gate struct stat statbuf; 9910Sstevel@tonic-gate char respath[MAXPATHLEN]; 9920Sstevel@tonic-gate int res; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate if (lstat(path, &statbuf) != 0) { 9950Sstevel@tonic-gate if (errno == ENOENT) 9960Sstevel@tonic-gate return (0); 9970Sstevel@tonic-gate zerror(zlogp, B_TRUE, "can't stat %s", path); 9980Sstevel@tonic-gate return (-1); 9990Sstevel@tonic-gate } 10000Sstevel@tonic-gate if (S_ISLNK(statbuf.st_mode)) { 10010Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is a symlink", path); 10020Sstevel@tonic-gate return (-1); 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate if (!S_ISDIR(statbuf.st_mode)) { 10051676Sjpk if (is_system_labeled() && S_ISREG(statbuf.st_mode)) { 10061676Sjpk /* 10071676Sjpk * The need to mount readonly copies of 10081676Sjpk * global zone /etc/ files is unique to 10091676Sjpk * Trusted Extensions. 10101676Sjpk * The check for /etc/ via strstr() is to 10111676Sjpk * allow paths like $ZONEROOT/etc/passwd 10121676Sjpk */ 10131676Sjpk if (strstr(path, "/etc/") == NULL) { 10141676Sjpk zerror(zlogp, B_FALSE, 10151676Sjpk "%s is not in /etc", path); 10161676Sjpk return (-1); 10171676Sjpk } 10181676Sjpk } else { 10191676Sjpk zerror(zlogp, B_FALSE, "%s is not a directory", path); 10201676Sjpk return (-1); 10211676Sjpk } 10220Sstevel@tonic-gate } 10230Sstevel@tonic-gate if ((res = resolvepath(path, respath, sizeof (respath))) == -1) { 10240Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to resolve path %s", path); 10250Sstevel@tonic-gate return (-1); 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate respath[res] = '\0'; 10280Sstevel@tonic-gate if (strcmp(path, respath) != 0) { 10290Sstevel@tonic-gate /* 10300Sstevel@tonic-gate * We don't like ".."s and "."s throwing us off 10310Sstevel@tonic-gate */ 10320Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not a canonical path", path); 10330Sstevel@tonic-gate return (-1); 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate return (0); 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate /* 10390Sstevel@tonic-gate * Check every component of rootpath/relpath. If any component fails (ie, 10400Sstevel@tonic-gate * exists but isn't the canonical path to a directory), it is returned in 10410Sstevel@tonic-gate * badpath, which is assumed to be at least of size MAXPATHLEN. 10420Sstevel@tonic-gate * 10430Sstevel@tonic-gate * Relpath must begin with '/'. 10440Sstevel@tonic-gate */ 10450Sstevel@tonic-gate static boolean_t 10460Sstevel@tonic-gate valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath) 10470Sstevel@tonic-gate { 10480Sstevel@tonic-gate char abspath[MAXPATHLEN], *slashp; 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate /* 10510Sstevel@tonic-gate * Make sure abspath has at least one '/' after its rootpath 10520Sstevel@tonic-gate * component, and ends with '/'. 10530Sstevel@tonic-gate */ 10540Sstevel@tonic-gate if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) > 10550Sstevel@tonic-gate sizeof (abspath)) { 10560Sstevel@tonic-gate zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath, 10570Sstevel@tonic-gate relpath); 10580Sstevel@tonic-gate return (B_FALSE); 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate slashp = &abspath[strlen(rootpath)]; 10620Sstevel@tonic-gate assert(*slashp == '/'); 10630Sstevel@tonic-gate do { 10640Sstevel@tonic-gate *slashp = '\0'; 10650Sstevel@tonic-gate if (check_path(zlogp, abspath) != 0) 10660Sstevel@tonic-gate return (B_FALSE); 10670Sstevel@tonic-gate *slashp = '/'; 10680Sstevel@tonic-gate slashp++; 10690Sstevel@tonic-gate } while ((slashp = strchr(slashp, '/')) != NULL); 10700Sstevel@tonic-gate return (B_TRUE); 10710Sstevel@tonic-gate } 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate static int 10740Sstevel@tonic-gate mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath) 10750Sstevel@tonic-gate { 10760Sstevel@tonic-gate char path[MAXPATHLEN]; 1077766Scarlsonj char specpath[MAXPATHLEN]; 10780Sstevel@tonic-gate char optstr[MAX_MNTOPT_STR]; 10790Sstevel@tonic-gate zone_fsopt_t *optptr; 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) { 10820Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 10830Sstevel@tonic-gate rootpath, fsptr->zone_fs_dir); 10840Sstevel@tonic-gate return (-1); 10850Sstevel@tonic-gate } 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir, 10880Sstevel@tonic-gate DEFAULT_DIR_MODE) != 0) 10890Sstevel@tonic-gate return (-1); 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", rootpath, 10920Sstevel@tonic-gate fsptr->zone_fs_dir); 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate if (strlen(fsptr->zone_fs_special) == 0) { 10950Sstevel@tonic-gate /* 10960Sstevel@tonic-gate * A zero-length special is how we distinguish IPDs from 1097766Scarlsonj * general-purpose FSs. Make sure it mounts from a place that 1098766Scarlsonj * can be seen via the alternate zone's root. 10990Sstevel@tonic-gate */ 1100766Scarlsonj if (snprintf(specpath, sizeof (specpath), "%s%s", 1101766Scarlsonj zonecfg_get_root(), fsptr->zone_fs_dir) >= 1102766Scarlsonj sizeof (specpath)) { 1103766Scarlsonj zerror(zlogp, B_FALSE, "cannot mount %s: path too " 1104766Scarlsonj "long in alternate root", fsptr->zone_fs_dir); 1105766Scarlsonj return (-1); 1106766Scarlsonj } 1107766Scarlsonj if (zonecfg_in_alt_root()) 1108766Scarlsonj resolve_lofs(zlogp, specpath, sizeof (specpath)); 11090Sstevel@tonic-gate if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, 1110766Scarlsonj specpath, path) != 0) { 11110Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to loopback mount %s", 1112766Scarlsonj specpath); 11130Sstevel@tonic-gate return (-1); 11140Sstevel@tonic-gate } 11150Sstevel@tonic-gate return (0); 11160Sstevel@tonic-gate } 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate /* 11190Sstevel@tonic-gate * In general the strategy here is to do just as much verification as 11200Sstevel@tonic-gate * necessary to avoid crashing or otherwise doing something bad; if the 11210Sstevel@tonic-gate * administrator initiated the operation via zoneadm(1m), he'll get 11220Sstevel@tonic-gate * auto-verification which will let him know what's wrong. If he 11230Sstevel@tonic-gate * modifies the zone configuration of a running zone and doesn't attempt 11240Sstevel@tonic-gate * to verify that it's OK we won't crash but won't bother trying to be 11250Sstevel@tonic-gate * too helpful either. zoneadm verify is only a couple keystrokes away. 11260Sstevel@tonic-gate */ 11270Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) { 11280Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot mount %s on %s: " 11290Sstevel@tonic-gate "invalid file-system type %s", fsptr->zone_fs_special, 11300Sstevel@tonic-gate fsptr->zone_fs_dir, fsptr->zone_fs_type); 11310Sstevel@tonic-gate return (-1); 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate /* 1135766Scarlsonj * If we're looking at an alternate root environment, then construct 1136766Scarlsonj * read-only loopback mounts as necessary. For all lofs mounts, make 1137766Scarlsonj * sure that the 'special' entry points inside the alternate root. (We 1138766Scarlsonj * don't do this with other mounts, as devfs isn't in the alternate 1139766Scarlsonj * root, and we need to assume the device environment is roughly the 1140766Scarlsonj * same.) 1141766Scarlsonj */ 1142766Scarlsonj if (zonecfg_in_alt_root()) { 1143766Scarlsonj struct stat64 st; 1144766Scarlsonj 1145766Scarlsonj if (stat64(fsptr->zone_fs_special, &st) != -1 && 1146766Scarlsonj S_ISBLK(st.st_mode) && 1147766Scarlsonj check_lofs_needed(zlogp, fsptr) == -1) 1148766Scarlsonj return (-1); 1149766Scarlsonj if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) { 1150766Scarlsonj if (snprintf(specpath, sizeof (specpath), "%s%s", 1151766Scarlsonj zonecfg_get_root(), fsptr->zone_fs_special) >= 1152766Scarlsonj sizeof (specpath)) { 1153766Scarlsonj zerror(zlogp, B_FALSE, "cannot mount %s: path " 1154766Scarlsonj "too long in alternate root", 1155766Scarlsonj fsptr->zone_fs_special); 1156766Scarlsonj return (-1); 1157766Scarlsonj } 1158766Scarlsonj resolve_lofs(zlogp, specpath, sizeof (specpath)); 1159766Scarlsonj (void) strlcpy(fsptr->zone_fs_special, specpath, 1160766Scarlsonj sizeof (fsptr->zone_fs_special)); 1161766Scarlsonj } 1162766Scarlsonj } 1163766Scarlsonj 1164766Scarlsonj /* 11650Sstevel@tonic-gate * Run 'fsck -m' if there's a device to fsck. 11660Sstevel@tonic-gate */ 11670Sstevel@tonic-gate if (fsptr->zone_fs_raw[0] != '\0' && 11680Sstevel@tonic-gate dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) 11690Sstevel@tonic-gate return (-1); 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate /* 11720Sstevel@tonic-gate * Build up mount option string. 11730Sstevel@tonic-gate */ 11740Sstevel@tonic-gate optstr[0] = '\0'; 11750Sstevel@tonic-gate if (fsptr->zone_fs_options != NULL) { 11760Sstevel@tonic-gate (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt, 11770Sstevel@tonic-gate sizeof (optstr)); 11780Sstevel@tonic-gate for (optptr = fsptr->zone_fs_options->zone_fsopt_next; 11790Sstevel@tonic-gate optptr != NULL; optptr = optptr->zone_fsopt_next) { 11800Sstevel@tonic-gate (void) strlcat(optstr, ",", sizeof (optstr)); 11810Sstevel@tonic-gate (void) strlcat(optstr, optptr->zone_fsopt_opt, 11820Sstevel@tonic-gate sizeof (optstr)); 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate } 11850Sstevel@tonic-gate return (domount(zlogp, fsptr->zone_fs_type, optstr, 11860Sstevel@tonic-gate fsptr->zone_fs_special, path)); 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate static void 11900Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem) 11910Sstevel@tonic-gate { 11920Sstevel@tonic-gate uint_t i; 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate if (fsarray == NULL) 11950Sstevel@tonic-gate return; 11960Sstevel@tonic-gate for (i = 0; i < nelem; i++) 11970Sstevel@tonic-gate zonecfg_free_fs_option_list(fsarray[i].zone_fs_options); 11980Sstevel@tonic-gate free(fsarray); 11990Sstevel@tonic-gate } 12000Sstevel@tonic-gate 1201766Scarlsonj /* 1202766Scarlsonj * This function constructs the miniroot-like "scratch zone" environment. If 1203766Scarlsonj * it returns B_FALSE, then the error has already been logged. 1204766Scarlsonj */ 1205766Scarlsonj static boolean_t 1206766Scarlsonj build_mounted(zlog_t *zlogp, char *rootpath, size_t rootlen, 1207766Scarlsonj const char *zonepath) 1208766Scarlsonj { 1209766Scarlsonj char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1210766Scarlsonj char luroot[MAXPATHLEN]; 1211766Scarlsonj const char **cpp; 1212766Scarlsonj static const char *mkdirs[] = { 12132592Sdp "/system", "/system/contract", "/system/object", "/proc", 12142592Sdp "/dev", "/tmp", "/a", NULL 1215766Scarlsonj }; 1216766Scarlsonj static const char *localdirs[] = { 1217766Scarlsonj "/etc", "/var", NULL 1218766Scarlsonj }; 1219766Scarlsonj static const char *loopdirs[] = { 1220766Scarlsonj "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform", 1221766Scarlsonj "/usr", NULL 1222766Scarlsonj }; 1223766Scarlsonj static const char *tmpdirs[] = { 1224766Scarlsonj "/tmp", "/var/run", NULL 1225766Scarlsonj }; 1226766Scarlsonj FILE *fp; 1227766Scarlsonj struct stat st; 1228766Scarlsonj char *altstr; 1229766Scarlsonj uuid_t uuid; 1230766Scarlsonj 1231766Scarlsonj /* 1232766Scarlsonj * Construct a small Solaris environment, including the zone root 1233766Scarlsonj * mounted on '/a' inside that environment. 1234766Scarlsonj */ 1235766Scarlsonj resolve_lofs(zlogp, rootpath, rootlen); 1236766Scarlsonj (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 1237766Scarlsonj resolve_lofs(zlogp, luroot, sizeof (luroot)); 1238766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot); 1239766Scarlsonj (void) symlink("./usr/bin", tmp); 1240766Scarlsonj 1241766Scarlsonj /* 1242766Scarlsonj * These are mostly special mount points; not handled here. (See 1243766Scarlsonj * zone_mount_early.) 1244766Scarlsonj */ 1245766Scarlsonj for (cpp = mkdirs; *cpp != NULL; cpp++) { 1246766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1247766Scarlsonj if (mkdir(tmp, 0755) != 0) { 1248766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1249766Scarlsonj return (B_FALSE); 1250766Scarlsonj } 1251766Scarlsonj } 1252766Scarlsonj 1253766Scarlsonj /* 1254766Scarlsonj * These are mounted read-write from the zone undergoing upgrade. We 1255766Scarlsonj * must be careful not to 'leak' things from the main system into the 1256766Scarlsonj * zone, and this accomplishes that goal. 1257766Scarlsonj */ 1258766Scarlsonj for (cpp = localdirs; *cpp != NULL; cpp++) { 1259766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1260766Scarlsonj (void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath, 1261766Scarlsonj *cpp); 1262766Scarlsonj if (mkdir(tmp, 0755) != 0) { 1263766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1264766Scarlsonj return (B_FALSE); 1265766Scarlsonj } 1266766Scarlsonj if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) { 1267766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1268766Scarlsonj *cpp); 1269766Scarlsonj return (B_FALSE); 1270766Scarlsonj } 1271766Scarlsonj } 1272766Scarlsonj 1273766Scarlsonj /* 1274766Scarlsonj * These are things mounted read-only from the running system because 1275766Scarlsonj * they contain binaries that must match system. 1276766Scarlsonj */ 1277766Scarlsonj for (cpp = loopdirs; *cpp != NULL; cpp++) { 1278766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1279766Scarlsonj if (mkdir(tmp, 0755) != 0) { 1280766Scarlsonj if (errno != EEXIST) { 1281766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1282766Scarlsonj return (B_FALSE); 1283766Scarlsonj } 1284766Scarlsonj if (lstat(tmp, &st) != 0) { 1285766Scarlsonj zerror(zlogp, B_TRUE, "cannot stat %s", tmp); 1286766Scarlsonj return (B_FALSE); 1287766Scarlsonj } 1288766Scarlsonj /* 1289766Scarlsonj * Ignore any non-directories encountered. These are 1290766Scarlsonj * things that have been converted into symlinks 1291766Scarlsonj * (/etc/fs and /etc/lib) and no longer need a lofs 1292766Scarlsonj * fixup. 1293766Scarlsonj */ 1294766Scarlsonj if (!S_ISDIR(st.st_mode)) 1295766Scarlsonj continue; 1296766Scarlsonj } 1297766Scarlsonj if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp, 1298766Scarlsonj tmp) != 0) { 1299766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1300766Scarlsonj *cpp); 1301766Scarlsonj return (B_FALSE); 1302766Scarlsonj } 1303766Scarlsonj } 1304766Scarlsonj 1305766Scarlsonj /* 1306766Scarlsonj * These are things with tmpfs mounted inside. 1307766Scarlsonj */ 1308766Scarlsonj for (cpp = tmpdirs; *cpp != NULL; cpp++) { 1309766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1310766Scarlsonj if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 1311766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1312766Scarlsonj return (B_FALSE); 1313766Scarlsonj } 1314766Scarlsonj if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) { 1315766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp); 1316766Scarlsonj return (B_FALSE); 1317766Scarlsonj } 1318766Scarlsonj } 1319766Scarlsonj 1320766Scarlsonj /* 1321766Scarlsonj * This is here to support lucopy. If there's an instance of this same 1322766Scarlsonj * zone on the current running system, then we mount its root up as 1323766Scarlsonj * read-only inside the scratch zone. 1324766Scarlsonj */ 1325766Scarlsonj (void) zonecfg_get_uuid(zone_name, uuid); 1326766Scarlsonj altstr = strdup(zonecfg_get_root()); 1327766Scarlsonj if (altstr == NULL) { 13282267Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 1329766Scarlsonj return (B_FALSE); 1330766Scarlsonj } 1331766Scarlsonj zonecfg_set_root(""); 1332766Scarlsonj (void) strlcpy(tmp, zone_name, sizeof (tmp)); 1333766Scarlsonj (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp)); 1334766Scarlsonj if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK && 1335766Scarlsonj strcmp(fromdir, rootpath) != 0) { 1336766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot); 1337766Scarlsonj if (mkdir(tmp, 0755) != 0) { 1338766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1339766Scarlsonj return (B_FALSE); 1340766Scarlsonj } 1341766Scarlsonj if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir, 1342766Scarlsonj tmp) != 0) { 1343766Scarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1344766Scarlsonj fromdir); 1345766Scarlsonj return (B_FALSE); 1346766Scarlsonj } 1347766Scarlsonj } 1348766Scarlsonj zonecfg_set_root(altstr); 1349766Scarlsonj free(altstr); 1350766Scarlsonj 1351766Scarlsonj if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) { 1352766Scarlsonj zerror(zlogp, B_TRUE, "cannot open zone mapfile"); 1353766Scarlsonj return (B_FALSE); 1354766Scarlsonj } 1355766Scarlsonj (void) ftruncate(fileno(fp), 0); 1356766Scarlsonj if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) { 1357766Scarlsonj zerror(zlogp, B_TRUE, "cannot add zone mapfile entry"); 1358766Scarlsonj } 1359766Scarlsonj zonecfg_close_scratch(fp); 1360766Scarlsonj (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot); 1361766Scarlsonj if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0) 1362766Scarlsonj return (B_FALSE); 1363766Scarlsonj (void) strlcpy(rootpath, tmp, rootlen); 1364766Scarlsonj return (B_TRUE); 1365766Scarlsonj } 1366766Scarlsonj 13670Sstevel@tonic-gate static int 1368766Scarlsonj mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd) 13690Sstevel@tonic-gate { 13700Sstevel@tonic-gate char rootpath[MAXPATHLEN]; 13710Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 13720Sstevel@tonic-gate int num_fs = 0, i; 13730Sstevel@tonic-gate struct zone_fstab fstab, *fs_ptr = NULL, *tmp_ptr; 13740Sstevel@tonic-gate struct zone_fstab *fsp; 13750Sstevel@tonic-gate zone_dochandle_t handle = NULL; 13760Sstevel@tonic-gate zone_state_t zstate; 13770Sstevel@tonic-gate 13780Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK || 1379766Scarlsonj (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) { 13800Sstevel@tonic-gate zerror(zlogp, B_FALSE, 1381766Scarlsonj "zone must be in '%s' or '%s' state to mount file-systems", 1382766Scarlsonj zone_state_str(ZONE_STATE_READY), 1383766Scarlsonj zone_state_str(ZONE_STATE_MOUNTED)); 13840Sstevel@tonic-gate goto bad; 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 13880Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone path"); 13890Sstevel@tonic-gate goto bad; 13900Sstevel@tonic-gate } 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 13930Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 13940Sstevel@tonic-gate goto bad; 13950Sstevel@tonic-gate } 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 13981645Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle"); 13990Sstevel@tonic-gate goto bad; 14000Sstevel@tonic-gate } 14010Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK || 14020Sstevel@tonic-gate zonecfg_setfsent(handle) != Z_OK) { 14030Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 14040Sstevel@tonic-gate goto bad; 14050Sstevel@tonic-gate } 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate /* 14080Sstevel@tonic-gate * /dev in the zone is loopback'd from the external /dev repository, 14090Sstevel@tonic-gate * in order to provide a largely read-only semantic. But because 14100Sstevel@tonic-gate * processes in the zone need to be able to chown, chmod, etc. zone 14110Sstevel@tonic-gate * /dev files, we can't use a 'ro' lofs mount. Instead we use a 14120Sstevel@tonic-gate * special mode just for zones, "zonedevfs". 14130Sstevel@tonic-gate * 14140Sstevel@tonic-gate * In the future we should front /dev with a full-fledged filesystem. 14150Sstevel@tonic-gate */ 14160Sstevel@tonic-gate num_fs++; 14170Sstevel@tonic-gate if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) { 14180Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 14190Sstevel@tonic-gate num_fs--; 14200Sstevel@tonic-gate goto bad; 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate fs_ptr = tmp_ptr; 14230Sstevel@tonic-gate fsp = &fs_ptr[num_fs - 1]; 1424766Scarlsonj /* 1425766Scarlsonj * Note that mount_one will prepend the alternate root to 1426766Scarlsonj * zone_fs_special and do the necessary resolution, so all that is 1427766Scarlsonj * needed here is to strip the root added by zone_get_zonepath. 1428766Scarlsonj */ 14290Sstevel@tonic-gate (void) strlcpy(fsp->zone_fs_dir, "/dev", sizeof (fsp->zone_fs_dir)); 14300Sstevel@tonic-gate (void) snprintf(fsp->zone_fs_special, sizeof (fsp->zone_fs_special), 1431766Scarlsonj "%s/dev", zonepath + strlen(zonecfg_get_root())); 14320Sstevel@tonic-gate fsp->zone_fs_raw[0] = '\0'; 14330Sstevel@tonic-gate (void) strlcpy(fsp->zone_fs_type, MNTTYPE_LOFS, 14340Sstevel@tonic-gate sizeof (fsp->zone_fs_type)); 14350Sstevel@tonic-gate fsp->zone_fs_options = NULL; 14360Sstevel@tonic-gate if (zonecfg_add_fs_option(fsp, MNTOPT_LOFS_ZONEDEVFS) != Z_OK) { 14370Sstevel@tonic-gate zerror(zlogp, B_FALSE, "error adding property"); 14380Sstevel@tonic-gate goto bad; 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate /* 14420Sstevel@tonic-gate * Iterate through the rest of the filesystems, first the IPDs, then 14430Sstevel@tonic-gate * the general FSs. Sort them all, then mount them in sorted order. 14440Sstevel@tonic-gate * This is to make sure the higher level directories (e.g., /usr) 14450Sstevel@tonic-gate * get mounted before any beneath them (e.g., /usr/local). 14460Sstevel@tonic-gate */ 14470Sstevel@tonic-gate if (zonecfg_setipdent(handle) != Z_OK) { 14480Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 14490Sstevel@tonic-gate goto bad; 14500Sstevel@tonic-gate } 14510Sstevel@tonic-gate while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 14520Sstevel@tonic-gate num_fs++; 14530Sstevel@tonic-gate if ((tmp_ptr = realloc(fs_ptr, 14540Sstevel@tonic-gate num_fs * sizeof (*tmp_ptr))) == NULL) { 14550Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 14560Sstevel@tonic-gate num_fs--; 14570Sstevel@tonic-gate (void) zonecfg_endipdent(handle); 14580Sstevel@tonic-gate goto bad; 14590Sstevel@tonic-gate } 14600Sstevel@tonic-gate fs_ptr = tmp_ptr; 14610Sstevel@tonic-gate fsp = &fs_ptr[num_fs - 1]; 14620Sstevel@tonic-gate /* 14630Sstevel@tonic-gate * IPDs logically only have a mount point; all other properties 14640Sstevel@tonic-gate * are implied. 14650Sstevel@tonic-gate */ 14660Sstevel@tonic-gate (void) strlcpy(fsp->zone_fs_dir, 14670Sstevel@tonic-gate fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 14680Sstevel@tonic-gate fsp->zone_fs_special[0] = '\0'; 14690Sstevel@tonic-gate fsp->zone_fs_raw[0] = '\0'; 14700Sstevel@tonic-gate fsp->zone_fs_type[0] = '\0'; 14710Sstevel@tonic-gate fsp->zone_fs_options = NULL; 14720Sstevel@tonic-gate } 14730Sstevel@tonic-gate (void) zonecfg_endipdent(handle); 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate if (zonecfg_setfsent(handle) != Z_OK) { 14760Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 14770Sstevel@tonic-gate goto bad; 14780Sstevel@tonic-gate } 14790Sstevel@tonic-gate while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 1480789Sahrens /* 1481789Sahrens * ZFS filesystems will not be accessible under an alternate 1482789Sahrens * root, since the pool will not be known. Ignore them in this 1483789Sahrens * case. 1484789Sahrens */ 1485789Sahrens if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0) 1486789Sahrens continue; 1487789Sahrens 14880Sstevel@tonic-gate num_fs++; 14890Sstevel@tonic-gate if ((tmp_ptr = realloc(fs_ptr, 14900Sstevel@tonic-gate num_fs * sizeof (*tmp_ptr))) == NULL) { 14910Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 14920Sstevel@tonic-gate num_fs--; 14930Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 14940Sstevel@tonic-gate goto bad; 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate fs_ptr = tmp_ptr; 14970Sstevel@tonic-gate fsp = &fs_ptr[num_fs - 1]; 14980Sstevel@tonic-gate (void) strlcpy(fsp->zone_fs_dir, 14990Sstevel@tonic-gate fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 15000Sstevel@tonic-gate (void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special, 15010Sstevel@tonic-gate sizeof (fsp->zone_fs_special)); 15020Sstevel@tonic-gate (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw, 15030Sstevel@tonic-gate sizeof (fsp->zone_fs_raw)); 15040Sstevel@tonic-gate (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type, 15050Sstevel@tonic-gate sizeof (fsp->zone_fs_type)); 15060Sstevel@tonic-gate fsp->zone_fs_options = fstab.zone_fs_options; 15070Sstevel@tonic-gate } 15080Sstevel@tonic-gate (void) zonecfg_endfsent(handle); 15090Sstevel@tonic-gate zonecfg_fini_handle(handle); 15100Sstevel@tonic-gate handle = NULL; 15110Sstevel@tonic-gate 1512766Scarlsonj /* 1513766Scarlsonj * If we're mounting a zone for administration, then we need to set up 1514766Scarlsonj * the "/a" environment inside the zone so that the commands that run 1515766Scarlsonj * in there have access to both the running system's utilities and the 1516766Scarlsonj * to-be-modified zone's files. 1517766Scarlsonj */ 1518766Scarlsonj if (mount_cmd && 1519766Scarlsonj !build_mounted(zlogp, rootpath, sizeof (rootpath), zonepath)) 1520766Scarlsonj goto bad; 1521766Scarlsonj 15220Sstevel@tonic-gate qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare); 15230Sstevel@tonic-gate for (i = 0; i < num_fs; i++) { 1524766Scarlsonj if (mount_cmd && strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) { 1525766Scarlsonj size_t slen = strlen(rootpath) - 2; 1526766Scarlsonj 1527766Scarlsonj /* /dev is special and always goes at the top */ 1528766Scarlsonj rootpath[slen] = '\0'; 1529766Scarlsonj if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 1530766Scarlsonj goto bad; 1531766Scarlsonj rootpath[slen] = '/'; 1532766Scarlsonj continue; 1533766Scarlsonj } 15340Sstevel@tonic-gate if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 15350Sstevel@tonic-gate goto bad; 15360Sstevel@tonic-gate } 15371676Sjpk 15381676Sjpk /* 15391676Sjpk * For Trusted Extensions cross-mount each lower level /export/home 15401676Sjpk */ 15411769Scarlsonj if (!mount_cmd && tsol_mounts(zlogp, zone_name, rootpath) != 0) 15421676Sjpk goto bad; 15431676Sjpk 15440Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs); 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate /* 15470Sstevel@tonic-gate * Everything looks fine. 15480Sstevel@tonic-gate */ 15490Sstevel@tonic-gate return (0); 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate bad: 15520Sstevel@tonic-gate if (handle != NULL) 15530Sstevel@tonic-gate zonecfg_fini_handle(handle); 15540Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs); 15550Sstevel@tonic-gate return (-1); 15560Sstevel@tonic-gate } 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */ 15590Sstevel@tonic-gate static int 15600Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr) 15610Sstevel@tonic-gate { 15620Sstevel@tonic-gate int prefixlen; 15630Sstevel@tonic-gate 15640Sstevel@tonic-gate prefixlen = atoi(prefixstr); 15650Sstevel@tonic-gate if (prefixlen < 0 || prefixlen > maxprefixlen) 15660Sstevel@tonic-gate return (1); 15670Sstevel@tonic-gate while (prefixlen > 0) { 15680Sstevel@tonic-gate if (prefixlen >= 8) { 15690Sstevel@tonic-gate *maskstr++ = 0xFF; 15700Sstevel@tonic-gate prefixlen -= 8; 15710Sstevel@tonic-gate continue; 15720Sstevel@tonic-gate } 15730Sstevel@tonic-gate *maskstr |= 1 << (8 - prefixlen); 15740Sstevel@tonic-gate prefixlen--; 15750Sstevel@tonic-gate } 15760Sstevel@tonic-gate return (0); 15770Sstevel@tonic-gate } 15780Sstevel@tonic-gate 15790Sstevel@tonic-gate /* 15800Sstevel@tonic-gate * Tear down all interfaces belonging to the given zone. This should 15810Sstevel@tonic-gate * be called with the zone in a state other than "running", so that 15820Sstevel@tonic-gate * interfaces can't be assigned to the zone after this returns. 15830Sstevel@tonic-gate * 15840Sstevel@tonic-gate * If anything goes wrong, log an error message and return an error. 15850Sstevel@tonic-gate */ 15860Sstevel@tonic-gate static int 15870Sstevel@tonic-gate unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id) 15880Sstevel@tonic-gate { 15890Sstevel@tonic-gate struct lifnum lifn; 15900Sstevel@tonic-gate struct lifconf lifc; 15910Sstevel@tonic-gate struct lifreq *lifrp, lifrl; 15920Sstevel@tonic-gate int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES; 15930Sstevel@tonic-gate int num_ifs, s, i, ret_code = 0; 15940Sstevel@tonic-gate uint_t bufsize; 15950Sstevel@tonic-gate char *buf = NULL; 15960Sstevel@tonic-gate 15970Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 15980Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket"); 15990Sstevel@tonic-gate ret_code = -1; 16000Sstevel@tonic-gate goto bad; 16010Sstevel@tonic-gate } 16020Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC; 16030Sstevel@tonic-gate lifn.lifn_flags = (int)lifc_flags; 16040Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) { 16050Sstevel@tonic-gate zerror(zlogp, B_TRUE, 16060Sstevel@tonic-gate "could not determine number of interfaces"); 16070Sstevel@tonic-gate ret_code = -1; 16080Sstevel@tonic-gate goto bad; 16090Sstevel@tonic-gate } 16100Sstevel@tonic-gate num_ifs = lifn.lifn_count; 16110Sstevel@tonic-gate bufsize = num_ifs * sizeof (struct lifreq); 16120Sstevel@tonic-gate if ((buf = malloc(bufsize)) == NULL) { 16130Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 16140Sstevel@tonic-gate ret_code = -1; 16150Sstevel@tonic-gate goto bad; 16160Sstevel@tonic-gate } 16170Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC; 16180Sstevel@tonic-gate lifc.lifc_flags = (int)lifc_flags; 16190Sstevel@tonic-gate lifc.lifc_len = bufsize; 16200Sstevel@tonic-gate lifc.lifc_buf = buf; 16210Sstevel@tonic-gate if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 16220Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get configured interfaces"); 16230Sstevel@tonic-gate ret_code = -1; 16240Sstevel@tonic-gate goto bad; 16250Sstevel@tonic-gate } 16260Sstevel@tonic-gate lifrp = lifc.lifc_req; 16270Sstevel@tonic-gate for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) { 16280Sstevel@tonic-gate (void) close(s); 16290Sstevel@tonic-gate if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) < 16300Sstevel@tonic-gate 0) { 16310Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get socket", 16320Sstevel@tonic-gate lifrl.lifr_name); 16330Sstevel@tonic-gate ret_code = -1; 16340Sstevel@tonic-gate continue; 16350Sstevel@tonic-gate } 16360Sstevel@tonic-gate (void) memset(&lifrl, 0, sizeof (lifrl)); 16370Sstevel@tonic-gate (void) strncpy(lifrl.lifr_name, lifrp->lifr_name, 16380Sstevel@tonic-gate sizeof (lifrl.lifr_name)); 16390Sstevel@tonic-gate if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) { 16400Sstevel@tonic-gate zerror(zlogp, B_TRUE, 16410Sstevel@tonic-gate "%s: could not determine zone interface belongs to", 16420Sstevel@tonic-gate lifrl.lifr_name); 16430Sstevel@tonic-gate ret_code = -1; 16440Sstevel@tonic-gate continue; 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate if (lifrl.lifr_zoneid == zone_id) { 16470Sstevel@tonic-gate if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) { 16480Sstevel@tonic-gate zerror(zlogp, B_TRUE, 16490Sstevel@tonic-gate "%s: could not remove interface", 16500Sstevel@tonic-gate lifrl.lifr_name); 16510Sstevel@tonic-gate ret_code = -1; 16520Sstevel@tonic-gate continue; 16530Sstevel@tonic-gate } 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate } 16560Sstevel@tonic-gate bad: 16570Sstevel@tonic-gate if (s > 0) 16580Sstevel@tonic-gate (void) close(s); 16590Sstevel@tonic-gate if (buf) 16600Sstevel@tonic-gate free(buf); 16610Sstevel@tonic-gate return (ret_code); 16620Sstevel@tonic-gate } 16630Sstevel@tonic-gate 16640Sstevel@tonic-gate static union sockunion { 16650Sstevel@tonic-gate struct sockaddr sa; 16660Sstevel@tonic-gate struct sockaddr_in sin; 16670Sstevel@tonic-gate struct sockaddr_dl sdl; 16680Sstevel@tonic-gate struct sockaddr_in6 sin6; 16690Sstevel@tonic-gate } so_dst, so_ifp; 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate static struct { 16720Sstevel@tonic-gate struct rt_msghdr hdr; 16730Sstevel@tonic-gate char space[512]; 16740Sstevel@tonic-gate } rtmsg; 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate static int 16770Sstevel@tonic-gate salen(struct sockaddr *sa) 16780Sstevel@tonic-gate { 16790Sstevel@tonic-gate switch (sa->sa_family) { 16800Sstevel@tonic-gate case AF_INET: 16810Sstevel@tonic-gate return (sizeof (struct sockaddr_in)); 16820Sstevel@tonic-gate case AF_LINK: 16830Sstevel@tonic-gate return (sizeof (struct sockaddr_dl)); 16840Sstevel@tonic-gate case AF_INET6: 16850Sstevel@tonic-gate return (sizeof (struct sockaddr_in6)); 16860Sstevel@tonic-gate default: 16870Sstevel@tonic-gate return (sizeof (struct sockaddr)); 16880Sstevel@tonic-gate } 16890Sstevel@tonic-gate } 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate #define ROUNDUP_LONG(a) \ 16920Sstevel@tonic-gate ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 16930Sstevel@tonic-gate 16940Sstevel@tonic-gate /* 16950Sstevel@tonic-gate * Look up which zone is using a given IP address. The address in question 16960Sstevel@tonic-gate * is expected to have been stuffed into the structure to which lifr points 16970Sstevel@tonic-gate * via a previous SIOCGLIFADDR ioctl(). 16980Sstevel@tonic-gate * 16990Sstevel@tonic-gate * This is done using black router socket magic. 17000Sstevel@tonic-gate * 17010Sstevel@tonic-gate * Return the name of the zone on success or NULL on failure. 17020Sstevel@tonic-gate * 17030Sstevel@tonic-gate * This is a lot of code for a simple task; a new ioctl request to take care 17040Sstevel@tonic-gate * of this might be a useful RFE. 17050Sstevel@tonic-gate */ 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate static char * 17080Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr) 17090Sstevel@tonic-gate { 17100Sstevel@tonic-gate static char answer[ZONENAME_MAX]; 17110Sstevel@tonic-gate pid_t pid; 17120Sstevel@tonic-gate int s, rlen, l, i; 17130Sstevel@tonic-gate char *cp = rtmsg.space; 17140Sstevel@tonic-gate struct sockaddr_dl *ifp = NULL; 17150Sstevel@tonic-gate struct sockaddr *sa; 17160Sstevel@tonic-gate char save_if_name[LIFNAMSIZ]; 17170Sstevel@tonic-gate 17180Sstevel@tonic-gate answer[0] = '\0'; 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate pid = getpid(); 17210Sstevel@tonic-gate if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 17220Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get routing socket"); 17230Sstevel@tonic-gate return (NULL); 17240Sstevel@tonic-gate } 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate if (lifr->lifr_addr.ss_family == AF_INET) { 17270Sstevel@tonic-gate struct sockaddr_in *sin4; 17280Sstevel@tonic-gate 17290Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET; 17300Sstevel@tonic-gate sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 17310Sstevel@tonic-gate so_dst.sin.sin_addr = sin4->sin_addr; 17320Sstevel@tonic-gate } else { 17330Sstevel@tonic-gate struct sockaddr_in6 *sin6; 17340Sstevel@tonic-gate 17350Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET6; 17360Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 17370Sstevel@tonic-gate so_dst.sin6.sin6_addr = sin6->sin6_addr; 17380Sstevel@tonic-gate } 17390Sstevel@tonic-gate 17400Sstevel@tonic-gate so_ifp.sa.sa_family = AF_LINK; 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate (void) memset(&rtmsg, 0, sizeof (rtmsg)); 17430Sstevel@tonic-gate rtmsg.hdr.rtm_type = RTM_GET; 17440Sstevel@tonic-gate rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST; 17450Sstevel@tonic-gate rtmsg.hdr.rtm_version = RTM_VERSION; 17460Sstevel@tonic-gate rtmsg.hdr.rtm_seq = ++rts_seqno; 17470Sstevel@tonic-gate rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST; 17480Sstevel@tonic-gate 17490Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_dst.sa)); 17500Sstevel@tonic-gate (void) memmove(cp, &(so_dst), l); 17510Sstevel@tonic-gate cp += l; 17520Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_ifp.sa)); 17530Sstevel@tonic-gate (void) memmove(cp, &(so_ifp), l); 17540Sstevel@tonic-gate cp += l; 17550Sstevel@tonic-gate 17560Sstevel@tonic-gate rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg; 17570Sstevel@tonic-gate 17580Sstevel@tonic-gate if ((rlen = write(s, &rtmsg, l)) < 0) { 17590Sstevel@tonic-gate zerror(zlogp, B_TRUE, "writing to routing socket"); 17600Sstevel@tonic-gate return (NULL); 17610Sstevel@tonic-gate } else if (rlen < (int)rtmsg.hdr.rtm_msglen) { 17620Sstevel@tonic-gate zerror(zlogp, B_TRUE, 17630Sstevel@tonic-gate "write to routing socket got only %d for len\n", rlen); 17640Sstevel@tonic-gate return (NULL); 17650Sstevel@tonic-gate } 17660Sstevel@tonic-gate do { 17670Sstevel@tonic-gate l = read(s, &rtmsg, sizeof (rtmsg)); 17680Sstevel@tonic-gate } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno || 17690Sstevel@tonic-gate rtmsg.hdr.rtm_pid != pid)); 17700Sstevel@tonic-gate if (l < 0) { 17710Sstevel@tonic-gate zerror(zlogp, B_TRUE, "reading from routing socket"); 17720Sstevel@tonic-gate return (NULL); 17730Sstevel@tonic-gate } 17740Sstevel@tonic-gate 17750Sstevel@tonic-gate if (rtmsg.hdr.rtm_version != RTM_VERSION) { 17760Sstevel@tonic-gate zerror(zlogp, B_FALSE, 17770Sstevel@tonic-gate "routing message version %d not understood", 17780Sstevel@tonic-gate rtmsg.hdr.rtm_version); 17790Sstevel@tonic-gate return (NULL); 17800Sstevel@tonic-gate } 17810Sstevel@tonic-gate if (rtmsg.hdr.rtm_msglen != (ushort_t)l) { 17820Sstevel@tonic-gate zerror(zlogp, B_FALSE, "message length mismatch, " 17830Sstevel@tonic-gate "expected %d bytes, returned %d bytes", 17840Sstevel@tonic-gate rtmsg.hdr.rtm_msglen, l); 17850Sstevel@tonic-gate return (NULL); 17860Sstevel@tonic-gate } 17870Sstevel@tonic-gate if (rtmsg.hdr.rtm_errno != 0) { 17880Sstevel@tonic-gate errno = rtmsg.hdr.rtm_errno; 17890Sstevel@tonic-gate zerror(zlogp, B_TRUE, "RTM_GET routing socket message"); 17900Sstevel@tonic-gate return (NULL); 17910Sstevel@tonic-gate } 17920Sstevel@tonic-gate if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) { 17930Sstevel@tonic-gate zerror(zlogp, B_FALSE, "interface not found"); 17940Sstevel@tonic-gate return (NULL); 17950Sstevel@tonic-gate } 17960Sstevel@tonic-gate cp = ((char *)(&rtmsg.hdr + 1)); 17970Sstevel@tonic-gate for (i = 1; i != 0; i <<= 1) { 17980Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 17990Sstevel@tonic-gate sa = (struct sockaddr *)cp; 18000Sstevel@tonic-gate if (i != RTA_IFP) { 18010Sstevel@tonic-gate if ((i & rtmsg.hdr.rtm_addrs) != 0) 18020Sstevel@tonic-gate cp += ROUNDUP_LONG(salen(sa)); 18030Sstevel@tonic-gate continue; 18040Sstevel@tonic-gate } 18050Sstevel@tonic-gate if (sa->sa_family == AF_LINK && 18060Sstevel@tonic-gate ((struct sockaddr_dl *)sa)->sdl_nlen != 0) 18070Sstevel@tonic-gate ifp = (struct sockaddr_dl *)sa; 18080Sstevel@tonic-gate break; 18090Sstevel@tonic-gate } 18100Sstevel@tonic-gate if (ifp == NULL) { 18110Sstevel@tonic-gate zerror(zlogp, B_FALSE, "interface could not be determined"); 18120Sstevel@tonic-gate return (NULL); 18130Sstevel@tonic-gate } 18140Sstevel@tonic-gate 18150Sstevel@tonic-gate /* 18160Sstevel@tonic-gate * We need to set the I/F name to what we got above, then do the 18170Sstevel@tonic-gate * appropriate ioctl to get its zone name. But lifr->lifr_name is 18180Sstevel@tonic-gate * used by the calling function to do a REMOVEIF, so if we leave the 18190Sstevel@tonic-gate * "good" zone's I/F name in place, *that* I/F will be removed instead 18200Sstevel@tonic-gate * of the bad one. So we save the old (bad) I/F name before over- 18210Sstevel@tonic-gate * writing it and doing the ioctl, then restore it after the ioctl. 18220Sstevel@tonic-gate */ 18230Sstevel@tonic-gate (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name)); 18240Sstevel@tonic-gate (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen); 18250Sstevel@tonic-gate lifr->lifr_name[ifp->sdl_nlen] = '\0'; 18260Sstevel@tonic-gate i = ioctl(s, SIOCGLIFZONE, lifr); 18270Sstevel@tonic-gate (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name)); 18280Sstevel@tonic-gate if (i < 0) { 18290Sstevel@tonic-gate zerror(zlogp, B_TRUE, 18300Sstevel@tonic-gate "%s: could not determine the zone interface belongs to", 18310Sstevel@tonic-gate lifr->lifr_name); 18320Sstevel@tonic-gate return (NULL); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0) 18350Sstevel@tonic-gate (void) snprintf(answer, sizeof (answer), "%d", 18360Sstevel@tonic-gate lifr->lifr_zoneid); 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate if (strlen(answer) > 0) 18390Sstevel@tonic-gate return (answer); 18400Sstevel@tonic-gate return (NULL); 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate typedef struct mcast_rtmsg_s { 18440Sstevel@tonic-gate struct rt_msghdr m_rtm; 18450Sstevel@tonic-gate union { 18460Sstevel@tonic-gate struct { 18470Sstevel@tonic-gate struct sockaddr_in m_dst; 18480Sstevel@tonic-gate struct sockaddr_in m_gw; 18490Sstevel@tonic-gate struct sockaddr_in m_netmask; 18500Sstevel@tonic-gate } m_v4; 18510Sstevel@tonic-gate struct { 18520Sstevel@tonic-gate struct sockaddr_in6 m_dst; 18530Sstevel@tonic-gate struct sockaddr_in6 m_gw; 18540Sstevel@tonic-gate struct sockaddr_in6 m_netmask; 18550Sstevel@tonic-gate } m_v6; 18560Sstevel@tonic-gate } m_u; 18570Sstevel@tonic-gate } mcast_rtmsg_t; 18580Sstevel@tonic-gate #define m_dst4 m_u.m_v4.m_dst 18590Sstevel@tonic-gate #define m_dst6 m_u.m_v6.m_dst 18600Sstevel@tonic-gate #define m_gw4 m_u.m_v4.m_gw 18610Sstevel@tonic-gate #define m_gw6 m_u.m_v6.m_gw 18620Sstevel@tonic-gate #define m_netmask4 m_u.m_v4.m_netmask 18630Sstevel@tonic-gate #define m_netmask6 m_u.m_v6.m_netmask 18640Sstevel@tonic-gate 18650Sstevel@tonic-gate /* 18660Sstevel@tonic-gate * Configures a single interface: a new virtual interface is added, based on 18670Sstevel@tonic-gate * the physical interface nwiftabptr->zone_nwif_physical, with the address 18680Sstevel@tonic-gate * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that 18690Sstevel@tonic-gate * the "address" can be an IPv6 address (with a /prefixlength required), an 18700Sstevel@tonic-gate * IPv4 address (with a /prefixlength optional), or a name; for the latter, 18710Sstevel@tonic-gate * an IPv4 name-to-address resolution will be attempted. 18720Sstevel@tonic-gate * 18730Sstevel@tonic-gate * A default interface route for multicast is created on the first IPv4 and 18740Sstevel@tonic-gate * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively. 18750Sstevel@tonic-gate * This should really be done in the init scripts if we ever allow zones to 18760Sstevel@tonic-gate * modify the routing tables. 18770Sstevel@tonic-gate * 18780Sstevel@tonic-gate * If anything goes wrong, we log an detailed error message, attempt to tear 18790Sstevel@tonic-gate * down whatever we set up and return an error. 18800Sstevel@tonic-gate */ 18810Sstevel@tonic-gate static int 18820Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id, 18830Sstevel@tonic-gate struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp, 18840Sstevel@tonic-gate boolean_t *mcast_rt_v6_setp) 18850Sstevel@tonic-gate { 18860Sstevel@tonic-gate struct lifreq lifr; 18870Sstevel@tonic-gate struct sockaddr_in netmask4; 18880Sstevel@tonic-gate struct sockaddr_in6 netmask6; 18890Sstevel@tonic-gate struct in_addr in4; 18900Sstevel@tonic-gate struct in6_addr in6; 18910Sstevel@tonic-gate sa_family_t af; 18920Sstevel@tonic-gate char *slashp = strchr(nwiftabptr->zone_nwif_address, '/'); 18930Sstevel@tonic-gate mcast_rtmsg_t mcast_rtmsg; 18940Sstevel@tonic-gate int s; 18950Sstevel@tonic-gate int rs; 18960Sstevel@tonic-gate int rlen; 18970Sstevel@tonic-gate boolean_t got_netmask = B_FALSE; 18980Sstevel@tonic-gate char addrstr4[INET_ADDRSTRLEN]; 18990Sstevel@tonic-gate int res; 19000Sstevel@tonic-gate 19010Sstevel@tonic-gate res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr); 19020Sstevel@tonic-gate if (res != Z_OK) { 19030Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res), 19040Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 19050Sstevel@tonic-gate return (-1); 19060Sstevel@tonic-gate } 19070Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 19080Sstevel@tonic-gate if (af == AF_INET) 19090Sstevel@tonic-gate in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr; 19100Sstevel@tonic-gate else 19110Sstevel@tonic-gate in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr; 19120Sstevel@tonic-gate 19130Sstevel@tonic-gate if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 19140Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket"); 19150Sstevel@tonic-gate return (-1); 19160Sstevel@tonic-gate } 19170Sstevel@tonic-gate 19180Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical, 19190Sstevel@tonic-gate sizeof (lifr.lifr_name)); 19200Sstevel@tonic-gate if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) { 1921*2611Svp157776 /* 1922*2611Svp157776 * Here, we know that the interface can't be brought up. 1923*2611Svp157776 * A similar warning message was already printed out to 1924*2611Svp157776 * the console by zoneadm(1M) so instead we log the 1925*2611Svp157776 * message to syslog and continue. 1926*2611Svp157776 */ 1927*2611Svp157776 zerror(&logsys, B_TRUE, "WARNING: skipping interface " 1928*2611Svp157776 "'%s' which may not be present/plumbed in the " 1929*2611Svp157776 "global zone.", lifr.lifr_name); 19300Sstevel@tonic-gate (void) close(s); 1931*2611Svp157776 return (Z_OK); 19320Sstevel@tonic-gate } 19330Sstevel@tonic-gate 19340Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 19350Sstevel@tonic-gate zerror(zlogp, B_TRUE, 19360Sstevel@tonic-gate "%s: could not set IP address to %s", 19370Sstevel@tonic-gate lifr.lifr_name, nwiftabptr->zone_nwif_address); 19380Sstevel@tonic-gate goto bad; 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate /* Preserve literal IPv4 address for later potential printing. */ 19420Sstevel@tonic-gate if (af == AF_INET) 19430Sstevel@tonic-gate (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN); 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate lifr.lifr_zoneid = zone_id; 19460Sstevel@tonic-gate if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) { 19470Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not place interface into zone", 19480Sstevel@tonic-gate lifr.lifr_name); 19490Sstevel@tonic-gate goto bad; 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) { 19530Sstevel@tonic-gate got_netmask = B_TRUE; /* default setting will be correct */ 19540Sstevel@tonic-gate } else { 19550Sstevel@tonic-gate if (af == AF_INET) { 19560Sstevel@tonic-gate /* 19570Sstevel@tonic-gate * The IPv4 netmask can be determined either 19580Sstevel@tonic-gate * directly if a prefix length was supplied with 19590Sstevel@tonic-gate * the address or via the netmasks database. Not 19600Sstevel@tonic-gate * being able to determine it is a common failure, 19610Sstevel@tonic-gate * but it often is not fatal to operation of the 19620Sstevel@tonic-gate * interface. In that case, a warning will be 19630Sstevel@tonic-gate * printed after the rest of the interface's 19640Sstevel@tonic-gate * parameters have been configured. 19650Sstevel@tonic-gate */ 19660Sstevel@tonic-gate (void) memset(&netmask4, 0, sizeof (netmask4)); 19670Sstevel@tonic-gate if (slashp != NULL) { 19680Sstevel@tonic-gate if (addr2netmask(slashp + 1, V4_ADDR_LEN, 19690Sstevel@tonic-gate (uchar_t *)&netmask4.sin_addr) != 0) { 19700Sstevel@tonic-gate *slashp = '/'; 19710Sstevel@tonic-gate zerror(zlogp, B_FALSE, 19720Sstevel@tonic-gate "%s: invalid prefix length in %s", 19730Sstevel@tonic-gate lifr.lifr_name, 19740Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 19750Sstevel@tonic-gate goto bad; 19760Sstevel@tonic-gate } 19770Sstevel@tonic-gate got_netmask = B_TRUE; 19780Sstevel@tonic-gate } else if (getnetmaskbyaddr(in4, 19790Sstevel@tonic-gate &netmask4.sin_addr) == 0) { 19800Sstevel@tonic-gate got_netmask = B_TRUE; 19810Sstevel@tonic-gate } 19820Sstevel@tonic-gate if (got_netmask) { 19830Sstevel@tonic-gate netmask4.sin_family = af; 19840Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask4, 19850Sstevel@tonic-gate sizeof (netmask4)); 19860Sstevel@tonic-gate } 19870Sstevel@tonic-gate } else { 19880Sstevel@tonic-gate (void) memset(&netmask6, 0, sizeof (netmask6)); 19890Sstevel@tonic-gate if (addr2netmask(slashp + 1, V6_ADDR_LEN, 19900Sstevel@tonic-gate (uchar_t *)&netmask6.sin6_addr) != 0) { 19910Sstevel@tonic-gate *slashp = '/'; 19920Sstevel@tonic-gate zerror(zlogp, B_FALSE, 19930Sstevel@tonic-gate "%s: invalid prefix length in %s", 19940Sstevel@tonic-gate lifr.lifr_name, 19950Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 19960Sstevel@tonic-gate goto bad; 19970Sstevel@tonic-gate } 19980Sstevel@tonic-gate got_netmask = B_TRUE; 19990Sstevel@tonic-gate netmask6.sin6_family = af; 20000Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask6, 20010Sstevel@tonic-gate sizeof (netmask6)); 20020Sstevel@tonic-gate } 20030Sstevel@tonic-gate if (got_netmask && 20040Sstevel@tonic-gate ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) { 20050Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not set netmask", 20060Sstevel@tonic-gate lifr.lifr_name); 20070Sstevel@tonic-gate goto bad; 20080Sstevel@tonic-gate } 20090Sstevel@tonic-gate 20100Sstevel@tonic-gate /* 20110Sstevel@tonic-gate * This doesn't set the broadcast address at all. Rather, it 20120Sstevel@tonic-gate * gets, then sets the interface's address, relying on the fact 20130Sstevel@tonic-gate * that resetting the address will reset the broadcast address. 20140Sstevel@tonic-gate */ 20150Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 20160Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get address", 20170Sstevel@tonic-gate lifr.lifr_name); 20180Sstevel@tonic-gate goto bad; 20190Sstevel@tonic-gate } 20200Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 20210Sstevel@tonic-gate zerror(zlogp, B_TRUE, 20220Sstevel@tonic-gate "%s: could not reset broadcast address", 20230Sstevel@tonic-gate lifr.lifr_name); 20240Sstevel@tonic-gate goto bad; 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate } 20270Sstevel@tonic-gate 20280Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 20290Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get flags", 20300Sstevel@tonic-gate lifr.lifr_name); 20310Sstevel@tonic-gate goto bad; 20320Sstevel@tonic-gate } 20330Sstevel@tonic-gate lifr.lifr_flags |= IFF_UP; 20340Sstevel@tonic-gate if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) { 20350Sstevel@tonic-gate int save_errno = errno; 20360Sstevel@tonic-gate char *zone_using; 20370Sstevel@tonic-gate 20380Sstevel@tonic-gate /* 20390Sstevel@tonic-gate * If we failed with something other than EADDRNOTAVAIL, 20400Sstevel@tonic-gate * then skip to the end. Otherwise, look up our address, 20410Sstevel@tonic-gate * then call a function to determine which zone is already 20420Sstevel@tonic-gate * using that address. 20430Sstevel@tonic-gate */ 20440Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) { 20450Sstevel@tonic-gate zerror(zlogp, B_TRUE, 20460Sstevel@tonic-gate "%s: could not bring interface up", lifr.lifr_name); 20470Sstevel@tonic-gate goto bad; 20480Sstevel@tonic-gate } 20490Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 20500Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get address", 20510Sstevel@tonic-gate lifr.lifr_name); 20520Sstevel@tonic-gate goto bad; 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate zone_using = who_is_using(zlogp, &lifr); 20550Sstevel@tonic-gate errno = save_errno; 20560Sstevel@tonic-gate if (zone_using == NULL) 20570Sstevel@tonic-gate zerror(zlogp, B_TRUE, 20580Sstevel@tonic-gate "%s: could not bring interface up", lifr.lifr_name); 20590Sstevel@tonic-gate else 20600Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not bring interface " 20610Sstevel@tonic-gate "up: address in use by zone '%s'", lifr.lifr_name, 20620Sstevel@tonic-gate zone_using); 20630Sstevel@tonic-gate goto bad; 20640Sstevel@tonic-gate } 20650Sstevel@tonic-gate if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET && 20660Sstevel@tonic-gate mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) || 20670Sstevel@tonic-gate (af == AF_INET6 && 20680Sstevel@tonic-gate mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) { 20690Sstevel@tonic-gate rs = socket(PF_ROUTE, SOCK_RAW, 0); 20700Sstevel@tonic-gate if (rs < 0) { 20710Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not create " 20720Sstevel@tonic-gate "routing socket", lifr.lifr_name); 20730Sstevel@tonic-gate goto bad; 20740Sstevel@tonic-gate } 20750Sstevel@tonic-gate (void) shutdown(rs, 0); 20760Sstevel@tonic-gate (void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t)); 20770Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_msglen = sizeof (struct rt_msghdr) + 20780Sstevel@tonic-gate 3 * (af == AF_INET ? sizeof (struct sockaddr_in) : 20790Sstevel@tonic-gate sizeof (struct sockaddr_in6)); 20800Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION; 20810Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_type = RTM_ADD; 20820Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_flags = RTF_UP; 20830Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_addrs = 20840Sstevel@tonic-gate RTA_DST | RTA_GATEWAY | RTA_NETMASK; 20850Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno; 20860Sstevel@tonic-gate if (af == AF_INET) { 20870Sstevel@tonic-gate mcast_rtmsg.m_dst4.sin_family = AF_INET; 20880Sstevel@tonic-gate mcast_rtmsg.m_dst4.sin_addr.s_addr = 20890Sstevel@tonic-gate htonl(INADDR_UNSPEC_GROUP); 20900Sstevel@tonic-gate mcast_rtmsg.m_gw4.sin_family = AF_INET; 20910Sstevel@tonic-gate mcast_rtmsg.m_gw4.sin_addr = in4; 20920Sstevel@tonic-gate mcast_rtmsg.m_netmask4.sin_family = AF_INET; 20930Sstevel@tonic-gate mcast_rtmsg.m_netmask4.sin_addr.s_addr = 20940Sstevel@tonic-gate htonl(IN_CLASSD_NET); 20950Sstevel@tonic-gate } else { 20960Sstevel@tonic-gate mcast_rtmsg.m_dst6.sin6_family = AF_INET6; 20970Sstevel@tonic-gate mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU; 20980Sstevel@tonic-gate mcast_rtmsg.m_gw6.sin6_family = AF_INET6; 20990Sstevel@tonic-gate mcast_rtmsg.m_gw6.sin6_addr = in6; 21000Sstevel@tonic-gate mcast_rtmsg.m_netmask6.sin6_family = AF_INET6; 21010Sstevel@tonic-gate mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU; 21020Sstevel@tonic-gate } 21030Sstevel@tonic-gate rlen = write(rs, (char *)&mcast_rtmsg, 21040Sstevel@tonic-gate mcast_rtmsg.m_rtm.rtm_msglen); 2105*2611Svp157776 /* 2106*2611Svp157776 * The write to the multicast socket will fail if the 2107*2611Svp157776 * interface belongs to a failed IPMP group. This is a 2108*2611Svp157776 * non-fatal error and the zone will continue booting. 2109*2611Svp157776 * While the zone is running, if any interface in the 2110*2611Svp157776 * failed IPMP group recovers, the zone will fallback to 2111*2611Svp157776 * using that interface. 2112*2611Svp157776 */ 21130Sstevel@tonic-gate if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) { 21140Sstevel@tonic-gate if (rlen < 0) { 2115*2611Svp157776 zerror(zlogp, B_TRUE, "WARNING: interface " 2116*2611Svp157776 "'%s' not available as default for " 2117*2611Svp157776 "multicast.", lifr.lifr_name); 21180Sstevel@tonic-gate } else { 2119*2611Svp157776 zerror(zlogp, B_FALSE, "WARNING: interface " 2120*2611Svp157776 "'%s' not available as default for " 2121*2611Svp157776 "multicast; routing socket returned " 2122*2611Svp157776 "unexpected %d bytes.", 2123*2611Svp157776 lifr.lifr_name, rlen); 21240Sstevel@tonic-gate } 21250Sstevel@tonic-gate } else { 2126*2611Svp157776 2127*2611Svp157776 if (af == AF_INET) { 2128*2611Svp157776 *mcast_rt_v4_setp = B_TRUE; 2129*2611Svp157776 } else { 2130*2611Svp157776 *mcast_rt_v6_setp = B_TRUE; 2131*2611Svp157776 } 21320Sstevel@tonic-gate } 21330Sstevel@tonic-gate (void) close(rs); 21340Sstevel@tonic-gate } 21350Sstevel@tonic-gate 21360Sstevel@tonic-gate if (!got_netmask) { 21370Sstevel@tonic-gate /* 21380Sstevel@tonic-gate * A common, but often non-fatal problem, is that the system 21390Sstevel@tonic-gate * cannot find the netmask for an interface address. This is 21400Sstevel@tonic-gate * often caused by it being only in /etc/inet/netmasks, but 21410Sstevel@tonic-gate * /etc/nsswitch.conf says to use NIS or NIS+ and it's not 21420Sstevel@tonic-gate * in that. This doesn't show up at boot because the netmask 21430Sstevel@tonic-gate * is obtained from /etc/inet/netmasks when no network 21440Sstevel@tonic-gate * interfaces are up, but isn't consulted when NIS/NIS+ is 21450Sstevel@tonic-gate * available. We warn the user here that something like this 21460Sstevel@tonic-gate * has happened and we're just running with a default and 21470Sstevel@tonic-gate * possible incorrect netmask. 21480Sstevel@tonic-gate */ 21490Sstevel@tonic-gate char buffer[INET6_ADDRSTRLEN]; 21500Sstevel@tonic-gate void *addr; 21510Sstevel@tonic-gate 21520Sstevel@tonic-gate if (af == AF_INET) 21530Sstevel@tonic-gate addr = &((struct sockaddr_in *) 21540Sstevel@tonic-gate (&lifr.lifr_addr))->sin_addr; 21550Sstevel@tonic-gate else 21560Sstevel@tonic-gate addr = &((struct sockaddr_in6 *) 21570Sstevel@tonic-gate (&lifr.lifr_addr))->sin6_addr; 21580Sstevel@tonic-gate 21590Sstevel@tonic-gate /* Find out what netmask interface is going to be using */ 21600Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 || 21610Sstevel@tonic-gate inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) 21620Sstevel@tonic-gate goto bad; 21630Sstevel@tonic-gate zerror(zlogp, B_FALSE, 21640Sstevel@tonic-gate "WARNING: %s: no matching subnet found in netmasks(4) for " 21650Sstevel@tonic-gate "%s; using default of %s.", 21660Sstevel@tonic-gate lifr.lifr_name, addrstr4, buffer); 21670Sstevel@tonic-gate } 21680Sstevel@tonic-gate 21690Sstevel@tonic-gate (void) close(s); 21700Sstevel@tonic-gate return (Z_OK); 21710Sstevel@tonic-gate bad: 21720Sstevel@tonic-gate (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 21730Sstevel@tonic-gate (void) close(s); 21740Sstevel@tonic-gate return (-1); 21750Sstevel@tonic-gate } 21760Sstevel@tonic-gate 21770Sstevel@tonic-gate /* 21780Sstevel@tonic-gate * Sets up network interfaces based on information from the zone configuration. 21790Sstevel@tonic-gate * An IPv4 loopback interface is set up "for free", modeling the global system. 21800Sstevel@tonic-gate * If any of the configuration interfaces were IPv6, then an IPv6 loopback 21810Sstevel@tonic-gate * address is set up as well. 21820Sstevel@tonic-gate * 21830Sstevel@tonic-gate * If anything goes wrong, we log a general error message, attempt to tear down 21840Sstevel@tonic-gate * whatever we set up, and return an error. 21850Sstevel@tonic-gate */ 21860Sstevel@tonic-gate static int 21870Sstevel@tonic-gate configure_network_interfaces(zlog_t *zlogp) 21880Sstevel@tonic-gate { 21890Sstevel@tonic-gate zone_dochandle_t handle; 21900Sstevel@tonic-gate struct zone_nwiftab nwiftab, loopback_iftab; 21910Sstevel@tonic-gate boolean_t saw_v6 = B_FALSE; 21920Sstevel@tonic-gate boolean_t mcast_rt_v4_set = B_FALSE; 21930Sstevel@tonic-gate boolean_t mcast_rt_v6_set = B_FALSE; 21940Sstevel@tonic-gate zoneid_t zoneid; 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) { 21970Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 21980Sstevel@tonic-gate return (-1); 21990Sstevel@tonic-gate } 22000Sstevel@tonic-gate 22010Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 22020Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 22030Sstevel@tonic-gate return (-1); 22040Sstevel@tonic-gate } 22050Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 22060Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 22070Sstevel@tonic-gate zonecfg_fini_handle(handle); 22080Sstevel@tonic-gate return (-1); 22090Sstevel@tonic-gate } 22100Sstevel@tonic-gate if (zonecfg_setnwifent(handle) == Z_OK) { 22110Sstevel@tonic-gate for (;;) { 22120Sstevel@tonic-gate struct in6_addr in6; 22130Sstevel@tonic-gate 22140Sstevel@tonic-gate if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 22150Sstevel@tonic-gate break; 22160Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, 22170Sstevel@tonic-gate &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) != 22180Sstevel@tonic-gate Z_OK) { 22190Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 22200Sstevel@tonic-gate zonecfg_fini_handle(handle); 22210Sstevel@tonic-gate return (-1); 22220Sstevel@tonic-gate } 22230Sstevel@tonic-gate if (inet_pton(AF_INET6, nwiftab.zone_nwif_address, 22240Sstevel@tonic-gate &in6) == 1) 22250Sstevel@tonic-gate saw_v6 = B_TRUE; 22260Sstevel@tonic-gate } 22270Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 22280Sstevel@tonic-gate } 22290Sstevel@tonic-gate zonecfg_fini_handle(handle); 22300Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0", 22310Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_physical)); 22320Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1", 22330Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address)); 22340Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL) 22350Sstevel@tonic-gate != Z_OK) { 22360Sstevel@tonic-gate return (-1); 22370Sstevel@tonic-gate } 22380Sstevel@tonic-gate if (saw_v6) { 22390Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128", 22400Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address)); 22410Sstevel@tonic-gate if (configure_one_interface(zlogp, zoneid, 22420Sstevel@tonic-gate &loopback_iftab, NULL, NULL) != Z_OK) { 22430Sstevel@tonic-gate return (-1); 22440Sstevel@tonic-gate } 22450Sstevel@tonic-gate } 22460Sstevel@tonic-gate return (0); 22470Sstevel@tonic-gate } 22480Sstevel@tonic-gate 22490Sstevel@tonic-gate static int 22500Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid, 22510Sstevel@tonic-gate const struct sockaddr_storage *local, const struct sockaddr_storage *remote) 22520Sstevel@tonic-gate { 22530Sstevel@tonic-gate int fd; 22540Sstevel@tonic-gate struct strioctl ioc; 22550Sstevel@tonic-gate tcp_ioc_abort_conn_t conn; 22560Sstevel@tonic-gate int error; 22570Sstevel@tonic-gate 22580Sstevel@tonic-gate conn.ac_local = *local; 22590Sstevel@tonic-gate conn.ac_remote = *remote; 22600Sstevel@tonic-gate conn.ac_start = TCPS_SYN_SENT; 22610Sstevel@tonic-gate conn.ac_end = TCPS_TIME_WAIT; 22620Sstevel@tonic-gate conn.ac_zoneid = zoneid; 22630Sstevel@tonic-gate 22640Sstevel@tonic-gate ioc.ic_cmd = TCP_IOC_ABORT_CONN; 22650Sstevel@tonic-gate ioc.ic_timout = -1; /* infinite timeout */ 22660Sstevel@tonic-gate ioc.ic_len = sizeof (conn); 22670Sstevel@tonic-gate ioc.ic_dp = (char *)&conn; 22680Sstevel@tonic-gate 22690Sstevel@tonic-gate if ((fd = open("/dev/tcp", O_RDONLY)) < 0) { 22700Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp"); 22710Sstevel@tonic-gate return (-1); 22720Sstevel@tonic-gate } 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate error = ioctl(fd, I_STR, &ioc); 22750Sstevel@tonic-gate (void) close(fd); 22760Sstevel@tonic-gate if (error == 0 || errno == ENOENT) /* ENOENT is not an error */ 22770Sstevel@tonic-gate return (0); 22780Sstevel@tonic-gate return (-1); 22790Sstevel@tonic-gate } 22800Sstevel@tonic-gate 22810Sstevel@tonic-gate static int 22820Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid) 22830Sstevel@tonic-gate { 22840Sstevel@tonic-gate struct sockaddr_storage l, r; 22850Sstevel@tonic-gate struct sockaddr_in *local, *remote; 22860Sstevel@tonic-gate struct sockaddr_in6 *local6, *remote6; 22870Sstevel@tonic-gate int error; 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate /* 22900Sstevel@tonic-gate * Abort IPv4 connections. 22910Sstevel@tonic-gate */ 22920Sstevel@tonic-gate bzero(&l, sizeof (*local)); 22930Sstevel@tonic-gate local = (struct sockaddr_in *)&l; 22940Sstevel@tonic-gate local->sin_family = AF_INET; 22950Sstevel@tonic-gate local->sin_addr.s_addr = INADDR_ANY; 22960Sstevel@tonic-gate local->sin_port = 0; 22970Sstevel@tonic-gate 22980Sstevel@tonic-gate bzero(&r, sizeof (*remote)); 22990Sstevel@tonic-gate remote = (struct sockaddr_in *)&r; 23000Sstevel@tonic-gate remote->sin_family = AF_INET; 23010Sstevel@tonic-gate remote->sin_addr.s_addr = INADDR_ANY; 23020Sstevel@tonic-gate remote->sin_port = 0; 23030Sstevel@tonic-gate 23040Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 23050Sstevel@tonic-gate return (error); 23060Sstevel@tonic-gate 23070Sstevel@tonic-gate /* 23080Sstevel@tonic-gate * Abort IPv6 connections. 23090Sstevel@tonic-gate */ 23100Sstevel@tonic-gate bzero(&l, sizeof (*local6)); 23110Sstevel@tonic-gate local6 = (struct sockaddr_in6 *)&l; 23120Sstevel@tonic-gate local6->sin6_family = AF_INET6; 23130Sstevel@tonic-gate local6->sin6_port = 0; 23140Sstevel@tonic-gate local6->sin6_addr = in6addr_any; 23150Sstevel@tonic-gate 23160Sstevel@tonic-gate bzero(&r, sizeof (*remote6)); 23170Sstevel@tonic-gate remote6 = (struct sockaddr_in6 *)&r; 23180Sstevel@tonic-gate remote6->sin6_family = AF_INET6; 23190Sstevel@tonic-gate remote6->sin6_port = 0; 23200Sstevel@tonic-gate remote6->sin6_addr = in6addr_any; 23210Sstevel@tonic-gate 23220Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 23230Sstevel@tonic-gate return (error); 23240Sstevel@tonic-gate return (0); 23250Sstevel@tonic-gate } 23260Sstevel@tonic-gate 23270Sstevel@tonic-gate static int 23280Sstevel@tonic-gate devfsadm_call(zlog_t *zlogp, const char *arg) 23290Sstevel@tonic-gate { 23300Sstevel@tonic-gate char *argv[4]; 23310Sstevel@tonic-gate int status; 23320Sstevel@tonic-gate 23330Sstevel@tonic-gate argv[0] = DEVFSADM; 23340Sstevel@tonic-gate argv[1] = (char *)arg; 23350Sstevel@tonic-gate argv[2] = zone_name; 23360Sstevel@tonic-gate argv[3] = NULL; 23370Sstevel@tonic-gate status = forkexec(zlogp, DEVFSADM_PATH, argv); 23380Sstevel@tonic-gate if (status == 0 || status == -1) 23390Sstevel@tonic-gate return (status); 23400Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s call (%s %s %s) unexpectedly returned %d", 23411645Scomay DEVFSADM, DEVFSADM_PATH, arg, zone_name, status); 23420Sstevel@tonic-gate return (-1); 23430Sstevel@tonic-gate } 23440Sstevel@tonic-gate 23450Sstevel@tonic-gate static int 23460Sstevel@tonic-gate devfsadm_register(zlog_t *zlogp) 23470Sstevel@tonic-gate { 23480Sstevel@tonic-gate /* 23490Sstevel@tonic-gate * Ready the zone's devices. 23500Sstevel@tonic-gate */ 23510Sstevel@tonic-gate return (devfsadm_call(zlogp, "-z")); 23520Sstevel@tonic-gate } 23530Sstevel@tonic-gate 23540Sstevel@tonic-gate static int 23550Sstevel@tonic-gate devfsadm_unregister(zlog_t *zlogp) 23560Sstevel@tonic-gate { 23570Sstevel@tonic-gate return (devfsadm_call(zlogp, "-Z")); 23580Sstevel@tonic-gate } 23590Sstevel@tonic-gate 23600Sstevel@tonic-gate static int 23611645Scomay get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd) 23621645Scomay { 23631645Scomay int error = -1; 23641645Scomay zone_dochandle_t handle; 23651645Scomay char *privname = NULL; 23661645Scomay 23671645Scomay if (mount_cmd) { 23681645Scomay if (zonecfg_default_privset(privs) == Z_OK) 23691645Scomay return (0); 23701645Scomay zerror(zlogp, B_FALSE, 23711645Scomay "failed to determine the zone's default privilege set"); 23721645Scomay return (-1); 23731645Scomay } 23741645Scomay 23751645Scomay if ((handle = zonecfg_init_handle()) == NULL) { 23761645Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle"); 23771645Scomay return (-1); 23781645Scomay } 23791645Scomay if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 23801645Scomay zerror(zlogp, B_FALSE, "invalid configuration"); 23811645Scomay zonecfg_fini_handle(handle); 23821645Scomay return (-1); 23831645Scomay } 23841645Scomay 23851645Scomay switch (zonecfg_get_privset(handle, privs, &privname)) { 23861645Scomay case Z_OK: 23871645Scomay error = 0; 23881645Scomay break; 23891645Scomay case Z_PRIV_PROHIBITED: 23901645Scomay zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted " 23911645Scomay "within the zone's privilege set", privname); 23921645Scomay break; 23931645Scomay case Z_PRIV_REQUIRED: 23941645Scomay zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing " 23951645Scomay "from the zone's privilege set", privname); 23961645Scomay break; 23971645Scomay case Z_PRIV_UNKNOWN: 23981645Scomay zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified " 23991645Scomay "in the zone's privilege set", privname); 24001645Scomay break; 24011645Scomay default: 24021645Scomay zerror(zlogp, B_FALSE, "failed to determine the zone's " 24031645Scomay "privilege set"); 24041645Scomay break; 24051645Scomay } 24061645Scomay 24071645Scomay free(privname); 24081645Scomay zonecfg_fini_handle(handle); 24091645Scomay return (error); 24101645Scomay } 24111645Scomay 24121645Scomay static int 24130Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep) 24140Sstevel@tonic-gate { 24150Sstevel@tonic-gate nvlist_t *nvl = NULL; 24160Sstevel@tonic-gate char *nvl_packed = NULL; 24170Sstevel@tonic-gate size_t nvl_size = 0; 24180Sstevel@tonic-gate nvlist_t **nvlv = NULL; 24190Sstevel@tonic-gate int rctlcount = 0; 24200Sstevel@tonic-gate int error = -1; 24210Sstevel@tonic-gate zone_dochandle_t handle; 24220Sstevel@tonic-gate struct zone_rctltab rctltab; 24230Sstevel@tonic-gate rctlblk_t *rctlblk = NULL; 24240Sstevel@tonic-gate 24250Sstevel@tonic-gate *bufp = NULL; 24260Sstevel@tonic-gate *bufsizep = 0; 24270Sstevel@tonic-gate 24280Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 24290Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 24300Sstevel@tonic-gate return (-1); 24310Sstevel@tonic-gate } 24320Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 24330Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 24340Sstevel@tonic-gate zonecfg_fini_handle(handle); 24350Sstevel@tonic-gate return (-1); 24360Sstevel@tonic-gate } 24370Sstevel@tonic-gate 24380Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 24390Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 24400Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc"); 24410Sstevel@tonic-gate goto out; 24420Sstevel@tonic-gate } 24430Sstevel@tonic-gate 24440Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 24450Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent"); 24460Sstevel@tonic-gate goto out; 24470Sstevel@tonic-gate } 24480Sstevel@tonic-gate 24490Sstevel@tonic-gate if ((rctlblk = malloc(rctlblk_size())) == NULL) { 24500Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 24510Sstevel@tonic-gate goto out; 24520Sstevel@tonic-gate } 24530Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 24540Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 24550Sstevel@tonic-gate uint_t i, count; 24560Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 24570Sstevel@tonic-gate 24580Sstevel@tonic-gate /* zoneadm should have already warned about unknown rctls. */ 24590Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 24600Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 24610Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 24620Sstevel@tonic-gate continue; 24630Sstevel@tonic-gate } 24640Sstevel@tonic-gate count = 0; 24650Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 24660Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 24670Sstevel@tonic-gate count++; 24680Sstevel@tonic-gate } 24690Sstevel@tonic-gate if (count == 0) { /* ignore */ 24700Sstevel@tonic-gate continue; /* Nothing to free */ 24710Sstevel@tonic-gate } 24720Sstevel@tonic-gate if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL) 24730Sstevel@tonic-gate goto out; 24740Sstevel@tonic-gate i = 0; 24750Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 24760Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next, i++) { 24770Sstevel@tonic-gate if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) { 24780Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", 24790Sstevel@tonic-gate "nvlist_alloc"); 24800Sstevel@tonic-gate goto out; 24810Sstevel@tonic-gate } 24820Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 24830Sstevel@tonic-gate != Z_OK) { 24840Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid rctl value: " 24850Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s)", 24860Sstevel@tonic-gate rctlval->zone_rctlval_priv, 24870Sstevel@tonic-gate rctlval->zone_rctlval_limit, 24880Sstevel@tonic-gate rctlval->zone_rctlval_action); 24890Sstevel@tonic-gate goto out; 24900Sstevel@tonic-gate } 24910Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 24920Sstevel@tonic-gate zerror(zlogp, B_FALSE, 24930Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s) is not a " 24940Sstevel@tonic-gate "valid value for rctl '%s'", 24950Sstevel@tonic-gate rctlval->zone_rctlval_priv, 24960Sstevel@tonic-gate rctlval->zone_rctlval_limit, 24970Sstevel@tonic-gate rctlval->zone_rctlval_action, 24980Sstevel@tonic-gate name); 24990Sstevel@tonic-gate goto out; 25000Sstevel@tonic-gate } 25010Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "privilege", 25021645Scomay rctlblk_get_privilege(rctlblk)) != 0) { 25030Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 25040Sstevel@tonic-gate "nvlist_add_uint64"); 25050Sstevel@tonic-gate goto out; 25060Sstevel@tonic-gate } 25070Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "limit", 25081645Scomay rctlblk_get_value(rctlblk)) != 0) { 25090Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 25100Sstevel@tonic-gate "nvlist_add_uint64"); 25110Sstevel@tonic-gate goto out; 25120Sstevel@tonic-gate } 25130Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "action", 25140Sstevel@tonic-gate (uint_t)rctlblk_get_local_action(rctlblk, NULL)) 25150Sstevel@tonic-gate != 0) { 25160Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 25170Sstevel@tonic-gate "nvlist_add_uint64"); 25180Sstevel@tonic-gate goto out; 25190Sstevel@tonic-gate } 25200Sstevel@tonic-gate } 25210Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 25220Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 25230Sstevel@tonic-gate if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count) 25240Sstevel@tonic-gate != 0) { 25250Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 25260Sstevel@tonic-gate "nvlist_add_nvlist_array"); 25270Sstevel@tonic-gate goto out; 25280Sstevel@tonic-gate } 25290Sstevel@tonic-gate for (i = 0; i < count; i++) 25300Sstevel@tonic-gate nvlist_free(nvlv[i]); 25310Sstevel@tonic-gate free(nvlv); 25320Sstevel@tonic-gate nvlv = NULL; 25330Sstevel@tonic-gate rctlcount++; 25340Sstevel@tonic-gate } 25350Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 25360Sstevel@tonic-gate 25370Sstevel@tonic-gate if (rctlcount == 0) { 25380Sstevel@tonic-gate error = 0; 25390Sstevel@tonic-gate goto out; 25400Sstevel@tonic-gate } 25410Sstevel@tonic-gate if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0) 25420Sstevel@tonic-gate != 0) { 25430Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack"); 25440Sstevel@tonic-gate goto out; 25450Sstevel@tonic-gate } 25460Sstevel@tonic-gate 25470Sstevel@tonic-gate error = 0; 25480Sstevel@tonic-gate *bufp = nvl_packed; 25490Sstevel@tonic-gate *bufsizep = nvl_size; 25500Sstevel@tonic-gate 25510Sstevel@tonic-gate out: 25520Sstevel@tonic-gate free(rctlblk); 25530Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 25540Sstevel@tonic-gate if (error && nvl_packed != NULL) 25550Sstevel@tonic-gate free(nvl_packed); 25560Sstevel@tonic-gate if (nvl != NULL) 25570Sstevel@tonic-gate nvlist_free(nvl); 25580Sstevel@tonic-gate if (nvlv != NULL) 25590Sstevel@tonic-gate free(nvlv); 25600Sstevel@tonic-gate if (handle != NULL) 25610Sstevel@tonic-gate zonecfg_fini_handle(handle); 25620Sstevel@tonic-gate return (error); 25630Sstevel@tonic-gate } 25640Sstevel@tonic-gate 25650Sstevel@tonic-gate static int 25660Sstevel@tonic-gate get_zone_pool(zlog_t *zlogp, char *poolbuf, size_t bufsz) 25670Sstevel@tonic-gate { 25680Sstevel@tonic-gate zone_dochandle_t handle; 25690Sstevel@tonic-gate int error; 25700Sstevel@tonic-gate 25710Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 25720Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 25731645Scomay return (Z_NOMEM); 25740Sstevel@tonic-gate } 25751645Scomay error = zonecfg_get_snapshot_handle(zone_name, handle); 25761645Scomay if (error != Z_OK) { 25770Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 25780Sstevel@tonic-gate zonecfg_fini_handle(handle); 25791645Scomay return (error); 25800Sstevel@tonic-gate } 25810Sstevel@tonic-gate error = zonecfg_get_pool(handle, poolbuf, bufsz); 25820Sstevel@tonic-gate zonecfg_fini_handle(handle); 25830Sstevel@tonic-gate return (error); 25840Sstevel@tonic-gate } 25850Sstevel@tonic-gate 25860Sstevel@tonic-gate static int 2587789Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2588789Sahrens { 2589789Sahrens zone_dochandle_t handle; 2590789Sahrens struct zone_dstab dstab; 2591789Sahrens size_t total, offset, len; 2592789Sahrens int error = -1; 2593789Sahrens char *str; 2594789Sahrens 2595789Sahrens *bufp = NULL; 2596789Sahrens *bufsizep = 0; 2597789Sahrens 2598789Sahrens if ((handle = zonecfg_init_handle()) == NULL) { 2599789Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2600789Sahrens return (-1); 2601789Sahrens } 2602789Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2603789Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2604789Sahrens zonecfg_fini_handle(handle); 2605789Sahrens return (-1); 2606789Sahrens } 2607789Sahrens 2608789Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2609789Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2610789Sahrens goto out; 2611789Sahrens } 2612789Sahrens 2613789Sahrens total = 0; 2614789Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) 2615789Sahrens total += strlen(dstab.zone_dataset_name) + 1; 2616789Sahrens (void) zonecfg_enddsent(handle); 2617789Sahrens 2618789Sahrens if (total == 0) { 2619789Sahrens error = 0; 2620789Sahrens goto out; 2621789Sahrens } 2622789Sahrens 2623789Sahrens if ((str = malloc(total)) == NULL) { 2624789Sahrens zerror(zlogp, B_TRUE, "memory allocation failed"); 2625789Sahrens goto out; 2626789Sahrens } 2627789Sahrens 2628789Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2629789Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2630789Sahrens goto out; 2631789Sahrens } 2632789Sahrens offset = 0; 2633789Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2634789Sahrens len = strlen(dstab.zone_dataset_name); 2635789Sahrens (void) strlcpy(str + offset, dstab.zone_dataset_name, 2636789Sahrens sizeof (dstab.zone_dataset_name) - offset); 2637789Sahrens offset += len; 2638789Sahrens if (offset != total - 1) 2639789Sahrens str[offset++] = ','; 2640789Sahrens } 2641789Sahrens (void) zonecfg_enddsent(handle); 2642789Sahrens 2643789Sahrens error = 0; 2644789Sahrens *bufp = str; 2645789Sahrens *bufsizep = total; 2646789Sahrens 2647789Sahrens out: 2648789Sahrens if (error != 0 && str != NULL) 2649789Sahrens free(str); 2650789Sahrens if (handle != NULL) 2651789Sahrens zonecfg_fini_handle(handle); 2652789Sahrens 2653789Sahrens return (error); 2654789Sahrens } 2655789Sahrens 2656789Sahrens static int 2657789Sahrens validate_datasets(zlog_t *zlogp) 2658789Sahrens { 2659789Sahrens zone_dochandle_t handle; 2660789Sahrens struct zone_dstab dstab; 2661789Sahrens zfs_handle_t *zhp; 26622082Seschrock libzfs_handle_t *hdl; 2663789Sahrens 2664789Sahrens if ((handle = zonecfg_init_handle()) == NULL) { 2665789Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2666789Sahrens return (-1); 2667789Sahrens } 2668789Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2669789Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2670789Sahrens zonecfg_fini_handle(handle); 2671789Sahrens return (-1); 2672789Sahrens } 2673789Sahrens 2674789Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 2675789Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 2676789Sahrens zonecfg_fini_handle(handle); 2677789Sahrens return (-1); 2678789Sahrens } 2679789Sahrens 26802082Seschrock if ((hdl = libzfs_init()) == NULL) { 26812082Seschrock zerror(zlogp, B_FALSE, "opening ZFS library"); 26822082Seschrock zonecfg_fini_handle(handle); 26832082Seschrock return (-1); 26842082Seschrock } 2685789Sahrens 2686789Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2687789Sahrens 26882082Seschrock if ((zhp = zfs_open(hdl, dstab.zone_dataset_name, 2689789Sahrens ZFS_TYPE_FILESYSTEM)) == NULL) { 2690789Sahrens zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'", 2691789Sahrens dstab.zone_dataset_name); 2692789Sahrens zonecfg_fini_handle(handle); 26932082Seschrock libzfs_fini(hdl); 2694789Sahrens return (-1); 2695789Sahrens } 2696789Sahrens 2697789Sahrens /* 2698789Sahrens * Automatically set the 'zoned' property. We check the value 2699789Sahrens * first because we'll get EPERM if it is already set. 2700789Sahrens */ 2701789Sahrens if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2702789Sahrens zfs_prop_set(zhp, ZFS_PROP_ZONED, "on") != 0) { 2703789Sahrens zerror(zlogp, B_FALSE, "cannot set 'zoned' " 2704789Sahrens "property for ZFS dataset '%s'\n", 2705789Sahrens dstab.zone_dataset_name); 2706789Sahrens zonecfg_fini_handle(handle); 2707789Sahrens zfs_close(zhp); 27082082Seschrock libzfs_fini(hdl); 2709789Sahrens return (-1); 2710789Sahrens } 2711789Sahrens 2712789Sahrens zfs_close(zhp); 2713789Sahrens } 2714789Sahrens (void) zonecfg_enddsent(handle); 2715789Sahrens 2716789Sahrens zonecfg_fini_handle(handle); 27172082Seschrock libzfs_fini(hdl); 2718789Sahrens 2719789Sahrens return (0); 2720789Sahrens } 2721789Sahrens 2722789Sahrens static int 27230Sstevel@tonic-gate bind_to_pool(zlog_t *zlogp, zoneid_t zoneid) 27240Sstevel@tonic-gate { 27250Sstevel@tonic-gate pool_conf_t *poolconf; 27260Sstevel@tonic-gate pool_t *pool; 27270Sstevel@tonic-gate char poolname[MAXPATHLEN]; 27280Sstevel@tonic-gate int status; 27290Sstevel@tonic-gate int error; 27300Sstevel@tonic-gate 27310Sstevel@tonic-gate /* 27320Sstevel@tonic-gate * Find the pool mentioned in the zone configuration, and bind to it. 27330Sstevel@tonic-gate */ 27340Sstevel@tonic-gate error = get_zone_pool(zlogp, poolname, sizeof (poolname)); 27350Sstevel@tonic-gate if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 27360Sstevel@tonic-gate /* 27370Sstevel@tonic-gate * The property is not set on the zone, so the pool 27380Sstevel@tonic-gate * should be bound to the default pool. But that's 27390Sstevel@tonic-gate * already done by the kernel, so we can just return. 27400Sstevel@tonic-gate */ 27410Sstevel@tonic-gate return (0); 27420Sstevel@tonic-gate } 27430Sstevel@tonic-gate if (error != Z_OK) { 27440Sstevel@tonic-gate /* 27450Sstevel@tonic-gate * Not an error, even though it shouldn't be happening. 27460Sstevel@tonic-gate */ 27470Sstevel@tonic-gate zerror(zlogp, B_FALSE, 27480Sstevel@tonic-gate "WARNING: unable to retrieve default pool."); 27490Sstevel@tonic-gate return (0); 27500Sstevel@tonic-gate } 27510Sstevel@tonic-gate /* 27520Sstevel@tonic-gate * Don't do anything if pools aren't enabled. 27530Sstevel@tonic-gate */ 27540Sstevel@tonic-gate if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 27550Sstevel@tonic-gate zerror(zlogp, B_FALSE, "WARNING: pools facility not active; " 27560Sstevel@tonic-gate "zone will not be bound to pool '%s'.", poolname); 27570Sstevel@tonic-gate return (0); 27580Sstevel@tonic-gate } 27590Sstevel@tonic-gate /* 27600Sstevel@tonic-gate * Try to provide a sane error message if the requested pool doesn't 27610Sstevel@tonic-gate * exist. 27620Sstevel@tonic-gate */ 27630Sstevel@tonic-gate if ((poolconf = pool_conf_alloc()) == NULL) { 27640Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "pool_conf_alloc"); 27650Sstevel@tonic-gate return (-1); 27660Sstevel@tonic-gate } 27670Sstevel@tonic-gate if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 27680Sstevel@tonic-gate PO_SUCCESS) { 27690Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "pool_conf_open"); 27700Sstevel@tonic-gate pool_conf_free(poolconf); 27710Sstevel@tonic-gate return (-1); 27720Sstevel@tonic-gate } 27730Sstevel@tonic-gate pool = pool_get_pool(poolconf, poolname); 27740Sstevel@tonic-gate (void) pool_conf_close(poolconf); 27750Sstevel@tonic-gate pool_conf_free(poolconf); 27760Sstevel@tonic-gate if (pool == NULL) { 27770Sstevel@tonic-gate zerror(zlogp, B_FALSE, "WARNING: pool '%s' not found; " 27780Sstevel@tonic-gate "using default pool.", poolname); 27790Sstevel@tonic-gate return (0); 27800Sstevel@tonic-gate } 27810Sstevel@tonic-gate /* 27820Sstevel@tonic-gate * Bind the zone to the pool. 27830Sstevel@tonic-gate */ 27840Sstevel@tonic-gate if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) { 27850Sstevel@tonic-gate zerror(zlogp, B_FALSE, "WARNING: unable to bind to pool '%s'; " 27860Sstevel@tonic-gate "using default pool.", poolname); 27870Sstevel@tonic-gate } 27880Sstevel@tonic-gate return (0); 27890Sstevel@tonic-gate } 27900Sstevel@tonic-gate 27911676Sjpk /* 27921676Sjpk * Mount lower level home directories into/from current zone 27931676Sjpk * Share exported directories specified in dfstab for zone 27941676Sjpk */ 27951676Sjpk static int 27961676Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath) 27971676Sjpk { 27981676Sjpk zoneid_t *zids = NULL; 27991676Sjpk priv_set_t *zid_privs; 28001676Sjpk const priv_impl_info_t *ip = NULL; 28011676Sjpk uint_t nzents_saved; 28021676Sjpk uint_t nzents; 28031676Sjpk int i; 28041676Sjpk char readonly[] = "ro"; 28051676Sjpk struct zone_fstab lower_fstab; 28061676Sjpk char *argv[4]; 28071676Sjpk 28081676Sjpk if (!is_system_labeled()) 28091676Sjpk return (0); 28101676Sjpk 28111676Sjpk if (zid_label == NULL) { 28121676Sjpk zid_label = m_label_alloc(MAC_LABEL); 28131676Sjpk if (zid_label == NULL) 28141676Sjpk return (-1); 28151676Sjpk } 28161676Sjpk 28171676Sjpk /* Make sure our zone has an /export/home dir */ 28181676Sjpk (void) make_one_dir(zlogp, rootpath, "/export/home", 28191676Sjpk DEFAULT_DIR_MODE); 28201676Sjpk 28211676Sjpk lower_fstab.zone_fs_raw[0] = '\0'; 28221676Sjpk (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS, 28231676Sjpk sizeof (lower_fstab.zone_fs_type)); 28241676Sjpk lower_fstab.zone_fs_options = NULL; 28251676Sjpk (void) zonecfg_add_fs_option(&lower_fstab, readonly); 28261676Sjpk 28271676Sjpk /* 28281676Sjpk * Get the list of zones from the kernel 28291676Sjpk */ 28301676Sjpk if (zone_list(NULL, &nzents) != 0) { 28311676Sjpk zerror(zlogp, B_TRUE, "unable to list zones"); 28321676Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 28331676Sjpk return (-1); 28341676Sjpk } 28351676Sjpk again: 28361676Sjpk if (nzents == 0) { 28371676Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 28381676Sjpk return (-1); 28391676Sjpk } 28401676Sjpk 28411676Sjpk zids = malloc(nzents * sizeof (zoneid_t)); 28421676Sjpk if (zids == NULL) { 28432267Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 28441676Sjpk return (-1); 28451676Sjpk } 28461676Sjpk nzents_saved = nzents; 28471676Sjpk 28481676Sjpk if (zone_list(zids, &nzents) != 0) { 28491676Sjpk zerror(zlogp, B_TRUE, "unable to list zones"); 28501676Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 28511676Sjpk free(zids); 28521676Sjpk return (-1); 28531676Sjpk } 28541676Sjpk if (nzents != nzents_saved) { 28551676Sjpk /* list changed, try again */ 28561676Sjpk free(zids); 28571676Sjpk goto again; 28581676Sjpk } 28591676Sjpk 28601676Sjpk ip = getprivimplinfo(); 28611676Sjpk if ((zid_privs = priv_allocset()) == NULL) { 28621676Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 28631676Sjpk zonecfg_free_fs_option_list( 28641676Sjpk lower_fstab.zone_fs_options); 28651676Sjpk free(zids); 28661676Sjpk return (-1); 28671676Sjpk } 28681676Sjpk 28691676Sjpk for (i = 0; i < nzents; i++) { 28701676Sjpk char zid_name[ZONENAME_MAX]; 28711676Sjpk zone_state_t zid_state; 28721676Sjpk char zid_rpath[MAXPATHLEN]; 28731676Sjpk struct stat stat_buf; 28741676Sjpk 28751676Sjpk if (zids[i] == GLOBAL_ZONEID) 28761676Sjpk continue; 28771676Sjpk 28781676Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 28791676Sjpk continue; 28801676Sjpk 28811676Sjpk /* 28821676Sjpk * Do special setup for the zone we are booting 28831676Sjpk */ 28841676Sjpk if (strcmp(zid_name, zone_name) == 0) { 28851676Sjpk struct zone_fstab autofs_fstab; 28861676Sjpk char map_path[MAXPATHLEN]; 28871676Sjpk int fd; 28881676Sjpk 28891676Sjpk /* 28901676Sjpk * Create auto_home_<zone> map for this zone 28911676Sjpk * in the global zone. The local zone entry 28921676Sjpk * will be created by automount when the zone 28931676Sjpk * is booted. 28941676Sjpk */ 28951676Sjpk 28961676Sjpk (void) snprintf(autofs_fstab.zone_fs_special, 28971676Sjpk MAXPATHLEN, "auto_home_%s", zid_name); 28981676Sjpk 28991676Sjpk (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN, 29001676Sjpk "/zone/%s/home", zid_name); 29011676Sjpk 29021676Sjpk (void) snprintf(map_path, sizeof (map_path), 29031676Sjpk "/etc/%s", autofs_fstab.zone_fs_special); 29041676Sjpk /* 29051676Sjpk * If the map file doesn't exist create a template 29061676Sjpk */ 29071676Sjpk if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL, 29081676Sjpk S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) { 29091676Sjpk int len; 29101676Sjpk char map_rec[MAXPATHLEN]; 29111676Sjpk 29121676Sjpk len = snprintf(map_rec, sizeof (map_rec), 29131676Sjpk "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n", 29141676Sjpk autofs_fstab.zone_fs_special, rootpath); 29151676Sjpk (void) write(fd, map_rec, len); 29161676Sjpk (void) close(fd); 29171676Sjpk } 29181676Sjpk 29191676Sjpk /* 29201676Sjpk * Mount auto_home_<zone> in the global zone if absent. 29211676Sjpk * If it's already of type autofs, then 29221676Sjpk * don't mount it again. 29231676Sjpk */ 29241676Sjpk if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) || 29251676Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) { 29261676Sjpk char optstr[] = "indirect,ignore,nobrowse"; 29271676Sjpk 29281676Sjpk (void) make_one_dir(zlogp, "", 29291676Sjpk autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE); 29301676Sjpk 29311676Sjpk /* 29321676Sjpk * Mount will fail if automounter has already 29331676Sjpk * processed the auto_home_<zonename> map 29341676Sjpk */ 29351676Sjpk (void) domount(zlogp, MNTTYPE_AUTOFS, optstr, 29361676Sjpk autofs_fstab.zone_fs_special, 29371676Sjpk autofs_fstab.zone_fs_dir); 29381676Sjpk } 29391676Sjpk continue; 29401676Sjpk } 29411676Sjpk 29421676Sjpk 29431676Sjpk if (zone_get_state(zid_name, &zid_state) != Z_OK || 29441769Scarlsonj (zid_state != ZONE_STATE_READY && 29451769Scarlsonj zid_state != ZONE_STATE_RUNNING)) 29461676Sjpk /* Skip over zones without mounted filesystems */ 29471676Sjpk continue; 29481676Sjpk 29491676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 29501676Sjpk sizeof (m_label_t)) < 0) 29511676Sjpk /* Skip over zones with unspecified label */ 29521676Sjpk continue; 29531676Sjpk 29541676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 29551676Sjpk sizeof (zid_rpath)) == -1) 29561676Sjpk /* Skip over zones with bad path */ 29571676Sjpk continue; 29581676Sjpk 29591676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs, 29601676Sjpk sizeof (priv_chunk_t) * ip->priv_setsize) == -1) 29611676Sjpk /* Skip over zones with bad privs */ 29621676Sjpk continue; 29631676Sjpk 29641676Sjpk /* 29651676Sjpk * Reading down is valid according to our label model 29661676Sjpk * but some customers want to disable it because it 29671676Sjpk * allows execute down and other possible attacks. 29681676Sjpk * Therefore, we restrict this feature to zones that 29691676Sjpk * have the NET_MAC_AWARE privilege which is required 29701676Sjpk * for NFS read-down semantics. 29711676Sjpk */ 29721676Sjpk if ((bldominates(zlabel, zid_label)) && 29731676Sjpk (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) { 29741676Sjpk /* 29751676Sjpk * Our zone dominates this one. 29761676Sjpk * Create a lofs mount from lower zone's /export/home 29771676Sjpk */ 29781676Sjpk (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 29791676Sjpk "%s/zone/%s/export/home", rootpath, zid_name); 29801676Sjpk 29811676Sjpk /* 29821676Sjpk * If the target is already an LOFS mount 29831676Sjpk * then don't do it again. 29841676Sjpk */ 29851676Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 29861676Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 29871676Sjpk 29881676Sjpk if (snprintf(lower_fstab.zone_fs_special, 29891676Sjpk MAXPATHLEN, "%s/export", 29901676Sjpk zid_rpath) > MAXPATHLEN) 29911676Sjpk continue; 29921676Sjpk 29931676Sjpk /* 29941676Sjpk * Make sure the lower-level home exists 29951676Sjpk */ 29961676Sjpk if (make_one_dir(zlogp, 29971676Sjpk lower_fstab.zone_fs_special, 29981676Sjpk "/home", DEFAULT_DIR_MODE) != 0) 29991676Sjpk continue; 30001676Sjpk 30011676Sjpk (void) strlcat(lower_fstab.zone_fs_special, 30021676Sjpk "/home", MAXPATHLEN); 30031676Sjpk 30041676Sjpk /* 30051676Sjpk * Mount can fail because the lower-level 30061676Sjpk * zone may have already done a mount up. 30071676Sjpk */ 30081676Sjpk (void) mount_one(zlogp, &lower_fstab, ""); 30091676Sjpk } 30101676Sjpk } else if ((bldominates(zid_label, zlabel)) && 30111676Sjpk (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) { 30121676Sjpk /* 30131676Sjpk * This zone dominates our zone. 30141676Sjpk * Create a lofs mount from our zone's /export/home 30151676Sjpk */ 30161676Sjpk if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 30171676Sjpk "%s/zone/%s/export/home", zid_rpath, 30181676Sjpk zone_name) > MAXPATHLEN) 30191676Sjpk continue; 30201676Sjpk 30211676Sjpk /* 30221676Sjpk * If the target is already an LOFS mount 30231676Sjpk * then don't do it again. 30241676Sjpk */ 30251676Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 30261676Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 30271676Sjpk 30281676Sjpk (void) snprintf(lower_fstab.zone_fs_special, 30291676Sjpk MAXPATHLEN, "%s/export/home", rootpath); 30301676Sjpk 30311676Sjpk /* 30321676Sjpk * Mount can fail because the higher-level 30331676Sjpk * zone may have already done a mount down. 30341676Sjpk */ 30351676Sjpk (void) mount_one(zlogp, &lower_fstab, ""); 30361676Sjpk } 30371676Sjpk } 30381676Sjpk } 30391676Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 30401676Sjpk priv_freeset(zid_privs); 30411676Sjpk free(zids); 30421676Sjpk 30431676Sjpk /* 30441676Sjpk * Now share any exported directories from this zone. 30451676Sjpk * Each zone can have its own dfstab. 30461676Sjpk */ 30471676Sjpk 30481676Sjpk argv[0] = "zoneshare"; 30491676Sjpk argv[1] = "-z"; 30501676Sjpk argv[2] = zone_name; 30511676Sjpk argv[3] = NULL; 30521676Sjpk 30531676Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv); 30541676Sjpk /* Don't check for errors since they don't affect the zone */ 30551676Sjpk 30561676Sjpk return (0); 30571676Sjpk } 30581676Sjpk 30591676Sjpk /* 30601676Sjpk * Unmount lofs mounts from higher level zones 30611676Sjpk * Unshare nfs exported directories 30621676Sjpk */ 30631676Sjpk static void 30641676Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name) 30651676Sjpk { 30661676Sjpk zoneid_t *zids = NULL; 30671676Sjpk uint_t nzents_saved; 30681676Sjpk uint_t nzents; 30691676Sjpk int i; 30701676Sjpk char *argv[4]; 30711676Sjpk char path[MAXPATHLEN]; 30721676Sjpk 30731676Sjpk if (!is_system_labeled()) 30741676Sjpk return; 30751676Sjpk 30761676Sjpk /* 30771676Sjpk * Get the list of zones from the kernel 30781676Sjpk */ 30791676Sjpk if (zone_list(NULL, &nzents) != 0) { 30801676Sjpk return; 30811676Sjpk } 30821676Sjpk 30831676Sjpk if (zid_label == NULL) { 30841676Sjpk zid_label = m_label_alloc(MAC_LABEL); 30851676Sjpk if (zid_label == NULL) 30861676Sjpk return; 30871676Sjpk } 30881676Sjpk 30891676Sjpk again: 30901676Sjpk if (nzents == 0) 30911676Sjpk return; 30921676Sjpk 30931676Sjpk zids = malloc(nzents * sizeof (zoneid_t)); 30941676Sjpk if (zids == NULL) { 30952267Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 30961676Sjpk return; 30971676Sjpk } 30981676Sjpk nzents_saved = nzents; 30991676Sjpk 31001676Sjpk if (zone_list(zids, &nzents) != 0) { 31011676Sjpk free(zids); 31021676Sjpk return; 31031676Sjpk } 31041676Sjpk if (nzents != nzents_saved) { 31051676Sjpk /* list changed, try again */ 31061676Sjpk free(zids); 31071676Sjpk goto again; 31081676Sjpk } 31091676Sjpk 31101676Sjpk for (i = 0; i < nzents; i++) { 31111676Sjpk char zid_name[ZONENAME_MAX]; 31121676Sjpk zone_state_t zid_state; 31131676Sjpk char zid_rpath[MAXPATHLEN]; 31141676Sjpk 31151676Sjpk if (zids[i] == GLOBAL_ZONEID) 31161676Sjpk continue; 31171676Sjpk 31181676Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 31191676Sjpk continue; 31201676Sjpk 31211676Sjpk /* 31221676Sjpk * Skip the zone we are halting 31231676Sjpk */ 31241676Sjpk if (strcmp(zid_name, zone_name) == 0) 31251676Sjpk continue; 31261676Sjpk 31271676Sjpk if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state, 31281676Sjpk sizeof (zid_state)) < 0) || 31291676Sjpk (zid_state < ZONE_IS_READY)) 31301676Sjpk /* Skip over zones without mounted filesystems */ 31311676Sjpk continue; 31321676Sjpk 31331676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 31341676Sjpk sizeof (m_label_t)) < 0) 31351676Sjpk /* Skip over zones with unspecified label */ 31361676Sjpk continue; 31371676Sjpk 31381676Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 31391676Sjpk sizeof (zid_rpath)) == -1) 31401676Sjpk /* Skip over zones with bad path */ 31411676Sjpk continue; 31421676Sjpk 31431676Sjpk if (zlabel != NULL && bldominates(zid_label, zlabel)) { 31441676Sjpk /* 31451676Sjpk * This zone dominates our zone. 31461676Sjpk * Unmount the lofs mount of our zone's /export/home 31471676Sjpk */ 31481676Sjpk 31491676Sjpk if (snprintf(path, MAXPATHLEN, 31501676Sjpk "%s/zone/%s/export/home", zid_rpath, 31511676Sjpk zone_name) > MAXPATHLEN) 31521676Sjpk continue; 31531676Sjpk 31541676Sjpk /* Skip over mount failures */ 31551676Sjpk (void) umount(path); 31561676Sjpk } 31571676Sjpk } 31581676Sjpk free(zids); 31591676Sjpk 31601676Sjpk /* 31611676Sjpk * Unmount global zone autofs trigger for this zone 31621676Sjpk */ 31631676Sjpk (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name); 31641676Sjpk /* Skip over mount failures */ 31651676Sjpk (void) umount(path); 31661676Sjpk 31671676Sjpk /* 31681676Sjpk * Next unshare any exported directories from this zone. 31691676Sjpk */ 31701676Sjpk 31711676Sjpk argv[0] = "zoneunshare"; 31721676Sjpk argv[1] = "-z"; 31731676Sjpk argv[2] = zone_name; 31741676Sjpk argv[3] = NULL; 31751676Sjpk 31761676Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv); 31771676Sjpk /* Don't check for errors since they don't affect the zone */ 31781676Sjpk 31791676Sjpk /* 31801676Sjpk * Finally, deallocate any devices in the zone. 31811676Sjpk */ 31821676Sjpk 31831676Sjpk argv[0] = "deallocate"; 31841676Sjpk argv[1] = "-Isz"; 31851676Sjpk argv[2] = zone_name; 31861676Sjpk argv[3] = NULL; 31871676Sjpk 31881676Sjpk (void) forkexec(zlogp, "/usr/sbin/deallocate", argv); 31891676Sjpk /* Don't check for errors since they don't affect the zone */ 31901676Sjpk } 31911676Sjpk 31921676Sjpk /* 31931676Sjpk * Fetch the Trusted Extensions label and multi-level ports (MLPs) for 31941676Sjpk * this zone. 31951676Sjpk */ 31961676Sjpk static tsol_zcent_t * 31971676Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs) 31981676Sjpk { 31991676Sjpk FILE *fp; 32001676Sjpk tsol_zcent_t *zcent = NULL; 32011676Sjpk char line[MAXTNZLEN]; 32021676Sjpk 32031676Sjpk if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) { 32041676Sjpk zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH); 32051676Sjpk return (NULL); 32061676Sjpk } 32071676Sjpk 32081676Sjpk while (fgets(line, sizeof (line), fp) != NULL) { 32091676Sjpk /* 32101676Sjpk * Check for malformed database 32111676Sjpk */ 32121676Sjpk if (strlen(line) == MAXTNZLEN - 1) 32131676Sjpk break; 32141676Sjpk if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL) 32151676Sjpk continue; 32161676Sjpk if (strcmp(zcent->zc_name, zone_name) == 0) 32171676Sjpk break; 32181676Sjpk tsol_freezcent(zcent); 32191676Sjpk zcent = NULL; 32201676Sjpk } 32211676Sjpk (void) fclose(fp); 32221676Sjpk 32231676Sjpk if (zcent == NULL) { 32241676Sjpk zerror(zlogp, B_FALSE, "zone requires a label assignment. " 32251676Sjpk "See tnzonecfg(4)"); 32261676Sjpk } else { 32271676Sjpk if (zlabel == NULL) 32281676Sjpk zlabel = m_label_alloc(MAC_LABEL); 32291676Sjpk /* 32301676Sjpk * Save this zone's privileges for later read-down processing 32311676Sjpk */ 32321676Sjpk if ((zprivs = priv_allocset()) == NULL) { 32331676Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 32341676Sjpk return (NULL); 32351676Sjpk } else { 32361676Sjpk priv_copyset(privs, zprivs); 32371676Sjpk } 32381676Sjpk } 32391676Sjpk return (zcent); 32401676Sjpk } 32411676Sjpk 32421676Sjpk /* 32431676Sjpk * Add the Trusted Extensions multi-level ports for this zone. 32441676Sjpk */ 32451676Sjpk static void 32461676Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent) 32471676Sjpk { 32481676Sjpk tsol_mlp_t *mlp; 32491676Sjpk tsol_mlpent_t tsme; 32501676Sjpk 32511676Sjpk if (!is_system_labeled()) 32521676Sjpk return; 32531676Sjpk 32541676Sjpk tsme.tsme_zoneid = zoneid; 32551676Sjpk tsme.tsme_flags = 0; 32561676Sjpk for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) { 32571676Sjpk tsme.tsme_mlp = *mlp; 32581676Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) { 32591676Sjpk zerror(zlogp, B_TRUE, "cannot set zone-specific MLP " 32601676Sjpk "on %d-%d/%d", mlp->mlp_port, 32611676Sjpk mlp->mlp_port_upper, mlp->mlp_ipp); 32621676Sjpk } 32631676Sjpk } 32641676Sjpk 32651676Sjpk tsme.tsme_flags = TSOL_MEF_SHARED; 32661676Sjpk for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) { 32671676Sjpk tsme.tsme_mlp = *mlp; 32681676Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) { 32691676Sjpk zerror(zlogp, B_TRUE, "cannot set shared MLP " 32701676Sjpk "on %d-%d/%d", mlp->mlp_port, 32711676Sjpk mlp->mlp_port_upper, mlp->mlp_ipp); 32721676Sjpk } 32731676Sjpk } 32741676Sjpk } 32751676Sjpk 32761676Sjpk static void 32771676Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid) 32781676Sjpk { 32791676Sjpk tsol_mlpent_t tsme; 32801676Sjpk 32811676Sjpk if (!is_system_labeled()) 32821676Sjpk return; 32831676Sjpk 32841676Sjpk (void) memset(&tsme, 0, sizeof (tsme)); 32851676Sjpk tsme.tsme_zoneid = zoneid; 32861676Sjpk if (tnmlp(TNDB_FLUSH, &tsme) != 0) 32871676Sjpk zerror(zlogp, B_TRUE, "cannot flush MLPs"); 32881676Sjpk } 32891676Sjpk 32900Sstevel@tonic-gate int 32910Sstevel@tonic-gate prtmount(const char *fs, void *x) { 32920Sstevel@tonic-gate zerror((zlog_t *)x, B_FALSE, " %s", fs); 32930Sstevel@tonic-gate return (0); 32940Sstevel@tonic-gate } 32950Sstevel@tonic-gate 3296766Scarlsonj /* 3297766Scarlsonj * Look for zones running on the main system that are using this root (or any 3298766Scarlsonj * subdirectory of it). Return B_TRUE and print an error if a conflicting zone 3299766Scarlsonj * is found or if we can't tell. 3300766Scarlsonj */ 3301766Scarlsonj static boolean_t 3302766Scarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath) 33030Sstevel@tonic-gate { 3304766Scarlsonj zoneid_t *zids = NULL; 3305766Scarlsonj uint_t nzids = 0; 3306766Scarlsonj boolean_t retv; 3307766Scarlsonj int rlen, zlen; 3308766Scarlsonj char zroot[MAXPATHLEN]; 3309766Scarlsonj char zonename[ZONENAME_MAX]; 3310766Scarlsonj 3311766Scarlsonj for (;;) { 3312766Scarlsonj nzids += 10; 3313766Scarlsonj zids = malloc(nzids * sizeof (*zids)); 3314766Scarlsonj if (zids == NULL) { 33152267Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 3316766Scarlsonj return (B_TRUE); 3317766Scarlsonj } 3318766Scarlsonj if (zone_list(zids, &nzids) == 0) 3319766Scarlsonj break; 3320766Scarlsonj free(zids); 3321766Scarlsonj } 3322766Scarlsonj retv = B_FALSE; 3323766Scarlsonj rlen = strlen(rootpath); 3324766Scarlsonj while (nzids > 0) { 3325766Scarlsonj /* 3326766Scarlsonj * Ignore errors; they just mean that the zone has disappeared 3327766Scarlsonj * while we were busy. 3328766Scarlsonj */ 3329766Scarlsonj if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot, 3330766Scarlsonj sizeof (zroot)) == -1) 3331766Scarlsonj continue; 3332766Scarlsonj zlen = strlen(zroot); 3333766Scarlsonj if (zlen > rlen) 3334766Scarlsonj zlen = rlen; 3335766Scarlsonj if (strncmp(rootpath, zroot, zlen) == 0 && 3336766Scarlsonj (zroot[zlen] == '\0' || zroot[zlen] == '/') && 3337766Scarlsonj (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) { 3338766Scarlsonj if (getzonenamebyid(zids[nzids], zonename, 3339766Scarlsonj sizeof (zonename)) == -1) 3340766Scarlsonj (void) snprintf(zonename, sizeof (zonename), 3341766Scarlsonj "id %d", (int)zids[nzids]); 3342766Scarlsonj zerror(zlogp, B_FALSE, 3343766Scarlsonj "zone root %s already in use by zone %s", 3344766Scarlsonj rootpath, zonename); 3345766Scarlsonj retv = B_TRUE; 3346766Scarlsonj break; 3347766Scarlsonj } 3348766Scarlsonj } 3349766Scarlsonj free(zids); 3350766Scarlsonj return (retv); 3351766Scarlsonj } 3352766Scarlsonj 3353766Scarlsonj /* 3354766Scarlsonj * Search for loopback mounts that use this same source node (same device and 3355766Scarlsonj * inode). Return B_TRUE if there is one or if we can't tell. 3356766Scarlsonj */ 3357766Scarlsonj static boolean_t 3358766Scarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath) 3359766Scarlsonj { 3360766Scarlsonj struct stat64 rst, zst; 3361766Scarlsonj struct mnttab *mnp; 3362766Scarlsonj 3363766Scarlsonj if (stat64(rootpath, &rst) == -1) { 3364766Scarlsonj zerror(zlogp, B_TRUE, "can't stat %s", rootpath); 3365766Scarlsonj return (B_TRUE); 3366766Scarlsonj } 3367766Scarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 3368766Scarlsonj return (B_TRUE); 3369766Scarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) { 3370766Scarlsonj if (mnp->mnt_fstype == NULL || 3371766Scarlsonj strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0) 3372766Scarlsonj continue; 3373766Scarlsonj /* We're looking at a loopback mount. Stat it. */ 3374766Scarlsonj if (mnp->mnt_special != NULL && 3375766Scarlsonj stat64(mnp->mnt_special, &zst) != -1 && 3376766Scarlsonj rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) { 3377766Scarlsonj zerror(zlogp, B_FALSE, 3378766Scarlsonj "zone root %s is reachable through %s", 3379766Scarlsonj rootpath, mnp->mnt_mountp); 3380766Scarlsonj return (B_TRUE); 3381766Scarlsonj } 3382766Scarlsonj } 3383766Scarlsonj return (B_FALSE); 3384766Scarlsonj } 3385766Scarlsonj 3386766Scarlsonj zoneid_t 3387766Scarlsonj vplat_create(zlog_t *zlogp, boolean_t mount_cmd) 3388766Scarlsonj { 3389766Scarlsonj zoneid_t rval = -1; 33900Sstevel@tonic-gate priv_set_t *privs; 33910Sstevel@tonic-gate char rootpath[MAXPATHLEN]; 33920Sstevel@tonic-gate char *rctlbuf = NULL; 3393766Scarlsonj size_t rctlbufsz = 0; 3394789Sahrens char *zfsbuf = NULL; 3395789Sahrens size_t zfsbufsz = 0; 3396766Scarlsonj zoneid_t zoneid = -1; 33970Sstevel@tonic-gate int xerr; 3398766Scarlsonj char *kzone; 3399766Scarlsonj FILE *fp = NULL; 34001676Sjpk tsol_zcent_t *zcent = NULL; 34011676Sjpk int match = 0; 34021676Sjpk int doi = 0; 34030Sstevel@tonic-gate 34040Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 34050Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 34060Sstevel@tonic-gate return (-1); 34070Sstevel@tonic-gate } 3408766Scarlsonj if (zonecfg_in_alt_root()) 3409766Scarlsonj resolve_lofs(zlogp, rootpath, sizeof (rootpath)); 34100Sstevel@tonic-gate 34110Sstevel@tonic-gate if ((privs = priv_allocset()) == NULL) { 34120Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 34130Sstevel@tonic-gate return (-1); 34140Sstevel@tonic-gate } 34150Sstevel@tonic-gate priv_emptyset(privs); 34161645Scomay if (get_privset(zlogp, privs, mount_cmd) != 0) 34170Sstevel@tonic-gate goto error; 34181645Scomay 3419766Scarlsonj if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) { 34200Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Unable to get list of rctls"); 34210Sstevel@tonic-gate goto error; 34220Sstevel@tonic-gate } 34231645Scomay 3424789Sahrens if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) { 3425789Sahrens zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets"); 3426789Sahrens goto error; 3427789Sahrens } 34280Sstevel@tonic-gate 34291769Scarlsonj if (!mount_cmd && is_system_labeled()) { 34301676Sjpk zcent = get_zone_label(zlogp, privs); 34311769Scarlsonj if (zcent != NULL) { 34321676Sjpk match = zcent->zc_match; 34331676Sjpk doi = zcent->zc_doi; 34341676Sjpk *zlabel = zcent->zc_label; 34351676Sjpk } else { 34361676Sjpk goto error; 34371676Sjpk } 34381676Sjpk } 34391676Sjpk 3440766Scarlsonj kzone = zone_name; 3441766Scarlsonj 3442766Scarlsonj /* 3443766Scarlsonj * We must do this scan twice. First, we look for zones running on the 3444766Scarlsonj * main system that are using this root (or any subdirectory of it). 3445766Scarlsonj * Next, we reduce to the shortest path and search for loopback mounts 3446766Scarlsonj * that use this same source node (same device and inode). 3447766Scarlsonj */ 3448766Scarlsonj if (duplicate_zone_root(zlogp, rootpath)) 3449766Scarlsonj goto error; 3450766Scarlsonj if (duplicate_reachable_path(zlogp, rootpath)) 3451766Scarlsonj goto error; 3452766Scarlsonj 3453766Scarlsonj if (mount_cmd) { 3454766Scarlsonj root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE); 3455766Scarlsonj 3456766Scarlsonj /* 3457766Scarlsonj * Forge up a special root for this zone. When a zone is 3458766Scarlsonj * mounted, we can't let the zone have its own root because the 3459766Scarlsonj * tools that will be used in this "scratch zone" need access 3460766Scarlsonj * to both the zone's resources and the running machine's 3461766Scarlsonj * executables. 3462766Scarlsonj * 3463766Scarlsonj * Note that the mkdir here also catches read-only filesystems. 3464766Scarlsonj */ 3465766Scarlsonj if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) { 3466766Scarlsonj zerror(zlogp, B_TRUE, "cannot create %s", rootpath); 3467766Scarlsonj goto error; 3468766Scarlsonj } 3469766Scarlsonj if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0) 3470766Scarlsonj goto error; 3471766Scarlsonj } 3472766Scarlsonj 3473766Scarlsonj if (zonecfg_in_alt_root()) { 3474766Scarlsonj /* 3475766Scarlsonj * If we are mounting up a zone in an alternate root partition, 3476766Scarlsonj * then we have some additional work to do before starting the 3477766Scarlsonj * zone. First, resolve the root path down so that we're not 3478766Scarlsonj * fooled by duplicates. Then forge up an internal name for 3479766Scarlsonj * the zone. 3480766Scarlsonj */ 3481766Scarlsonj if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) { 3482766Scarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile"); 3483766Scarlsonj goto error; 3484766Scarlsonj } 3485766Scarlsonj if (zonecfg_lock_scratch(fp) != 0) { 3486766Scarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3487766Scarlsonj goto error; 3488766Scarlsonj } 3489766Scarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3490766Scarlsonj NULL, 0) == 0) { 3491766Scarlsonj zerror(zlogp, B_FALSE, "scratch zone already running"); 3492766Scarlsonj goto error; 3493766Scarlsonj } 3494766Scarlsonj /* This is the preferred name */ 3495766Scarlsonj (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s", 3496766Scarlsonj zone_name); 3497766Scarlsonj srandom(getpid()); 3498766Scarlsonj while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL, 3499766Scarlsonj 0) == 0) { 3500766Scarlsonj /* This is just an arbitrary name; note "." usage */ 3501766Scarlsonj (void) snprintf(kernzone, sizeof (kernzone), 3502766Scarlsonj "SUNWlu.%08lX%08lX", random(), random()); 3503766Scarlsonj } 3504766Scarlsonj kzone = kernzone; 3505766Scarlsonj } 3506766Scarlsonj 35070Sstevel@tonic-gate xerr = 0; 3508766Scarlsonj if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf, 35091676Sjpk rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel)) == -1) { 35100Sstevel@tonic-gate if (xerr == ZE_AREMOUNTS) { 35110Sstevel@tonic-gate if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) { 35120Sstevel@tonic-gate zerror(zlogp, B_FALSE, 35130Sstevel@tonic-gate "An unknown file-system is mounted on " 35140Sstevel@tonic-gate "a subdirectory of %s", rootpath); 35150Sstevel@tonic-gate } else { 35160Sstevel@tonic-gate 35170Sstevel@tonic-gate zerror(zlogp, B_FALSE, 35180Sstevel@tonic-gate "These file-systems are mounted on " 35190Sstevel@tonic-gate "subdirectories of %s:", rootpath); 35200Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, 35210Sstevel@tonic-gate prtmount, zlogp); 35220Sstevel@tonic-gate } 35230Sstevel@tonic-gate } else if (xerr == ZE_CHROOTED) { 35240Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: " 35250Sstevel@tonic-gate "cannot create a zone from a chrooted " 35260Sstevel@tonic-gate "environment", "zone_create"); 35270Sstevel@tonic-gate } else { 35280Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "zone_create"); 35290Sstevel@tonic-gate } 35300Sstevel@tonic-gate goto error; 35310Sstevel@tonic-gate } 3532766Scarlsonj 3533766Scarlsonj if (zonecfg_in_alt_root() && 3534766Scarlsonj zonecfg_add_scratch(fp, zone_name, kernzone, 3535766Scarlsonj zonecfg_get_root()) == -1) { 3536766Scarlsonj zerror(zlogp, B_TRUE, "cannot add mapfile entry"); 3537766Scarlsonj goto error; 3538766Scarlsonj } 3539766Scarlsonj 35400Sstevel@tonic-gate /* 3541766Scarlsonj * The following is a warning, not an error, and is not performed when 3542766Scarlsonj * merely mounting a zone for administrative use. 35430Sstevel@tonic-gate */ 3544766Scarlsonj if (!mount_cmd && bind_to_pool(zlogp, zoneid) != 0) 35450Sstevel@tonic-gate zerror(zlogp, B_FALSE, "WARNING: unable to bind zone to " 35460Sstevel@tonic-gate "requested pool; using default pool."); 35471769Scarlsonj if (!mount_cmd) 35481769Scarlsonj set_mlps(zlogp, zoneid, zcent); 3549766Scarlsonj rval = zoneid; 3550766Scarlsonj zoneid = -1; 3551766Scarlsonj 35520Sstevel@tonic-gate error: 3553766Scarlsonj if (zoneid != -1) 3554766Scarlsonj (void) zone_destroy(zoneid); 35550Sstevel@tonic-gate if (rctlbuf != NULL) 35560Sstevel@tonic-gate free(rctlbuf); 35570Sstevel@tonic-gate priv_freeset(privs); 3558766Scarlsonj if (fp != NULL) 3559766Scarlsonj zonecfg_close_scratch(fp); 3560766Scarlsonj lofs_discard_mnttab(); 35611676Sjpk if (zcent != NULL) 35621676Sjpk tsol_freezcent(zcent); 35630Sstevel@tonic-gate return (rval); 35640Sstevel@tonic-gate } 35650Sstevel@tonic-gate 35662303Scarlsonj /* 35672303Scarlsonj * Enter the zone and write a /etc/zones/index file there. This allows 35682303Scarlsonj * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone 35692303Scarlsonj * details from inside the zone. 35702303Scarlsonj */ 35712303Scarlsonj static void 35722303Scarlsonj write_index_file(zoneid_t zoneid) 35732303Scarlsonj { 35742303Scarlsonj FILE *zef; 35752303Scarlsonj FILE *zet; 35762303Scarlsonj struct zoneent *zep; 35772303Scarlsonj pid_t child; 35782303Scarlsonj int tmpl_fd; 35792303Scarlsonj ctid_t ct; 35802303Scarlsonj int fd; 35812303Scarlsonj char uuidstr[UUID_PRINTABLE_STRING_LENGTH]; 35822303Scarlsonj 35832303Scarlsonj /* Locate the zone entry in the global zone's index file */ 35842303Scarlsonj if ((zef = setzoneent()) == NULL) 35852303Scarlsonj return; 35862303Scarlsonj while ((zep = getzoneent_private(zef)) != NULL) { 35872303Scarlsonj if (strcmp(zep->zone_name, zone_name) == 0) 35882303Scarlsonj break; 35892303Scarlsonj free(zep); 35902303Scarlsonj } 35912303Scarlsonj endzoneent(zef); 35922303Scarlsonj if (zep == NULL) 35932303Scarlsonj return; 35942303Scarlsonj 35952303Scarlsonj if ((tmpl_fd = init_template()) == -1) { 35962303Scarlsonj free(zep); 35972303Scarlsonj return; 35982303Scarlsonj } 35992303Scarlsonj 36002303Scarlsonj if ((child = fork()) == -1) { 36012303Scarlsonj (void) ct_tmpl_clear(tmpl_fd); 36022303Scarlsonj (void) close(tmpl_fd); 36032303Scarlsonj free(zep); 36042303Scarlsonj return; 36052303Scarlsonj } 36062303Scarlsonj 36072303Scarlsonj /* parent waits for child to finish */ 36082303Scarlsonj if (child != 0) { 36092303Scarlsonj free(zep); 36102303Scarlsonj if (contract_latest(&ct) == -1) 36112303Scarlsonj ct = -1; 36122303Scarlsonj (void) ct_tmpl_clear(tmpl_fd); 36132303Scarlsonj (void) close(tmpl_fd); 36142303Scarlsonj (void) waitpid(child, NULL, 0); 36152303Scarlsonj (void) contract_abandon_id(ct); 36162303Scarlsonj return; 36172303Scarlsonj } 36182303Scarlsonj 36192303Scarlsonj /* child enters zone and sets up index file */ 36202303Scarlsonj (void) ct_tmpl_clear(tmpl_fd); 36212303Scarlsonj if (zone_enter(zoneid) != -1) { 36222303Scarlsonj (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE); 36232303Scarlsonj (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID, 36242303Scarlsonj ZONE_CONFIG_GID); 36252303Scarlsonj fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC, 36262303Scarlsonj ZONE_INDEX_MODE); 36272303Scarlsonj if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) { 36282303Scarlsonj (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID); 36292303Scarlsonj if (uuid_is_null(zep->zone_uuid)) 36302303Scarlsonj uuidstr[0] = '\0'; 36312303Scarlsonj else 36322303Scarlsonj uuid_unparse(zep->zone_uuid, uuidstr); 36332303Scarlsonj (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name, 36342303Scarlsonj zone_state_str(zep->zone_state), 36352303Scarlsonj uuidstr); 36362303Scarlsonj (void) fclose(zet); 36372303Scarlsonj } 36382303Scarlsonj } 36392303Scarlsonj _exit(0); 36402303Scarlsonj } 36412303Scarlsonj 36422503Sdp /*ARGSUSED1*/ 36432503Sdp static int 36442503Sdp devcleanup_cb(const char *path, uid_t u, gid_t g, mode_t m, const char *a, 36452503Sdp void *data) 36462503Sdp { 36472503Sdp zone_dochandle_t h = (zone_dochandle_t)data; 36482503Sdp boolean_t del; 36492503Sdp char fullpath[MAXPATHLEN]; 36502503Sdp char zonepath[MAXPATHLEN]; 36512503Sdp 36522503Sdp if (zonecfg_should_deldev(h, path, &del) == Z_OK) { 36532503Sdp if (del) { 36542503Sdp if (zonecfg_get_zonepath(h, zonepath, 36552503Sdp sizeof (zonepath)) != Z_OK) 36562503Sdp return (Z_OK); 36572503Sdp (void) snprintf(fullpath, sizeof (fullpath), 36582503Sdp "%s/dev/%s", zonepath, path); 36592503Sdp (void) unlink(fullpath); 36602503Sdp } 36612503Sdp } 36622503Sdp return (Z_OK); 36632503Sdp } 36642503Sdp 36652503Sdp /* 36662503Sdp * If needed, initiate a walk of the zone's /dev tree, looking for device 36672503Sdp * entries which need to be cleaned up: this is a wrapper around functionality 36682503Sdp * in libzonecfg which keeps track of newly-defunct device entries. 36692503Sdp */ 36702503Sdp static int 36712503Sdp devcleanup(zlog_t *zlogp) 36722503Sdp { 36732503Sdp zone_dochandle_t handle; 36742503Sdp 36752503Sdp if ((handle = zonecfg_init_handle()) == NULL) { 36762503Sdp zerror(zlogp, B_TRUE, "getting zone configuration handle"); 36772503Sdp return (-1); 36782503Sdp } 36792503Sdp 36802503Sdp /* 36812503Sdp * Note that this is a rare case when we consult the *real* zone 36822503Sdp * config handle, not a snapshot-- that's because we want to 36832503Sdp * drop the deleted-device markers out of the config once we've 36842503Sdp * purged them from the FS. 36852503Sdp */ 36862503Sdp if (zonecfg_get_handle(zone_name, handle) != Z_OK) { 36872503Sdp zerror(zlogp, B_FALSE, "invalid configuration"); 36882503Sdp zonecfg_fini_handle(handle); 36892503Sdp return (-1); 36902503Sdp } 36912503Sdp 36922503Sdp /* 36932503Sdp * Quickly check whether there is any work to do prior to scanning 36942503Sdp * all of /dev. 36952503Sdp */ 36962503Sdp if (zonecfg_has_deldevs(handle) != Z_OK) { 36972503Sdp zonecfg_fini_handle(handle); 36982503Sdp return (0); 36992503Sdp } 37002503Sdp 37012503Sdp if (zonecfg_devwalk(handle, devcleanup_cb, (void *)handle) != Z_OK) { 37022503Sdp zerror(zlogp, B_FALSE, "failed to walk devices"); 37032503Sdp zonecfg_fini_handle(handle); 37042503Sdp return (-1); 37052503Sdp } 37062503Sdp 37072503Sdp /* 37082503Sdp * We don't need to process the deleted devices more than the one time. 37092503Sdp */ 37102503Sdp (void) zonecfg_clear_deldevs(handle); 37112503Sdp (void) zonecfg_save(handle); 37122503Sdp zonecfg_fini_handle(handle); 37132503Sdp return (0); 37142503Sdp } 37152503Sdp 37160Sstevel@tonic-gate int 37172303Scarlsonj vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd, zoneid_t zoneid) 37180Sstevel@tonic-gate { 37192503Sdp 3720789Sahrens if (!mount_cmd && validate_datasets(zlogp) != 0) { 3721789Sahrens lofs_discard_mnttab(); 3722789Sahrens return (-1); 3723789Sahrens } 3724789Sahrens 37252503Sdp if (devcleanup(zlogp) != 0) { 37262503Sdp zerror(zlogp, B_TRUE, "device cleanup failed"); 37272503Sdp return (-1); 37282503Sdp } 37292503Sdp 3730766Scarlsonj if (create_dev_files(zlogp) != 0 || 3731766Scarlsonj mount_filesystems(zlogp, mount_cmd) != 0) { 3732766Scarlsonj lofs_discard_mnttab(); 37330Sstevel@tonic-gate return (-1); 3734766Scarlsonj } 3735766Scarlsonj if (!mount_cmd && (devfsadm_register(zlogp) != 0 || 3736766Scarlsonj configure_network_interfaces(zlogp) != 0)) { 3737766Scarlsonj lofs_discard_mnttab(); 37380Sstevel@tonic-gate return (-1); 3739766Scarlsonj } 37402303Scarlsonj 37412303Scarlsonj write_index_file(zoneid); 37422303Scarlsonj 3743766Scarlsonj lofs_discard_mnttab(); 37440Sstevel@tonic-gate return (0); 37450Sstevel@tonic-gate } 37460Sstevel@tonic-gate 3747766Scarlsonj static int 3748766Scarlsonj lu_root_teardown(zlog_t *zlogp) 3749766Scarlsonj { 3750766Scarlsonj char zroot[MAXPATHLEN]; 3751766Scarlsonj 3752766Scarlsonj if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 3753766Scarlsonj zerror(zlogp, B_FALSE, "unable to determine zone root"); 3754766Scarlsonj return (-1); 3755766Scarlsonj } 3756766Scarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 3757766Scarlsonj 3758766Scarlsonj /* 3759766Scarlsonj * At this point, the processes are gone, the filesystems (save the 3760766Scarlsonj * root) are unmounted, and the zone is on death row. But there may 3761766Scarlsonj * still be creds floating about in the system that reference the 3762766Scarlsonj * zone_t, and which pin down zone_rootvp causing this call to fail 3763766Scarlsonj * with EBUSY. Thus, we try for a little while before just giving up. 3764766Scarlsonj * (How I wish this were not true, and umount2 just did the right 3765766Scarlsonj * thing, or tmpfs supported MS_FORCE This is a gross hack.) 3766766Scarlsonj */ 3767766Scarlsonj if (umount2(zroot, MS_FORCE) != 0) { 3768766Scarlsonj if (errno == ENOTSUP && umount2(zroot, 0) == 0) 3769766Scarlsonj goto unmounted; 3770766Scarlsonj if (errno == EBUSY) { 3771766Scarlsonj int tries = 10; 3772766Scarlsonj 3773766Scarlsonj while (--tries >= 0) { 3774766Scarlsonj (void) sleep(1); 3775766Scarlsonj if (umount2(zroot, 0) == 0) 3776766Scarlsonj goto unmounted; 3777766Scarlsonj if (errno != EBUSY) 3778766Scarlsonj break; 3779766Scarlsonj } 3780766Scarlsonj } 3781766Scarlsonj zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot); 3782766Scarlsonj return (-1); 3783766Scarlsonj } 3784766Scarlsonj unmounted: 3785766Scarlsonj 3786766Scarlsonj /* 3787766Scarlsonj * Only zones in an alternate root environment have scratch zone 3788766Scarlsonj * entries. 3789766Scarlsonj */ 3790766Scarlsonj if (zonecfg_in_alt_root()) { 3791766Scarlsonj FILE *fp; 3792766Scarlsonj int retv; 3793766Scarlsonj 3794766Scarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3795766Scarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile"); 3796766Scarlsonj return (-1); 3797766Scarlsonj } 3798766Scarlsonj retv = -1; 3799766Scarlsonj if (zonecfg_lock_scratch(fp) != 0) 3800766Scarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3801766Scarlsonj else if (zonecfg_delete_scratch(fp, kernzone) != 0) 3802766Scarlsonj zerror(zlogp, B_TRUE, "cannot delete map entry"); 3803766Scarlsonj else 3804766Scarlsonj retv = 0; 3805766Scarlsonj zonecfg_close_scratch(fp); 3806766Scarlsonj return (retv); 3807766Scarlsonj } else { 3808766Scarlsonj return (0); 3809766Scarlsonj } 3810766Scarlsonj } 3811766Scarlsonj 38120Sstevel@tonic-gate int 3813766Scarlsonj vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd) 38140Sstevel@tonic-gate { 3815766Scarlsonj char *kzone; 38160Sstevel@tonic-gate zoneid_t zoneid; 38170Sstevel@tonic-gate 3818766Scarlsonj kzone = zone_name; 3819766Scarlsonj if (zonecfg_in_alt_root()) { 3820766Scarlsonj FILE *fp; 3821766Scarlsonj 3822766Scarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3823766Scarlsonj zerror(zlogp, B_TRUE, "unable to open map file"); 3824766Scarlsonj goto error; 3825766Scarlsonj } 3826766Scarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3827766Scarlsonj kernzone, sizeof (kernzone)) != 0) { 3828766Scarlsonj zerror(zlogp, B_FALSE, "unable to find scratch zone"); 3829766Scarlsonj zonecfg_close_scratch(fp); 3830766Scarlsonj goto error; 3831766Scarlsonj } 3832766Scarlsonj zonecfg_close_scratch(fp); 3833766Scarlsonj kzone = kernzone; 3834766Scarlsonj } 3835766Scarlsonj 3836766Scarlsonj if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) { 38370Sstevel@tonic-gate if (!bringup_failure_recovery) 38380Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 3839766Scarlsonj if (unmount_cmd) 3840766Scarlsonj (void) lu_root_teardown(zlogp); 38410Sstevel@tonic-gate goto error; 38420Sstevel@tonic-gate } 38430Sstevel@tonic-gate 38440Sstevel@tonic-gate if (zone_shutdown(zoneid) != 0) { 38450Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to shutdown zone"); 38460Sstevel@tonic-gate goto error; 38470Sstevel@tonic-gate } 38480Sstevel@tonic-gate 3849766Scarlsonj if (!unmount_cmd && devfsadm_unregister(zlogp) != 0) 38500Sstevel@tonic-gate goto error; 38510Sstevel@tonic-gate 3852766Scarlsonj if (!unmount_cmd && 3853766Scarlsonj unconfigure_network_interfaces(zlogp, zoneid) != 0) { 38540Sstevel@tonic-gate zerror(zlogp, B_FALSE, 38550Sstevel@tonic-gate "unable to unconfigure network interfaces in zone"); 38560Sstevel@tonic-gate goto error; 38570Sstevel@tonic-gate } 38580Sstevel@tonic-gate 3859766Scarlsonj if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) { 38600Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to abort TCP connections"); 38610Sstevel@tonic-gate goto error; 38620Sstevel@tonic-gate } 38630Sstevel@tonic-gate 3864766Scarlsonj if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) { 38650Sstevel@tonic-gate zerror(zlogp, B_FALSE, 38660Sstevel@tonic-gate "unable to unmount file systems in zone"); 38670Sstevel@tonic-gate goto error; 38680Sstevel@tonic-gate } 38690Sstevel@tonic-gate 38701676Sjpk remove_mlps(zlogp, zoneid); 38711676Sjpk 38720Sstevel@tonic-gate if (zone_destroy(zoneid) != 0) { 38730Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to destroy zone"); 38740Sstevel@tonic-gate goto error; 38750Sstevel@tonic-gate } 38760Sstevel@tonic-gate 3877766Scarlsonj /* 3878766Scarlsonj * Special teardown for alternate boot environments: remove the tmpfs 3879766Scarlsonj * root for the zone and then remove it from the map file. 3880766Scarlsonj */ 3881766Scarlsonj if (unmount_cmd && lu_root_teardown(zlogp) != 0) 3882766Scarlsonj goto error; 3883766Scarlsonj 3884766Scarlsonj if (!unmount_cmd) 3885766Scarlsonj destroy_console_slave(); 3886766Scarlsonj 3887766Scarlsonj lofs_discard_mnttab(); 38880Sstevel@tonic-gate return (0); 38890Sstevel@tonic-gate 38900Sstevel@tonic-gate error: 3891766Scarlsonj lofs_discard_mnttab(); 38920Sstevel@tonic-gate return (-1); 38930Sstevel@tonic-gate } 3894