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