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