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  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2016 Joyent, Inc.
  24  */
  25 
  26 #include <door.h>
  27 #include <errno.h>
  28 #include <assert.h>
  29 #include <stdio.h>
  30 #include <stdlib.h>
  31 #include <unistd.h>
  32 #include <string.h>
  33 #include <strings.h>
  34 #include <zone.h>
  35 #include <sys/types.h>
  36 #include <sys/stat.h>
  37 #include <sys/aggr.h>
  38 #include <sys/mman.h>
  39 #include <fcntl.h>
  40 #include <libdladm.h>
  41 #include <libdladm_impl.h>
  42 #include <libdllink.h>
  43 #include <libdlmgmt.h>
 
 
 106 /*
 107  * Allocate a new linkid with the given name. Return the new linkid.
 108  */
 109 dladm_status_t
 110 dladm_create_datalink_id(dladm_handle_t handle, const char *link,
 111     datalink_class_t class, uint32_t media, uint32_t flags,
 112     datalink_id_t *linkidp)
 113 {
 114         dlmgmt_door_createid_t  createid;
 115         dlmgmt_createid_retval_t retval;
 116         uint32_t                dlmgmt_flags;
 117         dladm_status_t          status;
 118         size_t                  sz = sizeof (retval);
 119 
 120         if (link == NULL || class == DATALINK_CLASS_ALL ||
 121             !(flags & (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST)) ||
 122             linkidp == NULL) {
 123                 return (DLADM_STATUS_BADARG);
 124         }
 125 
 126         dlmgmt_flags = (flags & DLADM_OPT_ACTIVE) ? DLMGMT_ACTIVE : 0;
 127         dlmgmt_flags |= (flags & DLADM_OPT_PERSIST) ? DLMGMT_PERSIST : 0;
 128         dlmgmt_flags |= (flags & DLADM_OPT_TRANSIENT) ? DLMGMT_TRANSIENT : 0;
 129 
 130         (void) strlcpy(createid.ld_link, link, MAXLINKNAMELEN);
 131         createid.ld_class = class;
 132         createid.ld_media = media;
 133         createid.ld_flags = dlmgmt_flags;
 134         createid.ld_cmd = DLMGMT_CMD_CREATE_LINKID;
 135         createid.ld_prefix = (flags & DLADM_OPT_PREFIX);
 136 
 137         if ((status = dladm_door_call(handle, &createid, sizeof (createid),
 138             &retval, &sz)) == DLADM_STATUS_OK) {
 139                 *linkidp = retval.lr_linkid;
 140         }
 141         return (status);
 142 }
 143 
 144 /*
 145  * Destroy the given link ID.
 
 564         if ((status = dladm_door_call(handle, &getlinkid, sizeof (getlinkid),
 565             &retval, &sz)) != DLADM_STATUS_OK) {
 566                 return (status);
 567         }
 568 
 569         linkid = retval.lr_linkid;
 570         if (retval.lr_class == DATALINK_CLASS_PHYS &&
 571             retval.lr_flags & DLMGMT_ACTIVE) {
 572                 /*
 573                  * An active physical link reported by the dlmgmtd daemon
 574                  * might not be active anymore. Check and set its real status.
 575                  */
 576                 status = i_dladm_phys_status(handle, linkid, &retval.lr_flags);
 577                 if (status != DLADM_STATUS_OK)
 578                         return (status);
 579         }
 580 
 581         if (linkidp != NULL)
 582                 *linkidp = linkid;
 583         if (flagp != NULL) {
 584                 *flagp = retval.lr_flags & DLMGMT_ACTIVE ? DLADM_OPT_ACTIVE : 0;
 585                 *flagp |= (retval.lr_flags & DLMGMT_PERSIST) ?
 586                     DLADM_OPT_PERSIST : 0;
 587         }
 588         if (classp != NULL)
 589                 *classp = retval.lr_class;
 590         if (mediap != NULL)
 591                 *mediap = retval.lr_media;
 592 
 593         return (DLADM_STATUS_OK);
 594 }
 595 
 596 /*
 597  * Get the link name that is associated with the given id.
 598  */
 599 dladm_status_t
 600 dladm_datalink_id2info(dladm_handle_t handle, datalink_id_t linkid,
 601     uint32_t *flagp, datalink_class_t *classp, uint32_t *mediap, char *link,
 602     size_t len)
 603 {
 604         dlmgmt_door_getname_t   getname;
 605         dlmgmt_getname_retval_t retval;
 606         dladm_status_t          status;
 
 | 
 
 
   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  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2016 Joyent, Inc.
  24  * Copyright 2023 Oxide Computer Company
  25  */
  26 
  27 #include <door.h>
  28 #include <errno.h>
  29 #include <assert.h>
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <unistd.h>
  33 #include <string.h>
  34 #include <strings.h>
  35 #include <zone.h>
  36 #include <sys/types.h>
  37 #include <sys/stat.h>
  38 #include <sys/aggr.h>
  39 #include <sys/mman.h>
  40 #include <fcntl.h>
  41 #include <libdladm.h>
  42 #include <libdladm_impl.h>
  43 #include <libdllink.h>
  44 #include <libdlmgmt.h>
 
 
 107 /*
 108  * Allocate a new linkid with the given name. Return the new linkid.
 109  */
 110 dladm_status_t
 111 dladm_create_datalink_id(dladm_handle_t handle, const char *link,
 112     datalink_class_t class, uint32_t media, uint32_t flags,
 113     datalink_id_t *linkidp)
 114 {
 115         dlmgmt_door_createid_t  createid;
 116         dlmgmt_createid_retval_t retval;
 117         uint32_t                dlmgmt_flags;
 118         dladm_status_t          status;
 119         size_t                  sz = sizeof (retval);
 120 
 121         if (link == NULL || class == DATALINK_CLASS_ALL ||
 122             !(flags & (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST)) ||
 123             linkidp == NULL) {
 124                 return (DLADM_STATUS_BADARG);
 125         }
 126 
 127         if (getzoneid() != GLOBAL_ZONEID) {
 128                 /*
 129                  * If we're creating this link in a non-global zone, then do
 130                  * not allow it to be persistent, and flag it as transient so
 131                  * that it will be automatically cleaned up on zone shutdown,
 132                  * rather than being moved to the GZ.
 133                  */
 134                 if (flags & DLADM_OPT_PERSIST)
 135                         return (DLADM_STATUS_TEMPONLY);
 136                 flags |= DLADM_OPT_TRANSIENT;
 137         }
 138 
 139         dlmgmt_flags = (flags & DLADM_OPT_ACTIVE) ? DLMGMT_ACTIVE : 0;
 140         dlmgmt_flags |= (flags & DLADM_OPT_PERSIST) ? DLMGMT_PERSIST : 0;
 141         dlmgmt_flags |= (flags & DLADM_OPT_TRANSIENT) ? DLMGMT_TRANSIENT : 0;
 142 
 143         (void) strlcpy(createid.ld_link, link, MAXLINKNAMELEN);
 144         createid.ld_class = class;
 145         createid.ld_media = media;
 146         createid.ld_flags = dlmgmt_flags;
 147         createid.ld_cmd = DLMGMT_CMD_CREATE_LINKID;
 148         createid.ld_prefix = (flags & DLADM_OPT_PREFIX);
 149 
 150         if ((status = dladm_door_call(handle, &createid, sizeof (createid),
 151             &retval, &sz)) == DLADM_STATUS_OK) {
 152                 *linkidp = retval.lr_linkid;
 153         }
 154         return (status);
 155 }
 156 
 157 /*
 158  * Destroy the given link ID.
 
 577         if ((status = dladm_door_call(handle, &getlinkid, sizeof (getlinkid),
 578             &retval, &sz)) != DLADM_STATUS_OK) {
 579                 return (status);
 580         }
 581 
 582         linkid = retval.lr_linkid;
 583         if (retval.lr_class == DATALINK_CLASS_PHYS &&
 584             retval.lr_flags & DLMGMT_ACTIVE) {
 585                 /*
 586                  * An active physical link reported by the dlmgmtd daemon
 587                  * might not be active anymore. Check and set its real status.
 588                  */
 589                 status = i_dladm_phys_status(handle, linkid, &retval.lr_flags);
 590                 if (status != DLADM_STATUS_OK)
 591                         return (status);
 592         }
 593 
 594         if (linkidp != NULL)
 595                 *linkidp = linkid;
 596         if (flagp != NULL) {
 597                 *flagp = (retval.lr_flags & DLMGMT_ACTIVE) ?
 598                     DLADM_OPT_ACTIVE : 0;
 599                 *flagp |= (retval.lr_flags & DLMGMT_PERSIST) ?
 600                     DLADM_OPT_PERSIST : 0;
 601                 *flagp |= (retval.lr_flags & DLMGMT_TRANSIENT) ?
 602                     DLADM_OPT_TRANSIENT : 0;
 603         }
 604         if (classp != NULL)
 605                 *classp = retval.lr_class;
 606         if (mediap != NULL)
 607                 *mediap = retval.lr_media;
 608 
 609         return (DLADM_STATUS_OK);
 610 }
 611 
 612 /*
 613  * Get the link name that is associated with the given id.
 614  */
 615 dladm_status_t
 616 dladm_datalink_id2info(dladm_handle_t handle, datalink_id_t linkid,
 617     uint32_t *flagp, datalink_class_t *classp, uint32_t *mediap, char *link,
 618     size_t len)
 619 {
 620         dlmgmt_door_getname_t   getname;
 621         dlmgmt_getname_retval_t retval;
 622         dladm_status_t          status;
 
 |