Print this page
    
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/cmd/dlmgmtd/dlmgmt_door.c
          +++ new/usr/src/cmd/dlmgmtd/dlmgmt_door.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  
    | 
      ↓ open down ↓ | 
    13 lines elided | 
    
      ↑ open up ↑ | 
  
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24      - * Copyright (c) 2011, Joyent Inc. All rights reserved.
  25   24   */
  26   25  
  27   26  /*
  28   27   * Main door handler functions used by dlmgmtd to process the different door
  29   28   * call requests. Door call requests can come from the user-land applications,
  30   29   * or from the kernel.
  31   30   *
  32   31   * Note on zones handling:
  33   32   *
  34   33   * There are two zoneid's associated with a link.  One is the zoneid of the
  35   34   * zone in which the link was created (ll_zoneid in the dlmgmt_link_t), and
  36   35   * the other is the zoneid of the zone where the link is currently assigned
  37   36   * (the "zone" link property).  The two can be different if a datalink is
  38   37   * created in the global zone and subsequently assigned to a non-global zone
  39   38   * via zonecfg or via explicitly setting the "zone" link property.
  40   39   *
  41   40   * Door clients can see links that were created in their zone, and links that
  42   41   * are currently assigned to their zone.  Door clients in a zone can only
  43   42   * modify links that were created in their zone.
  44   43   *
  45   44   * The datalink ID space is global, while each zone has its own datalink name
  46   45   * space.  This allows each zone to have complete freedom over the names that
  47   46   * they assign to links created within the zone.
  48   47   */
  49   48  
  50   49  #include <assert.h>
  51   50  #include <alloca.h>
  52   51  #include <errno.h>
  53   52  #include <priv_utils.h>
  54   53  #include <stdlib.h>
  55   54  #include <strings.h>
  56   55  #include <syslog.h>
  57   56  #include <sys/sysevent/eventdefs.h>
  
    | 
      ↓ open down ↓ | 
    23 lines elided | 
    
      ↑ open up ↑ | 
  
  58   57  #include <zone.h>
  59   58  #include <libsysevent.h>
  60   59  #include <libdlmgmt.h>
  61   60  #include <librcm.h>
  62   61  #include <sys/types.h>
  63   62  #include <sys/stat.h>
  64   63  #include <fcntl.h>
  65   64  #include <unistd.h>
  66   65  #include "dlmgmt_impl.h"
  67   66  
       67 +
  68   68  typedef void dlmgmt_door_handler_t(void *, void *, size_t *, zoneid_t,
  69   69      ucred_t *);
  70   70  
  71   71  typedef struct dlmgmt_door_info_s {
  72   72          uint_t                  di_cmd;
  73   73          size_t                  di_reqsz;
  74   74          size_t                  di_acksz;
  75   75          dlmgmt_door_handler_t   *di_handler;
  76   76  } dlmgmt_door_info_t;
  77   77  
  78   78  /*
  79   79   * Check if the caller has the required privileges to operate on a link of the
  80   80   * given class.
  81   81   */
  82   82  static int
  83   83  dlmgmt_checkprivs(datalink_class_t class, ucred_t *cred)
  84   84  {
  85   85          const priv_set_t *eset;
  86   86  
  87   87          eset = ucred_getprivset(cred, PRIV_EFFECTIVE);
  88   88          if (eset != NULL && ((class == DATALINK_CLASS_IPTUN &&
  89   89              priv_ismember(eset, PRIV_SYS_IPTUN_CONFIG)) ||
  90   90              priv_ismember(eset, PRIV_SYS_DL_CONFIG) ||
  91   91              priv_ismember(eset, PRIV_SYS_NET_CONFIG)))
  92   92                  return (0);
  93   93          return (EACCES);
  94   94  }
  95   95  
  96   96  static dlmgmt_link_t *
  97   97  dlmgmt_getlink_by_dev(char *devname, zoneid_t zoneid)
  98   98  {
  99   99          dlmgmt_link_t *linkp = avl_first(&dlmgmt_id_avl);
 100  100  
 101  101          for (; linkp != NULL; linkp = AVL_NEXT(&dlmgmt_id_avl, linkp)) {
 102  102                  if (link_is_visible(linkp, zoneid) &&
 103  103                      (linkp->ll_class == DATALINK_CLASS_PHYS) &&
 104  104                      linkattr_equal(&(linkp->ll_head), FDEVNAME, devname,
 105  105                      strlen(devname) + 1)) {
 106  106                          return (linkp);
 107  107                  }
 108  108          }
 109  109          return (NULL);
 110  110  }
 111  111  
 112  112  /*
 113  113   * Post the EC_DATALINK sysevent for the given linkid. This sysevent will
 114  114   * be consumed by the datalink sysevent module.
 115  115   */
 116  116  static void
 117  117  dlmgmt_post_sysevent(const char *subclass, datalink_id_t linkid,
 118  118      boolean_t reconfigured)
 119  119  {
 120  120          nvlist_t        *nvl = NULL;
 121  121          sysevent_id_t   eid;
 122  122          int             err;
 123  123  
 124  124          if (((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) != 0) ||
 125  125              ((err = nvlist_add_uint64(nvl, RCM_NV_LINKID, linkid)) != 0) ||
 126  126              ((err = nvlist_add_boolean_value(nvl, RCM_NV_RECONFIGURED,
 127  127              reconfigured)) != 0)) {
 128  128                  goto done;
 129  129          }
 130  130  
 131  131          if (sysevent_post_event(EC_DATALINK, (char *)subclass, SUNW_VENDOR,
 132  132              (char *)progname, nvl, &eid) == -1) {
 133  133                  err = errno;
 134  134          }
 135  135  
 136  136  done:
 137  137          if (err != 0) {
 138  138                  dlmgmt_log(LOG_WARNING, "dlmgmt_post_sysevent(%d) failed: %s",
 139  139                      linkid, strerror(err));
 140  140          }
 141  141          nvlist_free(nvl);
 142  142  }
 143  143  
 144  144  /* ARGSUSED */
 145  145  static void
 146  146  dlmgmt_upcall_create(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 147  147      ucred_t *cred)
 148  148  {
 149  149          dlmgmt_upcall_arg_create_t *create = argp;
 150  150          dlmgmt_create_retval_t  *retvalp = retp;
 151  151          datalink_class_t        class;
 152  152          uint32_t                media;
 153  153          dlmgmt_link_t           *linkp;
 154  154          char                    link[MAXLINKNAMELEN];
 155  155          uint32_t                flags;
 156  156          int                     err = 0;
 157  157          boolean_t               created = B_FALSE;
 158  158          boolean_t               reconfigured = B_FALSE;
 159  159  
 160  160          /*
 161  161           * Determine whether this link is persistent. Note that this request
 162  162           * is coming from kernel so this link must be active.
 163  163           */
 164  164          flags = DLMGMT_ACTIVE | (create->ld_persist ? DLMGMT_PERSIST : 0);
 165  165  
 166  166          class = create->ld_class;
 167  167          media = create->ld_media;
 168  168  
 169  169          /*
 170  170           * Hold the writer lock to update the link table.
 171  171           */
 172  172          dlmgmt_table_lock(B_TRUE);
 173  173  
 174  174          if ((err = dlmgmt_checkprivs(class, cred)) != 0)
 175  175                  goto done;
 176  176  
 177  177          /*
 178  178           * Check to see whether this is the reattachment of an existing
 179  179           * physical link. If so, return its linkid.
 180  180           */
 181  181          if ((class == DATALINK_CLASS_PHYS) && (linkp =
 182  182              dlmgmt_getlink_by_dev(create->ld_devname, zoneid)) != NULL) {
 183  183                  if (linkattr_equal(&(linkp->ll_head), FPHYMAJ,
 184  184                      &create->ld_phymaj, sizeof (uint64_t)) &&
 185  185                      linkattr_equal(&(linkp->ll_head), FPHYINST,
 186  186                      &create->ld_phyinst, sizeof (uint64_t)) &&
 187  187                      (linkp->ll_flags & flags) == flags) {
 188  188                          /*
 189  189                           * If nothing has been changed, directly return.
 190  190                           */
 191  191                          goto noupdate;
 192  192                  }
 193  193  
 194  194                  err = linkattr_set(&(linkp->ll_head), FPHYMAJ,
 195  195                      &create->ld_phymaj, sizeof (uint64_t), DLADM_TYPE_UINT64);
 196  196                  if (err != 0)
 197  197                          goto done;
 198  198  
 199  199                  err = linkattr_set(&(linkp->ll_head), FPHYINST,
 200  200                      &create->ld_phyinst, sizeof (uint64_t), DLADM_TYPE_UINT64);
 201  201                  if (err != 0)
 202  202                          goto done;
 203  203  
 204  204                  /*
 205  205                   * This is a device that is dynamic reconfigured.
 206  206                   */
 207  207                  if ((linkp->ll_flags & DLMGMT_ACTIVE) == 0)
 208  208                          reconfigured = B_TRUE;
 209  209  
 210  210                  if ((err = link_activate(linkp)) != 0)
 211  211                          goto done;
 212  212                  linkp->ll_flags |= flags;
 213  213                  linkp->ll_gen++;
 214  214  
 215  215                  goto done;
 216  216          }
 217  217  
 218  218          if ((err = dlmgmt_create_common(create->ld_devname, class, media,
 219  219              zoneid, flags, &linkp)) == EEXIST) {
 220  220                  /*
 221  221                   * The link name already exists. Return error if this is a
 222  222                   * non-physical link (in that case, the link name must be
 223  223                   * the same as the given name).
 224  224                   */
 225  225                  if (class != DATALINK_CLASS_PHYS)
 226  226                          goto done;
 227  227  
 228  228                  /*
 229  229                   * The physical link's name already exists, request
 230  230                   * a suggested link name: net<nextppa>
 231  231                   */
 232  232                  err = dlmgmt_generate_name("net", link, MAXLINKNAMELEN, zoneid);
 233  233                  if (err != 0)
 234  234                          goto done;
 235  235  
 236  236                  err = dlmgmt_create_common(link, class, media, zoneid, flags,
 237  237                      &linkp);
 238  238          }
 239  239  
 240  240          if (err != 0)
 241  241                  goto done;
 242  242  
 243  243          created = B_TRUE;
 244  244  
 245  245          /*
 246  246           * This is a new link.  Only need to persist link attributes for
 247  247           * physical links.
 248  248           */
 249  249          if (class == DATALINK_CLASS_PHYS &&
 250  250              (((err = linkattr_set(&linkp->ll_head, FDEVNAME, create->ld_devname,
 251  251              strlen(create->ld_devname) + 1, DLADM_TYPE_STR)) != 0) ||
 252  252              ((err = linkattr_set(&linkp->ll_head, FPHYMAJ, &create->ld_phymaj,
 253  253              sizeof (uint64_t), DLADM_TYPE_UINT64)) != 0) ||
 254  254              ((err = linkattr_set(&linkp->ll_head, FPHYINST, &create->ld_phyinst,
 255  255              sizeof (uint64_t), DLADM_TYPE_UINT64)) != 0))) {
 256  256                  (void) dlmgmt_destroy_common(linkp, flags);
 257  257          }
 258  258  
 259  259  done:
 260  260          if ((err == 0) && ((err = dlmgmt_write_db_entry(linkp->ll_link, linkp,
 261  261              linkp->ll_flags)) != 0) && created) {
 262  262                  (void) dlmgmt_destroy_common(linkp, flags);
 263  263          }
 264  264  
 265  265  noupdate:
 266  266          if (err == 0)
 267  267                  retvalp->lr_linkid = linkp->ll_linkid;
 268  268  
 269  269          dlmgmt_table_unlock();
 270  270  
 271  271          if ((err == 0) && (class == DATALINK_CLASS_PHYS)) {
 272  272                  /*
 273  273                   * Post the ESC_DATALINK_PHYS_ADD sysevent. This sysevent
 274  274                   * is consumed by the datalink sysevent module which in
 275  275                   * turn generates the RCM_RESOURCE_LINK_NEW RCM event.
 276  276                   */
 277  277                  dlmgmt_post_sysevent(ESC_DATALINK_PHYS_ADD,
 278  278                      retvalp->lr_linkid, reconfigured);
 279  279          }
 280  280  
 281  281          retvalp->lr_err = err;
 282  282  }
 283  283  
 284  284  /* ARGSUSED */
 285  285  static void
 286  286  dlmgmt_upcall_update(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 287  287      ucred_t *cred)
 288  288  {
 289  289          dlmgmt_upcall_arg_update_t      *update = argp;
 290  290          dlmgmt_update_retval_t          *retvalp = retp;
 291  291          uint32_t                        media = update->ld_media;
 292  292          dlmgmt_link_t                   *linkp;
 293  293          int                             err = 0;
 294  294  
 295  295          /*
 296  296           * Hold the writer lock to update the link table.
 297  297           */
 298  298          dlmgmt_table_lock(B_TRUE);
 299  299  
 300  300          /*
 301  301           * Check to see whether this is the reattachment of an existing
 302  302           * physical link. If so, return its linkid.
 303  303           */
 304  304          if ((linkp = dlmgmt_getlink_by_dev(update->ld_devname, zoneid)) ==
 305  305              NULL) {
 306  306                  err = ENOENT;
 307  307                  goto done;
 308  308          }
 309  309  
 310  310          if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
 311  311                  goto done;
 312  312  
 313  313          retvalp->lr_linkid = linkp->ll_linkid;
 314  314          retvalp->lr_media = media;
 315  315          if (linkp->ll_media != media && linkp->ll_media != DL_OTHER) {
 316  316                  /*
 317  317                   * Assume a DL_ETHER link ce0, a DL_WIFI link ath0
 318  318                   * 1. # dladm rename-link ce0 net0
 319  319                   * 2. DR out ce0. net0 is down.
 320  320                   * 3. use rename-link to have the ath0 device inherit
 321  321                   *    the configuration from net0
 322  322                   *    # dladm rename-link ath0 net0
 323  323                   * 4. DR in ath0.
 324  324                   * As ath0 and ce0 do not have the same media type, ath0
 325  325                   * cannot inherit the configuration of net0.
 326  326                   */
 327  327                  err = EEXIST;
 328  328  
 329  329                  /*
 330  330                   * Return the media type of the existing link to indicate the
 331  331                   * reason for the name conflict.
 332  332                   */
 333  333                  retvalp->lr_media = linkp->ll_media;
 334  334                  goto done;
 335  335          }
 336  336  
 337  337          if (update->ld_novanity &&
 338  338              (strcmp(update->ld_devname, linkp->ll_link) != 0)) {
 339  339                  /*
 340  340                   * Return an error if this is a physical link that does not
 341  341                   * support vanity naming, but the link name is not the same
 342  342                   * as the given device name.
 343  343                   */
 344  344                  err = EEXIST;
 345  345                  goto done;
 346  346          }
 347  347  
 348  348          if (linkp->ll_media != media) {
 349  349                  linkp->ll_media = media;
 350  350                  linkp->ll_gen++;
 351  351                  (void) dlmgmt_write_db_entry(linkp->ll_link, linkp,
 352  352                      linkp->ll_flags);
 353  353          }
 354  354  
 355  355  done:
 356  356          dlmgmt_table_unlock();
 357  357          retvalp->lr_err = err;
 358  358  }
 359  359  
 360  360  /* ARGSUSED */
 361  361  static void
 362  362  dlmgmt_upcall_destroy(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 363  363      ucred_t *cred)
 364  364  {
 365  365          dlmgmt_upcall_arg_destroy_t     *destroy = argp;
 366  366          dlmgmt_destroy_retval_t         *retvalp = retp;
 367  367          datalink_id_t                   linkid = destroy->ld_linkid;
 368  368          dlmgmt_link_t                   *linkp = NULL;
 369  369          uint32_t                        flags, dflags = 0;
 370  370          int                             err = 0;
 371  371  
 372  372          flags = DLMGMT_ACTIVE | (destroy->ld_persist ? DLMGMT_PERSIST : 0);
 373  373  
 374  374          /*
 375  375           * Hold the writer lock to update the link table.
 376  376           */
 377  377          dlmgmt_table_lock(B_TRUE);
 378  378  
 379  379          if ((linkp = link_by_id(linkid, zoneid)) == NULL) {
 380  380                  err = ENOENT;
 381  381                  goto done;
 382  382          }
 383  383  
 384  384          if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
 385  385                  goto done;
 386  386  
 387  387          if (linkp->ll_tomb == B_TRUE) {
 388  388                  err = EINPROGRESS;
 389  389                  goto done;
 390  390          }
 391  391  
 392  392          if (((linkp->ll_flags & flags) & DLMGMT_ACTIVE) != 0) {
 393  393                  if ((err = dlmgmt_delete_db_entry(linkp, DLMGMT_ACTIVE)) != 0)
 394  394                          goto done;
 395  395                  dflags |= DLMGMT_ACTIVE;
 396  396          }
 397  397  
 398  398          if (((linkp->ll_flags & flags) & DLMGMT_PERSIST) != 0) {
 399  399                  if ((err = dlmgmt_delete_db_entry(linkp, DLMGMT_PERSIST)) != 0)
 400  400                          goto done;
 401  401                  dflags |= DLMGMT_PERSIST;
 402  402          }
 403  403  
 404  404          err = dlmgmt_destroy_common(linkp, flags);
 405  405  done:
 406  406          if (err != 0 && dflags != 0)
 407  407                  (void) dlmgmt_write_db_entry(linkp->ll_link, linkp, dflags);
 408  408  
 409  409          dlmgmt_table_unlock();
 410  410          retvalp->lr_err = err;
 411  411  }
 412  412  
 413  413  /* ARGSUSED */
 414  414  static void
 415  415  dlmgmt_getname(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 416  416      ucred_t *cred)
 417  417  {
 418  418          dlmgmt_door_getname_t   *getname = argp;
 419  419          dlmgmt_getname_retval_t *retvalp = retp;
 420  420          dlmgmt_link_t           *linkp;
 421  421          int                     err = 0;
 422  422  
 423  423          /*
 424  424           * Hold the reader lock to access the link
 425  425           */
 426  426          dlmgmt_table_lock(B_FALSE);
 427  427          if ((linkp = link_by_id(getname->ld_linkid, zoneid)) == NULL) {
 428  428                  err = ENOENT;
 429  429          } else if (strlcpy(retvalp->lr_link, linkp->ll_link, MAXLINKNAMELEN) >=
 430  430              MAXLINKNAMELEN) {
 431  431                  err = ENOSPC;
 432  432          } else {
 433  433                  retvalp->lr_flags = linkp->ll_flags;
 434  434                  retvalp->lr_class = linkp->ll_class;
 435  435                  retvalp->lr_media = linkp->ll_media;
 436  436          }
 437  437  
 438  438          dlmgmt_table_unlock();
 439  439          retvalp->lr_err = err;
 440  440  }
 441  441  
  
    | 
      ↓ open down ↓ | 
    364 lines elided | 
    
      ↑ open up ↑ | 
  
 442  442  /* ARGSUSED */
 443  443  static void
 444  444  dlmgmt_getlinkid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 445  445      ucred_t *cred)
 446  446  {
 447  447          dlmgmt_door_getlinkid_t *getlinkid = argp;
 448  448          dlmgmt_getlinkid_retval_t *retvalp = retp;
 449  449          dlmgmt_link_t           *linkp;
 450  450          int                     err = 0;
 451  451  
 452      -        /* Enable the global zone to lookup links it has given away. */
 453      -        if (zoneid == GLOBAL_ZONEID && getlinkid->ld_zoneid != -1)
 454      -                zoneid = getlinkid->ld_zoneid;
 455      -
 456  452          /*
 457  453           * Hold the reader lock to access the link
 458  454           */
 459  455          dlmgmt_table_lock(B_FALSE);
 460  456  
 461  457          if ((linkp = link_by_name(getlinkid->ld_link, zoneid)) == NULL) {
 462  458                  /*
 463  459                   * The link does not exist in this zone.
 464  460                   */
 465  461                  err = ENOENT;
 466  462                  goto done;
 467  463          }
 468  464  
 469  465          retvalp->lr_linkid = linkp->ll_linkid;
 470  466          retvalp->lr_flags = linkp->ll_flags;
 471  467          retvalp->lr_class = linkp->ll_class;
 472  468          retvalp->lr_media = linkp->ll_media;
 473  469  
 474  470  done:
 475  471          dlmgmt_table_unlock();
 476  472          retvalp->lr_err = err;
 477  473  }
 478  474  
 479  475  /* ARGSUSED */
 480  476  static void
 481  477  dlmgmt_getnext(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 482  478      ucred_t *cred)
 483  479  {
 484  480          dlmgmt_door_getnext_t   *getnext = argp;
 485  481          dlmgmt_getnext_retval_t *retvalp = retp;
 486  482          dlmgmt_link_t           link, *linkp;
 487  483          avl_index_t             where;
 488  484          int                     err = 0;
 489  485  
 490  486          /*
 491  487           * Hold the reader lock to access the link
 492  488           */
 493  489          dlmgmt_table_lock(B_FALSE);
 494  490  
 495  491          link.ll_linkid = (getnext->ld_linkid + 1);
 496  492          if ((linkp = avl_find(&dlmgmt_id_avl, &link, &where)) == NULL)
 497  493                  linkp = avl_nearest(&dlmgmt_id_avl, where, AVL_AFTER);
 498  494  
 499  495          for (; linkp != NULL; linkp = AVL_NEXT(&dlmgmt_id_avl, linkp)) {
 500  496                  if (!link_is_visible(linkp, zoneid))
 501  497                          continue;
 502  498                  if ((linkp->ll_class & getnext->ld_class) &&
 503  499                      (linkp->ll_flags & getnext->ld_flags) &&
 504  500                      DATALINK_MEDIA_ACCEPTED(getnext->ld_dmedia,
 505  501                      linkp->ll_media))
 506  502                          break;
 507  503          }
 508  504  
 509  505          if (linkp == NULL) {
 510  506                  err = ENOENT;
 511  507          } else {
 512  508                  retvalp->lr_linkid = linkp->ll_linkid;
 513  509                  retvalp->lr_class = linkp->ll_class;
 514  510                  retvalp->lr_media = linkp->ll_media;
 515  511                  retvalp->lr_flags = linkp->ll_flags;
 516  512          }
 517  513  
 518  514          dlmgmt_table_unlock();
 519  515          retvalp->lr_err = err;
 520  516  }
 521  517  
 522  518  /* ARGSUSED */
 523  519  static void
 524  520  dlmgmt_upcall_getattr(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 525  521      ucred_t *cred)
 526  522  {
 527  523          dlmgmt_upcall_arg_getattr_t     *getattr = argp;
 528  524          dlmgmt_getattr_retval_t         *retvalp = retp;
 529  525          dlmgmt_link_t                   *linkp;
 530  526  
 531  527          /*
 532  528           * Hold the reader lock to access the link
 533  529           */
 534  530          dlmgmt_table_lock(B_FALSE);
 535  531          if ((linkp = link_by_id(getattr->ld_linkid, zoneid)) == NULL) {
 536  532                  retvalp->lr_err = ENOENT;
 537  533          } else {
 538  534                  retvalp->lr_err = dlmgmt_getattr_common(&linkp->ll_head,
 539  535                      getattr->ld_attr, retvalp);
 540  536          }
 541  537          dlmgmt_table_unlock();
 542  538  }
 543  539  
 544  540  /* ARGSUSED */
 545  541  static void
 546  542  dlmgmt_createid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 547  543      ucred_t *cred)
 548  544  {
 549  545          dlmgmt_door_createid_t  *createid = argp;
 550  546          dlmgmt_createid_retval_t *retvalp = retp;
 551  547          dlmgmt_link_t           *linkp;
 552  548          datalink_id_t           linkid = DATALINK_INVALID_LINKID;
 553  549          char                    link[MAXLINKNAMELEN];
 554  550          int                     err;
 555  551  
 556  552          /*
 557  553           * Hold the writer lock to update the dlconf table.
 558  554           */
 559  555          dlmgmt_table_lock(B_TRUE);
 560  556  
 561  557          if ((err = dlmgmt_checkprivs(createid->ld_class, cred)) != 0)
 562  558                  goto done;
 563  559  
 564  560          if (createid->ld_prefix) {
 565  561                  err = dlmgmt_generate_name(createid->ld_link, link,
 566  562                      MAXLINKNAMELEN, zoneid);
 567  563                  if (err != 0)
 568  564                          goto done;
 569  565  
 570  566                  err = dlmgmt_create_common(link, createid->ld_class,
 571  567                      createid->ld_media, zoneid, createid->ld_flags, &linkp);
 572  568          } else {
 573  569                  err = dlmgmt_create_common(createid->ld_link,
 574  570                      createid->ld_class, createid->ld_media, zoneid,
 575  571                      createid->ld_flags, &linkp);
 576  572          }
 577  573  
 578  574          if (err == 0) {
 579  575                  /*
 580  576                   * Keep the active mapping.
 581  577                   */
 582  578                  linkid = linkp->ll_linkid;
 583  579                  if (createid->ld_flags & DLMGMT_ACTIVE) {
 584  580                          (void) dlmgmt_write_db_entry(linkp->ll_link, linkp,
 585  581                              DLMGMT_ACTIVE);
 586  582                  }
 587  583          }
 588  584  
 589  585  done:
 590  586          dlmgmt_table_unlock();
 591  587          retvalp->lr_linkid = linkid;
 592  588          retvalp->lr_err = err;
 593  589  }
 594  590  
 595  591  /* ARGSUSED */
 596  592  static void
 597  593  dlmgmt_destroyid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 598  594      ucred_t *cred)
 599  595  {
 600  596          dlmgmt_door_destroyid_t *destroyid = argp;
 601  597          dlmgmt_destroyid_retval_t *retvalp = retp;
 602  598          datalink_id_t           linkid = destroyid->ld_linkid;
 603  599          uint32_t                flags = destroyid->ld_flags;
 604  600          dlmgmt_link_t           *linkp = NULL;
 605  601          int                     err = 0;
 606  602  
 607  603          /*
 608  604           * Hold the writer lock to update the link table.
 609  605           */
 610  606          dlmgmt_table_lock(B_TRUE);
 611  607          if ((linkp = link_by_id(linkid, zoneid)) == NULL) {
 612  608                  err = ENOENT;
 613  609                  goto done;
 614  610          }
 615  611  
 616  612          if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
 617  613                  goto done;
 618  614  
 619  615          /*
 620  616           * Delete the active mapping.
 621  617           */
 622  618          if (flags & DLMGMT_ACTIVE)
 623  619                  err = dlmgmt_delete_db_entry(linkp, DLMGMT_ACTIVE);
 624  620          if (err == 0)
 625  621                  err = dlmgmt_destroy_common(linkp, flags);
 626  622  done:
 627  623          dlmgmt_table_unlock();
 628  624          retvalp->lr_err = err;
 629  625  }
 630  626  
 631  627  /*
 632  628   * Remap a linkid to a given link name, i.e., rename an existing link1
 633  629   * (ld_linkid) to a non-existent link2 (ld_link): rename link1's name to
 634  630   * the given link name.
 635  631   */
 636  632  /* ARGSUSED */
 637  633  static void
 638  634  dlmgmt_remapid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 639  635      ucred_t *cred)
 640  636  {
 641  637          dlmgmt_door_remapid_t   *remapid = argp;
 642  638          dlmgmt_remapid_retval_t *retvalp = retp;
 643  639          dlmgmt_link_t           *linkp;
 644  640          char                    oldname[MAXLINKNAMELEN];
 645  641          boolean_t               renamed = B_FALSE;
 646  642          int                     err = 0;
 647  643  
 648  644          if (!dladm_valid_linkname(remapid->ld_link)) {
 649  645                  retvalp->lr_err = EINVAL;
 650  646                  return;
 651  647          }
 652  648  
 653  649          /*
 654  650           * Hold the writer lock to update the link table.
 655  651           */
 656  652          dlmgmt_table_lock(B_TRUE);
 657  653          if ((linkp = link_by_id(remapid->ld_linkid, zoneid)) == NULL) {
 658  654                  err = ENOENT;
 659  655                  goto done;
 660  656          }
 661  657  
 662  658          if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
 663  659                  goto done;
 664  660  
 665  661          if (linkp->ll_tomb == B_TRUE) {
 666  662                  err = EBUSY;
 667  663                  goto done;
 668  664          }
 669  665  
 670  666  
 671  667          if (link_by_name(remapid->ld_link, linkp->ll_zoneid) != NULL) {
 672  668                  err = EEXIST;
 673  669                  goto done;
 674  670          }
 675  671  
 676  672          (void) strlcpy(oldname, linkp->ll_link, MAXLINKNAMELEN);
 677  673          avl_remove(&dlmgmt_name_avl, linkp);
 678  674          (void) strlcpy(linkp->ll_link, remapid->ld_link, MAXLINKNAMELEN);
 679  675          avl_add(&dlmgmt_name_avl, linkp);
 680  676          renamed = B_TRUE;
 681  677  
 682  678          if (linkp->ll_flags & DLMGMT_ACTIVE) {
 683  679                  err = dlmgmt_write_db_entry(oldname, linkp, DLMGMT_ACTIVE);
 684  680                  if (err != 0)
 685  681                          goto done;
 686  682          }
 687  683          if (linkp->ll_flags & DLMGMT_PERSIST) {
 688  684                  err = dlmgmt_write_db_entry(oldname, linkp, DLMGMT_PERSIST);
 689  685                  if (err != 0) {
 690  686                          if (linkp->ll_flags & DLMGMT_ACTIVE) {
 691  687                                  (void) dlmgmt_write_db_entry(remapid->ld_link,
 692  688                                      linkp, DLMGMT_ACTIVE);
 693  689                          }
 694  690                          goto done;
 695  691                  }
 696  692          }
 697  693  
 698  694          dlmgmt_advance(linkp);
 699  695          linkp->ll_gen++;
 700  696  done:
 701  697          if (err != 0 && renamed) {
 702  698                  avl_remove(&dlmgmt_name_avl, linkp);
 703  699                  (void) strlcpy(linkp->ll_link, oldname, MAXLINKNAMELEN);
 704  700                  avl_add(&dlmgmt_name_avl, linkp);
 705  701          }
 706  702          dlmgmt_table_unlock();
 707  703          retvalp->lr_err = err;
 708  704  }
 709  705  
 710  706  /* ARGSUSED */
 711  707  static void
 712  708  dlmgmt_upid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 713  709      ucred_t *cred)
 714  710  {
 715  711          dlmgmt_door_upid_t      *upid = argp;
 716  712          dlmgmt_upid_retval_t    *retvalp = retp;
 717  713          dlmgmt_link_t           *linkp;
 718  714          int                     err = 0;
 719  715  
 720  716          /*
 721  717           * Hold the writer lock to update the link table.
 722  718           */
 723  719          dlmgmt_table_lock(B_TRUE);
 724  720          if ((linkp = link_by_id(upid->ld_linkid, zoneid)) == NULL) {
 725  721                  err = ENOENT;
 726  722                  goto done;
 727  723          }
 728  724  
 729  725          if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
 730  726                  goto done;
 731  727  
 732  728          if (linkp->ll_tomb == B_TRUE) {
 733  729                  err = EBUSY;
 734  730                  goto done;
 735  731          }
 736  732  
 737  733          if (linkp->ll_flags & DLMGMT_ACTIVE) {
 738  734                  err = EINVAL;
 739  735                  goto done;
 740  736          }
 741  737  
 742  738          if ((err = link_activate(linkp)) == 0) {
 743  739                  (void) dlmgmt_write_db_entry(linkp->ll_link, linkp,
 744  740                      DLMGMT_ACTIVE);
 745  741          }
 746  742  done:
 747  743          dlmgmt_table_unlock();
 748  744          retvalp->lr_err = err;
 749  745  }
 750  746  
 751  747  /* ARGSUSED */
 752  748  static void
 753  749  dlmgmt_createconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 754  750      ucred_t *cred)
 755  751  {
 756  752          dlmgmt_door_createconf_t *createconf = argp;
 757  753          dlmgmt_createconf_retval_t *retvalp = retp;
 758  754          dlmgmt_dlconf_t         *dlconfp;
 759  755          int                     err;
 760  756  
 761  757          /*
 762  758           * Hold the writer lock to update the dlconf table.
 763  759           */
 764  760          dlmgmt_dlconf_table_lock(B_TRUE);
 765  761  
 766  762          if ((err = dlmgmt_checkprivs(createconf->ld_class, cred)) != 0)
 767  763                  goto done;
 768  764  
 769  765          err = dlconf_create(createconf->ld_link, createconf->ld_linkid,
 770  766              createconf->ld_class, createconf->ld_media, zoneid, &dlconfp);
 771  767          if (err == 0) {
 772  768                  avl_add(&dlmgmt_dlconf_avl, dlconfp);
 773  769                  dlmgmt_advance_dlconfid(dlconfp);
 774  770                  retvalp->lr_confid = dlconfp->ld_id;
 775  771          }
 776  772  done:
 777  773          dlmgmt_dlconf_table_unlock();
 778  774          retvalp->lr_err = err;
 779  775  }
 780  776  
 781  777  /* ARGSUSED */
 782  778  static void
 783  779  dlmgmt_setattr(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 784  780      ucred_t *cred)
 785  781  {
 786  782          dlmgmt_door_setattr_t   *setattr = argp;
 787  783          dlmgmt_setattr_retval_t *retvalp = retp;
 788  784          dlmgmt_dlconf_t         dlconf, *dlconfp;
 789  785          int                     err = 0;
 790  786  
 791  787          /*
 792  788           * Hold the writer lock to update the dlconf table.
 793  789           */
 794  790          dlmgmt_dlconf_table_lock(B_TRUE);
 795  791  
 796  792          dlconf.ld_id = setattr->ld_confid;
 797  793          dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL);
 798  794          if (dlconfp == NULL || zoneid != dlconfp->ld_zoneid) {
 799  795                  err = ENOENT;
 800  796                  goto done;
 801  797          }
 802  798  
 803  799          if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0)
 804  800                  goto done;
 805  801  
 806  802          err = linkattr_set(&(dlconfp->ld_head), setattr->ld_attr,
 807  803              &setattr->ld_attrval, setattr->ld_attrsz, setattr->ld_type);
 808  804  
 809  805  done:
 810  806          dlmgmt_dlconf_table_unlock();
 811  807          retvalp->lr_err = err;
 812  808  }
 813  809  
 814  810  /* ARGSUSED */
 815  811  static void
 816  812  dlmgmt_unsetconfattr(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 817  813      ucred_t *cred)
 818  814  {
 819  815          dlmgmt_door_unsetattr_t *unsetattr = argp;
 820  816          dlmgmt_unsetattr_retval_t *retvalp = retp;
 821  817          dlmgmt_dlconf_t         dlconf, *dlconfp;
 822  818          int                     err = 0;
 823  819  
 824  820          /*
 825  821           * Hold the writer lock to update the dlconf table.
 826  822           */
 827  823          dlmgmt_dlconf_table_lock(B_TRUE);
 828  824  
 829  825          dlconf.ld_id = unsetattr->ld_confid;
 830  826          dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL);
 831  827          if (dlconfp == NULL || zoneid != dlconfp->ld_zoneid) {
 832  828                  err = ENOENT;
 833  829                  goto done;
 834  830          }
 835  831  
 836  832          if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0)
 837  833                  goto done;
 838  834  
 839  835          linkattr_unset(&(dlconfp->ld_head), unsetattr->ld_attr);
 840  836  
 841  837  done:
 842  838          dlmgmt_dlconf_table_unlock();
 843  839          retvalp->lr_err = err;
 844  840  }
 845  841  
 846  842  /*
 847  843   * Note that dlmgmt_openconf() returns a conf ID of a conf AVL tree entry,
 848  844   * which is managed by dlmgmtd.  The ID is used to find the conf entry when
 849  845   * dlmgmt_write_conf() is called.  The conf entry contains an ld_gen value
 850  846   * (which is the generation number - ll_gen) of the dlmgmt_link_t at the time
 851  847   * of dlmgmt_openconf(), and ll_gen changes every time the dlmgmt_link_t
 852  848   * changes its attributes.  Therefore, dlmgmt_write_conf() can compare ld_gen
 853  849   * in the conf entry against the latest dlmgmt_link_t ll_gen value to see if
 854  850   * anything has changed between the dlmgmt_openconf() and dlmgmt_writeconf()
 855  851   * calls.  If so, EAGAIN is returned.  This mechanism can ensures atomicity
 856  852   * across the pair of dladm_read_conf() and dladm_write_conf() calls.
 857  853   */
 858  854  /* ARGSUSED */
 859  855  static void
 860  856  dlmgmt_writeconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 861  857      ucred_t *cred)
 862  858  {
 863  859          dlmgmt_door_writeconf_t *writeconf = argp;
 864  860          dlmgmt_writeconf_retval_t *retvalp = retp;
 865  861          dlmgmt_dlconf_t         dlconf, *dlconfp;
 866  862          dlmgmt_link_t           *linkp;
 867  863          dlmgmt_linkattr_t       *attrp, *next;
 868  864          int                     err = 0;
 869  865  
 870  866          /*
 871  867           * Hold the lock to access the dlconf table.
 872  868           */
 873  869          dlmgmt_dlconf_table_lock(B_TRUE);
 874  870  
 875  871          dlconf.ld_id = writeconf->ld_confid;
 876  872          dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL);
 877  873          if (dlconfp == NULL || zoneid != dlconfp->ld_zoneid) {
 878  874                  err = ENOENT;
 879  875                  goto done;
 880  876          }
 881  877  
 882  878          if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0)
 883  879                  goto done;
 884  880  
 885  881          /*
 886  882           * Hold the writer lock to update the link table.
 887  883           */
 888  884          dlmgmt_table_lock(B_TRUE);
 889  885          linkp = link_by_id(dlconfp->ld_linkid, zoneid);
 890  886          if ((linkp == NULL) || (linkp->ll_class != dlconfp->ld_class) ||
 891  887              (linkp->ll_media != dlconfp->ld_media) ||
 892  888              (strcmp(linkp->ll_link, dlconfp->ld_link) != 0)) {
 893  889                  /*
 894  890                   * The link does not exist.
 895  891                   */
 896  892                  dlmgmt_table_unlock();
 897  893                  err = ENOENT;
 898  894                  goto done;
 899  895          }
 900  896  
 901  897          if (linkp->ll_gen != dlconfp->ld_gen) {
 902  898                  /*
 903  899                   * Something has changed the link configuration; try again.
 904  900                   */
 905  901                  dlmgmt_table_unlock();
 906  902                  err = EAGAIN;
 907  903                  goto done;
 908  904          }
 909  905  
 910  906          /*
 911  907           * Delete the old attribute list.
 912  908           */
 913  909          for (attrp = linkp->ll_head; attrp != NULL; attrp = next) {
 914  910                  next = attrp->lp_next;
 915  911                  free(attrp->lp_val);
 916  912                  free(attrp);
 917  913          }
 918  914          linkp->ll_head = NULL;
 919  915  
 920  916          /*
 921  917           * Set the new attribute.
 922  918           */
 923  919          for (attrp = dlconfp->ld_head; attrp != NULL; attrp = attrp->lp_next) {
 924  920                  if ((err = linkattr_set(&(linkp->ll_head), attrp->lp_name,
 925  921                      attrp->lp_val, attrp->lp_sz, attrp->lp_type)) != 0) {
 926  922                          dlmgmt_table_unlock();
 927  923                          goto done;
 928  924                  }
 929  925          }
 930  926  
 931  927          linkp->ll_gen++;
 932  928          err = dlmgmt_write_db_entry(linkp->ll_link, linkp, DLMGMT_PERSIST);
 933  929          dlmgmt_table_unlock();
 934  930  done:
 935  931          dlmgmt_dlconf_table_unlock();
 936  932          retvalp->lr_err = err;
 937  933  }
 938  934  
 939  935  /* ARGSUSED */
 940  936  static void
 941  937  dlmgmt_removeconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 942  938      ucred_t *cred)
 943  939  {
 944  940          dlmgmt_door_removeconf_t        *removeconf = argp;
 945  941          dlmgmt_removeconf_retval_t      *retvalp = retp;
 946  942          dlmgmt_link_t                   *linkp;
 947  943          int                             err;
 948  944  
 949  945          dlmgmt_table_lock(B_TRUE);
 950  946          if ((linkp = link_by_id(removeconf->ld_linkid, zoneid)) == NULL) {
 951  947                  err = ENOENT;
 952  948                  goto done;
 953  949          }
 954  950          if (zoneid != GLOBAL_ZONEID && linkp->ll_onloan) {
 955  951                  /*
 956  952                   * A non-global zone cannot remove the persistent
 957  953                   * configuration of a link that is on loan from the global
 958  954                   * zone.
 959  955                   */
 960  956                  err = EACCES;
 961  957                  goto done;
 962  958          }
 963  959          if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
 964  960                  goto done;
 965  961  
 966  962          err = dlmgmt_delete_db_entry(linkp, DLMGMT_PERSIST);
 967  963  done:
 968  964          dlmgmt_table_unlock();
 969  965          retvalp->lr_err = err;
 970  966  }
 971  967  
 972  968  /* ARGSUSED */
 973  969  static void
 974  970  dlmgmt_destroyconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
 975  971      ucred_t *cred)
 976  972  {
 977  973          dlmgmt_door_destroyconf_t       *destroyconf = argp;
 978  974          dlmgmt_destroyconf_retval_t     *retvalp = retp;
 979  975          dlmgmt_dlconf_t                 dlconf, *dlconfp;
 980  976          int                             err = 0;
 981  977  
 982  978          /*
 983  979           * Hold the writer lock to update the dlconf table.
 984  980           */
 985  981          dlmgmt_dlconf_table_lock(B_TRUE);
 986  982  
 987  983          dlconf.ld_id = destroyconf->ld_confid;
 988  984          dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL);
 989  985          if (dlconfp == NULL || zoneid != dlconfp->ld_zoneid) {
 990  986                  err = ENOENT;
 991  987                  goto done;
 992  988          }
 993  989  
 994  990          if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0)
 995  991                  goto done;
 996  992  
 997  993          avl_remove(&dlmgmt_dlconf_avl, dlconfp);
 998  994          dlconf_destroy(dlconfp);
 999  995  
1000  996  done:
1001  997          dlmgmt_dlconf_table_unlock();
1002  998          retvalp->lr_err = err;
1003  999  }
1004 1000  
1005 1001  /*
1006 1002   * dlmgmt_openconf() returns a handle of the current configuration, which
1007 1003   * is then used to update the configuration by dlmgmt_writeconf(). Therefore,
1008 1004   * it requires privileges.
1009 1005   *
1010 1006   * Further, please see the comments above dladm_write_conf() to see how
1011 1007   * ld_gen is used to ensure atomicity across the {dlmgmt_openconf(),
1012 1008   * dlmgmt_writeconf()} pair.
1013 1009   */
1014 1010  /* ARGSUSED */
1015 1011  static void
1016 1012  dlmgmt_openconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1017 1013      ucred_t *cred)
1018 1014  {
1019 1015          dlmgmt_door_openconf_t  *openconf = argp;
1020 1016          dlmgmt_openconf_retval_t *retvalp = retp;
1021 1017          dlmgmt_link_t           *linkp;
1022 1018          datalink_id_t           linkid = openconf->ld_linkid;
1023 1019          dlmgmt_dlconf_t         *dlconfp;
1024 1020          dlmgmt_linkattr_t       *attrp;
1025 1021          int                     err = 0;
1026 1022  
1027 1023          /*
1028 1024           * Hold the writer lock to update the dlconf table.
1029 1025           */
1030 1026          dlmgmt_dlconf_table_lock(B_TRUE);
1031 1027  
1032 1028          /*
1033 1029           * Hold the reader lock to access the link
1034 1030           */
1035 1031          dlmgmt_table_lock(B_FALSE);
1036 1032          linkp = link_by_id(linkid, zoneid);
1037 1033          if ((linkp == NULL) || !(linkp->ll_flags & DLMGMT_PERSIST)) {
1038 1034                  /* The persistent link configuration does not exist. */
1039 1035                  err = ENOENT;
1040 1036                  goto done;
1041 1037          }
1042 1038          if (linkp->ll_onloan && zoneid != GLOBAL_ZONEID) {
1043 1039                  /*
1044 1040                   * The caller is in a non-global zone and the persistent
1045 1041                   * configuration belongs to the global zone.
1046 1042                   */
1047 1043                  err = EACCES;
1048 1044                  goto done;
1049 1045          }
1050 1046  
1051 1047          if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
1052 1048                  goto done;
1053 1049  
1054 1050          if ((err = dlconf_create(linkp->ll_link, linkp->ll_linkid,
1055 1051              linkp->ll_class, linkp->ll_media, zoneid, &dlconfp)) != 0)
1056 1052                  goto done;
1057 1053  
1058 1054          for (attrp = linkp->ll_head; attrp != NULL; attrp = attrp->lp_next) {
1059 1055                  if ((err = linkattr_set(&(dlconfp->ld_head), attrp->lp_name,
1060 1056                      attrp->lp_val, attrp->lp_sz, attrp->lp_type)) != 0) {
1061 1057                          dlconf_destroy(dlconfp);
1062 1058                          goto done;
1063 1059                  }
1064 1060          }
1065 1061          dlconfp->ld_gen = linkp->ll_gen;
1066 1062          avl_add(&dlmgmt_dlconf_avl, dlconfp);
1067 1063          dlmgmt_advance_dlconfid(dlconfp);
1068 1064  
1069 1065          retvalp->lr_confid = dlconfp->ld_id;
1070 1066  done:
1071 1067          dlmgmt_table_unlock();
1072 1068          dlmgmt_dlconf_table_unlock();
1073 1069          retvalp->lr_err = err;
1074 1070  }
1075 1071  
1076 1072  /*
1077 1073   * dlmgmt_getconfsnapshot() returns a read-only snapshot of all the
1078 1074   * configuration, and requires no privileges.
1079 1075   *
1080 1076   * If the given size cannot hold all the configuration, set the size
1081 1077   * that is needed, and return ENOSPC.
1082 1078   */
1083 1079  /* ARGSUSED */
1084 1080  static void
1085 1081  dlmgmt_getconfsnapshot(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1086 1082      ucred_t *cred)
1087 1083  {
1088 1084          dlmgmt_door_getconfsnapshot_t   *snapshot = argp;
1089 1085          dlmgmt_getconfsnapshot_retval_t *retvalp = retp;
1090 1086          dlmgmt_link_t                   *linkp;
1091 1087          datalink_id_t                   linkid = snapshot->ld_linkid;
1092 1088          dlmgmt_linkattr_t               *attrp;
1093 1089          char                            *buf;
1094 1090          size_t                          nvlsz;
1095 1091          nvlist_t                        *nvl = NULL;
1096 1092          int                             err = 0;
1097 1093  
1098 1094          assert(*sz >= sizeof (dlmgmt_getconfsnapshot_retval_t));
1099 1095  
1100 1096          /*
1101 1097           * Hold the reader lock to access the link
1102 1098           */
1103 1099          dlmgmt_table_lock(B_FALSE);
1104 1100          linkp = link_by_id(linkid, zoneid);
1105 1101          if ((linkp == NULL) || !(linkp->ll_flags & DLMGMT_PERSIST)) {
1106 1102                  /* The persistent link configuration does not exist. */
1107 1103                  err = ENOENT;
1108 1104                  goto done;
1109 1105          }
1110 1106          if (linkp->ll_onloan && zoneid != GLOBAL_ZONEID) {
1111 1107                  /*
1112 1108                   * The caller is in a non-global zone and the persistent
1113 1109                   * configuration belongs to the global zone.
1114 1110                   */
1115 1111                  err = EACCES;
1116 1112                  goto done;
1117 1113          }
1118 1114  
1119 1115          err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0);
1120 1116          if (err != 0)
1121 1117                  goto done;
1122 1118  
1123 1119          for (attrp = linkp->ll_head; attrp != NULL; attrp = attrp->lp_next) {
1124 1120                  if ((err = nvlist_add_byte_array(nvl, attrp->lp_name,
1125 1121                      attrp->lp_val, attrp->lp_sz)) != 0) {
1126 1122                          goto done;
1127 1123                  }
1128 1124          }
1129 1125  
1130 1126          if ((err = nvlist_size(nvl, &nvlsz, NV_ENCODE_NATIVE)) != 0)
1131 1127                  goto done;
1132 1128  
1133 1129          if (nvlsz + sizeof (dlmgmt_getconfsnapshot_retval_t) > *sz) {
1134 1130                  *sz = nvlsz + sizeof (dlmgmt_getconfsnapshot_retval_t);
1135 1131                  err = ENOSPC;
1136 1132                  goto done;
1137 1133          }
1138 1134  
1139 1135          /*
1140 1136           * pack the the nvlist into the return value.
1141 1137           */
1142 1138          *sz = nvlsz + sizeof (dlmgmt_getconfsnapshot_retval_t);
1143 1139          retvalp->lr_nvlsz = nvlsz;
1144 1140          buf = (char *)retvalp + sizeof (dlmgmt_getconfsnapshot_retval_t);
1145 1141          err = nvlist_pack(nvl, &buf, &nvlsz, NV_ENCODE_NATIVE, 0);
1146 1142  
1147 1143  done:
1148 1144          dlmgmt_table_unlock();
1149 1145          nvlist_free(nvl);
1150 1146          retvalp->lr_err = err;
1151 1147  }
1152 1148  
1153 1149  /* ARGSUSED */
1154 1150  static void
1155 1151  dlmgmt_getattr(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1156 1152      ucred_t *cred)
1157 1153  {
1158 1154          dlmgmt_door_getattr_t   *getattr = argp;
1159 1155          dlmgmt_getattr_retval_t *retvalp = retp;
1160 1156          dlmgmt_dlconf_t         dlconf, *dlconfp;
1161 1157          int                     err;
1162 1158  
1163 1159          /*
1164 1160           * Hold the read lock to access the dlconf table.
1165 1161           */
1166 1162          dlmgmt_dlconf_table_lock(B_FALSE);
1167 1163  
1168 1164          dlconf.ld_id = getattr->ld_confid;
1169 1165          if ((dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL)) == NULL ||
1170 1166              zoneid != dlconfp->ld_zoneid) {
1171 1167                  retvalp->lr_err = ENOENT;
1172 1168          } else {
1173 1169                  if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0) {
1174 1170                          retvalp->lr_err = err;
1175 1171                  } else {
1176 1172                          retvalp->lr_err = dlmgmt_getattr_common(
1177 1173                              &dlconfp->ld_head, getattr->ld_attr, retvalp);
1178 1174                  }
1179 1175          }
1180 1176  
1181 1177          dlmgmt_dlconf_table_unlock();
1182 1178  }
1183 1179  
1184 1180  /* ARGSUSED */
1185 1181  static void
1186 1182  dlmgmt_upcall_linkprop_init(void *argp, void *retp, size_t *sz,
1187 1183      zoneid_t zoneid, ucred_t *cred)
1188 1184  {
1189 1185          dlmgmt_door_linkprop_init_t     *lip = argp;
1190 1186          dlmgmt_linkprop_init_retval_t   *retvalp = retp;
1191 1187          dlmgmt_link_t                   *linkp;
1192 1188          int                             err;
1193 1189  
1194 1190          dlmgmt_table_lock(B_FALSE);
1195 1191          if ((linkp = link_by_id(lip->ld_linkid, zoneid)) == NULL)
1196 1192                  err = ENOENT;
1197 1193          else
1198 1194                  err = dlmgmt_checkprivs(linkp->ll_class, cred);
1199 1195          dlmgmt_table_unlock();
1200 1196  
1201 1197          if (err == 0) {
1202 1198                  dladm_status_t  s;
1203 1199                  char            buf[DLADM_STRSIZE];
1204 1200  
1205 1201                  s = dladm_init_linkprop(dld_handle, lip->ld_linkid, B_TRUE);
1206 1202                  if (s != DLADM_STATUS_OK) {
1207 1203                          dlmgmt_log(LOG_WARNING,
1208 1204                              "linkprop initialization failed on link %d: %s",
1209 1205                              lip->ld_linkid, dladm_status2str(s, buf));
1210 1206                          err = EINVAL;
1211 1207                  }
1212 1208          }
1213 1209          retvalp->lr_err = err;
1214 1210  }
1215 1211  
1216 1212  /* ARGSUSED */
1217 1213  static void
1218 1214  dlmgmt_setzoneid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1219 1215      ucred_t *cred)
1220 1216  {
1221 1217          dlmgmt_door_setzoneid_t *setzoneid = argp;
1222 1218          dlmgmt_setzoneid_retval_t *retvalp = retp;
1223 1219          dlmgmt_link_t           *linkp;
1224 1220          datalink_id_t           linkid = setzoneid->ld_linkid;
1225 1221          zoneid_t                oldzoneid, newzoneid;
1226 1222          int                     err = 0;
1227 1223  
1228 1224          dlmgmt_table_lock(B_TRUE);
1229 1225  
1230 1226          /* We currently only allow changing zoneid's from the global zone. */
1231 1227          if (zoneid != GLOBAL_ZONEID) {
1232 1228                  err = EACCES;
1233 1229                  goto done;
1234 1230          }
1235 1231  
1236 1232          if ((linkp = link_by_id(linkid, zoneid)) == NULL) {
1237 1233                  err = ENOENT;
1238 1234                  goto done;
1239 1235          }
1240 1236  
1241 1237          if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
1242 1238                  goto done;
1243 1239  
1244 1240          if (linkp->ll_tomb == B_TRUE) {
1245 1241                  err = EBUSY;
1246 1242                  goto done;
1247 1243          }
1248 1244  
1249 1245          /* We can only assign an active link to a zone. */
1250 1246          if (!(linkp->ll_flags & DLMGMT_ACTIVE)) {
1251 1247                  err = EINVAL;
1252 1248                  goto done;
1253 1249          }
1254 1250  
1255 1251          oldzoneid = linkp->ll_zoneid;
1256 1252          newzoneid = setzoneid->ld_zoneid;
1257 1253  
1258 1254          if (oldzoneid == newzoneid)
1259 1255                  goto done;
1260 1256  
1261 1257          /*
1262 1258           * Before we remove the link from its current zone, make sure that
1263 1259           * there isn't a link with the same name in the destination zone.
1264 1260           */
1265 1261          if (zoneid != GLOBAL_ZONEID &&
1266 1262              link_by_name(linkp->ll_link, newzoneid) != NULL) {
1267 1263                  err = EEXIST;
  
    | 
      ↓ open down ↓ | 
    802 lines elided | 
    
      ↑ open up ↑ | 
  
1268 1264                  goto done;
1269 1265          }
1270 1266  
1271 1267          if (oldzoneid != GLOBAL_ZONEID) {
1272 1268                  if (zone_remove_datalink(oldzoneid, linkid) != 0) {
1273 1269                          err = errno;
1274 1270                          dlmgmt_log(LOG_WARNING, "unable to remove link %d from "
1275 1271                              "zone %d: %s", linkid, oldzoneid, strerror(err));
1276 1272                          goto done;
1277 1273                  }
1278      -
1279      -                if (newzoneid == GLOBAL_ZONEID && linkp->ll_onloan) {
1280      -                        /*
1281      -                         * We can only reassign a loaned VNIC back to the
1282      -                         * global zone when the zone is shutting down, since
1283      -                         * otherwise the VNIC is in use by the zone and will be
1284      -                         * busy.  Leave the VNIC assigned to the zone so we can
1285      -                         * still see it and delete it when dlmgmt_zonehalt()
1286      -                         * runs.
1287      -                         */
1288      -                        goto done;
1289      -                }
1290      -
     1274 +                avl_remove(&dlmgmt_loan_avl, linkp);
1291 1275                  linkp->ll_onloan = B_FALSE;
1292 1276          }
1293 1277          if (newzoneid != GLOBAL_ZONEID) {
1294 1278                  if (zone_add_datalink(newzoneid, linkid) != 0) {
1295 1279                          err = errno;
1296 1280                          dlmgmt_log(LOG_WARNING, "unable to add link %d to zone "
1297 1281                              "%d: %s", linkid, newzoneid, strerror(err));
1298 1282                          (void) zone_add_datalink(oldzoneid, linkid);
1299 1283                          goto done;
1300 1284                  }
     1285 +                avl_add(&dlmgmt_loan_avl, linkp);
1301 1286                  linkp->ll_onloan = B_TRUE;
1302 1287          }
1303 1288  
1304 1289          avl_remove(&dlmgmt_name_avl, linkp);
1305 1290          linkp->ll_zoneid = newzoneid;
1306 1291          avl_add(&dlmgmt_name_avl, linkp);
1307 1292  
1308 1293  done:
1309 1294          dlmgmt_table_unlock();
1310 1295          retvalp->lr_err = err;
1311 1296  }
1312 1297  
1313 1298  /* ARGSUSED */
1314 1299  static void
1315 1300  dlmgmt_zoneboot(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1316 1301      ucred_t *cred)
1317 1302  {
1318 1303          int                     err;
1319 1304          dlmgmt_door_zoneboot_t  *zoneboot = argp;
1320 1305          dlmgmt_zoneboot_retval_t *retvalp = retp;
1321 1306  
1322 1307          dlmgmt_table_lock(B_TRUE);
1323 1308  
1324 1309          if ((err = dlmgmt_checkprivs(0, cred)) != 0)
1325 1310                  goto done;
1326 1311  
1327 1312          if (zoneid != GLOBAL_ZONEID) {
1328 1313                  err = EACCES;
1329 1314                  goto done;
1330 1315          }
1331 1316          if (zoneboot->ld_zoneid == GLOBAL_ZONEID) {
1332 1317                  err = EINVAL;
1333 1318                  goto done;
1334 1319          }
1335 1320  
1336 1321          if ((err = dlmgmt_elevate_privileges()) == 0) {
1337 1322                  err = dlmgmt_zone_init(zoneboot->ld_zoneid);
1338 1323                  (void) dlmgmt_drop_privileges();
1339 1324          }
1340 1325  done:
1341 1326          dlmgmt_table_unlock();
1342 1327          retvalp->lr_err = err;
1343 1328  }
1344 1329  
1345 1330  /* ARGSUSED */
1346 1331  static void
1347 1332  dlmgmt_zonehalt(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1348 1333      ucred_t *cred)
1349 1334  {
1350 1335          int                     err = 0;
1351 1336          dlmgmt_door_zonehalt_t  *zonehalt = argp;
1352 1337          dlmgmt_zonehalt_retval_t *retvalp = retp;
1353 1338          static char my_pid[10];
1354 1339  
1355 1340          if (my_pid[0] == NULL)
1356 1341                  (void) snprintf(my_pid, sizeof (my_pid), "%d\n", getpid());
1357 1342  
1358 1343          if ((err = dlmgmt_checkprivs(0, cred)) == 0) {
1359 1344                  if (zoneid != GLOBAL_ZONEID) {
1360 1345                          err = EACCES;
1361 1346                  } else if (zonehalt->ld_zoneid == GLOBAL_ZONEID) {
1362 1347                          err = EINVAL;
1363 1348                  } else {
1364 1349                          /*
1365 1350                           * dls and mac don't honor the locking rules defined in
1366 1351                           * mac. In order to try and make that case less likely
1367 1352                           * to happen, we try to serialize some of the zone
1368 1353                           * activity here between dlmgmtd and the brands on
1369 1354                           * /etc/dladm/zone.lck
1370 1355                           */
1371 1356                          int fd;
1372 1357  
1373 1358                          while ((fd = open(ZONE_LOCK, O_WRONLY |
1374 1359                              O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) < 0)
1375 1360                          (void) sleep(1);
1376 1361                          (void) write(fd, my_pid, sizeof (my_pid));
1377 1362                          (void) close(fd);
1378 1363  
1379 1364                          dlmgmt_table_lock(B_TRUE);
1380 1365                          dlmgmt_db_fini(zonehalt->ld_zoneid);
1381 1366                          dlmgmt_table_unlock();
1382 1367  
1383 1368                          (void) unlink(ZONE_LOCK);
1384 1369                  }
1385 1370          }
1386 1371          retvalp->lr_err = err;
1387 1372  }
1388 1373  
1389 1374  static dlmgmt_door_info_t i_dlmgmt_door_info_tbl[] = {
1390 1375          { DLMGMT_CMD_DLS_CREATE, sizeof (dlmgmt_upcall_arg_create_t),
1391 1376              sizeof (dlmgmt_create_retval_t), dlmgmt_upcall_create },
1392 1377          { DLMGMT_CMD_DLS_GETATTR, sizeof (dlmgmt_upcall_arg_getattr_t),
1393 1378              sizeof (dlmgmt_getattr_retval_t), dlmgmt_upcall_getattr },
1394 1379          { DLMGMT_CMD_DLS_DESTROY, sizeof (dlmgmt_upcall_arg_destroy_t),
1395 1380              sizeof (dlmgmt_destroy_retval_t), dlmgmt_upcall_destroy },
1396 1381          { DLMGMT_CMD_GETNAME, sizeof (dlmgmt_door_getname_t),
1397 1382              sizeof (dlmgmt_getname_retval_t), dlmgmt_getname },
1398 1383          { DLMGMT_CMD_GETLINKID, sizeof (dlmgmt_door_getlinkid_t),
1399 1384              sizeof (dlmgmt_getlinkid_retval_t), dlmgmt_getlinkid },
1400 1385          { DLMGMT_CMD_GETNEXT, sizeof (dlmgmt_door_getnext_t),
1401 1386              sizeof (dlmgmt_getnext_retval_t), dlmgmt_getnext },
1402 1387          { DLMGMT_CMD_DLS_UPDATE, sizeof (dlmgmt_upcall_arg_update_t),
1403 1388              sizeof (dlmgmt_update_retval_t), dlmgmt_upcall_update },
1404 1389          { DLMGMT_CMD_CREATE_LINKID, sizeof (dlmgmt_door_createid_t),
1405 1390              sizeof (dlmgmt_createid_retval_t), dlmgmt_createid },
1406 1391          { DLMGMT_CMD_DESTROY_LINKID, sizeof (dlmgmt_door_destroyid_t),
1407 1392              sizeof (dlmgmt_destroyid_retval_t), dlmgmt_destroyid },
1408 1393          { DLMGMT_CMD_REMAP_LINKID, sizeof (dlmgmt_door_remapid_t),
1409 1394              sizeof (dlmgmt_remapid_retval_t), dlmgmt_remapid },
1410 1395          { DLMGMT_CMD_CREATECONF, sizeof (dlmgmt_door_createconf_t),
1411 1396              sizeof (dlmgmt_createconf_retval_t), dlmgmt_createconf },
1412 1397          { DLMGMT_CMD_OPENCONF, sizeof (dlmgmt_door_openconf_t),
1413 1398              sizeof (dlmgmt_openconf_retval_t), dlmgmt_openconf },
1414 1399          { DLMGMT_CMD_WRITECONF, sizeof (dlmgmt_door_writeconf_t),
1415 1400              sizeof (dlmgmt_writeconf_retval_t), dlmgmt_writeconf },
1416 1401          { DLMGMT_CMD_UP_LINKID, sizeof (dlmgmt_door_upid_t),
1417 1402              sizeof (dlmgmt_upid_retval_t), dlmgmt_upid },
1418 1403          { DLMGMT_CMD_SETATTR, sizeof (dlmgmt_door_setattr_t),
1419 1404              sizeof (dlmgmt_setattr_retval_t), dlmgmt_setattr },
1420 1405          { DLMGMT_CMD_UNSETATTR, sizeof (dlmgmt_door_unsetattr_t),
1421 1406              sizeof (dlmgmt_unsetattr_retval_t), dlmgmt_unsetconfattr },
1422 1407          { DLMGMT_CMD_REMOVECONF, sizeof (dlmgmt_door_removeconf_t),
1423 1408              sizeof (dlmgmt_removeconf_retval_t), dlmgmt_removeconf },
1424 1409          { DLMGMT_CMD_DESTROYCONF, sizeof (dlmgmt_door_destroyconf_t),
1425 1410              sizeof (dlmgmt_destroyconf_retval_t), dlmgmt_destroyconf },
1426 1411          { DLMGMT_CMD_GETATTR, sizeof (dlmgmt_door_getattr_t),
1427 1412              sizeof (dlmgmt_getattr_retval_t), dlmgmt_getattr },
1428 1413          { DLMGMT_CMD_GETCONFSNAPSHOT, sizeof (dlmgmt_door_getconfsnapshot_t),
1429 1414              sizeof (dlmgmt_getconfsnapshot_retval_t), dlmgmt_getconfsnapshot },
1430 1415          { DLMGMT_CMD_LINKPROP_INIT, sizeof (dlmgmt_door_linkprop_init_t),
1431 1416              sizeof (dlmgmt_linkprop_init_retval_t),
1432 1417              dlmgmt_upcall_linkprop_init },
1433 1418          { DLMGMT_CMD_SETZONEID, sizeof (dlmgmt_door_setzoneid_t),
1434 1419              sizeof (dlmgmt_setzoneid_retval_t), dlmgmt_setzoneid },
1435 1420          { DLMGMT_CMD_ZONEBOOT, sizeof (dlmgmt_door_zoneboot_t),
1436 1421              sizeof (dlmgmt_zoneboot_retval_t), dlmgmt_zoneboot },
1437 1422          { DLMGMT_CMD_ZONEHALT, sizeof (dlmgmt_door_zonehalt_t),
1438 1423              sizeof (dlmgmt_zonehalt_retval_t), dlmgmt_zonehalt },
1439 1424          { 0, 0, 0, NULL }
1440 1425  };
1441 1426  
1442 1427  static dlmgmt_door_info_t *
1443 1428  dlmgmt_getcmdinfo(int cmd)
1444 1429  {
1445 1430          dlmgmt_door_info_t      *infop = i_dlmgmt_door_info_tbl;
1446 1431  
1447 1432          while (infop->di_handler != NULL) {
1448 1433                  if (infop->di_cmd == cmd)
1449 1434                          break;
1450 1435                  infop++;
1451 1436          }
1452 1437          return (infop);
1453 1438  }
1454 1439  
1455 1440  /* ARGSUSED */
1456 1441  void
1457 1442  dlmgmt_handler(void *cookie, char *argp, size_t argsz, door_desc_t *dp,
1458 1443      uint_t n_desc)
1459 1444  {
1460 1445          dlmgmt_door_arg_t       *door_arg = (dlmgmt_door_arg_t *)(void *)argp;
1461 1446          dlmgmt_door_info_t      *infop = NULL;
1462 1447          dlmgmt_retval_t         retval;
1463 1448          ucred_t                 *cred = NULL;
1464 1449          zoneid_t                zoneid;
1465 1450          void                    *retvalp = NULL;
1466 1451          size_t                  sz, acksz;
1467 1452          int                     err = 0;
1468 1453  
1469 1454          infop = dlmgmt_getcmdinfo(door_arg->ld_cmd);
1470 1455          if (infop == NULL || argsz != infop->di_reqsz) {
1471 1456                  err = EINVAL;
1472 1457                  goto done;
1473 1458          }
1474 1459  
1475 1460          if (door_ucred(&cred) != 0 || (zoneid = ucred_getzoneid(cred)) == -1) {
1476 1461                  err = errno;
1477 1462                  goto done;
1478 1463          }
1479 1464  
1480 1465          /*
1481 1466           * Note that malloc() cannot be used here because door_return
1482 1467           * never returns, and memory allocated by malloc() would get leaked.
1483 1468           * Use alloca() instead.
1484 1469           */
1485 1470          acksz = infop->di_acksz;
1486 1471  
1487 1472  again:
1488 1473          retvalp = alloca(acksz);
1489 1474          sz = acksz;
1490 1475          infop->di_handler(argp, retvalp, &acksz, zoneid, cred);
1491 1476          if (acksz > sz) {
1492 1477                  /*
1493 1478                   * If the specified buffer size is not big enough to hold the
1494 1479                   * return value, reallocate the buffer and try to get the
1495 1480                   * result one more time.
1496 1481                   */
1497 1482                  assert(((dlmgmt_retval_t *)retvalp)->lr_err == ENOSPC);
1498 1483                  goto again;
1499 1484          }
1500 1485  
1501 1486  done:
1502 1487          if (cred != NULL)
1503 1488                  ucred_free(cred);
1504 1489          if (err == 0) {
1505 1490                  (void) door_return(retvalp, acksz, NULL, 0);
1506 1491          } else {
1507 1492                  retval.lr_err = err;
1508 1493                  (void) door_return((char *)&retval, sizeof (retval), NULL, 0);
1509 1494          }
1510 1495  }
  
    | 
      ↓ open down ↓ | 
    200 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX