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