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) 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2011, Joyent Inc. All rights reserved.
  25  */
  26 
  27 #include <stdio.h>
  28 #include <door.h>
  29 #include <errno.h>
  30 #include <strings.h>
  31 #include <sys/mman.h>
  32 #include <libdladm.h>
  33 #include <libdlib.h>
  34 #include <libdllink.h>
  35 
  36 extern dladm_status_t   dladm_door_fd(dladm_handle_t, int *);
  37 
  38 static dladm_status_t
  39 ibd_dladm_door_call(dladm_handle_t handle, void *arg, size_t asize, void *rbuf,
  40     size_t rsize)
  41 {
  42         door_arg_t      darg;
  43         int             door_fd;
  44         dladm_status_t  status = DLADM_STATUS_OK;
  45 
  46         darg.data_ptr   = arg;
  47         darg.data_size  = asize;
  48         darg.desc_ptr   = NULL;
  49         darg.desc_num   = 0;
  50         darg.rbuf       = rbuf;
  51         darg.rsize      = rsize;
  52 
  53         /* The door descriptor is opened if it isn't already */
  54         if ((status = dladm_door_fd(handle, &door_fd)) != DLADM_STATUS_OK)
  55                 return (status);
  56 
  57         if (door_call(door_fd, &darg) == -1)
  58                 return (DLADM_STATUS_FAILED);
  59 
  60         if (darg.rbuf != rbuf) {
  61                 /*
  62                  * The size of the input rbuf is not big enough so that
  63                  * the door allocate the rbuf itself. In this case, simply
  64                  * think something wrong with the door call.
  65                  */
  66                 (void) munmap(darg.rbuf, darg.rsize);
  67                 return (DLADM_STATUS_TOOSMALL);
  68         }
  69 
  70         if (darg.rsize != rsize)
  71                 return (DLADM_STATUS_FAILED);
  72 
  73         if ((((dlmgmt_retval_t *)rbuf)->lr_err) == 0)
  74                 return (DLADM_STATUS_OK);
  75         else
  76                 return (DLADM_STATUS_FAILED);
  77 }
  78 
  79 static int
  80 ibd_delete_link(dladm_handle_t dlh, char *link)
  81 {
  82         dlmgmt_door_getlinkid_t         getlinkid;
  83         dlmgmt_getlinkid_retval_t       retval;
  84         datalink_id_t                   linkid;
  85         dladm_status_t                  status;
  86         char                            errmsg[DLADM_STRSIZE];
  87 
  88         getlinkid.ld_cmd = DLMGMT_CMD_GETLINKID;
  89         (void) strlcpy(getlinkid.ld_link, link, MAXLINKNAMELEN);
  90         getlinkid.ld_zoneid = -1;
  91 
  92         if ((status = ibd_dladm_door_call(dlh, &getlinkid, sizeof (getlinkid),
  93             &retval, sizeof (retval))) != DLADM_STATUS_OK) {
  94                 (void) fprintf(stderr,
  95                     "dladm_door_call failed: %s; linkname = %s\n",
  96                     dladm_status2str(status, errmsg), link);
  97                 return (status);
  98         }
  99 
 100         if (retval.lr_class != DATALINK_CLASS_PHYS) {
 101                 (void) fprintf(stderr,
 102                     "Not a physical link: linkname = %s, class = 0x%x\n",
 103                     link, (uint_t)retval.lr_class);
 104                 return (status);
 105         }
 106 
 107         linkid = retval.lr_linkid;
 108 
 109         if ((status = dladm_remove_conf(dlh, linkid)) != DLADM_STATUS_OK) {
 110                 (void) fprintf(stderr, "dladm_remove_conf failed: %s\n",
 111                     dladm_status2str(status, errmsg));
 112                 return (status);
 113         }
 114 
 115         if ((status = dladm_destroy_datalink_id(dlh, linkid,
 116             DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST)) != DLADM_STATUS_OK) {
 117                 (void) fprintf(stderr, "dladm_destroy_datalink_id failed: %s\n",
 118                     dladm_status2str(status, errmsg));
 119         }
 120 
 121         return (status);
 122 }
 123 
 124 int
 125 main(int argc, char *argv[])
 126 {
 127         dladm_handle_t  dlh;
 128         int             i;
 129         dladm_status_t  status;
 130         char            errmsg[DLADM_STRSIZE];
 131 
 132         if (argc < 2) {
 133                 (void) fprintf(stderr,
 134                     "Usage: ibd_delete_link linkname ...\n");
 135                 return (2);
 136         }
 137 
 138         if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK) {
 139                 (void) fprintf(stderr, "Failed to open dladm handle: %s\n",
 140                     dladm_status2str(status, errmsg));
 141                 return (1);
 142         }
 143 
 144         for (i = 1; i < argc; i++) {
 145                 if (ibd_delete_link(dlh, argv[i]) != DLADM_STATUS_OK) {
 146                         dladm_close(dlh);
 147                         return (1);
 148                 }
 149         }
 150 
 151         dladm_close(dlh);
 152         return (0);
 153 }