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