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 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * This module contains functions used to bring up and tear down the 31 * Virtual Platform: [un]mounting file-systems, [un]plumbing network 32 * interfaces, [un]configuring devices, establishing resource controls, 33 * and creating/destroying the zone in the kernel. These actions, on 34 * the way up, ready the zone; on the way down, they halt the zone. 35 * See the much longer block comment at the beginning of zoneadmd.c 36 * for a bigger picture of how the whole program functions. 37 * 38 * This module also has primary responsibility for the layout of "scratch 39 * zones." These are mounted, but inactive, zones that are used during 40 * operating system upgrade and potentially other administrative action. The 41 * scratch zone environment is similar to the miniroot environment. The zone's 42 * actual root is mounted read-write on /a, and the standard paths (/usr, 43 * /sbin, /lib) all lead to read-only copies of the running system's binaries. 44 * This allows the administrative tools to manipulate the zone using "-R /a" 45 * without relying on any binaries in the zone itself. 46 * 47 * If the scratch zone is on an alternate root (Live Upgrade [LU] boot 48 * environment), then we must resolve the lofs mounts used there to uncover 49 * writable (unshared) resources. Shared resources, though, are always 50 * read-only. In addition, if the "same" zone with a different root path is 51 * currently running, then "/b" inside the zone points to the running zone's 52 * root. This allows LU to synchronize configuration files during the upgrade 53 * process. 54 * 55 * To construct this environment, this module creates a tmpfs mount on 56 * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as 57 * described above is constructed on the fly. The zone is then created using 58 * $ZONEPATH/lu as the root. 59 * 60 * Note that scratch zones are inactive. The zone's bits are not running and 61 * likely cannot be run correctly until upgrade is done. Init is not running 62 * there, nor is SMF. Because of this, the "mounted" state of a scratch zone 63 * is not a part of the usual halt/ready/boot state machine. 64 */ 65 66 #include <sys/param.h> 67 #include <sys/mount.h> 68 #include <sys/mntent.h> 69 #include <sys/socket.h> 70 #include <sys/utsname.h> 71 #include <sys/types.h> 72 #include <sys/stat.h> 73 #include <sys/sockio.h> 74 #include <sys/stropts.h> 75 #include <sys/conf.h> 76 77 #include <inet/tcp.h> 78 #include <arpa/inet.h> 79 #include <netinet/in.h> 80 #include <net/route.h> 81 82 #include <stdio.h> 83 #include <errno.h> 84 #include <fcntl.h> 85 #include <unistd.h> 86 #include <rctl.h> 87 #include <stdlib.h> 88 #include <string.h> 89 #include <strings.h> 90 #include <wait.h> 91 #include <limits.h> 92 #include <libgen.h> 93 #include <libzfs.h> 94 #include <zone.h> 95 #include <assert.h> 96 #include <libcontract.h> 97 #include <libcontract_priv.h> 98 #include <uuid/uuid.h> 99 100 #include <sys/mntio.h> 101 #include <sys/mnttab.h> 102 #include <sys/fs/autofs.h> /* for _autofssys() */ 103 #include <sys/fs/lofs_info.h> 104 #include <sys/fs/zfs.h> 105 106 #include <pool.h> 107 #include <sys/pool.h> 108 109 #include <libzonecfg.h> 110 #include <synch.h> 111 112 #include "zoneadmd.h" 113 #include <tsol/label.h> 114 #include <libtsnet.h> 115 #include <sys/priv.h> 116 117 #define V4_ADDR_LEN 32 118 #define V6_ADDR_LEN 128 119 120 /* 0755 is the default directory mode. */ 121 #define DEFAULT_DIR_MODE \ 122 (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 123 124 #define IPD_DEFAULT_OPTS \ 125 MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES 126 127 #define DFSTYPES "/etc/dfs/fstypes" 128 #define MAXTNZLEN 2048 129 130 /* 131 * A list of directories which should be created. 132 */ 133 134 struct dir_info { 135 char *dir_name; 136 mode_t dir_mode; 137 }; 138 139 /* 140 * The pathnames below are relative to the zonepath 141 */ 142 static struct dir_info dev_dirs[] = { 143 { "/dev", 0755 }, 144 { "/dev/dsk", 0755 }, 145 { "/dev/fd", 0555 }, 146 { "/dev/pts", 0755 }, 147 { "/dev/rdsk", 0755 }, 148 { "/dev/rmt", 0755 }, 149 { "/dev/sad", 0755 }, 150 { "/dev/swap", 0755 }, 151 { "/dev/term", 0755 }, 152 }; 153 154 /* 155 * A list of devices which should be symlinked to /dev/zconsole. 156 */ 157 158 struct symlink_info { 159 char *sl_source; 160 char *sl_target; 161 }; 162 163 /* 164 * The "source" paths are relative to the zonepath 165 */ 166 static struct symlink_info dev_symlinks[] = { 167 { "/dev/stderr", "./fd/2" }, 168 { "/dev/stdin", "./fd/0" }, 169 { "/dev/stdout", "./fd/1" }, 170 { "/dev/dtremote", "/dev/null" }, 171 { "/dev/console", "zconsole" }, 172 { "/dev/syscon", "zconsole" }, 173 { "/dev/sysmsg", "zconsole" }, 174 { "/dev/systty", "zconsole" }, 175 { "/dev/msglog", "zconsole" }, 176 }; 177 178 /* for routing socket */ 179 static int rts_seqno = 0; 180 181 /* mangled zone name when mounting in an alternate root environment */ 182 static char kernzone[ZONENAME_MAX]; 183 184 /* array of cached mount entries for resolve_lofs */ 185 static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max; 186 187 /* for Trusted Extensions */ 188 static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *); 189 static int tsol_mounts(zlog_t *, char *, char *); 190 static void tsol_unmounts(zlog_t *, char *); 191 static m_label_t *zlabel = NULL; 192 static m_label_t *zid_label = NULL; 193 static priv_set_t *zprivs = NULL; 194 195 /* from libsocket, not in any header file */ 196 extern int getnetmaskbyaddr(struct in_addr, struct in_addr *); 197 198 /* 199 * An optimization for build_mnttable: reallocate (and potentially copy the 200 * data) only once every N times through the loop. 201 */ 202 #define MNTTAB_HUNK 32 203 204 /* 205 * Private autofs system call 206 */ 207 extern int _autofssys(int, void *); 208 209 static int 210 autofs_cleanup(zoneid_t zoneid) 211 { 212 /* 213 * Ask autofs to unmount all trigger nodes in the given zone. 214 */ 215 return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid)); 216 } 217 218 static void 219 free_mnttable(struct mnttab *mnt_array, uint_t nelem) 220 { 221 uint_t i; 222 223 if (mnt_array == NULL) 224 return; 225 for (i = 0; i < nelem; i++) { 226 free(mnt_array[i].mnt_mountp); 227 free(mnt_array[i].mnt_fstype); 228 free(mnt_array[i].mnt_special); 229 free(mnt_array[i].mnt_mntopts); 230 assert(mnt_array[i].mnt_time == NULL); 231 } 232 free(mnt_array); 233 } 234 235 /* 236 * Build the mount table for the zone rooted at "zroot", storing the resulting 237 * array of struct mnttabs in "mnt_arrayp" and the number of elements in the 238 * array in "nelemp". 239 */ 240 static int 241 build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab, 242 struct mnttab **mnt_arrayp, uint_t *nelemp) 243 { 244 struct mnttab mnt; 245 struct mnttab *mnts; 246 struct mnttab *mnp; 247 uint_t nmnt; 248 249 rewind(mnttab); 250 resetmnttab(mnttab); 251 nmnt = 0; 252 mnts = NULL; 253 while (getmntent(mnttab, &mnt) == 0) { 254 struct mnttab *tmp_array; 255 256 if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0) 257 continue; 258 if (nmnt % MNTTAB_HUNK == 0) { 259 tmp_array = realloc(mnts, 260 (nmnt + MNTTAB_HUNK) * sizeof (*mnts)); 261 if (tmp_array == NULL) { 262 free_mnttable(mnts, nmnt); 263 return (-1); 264 } 265 mnts = tmp_array; 266 } 267 mnp = &mnts[nmnt++]; 268 269 /* 270 * Zero out any fields we're not using. 271 */ 272 (void) memset(mnp, 0, sizeof (*mnp)); 273 274 if (mnt.mnt_special != NULL) 275 mnp->mnt_special = strdup(mnt.mnt_special); 276 if (mnt.mnt_mntopts != NULL) 277 mnp->mnt_mntopts = strdup(mnt.mnt_mntopts); 278 mnp->mnt_mountp = strdup(mnt.mnt_mountp); 279 mnp->mnt_fstype = strdup(mnt.mnt_fstype); 280 if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) || 281 (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) || 282 mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) { 283 zerror(zlogp, B_TRUE, "memory allocation failed"); 284 free_mnttable(mnts, nmnt); 285 return (-1); 286 } 287 } 288 *mnt_arrayp = mnts; 289 *nelemp = nmnt; 290 return (0); 291 } 292 293 /* 294 * This is an optimization. The resolve_lofs function is used quite frequently 295 * to manipulate file paths, and on a machine with a large number of zones, 296 * there will be a huge number of mounted file systems. Thus, we trigger a 297 * reread of the list of mount points 298 */ 299 static void 300 lofs_discard_mnttab(void) 301 { 302 free_mnttable(resolve_lofs_mnts, 303 resolve_lofs_mnt_max - resolve_lofs_mnts); 304 resolve_lofs_mnts = resolve_lofs_mnt_max = NULL; 305 } 306 307 static int 308 lofs_read_mnttab(zlog_t *zlogp) 309 { 310 FILE *mnttab; 311 uint_t nmnts; 312 313 if ((mnttab = fopen(MNTTAB, "r")) == NULL) 314 return (-1); 315 if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts, 316 &nmnts) == -1) { 317 (void) fclose(mnttab); 318 return (-1); 319 } 320 (void) fclose(mnttab); 321 resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts; 322 return (0); 323 } 324 325 /* 326 * This function loops over potential loopback mounts and symlinks in a given 327 * path and resolves them all down to an absolute path. 328 */ 329 static void 330 resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen) 331 { 332 int len, arlen; 333 const char *altroot; 334 char tmppath[MAXPATHLEN]; 335 boolean_t outside_altroot; 336 337 if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1) 338 return; 339 tmppath[len] = '\0'; 340 (void) strlcpy(path, tmppath, sizeof (tmppath)); 341 342 /* This happens once per zoneadmd operation. */ 343 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 344 return; 345 346 altroot = zonecfg_get_root(); 347 arlen = strlen(altroot); 348 outside_altroot = B_FALSE; 349 for (;;) { 350 struct mnttab *mnp; 351 352 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 353 mnp++) { 354 if (mnp->mnt_fstype == NULL || 355 mnp->mnt_mountp == NULL || 356 mnp->mnt_special == NULL || 357 strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0) 358 continue; 359 len = strlen(mnp->mnt_mountp); 360 if (strncmp(mnp->mnt_mountp, path, len) == 0 && 361 (path[len] == '/' || path[len] == '\0')) 362 break; 363 } 364 if (mnp >= resolve_lofs_mnt_max) 365 break; 366 if (outside_altroot) { 367 char *cp; 368 int olen = sizeof (MNTOPT_RO) - 1; 369 370 /* 371 * If we run into a read-only mount outside of the 372 * alternate root environment, then the user doesn't 373 * want this path to be made read-write. 374 */ 375 if (mnp->mnt_mntopts != NULL && 376 (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) != 377 NULL && 378 (cp == mnp->mnt_mntopts || cp[-1] == ',') && 379 (cp[olen] == '\0' || cp[olen] == ',')) { 380 break; 381 } 382 } else if (arlen > 0 && 383 (strncmp(mnp->mnt_special, altroot, arlen) != 0 || 384 (mnp->mnt_special[arlen] != '\0' && 385 mnp->mnt_special[arlen] != '/'))) { 386 outside_altroot = B_TRUE; 387 } 388 /* use temporary buffer because new path might be longer */ 389 (void) snprintf(tmppath, sizeof (tmppath), "%s%s", 390 mnp->mnt_special, path + len); 391 if ((len = resolvepath(tmppath, path, pathlen)) == -1) 392 break; 393 path[len] = '\0'; 394 } 395 } 396 397 /* 398 * For a regular mount, check if a replacement lofs mount is needed because the 399 * referenced device is already mounted somewhere. 400 */ 401 static int 402 check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr) 403 { 404 struct mnttab *mnp; 405 zone_fsopt_t *optptr, *onext; 406 407 /* This happens once per zoneadmd operation. */ 408 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 409 return (-1); 410 411 /* 412 * If this special node isn't already in use, then it's ours alone; 413 * no need to worry about conflicting mounts. 414 */ 415 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; 416 mnp++) { 417 if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0) 418 break; 419 } 420 if (mnp >= resolve_lofs_mnt_max) 421 return (0); 422 423 /* 424 * Convert this duplicate mount into a lofs mount. 425 */ 426 (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp, 427 sizeof (fsptr->zone_fs_special)); 428 (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS, 429 sizeof (fsptr->zone_fs_type)); 430 fsptr->zone_fs_raw[0] = '\0'; 431 432 /* 433 * Discard all but one of the original options and set that to be the 434 * same set of options used for inherit package directory resources. 435 */ 436 optptr = fsptr->zone_fs_options; 437 if (optptr == NULL) { 438 optptr = malloc(sizeof (*optptr)); 439 if (optptr == NULL) { 440 zerror(zlogp, B_TRUE, "cannot mount %s", 441 fsptr->zone_fs_dir); 442 return (-1); 443 } 444 } else { 445 while ((onext = optptr->zone_fsopt_next) != NULL) { 446 optptr->zone_fsopt_next = onext->zone_fsopt_next; 447 free(onext); 448 } 449 } 450 (void) strcpy(optptr->zone_fsopt_opt, IPD_DEFAULT_OPTS); 451 optptr->zone_fsopt_next = NULL; 452 fsptr->zone_fs_options = optptr; 453 return (0); 454 } 455 456 static int 457 make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode) 458 { 459 char path[MAXPATHLEN]; 460 struct stat st; 461 462 if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) > 463 sizeof (path)) { 464 zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix, 465 subdir); 466 return (-1); 467 } 468 469 if (lstat(path, &st) == 0) { 470 /* 471 * We don't check the file mode since presumably the zone 472 * administrator may have had good reason to change the mode, 473 * and we don't need to second guess him. 474 */ 475 if (!S_ISDIR(st.st_mode)) { 476 if (is_system_labeled() && 477 S_ISREG(st.st_mode)) { 478 /* 479 * The need to mount readonly copies of 480 * global zone /etc/ files is unique to 481 * Trusted Extensions. 482 */ 483 if (strncmp(subdir, "/etc/", 484 strlen("/etc/")) != 0) { 485 zerror(zlogp, B_FALSE, 486 "%s is not in /etc", path); 487 return (-1); 488 } 489 } else { 490 zerror(zlogp, B_FALSE, 491 "%s is not a directory", path); 492 return (-1); 493 } 494 } 495 } else if (mkdirp(path, mode) != 0) { 496 if (errno == EROFS) 497 zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on " 498 "a read-only file system in this local zone.\nMake " 499 "sure %s exists in the global zone.", path, subdir); 500 else 501 zerror(zlogp, B_TRUE, "mkdirp of %s failed", path); 502 return (-1); 503 } 504 return (0); 505 } 506 507 /* 508 * Make /dev and various directories underneath it. 509 */ 510 static int 511 make_dev_dirs(zlog_t *zlogp, const char *zonepath) 512 { 513 int i; 514 515 for (i = 0; i < sizeof (dev_dirs) / sizeof (struct dir_info); i++) { 516 if (make_one_dir(zlogp, zonepath, dev_dirs[i].dir_name, 517 dev_dirs[i].dir_mode) != 0) 518 return (-1); 519 } 520 return (0); 521 } 522 523 /* 524 * Make various sym-links underneath /dev. 525 */ 526 static int 527 make_dev_links(zlog_t *zlogp, char *zonepath) 528 { 529 int i; 530 531 for (i = 0; i < sizeof (dev_symlinks) / sizeof (struct symlink_info); 532 i++) { 533 char dev[MAXPATHLEN]; 534 struct stat st; 535 536 (void) snprintf(dev, sizeof (dev), "%s%s", zonepath, 537 dev_symlinks[i].sl_source); 538 if (lstat(dev, &st) == 0) { 539 /* 540 * Try not to call unlink(2) on directories, since that 541 * makes UFS unhappy. 542 */ 543 if (S_ISDIR(st.st_mode)) { 544 zerror(zlogp, B_FALSE, "symlink path %s is a " 545 "directory", dev_symlinks[i].sl_source); 546 return (-1); 547 } 548 (void) unlink(dev); 549 } 550 if (symlink(dev_symlinks[i].sl_target, dev) != 0) { 551 zerror(zlogp, B_TRUE, "could not setup %s->%s symlink", 552 dev_symlinks[i].sl_source, 553 dev_symlinks[i].sl_target); 554 return (-1); 555 } 556 } 557 return (0); 558 } 559 560 /* 561 * Create various directories and sym-links under /dev. 562 */ 563 static int 564 create_dev_files(zlog_t *zlogp) 565 { 566 char zonepath[MAXPATHLEN]; 567 568 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 569 zerror(zlogp, B_TRUE, "unable to determine zone root"); 570 return (-1); 571 } 572 if (zonecfg_in_alt_root()) 573 resolve_lofs(zlogp, zonepath, sizeof (zonepath)); 574 575 if (make_dev_dirs(zlogp, zonepath) != 0) 576 return (-1); 577 if (make_dev_links(zlogp, zonepath) != 0) 578 return (-1); 579 return (0); 580 } 581 582 static void 583 free_remote_fstypes(char **types) 584 { 585 uint_t i; 586 587 if (types == NULL) 588 return; 589 for (i = 0; types[i] != NULL; i++) 590 free(types[i]); 591 free(types); 592 } 593 594 static char ** 595 get_remote_fstypes(zlog_t *zlogp) 596 { 597 char **types = NULL; 598 FILE *fp; 599 char buf[MAXPATHLEN]; 600 char fstype[MAXPATHLEN]; 601 uint_t lines = 0; 602 uint_t i; 603 604 if ((fp = fopen(DFSTYPES, "r")) == NULL) { 605 zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES); 606 return (NULL); 607 } 608 /* 609 * Count the number of lines 610 */ 611 while (fgets(buf, sizeof (buf), fp) != NULL) 612 lines++; 613 if (lines == 0) /* didn't read anything; empty file */ 614 goto out; 615 rewind(fp); 616 /* 617 * Allocate enough space for a NULL-terminated array. 618 */ 619 types = calloc(lines + 1, sizeof (char *)); 620 if (types == NULL) { 621 zerror(zlogp, B_TRUE, "memory allocation failed"); 622 goto out; 623 } 624 i = 0; 625 while (fgets(buf, sizeof (buf), fp) != NULL) { 626 /* LINTED - fstype is big enough to hold buf */ 627 if (sscanf(buf, "%s", fstype) == 0) { 628 zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES); 629 free_remote_fstypes(types); 630 types = NULL; 631 goto out; 632 } 633 types[i] = strdup(fstype); 634 if (types[i] == NULL) { 635 zerror(zlogp, B_TRUE, "memory allocation failed"); 636 free_remote_fstypes(types); 637 types = NULL; 638 goto out; 639 } 640 i++; 641 } 642 out: 643 (void) fclose(fp); 644 return (types); 645 } 646 647 static boolean_t 648 is_remote_fstype(const char *fstype, char *const *remote_fstypes) 649 { 650 uint_t i; 651 652 if (remote_fstypes == NULL) 653 return (B_FALSE); 654 for (i = 0; remote_fstypes[i] != NULL; i++) { 655 if (strcmp(remote_fstypes[i], fstype) == 0) 656 return (B_TRUE); 657 } 658 return (B_FALSE); 659 } 660 661 /* 662 * This converts a zone root path (normally of the form .../root) to a Live 663 * Upgrade scratch zone root (of the form .../lu). 664 */ 665 static void 666 root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved) 667 { 668 if (!isresolved && zonecfg_in_alt_root()) 669 resolve_lofs(zlogp, zroot, zrootlen); 670 (void) strcpy(strrchr(zroot, '/') + 1, "lu"); 671 } 672 673 /* 674 * The general strategy for unmounting filesystems is as follows: 675 * 676 * - Remote filesystems may be dead, and attempting to contact them as 677 * part of a regular unmount may hang forever; we want to always try to 678 * forcibly unmount such filesystems and only fall back to regular 679 * unmounts if the filesystem doesn't support forced unmounts. 680 * 681 * - We don't want to unnecessarily corrupt metadata on local 682 * filesystems (ie UFS), so we want to start off with graceful unmounts, 683 * and only escalate to doing forced unmounts if we get stuck. 684 * 685 * We start off walking backwards through the mount table. This doesn't 686 * give us strict ordering but ensures that we try to unmount submounts 687 * first. We thus limit the number of failed umount2(2) calls. 688 * 689 * The mechanism for determining if we're stuck is to count the number 690 * of failed unmounts each iteration through the mount table. This 691 * gives us an upper bound on the number of filesystems which remain 692 * mounted (autofs trigger nodes are dealt with separately). If at the 693 * end of one unmount+autofs_cleanup cycle we still have the same number 694 * of mounts that we started out with, we're stuck and try a forced 695 * unmount. If that fails (filesystem doesn't support forced unmounts) 696 * then we bail and are unable to teardown the zone. If it succeeds, 697 * we're no longer stuck so we continue with our policy of trying 698 * graceful mounts first. 699 * 700 * Zone must be down (ie, no processes or threads active). 701 */ 702 static int 703 unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd) 704 { 705 int error = 0; 706 FILE *mnttab; 707 struct mnttab *mnts; 708 uint_t nmnt; 709 char zroot[MAXPATHLEN + 1]; 710 size_t zrootlen; 711 uint_t oldcount = UINT_MAX; 712 boolean_t stuck = B_FALSE; 713 char **remote_fstypes = NULL; 714 715 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 716 zerror(zlogp, B_FALSE, "unable to determine zone root"); 717 return (-1); 718 } 719 if (unmount_cmd) 720 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 721 722 (void) strcat(zroot, "/"); 723 zrootlen = strlen(zroot); 724 725 /* 726 * For Trusted Extensions unmount each higher level zone's mount 727 * of our zone's /export/home 728 */ 729 if (!unmount_cmd) 730 tsol_unmounts(zlogp, zone_name); 731 732 if ((mnttab = fopen(MNTTAB, "r")) == NULL) { 733 zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB); 734 return (-1); 735 } 736 /* 737 * Use our hacky mntfs ioctl so we see everything, even mounts with 738 * MS_NOMNTTAB. 739 */ 740 if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) { 741 zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB); 742 error++; 743 goto out; 744 } 745 746 /* 747 * Build the list of remote fstypes so we know which ones we 748 * should forcibly unmount. 749 */ 750 remote_fstypes = get_remote_fstypes(zlogp); 751 for (; /* ever */; ) { 752 uint_t newcount = 0; 753 boolean_t unmounted; 754 struct mnttab *mnp; 755 char *path; 756 uint_t i; 757 758 mnts = NULL; 759 nmnt = 0; 760 /* 761 * MNTTAB gives us a way to walk through mounted 762 * filesystems; we need to be able to walk them in 763 * reverse order, so we build a list of all mounted 764 * filesystems. 765 */ 766 if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts, 767 &nmnt) != 0) { 768 error++; 769 goto out; 770 } 771 for (i = 0; i < nmnt; i++) { 772 mnp = &mnts[nmnt - i - 1]; /* access in reverse order */ 773 path = mnp->mnt_mountp; 774 unmounted = B_FALSE; 775 /* 776 * Try forced unmount first for remote filesystems. 777 * 778 * Not all remote filesystems support forced unmounts, 779 * so if this fails (ENOTSUP) we'll continue on 780 * and try a regular unmount. 781 */ 782 if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) { 783 if (umount2(path, MS_FORCE) == 0) 784 unmounted = B_TRUE; 785 } 786 /* 787 * Try forced unmount if we're stuck. 788 */ 789 if (stuck) { 790 if (umount2(path, MS_FORCE) == 0) { 791 unmounted = B_TRUE; 792 stuck = B_FALSE; 793 } else { 794 /* 795 * The first failure indicates a 796 * mount we won't be able to get 797 * rid of automatically, so we 798 * bail. 799 */ 800 error++; 801 zerror(zlogp, B_FALSE, 802 "unable to unmount '%s'", path); 803 free_mnttable(mnts, nmnt); 804 goto out; 805 } 806 } 807 /* 808 * Try regular unmounts for everything else. 809 */ 810 if (!unmounted && umount2(path, 0) != 0) 811 newcount++; 812 } 813 free_mnttable(mnts, nmnt); 814 815 if (newcount == 0) 816 break; 817 if (newcount >= oldcount) { 818 /* 819 * Last round didn't unmount anything; we're stuck and 820 * should start trying forced unmounts. 821 */ 822 stuck = B_TRUE; 823 } 824 oldcount = newcount; 825 826 /* 827 * Autofs doesn't let you unmount its trigger nodes from 828 * userland so we have to tell the kernel to cleanup for us. 829 */ 830 if (autofs_cleanup(zoneid) != 0) { 831 zerror(zlogp, B_TRUE, "unable to remove autofs nodes"); 832 error++; 833 goto out; 834 } 835 } 836 837 out: 838 free_remote_fstypes(remote_fstypes); 839 (void) fclose(mnttab); 840 return (error ? -1 : 0); 841 } 842 843 static int 844 fs_compare(const void *m1, const void *m2) 845 { 846 struct zone_fstab *i = (struct zone_fstab *)m1; 847 struct zone_fstab *j = (struct zone_fstab *)m2; 848 849 return (strcmp(i->zone_fs_dir, j->zone_fs_dir)); 850 } 851 852 /* 853 * Fork and exec (and wait for) the mentioned binary with the provided 854 * arguments. Returns (-1) if something went wrong with fork(2) or exec(2), 855 * returns the exit status otherwise. 856 * 857 * If we were unable to exec the provided pathname (for whatever 858 * reason), we return the special token ZEXIT_EXEC. The current value 859 * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the 860 * consumers of this function; any future consumers must make sure this 861 * remains the case. 862 */ 863 static int 864 forkexec(zlog_t *zlogp, const char *path, char *const argv[]) 865 { 866 pid_t child_pid; 867 int child_status = 0; 868 869 /* 870 * Do not let another thread localize a message while we are forking. 871 */ 872 (void) mutex_lock(&msglock); 873 child_pid = fork(); 874 (void) mutex_unlock(&msglock); 875 if (child_pid == -1) { 876 zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]); 877 return (-1); 878 } else if (child_pid == 0) { 879 closefrom(0); 880 /* redirect stdin, stdout & stderr to /dev/null */ 881 (void) open("/dev/null", O_RDONLY); /* stdin */ 882 (void) open("/dev/null", O_WRONLY); /* stdout */ 883 (void) open("/dev/null", O_WRONLY); /* stderr */ 884 (void) execv(path, argv); 885 /* 886 * Since we are in the child, there is no point calling zerror() 887 * since there is nobody waiting to consume it. So exit with a 888 * special code that the parent will recognize and call zerror() 889 * accordingly. 890 */ 891 892 _exit(ZEXIT_EXEC); 893 } else { 894 (void) waitpid(child_pid, &child_status, 0); 895 } 896 897 if (WIFSIGNALED(child_status)) { 898 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to " 899 "signal %d", path, WTERMSIG(child_status)); 900 return (-1); 901 } 902 assert(WIFEXITED(child_status)); 903 if (WEXITSTATUS(child_status) == ZEXIT_EXEC) { 904 zerror(zlogp, B_FALSE, "failed to exec %s", path); 905 return (-1); 906 } 907 return (WEXITSTATUS(child_status)); 908 } 909 910 static int 911 dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev) 912 { 913 char cmdbuf[MAXPATHLEN]; 914 char *argv[4]; 915 int status; 916 917 /* 918 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but 919 * that would cost us an extra fork/exec without buying us anything. 920 */ 921 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype) 922 > sizeof (cmdbuf)) { 923 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 924 return (-1); 925 } 926 927 argv[0] = "fsck"; 928 argv[1] = "-m"; 929 argv[2] = (char *)rawdev; 930 argv[3] = NULL; 931 932 status = forkexec(zlogp, cmdbuf, argv); 933 if (status == 0 || status == -1) 934 return (status); 935 zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; " 936 "run fsck manually", rawdev, status); 937 return (-1); 938 } 939 940 static int 941 domount(zlog_t *zlogp, const char *fstype, const char *opts, 942 const char *special, const char *directory) 943 { 944 char cmdbuf[MAXPATHLEN]; 945 char *argv[6]; 946 int status; 947 948 /* 949 * We could alternatively have called /usr/sbin/mount -F <fstype>, but 950 * that would cost us an extra fork/exec without buying us anything. 951 */ 952 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype) 953 > sizeof (cmdbuf)) { 954 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype); 955 return (-1); 956 } 957 argv[0] = "mount"; 958 if (opts[0] == '\0') { 959 argv[1] = (char *)special; 960 argv[2] = (char *)directory; 961 argv[3] = NULL; 962 } else { 963 argv[1] = "-o"; 964 argv[2] = (char *)opts; 965 argv[3] = (char *)special; 966 argv[4] = (char *)directory; 967 argv[5] = NULL; 968 } 969 970 status = forkexec(zlogp, cmdbuf, argv); 971 if (status == 0 || status == -1) 972 return (status); 973 if (opts[0] == '\0') 974 zerror(zlogp, B_FALSE, "\"%s %s %s\" " 975 "failed with exit code %d", 976 cmdbuf, special, directory, status); 977 else 978 zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" " 979 "failed with exit code %d", 980 cmdbuf, opts, special, directory, status); 981 return (-1); 982 } 983 984 /* 985 * Make sure if a given path exists, it is not a sym-link, and is a directory. 986 */ 987 static int 988 check_path(zlog_t *zlogp, const char *path) 989 { 990 struct stat statbuf; 991 char respath[MAXPATHLEN]; 992 int res; 993 994 if (lstat(path, &statbuf) != 0) { 995 if (errno == ENOENT) 996 return (0); 997 zerror(zlogp, B_TRUE, "can't stat %s", path); 998 return (-1); 999 } 1000 if (S_ISLNK(statbuf.st_mode)) { 1001 zerror(zlogp, B_FALSE, "%s is a symlink", path); 1002 return (-1); 1003 } 1004 if (!S_ISDIR(statbuf.st_mode)) { 1005 if (is_system_labeled() && S_ISREG(statbuf.st_mode)) { 1006 /* 1007 * The need to mount readonly copies of 1008 * global zone /etc/ files is unique to 1009 * Trusted Extensions. 1010 * The check for /etc/ via strstr() is to 1011 * allow paths like $ZONEROOT/etc/passwd 1012 */ 1013 if (strstr(path, "/etc/") == NULL) { 1014 zerror(zlogp, B_FALSE, 1015 "%s is not in /etc", path); 1016 return (-1); 1017 } 1018 } else { 1019 zerror(zlogp, B_FALSE, "%s is not a directory", path); 1020 return (-1); 1021 } 1022 } 1023 if ((res = resolvepath(path, respath, sizeof (respath))) == -1) { 1024 zerror(zlogp, B_TRUE, "unable to resolve path %s", path); 1025 return (-1); 1026 } 1027 respath[res] = '\0'; 1028 if (strcmp(path, respath) != 0) { 1029 /* 1030 * We don't like ".."s and "."s throwing us off 1031 */ 1032 zerror(zlogp, B_FALSE, "%s is not a canonical path", path); 1033 return (-1); 1034 } 1035 return (0); 1036 } 1037 1038 /* 1039 * Check every component of rootpath/relpath. If any component fails (ie, 1040 * exists but isn't the canonical path to a directory), it is returned in 1041 * badpath, which is assumed to be at least of size MAXPATHLEN. 1042 * 1043 * Relpath must begin with '/'. 1044 */ 1045 static boolean_t 1046 valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *relpath) 1047 { 1048 char abspath[MAXPATHLEN], *slashp; 1049 1050 /* 1051 * Make sure abspath has at least one '/' after its rootpath 1052 * component, and ends with '/'. 1053 */ 1054 if (snprintf(abspath, sizeof (abspath), "%s%s/", rootpath, relpath) > 1055 sizeof (abspath)) { 1056 zerror(zlogp, B_FALSE, "pathname %s%s is too long", rootpath, 1057 relpath); 1058 return (B_FALSE); 1059 } 1060 1061 slashp = &abspath[strlen(rootpath)]; 1062 assert(*slashp == '/'); 1063 do { 1064 *slashp = '\0'; 1065 if (check_path(zlogp, abspath) != 0) 1066 return (B_FALSE); 1067 *slashp = '/'; 1068 slashp++; 1069 } while ((slashp = strchr(slashp, '/')) != NULL); 1070 return (B_TRUE); 1071 } 1072 1073 static int 1074 mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath) 1075 { 1076 char path[MAXPATHLEN]; 1077 char specpath[MAXPATHLEN]; 1078 char optstr[MAX_MNTOPT_STR]; 1079 zone_fsopt_t *optptr; 1080 1081 if (!valid_mount_path(zlogp, rootpath, fsptr->zone_fs_dir)) { 1082 zerror(zlogp, B_FALSE, "%s%s is not a valid mount point", 1083 rootpath, fsptr->zone_fs_dir); 1084 return (-1); 1085 } 1086 1087 if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir, 1088 DEFAULT_DIR_MODE) != 0) 1089 return (-1); 1090 1091 (void) snprintf(path, sizeof (path), "%s%s", rootpath, 1092 fsptr->zone_fs_dir); 1093 1094 if (strlen(fsptr->zone_fs_special) == 0) { 1095 /* 1096 * A zero-length special is how we distinguish IPDs from 1097 * general-purpose FSs. Make sure it mounts from a place that 1098 * can be seen via the alternate zone's root. 1099 */ 1100 if (snprintf(specpath, sizeof (specpath), "%s%s", 1101 zonecfg_get_root(), fsptr->zone_fs_dir) >= 1102 sizeof (specpath)) { 1103 zerror(zlogp, B_FALSE, "cannot mount %s: path too " 1104 "long in alternate root", fsptr->zone_fs_dir); 1105 return (-1); 1106 } 1107 if (zonecfg_in_alt_root()) 1108 resolve_lofs(zlogp, specpath, sizeof (specpath)); 1109 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, 1110 specpath, path) != 0) { 1111 zerror(zlogp, B_TRUE, "failed to loopback mount %s", 1112 specpath); 1113 return (-1); 1114 } 1115 return (0); 1116 } 1117 1118 /* 1119 * In general the strategy here is to do just as much verification as 1120 * necessary to avoid crashing or otherwise doing something bad; if the 1121 * administrator initiated the operation via zoneadm(1m), he'll get 1122 * auto-verification which will let him know what's wrong. If he 1123 * modifies the zone configuration of a running zone and doesn't attempt 1124 * to verify that it's OK we won't crash but won't bother trying to be 1125 * too helpful either. zoneadm verify is only a couple keystrokes away. 1126 */ 1127 if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) { 1128 zerror(zlogp, B_FALSE, "cannot mount %s on %s: " 1129 "invalid file-system type %s", fsptr->zone_fs_special, 1130 fsptr->zone_fs_dir, fsptr->zone_fs_type); 1131 return (-1); 1132 } 1133 1134 /* 1135 * If we're looking at an alternate root environment, then construct 1136 * read-only loopback mounts as necessary. For all lofs mounts, make 1137 * sure that the 'special' entry points inside the alternate root. (We 1138 * don't do this with other mounts, as devfs isn't in the alternate 1139 * root, and we need to assume the device environment is roughly the 1140 * same.) 1141 */ 1142 if (zonecfg_in_alt_root()) { 1143 struct stat64 st; 1144 1145 if (stat64(fsptr->zone_fs_special, &st) != -1 && 1146 S_ISBLK(st.st_mode) && 1147 check_lofs_needed(zlogp, fsptr) == -1) 1148 return (-1); 1149 if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) { 1150 if (snprintf(specpath, sizeof (specpath), "%s%s", 1151 zonecfg_get_root(), fsptr->zone_fs_special) >= 1152 sizeof (specpath)) { 1153 zerror(zlogp, B_FALSE, "cannot mount %s: path " 1154 "too long in alternate root", 1155 fsptr->zone_fs_special); 1156 return (-1); 1157 } 1158 resolve_lofs(zlogp, specpath, sizeof (specpath)); 1159 (void) strlcpy(fsptr->zone_fs_special, specpath, 1160 sizeof (fsptr->zone_fs_special)); 1161 } 1162 } 1163 1164 /* 1165 * Run 'fsck -m' if there's a device to fsck. 1166 */ 1167 if (fsptr->zone_fs_raw[0] != '\0' && 1168 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) 1169 return (-1); 1170 1171 /* 1172 * Build up mount option string. 1173 */ 1174 optstr[0] = '\0'; 1175 if (fsptr->zone_fs_options != NULL) { 1176 (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt, 1177 sizeof (optstr)); 1178 for (optptr = fsptr->zone_fs_options->zone_fsopt_next; 1179 optptr != NULL; optptr = optptr->zone_fsopt_next) { 1180 (void) strlcat(optstr, ",", sizeof (optstr)); 1181 (void) strlcat(optstr, optptr->zone_fsopt_opt, 1182 sizeof (optstr)); 1183 } 1184 } 1185 return (domount(zlogp, fsptr->zone_fs_type, optstr, 1186 fsptr->zone_fs_special, path)); 1187 } 1188 1189 static void 1190 free_fs_data(struct zone_fstab *fsarray, uint_t nelem) 1191 { 1192 uint_t i; 1193 1194 if (fsarray == NULL) 1195 return; 1196 for (i = 0; i < nelem; i++) 1197 zonecfg_free_fs_option_list(fsarray[i].zone_fs_options); 1198 free(fsarray); 1199 } 1200 1201 /* 1202 * This function constructs the miniroot-like "scratch zone" environment. If 1203 * it returns B_FALSE, then the error has already been logged. 1204 */ 1205 static boolean_t 1206 build_mounted(zlog_t *zlogp, char *rootpath, size_t rootlen, 1207 const char *zonepath) 1208 { 1209 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN]; 1210 char luroot[MAXPATHLEN]; 1211 const char **cpp; 1212 static const char *mkdirs[] = { 1213 "/system", "/system/contract", "/system/object", "/proc", 1214 "/dev", "/tmp", "/a", NULL 1215 }; 1216 static const char *localdirs[] = { 1217 "/etc", "/var", NULL 1218 }; 1219 static const char *loopdirs[] = { 1220 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform", 1221 "/usr", NULL 1222 }; 1223 static const char *tmpdirs[] = { 1224 "/tmp", "/var/run", NULL 1225 }; 1226 FILE *fp; 1227 struct stat st; 1228 char *altstr; 1229 uuid_t uuid; 1230 1231 /* 1232 * Construct a small Solaris environment, including the zone root 1233 * mounted on '/a' inside that environment. 1234 */ 1235 resolve_lofs(zlogp, rootpath, rootlen); 1236 (void) snprintf(luroot, sizeof (luroot), "%s/lu", zonepath); 1237 resolve_lofs(zlogp, luroot, sizeof (luroot)); 1238 (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot); 1239 (void) symlink("./usr/bin", tmp); 1240 1241 /* 1242 * These are mostly special mount points; not handled here. (See 1243 * zone_mount_early.) 1244 */ 1245 for (cpp = mkdirs; *cpp != NULL; cpp++) { 1246 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1247 if (mkdir(tmp, 0755) != 0) { 1248 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1249 return (B_FALSE); 1250 } 1251 } 1252 1253 /* 1254 * These are mounted read-write from the zone undergoing upgrade. We 1255 * must be careful not to 'leak' things from the main system into the 1256 * zone, and this accomplishes that goal. 1257 */ 1258 for (cpp = localdirs; *cpp != NULL; cpp++) { 1259 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1260 (void) snprintf(fromdir, sizeof (fromdir), "%s%s", rootpath, 1261 *cpp); 1262 if (mkdir(tmp, 0755) != 0) { 1263 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1264 return (B_FALSE); 1265 } 1266 if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp) != 0) { 1267 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1268 *cpp); 1269 return (B_FALSE); 1270 } 1271 } 1272 1273 /* 1274 * These are things mounted read-only from the running system because 1275 * they contain binaries that must match system. 1276 */ 1277 for (cpp = loopdirs; *cpp != NULL; cpp++) { 1278 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1279 if (mkdir(tmp, 0755) != 0) { 1280 if (errno != EEXIST) { 1281 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1282 return (B_FALSE); 1283 } 1284 if (lstat(tmp, &st) != 0) { 1285 zerror(zlogp, B_TRUE, "cannot stat %s", tmp); 1286 return (B_FALSE); 1287 } 1288 /* 1289 * Ignore any non-directories encountered. These are 1290 * things that have been converted into symlinks 1291 * (/etc/fs and /etc/lib) and no longer need a lofs 1292 * fixup. 1293 */ 1294 if (!S_ISDIR(st.st_mode)) 1295 continue; 1296 } 1297 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, *cpp, 1298 tmp) != 0) { 1299 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1300 *cpp); 1301 return (B_FALSE); 1302 } 1303 } 1304 1305 /* 1306 * These are things with tmpfs mounted inside. 1307 */ 1308 for (cpp = tmpdirs; *cpp != NULL; cpp++) { 1309 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp); 1310 if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 1311 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1312 return (B_FALSE); 1313 } 1314 if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) { 1315 zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp); 1316 return (B_FALSE); 1317 } 1318 } 1319 1320 /* 1321 * This is here to support lucopy. If there's an instance of this same 1322 * zone on the current running system, then we mount its root up as 1323 * read-only inside the scratch zone. 1324 */ 1325 (void) zonecfg_get_uuid(zone_name, uuid); 1326 altstr = strdup(zonecfg_get_root()); 1327 if (altstr == NULL) { 1328 zerror(zlogp, B_TRUE, "memory allocation failed"); 1329 return (B_FALSE); 1330 } 1331 zonecfg_set_root(""); 1332 (void) strlcpy(tmp, zone_name, sizeof (tmp)); 1333 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp)); 1334 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK && 1335 strcmp(fromdir, rootpath) != 0) { 1336 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot); 1337 if (mkdir(tmp, 0755) != 0) { 1338 zerror(zlogp, B_TRUE, "cannot create %s", tmp); 1339 return (B_FALSE); 1340 } 1341 if (domount(zlogp, MNTTYPE_LOFS, IPD_DEFAULT_OPTS, fromdir, 1342 tmp) != 0) { 1343 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp, 1344 fromdir); 1345 return (B_FALSE); 1346 } 1347 } 1348 zonecfg_set_root(altstr); 1349 free(altstr); 1350 1351 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) { 1352 zerror(zlogp, B_TRUE, "cannot open zone mapfile"); 1353 return (B_FALSE); 1354 } 1355 (void) ftruncate(fileno(fp), 0); 1356 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) { 1357 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry"); 1358 } 1359 zonecfg_close_scratch(fp); 1360 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot); 1361 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0) 1362 return (B_FALSE); 1363 (void) strlcpy(rootpath, tmp, rootlen); 1364 return (B_TRUE); 1365 } 1366 1367 static int 1368 mount_filesystems(zlog_t *zlogp, boolean_t mount_cmd) 1369 { 1370 char rootpath[MAXPATHLEN]; 1371 char zonepath[MAXPATHLEN]; 1372 int num_fs = 0, i; 1373 struct zone_fstab fstab, *fs_ptr = NULL, *tmp_ptr; 1374 struct zone_fstab *fsp; 1375 zone_dochandle_t handle = NULL; 1376 zone_state_t zstate; 1377 1378 if (zone_get_state(zone_name, &zstate) != Z_OK || 1379 (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) { 1380 zerror(zlogp, B_FALSE, 1381 "zone must be in '%s' or '%s' state to mount file-systems", 1382 zone_state_str(ZONE_STATE_READY), 1383 zone_state_str(ZONE_STATE_MOUNTED)); 1384 goto bad; 1385 } 1386 1387 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) { 1388 zerror(zlogp, B_TRUE, "unable to determine zone path"); 1389 goto bad; 1390 } 1391 1392 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 1393 zerror(zlogp, B_TRUE, "unable to determine zone root"); 1394 goto bad; 1395 } 1396 1397 if ((handle = zonecfg_init_handle()) == NULL) { 1398 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 1399 goto bad; 1400 } 1401 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK || 1402 zonecfg_setfsent(handle) != Z_OK) { 1403 zerror(zlogp, B_FALSE, "invalid configuration"); 1404 goto bad; 1405 } 1406 1407 /* 1408 * /dev in the zone is loopback'd from the external /dev repository, 1409 * in order to provide a largely read-only semantic. But because 1410 * processes in the zone need to be able to chown, chmod, etc. zone 1411 * /dev files, we can't use a 'ro' lofs mount. Instead we use a 1412 * special mode just for zones, "zonedevfs". 1413 * 1414 * In the future we should front /dev with a full-fledged filesystem. 1415 */ 1416 num_fs++; 1417 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) { 1418 zerror(zlogp, B_TRUE, "memory allocation failed"); 1419 num_fs--; 1420 goto bad; 1421 } 1422 fs_ptr = tmp_ptr; 1423 fsp = &fs_ptr[num_fs - 1]; 1424 /* 1425 * Note that mount_one will prepend the alternate root to 1426 * zone_fs_special and do the necessary resolution, so all that is 1427 * needed here is to strip the root added by zone_get_zonepath. 1428 */ 1429 (void) strlcpy(fsp->zone_fs_dir, "/dev", sizeof (fsp->zone_fs_dir)); 1430 (void) snprintf(fsp->zone_fs_special, sizeof (fsp->zone_fs_special), 1431 "%s/dev", zonepath + strlen(zonecfg_get_root())); 1432 fsp->zone_fs_raw[0] = '\0'; 1433 (void) strlcpy(fsp->zone_fs_type, MNTTYPE_LOFS, 1434 sizeof (fsp->zone_fs_type)); 1435 fsp->zone_fs_options = NULL; 1436 if (zonecfg_add_fs_option(fsp, MNTOPT_LOFS_ZONEDEVFS) != Z_OK) { 1437 zerror(zlogp, B_FALSE, "error adding property"); 1438 goto bad; 1439 } 1440 1441 /* 1442 * Iterate through the rest of the filesystems, first the IPDs, then 1443 * the general FSs. Sort them all, then mount them in sorted order. 1444 * This is to make sure the higher level directories (e.g., /usr) 1445 * get mounted before any beneath them (e.g., /usr/local). 1446 */ 1447 if (zonecfg_setipdent(handle) != Z_OK) { 1448 zerror(zlogp, B_FALSE, "invalid configuration"); 1449 goto bad; 1450 } 1451 while (zonecfg_getipdent(handle, &fstab) == Z_OK) { 1452 num_fs++; 1453 if ((tmp_ptr = realloc(fs_ptr, 1454 num_fs * sizeof (*tmp_ptr))) == NULL) { 1455 zerror(zlogp, B_TRUE, "memory allocation failed"); 1456 num_fs--; 1457 (void) zonecfg_endipdent(handle); 1458 goto bad; 1459 } 1460 fs_ptr = tmp_ptr; 1461 fsp = &fs_ptr[num_fs - 1]; 1462 /* 1463 * IPDs logically only have a mount point; all other properties 1464 * are implied. 1465 */ 1466 (void) strlcpy(fsp->zone_fs_dir, 1467 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 1468 fsp->zone_fs_special[0] = '\0'; 1469 fsp->zone_fs_raw[0] = '\0'; 1470 fsp->zone_fs_type[0] = '\0'; 1471 fsp->zone_fs_options = NULL; 1472 } 1473 (void) zonecfg_endipdent(handle); 1474 1475 if (zonecfg_setfsent(handle) != Z_OK) { 1476 zerror(zlogp, B_FALSE, "invalid configuration"); 1477 goto bad; 1478 } 1479 while (zonecfg_getfsent(handle, &fstab) == Z_OK) { 1480 /* 1481 * ZFS filesystems will not be accessible under an alternate 1482 * root, since the pool will not be known. Ignore them in this 1483 * case. 1484 */ 1485 if (mount_cmd && strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0) 1486 continue; 1487 1488 num_fs++; 1489 if ((tmp_ptr = realloc(fs_ptr, 1490 num_fs * sizeof (*tmp_ptr))) == NULL) { 1491 zerror(zlogp, B_TRUE, "memory allocation failed"); 1492 num_fs--; 1493 (void) zonecfg_endfsent(handle); 1494 goto bad; 1495 } 1496 fs_ptr = tmp_ptr; 1497 fsp = &fs_ptr[num_fs - 1]; 1498 (void) strlcpy(fsp->zone_fs_dir, 1499 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir)); 1500 (void) strlcpy(fsp->zone_fs_special, fstab.zone_fs_special, 1501 sizeof (fsp->zone_fs_special)); 1502 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw, 1503 sizeof (fsp->zone_fs_raw)); 1504 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type, 1505 sizeof (fsp->zone_fs_type)); 1506 fsp->zone_fs_options = fstab.zone_fs_options; 1507 } 1508 (void) zonecfg_endfsent(handle); 1509 zonecfg_fini_handle(handle); 1510 handle = NULL; 1511 1512 /* 1513 * If we're mounting a zone for administration, then we need to set up 1514 * the "/a" environment inside the zone so that the commands that run 1515 * in there have access to both the running system's utilities and the 1516 * to-be-modified zone's files. 1517 */ 1518 if (mount_cmd && 1519 !build_mounted(zlogp, rootpath, sizeof (rootpath), zonepath)) 1520 goto bad; 1521 1522 qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare); 1523 for (i = 0; i < num_fs; i++) { 1524 if (mount_cmd && strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) { 1525 size_t slen = strlen(rootpath) - 2; 1526 1527 /* /dev is special and always goes at the top */ 1528 rootpath[slen] = '\0'; 1529 if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 1530 goto bad; 1531 rootpath[slen] = '/'; 1532 continue; 1533 } 1534 if (mount_one(zlogp, &fs_ptr[i], rootpath) != 0) 1535 goto bad; 1536 } 1537 1538 /* 1539 * For Trusted Extensions cross-mount each lower level /export/home 1540 */ 1541 if (!mount_cmd && tsol_mounts(zlogp, zone_name, rootpath) != 0) 1542 goto bad; 1543 1544 free_fs_data(fs_ptr, num_fs); 1545 1546 /* 1547 * Everything looks fine. 1548 */ 1549 return (0); 1550 1551 bad: 1552 if (handle != NULL) 1553 zonecfg_fini_handle(handle); 1554 free_fs_data(fs_ptr, num_fs); 1555 return (-1); 1556 } 1557 1558 /* caller makes sure neither parameter is NULL */ 1559 static int 1560 addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr) 1561 { 1562 int prefixlen; 1563 1564 prefixlen = atoi(prefixstr); 1565 if (prefixlen < 0 || prefixlen > maxprefixlen) 1566 return (1); 1567 while (prefixlen > 0) { 1568 if (prefixlen >= 8) { 1569 *maskstr++ = 0xFF; 1570 prefixlen -= 8; 1571 continue; 1572 } 1573 *maskstr |= 1 << (8 - prefixlen); 1574 prefixlen--; 1575 } 1576 return (0); 1577 } 1578 1579 /* 1580 * Tear down all interfaces belonging to the given zone. This should 1581 * be called with the zone in a state other than "running", so that 1582 * interfaces can't be assigned to the zone after this returns. 1583 * 1584 * If anything goes wrong, log an error message and return an error. 1585 */ 1586 static int 1587 unconfigure_network_interfaces(zlog_t *zlogp, zoneid_t zone_id) 1588 { 1589 struct lifnum lifn; 1590 struct lifconf lifc; 1591 struct lifreq *lifrp, lifrl; 1592 int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES; 1593 int num_ifs, s, i, ret_code = 0; 1594 uint_t bufsize; 1595 char *buf = NULL; 1596 1597 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 1598 zerror(zlogp, B_TRUE, "could not get socket"); 1599 ret_code = -1; 1600 goto bad; 1601 } 1602 lifn.lifn_family = AF_UNSPEC; 1603 lifn.lifn_flags = (int)lifc_flags; 1604 if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) { 1605 zerror(zlogp, B_TRUE, 1606 "could not determine number of interfaces"); 1607 ret_code = -1; 1608 goto bad; 1609 } 1610 num_ifs = lifn.lifn_count; 1611 bufsize = num_ifs * sizeof (struct lifreq); 1612 if ((buf = malloc(bufsize)) == NULL) { 1613 zerror(zlogp, B_TRUE, "memory allocation failed"); 1614 ret_code = -1; 1615 goto bad; 1616 } 1617 lifc.lifc_family = AF_UNSPEC; 1618 lifc.lifc_flags = (int)lifc_flags; 1619 lifc.lifc_len = bufsize; 1620 lifc.lifc_buf = buf; 1621 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) { 1622 zerror(zlogp, B_TRUE, "could not get configured interfaces"); 1623 ret_code = -1; 1624 goto bad; 1625 } 1626 lifrp = lifc.lifc_req; 1627 for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) { 1628 (void) close(s); 1629 if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) < 1630 0) { 1631 zerror(zlogp, B_TRUE, "%s: could not get socket", 1632 lifrl.lifr_name); 1633 ret_code = -1; 1634 continue; 1635 } 1636 (void) memset(&lifrl, 0, sizeof (lifrl)); 1637 (void) strncpy(lifrl.lifr_name, lifrp->lifr_name, 1638 sizeof (lifrl.lifr_name)); 1639 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) { 1640 zerror(zlogp, B_TRUE, 1641 "%s: could not determine zone interface belongs to", 1642 lifrl.lifr_name); 1643 ret_code = -1; 1644 continue; 1645 } 1646 if (lifrl.lifr_zoneid == zone_id) { 1647 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) { 1648 zerror(zlogp, B_TRUE, 1649 "%s: could not remove interface", 1650 lifrl.lifr_name); 1651 ret_code = -1; 1652 continue; 1653 } 1654 } 1655 } 1656 bad: 1657 if (s > 0) 1658 (void) close(s); 1659 if (buf) 1660 free(buf); 1661 return (ret_code); 1662 } 1663 1664 static union sockunion { 1665 struct sockaddr sa; 1666 struct sockaddr_in sin; 1667 struct sockaddr_dl sdl; 1668 struct sockaddr_in6 sin6; 1669 } so_dst, so_ifp; 1670 1671 static struct { 1672 struct rt_msghdr hdr; 1673 char space[512]; 1674 } rtmsg; 1675 1676 static int 1677 salen(struct sockaddr *sa) 1678 { 1679 switch (sa->sa_family) { 1680 case AF_INET: 1681 return (sizeof (struct sockaddr_in)); 1682 case AF_LINK: 1683 return (sizeof (struct sockaddr_dl)); 1684 case AF_INET6: 1685 return (sizeof (struct sockaddr_in6)); 1686 default: 1687 return (sizeof (struct sockaddr)); 1688 } 1689 } 1690 1691 #define ROUNDUP_LONG(a) \ 1692 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 1693 1694 /* 1695 * Look up which zone is using a given IP address. The address in question 1696 * is expected to have been stuffed into the structure to which lifr points 1697 * via a previous SIOCGLIFADDR ioctl(). 1698 * 1699 * This is done using black router socket magic. 1700 * 1701 * Return the name of the zone on success or NULL on failure. 1702 * 1703 * This is a lot of code for a simple task; a new ioctl request to take care 1704 * of this might be a useful RFE. 1705 */ 1706 1707 static char * 1708 who_is_using(zlog_t *zlogp, struct lifreq *lifr) 1709 { 1710 static char answer[ZONENAME_MAX]; 1711 pid_t pid; 1712 int s, rlen, l, i; 1713 char *cp = rtmsg.space; 1714 struct sockaddr_dl *ifp = NULL; 1715 struct sockaddr *sa; 1716 char save_if_name[LIFNAMSIZ]; 1717 1718 answer[0] = '\0'; 1719 1720 pid = getpid(); 1721 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) { 1722 zerror(zlogp, B_TRUE, "could not get routing socket"); 1723 return (NULL); 1724 } 1725 1726 if (lifr->lifr_addr.ss_family == AF_INET) { 1727 struct sockaddr_in *sin4; 1728 1729 so_dst.sa.sa_family = AF_INET; 1730 sin4 = (struct sockaddr_in *)&lifr->lifr_addr; 1731 so_dst.sin.sin_addr = sin4->sin_addr; 1732 } else { 1733 struct sockaddr_in6 *sin6; 1734 1735 so_dst.sa.sa_family = AF_INET6; 1736 sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr; 1737 so_dst.sin6.sin6_addr = sin6->sin6_addr; 1738 } 1739 1740 so_ifp.sa.sa_family = AF_LINK; 1741 1742 (void) memset(&rtmsg, 0, sizeof (rtmsg)); 1743 rtmsg.hdr.rtm_type = RTM_GET; 1744 rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST; 1745 rtmsg.hdr.rtm_version = RTM_VERSION; 1746 rtmsg.hdr.rtm_seq = ++rts_seqno; 1747 rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST; 1748 1749 l = ROUNDUP_LONG(salen(&so_dst.sa)); 1750 (void) memmove(cp, &(so_dst), l); 1751 cp += l; 1752 l = ROUNDUP_LONG(salen(&so_ifp.sa)); 1753 (void) memmove(cp, &(so_ifp), l); 1754 cp += l; 1755 1756 rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg; 1757 1758 if ((rlen = write(s, &rtmsg, l)) < 0) { 1759 zerror(zlogp, B_TRUE, "writing to routing socket"); 1760 return (NULL); 1761 } else if (rlen < (int)rtmsg.hdr.rtm_msglen) { 1762 zerror(zlogp, B_TRUE, 1763 "write to routing socket got only %d for len\n", rlen); 1764 return (NULL); 1765 } 1766 do { 1767 l = read(s, &rtmsg, sizeof (rtmsg)); 1768 } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno || 1769 rtmsg.hdr.rtm_pid != pid)); 1770 if (l < 0) { 1771 zerror(zlogp, B_TRUE, "reading from routing socket"); 1772 return (NULL); 1773 } 1774 1775 if (rtmsg.hdr.rtm_version != RTM_VERSION) { 1776 zerror(zlogp, B_FALSE, 1777 "routing message version %d not understood", 1778 rtmsg.hdr.rtm_version); 1779 return (NULL); 1780 } 1781 if (rtmsg.hdr.rtm_msglen != (ushort_t)l) { 1782 zerror(zlogp, B_FALSE, "message length mismatch, " 1783 "expected %d bytes, returned %d bytes", 1784 rtmsg.hdr.rtm_msglen, l); 1785 return (NULL); 1786 } 1787 if (rtmsg.hdr.rtm_errno != 0) { 1788 errno = rtmsg.hdr.rtm_errno; 1789 zerror(zlogp, B_TRUE, "RTM_GET routing socket message"); 1790 return (NULL); 1791 } 1792 if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) { 1793 zerror(zlogp, B_FALSE, "interface not found"); 1794 return (NULL); 1795 } 1796 cp = ((char *)(&rtmsg.hdr + 1)); 1797 for (i = 1; i != 0; i <<= 1) { 1798 /* LINTED E_BAD_PTR_CAST_ALIGN */ 1799 sa = (struct sockaddr *)cp; 1800 if (i != RTA_IFP) { 1801 if ((i & rtmsg.hdr.rtm_addrs) != 0) 1802 cp += ROUNDUP_LONG(salen(sa)); 1803 continue; 1804 } 1805 if (sa->sa_family == AF_LINK && 1806 ((struct sockaddr_dl *)sa)->sdl_nlen != 0) 1807 ifp = (struct sockaddr_dl *)sa; 1808 break; 1809 } 1810 if (ifp == NULL) { 1811 zerror(zlogp, B_FALSE, "interface could not be determined"); 1812 return (NULL); 1813 } 1814 1815 /* 1816 * We need to set the I/F name to what we got above, then do the 1817 * appropriate ioctl to get its zone name. But lifr->lifr_name is 1818 * used by the calling function to do a REMOVEIF, so if we leave the 1819 * "good" zone's I/F name in place, *that* I/F will be removed instead 1820 * of the bad one. So we save the old (bad) I/F name before over- 1821 * writing it and doing the ioctl, then restore it after the ioctl. 1822 */ 1823 (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name)); 1824 (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen); 1825 lifr->lifr_name[ifp->sdl_nlen] = '\0'; 1826 i = ioctl(s, SIOCGLIFZONE, lifr); 1827 (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name)); 1828 if (i < 0) { 1829 zerror(zlogp, B_TRUE, 1830 "%s: could not determine the zone interface belongs to", 1831 lifr->lifr_name); 1832 return (NULL); 1833 } 1834 if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0) 1835 (void) snprintf(answer, sizeof (answer), "%d", 1836 lifr->lifr_zoneid); 1837 1838 if (strlen(answer) > 0) 1839 return (answer); 1840 return (NULL); 1841 } 1842 1843 typedef struct mcast_rtmsg_s { 1844 struct rt_msghdr m_rtm; 1845 union { 1846 struct { 1847 struct sockaddr_in m_dst; 1848 struct sockaddr_in m_gw; 1849 struct sockaddr_in m_netmask; 1850 } m_v4; 1851 struct { 1852 struct sockaddr_in6 m_dst; 1853 struct sockaddr_in6 m_gw; 1854 struct sockaddr_in6 m_netmask; 1855 } m_v6; 1856 } m_u; 1857 } mcast_rtmsg_t; 1858 #define m_dst4 m_u.m_v4.m_dst 1859 #define m_dst6 m_u.m_v6.m_dst 1860 #define m_gw4 m_u.m_v4.m_gw 1861 #define m_gw6 m_u.m_v6.m_gw 1862 #define m_netmask4 m_u.m_v4.m_netmask 1863 #define m_netmask6 m_u.m_v6.m_netmask 1864 1865 /* 1866 * Configures a single interface: a new virtual interface is added, based on 1867 * the physical interface nwiftabptr->zone_nwif_physical, with the address 1868 * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that 1869 * the "address" can be an IPv6 address (with a /prefixlength required), an 1870 * IPv4 address (with a /prefixlength optional), or a name; for the latter, 1871 * an IPv4 name-to-address resolution will be attempted. 1872 * 1873 * A default interface route for multicast is created on the first IPv4 and 1874 * IPv6 interfaces (that have the IFF_MULTICAST flag set), respectively. 1875 * This should really be done in the init scripts if we ever allow zones to 1876 * modify the routing tables. 1877 * 1878 * If anything goes wrong, we log an detailed error message, attempt to tear 1879 * down whatever we set up and return an error. 1880 */ 1881 static int 1882 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id, 1883 struct zone_nwiftab *nwiftabptr, boolean_t *mcast_rt_v4_setp, 1884 boolean_t *mcast_rt_v6_setp) 1885 { 1886 struct lifreq lifr; 1887 struct sockaddr_in netmask4; 1888 struct sockaddr_in6 netmask6; 1889 struct in_addr in4; 1890 struct in6_addr in6; 1891 sa_family_t af; 1892 char *slashp = strchr(nwiftabptr->zone_nwif_address, '/'); 1893 mcast_rtmsg_t mcast_rtmsg; 1894 int s; 1895 int rs; 1896 int rlen; 1897 boolean_t got_netmask = B_FALSE; 1898 char addrstr4[INET_ADDRSTRLEN]; 1899 int res; 1900 1901 res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr); 1902 if (res != Z_OK) { 1903 zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res), 1904 nwiftabptr->zone_nwif_address); 1905 return (-1); 1906 } 1907 af = lifr.lifr_addr.ss_family; 1908 if (af == AF_INET) 1909 in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr; 1910 else 1911 in6 = ((struct sockaddr_in6 *)(&lifr.lifr_addr))->sin6_addr; 1912 1913 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) { 1914 zerror(zlogp, B_TRUE, "could not get socket"); 1915 return (-1); 1916 } 1917 1918 (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical, 1919 sizeof (lifr.lifr_name)); 1920 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) { 1921 /* 1922 * Here, we know that the interface can't be brought up. 1923 * A similar warning message was already printed out to 1924 * the console by zoneadm(1M) so instead we log the 1925 * message to syslog and continue. 1926 */ 1927 zerror(&logsys, B_TRUE, "WARNING: skipping interface " 1928 "'%s' which may not be present/plumbed in the " 1929 "global zone.", lifr.lifr_name); 1930 (void) close(s); 1931 return (Z_OK); 1932 } 1933 1934 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 1935 zerror(zlogp, B_TRUE, 1936 "%s: could not set IP address to %s", 1937 lifr.lifr_name, nwiftabptr->zone_nwif_address); 1938 goto bad; 1939 } 1940 1941 /* Preserve literal IPv4 address for later potential printing. */ 1942 if (af == AF_INET) 1943 (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN); 1944 1945 lifr.lifr_zoneid = zone_id; 1946 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) { 1947 zerror(zlogp, B_TRUE, "%s: could not place interface into zone", 1948 lifr.lifr_name); 1949 goto bad; 1950 } 1951 1952 if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) { 1953 got_netmask = B_TRUE; /* default setting will be correct */ 1954 } else { 1955 if (af == AF_INET) { 1956 /* 1957 * The IPv4 netmask can be determined either 1958 * directly if a prefix length was supplied with 1959 * the address or via the netmasks database. Not 1960 * being able to determine it is a common failure, 1961 * but it often is not fatal to operation of the 1962 * interface. In that case, a warning will be 1963 * printed after the rest of the interface's 1964 * parameters have been configured. 1965 */ 1966 (void) memset(&netmask4, 0, sizeof (netmask4)); 1967 if (slashp != NULL) { 1968 if (addr2netmask(slashp + 1, V4_ADDR_LEN, 1969 (uchar_t *)&netmask4.sin_addr) != 0) { 1970 *slashp = '/'; 1971 zerror(zlogp, B_FALSE, 1972 "%s: invalid prefix length in %s", 1973 lifr.lifr_name, 1974 nwiftabptr->zone_nwif_address); 1975 goto bad; 1976 } 1977 got_netmask = B_TRUE; 1978 } else if (getnetmaskbyaddr(in4, 1979 &netmask4.sin_addr) == 0) { 1980 got_netmask = B_TRUE; 1981 } 1982 if (got_netmask) { 1983 netmask4.sin_family = af; 1984 (void) memcpy(&lifr.lifr_addr, &netmask4, 1985 sizeof (netmask4)); 1986 } 1987 } else { 1988 (void) memset(&netmask6, 0, sizeof (netmask6)); 1989 if (addr2netmask(slashp + 1, V6_ADDR_LEN, 1990 (uchar_t *)&netmask6.sin6_addr) != 0) { 1991 *slashp = '/'; 1992 zerror(zlogp, B_FALSE, 1993 "%s: invalid prefix length in %s", 1994 lifr.lifr_name, 1995 nwiftabptr->zone_nwif_address); 1996 goto bad; 1997 } 1998 got_netmask = B_TRUE; 1999 netmask6.sin6_family = af; 2000 (void) memcpy(&lifr.lifr_addr, &netmask6, 2001 sizeof (netmask6)); 2002 } 2003 if (got_netmask && 2004 ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) { 2005 zerror(zlogp, B_TRUE, "%s: could not set netmask", 2006 lifr.lifr_name); 2007 goto bad; 2008 } 2009 2010 /* 2011 * This doesn't set the broadcast address at all. Rather, it 2012 * gets, then sets the interface's address, relying on the fact 2013 * that resetting the address will reset the broadcast address. 2014 */ 2015 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 2016 zerror(zlogp, B_TRUE, "%s: could not get address", 2017 lifr.lifr_name); 2018 goto bad; 2019 } 2020 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) { 2021 zerror(zlogp, B_TRUE, 2022 "%s: could not reset broadcast address", 2023 lifr.lifr_name); 2024 goto bad; 2025 } 2026 } 2027 2028 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) { 2029 zerror(zlogp, B_TRUE, "%s: could not get flags", 2030 lifr.lifr_name); 2031 goto bad; 2032 } 2033 lifr.lifr_flags |= IFF_UP; 2034 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) { 2035 int save_errno = errno; 2036 char *zone_using; 2037 2038 /* 2039 * If we failed with something other than EADDRNOTAVAIL, 2040 * then skip to the end. Otherwise, look up our address, 2041 * then call a function to determine which zone is already 2042 * using that address. 2043 */ 2044 if (errno != EADDRNOTAVAIL) { 2045 zerror(zlogp, B_TRUE, 2046 "%s: could not bring interface up", lifr.lifr_name); 2047 goto bad; 2048 } 2049 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) { 2050 zerror(zlogp, B_TRUE, "%s: could not get address", 2051 lifr.lifr_name); 2052 goto bad; 2053 } 2054 zone_using = who_is_using(zlogp, &lifr); 2055 errno = save_errno; 2056 if (zone_using == NULL) 2057 zerror(zlogp, B_TRUE, 2058 "%s: could not bring interface up", lifr.lifr_name); 2059 else 2060 zerror(zlogp, B_TRUE, "%s: could not bring interface " 2061 "up: address in use by zone '%s'", lifr.lifr_name, 2062 zone_using); 2063 goto bad; 2064 } 2065 if ((lifr.lifr_flags & IFF_MULTICAST) && ((af == AF_INET && 2066 mcast_rt_v4_setp != NULL && *mcast_rt_v4_setp == B_FALSE) || 2067 (af == AF_INET6 && 2068 mcast_rt_v6_setp != NULL && *mcast_rt_v6_setp == B_FALSE))) { 2069 rs = socket(PF_ROUTE, SOCK_RAW, 0); 2070 if (rs < 0) { 2071 zerror(zlogp, B_TRUE, "%s: could not create " 2072 "routing socket", lifr.lifr_name); 2073 goto bad; 2074 } 2075 (void) shutdown(rs, 0); 2076 (void) memset((void *)&mcast_rtmsg, 0, sizeof (mcast_rtmsg_t)); 2077 mcast_rtmsg.m_rtm.rtm_msglen = sizeof (struct rt_msghdr) + 2078 3 * (af == AF_INET ? sizeof (struct sockaddr_in) : 2079 sizeof (struct sockaddr_in6)); 2080 mcast_rtmsg.m_rtm.rtm_version = RTM_VERSION; 2081 mcast_rtmsg.m_rtm.rtm_type = RTM_ADD; 2082 mcast_rtmsg.m_rtm.rtm_flags = RTF_UP; 2083 mcast_rtmsg.m_rtm.rtm_addrs = 2084 RTA_DST | RTA_GATEWAY | RTA_NETMASK; 2085 mcast_rtmsg.m_rtm.rtm_seq = ++rts_seqno; 2086 if (af == AF_INET) { 2087 mcast_rtmsg.m_dst4.sin_family = AF_INET; 2088 mcast_rtmsg.m_dst4.sin_addr.s_addr = 2089 htonl(INADDR_UNSPEC_GROUP); 2090 mcast_rtmsg.m_gw4.sin_family = AF_INET; 2091 mcast_rtmsg.m_gw4.sin_addr = in4; 2092 mcast_rtmsg.m_netmask4.sin_family = AF_INET; 2093 mcast_rtmsg.m_netmask4.sin_addr.s_addr = 2094 htonl(IN_CLASSD_NET); 2095 } else { 2096 mcast_rtmsg.m_dst6.sin6_family = AF_INET6; 2097 mcast_rtmsg.m_dst6.sin6_addr.s6_addr[0] = 0xffU; 2098 mcast_rtmsg.m_gw6.sin6_family = AF_INET6; 2099 mcast_rtmsg.m_gw6.sin6_addr = in6; 2100 mcast_rtmsg.m_netmask6.sin6_family = AF_INET6; 2101 mcast_rtmsg.m_netmask6.sin6_addr.s6_addr[0] = 0xffU; 2102 } 2103 rlen = write(rs, (char *)&mcast_rtmsg, 2104 mcast_rtmsg.m_rtm.rtm_msglen); 2105 /* 2106 * The write to the multicast socket will fail if the 2107 * interface belongs to a failed IPMP group. This is a 2108 * non-fatal error and the zone will continue booting. 2109 * While the zone is running, if any interface in the 2110 * failed IPMP group recovers, the zone will fallback to 2111 * using that interface. 2112 */ 2113 if (rlen < mcast_rtmsg.m_rtm.rtm_msglen) { 2114 if (rlen < 0) { 2115 zerror(zlogp, B_TRUE, "WARNING: interface " 2116 "'%s' not available as default for " 2117 "multicast.", lifr.lifr_name); 2118 } else { 2119 zerror(zlogp, B_FALSE, "WARNING: interface " 2120 "'%s' not available as default for " 2121 "multicast; routing socket returned " 2122 "unexpected %d bytes.", 2123 lifr.lifr_name, rlen); 2124 } 2125 } else { 2126 2127 if (af == AF_INET) { 2128 *mcast_rt_v4_setp = B_TRUE; 2129 } else { 2130 *mcast_rt_v6_setp = B_TRUE; 2131 } 2132 } 2133 (void) close(rs); 2134 } 2135 2136 if (!got_netmask) { 2137 /* 2138 * A common, but often non-fatal problem, is that the system 2139 * cannot find the netmask for an interface address. This is 2140 * often caused by it being only in /etc/inet/netmasks, but 2141 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not 2142 * in that. This doesn't show up at boot because the netmask 2143 * is obtained from /etc/inet/netmasks when no network 2144 * interfaces are up, but isn't consulted when NIS/NIS+ is 2145 * available. We warn the user here that something like this 2146 * has happened and we're just running with a default and 2147 * possible incorrect netmask. 2148 */ 2149 char buffer[INET6_ADDRSTRLEN]; 2150 void *addr; 2151 2152 if (af == AF_INET) 2153 addr = &((struct sockaddr_in *) 2154 (&lifr.lifr_addr))->sin_addr; 2155 else 2156 addr = &((struct sockaddr_in6 *) 2157 (&lifr.lifr_addr))->sin6_addr; 2158 2159 /* Find out what netmask interface is going to be using */ 2160 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 || 2161 inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) 2162 goto bad; 2163 zerror(zlogp, B_FALSE, 2164 "WARNING: %s: no matching subnet found in netmasks(4) for " 2165 "%s; using default of %s.", 2166 lifr.lifr_name, addrstr4, buffer); 2167 } 2168 2169 (void) close(s); 2170 return (Z_OK); 2171 bad: 2172 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr); 2173 (void) close(s); 2174 return (-1); 2175 } 2176 2177 /* 2178 * Sets up network interfaces based on information from the zone configuration. 2179 * An IPv4 loopback interface is set up "for free", modeling the global system. 2180 * If any of the configuration interfaces were IPv6, then an IPv6 loopback 2181 * address is set up as well. 2182 * 2183 * If anything goes wrong, we log a general error message, attempt to tear down 2184 * whatever we set up, and return an error. 2185 */ 2186 static int 2187 configure_network_interfaces(zlog_t *zlogp) 2188 { 2189 zone_dochandle_t handle; 2190 struct zone_nwiftab nwiftab, loopback_iftab; 2191 boolean_t saw_v6 = B_FALSE; 2192 boolean_t mcast_rt_v4_set = B_FALSE; 2193 boolean_t mcast_rt_v6_set = B_FALSE; 2194 zoneid_t zoneid; 2195 2196 if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) { 2197 zerror(zlogp, B_TRUE, "unable to get zoneid"); 2198 return (-1); 2199 } 2200 2201 if ((handle = zonecfg_init_handle()) == NULL) { 2202 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2203 return (-1); 2204 } 2205 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2206 zerror(zlogp, B_FALSE, "invalid configuration"); 2207 zonecfg_fini_handle(handle); 2208 return (-1); 2209 } 2210 if (zonecfg_setnwifent(handle) == Z_OK) { 2211 for (;;) { 2212 struct in6_addr in6; 2213 2214 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK) 2215 break; 2216 if (configure_one_interface(zlogp, zoneid, 2217 &nwiftab, &mcast_rt_v4_set, &mcast_rt_v6_set) != 2218 Z_OK) { 2219 (void) zonecfg_endnwifent(handle); 2220 zonecfg_fini_handle(handle); 2221 return (-1); 2222 } 2223 if (inet_pton(AF_INET6, nwiftab.zone_nwif_address, 2224 &in6) == 1) 2225 saw_v6 = B_TRUE; 2226 } 2227 (void) zonecfg_endnwifent(handle); 2228 } 2229 zonecfg_fini_handle(handle); 2230 (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0", 2231 sizeof (loopback_iftab.zone_nwif_physical)); 2232 (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1", 2233 sizeof (loopback_iftab.zone_nwif_address)); 2234 if (configure_one_interface(zlogp, zoneid, &loopback_iftab, NULL, NULL) 2235 != Z_OK) { 2236 return (-1); 2237 } 2238 if (saw_v6) { 2239 (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128", 2240 sizeof (loopback_iftab.zone_nwif_address)); 2241 if (configure_one_interface(zlogp, zoneid, 2242 &loopback_iftab, NULL, NULL) != Z_OK) { 2243 return (-1); 2244 } 2245 } 2246 return (0); 2247 } 2248 2249 static int 2250 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid, 2251 const struct sockaddr_storage *local, const struct sockaddr_storage *remote) 2252 { 2253 int fd; 2254 struct strioctl ioc; 2255 tcp_ioc_abort_conn_t conn; 2256 int error; 2257 2258 conn.ac_local = *local; 2259 conn.ac_remote = *remote; 2260 conn.ac_start = TCPS_SYN_SENT; 2261 conn.ac_end = TCPS_TIME_WAIT; 2262 conn.ac_zoneid = zoneid; 2263 2264 ioc.ic_cmd = TCP_IOC_ABORT_CONN; 2265 ioc.ic_timout = -1; /* infinite timeout */ 2266 ioc.ic_len = sizeof (conn); 2267 ioc.ic_dp = (char *)&conn; 2268 2269 if ((fd = open("/dev/tcp", O_RDONLY)) < 0) { 2270 zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp"); 2271 return (-1); 2272 } 2273 2274 error = ioctl(fd, I_STR, &ioc); 2275 (void) close(fd); 2276 if (error == 0 || errno == ENOENT) /* ENOENT is not an error */ 2277 return (0); 2278 return (-1); 2279 } 2280 2281 static int 2282 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid) 2283 { 2284 struct sockaddr_storage l, r; 2285 struct sockaddr_in *local, *remote; 2286 struct sockaddr_in6 *local6, *remote6; 2287 int error; 2288 2289 /* 2290 * Abort IPv4 connections. 2291 */ 2292 bzero(&l, sizeof (*local)); 2293 local = (struct sockaddr_in *)&l; 2294 local->sin_family = AF_INET; 2295 local->sin_addr.s_addr = INADDR_ANY; 2296 local->sin_port = 0; 2297 2298 bzero(&r, sizeof (*remote)); 2299 remote = (struct sockaddr_in *)&r; 2300 remote->sin_family = AF_INET; 2301 remote->sin_addr.s_addr = INADDR_ANY; 2302 remote->sin_port = 0; 2303 2304 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 2305 return (error); 2306 2307 /* 2308 * Abort IPv6 connections. 2309 */ 2310 bzero(&l, sizeof (*local6)); 2311 local6 = (struct sockaddr_in6 *)&l; 2312 local6->sin6_family = AF_INET6; 2313 local6->sin6_port = 0; 2314 local6->sin6_addr = in6addr_any; 2315 2316 bzero(&r, sizeof (*remote6)); 2317 remote6 = (struct sockaddr_in6 *)&r; 2318 remote6->sin6_family = AF_INET6; 2319 remote6->sin6_port = 0; 2320 remote6->sin6_addr = in6addr_any; 2321 2322 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0) 2323 return (error); 2324 return (0); 2325 } 2326 2327 static int 2328 devfsadm_call(zlog_t *zlogp, const char *arg) 2329 { 2330 char *argv[4]; 2331 int status; 2332 2333 argv[0] = DEVFSADM; 2334 argv[1] = (char *)arg; 2335 argv[2] = zone_name; 2336 argv[3] = NULL; 2337 status = forkexec(zlogp, DEVFSADM_PATH, argv); 2338 if (status == 0 || status == -1) 2339 return (status); 2340 zerror(zlogp, B_FALSE, "%s call (%s %s %s) unexpectedly returned %d", 2341 DEVFSADM, DEVFSADM_PATH, arg, zone_name, status); 2342 return (-1); 2343 } 2344 2345 static int 2346 devfsadm_register(zlog_t *zlogp) 2347 { 2348 /* 2349 * Ready the zone's devices. 2350 */ 2351 return (devfsadm_call(zlogp, "-z")); 2352 } 2353 2354 static int 2355 devfsadm_unregister(zlog_t *zlogp) 2356 { 2357 return (devfsadm_call(zlogp, "-Z")); 2358 } 2359 2360 static int 2361 get_privset(zlog_t *zlogp, priv_set_t *privs, boolean_t mount_cmd) 2362 { 2363 int error = -1; 2364 zone_dochandle_t handle; 2365 char *privname = NULL; 2366 2367 if (mount_cmd) { 2368 if (zonecfg_default_privset(privs) == Z_OK) 2369 return (0); 2370 zerror(zlogp, B_FALSE, 2371 "failed to determine the zone's default privilege set"); 2372 return (-1); 2373 } 2374 2375 if ((handle = zonecfg_init_handle()) == NULL) { 2376 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2377 return (-1); 2378 } 2379 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2380 zerror(zlogp, B_FALSE, "invalid configuration"); 2381 zonecfg_fini_handle(handle); 2382 return (-1); 2383 } 2384 2385 switch (zonecfg_get_privset(handle, privs, &privname)) { 2386 case Z_OK: 2387 error = 0; 2388 break; 2389 case Z_PRIV_PROHIBITED: 2390 zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted " 2391 "within the zone's privilege set", privname); 2392 break; 2393 case Z_PRIV_REQUIRED: 2394 zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing " 2395 "from the zone's privilege set", privname); 2396 break; 2397 case Z_PRIV_UNKNOWN: 2398 zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified " 2399 "in the zone's privilege set", privname); 2400 break; 2401 default: 2402 zerror(zlogp, B_FALSE, "failed to determine the zone's " 2403 "privilege set"); 2404 break; 2405 } 2406 2407 free(privname); 2408 zonecfg_fini_handle(handle); 2409 return (error); 2410 } 2411 2412 static int 2413 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2414 { 2415 nvlist_t *nvl = NULL; 2416 char *nvl_packed = NULL; 2417 size_t nvl_size = 0; 2418 nvlist_t **nvlv = NULL; 2419 int rctlcount = 0; 2420 int error = -1; 2421 zone_dochandle_t handle; 2422 struct zone_rctltab rctltab; 2423 rctlblk_t *rctlblk = NULL; 2424 2425 *bufp = NULL; 2426 *bufsizep = 0; 2427 2428 if ((handle = zonecfg_init_handle()) == NULL) { 2429 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2430 return (-1); 2431 } 2432 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2433 zerror(zlogp, B_FALSE, "invalid configuration"); 2434 zonecfg_fini_handle(handle); 2435 return (-1); 2436 } 2437 2438 rctltab.zone_rctl_valptr = NULL; 2439 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 2440 zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc"); 2441 goto out; 2442 } 2443 2444 if (zonecfg_setrctlent(handle) != Z_OK) { 2445 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent"); 2446 goto out; 2447 } 2448 2449 if ((rctlblk = malloc(rctlblk_size())) == NULL) { 2450 zerror(zlogp, B_TRUE, "memory allocation failed"); 2451 goto out; 2452 } 2453 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) { 2454 struct zone_rctlvaltab *rctlval; 2455 uint_t i, count; 2456 const char *name = rctltab.zone_rctl_name; 2457 2458 /* zoneadm should have already warned about unknown rctls. */ 2459 if (!zonecfg_is_rctl(name)) { 2460 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2461 rctltab.zone_rctl_valptr = NULL; 2462 continue; 2463 } 2464 count = 0; 2465 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 2466 rctlval = rctlval->zone_rctlval_next) { 2467 count++; 2468 } 2469 if (count == 0) { /* ignore */ 2470 continue; /* Nothing to free */ 2471 } 2472 if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL) 2473 goto out; 2474 i = 0; 2475 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL; 2476 rctlval = rctlval->zone_rctlval_next, i++) { 2477 if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) { 2478 zerror(zlogp, B_TRUE, "%s failed", 2479 "nvlist_alloc"); 2480 goto out; 2481 } 2482 if (zonecfg_construct_rctlblk(rctlval, rctlblk) 2483 != Z_OK) { 2484 zerror(zlogp, B_FALSE, "invalid rctl value: " 2485 "(priv=%s,limit=%s,action=%s)", 2486 rctlval->zone_rctlval_priv, 2487 rctlval->zone_rctlval_limit, 2488 rctlval->zone_rctlval_action); 2489 goto out; 2490 } 2491 if (!zonecfg_valid_rctl(name, rctlblk)) { 2492 zerror(zlogp, B_FALSE, 2493 "(priv=%s,limit=%s,action=%s) is not a " 2494 "valid value for rctl '%s'", 2495 rctlval->zone_rctlval_priv, 2496 rctlval->zone_rctlval_limit, 2497 rctlval->zone_rctlval_action, 2498 name); 2499 goto out; 2500 } 2501 if (nvlist_add_uint64(nvlv[i], "privilege", 2502 rctlblk_get_privilege(rctlblk)) != 0) { 2503 zerror(zlogp, B_FALSE, "%s failed", 2504 "nvlist_add_uint64"); 2505 goto out; 2506 } 2507 if (nvlist_add_uint64(nvlv[i], "limit", 2508 rctlblk_get_value(rctlblk)) != 0) { 2509 zerror(zlogp, B_FALSE, "%s failed", 2510 "nvlist_add_uint64"); 2511 goto out; 2512 } 2513 if (nvlist_add_uint64(nvlv[i], "action", 2514 (uint_t)rctlblk_get_local_action(rctlblk, NULL)) 2515 != 0) { 2516 zerror(zlogp, B_FALSE, "%s failed", 2517 "nvlist_add_uint64"); 2518 goto out; 2519 } 2520 } 2521 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2522 rctltab.zone_rctl_valptr = NULL; 2523 if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count) 2524 != 0) { 2525 zerror(zlogp, B_FALSE, "%s failed", 2526 "nvlist_add_nvlist_array"); 2527 goto out; 2528 } 2529 for (i = 0; i < count; i++) 2530 nvlist_free(nvlv[i]); 2531 free(nvlv); 2532 nvlv = NULL; 2533 rctlcount++; 2534 } 2535 (void) zonecfg_endrctlent(handle); 2536 2537 if (rctlcount == 0) { 2538 error = 0; 2539 goto out; 2540 } 2541 if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0) 2542 != 0) { 2543 zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack"); 2544 goto out; 2545 } 2546 2547 error = 0; 2548 *bufp = nvl_packed; 2549 *bufsizep = nvl_size; 2550 2551 out: 2552 free(rctlblk); 2553 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr); 2554 if (error && nvl_packed != NULL) 2555 free(nvl_packed); 2556 if (nvl != NULL) 2557 nvlist_free(nvl); 2558 if (nvlv != NULL) 2559 free(nvlv); 2560 if (handle != NULL) 2561 zonecfg_fini_handle(handle); 2562 return (error); 2563 } 2564 2565 static int 2566 get_zone_pool(zlog_t *zlogp, char *poolbuf, size_t bufsz) 2567 { 2568 zone_dochandle_t handle; 2569 int error; 2570 2571 if ((handle = zonecfg_init_handle()) == NULL) { 2572 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2573 return (Z_NOMEM); 2574 } 2575 error = zonecfg_get_snapshot_handle(zone_name, handle); 2576 if (error != Z_OK) { 2577 zerror(zlogp, B_FALSE, "invalid configuration"); 2578 zonecfg_fini_handle(handle); 2579 return (error); 2580 } 2581 error = zonecfg_get_pool(handle, poolbuf, bufsz); 2582 zonecfg_fini_handle(handle); 2583 return (error); 2584 } 2585 2586 static int 2587 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep) 2588 { 2589 zone_dochandle_t handle; 2590 struct zone_dstab dstab; 2591 size_t total, offset, len; 2592 int error = -1; 2593 char *str; 2594 2595 *bufp = NULL; 2596 *bufsizep = 0; 2597 2598 if ((handle = zonecfg_init_handle()) == NULL) { 2599 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2600 return (-1); 2601 } 2602 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2603 zerror(zlogp, B_FALSE, "invalid configuration"); 2604 zonecfg_fini_handle(handle); 2605 return (-1); 2606 } 2607 2608 if (zonecfg_setdsent(handle) != Z_OK) { 2609 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2610 goto out; 2611 } 2612 2613 total = 0; 2614 while (zonecfg_getdsent(handle, &dstab) == Z_OK) 2615 total += strlen(dstab.zone_dataset_name) + 1; 2616 (void) zonecfg_enddsent(handle); 2617 2618 if (total == 0) { 2619 error = 0; 2620 goto out; 2621 } 2622 2623 if ((str = malloc(total)) == NULL) { 2624 zerror(zlogp, B_TRUE, "memory allocation failed"); 2625 goto out; 2626 } 2627 2628 if (zonecfg_setdsent(handle) != Z_OK) { 2629 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent"); 2630 goto out; 2631 } 2632 offset = 0; 2633 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2634 len = strlen(dstab.zone_dataset_name); 2635 (void) strlcpy(str + offset, dstab.zone_dataset_name, 2636 sizeof (dstab.zone_dataset_name) - offset); 2637 offset += len; 2638 if (offset != total - 1) 2639 str[offset++] = ','; 2640 } 2641 (void) zonecfg_enddsent(handle); 2642 2643 error = 0; 2644 *bufp = str; 2645 *bufsizep = total; 2646 2647 out: 2648 if (error != 0 && str != NULL) 2649 free(str); 2650 if (handle != NULL) 2651 zonecfg_fini_handle(handle); 2652 2653 return (error); 2654 } 2655 2656 static int 2657 validate_datasets(zlog_t *zlogp) 2658 { 2659 zone_dochandle_t handle; 2660 struct zone_dstab dstab; 2661 zfs_handle_t *zhp; 2662 libzfs_handle_t *hdl; 2663 2664 if ((handle = zonecfg_init_handle()) == NULL) { 2665 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 2666 return (-1); 2667 } 2668 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) { 2669 zerror(zlogp, B_FALSE, "invalid configuration"); 2670 zonecfg_fini_handle(handle); 2671 return (-1); 2672 } 2673 2674 if (zonecfg_setdsent(handle) != Z_OK) { 2675 zerror(zlogp, B_FALSE, "invalid configuration"); 2676 zonecfg_fini_handle(handle); 2677 return (-1); 2678 } 2679 2680 if ((hdl = libzfs_init()) == NULL) { 2681 zerror(zlogp, B_FALSE, "opening ZFS library"); 2682 zonecfg_fini_handle(handle); 2683 return (-1); 2684 } 2685 2686 while (zonecfg_getdsent(handle, &dstab) == Z_OK) { 2687 2688 if ((zhp = zfs_open(hdl, dstab.zone_dataset_name, 2689 ZFS_TYPE_FILESYSTEM)) == NULL) { 2690 zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'", 2691 dstab.zone_dataset_name); 2692 zonecfg_fini_handle(handle); 2693 libzfs_fini(hdl); 2694 return (-1); 2695 } 2696 2697 /* 2698 * Automatically set the 'zoned' property. We check the value 2699 * first because we'll get EPERM if it is already set. 2700 */ 2701 if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 2702 zfs_prop_set(zhp, ZFS_PROP_ZONED, "on") != 0) { 2703 zerror(zlogp, B_FALSE, "cannot set 'zoned' " 2704 "property for ZFS dataset '%s'\n", 2705 dstab.zone_dataset_name); 2706 zonecfg_fini_handle(handle); 2707 zfs_close(zhp); 2708 libzfs_fini(hdl); 2709 return (-1); 2710 } 2711 2712 zfs_close(zhp); 2713 } 2714 (void) zonecfg_enddsent(handle); 2715 2716 zonecfg_fini_handle(handle); 2717 libzfs_fini(hdl); 2718 2719 return (0); 2720 } 2721 2722 static int 2723 bind_to_pool(zlog_t *zlogp, zoneid_t zoneid) 2724 { 2725 pool_conf_t *poolconf; 2726 pool_t *pool; 2727 char poolname[MAXPATHLEN]; 2728 int status; 2729 int error; 2730 2731 /* 2732 * Find the pool mentioned in the zone configuration, and bind to it. 2733 */ 2734 error = get_zone_pool(zlogp, poolname, sizeof (poolname)); 2735 if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) { 2736 /* 2737 * The property is not set on the zone, so the pool 2738 * should be bound to the default pool. But that's 2739 * already done by the kernel, so we can just return. 2740 */ 2741 return (0); 2742 } 2743 if (error != Z_OK) { 2744 /* 2745 * Not an error, even though it shouldn't be happening. 2746 */ 2747 zerror(zlogp, B_FALSE, 2748 "WARNING: unable to retrieve default pool."); 2749 return (0); 2750 } 2751 /* 2752 * Don't do anything if pools aren't enabled. 2753 */ 2754 if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) { 2755 zerror(zlogp, B_FALSE, "WARNING: pools facility not active; " 2756 "zone will not be bound to pool '%s'.", poolname); 2757 return (0); 2758 } 2759 /* 2760 * Try to provide a sane error message if the requested pool doesn't 2761 * exist. 2762 */ 2763 if ((poolconf = pool_conf_alloc()) == NULL) { 2764 zerror(zlogp, B_FALSE, "%s failed", "pool_conf_alloc"); 2765 return (-1); 2766 } 2767 if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) != 2768 PO_SUCCESS) { 2769 zerror(zlogp, B_FALSE, "%s failed", "pool_conf_open"); 2770 pool_conf_free(poolconf); 2771 return (-1); 2772 } 2773 pool = pool_get_pool(poolconf, poolname); 2774 (void) pool_conf_close(poolconf); 2775 pool_conf_free(poolconf); 2776 if (pool == NULL) { 2777 zerror(zlogp, B_FALSE, "WARNING: pool '%s' not found; " 2778 "using default pool.", poolname); 2779 return (0); 2780 } 2781 /* 2782 * Bind the zone to the pool. 2783 */ 2784 if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) { 2785 zerror(zlogp, B_FALSE, "WARNING: unable to bind to pool '%s'; " 2786 "using default pool.", poolname); 2787 } 2788 return (0); 2789 } 2790 2791 /* 2792 * Mount lower level home directories into/from current zone 2793 * Share exported directories specified in dfstab for zone 2794 */ 2795 static int 2796 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath) 2797 { 2798 zoneid_t *zids = NULL; 2799 priv_set_t *zid_privs; 2800 const priv_impl_info_t *ip = NULL; 2801 uint_t nzents_saved; 2802 uint_t nzents; 2803 int i; 2804 char readonly[] = "ro"; 2805 struct zone_fstab lower_fstab; 2806 char *argv[4]; 2807 2808 if (!is_system_labeled()) 2809 return (0); 2810 2811 if (zid_label == NULL) { 2812 zid_label = m_label_alloc(MAC_LABEL); 2813 if (zid_label == NULL) 2814 return (-1); 2815 } 2816 2817 /* Make sure our zone has an /export/home dir */ 2818 (void) make_one_dir(zlogp, rootpath, "/export/home", 2819 DEFAULT_DIR_MODE); 2820 2821 lower_fstab.zone_fs_raw[0] = '\0'; 2822 (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS, 2823 sizeof (lower_fstab.zone_fs_type)); 2824 lower_fstab.zone_fs_options = NULL; 2825 (void) zonecfg_add_fs_option(&lower_fstab, readonly); 2826 2827 /* 2828 * Get the list of zones from the kernel 2829 */ 2830 if (zone_list(NULL, &nzents) != 0) { 2831 zerror(zlogp, B_TRUE, "unable to list zones"); 2832 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2833 return (-1); 2834 } 2835 again: 2836 if (nzents == 0) { 2837 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2838 return (-1); 2839 } 2840 2841 zids = malloc(nzents * sizeof (zoneid_t)); 2842 if (zids == NULL) { 2843 zerror(zlogp, B_TRUE, "memory allocation failed"); 2844 return (-1); 2845 } 2846 nzents_saved = nzents; 2847 2848 if (zone_list(zids, &nzents) != 0) { 2849 zerror(zlogp, B_TRUE, "unable to list zones"); 2850 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 2851 free(zids); 2852 return (-1); 2853 } 2854 if (nzents != nzents_saved) { 2855 /* list changed, try again */ 2856 free(zids); 2857 goto again; 2858 } 2859 2860 ip = getprivimplinfo(); 2861 if ((zid_privs = priv_allocset()) == NULL) { 2862 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 2863 zonecfg_free_fs_option_list( 2864 lower_fstab.zone_fs_options); 2865 free(zids); 2866 return (-1); 2867 } 2868 2869 for (i = 0; i < nzents; i++) { 2870 char zid_name[ZONENAME_MAX]; 2871 zone_state_t zid_state; 2872 char zid_rpath[MAXPATHLEN]; 2873 struct stat stat_buf; 2874 2875 if (zids[i] == GLOBAL_ZONEID) 2876 continue; 2877 2878 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 2879 continue; 2880 2881 /* 2882 * Do special setup for the zone we are booting 2883 */ 2884 if (strcmp(zid_name, zone_name) == 0) { 2885 struct zone_fstab autofs_fstab; 2886 char map_path[MAXPATHLEN]; 2887 int fd; 2888 2889 /* 2890 * Create auto_home_<zone> map for this zone 2891 * in the global zone. The local zone entry 2892 * will be created by automount when the zone 2893 * is booted. 2894 */ 2895 2896 (void) snprintf(autofs_fstab.zone_fs_special, 2897 MAXPATHLEN, "auto_home_%s", zid_name); 2898 2899 (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN, 2900 "/zone/%s/home", zid_name); 2901 2902 (void) snprintf(map_path, sizeof (map_path), 2903 "/etc/%s", autofs_fstab.zone_fs_special); 2904 /* 2905 * If the map file doesn't exist create a template 2906 */ 2907 if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL, 2908 S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) { 2909 int len; 2910 char map_rec[MAXPATHLEN]; 2911 2912 len = snprintf(map_rec, sizeof (map_rec), 2913 "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n", 2914 autofs_fstab.zone_fs_special, rootpath); 2915 (void) write(fd, map_rec, len); 2916 (void) close(fd); 2917 } 2918 2919 /* 2920 * Mount auto_home_<zone> in the global zone if absent. 2921 * If it's already of type autofs, then 2922 * don't mount it again. 2923 */ 2924 if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) || 2925 strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) { 2926 char optstr[] = "indirect,ignore,nobrowse"; 2927 2928 (void) make_one_dir(zlogp, "", 2929 autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE); 2930 2931 /* 2932 * Mount will fail if automounter has already 2933 * processed the auto_home_<zonename> map 2934 */ 2935 (void) domount(zlogp, MNTTYPE_AUTOFS, optstr, 2936 autofs_fstab.zone_fs_special, 2937 autofs_fstab.zone_fs_dir); 2938 } 2939 continue; 2940 } 2941 2942 2943 if (zone_get_state(zid_name, &zid_state) != Z_OK || 2944 (zid_state != ZONE_STATE_READY && 2945 zid_state != ZONE_STATE_RUNNING)) 2946 /* Skip over zones without mounted filesystems */ 2947 continue; 2948 2949 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 2950 sizeof (m_label_t)) < 0) 2951 /* Skip over zones with unspecified label */ 2952 continue; 2953 2954 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 2955 sizeof (zid_rpath)) == -1) 2956 /* Skip over zones with bad path */ 2957 continue; 2958 2959 if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs, 2960 sizeof (priv_chunk_t) * ip->priv_setsize) == -1) 2961 /* Skip over zones with bad privs */ 2962 continue; 2963 2964 /* 2965 * Reading down is valid according to our label model 2966 * but some customers want to disable it because it 2967 * allows execute down and other possible attacks. 2968 * Therefore, we restrict this feature to zones that 2969 * have the NET_MAC_AWARE privilege which is required 2970 * for NFS read-down semantics. 2971 */ 2972 if ((bldominates(zlabel, zid_label)) && 2973 (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) { 2974 /* 2975 * Our zone dominates this one. 2976 * Create a lofs mount from lower zone's /export/home 2977 */ 2978 (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 2979 "%s/zone/%s/export/home", rootpath, zid_name); 2980 2981 /* 2982 * If the target is already an LOFS mount 2983 * then don't do it again. 2984 */ 2985 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 2986 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 2987 2988 if (snprintf(lower_fstab.zone_fs_special, 2989 MAXPATHLEN, "%s/export", 2990 zid_rpath) > MAXPATHLEN) 2991 continue; 2992 2993 /* 2994 * Make sure the lower-level home exists 2995 */ 2996 if (make_one_dir(zlogp, 2997 lower_fstab.zone_fs_special, 2998 "/home", DEFAULT_DIR_MODE) != 0) 2999 continue; 3000 3001 (void) strlcat(lower_fstab.zone_fs_special, 3002 "/home", MAXPATHLEN); 3003 3004 /* 3005 * Mount can fail because the lower-level 3006 * zone may have already done a mount up. 3007 */ 3008 (void) mount_one(zlogp, &lower_fstab, ""); 3009 } 3010 } else if ((bldominates(zid_label, zlabel)) && 3011 (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) { 3012 /* 3013 * This zone dominates our zone. 3014 * Create a lofs mount from our zone's /export/home 3015 */ 3016 if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN, 3017 "%s/zone/%s/export/home", zid_rpath, 3018 zone_name) > MAXPATHLEN) 3019 continue; 3020 3021 /* 3022 * If the target is already an LOFS mount 3023 * then don't do it again. 3024 */ 3025 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) || 3026 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) { 3027 3028 (void) snprintf(lower_fstab.zone_fs_special, 3029 MAXPATHLEN, "%s/export/home", rootpath); 3030 3031 /* 3032 * Mount can fail because the higher-level 3033 * zone may have already done a mount down. 3034 */ 3035 (void) mount_one(zlogp, &lower_fstab, ""); 3036 } 3037 } 3038 } 3039 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options); 3040 priv_freeset(zid_privs); 3041 free(zids); 3042 3043 /* 3044 * Now share any exported directories from this zone. 3045 * Each zone can have its own dfstab. 3046 */ 3047 3048 argv[0] = "zoneshare"; 3049 argv[1] = "-z"; 3050 argv[2] = zone_name; 3051 argv[3] = NULL; 3052 3053 (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv); 3054 /* Don't check for errors since they don't affect the zone */ 3055 3056 return (0); 3057 } 3058 3059 /* 3060 * Unmount lofs mounts from higher level zones 3061 * Unshare nfs exported directories 3062 */ 3063 static void 3064 tsol_unmounts(zlog_t *zlogp, char *zone_name) 3065 { 3066 zoneid_t *zids = NULL; 3067 uint_t nzents_saved; 3068 uint_t nzents; 3069 int i; 3070 char *argv[4]; 3071 char path[MAXPATHLEN]; 3072 3073 if (!is_system_labeled()) 3074 return; 3075 3076 /* 3077 * Get the list of zones from the kernel 3078 */ 3079 if (zone_list(NULL, &nzents) != 0) { 3080 return; 3081 } 3082 3083 if (zid_label == NULL) { 3084 zid_label = m_label_alloc(MAC_LABEL); 3085 if (zid_label == NULL) 3086 return; 3087 } 3088 3089 again: 3090 if (nzents == 0) 3091 return; 3092 3093 zids = malloc(nzents * sizeof (zoneid_t)); 3094 if (zids == NULL) { 3095 zerror(zlogp, B_TRUE, "memory allocation failed"); 3096 return; 3097 } 3098 nzents_saved = nzents; 3099 3100 if (zone_list(zids, &nzents) != 0) { 3101 free(zids); 3102 return; 3103 } 3104 if (nzents != nzents_saved) { 3105 /* list changed, try again */ 3106 free(zids); 3107 goto again; 3108 } 3109 3110 for (i = 0; i < nzents; i++) { 3111 char zid_name[ZONENAME_MAX]; 3112 zone_state_t zid_state; 3113 char zid_rpath[MAXPATHLEN]; 3114 3115 if (zids[i] == GLOBAL_ZONEID) 3116 continue; 3117 3118 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1) 3119 continue; 3120 3121 /* 3122 * Skip the zone we are halting 3123 */ 3124 if (strcmp(zid_name, zone_name) == 0) 3125 continue; 3126 3127 if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state, 3128 sizeof (zid_state)) < 0) || 3129 (zid_state < ZONE_IS_READY)) 3130 /* Skip over zones without mounted filesystems */ 3131 continue; 3132 3133 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label, 3134 sizeof (m_label_t)) < 0) 3135 /* Skip over zones with unspecified label */ 3136 continue; 3137 3138 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath, 3139 sizeof (zid_rpath)) == -1) 3140 /* Skip over zones with bad path */ 3141 continue; 3142 3143 if (zlabel != NULL && bldominates(zid_label, zlabel)) { 3144 /* 3145 * This zone dominates our zone. 3146 * Unmount the lofs mount of our zone's /export/home 3147 */ 3148 3149 if (snprintf(path, MAXPATHLEN, 3150 "%s/zone/%s/export/home", zid_rpath, 3151 zone_name) > MAXPATHLEN) 3152 continue; 3153 3154 /* Skip over mount failures */ 3155 (void) umount(path); 3156 } 3157 } 3158 free(zids); 3159 3160 /* 3161 * Unmount global zone autofs trigger for this zone 3162 */ 3163 (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name); 3164 /* Skip over mount failures */ 3165 (void) umount(path); 3166 3167 /* 3168 * Next unshare any exported directories from this zone. 3169 */ 3170 3171 argv[0] = "zoneunshare"; 3172 argv[1] = "-z"; 3173 argv[2] = zone_name; 3174 argv[3] = NULL; 3175 3176 (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv); 3177 /* Don't check for errors since they don't affect the zone */ 3178 3179 /* 3180 * Finally, deallocate any devices in the zone. 3181 */ 3182 3183 argv[0] = "deallocate"; 3184 argv[1] = "-Isz"; 3185 argv[2] = zone_name; 3186 argv[3] = NULL; 3187 3188 (void) forkexec(zlogp, "/usr/sbin/deallocate", argv); 3189 /* Don't check for errors since they don't affect the zone */ 3190 } 3191 3192 /* 3193 * Fetch the Trusted Extensions label and multi-level ports (MLPs) for 3194 * this zone. 3195 */ 3196 static tsol_zcent_t * 3197 get_zone_label(zlog_t *zlogp, priv_set_t *privs) 3198 { 3199 FILE *fp; 3200 tsol_zcent_t *zcent = NULL; 3201 char line[MAXTNZLEN]; 3202 3203 if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) { 3204 zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH); 3205 return (NULL); 3206 } 3207 3208 while (fgets(line, sizeof (line), fp) != NULL) { 3209 /* 3210 * Check for malformed database 3211 */ 3212 if (strlen(line) == MAXTNZLEN - 1) 3213 break; 3214 if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL) 3215 continue; 3216 if (strcmp(zcent->zc_name, zone_name) == 0) 3217 break; 3218 tsol_freezcent(zcent); 3219 zcent = NULL; 3220 } 3221 (void) fclose(fp); 3222 3223 if (zcent == NULL) { 3224 zerror(zlogp, B_FALSE, "zone requires a label assignment. " 3225 "See tnzonecfg(4)"); 3226 } else { 3227 if (zlabel == NULL) 3228 zlabel = m_label_alloc(MAC_LABEL); 3229 /* 3230 * Save this zone's privileges for later read-down processing 3231 */ 3232 if ((zprivs = priv_allocset()) == NULL) { 3233 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 3234 return (NULL); 3235 } else { 3236 priv_copyset(privs, zprivs); 3237 } 3238 } 3239 return (zcent); 3240 } 3241 3242 /* 3243 * Add the Trusted Extensions multi-level ports for this zone. 3244 */ 3245 static void 3246 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent) 3247 { 3248 tsol_mlp_t *mlp; 3249 tsol_mlpent_t tsme; 3250 3251 if (!is_system_labeled()) 3252 return; 3253 3254 tsme.tsme_zoneid = zoneid; 3255 tsme.tsme_flags = 0; 3256 for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) { 3257 tsme.tsme_mlp = *mlp; 3258 if (tnmlp(TNDB_LOAD, &tsme) != 0) { 3259 zerror(zlogp, B_TRUE, "cannot set zone-specific MLP " 3260 "on %d-%d/%d", mlp->mlp_port, 3261 mlp->mlp_port_upper, mlp->mlp_ipp); 3262 } 3263 } 3264 3265 tsme.tsme_flags = TSOL_MEF_SHARED; 3266 for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) { 3267 tsme.tsme_mlp = *mlp; 3268 if (tnmlp(TNDB_LOAD, &tsme) != 0) { 3269 zerror(zlogp, B_TRUE, "cannot set shared MLP " 3270 "on %d-%d/%d", mlp->mlp_port, 3271 mlp->mlp_port_upper, mlp->mlp_ipp); 3272 } 3273 } 3274 } 3275 3276 static void 3277 remove_mlps(zlog_t *zlogp, zoneid_t zoneid) 3278 { 3279 tsol_mlpent_t tsme; 3280 3281 if (!is_system_labeled()) 3282 return; 3283 3284 (void) memset(&tsme, 0, sizeof (tsme)); 3285 tsme.tsme_zoneid = zoneid; 3286 if (tnmlp(TNDB_FLUSH, &tsme) != 0) 3287 zerror(zlogp, B_TRUE, "cannot flush MLPs"); 3288 } 3289 3290 int 3291 prtmount(const char *fs, void *x) { 3292 zerror((zlog_t *)x, B_FALSE, " %s", fs); 3293 return (0); 3294 } 3295 3296 /* 3297 * Look for zones running on the main system that are using this root (or any 3298 * subdirectory of it). Return B_TRUE and print an error if a conflicting zone 3299 * is found or if we can't tell. 3300 */ 3301 static boolean_t 3302 duplicate_zone_root(zlog_t *zlogp, const char *rootpath) 3303 { 3304 zoneid_t *zids = NULL; 3305 uint_t nzids = 0; 3306 boolean_t retv; 3307 int rlen, zlen; 3308 char zroot[MAXPATHLEN]; 3309 char zonename[ZONENAME_MAX]; 3310 3311 for (;;) { 3312 nzids += 10; 3313 zids = malloc(nzids * sizeof (*zids)); 3314 if (zids == NULL) { 3315 zerror(zlogp, B_TRUE, "memory allocation failed"); 3316 return (B_TRUE); 3317 } 3318 if (zone_list(zids, &nzids) == 0) 3319 break; 3320 free(zids); 3321 } 3322 retv = B_FALSE; 3323 rlen = strlen(rootpath); 3324 while (nzids > 0) { 3325 /* 3326 * Ignore errors; they just mean that the zone has disappeared 3327 * while we were busy. 3328 */ 3329 if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot, 3330 sizeof (zroot)) == -1) 3331 continue; 3332 zlen = strlen(zroot); 3333 if (zlen > rlen) 3334 zlen = rlen; 3335 if (strncmp(rootpath, zroot, zlen) == 0 && 3336 (zroot[zlen] == '\0' || zroot[zlen] == '/') && 3337 (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) { 3338 if (getzonenamebyid(zids[nzids], zonename, 3339 sizeof (zonename)) == -1) 3340 (void) snprintf(zonename, sizeof (zonename), 3341 "id %d", (int)zids[nzids]); 3342 zerror(zlogp, B_FALSE, 3343 "zone root %s already in use by zone %s", 3344 rootpath, zonename); 3345 retv = B_TRUE; 3346 break; 3347 } 3348 } 3349 free(zids); 3350 return (retv); 3351 } 3352 3353 /* 3354 * Search for loopback mounts that use this same source node (same device and 3355 * inode). Return B_TRUE if there is one or if we can't tell. 3356 */ 3357 static boolean_t 3358 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath) 3359 { 3360 struct stat64 rst, zst; 3361 struct mnttab *mnp; 3362 3363 if (stat64(rootpath, &rst) == -1) { 3364 zerror(zlogp, B_TRUE, "can't stat %s", rootpath); 3365 return (B_TRUE); 3366 } 3367 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1) 3368 return (B_TRUE); 3369 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) { 3370 if (mnp->mnt_fstype == NULL || 3371 strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0) 3372 continue; 3373 /* We're looking at a loopback mount. Stat it. */ 3374 if (mnp->mnt_special != NULL && 3375 stat64(mnp->mnt_special, &zst) != -1 && 3376 rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) { 3377 zerror(zlogp, B_FALSE, 3378 "zone root %s is reachable through %s", 3379 rootpath, mnp->mnt_mountp); 3380 return (B_TRUE); 3381 } 3382 } 3383 return (B_FALSE); 3384 } 3385 3386 zoneid_t 3387 vplat_create(zlog_t *zlogp, boolean_t mount_cmd) 3388 { 3389 zoneid_t rval = -1; 3390 priv_set_t *privs; 3391 char rootpath[MAXPATHLEN]; 3392 char *rctlbuf = NULL; 3393 size_t rctlbufsz = 0; 3394 char *zfsbuf = NULL; 3395 size_t zfsbufsz = 0; 3396 zoneid_t zoneid = -1; 3397 int xerr; 3398 char *kzone; 3399 FILE *fp = NULL; 3400 tsol_zcent_t *zcent = NULL; 3401 int match = 0; 3402 int doi = 0; 3403 3404 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) { 3405 zerror(zlogp, B_TRUE, "unable to determine zone root"); 3406 return (-1); 3407 } 3408 if (zonecfg_in_alt_root()) 3409 resolve_lofs(zlogp, rootpath, sizeof (rootpath)); 3410 3411 if ((privs = priv_allocset()) == NULL) { 3412 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset"); 3413 return (-1); 3414 } 3415 priv_emptyset(privs); 3416 if (get_privset(zlogp, privs, mount_cmd) != 0) 3417 goto error; 3418 3419 if (!mount_cmd && get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) { 3420 zerror(zlogp, B_FALSE, "Unable to get list of rctls"); 3421 goto error; 3422 } 3423 3424 if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) { 3425 zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets"); 3426 goto error; 3427 } 3428 3429 if (!mount_cmd && is_system_labeled()) { 3430 zcent = get_zone_label(zlogp, privs); 3431 if (zcent != NULL) { 3432 match = zcent->zc_match; 3433 doi = zcent->zc_doi; 3434 *zlabel = zcent->zc_label; 3435 } else { 3436 goto error; 3437 } 3438 } 3439 3440 kzone = zone_name; 3441 3442 /* 3443 * We must do this scan twice. First, we look for zones running on the 3444 * main system that are using this root (or any subdirectory of it). 3445 * Next, we reduce to the shortest path and search for loopback mounts 3446 * that use this same source node (same device and inode). 3447 */ 3448 if (duplicate_zone_root(zlogp, rootpath)) 3449 goto error; 3450 if (duplicate_reachable_path(zlogp, rootpath)) 3451 goto error; 3452 3453 if (mount_cmd) { 3454 root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE); 3455 3456 /* 3457 * Forge up a special root for this zone. When a zone is 3458 * mounted, we can't let the zone have its own root because the 3459 * tools that will be used in this "scratch zone" need access 3460 * to both the zone's resources and the running machine's 3461 * executables. 3462 * 3463 * Note that the mkdir here also catches read-only filesystems. 3464 */ 3465 if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) { 3466 zerror(zlogp, B_TRUE, "cannot create %s", rootpath); 3467 goto error; 3468 } 3469 if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0) 3470 goto error; 3471 } 3472 3473 if (zonecfg_in_alt_root()) { 3474 /* 3475 * If we are mounting up a zone in an alternate root partition, 3476 * then we have some additional work to do before starting the 3477 * zone. First, resolve the root path down so that we're not 3478 * fooled by duplicates. Then forge up an internal name for 3479 * the zone. 3480 */ 3481 if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) { 3482 zerror(zlogp, B_TRUE, "cannot open mapfile"); 3483 goto error; 3484 } 3485 if (zonecfg_lock_scratch(fp) != 0) { 3486 zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3487 goto error; 3488 } 3489 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3490 NULL, 0) == 0) { 3491 zerror(zlogp, B_FALSE, "scratch zone already running"); 3492 goto error; 3493 } 3494 /* This is the preferred name */ 3495 (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s", 3496 zone_name); 3497 srandom(getpid()); 3498 while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL, 3499 0) == 0) { 3500 /* This is just an arbitrary name; note "." usage */ 3501 (void) snprintf(kernzone, sizeof (kernzone), 3502 "SUNWlu.%08lX%08lX", random(), random()); 3503 } 3504 kzone = kernzone; 3505 } 3506 3507 xerr = 0; 3508 if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf, 3509 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel)) == -1) { 3510 if (xerr == ZE_AREMOUNTS) { 3511 if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) { 3512 zerror(zlogp, B_FALSE, 3513 "An unknown file-system is mounted on " 3514 "a subdirectory of %s", rootpath); 3515 } else { 3516 3517 zerror(zlogp, B_FALSE, 3518 "These file-systems are mounted on " 3519 "subdirectories of %s:", rootpath); 3520 (void) zonecfg_find_mounts(rootpath, 3521 prtmount, zlogp); 3522 } 3523 } else if (xerr == ZE_CHROOTED) { 3524 zerror(zlogp, B_FALSE, "%s: " 3525 "cannot create a zone from a chrooted " 3526 "environment", "zone_create"); 3527 } else { 3528 zerror(zlogp, B_TRUE, "%s failed", "zone_create"); 3529 } 3530 goto error; 3531 } 3532 3533 if (zonecfg_in_alt_root() && 3534 zonecfg_add_scratch(fp, zone_name, kernzone, 3535 zonecfg_get_root()) == -1) { 3536 zerror(zlogp, B_TRUE, "cannot add mapfile entry"); 3537 goto error; 3538 } 3539 3540 /* 3541 * The following is a warning, not an error, and is not performed when 3542 * merely mounting a zone for administrative use. 3543 */ 3544 if (!mount_cmd && bind_to_pool(zlogp, zoneid) != 0) 3545 zerror(zlogp, B_FALSE, "WARNING: unable to bind zone to " 3546 "requested pool; using default pool."); 3547 if (!mount_cmd) 3548 set_mlps(zlogp, zoneid, zcent); 3549 rval = zoneid; 3550 zoneid = -1; 3551 3552 error: 3553 if (zoneid != -1) 3554 (void) zone_destroy(zoneid); 3555 if (rctlbuf != NULL) 3556 free(rctlbuf); 3557 priv_freeset(privs); 3558 if (fp != NULL) 3559 zonecfg_close_scratch(fp); 3560 lofs_discard_mnttab(); 3561 if (zcent != NULL) 3562 tsol_freezcent(zcent); 3563 return (rval); 3564 } 3565 3566 /* 3567 * Enter the zone and write a /etc/zones/index file there. This allows 3568 * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone 3569 * details from inside the zone. 3570 */ 3571 static void 3572 write_index_file(zoneid_t zoneid) 3573 { 3574 FILE *zef; 3575 FILE *zet; 3576 struct zoneent *zep; 3577 pid_t child; 3578 int tmpl_fd; 3579 ctid_t ct; 3580 int fd; 3581 char uuidstr[UUID_PRINTABLE_STRING_LENGTH]; 3582 3583 /* Locate the zone entry in the global zone's index file */ 3584 if ((zef = setzoneent()) == NULL) 3585 return; 3586 while ((zep = getzoneent_private(zef)) != NULL) { 3587 if (strcmp(zep->zone_name, zone_name) == 0) 3588 break; 3589 free(zep); 3590 } 3591 endzoneent(zef); 3592 if (zep == NULL) 3593 return; 3594 3595 if ((tmpl_fd = init_template()) == -1) { 3596 free(zep); 3597 return; 3598 } 3599 3600 if ((child = fork()) == -1) { 3601 (void) ct_tmpl_clear(tmpl_fd); 3602 (void) close(tmpl_fd); 3603 free(zep); 3604 return; 3605 } 3606 3607 /* parent waits for child to finish */ 3608 if (child != 0) { 3609 free(zep); 3610 if (contract_latest(&ct) == -1) 3611 ct = -1; 3612 (void) ct_tmpl_clear(tmpl_fd); 3613 (void) close(tmpl_fd); 3614 (void) waitpid(child, NULL, 0); 3615 (void) contract_abandon_id(ct); 3616 return; 3617 } 3618 3619 /* child enters zone and sets up index file */ 3620 (void) ct_tmpl_clear(tmpl_fd); 3621 if (zone_enter(zoneid) != -1) { 3622 (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE); 3623 (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID, 3624 ZONE_CONFIG_GID); 3625 fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC, 3626 ZONE_INDEX_MODE); 3627 if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) { 3628 (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID); 3629 if (uuid_is_null(zep->zone_uuid)) 3630 uuidstr[0] = '\0'; 3631 else 3632 uuid_unparse(zep->zone_uuid, uuidstr); 3633 (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name, 3634 zone_state_str(zep->zone_state), 3635 uuidstr); 3636 (void) fclose(zet); 3637 } 3638 } 3639 _exit(0); 3640 } 3641 3642 /*ARGSUSED1*/ 3643 static int 3644 devcleanup_cb(const char *path, uid_t u, gid_t g, mode_t m, const char *a, 3645 void *data) 3646 { 3647 zone_dochandle_t h = (zone_dochandle_t)data; 3648 boolean_t del; 3649 char fullpath[MAXPATHLEN]; 3650 char zonepath[MAXPATHLEN]; 3651 3652 if (zonecfg_should_deldev(h, path, &del) == Z_OK) { 3653 if (del) { 3654 if (zonecfg_get_zonepath(h, zonepath, 3655 sizeof (zonepath)) != Z_OK) 3656 return (Z_OK); 3657 (void) snprintf(fullpath, sizeof (fullpath), 3658 "%s/dev/%s", zonepath, path); 3659 (void) unlink(fullpath); 3660 } 3661 } 3662 return (Z_OK); 3663 } 3664 3665 /* 3666 * If needed, initiate a walk of the zone's /dev tree, looking for device 3667 * entries which need to be cleaned up: this is a wrapper around functionality 3668 * in libzonecfg which keeps track of newly-defunct device entries. 3669 */ 3670 static int 3671 devcleanup(zlog_t *zlogp) 3672 { 3673 zone_dochandle_t handle; 3674 3675 if ((handle = zonecfg_init_handle()) == NULL) { 3676 zerror(zlogp, B_TRUE, "getting zone configuration handle"); 3677 return (-1); 3678 } 3679 3680 /* 3681 * Note that this is a rare case when we consult the *real* zone 3682 * config handle, not a snapshot-- that's because we want to 3683 * drop the deleted-device markers out of the config once we've 3684 * purged them from the FS. 3685 */ 3686 if (zonecfg_get_handle(zone_name, handle) != Z_OK) { 3687 zerror(zlogp, B_FALSE, "invalid configuration"); 3688 zonecfg_fini_handle(handle); 3689 return (-1); 3690 } 3691 3692 /* 3693 * Quickly check whether there is any work to do prior to scanning 3694 * all of /dev. 3695 */ 3696 if (zonecfg_has_deldevs(handle) != Z_OK) { 3697 zonecfg_fini_handle(handle); 3698 return (0); 3699 } 3700 3701 if (zonecfg_devwalk(handle, devcleanup_cb, (void *)handle) != Z_OK) { 3702 zerror(zlogp, B_FALSE, "failed to walk devices"); 3703 zonecfg_fini_handle(handle); 3704 return (-1); 3705 } 3706 3707 /* 3708 * We don't need to process the deleted devices more than the one time. 3709 */ 3710 (void) zonecfg_clear_deldevs(handle); 3711 (void) zonecfg_save(handle); 3712 zonecfg_fini_handle(handle); 3713 return (0); 3714 } 3715 3716 int 3717 vplat_bringup(zlog_t *zlogp, boolean_t mount_cmd, zoneid_t zoneid) 3718 { 3719 3720 if (!mount_cmd && validate_datasets(zlogp) != 0) { 3721 lofs_discard_mnttab(); 3722 return (-1); 3723 } 3724 3725 if (devcleanup(zlogp) != 0) { 3726 zerror(zlogp, B_TRUE, "device cleanup failed"); 3727 return (-1); 3728 } 3729 3730 if (create_dev_files(zlogp) != 0 || 3731 mount_filesystems(zlogp, mount_cmd) != 0) { 3732 lofs_discard_mnttab(); 3733 return (-1); 3734 } 3735 if (!mount_cmd && (devfsadm_register(zlogp) != 0 || 3736 configure_network_interfaces(zlogp) != 0)) { 3737 lofs_discard_mnttab(); 3738 return (-1); 3739 } 3740 3741 write_index_file(zoneid); 3742 3743 lofs_discard_mnttab(); 3744 return (0); 3745 } 3746 3747 static int 3748 lu_root_teardown(zlog_t *zlogp) 3749 { 3750 char zroot[MAXPATHLEN]; 3751 3752 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) { 3753 zerror(zlogp, B_FALSE, "unable to determine zone root"); 3754 return (-1); 3755 } 3756 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE); 3757 3758 /* 3759 * At this point, the processes are gone, the filesystems (save the 3760 * root) are unmounted, and the zone is on death row. But there may 3761 * still be creds floating about in the system that reference the 3762 * zone_t, and which pin down zone_rootvp causing this call to fail 3763 * with EBUSY. Thus, we try for a little while before just giving up. 3764 * (How I wish this were not true, and umount2 just did the right 3765 * thing, or tmpfs supported MS_FORCE This is a gross hack.) 3766 */ 3767 if (umount2(zroot, MS_FORCE) != 0) { 3768 if (errno == ENOTSUP && umount2(zroot, 0) == 0) 3769 goto unmounted; 3770 if (errno == EBUSY) { 3771 int tries = 10; 3772 3773 while (--tries >= 0) { 3774 (void) sleep(1); 3775 if (umount2(zroot, 0) == 0) 3776 goto unmounted; 3777 if (errno != EBUSY) 3778 break; 3779 } 3780 } 3781 zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot); 3782 return (-1); 3783 } 3784 unmounted: 3785 3786 /* 3787 * Only zones in an alternate root environment have scratch zone 3788 * entries. 3789 */ 3790 if (zonecfg_in_alt_root()) { 3791 FILE *fp; 3792 int retv; 3793 3794 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3795 zerror(zlogp, B_TRUE, "cannot open mapfile"); 3796 return (-1); 3797 } 3798 retv = -1; 3799 if (zonecfg_lock_scratch(fp) != 0) 3800 zerror(zlogp, B_TRUE, "cannot lock mapfile"); 3801 else if (zonecfg_delete_scratch(fp, kernzone) != 0) 3802 zerror(zlogp, B_TRUE, "cannot delete map entry"); 3803 else 3804 retv = 0; 3805 zonecfg_close_scratch(fp); 3806 return (retv); 3807 } else { 3808 return (0); 3809 } 3810 } 3811 3812 int 3813 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd) 3814 { 3815 char *kzone; 3816 zoneid_t zoneid; 3817 3818 kzone = zone_name; 3819 if (zonecfg_in_alt_root()) { 3820 FILE *fp; 3821 3822 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) { 3823 zerror(zlogp, B_TRUE, "unable to open map file"); 3824 goto error; 3825 } 3826 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(), 3827 kernzone, sizeof (kernzone)) != 0) { 3828 zerror(zlogp, B_FALSE, "unable to find scratch zone"); 3829 zonecfg_close_scratch(fp); 3830 goto error; 3831 } 3832 zonecfg_close_scratch(fp); 3833 kzone = kernzone; 3834 } 3835 3836 if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) { 3837 if (!bringup_failure_recovery) 3838 zerror(zlogp, B_TRUE, "unable to get zoneid"); 3839 if (unmount_cmd) 3840 (void) lu_root_teardown(zlogp); 3841 goto error; 3842 } 3843 3844 if (zone_shutdown(zoneid) != 0) { 3845 zerror(zlogp, B_TRUE, "unable to shutdown zone"); 3846 goto error; 3847 } 3848 3849 if (!unmount_cmd && devfsadm_unregister(zlogp) != 0) 3850 goto error; 3851 3852 if (!unmount_cmd && 3853 unconfigure_network_interfaces(zlogp, zoneid) != 0) { 3854 zerror(zlogp, B_FALSE, 3855 "unable to unconfigure network interfaces in zone"); 3856 goto error; 3857 } 3858 3859 if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) { 3860 zerror(zlogp, B_TRUE, "unable to abort TCP connections"); 3861 goto error; 3862 } 3863 3864 if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) { 3865 zerror(zlogp, B_FALSE, 3866 "unable to unmount file systems in zone"); 3867 goto error; 3868 } 3869 3870 remove_mlps(zlogp, zoneid); 3871 3872 if (zone_destroy(zoneid) != 0) { 3873 zerror(zlogp, B_TRUE, "unable to destroy zone"); 3874 goto error; 3875 } 3876 3877 /* 3878 * Special teardown for alternate boot environments: remove the tmpfs 3879 * root for the zone and then remove it from the map file. 3880 */ 3881 if (unmount_cmd && lu_root_teardown(zlogp) != 0) 3882 goto error; 3883 3884 if (!unmount_cmd) 3885 destroy_console_slave(); 3886 3887 lofs_discard_mnttab(); 3888 return (0); 3889 3890 error: 3891 lofs_discard_mnttab(); 3892 return (-1); 3893 } 3894