xref: /titanic_50/usr/src/lib/libdladm/common/libdladm.c (revision a776d98e5f349dbb7f5a47eca48f50e6117adcb7)
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 /*
221cfa752fSRamaswamy 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>
41c569ef53SMichael Lim #include <libdllink.h>
42aae21359Skrgopi 
430ba2cbe9Sxc151355 static char	dladm_rootdir[MAXPATHLEN] = "/";
440ba2cbe9Sxc151355 
45b509e89bSRishi Srivatsavai typedef struct media_type_desc {
46b509e89bSRishi Srivatsavai 	uint32_t	media_type;
47b509e89bSRishi Srivatsavai #define	MAX_MEDIA_TYPE_STRING	32
48b509e89bSRishi Srivatsavai 	const char	media_type_str[MAX_MEDIA_TYPE_STRING];
49b509e89bSRishi Srivatsavai } media_type_t;
50b509e89bSRishi Srivatsavai 
51b509e89bSRishi Srivatsavai static media_type_t media_type_table[] =  {
52b509e89bSRishi Srivatsavai 	{ DL_ETHER,	"Ethernet" },
53b509e89bSRishi Srivatsavai 	{ DL_WIFI,	"WiFi" },
54b509e89bSRishi Srivatsavai 	{ DL_IB,	"Infiniband" },
55b509e89bSRishi Srivatsavai 	{ DL_IPV4,	"IPv4Tunnel" },
56b509e89bSRishi Srivatsavai 	{ DL_IPV6,	"IPv6Tunnel" },
572b24ab6bSSebastien Roy 	{ DL_6TO4,	"6to4Tunnel" },
58b509e89bSRishi Srivatsavai 	{ DL_CSMACD,	"CSMA/CD" },
59b509e89bSRishi Srivatsavai 	{ DL_TPB,	"TokenBus" },
60b509e89bSRishi Srivatsavai 	{ DL_TPR,	"TokenRing" },
61b509e89bSRishi Srivatsavai 	{ DL_METRO,	"MetroNet" },
62b509e89bSRishi Srivatsavai 	{ DL_HDLC,	"HDLC" },
63b509e89bSRishi Srivatsavai 	{ DL_CHAR,	"SyncCharacter" },
64b509e89bSRishi Srivatsavai 	{ DL_CTCA,	"CTCA" },
65b509e89bSRishi Srivatsavai 	{ DL_FDDI, 	"FDDI" },
66b509e89bSRishi Srivatsavai 	{ DL_FC, 	"FiberChannel" },
67b509e89bSRishi Srivatsavai 	{ DL_ATM, 	"ATM" },
68b509e89bSRishi Srivatsavai 	{ DL_IPATM, 	"ATM(ClassicIP)" },
69b509e89bSRishi Srivatsavai 	{ DL_X25, 	"X.25" },
70b509e89bSRishi Srivatsavai 	{ DL_IPX25, 	"X.25(ClassicIP)" },
71b509e89bSRishi Srivatsavai 	{ DL_ISDN, 	"ISDN" },
72b509e89bSRishi Srivatsavai 	{ DL_HIPPI, 	"HIPPI" },
73b509e89bSRishi Srivatsavai 	{ DL_100VG, 	"100BaseVGEthernet" },
74b509e89bSRishi Srivatsavai 	{ DL_100VGTPR, 	"100BaseVGTokenRing" },
75b509e89bSRishi Srivatsavai 	{ DL_ETH_CSMA, 	"IEEE802.3" },
76b509e89bSRishi Srivatsavai 	{ DL_100BT, 	"100BaseT" },
77b509e89bSRishi Srivatsavai 	{ DL_FRAME, 	"FrameRelay" },
78b509e89bSRishi Srivatsavai 	{ DL_MPFRAME, 	"MPFrameRelay" },
79b509e89bSRishi Srivatsavai 	{ DL_ASYNC, 	"AsyncCharacter" },
80b509e89bSRishi Srivatsavai 	{ DL_IPNET, 	"IPNET" },
81b509e89bSRishi Srivatsavai 	{ DL_OTHER, 	"Other" }
82b509e89bSRishi Srivatsavai };
83b509e89bSRishi Srivatsavai #define	MEDIATYPECOUNT	(sizeof (media_type_table) / sizeof (media_type_t))
84b509e89bSRishi Srivatsavai 
8525ec3e3dSEric Cheng typedef struct {
8625ec3e3dSEric Cheng 	uint32_t	lp_type;
8725ec3e3dSEric Cheng 	char		*lp_name;
8825ec3e3dSEric Cheng } link_protect_t;
8925ec3e3dSEric Cheng 
9025ec3e3dSEric Cheng static link_protect_t link_protect_types[] = {
9125ec3e3dSEric Cheng 	{ MPT_MACNOSPOOF, "mac-nospoof" },
920dc2366fSVenugopal Iyer 	{ MPT_RESTRICTED, "restricted" },
9325ec3e3dSEric Cheng 	{ MPT_IPNOSPOOF, "ip-nospoof" },
940dc2366fSVenugopal Iyer 	{ MPT_DHCPNOSPOOF, "dhcp-nospoof" }
9525ec3e3dSEric Cheng };
9625ec3e3dSEric Cheng #define	LPTYPES	(sizeof (link_protect_types) / sizeof (link_protect_t))
9725ec3e3dSEric Cheng 
984ac67f02SAnurag S. Maskey dladm_status_t
dladm_open(dladm_handle_t * handle)994ac67f02SAnurag S. Maskey dladm_open(dladm_handle_t *handle)
1004ac67f02SAnurag S. Maskey {
1014ac67f02SAnurag S. Maskey 	int dld_fd;
1024ac67f02SAnurag S. Maskey 
1034ac67f02SAnurag S. Maskey 	if (handle == NULL)
1044ac67f02SAnurag S. Maskey 		return (DLADM_STATUS_BADARG);
1054ac67f02SAnurag S. Maskey 
1064ac67f02SAnurag S. Maskey 	if ((dld_fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1074ac67f02SAnurag S. Maskey 		return (dladm_errno2status(errno));
1084ac67f02SAnurag S. Maskey 
1094ac67f02SAnurag S. Maskey 	/*
1104ac67f02SAnurag S. Maskey 	 * Don't open DLMGMT_DOOR now.  dlmgmtd(1M) is not able to
1114ac67f02SAnurag S. Maskey 	 * open the door when the dladm handle is opened because the
1124ac67f02SAnurag S. Maskey 	 * door hasn't been created yet at that time.  Thus, we must
1134ac67f02SAnurag S. Maskey 	 * open it on-demand in dladm_door_fd().  Move the open()
1144ac67f02SAnurag S. Maskey 	 * to dladm_door_fd() for all cases.
1154ac67f02SAnurag S. Maskey 	 */
1164ac67f02SAnurag S. Maskey 
1174ac67f02SAnurag S. Maskey 	if ((*handle = malloc(sizeof (struct dladm_handle))) == NULL) {
1184ac67f02SAnurag S. Maskey 		(void) close(dld_fd);
1194ac67f02SAnurag S. Maskey 		return (DLADM_STATUS_NOMEM);
1204ac67f02SAnurag S. Maskey 	}
1214ac67f02SAnurag S. Maskey 
1224ac67f02SAnurag S. Maskey 	(*handle)->dld_fd = dld_fd;
1234ac67f02SAnurag S. Maskey 	(*handle)->door_fd = -1;
1244ac67f02SAnurag S. Maskey 
1254ac67f02SAnurag S. Maskey 	return (DLADM_STATUS_OK);
1264ac67f02SAnurag S. Maskey }
1274ac67f02SAnurag S. Maskey 
1284ac67f02SAnurag S. Maskey void
dladm_close(dladm_handle_t handle)1294ac67f02SAnurag S. Maskey dladm_close(dladm_handle_t handle)
1304ac67f02SAnurag S. Maskey {
1314ac67f02SAnurag S. Maskey 	if (handle != NULL) {
1324ac67f02SAnurag S. Maskey 		(void) close(handle->dld_fd);
1334ac67f02SAnurag S. Maskey 		if (handle->door_fd != -1)
1344ac67f02SAnurag S. Maskey 			(void) close(handle->door_fd);
1354ac67f02SAnurag S. Maskey 		free(handle);
1364ac67f02SAnurag S. Maskey 	}
1374ac67f02SAnurag S. Maskey }
1384ac67f02SAnurag S. Maskey 
1394ac67f02SAnurag S. Maskey int
dladm_dld_fd(dladm_handle_t handle)1404ac67f02SAnurag S. Maskey dladm_dld_fd(dladm_handle_t handle)
1414ac67f02SAnurag S. Maskey {
1424ac67f02SAnurag S. Maskey 	return (handle->dld_fd);
1434ac67f02SAnurag S. Maskey }
1444ac67f02SAnurag S. Maskey 
1454ac67f02SAnurag S. Maskey /*
1464ac67f02SAnurag S. Maskey  * If DLMGMT_DOOR hasn't been opened in the handle yet, open it.
1474ac67f02SAnurag S. Maskey  */
1484ac67f02SAnurag S. Maskey dladm_status_t
dladm_door_fd(dladm_handle_t handle,int * door_fd)1494ac67f02SAnurag S. Maskey dladm_door_fd(dladm_handle_t handle, int *door_fd)
1504ac67f02SAnurag S. Maskey {
1514ac67f02SAnurag S. Maskey 	int fd;
1524ac67f02SAnurag S. Maskey 
1534ac67f02SAnurag S. Maskey 	if (handle->door_fd == -1) {
1544ac67f02SAnurag S. Maskey 		if ((fd = open(DLMGMT_DOOR, O_RDONLY)) < 0)
1554ac67f02SAnurag S. Maskey 			return (dladm_errno2status(errno));
1564ac67f02SAnurag S. Maskey 		handle->door_fd = fd;
1574ac67f02SAnurag S. Maskey 	}
1584ac67f02SAnurag S. Maskey 	*door_fd = handle->door_fd;
1594ac67f02SAnurag S. Maskey 
1604ac67f02SAnurag S. Maskey 	return (DLADM_STATUS_OK);
1614ac67f02SAnurag S. Maskey }
1624ac67f02SAnurag S. Maskey 
1630ba2cbe9Sxc151355 const char *
dladm_status2str(dladm_status_t status,char * buf)1640ba2cbe9Sxc151355 dladm_status2str(dladm_status_t status, char *buf)
1650ba2cbe9Sxc151355 {
1660ba2cbe9Sxc151355 	const char	*s;
1670ba2cbe9Sxc151355 
1680ba2cbe9Sxc151355 	switch (status) {
1690ba2cbe9Sxc151355 	case DLADM_STATUS_OK:
1700ba2cbe9Sxc151355 		s = "ok";
1710ba2cbe9Sxc151355 		break;
1720ba2cbe9Sxc151355 	case DLADM_STATUS_BADARG:
1730ba2cbe9Sxc151355 		s = "invalid argument";
1740ba2cbe9Sxc151355 		break;
1750ba2cbe9Sxc151355 	case DLADM_STATUS_FAILED:
1760ba2cbe9Sxc151355 		s = "operation failed";
1770ba2cbe9Sxc151355 		break;
1780ba2cbe9Sxc151355 	case DLADM_STATUS_TOOSMALL:
1790ba2cbe9Sxc151355 		s = "buffer size too small";
1800ba2cbe9Sxc151355 		break;
1810ba2cbe9Sxc151355 	case DLADM_STATUS_NOTSUP:
1820ba2cbe9Sxc151355 		s = "operation not supported";
1830ba2cbe9Sxc151355 		break;
1840ba2cbe9Sxc151355 	case DLADM_STATUS_NOTFOUND:
1850ba2cbe9Sxc151355 		s = "object not found";
1860ba2cbe9Sxc151355 		break;
1870ba2cbe9Sxc151355 	case DLADM_STATUS_BADVAL:
1880ba2cbe9Sxc151355 		s = "invalid value";
1890ba2cbe9Sxc151355 		break;
1900ba2cbe9Sxc151355 	case DLADM_STATUS_NOMEM:
1910ba2cbe9Sxc151355 		s = "insufficient memory";
1920ba2cbe9Sxc151355 		break;
1930ba2cbe9Sxc151355 	case DLADM_STATUS_EXIST:
1940ba2cbe9Sxc151355 		s = "object already exists";
1950ba2cbe9Sxc151355 		break;
1960ba2cbe9Sxc151355 	case DLADM_STATUS_LINKINVAL:
1970ba2cbe9Sxc151355 		s = "invalid link";
1980ba2cbe9Sxc151355 		break;
1990ba2cbe9Sxc151355 	case DLADM_STATUS_PROPRDONLY:
2000ba2cbe9Sxc151355 		s = "read-only property";
2010ba2cbe9Sxc151355 		break;
2020ba2cbe9Sxc151355 	case DLADM_STATUS_BADVALCNT:
2030ba2cbe9Sxc151355 		s = "invalid number of values";
2040ba2cbe9Sxc151355 		break;
2050ba2cbe9Sxc151355 	case DLADM_STATUS_DBNOTFOUND:
2060ba2cbe9Sxc151355 		s = "database not found";
2070ba2cbe9Sxc151355 		break;
2080ba2cbe9Sxc151355 	case DLADM_STATUS_DENIED:
2090ba2cbe9Sxc151355 		s = "permission denied";
2100ba2cbe9Sxc151355 		break;
2110ba2cbe9Sxc151355 	case DLADM_STATUS_IOERR:
2120ba2cbe9Sxc151355 		s = "I/O error";
2130ba2cbe9Sxc151355 		break;
214f4b3ec61Sdh155122 	case DLADM_STATUS_TEMPONLY:
215da14cebeSEric Cheng 		s = "change cannot be persistent";
216f4b3ec61Sdh155122 		break;
217f595a68aSyz147064 	case DLADM_STATUS_TIMEDOUT:
218f595a68aSyz147064 		s = "operation timed out";
219f595a68aSyz147064 		break;
220f595a68aSyz147064 	case DLADM_STATUS_ISCONN:
221f595a68aSyz147064 		s = "already connected";
222f595a68aSyz147064 		break;
223f595a68aSyz147064 	case DLADM_STATUS_NOTCONN:
224f595a68aSyz147064 		s = "not connected";
225f595a68aSyz147064 		break;
226f595a68aSyz147064 	case DLADM_STATUS_REPOSITORYINVAL:
227f595a68aSyz147064 		s = "invalid configuration repository";
228f595a68aSyz147064 		break;
229f595a68aSyz147064 	case DLADM_STATUS_MACADDRINVAL:
230f595a68aSyz147064 		s = "invalid MAC address";
231f595a68aSyz147064 		break;
232f595a68aSyz147064 	case DLADM_STATUS_KEYINVAL:
233f595a68aSyz147064 		s = "invalid key";
234f595a68aSyz147064 		break;
235843e1988Sjohnlev 	case DLADM_STATUS_INVALIDMACADDRLEN:
236843e1988Sjohnlev 		s = "invalid MAC address length";
237843e1988Sjohnlev 		break;
238843e1988Sjohnlev 	case DLADM_STATUS_INVALIDMACADDRTYPE:
239843e1988Sjohnlev 		s = "invalid MAC address type";
240843e1988Sjohnlev 		break;
241d62bc4baSyz147064 	case DLADM_STATUS_LINKBUSY:
242d62bc4baSyz147064 		s = "link busy";
243843e1988Sjohnlev 		break;
244d62bc4baSyz147064 	case DLADM_STATUS_VIDINVAL:
245d62bc4baSyz147064 		s = "invalid VLAN identifier";
246843e1988Sjohnlev 		break;
247d62bc4baSyz147064 	case DLADM_STATUS_TRYAGAIN:
248d62bc4baSyz147064 		s = "try again later";
249d62bc4baSyz147064 		break;
250d62bc4baSyz147064 	case DLADM_STATUS_NONOTIF:
251d62bc4baSyz147064 		s = "link notification is not supported";
252843e1988Sjohnlev 		break;
253da14cebeSEric Cheng 	case DLADM_STATUS_BADTIMEVAL:
254da14cebeSEric Cheng 		s = "invalid time range";
255da14cebeSEric Cheng 		break;
256da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACADDR:
257da14cebeSEric Cheng 		s = "invalid MAC address value";
258da14cebeSEric Cheng 		break;
259da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACADDRNIC:
260da14cebeSEric Cheng 		s = "MAC address reserved for use by underlying data-link";
261da14cebeSEric Cheng 		break;
262da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACADDRINUSE:
263da14cebeSEric Cheng 		s = "MAC address is already in use";
264da14cebeSEric Cheng 		break;
265da14cebeSEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTINVALID:
266da14cebeSEric Cheng 		s = "invalid factory MAC address slot";
267da14cebeSEric Cheng 		break;
268da14cebeSEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTUSED:
269da14cebeSEric Cheng 		s = "factory MAC address slot already used";
270da14cebeSEric Cheng 		break;
271da14cebeSEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTALLUSED:
272da14cebeSEric Cheng 		s = "all factory MAC address slots are in use";
273da14cebeSEric Cheng 		break;
274da14cebeSEric Cheng 	case DLADM_STATUS_MACFACTORYNOTSUP:
275da14cebeSEric Cheng 		s = "factory MAC address slots not supported";
276da14cebeSEric Cheng 		break;
277da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACPREFIX:
278da14cebeSEric Cheng 		s = "Invalid MAC address prefix value";
279da14cebeSEric Cheng 		break;
280da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACPREFIXLEN:
281da14cebeSEric Cheng 		s = "Invalid MAC address prefix length";
282da14cebeSEric Cheng 		break;
283c569ef53SMichael Lim 	case DLADM_STATUS_BADCPUID:
284da14cebeSEric Cheng 		s = "non-existent processor ID";
285da14cebeSEric Cheng 		break;
286da14cebeSEric Cheng 	case DLADM_STATUS_CPUERR:
287da14cebeSEric Cheng 		s = "could not determine processor status";
288da14cebeSEric Cheng 		break;
289da14cebeSEric Cheng 	case DLADM_STATUS_CPUNOTONLINE:
290da14cebeSEric Cheng 		s = "processor not online";
291da14cebeSEric Cheng 		break;
292c569ef53SMichael Lim 	case DLADM_STATUS_TOOMANYELEMENTS:
293c569ef53SMichael Lim 		s = "too many elements specified";
294c569ef53SMichael Lim 		break;
295c569ef53SMichael Lim 	case DLADM_STATUS_BADRANGE:
296c569ef53SMichael Lim 		s = "invalid range";
297c569ef53SMichael Lim 		break;
298da14cebeSEric Cheng 	case DLADM_STATUS_DB_NOTFOUND:
299da14cebeSEric Cheng 		s = "database not found";
300da14cebeSEric Cheng 		break;
301da14cebeSEric Cheng 	case DLADM_STATUS_DB_PARSE_ERR:
302da14cebeSEric Cheng 		s = "database parse error";
303da14cebeSEric Cheng 		break;
304da14cebeSEric Cheng 	case DLADM_STATUS_PROP_PARSE_ERR:
305da14cebeSEric Cheng 		s = "property parse error";
306da14cebeSEric Cheng 		break;
307da14cebeSEric Cheng 	case DLADM_STATUS_ATTR_PARSE_ERR:
308da14cebeSEric Cheng 		s = "attribute parse error";
309da14cebeSEric Cheng 		break;
310da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_DB_ERR:
311da14cebeSEric Cheng 		s = "flow database error";
312da14cebeSEric Cheng 		break;
313da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_DB_OPEN_ERR:
314da14cebeSEric Cheng 		s = "flow database open error";
315da14cebeSEric Cheng 		break;
316da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_DB_PARSE_ERR:
317da14cebeSEric Cheng 		s = "flow database parse error";
318da14cebeSEric Cheng 		break;
319da14cebeSEric Cheng 	case DLADM_STATUS_FLOWPROP_DB_PARSE_ERR:
320da14cebeSEric Cheng 		s = "flow property database parse error";
321da14cebeSEric Cheng 		break;
322da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_ADD_ERR:
323da14cebeSEric Cheng 		s = "flow add error";
324da14cebeSEric Cheng 		break;
325da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_WALK_ERR:
326da14cebeSEric Cheng 		s = "flow walk error";
327da14cebeSEric Cheng 		break;
328da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_IDENTICAL:
329da14cebeSEric Cheng 		s = "a flow with identical attributes exists";
330da14cebeSEric Cheng 		break;
331da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_INCOMPATIBLE:
332da14cebeSEric Cheng 		s = "flow(s) with incompatible attributes exists";
333da14cebeSEric Cheng 		break;
334da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_EXISTS:
335da14cebeSEric Cheng 		s = "link still has flows";
336da14cebeSEric Cheng 		break;
337da14cebeSEric Cheng 	case DLADM_STATUS_PERSIST_FLOW_EXISTS:
338da14cebeSEric Cheng 		s = "persistent flow with the same name exists";
339da14cebeSEric Cheng 		break;
340da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_IP:
341da14cebeSEric Cheng 		s = "invalid IP address";
342da14cebeSEric Cheng 		break;
343da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_PREFIXLEN:
344da14cebeSEric Cheng 		s = "invalid IP prefix length";
345da14cebeSEric Cheng 		break;
346da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_PROTOCOL:
347da14cebeSEric Cheng 		s = "invalid IP protocol";
348da14cebeSEric Cheng 		break;
349da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_PORT:
350da14cebeSEric Cheng 		s = "invalid port number";
351da14cebeSEric Cheng 		break;
352da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_DSF:
353da14cebeSEric Cheng 		s = "invalid dsfield";
354da14cebeSEric Cheng 		break;
355da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_DSFMASK:
356da14cebeSEric Cheng 		s = "invalid dsfield mask";
357da14cebeSEric Cheng 		break;
358da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_MACMARGIN:
359da14cebeSEric Cheng 		s = "MTU check failed, use lower MTU or -f option";
360da14cebeSEric Cheng 		break;
361da14cebeSEric Cheng 	case DLADM_STATUS_BADPROP:
362da14cebeSEric Cheng 		s = "invalid property";
363da14cebeSEric Cheng 		break;
364da14cebeSEric Cheng 	case DLADM_STATUS_MINMAXBW:
36580109390SMichael Lim 		s = "minimum value for maxbw is 1200K";
366da14cebeSEric Cheng 		break;
367da14cebeSEric Cheng 	case DLADM_STATUS_NO_HWRINGS:
368da14cebeSEric Cheng 		s = "request hw rings failed";
369da14cebeSEric Cheng 		break;
3704eaa4710SRishi Srivatsavai 	case DLADM_STATUS_PERMONLY:
3714eaa4710SRishi Srivatsavai 		s = "change must be persistent";
3724eaa4710SRishi Srivatsavai 		break;
3734eaa4710SRishi Srivatsavai 	case DLADM_STATUS_OPTMISSING:
3744eaa4710SRishi Srivatsavai 		s = "optional software not installed";
3754eaa4710SRishi Srivatsavai 		break;
3762b24ab6bSSebastien Roy 	case DLADM_STATUS_IPTUNTYPE:
3772b24ab6bSSebastien Roy 		s = "invalid IP tunnel type";
3782b24ab6bSSebastien Roy 		break;
3792b24ab6bSSebastien Roy 	case DLADM_STATUS_IPTUNTYPEREQD:
3802b24ab6bSSebastien Roy 		s = "IP tunnel type required";
3812b24ab6bSSebastien Roy 		break;
3822b24ab6bSSebastien Roy 	case DLADM_STATUS_BADIPTUNLADDR:
3832b24ab6bSSebastien Roy 		s = "invalid local IP tunnel address";
3842b24ab6bSSebastien Roy 		break;
3852b24ab6bSSebastien Roy 	case DLADM_STATUS_BADIPTUNRADDR:
3862b24ab6bSSebastien Roy 		s = "invalid remote IP tunnel address";
3872b24ab6bSSebastien Roy 		break;
3882b24ab6bSSebastien Roy 	case DLADM_STATUS_ADDRINUSE:
3892b24ab6bSSebastien Roy 		s = "address already in use";
3902b24ab6bSSebastien Roy 		break;
3910dc2366fSVenugopal Iyer 	case DLADM_STATUS_POOLCPU:
3920dc2366fSVenugopal Iyer 		s = "pool and cpus property are mutually exclusive";
3930dc2366fSVenugopal Iyer 		break;
3941cfa752fSRamaswamy Tummala 	case DLADM_STATUS_INVALID_PORT_INSTANCE:
3951cfa752fSRamaswamy Tummala 		s = "invalid IB phys link";
3961cfa752fSRamaswamy Tummala 		break;
3971cfa752fSRamaswamy Tummala 	case DLADM_STATUS_PORT_IS_DOWN:
3981cfa752fSRamaswamy Tummala 		s = "port is down";
3991cfa752fSRamaswamy Tummala 		break;
4001cfa752fSRamaswamy Tummala 	case DLADM_STATUS_PARTITION_EXISTS:
4011cfa752fSRamaswamy Tummala 		s = "partition already exists";
4021cfa752fSRamaswamy Tummala 		break;
4031cfa752fSRamaswamy Tummala 	case DLADM_STATUS_PKEY_NOT_PRESENT:
4041cfa752fSRamaswamy Tummala 		s = "PKEY is not present on the port";
4051cfa752fSRamaswamy Tummala 		break;
4061cfa752fSRamaswamy Tummala 	case DLADM_STATUS_INVALID_PKEY:
4071cfa752fSRamaswamy Tummala 		s = "invalid PKEY";
4081cfa752fSRamaswamy Tummala 		break;
4091cfa752fSRamaswamy Tummala 	case DLADM_STATUS_NO_IB_HW_RESOURCE:
4101cfa752fSRamaswamy Tummala 		s = "IB internal resource not available";
4111cfa752fSRamaswamy Tummala 		break;
4121cfa752fSRamaswamy Tummala 	case DLADM_STATUS_INVALID_PKEY_TBL_SIZE:
4131cfa752fSRamaswamy Tummala 		s = "invalid PKEY table size";
4141cfa752fSRamaswamy Tummala 		break;
41580109390SMichael Lim 	case DLADM_STATUS_PORT_NOPROTO:
41680109390SMichael Lim 		s = "local or remote port requires transport";
41780109390SMichael Lim 		break;
418*a776d98eSRobert Mustacchi 	case DLADM_STATUS_INVALID_MTU:
419*a776d98eSRobert Mustacchi 		s = "MTU check failed, MTU outside of device's supported range";
420*a776d98eSRobert Mustacchi 		break;
4210ba2cbe9Sxc151355 	default:
42233343a97Smeem 		s = "<unknown error>";
42333343a97Smeem 		break;
4240ba2cbe9Sxc151355 	}
42533343a97Smeem 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
4260ba2cbe9Sxc151355 	return (buf);
4270ba2cbe9Sxc151355 }
4280ba2cbe9Sxc151355 
4290ba2cbe9Sxc151355 /*
4300ba2cbe9Sxc151355  * Convert a unix errno to a dladm_status_t.
4310ba2cbe9Sxc151355  * We only convert errnos that are likely to be encountered. All others
4320ba2cbe9Sxc151355  * are mapped to DLADM_STATUS_FAILED.
4330ba2cbe9Sxc151355  */
4340ba2cbe9Sxc151355 dladm_status_t
dladm_errno2status(int err)4350ba2cbe9Sxc151355 dladm_errno2status(int err)
4360ba2cbe9Sxc151355 {
4370ba2cbe9Sxc151355 	switch (err) {
438e7801d59Ssowmini 	case 0:
439e7801d59Ssowmini 		return (DLADM_STATUS_OK);
4400ba2cbe9Sxc151355 	case EINVAL:
4410ba2cbe9Sxc151355 		return (DLADM_STATUS_BADARG);
4420ba2cbe9Sxc151355 	case EEXIST:
4430ba2cbe9Sxc151355 		return (DLADM_STATUS_EXIST);
4440ba2cbe9Sxc151355 	case ENOENT:
4450ba2cbe9Sxc151355 		return (DLADM_STATUS_NOTFOUND);
4460ba2cbe9Sxc151355 	case ENOSPC:
4470ba2cbe9Sxc151355 		return (DLADM_STATUS_TOOSMALL);
4480ba2cbe9Sxc151355 	case ENOMEM:
4490ba2cbe9Sxc151355 		return (DLADM_STATUS_NOMEM);
4500ba2cbe9Sxc151355 	case ENOTSUP:
4510ba2cbe9Sxc151355 		return (DLADM_STATUS_NOTSUP);
452d62bc4baSyz147064 	case ENETDOWN:
453d62bc4baSyz147064 		return (DLADM_STATUS_NONOTIF);
4540ba2cbe9Sxc151355 	case EACCES:
455eae72b5bSSebastien Roy 	case EPERM:
4560ba2cbe9Sxc151355 		return (DLADM_STATUS_DENIED);
4570ba2cbe9Sxc151355 	case EIO:
4580ba2cbe9Sxc151355 		return (DLADM_STATUS_IOERR);
459843e1988Sjohnlev 	case EBUSY:
460d62bc4baSyz147064 		return (DLADM_STATUS_LINKBUSY);
461d62bc4baSyz147064 	case EAGAIN:
462d62bc4baSyz147064 		return (DLADM_STATUS_TRYAGAIN);
463da14cebeSEric Cheng 	case ENOTEMPTY:
464da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_EXISTS);
465da14cebeSEric Cheng 	case EOPNOTSUPP:
466da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_INCOMPATIBLE);
467da14cebeSEric Cheng 	case EALREADY:
468da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_IDENTICAL);
4692b24ab6bSSebastien Roy 	case EADDRINUSE:
4702b24ab6bSSebastien Roy 		return (DLADM_STATUS_ADDRINUSE);
4710ba2cbe9Sxc151355 	default:
4720ba2cbe9Sxc151355 		return (DLADM_STATUS_FAILED);
4730ba2cbe9Sxc151355 	}
4740ba2cbe9Sxc151355 }
4750ba2cbe9Sxc151355 
47663a6526dSMichael Lim boolean_t
dladm_str2interval(char * oarg,uint32_t * interval)47763a6526dSMichael Lim dladm_str2interval(char *oarg, uint32_t *interval)
47863a6526dSMichael Lim {
47963a6526dSMichael Lim 	int		val;
48063a6526dSMichael Lim 	char		*endp = NULL;
48163a6526dSMichael Lim 
48263a6526dSMichael Lim 	errno = 0;
48363a6526dSMichael Lim 	val = strtol(oarg, &endp, 10);
48463a6526dSMichael Lim 	if (errno != 0 || val <= 0 || *endp != '\0')
48563a6526dSMichael Lim 		return (B_FALSE);
48663a6526dSMichael Lim 
48763a6526dSMichael Lim 	*interval = val;
48863a6526dSMichael Lim 
48963a6526dSMichael Lim 	return (B_TRUE);
49063a6526dSMichael Lim }
49163a6526dSMichael Lim 
492da14cebeSEric Cheng dladm_status_t
dladm_str2bw(char * oarg,uint64_t * bw)493da14cebeSEric Cheng dladm_str2bw(char *oarg, uint64_t *bw)
494da14cebeSEric Cheng {
495da14cebeSEric Cheng 	char		*endp = NULL;
496da14cebeSEric Cheng 	int64_t		n;
497da14cebeSEric Cheng 	int		mult = 1;
498da14cebeSEric Cheng 
499da14cebeSEric Cheng 	n = strtoull(oarg, &endp, 10);
500da14cebeSEric Cheng 
501da14cebeSEric Cheng 	if ((errno != 0) || (strlen(endp) > 1))
502da14cebeSEric Cheng 		return (DLADM_STATUS_BADARG);
503da14cebeSEric Cheng 
504da14cebeSEric Cheng 	if (n < 0)
505da14cebeSEric Cheng 		return (DLADM_STATUS_BADVAL);
506da14cebeSEric Cheng 
507da14cebeSEric Cheng 	switch (*endp) {
508da14cebeSEric Cheng 	case 'k':
509da14cebeSEric Cheng 	case 'K':
510da14cebeSEric Cheng 		mult = 1000;
511da14cebeSEric Cheng 		break;
512da14cebeSEric Cheng 	case 'm':
513da14cebeSEric Cheng 	case 'M':
514da14cebeSEric Cheng 	case '\0':
515da14cebeSEric Cheng 		mult = 1000000;
516da14cebeSEric Cheng 		break;
517da14cebeSEric Cheng 	case 'g':
518da14cebeSEric Cheng 	case 'G':
519da14cebeSEric Cheng 		mult = 1000000000;
520da14cebeSEric Cheng 		break;
521da14cebeSEric Cheng 	case '%':
522da14cebeSEric Cheng 		/*
523da14cebeSEric Cheng 		 * percentages not supported for now,
524da14cebeSEric Cheng 		 * see RFE 6540675
525da14cebeSEric Cheng 		 */
526da14cebeSEric Cheng 		return (DLADM_STATUS_NOTSUP);
527da14cebeSEric Cheng 	default:
528da14cebeSEric Cheng 		return (DLADM_STATUS_BADVAL);
529da14cebeSEric Cheng 	}
530da14cebeSEric Cheng 
531da14cebeSEric Cheng 	*bw = n * mult;
532da14cebeSEric Cheng 
533da14cebeSEric Cheng 	/* check for overflow */
534da14cebeSEric Cheng 	if (*bw / mult != n)
535da14cebeSEric Cheng 		return (DLADM_STATUS_BADARG);
536da14cebeSEric Cheng 
537da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
538da14cebeSEric Cheng }
539da14cebeSEric Cheng 
540da14cebeSEric Cheng /*
5414a3b1d5bSBart Coddens  * Convert bandwidth in bps to a string in Mbps.  For values greater
5424a3b1d5bSBart Coddens  * than 1Mbps or 1000000, print a whole Mbps value.  For values that
543da14cebeSEric Cheng  * have fractional Mbps in whole Kbps, print the bandwidth in a manner
5444a3b1d5bSBart Coddens  * similar to a floating point format.
545da14cebeSEric Cheng  *
546da14cebeSEric Cheng  *        bps       string
547da14cebeSEric Cheng  *          0            0
548da14cebeSEric Cheng  *        100            0
549da14cebeSEric Cheng  *       2000        0.002
550da14cebeSEric Cheng  *     431000        0.431
551da14cebeSEric Cheng  *    1000000            1
552da14cebeSEric Cheng  *    1030000        1.030
553da14cebeSEric Cheng  *  100000000          100
554da14cebeSEric Cheng  */
555da14cebeSEric Cheng const char *
dladm_bw2str(int64_t bw,char * buf)556da14cebeSEric Cheng dladm_bw2str(int64_t bw, char *buf)
557da14cebeSEric Cheng {
558da14cebeSEric Cheng 	int kbps, mbps;
559da14cebeSEric Cheng 
560da14cebeSEric Cheng 	kbps = (bw%1000000)/1000;
561da14cebeSEric Cheng 	mbps = bw/1000000;
562da14cebeSEric Cheng 	if (kbps != 0) {
563da14cebeSEric Cheng 		if (mbps == 0)
564da14cebeSEric Cheng 			(void) snprintf(buf, DLADM_STRSIZE, "0.%03u", kbps);
565da14cebeSEric Cheng 		else
566da14cebeSEric Cheng 			(void) snprintf(buf, DLADM_STRSIZE, "%5u.%03u", mbps,
567da14cebeSEric Cheng 			    kbps);
568da14cebeSEric Cheng 	} else {
569da14cebeSEric Cheng 		(void) snprintf(buf, DLADM_STRSIZE, "%5u", mbps);
570da14cebeSEric Cheng 	}
571da14cebeSEric Cheng 
572da14cebeSEric Cheng 	return (buf);
573da14cebeSEric Cheng }
574da14cebeSEric Cheng 
5750ba2cbe9Sxc151355 #define	LOCK_DB_PERMS	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
5760ba2cbe9Sxc151355 
5770ba2cbe9Sxc151355 static int
i_dladm_lock_db(const char * lock_file,short type)5780ba2cbe9Sxc151355 i_dladm_lock_db(const char *lock_file, short type)
5790ba2cbe9Sxc151355 {
5800ba2cbe9Sxc151355 	int	lock_fd;
5810ba2cbe9Sxc151355 	struct	flock lock;
5820ba2cbe9Sxc151355 
5830ba2cbe9Sxc151355 	if ((lock_fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC,
5840ba2cbe9Sxc151355 	    LOCK_DB_PERMS)) < 0)
5850ba2cbe9Sxc151355 		return (-1);
5860ba2cbe9Sxc151355 
5870ba2cbe9Sxc151355 	lock.l_type = type;
5880ba2cbe9Sxc151355 	lock.l_whence = SEEK_SET;
5890ba2cbe9Sxc151355 	lock.l_start = 0;
5900ba2cbe9Sxc151355 	lock.l_len = 0;
5910ba2cbe9Sxc151355 
5920ba2cbe9Sxc151355 	if (fcntl(lock_fd, F_SETLKW, &lock) < 0) {
5930ba2cbe9Sxc151355 		int err = errno;
5940ba2cbe9Sxc151355 
5950ba2cbe9Sxc151355 		(void) close(lock_fd);
5960ba2cbe9Sxc151355 		(void) unlink(lock_file);
5970ba2cbe9Sxc151355 		errno = err;
5980ba2cbe9Sxc151355 		return (-1);
5990ba2cbe9Sxc151355 	}
6000ba2cbe9Sxc151355 	return (lock_fd);
6010ba2cbe9Sxc151355 }
6020ba2cbe9Sxc151355 
6030ba2cbe9Sxc151355 static void
i_dladm_unlock_db(const char * lock_file,int fd)6040ba2cbe9Sxc151355 i_dladm_unlock_db(const char *lock_file, int fd)
6050ba2cbe9Sxc151355 {
6060ba2cbe9Sxc151355 	struct flock lock;
6070ba2cbe9Sxc151355 
6080ba2cbe9Sxc151355 	if (fd < 0)
6090ba2cbe9Sxc151355 		return;
6100ba2cbe9Sxc151355 
6110ba2cbe9Sxc151355 	lock.l_type = F_UNLCK;
6120ba2cbe9Sxc151355 	lock.l_whence = SEEK_SET;
6130ba2cbe9Sxc151355 	lock.l_start = 0;
6140ba2cbe9Sxc151355 	lock.l_len = 0;
6150ba2cbe9Sxc151355 
6160ba2cbe9Sxc151355 	(void) fcntl(fd, F_SETLKW, &lock);
6170ba2cbe9Sxc151355 	(void) close(fd);
6180ba2cbe9Sxc151355 	(void) unlink(lock_file);
6190ba2cbe9Sxc151355 }
6200ba2cbe9Sxc151355 
621d62bc4baSyz147064 /*
622d62bc4baSyz147064  * Given a link class, returns its class string.
623d62bc4baSyz147064  */
624d62bc4baSyz147064 const char *
dladm_class2str(datalink_class_t class,char * buf)625d62bc4baSyz147064 dladm_class2str(datalink_class_t class, char *buf)
626d62bc4baSyz147064 {
627d62bc4baSyz147064 	const char *s;
628d62bc4baSyz147064 
629d62bc4baSyz147064 	switch (class) {
630d62bc4baSyz147064 	case DATALINK_CLASS_PHYS:
631d62bc4baSyz147064 		s = "phys";
632d62bc4baSyz147064 		break;
633d62bc4baSyz147064 	case DATALINK_CLASS_VLAN:
634d62bc4baSyz147064 		s = "vlan";
635d62bc4baSyz147064 		break;
636d62bc4baSyz147064 	case DATALINK_CLASS_AGGR:
637d62bc4baSyz147064 		s = "aggr";
638d62bc4baSyz147064 		break;
639d62bc4baSyz147064 	case DATALINK_CLASS_VNIC:
640d62bc4baSyz147064 		s = "vnic";
641d62bc4baSyz147064 		break;
642da14cebeSEric Cheng 	case DATALINK_CLASS_ETHERSTUB:
643da14cebeSEric Cheng 		s = "etherstub";
644da14cebeSEric Cheng 		break;
6452b24ab6bSSebastien Roy 	case DATALINK_CLASS_IPTUN:
6462b24ab6bSSebastien Roy 		s = "iptun";
6472b24ab6bSSebastien Roy 		break;
648b509e89bSRishi Srivatsavai 	case DATALINK_CLASS_SIMNET:
649b509e89bSRishi Srivatsavai 		s = "simnet";
650b509e89bSRishi Srivatsavai 		break;
6514eaa4710SRishi Srivatsavai 	case DATALINK_CLASS_BRIDGE:
6524eaa4710SRishi Srivatsavai 		s = "bridge";
6534eaa4710SRishi Srivatsavai 		break;
6541cfa752fSRamaswamy Tummala 	case DATALINK_CLASS_PART:
6551cfa752fSRamaswamy Tummala 		s = "part";
6561cfa752fSRamaswamy Tummala 		break;
657d62bc4baSyz147064 	default:
658d62bc4baSyz147064 		s = "unknown";
659d62bc4baSyz147064 		break;
660d62bc4baSyz147064 	}
661d62bc4baSyz147064 
662d62bc4baSyz147064 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
663d62bc4baSyz147064 	return (buf);
664d62bc4baSyz147064 }
665d62bc4baSyz147064 
666d62bc4baSyz147064 /*
667d62bc4baSyz147064  * Given a physical link media type, returns its media type string.
668d62bc4baSyz147064  */
669d62bc4baSyz147064 const char *
dladm_media2str(uint32_t media,char * buf)670d62bc4baSyz147064 dladm_media2str(uint32_t media, char *buf)
671d62bc4baSyz147064 {
672b509e89bSRishi Srivatsavai 	const char *s = "--";
673b509e89bSRishi Srivatsavai 	media_type_t *mt;
674b509e89bSRishi Srivatsavai 	int idx;
675d62bc4baSyz147064 
676b509e89bSRishi Srivatsavai 	for (idx = 0; idx < MEDIATYPECOUNT; idx++) {
677b509e89bSRishi Srivatsavai 		mt = media_type_table + idx;
678b509e89bSRishi Srivatsavai 		if (mt->media_type == media) {
679b509e89bSRishi Srivatsavai 			s = mt->media_type_str;
680d62bc4baSyz147064 			break;
681b509e89bSRishi Srivatsavai 		}
682d62bc4baSyz147064 	}
683d62bc4baSyz147064 
684d62bc4baSyz147064 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
685d62bc4baSyz147064 	return (buf);
686d62bc4baSyz147064 }
687d62bc4baSyz147064 
688b509e89bSRishi Srivatsavai /*
689b509e89bSRishi Srivatsavai  * Given a physical link media type string, returns its media type constant.
690b509e89bSRishi Srivatsavai  */
691b509e89bSRishi Srivatsavai uint32_t
dladm_str2media(const char * buf)692b509e89bSRishi Srivatsavai dladm_str2media(const char *buf)
693b509e89bSRishi Srivatsavai {
694b509e89bSRishi Srivatsavai 	media_type_t *mt;
695b509e89bSRishi Srivatsavai 	int idx;
696b509e89bSRishi Srivatsavai 
697b509e89bSRishi Srivatsavai 	for (idx = 0; idx < MEDIATYPECOUNT; idx++) {
698b509e89bSRishi Srivatsavai 		mt = media_type_table + idx;
699b509e89bSRishi Srivatsavai 		if (strcasecmp(buf, mt->media_type_str) == 0)
700b509e89bSRishi Srivatsavai 			return (mt->media_type);
701b509e89bSRishi Srivatsavai 	}
702b509e89bSRishi Srivatsavai 
703b509e89bSRishi Srivatsavai 	return (DL_OTHER);
704b509e89bSRishi Srivatsavai }
705b509e89bSRishi Srivatsavai 
7060ba2cbe9Sxc151355 dladm_status_t
i_dladm_rw_db(dladm_handle_t handle,const char * db_file,mode_t db_perms,dladm_status_t (* process_db)(dladm_handle_t,void *,FILE *,FILE *),void * arg,boolean_t writeop)7074ac67f02SAnurag S. Maskey i_dladm_rw_db(dladm_handle_t handle, const char *db_file, mode_t db_perms,
7084ac67f02SAnurag S. Maskey     dladm_status_t (*process_db)(dladm_handle_t, void *, FILE *, FILE *),
7090ba2cbe9Sxc151355     void *arg, boolean_t writeop)
7100ba2cbe9Sxc151355 {
7110ba2cbe9Sxc151355 	dladm_status_t	status = DLADM_STATUS_OK;
7120ba2cbe9Sxc151355 	FILE		*fp, *nfp = NULL;
7130ba2cbe9Sxc151355 	char		lock[MAXPATHLEN];
7140ba2cbe9Sxc151355 	char		file[MAXPATHLEN];
7150ba2cbe9Sxc151355 	char		newfile[MAXPATHLEN];
7160ba2cbe9Sxc151355 	char		*db_basename;
7170ba2cbe9Sxc151355 	int		nfd, lock_fd;
7180ba2cbe9Sxc151355 
7190ba2cbe9Sxc151355 	/*
7200ba2cbe9Sxc151355 	 * If we are called from a boot script such as net-physical,
7210ba2cbe9Sxc151355 	 * it's quite likely that the root fs is still not writable.
7220ba2cbe9Sxc151355 	 * For this case, it's ok for the lock creation to fail since
7230ba2cbe9Sxc151355 	 * no one else could be accessing our configuration file.
7240ba2cbe9Sxc151355 	 */
7250ba2cbe9Sxc151355 	db_basename = strrchr(db_file, '/');
7260ba2cbe9Sxc151355 	if (db_basename == NULL || db_basename[1] == '\0')
7270ba2cbe9Sxc151355 		return (dladm_errno2status(EINVAL));
7280ba2cbe9Sxc151355 	db_basename++;
7290ba2cbe9Sxc151355 	(void) snprintf(lock, MAXPATHLEN, "/tmp/%s.lock", db_basename);
7300ba2cbe9Sxc151355 	if ((lock_fd = i_dladm_lock_db
7310ba2cbe9Sxc151355 	    (lock, (writeop ? F_WRLCK : F_RDLCK))) < 0 && errno != EROFS)
7320ba2cbe9Sxc151355 		return (dladm_errno2status(errno));
7330ba2cbe9Sxc151355 
7340ba2cbe9Sxc151355 	(void) snprintf(file, MAXPATHLEN, "%s/%s", dladm_rootdir, db_file);
7350ba2cbe9Sxc151355 	if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) {
7360ba2cbe9Sxc151355 		int	err = errno;
7370ba2cbe9Sxc151355 
7380ba2cbe9Sxc151355 		i_dladm_unlock_db(lock, lock_fd);
7390ba2cbe9Sxc151355 		if (err == ENOENT)
7400ba2cbe9Sxc151355 			return (DLADM_STATUS_DBNOTFOUND);
7410ba2cbe9Sxc151355 
7420ba2cbe9Sxc151355 		return (dladm_errno2status(err));
7430ba2cbe9Sxc151355 	}
7440ba2cbe9Sxc151355 
7450ba2cbe9Sxc151355 	if (writeop) {
7460ba2cbe9Sxc151355 		(void) snprintf(newfile, MAXPATHLEN, "%s/%s.new",
7470ba2cbe9Sxc151355 		    dladm_rootdir, db_file);
7480ba2cbe9Sxc151355 		if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC,
7490ba2cbe9Sxc151355 		    db_perms)) < 0) {
7500ba2cbe9Sxc151355 			(void) fclose(fp);
7510ba2cbe9Sxc151355 			i_dladm_unlock_db(lock, lock_fd);
7520ba2cbe9Sxc151355 			return (dladm_errno2status(errno));
7530ba2cbe9Sxc151355 		}
7540ba2cbe9Sxc151355 
7550ba2cbe9Sxc151355 		if ((nfp = fdopen(nfd, "w")) == NULL) {
7560ba2cbe9Sxc151355 			(void) close(nfd);
7570ba2cbe9Sxc151355 			(void) fclose(fp);
7580ba2cbe9Sxc151355 			(void) unlink(newfile);
7590ba2cbe9Sxc151355 			i_dladm_unlock_db(lock, lock_fd);
7600ba2cbe9Sxc151355 			return (dladm_errno2status(errno));
7610ba2cbe9Sxc151355 		}
7620ba2cbe9Sxc151355 	}
7634ac67f02SAnurag S. Maskey 	status = (*process_db)(handle, arg, fp, nfp);
7640ba2cbe9Sxc151355 	if (!writeop || status != DLADM_STATUS_OK)
7650ba2cbe9Sxc151355 		goto done;
7660ba2cbe9Sxc151355 
7676ba597c5SAnurag S. Maskey 	/* Set permissions on file to db_perms */
7686ba597c5SAnurag S. Maskey 	if (fchmod(nfd, db_perms) < 0) {
7690ba2cbe9Sxc151355 		status = dladm_errno2status(errno);
7700ba2cbe9Sxc151355 		goto done;
7710ba2cbe9Sxc151355 	}
7726ba597c5SAnurag S. Maskey 
7736ba597c5SAnurag S. Maskey 	/*
7746ba597c5SAnurag S. Maskey 	 * Configuration files need to be owned by the 'dladm' user and
7756ba597c5SAnurag S. Maskey 	 * 'netadm' group.
7766ba597c5SAnurag S. Maskey 	 */
7776ba597c5SAnurag S. Maskey 	if (fchown(nfd, UID_DLADM, GID_NETADM) < 0) {
7786ba597c5SAnurag S. Maskey 		status = dladm_errno2status(errno);
7796ba597c5SAnurag S. Maskey 		goto done;
7800ba2cbe9Sxc151355 	}
7810ba2cbe9Sxc151355 
7820ba2cbe9Sxc151355 	if (fflush(nfp) == EOF) {
7830ba2cbe9Sxc151355 		status = dladm_errno2status(errno);
7840ba2cbe9Sxc151355 		goto done;
7850ba2cbe9Sxc151355 	}
7860ba2cbe9Sxc151355 	(void) fclose(fp);
7870ba2cbe9Sxc151355 	(void) fclose(nfp);
7880ba2cbe9Sxc151355 
7890ba2cbe9Sxc151355 	if (rename(newfile, file) < 0) {
7900ba2cbe9Sxc151355 		(void) unlink(newfile);
7910ba2cbe9Sxc151355 		i_dladm_unlock_db(lock, lock_fd);
7920ba2cbe9Sxc151355 		return (dladm_errno2status(errno));
7930ba2cbe9Sxc151355 	}
7940ba2cbe9Sxc151355 
7950ba2cbe9Sxc151355 	i_dladm_unlock_db(lock, lock_fd);
7960ba2cbe9Sxc151355 	return (DLADM_STATUS_OK);
7970ba2cbe9Sxc151355 
7980ba2cbe9Sxc151355 done:
7990ba2cbe9Sxc151355 	if (nfp != NULL) {
8000ba2cbe9Sxc151355 		(void) fclose(nfp);
8010ba2cbe9Sxc151355 		if (status != DLADM_STATUS_OK)
8020ba2cbe9Sxc151355 			(void) unlink(newfile);
8030ba2cbe9Sxc151355 	}
8040ba2cbe9Sxc151355 	(void) fclose(fp);
8050ba2cbe9Sxc151355 	i_dladm_unlock_db(lock, lock_fd);
8060ba2cbe9Sxc151355 	return (status);
8070ba2cbe9Sxc151355 }
8080ba2cbe9Sxc151355 
8090ba2cbe9Sxc151355 dladm_status_t
dladm_set_rootdir(const char * rootdir)8100ba2cbe9Sxc151355 dladm_set_rootdir(const char *rootdir)
8110ba2cbe9Sxc151355 {
8120ba2cbe9Sxc151355 	DIR	*dp;
8130ba2cbe9Sxc151355 
8140ba2cbe9Sxc151355 	if (rootdir == NULL || *rootdir != '/' ||
8150ba2cbe9Sxc151355 	    (dp = opendir(rootdir)) == NULL)
8160ba2cbe9Sxc151355 		return (DLADM_STATUS_BADARG);
8170ba2cbe9Sxc151355 
8180ba2cbe9Sxc151355 	(void) strncpy(dladm_rootdir, rootdir, MAXPATHLEN);
8190ba2cbe9Sxc151355 	(void) closedir(dp);
8200ba2cbe9Sxc151355 	return (DLADM_STATUS_OK);
8210ba2cbe9Sxc151355 }
822d62bc4baSyz147064 
823d62bc4baSyz147064 boolean_t
dladm_valid_linkname(const char * link)824d62bc4baSyz147064 dladm_valid_linkname(const char *link)
825d62bc4baSyz147064 {
826d62bc4baSyz147064 	size_t		len = strlen(link);
827d62bc4baSyz147064 	const char	*cp;
828d62bc4baSyz147064 
8292b24ab6bSSebastien Roy 	if (len >= MAXLINKNAMELEN)
830d62bc4baSyz147064 		return (B_FALSE);
831d62bc4baSyz147064 
832d62bc4baSyz147064 	/*
833d62bc4baSyz147064 	 * The link name cannot start with a digit and must end with a digit.
834d62bc4baSyz147064 	 */
835d62bc4baSyz147064 	if ((isdigit(link[0]) != 0) || (isdigit(link[len - 1]) == 0))
836d62bc4baSyz147064 		return (B_FALSE);
837d62bc4baSyz147064 
838d62bc4baSyz147064 	/*
839d62bc4baSyz147064 	 * The legal characters in a link name are:
8402b24ab6bSSebastien Roy 	 * alphanumeric (a-z,  A-Z,  0-9), underscore ('_'), and '.'.
841d62bc4baSyz147064 	 */
842d62bc4baSyz147064 	for (cp = link; *cp != '\0'; cp++) {
8432b24ab6bSSebastien Roy 		if ((isalnum(*cp) == 0) && (*cp != '_') && (*cp != '.'))
844d62bc4baSyz147064 			return (B_FALSE);
845d62bc4baSyz147064 	}
846d62bc4baSyz147064 
847d62bc4baSyz147064 	return (B_TRUE);
848d62bc4baSyz147064 }
849da14cebeSEric Cheng 
850da14cebeSEric Cheng /*
851da14cebeSEric Cheng  * Convert priority string to a value.
852da14cebeSEric Cheng  */
853da14cebeSEric Cheng dladm_status_t
dladm_str2pri(char * token,mac_priority_level_t * pri)854da14cebeSEric Cheng dladm_str2pri(char *token, mac_priority_level_t *pri)
855da14cebeSEric Cheng {
856da14cebeSEric Cheng 	if (strlen(token) == strlen("low") &&
857da14cebeSEric Cheng 	    strncasecmp(token, "low", strlen("low")) == 0) {
858da14cebeSEric Cheng 		*pri = MPL_LOW;
859da14cebeSEric Cheng 	} else if (strlen(token) == strlen("medium") &&
860da14cebeSEric Cheng 	    strncasecmp(token, "medium", strlen("medium")) == 0) {
861da14cebeSEric Cheng 		*pri = MPL_MEDIUM;
862da14cebeSEric Cheng 	} else if (strlen(token) == strlen("high") &&
863da14cebeSEric Cheng 	    strncasecmp(token, "high", strlen("high")) == 0) {
864da14cebeSEric Cheng 		*pri = MPL_HIGH;
865da14cebeSEric Cheng 	} else {
866da14cebeSEric Cheng 		return (DLADM_STATUS_BADVAL);
867da14cebeSEric Cheng 	}
868da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
869da14cebeSEric Cheng }
870da14cebeSEric Cheng 
871da14cebeSEric Cheng /*
872da14cebeSEric Cheng  * Convert priority value to a string.
873da14cebeSEric Cheng  */
874da14cebeSEric Cheng const char *
dladm_pri2str(mac_priority_level_t pri,char * buf)875da14cebeSEric Cheng dladm_pri2str(mac_priority_level_t pri, char *buf)
876da14cebeSEric Cheng {
877da14cebeSEric Cheng 	const char	*s;
878da14cebeSEric Cheng 
879da14cebeSEric Cheng 	switch (pri) {
880da14cebeSEric Cheng 	case MPL_LOW:
881da14cebeSEric Cheng 		s = "low";
882da14cebeSEric Cheng 		break;
883da14cebeSEric Cheng 	case MPL_MEDIUM:
884da14cebeSEric Cheng 		s = "medium";
885da14cebeSEric Cheng 		break;
886da14cebeSEric Cheng 	case MPL_HIGH:
887da14cebeSEric Cheng 		s = "high";
888da14cebeSEric Cheng 		break;
889da14cebeSEric Cheng 	default:
890da14cebeSEric Cheng 		s = "--";
891da14cebeSEric Cheng 		break;
892da14cebeSEric Cheng 	}
893da14cebeSEric Cheng 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
894da14cebeSEric Cheng 	return (buf);
895da14cebeSEric Cheng }
896da14cebeSEric Cheng 
89725ec3e3dSEric Cheng /*
89825ec3e3dSEric Cheng  * Convert protect string to a value.
89925ec3e3dSEric Cheng  */
90025ec3e3dSEric Cheng dladm_status_t
dladm_str2protect(char * token,uint32_t * ptype)90125ec3e3dSEric Cheng dladm_str2protect(char *token, uint32_t *ptype)
90225ec3e3dSEric Cheng {
90325ec3e3dSEric Cheng 	link_protect_t	*lp;
90425ec3e3dSEric Cheng 	int		i;
90525ec3e3dSEric Cheng 
90625ec3e3dSEric Cheng 	for (i = 0; i < LPTYPES; i++) {
90725ec3e3dSEric Cheng 		lp = &link_protect_types[i];
90825ec3e3dSEric Cheng 		if (strcmp(token, lp->lp_name) == 0) {
90925ec3e3dSEric Cheng 			*ptype = lp->lp_type;
91025ec3e3dSEric Cheng 			return (DLADM_STATUS_OK);
91125ec3e3dSEric Cheng 		}
91225ec3e3dSEric Cheng 	}
91325ec3e3dSEric Cheng 	return (DLADM_STATUS_BADVAL);
91425ec3e3dSEric Cheng }
91525ec3e3dSEric Cheng 
91625ec3e3dSEric Cheng /*
91725ec3e3dSEric Cheng  * Convert protect value to a string.
91825ec3e3dSEric Cheng  */
91925ec3e3dSEric Cheng const char *
dladm_protect2str(uint32_t ptype,char * buf)92025ec3e3dSEric Cheng dladm_protect2str(uint32_t ptype, char *buf)
92125ec3e3dSEric Cheng {
92225ec3e3dSEric Cheng 	const char	*s = "--";
92325ec3e3dSEric Cheng 	link_protect_t	*lp;
92425ec3e3dSEric Cheng 	int		i;
92525ec3e3dSEric Cheng 
92625ec3e3dSEric Cheng 	for (i = 0; i < LPTYPES; i++) {
92725ec3e3dSEric Cheng 		lp = &link_protect_types[i];
92825ec3e3dSEric Cheng 		if (lp->lp_type == ptype) {
92925ec3e3dSEric Cheng 			s = lp->lp_name;
93025ec3e3dSEric Cheng 			break;
93125ec3e3dSEric Cheng 		}
93225ec3e3dSEric Cheng 	}
93325ec3e3dSEric Cheng 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
93425ec3e3dSEric Cheng 	return (buf);
93525ec3e3dSEric Cheng }
93625ec3e3dSEric Cheng 
93725ec3e3dSEric Cheng /*
93825ec3e3dSEric Cheng  * Convert an IPv4 address to/from a string.
93925ec3e3dSEric Cheng  */
94025ec3e3dSEric Cheng const char *
dladm_ipv4addr2str(void * addr,char * buf)94125ec3e3dSEric Cheng dladm_ipv4addr2str(void *addr, char *buf)
94225ec3e3dSEric Cheng {
94325ec3e3dSEric Cheng 	if (inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN) == NULL)
9440dc2366fSVenugopal Iyer 		buf[0] = '\0';
94525ec3e3dSEric Cheng 
94625ec3e3dSEric Cheng 	return (buf);
94725ec3e3dSEric Cheng }
94825ec3e3dSEric Cheng 
94925ec3e3dSEric Cheng dladm_status_t
dladm_str2ipv4addr(char * token,void * addr)95025ec3e3dSEric Cheng dladm_str2ipv4addr(char *token, void *addr)
95125ec3e3dSEric Cheng {
95225ec3e3dSEric Cheng 	return (inet_pton(AF_INET, token, addr) == 1 ?
95325ec3e3dSEric Cheng 	    DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP);
95425ec3e3dSEric Cheng }
95525ec3e3dSEric Cheng 
9560dc2366fSVenugopal Iyer const char *
dladm_ipv6addr2str(void * addr,char * buf)9570dc2366fSVenugopal Iyer dladm_ipv6addr2str(void *addr, char *buf)
9580dc2366fSVenugopal Iyer {
9590dc2366fSVenugopal Iyer 	if (inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN) == NULL)
9600dc2366fSVenugopal Iyer 		buf[0] = '\0';
9610dc2366fSVenugopal Iyer 
9620dc2366fSVenugopal Iyer 	return (buf);
9630dc2366fSVenugopal Iyer }
9640dc2366fSVenugopal Iyer 
9650dc2366fSVenugopal Iyer dladm_status_t
dladm_str2ipv6addr(char * token,void * addr)9660dc2366fSVenugopal Iyer dladm_str2ipv6addr(char *token, void *addr)
9670dc2366fSVenugopal Iyer {
9680dc2366fSVenugopal Iyer 	return (inet_pton(AF_INET6, token, addr) == 1 ?
9690dc2366fSVenugopal Iyer 	    DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP);
9700dc2366fSVenugopal Iyer }
9710dc2366fSVenugopal Iyer 
97225ec3e3dSEric Cheng /*
97325ec3e3dSEric Cheng  * Find the set bits in a mask.
97425ec3e3dSEric Cheng  * This is used for expanding a bitmask into individual sub-masks
97525ec3e3dSEric Cheng  * which can be used for further processing.
97625ec3e3dSEric Cheng  */
97725ec3e3dSEric Cheng void
dladm_find_setbits32(uint32_t mask,uint32_t * list,uint32_t * cnt)97825ec3e3dSEric Cheng dladm_find_setbits32(uint32_t mask, uint32_t *list, uint32_t *cnt)
97925ec3e3dSEric Cheng {
98025ec3e3dSEric Cheng 	int	i, c = 0;
98125ec3e3dSEric Cheng 
98225ec3e3dSEric Cheng 	for (i = 0; i < 32; i++) {
98325ec3e3dSEric Cheng 		if (((1 << i) & mask) != 0)
98425ec3e3dSEric Cheng 			list[c++] = 1 << i;
98525ec3e3dSEric Cheng 	}
98625ec3e3dSEric Cheng 	*cnt = c;
98725ec3e3dSEric Cheng }
98825ec3e3dSEric Cheng 
989da14cebeSEric Cheng void
dladm_free_args(dladm_arg_list_t * list)990da14cebeSEric Cheng dladm_free_args(dladm_arg_list_t *list)
991da14cebeSEric Cheng {
992da14cebeSEric Cheng 	if (list != NULL) {
993da14cebeSEric Cheng 		free(list->al_buf);
994da14cebeSEric Cheng 		free(list);
995da14cebeSEric Cheng 	}
996da14cebeSEric Cheng }
997da14cebeSEric Cheng 
998da14cebeSEric Cheng dladm_status_t
dladm_parse_args(char * str,dladm_arg_list_t ** listp,boolean_t novalues)999da14cebeSEric Cheng dladm_parse_args(char *str, dladm_arg_list_t **listp, boolean_t novalues)
1000da14cebeSEric Cheng {
1001da14cebeSEric Cheng 	dladm_arg_list_t	*list;
1002da14cebeSEric Cheng 	dladm_arg_info_t	*aip;
1003da14cebeSEric Cheng 	char			*buf, *curr;
1004da14cebeSEric Cheng 	int			len, i;
1005da14cebeSEric Cheng 
100663a6526dSMichael Lim 	if (str == NULL)
100763a6526dSMichael Lim 		return (DLADM_STATUS_BADVAL);
100863a6526dSMichael Lim 
100963a6526dSMichael Lim 	if (str[0] == '\0')
101063a6526dSMichael Lim 		return (DLADM_STATUS_OK);
101163a6526dSMichael Lim 
1012da14cebeSEric Cheng 	list = malloc(sizeof (dladm_arg_list_t));
1013da14cebeSEric Cheng 	if (list == NULL)
1014da14cebeSEric Cheng 		return (dladm_errno2status(errno));
1015da14cebeSEric Cheng 
1016da14cebeSEric Cheng 	list->al_count = 0;
1017da14cebeSEric Cheng 	list->al_buf = buf = strdup(str);
1018da14cebeSEric Cheng 	if (buf == NULL)
1019da14cebeSEric Cheng 		return (dladm_errno2status(errno));
1020da14cebeSEric Cheng 
1021da14cebeSEric Cheng 	curr = buf;
1022da14cebeSEric Cheng 	len = strlen(buf);
1023da14cebeSEric Cheng 	aip = NULL;
1024da14cebeSEric Cheng 	for (i = 0; i < len; i++) {
1025da14cebeSEric Cheng 		char		c = buf[i];
1026da14cebeSEric Cheng 		boolean_t	match = (c == '=' || c == ',');
1027da14cebeSEric Cheng 
1028da14cebeSEric Cheng 		if (!match && i != len - 1)
1029da14cebeSEric Cheng 			continue;
1030da14cebeSEric Cheng 
1031da14cebeSEric Cheng 		if (match) {
1032da14cebeSEric Cheng 			buf[i] = '\0';
1033da14cebeSEric Cheng 			if (*curr == '\0')
1034da14cebeSEric Cheng 				goto fail;
1035da14cebeSEric Cheng 		}
1036da14cebeSEric Cheng 
1037da14cebeSEric Cheng 		if (aip != NULL && c != '=') {
1038da14cebeSEric Cheng 			if (aip->ai_count > DLADM_MAX_ARG_VALS)
1039da14cebeSEric Cheng 				goto fail;
1040da14cebeSEric Cheng 
1041da14cebeSEric Cheng 			if (novalues)
1042da14cebeSEric Cheng 				goto fail;
1043da14cebeSEric Cheng 
1044da14cebeSEric Cheng 			aip->ai_val[aip->ai_count] = curr;
1045da14cebeSEric Cheng 			aip->ai_count++;
1046da14cebeSEric Cheng 		} else {
1047da14cebeSEric Cheng 			if (list->al_count > DLADM_MAX_ARG_VALS)
1048da14cebeSEric Cheng 				goto fail;
1049da14cebeSEric Cheng 
1050da14cebeSEric Cheng 			aip = &list->al_info[list->al_count];
1051da14cebeSEric Cheng 			aip->ai_name = curr;
1052da14cebeSEric Cheng 			aip->ai_count = 0;
1053da14cebeSEric Cheng 			list->al_count++;
1054da14cebeSEric Cheng 			if (c == ',')
1055da14cebeSEric Cheng 				aip = NULL;
1056da14cebeSEric Cheng 		}
1057da14cebeSEric Cheng 		curr = buf + i + 1;
1058da14cebeSEric Cheng 	}
1059da14cebeSEric Cheng 
1060da14cebeSEric Cheng 	*listp = list;
1061da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
1062da14cebeSEric Cheng 
1063da14cebeSEric Cheng fail:
1064da14cebeSEric Cheng 	dladm_free_args(list);
1065da14cebeSEric Cheng 	return (DLADM_STATUS_FAILED);
1066da14cebeSEric Cheng }
1067c569ef53SMichael Lim 
1068c569ef53SMichael Lim /*
1069c569ef53SMichael Lim  * mac_propval_range_t functions.  Currently implemented for only
1070c569ef53SMichael Lim  * ranges of uint32_t elements, but can be expanded as required.
1071c569ef53SMichael Lim  */
1072c569ef53SMichael Lim /*
1073c569ef53SMichael Lim  * Convert an array of strings (which can be ranges or individual
1074c569ef53SMichael Lim  * elements) into a single mac_propval_range_t structure which
10754a3b1d5bSBart Coddens  * is allocated here but should be freed by the caller.
1076c569ef53SMichael Lim  */
1077c569ef53SMichael Lim dladm_status_t
dladm_strs2range(char ** prop_val,uint_t val_cnt,mac_propval_type_t type,mac_propval_range_t ** range)1078c569ef53SMichael Lim dladm_strs2range(char **prop_val, uint_t val_cnt,
1079c569ef53SMichael Lim 	mac_propval_type_t type, mac_propval_range_t **range)
1080c569ef53SMichael Lim {
1081c569ef53SMichael Lim 	int			i;
1082c569ef53SMichael Lim 	char			*endp;
1083c569ef53SMichael Lim 	mac_propval_range_t	*rangep;
1084c569ef53SMichael Lim 	dladm_status_t		status = DLADM_STATUS_OK;
1085c569ef53SMichael Lim 
1086c569ef53SMichael Lim 	switch (type) {
1087c569ef53SMichael Lim 	case MAC_PROPVAL_UINT32: {
1088c569ef53SMichael Lim 		mac_propval_uint32_range_t	*ur;
1089c569ef53SMichael Lim 
1090c569ef53SMichael Lim 		/* Allocate range structure */
1091c569ef53SMichael Lim 		rangep = malloc(sizeof (mac_propval_range_t) +
1092c569ef53SMichael Lim 		    (val_cnt-1)*(sizeof (mac_propval_uint32_range_t)));
1093c569ef53SMichael Lim 		if (rangep == NULL)
1094c569ef53SMichael Lim 			return (DLADM_STATUS_NOMEM);
1095c569ef53SMichael Lim 
1096c569ef53SMichael Lim 		rangep->mpr_count = 0;
1097c569ef53SMichael Lim 		ur = &rangep->mpr_range_uint32[0];
1098c569ef53SMichael Lim 		for (i = 0; i < val_cnt; i++, ur++) {
1099c569ef53SMichael Lim 			errno = 0;
1100c569ef53SMichael Lim 			if (strchr(prop_val[i], '-') == NULL) {
1101c569ef53SMichael Lim 				/* single element */
1102c569ef53SMichael Lim 				ur->mpur_min = ur->mpur_max =
1103c569ef53SMichael Lim 				    strtol(prop_val[i], &endp, 10);
1104c569ef53SMichael Lim 				if ((endp != NULL) && (*endp != '\0')) {
1105c569ef53SMichael Lim 					return (DLADM_STATUS_BADRANGE);
1106c569ef53SMichael Lim 				}
1107c569ef53SMichael Lim 			} else {
1108c569ef53SMichael Lim 				/* range of elements */
1109c569ef53SMichael Lim 				ur->mpur_min = strtol(prop_val[i], &endp, 10);
1110c569ef53SMichael Lim 				if (*endp++ != '-')
1111c569ef53SMichael Lim 					return (DLADM_STATUS_BADRANGE);
1112c569ef53SMichael Lim 				ur->mpur_max = strtol(endp, &endp, 10);
1113c569ef53SMichael Lim 				if (endp != NULL && *endp != '\0' ||
1114c569ef53SMichael Lim 				    ur->mpur_max < ur->mpur_min)
1115c569ef53SMichael Lim 					return (DLADM_STATUS_BADRANGE);
1116c569ef53SMichael Lim 			}
1117c569ef53SMichael Lim 			rangep->mpr_count++;
1118c569ef53SMichael Lim 		}
1119c569ef53SMichael Lim 		break;
1120c569ef53SMichael Lim 	}
1121c569ef53SMichael Lim 	default:
1122c569ef53SMichael Lim 		return (DLADM_STATUS_BADVAL);
1123c569ef53SMichael Lim 	}
1124c569ef53SMichael Lim 
1125c569ef53SMichael Lim 	rangep->mpr_type = type;
1126c569ef53SMichael Lim 	*range = rangep;
1127c569ef53SMichael Lim 
1128c569ef53SMichael Lim 	return (status);
1129c569ef53SMichael Lim }
1130c569ef53SMichael Lim 
1131c569ef53SMichael Lim /*
1132c569ef53SMichael Lim  * Convert a mac_propval_range_t structure into an array of elements.
1133c569ef53SMichael Lim  */
1134c569ef53SMichael Lim dladm_status_t
dladm_range2list(mac_propval_range_t * rangep,void * elem,uint_t * nelem)1135c569ef53SMichael Lim dladm_range2list(mac_propval_range_t *rangep, void *elem, uint_t *nelem)
1136c569ef53SMichael Lim {
1137c569ef53SMichael Lim 	int		i, j, k;
1138c569ef53SMichael Lim 	dladm_status_t	status = DLADM_STATUS_OK;
1139c569ef53SMichael Lim 
1140c569ef53SMichael Lim 	switch (rangep->mpr_type) {
1141c569ef53SMichael Lim 	case MAC_PROPVAL_UINT32: {
1142c569ef53SMichael Lim 		mac_propval_uint32_range_t	*ur;
1143c569ef53SMichael Lim 		uint32_t			*elem32 = elem;
1144c569ef53SMichael Lim 
1145c569ef53SMichael Lim 		k = 0;
1146c569ef53SMichael Lim 		ur = &rangep->mpr_range_uint32[0];
1147c569ef53SMichael Lim 		for (i = 0; i < rangep->mpr_count; i++, ur++) {
1148c569ef53SMichael Lim 			for (j = 0; j <= ur->mpur_max - ur->mpur_min; j++) {
1149c569ef53SMichael Lim 				elem32[k++] = ur->mpur_min + j;
1150c569ef53SMichael Lim 				if (k > *nelem) {
1151c569ef53SMichael Lim 					status = DLADM_STATUS_TOOMANYELEMENTS;
1152c569ef53SMichael Lim 					break;
1153c569ef53SMichael Lim 				}
1154c569ef53SMichael Lim 			}
1155c569ef53SMichael Lim 		}
1156c569ef53SMichael Lim 		*nelem = k;
1157c569ef53SMichael Lim 		break;
1158c569ef53SMichael Lim 	}
1159c569ef53SMichael Lim 	default:
1160c569ef53SMichael Lim 		status = DLADM_STATUS_BADVAL;
1161c569ef53SMichael Lim 		break;
1162c569ef53SMichael Lim 	}
1163c569ef53SMichael Lim 	return (status);
1164c569ef53SMichael Lim }
1165c569ef53SMichael Lim 
1166c569ef53SMichael Lim /*
1167c569ef53SMichael Lim  * Convert a mac_propval_range_t structure into an array of strings
1168c569ef53SMichael Lim  * of single elements or ranges.
1169c569ef53SMichael Lim  */
1170c569ef53SMichael Lim int
dladm_range2strs(mac_propval_range_t * rangep,char ** prop_val)1171c569ef53SMichael Lim dladm_range2strs(mac_propval_range_t *rangep, char **prop_val)
1172c569ef53SMichael Lim {
1173c569ef53SMichael Lim 	int	i;
1174c569ef53SMichael Lim 
1175c569ef53SMichael Lim 	switch (rangep->mpr_type) {
1176c569ef53SMichael Lim 	case MAC_PROPVAL_UINT32: {
1177c569ef53SMichael Lim 		mac_propval_uint32_range_t	*ur;
1178c569ef53SMichael Lim 
1179c569ef53SMichael Lim 		/* Write ranges and individual elements */
1180c569ef53SMichael Lim 		ur = &rangep->mpr_range_uint32[0];
1181094fb5acSRobert Mustacchi 		for (i = 0; i < rangep->mpr_count; i++, ur++) {
1182c569ef53SMichael Lim 			if (ur->mpur_min == ur->mpur_max) {
1183c569ef53SMichael Lim 				/* single element */
1184c569ef53SMichael Lim 				(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
1185c569ef53SMichael Lim 				    "%u", ur->mpur_min);
1186c569ef53SMichael Lim 			} else {
1187c569ef53SMichael Lim 				/* range of elements */
1188c569ef53SMichael Lim 				(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
1189c569ef53SMichael Lim 				    "%u-%u", ur->mpur_min, ur->mpur_max);
1190c569ef53SMichael Lim 			}
1191c569ef53SMichael Lim 		}
1192c569ef53SMichael Lim 		return (0);
1193c569ef53SMichael Lim 	}
1194c569ef53SMichael Lim 	default:
1195c569ef53SMichael Lim 		break;
1196c569ef53SMichael Lim 	}
1197c569ef53SMichael Lim 	return (EINVAL);
1198c569ef53SMichael Lim }
1199c569ef53SMichael Lim 
1200c569ef53SMichael Lim static int
uint32cmp(const void * a,const void * b)1201c569ef53SMichael Lim uint32cmp(const void *a, const void *b)
1202c569ef53SMichael Lim {
1203c569ef53SMichael Lim 	return (*(uint32_t *)a - *(uint32_t *)b);
1204c569ef53SMichael Lim }
1205c569ef53SMichael Lim 
1206c569ef53SMichael Lim /*
1207c569ef53SMichael Lim  * Sort and convert an array of elements into a single
1208c569ef53SMichael Lim  * mac_propval_range_t structure which is allocated here but
1209c569ef53SMichael Lim  * should be freed by the caller.
1210c569ef53SMichael Lim  */
1211c569ef53SMichael Lim dladm_status_t
dladm_list2range(void * elem,uint_t nelem,mac_propval_type_t type,mac_propval_range_t ** range)1212c569ef53SMichael Lim dladm_list2range(void *elem, uint_t nelem, mac_propval_type_t type,
1213c569ef53SMichael Lim     mac_propval_range_t **range)
1214c569ef53SMichael Lim {
1215c569ef53SMichael Lim 	int			i;
1216c569ef53SMichael Lim 	uint_t			nr = 0;
1217c569ef53SMichael Lim 	mac_propval_range_t	*rangep;
1218c569ef53SMichael Lim 	dladm_status_t		status = DLADM_STATUS_OK;
1219c569ef53SMichael Lim 
1220c569ef53SMichael Lim 	switch (type) {
1221c569ef53SMichael Lim 	case MAC_PROPVAL_UINT32: {
1222c569ef53SMichael Lim 		mac_propval_uint32_range_t	*ur;
1223c569ef53SMichael Lim 		uint32_t			*elem32 = elem;
1224c569ef53SMichael Lim 		uint32_t			*sort32;
1225c569ef53SMichael Lim 
1226c569ef53SMichael Lim 		/* Allocate range structure */
1227c569ef53SMichael Lim 		rangep = malloc(sizeof (mac_propval_range_t) +
1228c569ef53SMichael Lim 		    (nelem-1)*(sizeof (mac_propval_uint32_range_t)));
1229c569ef53SMichael Lim 		if (rangep == NULL)
1230c569ef53SMichael Lim 			return (DLADM_STATUS_NOMEM);
1231c569ef53SMichael Lim 
1232c569ef53SMichael Lim 		/* Allocate array for sorting */
1233c569ef53SMichael Lim 		sort32 = malloc(nelem * sizeof (uint32_t));
1234c569ef53SMichael Lim 		if (sort32 == NULL) {
1235c569ef53SMichael Lim 			free(rangep);
1236c569ef53SMichael Lim 			return (DLADM_STATUS_NOMEM);
1237c569ef53SMichael Lim 		}
1238c569ef53SMichael Lim 
1239c569ef53SMichael Lim 		/* Copy and sort list */
1240c569ef53SMichael Lim 		for (i = 0; i < nelem; i++)
1241c569ef53SMichael Lim 			sort32[i] =  elem32[i];
1242c569ef53SMichael Lim 		if (nelem > 1)
1243c569ef53SMichael Lim 			qsort(sort32, nelem, sizeof (uint32_t), uint32cmp);
1244c569ef53SMichael Lim 
1245c569ef53SMichael Lim 		/* Convert list to ranges */
1246c569ef53SMichael Lim 		ur = &rangep->mpr_range_uint32[0];
1247c569ef53SMichael Lim 		ur->mpur_min = ur->mpur_max = sort32[0];
1248c569ef53SMichael Lim 		for (i = 1; i < nelem; i++) {
1249c569ef53SMichael Lim 			if (sort32[i]-sort32[i-1] == 1) {
1250c569ef53SMichael Lim 				/* part of current range */
1251c569ef53SMichael Lim 				ur->mpur_max = sort32[i];
1252c569ef53SMichael Lim 			} else {
1253c569ef53SMichael Lim 				/* start a new range */
1254c569ef53SMichael Lim 				nr++; ur++;
1255c569ef53SMichael Lim 				ur->mpur_min = ur->mpur_max = sort32[i];
1256c569ef53SMichael Lim 			}
1257c569ef53SMichael Lim 		}
1258c569ef53SMichael Lim 		free(sort32);
1259c569ef53SMichael Lim 		break;
1260c569ef53SMichael Lim 	}
1261c569ef53SMichael Lim 	default:
1262c569ef53SMichael Lim 		return (DLADM_STATUS_BADRANGE);
1263c569ef53SMichael Lim 	}
1264c569ef53SMichael Lim 
1265c569ef53SMichael Lim 	rangep->mpr_type = type;
1266c569ef53SMichael Lim 	rangep->mpr_count = nr + 1;
1267c569ef53SMichael Lim 	*range = rangep;
1268c569ef53SMichael Lim 
1269c569ef53SMichael Lim 	return (status);
1270c569ef53SMichael Lim }
1271