17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ba2e4443Sseb * Common Development and Distribution License (the "License"). 6ba2e4443Sseb * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*1cfa752fSRamaswamy Tummala * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate #include <unistd.h> 267c478bd9Sstevel@tonic-gate #include <errno.h> 27d62bc4baSyz147064 #include <ctype.h> 280ba2cbe9Sxc151355 #include <fcntl.h> 290ba2cbe9Sxc151355 #include <strings.h> 300ba2cbe9Sxc151355 #include <dirent.h> 31da14cebeSEric Cheng #include <stdlib.h> 3225ec3e3dSEric Cheng #include <netinet/in.h> 3325ec3e3dSEric Cheng #include <arpa/inet.h> 34b9e076dcSyz147064 #include <sys/param.h> 350ba2cbe9Sxc151355 #include <sys/stat.h> 364ac67f02SAnurag S. Maskey #include <sys/dld.h> 3782a2fc47SJames Carlson #include <sys/dld_ioc.h> 380ba2cbe9Sxc151355 #include <libdladm_impl.h> 3933343a97Smeem #include <libintl.h> 40d62bc4baSyz147064 #include <libdlpi.h> 41aae21359Skrgopi 420ba2cbe9Sxc151355 static char dladm_rootdir[MAXPATHLEN] = "/"; 430ba2cbe9Sxc151355 44b509e89bSRishi Srivatsavai typedef struct media_type_desc { 45b509e89bSRishi Srivatsavai uint32_t media_type; 46b509e89bSRishi Srivatsavai #define MAX_MEDIA_TYPE_STRING 32 47b509e89bSRishi Srivatsavai const char media_type_str[MAX_MEDIA_TYPE_STRING]; 48b509e89bSRishi Srivatsavai } media_type_t; 49b509e89bSRishi Srivatsavai 50b509e89bSRishi Srivatsavai static media_type_t media_type_table[] = { 51b509e89bSRishi Srivatsavai { DL_ETHER, "Ethernet" }, 52b509e89bSRishi Srivatsavai { DL_WIFI, "WiFi" }, 53b509e89bSRishi Srivatsavai { DL_IB, "Infiniband" }, 54b509e89bSRishi Srivatsavai { DL_IPV4, "IPv4Tunnel" }, 55b509e89bSRishi Srivatsavai { DL_IPV6, "IPv6Tunnel" }, 562b24ab6bSSebastien Roy { DL_6TO4, "6to4Tunnel" }, 57b509e89bSRishi Srivatsavai { DL_CSMACD, "CSMA/CD" }, 58b509e89bSRishi Srivatsavai { DL_TPB, "TokenBus" }, 59b509e89bSRishi Srivatsavai { DL_TPR, "TokenRing" }, 60b509e89bSRishi Srivatsavai { DL_METRO, "MetroNet" }, 61b509e89bSRishi Srivatsavai { DL_HDLC, "HDLC" }, 62b509e89bSRishi Srivatsavai { DL_CHAR, "SyncCharacter" }, 63b509e89bSRishi Srivatsavai { DL_CTCA, "CTCA" }, 64b509e89bSRishi Srivatsavai { DL_FDDI, "FDDI" }, 65b509e89bSRishi Srivatsavai { DL_FC, "FiberChannel" }, 66b509e89bSRishi Srivatsavai { DL_ATM, "ATM" }, 67b509e89bSRishi Srivatsavai { DL_IPATM, "ATM(ClassicIP)" }, 68b509e89bSRishi Srivatsavai { DL_X25, "X.25" }, 69b509e89bSRishi Srivatsavai { DL_IPX25, "X.25(ClassicIP)" }, 70b509e89bSRishi Srivatsavai { DL_ISDN, "ISDN" }, 71b509e89bSRishi Srivatsavai { DL_HIPPI, "HIPPI" }, 72b509e89bSRishi Srivatsavai { DL_100VG, "100BaseVGEthernet" }, 73b509e89bSRishi Srivatsavai { DL_100VGTPR, "100BaseVGTokenRing" }, 74b509e89bSRishi Srivatsavai { DL_ETH_CSMA, "IEEE802.3" }, 75b509e89bSRishi Srivatsavai { DL_100BT, "100BaseT" }, 76b509e89bSRishi Srivatsavai { DL_FRAME, "FrameRelay" }, 77b509e89bSRishi Srivatsavai { DL_MPFRAME, "MPFrameRelay" }, 78b509e89bSRishi Srivatsavai { DL_ASYNC, "AsyncCharacter" }, 79b509e89bSRishi Srivatsavai { DL_IPNET, "IPNET" }, 80b509e89bSRishi Srivatsavai { DL_OTHER, "Other" } 81b509e89bSRishi Srivatsavai }; 82b509e89bSRishi Srivatsavai #define MEDIATYPECOUNT (sizeof (media_type_table) / sizeof (media_type_t)) 83b509e89bSRishi Srivatsavai 8425ec3e3dSEric Cheng typedef struct { 8525ec3e3dSEric Cheng uint32_t lp_type; 8625ec3e3dSEric Cheng char *lp_name; 8725ec3e3dSEric Cheng } link_protect_t; 8825ec3e3dSEric Cheng 8925ec3e3dSEric Cheng static link_protect_t link_protect_types[] = { 9025ec3e3dSEric Cheng { MPT_MACNOSPOOF, "mac-nospoof" }, 910dc2366fSVenugopal Iyer { MPT_RESTRICTED, "restricted" }, 9225ec3e3dSEric Cheng { MPT_IPNOSPOOF, "ip-nospoof" }, 930dc2366fSVenugopal Iyer { MPT_DHCPNOSPOOF, "dhcp-nospoof" } 9425ec3e3dSEric Cheng }; 9525ec3e3dSEric Cheng #define LPTYPES (sizeof (link_protect_types) / sizeof (link_protect_t)) 9625ec3e3dSEric Cheng 974ac67f02SAnurag S. Maskey dladm_status_t 984ac67f02SAnurag S. Maskey dladm_open(dladm_handle_t *handle) 994ac67f02SAnurag S. Maskey { 1004ac67f02SAnurag S. Maskey int dld_fd; 1014ac67f02SAnurag S. Maskey 1024ac67f02SAnurag S. Maskey if (handle == NULL) 1034ac67f02SAnurag S. Maskey return (DLADM_STATUS_BADARG); 1044ac67f02SAnurag S. Maskey 1054ac67f02SAnurag S. Maskey if ((dld_fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) 1064ac67f02SAnurag S. Maskey return (dladm_errno2status(errno)); 1074ac67f02SAnurag S. Maskey 1084ac67f02SAnurag S. Maskey /* 1094ac67f02SAnurag S. Maskey * Don't open DLMGMT_DOOR now. dlmgmtd(1M) is not able to 1104ac67f02SAnurag S. Maskey * open the door when the dladm handle is opened because the 1114ac67f02SAnurag S. Maskey * door hasn't been created yet at that time. Thus, we must 1124ac67f02SAnurag S. Maskey * open it on-demand in dladm_door_fd(). Move the open() 1134ac67f02SAnurag S. Maskey * to dladm_door_fd() for all cases. 1144ac67f02SAnurag S. Maskey */ 1154ac67f02SAnurag S. Maskey 1164ac67f02SAnurag S. Maskey if ((*handle = malloc(sizeof (struct dladm_handle))) == NULL) { 1174ac67f02SAnurag S. Maskey (void) close(dld_fd); 1184ac67f02SAnurag S. Maskey return (DLADM_STATUS_NOMEM); 1194ac67f02SAnurag S. Maskey } 1204ac67f02SAnurag S. Maskey 1214ac67f02SAnurag S. Maskey (*handle)->dld_fd = dld_fd; 1224ac67f02SAnurag S. Maskey (*handle)->door_fd = -1; 1234ac67f02SAnurag S. Maskey 1244ac67f02SAnurag S. Maskey return (DLADM_STATUS_OK); 1254ac67f02SAnurag S. Maskey } 1264ac67f02SAnurag S. Maskey 1274ac67f02SAnurag S. Maskey void 1284ac67f02SAnurag S. Maskey dladm_close(dladm_handle_t handle) 1294ac67f02SAnurag S. Maskey { 1304ac67f02SAnurag S. Maskey if (handle != NULL) { 1314ac67f02SAnurag S. Maskey (void) close(handle->dld_fd); 1324ac67f02SAnurag S. Maskey if (handle->door_fd != -1) 1334ac67f02SAnurag S. Maskey (void) close(handle->door_fd); 1344ac67f02SAnurag S. Maskey free(handle); 1354ac67f02SAnurag S. Maskey } 1364ac67f02SAnurag S. Maskey } 1374ac67f02SAnurag S. Maskey 1384ac67f02SAnurag S. Maskey int 1394ac67f02SAnurag S. Maskey dladm_dld_fd(dladm_handle_t handle) 1404ac67f02SAnurag S. Maskey { 1414ac67f02SAnurag S. Maskey return (handle->dld_fd); 1424ac67f02SAnurag S. Maskey } 1434ac67f02SAnurag S. Maskey 1444ac67f02SAnurag S. Maskey /* 1454ac67f02SAnurag S. Maskey * If DLMGMT_DOOR hasn't been opened in the handle yet, open it. 1464ac67f02SAnurag S. Maskey */ 1474ac67f02SAnurag S. Maskey dladm_status_t 1484ac67f02SAnurag S. Maskey dladm_door_fd(dladm_handle_t handle, int *door_fd) 1494ac67f02SAnurag S. Maskey { 1504ac67f02SAnurag S. Maskey int fd; 1514ac67f02SAnurag S. Maskey 1524ac67f02SAnurag S. Maskey if (handle->door_fd == -1) { 1534ac67f02SAnurag S. Maskey if ((fd = open(DLMGMT_DOOR, O_RDONLY)) < 0) 1544ac67f02SAnurag S. Maskey return (dladm_errno2status(errno)); 1554ac67f02SAnurag S. Maskey handle->door_fd = fd; 1564ac67f02SAnurag S. Maskey } 1574ac67f02SAnurag S. Maskey *door_fd = handle->door_fd; 1584ac67f02SAnurag S. Maskey 1594ac67f02SAnurag S. Maskey return (DLADM_STATUS_OK); 1604ac67f02SAnurag S. Maskey } 1614ac67f02SAnurag S. Maskey 1620ba2cbe9Sxc151355 const char * 1630ba2cbe9Sxc151355 dladm_status2str(dladm_status_t status, char *buf) 1640ba2cbe9Sxc151355 { 1650ba2cbe9Sxc151355 const char *s; 1660ba2cbe9Sxc151355 1670ba2cbe9Sxc151355 switch (status) { 1680ba2cbe9Sxc151355 case DLADM_STATUS_OK: 1690ba2cbe9Sxc151355 s = "ok"; 1700ba2cbe9Sxc151355 break; 1710ba2cbe9Sxc151355 case DLADM_STATUS_BADARG: 1720ba2cbe9Sxc151355 s = "invalid argument"; 1730ba2cbe9Sxc151355 break; 1740ba2cbe9Sxc151355 case DLADM_STATUS_FAILED: 1750ba2cbe9Sxc151355 s = "operation failed"; 1760ba2cbe9Sxc151355 break; 1770ba2cbe9Sxc151355 case DLADM_STATUS_TOOSMALL: 1780ba2cbe9Sxc151355 s = "buffer size too small"; 1790ba2cbe9Sxc151355 break; 1800ba2cbe9Sxc151355 case DLADM_STATUS_NOTSUP: 1810ba2cbe9Sxc151355 s = "operation not supported"; 1820ba2cbe9Sxc151355 break; 1830ba2cbe9Sxc151355 case DLADM_STATUS_NOTFOUND: 1840ba2cbe9Sxc151355 s = "object not found"; 1850ba2cbe9Sxc151355 break; 1860ba2cbe9Sxc151355 case DLADM_STATUS_BADVAL: 1870ba2cbe9Sxc151355 s = "invalid value"; 1880ba2cbe9Sxc151355 break; 1890ba2cbe9Sxc151355 case DLADM_STATUS_NOMEM: 1900ba2cbe9Sxc151355 s = "insufficient memory"; 1910ba2cbe9Sxc151355 break; 1920ba2cbe9Sxc151355 case DLADM_STATUS_EXIST: 1930ba2cbe9Sxc151355 s = "object already exists"; 1940ba2cbe9Sxc151355 break; 1950ba2cbe9Sxc151355 case DLADM_STATUS_LINKINVAL: 1960ba2cbe9Sxc151355 s = "invalid link"; 1970ba2cbe9Sxc151355 break; 1980ba2cbe9Sxc151355 case DLADM_STATUS_PROPRDONLY: 1990ba2cbe9Sxc151355 s = "read-only property"; 2000ba2cbe9Sxc151355 break; 2010ba2cbe9Sxc151355 case DLADM_STATUS_BADVALCNT: 2020ba2cbe9Sxc151355 s = "invalid number of values"; 2030ba2cbe9Sxc151355 break; 2040ba2cbe9Sxc151355 case DLADM_STATUS_DBNOTFOUND: 2050ba2cbe9Sxc151355 s = "database not found"; 2060ba2cbe9Sxc151355 break; 2070ba2cbe9Sxc151355 case DLADM_STATUS_DENIED: 2080ba2cbe9Sxc151355 s = "permission denied"; 2090ba2cbe9Sxc151355 break; 2100ba2cbe9Sxc151355 case DLADM_STATUS_IOERR: 2110ba2cbe9Sxc151355 s = "I/O error"; 2120ba2cbe9Sxc151355 break; 213f4b3ec61Sdh155122 case DLADM_STATUS_TEMPONLY: 214da14cebeSEric Cheng s = "change cannot be persistent"; 215f4b3ec61Sdh155122 break; 216f595a68aSyz147064 case DLADM_STATUS_TIMEDOUT: 217f595a68aSyz147064 s = "operation timed out"; 218f595a68aSyz147064 break; 219f595a68aSyz147064 case DLADM_STATUS_ISCONN: 220f595a68aSyz147064 s = "already connected"; 221f595a68aSyz147064 break; 222f595a68aSyz147064 case DLADM_STATUS_NOTCONN: 223f595a68aSyz147064 s = "not connected"; 224f595a68aSyz147064 break; 225f595a68aSyz147064 case DLADM_STATUS_REPOSITORYINVAL: 226f595a68aSyz147064 s = "invalid configuration repository"; 227f595a68aSyz147064 break; 228f595a68aSyz147064 case DLADM_STATUS_MACADDRINVAL: 229f595a68aSyz147064 s = "invalid MAC address"; 230f595a68aSyz147064 break; 231f595a68aSyz147064 case DLADM_STATUS_KEYINVAL: 232f595a68aSyz147064 s = "invalid key"; 233f595a68aSyz147064 break; 234843e1988Sjohnlev case DLADM_STATUS_INVALIDMACADDRLEN: 235843e1988Sjohnlev s = "invalid MAC address length"; 236843e1988Sjohnlev break; 237843e1988Sjohnlev case DLADM_STATUS_INVALIDMACADDRTYPE: 238843e1988Sjohnlev s = "invalid MAC address type"; 239843e1988Sjohnlev break; 240d62bc4baSyz147064 case DLADM_STATUS_LINKBUSY: 241d62bc4baSyz147064 s = "link busy"; 242843e1988Sjohnlev break; 243d62bc4baSyz147064 case DLADM_STATUS_VIDINVAL: 244d62bc4baSyz147064 s = "invalid VLAN identifier"; 245843e1988Sjohnlev break; 246d62bc4baSyz147064 case DLADM_STATUS_TRYAGAIN: 247d62bc4baSyz147064 s = "try again later"; 248d62bc4baSyz147064 break; 249d62bc4baSyz147064 case DLADM_STATUS_NONOTIF: 250d62bc4baSyz147064 s = "link notification is not supported"; 251843e1988Sjohnlev break; 252da14cebeSEric Cheng case DLADM_STATUS_BADTIMEVAL: 253da14cebeSEric Cheng s = "invalid time range"; 254da14cebeSEric Cheng break; 255da14cebeSEric Cheng case DLADM_STATUS_INVALIDMACADDR: 256da14cebeSEric Cheng s = "invalid MAC address value"; 257da14cebeSEric Cheng break; 258da14cebeSEric Cheng case DLADM_STATUS_INVALIDMACADDRNIC: 259da14cebeSEric Cheng s = "MAC address reserved for use by underlying data-link"; 260da14cebeSEric Cheng break; 261da14cebeSEric Cheng case DLADM_STATUS_INVALIDMACADDRINUSE: 262da14cebeSEric Cheng s = "MAC address is already in use"; 263da14cebeSEric Cheng break; 264da14cebeSEric Cheng case DLADM_STATUS_MACFACTORYSLOTINVALID: 265da14cebeSEric Cheng s = "invalid factory MAC address slot"; 266da14cebeSEric Cheng break; 267da14cebeSEric Cheng case DLADM_STATUS_MACFACTORYSLOTUSED: 268da14cebeSEric Cheng s = "factory MAC address slot already used"; 269da14cebeSEric Cheng break; 270da14cebeSEric Cheng case DLADM_STATUS_MACFACTORYSLOTALLUSED: 271da14cebeSEric Cheng s = "all factory MAC address slots are in use"; 272da14cebeSEric Cheng break; 273da14cebeSEric Cheng case DLADM_STATUS_MACFACTORYNOTSUP: 274da14cebeSEric Cheng s = "factory MAC address slots not supported"; 275da14cebeSEric Cheng break; 276da14cebeSEric Cheng case DLADM_STATUS_INVALIDMACPREFIX: 277da14cebeSEric Cheng s = "Invalid MAC address prefix value"; 278da14cebeSEric Cheng break; 279da14cebeSEric Cheng case DLADM_STATUS_INVALIDMACPREFIXLEN: 280da14cebeSEric Cheng s = "Invalid MAC address prefix length"; 281da14cebeSEric Cheng break; 282da14cebeSEric Cheng case DLADM_STATUS_CPUMAX: 283da14cebeSEric Cheng s = "non-existent processor ID"; 284da14cebeSEric Cheng break; 285da14cebeSEric Cheng case DLADM_STATUS_CPUERR: 286da14cebeSEric Cheng s = "could not determine processor status"; 287da14cebeSEric Cheng break; 288da14cebeSEric Cheng case DLADM_STATUS_CPUNOTONLINE: 289da14cebeSEric Cheng s = "processor not online"; 290da14cebeSEric Cheng break; 291da14cebeSEric Cheng case DLADM_STATUS_DB_NOTFOUND: 292da14cebeSEric Cheng s = "database not found"; 293da14cebeSEric Cheng break; 294da14cebeSEric Cheng case DLADM_STATUS_DB_PARSE_ERR: 295da14cebeSEric Cheng s = "database parse error"; 296da14cebeSEric Cheng break; 297da14cebeSEric Cheng case DLADM_STATUS_PROP_PARSE_ERR: 298da14cebeSEric Cheng s = "property parse error"; 299da14cebeSEric Cheng break; 300da14cebeSEric Cheng case DLADM_STATUS_ATTR_PARSE_ERR: 301da14cebeSEric Cheng s = "attribute parse error"; 302da14cebeSEric Cheng break; 303da14cebeSEric Cheng case DLADM_STATUS_FLOW_DB_ERR: 304da14cebeSEric Cheng s = "flow database error"; 305da14cebeSEric Cheng break; 306da14cebeSEric Cheng case DLADM_STATUS_FLOW_DB_OPEN_ERR: 307da14cebeSEric Cheng s = "flow database open error"; 308da14cebeSEric Cheng break; 309da14cebeSEric Cheng case DLADM_STATUS_FLOW_DB_PARSE_ERR: 310da14cebeSEric Cheng s = "flow database parse error"; 311da14cebeSEric Cheng break; 312da14cebeSEric Cheng case DLADM_STATUS_FLOWPROP_DB_PARSE_ERR: 313da14cebeSEric Cheng s = "flow property database parse error"; 314da14cebeSEric Cheng break; 315da14cebeSEric Cheng case DLADM_STATUS_FLOW_ADD_ERR: 316da14cebeSEric Cheng s = "flow add error"; 317da14cebeSEric Cheng break; 318da14cebeSEric Cheng case DLADM_STATUS_FLOW_WALK_ERR: 319da14cebeSEric Cheng s = "flow walk error"; 320da14cebeSEric Cheng break; 321da14cebeSEric Cheng case DLADM_STATUS_FLOW_IDENTICAL: 322da14cebeSEric Cheng s = "a flow with identical attributes exists"; 323da14cebeSEric Cheng break; 324da14cebeSEric Cheng case DLADM_STATUS_FLOW_INCOMPATIBLE: 325da14cebeSEric Cheng s = "flow(s) with incompatible attributes exists"; 326da14cebeSEric Cheng break; 327da14cebeSEric Cheng case DLADM_STATUS_FLOW_EXISTS: 328da14cebeSEric Cheng s = "link still has flows"; 329da14cebeSEric Cheng break; 330da14cebeSEric Cheng case DLADM_STATUS_PERSIST_FLOW_EXISTS: 331da14cebeSEric Cheng s = "persistent flow with the same name exists"; 332da14cebeSEric Cheng break; 333da14cebeSEric Cheng case DLADM_STATUS_INVALID_IP: 334da14cebeSEric Cheng s = "invalid IP address"; 335da14cebeSEric Cheng break; 336da14cebeSEric Cheng case DLADM_STATUS_INVALID_PREFIXLEN: 337da14cebeSEric Cheng s = "invalid IP prefix length"; 338da14cebeSEric Cheng break; 339da14cebeSEric Cheng case DLADM_STATUS_INVALID_PROTOCOL: 340da14cebeSEric Cheng s = "invalid IP protocol"; 341da14cebeSEric Cheng break; 342da14cebeSEric Cheng case DLADM_STATUS_INVALID_PORT: 343da14cebeSEric Cheng s = "invalid port number"; 344da14cebeSEric Cheng break; 345da14cebeSEric Cheng case DLADM_STATUS_INVALID_DSF: 346da14cebeSEric Cheng s = "invalid dsfield"; 347da14cebeSEric Cheng break; 348da14cebeSEric Cheng case DLADM_STATUS_INVALID_DSFMASK: 349da14cebeSEric Cheng s = "invalid dsfield mask"; 350da14cebeSEric Cheng break; 351da14cebeSEric Cheng case DLADM_STATUS_INVALID_MACMARGIN: 352da14cebeSEric Cheng s = "MTU check failed, use lower MTU or -f option"; 353da14cebeSEric Cheng break; 354da14cebeSEric Cheng case DLADM_STATUS_BADPROP: 355da14cebeSEric Cheng s = "invalid property"; 356da14cebeSEric Cheng break; 357da14cebeSEric Cheng case DLADM_STATUS_MINMAXBW: 358da14cebeSEric Cheng s = "minimum value for maxbw is 1.2M"; 359da14cebeSEric Cheng break; 360da14cebeSEric Cheng case DLADM_STATUS_NO_HWRINGS: 361da14cebeSEric Cheng s = "request hw rings failed"; 362da14cebeSEric Cheng break; 3634eaa4710SRishi Srivatsavai case DLADM_STATUS_PERMONLY: 3644eaa4710SRishi Srivatsavai s = "change must be persistent"; 3654eaa4710SRishi Srivatsavai break; 3664eaa4710SRishi Srivatsavai case DLADM_STATUS_OPTMISSING: 3674eaa4710SRishi Srivatsavai s = "optional software not installed"; 3684eaa4710SRishi Srivatsavai break; 3692b24ab6bSSebastien Roy case DLADM_STATUS_IPTUNTYPE: 3702b24ab6bSSebastien Roy s = "invalid IP tunnel type"; 3712b24ab6bSSebastien Roy break; 3722b24ab6bSSebastien Roy case DLADM_STATUS_IPTUNTYPEREQD: 3732b24ab6bSSebastien Roy s = "IP tunnel type required"; 3742b24ab6bSSebastien Roy break; 3752b24ab6bSSebastien Roy case DLADM_STATUS_BADIPTUNLADDR: 3762b24ab6bSSebastien Roy s = "invalid local IP tunnel address"; 3772b24ab6bSSebastien Roy break; 3782b24ab6bSSebastien Roy case DLADM_STATUS_BADIPTUNRADDR: 3792b24ab6bSSebastien Roy s = "invalid remote IP tunnel address"; 3802b24ab6bSSebastien Roy break; 3812b24ab6bSSebastien Roy case DLADM_STATUS_ADDRINUSE: 3822b24ab6bSSebastien Roy s = "address already in use"; 3832b24ab6bSSebastien Roy break; 3840dc2366fSVenugopal Iyer case DLADM_STATUS_POOLCPU: 3850dc2366fSVenugopal Iyer s = "pool and cpus property are mutually exclusive"; 3860dc2366fSVenugopal Iyer break; 387*1cfa752fSRamaswamy Tummala case DLADM_STATUS_INVALID_PORT_INSTANCE: 388*1cfa752fSRamaswamy Tummala s = "invalid IB phys link"; 389*1cfa752fSRamaswamy Tummala break; 390*1cfa752fSRamaswamy Tummala case DLADM_STATUS_PORT_IS_DOWN: 391*1cfa752fSRamaswamy Tummala s = "port is down"; 392*1cfa752fSRamaswamy Tummala break; 393*1cfa752fSRamaswamy Tummala case DLADM_STATUS_PARTITION_EXISTS: 394*1cfa752fSRamaswamy Tummala s = "partition already exists"; 395*1cfa752fSRamaswamy Tummala break; 396*1cfa752fSRamaswamy Tummala case DLADM_STATUS_PKEY_NOT_PRESENT: 397*1cfa752fSRamaswamy Tummala s = "PKEY is not present on the port"; 398*1cfa752fSRamaswamy Tummala break; 399*1cfa752fSRamaswamy Tummala case DLADM_STATUS_INVALID_PKEY: 400*1cfa752fSRamaswamy Tummala s = "invalid PKEY"; 401*1cfa752fSRamaswamy Tummala break; 402*1cfa752fSRamaswamy Tummala case DLADM_STATUS_NO_IB_HW_RESOURCE: 403*1cfa752fSRamaswamy Tummala s = "IB internal resource not available"; 404*1cfa752fSRamaswamy Tummala break; 405*1cfa752fSRamaswamy Tummala case DLADM_STATUS_INVALID_PKEY_TBL_SIZE: 406*1cfa752fSRamaswamy Tummala s = "invalid PKEY table size"; 407*1cfa752fSRamaswamy Tummala break; 4080ba2cbe9Sxc151355 default: 40933343a97Smeem s = "<unknown error>"; 41033343a97Smeem break; 4110ba2cbe9Sxc151355 } 41233343a97Smeem (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 4130ba2cbe9Sxc151355 return (buf); 4140ba2cbe9Sxc151355 } 4150ba2cbe9Sxc151355 4160ba2cbe9Sxc151355 /* 4170ba2cbe9Sxc151355 * Convert a unix errno to a dladm_status_t. 4180ba2cbe9Sxc151355 * We only convert errnos that are likely to be encountered. All others 4190ba2cbe9Sxc151355 * are mapped to DLADM_STATUS_FAILED. 4200ba2cbe9Sxc151355 */ 4210ba2cbe9Sxc151355 dladm_status_t 4220ba2cbe9Sxc151355 dladm_errno2status(int err) 4230ba2cbe9Sxc151355 { 4240ba2cbe9Sxc151355 switch (err) { 425e7801d59Ssowmini case 0: 426e7801d59Ssowmini return (DLADM_STATUS_OK); 4270ba2cbe9Sxc151355 case EINVAL: 4280ba2cbe9Sxc151355 return (DLADM_STATUS_BADARG); 4290ba2cbe9Sxc151355 case EEXIST: 4300ba2cbe9Sxc151355 return (DLADM_STATUS_EXIST); 4310ba2cbe9Sxc151355 case ENOENT: 4320ba2cbe9Sxc151355 return (DLADM_STATUS_NOTFOUND); 4330ba2cbe9Sxc151355 case ENOSPC: 4340ba2cbe9Sxc151355 return (DLADM_STATUS_TOOSMALL); 4350ba2cbe9Sxc151355 case ENOMEM: 4360ba2cbe9Sxc151355 return (DLADM_STATUS_NOMEM); 4370ba2cbe9Sxc151355 case ENOTSUP: 4380ba2cbe9Sxc151355 return (DLADM_STATUS_NOTSUP); 439d62bc4baSyz147064 case ENETDOWN: 440d62bc4baSyz147064 return (DLADM_STATUS_NONOTIF); 4410ba2cbe9Sxc151355 case EACCES: 442eae72b5bSSebastien Roy case EPERM: 4430ba2cbe9Sxc151355 return (DLADM_STATUS_DENIED); 4440ba2cbe9Sxc151355 case EIO: 4450ba2cbe9Sxc151355 return (DLADM_STATUS_IOERR); 446843e1988Sjohnlev case EBUSY: 447d62bc4baSyz147064 return (DLADM_STATUS_LINKBUSY); 448d62bc4baSyz147064 case EAGAIN: 449d62bc4baSyz147064 return (DLADM_STATUS_TRYAGAIN); 450da14cebeSEric Cheng case ENOTEMPTY: 451da14cebeSEric Cheng return (DLADM_STATUS_FLOW_EXISTS); 452da14cebeSEric Cheng case EOPNOTSUPP: 453da14cebeSEric Cheng return (DLADM_STATUS_FLOW_INCOMPATIBLE); 454da14cebeSEric Cheng case EALREADY: 455da14cebeSEric Cheng return (DLADM_STATUS_FLOW_IDENTICAL); 4562b24ab6bSSebastien Roy case EADDRINUSE: 4572b24ab6bSSebastien Roy return (DLADM_STATUS_ADDRINUSE); 4580ba2cbe9Sxc151355 default: 4590ba2cbe9Sxc151355 return (DLADM_STATUS_FAILED); 4600ba2cbe9Sxc151355 } 4610ba2cbe9Sxc151355 } 4620ba2cbe9Sxc151355 46363a6526dSMichael Lim boolean_t 46463a6526dSMichael Lim dladm_str2interval(char *oarg, uint32_t *interval) 46563a6526dSMichael Lim { 46663a6526dSMichael Lim int val; 46763a6526dSMichael Lim char *endp = NULL; 46863a6526dSMichael Lim 46963a6526dSMichael Lim errno = 0; 47063a6526dSMichael Lim val = strtol(oarg, &endp, 10); 47163a6526dSMichael Lim if (errno != 0 || val <= 0 || *endp != '\0') 47263a6526dSMichael Lim return (B_FALSE); 47363a6526dSMichael Lim 47463a6526dSMichael Lim *interval = val; 47563a6526dSMichael Lim 47663a6526dSMichael Lim return (B_TRUE); 47763a6526dSMichael Lim } 47863a6526dSMichael Lim 479da14cebeSEric Cheng dladm_status_t 480da14cebeSEric Cheng dladm_str2bw(char *oarg, uint64_t *bw) 481da14cebeSEric Cheng { 482da14cebeSEric Cheng char *endp = NULL; 483da14cebeSEric Cheng int64_t n; 484da14cebeSEric Cheng int mult = 1; 485da14cebeSEric Cheng 486da14cebeSEric Cheng n = strtoull(oarg, &endp, 10); 487da14cebeSEric Cheng 488da14cebeSEric Cheng if ((errno != 0) || (strlen(endp) > 1)) 489da14cebeSEric Cheng return (DLADM_STATUS_BADARG); 490da14cebeSEric Cheng 491da14cebeSEric Cheng if (n < 0) 492da14cebeSEric Cheng return (DLADM_STATUS_BADVAL); 493da14cebeSEric Cheng 494da14cebeSEric Cheng switch (*endp) { 495da14cebeSEric Cheng case 'k': 496da14cebeSEric Cheng case 'K': 497da14cebeSEric Cheng mult = 1000; 498da14cebeSEric Cheng break; 499da14cebeSEric Cheng case 'm': 500da14cebeSEric Cheng case 'M': 501da14cebeSEric Cheng case '\0': 502da14cebeSEric Cheng mult = 1000000; 503da14cebeSEric Cheng break; 504da14cebeSEric Cheng case 'g': 505da14cebeSEric Cheng case 'G': 506da14cebeSEric Cheng mult = 1000000000; 507da14cebeSEric Cheng break; 508da14cebeSEric Cheng case '%': 509da14cebeSEric Cheng /* 510da14cebeSEric Cheng * percentages not supported for now, 511da14cebeSEric Cheng * see RFE 6540675 512da14cebeSEric Cheng */ 513da14cebeSEric Cheng return (DLADM_STATUS_NOTSUP); 514da14cebeSEric Cheng default: 515da14cebeSEric Cheng return (DLADM_STATUS_BADVAL); 516da14cebeSEric Cheng } 517da14cebeSEric Cheng 518da14cebeSEric Cheng *bw = n * mult; 519da14cebeSEric Cheng 520da14cebeSEric Cheng /* check for overflow */ 521da14cebeSEric Cheng if (*bw / mult != n) 522da14cebeSEric Cheng return (DLADM_STATUS_BADARG); 523da14cebeSEric Cheng 524da14cebeSEric Cheng return (DLADM_STATUS_OK); 525da14cebeSEric Cheng } 526da14cebeSEric Cheng 527da14cebeSEric Cheng /* 528da14cebeSEric Cheng * Convert bandwidth in bps to a string in mpbs. For values greater 529da14cebeSEric Cheng * than 1mbps or 1000000, print a whole mbps value. For values that 530da14cebeSEric Cheng * have fractional Mbps in whole Kbps , print the bandwidth in a manner 531da14cebeSEric Cheng * simlilar to a floating point format. 532da14cebeSEric Cheng * 533da14cebeSEric Cheng * bps string 534da14cebeSEric Cheng * 0 0 535da14cebeSEric Cheng * 100 0 536da14cebeSEric Cheng * 2000 0.002 537da14cebeSEric Cheng * 431000 0.431 538da14cebeSEric Cheng * 1000000 1 539da14cebeSEric Cheng * 1030000 1.030 540da14cebeSEric Cheng * 100000000 100 541da14cebeSEric Cheng */ 542da14cebeSEric Cheng const char * 543da14cebeSEric Cheng dladm_bw2str(int64_t bw, char *buf) 544da14cebeSEric Cheng { 545da14cebeSEric Cheng int kbps, mbps; 546da14cebeSEric Cheng 547da14cebeSEric Cheng kbps = (bw%1000000)/1000; 548da14cebeSEric Cheng mbps = bw/1000000; 549da14cebeSEric Cheng if (kbps != 0) { 550da14cebeSEric Cheng if (mbps == 0) 551da14cebeSEric Cheng (void) snprintf(buf, DLADM_STRSIZE, "0.%03u", kbps); 552da14cebeSEric Cheng else 553da14cebeSEric Cheng (void) snprintf(buf, DLADM_STRSIZE, "%5u.%03u", mbps, 554da14cebeSEric Cheng kbps); 555da14cebeSEric Cheng } else { 556da14cebeSEric Cheng (void) snprintf(buf, DLADM_STRSIZE, "%5u", mbps); 557da14cebeSEric Cheng } 558da14cebeSEric Cheng 559da14cebeSEric Cheng return (buf); 560da14cebeSEric Cheng } 561da14cebeSEric Cheng 5620ba2cbe9Sxc151355 #define LOCK_DB_PERMS S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH 5630ba2cbe9Sxc151355 5640ba2cbe9Sxc151355 static int 5650ba2cbe9Sxc151355 i_dladm_lock_db(const char *lock_file, short type) 5660ba2cbe9Sxc151355 { 5670ba2cbe9Sxc151355 int lock_fd; 5680ba2cbe9Sxc151355 struct flock lock; 5690ba2cbe9Sxc151355 5700ba2cbe9Sxc151355 if ((lock_fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC, 5710ba2cbe9Sxc151355 LOCK_DB_PERMS)) < 0) 5720ba2cbe9Sxc151355 return (-1); 5730ba2cbe9Sxc151355 5740ba2cbe9Sxc151355 lock.l_type = type; 5750ba2cbe9Sxc151355 lock.l_whence = SEEK_SET; 5760ba2cbe9Sxc151355 lock.l_start = 0; 5770ba2cbe9Sxc151355 lock.l_len = 0; 5780ba2cbe9Sxc151355 5790ba2cbe9Sxc151355 if (fcntl(lock_fd, F_SETLKW, &lock) < 0) { 5800ba2cbe9Sxc151355 int err = errno; 5810ba2cbe9Sxc151355 5820ba2cbe9Sxc151355 (void) close(lock_fd); 5830ba2cbe9Sxc151355 (void) unlink(lock_file); 5840ba2cbe9Sxc151355 errno = err; 5850ba2cbe9Sxc151355 return (-1); 5860ba2cbe9Sxc151355 } 5870ba2cbe9Sxc151355 return (lock_fd); 5880ba2cbe9Sxc151355 } 5890ba2cbe9Sxc151355 5900ba2cbe9Sxc151355 static void 5910ba2cbe9Sxc151355 i_dladm_unlock_db(const char *lock_file, int fd) 5920ba2cbe9Sxc151355 { 5930ba2cbe9Sxc151355 struct flock lock; 5940ba2cbe9Sxc151355 5950ba2cbe9Sxc151355 if (fd < 0) 5960ba2cbe9Sxc151355 return; 5970ba2cbe9Sxc151355 5980ba2cbe9Sxc151355 lock.l_type = F_UNLCK; 5990ba2cbe9Sxc151355 lock.l_whence = SEEK_SET; 6000ba2cbe9Sxc151355 lock.l_start = 0; 6010ba2cbe9Sxc151355 lock.l_len = 0; 6020ba2cbe9Sxc151355 6030ba2cbe9Sxc151355 (void) fcntl(fd, F_SETLKW, &lock); 6040ba2cbe9Sxc151355 (void) close(fd); 6050ba2cbe9Sxc151355 (void) unlink(lock_file); 6060ba2cbe9Sxc151355 } 6070ba2cbe9Sxc151355 608d62bc4baSyz147064 /* 609d62bc4baSyz147064 * Given a link class, returns its class string. 610d62bc4baSyz147064 */ 611d62bc4baSyz147064 const char * 612d62bc4baSyz147064 dladm_class2str(datalink_class_t class, char *buf) 613d62bc4baSyz147064 { 614d62bc4baSyz147064 const char *s; 615d62bc4baSyz147064 616d62bc4baSyz147064 switch (class) { 617d62bc4baSyz147064 case DATALINK_CLASS_PHYS: 618d62bc4baSyz147064 s = "phys"; 619d62bc4baSyz147064 break; 620d62bc4baSyz147064 case DATALINK_CLASS_VLAN: 621d62bc4baSyz147064 s = "vlan"; 622d62bc4baSyz147064 break; 623d62bc4baSyz147064 case DATALINK_CLASS_AGGR: 624d62bc4baSyz147064 s = "aggr"; 625d62bc4baSyz147064 break; 626d62bc4baSyz147064 case DATALINK_CLASS_VNIC: 627d62bc4baSyz147064 s = "vnic"; 628d62bc4baSyz147064 break; 629da14cebeSEric Cheng case DATALINK_CLASS_ETHERSTUB: 630da14cebeSEric Cheng s = "etherstub"; 631da14cebeSEric Cheng break; 6322b24ab6bSSebastien Roy case DATALINK_CLASS_IPTUN: 6332b24ab6bSSebastien Roy s = "iptun"; 6342b24ab6bSSebastien Roy break; 635b509e89bSRishi Srivatsavai case DATALINK_CLASS_SIMNET: 636b509e89bSRishi Srivatsavai s = "simnet"; 637b509e89bSRishi Srivatsavai break; 6384eaa4710SRishi Srivatsavai case DATALINK_CLASS_BRIDGE: 6394eaa4710SRishi Srivatsavai s = "bridge"; 6404eaa4710SRishi Srivatsavai break; 641*1cfa752fSRamaswamy Tummala case DATALINK_CLASS_PART: 642*1cfa752fSRamaswamy Tummala s = "part"; 643*1cfa752fSRamaswamy Tummala break; 644d62bc4baSyz147064 default: 645d62bc4baSyz147064 s = "unknown"; 646d62bc4baSyz147064 break; 647d62bc4baSyz147064 } 648d62bc4baSyz147064 649d62bc4baSyz147064 (void) snprintf(buf, DLADM_STRSIZE, "%s", s); 650d62bc4baSyz147064 return (buf); 651d62bc4baSyz147064 } 652d62bc4baSyz147064 653d62bc4baSyz147064 /* 654d62bc4baSyz147064 * Given a physical link media type, returns its media type string. 655d62bc4baSyz147064 */ 656d62bc4baSyz147064 const char * 657d62bc4baSyz147064 dladm_media2str(uint32_t media, char *buf) 658d62bc4baSyz147064 { 659b509e89bSRishi Srivatsavai const char *s = "--"; 660b509e89bSRishi Srivatsavai media_type_t *mt; 661b509e89bSRishi Srivatsavai int idx; 662d62bc4baSyz147064 663b509e89bSRishi Srivatsavai for (idx = 0; idx < MEDIATYPECOUNT; idx++) { 664b509e89bSRishi Srivatsavai mt = media_type_table + idx; 665b509e89bSRishi Srivatsavai if (mt->media_type == media) { 666b509e89bSRishi Srivatsavai s = mt->media_type_str; 667d62bc4baSyz147064 break; 668b509e89bSRishi Srivatsavai } 669d62bc4baSyz147064 } 670d62bc4baSyz147064 671d62bc4baSyz147064 (void) snprintf(buf, DLADM_STRSIZE, "%s", s); 672d62bc4baSyz147064 return (buf); 673d62bc4baSyz147064 } 674d62bc4baSyz147064 675b509e89bSRishi Srivatsavai /* 676b509e89bSRishi Srivatsavai * Given a physical link media type string, returns its media type constant. 677b509e89bSRishi Srivatsavai */ 678b509e89bSRishi Srivatsavai uint32_t 679b509e89bSRishi Srivatsavai dladm_str2media(const char *buf) 680b509e89bSRishi Srivatsavai { 681b509e89bSRishi Srivatsavai media_type_t *mt; 682b509e89bSRishi Srivatsavai int idx; 683b509e89bSRishi Srivatsavai 684b509e89bSRishi Srivatsavai for (idx = 0; idx < MEDIATYPECOUNT; idx++) { 685b509e89bSRishi Srivatsavai mt = media_type_table + idx; 686b509e89bSRishi Srivatsavai if (strcasecmp(buf, mt->media_type_str) == 0) 687b509e89bSRishi Srivatsavai return (mt->media_type); 688b509e89bSRishi Srivatsavai } 689b509e89bSRishi Srivatsavai 690b509e89bSRishi Srivatsavai return (DL_OTHER); 691b509e89bSRishi Srivatsavai } 692b509e89bSRishi Srivatsavai 6930ba2cbe9Sxc151355 dladm_status_t 6944ac67f02SAnurag S. Maskey i_dladm_rw_db(dladm_handle_t handle, const char *db_file, mode_t db_perms, 6954ac67f02SAnurag S. Maskey dladm_status_t (*process_db)(dladm_handle_t, void *, FILE *, FILE *), 6960ba2cbe9Sxc151355 void *arg, boolean_t writeop) 6970ba2cbe9Sxc151355 { 6980ba2cbe9Sxc151355 dladm_status_t status = DLADM_STATUS_OK; 6990ba2cbe9Sxc151355 FILE *fp, *nfp = NULL; 7000ba2cbe9Sxc151355 char lock[MAXPATHLEN]; 7010ba2cbe9Sxc151355 char file[MAXPATHLEN]; 7020ba2cbe9Sxc151355 char newfile[MAXPATHLEN]; 7030ba2cbe9Sxc151355 char *db_basename; 7040ba2cbe9Sxc151355 int nfd, lock_fd; 7050ba2cbe9Sxc151355 7060ba2cbe9Sxc151355 /* 7070ba2cbe9Sxc151355 * If we are called from a boot script such as net-physical, 7080ba2cbe9Sxc151355 * it's quite likely that the root fs is still not writable. 7090ba2cbe9Sxc151355 * For this case, it's ok for the lock creation to fail since 7100ba2cbe9Sxc151355 * no one else could be accessing our configuration file. 7110ba2cbe9Sxc151355 */ 7120ba2cbe9Sxc151355 db_basename = strrchr(db_file, '/'); 7130ba2cbe9Sxc151355 if (db_basename == NULL || db_basename[1] == '\0') 7140ba2cbe9Sxc151355 return (dladm_errno2status(EINVAL)); 7150ba2cbe9Sxc151355 db_basename++; 7160ba2cbe9Sxc151355 (void) snprintf(lock, MAXPATHLEN, "/tmp/%s.lock", db_basename); 7170ba2cbe9Sxc151355 if ((lock_fd = i_dladm_lock_db 7180ba2cbe9Sxc151355 (lock, (writeop ? F_WRLCK : F_RDLCK))) < 0 && errno != EROFS) 7190ba2cbe9Sxc151355 return (dladm_errno2status(errno)); 7200ba2cbe9Sxc151355 7210ba2cbe9Sxc151355 (void) snprintf(file, MAXPATHLEN, "%s/%s", dladm_rootdir, db_file); 7220ba2cbe9Sxc151355 if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) { 7230ba2cbe9Sxc151355 int err = errno; 7240ba2cbe9Sxc151355 7250ba2cbe9Sxc151355 i_dladm_unlock_db(lock, lock_fd); 7260ba2cbe9Sxc151355 if (err == ENOENT) 7270ba2cbe9Sxc151355 return (DLADM_STATUS_DBNOTFOUND); 7280ba2cbe9Sxc151355 7290ba2cbe9Sxc151355 return (dladm_errno2status(err)); 7300ba2cbe9Sxc151355 } 7310ba2cbe9Sxc151355 7320ba2cbe9Sxc151355 if (writeop) { 7330ba2cbe9Sxc151355 (void) snprintf(newfile, MAXPATHLEN, "%s/%s.new", 7340ba2cbe9Sxc151355 dladm_rootdir, db_file); 7350ba2cbe9Sxc151355 if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC, 7360ba2cbe9Sxc151355 db_perms)) < 0) { 7370ba2cbe9Sxc151355 (void) fclose(fp); 7380ba2cbe9Sxc151355 i_dladm_unlock_db(lock, lock_fd); 7390ba2cbe9Sxc151355 return (dladm_errno2status(errno)); 7400ba2cbe9Sxc151355 } 7410ba2cbe9Sxc151355 7420ba2cbe9Sxc151355 if ((nfp = fdopen(nfd, "w")) == NULL) { 7430ba2cbe9Sxc151355 (void) close(nfd); 7440ba2cbe9Sxc151355 (void) fclose(fp); 7450ba2cbe9Sxc151355 (void) unlink(newfile); 7460ba2cbe9Sxc151355 i_dladm_unlock_db(lock, lock_fd); 7470ba2cbe9Sxc151355 return (dladm_errno2status(errno)); 7480ba2cbe9Sxc151355 } 7490ba2cbe9Sxc151355 } 7504ac67f02SAnurag S. Maskey status = (*process_db)(handle, arg, fp, nfp); 7510ba2cbe9Sxc151355 if (!writeop || status != DLADM_STATUS_OK) 7520ba2cbe9Sxc151355 goto done; 7530ba2cbe9Sxc151355 7546ba597c5SAnurag S. Maskey /* Set permissions on file to db_perms */ 7556ba597c5SAnurag S. Maskey if (fchmod(nfd, db_perms) < 0) { 7560ba2cbe9Sxc151355 status = dladm_errno2status(errno); 7570ba2cbe9Sxc151355 goto done; 7580ba2cbe9Sxc151355 } 7596ba597c5SAnurag S. Maskey 7606ba597c5SAnurag S. Maskey /* 7616ba597c5SAnurag S. Maskey * Configuration files need to be owned by the 'dladm' user and 7626ba597c5SAnurag S. Maskey * 'netadm' group. 7636ba597c5SAnurag S. Maskey */ 7646ba597c5SAnurag S. Maskey if (fchown(nfd, UID_DLADM, GID_NETADM) < 0) { 7656ba597c5SAnurag S. Maskey status = dladm_errno2status(errno); 7666ba597c5SAnurag S. Maskey goto done; 7670ba2cbe9Sxc151355 } 7680ba2cbe9Sxc151355 7690ba2cbe9Sxc151355 if (fflush(nfp) == EOF) { 7700ba2cbe9Sxc151355 status = dladm_errno2status(errno); 7710ba2cbe9Sxc151355 goto done; 7720ba2cbe9Sxc151355 } 7730ba2cbe9Sxc151355 (void) fclose(fp); 7740ba2cbe9Sxc151355 (void) fclose(nfp); 7750ba2cbe9Sxc151355 7760ba2cbe9Sxc151355 if (rename(newfile, file) < 0) { 7770ba2cbe9Sxc151355 (void) unlink(newfile); 7780ba2cbe9Sxc151355 i_dladm_unlock_db(lock, lock_fd); 7790ba2cbe9Sxc151355 return (dladm_errno2status(errno)); 7800ba2cbe9Sxc151355 } 7810ba2cbe9Sxc151355 7820ba2cbe9Sxc151355 i_dladm_unlock_db(lock, lock_fd); 7830ba2cbe9Sxc151355 return (DLADM_STATUS_OK); 7840ba2cbe9Sxc151355 7850ba2cbe9Sxc151355 done: 7860ba2cbe9Sxc151355 if (nfp != NULL) { 7870ba2cbe9Sxc151355 (void) fclose(nfp); 7880ba2cbe9Sxc151355 if (status != DLADM_STATUS_OK) 7890ba2cbe9Sxc151355 (void) unlink(newfile); 7900ba2cbe9Sxc151355 } 7910ba2cbe9Sxc151355 (void) fclose(fp); 7920ba2cbe9Sxc151355 i_dladm_unlock_db(lock, lock_fd); 7930ba2cbe9Sxc151355 return (status); 7940ba2cbe9Sxc151355 } 7950ba2cbe9Sxc151355 7960ba2cbe9Sxc151355 dladm_status_t 7970ba2cbe9Sxc151355 dladm_set_rootdir(const char *rootdir) 7980ba2cbe9Sxc151355 { 7990ba2cbe9Sxc151355 DIR *dp; 8000ba2cbe9Sxc151355 8010ba2cbe9Sxc151355 if (rootdir == NULL || *rootdir != '/' || 8020ba2cbe9Sxc151355 (dp = opendir(rootdir)) == NULL) 8030ba2cbe9Sxc151355 return (DLADM_STATUS_BADARG); 8040ba2cbe9Sxc151355 8050ba2cbe9Sxc151355 (void) strncpy(dladm_rootdir, rootdir, MAXPATHLEN); 8060ba2cbe9Sxc151355 (void) closedir(dp); 8070ba2cbe9Sxc151355 return (DLADM_STATUS_OK); 8080ba2cbe9Sxc151355 } 809d62bc4baSyz147064 810d62bc4baSyz147064 boolean_t 811d62bc4baSyz147064 dladm_valid_linkname(const char *link) 812d62bc4baSyz147064 { 813d62bc4baSyz147064 size_t len = strlen(link); 814d62bc4baSyz147064 const char *cp; 815d62bc4baSyz147064 8162b24ab6bSSebastien Roy if (len >= MAXLINKNAMELEN) 817d62bc4baSyz147064 return (B_FALSE); 818d62bc4baSyz147064 819d62bc4baSyz147064 /* 820d62bc4baSyz147064 * The link name cannot start with a digit and must end with a digit. 821d62bc4baSyz147064 */ 822d62bc4baSyz147064 if ((isdigit(link[0]) != 0) || (isdigit(link[len - 1]) == 0)) 823d62bc4baSyz147064 return (B_FALSE); 824d62bc4baSyz147064 825d62bc4baSyz147064 /* 826d62bc4baSyz147064 * The legal characters in a link name are: 8272b24ab6bSSebastien Roy * alphanumeric (a-z, A-Z, 0-9), underscore ('_'), and '.'. 828d62bc4baSyz147064 */ 829d62bc4baSyz147064 for (cp = link; *cp != '\0'; cp++) { 8302b24ab6bSSebastien Roy if ((isalnum(*cp) == 0) && (*cp != '_') && (*cp != '.')) 831d62bc4baSyz147064 return (B_FALSE); 832d62bc4baSyz147064 } 833d62bc4baSyz147064 834d62bc4baSyz147064 return (B_TRUE); 835d62bc4baSyz147064 } 836da14cebeSEric Cheng 837da14cebeSEric Cheng /* 838da14cebeSEric Cheng * Convert priority string to a value. 839da14cebeSEric Cheng */ 840da14cebeSEric Cheng dladm_status_t 841da14cebeSEric Cheng dladm_str2pri(char *token, mac_priority_level_t *pri) 842da14cebeSEric Cheng { 843da14cebeSEric Cheng if (strlen(token) == strlen("low") && 844da14cebeSEric Cheng strncasecmp(token, "low", strlen("low")) == 0) { 845da14cebeSEric Cheng *pri = MPL_LOW; 846da14cebeSEric Cheng } else if (strlen(token) == strlen("medium") && 847da14cebeSEric Cheng strncasecmp(token, "medium", strlen("medium")) == 0) { 848da14cebeSEric Cheng *pri = MPL_MEDIUM; 849da14cebeSEric Cheng } else if (strlen(token) == strlen("high") && 850da14cebeSEric Cheng strncasecmp(token, "high", strlen("high")) == 0) { 851da14cebeSEric Cheng *pri = MPL_HIGH; 852da14cebeSEric Cheng } else { 853da14cebeSEric Cheng return (DLADM_STATUS_BADVAL); 854da14cebeSEric Cheng } 855da14cebeSEric Cheng return (DLADM_STATUS_OK); 856da14cebeSEric Cheng } 857da14cebeSEric Cheng 858da14cebeSEric Cheng /* 859da14cebeSEric Cheng * Convert priority value to a string. 860da14cebeSEric Cheng */ 861da14cebeSEric Cheng const char * 862da14cebeSEric Cheng dladm_pri2str(mac_priority_level_t pri, char *buf) 863da14cebeSEric Cheng { 864da14cebeSEric Cheng const char *s; 865da14cebeSEric Cheng 866da14cebeSEric Cheng switch (pri) { 867da14cebeSEric Cheng case MPL_LOW: 868da14cebeSEric Cheng s = "low"; 869da14cebeSEric Cheng break; 870da14cebeSEric Cheng case MPL_MEDIUM: 871da14cebeSEric Cheng s = "medium"; 872da14cebeSEric Cheng break; 873da14cebeSEric Cheng case MPL_HIGH: 874da14cebeSEric Cheng s = "high"; 875da14cebeSEric Cheng break; 876da14cebeSEric Cheng default: 877da14cebeSEric Cheng s = "--"; 878da14cebeSEric Cheng break; 879da14cebeSEric Cheng } 880da14cebeSEric Cheng (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 881da14cebeSEric Cheng return (buf); 882da14cebeSEric Cheng } 883da14cebeSEric Cheng 88425ec3e3dSEric Cheng /* 88525ec3e3dSEric Cheng * Convert protect string to a value. 88625ec3e3dSEric Cheng */ 88725ec3e3dSEric Cheng dladm_status_t 88825ec3e3dSEric Cheng dladm_str2protect(char *token, uint32_t *ptype) 88925ec3e3dSEric Cheng { 89025ec3e3dSEric Cheng link_protect_t *lp; 89125ec3e3dSEric Cheng int i; 89225ec3e3dSEric Cheng 89325ec3e3dSEric Cheng for (i = 0; i < LPTYPES; i++) { 89425ec3e3dSEric Cheng lp = &link_protect_types[i]; 89525ec3e3dSEric Cheng if (strcmp(token, lp->lp_name) == 0) { 89625ec3e3dSEric Cheng *ptype = lp->lp_type; 89725ec3e3dSEric Cheng return (DLADM_STATUS_OK); 89825ec3e3dSEric Cheng } 89925ec3e3dSEric Cheng } 90025ec3e3dSEric Cheng return (DLADM_STATUS_BADVAL); 90125ec3e3dSEric Cheng } 90225ec3e3dSEric Cheng 90325ec3e3dSEric Cheng /* 90425ec3e3dSEric Cheng * Convert protect value to a string. 90525ec3e3dSEric Cheng */ 90625ec3e3dSEric Cheng const char * 90725ec3e3dSEric Cheng dladm_protect2str(uint32_t ptype, char *buf) 90825ec3e3dSEric Cheng { 90925ec3e3dSEric Cheng const char *s = "--"; 91025ec3e3dSEric Cheng link_protect_t *lp; 91125ec3e3dSEric Cheng int i; 91225ec3e3dSEric Cheng 91325ec3e3dSEric Cheng for (i = 0; i < LPTYPES; i++) { 91425ec3e3dSEric Cheng lp = &link_protect_types[i]; 91525ec3e3dSEric Cheng if (lp->lp_type == ptype) { 91625ec3e3dSEric Cheng s = lp->lp_name; 91725ec3e3dSEric Cheng break; 91825ec3e3dSEric Cheng } 91925ec3e3dSEric Cheng } 92025ec3e3dSEric Cheng (void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s)); 92125ec3e3dSEric Cheng return (buf); 92225ec3e3dSEric Cheng } 92325ec3e3dSEric Cheng 92425ec3e3dSEric Cheng /* 92525ec3e3dSEric Cheng * Convert an IPv4 address to/from a string. 92625ec3e3dSEric Cheng */ 92725ec3e3dSEric Cheng const char * 92825ec3e3dSEric Cheng dladm_ipv4addr2str(void *addr, char *buf) 92925ec3e3dSEric Cheng { 93025ec3e3dSEric Cheng if (inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN) == NULL) 9310dc2366fSVenugopal Iyer buf[0] = '\0'; 93225ec3e3dSEric Cheng 93325ec3e3dSEric Cheng return (buf); 93425ec3e3dSEric Cheng } 93525ec3e3dSEric Cheng 93625ec3e3dSEric Cheng dladm_status_t 93725ec3e3dSEric Cheng dladm_str2ipv4addr(char *token, void *addr) 93825ec3e3dSEric Cheng { 93925ec3e3dSEric Cheng return (inet_pton(AF_INET, token, addr) == 1 ? 94025ec3e3dSEric Cheng DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP); 94125ec3e3dSEric Cheng } 94225ec3e3dSEric Cheng 9430dc2366fSVenugopal Iyer const char * 9440dc2366fSVenugopal Iyer dladm_ipv6addr2str(void *addr, char *buf) 9450dc2366fSVenugopal Iyer { 9460dc2366fSVenugopal Iyer if (inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN) == NULL) 9470dc2366fSVenugopal Iyer buf[0] = '\0'; 9480dc2366fSVenugopal Iyer 9490dc2366fSVenugopal Iyer return (buf); 9500dc2366fSVenugopal Iyer } 9510dc2366fSVenugopal Iyer 9520dc2366fSVenugopal Iyer dladm_status_t 9530dc2366fSVenugopal Iyer dladm_str2ipv6addr(char *token, void *addr) 9540dc2366fSVenugopal Iyer { 9550dc2366fSVenugopal Iyer return (inet_pton(AF_INET6, token, addr) == 1 ? 9560dc2366fSVenugopal Iyer DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP); 9570dc2366fSVenugopal Iyer } 9580dc2366fSVenugopal Iyer 95925ec3e3dSEric Cheng /* 96025ec3e3dSEric Cheng * Find the set bits in a mask. 96125ec3e3dSEric Cheng * This is used for expanding a bitmask into individual sub-masks 96225ec3e3dSEric Cheng * which can be used for further processing. 96325ec3e3dSEric Cheng */ 96425ec3e3dSEric Cheng void 96525ec3e3dSEric Cheng dladm_find_setbits32(uint32_t mask, uint32_t *list, uint32_t *cnt) 96625ec3e3dSEric Cheng { 96725ec3e3dSEric Cheng int i, c = 0; 96825ec3e3dSEric Cheng 96925ec3e3dSEric Cheng for (i = 0; i < 32; i++) { 97025ec3e3dSEric Cheng if (((1 << i) & mask) != 0) 97125ec3e3dSEric Cheng list[c++] = 1 << i; 97225ec3e3dSEric Cheng } 97325ec3e3dSEric Cheng *cnt = c; 97425ec3e3dSEric Cheng } 97525ec3e3dSEric Cheng 976da14cebeSEric Cheng void 977da14cebeSEric Cheng dladm_free_args(dladm_arg_list_t *list) 978da14cebeSEric Cheng { 979da14cebeSEric Cheng if (list != NULL) { 980da14cebeSEric Cheng free(list->al_buf); 981da14cebeSEric Cheng free(list); 982da14cebeSEric Cheng } 983da14cebeSEric Cheng } 984da14cebeSEric Cheng 985da14cebeSEric Cheng dladm_status_t 986da14cebeSEric Cheng dladm_parse_args(char *str, dladm_arg_list_t **listp, boolean_t novalues) 987da14cebeSEric Cheng { 988da14cebeSEric Cheng dladm_arg_list_t *list; 989da14cebeSEric Cheng dladm_arg_info_t *aip; 990da14cebeSEric Cheng char *buf, *curr; 991da14cebeSEric Cheng int len, i; 992da14cebeSEric Cheng 99363a6526dSMichael Lim if (str == NULL) 99463a6526dSMichael Lim return (DLADM_STATUS_BADVAL); 99563a6526dSMichael Lim 99663a6526dSMichael Lim if (str[0] == '\0') 99763a6526dSMichael Lim return (DLADM_STATUS_OK); 99863a6526dSMichael Lim 999da14cebeSEric Cheng list = malloc(sizeof (dladm_arg_list_t)); 1000da14cebeSEric Cheng if (list == NULL) 1001da14cebeSEric Cheng return (dladm_errno2status(errno)); 1002da14cebeSEric Cheng 1003da14cebeSEric Cheng list->al_count = 0; 1004da14cebeSEric Cheng list->al_buf = buf = strdup(str); 1005da14cebeSEric Cheng if (buf == NULL) 1006da14cebeSEric Cheng return (dladm_errno2status(errno)); 1007da14cebeSEric Cheng 1008da14cebeSEric Cheng curr = buf; 1009da14cebeSEric Cheng len = strlen(buf); 1010da14cebeSEric Cheng aip = NULL; 1011da14cebeSEric Cheng for (i = 0; i < len; i++) { 1012da14cebeSEric Cheng char c = buf[i]; 1013da14cebeSEric Cheng boolean_t match = (c == '=' || c == ','); 1014da14cebeSEric Cheng 1015da14cebeSEric Cheng if (!match && i != len - 1) 1016da14cebeSEric Cheng continue; 1017da14cebeSEric Cheng 1018da14cebeSEric Cheng if (match) { 1019da14cebeSEric Cheng buf[i] = '\0'; 1020da14cebeSEric Cheng if (*curr == '\0') 1021da14cebeSEric Cheng goto fail; 1022da14cebeSEric Cheng } 1023da14cebeSEric Cheng 1024da14cebeSEric Cheng if (aip != NULL && c != '=') { 1025da14cebeSEric Cheng if (aip->ai_count > DLADM_MAX_ARG_VALS) 1026da14cebeSEric Cheng goto fail; 1027da14cebeSEric Cheng 1028da14cebeSEric Cheng if (novalues) 1029da14cebeSEric Cheng goto fail; 1030da14cebeSEric Cheng 1031da14cebeSEric Cheng aip->ai_val[aip->ai_count] = curr; 1032da14cebeSEric Cheng aip->ai_count++; 1033da14cebeSEric Cheng } else { 1034da14cebeSEric Cheng if (list->al_count > DLADM_MAX_ARG_VALS) 1035da14cebeSEric Cheng goto fail; 1036da14cebeSEric Cheng 1037da14cebeSEric Cheng aip = &list->al_info[list->al_count]; 1038da14cebeSEric Cheng aip->ai_name = curr; 1039da14cebeSEric Cheng aip->ai_count = 0; 1040da14cebeSEric Cheng list->al_count++; 1041da14cebeSEric Cheng if (c == ',') 1042da14cebeSEric Cheng aip = NULL; 1043da14cebeSEric Cheng } 1044da14cebeSEric Cheng curr = buf + i + 1; 1045da14cebeSEric Cheng } 1046da14cebeSEric Cheng 1047da14cebeSEric Cheng *listp = list; 1048da14cebeSEric Cheng return (DLADM_STATUS_OK); 1049da14cebeSEric Cheng 1050da14cebeSEric Cheng fail: 1051da14cebeSEric Cheng dladm_free_args(list); 1052da14cebeSEric Cheng return (DLADM_STATUS_FAILED); 1053da14cebeSEric Cheng } 1054