xref: /onnv-gate/usr/src/lib/libdladm/common/libdladm.c (revision 12742:457040688239)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52311Sseb  * Common Development and Distribution License (the "License").
62311Sseb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212163SRamaswamy.Tummala@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <unistd.h>
260Sstevel@tonic-gate #include <errno.h>
275895Syz147064 #include <ctype.h>
283147Sxc151355 #include <fcntl.h>
293147Sxc151355 #include <strings.h>
303147Sxc151355 #include <dirent.h>
318275SEric Cheng #include <stdlib.h>
3210734SEric Cheng #include <netinet/in.h>
3310734SEric Cheng #include <arpa/inet.h>
346173Syz147064 #include <sys/param.h>
353147Sxc151355 #include <sys/stat.h>
368453SAnurag.Maskey@Sun.COM #include <sys/dld.h>
379107Sjames.d.carlson@sun.com #include <sys/dld_ioc.h>
383147Sxc151355 #include <libdladm_impl.h>
393184Smeem #include <libintl.h>
405895Syz147064 #include <libdlpi.h>
41*12742Smichael.l.lim@oracle.com #include <libdllink.h>
42733Skrgopi 
438453SAnurag.Maskey@Sun.COM static char	dladm_rootdir[MAXPATHLEN] = "/";
448453SAnurag.Maskey@Sun.COM 
459815SRishi.Srivatsavai@Sun.COM typedef struct media_type_desc {
469815SRishi.Srivatsavai@Sun.COM 	uint32_t	media_type;
479815SRishi.Srivatsavai@Sun.COM #define	MAX_MEDIA_TYPE_STRING	32
489815SRishi.Srivatsavai@Sun.COM 	const char	media_type_str[MAX_MEDIA_TYPE_STRING];
499815SRishi.Srivatsavai@Sun.COM } media_type_t;
509815SRishi.Srivatsavai@Sun.COM 
519815SRishi.Srivatsavai@Sun.COM static media_type_t media_type_table[] =  {
529815SRishi.Srivatsavai@Sun.COM 	{ DL_ETHER,	"Ethernet" },
539815SRishi.Srivatsavai@Sun.COM 	{ DL_WIFI,	"WiFi" },
549815SRishi.Srivatsavai@Sun.COM 	{ DL_IB,	"Infiniband" },
559815SRishi.Srivatsavai@Sun.COM 	{ DL_IPV4,	"IPv4Tunnel" },
569815SRishi.Srivatsavai@Sun.COM 	{ DL_IPV6,	"IPv6Tunnel" },
5710616SSebastien.Roy@Sun.COM 	{ DL_6TO4,	"6to4Tunnel" },
589815SRishi.Srivatsavai@Sun.COM 	{ DL_CSMACD,	"CSMA/CD" },
599815SRishi.Srivatsavai@Sun.COM 	{ DL_TPB,	"TokenBus" },
609815SRishi.Srivatsavai@Sun.COM 	{ DL_TPR,	"TokenRing" },
619815SRishi.Srivatsavai@Sun.COM 	{ DL_METRO,	"MetroNet" },
629815SRishi.Srivatsavai@Sun.COM 	{ DL_HDLC,	"HDLC" },
639815SRishi.Srivatsavai@Sun.COM 	{ DL_CHAR,	"SyncCharacter" },
649815SRishi.Srivatsavai@Sun.COM 	{ DL_CTCA,	"CTCA" },
659815SRishi.Srivatsavai@Sun.COM 	{ DL_FDDI, 	"FDDI" },
669815SRishi.Srivatsavai@Sun.COM 	{ DL_FC, 	"FiberChannel" },
679815SRishi.Srivatsavai@Sun.COM 	{ DL_ATM, 	"ATM" },
689815SRishi.Srivatsavai@Sun.COM 	{ DL_IPATM, 	"ATM(ClassicIP)" },
699815SRishi.Srivatsavai@Sun.COM 	{ DL_X25, 	"X.25" },
709815SRishi.Srivatsavai@Sun.COM 	{ DL_IPX25, 	"X.25(ClassicIP)" },
719815SRishi.Srivatsavai@Sun.COM 	{ DL_ISDN, 	"ISDN" },
729815SRishi.Srivatsavai@Sun.COM 	{ DL_HIPPI, 	"HIPPI" },
739815SRishi.Srivatsavai@Sun.COM 	{ DL_100VG, 	"100BaseVGEthernet" },
749815SRishi.Srivatsavai@Sun.COM 	{ DL_100VGTPR, 	"100BaseVGTokenRing" },
759815SRishi.Srivatsavai@Sun.COM 	{ DL_ETH_CSMA, 	"IEEE802.3" },
769815SRishi.Srivatsavai@Sun.COM 	{ DL_100BT, 	"100BaseT" },
779815SRishi.Srivatsavai@Sun.COM 	{ DL_FRAME, 	"FrameRelay" },
789815SRishi.Srivatsavai@Sun.COM 	{ DL_MPFRAME, 	"MPFrameRelay" },
799815SRishi.Srivatsavai@Sun.COM 	{ DL_ASYNC, 	"AsyncCharacter" },
809815SRishi.Srivatsavai@Sun.COM 	{ DL_IPNET, 	"IPNET" },
819815SRishi.Srivatsavai@Sun.COM 	{ DL_OTHER, 	"Other" }
829815SRishi.Srivatsavai@Sun.COM };
839815SRishi.Srivatsavai@Sun.COM #define	MEDIATYPECOUNT	(sizeof (media_type_table) / sizeof (media_type_t))
849815SRishi.Srivatsavai@Sun.COM 
8510734SEric Cheng typedef struct {
8610734SEric Cheng 	uint32_t	lp_type;
8710734SEric Cheng 	char		*lp_name;
8810734SEric Cheng } link_protect_t;
8910734SEric Cheng 
9010734SEric Cheng static link_protect_t link_protect_types[] = {
9110734SEric Cheng 	{ MPT_MACNOSPOOF, "mac-nospoof" },
9211878SVenu.Iyer@Sun.COM 	{ MPT_RESTRICTED, "restricted" },
9310734SEric Cheng 	{ MPT_IPNOSPOOF, "ip-nospoof" },
9411878SVenu.Iyer@Sun.COM 	{ MPT_DHCPNOSPOOF, "dhcp-nospoof" }
9510734SEric Cheng };
9610734SEric Cheng #define	LPTYPES	(sizeof (link_protect_types) / sizeof (link_protect_t))
9710734SEric Cheng 
988453SAnurag.Maskey@Sun.COM dladm_status_t
dladm_open(dladm_handle_t * handle)998453SAnurag.Maskey@Sun.COM dladm_open(dladm_handle_t *handle)
1008453SAnurag.Maskey@Sun.COM {
1018453SAnurag.Maskey@Sun.COM 	int dld_fd;
1028453SAnurag.Maskey@Sun.COM 
1038453SAnurag.Maskey@Sun.COM 	if (handle == NULL)
1048453SAnurag.Maskey@Sun.COM 		return (DLADM_STATUS_BADARG);
1058453SAnurag.Maskey@Sun.COM 
1068453SAnurag.Maskey@Sun.COM 	if ((dld_fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1078453SAnurag.Maskey@Sun.COM 		return (dladm_errno2status(errno));
1088453SAnurag.Maskey@Sun.COM 
1098453SAnurag.Maskey@Sun.COM 	/*
1108453SAnurag.Maskey@Sun.COM 	 * Don't open DLMGMT_DOOR now.  dlmgmtd(1M) is not able to
1118453SAnurag.Maskey@Sun.COM 	 * open the door when the dladm handle is opened because the
1128453SAnurag.Maskey@Sun.COM 	 * door hasn't been created yet at that time.  Thus, we must
1138453SAnurag.Maskey@Sun.COM 	 * open it on-demand in dladm_door_fd().  Move the open()
1148453SAnurag.Maskey@Sun.COM 	 * to dladm_door_fd() for all cases.
1158453SAnurag.Maskey@Sun.COM 	 */
1168453SAnurag.Maskey@Sun.COM 
1178453SAnurag.Maskey@Sun.COM 	if ((*handle = malloc(sizeof (struct dladm_handle))) == NULL) {
1188453SAnurag.Maskey@Sun.COM 		(void) close(dld_fd);
1198453SAnurag.Maskey@Sun.COM 		return (DLADM_STATUS_NOMEM);
1208453SAnurag.Maskey@Sun.COM 	}
1218453SAnurag.Maskey@Sun.COM 
1228453SAnurag.Maskey@Sun.COM 	(*handle)->dld_fd = dld_fd;
1238453SAnurag.Maskey@Sun.COM 	(*handle)->door_fd = -1;
1248453SAnurag.Maskey@Sun.COM 
1258453SAnurag.Maskey@Sun.COM 	return (DLADM_STATUS_OK);
1268453SAnurag.Maskey@Sun.COM }
1278453SAnurag.Maskey@Sun.COM 
1288453SAnurag.Maskey@Sun.COM void
dladm_close(dladm_handle_t handle)1298453SAnurag.Maskey@Sun.COM dladm_close(dladm_handle_t handle)
1308453SAnurag.Maskey@Sun.COM {
1318453SAnurag.Maskey@Sun.COM 	if (handle != NULL) {
1328453SAnurag.Maskey@Sun.COM 		(void) close(handle->dld_fd);
1338453SAnurag.Maskey@Sun.COM 		if (handle->door_fd != -1)
1348453SAnurag.Maskey@Sun.COM 			(void) close(handle->door_fd);
1358453SAnurag.Maskey@Sun.COM 		free(handle);
1368453SAnurag.Maskey@Sun.COM 	}
1378453SAnurag.Maskey@Sun.COM }
1388453SAnurag.Maskey@Sun.COM 
1398453SAnurag.Maskey@Sun.COM int
dladm_dld_fd(dladm_handle_t handle)1408453SAnurag.Maskey@Sun.COM dladm_dld_fd(dladm_handle_t handle)
1418453SAnurag.Maskey@Sun.COM {
1428453SAnurag.Maskey@Sun.COM 	return (handle->dld_fd);
1438453SAnurag.Maskey@Sun.COM }
1448453SAnurag.Maskey@Sun.COM 
1458453SAnurag.Maskey@Sun.COM /*
1468453SAnurag.Maskey@Sun.COM  * If DLMGMT_DOOR hasn't been opened in the handle yet, open it.
1478453SAnurag.Maskey@Sun.COM  */
1488453SAnurag.Maskey@Sun.COM dladm_status_t
dladm_door_fd(dladm_handle_t handle,int * door_fd)1498453SAnurag.Maskey@Sun.COM dladm_door_fd(dladm_handle_t handle, int *door_fd)
1508453SAnurag.Maskey@Sun.COM {
1518453SAnurag.Maskey@Sun.COM 	int fd;
1528453SAnurag.Maskey@Sun.COM 
1538453SAnurag.Maskey@Sun.COM 	if (handle->door_fd == -1) {
1548453SAnurag.Maskey@Sun.COM 		if ((fd = open(DLMGMT_DOOR, O_RDONLY)) < 0)
1558453SAnurag.Maskey@Sun.COM 			return (dladm_errno2status(errno));
1568453SAnurag.Maskey@Sun.COM 		handle->door_fd = fd;
1578453SAnurag.Maskey@Sun.COM 	}
1588453SAnurag.Maskey@Sun.COM 	*door_fd = handle->door_fd;
1598453SAnurag.Maskey@Sun.COM 
1608453SAnurag.Maskey@Sun.COM 	return (DLADM_STATUS_OK);
1618453SAnurag.Maskey@Sun.COM }
1623147Sxc151355 
1633147Sxc151355 const char *
dladm_status2str(dladm_status_t status,char * buf)1643147Sxc151355 dladm_status2str(dladm_status_t status, char *buf)
1653147Sxc151355 {
1663147Sxc151355 	const char	*s;
1673147Sxc151355 
1683147Sxc151355 	switch (status) {
1693147Sxc151355 	case DLADM_STATUS_OK:
1703147Sxc151355 		s = "ok";
1713147Sxc151355 		break;
1723147Sxc151355 	case DLADM_STATUS_BADARG:
1733147Sxc151355 		s = "invalid argument";
1743147Sxc151355 		break;
1753147Sxc151355 	case DLADM_STATUS_FAILED:
1763147Sxc151355 		s = "operation failed";
1773147Sxc151355 		break;
1783147Sxc151355 	case DLADM_STATUS_TOOSMALL:
1793147Sxc151355 		s = "buffer size too small";
1803147Sxc151355 		break;
1813147Sxc151355 	case DLADM_STATUS_NOTSUP:
1823147Sxc151355 		s = "operation not supported";
1833147Sxc151355 		break;
1843147Sxc151355 	case DLADM_STATUS_NOTFOUND:
1853147Sxc151355 		s = "object not found";
1863147Sxc151355 		break;
1873147Sxc151355 	case DLADM_STATUS_BADVAL:
1883147Sxc151355 		s = "invalid value";
1893147Sxc151355 		break;
1903147Sxc151355 	case DLADM_STATUS_NOMEM:
1913147Sxc151355 		s = "insufficient memory";
1923147Sxc151355 		break;
1933147Sxc151355 	case DLADM_STATUS_EXIST:
1943147Sxc151355 		s = "object already exists";
1953147Sxc151355 		break;
1963147Sxc151355 	case DLADM_STATUS_LINKINVAL:
1973147Sxc151355 		s = "invalid link";
1983147Sxc151355 		break;
1993147Sxc151355 	case DLADM_STATUS_PROPRDONLY:
2003147Sxc151355 		s = "read-only property";
2013147Sxc151355 		break;
2023147Sxc151355 	case DLADM_STATUS_BADVALCNT:
2033147Sxc151355 		s = "invalid number of values";
2043147Sxc151355 		break;
2053147Sxc151355 	case DLADM_STATUS_DBNOTFOUND:
2063147Sxc151355 		s = "database not found";
2073147Sxc151355 		break;
2083147Sxc151355 	case DLADM_STATUS_DENIED:
2093147Sxc151355 		s = "permission denied";
2103147Sxc151355 		break;
2113147Sxc151355 	case DLADM_STATUS_IOERR:
2123147Sxc151355 		s = "I/O error";
2133147Sxc151355 		break;
2143448Sdh155122 	case DLADM_STATUS_TEMPONLY:
2158275SEric Cheng 		s = "change cannot be persistent";
2163448Sdh155122 		break;
2173871Syz147064 	case DLADM_STATUS_TIMEDOUT:
2183871Syz147064 		s = "operation timed out";
2193871Syz147064 		break;
2203871Syz147064 	case DLADM_STATUS_ISCONN:
2213871Syz147064 		s = "already connected";
2223871Syz147064 		break;
2233871Syz147064 	case DLADM_STATUS_NOTCONN:
2243871Syz147064 		s = "not connected";
2253871Syz147064 		break;
2263871Syz147064 	case DLADM_STATUS_REPOSITORYINVAL:
2273871Syz147064 		s = "invalid configuration repository";
2283871Syz147064 		break;
2293871Syz147064 	case DLADM_STATUS_MACADDRINVAL:
2303871Syz147064 		s = "invalid MAC address";
2313871Syz147064 		break;
2323871Syz147064 	case DLADM_STATUS_KEYINVAL:
2333871Syz147064 		s = "invalid key";
2343871Syz147064 		break;
2355084Sjohnlev 	case DLADM_STATUS_INVALIDMACADDRLEN:
2365084Sjohnlev 		s = "invalid MAC address length";
2375084Sjohnlev 		break;
2385084Sjohnlev 	case DLADM_STATUS_INVALIDMACADDRTYPE:
2395084Sjohnlev 		s = "invalid MAC address type";
2405084Sjohnlev 		break;
2415895Syz147064 	case DLADM_STATUS_LINKBUSY:
2425895Syz147064 		s = "link busy";
2435895Syz147064 		break;
2445895Syz147064 	case DLADM_STATUS_VIDINVAL:
2455895Syz147064 		s = "invalid VLAN identifier";
2465084Sjohnlev 		break;
2475895Syz147064 	case DLADM_STATUS_TRYAGAIN:
2485895Syz147064 		s = "try again later";
2495084Sjohnlev 		break;
2505895Syz147064 	case DLADM_STATUS_NONOTIF:
2515895Syz147064 		s = "link notification is not supported";
2525084Sjohnlev 		break;
2538275SEric Cheng 	case DLADM_STATUS_BADTIMEVAL:
2548275SEric Cheng 		s = "invalid time range";
2558275SEric Cheng 		break;
2568275SEric Cheng 	case DLADM_STATUS_INVALIDMACADDR:
2578275SEric Cheng 		s = "invalid MAC address value";
2588275SEric Cheng 		break;
2598275SEric Cheng 	case DLADM_STATUS_INVALIDMACADDRNIC:
2608275SEric Cheng 		s = "MAC address reserved for use by underlying data-link";
2618275SEric Cheng 		break;
2628275SEric Cheng 	case DLADM_STATUS_INVALIDMACADDRINUSE:
2638275SEric Cheng 		s = "MAC address is already in use";
2648275SEric Cheng 		break;
2658275SEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTINVALID:
2668275SEric Cheng 		s = "invalid factory MAC address slot";
2678275SEric Cheng 		break;
2688275SEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTUSED:
2698275SEric Cheng 		s = "factory MAC address slot already used";
2708275SEric Cheng 		break;
2718275SEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTALLUSED:
2728275SEric Cheng 		s = "all factory MAC address slots are in use";
2738275SEric Cheng 		break;
2748275SEric Cheng 	case DLADM_STATUS_MACFACTORYNOTSUP:
2758275SEric Cheng 		s = "factory MAC address slots not supported";
2768275SEric Cheng 		break;
2778275SEric Cheng 	case DLADM_STATUS_INVALIDMACPREFIX:
2788275SEric Cheng 		s = "Invalid MAC address prefix value";
2798275SEric Cheng 		break;
2808275SEric Cheng 	case DLADM_STATUS_INVALIDMACPREFIXLEN:
2818275SEric Cheng 		s = "Invalid MAC address prefix length";
2828275SEric Cheng 		break;
283*12742Smichael.l.lim@oracle.com 	case DLADM_STATUS_BADCPUID:
2848275SEric Cheng 		s = "non-existent processor ID";
2858275SEric Cheng 		break;
2868275SEric Cheng 	case DLADM_STATUS_CPUERR:
2878275SEric Cheng 		s = "could not determine processor status";
2888275SEric Cheng 		break;
2898275SEric Cheng 	case DLADM_STATUS_CPUNOTONLINE:
2908275SEric Cheng 		s = "processor not online";
2918275SEric Cheng 		break;
292*12742Smichael.l.lim@oracle.com 	case DLADM_STATUS_TOOMANYELEMENTS:
293*12742Smichael.l.lim@oracle.com 		s = "too many elements specified";
294*12742Smichael.l.lim@oracle.com 		break;
295*12742Smichael.l.lim@oracle.com 	case DLADM_STATUS_BADRANGE:
296*12742Smichael.l.lim@oracle.com 		s = "invalid range";
297*12742Smichael.l.lim@oracle.com 		break;
2988275SEric Cheng 	case DLADM_STATUS_DB_NOTFOUND:
2998275SEric Cheng 		s = "database not found";
3008275SEric Cheng 		break;
3018275SEric Cheng 	case DLADM_STATUS_DB_PARSE_ERR:
3028275SEric Cheng 		s = "database parse error";
3038275SEric Cheng 		break;
3048275SEric Cheng 	case DLADM_STATUS_PROP_PARSE_ERR:
3058275SEric Cheng 		s = "property parse error";
3068275SEric Cheng 		break;
3078275SEric Cheng 	case DLADM_STATUS_ATTR_PARSE_ERR:
3088275SEric Cheng 		s = "attribute parse error";
3098275SEric Cheng 		break;
3108275SEric Cheng 	case DLADM_STATUS_FLOW_DB_ERR:
3118275SEric Cheng 		s = "flow database error";
3128275SEric Cheng 		break;
3138275SEric Cheng 	case DLADM_STATUS_FLOW_DB_OPEN_ERR:
3148275SEric Cheng 		s = "flow database open error";
3158275SEric Cheng 		break;
3168275SEric Cheng 	case DLADM_STATUS_FLOW_DB_PARSE_ERR:
3178275SEric Cheng 		s = "flow database parse error";
3188275SEric Cheng 		break;
3198275SEric Cheng 	case DLADM_STATUS_FLOWPROP_DB_PARSE_ERR:
3208275SEric Cheng 		s = "flow property database parse error";
3218275SEric Cheng 		break;
3228275SEric Cheng 	case DLADM_STATUS_FLOW_ADD_ERR:
3238275SEric Cheng 		s = "flow add error";
3248275SEric Cheng 		break;
3258275SEric Cheng 	case DLADM_STATUS_FLOW_WALK_ERR:
3268275SEric Cheng 		s = "flow walk error";
3278275SEric Cheng 		break;
3288275SEric Cheng 	case DLADM_STATUS_FLOW_IDENTICAL:
3298275SEric Cheng 		s = "a flow with identical attributes exists";
3308275SEric Cheng 		break;
3318275SEric Cheng 	case DLADM_STATUS_FLOW_INCOMPATIBLE:
3328275SEric Cheng 		s = "flow(s) with incompatible attributes exists";
3338275SEric Cheng 		break;
3348275SEric Cheng 	case DLADM_STATUS_FLOW_EXISTS:
3358275SEric Cheng 		s = "link still has flows";
3368275SEric Cheng 		break;
3378275SEric Cheng 	case DLADM_STATUS_PERSIST_FLOW_EXISTS:
3388275SEric Cheng 		s = "persistent flow with the same name exists";
3398275SEric Cheng 		break;
3408275SEric Cheng 	case DLADM_STATUS_INVALID_IP:
3418275SEric Cheng 		s = "invalid IP address";
3428275SEric Cheng 		break;
3438275SEric Cheng 	case DLADM_STATUS_INVALID_PREFIXLEN:
3448275SEric Cheng 		s = "invalid IP prefix length";
3458275SEric Cheng 		break;
3468275SEric Cheng 	case DLADM_STATUS_INVALID_PROTOCOL:
3478275SEric Cheng 		s = "invalid IP protocol";
3488275SEric Cheng 		break;
3498275SEric Cheng 	case DLADM_STATUS_INVALID_PORT:
3508275SEric Cheng 		s = "invalid port number";
3518275SEric Cheng 		break;
3528275SEric Cheng 	case DLADM_STATUS_INVALID_DSF:
3538275SEric Cheng 		s = "invalid dsfield";
3548275SEric Cheng 		break;
3558275SEric Cheng 	case DLADM_STATUS_INVALID_DSFMASK:
3568275SEric Cheng 		s = "invalid dsfield mask";
3578275SEric Cheng 		break;
3588275SEric Cheng 	case DLADM_STATUS_INVALID_MACMARGIN:
3598275SEric Cheng 		s = "MTU check failed, use lower MTU or -f option";
3608275SEric Cheng 		break;
3618275SEric Cheng 	case DLADM_STATUS_BADPROP:
3628275SEric Cheng 		s = "invalid property";
3638275SEric Cheng 		break;
3648275SEric Cheng 	case DLADM_STATUS_MINMAXBW:
36512309SMichael.Lim@Sun.COM 		s = "minimum value for maxbw is 1200K";
3668275SEric Cheng 		break;
3678275SEric Cheng 	case DLADM_STATUS_NO_HWRINGS:
3688275SEric Cheng 		s = "request hw rings failed";
3698275SEric Cheng 		break;
37010491SRishi.Srivatsavai@Sun.COM 	case DLADM_STATUS_PERMONLY:
37110491SRishi.Srivatsavai@Sun.COM 		s = "change must be persistent";
37210491SRishi.Srivatsavai@Sun.COM 		break;
37310491SRishi.Srivatsavai@Sun.COM 	case DLADM_STATUS_OPTMISSING:
37410491SRishi.Srivatsavai@Sun.COM 		s = "optional software not installed";
37510491SRishi.Srivatsavai@Sun.COM 		break;
37610616SSebastien.Roy@Sun.COM 	case DLADM_STATUS_IPTUNTYPE:
37710616SSebastien.Roy@Sun.COM 		s = "invalid IP tunnel type";
37810616SSebastien.Roy@Sun.COM 		break;
37910616SSebastien.Roy@Sun.COM 	case DLADM_STATUS_IPTUNTYPEREQD:
38010616SSebastien.Roy@Sun.COM 		s = "IP tunnel type required";
38110616SSebastien.Roy@Sun.COM 		break;
38210616SSebastien.Roy@Sun.COM 	case DLADM_STATUS_BADIPTUNLADDR:
38310616SSebastien.Roy@Sun.COM 		s = "invalid local IP tunnel address";
38410616SSebastien.Roy@Sun.COM 		break;
38510616SSebastien.Roy@Sun.COM 	case DLADM_STATUS_BADIPTUNRADDR:
38610616SSebastien.Roy@Sun.COM 		s = "invalid remote IP tunnel address";
38710616SSebastien.Roy@Sun.COM 		break;
38810616SSebastien.Roy@Sun.COM 	case DLADM_STATUS_ADDRINUSE:
38910616SSebastien.Roy@Sun.COM 		s = "address already in use";
39010616SSebastien.Roy@Sun.COM 		break;
39111878SVenu.Iyer@Sun.COM 	case DLADM_STATUS_POOLCPU:
39211878SVenu.Iyer@Sun.COM 		s = "pool and cpus property are mutually exclusive";
39311878SVenu.Iyer@Sun.COM 		break;
39412163SRamaswamy.Tummala@Sun.COM 	case DLADM_STATUS_INVALID_PORT_INSTANCE:
39512163SRamaswamy.Tummala@Sun.COM 		s = "invalid IB phys link";
39612163SRamaswamy.Tummala@Sun.COM 		break;
39712163SRamaswamy.Tummala@Sun.COM 	case DLADM_STATUS_PORT_IS_DOWN:
39812163SRamaswamy.Tummala@Sun.COM 		s = "port is down";
39912163SRamaswamy.Tummala@Sun.COM 		break;
40012163SRamaswamy.Tummala@Sun.COM 	case DLADM_STATUS_PARTITION_EXISTS:
40112163SRamaswamy.Tummala@Sun.COM 		s = "partition already exists";
40212163SRamaswamy.Tummala@Sun.COM 		break;
40312163SRamaswamy.Tummala@Sun.COM 	case DLADM_STATUS_PKEY_NOT_PRESENT:
40412163SRamaswamy.Tummala@Sun.COM 		s = "PKEY is not present on the port";
40512163SRamaswamy.Tummala@Sun.COM 		break;
40612163SRamaswamy.Tummala@Sun.COM 	case DLADM_STATUS_INVALID_PKEY:
40712163SRamaswamy.Tummala@Sun.COM 		s = "invalid PKEY";
40812163SRamaswamy.Tummala@Sun.COM 		break;
40912163SRamaswamy.Tummala@Sun.COM 	case DLADM_STATUS_NO_IB_HW_RESOURCE:
41012163SRamaswamy.Tummala@Sun.COM 		s = "IB internal resource not available";
41112163SRamaswamy.Tummala@Sun.COM 		break;
41212163SRamaswamy.Tummala@Sun.COM 	case DLADM_STATUS_INVALID_PKEY_TBL_SIZE:
41312163SRamaswamy.Tummala@Sun.COM 		s = "invalid PKEY table size";
41412163SRamaswamy.Tummala@Sun.COM 		break;
41512309SMichael.Lim@Sun.COM 	case DLADM_STATUS_PORT_NOPROTO:
41612309SMichael.Lim@Sun.COM 		s = "local or remote port requires transport";
41712309SMichael.Lim@Sun.COM 		break;
4183147Sxc151355 	default:
4193184Smeem 		s = "<unknown error>";
4203184Smeem 		break;
4213147Sxc151355 	}
4223184Smeem 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
4233147Sxc151355 	return (buf);
4243147Sxc151355 }
4253147Sxc151355 
4263147Sxc151355 /*
4273147Sxc151355  * Convert a unix errno to a dladm_status_t.
4283147Sxc151355  * We only convert errnos that are likely to be encountered. All others
4293147Sxc151355  * are mapped to DLADM_STATUS_FAILED.
4303147Sxc151355  */
4313147Sxc151355 dladm_status_t
dladm_errno2status(int err)4323147Sxc151355 dladm_errno2status(int err)
4333147Sxc151355 {
4343147Sxc151355 	switch (err) {
4355903Ssowmini 	case 0:
4365903Ssowmini 		return (DLADM_STATUS_OK);
4373147Sxc151355 	case EINVAL:
4383147Sxc151355 		return (DLADM_STATUS_BADARG);
4393147Sxc151355 	case EEXIST:
4403147Sxc151355 		return (DLADM_STATUS_EXIST);
4413147Sxc151355 	case ENOENT:
4423147Sxc151355 		return (DLADM_STATUS_NOTFOUND);
4433147Sxc151355 	case ENOSPC:
4443147Sxc151355 		return (DLADM_STATUS_TOOSMALL);
4453147Sxc151355 	case ENOMEM:
4463147Sxc151355 		return (DLADM_STATUS_NOMEM);
4473147Sxc151355 	case ENOTSUP:
4483147Sxc151355 		return (DLADM_STATUS_NOTSUP);
4495895Syz147064 	case ENETDOWN:
4505895Syz147064 		return (DLADM_STATUS_NONOTIF);
4513147Sxc151355 	case EACCES:
4527408SSebastien.Roy@Sun.COM 	case EPERM:
4533147Sxc151355 		return (DLADM_STATUS_DENIED);
4543147Sxc151355 	case EIO:
4553147Sxc151355 		return (DLADM_STATUS_IOERR);
4565084Sjohnlev 	case EBUSY:
4575895Syz147064 		return (DLADM_STATUS_LINKBUSY);
4585895Syz147064 	case EAGAIN:
4595895Syz147064 		return (DLADM_STATUS_TRYAGAIN);
4608275SEric Cheng 	case ENOTEMPTY:
4618275SEric Cheng 		return (DLADM_STATUS_FLOW_EXISTS);
4628275SEric Cheng 	case EOPNOTSUPP:
4638275SEric Cheng 		return (DLADM_STATUS_FLOW_INCOMPATIBLE);
4648275SEric Cheng 	case EALREADY:
4658275SEric Cheng 		return (DLADM_STATUS_FLOW_IDENTICAL);
46610616SSebastien.Roy@Sun.COM 	case EADDRINUSE:
46710616SSebastien.Roy@Sun.COM 		return (DLADM_STATUS_ADDRINUSE);
4683147Sxc151355 	default:
4693147Sxc151355 		return (DLADM_STATUS_FAILED);
4703147Sxc151355 	}
4713147Sxc151355 }
4723147Sxc151355 
4739055SMichael.Lim@Sun.COM boolean_t
dladm_str2interval(char * oarg,uint32_t * interval)4749055SMichael.Lim@Sun.COM dladm_str2interval(char *oarg, uint32_t *interval)
4759055SMichael.Lim@Sun.COM {
4769055SMichael.Lim@Sun.COM 	int		val;
4779055SMichael.Lim@Sun.COM 	char		*endp = NULL;
4789055SMichael.Lim@Sun.COM 
4799055SMichael.Lim@Sun.COM 	errno = 0;
4809055SMichael.Lim@Sun.COM 	val = strtol(oarg, &endp, 10);
4819055SMichael.Lim@Sun.COM 	if (errno != 0 || val <= 0 || *endp != '\0')
4829055SMichael.Lim@Sun.COM 		return (B_FALSE);
4839055SMichael.Lim@Sun.COM 
4849055SMichael.Lim@Sun.COM 	*interval = val;
4859055SMichael.Lim@Sun.COM 
4869055SMichael.Lim@Sun.COM 	return (B_TRUE);
4879055SMichael.Lim@Sun.COM }
4889055SMichael.Lim@Sun.COM 
4898275SEric Cheng dladm_status_t
dladm_str2bw(char * oarg,uint64_t * bw)4908275SEric Cheng dladm_str2bw(char *oarg, uint64_t *bw)
4918275SEric Cheng {
4928275SEric Cheng 	char		*endp = NULL;
4938275SEric Cheng 	int64_t		n;
4948275SEric Cheng 	int		mult = 1;
4958275SEric Cheng 
4968275SEric Cheng 	n = strtoull(oarg, &endp, 10);
4978275SEric Cheng 
4988275SEric Cheng 	if ((errno != 0) || (strlen(endp) > 1))
4998275SEric Cheng 		return (DLADM_STATUS_BADARG);
5008275SEric Cheng 
5018275SEric Cheng 	if (n < 0)
5028275SEric Cheng 		return (DLADM_STATUS_BADVAL);
5038275SEric Cheng 
5048275SEric Cheng 	switch (*endp) {
5058275SEric Cheng 	case 'k':
5068275SEric Cheng 	case 'K':
5078275SEric Cheng 		mult = 1000;
5088275SEric Cheng 		break;
5098275SEric Cheng 	case 'm':
5108275SEric Cheng 	case 'M':
5118275SEric Cheng 	case '\0':
5128275SEric Cheng 		mult = 1000000;
5138275SEric Cheng 		break;
5148275SEric Cheng 	case 'g':
5158275SEric Cheng 	case 'G':
5168275SEric Cheng 		mult = 1000000000;
5178275SEric Cheng 		break;
5188275SEric Cheng 	case '%':
5198275SEric Cheng 		/*
5208275SEric Cheng 		 * percentages not supported for now,
5218275SEric Cheng 		 * see RFE 6540675
5228275SEric Cheng 		 */
5238275SEric Cheng 		return (DLADM_STATUS_NOTSUP);
5248275SEric Cheng 	default:
5258275SEric Cheng 		return (DLADM_STATUS_BADVAL);
5268275SEric Cheng 	}
5278275SEric Cheng 
5288275SEric Cheng 	*bw = n * mult;
5298275SEric Cheng 
5308275SEric Cheng 	/* check for overflow */
5318275SEric Cheng 	if (*bw / mult != n)
5328275SEric Cheng 		return (DLADM_STATUS_BADARG);
5338275SEric Cheng 
5348275SEric Cheng 	return (DLADM_STATUS_OK);
5358275SEric Cheng }
5368275SEric Cheng 
5378275SEric Cheng /*
5388275SEric Cheng  * Convert bandwidth in bps to a string in mpbs.  For values greater
5398275SEric Cheng  * than 1mbps or 1000000, print a whole mbps value.  For values that
5408275SEric Cheng  * have fractional Mbps in whole Kbps , print the bandwidth in a manner
5418275SEric Cheng  * simlilar to a floating point format.
5428275SEric Cheng  *
5438275SEric Cheng  *        bps       string
5448275SEric Cheng  *          0            0
5458275SEric Cheng  *        100            0
5468275SEric Cheng  *       2000        0.002
5478275SEric Cheng  *     431000        0.431
5488275SEric Cheng  *    1000000            1
5498275SEric Cheng  *    1030000        1.030
5508275SEric Cheng  *  100000000          100
5518275SEric Cheng  */
5528275SEric Cheng const char *
dladm_bw2str(int64_t bw,char * buf)5538275SEric Cheng dladm_bw2str(int64_t bw, char *buf)
5548275SEric Cheng {
5558275SEric Cheng 	int kbps, mbps;
5568275SEric Cheng 
5578275SEric Cheng 	kbps = (bw%1000000)/1000;
5588275SEric Cheng 	mbps = bw/1000000;
5598275SEric Cheng 	if (kbps != 0) {
5608275SEric Cheng 		if (mbps == 0)
5618275SEric Cheng 			(void) snprintf(buf, DLADM_STRSIZE, "0.%03u", kbps);
5628275SEric Cheng 		else
5638275SEric Cheng 			(void) snprintf(buf, DLADM_STRSIZE, "%5u.%03u", mbps,
5648275SEric Cheng 			    kbps);
5658275SEric Cheng 	} else {
5668275SEric Cheng 		(void) snprintf(buf, DLADM_STRSIZE, "%5u", mbps);
5678275SEric Cheng 	}
5688275SEric Cheng 
5698275SEric Cheng 	return (buf);
5708275SEric Cheng }
5718275SEric Cheng 
5725895Syz147064 #define	LOCK_DB_PERMS	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
5733147Sxc151355 
5743147Sxc151355 static int
i_dladm_lock_db(const char * lock_file,short type)5753147Sxc151355 i_dladm_lock_db(const char *lock_file, short type)
5763147Sxc151355 {
5773147Sxc151355 	int	lock_fd;
5785895Syz147064 	struct	flock lock;
5793147Sxc151355 
5803147Sxc151355 	if ((lock_fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC,
5813147Sxc151355 	    LOCK_DB_PERMS)) < 0)
5823147Sxc151355 		return (-1);
5833147Sxc151355 
5843147Sxc151355 	lock.l_type = type;
5853147Sxc151355 	lock.l_whence = SEEK_SET;
5863147Sxc151355 	lock.l_start = 0;
5873147Sxc151355 	lock.l_len = 0;
5883147Sxc151355 
5893147Sxc151355 	if (fcntl(lock_fd, F_SETLKW, &lock) < 0) {
5903147Sxc151355 		int err = errno;
5913147Sxc151355 
5923147Sxc151355 		(void) close(lock_fd);
5933147Sxc151355 		(void) unlink(lock_file);
5943147Sxc151355 		errno = err;
5953147Sxc151355 		return (-1);
5963147Sxc151355 	}
5973147Sxc151355 	return (lock_fd);
5983147Sxc151355 }
5993147Sxc151355 
6003147Sxc151355 static void
i_dladm_unlock_db(const char * lock_file,int fd)6013147Sxc151355 i_dladm_unlock_db(const char *lock_file, int fd)
6023147Sxc151355 {
6033147Sxc151355 	struct flock lock;
6043147Sxc151355 
6053147Sxc151355 	if (fd < 0)
6063147Sxc151355 		return;
6073147Sxc151355 
6083147Sxc151355 	lock.l_type = F_UNLCK;
6093147Sxc151355 	lock.l_whence = SEEK_SET;
6103147Sxc151355 	lock.l_start = 0;
6113147Sxc151355 	lock.l_len = 0;
6123147Sxc151355 
6133147Sxc151355 	(void) fcntl(fd, F_SETLKW, &lock);
6143147Sxc151355 	(void) close(fd);
6153147Sxc151355 	(void) unlink(lock_file);
6163147Sxc151355 }
6173147Sxc151355 
6185895Syz147064 /*
6195895Syz147064  * Given a link class, returns its class string.
6205895Syz147064  */
6215895Syz147064 const char *
dladm_class2str(datalink_class_t class,char * buf)6225895Syz147064 dladm_class2str(datalink_class_t class, char *buf)
6235895Syz147064 {
6245895Syz147064 	const char *s;
6255895Syz147064 
6265895Syz147064 	switch (class) {
6275895Syz147064 	case DATALINK_CLASS_PHYS:
6285895Syz147064 		s = "phys";
6295895Syz147064 		break;
6305895Syz147064 	case DATALINK_CLASS_VLAN:
6315895Syz147064 		s = "vlan";
6325895Syz147064 		break;
6335895Syz147064 	case DATALINK_CLASS_AGGR:
6345895Syz147064 		s = "aggr";
6355895Syz147064 		break;
6365895Syz147064 	case DATALINK_CLASS_VNIC:
6375895Syz147064 		s = "vnic";
6385895Syz147064 		break;
6398275SEric Cheng 	case DATALINK_CLASS_ETHERSTUB:
6408275SEric Cheng 		s = "etherstub";
6418275SEric Cheng 		break;
64210616SSebastien.Roy@Sun.COM 	case DATALINK_CLASS_IPTUN:
64310616SSebastien.Roy@Sun.COM 		s = "iptun";
64410616SSebastien.Roy@Sun.COM 		break;
6459815SRishi.Srivatsavai@Sun.COM 	case DATALINK_CLASS_SIMNET:
6469815SRishi.Srivatsavai@Sun.COM 		s = "simnet";
6479815SRishi.Srivatsavai@Sun.COM 		break;
64810491SRishi.Srivatsavai@Sun.COM 	case DATALINK_CLASS_BRIDGE:
64910491SRishi.Srivatsavai@Sun.COM 		s = "bridge";
65010491SRishi.Srivatsavai@Sun.COM 		break;
65112163SRamaswamy.Tummala@Sun.COM 	case DATALINK_CLASS_PART:
65212163SRamaswamy.Tummala@Sun.COM 		s = "part";
65312163SRamaswamy.Tummala@Sun.COM 		break;
6545895Syz147064 	default:
6555895Syz147064 		s = "unknown";
6565895Syz147064 		break;
6575895Syz147064 	}
6585895Syz147064 
6595895Syz147064 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
6605895Syz147064 	return (buf);
6615895Syz147064 }
6625895Syz147064 
6635895Syz147064 /*
6645895Syz147064  * Given a physical link media type, returns its media type string.
6655895Syz147064  */
6665895Syz147064 const char *
dladm_media2str(uint32_t media,char * buf)6675895Syz147064 dladm_media2str(uint32_t media, char *buf)
6685895Syz147064 {
6699815SRishi.Srivatsavai@Sun.COM 	const char *s = "--";
6709815SRishi.Srivatsavai@Sun.COM 	media_type_t *mt;
6719815SRishi.Srivatsavai@Sun.COM 	int idx;
6725895Syz147064 
6739815SRishi.Srivatsavai@Sun.COM 	for (idx = 0; idx < MEDIATYPECOUNT; idx++) {
6749815SRishi.Srivatsavai@Sun.COM 		mt = media_type_table + idx;
6759815SRishi.Srivatsavai@Sun.COM 		if (mt->media_type == media) {
6769815SRishi.Srivatsavai@Sun.COM 			s = mt->media_type_str;
6779815SRishi.Srivatsavai@Sun.COM 			break;
6789815SRishi.Srivatsavai@Sun.COM 		}
6795895Syz147064 	}
6805895Syz147064 
6815895Syz147064 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
6825895Syz147064 	return (buf);
6835895Syz147064 }
6845895Syz147064 
6859815SRishi.Srivatsavai@Sun.COM /*
6869815SRishi.Srivatsavai@Sun.COM  * Given a physical link media type string, returns its media type constant.
6879815SRishi.Srivatsavai@Sun.COM  */
6889815SRishi.Srivatsavai@Sun.COM uint32_t
dladm_str2media(const char * buf)6899815SRishi.Srivatsavai@Sun.COM dladm_str2media(const char *buf)
6909815SRishi.Srivatsavai@Sun.COM {
6919815SRishi.Srivatsavai@Sun.COM 	media_type_t *mt;
6929815SRishi.Srivatsavai@Sun.COM 	int idx;
6939815SRishi.Srivatsavai@Sun.COM 
6949815SRishi.Srivatsavai@Sun.COM 	for (idx = 0; idx < MEDIATYPECOUNT; idx++) {
6959815SRishi.Srivatsavai@Sun.COM 		mt = media_type_table + idx;
6969815SRishi.Srivatsavai@Sun.COM 		if (strcasecmp(buf, mt->media_type_str) == 0)
6979815SRishi.Srivatsavai@Sun.COM 			return (mt->media_type);
6989815SRishi.Srivatsavai@Sun.COM 	}
6999815SRishi.Srivatsavai@Sun.COM 
7009815SRishi.Srivatsavai@Sun.COM 	return (DL_OTHER);
7019815SRishi.Srivatsavai@Sun.COM }
7029815SRishi.Srivatsavai@Sun.COM 
7033147Sxc151355 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)7048453SAnurag.Maskey@Sun.COM i_dladm_rw_db(dladm_handle_t handle, const char *db_file, mode_t db_perms,
7058453SAnurag.Maskey@Sun.COM     dladm_status_t (*process_db)(dladm_handle_t, void *, FILE *, FILE *),
7063147Sxc151355     void *arg, boolean_t writeop)
7073147Sxc151355 {
7083147Sxc151355 	dladm_status_t	status = DLADM_STATUS_OK;
7093147Sxc151355 	FILE		*fp, *nfp = NULL;
7103147Sxc151355 	char		lock[MAXPATHLEN];
7113147Sxc151355 	char		file[MAXPATHLEN];
7123147Sxc151355 	char		newfile[MAXPATHLEN];
7133147Sxc151355 	char		*db_basename;
7143147Sxc151355 	int		nfd, lock_fd;
7153147Sxc151355 
7163147Sxc151355 	/*
7173147Sxc151355 	 * If we are called from a boot script such as net-physical,
7183147Sxc151355 	 * it's quite likely that the root fs is still not writable.
7193147Sxc151355 	 * For this case, it's ok for the lock creation to fail since
7203147Sxc151355 	 * no one else could be accessing our configuration file.
7213147Sxc151355 	 */
7223147Sxc151355 	db_basename = strrchr(db_file, '/');
7233147Sxc151355 	if (db_basename == NULL || db_basename[1] == '\0')
7243147Sxc151355 		return (dladm_errno2status(EINVAL));
7253147Sxc151355 	db_basename++;
7263147Sxc151355 	(void) snprintf(lock, MAXPATHLEN, "/tmp/%s.lock", db_basename);
7273147Sxc151355 	if ((lock_fd = i_dladm_lock_db
7283147Sxc151355 	    (lock, (writeop ? F_WRLCK : F_RDLCK))) < 0 && errno != EROFS)
7293147Sxc151355 		return (dladm_errno2status(errno));
7303147Sxc151355 
7313147Sxc151355 	(void) snprintf(file, MAXPATHLEN, "%s/%s", dladm_rootdir, db_file);
7323147Sxc151355 	if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) {
7333147Sxc151355 		int	err = errno;
7343147Sxc151355 
7353147Sxc151355 		i_dladm_unlock_db(lock, lock_fd);
7363147Sxc151355 		if (err == ENOENT)
7373147Sxc151355 			return (DLADM_STATUS_DBNOTFOUND);
7383147Sxc151355 
7393147Sxc151355 		return (dladm_errno2status(err));
7403147Sxc151355 	}
7413147Sxc151355 
7423147Sxc151355 	if (writeop) {
7433147Sxc151355 		(void) snprintf(newfile, MAXPATHLEN, "%s/%s.new",
7443147Sxc151355 		    dladm_rootdir, db_file);
7453147Sxc151355 		if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC,
7463147Sxc151355 		    db_perms)) < 0) {
7473147Sxc151355 			(void) fclose(fp);
7483147Sxc151355 			i_dladm_unlock_db(lock, lock_fd);
7493147Sxc151355 			return (dladm_errno2status(errno));
7503147Sxc151355 		}
7513147Sxc151355 
7523147Sxc151355 		if ((nfp = fdopen(nfd, "w")) == NULL) {
7533147Sxc151355 			(void) close(nfd);
7543147Sxc151355 			(void) fclose(fp);
7553147Sxc151355 			(void) unlink(newfile);
7563147Sxc151355 			i_dladm_unlock_db(lock, lock_fd);
7573147Sxc151355 			return (dladm_errno2status(errno));
7583147Sxc151355 		}
7593147Sxc151355 	}
7608453SAnurag.Maskey@Sun.COM 	status = (*process_db)(handle, arg, fp, nfp);
7613147Sxc151355 	if (!writeop || status != DLADM_STATUS_OK)
7623147Sxc151355 		goto done;
7633147Sxc151355 
76411767SAnurag.Maskey@Sun.COM 	/* Set permissions on file to db_perms */
76511767SAnurag.Maskey@Sun.COM 	if (fchmod(nfd, db_perms) < 0) {
76611767SAnurag.Maskey@Sun.COM 		status = dladm_errno2status(errno);
76711767SAnurag.Maskey@Sun.COM 		goto done;
76811767SAnurag.Maskey@Sun.COM 	}
76911767SAnurag.Maskey@Sun.COM 
7703147Sxc151355 	/*
77111767SAnurag.Maskey@Sun.COM 	 * Configuration files need to be owned by the 'dladm' user and
77211767SAnurag.Maskey@Sun.COM 	 * 'netadm' group.
7733147Sxc151355 	 */
77411767SAnurag.Maskey@Sun.COM 	if (fchown(nfd, UID_DLADM, GID_NETADM) < 0) {
77511767SAnurag.Maskey@Sun.COM 		status = dladm_errno2status(errno);
77611767SAnurag.Maskey@Sun.COM 		goto done;
7773147Sxc151355 	}
7783147Sxc151355 
7793147Sxc151355 	if (fflush(nfp) == EOF) {
7803147Sxc151355 		status = dladm_errno2status(errno);
7813147Sxc151355 		goto done;
7823147Sxc151355 	}
7833147Sxc151355 	(void) fclose(fp);
7843147Sxc151355 	(void) fclose(nfp);
7853147Sxc151355 
7863147Sxc151355 	if (rename(newfile, file) < 0) {
7873147Sxc151355 		(void) unlink(newfile);
7883147Sxc151355 		i_dladm_unlock_db(lock, lock_fd);
7893147Sxc151355 		return (dladm_errno2status(errno));
7903147Sxc151355 	}
7913147Sxc151355 
7923147Sxc151355 	i_dladm_unlock_db(lock, lock_fd);
7933147Sxc151355 	return (DLADM_STATUS_OK);
7943147Sxc151355 
7953147Sxc151355 done:
7963147Sxc151355 	if (nfp != NULL) {
7973147Sxc151355 		(void) fclose(nfp);
7983147Sxc151355 		if (status != DLADM_STATUS_OK)
7993147Sxc151355 			(void) unlink(newfile);
8003147Sxc151355 	}
8013147Sxc151355 	(void) fclose(fp);
8023147Sxc151355 	i_dladm_unlock_db(lock, lock_fd);
8033147Sxc151355 	return (status);
8043147Sxc151355 }
8053147Sxc151355 
8063147Sxc151355 dladm_status_t
dladm_set_rootdir(const char * rootdir)8073147Sxc151355 dladm_set_rootdir(const char *rootdir)
8083147Sxc151355 {
8093147Sxc151355 	DIR	*dp;
8103147Sxc151355 
8113147Sxc151355 	if (rootdir == NULL || *rootdir != '/' ||
8123147Sxc151355 	    (dp = opendir(rootdir)) == NULL)
8133147Sxc151355 		return (DLADM_STATUS_BADARG);
8143147Sxc151355 
8153147Sxc151355 	(void) strncpy(dladm_rootdir, rootdir, MAXPATHLEN);
8163147Sxc151355 	(void) closedir(dp);
8173147Sxc151355 	return (DLADM_STATUS_OK);
8183147Sxc151355 }
8195895Syz147064 
8205895Syz147064 boolean_t
dladm_valid_linkname(const char * link)8215895Syz147064 dladm_valid_linkname(const char *link)
8225895Syz147064 {
8235895Syz147064 	size_t		len = strlen(link);
8245895Syz147064 	const char	*cp;
8255895Syz147064 
82610616SSebastien.Roy@Sun.COM 	if (len >= MAXLINKNAMELEN)
8275895Syz147064 		return (B_FALSE);
8285895Syz147064 
8295895Syz147064 	/*
8305895Syz147064 	 * The link name cannot start with a digit and must end with a digit.
8315895Syz147064 	 */
8325895Syz147064 	if ((isdigit(link[0]) != 0) || (isdigit(link[len - 1]) == 0))
8335895Syz147064 		return (B_FALSE);
8345895Syz147064 
8355895Syz147064 	/*
8365895Syz147064 	 * The legal characters in a link name are:
83710616SSebastien.Roy@Sun.COM 	 * alphanumeric (a-z,  A-Z,  0-9), underscore ('_'), and '.'.
8385895Syz147064 	 */
8395895Syz147064 	for (cp = link; *cp != '\0'; cp++) {
84010616SSebastien.Roy@Sun.COM 		if ((isalnum(*cp) == 0) && (*cp != '_') && (*cp != '.'))
8415895Syz147064 			return (B_FALSE);
8425895Syz147064 	}
8435895Syz147064 
8445895Syz147064 	return (B_TRUE);
8455895Syz147064 }
8468275SEric Cheng 
8478275SEric Cheng /*
8488275SEric Cheng  * Convert priority string to a value.
8498275SEric Cheng  */
8508275SEric Cheng dladm_status_t
dladm_str2pri(char * token,mac_priority_level_t * pri)8518275SEric Cheng dladm_str2pri(char *token, mac_priority_level_t *pri)
8528275SEric Cheng {
8538275SEric Cheng 	if (strlen(token) == strlen("low") &&
8548275SEric Cheng 	    strncasecmp(token, "low", strlen("low")) == 0) {
8558275SEric Cheng 		*pri = MPL_LOW;
8568275SEric Cheng 	} else if (strlen(token) == strlen("medium") &&
8578275SEric Cheng 	    strncasecmp(token, "medium", strlen("medium")) == 0) {
8588275SEric Cheng 		*pri = MPL_MEDIUM;
8598275SEric Cheng 	} else if (strlen(token) == strlen("high") &&
8608275SEric Cheng 	    strncasecmp(token, "high", strlen("high")) == 0) {
8618275SEric Cheng 		*pri = MPL_HIGH;
8628275SEric Cheng 	} else {
8638275SEric Cheng 		return (DLADM_STATUS_BADVAL);
8648275SEric Cheng 	}
8658275SEric Cheng 	return (DLADM_STATUS_OK);
8668275SEric Cheng }
8678275SEric Cheng 
8688275SEric Cheng /*
8698275SEric Cheng  * Convert priority value to a string.
8708275SEric Cheng  */
8718275SEric Cheng const char *
dladm_pri2str(mac_priority_level_t pri,char * buf)8728275SEric Cheng dladm_pri2str(mac_priority_level_t pri, char *buf)
8738275SEric Cheng {
8748275SEric Cheng 	const char	*s;
8758275SEric Cheng 
8768275SEric Cheng 	switch (pri) {
8778275SEric Cheng 	case MPL_LOW:
8788275SEric Cheng 		s = "low";
8798275SEric Cheng 		break;
8808275SEric Cheng 	case MPL_MEDIUM:
8818275SEric Cheng 		s = "medium";
8828275SEric Cheng 		break;
8838275SEric Cheng 	case MPL_HIGH:
8848275SEric Cheng 		s = "high";
8858275SEric Cheng 		break;
8868275SEric Cheng 	default:
8878275SEric Cheng 		s = "--";
8888275SEric Cheng 		break;
8898275SEric Cheng 	}
8908275SEric Cheng 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
8918275SEric Cheng 	return (buf);
8928275SEric Cheng }
8938275SEric Cheng 
89410734SEric Cheng /*
89510734SEric Cheng  * Convert protect string to a value.
89610734SEric Cheng  */
89710734SEric Cheng dladm_status_t
dladm_str2protect(char * token,uint32_t * ptype)89810734SEric Cheng dladm_str2protect(char *token, uint32_t *ptype)
89910734SEric Cheng {
90010734SEric Cheng 	link_protect_t	*lp;
90110734SEric Cheng 	int		i;
90210734SEric Cheng 
90310734SEric Cheng 	for (i = 0; i < LPTYPES; i++) {
90410734SEric Cheng 		lp = &link_protect_types[i];
90510734SEric Cheng 		if (strcmp(token, lp->lp_name) == 0) {
90610734SEric Cheng 			*ptype = lp->lp_type;
90710734SEric Cheng 			return (DLADM_STATUS_OK);
90810734SEric Cheng 		}
90910734SEric Cheng 	}
91010734SEric Cheng 	return (DLADM_STATUS_BADVAL);
91110734SEric Cheng }
91210734SEric Cheng 
91310734SEric Cheng /*
91410734SEric Cheng  * Convert protect value to a string.
91510734SEric Cheng  */
91610734SEric Cheng const char *
dladm_protect2str(uint32_t ptype,char * buf)91710734SEric Cheng dladm_protect2str(uint32_t ptype, char *buf)
91810734SEric Cheng {
91910734SEric Cheng 	const char	*s = "--";
92010734SEric Cheng 	link_protect_t	*lp;
92110734SEric Cheng 	int		i;
92210734SEric Cheng 
92310734SEric Cheng 	for (i = 0; i < LPTYPES; i++) {
92410734SEric Cheng 		lp = &link_protect_types[i];
92510734SEric Cheng 		if (lp->lp_type == ptype) {
92610734SEric Cheng 			s = lp->lp_name;
92710734SEric Cheng 			break;
92810734SEric Cheng 		}
92910734SEric Cheng 	}
93010734SEric Cheng 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
93110734SEric Cheng 	return (buf);
93210734SEric Cheng }
93310734SEric Cheng 
93410734SEric Cheng /*
93510734SEric Cheng  * Convert an IPv4 address to/from a string.
93610734SEric Cheng  */
93710734SEric Cheng const char *
dladm_ipv4addr2str(void * addr,char * buf)93810734SEric Cheng dladm_ipv4addr2str(void *addr, char *buf)
93910734SEric Cheng {
94010734SEric Cheng 	if (inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN) == NULL)
94111878SVenu.Iyer@Sun.COM 		buf[0] = '\0';
94210734SEric Cheng 
94310734SEric Cheng 	return (buf);
94410734SEric Cheng }
94510734SEric Cheng 
94610734SEric Cheng dladm_status_t
dladm_str2ipv4addr(char * token,void * addr)94710734SEric Cheng dladm_str2ipv4addr(char *token, void *addr)
94810734SEric Cheng {
94910734SEric Cheng 	return (inet_pton(AF_INET, token, addr) == 1 ?
95010734SEric Cheng 	    DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP);
95110734SEric Cheng }
95210734SEric Cheng 
95311878SVenu.Iyer@Sun.COM const char *
dladm_ipv6addr2str(void * addr,char * buf)95411878SVenu.Iyer@Sun.COM dladm_ipv6addr2str(void *addr, char *buf)
95511878SVenu.Iyer@Sun.COM {
95611878SVenu.Iyer@Sun.COM 	if (inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN) == NULL)
95711878SVenu.Iyer@Sun.COM 		buf[0] = '\0';
95811878SVenu.Iyer@Sun.COM 
95911878SVenu.Iyer@Sun.COM 	return (buf);
96011878SVenu.Iyer@Sun.COM }
96111878SVenu.Iyer@Sun.COM 
96211878SVenu.Iyer@Sun.COM dladm_status_t
dladm_str2ipv6addr(char * token,void * addr)96311878SVenu.Iyer@Sun.COM dladm_str2ipv6addr(char *token, void *addr)
96411878SVenu.Iyer@Sun.COM {
96511878SVenu.Iyer@Sun.COM 	return (inet_pton(AF_INET6, token, addr) == 1 ?
96611878SVenu.Iyer@Sun.COM 	    DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP);
96711878SVenu.Iyer@Sun.COM }
96811878SVenu.Iyer@Sun.COM 
96910734SEric Cheng /*
97010734SEric Cheng  * Find the set bits in a mask.
97110734SEric Cheng  * This is used for expanding a bitmask into individual sub-masks
97210734SEric Cheng  * which can be used for further processing.
97310734SEric Cheng  */
97410734SEric Cheng void
dladm_find_setbits32(uint32_t mask,uint32_t * list,uint32_t * cnt)97510734SEric Cheng dladm_find_setbits32(uint32_t mask, uint32_t *list, uint32_t *cnt)
97610734SEric Cheng {
97710734SEric Cheng 	int	i, c = 0;
97810734SEric Cheng 
97910734SEric Cheng 	for (i = 0; i < 32; i++) {
98010734SEric Cheng 		if (((1 << i) & mask) != 0)
98110734SEric Cheng 			list[c++] = 1 << i;
98210734SEric Cheng 	}
98310734SEric Cheng 	*cnt = c;
98410734SEric Cheng }
98510734SEric Cheng 
9868275SEric Cheng void
dladm_free_args(dladm_arg_list_t * list)9878275SEric Cheng dladm_free_args(dladm_arg_list_t *list)
9888275SEric Cheng {
9898275SEric Cheng 	if (list != NULL) {
9908275SEric Cheng 		free(list->al_buf);
9918275SEric Cheng 		free(list);
9928275SEric Cheng 	}
9938275SEric Cheng }
9948275SEric Cheng 
9958275SEric Cheng dladm_status_t
dladm_parse_args(char * str,dladm_arg_list_t ** listp,boolean_t novalues)9968275SEric Cheng dladm_parse_args(char *str, dladm_arg_list_t **listp, boolean_t novalues)
9978275SEric Cheng {
9988275SEric Cheng 	dladm_arg_list_t	*list;
9998275SEric Cheng 	dladm_arg_info_t	*aip;
10008275SEric Cheng 	char			*buf, *curr;
10018275SEric Cheng 	int			len, i;
10028275SEric Cheng 
10039055SMichael.Lim@Sun.COM 	if (str == NULL)
10049055SMichael.Lim@Sun.COM 		return (DLADM_STATUS_BADVAL);
10059055SMichael.Lim@Sun.COM 
10069055SMichael.Lim@Sun.COM 	if (str[0] == '\0')
10079055SMichael.Lim@Sun.COM 		return (DLADM_STATUS_OK);
10089055SMichael.Lim@Sun.COM 
10098275SEric Cheng 	list = malloc(sizeof (dladm_arg_list_t));
10108275SEric Cheng 	if (list == NULL)
10118275SEric Cheng 		return (dladm_errno2status(errno));
10128275SEric Cheng 
10138275SEric Cheng 	list->al_count = 0;
10148275SEric Cheng 	list->al_buf = buf = strdup(str);
10158275SEric Cheng 	if (buf == NULL)
10168275SEric Cheng 		return (dladm_errno2status(errno));
10178275SEric Cheng 
10188275SEric Cheng 	curr = buf;
10198275SEric Cheng 	len = strlen(buf);
10208275SEric Cheng 	aip = NULL;
10218275SEric Cheng 	for (i = 0; i < len; i++) {
10228275SEric Cheng 		char		c = buf[i];
10238275SEric Cheng 		boolean_t	match = (c == '=' || c == ',');
10248275SEric Cheng 
10258275SEric Cheng 		if (!match && i != len - 1)
10268275SEric Cheng 			continue;
10278275SEric Cheng 
10288275SEric Cheng 		if (match) {
10298275SEric Cheng 			buf[i] = '\0';
10308275SEric Cheng 			if (*curr == '\0')
10318275SEric Cheng 				goto fail;
10328275SEric Cheng 		}
10338275SEric Cheng 
10348275SEric Cheng 		if (aip != NULL && c != '=') {
10358275SEric Cheng 			if (aip->ai_count > DLADM_MAX_ARG_VALS)
10368275SEric Cheng 				goto fail;
10378275SEric Cheng 
10388275SEric Cheng 			if (novalues)
10398275SEric Cheng 				goto fail;
10408275SEric Cheng 
10418275SEric Cheng 			aip->ai_val[aip->ai_count] = curr;
10428275SEric Cheng 			aip->ai_count++;
10438275SEric Cheng 		} else {
10448275SEric Cheng 			if (list->al_count > DLADM_MAX_ARG_VALS)
10458275SEric Cheng 				goto fail;
10468275SEric Cheng 
10478275SEric Cheng 			aip = &list->al_info[list->al_count];
10488275SEric Cheng 			aip->ai_name = curr;
10498275SEric Cheng 			aip->ai_count = 0;
10508275SEric Cheng 			list->al_count++;
10518275SEric Cheng 			if (c == ',')
10528275SEric Cheng 				aip = NULL;
10538275SEric Cheng 		}
10548275SEric Cheng 		curr = buf + i + 1;
10558275SEric Cheng 	}
10568275SEric Cheng 
10578275SEric Cheng 	*listp = list;
10588275SEric Cheng 	return (DLADM_STATUS_OK);
10598275SEric Cheng 
10608275SEric Cheng fail:
10618275SEric Cheng 	dladm_free_args(list);
10628275SEric Cheng 	return (DLADM_STATUS_FAILED);
10638275SEric Cheng }
1064*12742Smichael.l.lim@oracle.com 
1065*12742Smichael.l.lim@oracle.com /*
1066*12742Smichael.l.lim@oracle.com  * mac_propval_range_t functions.  Currently implemented for only
1067*12742Smichael.l.lim@oracle.com  * ranges of uint32_t elements, but can be expanded as required.
1068*12742Smichael.l.lim@oracle.com  */
1069*12742Smichael.l.lim@oracle.com /*
1070*12742Smichael.l.lim@oracle.com  * Convert an array of strings (which can be ranges or individual
1071*12742Smichael.l.lim@oracle.com  * elements) into a single mac_propval_range_t structure which
1072*12742Smichael.l.lim@oracle.com  * allocated here but should be freed by the caller.
1073*12742Smichael.l.lim@oracle.com  */
1074*12742Smichael.l.lim@oracle.com dladm_status_t
dladm_strs2range(char ** prop_val,uint_t val_cnt,mac_propval_type_t type,mac_propval_range_t ** range)1075*12742Smichael.l.lim@oracle.com dladm_strs2range(char **prop_val, uint_t val_cnt,
1076*12742Smichael.l.lim@oracle.com 	mac_propval_type_t type, mac_propval_range_t **range)
1077*12742Smichael.l.lim@oracle.com {
1078*12742Smichael.l.lim@oracle.com 	int			i;
1079*12742Smichael.l.lim@oracle.com 	char			*endp;
1080*12742Smichael.l.lim@oracle.com 	mac_propval_range_t	*rangep;
1081*12742Smichael.l.lim@oracle.com 	dladm_status_t		status = DLADM_STATUS_OK;
1082*12742Smichael.l.lim@oracle.com 
1083*12742Smichael.l.lim@oracle.com 	switch (type) {
1084*12742Smichael.l.lim@oracle.com 	case MAC_PROPVAL_UINT32: {
1085*12742Smichael.l.lim@oracle.com 		mac_propval_uint32_range_t	*ur;
1086*12742Smichael.l.lim@oracle.com 
1087*12742Smichael.l.lim@oracle.com 		/* Allocate range structure */
1088*12742Smichael.l.lim@oracle.com 		rangep = malloc(sizeof (mac_propval_range_t) +
1089*12742Smichael.l.lim@oracle.com 		    (val_cnt-1)*(sizeof (mac_propval_uint32_range_t)));
1090*12742Smichael.l.lim@oracle.com 		if (rangep == NULL)
1091*12742Smichael.l.lim@oracle.com 			return (DLADM_STATUS_NOMEM);
1092*12742Smichael.l.lim@oracle.com 
1093*12742Smichael.l.lim@oracle.com 		rangep->mpr_count = 0;
1094*12742Smichael.l.lim@oracle.com 		ur = &rangep->mpr_range_uint32[0];
1095*12742Smichael.l.lim@oracle.com 		for (i = 0; i < val_cnt; i++, ur++) {
1096*12742Smichael.l.lim@oracle.com 			errno = 0;
1097*12742Smichael.l.lim@oracle.com 			if (strchr(prop_val[i], '-') == NULL) {
1098*12742Smichael.l.lim@oracle.com 				/* single element */
1099*12742Smichael.l.lim@oracle.com 				ur->mpur_min = ur->mpur_max =
1100*12742Smichael.l.lim@oracle.com 				    strtol(prop_val[i], &endp, 10);
1101*12742Smichael.l.lim@oracle.com 				if ((endp != NULL) && (*endp != '\0')) {
1102*12742Smichael.l.lim@oracle.com 					return (DLADM_STATUS_BADRANGE);
1103*12742Smichael.l.lim@oracle.com 				}
1104*12742Smichael.l.lim@oracle.com 			} else {
1105*12742Smichael.l.lim@oracle.com 				/* range of elements */
1106*12742Smichael.l.lim@oracle.com 				ur->mpur_min = strtol(prop_val[i], &endp, 10);
1107*12742Smichael.l.lim@oracle.com 				if (*endp++ != '-')
1108*12742Smichael.l.lim@oracle.com 					return (DLADM_STATUS_BADRANGE);
1109*12742Smichael.l.lim@oracle.com 				ur->mpur_max = strtol(endp, &endp, 10);
1110*12742Smichael.l.lim@oracle.com 				if (endp != NULL && *endp != '\0' ||
1111*12742Smichael.l.lim@oracle.com 				    ur->mpur_max < ur->mpur_min)
1112*12742Smichael.l.lim@oracle.com 					return (DLADM_STATUS_BADRANGE);
1113*12742Smichael.l.lim@oracle.com 			}
1114*12742Smichael.l.lim@oracle.com 			rangep->mpr_count++;
1115*12742Smichael.l.lim@oracle.com 		}
1116*12742Smichael.l.lim@oracle.com 		break;
1117*12742Smichael.l.lim@oracle.com 	}
1118*12742Smichael.l.lim@oracle.com 	default:
1119*12742Smichael.l.lim@oracle.com 		return (DLADM_STATUS_BADVAL);
1120*12742Smichael.l.lim@oracle.com 	}
1121*12742Smichael.l.lim@oracle.com 
1122*12742Smichael.l.lim@oracle.com 	rangep->mpr_type = type;
1123*12742Smichael.l.lim@oracle.com 	*range = rangep;
1124*12742Smichael.l.lim@oracle.com 
1125*12742Smichael.l.lim@oracle.com 	return (status);
1126*12742Smichael.l.lim@oracle.com }
1127*12742Smichael.l.lim@oracle.com 
1128*12742Smichael.l.lim@oracle.com /*
1129*12742Smichael.l.lim@oracle.com  * Convert a mac_propval_range_t structure into an array of elements.
1130*12742Smichael.l.lim@oracle.com  */
1131*12742Smichael.l.lim@oracle.com dladm_status_t
dladm_range2list(mac_propval_range_t * rangep,void * elem,uint_t * nelem)1132*12742Smichael.l.lim@oracle.com dladm_range2list(mac_propval_range_t *rangep, void *elem, uint_t *nelem)
1133*12742Smichael.l.lim@oracle.com {
1134*12742Smichael.l.lim@oracle.com 	int		i, j, k;
1135*12742Smichael.l.lim@oracle.com 	dladm_status_t	status = DLADM_STATUS_OK;
1136*12742Smichael.l.lim@oracle.com 
1137*12742Smichael.l.lim@oracle.com 	switch (rangep->mpr_type) {
1138*12742Smichael.l.lim@oracle.com 	case MAC_PROPVAL_UINT32: {
1139*12742Smichael.l.lim@oracle.com 		mac_propval_uint32_range_t	*ur;
1140*12742Smichael.l.lim@oracle.com 		uint32_t			*elem32 = elem;
1141*12742Smichael.l.lim@oracle.com 
1142*12742Smichael.l.lim@oracle.com 		k = 0;
1143*12742Smichael.l.lim@oracle.com 		ur = &rangep->mpr_range_uint32[0];
1144*12742Smichael.l.lim@oracle.com 		for (i = 0; i < rangep->mpr_count; i++, ur++) {
1145*12742Smichael.l.lim@oracle.com 			for (j = 0; j <= ur->mpur_max - ur->mpur_min; j++) {
1146*12742Smichael.l.lim@oracle.com 				elem32[k++] = ur->mpur_min + j;
1147*12742Smichael.l.lim@oracle.com 				if (k > *nelem) {
1148*12742Smichael.l.lim@oracle.com 					status = DLADM_STATUS_TOOMANYELEMENTS;
1149*12742Smichael.l.lim@oracle.com 					break;
1150*12742Smichael.l.lim@oracle.com 				}
1151*12742Smichael.l.lim@oracle.com 			}
1152*12742Smichael.l.lim@oracle.com 		}
1153*12742Smichael.l.lim@oracle.com 		*nelem = k;
1154*12742Smichael.l.lim@oracle.com 		break;
1155*12742Smichael.l.lim@oracle.com 	}
1156*12742Smichael.l.lim@oracle.com 	default:
1157*12742Smichael.l.lim@oracle.com 		status = DLADM_STATUS_BADVAL;
1158*12742Smichael.l.lim@oracle.com 		break;
1159*12742Smichael.l.lim@oracle.com 	}
1160*12742Smichael.l.lim@oracle.com 	return (status);
1161*12742Smichael.l.lim@oracle.com }
1162*12742Smichael.l.lim@oracle.com 
1163*12742Smichael.l.lim@oracle.com /*
1164*12742Smichael.l.lim@oracle.com  * Convert a mac_propval_range_t structure into an array of strings
1165*12742Smichael.l.lim@oracle.com  * of single elements or ranges.
1166*12742Smichael.l.lim@oracle.com  */
1167*12742Smichael.l.lim@oracle.com int
dladm_range2strs(mac_propval_range_t * rangep,char ** prop_val)1168*12742Smichael.l.lim@oracle.com dladm_range2strs(mac_propval_range_t *rangep, char **prop_val)
1169*12742Smichael.l.lim@oracle.com {
1170*12742Smichael.l.lim@oracle.com 	int	i;
1171*12742Smichael.l.lim@oracle.com 
1172*12742Smichael.l.lim@oracle.com 	switch (rangep->mpr_type) {
1173*12742Smichael.l.lim@oracle.com 	case MAC_PROPVAL_UINT32: {
1174*12742Smichael.l.lim@oracle.com 		mac_propval_uint32_range_t	*ur;
1175*12742Smichael.l.lim@oracle.com 
1176*12742Smichael.l.lim@oracle.com 		/* Write ranges and individual elements */
1177*12742Smichael.l.lim@oracle.com 		ur = &rangep->mpr_range_uint32[0];
1178*12742Smichael.l.lim@oracle.com 		for (i = 0; i <= rangep->mpr_count; i++, ur++) {
1179*12742Smichael.l.lim@oracle.com 			if (ur->mpur_min == ur->mpur_max) {
1180*12742Smichael.l.lim@oracle.com 				/* single element */
1181*12742Smichael.l.lim@oracle.com 				(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
1182*12742Smichael.l.lim@oracle.com 				    "%u", ur->mpur_min);
1183*12742Smichael.l.lim@oracle.com 			} else {
1184*12742Smichael.l.lim@oracle.com 				/* range of elements */
1185*12742Smichael.l.lim@oracle.com 				(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
1186*12742Smichael.l.lim@oracle.com 				    "%u-%u", ur->mpur_min, ur->mpur_max);
1187*12742Smichael.l.lim@oracle.com 			}
1188*12742Smichael.l.lim@oracle.com 		}
1189*12742Smichael.l.lim@oracle.com 		return (0);
1190*12742Smichael.l.lim@oracle.com 	}
1191*12742Smichael.l.lim@oracle.com 	default:
1192*12742Smichael.l.lim@oracle.com 		break;
1193*12742Smichael.l.lim@oracle.com 	}
1194*12742Smichael.l.lim@oracle.com 	return (EINVAL);
1195*12742Smichael.l.lim@oracle.com }
1196*12742Smichael.l.lim@oracle.com 
1197*12742Smichael.l.lim@oracle.com static int
uint32cmp(const void * a,const void * b)1198*12742Smichael.l.lim@oracle.com uint32cmp(const void *a, const void *b)
1199*12742Smichael.l.lim@oracle.com {
1200*12742Smichael.l.lim@oracle.com 	return (*(uint32_t *)a - *(uint32_t *)b);
1201*12742Smichael.l.lim@oracle.com }
1202*12742Smichael.l.lim@oracle.com 
1203*12742Smichael.l.lim@oracle.com /*
1204*12742Smichael.l.lim@oracle.com  * Sort and convert an array of elements into a single
1205*12742Smichael.l.lim@oracle.com  * mac_propval_range_t structure which is allocated here but
1206*12742Smichael.l.lim@oracle.com  * should be freed by the caller.
1207*12742Smichael.l.lim@oracle.com  */
1208*12742Smichael.l.lim@oracle.com dladm_status_t
dladm_list2range(void * elem,uint_t nelem,mac_propval_type_t type,mac_propval_range_t ** range)1209*12742Smichael.l.lim@oracle.com dladm_list2range(void *elem, uint_t nelem, mac_propval_type_t type,
1210*12742Smichael.l.lim@oracle.com     mac_propval_range_t **range)
1211*12742Smichael.l.lim@oracle.com {
1212*12742Smichael.l.lim@oracle.com 	int			i;
1213*12742Smichael.l.lim@oracle.com 	uint_t			nr = 0;
1214*12742Smichael.l.lim@oracle.com 	mac_propval_range_t	*rangep;
1215*12742Smichael.l.lim@oracle.com 	dladm_status_t		status = DLADM_STATUS_OK;
1216*12742Smichael.l.lim@oracle.com 
1217*12742Smichael.l.lim@oracle.com 	switch (type) {
1218*12742Smichael.l.lim@oracle.com 	case MAC_PROPVAL_UINT32: {
1219*12742Smichael.l.lim@oracle.com 		mac_propval_uint32_range_t	*ur;
1220*12742Smichael.l.lim@oracle.com 		uint32_t			*elem32 = elem;
1221*12742Smichael.l.lim@oracle.com 		uint32_t			*sort32;
1222*12742Smichael.l.lim@oracle.com 
1223*12742Smichael.l.lim@oracle.com 		/* Allocate range structure */
1224*12742Smichael.l.lim@oracle.com 		rangep = malloc(sizeof (mac_propval_range_t) +
1225*12742Smichael.l.lim@oracle.com 		    (nelem-1)*(sizeof (mac_propval_uint32_range_t)));
1226*12742Smichael.l.lim@oracle.com 		if (rangep == NULL)
1227*12742Smichael.l.lim@oracle.com 			return (DLADM_STATUS_NOMEM);
1228*12742Smichael.l.lim@oracle.com 
1229*12742Smichael.l.lim@oracle.com 		/* Allocate array for sorting */
1230*12742Smichael.l.lim@oracle.com 		sort32 = malloc(nelem * sizeof (uint32_t));
1231*12742Smichael.l.lim@oracle.com 		if (sort32 == NULL) {
1232*12742Smichael.l.lim@oracle.com 			free(rangep);
1233*12742Smichael.l.lim@oracle.com 			return (DLADM_STATUS_NOMEM);
1234*12742Smichael.l.lim@oracle.com 		}
1235*12742Smichael.l.lim@oracle.com 
1236*12742Smichael.l.lim@oracle.com 		/* Copy and sort list */
1237*12742Smichael.l.lim@oracle.com 		for (i = 0; i < nelem; i++)
1238*12742Smichael.l.lim@oracle.com 			sort32[i] =  elem32[i];
1239*12742Smichael.l.lim@oracle.com 		if (nelem > 1)
1240*12742Smichael.l.lim@oracle.com 			qsort(sort32, nelem, sizeof (uint32_t), uint32cmp);
1241*12742Smichael.l.lim@oracle.com 
1242*12742Smichael.l.lim@oracle.com 		/* Convert list to ranges */
1243*12742Smichael.l.lim@oracle.com 		ur = &rangep->mpr_range_uint32[0];
1244*12742Smichael.l.lim@oracle.com 		ur->mpur_min = ur->mpur_max = sort32[0];
1245*12742Smichael.l.lim@oracle.com 		for (i = 1; i < nelem; i++) {
1246*12742Smichael.l.lim@oracle.com 			if (sort32[i]-sort32[i-1] == 1) {
1247*12742Smichael.l.lim@oracle.com 				/* part of current range */
1248*12742Smichael.l.lim@oracle.com 				ur->mpur_max = sort32[i];
1249*12742Smichael.l.lim@oracle.com 			} else {
1250*12742Smichael.l.lim@oracle.com 				/* start a new range */
1251*12742Smichael.l.lim@oracle.com 				nr++; ur++;
1252*12742Smichael.l.lim@oracle.com 				ur->mpur_min = ur->mpur_max = sort32[i];
1253*12742Smichael.l.lim@oracle.com 			}
1254*12742Smichael.l.lim@oracle.com 		}
1255*12742Smichael.l.lim@oracle.com 		free(sort32);
1256*12742Smichael.l.lim@oracle.com 		break;
1257*12742Smichael.l.lim@oracle.com 	}
1258*12742Smichael.l.lim@oracle.com 	default:
1259*12742Smichael.l.lim@oracle.com 		return (DLADM_STATUS_BADRANGE);
1260*12742Smichael.l.lim@oracle.com 	}
1261*12742Smichael.l.lim@oracle.com 
1262*12742Smichael.l.lim@oracle.com 	rangep->mpr_type = type;
1263*12742Smichael.l.lim@oracle.com 	rangep->mpr_count = nr + 1;
1264*12742Smichael.l.lim@oracle.com 	*range = rangep;
1265*12742Smichael.l.lim@oracle.com 
1266*12742Smichael.l.lim@oracle.com 	return (status);
1267*12742Smichael.l.lim@oracle.com }
1268