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