17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21ffbafc53Scomay 227c478bd9Sstevel@tonic-gate /* 230fbb751dSJohn Levon * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * This module contains functions used to bring up and tear down the 287c478bd9Sstevel@tonic-gate * Virtual Platform: [un]mounting file-systems, [un]plumbing network 297c478bd9Sstevel@tonic-gate * interfaces, [un]configuring devices, establishing resource controls, 307c478bd9Sstevel@tonic-gate * and creating/destroying the zone in the kernel. These actions, on 317c478bd9Sstevel@tonic-gate * the way up, ready the zone; on the way down, they halt the zone. 327c478bd9Sstevel@tonic-gate * See the much longer block comment at the beginning of zoneadmd.c 337c478bd9Sstevel@tonic-gate * for a bigger picture of how the whole program functions. 34108322fbScarlsonj * 35108322fbScarlsonj * This module also has primary responsibility for the layout of "scratch 36108322fbScarlsonj * zones." These are mounted, but inactive, zones that are used during 37108322fbScarlsonj * operating system upgrade and potentially other administrative action. The 38108322fbScarlsonj * scratch zone environment is similar to the miniroot environment. The zone's 39108322fbScarlsonj * actual root is mounted read-write on /a, and the standard paths (/usr, 40108322fbScarlsonj * /sbin, /lib) all lead to read-only copies of the running system's binaries. 41108322fbScarlsonj * This allows the administrative tools to manipulate the zone using "-R /a" 42108322fbScarlsonj * without relying on any binaries in the zone itself. 43108322fbScarlsonj * 44108322fbScarlsonj * If the scratch zone is on an alternate root (Live Upgrade [LU] boot 45108322fbScarlsonj * environment), then we must resolve the lofs mounts used there to uncover 46108322fbScarlsonj * writable (unshared) resources. Shared resources, though, are always 47108322fbScarlsonj * read-only. In addition, if the "same" zone with a different root path is 48108322fbScarlsonj * currently running, then "/b" inside the zone points to the running zone's 49108322fbScarlsonj * root. This allows LU to synchronize configuration files during the upgrade 50108322fbScarlsonj * process. 51108322fbScarlsonj * 52108322fbScarlsonj * To construct this environment, this module creates a tmpfs mount on 53108322fbScarlsonj * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as 54108322fbScarlsonj * described above is constructed on the fly. The zone is then created using 55108322fbScarlsonj * $ZONEPATH/lu as the root. 56108322fbScarlsonj * 57108322fbScarlsonj * Note that scratch zones are inactive. The zone's bits are not running and 58108322fbScarlsonj * likely cannot be run correctly until upgrade is done. Init is not running 59108322fbScarlsonj * there, nor is SMF. Because of this, the "mounted" state of a scratch zone 60108322fbScarlsonj * is not a part of the usual halt/ready/boot state machine. 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #include <sys/param.h> 647c478bd9Sstevel@tonic-gate #include <sys/mount.h> 657c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 667c478bd9Sstevel@tonic-gate #include <sys/socket.h> 677c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 687c478bd9Sstevel@tonic-gate #include <sys/types.h> 697c478bd9Sstevel@tonic-gate #include <sys/stat.h> 707c478bd9Sstevel@tonic-gate #include <sys/sockio.h> 717c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 727c478bd9Sstevel@tonic-gate #include <sys/conf.h> 735679c89fSjv227347 #include <sys/systeminfo.h> 747c478bd9Sstevel@tonic-gate 75f4b3ec61Sdh155122 #include <libdlpi.h> 76f595a68aSyz147064 #include <libdllink.h> 77d62bc4baSyz147064 #include <libdlvlan.h> 78f4b3ec61Sdh155122 797c478bd9Sstevel@tonic-gate #include <inet/tcp.h> 807c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 817c478bd9Sstevel@tonic-gate #include <netinet/in.h> 827c478bd9Sstevel@tonic-gate #include <net/route.h> 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate #include <stdio.h> 857c478bd9Sstevel@tonic-gate #include <errno.h> 867c478bd9Sstevel@tonic-gate #include <fcntl.h> 877c478bd9Sstevel@tonic-gate #include <unistd.h> 887c478bd9Sstevel@tonic-gate #include <rctl.h> 897c478bd9Sstevel@tonic-gate #include <stdlib.h> 907c478bd9Sstevel@tonic-gate #include <string.h> 917c478bd9Sstevel@tonic-gate #include <strings.h> 927c478bd9Sstevel@tonic-gate #include <wait.h> 937c478bd9Sstevel@tonic-gate #include <limits.h> 947c478bd9Sstevel@tonic-gate #include <libgen.h> 95fa9e4066Sahrens #include <libzfs.h> 96facf4a8dSllai1 #include <libdevinfo.h> 977c478bd9Sstevel@tonic-gate #include <zone.h> 987c478bd9Sstevel@tonic-gate #include <assert.h> 99555afedfScarlsonj #include <libcontract.h> 100555afedfScarlsonj #include <libcontract_priv.h> 101555afedfScarlsonj #include <uuid/uuid.h> 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate #include <sys/mntio.h> 1047c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 1057c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h> /* for _autofssys() */ 1067c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h> 107fa9e4066Sahrens #include <sys/fs/zfs.h> 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate #include <pool.h> 1107c478bd9Sstevel@tonic-gate #include <sys/pool.h> 1110209230bSgjelinek #include <sys/priocntl.h> 1127c478bd9Sstevel@tonic-gate 1139acbbeafSnn35248 #include <libbrand.h> 1149acbbeafSnn35248 #include <sys/brand.h> 1157c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 11639d3e169Sevanl #include <synch.h> 11722321485Svp157776 1187c478bd9Sstevel@tonic-gate #include "zoneadmd.h" 11945916cd2Sjpk #include <tsol/label.h> 12045916cd2Sjpk #include <libtsnet.h> 12145916cd2Sjpk #include <sys/priv.h> 122*550b6e40SSowmini Varadhan #include <libinetutil.h> 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate #define V4_ADDR_LEN 32 1257c478bd9Sstevel@tonic-gate #define V6_ADDR_LEN 128 1267c478bd9Sstevel@tonic-gate 1276e1ae2a3SGary Pennington #define RESOURCE_DEFAULT_OPTS \ 1287c478bd9Sstevel@tonic-gate MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #define DFSTYPES "/etc/dfs/fstypes" 13145916cd2Sjpk #define MAXTNZLEN 2048 1327c478bd9Sstevel@tonic-gate 1336cfd72c6Sgjelinek #define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT) 1346cfd72c6Sgjelinek 135ff19e029SMenno Lageman /* a reasonable estimate for the number of lwps per process */ 136ff19e029SMenno Lageman #define LWPS_PER_PROCESS 10 137ff19e029SMenno Lageman 1387c478bd9Sstevel@tonic-gate /* for routing socket */ 1397c478bd9Sstevel@tonic-gate static int rts_seqno = 0; 1407c478bd9Sstevel@tonic-gate 141108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */ 142108322fbScarlsonj static char kernzone[ZONENAME_MAX]; 143108322fbScarlsonj 144108322fbScarlsonj /* array of cached mount entries for resolve_lofs */ 145108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max; 146108322fbScarlsonj 14745916cd2Sjpk /* for Trusted Extensions */ 14845916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *); 14945916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *); 15045916cd2Sjpk static void tsol_unmounts(zlog_t *, char *); 1519d48116cSdh155122 15245916cd2Sjpk static m_label_t *zlabel = NULL; 15345916cd2Sjpk static m_label_t *zid_label = NULL; 15445916cd2Sjpk static priv_set_t *zprivs = NULL; 15545916cd2Sjpk 1567c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */ 1577c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *); 1587c478bd9Sstevel@tonic-gate 159c5cd6260S /* from zoneadmd */ 160c5cd6260S extern char query_hook[]; 161c5cd6260S 1627c478bd9Sstevel@tonic-gate /* 163*550b6e40SSowmini Varadhan * For each "net" resource configured in zonecfg, we track a zone_addr_list_t 164*550b6e40SSowmini Varadhan * node in a linked list that is sorted by linkid. The list is constructed as 165*550b6e40SSowmini Varadhan * the xml configuration file is parsed, and the information 166*550b6e40SSowmini Varadhan * contained in each node is added to the kernel before the zone is 167*550b6e40SSowmini Varadhan * booted, to be retrieved and applied from within the exclusive-IP NGZ 168*550b6e40SSowmini Varadhan * on boot. 169*550b6e40SSowmini Varadhan */ 170*550b6e40SSowmini Varadhan typedef struct zone_addr_list { 171*550b6e40SSowmini Varadhan struct zone_addr_list *za_next; 172*550b6e40SSowmini Varadhan datalink_id_t za_linkid; /* datalink_id_t of interface */ 173*550b6e40SSowmini Varadhan struct zone_nwiftab za_nwiftab; /* address, defrouter properties */ 174*550b6e40SSowmini Varadhan } zone_addr_list_t; 175*550b6e40SSowmini Varadhan 176*550b6e40SSowmini Varadhan /* 177108322fbScarlsonj * An optimization for build_mnttable: reallocate (and potentially copy the 178108322fbScarlsonj * data) only once every N times through the loop. 179108322fbScarlsonj */ 180108322fbScarlsonj #define MNTTAB_HUNK 32 181108322fbScarlsonj 182*550b6e40SSowmini Varadhan /* some handy macros */ 183*550b6e40SSowmini Varadhan #define SIN(s) ((struct sockaddr_in *)s) 184*550b6e40SSowmini Varadhan #define SIN6(s) ((struct sockaddr_in6 *)s) 185*550b6e40SSowmini Varadhan 186108322fbScarlsonj /* 1877c478bd9Sstevel@tonic-gate * Private autofs system call 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate static int 1927c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * Ask autofs to unmount all trigger nodes in the given zone. 1967c478bd9Sstevel@tonic-gate */ 1977c478bd9Sstevel@tonic-gate return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid)); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 200108322fbScarlsonj static void 201108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem) 202108322fbScarlsonj { 203108322fbScarlsonj uint_t i; 204108322fbScarlsonj 205108322fbScarlsonj if (mnt_array == NULL) 206108322fbScarlsonj return; 207108322fbScarlsonj for (i = 0; i < nelem; i++) { 208108322fbScarlsonj free(mnt_array[i].mnt_mountp); 209108322fbScarlsonj free(mnt_array[i].mnt_fstype); 210108322fbScarlsonj free(mnt_array[i].mnt_special); 211108322fbScarlsonj free(mnt_array[i].mnt_mntopts); 212108322fbScarlsonj assert(mnt_array[i].mnt_time == NULL); 213108322fbScarlsonj } 214108322fbScarlsonj free(mnt_array); 215108322fbScarlsonj } 216108322fbScarlsonj 217108322fbScarlsonj /* 218108322fbScarlsonj * Build the mount table for the zone rooted at "zroot", storing the resulting 219108322fbScarlsonj * array of struct mnttabs in "mnt_arrayp" and the number of elements in the 220108322fbScarlsonj * array in "nelemp". 221108322fbScarlsonj */ 222108322fbScarlsonj static int 223108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab, 224108322fbScarlsonj struct mnttab **mnt_arrayp, uint_t *nelemp) 225108322fbScarlsonj { 226108322fbScarlsonj struct mnttab mnt; 227108322fbScarlsonj struct mnttab *mnts; 228108322fbScarlsonj struct mnttab *mnp; 229108322fbScarlsonj uint_t nmnt; 230108322fbScarlsonj 231108322fbScarlsonj rewind(mnttab); 232108322fbScarlsonj resetmnttab(mnttab); 233108322fbScarlsonj nmnt = 0; 234108322fbScarlsonj mnts = NULL; 235108322fbScarlsonj while (getmntent(mnttab, &mnt) == 0) { 236108322fbScarlsonj struct mnttab *tmp_array; 237108322fbScarlsonj 238108322fbScarlsonj if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0) 239108322fbScarlsonj continue; 240108322fbScarlsonj if (nmnt % MNTTAB_HUNK == 0) { 241108322fbScarlsonj tmp_array = realloc(mnts, 242108322fbScarlsonj (nmnt + MNTTAB_HUNK) * sizeof (*mnts)); 243108322fbScarlsonj if (tmp_array == NULL) { 244108322fbScarlsonj free_mnttable(mnts, nmnt); 245108322fbScarlsonj return (-1); 246108322fbScarlsonj } 247108322fbScarlsonj mnts = tmp_array; 248108322fbScarlsonj } 249108322fbScarlsonj mnp = &mnts[nmnt++]; 250108322fbScarlsonj 251108322fbScarlsonj /* 252108322fbScarlsonj * Zero out any fields we're not using. 253108322fbScarlsonj */ 254108322fbScarlsonj (void) memset(mnp, 0, sizeof (*mnp)); 255108322fbScarlsonj 256108322fbScarlsonj if (mnt.mnt_special != NULL) 257108322fbScarlsonj mnp->mnt_special = strdup(mnt.mnt_special); 258108322fbScarlsonj if (mnt.mnt_mntopts != NULL) 259108322fbScarlsonj mnp->mnt_mntopts = strdup(mnt.mnt_mntopts); 260108322fbScarlsonj mnp->mnt_mountp = strdup(mnt.mnt_mountp); 261108322fbScarlsonj mnp->mnt_fstype = strdup(mnt.mnt_fstype); 262108322fbScarlsonj if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) || 263108322fbScarlsonj (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) || 264108322fbScarlsonj mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) { 265108322fbScarlsonj zerror(zlogp, B_TRUE, "memory allocation failed"); 266108322fbScarlsonj free_mnttable(mnts, nmnt); 267108322fbScarlsonj return (-1); 268108322fbScarlsonj } 269108322fbScarlsonj } 270108322fbScarlsonj *mnt_arrayp = mnts; 271108322fbScarlsonj *nelemp = nmnt; 272108322fbScarlsonj return (0); 273108322fbScarlsonj } 274108322fbScarlsonj 275108322fbScarlsonj /* 276108322fbScarlsonj * This is an optimization. The resolve_lofs function is used quite frequently 277108322fbScarlsonj * to manipulate file paths, and on a machine with a large number of zones, 278108322fbScarlsonj * there will be a huge number of mounted file systems. Thus, we trigger a 279108322fbScarlsonj * reread of the list of mount points 280108322fbScarlsonj */ 281108322fbScarlsonj static void 282108322fbScarlsonj lofs_discard_mnttab(void) 283108322fbScarlsonj { 284108322fbScarlsonj free_mnttable(resolve_lofs_mnts, 285108322fbScarlsonj resolve_lofs_mnt_max - resolve_lofs_mnts); 286108322fbScarlsonj resolve_lofs_mnts = resolve_lofs_mnt_max = NULL; 287108322fbScarlsonj } 288108322fbScarlsonj 289108322fbScarlsonj static int 290108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp) 291108322fbScarlsonj { 292108322fbScarlsonj FILE *mnttab; 293108322fbScarlsonj uint_t nmnts; 294108322fbScarlsonj 295108322fbScarlsonj if ((mnttab = fopen(MNTTAB, "r")) == NULL) 296108322fbScarlsonj return (-1); 297108322fbScarlsonj if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts, 298108322fbScarlsonj &nmnts) == -1) { 299108322fbScarlsonj (void) fclose(mnttab); 300108322fbScarlsonj return (-1); 301108322fbScarlsonj } 302108322fbScarlsonj (void) fclose(mnttab); 303108322fbScarlsonj resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts; 304108322fbScarlsonj return (0); 305108322fbScarlsonj } 306108322fbScarlsonj 307108322fbScarlsonj /* 308108322fbScarlsonj * This function loops over potential loopback mounts and symlinks in a given 309108322fbScarlsonj * path and resolves them all down to an absolute path. 310108322fbScarlsonj */ 311910f48daSedp void 312108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen) 313108322fbScarlsonj { 314108322fbScarlsonj int len, arlen; 315108322fbScarlsonj const char *altroot; 316108322fbScarlsonj char tmppath[MAXPATHLEN]; 317108322fbScarlsonj boolean_t outside_altroot; 318108322fbScarlsonj 319108322fbScarlsonj if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1) 320108322fbScarlsonj return; 321108322fbScarlsonj tmppath[len] = '\0'; 322108322fbScarlsonj (void) strlcpy(path, tmppath, sizeof (tmppath)); 323108322fbScarlsonj 324108322fbScarlsonj /* This happens once per zoneadmd operation. */ 325108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 326108322fbScarlsonj return; 327108322fbScarlsonj 328108322fbScarlsonj altroot = zonecfg_get_root(); 329108322fbScarlsonj arlen = strlen(altroot); 330108322fbScarlsonj outside_altroot = B_FALSE; 331108322fbScarlsonj for (;;) { 332108322fbScarlsonj struct mnttab *mnp; 333108322fbScarlsonj 3341e9d8f9fSdminer /* Search in reverse order to find longest match */ 3351e9d8f9fSdminer for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts; 3361e9d8f9fSdminer mnp--) { 337108322fbScarlsonj if (mnp->mnt_fstype == NULL || 338108322fbScarlsonj mnp->mnt_mountp == NULL || 3391e9d8f9fSdminer mnp->mnt_special == NULL) 340108322fbScarlsonj continue; 341108322fbScarlsonj len = strlen(mnp->mnt_mountp); 342108322fbScarlsonj if (strncmp(mnp->mnt_mountp, path, len) == 0 && 343108322fbScarlsonj (path[len] == '/' || path[len] == '\0')) 344108322fbScarlsonj break; 345108322fbScarlsonj } 3461e9d8f9fSdminer if (mnp < resolve_lofs_mnts) 3471e9d8f9fSdminer break; 3481e9d8f9fSdminer /* If it's not a lofs then we're done */ 3491e9d8f9fSdminer if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0) 350108322fbScarlsonj break; 351108322fbScarlsonj if (outside_altroot) { 352108322fbScarlsonj char *cp; 353108322fbScarlsonj int olen = sizeof (MNTOPT_RO) - 1; 354108322fbScarlsonj 355108322fbScarlsonj /* 356108322fbScarlsonj * If we run into a read-only mount outside of the 357108322fbScarlsonj * alternate root environment, then the user doesn't 358108322fbScarlsonj * want this path to be made read-write. 359108322fbScarlsonj */ 360108322fbScarlsonj if (mnp->mnt_mntopts != NULL && 361108322fbScarlsonj (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) != 362108322fbScarlsonj NULL && 363108322fbScarlsonj (cp == mnp->mnt_mntopts || cp[-1] == ',') && 364108322fbScarlsonj (cp[olen] == '\0' || cp[olen] == ',')) { 365108322fbScarlsonj break; 366108322fbScarlsonj } 367108322fbScarlsonj } else if (arlen > 0 && 368108322fbScarlsonj (strncmp(mnp->mnt_special, altroot, arlen) != 0 || 369108322fbScarlsonj (mnp->mnt_special[arlen] != '\0' && 370108322fbScarlsonj mnp->mnt_special[arlen] != '/'))) { 371108322fbScarlsonj outside_altroot = B_TRUE; 372108322fbScarlsonj } 373108322fbScarlsonj /* use temporary buffer because new path might be longer */ 374108322fbScarlsonj (void) snprintf(tmppath, sizeof (tmppath), "%s%s", 375108322fbScarlsonj mnp->mnt_special, path + len); 376108322fbScarlsonj if ((len = resolvepath(tmppath, path, pathlen)) == -1) 377108322fbScarlsonj break; 378108322fbScarlsonj path[len] = '\0'; 379108322fbScarlsonj } 380108322fbScarlsonj } 381108322fbScarlsonj 382108322fbScarlsonj /* 383108322fbScarlsonj * For a regular mount, check if a replacement lofs mount is needed because the 384108322fbScarlsonj * referenced device is already mounted somewhere. 385108322fbScarlsonj */ 386108322fbScarlsonj static int 387108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr) 388108322fbScarlsonj { 389108322fbScarlsonj struct mnttab *mnp; 390108322fbScarlsonj zone_fsopt_t *optptr, *onext; 391108322fbScarlsonj 392108322fbScarlsonj /* This happens once per zoneadmd operation. */ 393108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 394108322fbScarlsonj return (-1); 395108322fbScarlsonj 396108322fbScarlsonj /* 397108322fbScarlsonj * If this special node isn't already in use, then it's ours alone; 398108322fbScarlsonj * no need to worry about conflicting mounts. 399108322fbScarlsonj */ 400108322fbScarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 401108322fbScarlsonj mnp++) { 402108322fbScarlsonj if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0) 403108322fbScarlsonj break; 404108322fbScarlsonj } 405108322fbScarlsonj if (mnp >= resolve_lofs_mnt_max) 406108322fbScarlsonj return (0); 407108322fbScarlsonj 408108322fbScarlsonj /* 409108322fbScarlsonj * Convert this duplicate mount into a lofs mount. 410108322fbScarlsonj */ 411108322fbScarlsonj (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp, 412108322fbScarlsonj sizeof (fsptr->zone_fs_special)); 413108322fbScarlsonj (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS, 414108322fbScarlsonj sizeof (fsptr->zone_fs_type)); 415108322fbScarlsonj fsptr->zone_fs_raw[0] = '\0'; 416108322fbScarlsonj 417108322fbScarlsonj /* 4186e1ae2a3SGary Pennington * Discard all but one of the original options and set that to our 4196e1ae2a3SGary Pennington * default set of options used for resources. 420108322fbScarlsonj */ 421108322fbScarlsonj optptr = fsptr->zone_fs_options; 422108322fbScarlsonj if (optptr == NULL) { 423108322fbScarlsonj optptr = malloc(sizeof (*optptr)); 424108322fbScarlsonj if (optptr == NULL) { 425108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount %s", 426108322fbScarlsonj fsptr->zone_fs_dir); 427108322fbScarlsonj return (-1); 428108322fbScarlsonj } 429108322fbScarlsonj } else { 430108322fbScarlsonj while ((onext = optptr->zone_fsopt_next) != NULL) { 431108322fbScarlsonj optptr->zone_fsopt_next = onext->zone_fsopt_next; 432108322fbScarlsonj free(onext); 433108322fbScarlsonj } 434108322fbScarlsonj } 4356e1ae2a3SGary Pennington (void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS); 436108322fbScarlsonj optptr->zone_fsopt_next = NULL; 437108322fbScarlsonj fsptr->zone_fs_options = optptr; 438108322fbScarlsonj return (0); 439108322fbScarlsonj } 440108322fbScarlsonj 441d314f035Sedp int 44227e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode, 44327e6fb21Sdp uid_t userid, gid_t groupid) 4447c478bd9Sstevel@tonic-gate { 4457c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 4467c478bd9Sstevel@tonic-gate struct stat st; 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) > 4497c478bd9Sstevel@tonic-gate sizeof (path)) { 4507c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix, 4517c478bd9Sstevel@tonic-gate subdir); 4527c478bd9Sstevel@tonic-gate return (-1); 4537c478bd9Sstevel@tonic-gate } 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate if (lstat(path, &st) == 0) { 4567c478bd9Sstevel@tonic-gate /* 4577c478bd9Sstevel@tonic-gate * We don't check the file mode since presumably the zone 4587c478bd9Sstevel@tonic-gate * administrator may have had good reason to change the mode, 4597c478bd9Sstevel@tonic-gate * and we don't need to second guess him. 4607c478bd9Sstevel@tonic-gate */ 4617c478bd9Sstevel@tonic-gate if (!S_ISDIR(st.st_mode)) { 462756cf395SRic Aleshire if (S_ISREG(st.st_mode)) { 46345916cd2Sjpk /* 464756cf395SRic Aleshire * Allow readonly mounts of /etc/ files; this 465756cf395SRic Aleshire * is needed most by Trusted Extensions. 46645916cd2Sjpk */ 46745916cd2Sjpk if (strncmp(subdir, "/etc/", 46845916cd2Sjpk strlen("/etc/")) != 0) { 46945916cd2Sjpk zerror(zlogp, B_FALSE, 47045916cd2Sjpk "%s is not in /etc", path); 4717c478bd9Sstevel@tonic-gate return (-1); 4727c478bd9Sstevel@tonic-gate } 47345916cd2Sjpk } else { 47445916cd2Sjpk zerror(zlogp, B_FALSE, 47545916cd2Sjpk "%s is not a directory", path); 47645916cd2Sjpk return (-1); 47745916cd2Sjpk } 47845916cd2Sjpk } 47927e6fb21Sdp return (0); 48027e6fb21Sdp } 48127e6fb21Sdp 48227e6fb21Sdp if (mkdirp(path, mode) != 0) { 4837c478bd9Sstevel@tonic-gate if (errno == EROFS) 4847c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on " 4857c478bd9Sstevel@tonic-gate "a read-only file system in this local zone.\nMake " 4867c478bd9Sstevel@tonic-gate "sure %s exists in the global zone.", path, subdir); 4877c478bd9Sstevel@tonic-gate else 4887c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mkdirp of %s failed", path); 4897c478bd9Sstevel@tonic-gate return (-1); 4907c478bd9Sstevel@tonic-gate } 49127e6fb21Sdp 49227e6fb21Sdp (void) chown(path, userid, groupid); 4937c478bd9Sstevel@tonic-gate return (0); 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate static void 4977c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types) 4987c478bd9Sstevel@tonic-gate { 4997c478bd9Sstevel@tonic-gate uint_t i; 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate if (types == NULL) 5027c478bd9Sstevel@tonic-gate return; 5037c478bd9Sstevel@tonic-gate for (i = 0; types[i] != NULL; i++) 5047c478bd9Sstevel@tonic-gate free(types[i]); 5057c478bd9Sstevel@tonic-gate free(types); 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate static char ** 5097c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp) 5107c478bd9Sstevel@tonic-gate { 5117c478bd9Sstevel@tonic-gate char **types = NULL; 5127c478bd9Sstevel@tonic-gate FILE *fp; 5137c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN]; 5147c478bd9Sstevel@tonic-gate char fstype[MAXPATHLEN]; 5157c478bd9Sstevel@tonic-gate uint_t lines = 0; 5167c478bd9Sstevel@tonic-gate uint_t i; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate if ((fp = fopen(DFSTYPES, "r")) == NULL) { 5197c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES); 5207c478bd9Sstevel@tonic-gate return (NULL); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate /* 5237c478bd9Sstevel@tonic-gate * Count the number of lines 5247c478bd9Sstevel@tonic-gate */ 5257c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) 5267c478bd9Sstevel@tonic-gate lines++; 5277c478bd9Sstevel@tonic-gate if (lines == 0) /* didn't read anything; empty file */ 5287c478bd9Sstevel@tonic-gate goto out; 5297c478bd9Sstevel@tonic-gate rewind(fp); 5307c478bd9Sstevel@tonic-gate /* 5317c478bd9Sstevel@tonic-gate * Allocate enough space for a NULL-terminated array. 5327c478bd9Sstevel@tonic-gate */ 5337c478bd9Sstevel@tonic-gate types = calloc(lines + 1, sizeof (char *)); 5347c478bd9Sstevel@tonic-gate if (types == NULL) { 5357c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 5367c478bd9Sstevel@tonic-gate goto out; 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate i = 0; 5397c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) { 5407c478bd9Sstevel@tonic-gate /* LINTED - fstype is big enough to hold buf */ 5417c478bd9Sstevel@tonic-gate if (sscanf(buf, "%s", fstype) == 0) { 5427c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES); 5437c478bd9Sstevel@tonic-gate free_remote_fstypes(types); 5447c478bd9Sstevel@tonic-gate types = NULL; 5457c478bd9Sstevel@tonic-gate goto out; 5467c478bd9Sstevel@tonic-gate } 5477c478bd9Sstevel@tonic-gate types[i] = strdup(fstype); 5487c478bd9Sstevel@tonic-gate if (types[i] == NULL) { 5497c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 5507c478bd9Sstevel@tonic-gate free_remote_fstypes(types); 5517c478bd9Sstevel@tonic-gate types = NULL; 5527c478bd9Sstevel@tonic-gate goto out; 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate i++; 5557c478bd9Sstevel@tonic-gate } 5567c478bd9Sstevel@tonic-gate out: 5577c478bd9Sstevel@tonic-gate (void) fclose(fp); 5587c478bd9Sstevel@tonic-gate return (types); 5597c478bd9Sstevel@tonic-gate } 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate static boolean_t 5627c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes) 5637c478bd9Sstevel@tonic-gate { 5647c478bd9Sstevel@tonic-gate uint_t i; 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate if (remote_fstypes == NULL) 5677c478bd9Sstevel@tonic-gate return (B_FALSE); 5687c478bd9Sstevel@tonic-gate for (i = 0; remote_fstypes[i] != NULL; i++) { 5697c478bd9Sstevel@tonic-gate if (strcmp(remote_fstypes[i], fstype) == 0) 5707c478bd9Sstevel@tonic-gate return (B_TRUE); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate return (B_FALSE); 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate 575108322fbScarlsonj /* 576108322fbScarlsonj * This converts a zone root path (normally of the form .../root) to a Live 577108322fbScarlsonj * Upgrade scratch zone root (of the form .../lu). 578108322fbScarlsonj */ 5797c478bd9Sstevel@tonic-gate static void 580108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved) 5817c478bd9Sstevel@tonic-gate { 582108322fbScarlsonj if (!isresolved && zonecfg_in_alt_root()) 583108322fbScarlsonj resolve_lofs(zlogp, zroot, zrootlen); 584108322fbScarlsonj (void) strcpy(strrchr(zroot, '/') + 1, "lu"); 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate /* 5887c478bd9Sstevel@tonic-gate * The general strategy for unmounting filesystems is as follows: 5897c478bd9Sstevel@tonic-gate * 5907c478bd9Sstevel@tonic-gate * - Remote filesystems may be dead, and attempting to contact them as 5917c478bd9Sstevel@tonic-gate * part of a regular unmount may hang forever; we want to always try to 5927c478bd9Sstevel@tonic-gate * forcibly unmount such filesystems and only fall back to regular 5937c478bd9Sstevel@tonic-gate * unmounts if the filesystem doesn't support forced unmounts. 5947c478bd9Sstevel@tonic-gate * 5957c478bd9Sstevel@tonic-gate * - We don't want to unnecessarily corrupt metadata on local 5967c478bd9Sstevel@tonic-gate * filesystems (ie UFS), so we want to start off with graceful unmounts, 5977c478bd9Sstevel@tonic-gate * and only escalate to doing forced unmounts if we get stuck. 5987c478bd9Sstevel@tonic-gate * 5997c478bd9Sstevel@tonic-gate * We start off walking backwards through the mount table. This doesn't 6007c478bd9Sstevel@tonic-gate * give us strict ordering but ensures that we try to unmount submounts 6017c478bd9Sstevel@tonic-gate * first. We thus limit the number of failed umount2(2) calls. 6027c478bd9Sstevel@tonic-gate * 6037c478bd9Sstevel@tonic-gate * The mechanism for determining if we're stuck is to count the number 6047c478bd9Sstevel@tonic-gate * of failed unmounts each iteration through the mount table. This 6057c478bd9Sstevel@tonic-gate * gives us an upper bound on the number of filesystems which remain 6067c478bd9Sstevel@tonic-gate * mounted (autofs trigger nodes are dealt with separately). If at the 6077c478bd9Sstevel@tonic-gate * end of one unmount+autofs_cleanup cycle we still have the same number 6087c478bd9Sstevel@tonic-gate * of mounts that we started out with, we're stuck and try a forced 6097c478bd9Sstevel@tonic-gate * unmount. If that fails (filesystem doesn't support forced unmounts) 6107c478bd9Sstevel@tonic-gate * then we bail and are unable to teardown the zone. If it succeeds, 6117c478bd9Sstevel@tonic-gate * we're no longer stuck so we continue with our policy of trying 6127c478bd9Sstevel@tonic-gate * graceful mounts first. 6137c478bd9Sstevel@tonic-gate * 6147c478bd9Sstevel@tonic-gate * Zone must be down (ie, no processes or threads active). 6157c478bd9Sstevel@tonic-gate */ 6167c478bd9Sstevel@tonic-gate static int 617108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd) 6187c478bd9Sstevel@tonic-gate { 6197c478bd9Sstevel@tonic-gate int error = 0; 6207c478bd9Sstevel@tonic-gate FILE *mnttab; 6217c478bd9Sstevel@tonic-gate struct mnttab *mnts; 6227c478bd9Sstevel@tonic-gate uint_t nmnt; 6237c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN + 1]; 6247c478bd9Sstevel@tonic-gate size_t zrootlen; 6257c478bd9Sstevel@tonic-gate uint_t oldcount = UINT_MAX; 6267c478bd9Sstevel@tonic-gate boolean_t stuck = B_FALSE; 6277c478bd9Sstevel@tonic-gate char **remote_fstypes = NULL; 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 6307c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root"); 6317c478bd9Sstevel@tonic-gate return (-1); 6327c478bd9Sstevel@tonic-gate } 633108322fbScarlsonj if (unmount_cmd) 634108322fbScarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate (void) strcat(zroot, "/"); 6377c478bd9Sstevel@tonic-gate zrootlen = strlen(zroot); 6387c478bd9Sstevel@tonic-gate 63945916cd2Sjpk /* 64045916cd2Sjpk * For Trusted Extensions unmount each higher level zone's mount 64145916cd2Sjpk * of our zone's /export/home 64245916cd2Sjpk */ 64348451833Scarlsonj if (!unmount_cmd) 64445916cd2Sjpk tsol_unmounts(zlogp, zone_name); 64545916cd2Sjpk 6467c478bd9Sstevel@tonic-gate if ((mnttab = fopen(MNTTAB, "r")) == NULL) { 6477c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB); 6487c478bd9Sstevel@tonic-gate return (-1); 6497c478bd9Sstevel@tonic-gate } 6507c478bd9Sstevel@tonic-gate /* 6517c478bd9Sstevel@tonic-gate * Use our hacky mntfs ioctl so we see everything, even mounts with 6527c478bd9Sstevel@tonic-gate * MS_NOMNTTAB. 6537c478bd9Sstevel@tonic-gate */ 6547c478bd9Sstevel@tonic-gate if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 6557c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB); 6567c478bd9Sstevel@tonic-gate error++; 6577c478bd9Sstevel@tonic-gate goto out; 6587c478bd9Sstevel@tonic-gate } 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate /* 6617c478bd9Sstevel@tonic-gate * Build the list of remote fstypes so we know which ones we 6627c478bd9Sstevel@tonic-gate * should forcibly unmount. 6637c478bd9Sstevel@tonic-gate */ 6647c478bd9Sstevel@tonic-gate remote_fstypes = get_remote_fstypes(zlogp); 6657c478bd9Sstevel@tonic-gate for (; /* ever */; ) { 6667c478bd9Sstevel@tonic-gate uint_t newcount = 0; 6677c478bd9Sstevel@tonic-gate boolean_t unmounted; 6687c478bd9Sstevel@tonic-gate struct mnttab *mnp; 6697c478bd9Sstevel@tonic-gate char *path; 6707c478bd9Sstevel@tonic-gate uint_t i; 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate mnts = NULL; 6737c478bd9Sstevel@tonic-gate nmnt = 0; 6747c478bd9Sstevel@tonic-gate /* 6757c478bd9Sstevel@tonic-gate * MNTTAB gives us a way to walk through mounted 6767c478bd9Sstevel@tonic-gate * filesystems; we need to be able to walk them in 6777c478bd9Sstevel@tonic-gate * reverse order, so we build a list of all mounted 6787c478bd9Sstevel@tonic-gate * filesystems. 6797c478bd9Sstevel@tonic-gate */ 6807c478bd9Sstevel@tonic-gate if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts, 6817c478bd9Sstevel@tonic-gate &nmnt) != 0) { 6827c478bd9Sstevel@tonic-gate error++; 6837c478bd9Sstevel@tonic-gate goto out; 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate for (i = 0; i < nmnt; i++) { 6867c478bd9Sstevel@tonic-gate mnp = &mnts[nmnt - i - 1]; /* access in reverse order */ 6877c478bd9Sstevel@tonic-gate path = mnp->mnt_mountp; 6887c478bd9Sstevel@tonic-gate unmounted = B_FALSE; 6897c478bd9Sstevel@tonic-gate /* 6907c478bd9Sstevel@tonic-gate * Try forced unmount first for remote filesystems. 6917c478bd9Sstevel@tonic-gate * 6927c478bd9Sstevel@tonic-gate * Not all remote filesystems support forced unmounts, 6937c478bd9Sstevel@tonic-gate * so if this fails (ENOTSUP) we'll continue on 6947c478bd9Sstevel@tonic-gate * and try a regular unmount. 6957c478bd9Sstevel@tonic-gate */ 6967c478bd9Sstevel@tonic-gate if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) { 6977c478bd9Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) 6987c478bd9Sstevel@tonic-gate unmounted = B_TRUE; 6997c478bd9Sstevel@tonic-gate } 7007c478bd9Sstevel@tonic-gate /* 7017c478bd9Sstevel@tonic-gate * Try forced unmount if we're stuck. 7027c478bd9Sstevel@tonic-gate */ 7037c478bd9Sstevel@tonic-gate if (stuck) { 7047c478bd9Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) { 7057c478bd9Sstevel@tonic-gate unmounted = B_TRUE; 7067c478bd9Sstevel@tonic-gate stuck = B_FALSE; 7077c478bd9Sstevel@tonic-gate } else { 7087c478bd9Sstevel@tonic-gate /* 7097c478bd9Sstevel@tonic-gate * The first failure indicates a 7107c478bd9Sstevel@tonic-gate * mount we won't be able to get 7117c478bd9Sstevel@tonic-gate * rid of automatically, so we 7127c478bd9Sstevel@tonic-gate * bail. 7137c478bd9Sstevel@tonic-gate */ 7147c478bd9Sstevel@tonic-gate error++; 7157c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 7167c478bd9Sstevel@tonic-gate "unable to unmount '%s'", path); 7177c478bd9Sstevel@tonic-gate free_mnttable(mnts, nmnt); 7187c478bd9Sstevel@tonic-gate goto out; 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate } 7217c478bd9Sstevel@tonic-gate /* 7227c478bd9Sstevel@tonic-gate * Try regular unmounts for everything else. 7237c478bd9Sstevel@tonic-gate */ 7247c478bd9Sstevel@tonic-gate if (!unmounted && umount2(path, 0) != 0) 7257c478bd9Sstevel@tonic-gate newcount++; 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate free_mnttable(mnts, nmnt); 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate if (newcount == 0) 7307c478bd9Sstevel@tonic-gate break; 7317c478bd9Sstevel@tonic-gate if (newcount >= oldcount) { 7327c478bd9Sstevel@tonic-gate /* 7337c478bd9Sstevel@tonic-gate * Last round didn't unmount anything; we're stuck and 7347c478bd9Sstevel@tonic-gate * should start trying forced unmounts. 7357c478bd9Sstevel@tonic-gate */ 7367c478bd9Sstevel@tonic-gate stuck = B_TRUE; 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate oldcount = newcount; 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate /* 7417c478bd9Sstevel@tonic-gate * Autofs doesn't let you unmount its trigger nodes from 7427c478bd9Sstevel@tonic-gate * userland so we have to tell the kernel to cleanup for us. 7437c478bd9Sstevel@tonic-gate */ 7447c478bd9Sstevel@tonic-gate if (autofs_cleanup(zoneid) != 0) { 7457c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to remove autofs nodes"); 7467c478bd9Sstevel@tonic-gate error++; 7477c478bd9Sstevel@tonic-gate goto out; 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate out: 7527c478bd9Sstevel@tonic-gate free_remote_fstypes(remote_fstypes); 7537c478bd9Sstevel@tonic-gate (void) fclose(mnttab); 7547c478bd9Sstevel@tonic-gate return (error ? -1 : 0); 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate static int 7587c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2) 7597c478bd9Sstevel@tonic-gate { 7607c478bd9Sstevel@tonic-gate struct zone_fstab *i = (struct zone_fstab *)m1; 7617c478bd9Sstevel@tonic-gate struct zone_fstab *j = (struct zone_fstab *)m2; 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate return (strcmp(i->zone_fs_dir, j->zone_fs_dir)); 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate /* 7677c478bd9Sstevel@tonic-gate * Fork and exec (and wait for) the mentioned binary with the provided 7687c478bd9Sstevel@tonic-gate * arguments. Returns (-1) if something went wrong with fork(2) or exec(2), 7697c478bd9Sstevel@tonic-gate * returns the exit status otherwise. 7707c478bd9Sstevel@tonic-gate * 7717c478bd9Sstevel@tonic-gate * If we were unable to exec the provided pathname (for whatever 7727c478bd9Sstevel@tonic-gate * reason), we return the special token ZEXIT_EXEC. The current value 7737c478bd9Sstevel@tonic-gate * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the 7747c478bd9Sstevel@tonic-gate * consumers of this function; any future consumers must make sure this 7757c478bd9Sstevel@tonic-gate * remains the case. 7767c478bd9Sstevel@tonic-gate */ 7777c478bd9Sstevel@tonic-gate static int 7787c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[]) 7797c478bd9Sstevel@tonic-gate { 7807c478bd9Sstevel@tonic-gate pid_t child_pid; 7817c478bd9Sstevel@tonic-gate int child_status = 0; 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate /* 7847c478bd9Sstevel@tonic-gate * Do not let another thread localize a message while we are forking. 7857c478bd9Sstevel@tonic-gate */ 7867c478bd9Sstevel@tonic-gate (void) mutex_lock(&msglock); 7877c478bd9Sstevel@tonic-gate child_pid = fork(); 7887c478bd9Sstevel@tonic-gate (void) mutex_unlock(&msglock); 7897c478bd9Sstevel@tonic-gate if (child_pid == -1) { 7907c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]); 7917c478bd9Sstevel@tonic-gate return (-1); 7927c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { 7937c478bd9Sstevel@tonic-gate closefrom(0); 7941390a385Sgjelinek /* redirect stdin, stdout & stderr to /dev/null */ 7951390a385Sgjelinek (void) open("/dev/null", O_RDONLY); /* stdin */ 7961390a385Sgjelinek (void) open("/dev/null", O_WRONLY); /* stdout */ 7971390a385Sgjelinek (void) open("/dev/null", O_WRONLY); /* stderr */ 7987c478bd9Sstevel@tonic-gate (void) execv(path, argv); 7997c478bd9Sstevel@tonic-gate /* 8007c478bd9Sstevel@tonic-gate * Since we are in the child, there is no point calling zerror() 8017c478bd9Sstevel@tonic-gate * since there is nobody waiting to consume it. So exit with a 8027c478bd9Sstevel@tonic-gate * special code that the parent will recognize and call zerror() 8037c478bd9Sstevel@tonic-gate * accordingly. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate _exit(ZEXIT_EXEC); 8077c478bd9Sstevel@tonic-gate } else { 8087c478bd9Sstevel@tonic-gate (void) waitpid(child_pid, &child_status, 0); 8097c478bd9Sstevel@tonic-gate } 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate if (WIFSIGNALED(child_status)) { 8127c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 8137c478bd9Sstevel@tonic-gate "signal %d", path, WTERMSIG(child_status)); 8147c478bd9Sstevel@tonic-gate return (-1); 8157c478bd9Sstevel@tonic-gate } 8167c478bd9Sstevel@tonic-gate assert(WIFEXITED(child_status)); 8177c478bd9Sstevel@tonic-gate if (WEXITSTATUS(child_status) == ZEXIT_EXEC) { 8187c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "failed to exec %s", path); 8197c478bd9Sstevel@tonic-gate return (-1); 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate return (WEXITSTATUS(child_status)); 8227c478bd9Sstevel@tonic-gate } 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate static int 82593239addSjohnlev isregfile(const char *path) 82693239addSjohnlev { 82793239addSjohnlev struct stat64 st; 82893239addSjohnlev 82993239addSjohnlev if (stat64(path, &st) == -1) 83093239addSjohnlev return (-1); 83193239addSjohnlev 83293239addSjohnlev return (S_ISREG(st.st_mode)); 83393239addSjohnlev } 83493239addSjohnlev 83593239addSjohnlev static int 8367c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev) 8377c478bd9Sstevel@tonic-gate { 8387c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 83957a250fbSbatschul char *argv[5]; 8407c478bd9Sstevel@tonic-gate int status; 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate /* 8437c478bd9Sstevel@tonic-gate * We could alternatively have called /usr/sbin/fsck -F <fstype>, but 8447c478bd9Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything. 8457c478bd9Sstevel@tonic-gate */ 8467c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype) 8479acbbeafSnn35248 >= sizeof (cmdbuf)) { 8487c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 8497c478bd9Sstevel@tonic-gate return (-1); 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate 85293239addSjohnlev /* 85393239addSjohnlev * If it doesn't exist, that's OK: we verified this previously 85493239addSjohnlev * in zoneadm. 85593239addSjohnlev */ 85693239addSjohnlev if (isregfile(cmdbuf) == -1) 85793239addSjohnlev return (0); 85893239addSjohnlev 8597c478bd9Sstevel@tonic-gate argv[0] = "fsck"; 86057a250fbSbatschul argv[1] = "-o"; 86157a250fbSbatschul argv[2] = "p"; 86257a250fbSbatschul argv[3] = (char *)rawdev; 86357a250fbSbatschul argv[4] = NULL; 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv); 8667c478bd9Sstevel@tonic-gate if (status == 0 || status == -1) 8677c478bd9Sstevel@tonic-gate return (status); 8687c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; " 8697c478bd9Sstevel@tonic-gate "run fsck manually", rawdev, status); 8707c478bd9Sstevel@tonic-gate return (-1); 8717c478bd9Sstevel@tonic-gate } 8727c478bd9Sstevel@tonic-gate 8737c478bd9Sstevel@tonic-gate static int 8747c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts, 8757c478bd9Sstevel@tonic-gate const char *special, const char *directory) 8767c478bd9Sstevel@tonic-gate { 8777c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN]; 8787c478bd9Sstevel@tonic-gate char *argv[6]; 8797c478bd9Sstevel@tonic-gate int status; 8807c478bd9Sstevel@tonic-gate 8817c478bd9Sstevel@tonic-gate /* 8827c478bd9Sstevel@tonic-gate * We could alternatively have called /usr/sbin/mount -F <fstype>, but 8837c478bd9Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything. 8847c478bd9Sstevel@tonic-gate */ 8857c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype) 8869acbbeafSnn35248 >= sizeof (cmdbuf)) { 8877c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 8887c478bd9Sstevel@tonic-gate return (-1); 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate argv[0] = "mount"; 8917c478bd9Sstevel@tonic-gate if (opts[0] == '\0') { 8927c478bd9Sstevel@tonic-gate argv[1] = (char *)special; 8937c478bd9Sstevel@tonic-gate argv[2] = (char *)directory; 8947c478bd9Sstevel@tonic-gate argv[3] = NULL; 8957c478bd9Sstevel@tonic-gate } else { 8967c478bd9Sstevel@tonic-gate argv[1] = "-o"; 8977c478bd9Sstevel@tonic-gate argv[2] = (char *)opts; 8987c478bd9Sstevel@tonic-gate argv[3] = (char *)special; 8997c478bd9Sstevel@tonic-gate argv[4] = (char *)directory; 9007c478bd9Sstevel@tonic-gate argv[5] = NULL; 9017c478bd9Sstevel@tonic-gate } 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv); 9047c478bd9Sstevel@tonic-gate if (status == 0 || status == -1) 9057c478bd9Sstevel@tonic-gate return (status); 9067c478bd9Sstevel@tonic-gate if (opts[0] == '\0') 9077c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s %s %s\" " 9087c478bd9Sstevel@tonic-gate "failed with exit code %d", 9097c478bd9Sstevel@tonic-gate cmdbuf, special, directory, status); 9107c478bd9Sstevel@tonic-gate else 9117c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" " 9127c478bd9Sstevel@tonic-gate "failed with exit code %d", 9137c478bd9Sstevel@tonic-gate cmdbuf, opts, special, directory, status); 9147c478bd9Sstevel@tonic-gate return (-1); 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate /* 918d314f035Sedp * Check if a given mount point path exists. 919d314f035Sedp * If it does, make sure it doesn't contain any symlinks. 920d314f035Sedp * Note that if "leaf" is false we're checking an intermediate 921d314f035Sedp * component of the mount point path, so it must be a directory. 922d314f035Sedp * If "leaf" is true, then we're checking the entire mount point 923d314f035Sedp * path, so the mount point itself can be anything aside from a 924d314f035Sedp * symbolic link. 925d314f035Sedp * 926d314f035Sedp * If the path is invalid then a negative value is returned. If the 927d314f035Sedp * path exists and is a valid mount point path then 0 is returned. 928d314f035Sedp * If the path doesn't exist return a positive value. 9297c478bd9Sstevel@tonic-gate */ 9307c478bd9Sstevel@tonic-gate static int 931d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf) 9327c478bd9Sstevel@tonic-gate { 9337c478bd9Sstevel@tonic-gate struct stat statbuf; 9347c478bd9Sstevel@tonic-gate char respath[MAXPATHLEN]; 9357c478bd9Sstevel@tonic-gate int res; 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate if (lstat(path, &statbuf) != 0) { 9387c478bd9Sstevel@tonic-gate if (errno == ENOENT) 939d314f035Sedp return (1); 9407c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "can't stat %s", path); 9417c478bd9Sstevel@tonic-gate return (-1); 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate if (S_ISLNK(statbuf.st_mode)) { 9447c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is a symlink", path); 9457c478bd9Sstevel@tonic-gate return (-1); 9467c478bd9Sstevel@tonic-gate } 947d314f035Sedp if (!leaf && !S_ISDIR(statbuf.st_mode)) { 9487c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not a directory", path); 9497c478bd9Sstevel@tonic-gate return (-1); 9507c478bd9Sstevel@tonic-gate } 9517c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, respath, sizeof (respath))) == -1) { 9527c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to resolve path %s", path); 9537c478bd9Sstevel@tonic-gate return (-1); 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate respath[res] = '\0'; 9567c478bd9Sstevel@tonic-gate if (strcmp(path, respath) != 0) { 9577c478bd9Sstevel@tonic-gate /* 958d314f035Sedp * We don't like ".."s, "."s, or "//"s throwing us off 9597c478bd9Sstevel@tonic-gate */ 9607c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not a canonical path", path); 9617c478bd9Sstevel@tonic-gate return (-1); 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate return (0); 9647c478bd9Sstevel@tonic-gate } 9657c478bd9Sstevel@tonic-gate 9667c478bd9Sstevel@tonic-gate /* 967d314f035Sedp * Validate a mount point path. A valid mount point path is an 968d314f035Sedp * absolute path that either doesn't exist, or, if it does exists it 969d314f035Sedp * must be an absolute canonical path that doesn't have any symbolic 970d314f035Sedp * links in it. The target of a mount point path can be any filesystem 971d314f035Sedp * object. (Different filesystems can support different mount points, 972d314f035Sedp * for example "lofs" and "mntfs" both support files and directories 973d314f035Sedp * while "ufs" just supports directories.) 9747c478bd9Sstevel@tonic-gate * 975d314f035Sedp * If the path is invalid then a negative value is returned. If the 976d314f035Sedp * path exists and is a valid mount point path then 0 is returned. 977d314f035Sedp * If the path doesn't exist return a positive value. 9787c478bd9Sstevel@tonic-gate */ 979d314f035Sedp int 980d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec, 981d314f035Sedp const char *dir, const char *fstype) 9827c478bd9Sstevel@tonic-gate { 983d314f035Sedp char abspath[MAXPATHLEN], *slashp, *slashp_next; 984d314f035Sedp int rv; 9857c478bd9Sstevel@tonic-gate 9867c478bd9Sstevel@tonic-gate /* 987d314f035Sedp * Sanity check the target mount point path. 988d314f035Sedp * It must be a non-null string that starts with a '/'. 9897c478bd9Sstevel@tonic-gate */ 990d314f035Sedp if (dir[0] != '/') { 991d314f035Sedp /* Something went wrong. */ 992d314f035Sedp zerror(zlogp, B_FALSE, "invalid mount directory, " 993d314f035Sedp "type: \"%s\", special: \"%s\", dir: \"%s\"", 994d314f035Sedp fstype, spec, dir); 995d314f035Sedp return (-1); 9967c478bd9Sstevel@tonic-gate } 9977c478bd9Sstevel@tonic-gate 998d314f035Sedp /* 999d314f035Sedp * Join rootpath and dir. Make sure abspath ends with '/', this 1000d314f035Sedp * is added to all paths (even non-directory paths) to allow us 1001d314f035Sedp * to detect the end of paths below. If the path already ends 1002d314f035Sedp * in a '/', then that's ok too (although we'll fail the 1003d314f035Sedp * cannonical path check in valid_mount_point()). 1004d314f035Sedp */ 1005d314f035Sedp if (snprintf(abspath, sizeof (abspath), 1006d314f035Sedp "%s%s/", rootpath, dir) >= sizeof (abspath)) { 1007d314f035Sedp zerror(zlogp, B_FALSE, "pathname %s%s is too long", 1008d314f035Sedp rootpath, dir); 1009d314f035Sedp return (-1); 1010d314f035Sedp } 1011d314f035Sedp 1012d314f035Sedp /* 1013d314f035Sedp * Starting with rootpath, verify the mount path one component 1014d314f035Sedp * at a time. Continue until we've evaluated all of abspath. 1015d314f035Sedp */ 10167c478bd9Sstevel@tonic-gate slashp = &abspath[strlen(rootpath)]; 10177c478bd9Sstevel@tonic-gate assert(*slashp == '/'); 10187c478bd9Sstevel@tonic-gate do { 1019d314f035Sedp slashp_next = strchr(slashp + 1, '/'); 10207c478bd9Sstevel@tonic-gate *slashp = '\0'; 1021d314f035Sedp if (slashp_next != NULL) { 1022d314f035Sedp /* This is an intermediary mount path component. */ 1023d314f035Sedp rv = valid_mount_point(zlogp, abspath, B_FALSE); 1024d314f035Sedp } else { 1025d314f035Sedp /* This is the last component of the mount path. */ 1026d314f035Sedp rv = valid_mount_point(zlogp, abspath, B_TRUE); 1027d314f035Sedp } 1028d314f035Sedp if (rv < 0) 1029d314f035Sedp return (rv); 10307c478bd9Sstevel@tonic-gate *slashp = '/'; 1031d314f035Sedp } while ((slashp = slashp_next) != NULL); 1032d314f035Sedp return (rv); 10337c478bd9Sstevel@tonic-gate } 10347c478bd9Sstevel@tonic-gate 10357c478bd9Sstevel@tonic-gate static int 10369acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name) 10379acbbeafSnn35248 { 10389acbbeafSnn35248 di_prof_t prof = arg; 10399acbbeafSnn35248 10409acbbeafSnn35248 if (name == NULL) 10419acbbeafSnn35248 return (di_prof_add_dev(prof, match)); 10429acbbeafSnn35248 return (di_prof_add_map(prof, match, name)); 10439acbbeafSnn35248 } 10449acbbeafSnn35248 10459acbbeafSnn35248 static int 10469acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target) 10479acbbeafSnn35248 { 10489acbbeafSnn35248 di_prof_t prof = arg; 10499acbbeafSnn35248 10509acbbeafSnn35248 return (di_prof_add_symlink(prof, source, target)); 10519acbbeafSnn35248 } 10529acbbeafSnn35248 10532b24ab6bSSebastien Roy int 10542b24ab6bSSebastien Roy vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep) 1055f4b3ec61Sdh155122 { 1056f4b3ec61Sdh155122 zone_dochandle_t handle; 1057f4b3ec61Sdh155122 1058f4b3ec61Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 1059f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 1060f4b3ec61Sdh155122 return (-1); 1061f4b3ec61Sdh155122 } 1062f4b3ec61Sdh155122 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 1063f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "invalid configuration"); 1064f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 1065f4b3ec61Sdh155122 return (-1); 1066f4b3ec61Sdh155122 } 1067f4b3ec61Sdh155122 if (zonecfg_get_iptype(handle, iptypep) != Z_OK) { 1068f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "invalid ip-type configuration"); 1069f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 1070f4b3ec61Sdh155122 return (-1); 1071f4b3ec61Sdh155122 } 1072f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 1073f4b3ec61Sdh155122 return (0); 1074f4b3ec61Sdh155122 } 1075f4b3ec61Sdh155122 10769acbbeafSnn35248 /* 10779acbbeafSnn35248 * Apply the standard lists of devices/symlinks/mappings and the user-specified 10789acbbeafSnn35248 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will 10799acbbeafSnn35248 * use these as a profile/filter to determine what exists in /dev. 10809acbbeafSnn35248 */ 10819acbbeafSnn35248 static int 10820679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd) 10839acbbeafSnn35248 { 10849acbbeafSnn35248 char brand[MAXNAMELEN]; 10859acbbeafSnn35248 zone_dochandle_t handle = NULL; 1086123807fbSedp brand_handle_t bh = NULL; 10879acbbeafSnn35248 struct zone_devtab ztab; 10889acbbeafSnn35248 di_prof_t prof = NULL; 10899acbbeafSnn35248 int err; 10909acbbeafSnn35248 int retval = -1; 1091f4b3ec61Sdh155122 zone_iptype_t iptype; 1092f4b3ec61Sdh155122 const char *curr_iptype; 10939acbbeafSnn35248 10949acbbeafSnn35248 if (di_prof_init(devpath, &prof)) { 10959acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to initialize profile"); 10969acbbeafSnn35248 goto cleanup; 10979acbbeafSnn35248 } 10989acbbeafSnn35248 10990679f794S /* 11000679f794S * Get a handle to the brand info for this zone. 1101e5816e35SEdward Pilatowicz * If we are mounting the zone, then we must always use the default 11020679f794S * brand device mounts. 11030679f794S */ 11040679f794S if (ALT_MOUNT(mount_cmd)) { 1105e5816e35SEdward Pilatowicz (void) strlcpy(brand, default_brand, sizeof (brand)); 11060679f794S } else { 110762868012SSteve Lawrence (void) strlcpy(brand, brand_name, sizeof (brand)); 11080679f794S } 11090679f794S 11100679f794S if ((bh = brand_open(brand)) == NULL) { 11119acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 11129acbbeafSnn35248 goto cleanup; 11139acbbeafSnn35248 } 11149acbbeafSnn35248 11152b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) < 0) { 1116f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to determine ip-type"); 1117f4b3ec61Sdh155122 goto cleanup; 1118f4b3ec61Sdh155122 } 1119f4b3ec61Sdh155122 switch (iptype) { 1120f4b3ec61Sdh155122 case ZS_SHARED: 1121f4b3ec61Sdh155122 curr_iptype = "shared"; 1122f4b3ec61Sdh155122 break; 1123f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 1124f4b3ec61Sdh155122 curr_iptype = "exclusive"; 1125f4b3ec61Sdh155122 break; 1126f4b3ec61Sdh155122 } 1127f4b3ec61Sdh155122 1128123807fbSedp if (brand_platform_iter_devices(bh, zone_name, 1129f4b3ec61Sdh155122 mount_one_dev_device_cb, prof, curr_iptype) != 0) { 11309acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add standard device"); 11319acbbeafSnn35248 goto cleanup; 11329acbbeafSnn35248 } 11339acbbeafSnn35248 1134123807fbSedp if (brand_platform_iter_link(bh, 11359acbbeafSnn35248 mount_one_dev_symlink_cb, prof) != 0) { 11369acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add standard symlink"); 11379acbbeafSnn35248 goto cleanup; 11389acbbeafSnn35248 } 11399acbbeafSnn35248 11409acbbeafSnn35248 /* Add user-specified devices and directories */ 11419acbbeafSnn35248 if ((handle = zonecfg_init_handle()) == NULL) { 11429acbbeafSnn35248 zerror(zlogp, B_FALSE, "can't initialize zone handle"); 11439acbbeafSnn35248 goto cleanup; 11449acbbeafSnn35248 } 11459acbbeafSnn35248 if (err = zonecfg_get_handle(zone_name, handle)) { 11469acbbeafSnn35248 zerror(zlogp, B_FALSE, "can't get handle for zone " 11479acbbeafSnn35248 "%s: %s", zone_name, zonecfg_strerror(err)); 11489acbbeafSnn35248 goto cleanup; 11499acbbeafSnn35248 } 11509acbbeafSnn35248 if (err = zonecfg_setdevent(handle)) { 11519acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s: %s", zone_name, 11529acbbeafSnn35248 zonecfg_strerror(err)); 11539acbbeafSnn35248 goto cleanup; 11549acbbeafSnn35248 } 11559acbbeafSnn35248 while (zonecfg_getdevent(handle, &ztab) == Z_OK) { 11569acbbeafSnn35248 if (di_prof_add_dev(prof, ztab.zone_dev_match)) { 11579acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add " 11589acbbeafSnn35248 "user-specified device"); 11599acbbeafSnn35248 goto cleanup; 11609acbbeafSnn35248 } 11619acbbeafSnn35248 } 11629acbbeafSnn35248 (void) zonecfg_enddevent(handle); 11639acbbeafSnn35248 11649acbbeafSnn35248 /* Send profile to kernel */ 11659acbbeafSnn35248 if (di_prof_commit(prof)) { 11669acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to commit profile"); 11679acbbeafSnn35248 goto cleanup; 11689acbbeafSnn35248 } 11699acbbeafSnn35248 11709acbbeafSnn35248 retval = 0; 11719acbbeafSnn35248 11729acbbeafSnn35248 cleanup: 1173123807fbSedp if (bh != NULL) 1174123807fbSedp brand_close(bh); 1175e767a340Sgjelinek if (handle != NULL) 11769acbbeafSnn35248 zonecfg_fini_handle(handle); 11779acbbeafSnn35248 if (prof) 11789acbbeafSnn35248 di_prof_fini(prof); 11799acbbeafSnn35248 return (retval); 11809acbbeafSnn35248 } 11819acbbeafSnn35248 11829acbbeafSnn35248 static int 11830679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath, 11840679f794S zone_mnt_t mount_cmd) 11857c478bd9Sstevel@tonic-gate { 11867c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 11877c478bd9Sstevel@tonic-gate char optstr[MAX_MNTOPT_STR]; 11887c478bd9Sstevel@tonic-gate zone_fsopt_t *optptr; 11899acbbeafSnn35248 int rv; 11907c478bd9Sstevel@tonic-gate 1191d314f035Sedp if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special, 1192d314f035Sedp fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) { 11937c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 11947c478bd9Sstevel@tonic-gate rootpath, fsptr->zone_fs_dir); 11957c478bd9Sstevel@tonic-gate return (-1); 1196d314f035Sedp } else if (rv > 0) { 1197d314f035Sedp /* The mount point path doesn't exist, create it now. */ 1198d314f035Sedp if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir, 1199d314f035Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 1200d314f035Sedp DEFAULT_DIR_GROUP) != 0) { 1201d314f035Sedp zerror(zlogp, B_FALSE, "failed to create mount point"); 1202d314f035Sedp return (-1); 12037c478bd9Sstevel@tonic-gate } 12047c478bd9Sstevel@tonic-gate 1205d314f035Sedp /* 1206d314f035Sedp * Now this might seem weird, but we need to invoke 1207d314f035Sedp * valid_mount_path() again. Why? Because it checks 1208d314f035Sedp * to make sure that the mount point path is canonical, 1209d314f035Sedp * which it can only do if the path exists, so now that 1210d314f035Sedp * we've created the path we have to verify it again. 1211d314f035Sedp */ 1212d314f035Sedp if ((rv = valid_mount_path(zlogp, rootpath, 1213d314f035Sedp fsptr->zone_fs_special, fsptr->zone_fs_dir, 1214d314f035Sedp fsptr->zone_fs_type)) < 0) { 1215d314f035Sedp zerror(zlogp, B_FALSE, 1216d314f035Sedp "%s%s is not a valid mount point", 1217d314f035Sedp rootpath, fsptr->zone_fs_dir); 12187c478bd9Sstevel@tonic-gate return (-1); 1219d314f035Sedp } 1220d314f035Sedp } 12217c478bd9Sstevel@tonic-gate 12227c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", rootpath, 12237c478bd9Sstevel@tonic-gate fsptr->zone_fs_dir); 12247c478bd9Sstevel@tonic-gate 12257c478bd9Sstevel@tonic-gate /* 12267c478bd9Sstevel@tonic-gate * In general the strategy here is to do just as much verification as 12277c478bd9Sstevel@tonic-gate * necessary to avoid crashing or otherwise doing something bad; if the 12287c478bd9Sstevel@tonic-gate * administrator initiated the operation via zoneadm(1m), he'll get 12297c478bd9Sstevel@tonic-gate * auto-verification which will let him know what's wrong. If he 12307c478bd9Sstevel@tonic-gate * modifies the zone configuration of a running zone and doesn't attempt 12317c478bd9Sstevel@tonic-gate * to verify that it's OK we won't crash but won't bother trying to be 12327c478bd9Sstevel@tonic-gate * too helpful either. zoneadm verify is only a couple keystrokes away. 12337c478bd9Sstevel@tonic-gate */ 12347c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) { 12357c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot mount %s on %s: " 12367c478bd9Sstevel@tonic-gate "invalid file-system type %s", fsptr->zone_fs_special, 12377c478bd9Sstevel@tonic-gate fsptr->zone_fs_dir, fsptr->zone_fs_type); 12387c478bd9Sstevel@tonic-gate return (-1); 12397c478bd9Sstevel@tonic-gate } 12407c478bd9Sstevel@tonic-gate 12417c478bd9Sstevel@tonic-gate /* 1242108322fbScarlsonj * If we're looking at an alternate root environment, then construct 1243fa3d7ae1Sedp * read-only loopback mounts as necessary. Note that any special 1244fa3d7ae1Sedp * paths for lofs zone mounts in an alternate root must have 1245fa3d7ae1Sedp * already been pre-pended with any alternate root path by the 1246fa3d7ae1Sedp * time we get here. 1247108322fbScarlsonj */ 1248108322fbScarlsonj if (zonecfg_in_alt_root()) { 1249108322fbScarlsonj struct stat64 st; 1250108322fbScarlsonj 1251108322fbScarlsonj if (stat64(fsptr->zone_fs_special, &st) != -1 && 125268eeb376Scarlsonj S_ISBLK(st.st_mode)) { 1253fa3d7ae1Sedp /* 1254fa3d7ae1Sedp * If we're going to mount a block device we need 1255fa3d7ae1Sedp * to check if that device is already mounted 1256fa3d7ae1Sedp * somewhere else, and if so, do a lofs mount 1257fa3d7ae1Sedp * of the device instead of a direct mount 1258fa3d7ae1Sedp */ 125968eeb376Scarlsonj if (check_lofs_needed(zlogp, fsptr) == -1) 1260108322fbScarlsonj return (-1); 126168eeb376Scarlsonj } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) { 1262fa3d7ae1Sedp /* 1263fa3d7ae1Sedp * For lofs mounts, the special node is inside the 1264fa3d7ae1Sedp * alternate root. We need lofs resolution for 1265fa3d7ae1Sedp * this case in order to get at the underlying 1266fa3d7ae1Sedp * read-write path. 1267fa3d7ae1Sedp */ 1268fa3d7ae1Sedp resolve_lofs(zlogp, fsptr->zone_fs_special, 1269108322fbScarlsonj sizeof (fsptr->zone_fs_special)); 1270108322fbScarlsonj } 1271108322fbScarlsonj } 1272108322fbScarlsonj 1273108322fbScarlsonj /* 12747c478bd9Sstevel@tonic-gate * Run 'fsck -m' if there's a device to fsck. 12757c478bd9Sstevel@tonic-gate */ 12767c478bd9Sstevel@tonic-gate if (fsptr->zone_fs_raw[0] != '\0' && 127793239addSjohnlev dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) { 12787c478bd9Sstevel@tonic-gate return (-1); 127993239addSjohnlev } else if (isregfile(fsptr->zone_fs_special) == 1 && 128093239addSjohnlev dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) { 128193239addSjohnlev return (-1); 128293239addSjohnlev } 12837c478bd9Sstevel@tonic-gate 12847c478bd9Sstevel@tonic-gate /* 12857c478bd9Sstevel@tonic-gate * Build up mount option string. 12867c478bd9Sstevel@tonic-gate */ 12877c478bd9Sstevel@tonic-gate optstr[0] = '\0'; 12887c478bd9Sstevel@tonic-gate if (fsptr->zone_fs_options != NULL) { 12897c478bd9Sstevel@tonic-gate (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt, 12907c478bd9Sstevel@tonic-gate sizeof (optstr)); 12917c478bd9Sstevel@tonic-gate for (optptr = fsptr->zone_fs_options->zone_fsopt_next; 12927c478bd9Sstevel@tonic-gate optptr != NULL; optptr = optptr->zone_fsopt_next) { 12937c478bd9Sstevel@tonic-gate (void) strlcat(optstr, ",", sizeof (optstr)); 12947c478bd9Sstevel@tonic-gate (void) strlcat(optstr, optptr->zone_fsopt_opt, 12957c478bd9Sstevel@tonic-gate sizeof (optstr)); 12967c478bd9Sstevel@tonic-gate } 12977c478bd9Sstevel@tonic-gate } 12989acbbeafSnn35248 12999acbbeafSnn35248 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr, 13009acbbeafSnn35248 fsptr->zone_fs_special, path)) != 0) 13019acbbeafSnn35248 return (rv); 13029acbbeafSnn35248 13039acbbeafSnn35248 /* 13049acbbeafSnn35248 * The mount succeeded. If this was not a mount of /dev then 13059acbbeafSnn35248 * we're done. 13069acbbeafSnn35248 */ 13079acbbeafSnn35248 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0) 13089acbbeafSnn35248 return (0); 13099acbbeafSnn35248 13109acbbeafSnn35248 /* 13119acbbeafSnn35248 * We just mounted an instance of a /dev filesystem, so now we 13129acbbeafSnn35248 * need to configure it. 13139acbbeafSnn35248 */ 13140679f794S return (mount_one_dev(zlogp, path, mount_cmd)); 13157c478bd9Sstevel@tonic-gate } 13167c478bd9Sstevel@tonic-gate 13177c478bd9Sstevel@tonic-gate static void 13187c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem) 13197c478bd9Sstevel@tonic-gate { 13207c478bd9Sstevel@tonic-gate uint_t i; 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate if (fsarray == NULL) 13237c478bd9Sstevel@tonic-gate return; 13247c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) 13257c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fsarray[i].zone_fs_options); 13267c478bd9Sstevel@tonic-gate free(fsarray); 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate 1329108322fbScarlsonj /* 1330f4368d3dSvp157776 * This function initiates the creation of a small Solaris Environment for 1331f4368d3dSvp157776 * scratch zone. The Environment creation process is split up into two 1332f4368d3dSvp157776 * functions(build_mounted_pre_var() and build_mounted_post_var()). It 1333f4368d3dSvp157776 * is done this way because: 1334f4368d3dSvp157776 * We need to have both /etc and /var in the root of the scratchzone. 1335f4368d3dSvp157776 * We loopback mount zone's own /etc and /var into the root of the 1336f4368d3dSvp157776 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we 1337f4368d3dSvp157776 * need to delay the mount of /var till the zone's root gets populated. 1338f4368d3dSvp157776 * So mounting of localdirs[](/etc and /var) have been moved to the 1339f4368d3dSvp157776 * build_mounted_post_var() which gets called only after the zone 1340f4368d3dSvp157776 * specific filesystems are mounted. 13416cfd72c6Sgjelinek * 13426cfd72c6Sgjelinek * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE) 13436cfd72c6Sgjelinek * does not loopback mount the zone's own /etc and /var into the root of the 13446cfd72c6Sgjelinek * scratch zone. 1345108322fbScarlsonj */ 1346108322fbScarlsonj static boolean_t 1347f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath, 134816bca938Svp157776 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen) 1349108322fbScarlsonj { 1350108322fbScarlsonj char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1351108322fbScarlsonj const char **cpp; 1352108322fbScarlsonj static const char *mkdirs[] = { 13533f604e0fSdp "/system", "/system/contract", "/system/object", "/proc", 13543f604e0fSdp "/dev", "/tmp", "/a", NULL 1355108322fbScarlsonj }; 1356108322fbScarlsonj char *altstr; 1357f4368d3dSvp157776 FILE *fp; 1358108322fbScarlsonj uuid_t uuid; 1359108322fbScarlsonj 1360108322fbScarlsonj resolve_lofs(zlogp, rootpath, rootlen); 136116bca938Svp157776 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath); 136216bca938Svp157776 resolve_lofs(zlogp, luroot, lurootlen); 1363108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot); 1364108322fbScarlsonj (void) symlink("./usr/bin", tmp); 1365108322fbScarlsonj 1366108322fbScarlsonj /* 1367108322fbScarlsonj * These are mostly special mount points; not handled here. (See 1368108322fbScarlsonj * zone_mount_early.) 1369108322fbScarlsonj */ 1370108322fbScarlsonj for (cpp = mkdirs; *cpp != NULL; cpp++) { 1371108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1372108322fbScarlsonj if (mkdir(tmp, 0755) != 0) { 1373108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1374108322fbScarlsonj return (B_FALSE); 1375108322fbScarlsonj } 1376108322fbScarlsonj } 1377f4368d3dSvp157776 /* 1378f4368d3dSvp157776 * This is here to support lucopy. If there's an instance of this same 1379f4368d3dSvp157776 * zone on the current running system, then we mount its root up as 1380f4368d3dSvp157776 * read-only inside the scratch zone. 1381f4368d3dSvp157776 */ 1382f4368d3dSvp157776 (void) zonecfg_get_uuid(zone_name, uuid); 1383f4368d3dSvp157776 altstr = strdup(zonecfg_get_root()); 1384f4368d3dSvp157776 if (altstr == NULL) { 1385f4368d3dSvp157776 zerror(zlogp, B_TRUE, "memory allocation failed"); 1386f4368d3dSvp157776 return (B_FALSE); 1387f4368d3dSvp157776 } 1388f4368d3dSvp157776 zonecfg_set_root(""); 1389f4368d3dSvp157776 (void) strlcpy(tmp, zone_name, sizeof (tmp)); 1390f4368d3dSvp157776 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp)); 1391f4368d3dSvp157776 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK && 1392f4368d3dSvp157776 strcmp(fromdir, rootpath) != 0) { 1393f4368d3dSvp157776 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot); 1394f4368d3dSvp157776 if (mkdir(tmp, 0755) != 0) { 1395f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1396f4368d3dSvp157776 return (B_FALSE); 1397f4368d3dSvp157776 } 13986e1ae2a3SGary Pennington if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir, 1399f4368d3dSvp157776 tmp) != 0) { 1400f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1401f4368d3dSvp157776 fromdir); 1402f4368d3dSvp157776 return (B_FALSE); 1403f4368d3dSvp157776 } 1404f4368d3dSvp157776 } 1405f4368d3dSvp157776 zonecfg_set_root(altstr); 1406f4368d3dSvp157776 free(altstr); 1407f4368d3dSvp157776 1408f4368d3dSvp157776 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) { 1409f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot open zone mapfile"); 1410f4368d3dSvp157776 return (B_FALSE); 1411f4368d3dSvp157776 } 1412f4368d3dSvp157776 (void) ftruncate(fileno(fp), 0); 1413f4368d3dSvp157776 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) { 1414f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry"); 1415f4368d3dSvp157776 } 1416f4368d3dSvp157776 zonecfg_close_scratch(fp); 1417f4368d3dSvp157776 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot); 1418f4368d3dSvp157776 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0) 1419f4368d3dSvp157776 return (B_FALSE); 1420f4368d3dSvp157776 (void) strlcpy(rootpath, tmp, rootlen); 1421f4368d3dSvp157776 return (B_TRUE); 1422f4368d3dSvp157776 } 1423f4368d3dSvp157776 1424f4368d3dSvp157776 1425f4368d3dSvp157776 static boolean_t 14266cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath, 14276cfd72c6Sgjelinek const char *luroot) 1428f4368d3dSvp157776 { 1429f4368d3dSvp157776 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1430f4368d3dSvp157776 const char **cpp; 14316cfd72c6Sgjelinek const char **loopdirs; 14326cfd72c6Sgjelinek const char **tmpdirs; 1433f4368d3dSvp157776 static const char *localdirs[] = { 1434f4368d3dSvp157776 "/etc", "/var", NULL 1435f4368d3dSvp157776 }; 14366cfd72c6Sgjelinek static const char *scr_loopdirs[] = { 1437f4368d3dSvp157776 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform", 1438f4368d3dSvp157776 "/usr", NULL 1439f4368d3dSvp157776 }; 14406cfd72c6Sgjelinek static const char *upd_loopdirs[] = { 14416cfd72c6Sgjelinek "/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin", 14426cfd72c6Sgjelinek "/usr", "/var", NULL 14436cfd72c6Sgjelinek }; 14446cfd72c6Sgjelinek static const char *scr_tmpdirs[] = { 1445f4368d3dSvp157776 "/tmp", "/var/run", NULL 1446f4368d3dSvp157776 }; 14476cfd72c6Sgjelinek static const char *upd_tmpdirs[] = { 14486cfd72c6Sgjelinek "/tmp", "/var/run", "/var/tmp", NULL 14496cfd72c6Sgjelinek }; 1450f4368d3dSvp157776 struct stat st; 1451f4368d3dSvp157776 14526cfd72c6Sgjelinek if (mount_cmd == Z_MNT_SCRATCH) { 1453108322fbScarlsonj /* 14546cfd72c6Sgjelinek * These are mounted read-write from the zone undergoing 14556cfd72c6Sgjelinek * upgrade. We must be careful not to 'leak' things from the 14566cfd72c6Sgjelinek * main system into the zone, and this accomplishes that goal. 1457108322fbScarlsonj */ 1458108322fbScarlsonj for (cpp = localdirs; *cpp != NULL; cpp++) { 14596cfd72c6Sgjelinek (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, 1460108322fbScarlsonj *cpp); 14616cfd72c6Sgjelinek (void) snprintf(fromdir, sizeof (fromdir), "%s%s", 14626cfd72c6Sgjelinek rootpath, *cpp); 1463108322fbScarlsonj if (mkdir(tmp, 0755) != 0) { 1464108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1465108322fbScarlsonj return (B_FALSE); 1466108322fbScarlsonj } 14676cfd72c6Sgjelinek if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) 14686cfd72c6Sgjelinek != 0) { 14696cfd72c6Sgjelinek zerror(zlogp, B_TRUE, "cannot mount %s on %s", 14706cfd72c6Sgjelinek tmp, *cpp); 1471108322fbScarlsonj return (B_FALSE); 1472108322fbScarlsonj } 1473108322fbScarlsonj } 14746cfd72c6Sgjelinek } 14756cfd72c6Sgjelinek 14766cfd72c6Sgjelinek if (mount_cmd == Z_MNT_UPDATE) 14776cfd72c6Sgjelinek loopdirs = upd_loopdirs; 14786cfd72c6Sgjelinek else 14796cfd72c6Sgjelinek loopdirs = scr_loopdirs; 1480108322fbScarlsonj 1481108322fbScarlsonj /* 1482108322fbScarlsonj * These are things mounted read-only from the running system because 1483108322fbScarlsonj * they contain binaries that must match system. 1484108322fbScarlsonj */ 1485108322fbScarlsonj for (cpp = loopdirs; *cpp != NULL; cpp++) { 1486108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1487108322fbScarlsonj if (mkdir(tmp, 0755) != 0) { 1488108322fbScarlsonj if (errno != EEXIST) { 1489108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1490108322fbScarlsonj return (B_FALSE); 1491108322fbScarlsonj } 1492108322fbScarlsonj if (lstat(tmp, &st) != 0) { 1493108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot stat %s", tmp); 1494108322fbScarlsonj return (B_FALSE); 1495108322fbScarlsonj } 1496108322fbScarlsonj /* 1497108322fbScarlsonj * Ignore any non-directories encountered. These are 1498108322fbScarlsonj * things that have been converted into symlinks 1499108322fbScarlsonj * (/etc/fs and /etc/lib) and no longer need a lofs 1500108322fbScarlsonj * fixup. 1501108322fbScarlsonj */ 1502108322fbScarlsonj if (!S_ISDIR(st.st_mode)) 1503108322fbScarlsonj continue; 1504108322fbScarlsonj } 15056e1ae2a3SGary Pennington if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp, 1506108322fbScarlsonj tmp) != 0) { 1507108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1508108322fbScarlsonj *cpp); 1509108322fbScarlsonj return (B_FALSE); 1510108322fbScarlsonj } 1511108322fbScarlsonj } 1512108322fbScarlsonj 15136cfd72c6Sgjelinek if (mount_cmd == Z_MNT_UPDATE) 15146cfd72c6Sgjelinek tmpdirs = upd_tmpdirs; 15156cfd72c6Sgjelinek else 15166cfd72c6Sgjelinek tmpdirs = scr_tmpdirs; 15176cfd72c6Sgjelinek 1518108322fbScarlsonj /* 1519108322fbScarlsonj * These are things with tmpfs mounted inside. 1520108322fbScarlsonj */ 1521108322fbScarlsonj for (cpp = tmpdirs; *cpp != NULL; cpp++) { 1522108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 15236cfd72c6Sgjelinek if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 && 15246cfd72c6Sgjelinek errno != EEXIST) { 1525108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1526108322fbScarlsonj return (B_FALSE); 1527108322fbScarlsonj } 1528d30e996aSgjelinek 1529d30e996aSgjelinek /* 1530d30e996aSgjelinek * We could set the mode for /tmp when we do the mkdir but 1531d30e996aSgjelinek * since that can be modified by the umask we will just set 1532d30e996aSgjelinek * the correct mode for /tmp now. 1533d30e996aSgjelinek */ 1534d30e996aSgjelinek if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) { 1535d30e996aSgjelinek zerror(zlogp, B_TRUE, "cannot chmod %s", tmp); 1536d30e996aSgjelinek return (B_FALSE); 1537d30e996aSgjelinek } 1538d30e996aSgjelinek 1539108322fbScarlsonj if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) { 1540108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp); 1541108322fbScarlsonj return (B_FALSE); 1542108322fbScarlsonj } 1543108322fbScarlsonj } 1544108322fbScarlsonj return (B_TRUE); 1545108322fbScarlsonj } 1546108322fbScarlsonj 15479acbbeafSnn35248 typedef struct plat_gmount_cb_data { 15489acbbeafSnn35248 zlog_t *pgcd_zlogp; 15499acbbeafSnn35248 struct zone_fstab **pgcd_fs_tab; 15509acbbeafSnn35248 int *pgcd_num_fs; 15519acbbeafSnn35248 } plat_gmount_cb_data_t; 15529acbbeafSnn35248 15539acbbeafSnn35248 /* 15549acbbeafSnn35248 * plat_gmount_cb() is a callback function invoked by libbrand to iterate 15559acbbeafSnn35248 * through all global brand platform mounts. 15569acbbeafSnn35248 */ 15579acbbeafSnn35248 int 15589acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir, 15599acbbeafSnn35248 const char *fstype, const char *opt) 15609acbbeafSnn35248 { 15619acbbeafSnn35248 plat_gmount_cb_data_t *cp = data; 15629acbbeafSnn35248 zlog_t *zlogp = cp->pgcd_zlogp; 15639acbbeafSnn35248 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab; 15649acbbeafSnn35248 int num_fs = *cp->pgcd_num_fs; 15659acbbeafSnn35248 struct zone_fstab *fsp, *tmp_ptr; 15669acbbeafSnn35248 15679acbbeafSnn35248 num_fs++; 15689acbbeafSnn35248 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) { 15699acbbeafSnn35248 zerror(zlogp, B_TRUE, "memory allocation failed"); 15709acbbeafSnn35248 return (-1); 15719acbbeafSnn35248 } 15729acbbeafSnn35248 15739acbbeafSnn35248 fs_ptr = tmp_ptr; 15749acbbeafSnn35248 fsp = &fs_ptr[num_fs - 1]; 15759acbbeafSnn35248 15769acbbeafSnn35248 /* update the callback struct passed in */ 15779acbbeafSnn35248 *cp->pgcd_fs_tab = fs_ptr; 15789acbbeafSnn35248 *cp->pgcd_num_fs = num_fs; 15799acbbeafSnn35248 15809acbbeafSnn35248 fsp->zone_fs_raw[0] = '\0'; 15819acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_special, spec, 15829acbbeafSnn35248 sizeof (fsp->zone_fs_special)); 15839acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir)); 15849acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type)); 15859acbbeafSnn35248 fsp->zone_fs_options = NULL; 1586fa3d7ae1Sedp if ((opt != NULL) && 1587fa3d7ae1Sedp (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) { 15889acbbeafSnn35248 zerror(zlogp, B_FALSE, "error adding property"); 15899acbbeafSnn35248 return (-1); 15909acbbeafSnn35248 } 15919acbbeafSnn35248 15929acbbeafSnn35248 return (0); 15939acbbeafSnn35248 } 15949acbbeafSnn35248 15959acbbeafSnn35248 static int 15969acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp, 15976cfd72c6Sgjelinek struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd) 15989acbbeafSnn35248 { 15999acbbeafSnn35248 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab; 16009acbbeafSnn35248 int num_fs; 16019acbbeafSnn35248 16029acbbeafSnn35248 num_fs = *num_fsp; 16039acbbeafSnn35248 fs_ptr = *fs_tabp; 16049acbbeafSnn35248 16059acbbeafSnn35248 if (zonecfg_setfsent(handle) != Z_OK) { 16069acbbeafSnn35248 zerror(zlogp, B_FALSE, "invalid configuration"); 16079acbbeafSnn35248 return (-1); 16089acbbeafSnn35248 } 16099acbbeafSnn35248 while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 16109acbbeafSnn35248 /* 16119acbbeafSnn35248 * ZFS filesystems will not be accessible under an alternate 16129acbbeafSnn35248 * root, since the pool will not be known. Ignore them in this 16139acbbeafSnn35248 * case. 16149acbbeafSnn35248 */ 16156cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd) && 16166cfd72c6Sgjelinek strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0) 16179acbbeafSnn35248 continue; 16189acbbeafSnn35248 16199acbbeafSnn35248 num_fs++; 16209acbbeafSnn35248 if ((tmp_ptr = realloc(fs_ptr, 16219acbbeafSnn35248 num_fs * sizeof (*tmp_ptr))) == NULL) { 16229acbbeafSnn35248 zerror(zlogp, B_TRUE, "memory allocation failed"); 16239acbbeafSnn35248 (void) zonecfg_endfsent(handle); 16249acbbeafSnn35248 return (-1); 16259acbbeafSnn35248 } 16269acbbeafSnn35248 /* update the pointers passed in */ 16279acbbeafSnn35248 *fs_tabp = tmp_ptr; 16289acbbeafSnn35248 *num_fsp = num_fs; 16299acbbeafSnn35248 16309acbbeafSnn35248 fs_ptr = tmp_ptr; 16319acbbeafSnn35248 fsp = &fs_ptr[num_fs - 1]; 16329acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_dir, 16339acbbeafSnn35248 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 16349acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw, 16359acbbeafSnn35248 sizeof (fsp->zone_fs_raw)); 16369acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type, 16379acbbeafSnn35248 sizeof (fsp->zone_fs_type)); 16389acbbeafSnn35248 fsp->zone_fs_options = fstab.zone_fs_options; 1639fa3d7ae1Sedp 1640fa3d7ae1Sedp /* 1641fa3d7ae1Sedp * For all lofs mounts, make sure that the 'special' 1642fa3d7ae1Sedp * entry points inside the alternate root. The 1643fa3d7ae1Sedp * source path for a lofs mount in a given zone needs 1644fa3d7ae1Sedp * to be relative to the root of the boot environment 1645fa3d7ae1Sedp * that contains the zone. Note that we don't do this 1646fa3d7ae1Sedp * for non-lofs mounts since they will have a device 1647fa3d7ae1Sedp * as a backing store and device paths must always be 1648fa3d7ae1Sedp * specified relative to the current boot environment. 1649fa3d7ae1Sedp */ 1650fa3d7ae1Sedp fsp->zone_fs_special[0] = '\0'; 1651fa3d7ae1Sedp if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) { 1652fa3d7ae1Sedp (void) strlcat(fsp->zone_fs_special, zonecfg_get_root(), 1653fa3d7ae1Sedp sizeof (fsp->zone_fs_special)); 1654fa3d7ae1Sedp } 1655fa3d7ae1Sedp (void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special, 1656fa3d7ae1Sedp sizeof (fsp->zone_fs_special)); 16579acbbeafSnn35248 } 16589acbbeafSnn35248 (void) zonecfg_endfsent(handle); 16599acbbeafSnn35248 return (0); 16609acbbeafSnn35248 } 16619acbbeafSnn35248 16627c478bd9Sstevel@tonic-gate static int 16636cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd) 16647c478bd9Sstevel@tonic-gate { 16657c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; 16667c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN]; 16679acbbeafSnn35248 char brand[MAXNAMELEN]; 166816bca938Svp157776 char luroot[MAXPATHLEN]; 16699acbbeafSnn35248 int i, num_fs = 0; 16709acbbeafSnn35248 struct zone_fstab *fs_ptr = NULL; 16717c478bd9Sstevel@tonic-gate zone_dochandle_t handle = NULL; 16727c478bd9Sstevel@tonic-gate zone_state_t zstate; 1673123807fbSedp brand_handle_t bh; 16749acbbeafSnn35248 plat_gmount_cb_data_t cb; 16757c478bd9Sstevel@tonic-gate 16767c478bd9Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK || 1677108322fbScarlsonj (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) { 16787c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 1679108322fbScarlsonj "zone must be in '%s' or '%s' state to mount file-systems", 1680108322fbScarlsonj zone_state_str(ZONE_STATE_READY), 1681108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED)); 16827c478bd9Sstevel@tonic-gate goto bad; 16837c478bd9Sstevel@tonic-gate } 16847c478bd9Sstevel@tonic-gate 16857c478bd9Sstevel@tonic-gate if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 16867c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone path"); 16877c478bd9Sstevel@tonic-gate goto bad; 16887c478bd9Sstevel@tonic-gate } 16897c478bd9Sstevel@tonic-gate 16907c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 16917c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 16927c478bd9Sstevel@tonic-gate goto bad; 16937c478bd9Sstevel@tonic-gate } 16947c478bd9Sstevel@tonic-gate 16957c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 1696ffbafc53Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle"); 16977c478bd9Sstevel@tonic-gate goto bad; 16987c478bd9Sstevel@tonic-gate } 16997c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK || 17007c478bd9Sstevel@tonic-gate zonecfg_setfsent(handle) != Z_OK) { 17017c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 17027c478bd9Sstevel@tonic-gate goto bad; 17037c478bd9Sstevel@tonic-gate } 17047c478bd9Sstevel@tonic-gate 17050679f794S /* 1706e5816e35SEdward Pilatowicz * If we are mounting the zone, then we must always use the default 17070679f794S * brand global mounts. 17080679f794S */ 17090679f794S if (ALT_MOUNT(mount_cmd)) { 1710e5816e35SEdward Pilatowicz (void) strlcpy(brand, default_brand, sizeof (brand)); 17110679f794S } else { 171262868012SSteve Lawrence (void) strlcpy(brand, brand_name, sizeof (brand)); 17130679f794S } 17140679f794S 17159acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 17160679f794S if ((bh = brand_open(brand)) == NULL) { 17179acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 1718e767a340Sgjelinek zonecfg_fini_handle(handle); 17199acbbeafSnn35248 return (-1); 17209acbbeafSnn35248 } 17219acbbeafSnn35248 17229acbbeafSnn35248 /* 17239acbbeafSnn35248 * Get the list of global filesystems to mount from the brand 17249acbbeafSnn35248 * configuration. 17259acbbeafSnn35248 */ 17269acbbeafSnn35248 cb.pgcd_zlogp = zlogp; 17279acbbeafSnn35248 cb.pgcd_fs_tab = &fs_ptr; 17289acbbeafSnn35248 cb.pgcd_num_fs = &num_fs; 1729123807fbSedp if (brand_platform_iter_gmounts(bh, zonepath, 17309acbbeafSnn35248 plat_gmount_cb, &cb) != 0) { 17319acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 1732123807fbSedp brand_close(bh); 1733e767a340Sgjelinek zonecfg_fini_handle(handle); 17349acbbeafSnn35248 return (-1); 17359acbbeafSnn35248 } 1736123807fbSedp brand_close(bh); 17379acbbeafSnn35248 17387c478bd9Sstevel@tonic-gate /* 17396e1ae2a3SGary Pennington * Iterate through the rest of the filesystems. Sort them all, 17406e1ae2a3SGary Pennington * then mount them in sorted order. This is to make sure the 17416e1ae2a3SGary Pennington * higher level directories (e.g., /usr) get mounted before 17426e1ae2a3SGary Pennington * any beneath them (e.g., /usr/local). 17437c478bd9Sstevel@tonic-gate */ 17449acbbeafSnn35248 if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs, 17459acbbeafSnn35248 mount_cmd) != 0) 17467c478bd9Sstevel@tonic-gate goto bad; 1747fa9e4066Sahrens 17487c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 17497c478bd9Sstevel@tonic-gate handle = NULL; 17507c478bd9Sstevel@tonic-gate 1751108322fbScarlsonj /* 17529acbbeafSnn35248 * Normally when we mount a zone all the zone filesystems 17539acbbeafSnn35248 * get mounted relative to rootpath, which is usually 17549acbbeafSnn35248 * <zonepath>/root. But when mounting a zone for administration 17559acbbeafSnn35248 * purposes via the zone "mount" state, build_mounted_pre_var() 17569acbbeafSnn35248 * updates rootpath to be <zonepath>/lu/a so we'll mount all 17579acbbeafSnn35248 * the zones filesystems there instead. 17589acbbeafSnn35248 * 17599acbbeafSnn35248 * build_mounted_pre_var() and build_mounted_post_var() will 17609acbbeafSnn35248 * also do some extra work to create directories and lofs mount 17619acbbeafSnn35248 * a bunch of global zone file system paths into <zonepath>/lu. 17629acbbeafSnn35248 * 17639acbbeafSnn35248 * This allows us to be able to enter the zone (now rooted at 17649acbbeafSnn35248 * <zonepath>/lu) and run the upgrade/patch tools that are in the 17659acbbeafSnn35248 * global zone and have them upgrade the to-be-modified zone's 17669acbbeafSnn35248 * files mounted on /a. (Which mirrors the existing standard 17679acbbeafSnn35248 * upgrade environment.) 17689acbbeafSnn35248 * 17699acbbeafSnn35248 * There is of course one catch. When doing the upgrade 17709acbbeafSnn35248 * we need <zoneroot>/lu/dev to be the /dev filesystem 17719acbbeafSnn35248 * for the zone and we don't want to have any /dev filesystem 17729acbbeafSnn35248 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified 17739acbbeafSnn35248 * as a normal zone filesystem by default we'll try to mount 17749acbbeafSnn35248 * it at <zoneroot>/lu/a/dev, so we have to detect this 17759acbbeafSnn35248 * case and instead mount it at <zoneroot>/lu/dev. 17769acbbeafSnn35248 * 17779acbbeafSnn35248 * All this work is done in three phases: 1778f4368d3dSvp157776 * 1) Create and populate lu directory (build_mounted_pre_var()). 1779f4368d3dSvp157776 * 2) Mount the required filesystems as per the zone configuration. 1780f4368d3dSvp157776 * 3) Set up the rest of the scratch zone environment 1781f4368d3dSvp157776 * (build_mounted_post_var()). 1782108322fbScarlsonj */ 17836cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp, 178416bca938Svp157776 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot))) 1785108322fbScarlsonj goto bad; 1786108322fbScarlsonj 17877c478bd9Sstevel@tonic-gate qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare); 17889acbbeafSnn35248 17897c478bd9Sstevel@tonic-gate for (i = 0; i < num_fs; i++) { 17906cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd) && 17919acbbeafSnn35248 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) { 17929acbbeafSnn35248 size_t slen = strlen(rootpath) - 2; 17939acbbeafSnn35248 17949acbbeafSnn35248 /* 17959acbbeafSnn35248 * By default we'll try to mount /dev as /a/dev 17969acbbeafSnn35248 * but /dev is special and always goes at the top 17979acbbeafSnn35248 * so strip the trailing '/a' from the rootpath. 17989acbbeafSnn35248 */ 17999acbbeafSnn35248 assert(strcmp(&rootpath[slen], "/a") == 0); 18009acbbeafSnn35248 rootpath[slen] = '\0'; 18010679f794S if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) 18020679f794S != 0) 18039acbbeafSnn35248 goto bad; 18049acbbeafSnn35248 rootpath[slen] = '/'; 18059acbbeafSnn35248 continue; 18069acbbeafSnn35248 } 18070679f794S if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0) 18087c478bd9Sstevel@tonic-gate goto bad; 18097c478bd9Sstevel@tonic-gate } 18106cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd) && 18116cfd72c6Sgjelinek !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot)) 1812f4368d3dSvp157776 goto bad; 181345916cd2Sjpk 181445916cd2Sjpk /* 181545916cd2Sjpk * For Trusted Extensions cross-mount each lower level /export/home 181645916cd2Sjpk */ 18176cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT && 18186cfd72c6Sgjelinek tsol_mounts(zlogp, zone_name, rootpath) != 0) 181945916cd2Sjpk goto bad; 182045916cd2Sjpk 18217c478bd9Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs); 18227c478bd9Sstevel@tonic-gate 18237c478bd9Sstevel@tonic-gate /* 18247c478bd9Sstevel@tonic-gate * Everything looks fine. 18257c478bd9Sstevel@tonic-gate */ 18267c478bd9Sstevel@tonic-gate return (0); 18277c478bd9Sstevel@tonic-gate 18287c478bd9Sstevel@tonic-gate bad: 18297c478bd9Sstevel@tonic-gate if (handle != NULL) 18307c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 18317c478bd9Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs); 18327c478bd9Sstevel@tonic-gate return (-1); 18337c478bd9Sstevel@tonic-gate } 18347c478bd9Sstevel@tonic-gate 18357c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */ 18367c478bd9Sstevel@tonic-gate static int 18377c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr) 18387c478bd9Sstevel@tonic-gate { 18397c478bd9Sstevel@tonic-gate int prefixlen; 18407c478bd9Sstevel@tonic-gate 18417c478bd9Sstevel@tonic-gate prefixlen = atoi(prefixstr); 18427c478bd9Sstevel@tonic-gate if (prefixlen < 0 || prefixlen > maxprefixlen) 18437c478bd9Sstevel@tonic-gate return (1); 18447c478bd9Sstevel@tonic-gate while (prefixlen > 0) { 18457c478bd9Sstevel@tonic-gate if (prefixlen >= 8) { 18467c478bd9Sstevel@tonic-gate *maskstr++ = 0xFF; 18477c478bd9Sstevel@tonic-gate prefixlen -= 8; 18487c478bd9Sstevel@tonic-gate continue; 18497c478bd9Sstevel@tonic-gate } 18507c478bd9Sstevel@tonic-gate *maskstr |= 1 << (8 - prefixlen); 18517c478bd9Sstevel@tonic-gate prefixlen--; 18527c478bd9Sstevel@tonic-gate } 18537c478bd9Sstevel@tonic-gate return (0); 18547c478bd9Sstevel@tonic-gate } 18557c478bd9Sstevel@tonic-gate 18567c478bd9Sstevel@tonic-gate /* 18577c478bd9Sstevel@tonic-gate * Tear down all interfaces belonging to the given zone. This should 18587c478bd9Sstevel@tonic-gate * be called with the zone in a state other than "running", so that 18597c478bd9Sstevel@tonic-gate * interfaces can't be assigned to the zone after this returns. 18607c478bd9Sstevel@tonic-gate * 18617c478bd9Sstevel@tonic-gate * If anything goes wrong, log an error message and return an error. 18627c478bd9Sstevel@tonic-gate */ 18637c478bd9Sstevel@tonic-gate static int 1864f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id) 18657c478bd9Sstevel@tonic-gate { 18667c478bd9Sstevel@tonic-gate struct lifnum lifn; 18677c478bd9Sstevel@tonic-gate struct lifconf lifc; 18687c478bd9Sstevel@tonic-gate struct lifreq *lifrp, lifrl; 18697c478bd9Sstevel@tonic-gate int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES; 18707c478bd9Sstevel@tonic-gate int num_ifs, s, i, ret_code = 0; 18717c478bd9Sstevel@tonic-gate uint_t bufsize; 18727c478bd9Sstevel@tonic-gate char *buf = NULL; 18737c478bd9Sstevel@tonic-gate 18747c478bd9Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 18757c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket"); 18767c478bd9Sstevel@tonic-gate ret_code = -1; 18777c478bd9Sstevel@tonic-gate goto bad; 18787c478bd9Sstevel@tonic-gate } 18797c478bd9Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC; 18807c478bd9Sstevel@tonic-gate lifn.lifn_flags = (int)lifc_flags; 18817c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) { 18827c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 1883f4b3ec61Sdh155122 "could not determine number of network interfaces"); 18847c478bd9Sstevel@tonic-gate ret_code = -1; 18857c478bd9Sstevel@tonic-gate goto bad; 18867c478bd9Sstevel@tonic-gate } 18877c478bd9Sstevel@tonic-gate num_ifs = lifn.lifn_count; 18887c478bd9Sstevel@tonic-gate bufsize = num_ifs * sizeof (struct lifreq); 18897c478bd9Sstevel@tonic-gate if ((buf = malloc(bufsize)) == NULL) { 18907c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 18917c478bd9Sstevel@tonic-gate ret_code = -1; 18927c478bd9Sstevel@tonic-gate goto bad; 18937c478bd9Sstevel@tonic-gate } 18947c478bd9Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC; 18957c478bd9Sstevel@tonic-gate lifc.lifc_flags = (int)lifc_flags; 18967c478bd9Sstevel@tonic-gate lifc.lifc_len = bufsize; 18977c478bd9Sstevel@tonic-gate lifc.lifc_buf = buf; 18987c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 1899f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "could not get configured network " 1900f4b3ec61Sdh155122 "interfaces"); 19017c478bd9Sstevel@tonic-gate ret_code = -1; 19027c478bd9Sstevel@tonic-gate goto bad; 19037c478bd9Sstevel@tonic-gate } 19047c478bd9Sstevel@tonic-gate lifrp = lifc.lifc_req; 19057c478bd9Sstevel@tonic-gate for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) { 19067c478bd9Sstevel@tonic-gate (void) close(s); 19077c478bd9Sstevel@tonic-gate if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) < 19087c478bd9Sstevel@tonic-gate 0) { 19097c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get socket", 19107c478bd9Sstevel@tonic-gate lifrl.lifr_name); 19117c478bd9Sstevel@tonic-gate ret_code = -1; 19127c478bd9Sstevel@tonic-gate continue; 19137c478bd9Sstevel@tonic-gate } 19147c478bd9Sstevel@tonic-gate (void) memset(&lifrl, 0, sizeof (lifrl)); 19157c478bd9Sstevel@tonic-gate (void) strncpy(lifrl.lifr_name, lifrp->lifr_name, 19167c478bd9Sstevel@tonic-gate sizeof (lifrl.lifr_name)); 19177c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) { 1918c1c0ebd5Ssl108498 if (errno == ENXIO) 1919c1c0ebd5Ssl108498 /* 1920c1c0ebd5Ssl108498 * Interface may have been removed by admin or 1921c1c0ebd5Ssl108498 * another zone halting. 1922c1c0ebd5Ssl108498 */ 1923c1c0ebd5Ssl108498 continue; 19247c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 1925c1c0ebd5Ssl108498 "%s: could not determine the zone to which this " 1926f4b3ec61Sdh155122 "network interface is bound", lifrl.lifr_name); 19277c478bd9Sstevel@tonic-gate ret_code = -1; 19287c478bd9Sstevel@tonic-gate continue; 19297c478bd9Sstevel@tonic-gate } 19307c478bd9Sstevel@tonic-gate if (lifrl.lifr_zoneid == zone_id) { 19317c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) { 19327c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 1933f4b3ec61Sdh155122 "%s: could not remove network interface", 19347c478bd9Sstevel@tonic-gate lifrl.lifr_name); 19357c478bd9Sstevel@tonic-gate ret_code = -1; 19367c478bd9Sstevel@tonic-gate continue; 19377c478bd9Sstevel@tonic-gate } 19387c478bd9Sstevel@tonic-gate } 19397c478bd9Sstevel@tonic-gate } 19407c478bd9Sstevel@tonic-gate bad: 19417c478bd9Sstevel@tonic-gate if (s > 0) 19427c478bd9Sstevel@tonic-gate (void) close(s); 19437c478bd9Sstevel@tonic-gate if (buf) 19447c478bd9Sstevel@tonic-gate free(buf); 19457c478bd9Sstevel@tonic-gate return (ret_code); 19467c478bd9Sstevel@tonic-gate } 19477c478bd9Sstevel@tonic-gate 19487c478bd9Sstevel@tonic-gate static union sockunion { 19497c478bd9Sstevel@tonic-gate struct sockaddr sa; 19507c478bd9Sstevel@tonic-gate struct sockaddr_in sin; 19517c478bd9Sstevel@tonic-gate struct sockaddr_dl sdl; 19527c478bd9Sstevel@tonic-gate struct sockaddr_in6 sin6; 19537c478bd9Sstevel@tonic-gate } so_dst, so_ifp; 19547c478bd9Sstevel@tonic-gate 19557c478bd9Sstevel@tonic-gate static struct { 19567c478bd9Sstevel@tonic-gate struct rt_msghdr hdr; 19577c478bd9Sstevel@tonic-gate char space[512]; 19587c478bd9Sstevel@tonic-gate } rtmsg; 19597c478bd9Sstevel@tonic-gate 19607c478bd9Sstevel@tonic-gate static int 19617c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa) 19627c478bd9Sstevel@tonic-gate { 19637c478bd9Sstevel@tonic-gate switch (sa->sa_family) { 19647c478bd9Sstevel@tonic-gate case AF_INET: 19657c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_in)); 19667c478bd9Sstevel@tonic-gate case AF_LINK: 19677c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_dl)); 19687c478bd9Sstevel@tonic-gate case AF_INET6: 19697c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_in6)); 19707c478bd9Sstevel@tonic-gate default: 19717c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr)); 19727c478bd9Sstevel@tonic-gate } 19737c478bd9Sstevel@tonic-gate } 19747c478bd9Sstevel@tonic-gate 19757c478bd9Sstevel@tonic-gate #define ROUNDUP_LONG(a) \ 19767c478bd9Sstevel@tonic-gate ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 19777c478bd9Sstevel@tonic-gate 19787c478bd9Sstevel@tonic-gate /* 19797c478bd9Sstevel@tonic-gate * Look up which zone is using a given IP address. The address in question 19807c478bd9Sstevel@tonic-gate * is expected to have been stuffed into the structure to which lifr points 19817c478bd9Sstevel@tonic-gate * via a previous SIOCGLIFADDR ioctl(). 19827c478bd9Sstevel@tonic-gate * 19837c478bd9Sstevel@tonic-gate * This is done using black router socket magic. 19847c478bd9Sstevel@tonic-gate * 19857c478bd9Sstevel@tonic-gate * Return the name of the zone on success or NULL on failure. 19867c478bd9Sstevel@tonic-gate * 19877c478bd9Sstevel@tonic-gate * This is a lot of code for a simple task; a new ioctl request to take care 19887c478bd9Sstevel@tonic-gate * of this might be a useful RFE. 19897c478bd9Sstevel@tonic-gate */ 19907c478bd9Sstevel@tonic-gate 19917c478bd9Sstevel@tonic-gate static char * 19927c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr) 19937c478bd9Sstevel@tonic-gate { 19947c478bd9Sstevel@tonic-gate static char answer[ZONENAME_MAX]; 19957c478bd9Sstevel@tonic-gate pid_t pid; 19967c478bd9Sstevel@tonic-gate int s, rlen, l, i; 19977c478bd9Sstevel@tonic-gate char *cp = rtmsg.space; 19987c478bd9Sstevel@tonic-gate struct sockaddr_dl *ifp = NULL; 19997c478bd9Sstevel@tonic-gate struct sockaddr *sa; 20007c478bd9Sstevel@tonic-gate char save_if_name[LIFNAMSIZ]; 20017c478bd9Sstevel@tonic-gate 20027c478bd9Sstevel@tonic-gate answer[0] = '\0'; 20037c478bd9Sstevel@tonic-gate 20047c478bd9Sstevel@tonic-gate pid = getpid(); 20057c478bd9Sstevel@tonic-gate if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 20067c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get routing socket"); 20077c478bd9Sstevel@tonic-gate return (NULL); 20087c478bd9Sstevel@tonic-gate } 20097c478bd9Sstevel@tonic-gate 20107c478bd9Sstevel@tonic-gate if (lifr->lifr_addr.ss_family == AF_INET) { 20117c478bd9Sstevel@tonic-gate struct sockaddr_in *sin4; 20127c478bd9Sstevel@tonic-gate 20137c478bd9Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET; 20147c478bd9Sstevel@tonic-gate sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 20157c478bd9Sstevel@tonic-gate so_dst.sin.sin_addr = sin4->sin_addr; 20167c478bd9Sstevel@tonic-gate } else { 20177c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 20187c478bd9Sstevel@tonic-gate 20197c478bd9Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET6; 20207c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 20217c478bd9Sstevel@tonic-gate so_dst.sin6.sin6_addr = sin6->sin6_addr; 20227c478bd9Sstevel@tonic-gate } 20237c478bd9Sstevel@tonic-gate 20247c478bd9Sstevel@tonic-gate so_ifp.sa.sa_family = AF_LINK; 20257c478bd9Sstevel@tonic-gate 20267c478bd9Sstevel@tonic-gate (void) memset(&rtmsg, 0, sizeof (rtmsg)); 20277c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_type = RTM_GET; 20287c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST; 20297c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_version = RTM_VERSION; 20307c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_seq = ++rts_seqno; 20317c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST; 20327c478bd9Sstevel@tonic-gate 20337c478bd9Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_dst.sa)); 20347c478bd9Sstevel@tonic-gate (void) memmove(cp, &(so_dst), l); 20357c478bd9Sstevel@tonic-gate cp += l; 20367c478bd9Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_ifp.sa)); 20377c478bd9Sstevel@tonic-gate (void) memmove(cp, &(so_ifp), l); 20387c478bd9Sstevel@tonic-gate cp += l; 20397c478bd9Sstevel@tonic-gate 20407c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg; 20417c478bd9Sstevel@tonic-gate 20427c478bd9Sstevel@tonic-gate if ((rlen = write(s, &rtmsg, l)) < 0) { 20437c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "writing to routing socket"); 20447c478bd9Sstevel@tonic-gate return (NULL); 20457c478bd9Sstevel@tonic-gate } else if (rlen < (int)rtmsg.hdr.rtm_msglen) { 20467c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 20477c478bd9Sstevel@tonic-gate "write to routing socket got only %d for len\n", rlen); 20487c478bd9Sstevel@tonic-gate return (NULL); 20497c478bd9Sstevel@tonic-gate } 20507c478bd9Sstevel@tonic-gate do { 20517c478bd9Sstevel@tonic-gate l = read(s, &rtmsg, sizeof (rtmsg)); 20527c478bd9Sstevel@tonic-gate } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno || 20537c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_pid != pid)); 20547c478bd9Sstevel@tonic-gate if (l < 0) { 20557c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "reading from routing socket"); 20567c478bd9Sstevel@tonic-gate return (NULL); 20577c478bd9Sstevel@tonic-gate } 20587c478bd9Sstevel@tonic-gate 20597c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_version != RTM_VERSION) { 20607c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 20617c478bd9Sstevel@tonic-gate "routing message version %d not understood", 20627c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_version); 20637c478bd9Sstevel@tonic-gate return (NULL); 20647c478bd9Sstevel@tonic-gate } 20657c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_msglen != (ushort_t)l) { 20667c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "message length mismatch, " 20677c478bd9Sstevel@tonic-gate "expected %d bytes, returned %d bytes", 20687c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_msglen, l); 20697c478bd9Sstevel@tonic-gate return (NULL); 20707c478bd9Sstevel@tonic-gate } 20717c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_errno != 0) { 20727c478bd9Sstevel@tonic-gate errno = rtmsg.hdr.rtm_errno; 20737c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "RTM_GET routing socket message"); 20747c478bd9Sstevel@tonic-gate return (NULL); 20757c478bd9Sstevel@tonic-gate } 20767c478bd9Sstevel@tonic-gate if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) { 2077f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "network interface not found"); 20787c478bd9Sstevel@tonic-gate return (NULL); 20797c478bd9Sstevel@tonic-gate } 20807c478bd9Sstevel@tonic-gate cp = ((char *)(&rtmsg.hdr + 1)); 20817c478bd9Sstevel@tonic-gate for (i = 1; i != 0; i <<= 1) { 20827c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */ 20837c478bd9Sstevel@tonic-gate sa = (struct sockaddr *)cp; 20847c478bd9Sstevel@tonic-gate if (i != RTA_IFP) { 20857c478bd9Sstevel@tonic-gate if ((i & rtmsg.hdr.rtm_addrs) != 0) 20867c478bd9Sstevel@tonic-gate cp += ROUNDUP_LONG(salen(sa)); 20877c478bd9Sstevel@tonic-gate continue; 20887c478bd9Sstevel@tonic-gate } 20897c478bd9Sstevel@tonic-gate if (sa->sa_family == AF_LINK && 20907c478bd9Sstevel@tonic-gate ((struct sockaddr_dl *)sa)->sdl_nlen != 0) 20917c478bd9Sstevel@tonic-gate ifp = (struct sockaddr_dl *)sa; 20927c478bd9Sstevel@tonic-gate break; 20937c478bd9Sstevel@tonic-gate } 20947c478bd9Sstevel@tonic-gate if (ifp == NULL) { 2095f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "network interface could not be " 2096f4b3ec61Sdh155122 "determined"); 20977c478bd9Sstevel@tonic-gate return (NULL); 20987c478bd9Sstevel@tonic-gate } 20997c478bd9Sstevel@tonic-gate 21007c478bd9Sstevel@tonic-gate /* 21017c478bd9Sstevel@tonic-gate * We need to set the I/F name to what we got above, then do the 21027c478bd9Sstevel@tonic-gate * appropriate ioctl to get its zone name. But lifr->lifr_name is 21037c478bd9Sstevel@tonic-gate * used by the calling function to do a REMOVEIF, so if we leave the 21047c478bd9Sstevel@tonic-gate * "good" zone's I/F name in place, *that* I/F will be removed instead 21057c478bd9Sstevel@tonic-gate * of the bad one. So we save the old (bad) I/F name before over- 21067c478bd9Sstevel@tonic-gate * writing it and doing the ioctl, then restore it after the ioctl. 21077c478bd9Sstevel@tonic-gate */ 21087c478bd9Sstevel@tonic-gate (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name)); 21097c478bd9Sstevel@tonic-gate (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen); 21107c478bd9Sstevel@tonic-gate lifr->lifr_name[ifp->sdl_nlen] = '\0'; 21117c478bd9Sstevel@tonic-gate i = ioctl(s, SIOCGLIFZONE, lifr); 21127c478bd9Sstevel@tonic-gate (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name)); 21137c478bd9Sstevel@tonic-gate if (i < 0) { 21147c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 2115f4b3ec61Sdh155122 "%s: could not determine the zone network interface " 2116f4b3ec61Sdh155122 "belongs to", lifr->lifr_name); 21177c478bd9Sstevel@tonic-gate return (NULL); 21187c478bd9Sstevel@tonic-gate } 21197c478bd9Sstevel@tonic-gate if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0) 21207c478bd9Sstevel@tonic-gate (void) snprintf(answer, sizeof (answer), "%d", 21217c478bd9Sstevel@tonic-gate lifr->lifr_zoneid); 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate if (strlen(answer) > 0) 21247c478bd9Sstevel@tonic-gate return (answer); 21257c478bd9Sstevel@tonic-gate return (NULL); 21267c478bd9Sstevel@tonic-gate } 21277c478bd9Sstevel@tonic-gate 21287c478bd9Sstevel@tonic-gate /* 21297c478bd9Sstevel@tonic-gate * Configures a single interface: a new virtual interface is added, based on 21307c478bd9Sstevel@tonic-gate * the physical interface nwiftabptr->zone_nwif_physical, with the address 21317c478bd9Sstevel@tonic-gate * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that 21327c478bd9Sstevel@tonic-gate * the "address" can be an IPv6 address (with a /prefixlength required), an 21337c478bd9Sstevel@tonic-gate * IPv4 address (with a /prefixlength optional), or a name; for the latter, 21347c478bd9Sstevel@tonic-gate * an IPv4 name-to-address resolution will be attempted. 21357c478bd9Sstevel@tonic-gate * 21367c478bd9Sstevel@tonic-gate * If anything goes wrong, we log an detailed error message, attempt to tear 21377c478bd9Sstevel@tonic-gate * down whatever we set up and return an error. 21387c478bd9Sstevel@tonic-gate */ 21397c478bd9Sstevel@tonic-gate static int 21407c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id, 2141f14f3ae7Sjv227347 struct zone_nwiftab *nwiftabptr) 21427c478bd9Sstevel@tonic-gate { 21437c478bd9Sstevel@tonic-gate struct lifreq lifr; 21447c478bd9Sstevel@tonic-gate struct sockaddr_in netmask4; 21457c478bd9Sstevel@tonic-gate struct sockaddr_in6 netmask6; 21462e481403SVamsi Nagineni struct sockaddr_storage laddr; 21477c478bd9Sstevel@tonic-gate struct in_addr in4; 21487c478bd9Sstevel@tonic-gate sa_family_t af; 21497c478bd9Sstevel@tonic-gate char *slashp = strchr(nwiftabptr->zone_nwif_address, '/'); 21507c478bd9Sstevel@tonic-gate int s; 21517c478bd9Sstevel@tonic-gate boolean_t got_netmask = B_FALSE; 2152f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India boolean_t is_loopback = B_FALSE; 21537c478bd9Sstevel@tonic-gate char addrstr4[INET_ADDRSTRLEN]; 21547c478bd9Sstevel@tonic-gate int res; 21557c478bd9Sstevel@tonic-gate 21567c478bd9Sstevel@tonic-gate res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr); 21577c478bd9Sstevel@tonic-gate if (res != Z_OK) { 21587c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res), 21597c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 21607c478bd9Sstevel@tonic-gate return (-1); 21617c478bd9Sstevel@tonic-gate } 21627c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family; 21637c478bd9Sstevel@tonic-gate if (af == AF_INET) 21647c478bd9Sstevel@tonic-gate in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr; 21657c478bd9Sstevel@tonic-gate if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 21667c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket"); 21677c478bd9Sstevel@tonic-gate return (-1); 21687c478bd9Sstevel@tonic-gate } 21697c478bd9Sstevel@tonic-gate 21702e481403SVamsi Nagineni /* 21712e481403SVamsi Nagineni * This is a similar kind of "hack" like in addif() to get around 21722e481403SVamsi Nagineni * the problem of SIOCLIFADDIF. The problem is that this ioctl 21732e481403SVamsi Nagineni * does not include the netmask when adding a logical interface. 21742e481403SVamsi Nagineni * To get around this problem, we first add the logical interface 21752e481403SVamsi Nagineni * with a 0 address. After that, we set the netmask if provided. 21762e481403SVamsi Nagineni * Finally we set the interface address. 21772e481403SVamsi Nagineni */ 21782e481403SVamsi Nagineni laddr = lifr.lifr_addr; 21797c478bd9Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical, 21807c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_name)); 21812e481403SVamsi Nagineni (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 21822e481403SVamsi Nagineni 21837c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) { 218422321485Svp157776 /* 218522321485Svp157776 * Here, we know that the interface can't be brought up. 218622321485Svp157776 * A similar warning message was already printed out to 218722321485Svp157776 * the console by zoneadm(1M) so instead we log the 218822321485Svp157776 * message to syslog and continue. 218922321485Svp157776 */ 2190f4b3ec61Sdh155122 zerror(&logsys, B_TRUE, "WARNING: skipping network interface " 219122321485Svp157776 "'%s' which may not be present/plumbed in the " 219222321485Svp157776 "global zone.", lifr.lifr_name); 21937c478bd9Sstevel@tonic-gate (void) close(s); 219422321485Svp157776 return (Z_OK); 21957c478bd9Sstevel@tonic-gate } 21967c478bd9Sstevel@tonic-gate 21977c478bd9Sstevel@tonic-gate /* Preserve literal IPv4 address for later potential printing. */ 21987c478bd9Sstevel@tonic-gate if (af == AF_INET) 21997c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN); 22007c478bd9Sstevel@tonic-gate 22017c478bd9Sstevel@tonic-gate lifr.lifr_zoneid = zone_id; 22027c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) { 2203f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "%s: could not place network interface " 2204f4b3ec61Sdh155122 "into zone", lifr.lifr_name); 22057c478bd9Sstevel@tonic-gate goto bad; 22067c478bd9Sstevel@tonic-gate } 22077c478bd9Sstevel@tonic-gate 2208f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India /* 2209f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India * Loopback interface will use the default netmask assigned, if no 2210f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India * netmask is found. 2211f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India */ 22127c478bd9Sstevel@tonic-gate if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) { 2213f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India is_loopback = B_TRUE; 2214f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India } 22157c478bd9Sstevel@tonic-gate if (af == AF_INET) { 22167c478bd9Sstevel@tonic-gate /* 22177c478bd9Sstevel@tonic-gate * The IPv4 netmask can be determined either 22187c478bd9Sstevel@tonic-gate * directly if a prefix length was supplied with 22197c478bd9Sstevel@tonic-gate * the address or via the netmasks database. Not 22207c478bd9Sstevel@tonic-gate * being able to determine it is a common failure, 22217c478bd9Sstevel@tonic-gate * but it often is not fatal to operation of the 22227c478bd9Sstevel@tonic-gate * interface. In that case, a warning will be 22237c478bd9Sstevel@tonic-gate * printed after the rest of the interface's 22247c478bd9Sstevel@tonic-gate * parameters have been configured. 22257c478bd9Sstevel@tonic-gate */ 22267c478bd9Sstevel@tonic-gate (void) memset(&netmask4, 0, sizeof (netmask4)); 22277c478bd9Sstevel@tonic-gate if (slashp != NULL) { 22287c478bd9Sstevel@tonic-gate if (addr2netmask(slashp + 1, V4_ADDR_LEN, 22297c478bd9Sstevel@tonic-gate (uchar_t *)&netmask4.sin_addr) != 0) { 22307c478bd9Sstevel@tonic-gate *slashp = '/'; 22317c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 22327c478bd9Sstevel@tonic-gate "%s: invalid prefix length in %s", 22337c478bd9Sstevel@tonic-gate lifr.lifr_name, 22347c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 22357c478bd9Sstevel@tonic-gate goto bad; 22367c478bd9Sstevel@tonic-gate } 22377c478bd9Sstevel@tonic-gate got_netmask = B_TRUE; 22387c478bd9Sstevel@tonic-gate } else if (getnetmaskbyaddr(in4, 22397c478bd9Sstevel@tonic-gate &netmask4.sin_addr) == 0) { 22407c478bd9Sstevel@tonic-gate got_netmask = B_TRUE; 22417c478bd9Sstevel@tonic-gate } 22427c478bd9Sstevel@tonic-gate if (got_netmask) { 22437c478bd9Sstevel@tonic-gate netmask4.sin_family = af; 22447c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask4, 22457c478bd9Sstevel@tonic-gate sizeof (netmask4)); 22467c478bd9Sstevel@tonic-gate } 22477c478bd9Sstevel@tonic-gate } else { 22487c478bd9Sstevel@tonic-gate (void) memset(&netmask6, 0, sizeof (netmask6)); 22497c478bd9Sstevel@tonic-gate if (addr2netmask(slashp + 1, V6_ADDR_LEN, 22507c478bd9Sstevel@tonic-gate (uchar_t *)&netmask6.sin6_addr) != 0) { 22517c478bd9Sstevel@tonic-gate *slashp = '/'; 22527c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 22537c478bd9Sstevel@tonic-gate "%s: invalid prefix length in %s", 22547c478bd9Sstevel@tonic-gate lifr.lifr_name, 22557c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address); 22567c478bd9Sstevel@tonic-gate goto bad; 22577c478bd9Sstevel@tonic-gate } 22587c478bd9Sstevel@tonic-gate got_netmask = B_TRUE; 22597c478bd9Sstevel@tonic-gate netmask6.sin6_family = af; 22607c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask6, 22617c478bd9Sstevel@tonic-gate sizeof (netmask6)); 22627c478bd9Sstevel@tonic-gate } 22637c478bd9Sstevel@tonic-gate if (got_netmask && 22647c478bd9Sstevel@tonic-gate ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) { 22657c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not set netmask", 22667c478bd9Sstevel@tonic-gate lifr.lifr_name); 22677c478bd9Sstevel@tonic-gate goto bad; 22687c478bd9Sstevel@tonic-gate } 22697c478bd9Sstevel@tonic-gate 22702e481403SVamsi Nagineni /* Set the interface address */ 22712e481403SVamsi Nagineni lifr.lifr_addr = laddr; 22727c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 22737c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 22742e481403SVamsi Nagineni "%s: could not set IP address to %s", 22752e481403SVamsi Nagineni lifr.lifr_name, nwiftabptr->zone_nwif_address); 22767c478bd9Sstevel@tonic-gate goto bad; 22777c478bd9Sstevel@tonic-gate } 22787c478bd9Sstevel@tonic-gate 22797c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 22807c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get flags", 22817c478bd9Sstevel@tonic-gate lifr.lifr_name); 22827c478bd9Sstevel@tonic-gate goto bad; 22837c478bd9Sstevel@tonic-gate } 22847c478bd9Sstevel@tonic-gate lifr.lifr_flags |= IFF_UP; 22857c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) { 22867c478bd9Sstevel@tonic-gate int save_errno = errno; 22877c478bd9Sstevel@tonic-gate char *zone_using; 22887c478bd9Sstevel@tonic-gate 22897c478bd9Sstevel@tonic-gate /* 22907c478bd9Sstevel@tonic-gate * If we failed with something other than EADDRNOTAVAIL, 22917c478bd9Sstevel@tonic-gate * then skip to the end. Otherwise, look up our address, 22927c478bd9Sstevel@tonic-gate * then call a function to determine which zone is already 22937c478bd9Sstevel@tonic-gate * using that address. 22947c478bd9Sstevel@tonic-gate */ 22957c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) { 22967c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 2297f4b3ec61Sdh155122 "%s: could not bring network interface up", 2298f4b3ec61Sdh155122 lifr.lifr_name); 22997c478bd9Sstevel@tonic-gate goto bad; 23007c478bd9Sstevel@tonic-gate } 23017c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 23027c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get address", 23037c478bd9Sstevel@tonic-gate lifr.lifr_name); 23047c478bd9Sstevel@tonic-gate goto bad; 23057c478bd9Sstevel@tonic-gate } 23067c478bd9Sstevel@tonic-gate zone_using = who_is_using(zlogp, &lifr); 23077c478bd9Sstevel@tonic-gate errno = save_errno; 23087c478bd9Sstevel@tonic-gate if (zone_using == NULL) 23097c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, 2310f4b3ec61Sdh155122 "%s: could not bring network interface up", 2311f4b3ec61Sdh155122 lifr.lifr_name); 23127c478bd9Sstevel@tonic-gate else 2313f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "%s: could not bring network " 2314f4b3ec61Sdh155122 "interface up: address in use by zone '%s'", 2315f4b3ec61Sdh155122 lifr.lifr_name, zone_using); 23167c478bd9Sstevel@tonic-gate goto bad; 23177c478bd9Sstevel@tonic-gate } 23187c478bd9Sstevel@tonic-gate 2319f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India if (!got_netmask && !is_loopback) { 23207c478bd9Sstevel@tonic-gate /* 23217c478bd9Sstevel@tonic-gate * A common, but often non-fatal problem, is that the system 23227c478bd9Sstevel@tonic-gate * cannot find the netmask for an interface address. This is 23237c478bd9Sstevel@tonic-gate * often caused by it being only in /etc/inet/netmasks, but 23247c478bd9Sstevel@tonic-gate * /etc/nsswitch.conf says to use NIS or NIS+ and it's not 23257c478bd9Sstevel@tonic-gate * in that. This doesn't show up at boot because the netmask 23267c478bd9Sstevel@tonic-gate * is obtained from /etc/inet/netmasks when no network 23277c478bd9Sstevel@tonic-gate * interfaces are up, but isn't consulted when NIS/NIS+ is 23287c478bd9Sstevel@tonic-gate * available. We warn the user here that something like this 23297c478bd9Sstevel@tonic-gate * has happened and we're just running with a default and 23307c478bd9Sstevel@tonic-gate * possible incorrect netmask. 23317c478bd9Sstevel@tonic-gate */ 23327c478bd9Sstevel@tonic-gate char buffer[INET6_ADDRSTRLEN]; 23337c478bd9Sstevel@tonic-gate void *addr; 2334e11c3f44Smeem const char *nomatch = "no matching subnet found in netmasks(4)"; 23357c478bd9Sstevel@tonic-gate 23367c478bd9Sstevel@tonic-gate if (af == AF_INET) 23377c478bd9Sstevel@tonic-gate addr = &((struct sockaddr_in *) 23387c478bd9Sstevel@tonic-gate (&lifr.lifr_addr))->sin_addr; 23397c478bd9Sstevel@tonic-gate else 23407c478bd9Sstevel@tonic-gate addr = &((struct sockaddr_in6 *) 23417c478bd9Sstevel@tonic-gate (&lifr.lifr_addr))->sin6_addr; 23427c478bd9Sstevel@tonic-gate 2343e11c3f44Smeem /* 2344e11c3f44Smeem * Find out what netmask the interface is going to be using. 2345e11c3f44Smeem * If we just brought up an IPMP data address on an underlying 2346e11c3f44Smeem * interface above, the address will have already migrated, so 2347e11c3f44Smeem * the SIOCGLIFNETMASK won't be able to find it (but we need 2348e11c3f44Smeem * to bring the address up to get the actual netmask). Just 2349e11c3f44Smeem * omit printing the actual netmask in this corner-case. 2350e11c3f44Smeem */ 23517c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 || 2352e11c3f44Smeem inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) { 2353e11c3f44Smeem zerror(zlogp, B_FALSE, "WARNING: %s; using default.", 2354e11c3f44Smeem nomatch); 2355e11c3f44Smeem } else { 23567c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 2357e11c3f44Smeem "WARNING: %s: %s: %s; using default of %s.", 2358e11c3f44Smeem lifr.lifr_name, nomatch, addrstr4, buffer); 2359e11c3f44Smeem } 23607c478bd9Sstevel@tonic-gate } 23617c478bd9Sstevel@tonic-gate 2362de860bd9Sgfaden /* 2363de860bd9Sgfaden * If a default router was specified for this interface 2364de860bd9Sgfaden * set the route now. Ignore if already set. 2365de860bd9Sgfaden */ 2366de860bd9Sgfaden if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) { 2367de860bd9Sgfaden int status; 2368de860bd9Sgfaden char *argv[7]; 2369de860bd9Sgfaden 2370de860bd9Sgfaden argv[0] = "route"; 2371de860bd9Sgfaden argv[1] = "add"; 2372de860bd9Sgfaden argv[2] = "-ifp"; 2373de860bd9Sgfaden argv[3] = nwiftabptr->zone_nwif_physical; 2374de860bd9Sgfaden argv[4] = "default"; 2375de860bd9Sgfaden argv[5] = nwiftabptr->zone_nwif_defrouter; 2376de860bd9Sgfaden argv[6] = NULL; 2377de860bd9Sgfaden 2378de860bd9Sgfaden status = forkexec(zlogp, "/usr/sbin/route", argv); 2379de860bd9Sgfaden if (status != 0 && status != EEXIST) 2380de860bd9Sgfaden zerror(zlogp, B_FALSE, "Unable to set route for " 2381de860bd9Sgfaden "interface %s to %s\n", 2382de860bd9Sgfaden nwiftabptr->zone_nwif_physical, 2383de860bd9Sgfaden nwiftabptr->zone_nwif_defrouter); 2384de860bd9Sgfaden } 2385de860bd9Sgfaden 23867c478bd9Sstevel@tonic-gate (void) close(s); 23877c478bd9Sstevel@tonic-gate return (Z_OK); 23887c478bd9Sstevel@tonic-gate bad: 23897c478bd9Sstevel@tonic-gate (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 23907c478bd9Sstevel@tonic-gate (void) close(s); 23917c478bd9Sstevel@tonic-gate return (-1); 23927c478bd9Sstevel@tonic-gate } 23937c478bd9Sstevel@tonic-gate 23947c478bd9Sstevel@tonic-gate /* 23957c478bd9Sstevel@tonic-gate * Sets up network interfaces based on information from the zone configuration. 2396f14f3ae7Sjv227347 * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global 2397f14f3ae7Sjv227347 * system. 23987c478bd9Sstevel@tonic-gate * 23997c478bd9Sstevel@tonic-gate * If anything goes wrong, we log a general error message, attempt to tear down 24007c478bd9Sstevel@tonic-gate * whatever we set up, and return an error. 24017c478bd9Sstevel@tonic-gate */ 24027c478bd9Sstevel@tonic-gate static int 2403f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp) 24047c478bd9Sstevel@tonic-gate { 24057c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 24067c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab, loopback_iftab; 24077c478bd9Sstevel@tonic-gate zoneid_t zoneid; 24087c478bd9Sstevel@tonic-gate 24097c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) { 24107c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 24117c478bd9Sstevel@tonic-gate return (-1); 24127c478bd9Sstevel@tonic-gate } 24137c478bd9Sstevel@tonic-gate 24147c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 24157c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 24167c478bd9Sstevel@tonic-gate return (-1); 24177c478bd9Sstevel@tonic-gate } 24187c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 24197c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 24207c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 24217c478bd9Sstevel@tonic-gate return (-1); 24227c478bd9Sstevel@tonic-gate } 24237c478bd9Sstevel@tonic-gate if (zonecfg_setnwifent(handle) == Z_OK) { 24247c478bd9Sstevel@tonic-gate for (;;) { 24257c478bd9Sstevel@tonic-gate if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 24267c478bd9Sstevel@tonic-gate break; 2427f14f3ae7Sjv227347 if (configure_one_interface(zlogp, zoneid, &nwiftab) != 24287c478bd9Sstevel@tonic-gate Z_OK) { 24297c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 24307c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 24317c478bd9Sstevel@tonic-gate return (-1); 24327c478bd9Sstevel@tonic-gate } 24337c478bd9Sstevel@tonic-gate } 24347c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle); 24357c478bd9Sstevel@tonic-gate } 24367c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 2437fdb7141fSgfaden if (is_system_labeled()) { 2438fdb7141fSgfaden /* 2439fdb7141fSgfaden * Labeled zones share the loopback interface 2440fdb7141fSgfaden * so it is not plumbed for shared stack instances. 2441fdb7141fSgfaden */ 2442fdb7141fSgfaden return (0); 2443fdb7141fSgfaden } 24447c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0", 24457c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_physical)); 24467c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1", 24477c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address)); 2448442d3e9eSgfaden loopback_iftab.zone_nwif_defrouter[0] = '\0'; 2449f14f3ae7Sjv227347 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK) 24507c478bd9Sstevel@tonic-gate return (-1); 2451f14f3ae7Sjv227347 2452f14f3ae7Sjv227347 /* Always plumb up the IPv6 loopback interface. */ 24537c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128", 24547c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address)); 2455f14f3ae7Sjv227347 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK) 24567c478bd9Sstevel@tonic-gate return (-1); 24577c478bd9Sstevel@tonic-gate return (0); 24587c478bd9Sstevel@tonic-gate } 24597c478bd9Sstevel@tonic-gate 246089b1c373Smeem static void 246189b1c373Smeem zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str) 246289b1c373Smeem { 246389b1c373Smeem char errmsg[DLADM_STRSIZE]; 246489b1c373Smeem 246589b1c373Smeem (void) dladm_status2str(err, errmsg); 246689b1c373Smeem zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg); 246789b1c373Smeem } 246889b1c373Smeem 2469f4b3ec61Sdh155122 static int 24702b24ab6bSSebastien Roy add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname) 2471f4b3ec61Sdh155122 { 247289b1c373Smeem dladm_status_t err; 24730dc2366fSVenugopal Iyer boolean_t cpuset, poolset; 247489b1c373Smeem 2475f4b3ec61Sdh155122 /* First check if it's in use by global zone. */ 2476f4b3ec61Sdh155122 if (zonecfg_ifname_exists(AF_INET, dlname) || 2477f4b3ec61Sdh155122 zonecfg_ifname_exists(AF_INET6, dlname)) { 247889b1c373Smeem zerror(zlogp, B_FALSE, "WARNING: skipping network interface " 247989b1c373Smeem "'%s' which is used in the global zone", dlname); 2480f4b3ec61Sdh155122 return (-1); 2481f4b3ec61Sdh155122 } 2482f4b3ec61Sdh155122 2483d62bc4baSyz147064 /* Set zoneid of this link. */ 24842b24ab6bSSebastien Roy err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1, 24852b24ab6bSSebastien Roy DLADM_OPT_ACTIVE); 248689b1c373Smeem if (err != DLADM_STATUS_OK) { 248789b1c373Smeem zdlerror(zlogp, err, dlname, 248889b1c373Smeem "WARNING: unable to add network interface"); 2489f4b3ec61Sdh155122 return (-1); 2490f4b3ec61Sdh155122 } 24910dc2366fSVenugopal Iyer 24920dc2366fSVenugopal Iyer /* 24930dc2366fSVenugopal Iyer * Set the pool of this link if the zone has a pool and 24940dc2366fSVenugopal Iyer * neither the cpus nor the pool datalink property is 24950dc2366fSVenugopal Iyer * already set. 24960dc2366fSVenugopal Iyer */ 24970dc2366fSVenugopal Iyer err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT, 24980dc2366fSVenugopal Iyer "cpus", &cpuset); 24990dc2366fSVenugopal Iyer if (err != DLADM_STATUS_OK) { 25000dc2366fSVenugopal Iyer zdlerror(zlogp, err, dlname, 25010dc2366fSVenugopal Iyer "WARNING: unable to check if cpus link property is set"); 25020dc2366fSVenugopal Iyer } 25030dc2366fSVenugopal Iyer err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT, 25040dc2366fSVenugopal Iyer "pool", &poolset); 25050dc2366fSVenugopal Iyer if (err != DLADM_STATUS_OK) { 25060dc2366fSVenugopal Iyer zdlerror(zlogp, err, dlname, 25070dc2366fSVenugopal Iyer "WARNING: unable to check if pool link property is set"); 25080dc2366fSVenugopal Iyer } 25090dc2366fSVenugopal Iyer 25100dc2366fSVenugopal Iyer if ((strlen(pool_name) != 0) && !cpuset && !poolset) { 25110dc2366fSVenugopal Iyer err = dladm_set_linkprop(dld_handle, linkid, "pool", 25120dc2366fSVenugopal Iyer &pool_name, 1, DLADM_OPT_ACTIVE); 25130dc2366fSVenugopal Iyer if (err != DLADM_STATUS_OK) { 25140dc2366fSVenugopal Iyer zerror(zlogp, B_FALSE, "WARNING: unable to set " 25150dc2366fSVenugopal Iyer "pool %s to datalink %s", pool_name, dlname); 25160dc2366fSVenugopal Iyer bzero(pool_name, MAXPATHLEN); 25170dc2366fSVenugopal Iyer } 25180dc2366fSVenugopal Iyer } else { 25190dc2366fSVenugopal Iyer bzero(pool_name, MAXPATHLEN); 25200dc2366fSVenugopal Iyer } 2521f4b3ec61Sdh155122 return (0); 2522f4b3ec61Sdh155122 } 2523f4b3ec61Sdh155122 2524*550b6e40SSowmini Varadhan static boolean_t 2525*550b6e40SSowmini Varadhan sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr, 2526*550b6e40SSowmini Varadhan char *straddr, size_t len) 2527*550b6e40SSowmini Varadhan { 2528*550b6e40SSowmini Varadhan struct sockaddr_in *sin; 2529*550b6e40SSowmini Varadhan struct sockaddr_in6 *sin6; 2530*550b6e40SSowmini Varadhan const char *str = NULL; 2531*550b6e40SSowmini Varadhan 2532*550b6e40SSowmini Varadhan if (af == AF_INET) { 2533*550b6e40SSowmini Varadhan /* LINTED E_BAD_PTR_CAST_ALIGN */ 2534*550b6e40SSowmini Varadhan sin = SIN(sockaddr); 2535*550b6e40SSowmini Varadhan str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len); 2536*550b6e40SSowmini Varadhan } else if (af == AF_INET6) { 2537*550b6e40SSowmini Varadhan /* LINTED E_BAD_PTR_CAST_ALIGN */ 2538*550b6e40SSowmini Varadhan sin6 = SIN6(sockaddr); 2539*550b6e40SSowmini Varadhan str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr, 2540*550b6e40SSowmini Varadhan len); 2541*550b6e40SSowmini Varadhan } 2542*550b6e40SSowmini Varadhan 2543*550b6e40SSowmini Varadhan return (str != NULL); 2544*550b6e40SSowmini Varadhan } 2545*550b6e40SSowmini Varadhan 2546*550b6e40SSowmini Varadhan static int 2547*550b6e40SSowmini Varadhan ipv4_prefixlen(struct sockaddr_in *sin) 2548*550b6e40SSowmini Varadhan { 2549*550b6e40SSowmini Varadhan struct sockaddr_in *m; 2550*550b6e40SSowmini Varadhan struct sockaddr_storage mask; 2551*550b6e40SSowmini Varadhan 2552*550b6e40SSowmini Varadhan m = SIN(&mask); 2553*550b6e40SSowmini Varadhan m->sin_family = AF_INET; 2554*550b6e40SSowmini Varadhan if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) { 2555*550b6e40SSowmini Varadhan return (mask2plen(&mask)); 2556*550b6e40SSowmini Varadhan } else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) { 2557*550b6e40SSowmini Varadhan return (8); 2558*550b6e40SSowmini Varadhan } else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) { 2559*550b6e40SSowmini Varadhan return (16); 2560*550b6e40SSowmini Varadhan } else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) { 2561*550b6e40SSowmini Varadhan return (24); 2562*550b6e40SSowmini Varadhan } 2563*550b6e40SSowmini Varadhan return (0); 2564*550b6e40SSowmini Varadhan } 2565*550b6e40SSowmini Varadhan 2566*550b6e40SSowmini Varadhan static int 2567*550b6e40SSowmini Varadhan zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid, 2568*550b6e40SSowmini Varadhan void *buf, size_t bufsize) 2569*550b6e40SSowmini Varadhan { 2570*550b6e40SSowmini Varadhan zone_net_data_t *zndata; 2571*550b6e40SSowmini Varadhan size_t znsize; 2572*550b6e40SSowmini Varadhan int err; 2573*550b6e40SSowmini Varadhan 2574*550b6e40SSowmini Varadhan znsize = sizeof (*zndata) + bufsize; 2575*550b6e40SSowmini Varadhan zndata = calloc(1, znsize); 2576*550b6e40SSowmini Varadhan if (zndata == NULL) 2577*550b6e40SSowmini Varadhan return (ENOMEM); 2578*550b6e40SSowmini Varadhan zndata->zn_type = type; 2579*550b6e40SSowmini Varadhan zndata->zn_len = bufsize; 2580*550b6e40SSowmini Varadhan zndata->zn_linkid = linkid; 2581*550b6e40SSowmini Varadhan bcopy(buf, zndata->zn_val, zndata->zn_len); 2582*550b6e40SSowmini Varadhan err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize); 2583*550b6e40SSowmini Varadhan free(zndata); 2584*550b6e40SSowmini Varadhan return (err); 2585*550b6e40SSowmini Varadhan } 2586*550b6e40SSowmini Varadhan 2587*550b6e40SSowmini Varadhan static int 2588*550b6e40SSowmini Varadhan add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start) 2589*550b6e40SSowmini Varadhan { 2590*550b6e40SSowmini Varadhan struct lifreq lifr; 2591*550b6e40SSowmini Varadhan char **astr, *address; 2592*550b6e40SSowmini Varadhan dladm_status_t dlstatus; 2593*550b6e40SSowmini Varadhan char *ip_nospoof = "ip-nospoof"; 2594*550b6e40SSowmini Varadhan int nnet, naddr, err = 0, j; 2595*550b6e40SSowmini Varadhan size_t zlen, cpleft; 2596*550b6e40SSowmini Varadhan zone_addr_list_t *ptr, *end; 2597*550b6e40SSowmini Varadhan char tmp[INET6_ADDRSTRLEN], *maskstr; 2598*550b6e40SSowmini Varadhan char *zaddr, *cp; 2599*550b6e40SSowmini Varadhan struct in6_addr *routes = NULL; 2600*550b6e40SSowmini Varadhan boolean_t is_set; 2601*550b6e40SSowmini Varadhan datalink_id_t linkid; 2602*550b6e40SSowmini Varadhan 2603*550b6e40SSowmini Varadhan assert(start != NULL); 2604*550b6e40SSowmini Varadhan naddr = 0; /* number of addresses */ 2605*550b6e40SSowmini Varadhan nnet = 0; /* number of net resources */ 2606*550b6e40SSowmini Varadhan linkid = start->za_linkid; 2607*550b6e40SSowmini Varadhan for (ptr = start; ptr != NULL && ptr->za_linkid == linkid; 2608*550b6e40SSowmini Varadhan ptr = ptr->za_next) { 2609*550b6e40SSowmini Varadhan nnet++; 2610*550b6e40SSowmini Varadhan } 2611*550b6e40SSowmini Varadhan end = ptr; 2612*550b6e40SSowmini Varadhan zlen = nnet * (INET6_ADDRSTRLEN + 1); 2613*550b6e40SSowmini Varadhan astr = calloc(1, nnet * sizeof (uintptr_t)); 2614*550b6e40SSowmini Varadhan zaddr = calloc(1, zlen); 2615*550b6e40SSowmini Varadhan if (astr == NULL || zaddr == NULL) { 2616*550b6e40SSowmini Varadhan err = ENOMEM; 2617*550b6e40SSowmini Varadhan goto done; 2618*550b6e40SSowmini Varadhan } 2619*550b6e40SSowmini Varadhan cp = zaddr; 2620*550b6e40SSowmini Varadhan cpleft = zlen; 2621*550b6e40SSowmini Varadhan j = 0; 2622*550b6e40SSowmini Varadhan for (ptr = start; ptr != end; ptr = ptr->za_next) { 2623*550b6e40SSowmini Varadhan address = ptr->za_nwiftab.zone_nwif_allowed_address; 2624*550b6e40SSowmini Varadhan if (address[0] == '\0') 2625*550b6e40SSowmini Varadhan continue; 2626*550b6e40SSowmini Varadhan (void) snprintf(tmp, sizeof (tmp), "%s", address); 2627*550b6e40SSowmini Varadhan /* 2628*550b6e40SSowmini Varadhan * Validate the data. zonecfg_valid_net_address() clobbers 2629*550b6e40SSowmini Varadhan * the /<mask> in the address string. 2630*550b6e40SSowmini Varadhan */ 2631*550b6e40SSowmini Varadhan if (zonecfg_valid_net_address(address, &lifr) != Z_OK) { 2632*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "invalid address [%s]\n", 2633*550b6e40SSowmini Varadhan address); 2634*550b6e40SSowmini Varadhan err = EINVAL; 2635*550b6e40SSowmini Varadhan goto done; 2636*550b6e40SSowmini Varadhan } 2637*550b6e40SSowmini Varadhan /* 2638*550b6e40SSowmini Varadhan * convert any hostnames to numeric address strings. 2639*550b6e40SSowmini Varadhan */ 2640*550b6e40SSowmini Varadhan if (!sockaddr_to_str(lifr.lifr_addr.ss_family, 2641*550b6e40SSowmini Varadhan (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) { 2642*550b6e40SSowmini Varadhan err = EINVAL; 2643*550b6e40SSowmini Varadhan goto done; 2644*550b6e40SSowmini Varadhan } 2645*550b6e40SSowmini Varadhan /* 2646*550b6e40SSowmini Varadhan * make a copy of the numeric string for the data needed 2647*550b6e40SSowmini Varadhan * by the "allowed-ips" datalink property. 2648*550b6e40SSowmini Varadhan */ 2649*550b6e40SSowmini Varadhan astr[j] = strdup(cp); 2650*550b6e40SSowmini Varadhan if (astr[j] == NULL) { 2651*550b6e40SSowmini Varadhan err = ENOMEM; 2652*550b6e40SSowmini Varadhan goto done; 2653*550b6e40SSowmini Varadhan } 2654*550b6e40SSowmini Varadhan j++; 2655*550b6e40SSowmini Varadhan /* 2656*550b6e40SSowmini Varadhan * compute the default netmask from the address, if necessary 2657*550b6e40SSowmini Varadhan */ 2658*550b6e40SSowmini Varadhan if ((maskstr = strchr(tmp, '/')) == NULL) { 2659*550b6e40SSowmini Varadhan int prefixlen; 2660*550b6e40SSowmini Varadhan 2661*550b6e40SSowmini Varadhan if (lifr.lifr_addr.ss_family == AF_INET) { 2662*550b6e40SSowmini Varadhan prefixlen = ipv4_prefixlen( 2663*550b6e40SSowmini Varadhan SIN(&lifr.lifr_addr)); 2664*550b6e40SSowmini Varadhan } else { 2665*550b6e40SSowmini Varadhan struct sockaddr_in6 *sin6; 2666*550b6e40SSowmini Varadhan 2667*550b6e40SSowmini Varadhan sin6 = SIN6(&lifr.lifr_addr); 2668*550b6e40SSowmini Varadhan if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 2669*550b6e40SSowmini Varadhan prefixlen = 10; 2670*550b6e40SSowmini Varadhan else 2671*550b6e40SSowmini Varadhan prefixlen = 64; 2672*550b6e40SSowmini Varadhan } 2673*550b6e40SSowmini Varadhan (void) snprintf(tmp, sizeof (tmp), "%d", prefixlen); 2674*550b6e40SSowmini Varadhan maskstr = tmp; 2675*550b6e40SSowmini Varadhan } else { 2676*550b6e40SSowmini Varadhan maskstr++; 2677*550b6e40SSowmini Varadhan } 2678*550b6e40SSowmini Varadhan /* append the "/<netmask>" */ 2679*550b6e40SSowmini Varadhan (void) strlcat(cp, "/", cpleft); 2680*550b6e40SSowmini Varadhan (void) strlcat(cp, maskstr, cpleft); 2681*550b6e40SSowmini Varadhan (void) strlcat(cp, ",", cpleft); 2682*550b6e40SSowmini Varadhan cp += strnlen(cp, zlen); 2683*550b6e40SSowmini Varadhan cpleft = &zaddr[INET6_ADDRSTRLEN] - cp; 2684*550b6e40SSowmini Varadhan } 2685*550b6e40SSowmini Varadhan naddr = j; /* the actual number of addresses in the net resource */ 2686*550b6e40SSowmini Varadhan assert(naddr <= nnet); 2687*550b6e40SSowmini Varadhan 2688*550b6e40SSowmini Varadhan /* 2689*550b6e40SSowmini Varadhan * zonecfg has already verified that the defrouter property can only 2690*550b6e40SSowmini Varadhan * be set if there is at least one address defined for the net resource. 2691*550b6e40SSowmini Varadhan * If j is 0, there are no addresses defined, and therefore no routers 2692*550b6e40SSowmini Varadhan * to configure, and we are done at that point. 2693*550b6e40SSowmini Varadhan */ 2694*550b6e40SSowmini Varadhan if (j == 0) 2695*550b6e40SSowmini Varadhan goto done; 2696*550b6e40SSowmini Varadhan 2697*550b6e40SSowmini Varadhan /* over-write last ',' with '\0' */ 2698*550b6e40SSowmini Varadhan zaddr[strnlen(zaddr, zlen) + 1] = '\0'; 2699*550b6e40SSowmini Varadhan 2700*550b6e40SSowmini Varadhan /* 2701*550b6e40SSowmini Varadhan * First make sure L3 protection is not already set on the link. 2702*550b6e40SSowmini Varadhan */ 2703*550b6e40SSowmini Varadhan dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE, 2704*550b6e40SSowmini Varadhan "protection", &is_set); 2705*550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) { 2706*550b6e40SSowmini Varadhan err = EINVAL; 2707*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "unable to check if protection is set"); 2708*550b6e40SSowmini Varadhan goto done; 2709*550b6e40SSowmini Varadhan } 2710*550b6e40SSowmini Varadhan if (is_set) { 2711*550b6e40SSowmini Varadhan err = EINVAL; 2712*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "Protection is already set"); 2713*550b6e40SSowmini Varadhan goto done; 2714*550b6e40SSowmini Varadhan } 2715*550b6e40SSowmini Varadhan dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE, 2716*550b6e40SSowmini Varadhan "allowed-ips", &is_set); 2717*550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) { 2718*550b6e40SSowmini Varadhan err = EINVAL; 2719*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set"); 2720*550b6e40SSowmini Varadhan goto done; 2721*550b6e40SSowmini Varadhan } 2722*550b6e40SSowmini Varadhan if (is_set) { 2723*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "allowed-ips is already set"); 2724*550b6e40SSowmini Varadhan err = EINVAL; 2725*550b6e40SSowmini Varadhan goto done; 2726*550b6e40SSowmini Varadhan } 2727*550b6e40SSowmini Varadhan 2728*550b6e40SSowmini Varadhan /* 2729*550b6e40SSowmini Varadhan * Enable ip-nospoof for the link, and add address to the allowed-ips 2730*550b6e40SSowmini Varadhan * list. 2731*550b6e40SSowmini Varadhan */ 2732*550b6e40SSowmini Varadhan dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection", 2733*550b6e40SSowmini Varadhan &ip_nospoof, 1, DLADM_OPT_ACTIVE); 2734*550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) { 2735*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "could not set protection\n"); 2736*550b6e40SSowmini Varadhan err = EINVAL; 2737*550b6e40SSowmini Varadhan goto done; 2738*550b6e40SSowmini Varadhan } 2739*550b6e40SSowmini Varadhan dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips", 2740*550b6e40SSowmini Varadhan astr, naddr, DLADM_OPT_ACTIVE); 2741*550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) { 2742*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "could not set allowed-ips\n"); 2743*550b6e40SSowmini Varadhan err = EINVAL; 2744*550b6e40SSowmini Varadhan goto done; 2745*550b6e40SSowmini Varadhan } 2746*550b6e40SSowmini Varadhan 2747*550b6e40SSowmini Varadhan /* now set the address in the data-store */ 2748*550b6e40SSowmini Varadhan err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid, 2749*550b6e40SSowmini Varadhan zaddr, strnlen(zaddr, zlen) + 1); 2750*550b6e40SSowmini Varadhan if (err != 0) 2751*550b6e40SSowmini Varadhan goto done; 2752*550b6e40SSowmini Varadhan 2753*550b6e40SSowmini Varadhan /* 2754*550b6e40SSowmini Varadhan * add the defaultrouters 2755*550b6e40SSowmini Varadhan */ 2756*550b6e40SSowmini Varadhan routes = calloc(1, nnet * sizeof (*routes)); 2757*550b6e40SSowmini Varadhan j = 0; 2758*550b6e40SSowmini Varadhan for (ptr = start; ptr != end; ptr = ptr->za_next) { 2759*550b6e40SSowmini Varadhan address = ptr->za_nwiftab.zone_nwif_defrouter; 2760*550b6e40SSowmini Varadhan if (address[0] == '\0') 2761*550b6e40SSowmini Varadhan continue; 2762*550b6e40SSowmini Varadhan if (strchr(address, '/') == NULL && strchr(address, ':') != 0) { 2763*550b6e40SSowmini Varadhan /* 2764*550b6e40SSowmini Varadhan * zonecfg_valid_net_address() expects numeric IPv6 2765*550b6e40SSowmini Varadhan * addresses to have a CIDR format netmask. 2766*550b6e40SSowmini Varadhan */ 2767*550b6e40SSowmini Varadhan (void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN); 2768*550b6e40SSowmini Varadhan (void) strlcat(address, tmp, INET6_ADDRSTRLEN); 2769*550b6e40SSowmini Varadhan } 2770*550b6e40SSowmini Varadhan if (zonecfg_valid_net_address(address, &lifr) != Z_OK) { 2771*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, 2772*550b6e40SSowmini Varadhan "invalid router [%s]\n", address); 2773*550b6e40SSowmini Varadhan err = EINVAL; 2774*550b6e40SSowmini Varadhan goto done; 2775*550b6e40SSowmini Varadhan } 2776*550b6e40SSowmini Varadhan if (lifr.lifr_addr.ss_family == AF_INET6) { 2777*550b6e40SSowmini Varadhan routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr; 2778*550b6e40SSowmini Varadhan } else { 2779*550b6e40SSowmini Varadhan IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr, 2780*550b6e40SSowmini Varadhan &routes[j]); 2781*550b6e40SSowmini Varadhan } 2782*550b6e40SSowmini Varadhan j++; 2783*550b6e40SSowmini Varadhan } 2784*550b6e40SSowmini Varadhan assert(j <= nnet); 2785*550b6e40SSowmini Varadhan if (j > 0) { 2786*550b6e40SSowmini Varadhan err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid, 2787*550b6e40SSowmini Varadhan linkid, routes, j * sizeof (*routes)); 2788*550b6e40SSowmini Varadhan } 2789*550b6e40SSowmini Varadhan done: 2790*550b6e40SSowmini Varadhan free(routes); 2791*550b6e40SSowmini Varadhan for (j = 0; j < naddr; j++) 2792*550b6e40SSowmini Varadhan free(astr[j]); 2793*550b6e40SSowmini Varadhan free(astr); 2794*550b6e40SSowmini Varadhan free(zaddr); 2795*550b6e40SSowmini Varadhan return (err); 2796*550b6e40SSowmini Varadhan 2797*550b6e40SSowmini Varadhan } 2798*550b6e40SSowmini Varadhan 2799*550b6e40SSowmini Varadhan static int 2800*550b6e40SSowmini Varadhan add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist) 2801*550b6e40SSowmini Varadhan { 2802*550b6e40SSowmini Varadhan zone_addr_list_t *ptr; 2803*550b6e40SSowmini Varadhan datalink_id_t linkid; 2804*550b6e40SSowmini Varadhan int err; 2805*550b6e40SSowmini Varadhan 2806*550b6e40SSowmini Varadhan if (zalist == NULL) 2807*550b6e40SSowmini Varadhan return (0); 2808*550b6e40SSowmini Varadhan 2809*550b6e40SSowmini Varadhan linkid = zalist->za_linkid; 2810*550b6e40SSowmini Varadhan 2811*550b6e40SSowmini Varadhan err = add_net_for_linkid(zlogp, zoneid, zalist); 2812*550b6e40SSowmini Varadhan if (err != 0) 2813*550b6e40SSowmini Varadhan return (err); 2814*550b6e40SSowmini Varadhan 2815*550b6e40SSowmini Varadhan for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) { 2816*550b6e40SSowmini Varadhan if (ptr->za_linkid == linkid) 2817*550b6e40SSowmini Varadhan continue; 2818*550b6e40SSowmini Varadhan linkid = ptr->za_linkid; 2819*550b6e40SSowmini Varadhan err = add_net_for_linkid(zlogp, zoneid, ptr); 2820*550b6e40SSowmini Varadhan if (err != 0) 2821*550b6e40SSowmini Varadhan return (err); 2822*550b6e40SSowmini Varadhan } 2823*550b6e40SSowmini Varadhan return (0); 2824*550b6e40SSowmini Varadhan } 2825*550b6e40SSowmini Varadhan 2826*550b6e40SSowmini Varadhan /* 2827*550b6e40SSowmini Varadhan * Add "new" to the list of network interfaces to be configured by 2828*550b6e40SSowmini Varadhan * add_net on zone boot in "old". The list of interfaces in "old" is 2829*550b6e40SSowmini Varadhan * sorted by datalink_id_t, with interfaces sorted FIFO for a given 2830*550b6e40SSowmini Varadhan * datalink_id_t. 2831*550b6e40SSowmini Varadhan * 2832*550b6e40SSowmini Varadhan * Returns the merged list of IP interfaces containing "old" and "new" 2833*550b6e40SSowmini Varadhan */ 2834*550b6e40SSowmini Varadhan static zone_addr_list_t * 2835*550b6e40SSowmini Varadhan add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new) 2836*550b6e40SSowmini Varadhan { 2837*550b6e40SSowmini Varadhan zone_addr_list_t *ptr, *next; 2838*550b6e40SSowmini Varadhan datalink_id_t linkid = new->za_linkid; 2839*550b6e40SSowmini Varadhan 2840*550b6e40SSowmini Varadhan assert(old != new); 2841*550b6e40SSowmini Varadhan 2842*550b6e40SSowmini Varadhan if (old == NULL) 2843*550b6e40SSowmini Varadhan return (new); 2844*550b6e40SSowmini Varadhan for (ptr = old; ptr != NULL; ptr = ptr->za_next) { 2845*550b6e40SSowmini Varadhan if (ptr->za_linkid == linkid) 2846*550b6e40SSowmini Varadhan break; 2847*550b6e40SSowmini Varadhan } 2848*550b6e40SSowmini Varadhan if (ptr == NULL) { 2849*550b6e40SSowmini Varadhan /* linkid does not already exist, add to the beginning */ 2850*550b6e40SSowmini Varadhan new->za_next = old; 2851*550b6e40SSowmini Varadhan return (new); 2852*550b6e40SSowmini Varadhan } 2853*550b6e40SSowmini Varadhan /* 2854*550b6e40SSowmini Varadhan * adding to the middle of the list; ptr points at the first 2855*550b6e40SSowmini Varadhan * occurrence of linkid. Find the last occurrence. 2856*550b6e40SSowmini Varadhan */ 2857*550b6e40SSowmini Varadhan while ((next = ptr->za_next) != NULL) { 2858*550b6e40SSowmini Varadhan if (next->za_linkid != linkid) 2859*550b6e40SSowmini Varadhan break; 2860*550b6e40SSowmini Varadhan ptr = next; 2861*550b6e40SSowmini Varadhan } 2862*550b6e40SSowmini Varadhan /* insert new after ptr */ 2863*550b6e40SSowmini Varadhan new->za_next = next; 2864*550b6e40SSowmini Varadhan ptr->za_next = new; 2865*550b6e40SSowmini Varadhan return (old); 2866*550b6e40SSowmini Varadhan } 2867*550b6e40SSowmini Varadhan 2868*550b6e40SSowmini Varadhan void 2869*550b6e40SSowmini Varadhan free_ip_interface(zone_addr_list_t *zalist) 2870*550b6e40SSowmini Varadhan { 2871*550b6e40SSowmini Varadhan zone_addr_list_t *ptr, *new; 2872*550b6e40SSowmini Varadhan 2873*550b6e40SSowmini Varadhan for (ptr = zalist; ptr != NULL; ) { 2874*550b6e40SSowmini Varadhan new = ptr; 2875*550b6e40SSowmini Varadhan ptr = ptr->za_next; 2876*550b6e40SSowmini Varadhan free(new); 2877*550b6e40SSowmini Varadhan } 2878*550b6e40SSowmini Varadhan } 2879*550b6e40SSowmini Varadhan 2880f4b3ec61Sdh155122 /* 2881f4b3ec61Sdh155122 * Add the kernel access control information for the interface names. 2882f4b3ec61Sdh155122 * If anything goes wrong, we log a general error message, attempt to tear down 2883f4b3ec61Sdh155122 * whatever we set up, and return an error. 2884f4b3ec61Sdh155122 */ 2885f4b3ec61Sdh155122 static int 2886*550b6e40SSowmini Varadhan configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid) 2887f4b3ec61Sdh155122 { 2888f4b3ec61Sdh155122 zone_dochandle_t handle; 2889f4b3ec61Sdh155122 struct zone_nwiftab nwiftab; 2890f4b3ec61Sdh155122 char rootpath[MAXPATHLEN]; 2891f4b3ec61Sdh155122 char path[MAXPATHLEN]; 28922b24ab6bSSebastien Roy datalink_id_t linkid; 2893f4b3ec61Sdh155122 di_prof_t prof = NULL; 2894f4b3ec61Sdh155122 boolean_t added = B_FALSE; 2895*550b6e40SSowmini Varadhan zone_addr_list_t *zalist = NULL, *new; 2896f4b3ec61Sdh155122 2897f4b3ec61Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) { 2898f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2899f4b3ec61Sdh155122 return (-1); 2900f4b3ec61Sdh155122 } 2901f4b3ec61Sdh155122 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2902f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "invalid configuration"); 2903f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2904f4b3ec61Sdh155122 return (-1); 2905f4b3ec61Sdh155122 } 2906f4b3ec61Sdh155122 2907f4b3ec61Sdh155122 if (zonecfg_setnwifent(handle) != Z_OK) { 2908f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2909f4b3ec61Sdh155122 return (0); 2910f4b3ec61Sdh155122 } 2911f4b3ec61Sdh155122 2912f4b3ec61Sdh155122 for (;;) { 2913f4b3ec61Sdh155122 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 2914f4b3ec61Sdh155122 break; 2915f4b3ec61Sdh155122 2916f4b3ec61Sdh155122 if (prof == NULL) { 2917f4b3ec61Sdh155122 if (zone_get_devroot(zone_name, rootpath, 2918f4b3ec61Sdh155122 sizeof (rootpath)) != Z_OK) { 2919f4b3ec61Sdh155122 (void) zonecfg_endnwifent(handle); 2920f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2921f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, 2922f4b3ec61Sdh155122 "unable to determine dev root"); 2923f4b3ec61Sdh155122 return (-1); 2924f4b3ec61Sdh155122 } 2925f4b3ec61Sdh155122 (void) snprintf(path, sizeof (path), "%s%s", rootpath, 2926f4b3ec61Sdh155122 "/dev"); 2927f4b3ec61Sdh155122 if (di_prof_init(path, &prof) != 0) { 2928f4b3ec61Sdh155122 (void) zonecfg_endnwifent(handle); 2929f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2930f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, 2931f4b3ec61Sdh155122 "failed to initialize profile"); 2932f4b3ec61Sdh155122 return (-1); 2933f4b3ec61Sdh155122 } 2934f4b3ec61Sdh155122 } 2935f4b3ec61Sdh155122 2936f4b3ec61Sdh155122 /* 2937d62bc4baSyz147064 * Create the /dev entry for backward compatibility. 2938f4b3ec61Sdh155122 * Only create the /dev entry if it's not in use. 2939d62bc4baSyz147064 * Note that the zone still boots when the assigned 2940d62bc4baSyz147064 * interface is inaccessible, used by others, etc. 2941d62bc4baSyz147064 * Also, when vanity naming is used, some interface do 2942d62bc4baSyz147064 * do not have corresponding /dev node names (for example, 2943d62bc4baSyz147064 * vanity named aggregations). The /dev entry is not 2944d62bc4baSyz147064 * created in that case. The /dev/net entry is always 2945d62bc4baSyz147064 * accessible. 2946f4b3ec61Sdh155122 */ 29472b24ab6bSSebastien Roy if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical, 29482b24ab6bSSebastien Roy &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK && 29492b24ab6bSSebastien Roy add_datalink(zlogp, zone_name, linkid, 29502b24ab6bSSebastien Roy nwiftab.zone_nwif_physical) == 0) { 29513bc21d0aSAruna Ramakrishna - Sun Microsystems added = B_TRUE; 29523bc21d0aSAruna Ramakrishna - Sun Microsystems } else { 2953f4b3ec61Sdh155122 (void) zonecfg_endnwifent(handle); 2954f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 29553bc21d0aSAruna Ramakrishna - Sun Microsystems zerror(zlogp, B_TRUE, "failed to add network device"); 2956f4b3ec61Sdh155122 return (-1); 2957f4b3ec61Sdh155122 } 2958*550b6e40SSowmini Varadhan /* set up the new IP interface, and add them all later */ 2959*550b6e40SSowmini Varadhan new = malloc(sizeof (*new)); 2960*550b6e40SSowmini Varadhan if (new == NULL) { 2961*550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "no memory for %s", 2962*550b6e40SSowmini Varadhan nwiftab.zone_nwif_physical); 2963*550b6e40SSowmini Varadhan zonecfg_fini_handle(handle); 2964*550b6e40SSowmini Varadhan free_ip_interface(zalist); 2965*550b6e40SSowmini Varadhan } 2966*550b6e40SSowmini Varadhan bzero(new, sizeof (*new)); 2967*550b6e40SSowmini Varadhan new->za_nwiftab = nwiftab; 2968*550b6e40SSowmini Varadhan new->za_linkid = linkid; 2969*550b6e40SSowmini Varadhan zalist = add_ip_interface(zalist, new); 2970*550b6e40SSowmini Varadhan } 2971*550b6e40SSowmini Varadhan if (zalist != NULL) { 2972*550b6e40SSowmini Varadhan if ((errno = add_net(zlogp, zoneid, zalist)) != 0) { 2973*550b6e40SSowmini Varadhan (void) zonecfg_endnwifent(handle); 2974*550b6e40SSowmini Varadhan zonecfg_fini_handle(handle); 2975*550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "failed to add address"); 2976*550b6e40SSowmini Varadhan free_ip_interface(zalist); 2977*550b6e40SSowmini Varadhan return (-1); 2978*550b6e40SSowmini Varadhan } 2979*550b6e40SSowmini Varadhan free_ip_interface(zalist); 2980d62bc4baSyz147064 } 2981f4b3ec61Sdh155122 (void) zonecfg_endnwifent(handle); 2982f4b3ec61Sdh155122 zonecfg_fini_handle(handle); 2983f4b3ec61Sdh155122 2984f4b3ec61Sdh155122 if (prof != NULL && added) { 2985f4b3ec61Sdh155122 if (di_prof_commit(prof) != 0) { 2986f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "failed to commit profile"); 2987f4b3ec61Sdh155122 return (-1); 2988f4b3ec61Sdh155122 } 2989f4b3ec61Sdh155122 } 2990f4b3ec61Sdh155122 if (prof != NULL) 2991f4b3ec61Sdh155122 di_prof_fini(prof); 2992f4b3ec61Sdh155122 2993f4b3ec61Sdh155122 return (0); 2994f4b3ec61Sdh155122 } 2995f4b3ec61Sdh155122 2996f4b3ec61Sdh155122 static int 29970dc2366fSVenugopal Iyer remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid) 29980dc2366fSVenugopal Iyer { 29990dc2366fSVenugopal Iyer ushort_t flags; 30000dc2366fSVenugopal Iyer zone_iptype_t iptype; 30010dc2366fSVenugopal Iyer int i, dlnum = 0; 30020dc2366fSVenugopal Iyer datalink_id_t *dllink, *dllinks = NULL; 30030dc2366fSVenugopal Iyer dladm_status_t err; 30040dc2366fSVenugopal Iyer 30050dc2366fSVenugopal Iyer if (strlen(pool_name) == 0) 30060dc2366fSVenugopal Iyer return (0); 30070dc2366fSVenugopal Iyer 30080dc2366fSVenugopal Iyer if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags, 30090dc2366fSVenugopal Iyer sizeof (flags)) < 0) { 30100dc2366fSVenugopal Iyer if (vplat_get_iptype(zlogp, &iptype) < 0) { 3011*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "unable to determine ip-type"); 30120dc2366fSVenugopal Iyer return (-1); 30130dc2366fSVenugopal Iyer } 30140dc2366fSVenugopal Iyer } else { 30150dc2366fSVenugopal Iyer if (flags & ZF_NET_EXCL) 30160dc2366fSVenugopal Iyer iptype = ZS_EXCLUSIVE; 30170dc2366fSVenugopal Iyer else 30180dc2366fSVenugopal Iyer iptype = ZS_SHARED; 30190dc2366fSVenugopal Iyer } 30200dc2366fSVenugopal Iyer 30210dc2366fSVenugopal Iyer if (iptype == ZS_EXCLUSIVE) { 30220dc2366fSVenugopal Iyer /* 30230dc2366fSVenugopal Iyer * Get the datalink count and for each datalink, 30240dc2366fSVenugopal Iyer * attempt to clear the pool property and clear 30250dc2366fSVenugopal Iyer * the pool_name. 30260dc2366fSVenugopal Iyer */ 30270dc2366fSVenugopal Iyer if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) { 30280dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE, "unable to count network " 30290dc2366fSVenugopal Iyer "interfaces"); 30300dc2366fSVenugopal Iyer return (-1); 30310dc2366fSVenugopal Iyer } 30320dc2366fSVenugopal Iyer 30330dc2366fSVenugopal Iyer if (dlnum == 0) 30340dc2366fSVenugopal Iyer return (0); 30350dc2366fSVenugopal Iyer 30360dc2366fSVenugopal Iyer if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) 30370dc2366fSVenugopal Iyer == NULL) { 30380dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE, "memory allocation failed"); 30390dc2366fSVenugopal Iyer return (-1); 30400dc2366fSVenugopal Iyer } 30410dc2366fSVenugopal Iyer if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) { 30420dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE, "unable to list network " 30430dc2366fSVenugopal Iyer "interfaces"); 30440dc2366fSVenugopal Iyer return (-1); 30450dc2366fSVenugopal Iyer } 30460dc2366fSVenugopal Iyer 30470dc2366fSVenugopal Iyer bzero(pool_name, MAXPATHLEN); 30480dc2366fSVenugopal Iyer for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) { 30490dc2366fSVenugopal Iyer err = dladm_set_linkprop(dld_handle, *dllink, "pool", 30500dc2366fSVenugopal Iyer NULL, 0, DLADM_OPT_ACTIVE); 30510dc2366fSVenugopal Iyer if (err != DLADM_STATUS_OK) { 30520dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE, 30530dc2366fSVenugopal Iyer "WARNING: unable to clear pool"); 30540dc2366fSVenugopal Iyer } 30550dc2366fSVenugopal Iyer } 30560dc2366fSVenugopal Iyer free(dllinks); 30570dc2366fSVenugopal Iyer } 30580dc2366fSVenugopal Iyer return (0); 30590dc2366fSVenugopal Iyer } 30600dc2366fSVenugopal Iyer 30610dc2366fSVenugopal Iyer static int 3062*550b6e40SSowmini Varadhan remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid) 3063*550b6e40SSowmini Varadhan { 3064*550b6e40SSowmini Varadhan ushort_t flags; 3065*550b6e40SSowmini Varadhan zone_iptype_t iptype; 3066*550b6e40SSowmini Varadhan int i, dlnum = 0; 3067*550b6e40SSowmini Varadhan dladm_status_t dlstatus; 3068*550b6e40SSowmini Varadhan datalink_id_t *dllink, *dllinks = NULL; 3069*550b6e40SSowmini Varadhan 3070*550b6e40SSowmini Varadhan if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags, 3071*550b6e40SSowmini Varadhan sizeof (flags)) < 0) { 3072*550b6e40SSowmini Varadhan if (vplat_get_iptype(zlogp, &iptype) < 0) { 3073*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "unable to determine ip-type"); 3074*550b6e40SSowmini Varadhan return (-1); 3075*550b6e40SSowmini Varadhan } 3076*550b6e40SSowmini Varadhan } else { 3077*550b6e40SSowmini Varadhan if (flags & ZF_NET_EXCL) 3078*550b6e40SSowmini Varadhan iptype = ZS_EXCLUSIVE; 3079*550b6e40SSowmini Varadhan else 3080*550b6e40SSowmini Varadhan iptype = ZS_SHARED; 3081*550b6e40SSowmini Varadhan } 3082*550b6e40SSowmini Varadhan 3083*550b6e40SSowmini Varadhan if (iptype != ZS_EXCLUSIVE) 3084*550b6e40SSowmini Varadhan return (0); 3085*550b6e40SSowmini Varadhan 3086*550b6e40SSowmini Varadhan /* 3087*550b6e40SSowmini Varadhan * Get the datalink count and for each datalink, 3088*550b6e40SSowmini Varadhan * attempt to clear the pool property and clear 3089*550b6e40SSowmini Varadhan * the pool_name. 3090*550b6e40SSowmini Varadhan */ 3091*550b6e40SSowmini Varadhan if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) { 3092*550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "unable to count network interfaces"); 3093*550b6e40SSowmini Varadhan return (-1); 3094*550b6e40SSowmini Varadhan } 3095*550b6e40SSowmini Varadhan 3096*550b6e40SSowmini Varadhan if (dlnum == 0) 3097*550b6e40SSowmini Varadhan return (0); 3098*550b6e40SSowmini Varadhan 3099*550b6e40SSowmini Varadhan if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) { 3100*550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "memory allocation failed"); 3101*550b6e40SSowmini Varadhan return (-1); 3102*550b6e40SSowmini Varadhan } 3103*550b6e40SSowmini Varadhan if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) { 3104*550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "unable to list network interfaces"); 3105*550b6e40SSowmini Varadhan free(dllinks); 3106*550b6e40SSowmini Varadhan return (-1); 3107*550b6e40SSowmini Varadhan } 3108*550b6e40SSowmini Varadhan 3109*550b6e40SSowmini Varadhan for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) { 3110*550b6e40SSowmini Varadhan dlstatus = dladm_set_linkprop(dld_handle, *dllink, 3111*550b6e40SSowmini Varadhan "protection", NULL, 0, DLADM_OPT_ACTIVE); 3112*550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) { 3113*550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "could not reset protection\n"); 3114*550b6e40SSowmini Varadhan free(dllinks); 3115*550b6e40SSowmini Varadhan return (-1); 3116*550b6e40SSowmini Varadhan } 3117*550b6e40SSowmini Varadhan dlstatus = dladm_set_linkprop(dld_handle, *dllink, 3118*550b6e40SSowmini Varadhan "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE); 3119*550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) { 3120*550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "could not reset allowed-ips\n"); 3121*550b6e40SSowmini Varadhan free(dllinks); 3122*550b6e40SSowmini Varadhan return (-1); 3123*550b6e40SSowmini Varadhan } 3124*550b6e40SSowmini Varadhan } 3125*550b6e40SSowmini Varadhan free(dllinks); 3126*550b6e40SSowmini Varadhan return (0); 3127*550b6e40SSowmini Varadhan } 3128*550b6e40SSowmini Varadhan 3129*550b6e40SSowmini Varadhan static int 31302b24ab6bSSebastien Roy unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid) 3131f4b3ec61Sdh155122 { 31322b24ab6bSSebastien Roy int dlnum = 0; 3133f4b3ec61Sdh155122 31342b24ab6bSSebastien Roy /* 31352b24ab6bSSebastien Roy * The kernel shutdown callback for the dls module should have removed 31362b24ab6bSSebastien Roy * all datalinks from this zone. If any remain, then there's a 31372b24ab6bSSebastien Roy * problem. 31382b24ab6bSSebastien Roy */ 3139f4b3ec61Sdh155122 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) { 3140f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to list network interfaces"); 3141f4b3ec61Sdh155122 return (-1); 3142f4b3ec61Sdh155122 } 31432b24ab6bSSebastien Roy if (dlnum != 0) { 31442b24ab6bSSebastien Roy zerror(zlogp, B_FALSE, 31452b24ab6bSSebastien Roy "datalinks remain in zone after shutdown"); 3146f4b3ec61Sdh155122 return (-1); 3147f4b3ec61Sdh155122 } 3148f4b3ec61Sdh155122 return (0); 3149f4b3ec61Sdh155122 } 3150f4b3ec61Sdh155122 31517c478bd9Sstevel@tonic-gate static int 31527c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid, 31537c478bd9Sstevel@tonic-gate const struct sockaddr_storage *local, const struct sockaddr_storage *remote) 31547c478bd9Sstevel@tonic-gate { 31557c478bd9Sstevel@tonic-gate int fd; 31567c478bd9Sstevel@tonic-gate struct strioctl ioc; 31577c478bd9Sstevel@tonic-gate tcp_ioc_abort_conn_t conn; 31587c478bd9Sstevel@tonic-gate int error; 31597c478bd9Sstevel@tonic-gate 31607c478bd9Sstevel@tonic-gate conn.ac_local = *local; 31617c478bd9Sstevel@tonic-gate conn.ac_remote = *remote; 31627c478bd9Sstevel@tonic-gate conn.ac_start = TCPS_SYN_SENT; 31637c478bd9Sstevel@tonic-gate conn.ac_end = TCPS_TIME_WAIT; 31647c478bd9Sstevel@tonic-gate conn.ac_zoneid = zoneid; 31657c478bd9Sstevel@tonic-gate 31667c478bd9Sstevel@tonic-gate ioc.ic_cmd = TCP_IOC_ABORT_CONN; 31677c478bd9Sstevel@tonic-gate ioc.ic_timout = -1; /* infinite timeout */ 31687c478bd9Sstevel@tonic-gate ioc.ic_len = sizeof (conn); 31697c478bd9Sstevel@tonic-gate ioc.ic_dp = (char *)&conn; 31707c478bd9Sstevel@tonic-gate 31717c478bd9Sstevel@tonic-gate if ((fd = open("/dev/tcp", O_RDONLY)) < 0) { 31727c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp"); 31737c478bd9Sstevel@tonic-gate return (-1); 31747c478bd9Sstevel@tonic-gate } 31757c478bd9Sstevel@tonic-gate 31767c478bd9Sstevel@tonic-gate error = ioctl(fd, I_STR, &ioc); 31777c478bd9Sstevel@tonic-gate (void) close(fd); 31787c478bd9Sstevel@tonic-gate if (error == 0 || errno == ENOENT) /* ENOENT is not an error */ 31797c478bd9Sstevel@tonic-gate return (0); 31807c478bd9Sstevel@tonic-gate return (-1); 31817c478bd9Sstevel@tonic-gate } 31827c478bd9Sstevel@tonic-gate 31837c478bd9Sstevel@tonic-gate static int 31847c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid) 31857c478bd9Sstevel@tonic-gate { 31867c478bd9Sstevel@tonic-gate struct sockaddr_storage l, r; 31877c478bd9Sstevel@tonic-gate struct sockaddr_in *local, *remote; 31887c478bd9Sstevel@tonic-gate struct sockaddr_in6 *local6, *remote6; 31897c478bd9Sstevel@tonic-gate int error; 31907c478bd9Sstevel@tonic-gate 31917c478bd9Sstevel@tonic-gate /* 31927c478bd9Sstevel@tonic-gate * Abort IPv4 connections. 31937c478bd9Sstevel@tonic-gate */ 31947c478bd9Sstevel@tonic-gate bzero(&l, sizeof (*local)); 31957c478bd9Sstevel@tonic-gate local = (struct sockaddr_in *)&l; 31967c478bd9Sstevel@tonic-gate local->sin_family = AF_INET; 31977c478bd9Sstevel@tonic-gate local->sin_addr.s_addr = INADDR_ANY; 31987c478bd9Sstevel@tonic-gate local->sin_port = 0; 31997c478bd9Sstevel@tonic-gate 32007c478bd9Sstevel@tonic-gate bzero(&r, sizeof (*remote)); 32017c478bd9Sstevel@tonic-gate remote = (struct sockaddr_in *)&r; 32027c478bd9Sstevel@tonic-gate remote->sin_family = AF_INET; 32037c478bd9Sstevel@tonic-gate remote->sin_addr.s_addr = INADDR_ANY; 32047c478bd9Sstevel@tonic-gate remote->sin_port = 0; 32057c478bd9Sstevel@tonic-gate 32067c478bd9Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 32077c478bd9Sstevel@tonic-gate return (error); 32087c478bd9Sstevel@tonic-gate 32097c478bd9Sstevel@tonic-gate /* 32107c478bd9Sstevel@tonic-gate * Abort IPv6 connections. 32117c478bd9Sstevel@tonic-gate */ 32127c478bd9Sstevel@tonic-gate bzero(&l, sizeof (*local6)); 32137c478bd9Sstevel@tonic-gate local6 = (struct sockaddr_in6 *)&l; 32147c478bd9Sstevel@tonic-gate local6->sin6_family = AF_INET6; 32157c478bd9Sstevel@tonic-gate local6->sin6_port = 0; 32167c478bd9Sstevel@tonic-gate local6->sin6_addr = in6addr_any; 32177c478bd9Sstevel@tonic-gate 32187c478bd9Sstevel@tonic-gate bzero(&r, sizeof (*remote6)); 32197c478bd9Sstevel@tonic-gate remote6 = (struct sockaddr_in6 *)&r; 32207c478bd9Sstevel@tonic-gate remote6->sin6_family = AF_INET6; 32217c478bd9Sstevel@tonic-gate remote6->sin6_port = 0; 32227c478bd9Sstevel@tonic-gate remote6->sin6_addr = in6addr_any; 32237c478bd9Sstevel@tonic-gate 32247c478bd9Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 32257c478bd9Sstevel@tonic-gate return (error); 32267c478bd9Sstevel@tonic-gate return (0); 32277c478bd9Sstevel@tonic-gate } 32287c478bd9Sstevel@tonic-gate 32297c478bd9Sstevel@tonic-gate static int 32306cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd) 3231ffbafc53Scomay { 3232ffbafc53Scomay int error = -1; 3233ffbafc53Scomay zone_dochandle_t handle; 3234ffbafc53Scomay char *privname = NULL; 3235ffbafc53Scomay 3236ffbafc53Scomay if ((handle = zonecfg_init_handle()) == NULL) { 3237ffbafc53Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3238ffbafc53Scomay return (-1); 3239ffbafc53Scomay } 3240ffbafc53Scomay if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 3241ffbafc53Scomay zerror(zlogp, B_FALSE, "invalid configuration"); 3242ffbafc53Scomay zonecfg_fini_handle(handle); 3243ffbafc53Scomay return (-1); 3244ffbafc53Scomay } 3245ffbafc53Scomay 32466cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd)) { 3247bf1d7e28Sdh155122 zone_iptype_t iptype; 3248bf1d7e28Sdh155122 const char *curr_iptype; 3249bf1d7e28Sdh155122 3250bf1d7e28Sdh155122 if (zonecfg_get_iptype(handle, &iptype) != Z_OK) { 3251bf1d7e28Sdh155122 zerror(zlogp, B_TRUE, "unable to determine ip-type"); 3252bf1d7e28Sdh155122 zonecfg_fini_handle(handle); 3253bf1d7e28Sdh155122 return (-1); 3254bf1d7e28Sdh155122 } 3255bf1d7e28Sdh155122 3256bf1d7e28Sdh155122 switch (iptype) { 3257bf1d7e28Sdh155122 case ZS_SHARED: 3258bf1d7e28Sdh155122 curr_iptype = "shared"; 3259bf1d7e28Sdh155122 break; 3260bf1d7e28Sdh155122 case ZS_EXCLUSIVE: 3261bf1d7e28Sdh155122 curr_iptype = "exclusive"; 3262bf1d7e28Sdh155122 break; 3263bf1d7e28Sdh155122 } 3264bf1d7e28Sdh155122 3265e767a340Sgjelinek if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) { 3266e767a340Sgjelinek zonecfg_fini_handle(handle); 32679acbbeafSnn35248 return (0); 3268e767a340Sgjelinek } 32699acbbeafSnn35248 zerror(zlogp, B_FALSE, 32709acbbeafSnn35248 "failed to determine the zone's default privilege set"); 32719acbbeafSnn35248 zonecfg_fini_handle(handle); 32729acbbeafSnn35248 return (-1); 32739acbbeafSnn35248 } 32749acbbeafSnn35248 3275ffbafc53Scomay switch (zonecfg_get_privset(handle, privs, &privname)) { 3276ffbafc53Scomay case Z_OK: 3277ffbafc53Scomay error = 0; 3278ffbafc53Scomay break; 3279ffbafc53Scomay case Z_PRIV_PROHIBITED: 3280ffbafc53Scomay zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted " 3281ffbafc53Scomay "within the zone's privilege set", privname); 3282ffbafc53Scomay break; 3283ffbafc53Scomay case Z_PRIV_REQUIRED: 3284ffbafc53Scomay zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing " 3285ffbafc53Scomay "from the zone's privilege set", privname); 3286ffbafc53Scomay break; 3287ffbafc53Scomay case Z_PRIV_UNKNOWN: 3288ffbafc53Scomay zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified " 3289ffbafc53Scomay "in the zone's privilege set", privname); 3290ffbafc53Scomay break; 3291ffbafc53Scomay default: 3292ffbafc53Scomay zerror(zlogp, B_FALSE, "failed to determine the zone's " 3293ffbafc53Scomay "privilege set"); 3294ffbafc53Scomay break; 3295ffbafc53Scomay } 3296ffbafc53Scomay 3297ffbafc53Scomay free(privname); 3298ffbafc53Scomay zonecfg_fini_handle(handle); 3299ffbafc53Scomay return (error); 3300ffbafc53Scomay } 3301ffbafc53Scomay 3302ffbafc53Scomay static int 33037c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep) 33047c478bd9Sstevel@tonic-gate { 33057c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL; 33067c478bd9Sstevel@tonic-gate char *nvl_packed = NULL; 33077c478bd9Sstevel@tonic-gate size_t nvl_size = 0; 33087c478bd9Sstevel@tonic-gate nvlist_t **nvlv = NULL; 33097c478bd9Sstevel@tonic-gate int rctlcount = 0; 33107c478bd9Sstevel@tonic-gate int error = -1; 33117c478bd9Sstevel@tonic-gate zone_dochandle_t handle; 33127c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab; 33137c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk = NULL; 3314ff19e029SMenno Lageman uint64_t maxlwps; 3315ff19e029SMenno Lageman uint64_t maxprocs; 33167c478bd9Sstevel@tonic-gate 33177c478bd9Sstevel@tonic-gate *bufp = NULL; 33187c478bd9Sstevel@tonic-gate *bufsizep = 0; 33197c478bd9Sstevel@tonic-gate 33207c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) { 33217c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle"); 33227c478bd9Sstevel@tonic-gate return (-1); 33237c478bd9Sstevel@tonic-gate } 33247c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 33257c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration"); 33267c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 33277c478bd9Sstevel@tonic-gate return (-1); 33287c478bd9Sstevel@tonic-gate } 33297c478bd9Sstevel@tonic-gate 33307c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 33317c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 33327c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc"); 33337c478bd9Sstevel@tonic-gate goto out; 33347c478bd9Sstevel@tonic-gate } 33357c478bd9Sstevel@tonic-gate 3336ff19e029SMenno Lageman /* 3337ff19e029SMenno Lageman * Allow the administrator to control both the maximum number of 3338ff19e029SMenno Lageman * process table slots and the maximum number of lwps with just the 3339ff19e029SMenno Lageman * max-processes property. If only the max-processes property is set, 3340ff19e029SMenno Lageman * we add a max-lwps property with a limit derived from max-processes. 3341ff19e029SMenno Lageman */ 3342ff19e029SMenno Lageman if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs) 3343ff19e029SMenno Lageman == Z_OK && 3344ff19e029SMenno Lageman zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps) 3345ff19e029SMenno Lageman == Z_NO_ENTRY) { 3346ff19e029SMenno Lageman if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS, 3347ff19e029SMenno Lageman maxprocs * LWPS_PER_PROCESS) != Z_OK) { 3348ff19e029SMenno Lageman zerror(zlogp, B_FALSE, "unable to set max-lwps alias"); 3349ff19e029SMenno Lageman goto out; 3350ff19e029SMenno Lageman } 3351ff19e029SMenno Lageman } 3352ff19e029SMenno Lageman 33537c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) { 33547c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent"); 33557c478bd9Sstevel@tonic-gate goto out; 33567c478bd9Sstevel@tonic-gate } 33577c478bd9Sstevel@tonic-gate 33587c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rctlblk_size())) == NULL) { 33597c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed"); 33607c478bd9Sstevel@tonic-gate goto out; 33617c478bd9Sstevel@tonic-gate } 33627c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 33637c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval; 33647c478bd9Sstevel@tonic-gate uint_t i, count; 33657c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name; 33667c478bd9Sstevel@tonic-gate 33677c478bd9Sstevel@tonic-gate /* zoneadm should have already warned about unknown rctls. */ 33687c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) { 33697c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 33707c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 33717c478bd9Sstevel@tonic-gate continue; 33727c478bd9Sstevel@tonic-gate } 33737c478bd9Sstevel@tonic-gate count = 0; 33747c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 33757c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) { 33767c478bd9Sstevel@tonic-gate count++; 33777c478bd9Sstevel@tonic-gate } 33787c478bd9Sstevel@tonic-gate if (count == 0) { /* ignore */ 33797c478bd9Sstevel@tonic-gate continue; /* Nothing to free */ 33807c478bd9Sstevel@tonic-gate } 33817c478bd9Sstevel@tonic-gate if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL) 33827c478bd9Sstevel@tonic-gate goto out; 33837c478bd9Sstevel@tonic-gate i = 0; 33847c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 33857c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next, i++) { 33867c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) { 33877c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", 33887c478bd9Sstevel@tonic-gate "nvlist_alloc"); 33897c478bd9Sstevel@tonic-gate goto out; 33907c478bd9Sstevel@tonic-gate } 33917c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk) 33927c478bd9Sstevel@tonic-gate != Z_OK) { 33937c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid rctl value: " 33947c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s)", 33957c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 33967c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 33977c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action); 33987c478bd9Sstevel@tonic-gate goto out; 33997c478bd9Sstevel@tonic-gate } 34007c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) { 34017c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 34027c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s) is not a " 34037c478bd9Sstevel@tonic-gate "valid value for rctl '%s'", 34047c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv, 34057c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit, 34067c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action, 34077c478bd9Sstevel@tonic-gate name); 34087c478bd9Sstevel@tonic-gate goto out; 34097c478bd9Sstevel@tonic-gate } 34107c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "privilege", 34117c478bd9Sstevel@tonic-gate rctlblk_get_privilege(rctlblk)) != 0) { 34127c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 34137c478bd9Sstevel@tonic-gate "nvlist_add_uint64"); 34147c478bd9Sstevel@tonic-gate goto out; 34157c478bd9Sstevel@tonic-gate } 34167c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "limit", 34177c478bd9Sstevel@tonic-gate rctlblk_get_value(rctlblk)) != 0) { 34187c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 34197c478bd9Sstevel@tonic-gate "nvlist_add_uint64"); 34207c478bd9Sstevel@tonic-gate goto out; 34217c478bd9Sstevel@tonic-gate } 34227c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "action", 34237c478bd9Sstevel@tonic-gate (uint_t)rctlblk_get_local_action(rctlblk, NULL)) 34247c478bd9Sstevel@tonic-gate != 0) { 34257c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 34267c478bd9Sstevel@tonic-gate "nvlist_add_uint64"); 34277c478bd9Sstevel@tonic-gate goto out; 34287c478bd9Sstevel@tonic-gate } 34297c478bd9Sstevel@tonic-gate } 34307c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 34317c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL; 34327c478bd9Sstevel@tonic-gate if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count) 34337c478bd9Sstevel@tonic-gate != 0) { 34347c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", 34357c478bd9Sstevel@tonic-gate "nvlist_add_nvlist_array"); 34367c478bd9Sstevel@tonic-gate goto out; 34377c478bd9Sstevel@tonic-gate } 34387c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++) 34397c478bd9Sstevel@tonic-gate nvlist_free(nvlv[i]); 34407c478bd9Sstevel@tonic-gate free(nvlv); 34417c478bd9Sstevel@tonic-gate nvlv = NULL; 34427c478bd9Sstevel@tonic-gate rctlcount++; 34437c478bd9Sstevel@tonic-gate } 34447c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle); 34457c478bd9Sstevel@tonic-gate 34467c478bd9Sstevel@tonic-gate if (rctlcount == 0) { 34477c478bd9Sstevel@tonic-gate error = 0; 34487c478bd9Sstevel@tonic-gate goto out; 34497c478bd9Sstevel@tonic-gate } 34507c478bd9Sstevel@tonic-gate if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0) 34517c478bd9Sstevel@tonic-gate != 0) { 34527c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack"); 34537c478bd9Sstevel@tonic-gate goto out; 34547c478bd9Sstevel@tonic-gate } 34557c478bd9Sstevel@tonic-gate 34567c478bd9Sstevel@tonic-gate error = 0; 34577c478bd9Sstevel@tonic-gate *bufp = nvl_packed; 34587c478bd9Sstevel@tonic-gate *bufsizep = nvl_size; 34597c478bd9Sstevel@tonic-gate 34607c478bd9Sstevel@tonic-gate out: 34617c478bd9Sstevel@tonic-gate free(rctlblk); 34627c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 34637c478bd9Sstevel@tonic-gate if (error && nvl_packed != NULL) 34647c478bd9Sstevel@tonic-gate free(nvl_packed); 34657c478bd9Sstevel@tonic-gate if (nvl != NULL) 34667c478bd9Sstevel@tonic-gate nvlist_free(nvl); 34677c478bd9Sstevel@tonic-gate if (nvlv != NULL) 34687c478bd9Sstevel@tonic-gate free(nvlv); 34697c478bd9Sstevel@tonic-gate if (handle != NULL) 34707c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle); 34717c478bd9Sstevel@tonic-gate return (error); 34727c478bd9Sstevel@tonic-gate } 34737c478bd9Sstevel@tonic-gate 34747c478bd9Sstevel@tonic-gate static int 3475c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr) 3476c5cd6260S { 3477c5cd6260S char cmdbuf[2 * MAXPATHLEN]; 3478c5cd6260S 3479c5cd6260S if (query_hook[0] == '\0') 3480c5cd6260S return (0); 3481c5cd6260S 3482c5cd6260S if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook) 3483c5cd6260S > sizeof (cmdbuf)) 3484c5cd6260S return (-1); 3485c5cd6260S 3486c5cd6260S if (do_subproc(zlogp, cmdbuf, retstr) != 0) 3487c5cd6260S return (-1); 3488c5cd6260S 3489c5cd6260S return (0); 3490c5cd6260S } 3491c5cd6260S 3492c5cd6260S static int 3493fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep) 3494fa9e4066Sahrens { 3495fa9e4066Sahrens zone_dochandle_t handle; 3496fa9e4066Sahrens struct zone_dstab dstab; 3497fa9e4066Sahrens size_t total, offset, len; 3498fa9e4066Sahrens int error = -1; 3499e5a17f6aSgjelinek char *str = NULL; 3500c5cd6260S char *implicit_datasets = NULL; 3501c5cd6260S int implicit_len = 0; 3502fa9e4066Sahrens 3503fa9e4066Sahrens *bufp = NULL; 3504fa9e4066Sahrens *bufsizep = 0; 3505fa9e4066Sahrens 3506fa9e4066Sahrens if ((handle = zonecfg_init_handle()) == NULL) { 3507fa9e4066Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3508fa9e4066Sahrens return (-1); 3509fa9e4066Sahrens } 3510fa9e4066Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 3511fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 3512fa9e4066Sahrens zonecfg_fini_handle(handle); 3513fa9e4066Sahrens return (-1); 3514fa9e4066Sahrens } 3515fa9e4066Sahrens 3516c5cd6260S if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) { 3517c5cd6260S zerror(zlogp, B_FALSE, "getting implicit datasets failed"); 3518c5cd6260S goto out; 3519c5cd6260S } 3520c5cd6260S 3521fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 3522fa9e4066Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 3523fa9e4066Sahrens goto out; 3524fa9e4066Sahrens } 3525fa9e4066Sahrens 3526fa9e4066Sahrens total = 0; 3527fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) 3528fa9e4066Sahrens total += strlen(dstab.zone_dataset_name) + 1; 3529fa9e4066Sahrens (void) zonecfg_enddsent(handle); 3530fa9e4066Sahrens 3531c5cd6260S if (implicit_datasets != NULL) 3532c5cd6260S implicit_len = strlen(implicit_datasets); 3533c5cd6260S if (implicit_len > 0) 3534c5cd6260S total += implicit_len + 1; 3535c5cd6260S 3536fa9e4066Sahrens if (total == 0) { 3537fa9e4066Sahrens error = 0; 3538fa9e4066Sahrens goto out; 3539fa9e4066Sahrens } 3540fa9e4066Sahrens 3541fa9e4066Sahrens if ((str = malloc(total)) == NULL) { 3542fa9e4066Sahrens zerror(zlogp, B_TRUE, "memory allocation failed"); 3543fa9e4066Sahrens goto out; 3544fa9e4066Sahrens } 3545fa9e4066Sahrens 3546fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 3547fa9e4066Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 3548fa9e4066Sahrens goto out; 3549fa9e4066Sahrens } 3550fa9e4066Sahrens offset = 0; 3551fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 3552fa9e4066Sahrens len = strlen(dstab.zone_dataset_name); 3553fa9e4066Sahrens (void) strlcpy(str + offset, dstab.zone_dataset_name, 3554e5a17f6aSgjelinek total - offset); 3555fa9e4066Sahrens offset += len; 3556e5a17f6aSgjelinek if (offset < total - 1) 3557fa9e4066Sahrens str[offset++] = ','; 3558fa9e4066Sahrens } 3559fa9e4066Sahrens (void) zonecfg_enddsent(handle); 3560fa9e4066Sahrens 3561c5cd6260S if (implicit_len > 0) 3562c5cd6260S (void) strlcpy(str + offset, implicit_datasets, total - offset); 3563c5cd6260S 3564fa9e4066Sahrens error = 0; 3565fa9e4066Sahrens *bufp = str; 3566fa9e4066Sahrens *bufsizep = total; 3567fa9e4066Sahrens 3568fa9e4066Sahrens out: 3569fa9e4066Sahrens if (error != 0 && str != NULL) 3570fa9e4066Sahrens free(str); 3571fa9e4066Sahrens if (handle != NULL) 3572fa9e4066Sahrens zonecfg_fini_handle(handle); 3573c5cd6260S if (implicit_datasets != NULL) 3574c5cd6260S free(implicit_datasets); 3575fa9e4066Sahrens 3576fa9e4066Sahrens return (error); 3577fa9e4066Sahrens } 3578fa9e4066Sahrens 3579fa9e4066Sahrens static int 3580fa9e4066Sahrens validate_datasets(zlog_t *zlogp) 3581fa9e4066Sahrens { 3582fa9e4066Sahrens zone_dochandle_t handle; 3583fa9e4066Sahrens struct zone_dstab dstab; 3584fa9e4066Sahrens zfs_handle_t *zhp; 358599653d4eSeschrock libzfs_handle_t *hdl; 3586fa9e4066Sahrens 3587fa9e4066Sahrens if ((handle = zonecfg_init_handle()) == NULL) { 3588fa9e4066Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3589fa9e4066Sahrens return (-1); 3590fa9e4066Sahrens } 3591fa9e4066Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 3592fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 3593fa9e4066Sahrens zonecfg_fini_handle(handle); 3594fa9e4066Sahrens return (-1); 3595fa9e4066Sahrens } 3596fa9e4066Sahrens 3597fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) { 3598fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration"); 3599fa9e4066Sahrens zonecfg_fini_handle(handle); 3600fa9e4066Sahrens return (-1); 3601fa9e4066Sahrens } 3602fa9e4066Sahrens 360399653d4eSeschrock if ((hdl = libzfs_init()) == NULL) { 360499653d4eSeschrock zerror(zlogp, B_FALSE, "opening ZFS library"); 360599653d4eSeschrock zonecfg_fini_handle(handle); 360699653d4eSeschrock return (-1); 360799653d4eSeschrock } 3608fa9e4066Sahrens 3609fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 3610fa9e4066Sahrens 361199653d4eSeschrock if ((zhp = zfs_open(hdl, dstab.zone_dataset_name, 3612fa9e4066Sahrens ZFS_TYPE_FILESYSTEM)) == NULL) { 3613fa9e4066Sahrens zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'", 3614fa9e4066Sahrens dstab.zone_dataset_name); 3615fa9e4066Sahrens zonecfg_fini_handle(handle); 361699653d4eSeschrock libzfs_fini(hdl); 3617fa9e4066Sahrens return (-1); 3618fa9e4066Sahrens } 3619fa9e4066Sahrens 3620fa9e4066Sahrens /* 3621fa9e4066Sahrens * Automatically set the 'zoned' property. We check the value 3622fa9e4066Sahrens * first because we'll get EPERM if it is already set. 3623fa9e4066Sahrens */ 3624fa9e4066Sahrens if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 3625e9dbad6fSeschrock zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED), 3626e9dbad6fSeschrock "on") != 0) { 3627fa9e4066Sahrens zerror(zlogp, B_FALSE, "cannot set 'zoned' " 3628fa9e4066Sahrens "property for ZFS dataset '%s'\n", 3629fa9e4066Sahrens dstab.zone_dataset_name); 3630fa9e4066Sahrens zonecfg_fini_handle(handle); 3631fa9e4066Sahrens zfs_close(zhp); 363299653d4eSeschrock libzfs_fini(hdl); 3633fa9e4066Sahrens return (-1); 3634fa9e4066Sahrens } 3635fa9e4066Sahrens 3636fa9e4066Sahrens zfs_close(zhp); 3637fa9e4066Sahrens } 3638fa9e4066Sahrens (void) zonecfg_enddsent(handle); 3639fa9e4066Sahrens 3640fa9e4066Sahrens zonecfg_fini_handle(handle); 364199653d4eSeschrock libzfs_fini(hdl); 3642fa9e4066Sahrens 3643fa9e4066Sahrens return (0); 3644fa9e4066Sahrens } 3645fa9e4066Sahrens 364645916cd2Sjpk /* 36474201a95eSRic Aleshire * Return true if the path is its own zfs file system. We determine this 36484201a95eSRic Aleshire * by stat-ing the path to see if it is zfs and stat-ing the parent to see 36494201a95eSRic Aleshire * if it is a different fs. 36504201a95eSRic Aleshire */ 36514201a95eSRic Aleshire boolean_t 36524201a95eSRic Aleshire is_zonepath_zfs(char *zonepath) 36534201a95eSRic Aleshire { 36544201a95eSRic Aleshire int res; 36554201a95eSRic Aleshire char *path; 36564201a95eSRic Aleshire char *parent; 36574201a95eSRic Aleshire struct statvfs64 buf1, buf2; 36584201a95eSRic Aleshire 36594201a95eSRic Aleshire if (statvfs64(zonepath, &buf1) != 0) 36604201a95eSRic Aleshire return (B_FALSE); 36614201a95eSRic Aleshire 36624201a95eSRic Aleshire if (strcmp(buf1.f_basetype, "zfs") != 0) 36634201a95eSRic Aleshire return (B_FALSE); 36644201a95eSRic Aleshire 36654201a95eSRic Aleshire if ((path = strdup(zonepath)) == NULL) 36664201a95eSRic Aleshire return (B_FALSE); 36674201a95eSRic Aleshire 36684201a95eSRic Aleshire parent = dirname(path); 36694201a95eSRic Aleshire res = statvfs64(parent, &buf2); 36704201a95eSRic Aleshire free(path); 36714201a95eSRic Aleshire 36724201a95eSRic Aleshire if (res != 0) 36734201a95eSRic Aleshire return (B_FALSE); 36744201a95eSRic Aleshire 36754201a95eSRic Aleshire if (buf1.f_fsid == buf2.f_fsid) 36764201a95eSRic Aleshire return (B_FALSE); 36774201a95eSRic Aleshire 36784201a95eSRic Aleshire return (B_TRUE); 36794201a95eSRic Aleshire } 36804201a95eSRic Aleshire 36814201a95eSRic Aleshire /* 36824201a95eSRic Aleshire * Verify the MAC label in the root dataset for the zone. 36834201a95eSRic Aleshire * If the label exists, it must match the label configured for the zone. 36844201a95eSRic Aleshire * Otherwise if there's no label on the dataset, create one here. 36854201a95eSRic Aleshire */ 36864201a95eSRic Aleshire 36874201a95eSRic Aleshire static int 36884201a95eSRic Aleshire validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl) 36894201a95eSRic Aleshire { 36904201a95eSRic Aleshire int error = -1; 36914201a95eSRic Aleshire zfs_handle_t *zhp; 36924201a95eSRic Aleshire libzfs_handle_t *hdl; 36934201a95eSRic Aleshire m_label_t ds_sl; 36944201a95eSRic Aleshire char zonepath[MAXPATHLEN]; 36954201a95eSRic Aleshire char ds_hexsl[MAXNAMELEN]; 36964201a95eSRic Aleshire 36974201a95eSRic Aleshire if (!is_system_labeled()) 36984201a95eSRic Aleshire return (0); 36994201a95eSRic Aleshire 37004201a95eSRic Aleshire if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 37014201a95eSRic Aleshire zerror(zlogp, B_TRUE, "unable to determine zone path"); 37024201a95eSRic Aleshire return (-1); 37034201a95eSRic Aleshire } 37044201a95eSRic Aleshire 37054201a95eSRic Aleshire if (!is_zonepath_zfs(zonepath)) 37064201a95eSRic Aleshire return (0); 37074201a95eSRic Aleshire 37084201a95eSRic Aleshire if ((hdl = libzfs_init()) == NULL) { 37094201a95eSRic Aleshire zerror(zlogp, B_FALSE, "opening ZFS library"); 37104201a95eSRic Aleshire return (-1); 37114201a95eSRic Aleshire } 37124201a95eSRic Aleshire 37134201a95eSRic Aleshire if ((zhp = zfs_path_to_zhandle(hdl, rootpath, 37144201a95eSRic Aleshire ZFS_TYPE_FILESYSTEM)) == NULL) { 37154201a95eSRic Aleshire zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'", 37164201a95eSRic Aleshire rootpath); 37174201a95eSRic Aleshire libzfs_fini(hdl); 37184201a95eSRic Aleshire return (-1); 37194201a95eSRic Aleshire } 37204201a95eSRic Aleshire 37214201a95eSRic Aleshire /* Get the mlslabel property if it exists. */ 37224201a95eSRic Aleshire if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN, 37234201a95eSRic Aleshire NULL, NULL, 0, B_TRUE) != 0) || 37244201a95eSRic Aleshire (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) { 37254201a95eSRic Aleshire char *str2 = NULL; 37264201a95eSRic Aleshire 37274201a95eSRic Aleshire /* 37284201a95eSRic Aleshire * No label on the dataset (or default only); create one. 37294201a95eSRic Aleshire * (Only do this automatic labeling for the labeled brand.) 37304201a95eSRic Aleshire */ 37314201a95eSRic Aleshire if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) { 37324201a95eSRic Aleshire error = 0; 37334201a95eSRic Aleshire goto out; 37344201a95eSRic Aleshire } 37354201a95eSRic Aleshire 37364201a95eSRic Aleshire error = l_to_str_internal(zone_sl, &str2); 37374201a95eSRic Aleshire if (error) 37384201a95eSRic Aleshire goto out; 37394201a95eSRic Aleshire if (str2 == NULL) { 37404201a95eSRic Aleshire error = -1; 37414201a95eSRic Aleshire goto out; 37424201a95eSRic Aleshire } 37434201a95eSRic Aleshire if ((error = zfs_prop_set(zhp, 37444201a95eSRic Aleshire zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) { 37454201a95eSRic Aleshire zerror(zlogp, B_FALSE, "cannot set 'mlslabel' " 37464201a95eSRic Aleshire "property for root dataset at '%s'\n", rootpath); 37474201a95eSRic Aleshire } 37484201a95eSRic Aleshire free(str2); 37494201a95eSRic Aleshire goto out; 37504201a95eSRic Aleshire } 37514201a95eSRic Aleshire 37524201a95eSRic Aleshire /* Convert the retrieved dataset label to binary form. */ 37534201a95eSRic Aleshire error = hexstr_to_label(ds_hexsl, &ds_sl); 37544201a95eSRic Aleshire if (error) { 37554201a95eSRic Aleshire zerror(zlogp, B_FALSE, "invalid 'mlslabel' " 37564201a95eSRic Aleshire "property on root dataset at '%s'\n", rootpath); 37574201a95eSRic Aleshire goto out; /* exit with error */ 37584201a95eSRic Aleshire } 37594201a95eSRic Aleshire 37604201a95eSRic Aleshire /* 37614201a95eSRic Aleshire * Perform a MAC check by comparing the zone label with the 37624201a95eSRic Aleshire * dataset label. 37634201a95eSRic Aleshire */ 37644201a95eSRic Aleshire error = (!blequal(zone_sl, &ds_sl)); 37654201a95eSRic Aleshire if (error) 37664201a95eSRic Aleshire zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label"); 37674201a95eSRic Aleshire out: 37684201a95eSRic Aleshire zfs_close(zhp); 37694201a95eSRic Aleshire libzfs_fini(hdl); 37704201a95eSRic Aleshire 37714201a95eSRic Aleshire return (error); 37724201a95eSRic Aleshire } 37734201a95eSRic Aleshire 37744201a95eSRic Aleshire /* 377545916cd2Sjpk * Mount lower level home directories into/from current zone 377645916cd2Sjpk * Share exported directories specified in dfstab for zone 377745916cd2Sjpk */ 377845916cd2Sjpk static int 377945916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath) 378045916cd2Sjpk { 378145916cd2Sjpk zoneid_t *zids = NULL; 378245916cd2Sjpk priv_set_t *zid_privs; 378345916cd2Sjpk const priv_impl_info_t *ip = NULL; 378445916cd2Sjpk uint_t nzents_saved; 378545916cd2Sjpk uint_t nzents; 378645916cd2Sjpk int i; 378745916cd2Sjpk char readonly[] = "ro"; 378845916cd2Sjpk struct zone_fstab lower_fstab; 378945916cd2Sjpk char *argv[4]; 379045916cd2Sjpk 379145916cd2Sjpk if (!is_system_labeled()) 379245916cd2Sjpk return (0); 379345916cd2Sjpk 379445916cd2Sjpk if (zid_label == NULL) { 379545916cd2Sjpk zid_label = m_label_alloc(MAC_LABEL); 379645916cd2Sjpk if (zid_label == NULL) 379745916cd2Sjpk return (-1); 379845916cd2Sjpk } 379945916cd2Sjpk 380045916cd2Sjpk /* Make sure our zone has an /export/home dir */ 380145916cd2Sjpk (void) make_one_dir(zlogp, rootpath, "/export/home", 380227e6fb21Sdp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP); 380345916cd2Sjpk 380445916cd2Sjpk lower_fstab.zone_fs_raw[0] = '\0'; 380545916cd2Sjpk (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS, 380645916cd2Sjpk sizeof (lower_fstab.zone_fs_type)); 380745916cd2Sjpk lower_fstab.zone_fs_options = NULL; 380845916cd2Sjpk (void) zonecfg_add_fs_option(&lower_fstab, readonly); 380945916cd2Sjpk 381045916cd2Sjpk /* 381145916cd2Sjpk * Get the list of zones from the kernel 381245916cd2Sjpk */ 381345916cd2Sjpk if (zone_list(NULL, &nzents) != 0) { 381445916cd2Sjpk zerror(zlogp, B_TRUE, "unable to list zones"); 381545916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 381645916cd2Sjpk return (-1); 381745916cd2Sjpk } 381845916cd2Sjpk again: 381945916cd2Sjpk if (nzents == 0) { 382045916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 382145916cd2Sjpk return (-1); 382245916cd2Sjpk } 382345916cd2Sjpk 382445916cd2Sjpk zids = malloc(nzents * sizeof (zoneid_t)); 382545916cd2Sjpk if (zids == NULL) { 38263f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 382745916cd2Sjpk return (-1); 382845916cd2Sjpk } 382945916cd2Sjpk nzents_saved = nzents; 383045916cd2Sjpk 383145916cd2Sjpk if (zone_list(zids, &nzents) != 0) { 383245916cd2Sjpk zerror(zlogp, B_TRUE, "unable to list zones"); 383345916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 383445916cd2Sjpk free(zids); 383545916cd2Sjpk return (-1); 383645916cd2Sjpk } 383745916cd2Sjpk if (nzents != nzents_saved) { 383845916cd2Sjpk /* list changed, try again */ 383945916cd2Sjpk free(zids); 384045916cd2Sjpk goto again; 384145916cd2Sjpk } 384245916cd2Sjpk 384345916cd2Sjpk ip = getprivimplinfo(); 384445916cd2Sjpk if ((zid_privs = priv_allocset()) == NULL) { 384545916cd2Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 384645916cd2Sjpk zonecfg_free_fs_option_list( 384745916cd2Sjpk lower_fstab.zone_fs_options); 384845916cd2Sjpk free(zids); 384945916cd2Sjpk return (-1); 385045916cd2Sjpk } 385145916cd2Sjpk 385245916cd2Sjpk for (i = 0; i < nzents; i++) { 385345916cd2Sjpk char zid_name[ZONENAME_MAX]; 385445916cd2Sjpk zone_state_t zid_state; 385545916cd2Sjpk char zid_rpath[MAXPATHLEN]; 385645916cd2Sjpk struct stat stat_buf; 385745916cd2Sjpk 385845916cd2Sjpk if (zids[i] == GLOBAL_ZONEID) 385945916cd2Sjpk continue; 386045916cd2Sjpk 386145916cd2Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 386245916cd2Sjpk continue; 386345916cd2Sjpk 386445916cd2Sjpk /* 386545916cd2Sjpk * Do special setup for the zone we are booting 386645916cd2Sjpk */ 386745916cd2Sjpk if (strcmp(zid_name, zone_name) == 0) { 386845916cd2Sjpk struct zone_fstab autofs_fstab; 386945916cd2Sjpk char map_path[MAXPATHLEN]; 387045916cd2Sjpk int fd; 387145916cd2Sjpk 387245916cd2Sjpk /* 387345916cd2Sjpk * Create auto_home_<zone> map for this zone 38749acbbeafSnn35248 * in the global zone. The non-global zone entry 387545916cd2Sjpk * will be created by automount when the zone 387645916cd2Sjpk * is booted. 387745916cd2Sjpk */ 387845916cd2Sjpk 387945916cd2Sjpk (void) snprintf(autofs_fstab.zone_fs_special, 388045916cd2Sjpk MAXPATHLEN, "auto_home_%s", zid_name); 388145916cd2Sjpk 388245916cd2Sjpk (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN, 388345916cd2Sjpk "/zone/%s/home", zid_name); 388445916cd2Sjpk 388545916cd2Sjpk (void) snprintf(map_path, sizeof (map_path), 388645916cd2Sjpk "/etc/%s", autofs_fstab.zone_fs_special); 388745916cd2Sjpk /* 388845916cd2Sjpk * If the map file doesn't exist create a template 388945916cd2Sjpk */ 389045916cd2Sjpk if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL, 389145916cd2Sjpk S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) { 389245916cd2Sjpk int len; 389345916cd2Sjpk char map_rec[MAXPATHLEN]; 389445916cd2Sjpk 389545916cd2Sjpk len = snprintf(map_rec, sizeof (map_rec), 389645916cd2Sjpk "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n", 389745916cd2Sjpk autofs_fstab.zone_fs_special, rootpath); 389845916cd2Sjpk (void) write(fd, map_rec, len); 389945916cd2Sjpk (void) close(fd); 390045916cd2Sjpk } 390145916cd2Sjpk 390245916cd2Sjpk /* 390345916cd2Sjpk * Mount auto_home_<zone> in the global zone if absent. 390445916cd2Sjpk * If it's already of type autofs, then 390545916cd2Sjpk * don't mount it again. 390645916cd2Sjpk */ 390745916cd2Sjpk if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) || 390845916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) { 390945916cd2Sjpk char optstr[] = "indirect,ignore,nobrowse"; 391045916cd2Sjpk 391145916cd2Sjpk (void) make_one_dir(zlogp, "", 391227e6fb21Sdp autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE, 391327e6fb21Sdp DEFAULT_DIR_USER, DEFAULT_DIR_GROUP); 391445916cd2Sjpk 391545916cd2Sjpk /* 391645916cd2Sjpk * Mount will fail if automounter has already 391745916cd2Sjpk * processed the auto_home_<zonename> map 391845916cd2Sjpk */ 391945916cd2Sjpk (void) domount(zlogp, MNTTYPE_AUTOFS, optstr, 392045916cd2Sjpk autofs_fstab.zone_fs_special, 392145916cd2Sjpk autofs_fstab.zone_fs_dir); 392245916cd2Sjpk } 392345916cd2Sjpk continue; 392445916cd2Sjpk } 392545916cd2Sjpk 392645916cd2Sjpk 392745916cd2Sjpk if (zone_get_state(zid_name, &zid_state) != Z_OK || 392848451833Scarlsonj (zid_state != ZONE_STATE_READY && 392948451833Scarlsonj zid_state != ZONE_STATE_RUNNING)) 393045916cd2Sjpk /* Skip over zones without mounted filesystems */ 393145916cd2Sjpk continue; 393245916cd2Sjpk 393345916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 393445916cd2Sjpk sizeof (m_label_t)) < 0) 393545916cd2Sjpk /* Skip over zones with unspecified label */ 393645916cd2Sjpk continue; 393745916cd2Sjpk 393845916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 393945916cd2Sjpk sizeof (zid_rpath)) == -1) 394045916cd2Sjpk /* Skip over zones with bad path */ 394145916cd2Sjpk continue; 394245916cd2Sjpk 394345916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs, 394445916cd2Sjpk sizeof (priv_chunk_t) * ip->priv_setsize) == -1) 394545916cd2Sjpk /* Skip over zones with bad privs */ 394645916cd2Sjpk continue; 394745916cd2Sjpk 394845916cd2Sjpk /* 394945916cd2Sjpk * Reading down is valid according to our label model 395045916cd2Sjpk * but some customers want to disable it because it 395145916cd2Sjpk * allows execute down and other possible attacks. 395245916cd2Sjpk * Therefore, we restrict this feature to zones that 395345916cd2Sjpk * have the NET_MAC_AWARE privilege which is required 395445916cd2Sjpk * for NFS read-down semantics. 395545916cd2Sjpk */ 395645916cd2Sjpk if ((bldominates(zlabel, zid_label)) && 395745916cd2Sjpk (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) { 395845916cd2Sjpk /* 395945916cd2Sjpk * Our zone dominates this one. 396045916cd2Sjpk * Create a lofs mount from lower zone's /export/home 396145916cd2Sjpk */ 396245916cd2Sjpk (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 396345916cd2Sjpk "%s/zone/%s/export/home", rootpath, zid_name); 396445916cd2Sjpk 396545916cd2Sjpk /* 396645916cd2Sjpk * If the target is already an LOFS mount 396745916cd2Sjpk * then don't do it again. 396845916cd2Sjpk */ 396945916cd2Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 397045916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 397145916cd2Sjpk 397245916cd2Sjpk if (snprintf(lower_fstab.zone_fs_special, 397345916cd2Sjpk MAXPATHLEN, "%s/export", 397445916cd2Sjpk zid_rpath) > MAXPATHLEN) 397545916cd2Sjpk continue; 397645916cd2Sjpk 397745916cd2Sjpk /* 397845916cd2Sjpk * Make sure the lower-level home exists 397945916cd2Sjpk */ 398045916cd2Sjpk if (make_one_dir(zlogp, 398127e6fb21Sdp lower_fstab.zone_fs_special, "/home", 398227e6fb21Sdp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 398327e6fb21Sdp DEFAULT_DIR_GROUP) != 0) 398445916cd2Sjpk continue; 398545916cd2Sjpk 398645916cd2Sjpk (void) strlcat(lower_fstab.zone_fs_special, 398745916cd2Sjpk "/home", MAXPATHLEN); 398845916cd2Sjpk 398945916cd2Sjpk /* 399045916cd2Sjpk * Mount can fail because the lower-level 399145916cd2Sjpk * zone may have already done a mount up. 399245916cd2Sjpk */ 39930679f794S (void) mount_one(zlogp, &lower_fstab, "", 39940679f794S Z_MNT_BOOT); 399545916cd2Sjpk } 399645916cd2Sjpk } else if ((bldominates(zid_label, zlabel)) && 399745916cd2Sjpk (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) { 399845916cd2Sjpk /* 399945916cd2Sjpk * This zone dominates our zone. 400045916cd2Sjpk * Create a lofs mount from our zone's /export/home 400145916cd2Sjpk */ 400245916cd2Sjpk if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 400345916cd2Sjpk "%s/zone/%s/export/home", zid_rpath, 400445916cd2Sjpk zone_name) > MAXPATHLEN) 400545916cd2Sjpk continue; 400645916cd2Sjpk 400745916cd2Sjpk /* 400845916cd2Sjpk * If the target is already an LOFS mount 400945916cd2Sjpk * then don't do it again. 401045916cd2Sjpk */ 401145916cd2Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 401245916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 401345916cd2Sjpk 401445916cd2Sjpk (void) snprintf(lower_fstab.zone_fs_special, 401545916cd2Sjpk MAXPATHLEN, "%s/export/home", rootpath); 401645916cd2Sjpk 401745916cd2Sjpk /* 401845916cd2Sjpk * Mount can fail because the higher-level 401945916cd2Sjpk * zone may have already done a mount down. 402045916cd2Sjpk */ 40210679f794S (void) mount_one(zlogp, &lower_fstab, "", 40220679f794S Z_MNT_BOOT); 402345916cd2Sjpk } 402445916cd2Sjpk } 402545916cd2Sjpk } 402645916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 402745916cd2Sjpk priv_freeset(zid_privs); 402845916cd2Sjpk free(zids); 402945916cd2Sjpk 403045916cd2Sjpk /* 403145916cd2Sjpk * Now share any exported directories from this zone. 403245916cd2Sjpk * Each zone can have its own dfstab. 403345916cd2Sjpk */ 403445916cd2Sjpk 403545916cd2Sjpk argv[0] = "zoneshare"; 403645916cd2Sjpk argv[1] = "-z"; 403745916cd2Sjpk argv[2] = zone_name; 403845916cd2Sjpk argv[3] = NULL; 403945916cd2Sjpk 404045916cd2Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv); 404145916cd2Sjpk /* Don't check for errors since they don't affect the zone */ 404245916cd2Sjpk 404345916cd2Sjpk return (0); 404445916cd2Sjpk } 404545916cd2Sjpk 404645916cd2Sjpk /* 404745916cd2Sjpk * Unmount lofs mounts from higher level zones 404845916cd2Sjpk * Unshare nfs exported directories 404945916cd2Sjpk */ 405045916cd2Sjpk static void 405145916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name) 405245916cd2Sjpk { 405345916cd2Sjpk zoneid_t *zids = NULL; 405445916cd2Sjpk uint_t nzents_saved; 405545916cd2Sjpk uint_t nzents; 405645916cd2Sjpk int i; 405745916cd2Sjpk char *argv[4]; 405845916cd2Sjpk char path[MAXPATHLEN]; 405945916cd2Sjpk 406045916cd2Sjpk if (!is_system_labeled()) 406145916cd2Sjpk return; 406245916cd2Sjpk 406345916cd2Sjpk /* 406445916cd2Sjpk * Get the list of zones from the kernel 406545916cd2Sjpk */ 406645916cd2Sjpk if (zone_list(NULL, &nzents) != 0) { 406745916cd2Sjpk return; 406845916cd2Sjpk } 406945916cd2Sjpk 407045916cd2Sjpk if (zid_label == NULL) { 407145916cd2Sjpk zid_label = m_label_alloc(MAC_LABEL); 407245916cd2Sjpk if (zid_label == NULL) 407345916cd2Sjpk return; 407445916cd2Sjpk } 407545916cd2Sjpk 407645916cd2Sjpk again: 407745916cd2Sjpk if (nzents == 0) 407845916cd2Sjpk return; 407945916cd2Sjpk 408045916cd2Sjpk zids = malloc(nzents * sizeof (zoneid_t)); 408145916cd2Sjpk if (zids == NULL) { 40823f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 408345916cd2Sjpk return; 408445916cd2Sjpk } 408545916cd2Sjpk nzents_saved = nzents; 408645916cd2Sjpk 408745916cd2Sjpk if (zone_list(zids, &nzents) != 0) { 408845916cd2Sjpk free(zids); 408945916cd2Sjpk return; 409045916cd2Sjpk } 409145916cd2Sjpk if (nzents != nzents_saved) { 409245916cd2Sjpk /* list changed, try again */ 409345916cd2Sjpk free(zids); 409445916cd2Sjpk goto again; 409545916cd2Sjpk } 409645916cd2Sjpk 409745916cd2Sjpk for (i = 0; i < nzents; i++) { 409845916cd2Sjpk char zid_name[ZONENAME_MAX]; 409945916cd2Sjpk zone_state_t zid_state; 410045916cd2Sjpk char zid_rpath[MAXPATHLEN]; 410145916cd2Sjpk 410245916cd2Sjpk if (zids[i] == GLOBAL_ZONEID) 410345916cd2Sjpk continue; 410445916cd2Sjpk 410545916cd2Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 410645916cd2Sjpk continue; 410745916cd2Sjpk 410845916cd2Sjpk /* 410945916cd2Sjpk * Skip the zone we are halting 411045916cd2Sjpk */ 411145916cd2Sjpk if (strcmp(zid_name, zone_name) == 0) 411245916cd2Sjpk continue; 411345916cd2Sjpk 411445916cd2Sjpk if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state, 411545916cd2Sjpk sizeof (zid_state)) < 0) || 411645916cd2Sjpk (zid_state < ZONE_IS_READY)) 411745916cd2Sjpk /* Skip over zones without mounted filesystems */ 411845916cd2Sjpk continue; 411945916cd2Sjpk 412045916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 412145916cd2Sjpk sizeof (m_label_t)) < 0) 412245916cd2Sjpk /* Skip over zones with unspecified label */ 412345916cd2Sjpk continue; 412445916cd2Sjpk 412545916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 412645916cd2Sjpk sizeof (zid_rpath)) == -1) 412745916cd2Sjpk /* Skip over zones with bad path */ 412845916cd2Sjpk continue; 412945916cd2Sjpk 413045916cd2Sjpk if (zlabel != NULL && bldominates(zid_label, zlabel)) { 413145916cd2Sjpk /* 413245916cd2Sjpk * This zone dominates our zone. 413345916cd2Sjpk * Unmount the lofs mount of our zone's /export/home 413445916cd2Sjpk */ 413545916cd2Sjpk 413645916cd2Sjpk if (snprintf(path, MAXPATHLEN, 413745916cd2Sjpk "%s/zone/%s/export/home", zid_rpath, 413845916cd2Sjpk zone_name) > MAXPATHLEN) 413945916cd2Sjpk continue; 414045916cd2Sjpk 414145916cd2Sjpk /* Skip over mount failures */ 414245916cd2Sjpk (void) umount(path); 414345916cd2Sjpk } 414445916cd2Sjpk } 414545916cd2Sjpk free(zids); 414645916cd2Sjpk 414745916cd2Sjpk /* 414845916cd2Sjpk * Unmount global zone autofs trigger for this zone 414945916cd2Sjpk */ 415045916cd2Sjpk (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name); 415145916cd2Sjpk /* Skip over mount failures */ 415245916cd2Sjpk (void) umount(path); 415345916cd2Sjpk 415445916cd2Sjpk /* 415545916cd2Sjpk * Next unshare any exported directories from this zone. 415645916cd2Sjpk */ 415745916cd2Sjpk 415845916cd2Sjpk argv[0] = "zoneunshare"; 415945916cd2Sjpk argv[1] = "-z"; 416045916cd2Sjpk argv[2] = zone_name; 416145916cd2Sjpk argv[3] = NULL; 416245916cd2Sjpk 416345916cd2Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv); 416445916cd2Sjpk /* Don't check for errors since they don't affect the zone */ 416545916cd2Sjpk 416645916cd2Sjpk /* 416745916cd2Sjpk * Finally, deallocate any devices in the zone. 416845916cd2Sjpk */ 416945916cd2Sjpk 417045916cd2Sjpk argv[0] = "deallocate"; 417145916cd2Sjpk argv[1] = "-Isz"; 417245916cd2Sjpk argv[2] = zone_name; 417345916cd2Sjpk argv[3] = NULL; 417445916cd2Sjpk 417545916cd2Sjpk (void) forkexec(zlogp, "/usr/sbin/deallocate", argv); 417645916cd2Sjpk /* Don't check for errors since they don't affect the zone */ 417745916cd2Sjpk } 417845916cd2Sjpk 417945916cd2Sjpk /* 418045916cd2Sjpk * Fetch the Trusted Extensions label and multi-level ports (MLPs) for 418145916cd2Sjpk * this zone. 418245916cd2Sjpk */ 418345916cd2Sjpk static tsol_zcent_t * 418445916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs) 418545916cd2Sjpk { 418645916cd2Sjpk FILE *fp; 418745916cd2Sjpk tsol_zcent_t *zcent = NULL; 418845916cd2Sjpk char line[MAXTNZLEN]; 418945916cd2Sjpk 419045916cd2Sjpk if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) { 419145916cd2Sjpk zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH); 419245916cd2Sjpk return (NULL); 419345916cd2Sjpk } 419445916cd2Sjpk 419545916cd2Sjpk while (fgets(line, sizeof (line), fp) != NULL) { 419645916cd2Sjpk /* 419745916cd2Sjpk * Check for malformed database 419845916cd2Sjpk */ 419945916cd2Sjpk if (strlen(line) == MAXTNZLEN - 1) 420045916cd2Sjpk break; 420145916cd2Sjpk if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL) 420245916cd2Sjpk continue; 420345916cd2Sjpk if (strcmp(zcent->zc_name, zone_name) == 0) 420445916cd2Sjpk break; 420545916cd2Sjpk tsol_freezcent(zcent); 420645916cd2Sjpk zcent = NULL; 420745916cd2Sjpk } 420845916cd2Sjpk (void) fclose(fp); 420945916cd2Sjpk 421045916cd2Sjpk if (zcent == NULL) { 421145916cd2Sjpk zerror(zlogp, B_FALSE, "zone requires a label assignment. " 421245916cd2Sjpk "See tnzonecfg(4)"); 421345916cd2Sjpk } else { 421445916cd2Sjpk if (zlabel == NULL) 421545916cd2Sjpk zlabel = m_label_alloc(MAC_LABEL); 421645916cd2Sjpk /* 421745916cd2Sjpk * Save this zone's privileges for later read-down processing 421845916cd2Sjpk */ 421945916cd2Sjpk if ((zprivs = priv_allocset()) == NULL) { 422045916cd2Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 422145916cd2Sjpk return (NULL); 422245916cd2Sjpk } else { 422345916cd2Sjpk priv_copyset(privs, zprivs); 422445916cd2Sjpk } 422545916cd2Sjpk } 422645916cd2Sjpk return (zcent); 422745916cd2Sjpk } 422845916cd2Sjpk 422945916cd2Sjpk /* 423045916cd2Sjpk * Add the Trusted Extensions multi-level ports for this zone. 423145916cd2Sjpk */ 423245916cd2Sjpk static void 423345916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent) 423445916cd2Sjpk { 423545916cd2Sjpk tsol_mlp_t *mlp; 423645916cd2Sjpk tsol_mlpent_t tsme; 423745916cd2Sjpk 423845916cd2Sjpk if (!is_system_labeled()) 423945916cd2Sjpk return; 424045916cd2Sjpk 424145916cd2Sjpk tsme.tsme_zoneid = zoneid; 424245916cd2Sjpk tsme.tsme_flags = 0; 424345916cd2Sjpk for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) { 424445916cd2Sjpk tsme.tsme_mlp = *mlp; 424545916cd2Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) { 424645916cd2Sjpk zerror(zlogp, B_TRUE, "cannot set zone-specific MLP " 424745916cd2Sjpk "on %d-%d/%d", mlp->mlp_port, 424845916cd2Sjpk mlp->mlp_port_upper, mlp->mlp_ipp); 424945916cd2Sjpk } 425045916cd2Sjpk } 425145916cd2Sjpk 425245916cd2Sjpk tsme.tsme_flags = TSOL_MEF_SHARED; 425345916cd2Sjpk for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) { 425445916cd2Sjpk tsme.tsme_mlp = *mlp; 425545916cd2Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) { 425645916cd2Sjpk zerror(zlogp, B_TRUE, "cannot set shared MLP " 425745916cd2Sjpk "on %d-%d/%d", mlp->mlp_port, 425845916cd2Sjpk mlp->mlp_port_upper, mlp->mlp_ipp); 425945916cd2Sjpk } 426045916cd2Sjpk } 426145916cd2Sjpk } 426245916cd2Sjpk 426345916cd2Sjpk static void 426445916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid) 426545916cd2Sjpk { 426645916cd2Sjpk tsol_mlpent_t tsme; 426745916cd2Sjpk 426845916cd2Sjpk if (!is_system_labeled()) 426945916cd2Sjpk return; 427045916cd2Sjpk 427145916cd2Sjpk (void) memset(&tsme, 0, sizeof (tsme)); 427245916cd2Sjpk tsme.tsme_zoneid = zoneid; 427345916cd2Sjpk if (tnmlp(TNDB_FLUSH, &tsme) != 0) 427445916cd2Sjpk zerror(zlogp, B_TRUE, "cannot flush MLPs"); 427545916cd2Sjpk } 427645916cd2Sjpk 42777c478bd9Sstevel@tonic-gate int 42780094b373Sjv227347 prtmount(const struct mnttab *fs, void *x) { 42790094b373Sjv227347 zerror((zlog_t *)x, B_FALSE, " %s", fs->mnt_mountp); 42807c478bd9Sstevel@tonic-gate return (0); 42817c478bd9Sstevel@tonic-gate } 42827c478bd9Sstevel@tonic-gate 4283108322fbScarlsonj /* 4284108322fbScarlsonj * Look for zones running on the main system that are using this root (or any 4285108322fbScarlsonj * subdirectory of it). Return B_TRUE and print an error if a conflicting zone 4286108322fbScarlsonj * is found or if we can't tell. 4287108322fbScarlsonj */ 4288108322fbScarlsonj static boolean_t 4289108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath) 42907c478bd9Sstevel@tonic-gate { 4291108322fbScarlsonj zoneid_t *zids = NULL; 4292108322fbScarlsonj uint_t nzids = 0; 4293108322fbScarlsonj boolean_t retv; 4294108322fbScarlsonj int rlen, zlen; 4295108322fbScarlsonj char zroot[MAXPATHLEN]; 4296108322fbScarlsonj char zonename[ZONENAME_MAX]; 4297108322fbScarlsonj 4298108322fbScarlsonj for (;;) { 4299108322fbScarlsonj nzids += 10; 4300108322fbScarlsonj zids = malloc(nzids * sizeof (*zids)); 4301108322fbScarlsonj if (zids == NULL) { 43023f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed"); 4303108322fbScarlsonj return (B_TRUE); 4304108322fbScarlsonj } 4305108322fbScarlsonj if (zone_list(zids, &nzids) == 0) 4306108322fbScarlsonj break; 4307108322fbScarlsonj free(zids); 4308108322fbScarlsonj } 4309108322fbScarlsonj retv = B_FALSE; 4310108322fbScarlsonj rlen = strlen(rootpath); 4311108322fbScarlsonj while (nzids > 0) { 4312108322fbScarlsonj /* 4313108322fbScarlsonj * Ignore errors; they just mean that the zone has disappeared 4314108322fbScarlsonj * while we were busy. 4315108322fbScarlsonj */ 4316108322fbScarlsonj if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot, 4317108322fbScarlsonj sizeof (zroot)) == -1) 4318108322fbScarlsonj continue; 4319108322fbScarlsonj zlen = strlen(zroot); 4320108322fbScarlsonj if (zlen > rlen) 4321108322fbScarlsonj zlen = rlen; 4322108322fbScarlsonj if (strncmp(rootpath, zroot, zlen) == 0 && 4323108322fbScarlsonj (zroot[zlen] == '\0' || zroot[zlen] == '/') && 4324108322fbScarlsonj (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) { 4325108322fbScarlsonj if (getzonenamebyid(zids[nzids], zonename, 4326108322fbScarlsonj sizeof (zonename)) == -1) 4327108322fbScarlsonj (void) snprintf(zonename, sizeof (zonename), 4328108322fbScarlsonj "id %d", (int)zids[nzids]); 4329108322fbScarlsonj zerror(zlogp, B_FALSE, 4330108322fbScarlsonj "zone root %s already in use by zone %s", 4331108322fbScarlsonj rootpath, zonename); 4332108322fbScarlsonj retv = B_TRUE; 4333108322fbScarlsonj break; 4334108322fbScarlsonj } 4335108322fbScarlsonj } 4336108322fbScarlsonj free(zids); 4337108322fbScarlsonj return (retv); 4338108322fbScarlsonj } 4339108322fbScarlsonj 4340108322fbScarlsonj /* 4341108322fbScarlsonj * Search for loopback mounts that use this same source node (same device and 4342108322fbScarlsonj * inode). Return B_TRUE if there is one or if we can't tell. 4343108322fbScarlsonj */ 4344108322fbScarlsonj static boolean_t 4345108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath) 4346108322fbScarlsonj { 4347108322fbScarlsonj struct stat64 rst, zst; 4348108322fbScarlsonj struct mnttab *mnp; 4349108322fbScarlsonj 4350108322fbScarlsonj if (stat64(rootpath, &rst) == -1) { 4351108322fbScarlsonj zerror(zlogp, B_TRUE, "can't stat %s", rootpath); 4352108322fbScarlsonj return (B_TRUE); 4353108322fbScarlsonj } 4354108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 4355108322fbScarlsonj return (B_TRUE); 4356108322fbScarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) { 4357108322fbScarlsonj if (mnp->mnt_fstype == NULL || 4358108322fbScarlsonj strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0) 4359108322fbScarlsonj continue; 4360108322fbScarlsonj /* We're looking at a loopback mount. Stat it. */ 4361108322fbScarlsonj if (mnp->mnt_special != NULL && 4362108322fbScarlsonj stat64(mnp->mnt_special, &zst) != -1 && 4363108322fbScarlsonj rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) { 4364108322fbScarlsonj zerror(zlogp, B_FALSE, 4365108322fbScarlsonj "zone root %s is reachable through %s", 4366108322fbScarlsonj rootpath, mnp->mnt_mountp); 4367108322fbScarlsonj return (B_TRUE); 4368108322fbScarlsonj } 4369108322fbScarlsonj } 4370108322fbScarlsonj return (B_FALSE); 4371108322fbScarlsonj } 4372108322fbScarlsonj 43730209230bSgjelinek /* 43740209230bSgjelinek * Set memory cap and pool info for the zone's resource management 43750209230bSgjelinek * configuration. 43760209230bSgjelinek */ 43770209230bSgjelinek static int 43780209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid) 43790209230bSgjelinek { 43800209230bSgjelinek int res; 43810209230bSgjelinek uint64_t tmp; 43820209230bSgjelinek struct zone_mcaptab mcap; 43830209230bSgjelinek char sched[MAXNAMELEN]; 43840209230bSgjelinek zone_dochandle_t handle = NULL; 43850209230bSgjelinek char pool_err[128]; 43860209230bSgjelinek 43870209230bSgjelinek if ((handle = zonecfg_init_handle()) == NULL) { 43880209230bSgjelinek zerror(zlogp, B_TRUE, "getting zone configuration handle"); 43890209230bSgjelinek return (Z_BAD_HANDLE); 43900209230bSgjelinek } 43910209230bSgjelinek 43920209230bSgjelinek if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) { 43930209230bSgjelinek zerror(zlogp, B_FALSE, "invalid configuration"); 43940209230bSgjelinek zonecfg_fini_handle(handle); 43950209230bSgjelinek return (res); 43960209230bSgjelinek } 43970209230bSgjelinek 43980209230bSgjelinek /* 43990209230bSgjelinek * If a memory cap is configured, set the cap in the kernel using 44000209230bSgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled. 44010209230bSgjelinek */ 44020209230bSgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 44030209230bSgjelinek uint64_t num; 44040209230bSgjelinek char smf_err[128]; 44050209230bSgjelinek 44060209230bSgjelinek num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10); 44070209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 44080209230bSgjelinek zerror(zlogp, B_TRUE, "could not set zone memory cap"); 44090209230bSgjelinek zonecfg_fini_handle(handle); 44100209230bSgjelinek return (Z_INVAL); 44110209230bSgjelinek } 44120209230bSgjelinek 44130209230bSgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 44140209230bSgjelinek zerror(zlogp, B_FALSE, "enabling system/rcap service " 44150209230bSgjelinek "failed: %s", smf_err); 44160209230bSgjelinek zonecfg_fini_handle(handle); 44170209230bSgjelinek return (Z_INVAL); 44180209230bSgjelinek } 44190209230bSgjelinek } 44200209230bSgjelinek 44210209230bSgjelinek /* Get the scheduling class set in the zone configuration. */ 44220209230bSgjelinek if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK && 44230209230bSgjelinek strlen(sched) > 0) { 44240209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched, 44250209230bSgjelinek strlen(sched)) == -1) 44260209230bSgjelinek zerror(zlogp, B_TRUE, "WARNING: unable to set the " 44270209230bSgjelinek "default scheduling class"); 44280209230bSgjelinek 44290209230bSgjelinek } else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp) 44300209230bSgjelinek == Z_OK) { 44310209230bSgjelinek /* 44320209230bSgjelinek * If the zone has the zone.cpu-shares rctl set then we want to 44330209230bSgjelinek * use the Fair Share Scheduler (FSS) for processes in the 44340209230bSgjelinek * zone. Check what scheduling class the zone would be running 44350209230bSgjelinek * in by default so we can print a warning and modify the class 44360209230bSgjelinek * if we wouldn't be using FSS. 44370209230bSgjelinek */ 44380209230bSgjelinek char class_name[PC_CLNMSZ]; 44390209230bSgjelinek 44400209230bSgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name, 44410209230bSgjelinek sizeof (class_name)) != Z_OK) { 44420209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: unable to determine " 44430209230bSgjelinek "the zone's scheduling class"); 44440209230bSgjelinek 44450209230bSgjelinek } else if (strcmp("FSS", class_name) != 0) { 44460209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares " 44470209230bSgjelinek "rctl is set but\nFSS is not the default " 44480209230bSgjelinek "scheduling class for\nthis zone. FSS will be " 44490209230bSgjelinek "used for processes\nin the zone but to get the " 44500209230bSgjelinek "full benefit of FSS,\nit should be the default " 44510209230bSgjelinek "scheduling class.\nSee dispadmin(1M) for more " 44520209230bSgjelinek "details."); 44530209230bSgjelinek 44540209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS", 44550209230bSgjelinek strlen("FSS")) == -1) 44560209230bSgjelinek zerror(zlogp, B_TRUE, "WARNING: unable to set " 44570209230bSgjelinek "zone scheduling class to FSS"); 44580209230bSgjelinek } 44590209230bSgjelinek } 44600209230bSgjelinek 44610209230bSgjelinek /* 44620209230bSgjelinek * The next few blocks of code attempt to set up temporary pools as 44630209230bSgjelinek * well as persistent pools. In all cases we call the functions 44640209230bSgjelinek * unconditionally. Within each funtion the code will check if the 44650209230bSgjelinek * zone is actually configured for a temporary pool or persistent pool 44660209230bSgjelinek * and just return if there is nothing to do. 44670209230bSgjelinek * 44680209230bSgjelinek * If we are rebooting we want to attempt to reuse any temporary pool 44690209230bSgjelinek * that was previously set up. zonecfg_bind_tmp_pool() will do the 44700209230bSgjelinek * right thing in all cases (reuse or create) based on the current 44710209230bSgjelinek * zonecfg. 44720209230bSgjelinek */ 44730209230bSgjelinek if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err, 44740209230bSgjelinek sizeof (pool_err))) != Z_OK) { 44750209230bSgjelinek if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND) 44760209230bSgjelinek zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting " 44770209230bSgjelinek "cannot be instantiated", zonecfg_strerror(res), 44780209230bSgjelinek pool_err); 44790209230bSgjelinek else 44800209230bSgjelinek zerror(zlogp, B_FALSE, "could not bind zone to " 44810209230bSgjelinek "temporary pool: %s", zonecfg_strerror(res)); 44820209230bSgjelinek zonecfg_fini_handle(handle); 44830209230bSgjelinek return (Z_POOL_BIND); 44840209230bSgjelinek } 44850209230bSgjelinek 44860209230bSgjelinek /* 44870209230bSgjelinek * Check if we need to warn about poold not being enabled. 44880209230bSgjelinek */ 44890209230bSgjelinek if (zonecfg_warn_poold(handle)) { 44900209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has " 44910209230bSgjelinek "been specified\nbut the dynamic pool service is not " 44920209230bSgjelinek "enabled.\nThe system will not dynamically adjust the\n" 44930209230bSgjelinek "processor allocation within the specified range\n" 44940209230bSgjelinek "until svc:/system/pools/dynamic is enabled.\n" 44950209230bSgjelinek "See poold(1M)."); 44960209230bSgjelinek } 44970209230bSgjelinek 44980209230bSgjelinek /* The following is a warning, not an error. */ 44990209230bSgjelinek if ((res = zonecfg_bind_pool(handle, zoneid, pool_err, 45000209230bSgjelinek sizeof (pool_err))) != Z_OK) { 45010209230bSgjelinek if (res == Z_POOL_BIND) 45020209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: unable to bind to " 45030209230bSgjelinek "pool '%s'; using default pool.", pool_err); 45040209230bSgjelinek else if (res == Z_POOL) 45050209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: %s: %s", 45060209230bSgjelinek zonecfg_strerror(res), pool_err); 45070209230bSgjelinek else 45080209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: %s", 45090209230bSgjelinek zonecfg_strerror(res)); 45100209230bSgjelinek } 45110dc2366fSVenugopal Iyer (void) zonecfg_get_poolname(handle, zone_name, pool_name, MAXPATHLEN); 45120209230bSgjelinek 45130209230bSgjelinek zonecfg_fini_handle(handle); 45140209230bSgjelinek return (Z_OK); 45150209230bSgjelinek } 45160209230bSgjelinek 45170fbb751dSJohn Levon static void 45180fbb751dSJohn Levon report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res) 45190fbb751dSJohn Levon { 45200fbb751dSJohn Levon switch (res) { 45210fbb751dSJohn Levon case Z_TOO_BIG: 45220fbb751dSJohn Levon zerror(zlogp, B_FALSE, "%s property value is too large.", name); 45230fbb751dSJohn Levon break; 45240fbb751dSJohn Levon 45250fbb751dSJohn Levon case Z_INVALID_PROPERTY: 45260fbb751dSJohn Levon zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid", 45270fbb751dSJohn Levon name, value); 45280fbb751dSJohn Levon break; 45290fbb751dSJohn Levon 45300fbb751dSJohn Levon default: 45310fbb751dSJohn Levon zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res); 45320fbb751dSJohn Levon break; 45330fbb751dSJohn Levon } 45340fbb751dSJohn Levon } 45350fbb751dSJohn Levon 45365679c89fSjv227347 /* 45375679c89fSjv227347 * Sets the hostid of the new zone based on its configured value. The zone's 45385679c89fSjv227347 * zone_t structure must already exist in kernel memory. 'zlogp' refers to the 45395679c89fSjv227347 * log used to report errors and warnings and must be non-NULL. 'zone_namep' 45405679c89fSjv227347 * is the name of the new zone and must be non-NULL. 'zoneid' is the numeric 45415679c89fSjv227347 * ID of the new zone. 45425679c89fSjv227347 * 45435679c89fSjv227347 * This function returns zero on success and a nonzero error code on failure. 45445679c89fSjv227347 */ 45455679c89fSjv227347 static int 45460fbb751dSJohn Levon setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid) 45475679c89fSjv227347 { 45485679c89fSjv227347 int res; 45495679c89fSjv227347 char hostidp[HW_HOSTID_LEN]; 45505679c89fSjv227347 unsigned int hostid; 45515679c89fSjv227347 45520fbb751dSJohn Levon res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp)); 45530fbb751dSJohn Levon 45540fbb751dSJohn Levon if (res == Z_BAD_PROPERTY) { 45550fbb751dSJohn Levon return (Z_OK); 45560fbb751dSJohn Levon } else if (res != Z_OK) { 45570fbb751dSJohn Levon report_prop_err(zlogp, "hostid", hostidp, res); 45580fbb751dSJohn Levon return (res); 45590fbb751dSJohn Levon } 45600fbb751dSJohn Levon 45610fbb751dSJohn Levon hostid = (unsigned int)strtoul(hostidp, NULL, 16); 45620fbb751dSJohn Levon if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid, 45630fbb751dSJohn Levon sizeof (hostid))) != 0) { 45640fbb751dSJohn Levon zerror(zlogp, B_TRUE, 45650fbb751dSJohn Levon "zone hostid is not valid: %s: %d", hostidp, res); 45660fbb751dSJohn Levon return (Z_SYSTEM); 45670fbb751dSJohn Levon } 45680fbb751dSJohn Levon 45690fbb751dSJohn Levon return (res); 45700fbb751dSJohn Levon } 45710fbb751dSJohn Levon 45720fbb751dSJohn Levon static int 45730fbb751dSJohn Levon setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid) 45740fbb751dSJohn Levon { 45750fbb751dSJohn Levon char fsallowedp[ZONE_FS_ALLOWED_MAX]; 45760fbb751dSJohn Levon int res; 45770fbb751dSJohn Levon 45780fbb751dSJohn Levon res = zonecfg_get_fs_allowed(handle, fsallowedp, sizeof (fsallowedp)); 45790fbb751dSJohn Levon 45800fbb751dSJohn Levon if (res == Z_BAD_PROPERTY) { 45810fbb751dSJohn Levon return (Z_OK); 45820fbb751dSJohn Levon } else if (res != Z_OK) { 45830fbb751dSJohn Levon report_prop_err(zlogp, "fs-allowed", fsallowedp, res); 45840fbb751dSJohn Levon return (res); 45850fbb751dSJohn Levon } 45860fbb751dSJohn Levon 45870fbb751dSJohn Levon if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, &fsallowedp, 45880fbb751dSJohn Levon sizeof (fsallowedp)) != 0) { 45890fbb751dSJohn Levon zerror(zlogp, B_TRUE, 45900fbb751dSJohn Levon "fs-allowed couldn't be set: %s: %d", fsallowedp, res); 45910fbb751dSJohn Levon return (Z_SYSTEM); 45920fbb751dSJohn Levon } 45930fbb751dSJohn Levon 45940fbb751dSJohn Levon return (res); 45950fbb751dSJohn Levon } 45960fbb751dSJohn Levon 45970fbb751dSJohn Levon static int 45980fbb751dSJohn Levon setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid) 45990fbb751dSJohn Levon { 46000fbb751dSJohn Levon zone_dochandle_t handle; 46010fbb751dSJohn Levon int res = Z_OK; 46020fbb751dSJohn Levon 46035679c89fSjv227347 if ((handle = zonecfg_init_handle()) == NULL) { 46045679c89fSjv227347 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 46055679c89fSjv227347 return (Z_BAD_HANDLE); 46065679c89fSjv227347 } 46075679c89fSjv227347 if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) { 46085679c89fSjv227347 zerror(zlogp, B_FALSE, "invalid configuration"); 46090fbb751dSJohn Levon goto out; 46105679c89fSjv227347 } 46115679c89fSjv227347 46120fbb751dSJohn Levon if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK) 46130fbb751dSJohn Levon goto out; 46140fbb751dSJohn Levon 46150fbb751dSJohn Levon if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK) 46160fbb751dSJohn Levon goto out; 46170fbb751dSJohn Levon 46180fbb751dSJohn Levon out: 46195679c89fSjv227347 zonecfg_fini_handle(handle); 46205679c89fSjv227347 return (res); 46215679c89fSjv227347 } 46225679c89fSjv227347 4623108322fbScarlsonj zoneid_t 46246cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd) 4625108322fbScarlsonj { 4626108322fbScarlsonj zoneid_t rval = -1; 46277c478bd9Sstevel@tonic-gate priv_set_t *privs; 46287c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN]; 46297c478bd9Sstevel@tonic-gate char *rctlbuf = NULL; 4630108322fbScarlsonj size_t rctlbufsz = 0; 4631fa9e4066Sahrens char *zfsbuf = NULL; 4632fa9e4066Sahrens size_t zfsbufsz = 0; 4633108322fbScarlsonj zoneid_t zoneid = -1; 46347c478bd9Sstevel@tonic-gate int xerr; 4635108322fbScarlsonj char *kzone; 4636108322fbScarlsonj FILE *fp = NULL; 463745916cd2Sjpk tsol_zcent_t *zcent = NULL; 463845916cd2Sjpk int match = 0; 463945916cd2Sjpk int doi = 0; 4640f4b3ec61Sdh155122 int flags; 4641f4b3ec61Sdh155122 zone_iptype_t iptype; 46427c478bd9Sstevel@tonic-gate 46437c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 46447c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root"); 46457c478bd9Sstevel@tonic-gate return (-1); 46467c478bd9Sstevel@tonic-gate } 4647108322fbScarlsonj if (zonecfg_in_alt_root()) 4648108322fbScarlsonj resolve_lofs(zlogp, rootpath, sizeof (rootpath)); 46497c478bd9Sstevel@tonic-gate 46502b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) < 0) { 4651f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to determine ip-type"); 4652f4b3ec61Sdh155122 return (-1); 4653f4b3ec61Sdh155122 } 4654f4b3ec61Sdh155122 switch (iptype) { 4655f4b3ec61Sdh155122 case ZS_SHARED: 4656f4b3ec61Sdh155122 flags = 0; 4657f4b3ec61Sdh155122 break; 4658f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 4659f4b3ec61Sdh155122 flags = ZCF_NET_EXCL; 4660f4b3ec61Sdh155122 break; 4661f4b3ec61Sdh155122 } 4662f4b3ec61Sdh155122 46637c478bd9Sstevel@tonic-gate if ((privs = priv_allocset()) == NULL) { 46647c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 46657c478bd9Sstevel@tonic-gate return (-1); 46667c478bd9Sstevel@tonic-gate } 46677c478bd9Sstevel@tonic-gate priv_emptyset(privs); 4668ffbafc53Scomay if (get_privset(zlogp, privs, mount_cmd) != 0) 46697c478bd9Sstevel@tonic-gate goto error; 4670ffbafc53Scomay 46716cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT && 46726cfd72c6Sgjelinek get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) { 46737c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Unable to get list of rctls"); 46747c478bd9Sstevel@tonic-gate goto error; 46757c478bd9Sstevel@tonic-gate } 4676ffbafc53Scomay 4677fa9e4066Sahrens if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) { 4678fa9e4066Sahrens zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets"); 4679fa9e4066Sahrens goto error; 4680fa9e4066Sahrens } 46817c478bd9Sstevel@tonic-gate 46826cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) { 468345916cd2Sjpk zcent = get_zone_label(zlogp, privs); 468448451833Scarlsonj if (zcent != NULL) { 468545916cd2Sjpk match = zcent->zc_match; 468645916cd2Sjpk doi = zcent->zc_doi; 468745916cd2Sjpk *zlabel = zcent->zc_label; 468845916cd2Sjpk } else { 468945916cd2Sjpk goto error; 469045916cd2Sjpk } 46914201a95eSRic Aleshire if (validate_rootds_label(zlogp, rootpath, zlabel) != 0) 46924201a95eSRic Aleshire goto error; 469345916cd2Sjpk } 469445916cd2Sjpk 4695108322fbScarlsonj kzone = zone_name; 4696108322fbScarlsonj 4697108322fbScarlsonj /* 4698108322fbScarlsonj * We must do this scan twice. First, we look for zones running on the 4699108322fbScarlsonj * main system that are using this root (or any subdirectory of it). 4700108322fbScarlsonj * Next, we reduce to the shortest path and search for loopback mounts 4701108322fbScarlsonj * that use this same source node (same device and inode). 4702108322fbScarlsonj */ 4703108322fbScarlsonj if (duplicate_zone_root(zlogp, rootpath)) 4704108322fbScarlsonj goto error; 4705108322fbScarlsonj if (duplicate_reachable_path(zlogp, rootpath)) 4706108322fbScarlsonj goto error; 4707108322fbScarlsonj 47086cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd)) { 4709108322fbScarlsonj root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE); 4710108322fbScarlsonj 4711108322fbScarlsonj /* 4712108322fbScarlsonj * Forge up a special root for this zone. When a zone is 4713108322fbScarlsonj * mounted, we can't let the zone have its own root because the 4714108322fbScarlsonj * tools that will be used in this "scratch zone" need access 4715108322fbScarlsonj * to both the zone's resources and the running machine's 4716108322fbScarlsonj * executables. 4717108322fbScarlsonj * 4718108322fbScarlsonj * Note that the mkdir here also catches read-only filesystems. 4719108322fbScarlsonj */ 4720108322fbScarlsonj if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) { 4721108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", rootpath); 4722108322fbScarlsonj goto error; 4723108322fbScarlsonj } 4724108322fbScarlsonj if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0) 4725108322fbScarlsonj goto error; 4726108322fbScarlsonj } 4727108322fbScarlsonj 4728108322fbScarlsonj if (zonecfg_in_alt_root()) { 4729108322fbScarlsonj /* 4730108322fbScarlsonj * If we are mounting up a zone in an alternate root partition, 4731108322fbScarlsonj * then we have some additional work to do before starting the 4732108322fbScarlsonj * zone. First, resolve the root path down so that we're not 4733108322fbScarlsonj * fooled by duplicates. Then forge up an internal name for 4734108322fbScarlsonj * the zone. 4735108322fbScarlsonj */ 4736108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) { 4737108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile"); 4738108322fbScarlsonj goto error; 4739108322fbScarlsonj } 4740108322fbScarlsonj if (zonecfg_lock_scratch(fp) != 0) { 4741108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile"); 4742108322fbScarlsonj goto error; 4743108322fbScarlsonj } 4744108322fbScarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 4745108322fbScarlsonj NULL, 0) == 0) { 4746108322fbScarlsonj zerror(zlogp, B_FALSE, "scratch zone already running"); 4747108322fbScarlsonj goto error; 4748108322fbScarlsonj } 4749108322fbScarlsonj /* This is the preferred name */ 4750108322fbScarlsonj (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s", 4751108322fbScarlsonj zone_name); 4752108322fbScarlsonj srandom(getpid()); 4753108322fbScarlsonj while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL, 4754108322fbScarlsonj 0) == 0) { 4755108322fbScarlsonj /* This is just an arbitrary name; note "." usage */ 4756108322fbScarlsonj (void) snprintf(kernzone, sizeof (kernzone), 4757108322fbScarlsonj "SUNWlu.%08lX%08lX", random(), random()); 4758108322fbScarlsonj } 4759108322fbScarlsonj kzone = kernzone; 4760108322fbScarlsonj } 4761108322fbScarlsonj 47627c478bd9Sstevel@tonic-gate xerr = 0; 4763108322fbScarlsonj if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf, 4764f4b3ec61Sdh155122 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel, 4765f4b3ec61Sdh155122 flags)) == -1) { 47667c478bd9Sstevel@tonic-gate if (xerr == ZE_AREMOUNTS) { 47677c478bd9Sstevel@tonic-gate if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) { 47687c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 47697c478bd9Sstevel@tonic-gate "An unknown file-system is mounted on " 47707c478bd9Sstevel@tonic-gate "a subdirectory of %s", rootpath); 47717c478bd9Sstevel@tonic-gate } else { 47727c478bd9Sstevel@tonic-gate 47737c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 47747c478bd9Sstevel@tonic-gate "These file-systems are mounted on " 47757c478bd9Sstevel@tonic-gate "subdirectories of %s:", rootpath); 47767c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath, 47777c478bd9Sstevel@tonic-gate prtmount, zlogp); 47787c478bd9Sstevel@tonic-gate } 47797c478bd9Sstevel@tonic-gate } else if (xerr == ZE_CHROOTED) { 47807c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: " 47817c478bd9Sstevel@tonic-gate "cannot create a zone from a chrooted " 47827c478bd9Sstevel@tonic-gate "environment", "zone_create"); 47838c55461bSton } else if (xerr == ZE_LABELINUSE) { 47848c55461bSton char zonename[ZONENAME_MAX]; 47858c55461bSton (void) getzonenamebyid(getzoneidbylabel(zlabel), 47868c55461bSton zonename, ZONENAME_MAX); 47878c55461bSton zerror(zlogp, B_FALSE, "The zone label is already " 47888c55461bSton "used by the zone '%s'.", zonename); 47897c478bd9Sstevel@tonic-gate } else { 47907c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "zone_create"); 47917c478bd9Sstevel@tonic-gate } 47927c478bd9Sstevel@tonic-gate goto error; 47937c478bd9Sstevel@tonic-gate } 4794108322fbScarlsonj 4795108322fbScarlsonj if (zonecfg_in_alt_root() && 4796108322fbScarlsonj zonecfg_add_scratch(fp, zone_name, kernzone, 4797108322fbScarlsonj zonecfg_get_root()) == -1) { 4798108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot add mapfile entry"); 4799108322fbScarlsonj goto error; 4800108322fbScarlsonj } 4801108322fbScarlsonj 48020dc2366fSVenugopal Iyer if ((pool_name = malloc(MAXPATHLEN)) == NULL) { 48030dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE, "memory allocation failed"); 48040dc2366fSVenugopal Iyer return (Z_NOMEM); 48050dc2366fSVenugopal Iyer } 48060dc2366fSVenugopal Iyer bzero(pool_name, MAXPATHLEN); 48070dc2366fSVenugopal Iyer 48080679f794S /* 48090679f794S * The following actions are not performed when merely mounting a zone 48100679f794S * for administrative use. 48110679f794S */ 48120679f794S if (mount_cmd == Z_MNT_BOOT) { 48130679f794S brand_handle_t bh; 48140679f794S struct brand_attr attr; 48150679f794S char modname[MAXPATHLEN]; 48160679f794S 48170fbb751dSJohn Levon if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK) 48185679c89fSjv227347 goto error; 48195679c89fSjv227347 482062868012SSteve Lawrence if ((bh = brand_open(brand_name)) == NULL) { 48210679f794S zerror(zlogp, B_FALSE, 48220679f794S "unable to determine brand name"); 48235679c89fSjv227347 goto error; 48249acbbeafSnn35248 } 48259acbbeafSnn35248 48269a5d73e0SRic Aleshire if (!is_system_labeled() && 482762868012SSteve Lawrence (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) { 48289a5d73e0SRic Aleshire brand_close(bh); 48299a5d73e0SRic Aleshire zerror(zlogp, B_FALSE, 48309a5d73e0SRic Aleshire "cannot boot labeled zone on unlabeled system"); 48319a5d73e0SRic Aleshire goto error; 48329a5d73e0SRic Aleshire } 48339a5d73e0SRic Aleshire 48349acbbeafSnn35248 /* 48359acbbeafSnn35248 * If this brand requires any kernel support, now is the time to 48369acbbeafSnn35248 * get it loaded and initialized. 48379acbbeafSnn35248 */ 4838123807fbSedp if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) { 4839e767a340Sgjelinek brand_close(bh); 48400679f794S zerror(zlogp, B_FALSE, 48410679f794S "unable to determine brand kernel module"); 48425679c89fSjv227347 goto error; 48439acbbeafSnn35248 } 4844e767a340Sgjelinek brand_close(bh); 48459acbbeafSnn35248 48469acbbeafSnn35248 if (strlen(modname) > 0) { 484762868012SSteve Lawrence (void) strlcpy(attr.ba_brandname, brand_name, 484862868012SSteve Lawrence sizeof (attr.ba_brandname)); 484962868012SSteve Lawrence (void) strlcpy(attr.ba_modname, modname, 485062868012SSteve Lawrence sizeof (attr.ba_modname)); 48519acbbeafSnn35248 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr, 48529acbbeafSnn35248 sizeof (attr) != 0)) { 48530679f794S zerror(zlogp, B_TRUE, 48540679f794S "could not set zone brand attribute."); 48559acbbeafSnn35248 goto error; 48569acbbeafSnn35248 } 48579acbbeafSnn35248 } 48589acbbeafSnn35248 48595679c89fSjv227347 if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) 48600209230bSgjelinek goto error; 48610209230bSgjelinek 486245916cd2Sjpk set_mlps(zlogp, zoneid, zcent); 48630209230bSgjelinek } 48640209230bSgjelinek 4865108322fbScarlsonj rval = zoneid; 4866108322fbScarlsonj zoneid = -1; 4867108322fbScarlsonj 48687c478bd9Sstevel@tonic-gate error: 48695679c89fSjv227347 if (zoneid != -1) { 48705679c89fSjv227347 (void) zone_shutdown(zoneid); 4871108322fbScarlsonj (void) zone_destroy(zoneid); 48725679c89fSjv227347 } 48737c478bd9Sstevel@tonic-gate if (rctlbuf != NULL) 48747c478bd9Sstevel@tonic-gate free(rctlbuf); 48757c478bd9Sstevel@tonic-gate priv_freeset(privs); 4876108322fbScarlsonj if (fp != NULL) 4877108322fbScarlsonj zonecfg_close_scratch(fp); 4878108322fbScarlsonj lofs_discard_mnttab(); 487945916cd2Sjpk if (zcent != NULL) 488045916cd2Sjpk tsol_freezcent(zcent); 48817c478bd9Sstevel@tonic-gate return (rval); 48827c478bd9Sstevel@tonic-gate } 48837c478bd9Sstevel@tonic-gate 4884555afedfScarlsonj /* 4885555afedfScarlsonj * Enter the zone and write a /etc/zones/index file there. This allows 4886555afedfScarlsonj * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone 4887555afedfScarlsonj * details from inside the zone. 4888555afedfScarlsonj */ 4889555afedfScarlsonj static void 4890555afedfScarlsonj write_index_file(zoneid_t zoneid) 4891555afedfScarlsonj { 4892555afedfScarlsonj FILE *zef; 4893555afedfScarlsonj FILE *zet; 4894555afedfScarlsonj struct zoneent *zep; 4895555afedfScarlsonj pid_t child; 4896555afedfScarlsonj int tmpl_fd; 4897555afedfScarlsonj ctid_t ct; 4898555afedfScarlsonj int fd; 4899555afedfScarlsonj char uuidstr[UUID_PRINTABLE_STRING_LENGTH]; 4900555afedfScarlsonj 4901555afedfScarlsonj /* Locate the zone entry in the global zone's index file */ 4902555afedfScarlsonj if ((zef = setzoneent()) == NULL) 4903555afedfScarlsonj return; 4904555afedfScarlsonj while ((zep = getzoneent_private(zef)) != NULL) { 4905555afedfScarlsonj if (strcmp(zep->zone_name, zone_name) == 0) 4906555afedfScarlsonj break; 4907555afedfScarlsonj free(zep); 4908555afedfScarlsonj } 4909555afedfScarlsonj endzoneent(zef); 4910555afedfScarlsonj if (zep == NULL) 4911555afedfScarlsonj return; 4912555afedfScarlsonj 4913555afedfScarlsonj if ((tmpl_fd = init_template()) == -1) { 4914555afedfScarlsonj free(zep); 4915555afedfScarlsonj return; 4916555afedfScarlsonj } 4917555afedfScarlsonj 4918555afedfScarlsonj if ((child = fork()) == -1) { 4919555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd); 4920555afedfScarlsonj (void) close(tmpl_fd); 4921555afedfScarlsonj free(zep); 4922555afedfScarlsonj return; 4923555afedfScarlsonj } 4924555afedfScarlsonj 4925555afedfScarlsonj /* parent waits for child to finish */ 4926555afedfScarlsonj if (child != 0) { 4927555afedfScarlsonj free(zep); 4928555afedfScarlsonj if (contract_latest(&ct) == -1) 4929555afedfScarlsonj ct = -1; 4930555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd); 4931555afedfScarlsonj (void) close(tmpl_fd); 4932555afedfScarlsonj (void) waitpid(child, NULL, 0); 4933555afedfScarlsonj (void) contract_abandon_id(ct); 4934555afedfScarlsonj return; 4935555afedfScarlsonj } 4936555afedfScarlsonj 4937555afedfScarlsonj /* child enters zone and sets up index file */ 4938555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd); 4939555afedfScarlsonj if (zone_enter(zoneid) != -1) { 4940555afedfScarlsonj (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE); 4941555afedfScarlsonj (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID, 4942555afedfScarlsonj ZONE_CONFIG_GID); 4943555afedfScarlsonj fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC, 4944555afedfScarlsonj ZONE_INDEX_MODE); 4945555afedfScarlsonj if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) { 4946555afedfScarlsonj (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID); 4947555afedfScarlsonj if (uuid_is_null(zep->zone_uuid)) 4948555afedfScarlsonj uuidstr[0] = '\0'; 4949555afedfScarlsonj else 4950555afedfScarlsonj uuid_unparse(zep->zone_uuid, uuidstr); 4951555afedfScarlsonj (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name, 4952555afedfScarlsonj zone_state_str(zep->zone_state), 4953555afedfScarlsonj uuidstr); 4954555afedfScarlsonj (void) fclose(zet); 4955555afedfScarlsonj } 4956555afedfScarlsonj } 4957555afedfScarlsonj _exit(0); 4958555afedfScarlsonj } 4959555afedfScarlsonj 49607c478bd9Sstevel@tonic-gate int 49616cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid) 49627c478bd9Sstevel@tonic-gate { 49639acbbeafSnn35248 char zonepath[MAXPATHLEN]; 49645749802bSdp 49656cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) { 4966fa9e4066Sahrens lofs_discard_mnttab(); 4967fa9e4066Sahrens return (-1); 4968fa9e4066Sahrens } 4969fa9e4066Sahrens 49709acbbeafSnn35248 /* 49719acbbeafSnn35248 * Before we try to mount filesystems we need to create the 49729acbbeafSnn35248 * attribute backing store for /dev 49739acbbeafSnn35248 */ 49749acbbeafSnn35248 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 49759acbbeafSnn35248 lofs_discard_mnttab(); 49769acbbeafSnn35248 return (-1); 49779acbbeafSnn35248 } 497816bca938Svp157776 resolve_lofs(zlogp, zonepath, sizeof (zonepath)); 497927e6fb21Sdp 498027e6fb21Sdp /* Make /dev directory owned by root, grouped sys */ 498127e6fb21Sdp if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE, 498227e6fb21Sdp 0, 3) != 0) { 4983108322fbScarlsonj lofs_discard_mnttab(); 49847c478bd9Sstevel@tonic-gate return (-1); 4985108322fbScarlsonj } 4986facf4a8dSllai1 49879acbbeafSnn35248 if (mount_filesystems(zlogp, mount_cmd) != 0) { 4988facf4a8dSllai1 lofs_discard_mnttab(); 4989facf4a8dSllai1 return (-1); 4990facf4a8dSllai1 } 4991facf4a8dSllai1 49926cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT) { 4993f4b3ec61Sdh155122 zone_iptype_t iptype; 4994f4b3ec61Sdh155122 49952b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) < 0) { 4996f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to determine ip-type"); 4997108322fbScarlsonj lofs_discard_mnttab(); 49987c478bd9Sstevel@tonic-gate return (-1); 4999108322fbScarlsonj } 5000555afedfScarlsonj 5001f4b3ec61Sdh155122 switch (iptype) { 5002f4b3ec61Sdh155122 case ZS_SHARED: 5003f4b3ec61Sdh155122 /* Always do this to make lo0 get configured */ 5004f4b3ec61Sdh155122 if (configure_shared_network_interfaces(zlogp) != 0) { 5005f4b3ec61Sdh155122 lofs_discard_mnttab(); 5006f4b3ec61Sdh155122 return (-1); 5007f4b3ec61Sdh155122 } 5008f4b3ec61Sdh155122 break; 5009f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 5010*550b6e40SSowmini Varadhan if (configure_exclusive_network_interfaces(zlogp, 5011*550b6e40SSowmini Varadhan zoneid) != 5012f4b3ec61Sdh155122 0) { 5013f4b3ec61Sdh155122 lofs_discard_mnttab(); 5014f4b3ec61Sdh155122 return (-1); 5015f4b3ec61Sdh155122 } 5016f4b3ec61Sdh155122 break; 5017f4b3ec61Sdh155122 } 5018f4b3ec61Sdh155122 } 5019f4b3ec61Sdh155122 5020555afedfScarlsonj write_index_file(zoneid); 5021555afedfScarlsonj 5022108322fbScarlsonj lofs_discard_mnttab(); 50237c478bd9Sstevel@tonic-gate return (0); 50247c478bd9Sstevel@tonic-gate } 50257c478bd9Sstevel@tonic-gate 5026108322fbScarlsonj static int 5027108322fbScarlsonj lu_root_teardown(zlog_t *zlogp) 50287c478bd9Sstevel@tonic-gate { 5029108322fbScarlsonj char zroot[MAXPATHLEN]; 5030108322fbScarlsonj 5031108322fbScarlsonj if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 5032108322fbScarlsonj zerror(zlogp, B_FALSE, "unable to determine zone root"); 5033108322fbScarlsonj return (-1); 5034108322fbScarlsonj } 5035108322fbScarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 5036108322fbScarlsonj 5037108322fbScarlsonj /* 5038108322fbScarlsonj * At this point, the processes are gone, the filesystems (save the 5039108322fbScarlsonj * root) are unmounted, and the zone is on death row. But there may 5040108322fbScarlsonj * still be creds floating about in the system that reference the 5041108322fbScarlsonj * zone_t, and which pin down zone_rootvp causing this call to fail 5042108322fbScarlsonj * with EBUSY. Thus, we try for a little while before just giving up. 5043108322fbScarlsonj * (How I wish this were not true, and umount2 just did the right 5044108322fbScarlsonj * thing, or tmpfs supported MS_FORCE This is a gross hack.) 5045108322fbScarlsonj */ 5046108322fbScarlsonj if (umount2(zroot, MS_FORCE) != 0) { 5047108322fbScarlsonj if (errno == ENOTSUP && umount2(zroot, 0) == 0) 5048108322fbScarlsonj goto unmounted; 5049108322fbScarlsonj if (errno == EBUSY) { 5050108322fbScarlsonj int tries = 10; 5051108322fbScarlsonj 5052108322fbScarlsonj while (--tries >= 0) { 5053108322fbScarlsonj (void) sleep(1); 5054108322fbScarlsonj if (umount2(zroot, 0) == 0) 5055108322fbScarlsonj goto unmounted; 5056108322fbScarlsonj if (errno != EBUSY) 5057108322fbScarlsonj break; 5058108322fbScarlsonj } 5059108322fbScarlsonj } 5060108322fbScarlsonj zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot); 5061108322fbScarlsonj return (-1); 5062108322fbScarlsonj } 5063108322fbScarlsonj unmounted: 5064108322fbScarlsonj 5065108322fbScarlsonj /* 5066108322fbScarlsonj * Only zones in an alternate root environment have scratch zone 5067108322fbScarlsonj * entries. 5068108322fbScarlsonj */ 5069108322fbScarlsonj if (zonecfg_in_alt_root()) { 5070108322fbScarlsonj FILE *fp; 5071108322fbScarlsonj int retv; 5072108322fbScarlsonj 5073108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 5074108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile"); 5075108322fbScarlsonj return (-1); 5076108322fbScarlsonj } 5077108322fbScarlsonj retv = -1; 5078108322fbScarlsonj if (zonecfg_lock_scratch(fp) != 0) 5079108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile"); 5080108322fbScarlsonj else if (zonecfg_delete_scratch(fp, kernzone) != 0) 5081108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot delete map entry"); 5082108322fbScarlsonj else 5083108322fbScarlsonj retv = 0; 5084108322fbScarlsonj zonecfg_close_scratch(fp); 5085108322fbScarlsonj return (retv); 5086108322fbScarlsonj } else { 5087108322fbScarlsonj return (0); 5088108322fbScarlsonj } 5089108322fbScarlsonj } 5090108322fbScarlsonj 5091108322fbScarlsonj int 50920209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting) 5093108322fbScarlsonj { 5094108322fbScarlsonj char *kzone; 50957c478bd9Sstevel@tonic-gate zoneid_t zoneid; 50960209230bSgjelinek int res; 50970209230bSgjelinek char pool_err[128]; 5098ff17c8bfSgjelinek char zpath[MAXPATHLEN]; 50999acbbeafSnn35248 char cmdbuf[MAXPATHLEN]; 5100123807fbSedp brand_handle_t bh = NULL; 51012b24ab6bSSebastien Roy dladm_status_t status; 51022b24ab6bSSebastien Roy char errmsg[DLADM_STRSIZE]; 5103f4b3ec61Sdh155122 ushort_t flags; 51047c478bd9Sstevel@tonic-gate 5105108322fbScarlsonj kzone = zone_name; 5106108322fbScarlsonj if (zonecfg_in_alt_root()) { 5107108322fbScarlsonj FILE *fp; 5108108322fbScarlsonj 5109108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 5110108322fbScarlsonj zerror(zlogp, B_TRUE, "unable to open map file"); 5111108322fbScarlsonj goto error; 5112108322fbScarlsonj } 5113108322fbScarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 5114108322fbScarlsonj kernzone, sizeof (kernzone)) != 0) { 5115108322fbScarlsonj zerror(zlogp, B_FALSE, "unable to find scratch zone"); 5116108322fbScarlsonj zonecfg_close_scratch(fp); 5117108322fbScarlsonj goto error; 5118108322fbScarlsonj } 5119108322fbScarlsonj zonecfg_close_scratch(fp); 5120108322fbScarlsonj kzone = kernzone; 5121108322fbScarlsonj } 5122108322fbScarlsonj 5123108322fbScarlsonj if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) { 51247c478bd9Sstevel@tonic-gate if (!bringup_failure_recovery) 51257c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid"); 5126108322fbScarlsonj if (unmount_cmd) 5127108322fbScarlsonj (void) lu_root_teardown(zlogp); 51287c478bd9Sstevel@tonic-gate goto error; 51297c478bd9Sstevel@tonic-gate } 51307c478bd9Sstevel@tonic-gate 51310dc2366fSVenugopal Iyer if (remove_datalink_pool(zlogp, zoneid) != 0) { 51320dc2366fSVenugopal Iyer zerror(zlogp, B_FALSE, "unable clear datalink pool property"); 51330dc2366fSVenugopal Iyer goto error; 51340dc2366fSVenugopal Iyer } 51350dc2366fSVenugopal Iyer 5136*550b6e40SSowmini Varadhan if (remove_datalink_protect(zlogp, zoneid) != 0) { 5137*550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, 5138*550b6e40SSowmini Varadhan "unable clear datalink protect property"); 5139*550b6e40SSowmini Varadhan goto error; 5140*550b6e40SSowmini Varadhan } 5141*550b6e40SSowmini Varadhan 5142*550b6e40SSowmini Varadhan /* 5143*550b6e40SSowmini Varadhan * The datalinks assigned to the zone will be removed from the NGZ as 5144*550b6e40SSowmini Varadhan * part of zone_shutdown() so that we need to remove protect/pool etc. 5145*550b6e40SSowmini Varadhan * before zone_shutdown(). Even if the shutdown itself fails, the zone 5146*550b6e40SSowmini Varadhan * will not be able to violate any constraints applied because the 5147*550b6e40SSowmini Varadhan * datalinks are no longer available to the zone. 5148*550b6e40SSowmini Varadhan */ 51497c478bd9Sstevel@tonic-gate if (zone_shutdown(zoneid) != 0) { 51507c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to shutdown zone"); 51517c478bd9Sstevel@tonic-gate goto error; 51527c478bd9Sstevel@tonic-gate } 51537c478bd9Sstevel@tonic-gate 5154ff17c8bfSgjelinek /* Get the zonepath of this zone */ 5155ff17c8bfSgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 5156ff17c8bfSgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path"); 51579acbbeafSnn35248 goto error; 51589acbbeafSnn35248 } 51599acbbeafSnn35248 51609acbbeafSnn35248 /* Get a handle to the brand info for this zone */ 516162868012SSteve Lawrence if ((bh = brand_open(brand_name)) == NULL) { 51629acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 51639acbbeafSnn35248 return (-1); 51649acbbeafSnn35248 } 51659acbbeafSnn35248 /* 51669acbbeafSnn35248 * If there is a brand 'halt' callback, execute it now to give the 51679acbbeafSnn35248 * brand a chance to cleanup any custom configuration. 51689acbbeafSnn35248 */ 51699acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX); 5170ff17c8bfSgjelinek if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 5171ff17c8bfSgjelinek sizeof (cmdbuf) - EXEC_LEN) < 0) { 5172123807fbSedp brand_close(bh); 51739acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine branded zone's " 51749acbbeafSnn35248 "halt callback."); 51759acbbeafSnn35248 goto error; 51769acbbeafSnn35248 } 5177123807fbSedp brand_close(bh); 51789acbbeafSnn35248 51799acbbeafSnn35248 if ((strlen(cmdbuf) > EXEC_LEN) && 5180c5cd6260S (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) { 51819acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 51829acbbeafSnn35248 goto error; 51839acbbeafSnn35248 } 51849acbbeafSnn35248 5185f4b3ec61Sdh155122 if (!unmount_cmd) { 5186f4b3ec61Sdh155122 zone_iptype_t iptype; 5187f4b3ec61Sdh155122 5188f4b3ec61Sdh155122 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags, 5189f4b3ec61Sdh155122 sizeof (flags)) < 0) { 51902b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) < 0) { 5191f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to determine " 5192f4b3ec61Sdh155122 "ip-type"); 51937c478bd9Sstevel@tonic-gate goto error; 51947c478bd9Sstevel@tonic-gate } 5195f4b3ec61Sdh155122 } else { 5196f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL) 5197f4b3ec61Sdh155122 iptype = ZS_EXCLUSIVE; 5198f4b3ec61Sdh155122 else 5199f4b3ec61Sdh155122 iptype = ZS_SHARED; 5200f4b3ec61Sdh155122 } 5201f4b3ec61Sdh155122 5202f4b3ec61Sdh155122 switch (iptype) { 5203f4b3ec61Sdh155122 case ZS_SHARED: 5204f4b3ec61Sdh155122 if (unconfigure_shared_network_interfaces(zlogp, 5205f4b3ec61Sdh155122 zoneid) != 0) { 5206f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "unable to unconfigure " 5207f4b3ec61Sdh155122 "network interfaces in zone"); 5208f4b3ec61Sdh155122 goto error; 5209f4b3ec61Sdh155122 } 5210f4b3ec61Sdh155122 break; 5211f4b3ec61Sdh155122 case ZS_EXCLUSIVE: 5212f4b3ec61Sdh155122 if (unconfigure_exclusive_network_interfaces(zlogp, 5213f4b3ec61Sdh155122 zoneid) != 0) { 5214f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "unable to unconfigure " 5215f4b3ec61Sdh155122 "network interfaces in zone"); 5216f4b3ec61Sdh155122 goto error; 5217f4b3ec61Sdh155122 } 52182b24ab6bSSebastien Roy status = dladm_zone_halt(dld_handle, zoneid); 52192b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) { 52202b24ab6bSSebastien Roy zerror(zlogp, B_FALSE, "unable to notify " 52212b24ab6bSSebastien Roy "dlmgmtd of zone halt: %s", 52222b24ab6bSSebastien Roy dladm_status2str(status, errmsg)); 52232b24ab6bSSebastien Roy } 5224f4b3ec61Sdh155122 break; 5225f4b3ec61Sdh155122 } 5226f4b3ec61Sdh155122 } 52277c478bd9Sstevel@tonic-gate 5228108322fbScarlsonj if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) { 52297c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to abort TCP connections"); 52307c478bd9Sstevel@tonic-gate goto error; 52317c478bd9Sstevel@tonic-gate } 52327c478bd9Sstevel@tonic-gate 5233108322fbScarlsonj if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) { 52347c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, 52357c478bd9Sstevel@tonic-gate "unable to unmount file systems in zone"); 52367c478bd9Sstevel@tonic-gate goto error; 52377c478bd9Sstevel@tonic-gate } 52387c478bd9Sstevel@tonic-gate 52390209230bSgjelinek /* 5240cf6b13d2Sgjelinek * If we are rebooting then we normally don't want to destroy an 5241cf6b13d2Sgjelinek * existing temporary pool at this point so that we can just reuse it 5242cf6b13d2Sgjelinek * when the zone boots back up. However, it is also possible we were 5243cf6b13d2Sgjelinek * running with a temporary pool and the zone configuration has been 5244cf6b13d2Sgjelinek * modified to no longer use a temporary pool. In that case we need 5245cf6b13d2Sgjelinek * to destroy the temporary pool now. This case looks like the case 5246cf6b13d2Sgjelinek * where we never had a temporary pool configured but 5247cf6b13d2Sgjelinek * zonecfg_destroy_tmp_pool will do the right thing either way. 52480209230bSgjelinek */ 5249cf6b13d2Sgjelinek if (!unmount_cmd) { 5250cf6b13d2Sgjelinek boolean_t destroy_tmp_pool = B_TRUE; 5251cf6b13d2Sgjelinek 5252cf6b13d2Sgjelinek if (rebooting) { 5253cf6b13d2Sgjelinek struct zone_psettab pset_tab; 5254cf6b13d2Sgjelinek zone_dochandle_t handle; 5255cf6b13d2Sgjelinek 5256cf6b13d2Sgjelinek if ((handle = zonecfg_init_handle()) != NULL && 5257cf6b13d2Sgjelinek zonecfg_get_handle(zone_name, handle) == Z_OK && 5258cf6b13d2Sgjelinek zonecfg_lookup_pset(handle, &pset_tab) == Z_OK) 5259cf6b13d2Sgjelinek destroy_tmp_pool = B_FALSE; 5260e767a340Sgjelinek 5261e767a340Sgjelinek zonecfg_fini_handle(handle); 5262cf6b13d2Sgjelinek } 5263cf6b13d2Sgjelinek 5264cf6b13d2Sgjelinek if (destroy_tmp_pool) { 52650209230bSgjelinek if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err, 52660209230bSgjelinek sizeof (pool_err))) != Z_OK) { 52670209230bSgjelinek if (res == Z_POOL) 52680209230bSgjelinek zerror(zlogp, B_FALSE, pool_err); 52690209230bSgjelinek } 52700209230bSgjelinek } 5271cf6b13d2Sgjelinek } 52720209230bSgjelinek 52730dc2366fSVenugopal Iyer free(pool_name); 52740dc2366fSVenugopal Iyer 527545916cd2Sjpk remove_mlps(zlogp, zoneid); 527645916cd2Sjpk 52777c478bd9Sstevel@tonic-gate if (zone_destroy(zoneid) != 0) { 52787c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to destroy zone"); 52797c478bd9Sstevel@tonic-gate goto error; 52807c478bd9Sstevel@tonic-gate } 5281108322fbScarlsonj 5282108322fbScarlsonj /* 5283108322fbScarlsonj * Special teardown for alternate boot environments: remove the tmpfs 5284108322fbScarlsonj * root for the zone and then remove it from the map file. 5285108322fbScarlsonj */ 5286108322fbScarlsonj if (unmount_cmd && lu_root_teardown(zlogp) != 0) 5287108322fbScarlsonj goto error; 5288108322fbScarlsonj 5289108322fbScarlsonj lofs_discard_mnttab(); 52907c478bd9Sstevel@tonic-gate return (0); 52917c478bd9Sstevel@tonic-gate 52927c478bd9Sstevel@tonic-gate error: 5293108322fbScarlsonj lofs_discard_mnttab(); 52947c478bd9Sstevel@tonic-gate return (-1); 52957c478bd9Sstevel@tonic-gate } 5296