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