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