1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright 2016, Joyent Inc. 25 * Copyright (c) 2015, 2016 by Delphix. All rights reserved. 26 * Copyright 2019 OmniOS Community Edition (OmniOSce) Association. 27 * Copyright 2020 RackTop Systems Inc. 28 */ 29 30 /* 31 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 32 */ 33 34 /* 35 * This module contains functions used to bring up and tear down the 36 * Virtual Platform: [un]mounting file-systems, [un]plumbing network 37 * interfaces, [un]configuring devices, establishing resource controls, 38 * and creating/destroying the zone in the kernel. These actions, on 39 * the way up, ready the zone; on the way down, they halt the zone. 40 * See the much longer block comment at the beginning of zoneadmd.c 41 * for a bigger picture of how the whole program functions. 42 * 43 * This module also has primary responsibility for the layout of "scratch 44 * zones." These are mounted, but inactive, zones that are used during 45 * operating system upgrade and potentially other administrative action. The 46 * scratch zone environment is similar to the miniroot environment. The zone's 47 * actual root is mounted read-write on /a, and the standard paths (/usr, 48 * /sbin, /lib) all lead to read-only copies of the running system's binaries. 49 * This allows the administrative tools to manipulate the zone using "-R /a" 50 * without relying on any binaries in the zone itself. 51 * 52 * If the scratch zone is on an alternate root (Live Upgrade [LU] boot 53 * environment), then we must resolve the lofs mounts used there to uncover 54 * writable (unshared) resources. Shared resources, though, are always 55 * read-only. In addition, if the "same" zone with a different root path is 56 * currently running, then "/b" inside the zone points to the running zone's 57 * root. This allows LU to synchronize configuration files during the upgrade 58 * process. 59 * 60 * To construct this environment, this module creates a tmpfs mount on 61 * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as 62 * described above is constructed on the fly. The zone is then created using 63 * $ZONEPATH/lu as the root. 64 * 65 * Note that scratch zones are inactive. The zone's bits are not running and 66 * likely cannot be run correctly until upgrade is done. Init is not running 67 * there, nor is SMF. Because of this, the "mounted" state of a scratch zone 68 * is not a part of the usual halt/ready/boot state machine. 69 */ 70 71 #include <sys/param.h> 72 #include <sys/mount.h> 73 #include <sys/mntent.h> 74 #include <sys/socket.h> 75 #include <sys/utsname.h> 76 #include <sys/types.h> 77 #include <sys/stat.h> 78 #include <sys/sockio.h> 79 #include <sys/stropts.h> 80 #include <sys/conf.h> 81 #include <sys/systeminfo.h> 82 #include <sys/secflags.h> 83 84 #include <libdlpi.h> 85 #include <libdllink.h> 86 #include <libdlvlan.h> 87 88 #include <inet/tcp.h> 89 #include <arpa/inet.h> 90 #include <netinet/in.h> 91 #include <net/route.h> 92 93 #include <stdio.h> 94 #include <errno.h> 95 #include <fcntl.h> 96 #include <unistd.h> 97 #include <rctl.h> 98 #include <stdlib.h> 99 #include <string.h> 100 #include <strings.h> 101 #include <wait.h> 102 #include <limits.h> 103 #include <libgen.h> 104 #include <libzfs.h> 105 #include <libdevinfo.h> 106 #include <zone.h> 107 #include <assert.h> 108 #include <libcontract.h> 109 #include <libcontract_priv.h> 110 #include <uuid/uuid.h> 111 112 #include <sys/mntio.h> 113 #include <sys/mnttab.h> 114 #include <sys/fs/autofs.h> /* for _autofssys() */ 115 #include <sys/fs/lofs_info.h> 116 #include <sys/fs/zfs.h> 117 118 #include <pool.h> 119 #include <sys/pool.h> 120 #include <sys/priocntl.h> 121 122 #include <libbrand.h> 123 #include <sys/brand.h> 124 #include <libzonecfg.h> 125 #include <synch.h> 126 127 #include "zoneadmd.h" 128 #include <tsol/label.h> 129 #include <libtsnet.h> 130 #include <sys/priv.h> 131 #include <libinetutil.h> 132 133 #define V4_ADDR_LEN 32 134 #define V6_ADDR_LEN 128 135 136 #define RESOURCE_DEFAULT_OPTS \ 137 MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES 138 139 #define DFSTYPES "/etc/dfs/fstypes" 140 #define MAXTNZLEN 2048 141 142 #define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT) 143 144 /* a reasonable estimate for the number of lwps per process */ 145 #define LWPS_PER_PROCESS 10 146 147 /* for routing socket */ 148 static int rts_seqno = 0; 149 150 /* mangled zone name when mounting in an alternate root environment */ 151 static char kernzone[ZONENAME_MAX]; 152 153 /* array of cached mount entries for resolve_lofs */ 154 static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max; 155 156 /* for Trusted Extensions */ 157 static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *); 158 static int tsol_mounts(zlog_t *, char *, char *); 159 static void tsol_unmounts(zlog_t *, char *); 160 161 static m_label_t *zlabel = NULL; 162 static m_label_t *zid_label = NULL; 163 static priv_set_t *zprivs = NULL; 164 165 static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn"; 166 167 /* from libsocket, not in any header file */ 168 extern int getnetmaskbyaddr(struct in_addr, struct in_addr *); 169 170 /* from zoneadmd */ 171 extern char query_hook[]; 172 173 /* 174 * For each "net" resource configured in zonecfg, we track a zone_addr_list_t 175 * node in a linked list that is sorted by linkid. The list is constructed as 176 * the xml configuration file is parsed, and the information 177 * contained in each node is added to the kernel before the zone is 178 * booted, to be retrieved and applied from within the exclusive-IP NGZ 179 * on boot. 180 */ 181 typedef struct zone_addr_list { 182 struct zone_addr_list *za_next; 183 datalink_id_t za_linkid; /* datalink_id_t of interface */ 184 struct zone_nwiftab za_nwiftab; /* address, defrouter properties */ 185 } zone_addr_list_t; 186 187 /* 188 * An optimization for build_mnttable: reallocate (and potentially copy the 189 * data) only once every N times through the loop. 190 */ 191 #define MNTTAB_HUNK 32 192 193 /* some handy macros */ 194 #define SIN(s) ((struct sockaddr_in *)s) 195 #define SIN6(s) ((struct sockaddr_in6 *)s) 196 197 /* 198 * Private autofs system call 199 */ 200 extern int _autofssys(int, void *); 201 202 static int 203 autofs_cleanup(zoneid_t zoneid) 204 { 205 int r; 206 207 /* 208 * Ask autofs to unmount all trigger nodes in the given zone. 209 * Handle ENOSYS in the case that the autofs kernel module is not 210 * installed. 211 */ 212 r = _autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid); 213 if (r != 0 && errno == ENOSYS) { 214 return (0); 215 } 216 return (r); 217 } 218 219 static void 220 free_mnttable(struct mnttab *mnt_array, uint_t nelem) 221 { 222 uint_t i; 223 224 if (mnt_array == NULL) 225 return; 226 for (i = 0; i < nelem; i++) { 227 free(mnt_array[i].mnt_mountp); 228 free(mnt_array[i].mnt_fstype); 229 free(mnt_array[i].mnt_special); 230 free(mnt_array[i].mnt_mntopts); 231 assert(mnt_array[i].mnt_time == NULL); 232 } 233 free(mnt_array); 234 } 235 236 /* 237 * Build the mount table for the zone rooted at "zroot", storing the resulting 238 * array of struct mnttabs in "mnt_arrayp" and the number of elements in the 239 * array in "nelemp". 240 */ 241 static int 242 build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab, 243 struct mnttab **mnt_arrayp, uint_t *nelemp) 244 { 245 struct mnttab mnt; 246 struct mnttab *mnts; 247 struct mnttab *mnp; 248 uint_t nmnt; 249 250 rewind(mnttab); 251 resetmnttab(mnttab); 252 nmnt = 0; 253 mnts = NULL; 254 while (getmntent(mnttab, &mnt) == 0) { 255 struct mnttab *tmp_array; 256 257 if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0) 258 continue; 259 if (nmnt % MNTTAB_HUNK == 0) { 260 tmp_array = realloc(mnts, 261 (nmnt + MNTTAB_HUNK) * sizeof (*mnts)); 262 if (tmp_array == NULL) { 263 free_mnttable(mnts, nmnt); 264 return (-1); 265 } 266 mnts = tmp_array; 267 } 268 mnp = &mnts[nmnt++]; 269 270 /* 271 * Zero out any fields we're not using. 272 */ 273 (void) memset(mnp, 0, sizeof (*mnp)); 274 275 if (mnt.mnt_special != NULL) 276 mnp->mnt_special = strdup(mnt.mnt_special); 277 if (mnt.mnt_mntopts != NULL) 278 mnp->mnt_mntopts = strdup(mnt.mnt_mntopts); 279 mnp->mnt_mountp = strdup(mnt.mnt_mountp); 280 mnp->mnt_fstype = strdup(mnt.mnt_fstype); 281 if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) || 282 (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) || 283 mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) { 284 zerror(zlogp, B_TRUE, "memory allocation failed"); 285 free_mnttable(mnts, nmnt); 286 return (-1); 287 } 288 } 289 *mnt_arrayp = mnts; 290 *nelemp = nmnt; 291 return (0); 292 } 293 294 /* 295 * This is an optimization. The resolve_lofs function is used quite frequently 296 * to manipulate file paths, and on a machine with a large number of zones, 297 * there will be a huge number of mounted file systems. Thus, we trigger a 298 * reread of the list of mount points 299 */ 300 static void 301 lofs_discard_mnttab(void) 302 { 303 free_mnttable(resolve_lofs_mnts, 304 resolve_lofs_mnt_max - resolve_lofs_mnts); 305 resolve_lofs_mnts = resolve_lofs_mnt_max = NULL; 306 } 307 308 static int 309 lofs_read_mnttab(zlog_t *zlogp) 310 { 311 FILE *mnttab; 312 uint_t nmnts; 313 314 if ((mnttab = fopen(MNTTAB, "r")) == NULL) 315 return (-1); 316 if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts, 317 &nmnts) == -1) { 318 (void) fclose(mnttab); 319 return (-1); 320 } 321 (void) fclose(mnttab); 322 resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts; 323 return (0); 324 } 325 326 /* 327 * This function loops over potential loopback mounts and symlinks in a given 328 * path and resolves them all down to an absolute path. 329 */ 330 void 331 resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen) 332 { 333 int len, arlen; 334 const char *altroot; 335 char tmppath[MAXPATHLEN]; 336 boolean_t outside_altroot; 337 338 if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1) 339 return; 340 tmppath[len] = '\0'; 341 (void) strlcpy(path, tmppath, sizeof (tmppath)); 342 343 /* This happens once per zoneadmd operation. */ 344 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 345 return; 346 347 altroot = zonecfg_get_root(); 348 arlen = strlen(altroot); 349 outside_altroot = B_FALSE; 350 for (;;) { 351 struct mnttab *mnp; 352 353 /* Search in reverse order to find longest match */ 354 for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts; 355 mnp--) { 356 if (mnp->mnt_fstype == NULL || 357 mnp->mnt_mountp == NULL || 358 mnp->mnt_special == NULL) 359 continue; 360 len = strlen(mnp->mnt_mountp); 361 if (strncmp(mnp->mnt_mountp, path, len) == 0 && 362 (path[len] == '/' || path[len] == '\0')) 363 break; 364 } 365 if (mnp < resolve_lofs_mnts) 366 break; 367 /* If it's not a lofs then we're done */ 368 if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0) 369 break; 370 if (outside_altroot) { 371 char *cp; 372 int olen = sizeof (MNTOPT_RO) - 1; 373 374 /* 375 * If we run into a read-only mount outside of the 376 * alternate root environment, then the user doesn't 377 * want this path to be made read-write. 378 */ 379 if (mnp->mnt_mntopts != NULL && 380 (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) != 381 NULL && 382 (cp == mnp->mnt_mntopts || cp[-1] == ',') && 383 (cp[olen] == '\0' || cp[olen] == ',')) { 384 break; 385 } 386 } else if (arlen > 0 && 387 (strncmp(mnp->mnt_special, altroot, arlen) != 0 || 388 (mnp->mnt_special[arlen] != '\0' && 389 mnp->mnt_special[arlen] != '/'))) { 390 outside_altroot = B_TRUE; 391 } 392 /* use temporary buffer because new path might be longer */ 393 (void) snprintf(tmppath, sizeof (tmppath), "%s%s", 394 mnp->mnt_special, path + len); 395 if ((len = resolvepath(tmppath, path, pathlen)) == -1) 396 break; 397 path[len] = '\0'; 398 } 399 } 400 401 /* 402 * For a regular mount, check if a replacement lofs mount is needed because the 403 * referenced device is already mounted somewhere. 404 */ 405 static int 406 check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr) 407 { 408 struct mnttab *mnp; 409 zone_fsopt_t *optptr, *onext; 410 411 /* This happens once per zoneadmd operation. */ 412 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 413 return (-1); 414 415 /* 416 * If this special node isn't already in use, then it's ours alone; 417 * no need to worry about conflicting mounts. 418 */ 419 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 420 mnp++) { 421 if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0) 422 break; 423 } 424 if (mnp >= resolve_lofs_mnt_max) 425 return (0); 426 427 /* 428 * Convert this duplicate mount into a lofs mount. 429 */ 430 (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp, 431 sizeof (fsptr->zone_fs_special)); 432 (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS, 433 sizeof (fsptr->zone_fs_type)); 434 fsptr->zone_fs_raw[0] = '\0'; 435 436 /* 437 * Discard all but one of the original options and set that to our 438 * default set of options used for resources. 439 */ 440 optptr = fsptr->zone_fs_options; 441 if (optptr == NULL) { 442 optptr = malloc(sizeof (*optptr)); 443 if (optptr == NULL) { 444 zerror(zlogp, B_TRUE, "cannot mount %s", 445 fsptr->zone_fs_dir); 446 return (-1); 447 } 448 } else { 449 while ((onext = optptr->zone_fsopt_next) != NULL) { 450 optptr->zone_fsopt_next = onext->zone_fsopt_next; 451 free(onext); 452 } 453 } 454 (void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS); 455 optptr->zone_fsopt_next = NULL; 456 fsptr->zone_fs_options = optptr; 457 return (0); 458 } 459 460 int 461 make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode, 462 uid_t userid, gid_t groupid) 463 { 464 char path[MAXPATHLEN]; 465 struct stat st; 466 467 if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) > 468 sizeof (path)) { 469 zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix, 470 subdir); 471 return (-1); 472 } 473 474 if (lstat(path, &st) == 0) { 475 /* 476 * We don't check the file mode since presumably the zone 477 * administrator may have had good reason to change the mode, 478 * and we don't need to second guess them. 479 */ 480 if (!S_ISDIR(st.st_mode)) { 481 if (S_ISREG(st.st_mode)) { 482 /* 483 * Allow readonly mounts of /etc/ files; this 484 * is needed most by Trusted Extensions. 485 */ 486 if (strncmp(subdir, "/etc/", 487 strlen("/etc/")) != 0) { 488 zerror(zlogp, B_FALSE, 489 "%s is not in /etc", path); 490 return (-1); 491 } 492 } else { 493 zerror(zlogp, B_FALSE, 494 "%s is not a directory", path); 495 return (-1); 496 } 497 } 498 return (0); 499 } 500 501 if (mkdirp(path, mode) != 0) { 502 if (errno == EROFS) 503 zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on " 504 "a read-only file system in this local zone.\nMake " 505 "sure %s exists in the global zone.", path, subdir); 506 else 507 zerror(zlogp, B_TRUE, "mkdirp of %s failed", path); 508 return (-1); 509 } 510 511 (void) chown(path, userid, groupid); 512 return (0); 513 } 514 515 static void 516 free_remote_fstypes(char **types) 517 { 518 uint_t i; 519 520 if (types == NULL) 521 return; 522 for (i = 0; types[i] != NULL; i++) 523 free(types[i]); 524 free(types); 525 } 526 527 static char ** 528 get_remote_fstypes(zlog_t *zlogp) 529 { 530 char **types = NULL; 531 FILE *fp; 532 char buf[MAXPATHLEN]; 533 char fstype[MAXPATHLEN]; 534 uint_t lines = 0; 535 uint_t i; 536 537 if ((fp = fopen(DFSTYPES, "r")) == NULL) { 538 zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES); 539 return (NULL); 540 } 541 /* 542 * Count the number of lines 543 */ 544 while (fgets(buf, sizeof (buf), fp) != NULL) 545 lines++; 546 if (lines == 0) /* didn't read anything; empty file */ 547 goto out; 548 rewind(fp); 549 /* 550 * Allocate enough space for a NULL-terminated array. 551 */ 552 types = calloc(lines + 1, sizeof (char *)); 553 if (types == NULL) { 554 zerror(zlogp, B_TRUE, "memory allocation failed"); 555 goto out; 556 } 557 i = 0; 558 while (fgets(buf, sizeof (buf), fp) != NULL) { 559 /* LINTED - fstype is big enough to hold buf */ 560 if (sscanf(buf, "%s", fstype) == 0) { 561 zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES); 562 free_remote_fstypes(types); 563 types = NULL; 564 goto out; 565 } 566 types[i] = strdup(fstype); 567 if (types[i] == NULL) { 568 zerror(zlogp, B_TRUE, "memory allocation failed"); 569 free_remote_fstypes(types); 570 types = NULL; 571 goto out; 572 } 573 i++; 574 } 575 out: 576 (void) fclose(fp); 577 return (types); 578 } 579 580 static boolean_t 581 is_remote_fstype(const char *fstype, char *const *remote_fstypes) 582 { 583 uint_t i; 584 585 if (remote_fstypes == NULL) 586 return (B_FALSE); 587 for (i = 0; remote_fstypes[i] != NULL; i++) { 588 if (strcmp(remote_fstypes[i], fstype) == 0) 589 return (B_TRUE); 590 } 591 return (B_FALSE); 592 } 593 594 /* 595 * This converts a zone root path (normally of the form .../root) to a Live 596 * Upgrade scratch zone root (of the form .../lu). 597 */ 598 static void 599 root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved) 600 { 601 if (!isresolved && zonecfg_in_alt_root()) 602 resolve_lofs(zlogp, zroot, zrootlen); 603 (void) strcpy(strrchr(zroot, '/') + 1, "lu"); 604 } 605 606 /* 607 * The general strategy for unmounting filesystems is as follows: 608 * 609 * - Remote filesystems may be dead, and attempting to contact them as 610 * part of a regular unmount may hang forever; we want to always try to 611 * forcibly unmount such filesystems and only fall back to regular 612 * unmounts if the filesystem doesn't support forced unmounts. 613 * 614 * - We don't want to unnecessarily corrupt metadata on local 615 * filesystems (ie UFS), so we want to start off with graceful unmounts, 616 * and only escalate to doing forced unmounts if we get stuck. 617 * 618 * We start off walking backwards through the mount table. This doesn't 619 * give us strict ordering but ensures that we try to unmount submounts 620 * first. We thus limit the number of failed umount2(2) calls. 621 * 622 * The mechanism for determining if we're stuck is to count the number 623 * of failed unmounts each iteration through the mount table. This 624 * gives us an upper bound on the number of filesystems which remain 625 * mounted (autofs trigger nodes are dealt with separately). If at the 626 * end of one unmount+autofs_cleanup cycle we still have the same number 627 * of mounts that we started out with, we're stuck and try a forced 628 * unmount. If that fails (filesystem doesn't support forced unmounts) 629 * then we bail and are unable to teardown the zone. If it succeeds, 630 * we're no longer stuck so we continue with our policy of trying 631 * graceful mounts first. 632 * 633 * Zone must be down (ie, no processes or threads active). 634 */ 635 static int 636 unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd) 637 { 638 int error = 0; 639 FILE *mnttab; 640 struct mnttab *mnts; 641 uint_t nmnt; 642 char zroot[MAXPATHLEN + 1]; 643 size_t zrootlen; 644 uint_t oldcount = UINT_MAX; 645 boolean_t stuck = B_FALSE; 646 char **remote_fstypes = NULL; 647 648 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 649 zerror(zlogp, B_FALSE, "unable to determine zone root"); 650 return (-1); 651 } 652 if (unmount_cmd) 653 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 654 655 (void) strcat(zroot, "/"); 656 zrootlen = strlen(zroot); 657 658 /* 659 * For Trusted Extensions unmount each higher level zone's mount 660 * of our zone's /export/home 661 */ 662 if (!unmount_cmd) 663 tsol_unmounts(zlogp, zone_name); 664 665 if ((mnttab = fopen(MNTTAB, "r")) == NULL) { 666 zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB); 667 return (-1); 668 } 669 /* 670 * Use our hacky mntfs ioctl so we see everything, even mounts with 671 * MS_NOMNTTAB. 672 */ 673 if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 674 zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB); 675 error++; 676 goto out; 677 } 678 679 /* 680 * Build the list of remote fstypes so we know which ones we 681 * should forcibly unmount. 682 */ 683 remote_fstypes = get_remote_fstypes(zlogp); 684 for (; /* ever */; ) { 685 uint_t newcount = 0; 686 boolean_t unmounted; 687 struct mnttab *mnp; 688 char *path; 689 uint_t i; 690 691 mnts = NULL; 692 nmnt = 0; 693 /* 694 * MNTTAB gives us a way to walk through mounted 695 * filesystems; we need to be able to walk them in 696 * reverse order, so we build a list of all mounted 697 * filesystems. 698 */ 699 if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts, 700 &nmnt) != 0) { 701 error++; 702 goto out; 703 } 704 for (i = 0; i < nmnt; i++) { 705 mnp = &mnts[nmnt - i - 1]; /* access in reverse order */ 706 path = mnp->mnt_mountp; 707 unmounted = B_FALSE; 708 /* 709 * Try forced unmount first for remote filesystems. 710 * 711 * Not all remote filesystems support forced unmounts, 712 * so if this fails (ENOTSUP) we'll continue on 713 * and try a regular unmount. 714 */ 715 if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) { 716 if (umount2(path, MS_FORCE) == 0) 717 unmounted = B_TRUE; 718 } 719 /* 720 * Try forced unmount if we're stuck. 721 */ 722 if (stuck) { 723 if (umount2(path, MS_FORCE) == 0) { 724 unmounted = B_TRUE; 725 stuck = B_FALSE; 726 } else { 727 /* 728 * The first failure indicates a 729 * mount we won't be able to get 730 * rid of automatically, so we 731 * bail. 732 */ 733 error++; 734 zerror(zlogp, B_FALSE, 735 "unable to unmount '%s'", path); 736 free_mnttable(mnts, nmnt); 737 goto out; 738 } 739 } 740 /* 741 * Try regular unmounts for everything else. 742 */ 743 if (!unmounted && umount2(path, 0) != 0) 744 newcount++; 745 } 746 free_mnttable(mnts, nmnt); 747 748 if (newcount == 0) 749 break; 750 if (newcount >= oldcount) { 751 /* 752 * Last round didn't unmount anything; we're stuck and 753 * should start trying forced unmounts. 754 */ 755 stuck = B_TRUE; 756 } 757 oldcount = newcount; 758 759 /* 760 * Autofs doesn't let you unmount its trigger nodes from 761 * userland so we have to tell the kernel to cleanup for us. 762 */ 763 if (autofs_cleanup(zoneid) != 0) { 764 zerror(zlogp, B_TRUE, "unable to remove autofs nodes"); 765 error++; 766 goto out; 767 } 768 } 769 770 out: 771 free_remote_fstypes(remote_fstypes); 772 (void) fclose(mnttab); 773 return (error ? -1 : 0); 774 } 775 776 static int 777 fs_compare(const void *m1, const void *m2) 778 { 779 struct zone_fstab *i = (struct zone_fstab *)m1; 780 struct zone_fstab *j = (struct zone_fstab *)m2; 781 782 return (strcmp(i->zone_fs_dir, j->zone_fs_dir)); 783 } 784 785 /* 786 * Fork and exec (and wait for) the mentioned binary with the provided 787 * arguments. Returns (-1) if something went wrong with fork(2) or exec(2), 788 * returns the exit status otherwise. 789 * 790 * If we were unable to exec the provided pathname (for whatever 791 * reason), we return the special token ZEXIT_EXEC. The current value 792 * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the 793 * consumers of this function; any future consumers must make sure this 794 * remains the case. 795 */ 796 static int 797 forkexec(zlog_t *zlogp, const char *path, char *const argv[]) 798 { 799 pid_t child_pid; 800 int child_status = 0; 801 802 /* 803 * Do not let another thread localize a message while we are forking. 804 */ 805 (void) mutex_lock(&msglock); 806 child_pid = fork(); 807 (void) mutex_unlock(&msglock); 808 if (child_pid == -1) { 809 zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]); 810 return (-1); 811 } else if (child_pid == 0) { 812 closefrom(0); 813 /* redirect stdin, stdout & stderr to /dev/null */ 814 (void) open("/dev/null", O_RDONLY); /* stdin */ 815 (void) open("/dev/null", O_WRONLY); /* stdout */ 816 (void) open("/dev/null", O_WRONLY); /* stderr */ 817 (void) execv(path, argv); 818 /* 819 * Since we are in the child, there is no point calling zerror() 820 * since there is nobody waiting to consume it. So exit with a 821 * special code that the parent will recognize and call zerror() 822 * accordingly. 823 */ 824 825 _exit(ZEXIT_EXEC); 826 } else { 827 (void) waitpid(child_pid, &child_status, 0); 828 } 829 830 if (WIFSIGNALED(child_status)) { 831 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 832 "signal %d", path, WTERMSIG(child_status)); 833 return (-1); 834 } 835 assert(WIFEXITED(child_status)); 836 if (WEXITSTATUS(child_status) == ZEXIT_EXEC) { 837 zerror(zlogp, B_FALSE, "failed to exec %s", path); 838 return (-1); 839 } 840 return (WEXITSTATUS(child_status)); 841 } 842 843 static int 844 isregfile(const char *path) 845 { 846 struct stat64 st; 847 848 if (stat64(path, &st) == -1) 849 return (-1); 850 851 return (S_ISREG(st.st_mode)); 852 } 853 854 static int 855 dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev) 856 { 857 char cmdbuf[MAXPATHLEN]; 858 char *argv[5]; 859 int status; 860 861 /* 862 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but 863 * that would cost us an extra fork/exec without buying us anything. 864 */ 865 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype) 866 >= sizeof (cmdbuf)) { 867 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 868 return (-1); 869 } 870 871 /* 872 * If it doesn't exist, that's OK: we verified this previously 873 * in zoneadm. 874 */ 875 if (isregfile(cmdbuf) == -1) 876 return (0); 877 878 argv[0] = "fsck"; 879 argv[1] = "-o"; 880 argv[2] = "p"; 881 argv[3] = (char *)rawdev; 882 argv[4] = NULL; 883 884 status = forkexec(zlogp, cmdbuf, argv); 885 if (status == 0 || status == -1) 886 return (status); 887 zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; " 888 "run fsck manually", rawdev, status); 889 return (-1); 890 } 891 892 static int 893 domount(zlog_t *zlogp, const char *fstype, const char *opts, 894 const char *special, const char *directory) 895 { 896 char cmdbuf[MAXPATHLEN]; 897 char *argv[6]; 898 int status; 899 900 /* 901 * We could alternatively have called /usr/sbin/mount -F <fstype>, but 902 * that would cost us an extra fork/exec without buying us anything. 903 */ 904 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype) 905 >= sizeof (cmdbuf)) { 906 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 907 return (-1); 908 } 909 argv[0] = "mount"; 910 if (opts[0] == '\0') { 911 argv[1] = (char *)special; 912 argv[2] = (char *)directory; 913 argv[3] = NULL; 914 } else { 915 argv[1] = "-o"; 916 argv[2] = (char *)opts; 917 argv[3] = (char *)special; 918 argv[4] = (char *)directory; 919 argv[5] = NULL; 920 } 921 922 status = forkexec(zlogp, cmdbuf, argv); 923 if (status == 0 || status == -1) 924 return (status); 925 if (opts[0] == '\0') 926 zerror(zlogp, B_FALSE, "\"%s %s %s\" " 927 "failed with exit code %d", 928 cmdbuf, special, directory, status); 929 else 930 zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" " 931 "failed with exit code %d", 932 cmdbuf, opts, special, directory, status); 933 return (-1); 934 } 935 936 /* 937 * Check if a given mount point path exists. 938 * If it does, make sure it doesn't contain any symlinks. 939 * Note that if "leaf" is false we're checking an intermediate 940 * component of the mount point path, so it must be a directory. 941 * If "leaf" is true, then we're checking the entire mount point 942 * path, so the mount point itself can be anything aside from a 943 * symbolic link. 944 * 945 * If the path is invalid then a negative value is returned. If the 946 * path exists and is a valid mount point path then 0 is returned. 947 * If the path doesn't exist return a positive value. 948 */ 949 static int 950 valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf) 951 { 952 struct stat statbuf; 953 char respath[MAXPATHLEN]; 954 int res; 955 956 if (lstat(path, &statbuf) != 0) { 957 if (errno == ENOENT) 958 return (1); 959 zerror(zlogp, B_TRUE, "can't stat %s", path); 960 return (-1); 961 } 962 if (S_ISLNK(statbuf.st_mode)) { 963 zerror(zlogp, B_FALSE, "%s is a symlink", path); 964 return (-1); 965 } 966 if (!leaf && !S_ISDIR(statbuf.st_mode)) { 967 zerror(zlogp, B_FALSE, "%s is not a directory", path); 968 return (-1); 969 } 970 if ((res = resolvepath(path, respath, sizeof (respath))) == -1) { 971 zerror(zlogp, B_TRUE, "unable to resolve path %s", path); 972 return (-1); 973 } 974 respath[res] = '\0'; 975 if (strcmp(path, respath) != 0) { 976 /* 977 * We don't like ".."s, "."s, or "//"s throwing us off 978 */ 979 zerror(zlogp, B_FALSE, "%s is not a canonical path", path); 980 return (-1); 981 } 982 return (0); 983 } 984 985 /* 986 * Validate a mount point path. A valid mount point path is an 987 * absolute path that either doesn't exist, or, if it does exists it 988 * must be an absolute canonical path that doesn't have any symbolic 989 * links in it. The target of a mount point path can be any filesystem 990 * object. (Different filesystems can support different mount points, 991 * for example "lofs" and "mntfs" both support files and directories 992 * while "ufs" just supports directories.) 993 * 994 * If the path is invalid then a negative value is returned. If the 995 * path exists and is a valid mount point path then 0 is returned. 996 * If the path doesn't exist return a positive value. 997 */ 998 int 999 valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec, 1000 const char *dir, const char *fstype) 1001 { 1002 char abspath[MAXPATHLEN], *slashp, *slashp_next; 1003 int rv; 1004 1005 /* 1006 * Sanity check the target mount point path. 1007 * It must be a non-null string that starts with a '/'. 1008 */ 1009 if (dir[0] != '/') { 1010 /* Something went wrong. */ 1011 zerror(zlogp, B_FALSE, "invalid mount directory, " 1012 "type: \"%s\", special: \"%s\", dir: \"%s\"", 1013 fstype, spec, dir); 1014 return (-1); 1015 } 1016 1017 /* 1018 * Join rootpath and dir. Make sure abspath ends with '/', this 1019 * is added to all paths (even non-directory paths) to allow us 1020 * to detect the end of paths below. If the path already ends 1021 * in a '/', then that's ok too (although we'll fail the 1022 * cannonical path check in valid_mount_point()). 1023 */ 1024 if (snprintf(abspath, sizeof (abspath), 1025 "%s%s/", rootpath, dir) >= sizeof (abspath)) { 1026 zerror(zlogp, B_FALSE, "pathname %s%s is too long", 1027 rootpath, dir); 1028 return (-1); 1029 } 1030 1031 /* 1032 * Starting with rootpath, verify the mount path one component 1033 * at a time. Continue until we've evaluated all of abspath. 1034 */ 1035 slashp = &abspath[strlen(rootpath)]; 1036 assert(*slashp == '/'); 1037 do { 1038 slashp_next = strchr(slashp + 1, '/'); 1039 *slashp = '\0'; 1040 if (slashp_next != NULL) { 1041 /* This is an intermediary mount path component. */ 1042 rv = valid_mount_point(zlogp, abspath, B_FALSE); 1043 } else { 1044 /* This is the last component of the mount path. */ 1045 rv = valid_mount_point(zlogp, abspath, B_TRUE); 1046 } 1047 if (rv < 0) 1048 return (rv); 1049 *slashp = '/'; 1050 } while ((slashp = slashp_next) != NULL); 1051 return (rv); 1052 } 1053 1054 static int 1055 mount_one_dev_device_cb(void *arg, const char *match, const char *name) 1056 { 1057 di_prof_t prof = arg; 1058 1059 if (name == NULL) 1060 return (di_prof_add_dev(prof, match)); 1061 return (di_prof_add_map(prof, match, name)); 1062 } 1063 1064 static int 1065 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target) 1066 { 1067 di_prof_t prof = arg; 1068 1069 return (di_prof_add_symlink(prof, source, target)); 1070 } 1071 1072 int 1073 vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep) 1074 { 1075 zone_dochandle_t handle; 1076 1077 if ((handle = zonecfg_init_handle()) == NULL) { 1078 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 1079 return (-1); 1080 } 1081 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 1082 zerror(zlogp, B_FALSE, "invalid configuration"); 1083 zonecfg_fini_handle(handle); 1084 return (-1); 1085 } 1086 if (zonecfg_get_iptype(handle, iptypep) != Z_OK) { 1087 zerror(zlogp, B_FALSE, "invalid ip-type configuration"); 1088 zonecfg_fini_handle(handle); 1089 return (-1); 1090 } 1091 zonecfg_fini_handle(handle); 1092 return (0); 1093 } 1094 1095 /* 1096 * Apply the standard lists of devices/symlinks/mappings and the user-specified 1097 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will 1098 * use these as a profile/filter to determine what exists in /dev. 1099 */ 1100 static int 1101 mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd) 1102 { 1103 char brand[MAXNAMELEN]; 1104 zone_dochandle_t handle = NULL; 1105 brand_handle_t bh = NULL; 1106 struct zone_devtab ztab; 1107 di_prof_t prof = NULL; 1108 int err; 1109 int retval = -1; 1110 zone_iptype_t iptype; 1111 const char *curr_iptype; 1112 1113 if (di_prof_init(devpath, &prof)) { 1114 zerror(zlogp, B_TRUE, "failed to initialize profile"); 1115 goto cleanup; 1116 } 1117 1118 /* 1119 * Get a handle to the brand info for this zone. 1120 * If we are mounting the zone, then we must always use the default 1121 * brand device mounts. 1122 */ 1123 if (ALT_MOUNT(mount_cmd)) { 1124 (void) strlcpy(brand, default_brand, sizeof (brand)); 1125 } else { 1126 (void) strlcpy(brand, brand_name, sizeof (brand)); 1127 } 1128 1129 if ((bh = brand_open(brand)) == NULL) { 1130 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 1131 goto cleanup; 1132 } 1133 1134 if (vplat_get_iptype(zlogp, &iptype) < 0) { 1135 zerror(zlogp, B_TRUE, "unable to determine ip-type"); 1136 goto cleanup; 1137 } 1138 switch (iptype) { 1139 case ZS_SHARED: 1140 curr_iptype = "shared"; 1141 break; 1142 case ZS_EXCLUSIVE: 1143 curr_iptype = "exclusive"; 1144 break; 1145 } 1146 1147 if (brand_platform_iter_devices(bh, zone_name, 1148 mount_one_dev_device_cb, prof, curr_iptype) != 0) { 1149 zerror(zlogp, B_TRUE, "failed to add standard device"); 1150 goto cleanup; 1151 } 1152 1153 if (brand_platform_iter_link(bh, 1154 mount_one_dev_symlink_cb, prof) != 0) { 1155 zerror(zlogp, B_TRUE, "failed to add standard symlink"); 1156 goto cleanup; 1157 } 1158 1159 /* Add user-specified devices and directories */ 1160 if ((handle = zonecfg_init_handle()) == NULL) { 1161 zerror(zlogp, B_FALSE, "can't initialize zone handle"); 1162 goto cleanup; 1163 } 1164 if (err = zonecfg_get_handle(zone_name, handle)) { 1165 zerror(zlogp, B_FALSE, "can't get handle for zone " 1166 "%s: %s", zone_name, zonecfg_strerror(err)); 1167 goto cleanup; 1168 } 1169 if (err = zonecfg_setdevent(handle)) { 1170 zerror(zlogp, B_FALSE, "%s: %s", zone_name, 1171 zonecfg_strerror(err)); 1172 goto cleanup; 1173 } 1174 while (zonecfg_getdevent(handle, &ztab) == Z_OK) { 1175 if (di_prof_add_dev(prof, ztab.zone_dev_match)) { 1176 zerror(zlogp, B_TRUE, "failed to add " 1177 "user-specified device"); 1178 goto cleanup; 1179 } 1180 } 1181 (void) zonecfg_enddevent(handle); 1182 1183 /* Send profile to kernel */ 1184 if (di_prof_commit(prof)) { 1185 zerror(zlogp, B_TRUE, "failed to commit profile"); 1186 goto cleanup; 1187 } 1188 1189 retval = 0; 1190 1191 cleanup: 1192 if (bh != NULL) 1193 brand_close(bh); 1194 if (handle != NULL) 1195 zonecfg_fini_handle(handle); 1196 if (prof) 1197 di_prof_fini(prof); 1198 return (retval); 1199 } 1200 1201 static int 1202 mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath, 1203 zone_mnt_t mount_cmd) 1204 { 1205 char path[MAXPATHLEN]; 1206 char optstr[MAX_MNTOPT_STR]; 1207 zone_fsopt_t *optptr; 1208 int rv; 1209 1210 if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special, 1211 fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) { 1212 zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 1213 rootpath, fsptr->zone_fs_dir); 1214 return (-1); 1215 } else if (rv > 0) { 1216 /* The mount point path doesn't exist, create it now. */ 1217 if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir, 1218 DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 1219 DEFAULT_DIR_GROUP) != 0) { 1220 zerror(zlogp, B_FALSE, "failed to create mount point"); 1221 return (-1); 1222 } 1223 1224 /* 1225 * Now this might seem weird, but we need to invoke 1226 * valid_mount_path() again. Why? Because it checks 1227 * to make sure that the mount point path is canonical, 1228 * which it can only do if the path exists, so now that 1229 * we've created the path we have to verify it again. 1230 */ 1231 if ((rv = valid_mount_path(zlogp, rootpath, 1232 fsptr->zone_fs_special, fsptr->zone_fs_dir, 1233 fsptr->zone_fs_type)) < 0) { 1234 zerror(zlogp, B_FALSE, 1235 "%s%s is not a valid mount point", 1236 rootpath, fsptr->zone_fs_dir); 1237 return (-1); 1238 } 1239 } 1240 1241 (void) snprintf(path, sizeof (path), "%s%s", rootpath, 1242 fsptr->zone_fs_dir); 1243 1244 /* 1245 * In general the strategy here is to do just as much verification as 1246 * necessary to avoid crashing or otherwise doing something bad; if the 1247 * administrator initiated the operation via zoneadm(1m), they'll get 1248 * auto-verification which will let them know what's wrong. If they 1249 * modify the zone configuration of a running zone, and don't attempt 1250 * to verify that it's OK, then we won't crash but won't bother trying 1251 * to be too helpful either. zoneadm verify is only a couple keystrokes 1252 * away. 1253 */ 1254 if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) { 1255 zerror(zlogp, B_FALSE, "cannot mount %s on %s: " 1256 "invalid file-system type %s", fsptr->zone_fs_special, 1257 fsptr->zone_fs_dir, fsptr->zone_fs_type); 1258 return (-1); 1259 } 1260 1261 /* 1262 * If we're looking at an alternate root environment, then construct 1263 * read-only loopback mounts as necessary. Note that any special 1264 * paths for lofs zone mounts in an alternate root must have 1265 * already been pre-pended with any alternate root path by the 1266 * time we get here. 1267 */ 1268 if (zonecfg_in_alt_root()) { 1269 struct stat64 st; 1270 1271 if (stat64(fsptr->zone_fs_special, &st) != -1 && 1272 S_ISBLK(st.st_mode)) { 1273 /* 1274 * If we're going to mount a block device we need 1275 * to check if that device is already mounted 1276 * somewhere else, and if so, do a lofs mount 1277 * of the device instead of a direct mount 1278 */ 1279 if (check_lofs_needed(zlogp, fsptr) == -1) 1280 return (-1); 1281 } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) { 1282 /* 1283 * For lofs mounts, the special node is inside the 1284 * alternate root. We need lofs resolution for 1285 * this case in order to get at the underlying 1286 * read-write path. 1287 */ 1288 resolve_lofs(zlogp, fsptr->zone_fs_special, 1289 sizeof (fsptr->zone_fs_special)); 1290 } 1291 } 1292 1293 /* 1294 * Run 'fsck -m' if there's a device to fsck. 1295 */ 1296 if (fsptr->zone_fs_raw[0] != '\0' && 1297 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) { 1298 return (-1); 1299 } else if (isregfile(fsptr->zone_fs_special) == 1 && 1300 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) { 1301 return (-1); 1302 } 1303 1304 /* 1305 * Build up mount option string. 1306 */ 1307 optstr[0] = '\0'; 1308 if (fsptr->zone_fs_options != NULL) { 1309 (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt, 1310 sizeof (optstr)); 1311 for (optptr = fsptr->zone_fs_options->zone_fsopt_next; 1312 optptr != NULL; optptr = optptr->zone_fsopt_next) { 1313 (void) strlcat(optstr, ",", sizeof (optstr)); 1314 (void) strlcat(optstr, optptr->zone_fsopt_opt, 1315 sizeof (optstr)); 1316 } 1317 } 1318 1319 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr, 1320 fsptr->zone_fs_special, path)) != 0) 1321 return (rv); 1322 1323 /* 1324 * The mount succeeded. If this was not a mount of /dev then 1325 * we're done. 1326 */ 1327 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0) 1328 return (0); 1329 1330 /* 1331 * We just mounted an instance of a /dev filesystem, so now we 1332 * need to configure it. 1333 */ 1334 return (mount_one_dev(zlogp, path, mount_cmd)); 1335 } 1336 1337 static void 1338 free_fs_data(struct zone_fstab *fsarray, uint_t nelem) 1339 { 1340 uint_t i; 1341 1342 if (fsarray == NULL) 1343 return; 1344 for (i = 0; i < nelem; i++) 1345 zonecfg_free_fs_option_list(fsarray[i].zone_fs_options); 1346 free(fsarray); 1347 } 1348 1349 /* 1350 * This function initiates the creation of a small Solaris Environment for 1351 * scratch zone. The Environment creation process is split up into two 1352 * functions(build_mounted_pre_var() and build_mounted_post_var()). It 1353 * is done this way because: 1354 * We need to have both /etc and /var in the root of the scratchzone. 1355 * We loopback mount zone's own /etc and /var into the root of the 1356 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we 1357 * need to delay the mount of /var till the zone's root gets populated. 1358 * So mounting of localdirs[](/etc and /var) have been moved to the 1359 * build_mounted_post_var() which gets called only after the zone 1360 * specific filesystems are mounted. 1361 * 1362 * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE) 1363 * does not loopback mount the zone's own /etc and /var into the root of the 1364 * scratch zone. 1365 */ 1366 static boolean_t 1367 build_mounted_pre_var(zlog_t *zlogp, char *rootpath, 1368 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen) 1369 { 1370 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1371 const char **cpp; 1372 static const char *mkdirs[] = { 1373 "/system", "/system/contract", "/system/object", "/proc", 1374 "/dev", "/tmp", "/a", NULL 1375 }; 1376 char *altstr; 1377 FILE *fp; 1378 uuid_t uuid; 1379 1380 resolve_lofs(zlogp, rootpath, rootlen); 1381 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath); 1382 resolve_lofs(zlogp, luroot, lurootlen); 1383 (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot); 1384 (void) symlink("./usr/bin", tmp); 1385 1386 /* 1387 * These are mostly special mount points; not handled here. (See 1388 * zone_mount_early.) 1389 */ 1390 for (cpp = mkdirs; *cpp != NULL; cpp++) { 1391 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1392 if (mkdir(tmp, 0755) != 0) { 1393 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1394 return (B_FALSE); 1395 } 1396 } 1397 /* 1398 * This is here to support lucopy. If there's an instance of this same 1399 * zone on the current running system, then we mount its root up as 1400 * read-only inside the scratch zone. 1401 */ 1402 (void) zonecfg_get_uuid(zone_name, uuid); 1403 altstr = strdup(zonecfg_get_root()); 1404 if (altstr == NULL) { 1405 zerror(zlogp, B_TRUE, "memory allocation failed"); 1406 return (B_FALSE); 1407 } 1408 zonecfg_set_root(""); 1409 (void) strlcpy(tmp, zone_name, sizeof (tmp)); 1410 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp)); 1411 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK && 1412 strcmp(fromdir, rootpath) != 0) { 1413 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot); 1414 if (mkdir(tmp, 0755) != 0) { 1415 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1416 return (B_FALSE); 1417 } 1418 if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir, 1419 tmp) != 0) { 1420 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1421 fromdir); 1422 return (B_FALSE); 1423 } 1424 } 1425 zonecfg_set_root(altstr); 1426 free(altstr); 1427 1428 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) { 1429 zerror(zlogp, B_TRUE, "cannot open zone mapfile"); 1430 return (B_FALSE); 1431 } 1432 (void) ftruncate(fileno(fp), 0); 1433 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) { 1434 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry"); 1435 } 1436 zonecfg_close_scratch(fp); 1437 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot); 1438 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0) 1439 return (B_FALSE); 1440 (void) strlcpy(rootpath, tmp, rootlen); 1441 return (B_TRUE); 1442 } 1443 1444 1445 static boolean_t 1446 build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath, 1447 const char *luroot) 1448 { 1449 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1450 const char **cpp; 1451 const char **loopdirs; 1452 const char **tmpdirs; 1453 static const char *localdirs[] = { 1454 "/etc", "/var", NULL 1455 }; 1456 static const char *scr_loopdirs[] = { 1457 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform", 1458 "/usr", NULL 1459 }; 1460 static const char *upd_loopdirs[] = { 1461 "/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin", 1462 "/usr", "/var", NULL 1463 }; 1464 static const char *scr_tmpdirs[] = { 1465 "/tmp", "/var/run", NULL 1466 }; 1467 static const char *upd_tmpdirs[] = { 1468 "/tmp", "/var/run", "/var/tmp", NULL 1469 }; 1470 struct stat st; 1471 1472 if (mount_cmd == Z_MNT_SCRATCH) { 1473 /* 1474 * These are mounted read-write from the zone undergoing 1475 * upgrade. We must be careful not to 'leak' things from the 1476 * main system into the zone, and this accomplishes that goal. 1477 */ 1478 for (cpp = localdirs; *cpp != NULL; cpp++) { 1479 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, 1480 *cpp); 1481 (void) snprintf(fromdir, sizeof (fromdir), "%s%s", 1482 rootpath, *cpp); 1483 if (mkdir(tmp, 0755) != 0) { 1484 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1485 return (B_FALSE); 1486 } 1487 if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) 1488 != 0) { 1489 zerror(zlogp, B_TRUE, "cannot mount %s on %s", 1490 tmp, *cpp); 1491 return (B_FALSE); 1492 } 1493 } 1494 } 1495 1496 if (mount_cmd == Z_MNT_UPDATE) 1497 loopdirs = upd_loopdirs; 1498 else 1499 loopdirs = scr_loopdirs; 1500 1501 /* 1502 * These are things mounted read-only from the running system because 1503 * they contain binaries that must match system. 1504 */ 1505 for (cpp = loopdirs; *cpp != NULL; cpp++) { 1506 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1507 if (mkdir(tmp, 0755) != 0) { 1508 if (errno != EEXIST) { 1509 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1510 return (B_FALSE); 1511 } 1512 if (lstat(tmp, &st) != 0) { 1513 zerror(zlogp, B_TRUE, "cannot stat %s", tmp); 1514 return (B_FALSE); 1515 } 1516 /* 1517 * Ignore any non-directories encountered. These are 1518 * things that have been converted into symlinks 1519 * (/etc/fs and /etc/lib) and no longer need a lofs 1520 * fixup. 1521 */ 1522 if (!S_ISDIR(st.st_mode)) 1523 continue; 1524 } 1525 if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp, 1526 tmp) != 0) { 1527 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1528 *cpp); 1529 return (B_FALSE); 1530 } 1531 } 1532 1533 if (mount_cmd == Z_MNT_UPDATE) 1534 tmpdirs = upd_tmpdirs; 1535 else 1536 tmpdirs = scr_tmpdirs; 1537 1538 /* 1539 * These are things with tmpfs mounted inside. 1540 */ 1541 for (cpp = tmpdirs; *cpp != NULL; cpp++) { 1542 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1543 if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 && 1544 errno != EEXIST) { 1545 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1546 return (B_FALSE); 1547 } 1548 1549 /* 1550 * We could set the mode for /tmp when we do the mkdir but 1551 * since that can be modified by the umask we will just set 1552 * the correct mode for /tmp now. 1553 */ 1554 if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) { 1555 zerror(zlogp, B_TRUE, "cannot chmod %s", tmp); 1556 return (B_FALSE); 1557 } 1558 1559 if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) { 1560 zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp); 1561 return (B_FALSE); 1562 } 1563 } 1564 return (B_TRUE); 1565 } 1566 1567 typedef struct plat_gmount_cb_data { 1568 zlog_t *pgcd_zlogp; 1569 struct zone_fstab **pgcd_fs_tab; 1570 int *pgcd_num_fs; 1571 } plat_gmount_cb_data_t; 1572 1573 /* 1574 * plat_gmount_cb() is a callback function invoked by libbrand to iterate 1575 * through all global brand platform mounts. 1576 */ 1577 int 1578 plat_gmount_cb(void *data, const char *spec, const char *dir, 1579 const char *fstype, const char *opt) 1580 { 1581 plat_gmount_cb_data_t *cp = data; 1582 zlog_t *zlogp = cp->pgcd_zlogp; 1583 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab; 1584 int num_fs = *cp->pgcd_num_fs; 1585 struct zone_fstab *fsp, *tmp_ptr; 1586 1587 num_fs++; 1588 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) { 1589 zerror(zlogp, B_TRUE, "memory allocation failed"); 1590 return (-1); 1591 } 1592 1593 fs_ptr = tmp_ptr; 1594 fsp = &fs_ptr[num_fs - 1]; 1595 1596 /* update the callback struct passed in */ 1597 *cp->pgcd_fs_tab = fs_ptr; 1598 *cp->pgcd_num_fs = num_fs; 1599 1600 fsp->zone_fs_raw[0] = '\0'; 1601 (void) strlcpy(fsp->zone_fs_special, spec, 1602 sizeof (fsp->zone_fs_special)); 1603 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir)); 1604 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type)); 1605 fsp->zone_fs_options = NULL; 1606 if ((opt != NULL) && 1607 (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) { 1608 zerror(zlogp, B_FALSE, "error adding property"); 1609 return (-1); 1610 } 1611 1612 return (0); 1613 } 1614 1615 static int 1616 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp, 1617 struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd) 1618 { 1619 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab; 1620 int num_fs; 1621 1622 num_fs = *num_fsp; 1623 fs_ptr = *fs_tabp; 1624 1625 if (zonecfg_setfsent(handle) != Z_OK) { 1626 zerror(zlogp, B_FALSE, "invalid configuration"); 1627 return (-1); 1628 } 1629 while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 1630 /* 1631 * ZFS filesystems will not be accessible under an alternate 1632 * root, since the pool will not be known. Ignore them in this 1633 * case. 1634 */ 1635 if (ALT_MOUNT(mount_cmd) && 1636 strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0) 1637 continue; 1638 1639 num_fs++; 1640 if ((tmp_ptr = realloc(fs_ptr, 1641 num_fs * sizeof (*tmp_ptr))) == NULL) { 1642 zerror(zlogp, B_TRUE, "memory allocation failed"); 1643 (void) zonecfg_endfsent(handle); 1644 return (-1); 1645 } 1646 /* update the pointers passed in */ 1647 *fs_tabp = tmp_ptr; 1648 *num_fsp = num_fs; 1649 1650 fs_ptr = tmp_ptr; 1651 fsp = &fs_ptr[num_fs - 1]; 1652 (void) strlcpy(fsp->zone_fs_dir, 1653 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 1654 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw, 1655 sizeof (fsp->zone_fs_raw)); 1656 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type, 1657 sizeof (fsp->zone_fs_type)); 1658 fsp->zone_fs_options = fstab.zone_fs_options; 1659 1660 /* 1661 * For all lofs mounts, make sure that the 'special' 1662 * entry points inside the alternate root. The 1663 * source path for a lofs mount in a given zone needs 1664 * to be relative to the root of the boot environment 1665 * that contains the zone. Note that we don't do this 1666 * for non-lofs mounts since they will have a device 1667 * as a backing store and device paths must always be 1668 * specified relative to the current boot environment. 1669 */ 1670 fsp->zone_fs_special[0] = '\0'; 1671 if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) { 1672 (void) strlcat(fsp->zone_fs_special, zonecfg_get_root(), 1673 sizeof (fsp->zone_fs_special)); 1674 } 1675 (void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special, 1676 sizeof (fsp->zone_fs_special)); 1677 } 1678 (void) zonecfg_endfsent(handle); 1679 return (0); 1680 } 1681 1682 static int 1683 mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd) 1684 { 1685 char rootpath[MAXPATHLEN]; 1686 char zonepath[MAXPATHLEN]; 1687 char brand[MAXNAMELEN]; 1688 char luroot[MAXPATHLEN]; 1689 int i, num_fs = 0; 1690 struct zone_fstab *fs_ptr = NULL; 1691 zone_dochandle_t handle = NULL; 1692 zone_state_t zstate; 1693 brand_handle_t bh; 1694 plat_gmount_cb_data_t cb; 1695 1696 if (zone_get_state(zone_name, &zstate) != Z_OK || 1697 (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) { 1698 zerror(zlogp, B_FALSE, 1699 "zone must be in '%s' or '%s' state to mount file-systems", 1700 zone_state_str(ZONE_STATE_READY), 1701 zone_state_str(ZONE_STATE_MOUNTED)); 1702 goto bad; 1703 } 1704 1705 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 1706 zerror(zlogp, B_TRUE, "unable to determine zone path"); 1707 goto bad; 1708 } 1709 1710 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 1711 zerror(zlogp, B_TRUE, "unable to determine zone root"); 1712 goto bad; 1713 } 1714 1715 if ((handle = zonecfg_init_handle()) == NULL) { 1716 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 1717 goto bad; 1718 } 1719 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK || 1720 zonecfg_setfsent(handle) != Z_OK) { 1721 zerror(zlogp, B_FALSE, "invalid configuration"); 1722 goto bad; 1723 } 1724 1725 /* 1726 * If we are mounting the zone, then we must always use the default 1727 * brand global mounts. 1728 */ 1729 if (ALT_MOUNT(mount_cmd)) { 1730 (void) strlcpy(brand, default_brand, sizeof (brand)); 1731 } else { 1732 (void) strlcpy(brand, brand_name, sizeof (brand)); 1733 } 1734 1735 /* Get a handle to the brand info for this zone */ 1736 if ((bh = brand_open(brand)) == NULL) { 1737 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 1738 zonecfg_fini_handle(handle); 1739 return (-1); 1740 } 1741 1742 /* 1743 * Get the list of global filesystems to mount from the brand 1744 * configuration. 1745 */ 1746 cb.pgcd_zlogp = zlogp; 1747 cb.pgcd_fs_tab = &fs_ptr; 1748 cb.pgcd_num_fs = &num_fs; 1749 if (brand_platform_iter_gmounts(bh, zone_name, zonepath, 1750 plat_gmount_cb, &cb) != 0) { 1751 zerror(zlogp, B_FALSE, "unable to mount filesystems"); 1752 brand_close(bh); 1753 zonecfg_fini_handle(handle); 1754 return (-1); 1755 } 1756 brand_close(bh); 1757 1758 /* 1759 * Iterate through the rest of the filesystems. Sort them all, 1760 * then mount them in sorted order. This is to make sure the 1761 * higher level directories (e.g., /usr) get mounted before 1762 * any beneath them (e.g., /usr/local). 1763 */ 1764 if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs, 1765 mount_cmd) != 0) 1766 goto bad; 1767 1768 zonecfg_fini_handle(handle); 1769 handle = NULL; 1770 1771 /* 1772 * Normally when we mount a zone all the zone filesystems 1773 * get mounted relative to rootpath, which is usually 1774 * <zonepath>/root. But when mounting a zone for administration 1775 * purposes via the zone "mount" state, build_mounted_pre_var() 1776 * updates rootpath to be <zonepath>/lu/a so we'll mount all 1777 * the zones filesystems there instead. 1778 * 1779 * build_mounted_pre_var() and build_mounted_post_var() will 1780 * also do some extra work to create directories and lofs mount 1781 * a bunch of global zone file system paths into <zonepath>/lu. 1782 * 1783 * This allows us to be able to enter the zone (now rooted at 1784 * <zonepath>/lu) and run the upgrade/patch tools that are in the 1785 * global zone and have them upgrade the to-be-modified zone's 1786 * files mounted on /a. (Which mirrors the existing standard 1787 * upgrade environment.) 1788 * 1789 * There is of course one catch. When doing the upgrade 1790 * we need <zoneroot>/lu/dev to be the /dev filesystem 1791 * for the zone and we don't want to have any /dev filesystem 1792 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified 1793 * as a normal zone filesystem by default we'll try to mount 1794 * it at <zoneroot>/lu/a/dev, so we have to detect this 1795 * case and instead mount it at <zoneroot>/lu/dev. 1796 * 1797 * All this work is done in three phases: 1798 * 1) Create and populate lu directory (build_mounted_pre_var()). 1799 * 2) Mount the required filesystems as per the zone configuration. 1800 * 3) Set up the rest of the scratch zone environment 1801 * (build_mounted_post_var()). 1802 */ 1803 if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp, 1804 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot))) 1805 goto bad; 1806 1807 qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare); 1808 1809 for (i = 0; i < num_fs; i++) { 1810 if (ALT_MOUNT(mount_cmd) && 1811 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) { 1812 size_t slen = strlen(rootpath) - 2; 1813 1814 /* 1815 * By default we'll try to mount /dev as /a/dev 1816 * but /dev is special and always goes at the top 1817 * so strip the trailing '/a' from the rootpath. 1818 */ 1819 assert(strcmp(&rootpath[slen], "/a") == 0); 1820 rootpath[slen] = '\0'; 1821 if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) 1822 != 0) 1823 goto bad; 1824 rootpath[slen] = '/'; 1825 continue; 1826 } 1827 if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0) 1828 goto bad; 1829 } 1830 if (ALT_MOUNT(mount_cmd) && 1831 !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot)) 1832 goto bad; 1833 1834 /* 1835 * For Trusted Extensions cross-mount each lower level /export/home 1836 */ 1837 if (mount_cmd == Z_MNT_BOOT && 1838 tsol_mounts(zlogp, zone_name, rootpath) != 0) 1839 goto bad; 1840 1841 free_fs_data(fs_ptr, num_fs); 1842 1843 /* 1844 * Everything looks fine. 1845 */ 1846 return (0); 1847 1848 bad: 1849 if (handle != NULL) 1850 zonecfg_fini_handle(handle); 1851 free_fs_data(fs_ptr, num_fs); 1852 return (-1); 1853 } 1854 1855 /* caller makes sure neither parameter is NULL */ 1856 static int 1857 addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr) 1858 { 1859 int prefixlen; 1860 1861 prefixlen = atoi(prefixstr); 1862 if (prefixlen < 0 || prefixlen > maxprefixlen) 1863 return (1); 1864 while (prefixlen > 0) { 1865 if (prefixlen >= 8) { 1866 *maskstr++ = 0xFF; 1867 prefixlen -= 8; 1868 continue; 1869 } 1870 *maskstr |= 1 << (8 - prefixlen); 1871 prefixlen--; 1872 } 1873 return (0); 1874 } 1875 1876 /* 1877 * Tear down all interfaces belonging to the given zone. This should 1878 * be called with the zone in a state other than "running", so that 1879 * interfaces can't be assigned to the zone after this returns. 1880 * 1881 * If anything goes wrong, log an error message and return an error. 1882 */ 1883 static int 1884 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id) 1885 { 1886 struct lifnum lifn; 1887 struct lifconf lifc; 1888 struct lifreq *lifrp, lifrl; 1889 int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES; 1890 int num_ifs, s, i, ret_code = 0; 1891 uint_t bufsize; 1892 char *buf = NULL; 1893 1894 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 1895 zerror(zlogp, B_TRUE, "could not get socket"); 1896 ret_code = -1; 1897 goto bad; 1898 } 1899 lifn.lifn_family = AF_UNSPEC; 1900 lifn.lifn_flags = (int)lifc_flags; 1901 if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) { 1902 zerror(zlogp, B_TRUE, 1903 "could not determine number of network interfaces"); 1904 ret_code = -1; 1905 goto bad; 1906 } 1907 num_ifs = lifn.lifn_count; 1908 bufsize = num_ifs * sizeof (struct lifreq); 1909 if ((buf = malloc(bufsize)) == NULL) { 1910 zerror(zlogp, B_TRUE, "memory allocation failed"); 1911 ret_code = -1; 1912 goto bad; 1913 } 1914 lifc.lifc_family = AF_UNSPEC; 1915 lifc.lifc_flags = (int)lifc_flags; 1916 lifc.lifc_len = bufsize; 1917 lifc.lifc_buf = buf; 1918 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 1919 zerror(zlogp, B_TRUE, "could not get configured network " 1920 "interfaces"); 1921 ret_code = -1; 1922 goto bad; 1923 } 1924 lifrp = lifc.lifc_req; 1925 for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) { 1926 (void) close(s); 1927 if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) < 1928 0) { 1929 zerror(zlogp, B_TRUE, "%s: could not get socket", 1930 lifrl.lifr_name); 1931 ret_code = -1; 1932 continue; 1933 } 1934 (void) memset(&lifrl, 0, sizeof (lifrl)); 1935 (void) strncpy(lifrl.lifr_name, lifrp->lifr_name, 1936 sizeof (lifrl.lifr_name)); 1937 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) { 1938 if (errno == ENXIO) 1939 /* 1940 * Interface may have been removed by admin or 1941 * another zone halting. 1942 */ 1943 continue; 1944 zerror(zlogp, B_TRUE, 1945 "%s: could not determine the zone to which this " 1946 "network interface is bound", lifrl.lifr_name); 1947 ret_code = -1; 1948 continue; 1949 } 1950 if (lifrl.lifr_zoneid == zone_id) { 1951 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) { 1952 zerror(zlogp, B_TRUE, 1953 "%s: could not remove network interface", 1954 lifrl.lifr_name); 1955 ret_code = -1; 1956 continue; 1957 } 1958 } 1959 } 1960 bad: 1961 if (s > 0) 1962 (void) close(s); 1963 if (buf) 1964 free(buf); 1965 return (ret_code); 1966 } 1967 1968 static union sockunion { 1969 struct sockaddr sa; 1970 struct sockaddr_in sin; 1971 struct sockaddr_dl sdl; 1972 struct sockaddr_in6 sin6; 1973 } so_dst, so_ifp; 1974 1975 static struct { 1976 struct rt_msghdr hdr; 1977 char space[512]; 1978 } rtmsg; 1979 1980 static int 1981 salen(struct sockaddr *sa) 1982 { 1983 switch (sa->sa_family) { 1984 case AF_INET: 1985 return (sizeof (struct sockaddr_in)); 1986 case AF_LINK: 1987 return (sizeof (struct sockaddr_dl)); 1988 case AF_INET6: 1989 return (sizeof (struct sockaddr_in6)); 1990 default: 1991 return (sizeof (struct sockaddr)); 1992 } 1993 } 1994 1995 #define ROUNDUP_LONG(a) \ 1996 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 1997 1998 /* 1999 * Look up which zone is using a given IP address. The address in question 2000 * is expected to have been stuffed into the structure to which lifr points 2001 * via a previous SIOCGLIFADDR ioctl(). 2002 * 2003 * This is done using black router socket magic. 2004 * 2005 * Return the name of the zone on success or NULL on failure. 2006 * 2007 * This is a lot of code for a simple task; a new ioctl request to take care 2008 * of this might be a useful RFE. 2009 */ 2010 2011 static char * 2012 who_is_using(zlog_t *zlogp, struct lifreq *lifr) 2013 { 2014 static char answer[ZONENAME_MAX]; 2015 pid_t pid; 2016 int s, rlen, l, i; 2017 char *cp = rtmsg.space; 2018 struct sockaddr_dl *ifp = NULL; 2019 struct sockaddr *sa; 2020 char save_if_name[LIFNAMSIZ]; 2021 2022 answer[0] = '\0'; 2023 2024 pid = getpid(); 2025 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 2026 zerror(zlogp, B_TRUE, "could not get routing socket"); 2027 return (NULL); 2028 } 2029 2030 if (lifr->lifr_addr.ss_family == AF_INET) { 2031 struct sockaddr_in *sin4; 2032 2033 so_dst.sa.sa_family = AF_INET; 2034 sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 2035 so_dst.sin.sin_addr = sin4->sin_addr; 2036 } else { 2037 struct sockaddr_in6 *sin6; 2038 2039 so_dst.sa.sa_family = AF_INET6; 2040 sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 2041 so_dst.sin6.sin6_addr = sin6->sin6_addr; 2042 } 2043 2044 so_ifp.sa.sa_family = AF_LINK; 2045 2046 (void) memset(&rtmsg, 0, sizeof (rtmsg)); 2047 rtmsg.hdr.rtm_type = RTM_GET; 2048 rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST; 2049 rtmsg.hdr.rtm_version = RTM_VERSION; 2050 rtmsg.hdr.rtm_seq = ++rts_seqno; 2051 rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST; 2052 2053 l = ROUNDUP_LONG(salen(&so_dst.sa)); 2054 (void) memmove(cp, &(so_dst), l); 2055 cp += l; 2056 l = ROUNDUP_LONG(salen(&so_ifp.sa)); 2057 (void) memmove(cp, &(so_ifp), l); 2058 cp += l; 2059 2060 rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg; 2061 2062 if ((rlen = write(s, &rtmsg, l)) < 0) { 2063 zerror(zlogp, B_TRUE, "writing to routing socket"); 2064 return (NULL); 2065 } else if (rlen < (int)rtmsg.hdr.rtm_msglen) { 2066 zerror(zlogp, B_TRUE, 2067 "write to routing socket got only %d for len\n", rlen); 2068 return (NULL); 2069 } 2070 do { 2071 l = read(s, &rtmsg, sizeof (rtmsg)); 2072 } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno || 2073 rtmsg.hdr.rtm_pid != pid)); 2074 if (l < 0) { 2075 zerror(zlogp, B_TRUE, "reading from routing socket"); 2076 return (NULL); 2077 } 2078 2079 if (rtmsg.hdr.rtm_version != RTM_VERSION) { 2080 zerror(zlogp, B_FALSE, 2081 "routing message version %d not understood", 2082 rtmsg.hdr.rtm_version); 2083 return (NULL); 2084 } 2085 if (rtmsg.hdr.rtm_msglen != (ushort_t)l) { 2086 zerror(zlogp, B_FALSE, "message length mismatch, " 2087 "expected %d bytes, returned %d bytes", 2088 rtmsg.hdr.rtm_msglen, l); 2089 return (NULL); 2090 } 2091 if (rtmsg.hdr.rtm_errno != 0) { 2092 errno = rtmsg.hdr.rtm_errno; 2093 zerror(zlogp, B_TRUE, "RTM_GET routing socket message"); 2094 return (NULL); 2095 } 2096 if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) { 2097 zerror(zlogp, B_FALSE, "network interface not found"); 2098 return (NULL); 2099 } 2100 cp = ((char *)(&rtmsg.hdr + 1)); 2101 for (i = 1; i != 0; i <<= 1) { 2102 /* LINTED E_BAD_PTR_CAST_ALIGN */ 2103 sa = (struct sockaddr *)cp; 2104 if (i != RTA_IFP) { 2105 if ((i & rtmsg.hdr.rtm_addrs) != 0) 2106 cp += ROUNDUP_LONG(salen(sa)); 2107 continue; 2108 } 2109 if (sa->sa_family == AF_LINK && 2110 ((struct sockaddr_dl *)sa)->sdl_nlen != 0) 2111 ifp = (struct sockaddr_dl *)sa; 2112 break; 2113 } 2114 if (ifp == NULL) { 2115 zerror(zlogp, B_FALSE, "network interface could not be " 2116 "determined"); 2117 return (NULL); 2118 } 2119 2120 /* 2121 * We need to set the I/F name to what we got above, then do the 2122 * appropriate ioctl to get its zone name. But lifr->lifr_name is 2123 * used by the calling function to do a REMOVEIF, so if we leave the 2124 * "good" zone's I/F name in place, *that* I/F will be removed instead 2125 * of the bad one. So we save the old (bad) I/F name before over- 2126 * writing it and doing the ioctl, then restore it after the ioctl. 2127 */ 2128 (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name)); 2129 (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen); 2130 lifr->lifr_name[ifp->sdl_nlen] = '\0'; 2131 i = ioctl(s, SIOCGLIFZONE, lifr); 2132 (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name)); 2133 if (i < 0) { 2134 zerror(zlogp, B_TRUE, 2135 "%s: could not determine the zone network interface " 2136 "belongs to", lifr->lifr_name); 2137 return (NULL); 2138 } 2139 if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0) 2140 (void) snprintf(answer, sizeof (answer), "%d", 2141 lifr->lifr_zoneid); 2142 2143 if (strlen(answer) > 0) 2144 return (answer); 2145 return (NULL); 2146 } 2147 2148 /* 2149 * Configures a single interface: a new virtual interface is added, based on 2150 * the physical interface nwiftabptr->zone_nwif_physical, with the address 2151 * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that 2152 * the "address" can be an IPv6 address (with a /prefixlength required), an 2153 * IPv4 address (with a /prefixlength optional), or a name; for the latter, 2154 * an IPv4 name-to-address resolution will be attempted. 2155 * 2156 * If anything goes wrong, we log an detailed error message, attempt to tear 2157 * down whatever we set up and return an error. 2158 */ 2159 static int 2160 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id, 2161 struct zone_nwiftab *nwiftabptr) 2162 { 2163 struct lifreq lifr; 2164 struct sockaddr_in netmask4; 2165 struct sockaddr_in6 netmask6; 2166 struct sockaddr_storage laddr; 2167 struct in_addr in4; 2168 sa_family_t af; 2169 char *slashp = strchr(nwiftabptr->zone_nwif_address, '/'); 2170 int s; 2171 boolean_t got_netmask = B_FALSE; 2172 boolean_t is_loopback = B_FALSE; 2173 char addrstr4[INET_ADDRSTRLEN]; 2174 int res; 2175 2176 res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr); 2177 if (res != Z_OK) { 2178 zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res), 2179 nwiftabptr->zone_nwif_address); 2180 return (-1); 2181 } 2182 af = lifr.lifr_addr.ss_family; 2183 if (af == AF_INET) 2184 in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr; 2185 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 2186 zerror(zlogp, B_TRUE, "could not get socket"); 2187 return (-1); 2188 } 2189 2190 /* 2191 * This is a similar kind of "hack" like in addif() to get around 2192 * the problem of SIOCLIFADDIF. The problem is that this ioctl 2193 * does not include the netmask when adding a logical interface. 2194 * To get around this problem, we first add the logical interface 2195 * with a 0 address. After that, we set the netmask if provided. 2196 * Finally we set the interface address. 2197 */ 2198 laddr = lifr.lifr_addr; 2199 (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical, 2200 sizeof (lifr.lifr_name)); 2201 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr)); 2202 2203 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) { 2204 /* 2205 * Here, we know that the interface can't be brought up. 2206 * A similar warning message was already printed out to 2207 * the console by zoneadm(1M) so instead we log the 2208 * message to syslog and continue. 2209 */ 2210 zerror(&logsys, B_TRUE, "WARNING: skipping network interface " 2211 "'%s' which may not be present/plumbed in the " 2212 "global zone.", lifr.lifr_name); 2213 (void) close(s); 2214 return (Z_OK); 2215 } 2216 2217 /* Preserve literal IPv4 address for later potential printing. */ 2218 if (af == AF_INET) 2219 (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN); 2220 2221 lifr.lifr_zoneid = zone_id; 2222 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) { 2223 zerror(zlogp, B_TRUE, "%s: could not place network interface " 2224 "into zone", lifr.lifr_name); 2225 goto bad; 2226 } 2227 2228 /* 2229 * Loopback interface will use the default netmask assigned, if no 2230 * netmask is found. 2231 */ 2232 if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) { 2233 is_loopback = B_TRUE; 2234 } 2235 if (af == AF_INET) { 2236 /* 2237 * The IPv4 netmask can be determined either 2238 * directly if a prefix length was supplied with 2239 * the address or via the netmasks database. Not 2240 * being able to determine it is a common failure, 2241 * but it often is not fatal to operation of the 2242 * interface. In that case, a warning will be 2243 * printed after the rest of the interface's 2244 * parameters have been configured. 2245 */ 2246 (void) memset(&netmask4, 0, sizeof (netmask4)); 2247 if (slashp != NULL) { 2248 if (addr2netmask(slashp + 1, V4_ADDR_LEN, 2249 (uchar_t *)&netmask4.sin_addr) != 0) { 2250 *slashp = '/'; 2251 zerror(zlogp, B_FALSE, 2252 "%s: invalid prefix length in %s", 2253 lifr.lifr_name, 2254 nwiftabptr->zone_nwif_address); 2255 goto bad; 2256 } 2257 got_netmask = B_TRUE; 2258 } else if (getnetmaskbyaddr(in4, 2259 &netmask4.sin_addr) == 0) { 2260 got_netmask = B_TRUE; 2261 } 2262 if (got_netmask) { 2263 netmask4.sin_family = af; 2264 (void) memcpy(&lifr.lifr_addr, &netmask4, 2265 sizeof (netmask4)); 2266 } 2267 } else { 2268 (void) memset(&netmask6, 0, sizeof (netmask6)); 2269 if (addr2netmask(slashp + 1, V6_ADDR_LEN, 2270 (uchar_t *)&netmask6.sin6_addr) != 0) { 2271 *slashp = '/'; 2272 zerror(zlogp, B_FALSE, 2273 "%s: invalid prefix length in %s", 2274 lifr.lifr_name, 2275 nwiftabptr->zone_nwif_address); 2276 goto bad; 2277 } 2278 got_netmask = B_TRUE; 2279 netmask6.sin6_family = af; 2280 (void) memcpy(&lifr.lifr_addr, &netmask6, 2281 sizeof (netmask6)); 2282 } 2283 if (got_netmask && 2284 ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) { 2285 zerror(zlogp, B_TRUE, "%s: could not set netmask", 2286 lifr.lifr_name); 2287 goto bad; 2288 } 2289 2290 /* Set the interface address */ 2291 lifr.lifr_addr = laddr; 2292 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 2293 zerror(zlogp, B_TRUE, 2294 "%s: could not set IP address to %s", 2295 lifr.lifr_name, nwiftabptr->zone_nwif_address); 2296 goto bad; 2297 } 2298 2299 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2300 zerror(zlogp, B_TRUE, "%s: could not get flags", 2301 lifr.lifr_name); 2302 goto bad; 2303 } 2304 lifr.lifr_flags |= IFF_UP; 2305 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) { 2306 int save_errno = errno; 2307 char *zone_using; 2308 2309 /* 2310 * If we failed with something other than EADDRNOTAVAIL, 2311 * then skip to the end. Otherwise, look up our address, 2312 * then call a function to determine which zone is already 2313 * using that address. 2314 */ 2315 if (errno != EADDRNOTAVAIL) { 2316 zerror(zlogp, B_TRUE, 2317 "%s: could not bring network interface up", 2318 lifr.lifr_name); 2319 goto bad; 2320 } 2321 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 2322 zerror(zlogp, B_TRUE, "%s: could not get address", 2323 lifr.lifr_name); 2324 goto bad; 2325 } 2326 zone_using = who_is_using(zlogp, &lifr); 2327 errno = save_errno; 2328 if (zone_using == NULL) 2329 zerror(zlogp, B_TRUE, 2330 "%s: could not bring network interface up", 2331 lifr.lifr_name); 2332 else 2333 zerror(zlogp, B_TRUE, "%s: could not bring network " 2334 "interface up: address in use by zone '%s'", 2335 lifr.lifr_name, zone_using); 2336 goto bad; 2337 } 2338 2339 if (!got_netmask && !is_loopback) { 2340 /* 2341 * A common, but often non-fatal problem, is that the system 2342 * cannot find the netmask for an interface address. This is 2343 * often caused by it being only in /etc/inet/netmasks, but 2344 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not 2345 * in that. This doesn't show up at boot because the netmask 2346 * is obtained from /etc/inet/netmasks when no network 2347 * interfaces are up, but isn't consulted when NIS/NIS+ is 2348 * available. We warn the user here that something like this 2349 * has happened and we're just running with a default and 2350 * possible incorrect netmask. 2351 */ 2352 char buffer[INET6_ADDRSTRLEN]; 2353 void *addr; 2354 const char *nomatch = "no matching subnet found in netmasks(4)"; 2355 2356 if (af == AF_INET) 2357 addr = &((struct sockaddr_in *) 2358 (&lifr.lifr_addr))->sin_addr; 2359 else 2360 addr = &((struct sockaddr_in6 *) 2361 (&lifr.lifr_addr))->sin6_addr; 2362 2363 /* 2364 * Find out what netmask the interface is going to be using. 2365 * If we just brought up an IPMP data address on an underlying 2366 * interface above, the address will have already migrated, so 2367 * the SIOCGLIFNETMASK won't be able to find it (but we need 2368 * to bring the address up to get the actual netmask). Just 2369 * omit printing the actual netmask in this corner-case. 2370 */ 2371 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 || 2372 inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) { 2373 zerror(zlogp, B_FALSE, "WARNING: %s; using default.", 2374 nomatch); 2375 } else { 2376 zerror(zlogp, B_FALSE, 2377 "WARNING: %s: %s: %s; using default of %s.", 2378 lifr.lifr_name, nomatch, addrstr4, buffer); 2379 } 2380 } 2381 2382 /* 2383 * If a default router was specified for this interface 2384 * set the route now. Ignore if already set. 2385 */ 2386 if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) { 2387 int status; 2388 char *argv[7]; 2389 2390 argv[0] = "route"; 2391 argv[1] = "add"; 2392 argv[2] = "-ifp"; 2393 argv[3] = nwiftabptr->zone_nwif_physical; 2394 argv[4] = "default"; 2395 argv[5] = nwiftabptr->zone_nwif_defrouter; 2396 argv[6] = NULL; 2397 2398 status = forkexec(zlogp, "/usr/sbin/route", argv); 2399 if (status != 0 && status != EEXIST) 2400 zerror(zlogp, B_FALSE, "Unable to set route for " 2401 "interface %s to %s\n", 2402 nwiftabptr->zone_nwif_physical, 2403 nwiftabptr->zone_nwif_defrouter); 2404 } 2405 2406 (void) close(s); 2407 return (Z_OK); 2408 bad: 2409 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 2410 (void) close(s); 2411 return (-1); 2412 } 2413 2414 /* 2415 * Sets up network interfaces based on information from the zone configuration. 2416 * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global 2417 * system. 2418 * 2419 * If anything goes wrong, we log a general error message, attempt to tear down 2420 * whatever we set up, and return an error. 2421 */ 2422 static int 2423 configure_shared_network_interfaces(zlog_t *zlogp) 2424 { 2425 zone_dochandle_t handle; 2426 struct zone_nwiftab nwiftab, loopback_iftab; 2427 zoneid_t zoneid; 2428 2429 if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) { 2430 zerror(zlogp, B_TRUE, "unable to get zoneid"); 2431 return (-1); 2432 } 2433 2434 if ((handle = zonecfg_init_handle()) == NULL) { 2435 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2436 return (-1); 2437 } 2438 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2439 zerror(zlogp, B_FALSE, "invalid configuration"); 2440 zonecfg_fini_handle(handle); 2441 return (-1); 2442 } 2443 if (zonecfg_setnwifent(handle) == Z_OK) { 2444 for (;;) { 2445 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 2446 break; 2447 if (configure_one_interface(zlogp, zoneid, &nwiftab) != 2448 Z_OK) { 2449 (void) zonecfg_endnwifent(handle); 2450 zonecfg_fini_handle(handle); 2451 return (-1); 2452 } 2453 } 2454 (void) zonecfg_endnwifent(handle); 2455 } 2456 zonecfg_fini_handle(handle); 2457 if (is_system_labeled()) { 2458 /* 2459 * Labeled zones share the loopback interface 2460 * so it is not plumbed for shared stack instances. 2461 */ 2462 return (0); 2463 } 2464 (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0", 2465 sizeof (loopback_iftab.zone_nwif_physical)); 2466 (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1", 2467 sizeof (loopback_iftab.zone_nwif_address)); 2468 loopback_iftab.zone_nwif_defrouter[0] = '\0'; 2469 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK) 2470 return (-1); 2471 2472 /* Always plumb up the IPv6 loopback interface. */ 2473 (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128", 2474 sizeof (loopback_iftab.zone_nwif_address)); 2475 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK) 2476 return (-1); 2477 return (0); 2478 } 2479 2480 static void 2481 zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str) 2482 { 2483 char errmsg[DLADM_STRSIZE]; 2484 2485 (void) dladm_status2str(err, errmsg); 2486 zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg); 2487 } 2488 2489 static int 2490 add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname) 2491 { 2492 dladm_status_t err; 2493 boolean_t cpuset, poolset; 2494 char *poolp; 2495 2496 /* First check if it's in use by global zone. */ 2497 if (zonecfg_ifname_exists(AF_INET, dlname) || 2498 zonecfg_ifname_exists(AF_INET6, dlname)) { 2499 zerror(zlogp, B_FALSE, "WARNING: skipping network interface " 2500 "'%s' which is used in the global zone", dlname); 2501 return (-1); 2502 } 2503 2504 /* Set zoneid of this link. */ 2505 err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1, 2506 DLADM_OPT_ACTIVE); 2507 if (err != DLADM_STATUS_OK) { 2508 zdlerror(zlogp, err, dlname, 2509 "WARNING: unable to add network interface"); 2510 return (-1); 2511 } 2512 2513 /* 2514 * Set the pool of this link if the zone has a pool and 2515 * neither the cpus nor the pool datalink property is 2516 * already set. 2517 */ 2518 err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT, 2519 "cpus", &cpuset); 2520 if (err != DLADM_STATUS_OK) { 2521 zdlerror(zlogp, err, dlname, 2522 "WARNING: unable to check if cpus link property is set"); 2523 } 2524 err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT, 2525 "pool", &poolset); 2526 if (err != DLADM_STATUS_OK) { 2527 zdlerror(zlogp, err, dlname, 2528 "WARNING: unable to check if pool link property is set"); 2529 } 2530 2531 if ((strlen(pool_name) != 0) && !cpuset && !poolset) { 2532 poolp = pool_name; 2533 err = dladm_set_linkprop(dld_handle, linkid, "pool", 2534 &poolp, 1, DLADM_OPT_ACTIVE); 2535 if (err != DLADM_STATUS_OK) { 2536 zerror(zlogp, B_FALSE, "WARNING: unable to set " 2537 "pool %s to datalink %s", pool_name, dlname); 2538 bzero(pool_name, sizeof (pool_name)); 2539 } 2540 } else { 2541 bzero(pool_name, sizeof (pool_name)); 2542 } 2543 return (0); 2544 } 2545 2546 static boolean_t 2547 sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr, 2548 char *straddr, size_t len) 2549 { 2550 struct sockaddr_in *sin; 2551 struct sockaddr_in6 *sin6; 2552 const char *str = NULL; 2553 2554 if (af == AF_INET) { 2555 /* LINTED E_BAD_PTR_CAST_ALIGN */ 2556 sin = SIN(sockaddr); 2557 str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len); 2558 } else if (af == AF_INET6) { 2559 /* LINTED E_BAD_PTR_CAST_ALIGN */ 2560 sin6 = SIN6(sockaddr); 2561 str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr, 2562 len); 2563 } 2564 2565 return (str != NULL); 2566 } 2567 2568 static int 2569 ipv4_prefixlen(struct sockaddr_in *sin) 2570 { 2571 struct sockaddr_in *m; 2572 struct sockaddr_storage mask; 2573 2574 m = SIN(&mask); 2575 m->sin_family = AF_INET; 2576 if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) { 2577 return (mask2plen((struct sockaddr *)&mask)); 2578 } else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) { 2579 return (8); 2580 } else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) { 2581 return (16); 2582 } else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) { 2583 return (24); 2584 } 2585 return (0); 2586 } 2587 2588 static int 2589 zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid, 2590 void *buf, size_t bufsize) 2591 { 2592 zone_net_data_t *zndata; 2593 size_t znsize; 2594 int err; 2595 2596 znsize = sizeof (*zndata) + bufsize; 2597 zndata = calloc(1, znsize); 2598 if (zndata == NULL) 2599 return (ENOMEM); 2600 zndata->zn_type = type; 2601 zndata->zn_len = bufsize; 2602 zndata->zn_linkid = linkid; 2603 bcopy(buf, zndata->zn_val, zndata->zn_len); 2604 err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize); 2605 free(zndata); 2606 return (err); 2607 } 2608 2609 static int 2610 add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start) 2611 { 2612 struct lifreq lifr; 2613 char **astr, *address; 2614 dladm_status_t dlstatus; 2615 char *ip_nospoof = "ip-nospoof"; 2616 int nnet, naddr, err = 0, j; 2617 size_t zlen, cpleft; 2618 zone_addr_list_t *ptr, *end; 2619 char tmp[INET6_ADDRSTRLEN], *maskstr; 2620 char *zaddr, *cp; 2621 struct in6_addr *routes = NULL; 2622 boolean_t is_set; 2623 datalink_id_t linkid; 2624 2625 assert(start != NULL); 2626 naddr = 0; /* number of addresses */ 2627 nnet = 0; /* number of net resources */ 2628 linkid = start->za_linkid; 2629 for (ptr = start; ptr != NULL && ptr->za_linkid == linkid; 2630 ptr = ptr->za_next) { 2631 nnet++; 2632 } 2633 end = ptr; 2634 zlen = nnet * (INET6_ADDRSTRLEN + 1); 2635 astr = calloc(1, nnet * sizeof (uintptr_t)); 2636 zaddr = calloc(1, zlen); 2637 if (astr == NULL || zaddr == NULL) { 2638 err = ENOMEM; 2639 goto done; 2640 } 2641 cp = zaddr; 2642 cpleft = zlen; 2643 j = 0; 2644 for (ptr = start; ptr != end; ptr = ptr->za_next) { 2645 address = ptr->za_nwiftab.zone_nwif_allowed_address; 2646 if (address[0] == '\0') 2647 continue; 2648 (void) snprintf(tmp, sizeof (tmp), "%s", address); 2649 /* 2650 * Validate the data. zonecfg_valid_net_address() clobbers 2651 * the /<mask> in the address string. 2652 */ 2653 if (zonecfg_valid_net_address(address, &lifr) != Z_OK) { 2654 zerror(zlogp, B_FALSE, "invalid address [%s]\n", 2655 address); 2656 err = EINVAL; 2657 goto done; 2658 } 2659 /* 2660 * convert any hostnames to numeric address strings. 2661 */ 2662 if (!sockaddr_to_str(lifr.lifr_addr.ss_family, 2663 (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) { 2664 err = EINVAL; 2665 goto done; 2666 } 2667 /* 2668 * make a copy of the numeric string for the data needed 2669 * by the "allowed-ips" datalink property. 2670 */ 2671 astr[j] = strdup(cp); 2672 if (astr[j] == NULL) { 2673 err = ENOMEM; 2674 goto done; 2675 } 2676 j++; 2677 /* 2678 * compute the default netmask from the address, if necessary 2679 */ 2680 if ((maskstr = strchr(tmp, '/')) == NULL) { 2681 int prefixlen; 2682 2683 if (lifr.lifr_addr.ss_family == AF_INET) { 2684 prefixlen = ipv4_prefixlen( 2685 SIN(&lifr.lifr_addr)); 2686 } else { 2687 struct sockaddr_in6 *sin6; 2688 2689 sin6 = SIN6(&lifr.lifr_addr); 2690 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 2691 prefixlen = 10; 2692 else 2693 prefixlen = 64; 2694 } 2695 (void) snprintf(tmp, sizeof (tmp), "%d", prefixlen); 2696 maskstr = tmp; 2697 } else { 2698 maskstr++; 2699 } 2700 /* append the "/<netmask>" */ 2701 (void) strlcat(cp, "/", cpleft); 2702 (void) strlcat(cp, maskstr, cpleft); 2703 (void) strlcat(cp, ",", cpleft); 2704 cp += strnlen(cp, zlen); 2705 cpleft = &zaddr[INET6_ADDRSTRLEN] - cp; 2706 } 2707 naddr = j; /* the actual number of addresses in the net resource */ 2708 assert(naddr <= nnet); 2709 2710 /* 2711 * zonecfg has already verified that the defrouter property can only 2712 * be set if there is at least one address defined for the net resource. 2713 * If j is 0, there are no addresses defined, and therefore no routers 2714 * to configure, and we are done at that point. 2715 */ 2716 if (j == 0) 2717 goto done; 2718 2719 /* over-write last ',' with '\0' */ 2720 zaddr[strnlen(zaddr, zlen) - 1] = '\0'; 2721 2722 /* 2723 * First make sure L3 protection is not already set on the link. 2724 */ 2725 dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE, 2726 "protection", &is_set); 2727 if (dlstatus != DLADM_STATUS_OK) { 2728 err = EINVAL; 2729 zerror(zlogp, B_FALSE, "unable to check if protection is set"); 2730 goto done; 2731 } 2732 if (is_set) { 2733 err = EINVAL; 2734 zerror(zlogp, B_FALSE, "Protection is already set"); 2735 goto done; 2736 } 2737 dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE, 2738 "allowed-ips", &is_set); 2739 if (dlstatus != DLADM_STATUS_OK) { 2740 err = EINVAL; 2741 zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set"); 2742 goto done; 2743 } 2744 if (is_set) { 2745 zerror(zlogp, B_FALSE, "allowed-ips is already set"); 2746 err = EINVAL; 2747 goto done; 2748 } 2749 2750 /* 2751 * Enable ip-nospoof for the link, and add address to the allowed-ips 2752 * list. 2753 */ 2754 dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection", 2755 &ip_nospoof, 1, DLADM_OPT_ACTIVE); 2756 if (dlstatus != DLADM_STATUS_OK) { 2757 zerror(zlogp, B_FALSE, "could not set protection\n"); 2758 err = EINVAL; 2759 goto done; 2760 } 2761 dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips", 2762 astr, naddr, DLADM_OPT_ACTIVE); 2763 if (dlstatus != DLADM_STATUS_OK) { 2764 zerror(zlogp, B_FALSE, "could not set allowed-ips\n"); 2765 err = EINVAL; 2766 goto done; 2767 } 2768 2769 /* now set the address in the data-store */ 2770 err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid, 2771 zaddr, strnlen(zaddr, zlen) + 1); 2772 if (err != 0) 2773 goto done; 2774 2775 /* 2776 * add the defaultrouters 2777 */ 2778 routes = calloc(1, nnet * sizeof (*routes)); 2779 j = 0; 2780 for (ptr = start; ptr != end; ptr = ptr->za_next) { 2781 address = ptr->za_nwiftab.zone_nwif_defrouter; 2782 if (address[0] == '\0') 2783 continue; 2784 if (strchr(address, '/') == NULL && strchr(address, ':') != 0) { 2785 /* 2786 * zonecfg_valid_net_address() expects numeric IPv6 2787 * addresses to have a CIDR format netmask. 2788 */ 2789 (void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN); 2790 (void) strlcat(address, tmp, INET6_ADDRSTRLEN); 2791 } 2792 if (zonecfg_valid_net_address(address, &lifr) != Z_OK) { 2793 zerror(zlogp, B_FALSE, 2794 "invalid router [%s]\n", address); 2795 err = EINVAL; 2796 goto done; 2797 } 2798 if (lifr.lifr_addr.ss_family == AF_INET6) { 2799 routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr; 2800 } else { 2801 IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr, 2802 &routes[j]); 2803 } 2804 j++; 2805 } 2806 assert(j <= nnet); 2807 if (j > 0) { 2808 err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid, 2809 linkid, routes, j * sizeof (*routes)); 2810 } 2811 done: 2812 free(routes); 2813 for (j = 0; j < naddr; j++) 2814 free(astr[j]); 2815 free(astr); 2816 free(zaddr); 2817 return (err); 2818 2819 } 2820 2821 static int 2822 add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist) 2823 { 2824 zone_addr_list_t *ptr; 2825 datalink_id_t linkid; 2826 int err; 2827 2828 if (zalist == NULL) 2829 return (0); 2830 2831 linkid = zalist->za_linkid; 2832 2833 err = add_net_for_linkid(zlogp, zoneid, zalist); 2834 if (err != 0) 2835 return (err); 2836 2837 for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) { 2838 if (ptr->za_linkid == linkid) 2839 continue; 2840 linkid = ptr->za_linkid; 2841 err = add_net_for_linkid(zlogp, zoneid, ptr); 2842 if (err != 0) 2843 return (err); 2844 } 2845 return (0); 2846 } 2847 2848 /* 2849 * Add "new" to the list of network interfaces to be configured by 2850 * add_net on zone boot in "old". The list of interfaces in "old" is 2851 * sorted by datalink_id_t, with interfaces sorted FIFO for a given 2852 * datalink_id_t. 2853 * 2854 * Returns the merged list of IP interfaces containing "old" and "new" 2855 */ 2856 static zone_addr_list_t * 2857 add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new) 2858 { 2859 zone_addr_list_t *ptr, *next; 2860 datalink_id_t linkid = new->za_linkid; 2861 2862 assert(old != new); 2863 2864 if (old == NULL) 2865 return (new); 2866 for (ptr = old; ptr != NULL; ptr = ptr->za_next) { 2867 if (ptr->za_linkid == linkid) 2868 break; 2869 } 2870 if (ptr == NULL) { 2871 /* linkid does not already exist, add to the beginning */ 2872 new->za_next = old; 2873 return (new); 2874 } 2875 /* 2876 * adding to the middle of the list; ptr points at the first 2877 * occurrence of linkid. Find the last occurrence. 2878 */ 2879 while ((next = ptr->za_next) != NULL) { 2880 if (next->za_linkid != linkid) 2881 break; 2882 ptr = next; 2883 } 2884 /* insert new after ptr */ 2885 new->za_next = next; 2886 ptr->za_next = new; 2887 return (old); 2888 } 2889 2890 void 2891 free_ip_interface(zone_addr_list_t *zalist) 2892 { 2893 zone_addr_list_t *ptr, *new; 2894 2895 for (ptr = zalist; ptr != NULL; ) { 2896 new = ptr; 2897 ptr = ptr->za_next; 2898 free(new); 2899 } 2900 } 2901 2902 /* 2903 * Add the kernel access control information for the interface names. 2904 * If anything goes wrong, we log a general error message, attempt to tear down 2905 * whatever we set up, and return an error. 2906 */ 2907 static int 2908 configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid) 2909 { 2910 zone_dochandle_t handle; 2911 struct zone_nwiftab nwiftab; 2912 char rootpath[MAXPATHLEN]; 2913 char path[MAXPATHLEN]; 2914 datalink_id_t linkid; 2915 di_prof_t prof = NULL; 2916 boolean_t added = B_FALSE; 2917 zone_addr_list_t *zalist = NULL, *new; 2918 2919 if ((handle = zonecfg_init_handle()) == NULL) { 2920 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2921 return (-1); 2922 } 2923 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2924 zerror(zlogp, B_FALSE, "invalid configuration"); 2925 zonecfg_fini_handle(handle); 2926 return (-1); 2927 } 2928 2929 if (zonecfg_setnwifent(handle) != Z_OK) { 2930 zonecfg_fini_handle(handle); 2931 return (0); 2932 } 2933 2934 for (;;) { 2935 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 2936 break; 2937 2938 if (prof == NULL) { 2939 if (zone_get_devroot(zone_name, rootpath, 2940 sizeof (rootpath)) != Z_OK) { 2941 (void) zonecfg_endnwifent(handle); 2942 zonecfg_fini_handle(handle); 2943 zerror(zlogp, B_TRUE, 2944 "unable to determine dev root"); 2945 return (-1); 2946 } 2947 (void) snprintf(path, sizeof (path), "%s%s", rootpath, 2948 "/dev"); 2949 if (di_prof_init(path, &prof) != 0) { 2950 (void) zonecfg_endnwifent(handle); 2951 zonecfg_fini_handle(handle); 2952 zerror(zlogp, B_TRUE, 2953 "failed to initialize profile"); 2954 return (-1); 2955 } 2956 } 2957 2958 /* 2959 * Create the /dev entry for backward compatibility. 2960 * Only create the /dev entry if it's not in use. 2961 * Note that the zone still boots when the assigned 2962 * interface is inaccessible, used by others, etc. 2963 * Also, when vanity naming is used, some interface do 2964 * do not have corresponding /dev node names (for example, 2965 * vanity named aggregations). The /dev entry is not 2966 * created in that case. The /dev/net entry is always 2967 * accessible. 2968 */ 2969 if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical, 2970 &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK && 2971 add_datalink(zlogp, zone_name, linkid, 2972 nwiftab.zone_nwif_physical) == 0) { 2973 added = B_TRUE; 2974 } else { 2975 (void) zonecfg_endnwifent(handle); 2976 zonecfg_fini_handle(handle); 2977 zerror(zlogp, B_TRUE, "failed to add network device"); 2978 return (-1); 2979 } 2980 /* set up the new IP interface, and add them all later */ 2981 new = malloc(sizeof (*new)); 2982 if (new == NULL) { 2983 zerror(zlogp, B_TRUE, "no memory for %s", 2984 nwiftab.zone_nwif_physical); 2985 zonecfg_fini_handle(handle); 2986 free_ip_interface(zalist); 2987 } 2988 bzero(new, sizeof (*new)); 2989 new->za_nwiftab = nwiftab; 2990 new->za_linkid = linkid; 2991 zalist = add_ip_interface(zalist, new); 2992 } 2993 if (zalist != NULL) { 2994 if ((errno = add_net(zlogp, zoneid, zalist)) != 0) { 2995 (void) zonecfg_endnwifent(handle); 2996 zonecfg_fini_handle(handle); 2997 zerror(zlogp, B_TRUE, "failed to add address"); 2998 free_ip_interface(zalist); 2999 return (-1); 3000 } 3001 free_ip_interface(zalist); 3002 } 3003 (void) zonecfg_endnwifent(handle); 3004 zonecfg_fini_handle(handle); 3005 3006 if (prof != NULL && added) { 3007 if (di_prof_commit(prof) != 0) { 3008 zerror(zlogp, B_TRUE, "failed to commit profile"); 3009 return (-1); 3010 } 3011 } 3012 if (prof != NULL) 3013 di_prof_fini(prof); 3014 3015 return (0); 3016 } 3017 3018 static int 3019 remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid) 3020 { 3021 ushort_t flags; 3022 zone_iptype_t iptype; 3023 int i, dlnum = 0; 3024 datalink_id_t *dllink, *dllinks = NULL; 3025 dladm_status_t err; 3026 3027 if (strlen(pool_name) == 0) 3028 return (0); 3029 3030 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags, 3031 sizeof (flags)) < 0) { 3032 if (vplat_get_iptype(zlogp, &iptype) < 0) { 3033 zerror(zlogp, B_FALSE, "unable to determine ip-type"); 3034 return (-1); 3035 } 3036 } else { 3037 if (flags & ZF_NET_EXCL) 3038 iptype = ZS_EXCLUSIVE; 3039 else 3040 iptype = ZS_SHARED; 3041 } 3042 3043 if (iptype == ZS_EXCLUSIVE) { 3044 /* 3045 * Get the datalink count and for each datalink, 3046 * attempt to clear the pool property and clear 3047 * the pool_name. 3048 */ 3049 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) { 3050 zerror(zlogp, B_TRUE, "unable to count network " 3051 "interfaces"); 3052 return (-1); 3053 } 3054 3055 if (dlnum == 0) 3056 return (0); 3057 3058 if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) 3059 == NULL) { 3060 zerror(zlogp, B_TRUE, "memory allocation failed"); 3061 return (-1); 3062 } 3063 if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) { 3064 zerror(zlogp, B_TRUE, "unable to list network " 3065 "interfaces"); 3066 return (-1); 3067 } 3068 3069 bzero(pool_name, sizeof (pool_name)); 3070 for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) { 3071 err = dladm_set_linkprop(dld_handle, *dllink, "pool", 3072 NULL, 0, DLADM_OPT_ACTIVE); 3073 if (err != DLADM_STATUS_OK) { 3074 zerror(zlogp, B_TRUE, 3075 "WARNING: unable to clear pool"); 3076 } 3077 } 3078 free(dllinks); 3079 } 3080 return (0); 3081 } 3082 3083 static int 3084 remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid) 3085 { 3086 ushort_t flags; 3087 zone_iptype_t iptype; 3088 int i, dlnum = 0; 3089 dladm_status_t dlstatus; 3090 datalink_id_t *dllink, *dllinks = NULL; 3091 3092 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags, 3093 sizeof (flags)) < 0) { 3094 if (vplat_get_iptype(zlogp, &iptype) < 0) { 3095 zerror(zlogp, B_FALSE, "unable to determine ip-type"); 3096 return (-1); 3097 } 3098 } else { 3099 if (flags & ZF_NET_EXCL) 3100 iptype = ZS_EXCLUSIVE; 3101 else 3102 iptype = ZS_SHARED; 3103 } 3104 3105 if (iptype != ZS_EXCLUSIVE) 3106 return (0); 3107 3108 /* 3109 * Get the datalink count and for each datalink, 3110 * attempt to clear the pool property and clear 3111 * the pool_name. 3112 */ 3113 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) { 3114 zerror(zlogp, B_TRUE, "unable to count network interfaces"); 3115 return (-1); 3116 } 3117 3118 if (dlnum == 0) 3119 return (0); 3120 3121 if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) { 3122 zerror(zlogp, B_TRUE, "memory allocation failed"); 3123 return (-1); 3124 } 3125 if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) { 3126 zerror(zlogp, B_TRUE, "unable to list network interfaces"); 3127 free(dllinks); 3128 return (-1); 3129 } 3130 3131 for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) { 3132 char dlerr[DLADM_STRSIZE]; 3133 3134 dlstatus = dladm_set_linkprop(dld_handle, *dllink, 3135 "protection", NULL, 0, DLADM_OPT_ACTIVE); 3136 if (dlstatus == DLADM_STATUS_NOTFOUND) { 3137 /* datalink does not belong to the GZ */ 3138 continue; 3139 } 3140 if (dlstatus != DLADM_STATUS_OK) { 3141 zerror(zlogp, B_FALSE, 3142 dladm_status2str(dlstatus, dlerr)); 3143 free(dllinks); 3144 return (-1); 3145 } 3146 dlstatus = dladm_set_linkprop(dld_handle, *dllink, 3147 "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE); 3148 if (dlstatus != DLADM_STATUS_OK) { 3149 zerror(zlogp, B_FALSE, 3150 dladm_status2str(dlstatus, dlerr)); 3151 free(dllinks); 3152 return (-1); 3153 } 3154 } 3155 free(dllinks); 3156 return (0); 3157 } 3158 3159 static int 3160 unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid) 3161 { 3162 int dlnum = 0; 3163 3164 /* 3165 * The kernel shutdown callback for the dls module should have removed 3166 * all datalinks from this zone. If any remain, then there's a 3167 * problem. 3168 */ 3169 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) { 3170 zerror(zlogp, B_TRUE, "unable to list network interfaces"); 3171 return (-1); 3172 } 3173 if (dlnum != 0) { 3174 zerror(zlogp, B_FALSE, 3175 "datalinks remain in zone after shutdown"); 3176 return (-1); 3177 } 3178 return (0); 3179 } 3180 3181 static int 3182 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid, 3183 const struct sockaddr_storage *local, const struct sockaddr_storage *remote) 3184 { 3185 int fd; 3186 struct strioctl ioc; 3187 tcp_ioc_abort_conn_t conn; 3188 int error; 3189 3190 conn.ac_local = *local; 3191 conn.ac_remote = *remote; 3192 conn.ac_start = TCPS_SYN_SENT; 3193 conn.ac_end = TCPS_TIME_WAIT; 3194 conn.ac_zoneid = zoneid; 3195 3196 ioc.ic_cmd = TCP_IOC_ABORT_CONN; 3197 ioc.ic_timout = -1; /* infinite timeout */ 3198 ioc.ic_len = sizeof (conn); 3199 ioc.ic_dp = (char *)&conn; 3200 3201 if ((fd = open("/dev/tcp", O_RDONLY)) < 0) { 3202 zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp"); 3203 return (-1); 3204 } 3205 3206 error = ioctl(fd, I_STR, &ioc); 3207 (void) close(fd); 3208 if (error == 0 || errno == ENOENT) /* ENOENT is not an error */ 3209 return (0); 3210 return (-1); 3211 } 3212 3213 static int 3214 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid) 3215 { 3216 struct sockaddr_storage l, r; 3217 struct sockaddr_in *local, *remote; 3218 struct sockaddr_in6 *local6, *remote6; 3219 int error; 3220 3221 /* 3222 * Abort IPv4 connections. 3223 */ 3224 bzero(&l, sizeof (*local)); 3225 local = (struct sockaddr_in *)&l; 3226 local->sin_family = AF_INET; 3227 local->sin_addr.s_addr = INADDR_ANY; 3228 local->sin_port = 0; 3229 3230 bzero(&r, sizeof (*remote)); 3231 remote = (struct sockaddr_in *)&r; 3232 remote->sin_family = AF_INET; 3233 remote->sin_addr.s_addr = INADDR_ANY; 3234 remote->sin_port = 0; 3235 3236 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 3237 return (error); 3238 3239 /* 3240 * Abort IPv6 connections. 3241 */ 3242 bzero(&l, sizeof (*local6)); 3243 local6 = (struct sockaddr_in6 *)&l; 3244 local6->sin6_family = AF_INET6; 3245 local6->sin6_port = 0; 3246 local6->sin6_addr = in6addr_any; 3247 3248 bzero(&r, sizeof (*remote6)); 3249 remote6 = (struct sockaddr_in6 *)&r; 3250 remote6->sin6_family = AF_INET6; 3251 remote6->sin6_port = 0; 3252 remote6->sin6_addr = in6addr_any; 3253 3254 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 3255 return (error); 3256 return (0); 3257 } 3258 3259 static int 3260 get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd) 3261 { 3262 int error = -1; 3263 zone_dochandle_t handle; 3264 char *privname = NULL; 3265 3266 if ((handle = zonecfg_init_handle()) == NULL) { 3267 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3268 return (-1); 3269 } 3270 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 3271 zerror(zlogp, B_FALSE, "invalid configuration"); 3272 zonecfg_fini_handle(handle); 3273 return (-1); 3274 } 3275 3276 if (ALT_MOUNT(mount_cmd)) { 3277 zone_iptype_t iptype; 3278 const char *curr_iptype; 3279 3280 if (zonecfg_get_iptype(handle, &iptype) != Z_OK) { 3281 zerror(zlogp, B_TRUE, "unable to determine ip-type"); 3282 zonecfg_fini_handle(handle); 3283 return (-1); 3284 } 3285 3286 switch (iptype) { 3287 case ZS_SHARED: 3288 curr_iptype = "shared"; 3289 break; 3290 case ZS_EXCLUSIVE: 3291 curr_iptype = "exclusive"; 3292 break; 3293 } 3294 3295 if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) { 3296 zonecfg_fini_handle(handle); 3297 return (0); 3298 } 3299 zerror(zlogp, B_FALSE, 3300 "failed to determine the zone's default privilege set"); 3301 zonecfg_fini_handle(handle); 3302 return (-1); 3303 } 3304 3305 switch (zonecfg_get_privset(handle, privs, &privname)) { 3306 case Z_OK: 3307 error = 0; 3308 break; 3309 case Z_PRIV_PROHIBITED: 3310 zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted " 3311 "within the zone's privilege set", privname); 3312 break; 3313 case Z_PRIV_REQUIRED: 3314 zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing " 3315 "from the zone's privilege set", privname); 3316 break; 3317 case Z_PRIV_UNKNOWN: 3318 zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified " 3319 "in the zone's privilege set", privname); 3320 break; 3321 default: 3322 zerror(zlogp, B_FALSE, "failed to determine the zone's " 3323 "privilege set"); 3324 break; 3325 } 3326 3327 free(privname); 3328 zonecfg_fini_handle(handle); 3329 return (error); 3330 } 3331 3332 static int 3333 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep) 3334 { 3335 nvlist_t *nvl = NULL; 3336 char *nvl_packed = NULL; 3337 size_t nvl_size = 0; 3338 nvlist_t **nvlv = NULL; 3339 int rctlcount = 0; 3340 int error = -1; 3341 zone_dochandle_t handle; 3342 struct zone_rctltab rctltab; 3343 rctlblk_t *rctlblk = NULL; 3344 uint64_t maxlwps; 3345 uint64_t maxprocs; 3346 int rproc, rlwp; 3347 3348 *bufp = NULL; 3349 *bufsizep = 0; 3350 3351 if ((handle = zonecfg_init_handle()) == NULL) { 3352 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3353 return (-1); 3354 } 3355 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 3356 zerror(zlogp, B_FALSE, "invalid configuration"); 3357 zonecfg_fini_handle(handle); 3358 return (-1); 3359 } 3360 3361 rctltab.zone_rctl_valptr = NULL; 3362 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 3363 zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc"); 3364 goto out; 3365 } 3366 3367 /* 3368 * Allow the administrator to control both the maximum number of 3369 * process table slots, and the maximum number of lwps, with a single 3370 * max-processes or max-lwps property. If only the max-processes 3371 * property is set, we add a max-lwps property with a limit derived 3372 * from max-processes. If only the max-lwps property is set, we add a 3373 * max-processes property with the same limit as max-lwps. 3374 */ 3375 rproc = zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs); 3376 rlwp = zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps); 3377 if (rproc == Z_OK && rlwp == Z_NO_ENTRY) { 3378 if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS, 3379 maxprocs * LWPS_PER_PROCESS) != Z_OK) { 3380 zerror(zlogp, B_FALSE, "unable to set max-lwps alias"); 3381 goto out; 3382 } 3383 } else if (rlwp == Z_OK && rproc == Z_NO_ENTRY) { 3384 /* no scaling for max-proc value */ 3385 if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXPROCS, 3386 maxlwps) != Z_OK) { 3387 zerror(zlogp, B_FALSE, 3388 "unable to set max-processes alias"); 3389 goto out; 3390 } 3391 } 3392 3393 if (zonecfg_setrctlent(handle) != Z_OK) { 3394 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent"); 3395 goto out; 3396 } 3397 3398 if ((rctlblk = malloc(rctlblk_size())) == NULL) { 3399 zerror(zlogp, B_TRUE, "memory allocation failed"); 3400 goto out; 3401 } 3402 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 3403 struct zone_rctlvaltab *rctlval; 3404 uint_t i, count; 3405 const char *name = rctltab.zone_rctl_name; 3406 3407 /* zoneadm should have already warned about unknown rctls. */ 3408 if (!zonecfg_is_rctl(name)) { 3409 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 3410 rctltab.zone_rctl_valptr = NULL; 3411 continue; 3412 } 3413 count = 0; 3414 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 3415 rctlval = rctlval->zone_rctlval_next) { 3416 count++; 3417 } 3418 if (count == 0) { /* ignore */ 3419 continue; /* Nothing to free */ 3420 } 3421 if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL) 3422 goto out; 3423 i = 0; 3424 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 3425 rctlval = rctlval->zone_rctlval_next, i++) { 3426 if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) { 3427 zerror(zlogp, B_TRUE, "%s failed", 3428 "nvlist_alloc"); 3429 goto out; 3430 } 3431 if (zonecfg_construct_rctlblk(rctlval, rctlblk) 3432 != Z_OK) { 3433 zerror(zlogp, B_FALSE, "invalid rctl value: " 3434 "(priv=%s,limit=%s,action=%s)", 3435 rctlval->zone_rctlval_priv, 3436 rctlval->zone_rctlval_limit, 3437 rctlval->zone_rctlval_action); 3438 goto out; 3439 } 3440 if (!zonecfg_valid_rctl(name, rctlblk)) { 3441 zerror(zlogp, B_FALSE, 3442 "(priv=%s,limit=%s,action=%s) is not a " 3443 "valid value for rctl '%s'", 3444 rctlval->zone_rctlval_priv, 3445 rctlval->zone_rctlval_limit, 3446 rctlval->zone_rctlval_action, 3447 name); 3448 goto out; 3449 } 3450 if (nvlist_add_uint64(nvlv[i], "privilege", 3451 rctlblk_get_privilege(rctlblk)) != 0) { 3452 zerror(zlogp, B_FALSE, "%s failed", 3453 "nvlist_add_uint64"); 3454 goto out; 3455 } 3456 if (nvlist_add_uint64(nvlv[i], "limit", 3457 rctlblk_get_value(rctlblk)) != 0) { 3458 zerror(zlogp, B_FALSE, "%s failed", 3459 "nvlist_add_uint64"); 3460 goto out; 3461 } 3462 if (nvlist_add_uint64(nvlv[i], "action", 3463 (uint_t)rctlblk_get_local_action(rctlblk, NULL)) 3464 != 0) { 3465 zerror(zlogp, B_FALSE, "%s failed", 3466 "nvlist_add_uint64"); 3467 goto out; 3468 } 3469 } 3470 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 3471 rctltab.zone_rctl_valptr = NULL; 3472 if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count) 3473 != 0) { 3474 zerror(zlogp, B_FALSE, "%s failed", 3475 "nvlist_add_nvlist_array"); 3476 goto out; 3477 } 3478 for (i = 0; i < count; i++) 3479 nvlist_free(nvlv[i]); 3480 free(nvlv); 3481 nvlv = NULL; 3482 rctlcount++; 3483 } 3484 (void) zonecfg_endrctlent(handle); 3485 3486 if (rctlcount == 0) { 3487 error = 0; 3488 goto out; 3489 } 3490 if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0) 3491 != 0) { 3492 zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack"); 3493 goto out; 3494 } 3495 3496 error = 0; 3497 *bufp = nvl_packed; 3498 *bufsizep = nvl_size; 3499 3500 out: 3501 free(rctlblk); 3502 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 3503 if (error && nvl_packed != NULL) 3504 free(nvl_packed); 3505 nvlist_free(nvl); 3506 if (nvlv != NULL) 3507 free(nvlv); 3508 if (handle != NULL) 3509 zonecfg_fini_handle(handle); 3510 return (error); 3511 } 3512 3513 static int 3514 get_implicit_datasets(zlog_t *zlogp, char **retstr) 3515 { 3516 char cmdbuf[2 * MAXPATHLEN]; 3517 3518 if (query_hook[0] == '\0') 3519 return (0); 3520 3521 if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook) 3522 > sizeof (cmdbuf)) 3523 return (-1); 3524 3525 if (do_subproc(zlogp, cmdbuf, retstr) != 0) 3526 return (-1); 3527 3528 return (0); 3529 } 3530 3531 static int 3532 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep) 3533 { 3534 zone_dochandle_t handle; 3535 struct zone_dstab dstab; 3536 size_t total, offset, len; 3537 int error = -1; 3538 char *str = NULL; 3539 char *implicit_datasets = NULL; 3540 int implicit_len = 0; 3541 3542 *bufp = NULL; 3543 *bufsizep = 0; 3544 3545 if ((handle = zonecfg_init_handle()) == NULL) { 3546 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3547 return (-1); 3548 } 3549 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 3550 zerror(zlogp, B_FALSE, "invalid configuration"); 3551 zonecfg_fini_handle(handle); 3552 return (-1); 3553 } 3554 3555 if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) { 3556 zerror(zlogp, B_FALSE, "getting implicit datasets failed"); 3557 goto out; 3558 } 3559 3560 if (zonecfg_setdsent(handle) != Z_OK) { 3561 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 3562 goto out; 3563 } 3564 3565 total = 0; 3566 while (zonecfg_getdsent(handle, &dstab) == Z_OK) 3567 total += strlen(dstab.zone_dataset_name) + 1; 3568 (void) zonecfg_enddsent(handle); 3569 3570 if (implicit_datasets != NULL) 3571 implicit_len = strlen(implicit_datasets); 3572 if (implicit_len > 0) 3573 total += implicit_len + 1; 3574 3575 if (total == 0) { 3576 error = 0; 3577 goto out; 3578 } 3579 3580 if ((str = malloc(total)) == NULL) { 3581 zerror(zlogp, B_TRUE, "memory allocation failed"); 3582 goto out; 3583 } 3584 3585 if (zonecfg_setdsent(handle) != Z_OK) { 3586 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 3587 goto out; 3588 } 3589 offset = 0; 3590 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 3591 len = strlen(dstab.zone_dataset_name); 3592 (void) strlcpy(str + offset, dstab.zone_dataset_name, 3593 total - offset); 3594 offset += len; 3595 if (offset < total - 1) 3596 str[offset++] = ','; 3597 } 3598 (void) zonecfg_enddsent(handle); 3599 3600 if (implicit_len > 0) 3601 (void) strlcpy(str + offset, implicit_datasets, total - offset); 3602 3603 error = 0; 3604 *bufp = str; 3605 *bufsizep = total; 3606 3607 out: 3608 if (error != 0 && str != NULL) 3609 free(str); 3610 if (handle != NULL) 3611 zonecfg_fini_handle(handle); 3612 if (implicit_datasets != NULL) 3613 free(implicit_datasets); 3614 3615 return (error); 3616 } 3617 3618 static int 3619 validate_datasets(zlog_t *zlogp) 3620 { 3621 zone_dochandle_t handle; 3622 struct zone_dstab dstab; 3623 zfs_handle_t *zhp; 3624 libzfs_handle_t *hdl; 3625 3626 if ((handle = zonecfg_init_handle()) == NULL) { 3627 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3628 return (-1); 3629 } 3630 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 3631 zerror(zlogp, B_FALSE, "invalid configuration"); 3632 zonecfg_fini_handle(handle); 3633 return (-1); 3634 } 3635 3636 if (zonecfg_setdsent(handle) != Z_OK) { 3637 zerror(zlogp, B_FALSE, "invalid configuration"); 3638 zonecfg_fini_handle(handle); 3639 return (-1); 3640 } 3641 3642 if ((hdl = libzfs_init()) == NULL) { 3643 zerror(zlogp, B_FALSE, "opening ZFS library"); 3644 zonecfg_fini_handle(handle); 3645 return (-1); 3646 } 3647 3648 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 3649 3650 if ((zhp = zfs_open(hdl, dstab.zone_dataset_name, 3651 ZFS_TYPE_FILESYSTEM)) == NULL) { 3652 zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'", 3653 dstab.zone_dataset_name); 3654 zonecfg_fini_handle(handle); 3655 libzfs_fini(hdl); 3656 return (-1); 3657 } 3658 3659 /* 3660 * Automatically set the 'zoned' property. We check the value 3661 * first because we'll get EPERM if it is already set. 3662 */ 3663 if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 3664 zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED), 3665 "on") != 0) { 3666 zerror(zlogp, B_FALSE, "cannot set 'zoned' " 3667 "property for ZFS dataset '%s'\n", 3668 dstab.zone_dataset_name); 3669 zonecfg_fini_handle(handle); 3670 zfs_close(zhp); 3671 libzfs_fini(hdl); 3672 return (-1); 3673 } 3674 3675 zfs_close(zhp); 3676 } 3677 (void) zonecfg_enddsent(handle); 3678 3679 zonecfg_fini_handle(handle); 3680 libzfs_fini(hdl); 3681 3682 return (0); 3683 } 3684 3685 /* 3686 * Return true if the path is its own zfs file system. We determine this 3687 * by stat-ing the path to see if it is zfs and stat-ing the parent to see 3688 * if it is a different fs. 3689 */ 3690 boolean_t 3691 is_zonepath_zfs(char *zonepath) 3692 { 3693 int res; 3694 char *path; 3695 char *parent; 3696 struct statvfs64 buf1, buf2; 3697 3698 if (statvfs64(zonepath, &buf1) != 0) 3699 return (B_FALSE); 3700 3701 if (strcmp(buf1.f_basetype, "zfs") != 0) 3702 return (B_FALSE); 3703 3704 if ((path = strdup(zonepath)) == NULL) 3705 return (B_FALSE); 3706 3707 parent = dirname(path); 3708 res = statvfs64(parent, &buf2); 3709 free(path); 3710 3711 if (res != 0) 3712 return (B_FALSE); 3713 3714 if (buf1.f_fsid == buf2.f_fsid) 3715 return (B_FALSE); 3716 3717 return (B_TRUE); 3718 } 3719 3720 /* 3721 * Verify the MAC label in the root dataset for the zone. 3722 * If the label exists, it must match the label configured for the zone. 3723 * Otherwise if there's no label on the dataset, create one here. 3724 */ 3725 3726 static int 3727 validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl) 3728 { 3729 int error = -1; 3730 zfs_handle_t *zhp; 3731 libzfs_handle_t *hdl; 3732 m_label_t ds_sl; 3733 char zonepath[MAXPATHLEN]; 3734 char ds_hexsl[MAXNAMELEN]; 3735 3736 if (!is_system_labeled()) 3737 return (0); 3738 3739 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 3740 zerror(zlogp, B_TRUE, "unable to determine zone path"); 3741 return (-1); 3742 } 3743 3744 if (!is_zonepath_zfs(zonepath)) 3745 return (0); 3746 3747 if ((hdl = libzfs_init()) == NULL) { 3748 zerror(zlogp, B_FALSE, "opening ZFS library"); 3749 return (-1); 3750 } 3751 3752 if ((zhp = zfs_path_to_zhandle(hdl, rootpath, 3753 ZFS_TYPE_FILESYSTEM)) == NULL) { 3754 zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'", 3755 rootpath); 3756 libzfs_fini(hdl); 3757 return (-1); 3758 } 3759 3760 /* Get the mlslabel property if it exists. */ 3761 if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN, 3762 NULL, NULL, 0, B_TRUE) != 0) || 3763 (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) { 3764 char *str2 = NULL; 3765 3766 /* 3767 * No label on the dataset (or default only); create one. 3768 * (Only do this automatic labeling for the labeled brand.) 3769 */ 3770 if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) { 3771 error = 0; 3772 goto out; 3773 } 3774 3775 error = l_to_str_internal(zone_sl, &str2); 3776 if (error) 3777 goto out; 3778 if (str2 == NULL) { 3779 error = -1; 3780 goto out; 3781 } 3782 if ((error = zfs_prop_set(zhp, 3783 zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) { 3784 zerror(zlogp, B_FALSE, "cannot set 'mlslabel' " 3785 "property for root dataset at '%s'\n", rootpath); 3786 } 3787 free(str2); 3788 goto out; 3789 } 3790 3791 /* Convert the retrieved dataset label to binary form. */ 3792 error = hexstr_to_label(ds_hexsl, &ds_sl); 3793 if (error) { 3794 zerror(zlogp, B_FALSE, "invalid 'mlslabel' " 3795 "property on root dataset at '%s'\n", rootpath); 3796 goto out; /* exit with error */ 3797 } 3798 3799 /* 3800 * Perform a MAC check by comparing the zone label with the 3801 * dataset label. 3802 */ 3803 error = (!blequal(zone_sl, &ds_sl)); 3804 if (error) 3805 zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label"); 3806 out: 3807 zfs_close(zhp); 3808 libzfs_fini(hdl); 3809 3810 return (error); 3811 } 3812 3813 /* 3814 * Mount lower level home directories into/from current zone 3815 * Share exported directories specified in dfstab for zone 3816 */ 3817 static int 3818 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath) 3819 { 3820 zoneid_t *zids = NULL; 3821 priv_set_t *zid_privs; 3822 const priv_impl_info_t *ip = NULL; 3823 uint_t nzents_saved; 3824 uint_t nzents; 3825 int i; 3826 char readonly[] = "ro"; 3827 struct zone_fstab lower_fstab; 3828 char *argv[4]; 3829 3830 if (!is_system_labeled()) 3831 return (0); 3832 3833 if (zid_label == NULL) { 3834 zid_label = m_label_alloc(MAC_LABEL); 3835 if (zid_label == NULL) 3836 return (-1); 3837 } 3838 3839 /* Make sure our zone has an /export/home dir */ 3840 (void) make_one_dir(zlogp, rootpath, "/export/home", 3841 DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP); 3842 3843 lower_fstab.zone_fs_raw[0] = '\0'; 3844 (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS, 3845 sizeof (lower_fstab.zone_fs_type)); 3846 lower_fstab.zone_fs_options = NULL; 3847 (void) zonecfg_add_fs_option(&lower_fstab, readonly); 3848 3849 /* 3850 * Get the list of zones from the kernel 3851 */ 3852 if (zone_list(NULL, &nzents) != 0) { 3853 zerror(zlogp, B_TRUE, "unable to list zones"); 3854 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 3855 return (-1); 3856 } 3857 again: 3858 if (nzents == 0) { 3859 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 3860 return (-1); 3861 } 3862 3863 zids = malloc(nzents * sizeof (zoneid_t)); 3864 if (zids == NULL) { 3865 zerror(zlogp, B_TRUE, "memory allocation failed"); 3866 return (-1); 3867 } 3868 nzents_saved = nzents; 3869 3870 if (zone_list(zids, &nzents) != 0) { 3871 zerror(zlogp, B_TRUE, "unable to list zones"); 3872 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 3873 free(zids); 3874 return (-1); 3875 } 3876 if (nzents != nzents_saved) { 3877 /* list changed, try again */ 3878 free(zids); 3879 goto again; 3880 } 3881 3882 ip = getprivimplinfo(); 3883 if ((zid_privs = priv_allocset()) == NULL) { 3884 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 3885 zonecfg_free_fs_option_list( 3886 lower_fstab.zone_fs_options); 3887 free(zids); 3888 return (-1); 3889 } 3890 3891 for (i = 0; i < nzents; i++) { 3892 char zid_name[ZONENAME_MAX]; 3893 zone_state_t zid_state; 3894 char zid_rpath[MAXPATHLEN]; 3895 struct stat stat_buf; 3896 3897 if (zids[i] == GLOBAL_ZONEID) 3898 continue; 3899 3900 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 3901 continue; 3902 3903 /* 3904 * Do special setup for the zone we are booting 3905 */ 3906 if (strcmp(zid_name, zone_name) == 0) { 3907 struct zone_fstab autofs_fstab; 3908 char map_path[MAXPATHLEN]; 3909 int fd; 3910 3911 /* 3912 * Create auto_home_<zone> map for this zone 3913 * in the global zone. The non-global zone entry 3914 * will be created by automount when the zone 3915 * is booted. 3916 */ 3917 3918 (void) snprintf(autofs_fstab.zone_fs_special, 3919 MAXPATHLEN, "auto_home_%s", zid_name); 3920 3921 (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN, 3922 "/zone/%s/home", zid_name); 3923 3924 (void) snprintf(map_path, sizeof (map_path), 3925 "/etc/%s", autofs_fstab.zone_fs_special); 3926 /* 3927 * If the map file doesn't exist create a template 3928 */ 3929 if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL, 3930 S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) { 3931 int len; 3932 char map_rec[MAXPATHLEN]; 3933 3934 len = snprintf(map_rec, sizeof (map_rec), 3935 "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n", 3936 autofs_fstab.zone_fs_special, rootpath); 3937 (void) write(fd, map_rec, len); 3938 (void) close(fd); 3939 } 3940 3941 /* 3942 * Mount auto_home_<zone> in the global zone if absent. 3943 * If it's already of type autofs, then 3944 * don't mount it again. 3945 */ 3946 if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) || 3947 strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) { 3948 char optstr[] = "indirect,ignore,nobrowse"; 3949 3950 (void) make_one_dir(zlogp, "", 3951 autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE, 3952 DEFAULT_DIR_USER, DEFAULT_DIR_GROUP); 3953 3954 /* 3955 * Mount will fail if automounter has already 3956 * processed the auto_home_<zonename> map 3957 */ 3958 (void) domount(zlogp, MNTTYPE_AUTOFS, optstr, 3959 autofs_fstab.zone_fs_special, 3960 autofs_fstab.zone_fs_dir); 3961 } 3962 continue; 3963 } 3964 3965 3966 if (zone_get_state(zid_name, &zid_state) != Z_OK || 3967 (zid_state != ZONE_STATE_READY && 3968 zid_state != ZONE_STATE_RUNNING)) 3969 /* Skip over zones without mounted filesystems */ 3970 continue; 3971 3972 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 3973 sizeof (m_label_t)) < 0) 3974 /* Skip over zones with unspecified label */ 3975 continue; 3976 3977 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 3978 sizeof (zid_rpath)) == -1) 3979 /* Skip over zones with bad path */ 3980 continue; 3981 3982 if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs, 3983 sizeof (priv_chunk_t) * ip->priv_setsize) == -1) 3984 /* Skip over zones with bad privs */ 3985 continue; 3986 3987 /* 3988 * Reading down is valid according to our label model 3989 * but some customers want to disable it because it 3990 * allows execute down and other possible attacks. 3991 * Therefore, we restrict this feature to zones that 3992 * have the NET_MAC_AWARE privilege which is required 3993 * for NFS read-down semantics. 3994 */ 3995 if ((bldominates(zlabel, zid_label)) && 3996 (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) { 3997 /* 3998 * Our zone dominates this one. 3999 * Create a lofs mount from lower zone's /export/home 4000 */ 4001 (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 4002 "%s/zone/%s/export/home", rootpath, zid_name); 4003 4004 /* 4005 * If the target is already an LOFS mount 4006 * then don't do it again. 4007 */ 4008 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 4009 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 4010 4011 if (snprintf(lower_fstab.zone_fs_special, 4012 MAXPATHLEN, "%s/export", 4013 zid_rpath) > MAXPATHLEN) 4014 continue; 4015 4016 /* 4017 * Make sure the lower-level home exists 4018 */ 4019 if (make_one_dir(zlogp, 4020 lower_fstab.zone_fs_special, "/home", 4021 DEFAULT_DIR_MODE, DEFAULT_DIR_USER, 4022 DEFAULT_DIR_GROUP) != 0) 4023 continue; 4024 4025 (void) strlcat(lower_fstab.zone_fs_special, 4026 "/home", MAXPATHLEN); 4027 4028 /* 4029 * Mount can fail because the lower-level 4030 * zone may have already done a mount up. 4031 */ 4032 (void) mount_one(zlogp, &lower_fstab, "", 4033 Z_MNT_BOOT); 4034 } 4035 } else if ((bldominates(zid_label, zlabel)) && 4036 (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) { 4037 /* 4038 * This zone dominates our zone. 4039 * Create a lofs mount from our zone's /export/home 4040 */ 4041 if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 4042 "%s/zone/%s/export/home", zid_rpath, 4043 zone_name) > MAXPATHLEN) 4044 continue; 4045 4046 /* 4047 * If the target is already an LOFS mount 4048 * then don't do it again. 4049 */ 4050 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 4051 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 4052 4053 (void) snprintf(lower_fstab.zone_fs_special, 4054 MAXPATHLEN, "%s/export/home", rootpath); 4055 4056 /* 4057 * Mount can fail because the higher-level 4058 * zone may have already done a mount down. 4059 */ 4060 (void) mount_one(zlogp, &lower_fstab, "", 4061 Z_MNT_BOOT); 4062 } 4063 } 4064 } 4065 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 4066 priv_freeset(zid_privs); 4067 free(zids); 4068 4069 /* 4070 * Now share any exported directories from this zone. 4071 * Each zone can have its own dfstab. 4072 */ 4073 4074 argv[0] = "zoneshare"; 4075 argv[1] = "-z"; 4076 argv[2] = zone_name; 4077 argv[3] = NULL; 4078 4079 (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv); 4080 /* Don't check for errors since they don't affect the zone */ 4081 4082 return (0); 4083 } 4084 4085 /* 4086 * Unmount lofs mounts from higher level zones 4087 * Unshare nfs exported directories 4088 */ 4089 static void 4090 tsol_unmounts(zlog_t *zlogp, char *zone_name) 4091 { 4092 zoneid_t *zids = NULL; 4093 uint_t nzents_saved; 4094 uint_t nzents; 4095 int i; 4096 char *argv[4]; 4097 char path[MAXPATHLEN]; 4098 4099 if (!is_system_labeled()) 4100 return; 4101 4102 /* 4103 * Get the list of zones from the kernel 4104 */ 4105 if (zone_list(NULL, &nzents) != 0) { 4106 return; 4107 } 4108 4109 if (zid_label == NULL) { 4110 zid_label = m_label_alloc(MAC_LABEL); 4111 if (zid_label == NULL) 4112 return; 4113 } 4114 4115 again: 4116 if (nzents == 0) 4117 return; 4118 4119 zids = malloc(nzents * sizeof (zoneid_t)); 4120 if (zids == NULL) { 4121 zerror(zlogp, B_TRUE, "memory allocation failed"); 4122 return; 4123 } 4124 nzents_saved = nzents; 4125 4126 if (zone_list(zids, &nzents) != 0) { 4127 free(zids); 4128 return; 4129 } 4130 if (nzents != nzents_saved) { 4131 /* list changed, try again */ 4132 free(zids); 4133 goto again; 4134 } 4135 4136 for (i = 0; i < nzents; i++) { 4137 char zid_name[ZONENAME_MAX]; 4138 zone_state_t zid_state; 4139 char zid_rpath[MAXPATHLEN]; 4140 4141 if (zids[i] == GLOBAL_ZONEID) 4142 continue; 4143 4144 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 4145 continue; 4146 4147 /* 4148 * Skip the zone we are halting 4149 */ 4150 if (strcmp(zid_name, zone_name) == 0) 4151 continue; 4152 4153 if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state, 4154 sizeof (zid_state)) < 0) || 4155 (zid_state < ZONE_IS_READY)) 4156 /* Skip over zones without mounted filesystems */ 4157 continue; 4158 4159 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 4160 sizeof (m_label_t)) < 0) 4161 /* Skip over zones with unspecified label */ 4162 continue; 4163 4164 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 4165 sizeof (zid_rpath)) == -1) 4166 /* Skip over zones with bad path */ 4167 continue; 4168 4169 if (zlabel != NULL && bldominates(zid_label, zlabel)) { 4170 /* 4171 * This zone dominates our zone. 4172 * Unmount the lofs mount of our zone's /export/home 4173 */ 4174 4175 if (snprintf(path, MAXPATHLEN, 4176 "%s/zone/%s/export/home", zid_rpath, 4177 zone_name) > MAXPATHLEN) 4178 continue; 4179 4180 /* Skip over mount failures */ 4181 (void) umount(path); 4182 } 4183 } 4184 free(zids); 4185 4186 /* 4187 * Unmount global zone autofs trigger for this zone 4188 */ 4189 (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name); 4190 /* Skip over mount failures */ 4191 (void) umount(path); 4192 4193 /* 4194 * Next unshare any exported directories from this zone. 4195 */ 4196 4197 argv[0] = "zoneunshare"; 4198 argv[1] = "-z"; 4199 argv[2] = zone_name; 4200 argv[3] = NULL; 4201 4202 (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv); 4203 /* Don't check for errors since they don't affect the zone */ 4204 4205 /* 4206 * Finally, deallocate any devices in the zone. 4207 */ 4208 4209 argv[0] = "deallocate"; 4210 argv[1] = "-Isz"; 4211 argv[2] = zone_name; 4212 argv[3] = NULL; 4213 4214 (void) forkexec(zlogp, "/usr/sbin/deallocate", argv); 4215 /* Don't check for errors since they don't affect the zone */ 4216 } 4217 4218 /* 4219 * Fetch the Trusted Extensions label and multi-level ports (MLPs) for 4220 * this zone. 4221 */ 4222 static tsol_zcent_t * 4223 get_zone_label(zlog_t *zlogp, priv_set_t *privs) 4224 { 4225 FILE *fp; 4226 tsol_zcent_t *zcent = NULL; 4227 char line[MAXTNZLEN]; 4228 4229 if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) { 4230 zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH); 4231 return (NULL); 4232 } 4233 4234 while (fgets(line, sizeof (line), fp) != NULL) { 4235 /* 4236 * Check for malformed database 4237 */ 4238 if (strlen(line) == MAXTNZLEN - 1) 4239 break; 4240 if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL) 4241 continue; 4242 if (strcmp(zcent->zc_name, zone_name) == 0) 4243 break; 4244 tsol_freezcent(zcent); 4245 zcent = NULL; 4246 } 4247 (void) fclose(fp); 4248 4249 if (zcent == NULL) { 4250 zerror(zlogp, B_FALSE, "zone requires a label assignment. " 4251 "See tnzonecfg(4)"); 4252 } else { 4253 if (zlabel == NULL) 4254 zlabel = m_label_alloc(MAC_LABEL); 4255 /* 4256 * Save this zone's privileges for later read-down processing 4257 */ 4258 if ((zprivs = priv_allocset()) == NULL) { 4259 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 4260 return (NULL); 4261 } else { 4262 priv_copyset(privs, zprivs); 4263 } 4264 } 4265 return (zcent); 4266 } 4267 4268 /* 4269 * Add the Trusted Extensions multi-level ports for this zone. 4270 */ 4271 static void 4272 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent) 4273 { 4274 tsol_mlp_t *mlp; 4275 tsol_mlpent_t tsme; 4276 4277 if (!is_system_labeled()) 4278 return; 4279 4280 tsme.tsme_zoneid = zoneid; 4281 tsme.tsme_flags = 0; 4282 for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) { 4283 tsme.tsme_mlp = *mlp; 4284 if (tnmlp(TNDB_LOAD, &tsme) != 0) { 4285 zerror(zlogp, B_TRUE, "cannot set zone-specific MLP " 4286 "on %d-%d/%d", mlp->mlp_port, 4287 mlp->mlp_port_upper, mlp->mlp_ipp); 4288 } 4289 } 4290 4291 tsme.tsme_flags = TSOL_MEF_SHARED; 4292 for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) { 4293 tsme.tsme_mlp = *mlp; 4294 if (tnmlp(TNDB_LOAD, &tsme) != 0) { 4295 zerror(zlogp, B_TRUE, "cannot set shared MLP " 4296 "on %d-%d/%d", mlp->mlp_port, 4297 mlp->mlp_port_upper, mlp->mlp_ipp); 4298 } 4299 } 4300 } 4301 4302 static void 4303 remove_mlps(zlog_t *zlogp, zoneid_t zoneid) 4304 { 4305 tsol_mlpent_t tsme; 4306 4307 if (!is_system_labeled()) 4308 return; 4309 4310 (void) memset(&tsme, 0, sizeof (tsme)); 4311 tsme.tsme_zoneid = zoneid; 4312 if (tnmlp(TNDB_FLUSH, &tsme) != 0) 4313 zerror(zlogp, B_TRUE, "cannot flush MLPs"); 4314 } 4315 4316 int 4317 prtmount(const struct mnttab *fs, void *x) 4318 { 4319 zerror((zlog_t *)x, B_FALSE, " %s", fs->mnt_mountp); 4320 return (0); 4321 } 4322 4323 /* 4324 * Look for zones running on the main system that are using this root (or any 4325 * subdirectory of it). Return B_TRUE and print an error if a conflicting zone 4326 * is found or if we can't tell. 4327 */ 4328 static boolean_t 4329 duplicate_zone_root(zlog_t *zlogp, const char *rootpath) 4330 { 4331 zoneid_t *zids = NULL; 4332 uint_t nzids = 0; 4333 boolean_t retv; 4334 int rlen, zlen; 4335 char zroot[MAXPATHLEN]; 4336 char zonename[ZONENAME_MAX]; 4337 4338 for (;;) { 4339 nzids += 10; 4340 zids = malloc(nzids * sizeof (*zids)); 4341 if (zids == NULL) { 4342 zerror(zlogp, B_TRUE, "memory allocation failed"); 4343 return (B_TRUE); 4344 } 4345 if (zone_list(zids, &nzids) == 0) 4346 break; 4347 free(zids); 4348 } 4349 retv = B_FALSE; 4350 rlen = strlen(rootpath); 4351 while (nzids > 0) { 4352 /* 4353 * Ignore errors; they just mean that the zone has disappeared 4354 * while we were busy. 4355 */ 4356 if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot, 4357 sizeof (zroot)) == -1) 4358 continue; 4359 zlen = strlen(zroot); 4360 if (zlen > rlen) 4361 zlen = rlen; 4362 if (strncmp(rootpath, zroot, zlen) == 0 && 4363 (zroot[zlen] == '\0' || zroot[zlen] == '/') && 4364 (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) { 4365 if (getzonenamebyid(zids[nzids], zonename, 4366 sizeof (zonename)) == -1) 4367 (void) snprintf(zonename, sizeof (zonename), 4368 "id %d", (int)zids[nzids]); 4369 zerror(zlogp, B_FALSE, 4370 "zone root %s already in use by zone %s", 4371 rootpath, zonename); 4372 retv = B_TRUE; 4373 break; 4374 } 4375 } 4376 free(zids); 4377 return (retv); 4378 } 4379 4380 /* 4381 * Search for loopback mounts that use this same source node (same device and 4382 * inode). Return B_TRUE if there is one or if we can't tell. 4383 */ 4384 static boolean_t 4385 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath) 4386 { 4387 struct stat64 rst, zst; 4388 struct mnttab *mnp; 4389 4390 if (stat64(rootpath, &rst) == -1) { 4391 zerror(zlogp, B_TRUE, "can't stat %s", rootpath); 4392 return (B_TRUE); 4393 } 4394 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 4395 return (B_TRUE); 4396 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) { 4397 if (mnp->mnt_fstype == NULL || 4398 strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0) 4399 continue; 4400 /* We're looking at a loopback mount. Stat it. */ 4401 if (mnp->mnt_special != NULL && 4402 stat64(mnp->mnt_special, &zst) != -1 && 4403 rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) { 4404 zerror(zlogp, B_FALSE, 4405 "zone root %s is reachable through %s", 4406 rootpath, mnp->mnt_mountp); 4407 return (B_TRUE); 4408 } 4409 } 4410 return (B_FALSE); 4411 } 4412 4413 /* 4414 * Set memory cap and pool info for the zone's resource management 4415 * configuration. 4416 */ 4417 static int 4418 setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid) 4419 { 4420 int res; 4421 uint64_t tmp; 4422 struct zone_mcaptab mcap; 4423 char sched[MAXNAMELEN]; 4424 zone_dochandle_t handle = NULL; 4425 char pool_err[128]; 4426 4427 if ((handle = zonecfg_init_handle()) == NULL) { 4428 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 4429 return (Z_BAD_HANDLE); 4430 } 4431 4432 if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) { 4433 zerror(zlogp, B_FALSE, "invalid configuration"); 4434 zonecfg_fini_handle(handle); 4435 return (res); 4436 } 4437 4438 /* 4439 * If a memory cap is configured, set the cap in the kernel using 4440 * zone_setattr() and make sure the rcapd SMF service is enabled. 4441 */ 4442 if (zonecfg_getmcapent(handle, &mcap) == Z_OK) { 4443 uint64_t num; 4444 char smf_err[128]; 4445 4446 num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10); 4447 if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) { 4448 zerror(zlogp, B_TRUE, "could not set zone memory cap"); 4449 zonecfg_fini_handle(handle); 4450 return (Z_INVAL); 4451 } 4452 4453 if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) { 4454 zerror(zlogp, B_FALSE, "enabling system/rcap service " 4455 "failed: %s", smf_err); 4456 zonecfg_fini_handle(handle); 4457 return (Z_INVAL); 4458 } 4459 } 4460 4461 /* Get the scheduling class set in the zone configuration. */ 4462 if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK && 4463 strlen(sched) > 0) { 4464 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched, 4465 strlen(sched)) == -1) 4466 zerror(zlogp, B_TRUE, "WARNING: unable to set the " 4467 "default scheduling class"); 4468 4469 } else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp) 4470 == Z_OK) { 4471 /* 4472 * If the zone has the zone.cpu-shares rctl set then we want to 4473 * use the Fair Share Scheduler (FSS) for processes in the 4474 * zone. Check what scheduling class the zone would be running 4475 * in by default so we can print a warning and modify the class 4476 * if we wouldn't be using FSS. 4477 */ 4478 char class_name[PC_CLNMSZ]; 4479 4480 if (zonecfg_get_dflt_sched_class(handle, class_name, 4481 sizeof (class_name)) != Z_OK) { 4482 zerror(zlogp, B_FALSE, "WARNING: unable to determine " 4483 "the zone's scheduling class"); 4484 4485 } else if (strcmp("FSS", class_name) != 0) { 4486 zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares " 4487 "rctl is set but\nFSS is not the default " 4488 "scheduling class for\nthis zone. FSS will be " 4489 "used for processes\nin the zone but to get the " 4490 "full benefit of FSS,\nit should be the default " 4491 "scheduling class.\nSee dispadmin(1M) for more " 4492 "details."); 4493 4494 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS", 4495 strlen("FSS")) == -1) 4496 zerror(zlogp, B_TRUE, "WARNING: unable to set " 4497 "zone scheduling class to FSS"); 4498 } 4499 } 4500 4501 /* 4502 * The next few blocks of code attempt to set up temporary pools as 4503 * well as persistent pools. In all cases we call the functions 4504 * unconditionally. Within each funtion the code will check if the 4505 * zone is actually configured for a temporary pool or persistent pool 4506 * and just return if there is nothing to do. 4507 * 4508 * If we are rebooting we want to attempt to reuse any temporary pool 4509 * that was previously set up. zonecfg_bind_tmp_pool() will do the 4510 * right thing in all cases (reuse or create) based on the current 4511 * zonecfg. 4512 */ 4513 if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err, 4514 sizeof (pool_err))) != Z_OK) { 4515 if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND) 4516 zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting " 4517 "cannot be instantiated", zonecfg_strerror(res), 4518 pool_err); 4519 else 4520 zerror(zlogp, B_FALSE, "could not bind zone to " 4521 "temporary pool: %s", zonecfg_strerror(res)); 4522 zonecfg_fini_handle(handle); 4523 return (Z_POOL_BIND); 4524 } 4525 4526 /* 4527 * Check if we need to warn about poold not being enabled. 4528 */ 4529 if (zonecfg_warn_poold(handle)) { 4530 zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has " 4531 "been specified\nbut the dynamic pool service is not " 4532 "enabled.\nThe system will not dynamically adjust the\n" 4533 "processor allocation within the specified range\n" 4534 "until svc:/system/pools/dynamic is enabled.\n" 4535 "See poold(1M)."); 4536 } 4537 4538 /* The following is a warning, not an error. */ 4539 if ((res = zonecfg_bind_pool(handle, zoneid, pool_err, 4540 sizeof (pool_err))) != Z_OK) { 4541 if (res == Z_POOL_BIND) 4542 zerror(zlogp, B_FALSE, "WARNING: unable to bind to " 4543 "pool '%s'; using default pool.", pool_err); 4544 else if (res == Z_POOL) 4545 zerror(zlogp, B_FALSE, "WARNING: %s: %s", 4546 zonecfg_strerror(res), pool_err); 4547 else 4548 zerror(zlogp, B_FALSE, "WARNING: %s", 4549 zonecfg_strerror(res)); 4550 } 4551 4552 /* Update saved pool name in case it has changed */ 4553 (void) zonecfg_get_poolname(handle, zone_name, pool_name, 4554 sizeof (pool_name)); 4555 4556 zonecfg_fini_handle(handle); 4557 return (Z_OK); 4558 } 4559 4560 static void 4561 report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res) 4562 { 4563 switch (res) { 4564 case Z_TOO_BIG: 4565 zerror(zlogp, B_FALSE, "%s property value is too large.", name); 4566 break; 4567 4568 case Z_INVALID_PROPERTY: 4569 zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid", 4570 name, value); 4571 break; 4572 4573 default: 4574 zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res); 4575 break; 4576 } 4577 } 4578 4579 /* 4580 * Sets the hostid of the new zone based on its configured value. The zone's 4581 * zone_t structure must already exist in kernel memory. 'zlogp' refers to the 4582 * log used to report errors and warnings and must be non-NULL. 'zone_namep' 4583 * is the name of the new zone and must be non-NULL. 'zoneid' is the numeric 4584 * ID of the new zone. 4585 * 4586 * This function returns zero on success and a nonzero error code on failure. 4587 */ 4588 static int 4589 setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid) 4590 { 4591 int res; 4592 char hostidp[HW_HOSTID_LEN]; 4593 unsigned int hostid; 4594 4595 res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp)); 4596 4597 if (res == Z_BAD_PROPERTY) { 4598 return (Z_OK); 4599 } else if (res != Z_OK) { 4600 report_prop_err(zlogp, "hostid", hostidp, res); 4601 return (res); 4602 } 4603 4604 hostid = (unsigned int)strtoul(hostidp, NULL, 16); 4605 if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid, 4606 sizeof (hostid))) != 0) { 4607 zerror(zlogp, B_TRUE, 4608 "zone hostid is not valid: %s: %d", hostidp, res); 4609 return (Z_SYSTEM); 4610 } 4611 4612 return (res); 4613 } 4614 4615 static int 4616 setup_zone_secflags(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid) 4617 { 4618 psecflags_t secflags; 4619 struct zone_secflagstab tab = {0}; 4620 secflagdelta_t delt; 4621 int res; 4622 4623 res = zonecfg_lookup_secflags(handle, &tab); 4624 4625 if ((res != Z_OK) && 4626 /* The general defaulting code will handle this */ 4627 (res != Z_NO_ENTRY) && (res != Z_BAD_PROPERTY)) { 4628 zerror(zlogp, B_FALSE, "security-flags property is " 4629 "invalid: %d", res); 4630 return (res); 4631 } 4632 4633 if (strlen(tab.zone_secflags_lower) == 0) 4634 (void) strlcpy(tab.zone_secflags_lower, "none", 4635 sizeof (tab.zone_secflags_lower)); 4636 if (strlen(tab.zone_secflags_default) == 0) 4637 (void) strlcpy(tab.zone_secflags_default, 4638 tab.zone_secflags_lower, 4639 sizeof (tab.zone_secflags_default)); 4640 if (strlen(tab.zone_secflags_upper) == 0) 4641 (void) strlcpy(tab.zone_secflags_upper, "all", 4642 sizeof (tab.zone_secflags_upper)); 4643 4644 if (secflags_parse(NULL, tab.zone_secflags_default, 4645 &delt) == -1) { 4646 zerror(zlogp, B_FALSE, "default security-flags: '%s'" 4647 "are invalid", tab.zone_secflags_default); 4648 return (Z_BAD_PROPERTY); 4649 } else if (delt.psd_ass_active != B_TRUE) { 4650 zerror(zlogp, B_FALSE, "relative security-flags are not " 4651 "allowed in zone configuration (default " 4652 "security-flags: '%s')", 4653 tab.zone_secflags_default); 4654 return (Z_BAD_PROPERTY); 4655 } else { 4656 secflags_copy(&secflags.psf_inherit, &delt.psd_assign); 4657 secflags_copy(&secflags.psf_effective, &delt.psd_assign); 4658 } 4659 4660 if (secflags_parse(NULL, tab.zone_secflags_lower, 4661 &delt) == -1) { 4662 zerror(zlogp, B_FALSE, "lower security-flags: '%s'" 4663 "are invalid", tab.zone_secflags_lower); 4664 return (Z_BAD_PROPERTY); 4665 } else if (delt.psd_ass_active != B_TRUE) { 4666 zerror(zlogp, B_FALSE, "relative security-flags are not " 4667 "allowed in zone configuration (lower " 4668 "security-flags: '%s')", 4669 tab.zone_secflags_lower); 4670 return (Z_BAD_PROPERTY); 4671 } else { 4672 secflags_copy(&secflags.psf_lower, &delt.psd_assign); 4673 } 4674 4675 if (secflags_parse(NULL, tab.zone_secflags_upper, 4676 &delt) == -1) { 4677 zerror(zlogp, B_FALSE, "upper security-flags: '%s'" 4678 "are invalid", tab.zone_secflags_upper); 4679 return (Z_BAD_PROPERTY); 4680 } else if (delt.psd_ass_active != B_TRUE) { 4681 zerror(zlogp, B_FALSE, "relative security-flags are not " 4682 "allowed in zone configuration (upper " 4683 "security-flags: '%s')", 4684 tab.zone_secflags_upper); 4685 return (Z_BAD_PROPERTY); 4686 } else { 4687 secflags_copy(&secflags.psf_upper, &delt.psd_assign); 4688 } 4689 4690 if (!psecflags_validate(&secflags)) { 4691 zerror(zlogp, B_TRUE, "security-flags violate invariants"); 4692 return (Z_BAD_PROPERTY); 4693 } 4694 4695 if ((res = zone_setattr(zoneid, ZONE_ATTR_SECFLAGS, &secflags, 4696 sizeof (secflags))) != 0) { 4697 zerror(zlogp, B_TRUE, 4698 "security-flags couldn't be set: %d", res); 4699 return (Z_SYSTEM); 4700 } 4701 4702 return (Z_OK); 4703 } 4704 4705 static int 4706 setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid) 4707 { 4708 char fsallowed[ZONE_FS_ALLOWED_MAX]; 4709 char *fsallowedp = fsallowed; 4710 int len = sizeof (fsallowed); 4711 int res; 4712 4713 res = zonecfg_get_fs_allowed(handle, fsallowed, len); 4714 4715 if (res == Z_BAD_PROPERTY) { 4716 /* No value, set the defaults */ 4717 (void) strlcpy(fsallowed, DFLT_FS_ALLOWED, len); 4718 } else if (res != Z_OK) { 4719 report_prop_err(zlogp, "fs-allowed", fsallowed, res); 4720 return (res); 4721 } else if (fsallowed[0] == '-') { 4722 /* dropping default filesystems - use remaining list */ 4723 if (fsallowed[1] != ',') 4724 return (Z_OK); 4725 fsallowedp += 2; 4726 len -= 2; 4727 } else { 4728 /* Has a value, append the defaults */ 4729 if (strlcat(fsallowed, ",", len) >= len || 4730 strlcat(fsallowed, DFLT_FS_ALLOWED, len) >= len) { 4731 report_prop_err(zlogp, "fs-allowed", fsallowed, 4732 Z_TOO_BIG); 4733 return (Z_TOO_BIG); 4734 } 4735 } 4736 4737 if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, fsallowedp, len) != 0) { 4738 zerror(zlogp, B_TRUE, 4739 "fs-allowed couldn't be set: %s: %d", fsallowedp, res); 4740 return (Z_SYSTEM); 4741 } 4742 4743 return (Z_OK); 4744 } 4745 4746 static int 4747 setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid) 4748 { 4749 zone_dochandle_t handle; 4750 int res = Z_OK; 4751 4752 if ((handle = zonecfg_init_handle()) == NULL) { 4753 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 4754 return (Z_BAD_HANDLE); 4755 } 4756 if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) { 4757 zerror(zlogp, B_FALSE, "invalid configuration"); 4758 goto out; 4759 } 4760 4761 if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK) 4762 goto out; 4763 4764 if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK) 4765 goto out; 4766 4767 if ((res = setup_zone_secflags(handle, zlogp, zoneid)) != Z_OK) 4768 goto out; 4769 4770 out: 4771 zonecfg_fini_handle(handle); 4772 return (res); 4773 } 4774 4775 zoneid_t 4776 vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd) 4777 { 4778 zoneid_t rval = -1; 4779 priv_set_t *privs; 4780 char rootpath[MAXPATHLEN]; 4781 char *rctlbuf = NULL; 4782 size_t rctlbufsz = 0; 4783 char *zfsbuf = NULL; 4784 size_t zfsbufsz = 0; 4785 zoneid_t zoneid = -1; 4786 int xerr; 4787 char *kzone; 4788 FILE *fp = NULL; 4789 tsol_zcent_t *zcent = NULL; 4790 int match = 0; 4791 int doi = 0; 4792 int flags; 4793 zone_iptype_t iptype; 4794 4795 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 4796 zerror(zlogp, B_TRUE, "unable to determine zone root"); 4797 return (-1); 4798 } 4799 if (zonecfg_in_alt_root()) 4800 resolve_lofs(zlogp, rootpath, sizeof (rootpath)); 4801 4802 if (vplat_get_iptype(zlogp, &iptype) < 0) { 4803 zerror(zlogp, B_TRUE, "unable to determine ip-type"); 4804 return (-1); 4805 } 4806 switch (iptype) { 4807 case ZS_SHARED: 4808 flags = 0; 4809 break; 4810 case ZS_EXCLUSIVE: 4811 flags = ZCF_NET_EXCL; 4812 break; 4813 } 4814 4815 if ((privs = priv_allocset()) == NULL) { 4816 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 4817 return (-1); 4818 } 4819 priv_emptyset(privs); 4820 if (get_privset(zlogp, privs, mount_cmd) != 0) 4821 goto error; 4822 4823 if (mount_cmd == Z_MNT_BOOT && 4824 get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) { 4825 zerror(zlogp, B_FALSE, "Unable to get list of rctls"); 4826 goto error; 4827 } 4828 4829 if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) { 4830 zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets"); 4831 goto error; 4832 } 4833 4834 if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) { 4835 zcent = get_zone_label(zlogp, privs); 4836 if (zcent != NULL) { 4837 match = zcent->zc_match; 4838 doi = zcent->zc_doi; 4839 *zlabel = zcent->zc_label; 4840 } else { 4841 goto error; 4842 } 4843 if (validate_rootds_label(zlogp, rootpath, zlabel) != 0) 4844 goto error; 4845 } 4846 4847 kzone = zone_name; 4848 4849 /* 4850 * We must do this scan twice. First, we look for zones running on the 4851 * main system that are using this root (or any subdirectory of it). 4852 * Next, we reduce to the shortest path and search for loopback mounts 4853 * that use this same source node (same device and inode). 4854 */ 4855 if (duplicate_zone_root(zlogp, rootpath)) 4856 goto error; 4857 if (duplicate_reachable_path(zlogp, rootpath)) 4858 goto error; 4859 4860 if (ALT_MOUNT(mount_cmd)) { 4861 root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE); 4862 4863 /* 4864 * Forge up a special root for this zone. When a zone is 4865 * mounted, we can't let the zone have its own root because the 4866 * tools that will be used in this "scratch zone" need access 4867 * to both the zone's resources and the running machine's 4868 * executables. 4869 * 4870 * Note that the mkdir here also catches read-only filesystems. 4871 */ 4872 if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) { 4873 zerror(zlogp, B_TRUE, "cannot create %s", rootpath); 4874 goto error; 4875 } 4876 if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0) 4877 goto error; 4878 } 4879 4880 if (zonecfg_in_alt_root()) { 4881 /* 4882 * If we are mounting up a zone in an alternate root partition, 4883 * then we have some additional work to do before starting the 4884 * zone. First, resolve the root path down so that we're not 4885 * fooled by duplicates. Then forge up an internal name for 4886 * the zone. 4887 */ 4888 if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) { 4889 zerror(zlogp, B_TRUE, "cannot open mapfile"); 4890 goto error; 4891 } 4892 if (zonecfg_lock_scratch(fp) != 0) { 4893 zerror(zlogp, B_TRUE, "cannot lock mapfile"); 4894 goto error; 4895 } 4896 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 4897 NULL, 0) == 0) { 4898 zerror(zlogp, B_FALSE, "scratch zone already running"); 4899 goto error; 4900 } 4901 /* This is the preferred name */ 4902 (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s", 4903 zone_name); 4904 srandom(getpid()); 4905 while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL, 4906 0) == 0) { 4907 /* This is just an arbitrary name; note "." usage */ 4908 (void) snprintf(kernzone, sizeof (kernzone), 4909 "SUNWlu.%08lX%08lX", random(), random()); 4910 } 4911 kzone = kernzone; 4912 } 4913 4914 xerr = 0; 4915 if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf, 4916 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel, 4917 flags)) == -1) { 4918 if (xerr == ZE_AREMOUNTS) { 4919 if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) { 4920 zerror(zlogp, B_FALSE, 4921 "An unknown file-system is mounted on " 4922 "a subdirectory of %s", rootpath); 4923 } else { 4924 4925 zerror(zlogp, B_FALSE, 4926 "These file-systems are mounted on " 4927 "subdirectories of %s:", rootpath); 4928 (void) zonecfg_find_mounts(rootpath, 4929 prtmount, zlogp); 4930 } 4931 } else if (xerr == ZE_CHROOTED) { 4932 zerror(zlogp, B_FALSE, "%s: " 4933 "cannot create a zone from a chrooted " 4934 "environment", "zone_create"); 4935 } else if (xerr == ZE_LABELINUSE) { 4936 char zonename[ZONENAME_MAX]; 4937 (void) getzonenamebyid(getzoneidbylabel(zlabel), 4938 zonename, ZONENAME_MAX); 4939 zerror(zlogp, B_FALSE, "The zone label is already " 4940 "used by the zone '%s'.", zonename); 4941 } else { 4942 zerror(zlogp, B_TRUE, "%s failed", "zone_create"); 4943 } 4944 goto error; 4945 } 4946 4947 if (zonecfg_in_alt_root() && 4948 zonecfg_add_scratch(fp, zone_name, kernzone, 4949 zonecfg_get_root()) == -1) { 4950 zerror(zlogp, B_TRUE, "cannot add mapfile entry"); 4951 goto error; 4952 } 4953 4954 /* 4955 * The following actions are not performed when merely mounting a zone 4956 * for administrative use. 4957 */ 4958 if (mount_cmd == Z_MNT_BOOT) { 4959 brand_handle_t bh; 4960 struct brand_attr attr; 4961 char modname[MAXPATHLEN]; 4962 4963 if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK) 4964 goto error; 4965 4966 if ((bh = brand_open(brand_name)) == NULL) { 4967 zerror(zlogp, B_FALSE, 4968 "unable to determine brand name"); 4969 goto error; 4970 } 4971 4972 if (!is_system_labeled() && 4973 (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) { 4974 brand_close(bh); 4975 zerror(zlogp, B_FALSE, 4976 "cannot boot labeled zone on unlabeled system"); 4977 goto error; 4978 } 4979 4980 /* 4981 * If this brand requires any kernel support, now is the time to 4982 * get it loaded and initialized. 4983 */ 4984 if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) { 4985 brand_close(bh); 4986 zerror(zlogp, B_FALSE, 4987 "unable to determine brand kernel module"); 4988 goto error; 4989 } 4990 brand_close(bh); 4991 4992 if (strlen(modname) > 0) { 4993 (void) strlcpy(attr.ba_brandname, brand_name, 4994 sizeof (attr.ba_brandname)); 4995 (void) strlcpy(attr.ba_modname, modname, 4996 sizeof (attr.ba_modname)); 4997 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr, 4998 sizeof (attr) != 0)) { 4999 zerror(zlogp, B_TRUE, 5000 "could not set zone brand attribute."); 5001 goto error; 5002 } 5003 } 5004 5005 if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK) 5006 goto error; 5007 5008 set_mlps(zlogp, zoneid, zcent); 5009 } 5010 5011 rval = zoneid; 5012 zoneid = -1; 5013 5014 error: 5015 if (zoneid != -1) { 5016 (void) zone_shutdown(zoneid); 5017 (void) zone_destroy(zoneid); 5018 } 5019 if (rctlbuf != NULL) 5020 free(rctlbuf); 5021 priv_freeset(privs); 5022 if (fp != NULL) 5023 zonecfg_close_scratch(fp); 5024 lofs_discard_mnttab(); 5025 if (zcent != NULL) 5026 tsol_freezcent(zcent); 5027 return (rval); 5028 } 5029 5030 /* 5031 * Enter the zone and write a /etc/zones/index file there. This allows 5032 * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone 5033 * details from inside the zone. 5034 */ 5035 static void 5036 write_index_file(zoneid_t zoneid) 5037 { 5038 FILE *zef; 5039 FILE *zet; 5040 struct zoneent *zep; 5041 pid_t child; 5042 int tmpl_fd; 5043 ctid_t ct; 5044 int fd; 5045 char uuidstr[UUID_PRINTABLE_STRING_LENGTH]; 5046 5047 /* Locate the zone entry in the global zone's index file */ 5048 if ((zef = setzoneent()) == NULL) 5049 return; 5050 while ((zep = getzoneent_private(zef)) != NULL) { 5051 if (strcmp(zep->zone_name, zone_name) == 0) 5052 break; 5053 free(zep); 5054 } 5055 endzoneent(zef); 5056 if (zep == NULL) 5057 return; 5058 5059 if ((tmpl_fd = init_template()) == -1) { 5060 free(zep); 5061 return; 5062 } 5063 5064 if ((child = fork()) == -1) { 5065 (void) ct_tmpl_clear(tmpl_fd); 5066 (void) close(tmpl_fd); 5067 free(zep); 5068 return; 5069 } 5070 5071 /* parent waits for child to finish */ 5072 if (child != 0) { 5073 free(zep); 5074 if (contract_latest(&ct) == -1) 5075 ct = -1; 5076 (void) ct_tmpl_clear(tmpl_fd); 5077 (void) close(tmpl_fd); 5078 (void) waitpid(child, NULL, 0); 5079 (void) contract_abandon_id(ct); 5080 return; 5081 } 5082 5083 /* child enters zone and sets up index file */ 5084 (void) ct_tmpl_clear(tmpl_fd); 5085 if (zone_enter(zoneid) != -1) { 5086 (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE); 5087 (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID, 5088 ZONE_CONFIG_GID); 5089 fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC, 5090 ZONE_INDEX_MODE); 5091 if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) { 5092 (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID); 5093 if (uuid_is_null(zep->zone_uuid)) 5094 uuidstr[0] = '\0'; 5095 else 5096 uuid_unparse(zep->zone_uuid, uuidstr); 5097 (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name, 5098 zone_state_str(zep->zone_state), 5099 uuidstr); 5100 (void) fclose(zet); 5101 } 5102 } 5103 _exit(0); 5104 } 5105 5106 int 5107 vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid) 5108 { 5109 char zonepath[MAXPATHLEN]; 5110 5111 if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) { 5112 lofs_discard_mnttab(); 5113 return (-1); 5114 } 5115 5116 /* 5117 * Before we try to mount filesystems we need to create the 5118 * attribute backing store for /dev 5119 */ 5120 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 5121 lofs_discard_mnttab(); 5122 return (-1); 5123 } 5124 resolve_lofs(zlogp, zonepath, sizeof (zonepath)); 5125 5126 /* Make /dev directory owned by root, grouped sys */ 5127 if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE, 5128 0, 3) != 0) { 5129 lofs_discard_mnttab(); 5130 return (-1); 5131 } 5132 5133 if (mount_filesystems(zlogp, mount_cmd) != 0) { 5134 lofs_discard_mnttab(); 5135 return (-1); 5136 } 5137 5138 if (mount_cmd == Z_MNT_BOOT) { 5139 zone_iptype_t iptype; 5140 5141 if (vplat_get_iptype(zlogp, &iptype) < 0) { 5142 zerror(zlogp, B_TRUE, "unable to determine ip-type"); 5143 lofs_discard_mnttab(); 5144 return (-1); 5145 } 5146 5147 switch (iptype) { 5148 case ZS_SHARED: 5149 /* Always do this to make lo0 get configured */ 5150 if (configure_shared_network_interfaces(zlogp) != 0) { 5151 lofs_discard_mnttab(); 5152 return (-1); 5153 } 5154 break; 5155 case ZS_EXCLUSIVE: 5156 if (configure_exclusive_network_interfaces(zlogp, 5157 zoneid) != 5158 0) { 5159 lofs_discard_mnttab(); 5160 return (-1); 5161 } 5162 break; 5163 } 5164 } 5165 5166 write_index_file(zoneid); 5167 5168 lofs_discard_mnttab(); 5169 return (0); 5170 } 5171 5172 static int 5173 lu_root_teardown(zlog_t *zlogp) 5174 { 5175 char zroot[MAXPATHLEN]; 5176 5177 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 5178 zerror(zlogp, B_FALSE, "unable to determine zone root"); 5179 return (-1); 5180 } 5181 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 5182 5183 /* 5184 * At this point, the processes are gone, the filesystems (save the 5185 * root) are unmounted, and the zone is on death row. But there may 5186 * still be creds floating about in the system that reference the 5187 * zone_t, and which pin down zone_rootvp causing this call to fail 5188 * with EBUSY. Thus, we try for a little while before just giving up. 5189 * (How I wish this were not true, and umount2 just did the right 5190 * thing, or tmpfs supported MS_FORCE This is a gross hack.) 5191 */ 5192 if (umount2(zroot, MS_FORCE) != 0) { 5193 if (errno == ENOTSUP && umount2(zroot, 0) == 0) 5194 goto unmounted; 5195 if (errno == EBUSY) { 5196 int tries = 10; 5197 5198 while (--tries >= 0) { 5199 (void) sleep(1); 5200 if (umount2(zroot, 0) == 0) 5201 goto unmounted; 5202 if (errno != EBUSY) 5203 break; 5204 } 5205 } 5206 zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot); 5207 return (-1); 5208 } 5209 unmounted: 5210 5211 /* 5212 * Only zones in an alternate root environment have scratch zone 5213 * entries. 5214 */ 5215 if (zonecfg_in_alt_root()) { 5216 FILE *fp; 5217 int retv; 5218 5219 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 5220 zerror(zlogp, B_TRUE, "cannot open mapfile"); 5221 return (-1); 5222 } 5223 retv = -1; 5224 if (zonecfg_lock_scratch(fp) != 0) 5225 zerror(zlogp, B_TRUE, "cannot lock mapfile"); 5226 else if (zonecfg_delete_scratch(fp, kernzone) != 0) 5227 zerror(zlogp, B_TRUE, "cannot delete map entry"); 5228 else 5229 retv = 0; 5230 zonecfg_close_scratch(fp); 5231 return (retv); 5232 } else { 5233 return (0); 5234 } 5235 } 5236 5237 int 5238 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting) 5239 { 5240 char *kzone; 5241 zoneid_t zoneid; 5242 int res; 5243 char pool_err[128]; 5244 char zpath[MAXPATHLEN]; 5245 char cmdbuf[MAXPATHLEN]; 5246 brand_handle_t bh = NULL; 5247 dladm_status_t status; 5248 char errmsg[DLADM_STRSIZE]; 5249 ushort_t flags; 5250 5251 kzone = zone_name; 5252 if (zonecfg_in_alt_root()) { 5253 FILE *fp; 5254 5255 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 5256 zerror(zlogp, B_TRUE, "unable to open map file"); 5257 goto error; 5258 } 5259 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 5260 kernzone, sizeof (kernzone)) != 0) { 5261 zerror(zlogp, B_FALSE, "unable to find scratch zone"); 5262 zonecfg_close_scratch(fp); 5263 goto error; 5264 } 5265 zonecfg_close_scratch(fp); 5266 kzone = kernzone; 5267 } 5268 5269 if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) { 5270 if (!bringup_failure_recovery) 5271 zerror(zlogp, B_TRUE, "unable to get zoneid"); 5272 if (unmount_cmd) 5273 (void) lu_root_teardown(zlogp); 5274 goto error; 5275 } 5276 5277 if (remove_datalink_pool(zlogp, zoneid) != 0) { 5278 zerror(zlogp, B_FALSE, "unable clear datalink pool property"); 5279 goto error; 5280 } 5281 5282 if (remove_datalink_protect(zlogp, zoneid) != 0) { 5283 zerror(zlogp, B_FALSE, 5284 "unable clear datalink protect property"); 5285 goto error; 5286 } 5287 5288 /* 5289 * The datalinks assigned to the zone will be removed from the NGZ as 5290 * part of zone_shutdown() so that we need to remove protect/pool etc. 5291 * before zone_shutdown(). Even if the shutdown itself fails, the zone 5292 * will not be able to violate any constraints applied because the 5293 * datalinks are no longer available to the zone. 5294 */ 5295 if (zone_shutdown(zoneid) != 0) { 5296 zerror(zlogp, B_TRUE, "unable to shutdown zone"); 5297 goto error; 5298 } 5299 5300 /* Get the zonepath of this zone */ 5301 if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) { 5302 zerror(zlogp, B_FALSE, "unable to determine zone path"); 5303 goto error; 5304 } 5305 5306 /* Get a handle to the brand info for this zone */ 5307 if ((bh = brand_open(brand_name)) == NULL) { 5308 zerror(zlogp, B_FALSE, "unable to determine zone brand"); 5309 return (-1); 5310 } 5311 /* 5312 * If there is a brand 'halt' callback, execute it now to give the 5313 * brand a chance to cleanup any custom configuration. 5314 */ 5315 (void) strcpy(cmdbuf, EXEC_PREFIX); 5316 if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN, 5317 sizeof (cmdbuf) - EXEC_LEN) < 0) { 5318 brand_close(bh); 5319 zerror(zlogp, B_FALSE, "unable to determine branded zone's " 5320 "halt callback."); 5321 goto error; 5322 } 5323 brand_close(bh); 5324 5325 if ((strlen(cmdbuf) > EXEC_LEN) && 5326 (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) { 5327 zerror(zlogp, B_FALSE, "%s failed", cmdbuf); 5328 goto error; 5329 } 5330 5331 if (!unmount_cmd) { 5332 zone_iptype_t iptype; 5333 5334 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags, 5335 sizeof (flags)) < 0) { 5336 if (vplat_get_iptype(zlogp, &iptype) < 0) { 5337 zerror(zlogp, B_TRUE, "unable to determine " 5338 "ip-type"); 5339 goto error; 5340 } 5341 } else { 5342 if (flags & ZF_NET_EXCL) 5343 iptype = ZS_EXCLUSIVE; 5344 else 5345 iptype = ZS_SHARED; 5346 } 5347 5348 switch (iptype) { 5349 case ZS_SHARED: 5350 if (unconfigure_shared_network_interfaces(zlogp, 5351 zoneid) != 0) { 5352 zerror(zlogp, B_FALSE, "unable to unconfigure " 5353 "network interfaces in zone"); 5354 goto error; 5355 } 5356 break; 5357 case ZS_EXCLUSIVE: 5358 if (unconfigure_exclusive_network_interfaces(zlogp, 5359 zoneid) != 0) { 5360 zerror(zlogp, B_FALSE, "unable to unconfigure " 5361 "network interfaces in zone"); 5362 goto error; 5363 } 5364 status = dladm_zone_halt(dld_handle, zoneid); 5365 if (status != DLADM_STATUS_OK) { 5366 zerror(zlogp, B_FALSE, "unable to notify " 5367 "dlmgmtd of zone halt: %s", 5368 dladm_status2str(status, errmsg)); 5369 } 5370 break; 5371 } 5372 } 5373 5374 if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) { 5375 zerror(zlogp, B_TRUE, "unable to abort TCP connections"); 5376 goto error; 5377 } 5378 5379 if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) { 5380 zerror(zlogp, B_FALSE, 5381 "unable to unmount file systems in zone"); 5382 goto error; 5383 } 5384 5385 /* 5386 * If we are rebooting then we normally don't want to destroy an 5387 * existing temporary pool at this point so that we can just reuse it 5388 * when the zone boots back up. However, it is also possible we were 5389 * running with a temporary pool and the zone configuration has been 5390 * modified to no longer use a temporary pool. In that case we need 5391 * to destroy the temporary pool now. This case looks like the case 5392 * where we never had a temporary pool configured but 5393 * zonecfg_destroy_tmp_pool will do the right thing either way. 5394 */ 5395 if (!unmount_cmd) { 5396 boolean_t destroy_tmp_pool = B_TRUE; 5397 5398 if (rebooting) { 5399 struct zone_psettab pset_tab; 5400 zone_dochandle_t handle; 5401 5402 if ((handle = zonecfg_init_handle()) != NULL && 5403 zonecfg_get_handle(zone_name, handle) == Z_OK && 5404 zonecfg_lookup_pset(handle, &pset_tab) == Z_OK) 5405 destroy_tmp_pool = B_FALSE; 5406 5407 zonecfg_fini_handle(handle); 5408 } 5409 5410 if (destroy_tmp_pool) { 5411 if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err, 5412 sizeof (pool_err))) != Z_OK) { 5413 if (res == Z_POOL) 5414 zerror(zlogp, B_FALSE, pool_err); 5415 } 5416 } 5417 } 5418 5419 remove_mlps(zlogp, zoneid); 5420 5421 if (zone_destroy(zoneid) != 0) { 5422 zerror(zlogp, B_TRUE, "unable to destroy zone"); 5423 goto error; 5424 } 5425 5426 /* 5427 * Special teardown for alternate boot environments: remove the tmpfs 5428 * root for the zone and then remove it from the map file. 5429 */ 5430 if (unmount_cmd && lu_root_teardown(zlogp) != 0) 5431 goto error; 5432 5433 lofs_discard_mnttab(); 5434 return (0); 5435 5436 error: 5437 lofs_discard_mnttab(); 5438 return (-1); 5439 } 5440