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