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