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 /*
2263a6526dSMichael Lim  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <unistd.h>
277c478bd9Sstevel@tonic-gate #include <errno.h>
28d62bc4baSyz #include <ctype.h>
290ba2cbe9Sxc #include <fcntl.h>
300ba2cbe9Sxc #include <strings.h>
310ba2cbe9Sxc #include <dirent.h>
32da14cebeSEric Cheng #include <stdlib.h>
33*25ec3e3dSEric Cheng #include <netinet/in.h>
34*25ec3e3dSEric Cheng #include <arpa/inet.h>
35b9e076dcSyz #include <sys/param.h>
360ba2cbe9Sxc #include <sys/stat.h>
374ac67f02SAnurag S. Maskey #include <sys/dld.h>
3882a2fc47SJames Carlson #include <sys/dld_ioc.h>
390ba2cbe9Sxc #include <libdladm_impl.h>
4033343a97Smeem #include <libintl.h>
41d62bc4baSyz #include <libdlpi.h>
42aae21359Skrgopi 
434ac67f02SAnurag S. Maskey static char	dladm_rootdir[MAXPATHLEN] = "/";
444ac67f02SAnurag S. Maskey 
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 
85*25ec3e3dSEric Cheng typedef struct {
86*25ec3e3dSEric Cheng 	uint32_t	lp_type;
87*25ec3e3dSEric Cheng 	char		*lp_name;
88*25ec3e3dSEric Cheng } link_protect_t;
89*25ec3e3dSEric Cheng 
90*25ec3e3dSEric Cheng static link_protect_t link_protect_types[] = {
91*25ec3e3dSEric Cheng 	{ MPT_MACNOSPOOF, "mac-nospoof" },
92*25ec3e3dSEric Cheng 	{ MPT_IPNOSPOOF, "ip-nospoof" },
93*25ec3e3dSEric Cheng 	{ MPT_RESTRICTED, "restricted" }
94*25ec3e3dSEric Cheng };
95*25ec3e3dSEric Cheng #define	LPTYPES	(sizeof (link_protect_types) / sizeof (link_protect_t))
96*25ec3e3dSEric Cheng 
974ac67f02SAnurag S. Maskey dladm_status_t
984ac67f02SAnurag S. Maskey dladm_open(dladm_handle_t *handle)
994ac67f02SAnurag S. Maskey {
1004ac67f02SAnurag S. Maskey 	int dld_fd;
1014ac67f02SAnurag S. Maskey 
1024ac67f02SAnurag S. Maskey 	if (handle == NULL)
1034ac67f02SAnurag S. Maskey 		return (DLADM_STATUS_BADARG);
1044ac67f02SAnurag S. Maskey 
1054ac67f02SAnurag S. Maskey 	if ((dld_fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1064ac67f02SAnurag S. Maskey 		return (dladm_errno2status(errno));
1074ac67f02SAnurag S. Maskey 
1084ac67f02SAnurag S. Maskey 	/*
1094ac67f02SAnurag S. Maskey 	 * Don't open DLMGMT_DOOR now.  dlmgmtd(1M) is not able to
1104ac67f02SAnurag S. Maskey 	 * open the door when the dladm handle is opened because the
1114ac67f02SAnurag S. Maskey 	 * door hasn't been created yet at that time.  Thus, we must
1124ac67f02SAnurag S. Maskey 	 * open it on-demand in dladm_door_fd().  Move the open()
1134ac67f02SAnurag S. Maskey 	 * to dladm_door_fd() for all cases.
1144ac67f02SAnurag S. Maskey 	 */
1154ac67f02SAnurag S. Maskey 
1164ac67f02SAnurag S. Maskey 	if ((*handle = malloc(sizeof (struct dladm_handle))) == NULL) {
1174ac67f02SAnurag S. Maskey 		(void) close(dld_fd);
1184ac67f02SAnurag S. Maskey 		return (DLADM_STATUS_NOMEM);
1194ac67f02SAnurag S. Maskey 	}
1204ac67f02SAnurag S. Maskey 
1214ac67f02SAnurag S. Maskey 	(*handle)->dld_fd = dld_fd;
1224ac67f02SAnurag S. Maskey 	(*handle)->door_fd = -1;
1234ac67f02SAnurag S. Maskey 
1244ac67f02SAnurag S. Maskey 	return (DLADM_STATUS_OK);
1254ac67f02SAnurag S. Maskey }
1264ac67f02SAnurag S. Maskey 
1274ac67f02SAnurag S. Maskey void
1284ac67f02SAnurag S. Maskey dladm_close(dladm_handle_t handle)
1294ac67f02SAnurag S. Maskey {
1304ac67f02SAnurag S. Maskey 	if (handle != NULL) {
1314ac67f02SAnurag S. Maskey 		(void) close(handle->dld_fd);
1324ac67f02SAnurag S. Maskey 		if (handle->door_fd != -1)
1334ac67f02SAnurag S. Maskey 			(void) close(handle->door_fd);
1344ac67f02SAnurag S. Maskey 		free(handle);
1354ac67f02SAnurag S. Maskey 	}
1364ac67f02SAnurag S. Maskey }
1374ac67f02SAnurag S. Maskey 
1384ac67f02SAnurag S. Maskey int
1394ac67f02SAnurag S. Maskey dladm_dld_fd(dladm_handle_t handle)
1404ac67f02SAnurag S. Maskey {
1414ac67f02SAnurag S. Maskey 	return (handle->dld_fd);
1424ac67f02SAnurag S. Maskey }
1434ac67f02SAnurag S. Maskey 
1444ac67f02SAnurag S. Maskey /*
1454ac67f02SAnurag S. Maskey  * If DLMGMT_DOOR hasn't been opened in the handle yet, open it.
1464ac67f02SAnurag S. Maskey  */
1474ac67f02SAnurag S. Maskey dladm_status_t
1484ac67f02SAnurag S. Maskey dladm_door_fd(dladm_handle_t handle, int *door_fd)
1494ac67f02SAnurag S. Maskey {
1504ac67f02SAnurag S. Maskey 	int fd;
1514ac67f02SAnurag S. Maskey 
1524ac67f02SAnurag S. Maskey 	if (handle->door_fd == -1) {
1534ac67f02SAnurag S. Maskey 		if ((fd = open(DLMGMT_DOOR, O_RDONLY)) < 0)
1544ac67f02SAnurag S. Maskey 			return (dladm_errno2status(errno));
1554ac67f02SAnurag S. Maskey 		handle->door_fd = fd;
1564ac67f02SAnurag S. Maskey 	}
1574ac67f02SAnurag S. Maskey 	*door_fd = handle->door_fd;
1584ac67f02SAnurag S. Maskey 
1594ac67f02SAnurag S. Maskey 	return (DLADM_STATUS_OK);
1604ac67f02SAnurag S. Maskey }
1610ba2cbe9Sxc 
1620ba2cbe9Sxc const char *
1630ba2cbe9Sxc dladm_status2str(dladm_status_t status, char *buf)
1640ba2cbe9Sxc {
1650ba2cbe9Sxc 	const char	*s;
1660ba2cbe9Sxc 
1670ba2cbe9Sxc 	switch (status) {
1680ba2cbe9Sxc 	case DLADM_STATUS_OK:
1690ba2cbe9Sxc 		s = "ok";
1700ba2cbe9Sxc 		break;
1710ba2cbe9Sxc 	case DLADM_STATUS_BADARG:
1720ba2cbe9Sxc 		s = "invalid argument";
1730ba2cbe9Sxc 		break;
1740ba2cbe9Sxc 	case DLADM_STATUS_FAILED:
1750ba2cbe9Sxc 		s = "operation failed";
1760ba2cbe9Sxc 		break;
1770ba2cbe9Sxc 	case DLADM_STATUS_TOOSMALL:
1780ba2cbe9Sxc 		s = "buffer size too small";
1790ba2cbe9Sxc 		break;
1800ba2cbe9Sxc 	case DLADM_STATUS_NOTSUP:
1810ba2cbe9Sxc 		s = "operation not supported";
1820ba2cbe9Sxc 		break;
1830ba2cbe9Sxc 	case DLADM_STATUS_NOTFOUND:
1840ba2cbe9Sxc 		s = "object not found";
1850ba2cbe9Sxc 		break;
1860ba2cbe9Sxc 	case DLADM_STATUS_BADVAL:
1870ba2cbe9Sxc 		s = "invalid value";
1880ba2cbe9Sxc 		break;
1890ba2cbe9Sxc 	case DLADM_STATUS_NOMEM:
1900ba2cbe9Sxc 		s = "insufficient memory";
1910ba2cbe9Sxc 		break;
1920ba2cbe9Sxc 	case DLADM_STATUS_EXIST:
1930ba2cbe9Sxc 		s = "object already exists";
1940ba2cbe9Sxc 		break;
1950ba2cbe9Sxc 	case DLADM_STATUS_LINKINVAL:
1960ba2cbe9Sxc 		s = "invalid link";
1970ba2cbe9Sxc 		break;
1980ba2cbe9Sxc 	case DLADM_STATUS_PROPRDONLY:
1990ba2cbe9Sxc 		s = "read-only property";
2000ba2cbe9Sxc 		break;
2010ba2cbe9Sxc 	case DLADM_STATUS_BADVALCNT:
2020ba2cbe9Sxc 		s = "invalid number of values";
2030ba2cbe9Sxc 		break;
2040ba2cbe9Sxc 	case DLADM_STATUS_DBNOTFOUND:
2050ba2cbe9Sxc 		s = "database not found";
2060ba2cbe9Sxc 		break;
2070ba2cbe9Sxc 	case DLADM_STATUS_DENIED:
2080ba2cbe9Sxc 		s = "permission denied";
2090ba2cbe9Sxc 		break;
2100ba2cbe9Sxc 	case DLADM_STATUS_IOERR:
2110ba2cbe9Sxc 		s = "I/O error";
2120ba2cbe9Sxc 		break;
213f4b3ec61Sdh 	case DLADM_STATUS_TEMPONLY:
214da14cebeSEric Cheng 		s = "change cannot be persistent";
215f4b3ec61Sdh 		break;
216f595a68aSyz 	case DLADM_STATUS_TIMEDOUT:
217f595a68aSyz 		s = "operation timed out";
218f595a68aSyz 		break;
219f595a68aSyz 	case DLADM_STATUS_ISCONN:
220f595a68aSyz 		s = "already connected";
221f595a68aSyz 		break;
222f595a68aSyz 	case DLADM_STATUS_NOTCONN:
223f595a68aSyz 		s = "not connected";
224f595a68aSyz 		break;
225f595a68aSyz 	case DLADM_STATUS_REPOSITORYINVAL:
226f595a68aSyz 		s = "invalid configuration repository";
227f595a68aSyz 		break;
228f595a68aSyz 	case DLADM_STATUS_MACADDRINVAL:
229f595a68aSyz 		s = "invalid MAC address";
230f595a68aSyz 		break;
231f595a68aSyz 	case DLADM_STATUS_KEYINVAL:
232f595a68aSyz 		s = "invalid key";
233f595a68aSyz 		break;
234843e1988Sjohnlev 	case DLADM_STATUS_INVALIDMACADDRLEN:
235843e1988Sjohnlev 		s = "invalid MAC address length";
236843e1988Sjohnlev 		break;
237843e1988Sjohnlev 	case DLADM_STATUS_INVALIDMACADDRTYPE:
238843e1988Sjohnlev 		s = "invalid MAC address type";
239843e1988Sjohnlev 		break;
240d62bc4baSyz 	case DLADM_STATUS_LINKBUSY:
241d62bc4baSyz 		s = "link busy";
242d62bc4baSyz 		break;
243d62bc4baSyz 	case DLADM_STATUS_VIDINVAL:
244d62bc4baSyz 		s = "invalid VLAN identifier";
245843e1988Sjohnlev 		break;
246d62bc4baSyz 	case DLADM_STATUS_TRYAGAIN:
247d62bc4baSyz 		s = "try again later";
248843e1988Sjohnlev 		break;
249d62bc4baSyz 	case DLADM_STATUS_NONOTIF:
250d62bc4baSyz 		s = "link notification is not supported";
251843e1988Sjohnlev 		break;
252da14cebeSEric Cheng 	case DLADM_STATUS_BADTIMEVAL:
253da14cebeSEric Cheng 		s = "invalid time range";
254da14cebeSEric Cheng 		break;
255da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACADDR:
256da14cebeSEric Cheng 		s = "invalid MAC address value";
257da14cebeSEric Cheng 		break;
258da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACADDRNIC:
259da14cebeSEric Cheng 		s = "MAC address reserved for use by underlying data-link";
260da14cebeSEric Cheng 		break;
261da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACADDRINUSE:
262da14cebeSEric Cheng 		s = "MAC address is already in use";
263da14cebeSEric Cheng 		break;
264da14cebeSEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTINVALID:
265da14cebeSEric Cheng 		s = "invalid factory MAC address slot";
266da14cebeSEric Cheng 		break;
267da14cebeSEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTUSED:
268da14cebeSEric Cheng 		s = "factory MAC address slot already used";
269da14cebeSEric Cheng 		break;
270da14cebeSEric Cheng 	case DLADM_STATUS_MACFACTORYSLOTALLUSED:
271da14cebeSEric Cheng 		s = "all factory MAC address slots are in use";
272da14cebeSEric Cheng 		break;
273da14cebeSEric Cheng 	case DLADM_STATUS_MACFACTORYNOTSUP:
274da14cebeSEric Cheng 		s = "factory MAC address slots not supported";
275da14cebeSEric Cheng 		break;
276da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACPREFIX:
277da14cebeSEric Cheng 		s = "Invalid MAC address prefix value";
278da14cebeSEric Cheng 		break;
279da14cebeSEric Cheng 	case DLADM_STATUS_INVALIDMACPREFIXLEN:
280da14cebeSEric Cheng 		s = "Invalid MAC address prefix length";
281da14cebeSEric Cheng 		break;
282da14cebeSEric Cheng 	case DLADM_STATUS_CPUMAX:
283da14cebeSEric Cheng 		s = "non-existent processor ID";
284da14cebeSEric Cheng 		break;
285da14cebeSEric Cheng 	case DLADM_STATUS_CPUERR:
286da14cebeSEric Cheng 		s = "could not determine processor status";
287da14cebeSEric Cheng 		break;
288da14cebeSEric Cheng 	case DLADM_STATUS_CPUNOTONLINE:
289da14cebeSEric Cheng 		s = "processor not online";
290da14cebeSEric Cheng 		break;
291da14cebeSEric Cheng 	case DLADM_STATUS_DB_NOTFOUND:
292da14cebeSEric Cheng 		s = "database not found";
293da14cebeSEric Cheng 		break;
294da14cebeSEric Cheng 	case DLADM_STATUS_DB_PARSE_ERR:
295da14cebeSEric Cheng 		s = "database parse error";
296da14cebeSEric Cheng 		break;
297da14cebeSEric Cheng 	case DLADM_STATUS_PROP_PARSE_ERR:
298da14cebeSEric Cheng 		s = "property parse error";
299da14cebeSEric Cheng 		break;
300da14cebeSEric Cheng 	case DLADM_STATUS_ATTR_PARSE_ERR:
301da14cebeSEric Cheng 		s = "attribute parse error";
302da14cebeSEric Cheng 		break;
303da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_DB_ERR:
304da14cebeSEric Cheng 		s = "flow database error";
305da14cebeSEric Cheng 		break;
306da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_DB_OPEN_ERR:
307da14cebeSEric Cheng 		s = "flow database open error";
308da14cebeSEric Cheng 		break;
309da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_DB_PARSE_ERR:
310da14cebeSEric Cheng 		s = "flow database parse error";
311da14cebeSEric Cheng 		break;
312da14cebeSEric Cheng 	case DLADM_STATUS_FLOWPROP_DB_PARSE_ERR:
313da14cebeSEric Cheng 		s = "flow property database parse error";
314da14cebeSEric Cheng 		break;
315da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_ADD_ERR:
316da14cebeSEric Cheng 		s = "flow add error";
317da14cebeSEric Cheng 		break;
318da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_WALK_ERR:
319da14cebeSEric Cheng 		s = "flow walk error";
320da14cebeSEric Cheng 		break;
321da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_IDENTICAL:
322da14cebeSEric Cheng 		s = "a flow with identical attributes exists";
323da14cebeSEric Cheng 		break;
324da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_INCOMPATIBLE:
325da14cebeSEric Cheng 		s = "flow(s) with incompatible attributes exists";
326da14cebeSEric Cheng 		break;
327da14cebeSEric Cheng 	case DLADM_STATUS_FLOW_EXISTS:
328da14cebeSEric Cheng 		s = "link still has flows";
329da14cebeSEric Cheng 		break;
330da14cebeSEric Cheng 	case DLADM_STATUS_PERSIST_FLOW_EXISTS:
331da14cebeSEric Cheng 		s = "persistent flow with the same name exists";
332da14cebeSEric Cheng 		break;
333da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_IP:
334da14cebeSEric Cheng 		s = "invalid IP address";
335da14cebeSEric Cheng 		break;
336da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_PREFIXLEN:
337da14cebeSEric Cheng 		s = "invalid IP prefix length";
338da14cebeSEric Cheng 		break;
339da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_PROTOCOL:
340da14cebeSEric Cheng 		s = "invalid IP protocol";
341da14cebeSEric Cheng 		break;
342da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_PORT:
343da14cebeSEric Cheng 		s = "invalid port number";
344da14cebeSEric Cheng 		break;
345da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_DSF:
346da14cebeSEric Cheng 		s = "invalid dsfield";
347da14cebeSEric Cheng 		break;
348da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_DSFMASK:
349da14cebeSEric Cheng 		s = "invalid dsfield mask";
350da14cebeSEric Cheng 		break;
351da14cebeSEric Cheng 	case DLADM_STATUS_INVALID_MACMARGIN:
352da14cebeSEric Cheng 		s = "MTU check failed, use lower MTU or -f option";
353da14cebeSEric Cheng 		break;
354da14cebeSEric Cheng 	case DLADM_STATUS_BADPROP:
355da14cebeSEric Cheng 		s = "invalid property";
356da14cebeSEric Cheng 		break;
357da14cebeSEric Cheng 	case DLADM_STATUS_MINMAXBW:
358da14cebeSEric Cheng 		s = "minimum value for maxbw is 1.2M";
359da14cebeSEric Cheng 		break;
360da14cebeSEric Cheng 	case DLADM_STATUS_NO_HWRINGS:
361da14cebeSEric Cheng 		s = "request hw rings failed";
362da14cebeSEric Cheng 		break;
3634eaa4710SRishi Srivatsavai 	case DLADM_STATUS_PERMONLY:
3644eaa4710SRishi Srivatsavai 		s = "change must be persistent";
3654eaa4710SRishi Srivatsavai 		break;
3664eaa4710SRishi Srivatsavai 	case DLADM_STATUS_OPTMISSING:
3674eaa4710SRishi Srivatsavai 		s = "optional software not installed";
3684eaa4710SRishi Srivatsavai 		break;
3692b24ab6bSSebastien Roy 	case DLADM_STATUS_IPTUNTYPE:
3702b24ab6bSSebastien Roy 		s = "invalid IP tunnel type";
3712b24ab6bSSebastien Roy 		break;
3722b24ab6bSSebastien Roy 	case DLADM_STATUS_IPTUNTYPEREQD:
3732b24ab6bSSebastien Roy 		s = "IP tunnel type required";
3742b24ab6bSSebastien Roy 		break;
3752b24ab6bSSebastien Roy 	case DLADM_STATUS_BADIPTUNLADDR:
3762b24ab6bSSebastien Roy 		s = "invalid local IP tunnel address";
3772b24ab6bSSebastien Roy 		break;
3782b24ab6bSSebastien Roy 	case DLADM_STATUS_BADIPTUNRADDR:
3792b24ab6bSSebastien Roy 		s = "invalid remote IP tunnel address";
3802b24ab6bSSebastien Roy 		break;
3812b24ab6bSSebastien Roy 	case DLADM_STATUS_ADDRINUSE:
3822b24ab6bSSebastien Roy 		s = "address already in use";
3832b24ab6bSSebastien Roy 		break;
3840ba2cbe9Sxc 	default:
38533343a97Smeem 		s = "<unknown error>";
38633343a97Smeem 		break;
3870ba2cbe9Sxc 	}
38833343a97Smeem 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
3890ba2cbe9Sxc 	return (buf);
3900ba2cbe9Sxc }
3910ba2cbe9Sxc 
3920ba2cbe9Sxc /*
3930ba2cbe9Sxc  * Convert a unix errno to a dladm_status_t.
3940ba2cbe9Sxc  * We only convert errnos that are likely to be encountered. All others
3950ba2cbe9Sxc  * are mapped to DLADM_STATUS_FAILED.
3960ba2cbe9Sxc  */
3970ba2cbe9Sxc dladm_status_t
3980ba2cbe9Sxc dladm_errno2status(int err)
3990ba2cbe9Sxc {
4000ba2cbe9Sxc 	switch (err) {
401e7801d59Ssowmini 	case 0:
402e7801d59Ssowmini 		return (DLADM_STATUS_OK);
4030ba2cbe9Sxc 	case EINVAL:
4040ba2cbe9Sxc 		return (DLADM_STATUS_BADARG);
4050ba2cbe9Sxc 	case EEXIST:
4060ba2cbe9Sxc 		return (DLADM_STATUS_EXIST);
4070ba2cbe9Sxc 	case ENOENT:
4080ba2cbe9Sxc 		return (DLADM_STATUS_NOTFOUND);
4090ba2cbe9Sxc 	case ENOSPC:
4100ba2cbe9Sxc 		return (DLADM_STATUS_TOOSMALL);
4110ba2cbe9Sxc 	case ENOMEM:
4120ba2cbe9Sxc 		return (DLADM_STATUS_NOMEM);
4130ba2cbe9Sxc 	case ENOTSUP:
4140ba2cbe9Sxc 		return (DLADM_STATUS_NOTSUP);
415d62bc4baSyz 	case ENETDOWN:
416d62bc4baSyz 		return (DLADM_STATUS_NONOTIF);
4170ba2cbe9Sxc 	case EACCES:
418eae72b5bSSebastien Roy 	case EPERM:
4190ba2cbe9Sxc 		return (DLADM_STATUS_DENIED);
4200ba2cbe9Sxc 	case EIO:
4210ba2cbe9Sxc 		return (DLADM_STATUS_IOERR);
422843e1988Sjohnlev 	case EBUSY:
423d62bc4baSyz 		return (DLADM_STATUS_LINKBUSY);
424d62bc4baSyz 	case EAGAIN:
425d62bc4baSyz 		return (DLADM_STATUS_TRYAGAIN);
426da14cebeSEric Cheng 	case ENOTEMPTY:
427da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_EXISTS);
428da14cebeSEric Cheng 	case EOPNOTSUPP:
429da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_INCOMPATIBLE);
430da14cebeSEric Cheng 	case EALREADY:
431da14cebeSEric Cheng 		return (DLADM_STATUS_FLOW_IDENTICAL);
4322b24ab6bSSebastien Roy 	case EADDRINUSE:
4332b24ab6bSSebastien Roy 		return (DLADM_STATUS_ADDRINUSE);
4340ba2cbe9Sxc 	default:
4350ba2cbe9Sxc 		return (DLADM_STATUS_FAILED);
4360ba2cbe9Sxc 	}
4370ba2cbe9Sxc }
4380ba2cbe9Sxc 
43963a6526dSMichael Lim boolean_t
44063a6526dSMichael Lim dladm_str2interval(char *oarg, uint32_t *interval)
44163a6526dSMichael Lim {
44263a6526dSMichael Lim 	int		val;
44363a6526dSMichael Lim 	char		*endp = NULL;
44463a6526dSMichael Lim 
44563a6526dSMichael Lim 	errno = 0;
44663a6526dSMichael Lim 	val = strtol(oarg, &endp, 10);
44763a6526dSMichael Lim 	if (errno != 0 || val <= 0 || *endp != '\0')
44863a6526dSMichael Lim 		return (B_FALSE);
44963a6526dSMichael Lim 
45063a6526dSMichael Lim 	*interval = val;
45163a6526dSMichael Lim 
45263a6526dSMichael Lim 	return (B_TRUE);
45363a6526dSMichael Lim }
45463a6526dSMichael Lim 
455da14cebeSEric Cheng dladm_status_t
456da14cebeSEric Cheng dladm_str2bw(char *oarg, uint64_t *bw)
457da14cebeSEric Cheng {
458da14cebeSEric Cheng 	char		*endp = NULL;
459da14cebeSEric Cheng 	int64_t		n;
460da14cebeSEric Cheng 	int		mult = 1;
461da14cebeSEric Cheng 
462da14cebeSEric Cheng 	n = strtoull(oarg, &endp, 10);
463da14cebeSEric Cheng 
464da14cebeSEric Cheng 	if ((errno != 0) || (strlen(endp) > 1))
465da14cebeSEric Cheng 		return (DLADM_STATUS_BADARG);
466da14cebeSEric Cheng 
467da14cebeSEric Cheng 	if (n < 0)
468da14cebeSEric Cheng 		return (DLADM_STATUS_BADVAL);
469da14cebeSEric Cheng 
470da14cebeSEric Cheng 	switch (*endp) {
471da14cebeSEric Cheng 	case 'k':
472da14cebeSEric Cheng 	case 'K':
473da14cebeSEric Cheng 		mult = 1000;
474da14cebeSEric Cheng 		break;
475da14cebeSEric Cheng 	case 'm':
476da14cebeSEric Cheng 	case 'M':
477da14cebeSEric Cheng 	case '\0':
478da14cebeSEric Cheng 		mult = 1000000;
479da14cebeSEric Cheng 		break;
480da14cebeSEric Cheng 	case 'g':
481da14cebeSEric Cheng 	case 'G':
482da14cebeSEric Cheng 		mult = 1000000000;
483da14cebeSEric Cheng 		break;
484da14cebeSEric Cheng 	case '%':
485da14cebeSEric Cheng 		/*
486da14cebeSEric Cheng 		 * percentages not supported for now,
487da14cebeSEric Cheng 		 * see RFE 6540675
488da14cebeSEric Cheng 		 */
489da14cebeSEric Cheng 		return (DLADM_STATUS_NOTSUP);
490da14cebeSEric Cheng 	default:
491da14cebeSEric Cheng 		return (DLADM_STATUS_BADVAL);
492da14cebeSEric Cheng 	}
493da14cebeSEric Cheng 
494da14cebeSEric Cheng 	*bw = n * mult;
495da14cebeSEric Cheng 
496da14cebeSEric Cheng 	/* check for overflow */
497da14cebeSEric Cheng 	if (*bw / mult != n)
498da14cebeSEric Cheng 		return (DLADM_STATUS_BADARG);
499da14cebeSEric Cheng 
500da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
501da14cebeSEric Cheng }
502da14cebeSEric Cheng 
503da14cebeSEric Cheng /*
504da14cebeSEric Cheng  * Convert bandwidth in bps to a string in mpbs.  For values greater
505da14cebeSEric Cheng  * than 1mbps or 1000000, print a whole mbps value.  For values that
506da14cebeSEric Cheng  * have fractional Mbps in whole Kbps , print the bandwidth in a manner
507da14cebeSEric Cheng  * simlilar to a floating point format.
508da14cebeSEric Cheng  *
509da14cebeSEric Cheng  *        bps       string
510da14cebeSEric Cheng  *          0            0
511da14cebeSEric Cheng  *        100            0
512da14cebeSEric Cheng  *       2000        0.002
513da14cebeSEric Cheng  *     431000        0.431
514da14cebeSEric Cheng  *    1000000            1
515da14cebeSEric Cheng  *    1030000        1.030
516da14cebeSEric Cheng  *  100000000          100
517da14cebeSEric Cheng  */
518da14cebeSEric Cheng const char *
519da14cebeSEric Cheng dladm_bw2str(int64_t bw, char *buf)
520da14cebeSEric Cheng {
521da14cebeSEric Cheng 	int kbps, mbps;
522da14cebeSEric Cheng 
523da14cebeSEric Cheng 	kbps = (bw%1000000)/1000;
524da14cebeSEric Cheng 	mbps = bw/1000000;
525da14cebeSEric Cheng 	if (kbps != 0) {
526da14cebeSEric Cheng 		if (mbps == 0)
527da14cebeSEric Cheng 			(void) snprintf(buf, DLADM_STRSIZE, "0.%03u", kbps);
528da14cebeSEric Cheng 		else
529da14cebeSEric Cheng 			(void) snprintf(buf, DLADM_STRSIZE, "%5u.%03u", mbps,
530da14cebeSEric Cheng 			    kbps);
531da14cebeSEric Cheng 	} else {
532da14cebeSEric Cheng 		(void) snprintf(buf, DLADM_STRSIZE, "%5u", mbps);
533da14cebeSEric Cheng 	}
534da14cebeSEric Cheng 
535da14cebeSEric Cheng 	return (buf);
536da14cebeSEric Cheng }
537da14cebeSEric Cheng 
538d62bc4baSyz #define	LOCK_DB_PERMS	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
5390ba2cbe9Sxc 
5400ba2cbe9Sxc static int
5410ba2cbe9Sxc i_dladm_lock_db(const char *lock_file, short type)
5420ba2cbe9Sxc {
5430ba2cbe9Sxc 	int	lock_fd;
544d62bc4baSyz 	struct	flock lock;
5450ba2cbe9Sxc 
5460ba2cbe9Sxc 	if ((lock_fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC,
5470ba2cbe9Sxc 	    LOCK_DB_PERMS)) < 0)
5480ba2cbe9Sxc 		return (-1);
5490ba2cbe9Sxc 
5500ba2cbe9Sxc 	lock.l_type = type;
5510ba2cbe9Sxc 	lock.l_whence = SEEK_SET;
5520ba2cbe9Sxc 	lock.l_start = 0;
5530ba2cbe9Sxc 	lock.l_len = 0;
5540ba2cbe9Sxc 
5550ba2cbe9Sxc 	if (fcntl(lock_fd, F_SETLKW, &lock) < 0) {
5560ba2cbe9Sxc 		int err = errno;
5570ba2cbe9Sxc 
5580ba2cbe9Sxc 		(void) close(lock_fd);
5590ba2cbe9Sxc 		(void) unlink(lock_file);
5600ba2cbe9Sxc 		errno = err;
5610ba2cbe9Sxc 		return (-1);
5620ba2cbe9Sxc 	}
5630ba2cbe9Sxc 	return (lock_fd);
5640ba2cbe9Sxc }
5650ba2cbe9Sxc 
5660ba2cbe9Sxc static void
5670ba2cbe9Sxc i_dladm_unlock_db(const char *lock_file, int fd)
5680ba2cbe9Sxc {
5690ba2cbe9Sxc 	struct flock lock;
5700ba2cbe9Sxc 
5710ba2cbe9Sxc 	if (fd < 0)
5720ba2cbe9Sxc 		return;
5730ba2cbe9Sxc 
5740ba2cbe9Sxc 	lock.l_type = F_UNLCK;
5750ba2cbe9Sxc 	lock.l_whence = SEEK_SET;
5760ba2cbe9Sxc 	lock.l_start = 0;
5770ba2cbe9Sxc 	lock.l_len = 0;
5780ba2cbe9Sxc 
5790ba2cbe9Sxc 	(void) fcntl(fd, F_SETLKW, &lock);
5800ba2cbe9Sxc 	(void) close(fd);
5810ba2cbe9Sxc 	(void) unlink(lock_file);
5820ba2cbe9Sxc }
5830ba2cbe9Sxc 
584d62bc4baSyz /*
585d62bc4baSyz  * Given a link class, returns its class string.
586d62bc4baSyz  */
587d62bc4baSyz const char *
588d62bc4baSyz dladm_class2str(datalink_class_t class, char *buf)
589d62bc4baSyz {
590d62bc4baSyz 	const char *s;
591d62bc4baSyz 
592d62bc4baSyz 	switch (class) {
593d62bc4baSyz 	case DATALINK_CLASS_PHYS:
594d62bc4baSyz 		s = "phys";
595d62bc4baSyz 		break;
596d62bc4baSyz 	case DATALINK_CLASS_VLAN:
597d62bc4baSyz 		s = "vlan";
598d62bc4baSyz 		break;
599d62bc4baSyz 	case DATALINK_CLASS_AGGR:
600d62bc4baSyz 		s = "aggr";
601d62bc4baSyz 		break;
602d62bc4baSyz 	case DATALINK_CLASS_VNIC:
603d62bc4baSyz 		s = "vnic";
604d62bc4baSyz 		break;
605da14cebeSEric Cheng 	case DATALINK_CLASS_ETHERSTUB:
606da14cebeSEric Cheng 		s = "etherstub";
607da14cebeSEric Cheng 		break;
6082b24ab6bSSebastien Roy 	case DATALINK_CLASS_IPTUN:
6092b24ab6bSSebastien Roy 		s = "iptun";
6102b24ab6bSSebastien Roy 		break;
611b509e89bSRishi Srivatsavai 	case DATALINK_CLASS_SIMNET:
612b509e89bSRishi Srivatsavai 		s = "simnet";
613b509e89bSRishi Srivatsavai 		break;
6144eaa4710SRishi Srivatsavai 	case DATALINK_CLASS_BRIDGE:
6154eaa4710SRishi Srivatsavai 		s = "bridge";
6164eaa4710SRishi Srivatsavai 		break;
617d62bc4baSyz 	default:
618d62bc4baSyz 		s = "unknown";
619d62bc4baSyz 		break;
620d62bc4baSyz 	}
621d62bc4baSyz 
622d62bc4baSyz 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
623d62bc4baSyz 	return (buf);
624d62bc4baSyz }
625d62bc4baSyz 
626d62bc4baSyz /*
627d62bc4baSyz  * Given a physical link media type, returns its media type string.
628d62bc4baSyz  */
629d62bc4baSyz const char *
630d62bc4baSyz dladm_media2str(uint32_t media, char *buf)
631d62bc4baSyz {
632b509e89bSRishi Srivatsavai 	const char *s = "--";
633b509e89bSRishi Srivatsavai 	media_type_t *mt;
634b509e89bSRishi Srivatsavai 	int idx;
635b509e89bSRishi Srivatsavai 
636b509e89bSRishi Srivatsavai 	for (idx = 0; idx < MEDIATYPECOUNT; idx++) {
637b509e89bSRishi Srivatsavai 		mt = media_type_table + idx;
638b509e89bSRishi Srivatsavai 		if (mt->media_type == media) {
639b509e89bSRishi Srivatsavai 			s = mt->media_type_str;
640b509e89bSRishi Srivatsavai 			break;
641b509e89bSRishi Srivatsavai 		}
642d62bc4baSyz 	}
643d62bc4baSyz 
644d62bc4baSyz 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
645d62bc4baSyz 	return (buf);
646d62bc4baSyz }
647d62bc4baSyz 
648b509e89bSRishi Srivatsavai /*
649b509e89bSRishi Srivatsavai  * Given a physical link media type string, returns its media type constant.
650b509e89bSRishi Srivatsavai  */
651b509e89bSRishi Srivatsavai uint32_t
652b509e89bSRishi Srivatsavai dladm_str2media(const char *buf)
653b509e89bSRishi Srivatsavai {
654b509e89bSRishi Srivatsavai 	media_type_t *mt;
655b509e89bSRishi Srivatsavai 	int idx;
656b509e89bSRishi Srivatsavai 
657b509e89bSRishi Srivatsavai 	for (idx = 0; idx < MEDIATYPECOUNT; idx++) {
658b509e89bSRishi Srivatsavai 		mt = media_type_table + idx;
659b509e89bSRishi Srivatsavai 		if (strcasecmp(buf, mt->media_type_str) == 0)
660b509e89bSRishi Srivatsavai 			return (mt->media_type);
661b509e89bSRishi Srivatsavai 	}
662b509e89bSRishi Srivatsavai 
663b509e89bSRishi Srivatsavai 	return (DL_OTHER);
664b509e89bSRishi Srivatsavai }
665b509e89bSRishi Srivatsavai 
6660ba2cbe9Sxc dladm_status_t
6674ac67f02SAnurag S. Maskey i_dladm_rw_db(dladm_handle_t handle, const char *db_file, mode_t db_perms,
6684ac67f02SAnurag S. Maskey     dladm_status_t (*process_db)(dladm_handle_t, void *, FILE *, FILE *),
6690ba2cbe9Sxc     void *arg, boolean_t writeop)
6700ba2cbe9Sxc {
6710ba2cbe9Sxc 	dladm_status_t	status = DLADM_STATUS_OK;
6720ba2cbe9Sxc 	FILE		*fp, *nfp = NULL;
6730ba2cbe9Sxc 	char		lock[MAXPATHLEN];
6740ba2cbe9Sxc 	char		file[MAXPATHLEN];
6750ba2cbe9Sxc 	char		newfile[MAXPATHLEN];
6760ba2cbe9Sxc 	char		*db_basename;
6770ba2cbe9Sxc 	int		nfd, lock_fd;
6780ba2cbe9Sxc 
6790ba2cbe9Sxc 	/*
6800ba2cbe9Sxc 	 * If we are called from a boot script such as net-physical,
6810ba2cbe9Sxc 	 * it's quite likely that the root fs is still not writable.
6820ba2cbe9Sxc 	 * For this case, it's ok for the lock creation to fail since
6830ba2cbe9Sxc 	 * no one else could be accessing our configuration file.
6840ba2cbe9Sxc 	 */
6850ba2cbe9Sxc 	db_basename = strrchr(db_file, '/');
6860ba2cbe9Sxc 	if (db_basename == NULL || db_basename[1] == '\0')
6870ba2cbe9Sxc 		return (dladm_errno2status(EINVAL));
6880ba2cbe9Sxc 	db_basename++;
6890ba2cbe9Sxc 	(void) snprintf(lock, MAXPATHLEN, "/tmp/%s.lock", db_basename);
6900ba2cbe9Sxc 	if ((lock_fd = i_dladm_lock_db
6910ba2cbe9Sxc 	    (lock, (writeop ? F_WRLCK : F_RDLCK))) < 0 && errno != EROFS)
6920ba2cbe9Sxc 		return (dladm_errno2status(errno));
6930ba2cbe9Sxc 
6940ba2cbe9Sxc 	(void) snprintf(file, MAXPATHLEN, "%s/%s", dladm_rootdir, db_file);
6950ba2cbe9Sxc 	if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) {
6960ba2cbe9Sxc 		int	err = errno;
6970ba2cbe9Sxc 
6980ba2cbe9Sxc 		i_dladm_unlock_db(lock, lock_fd);
6990ba2cbe9Sxc 		if (err == ENOENT)
7000ba2cbe9Sxc 			return (DLADM_STATUS_DBNOTFOUND);
7010ba2cbe9Sxc 
7020ba2cbe9Sxc 		return (dladm_errno2status(err));
7030ba2cbe9Sxc 	}
7040ba2cbe9Sxc 
7050ba2cbe9Sxc 	if (writeop) {
7060ba2cbe9Sxc 		(void) snprintf(newfile, MAXPATHLEN, "%s/%s.new",
7070ba2cbe9Sxc 		    dladm_rootdir, db_file);
7080ba2cbe9Sxc 		if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC,
7090ba2cbe9Sxc 		    db_perms)) < 0) {
7100ba2cbe9Sxc 			(void) fclose(fp);
7110ba2cbe9Sxc 			i_dladm_unlock_db(lock, lock_fd);
7120ba2cbe9Sxc 			return (dladm_errno2status(errno));
7130ba2cbe9Sxc 		}
7140ba2cbe9Sxc 
7150ba2cbe9Sxc 		if ((nfp = fdopen(nfd, "w")) == NULL) {
7160ba2cbe9Sxc 			(void) close(nfd);
7170ba2cbe9Sxc 			(void) fclose(fp);
7180ba2cbe9Sxc 			(void) unlink(newfile);
7190ba2cbe9Sxc 			i_dladm_unlock_db(lock, lock_fd);
7200ba2cbe9Sxc 			return (dladm_errno2status(errno));
7210ba2cbe9Sxc 		}
7220ba2cbe9Sxc 	}
7234ac67f02SAnurag S. Maskey 	status = (*process_db)(handle, arg, fp, nfp);
7240ba2cbe9Sxc 	if (!writeop || status != DLADM_STATUS_OK)
7250ba2cbe9Sxc 		goto done;
7260ba2cbe9Sxc 
7270ba2cbe9Sxc 	/*
7280ba2cbe9Sxc 	 * Configuration files need to be owned by the 'dladm' user.
7290ba2cbe9Sxc 	 * If we are invoked by root, the file ownership needs to be fixed.
7300ba2cbe9Sxc 	 */
7310ba2cbe9Sxc 	if (getuid() == 0 || geteuid() == 0) {
732b9e076dcSyz 		if (fchown(nfd, UID_DLADM, GID_SYS) < 0) {
7330ba2cbe9Sxc 			status = dladm_errno2status(errno);
7340ba2cbe9Sxc 			goto done;
7350ba2cbe9Sxc 		}
7360ba2cbe9Sxc 	}
7370ba2cbe9Sxc 
7380ba2cbe9Sxc 	if (fflush(nfp) == EOF) {
7390ba2cbe9Sxc 		status = dladm_errno2status(errno);
7400ba2cbe9Sxc 		goto done;
7410ba2cbe9Sxc 	}
7420ba2cbe9Sxc 	(void) fclose(fp);
7430ba2cbe9Sxc 	(void) fclose(nfp);
7440ba2cbe9Sxc 
7450ba2cbe9Sxc 	if (rename(newfile, file) < 0) {
7460ba2cbe9Sxc 		(void) unlink(newfile);
7470ba2cbe9Sxc 		i_dladm_unlock_db(lock, lock_fd);
7480ba2cbe9Sxc 		return (dladm_errno2status(errno));
7490ba2cbe9Sxc 	}
7500ba2cbe9Sxc 
7510ba2cbe9Sxc 	i_dladm_unlock_db(lock, lock_fd);
7520ba2cbe9Sxc 	return (DLADM_STATUS_OK);
7530ba2cbe9Sxc 
7540ba2cbe9Sxc done:
7550ba2cbe9Sxc 	if (nfp != NULL) {
7560ba2cbe9Sxc 		(void) fclose(nfp);
7570ba2cbe9Sxc 		if (status != DLADM_STATUS_OK)
7580ba2cbe9Sxc 			(void) unlink(newfile);
7590ba2cbe9Sxc 	}
7600ba2cbe9Sxc 	(void) fclose(fp);
7610ba2cbe9Sxc 	i_dladm_unlock_db(lock, lock_fd);
7620ba2cbe9Sxc 	return (status);
7630ba2cbe9Sxc }
7640ba2cbe9Sxc 
7650ba2cbe9Sxc dladm_status_t
7660ba2cbe9Sxc dladm_set_rootdir(const char *rootdir)
7670ba2cbe9Sxc {
7680ba2cbe9Sxc 	DIR	*dp;
7690ba2cbe9Sxc 
7700ba2cbe9Sxc 	if (rootdir == NULL || *rootdir != '/' ||
7710ba2cbe9Sxc 	    (dp = opendir(rootdir)) == NULL)
7720ba2cbe9Sxc 		return (DLADM_STATUS_BADARG);
7730ba2cbe9Sxc 
7740ba2cbe9Sxc 	(void) strncpy(dladm_rootdir, rootdir, MAXPATHLEN);
7750ba2cbe9Sxc 	(void) closedir(dp);
7760ba2cbe9Sxc 	return (DLADM_STATUS_OK);
7770ba2cbe9Sxc }
778d62bc4baSyz 
779d62bc4baSyz boolean_t
780d62bc4baSyz dladm_valid_linkname(const char *link)
781d62bc4baSyz {
782d62bc4baSyz 	size_t		len = strlen(link);
783d62bc4baSyz 	const char	*cp;
784d62bc4baSyz 
7852b24ab6bSSebastien Roy 	if (len >= MAXLINKNAMELEN)
786d62bc4baSyz 		return (B_FALSE);
787d62bc4baSyz 
788d62bc4baSyz 	/*
789d62bc4baSyz 	 * The link name cannot start with a digit and must end with a digit.
790d62bc4baSyz 	 */
791d62bc4baSyz 	if ((isdigit(link[0]) != 0) || (isdigit(link[len - 1]) == 0))
792d62bc4baSyz 		return (B_FALSE);
793d62bc4baSyz 
794d62bc4baSyz 	/*
795d62bc4baSyz 	 * The legal characters in a link name are:
7962b24ab6bSSebastien Roy 	 * alphanumeric (a-z,  A-Z,  0-9), underscore ('_'), and '.'.
797d62bc4baSyz 	 */
798d62bc4baSyz 	for (cp = link; *cp != '\0'; cp++) {
7992b24ab6bSSebastien Roy 		if ((isalnum(*cp) == 0) && (*cp != '_') && (*cp != '.'))
800d62bc4baSyz 			return (B_FALSE);
801d62bc4baSyz 	}
802d62bc4baSyz 
803d62bc4baSyz 	return (B_TRUE);
804d62bc4baSyz }
805da14cebeSEric Cheng 
806da14cebeSEric Cheng /*
807da14cebeSEric Cheng  * Convert priority string to a value.
808da14cebeSEric Cheng  */
809da14cebeSEric Cheng dladm_status_t
810da14cebeSEric Cheng dladm_str2pri(char *token, mac_priority_level_t *pri)
811da14cebeSEric Cheng {
812da14cebeSEric Cheng 	if (strlen(token) == strlen("low") &&
813da14cebeSEric Cheng 	    strncasecmp(token, "low", strlen("low")) == 0) {
814da14cebeSEric Cheng 		*pri = MPL_LOW;
815da14cebeSEric Cheng 	} else if (strlen(token) == strlen("medium") &&
816da14cebeSEric Cheng 	    strncasecmp(token, "medium", strlen("medium")) == 0) {
817da14cebeSEric Cheng 		*pri = MPL_MEDIUM;
818da14cebeSEric Cheng 	} else if (strlen(token) == strlen("high") &&
819da14cebeSEric Cheng 	    strncasecmp(token, "high", strlen("high")) == 0) {
820da14cebeSEric Cheng 		*pri = MPL_HIGH;
821da14cebeSEric Cheng 	} else {
822da14cebeSEric Cheng 		return (DLADM_STATUS_BADVAL);
823da14cebeSEric Cheng 	}
824da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
825da14cebeSEric Cheng }
826da14cebeSEric Cheng 
827da14cebeSEric Cheng /*
828da14cebeSEric Cheng  * Convert priority value to a string.
829da14cebeSEric Cheng  */
830da14cebeSEric Cheng const char *
831da14cebeSEric Cheng dladm_pri2str(mac_priority_level_t pri, char *buf)
832da14cebeSEric Cheng {
833da14cebeSEric Cheng 	const char	*s;
834da14cebeSEric Cheng 
835da14cebeSEric Cheng 	switch (pri) {
836da14cebeSEric Cheng 	case MPL_LOW:
837da14cebeSEric Cheng 		s = "low";
838da14cebeSEric Cheng 		break;
839da14cebeSEric Cheng 	case MPL_MEDIUM:
840da14cebeSEric Cheng 		s = "medium";
841da14cebeSEric Cheng 		break;
842da14cebeSEric Cheng 	case MPL_HIGH:
843da14cebeSEric Cheng 		s = "high";
844da14cebeSEric Cheng 		break;
845da14cebeSEric Cheng 	default:
846da14cebeSEric Cheng 		s = "--";
847da14cebeSEric Cheng 		break;
848da14cebeSEric Cheng 	}
849da14cebeSEric Cheng 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
850da14cebeSEric Cheng 	return (buf);
851da14cebeSEric Cheng }
852da14cebeSEric Cheng 
853*25ec3e3dSEric Cheng /*
854*25ec3e3dSEric Cheng  * Convert protect string to a value.
855*25ec3e3dSEric Cheng  */
856*25ec3e3dSEric Cheng dladm_status_t
857*25ec3e3dSEric Cheng dladm_str2protect(char *token, uint32_t *ptype)
858*25ec3e3dSEric Cheng {
859*25ec3e3dSEric Cheng 	link_protect_t	*lp;
860*25ec3e3dSEric Cheng 	int		i;
861*25ec3e3dSEric Cheng 
862*25ec3e3dSEric Cheng 	for (i = 0; i < LPTYPES; i++) {
863*25ec3e3dSEric Cheng 		lp = &link_protect_types[i];
864*25ec3e3dSEric Cheng 		if (strcmp(token, lp->lp_name) == 0) {
865*25ec3e3dSEric Cheng 			*ptype = lp->lp_type;
866*25ec3e3dSEric Cheng 			return (DLADM_STATUS_OK);
867*25ec3e3dSEric Cheng 		}
868*25ec3e3dSEric Cheng 	}
869*25ec3e3dSEric Cheng 	return (DLADM_STATUS_BADVAL);
870*25ec3e3dSEric Cheng }
871*25ec3e3dSEric Cheng 
872*25ec3e3dSEric Cheng /*
873*25ec3e3dSEric Cheng  * Convert protect value to a string.
874*25ec3e3dSEric Cheng  */
875*25ec3e3dSEric Cheng const char *
876*25ec3e3dSEric Cheng dladm_protect2str(uint32_t ptype, char *buf)
877*25ec3e3dSEric Cheng {
878*25ec3e3dSEric Cheng 	const char	*s = "--";
879*25ec3e3dSEric Cheng 	link_protect_t	*lp;
880*25ec3e3dSEric Cheng 	int		i;
881*25ec3e3dSEric Cheng 
882*25ec3e3dSEric Cheng 	for (i = 0; i < LPTYPES; i++) {
883*25ec3e3dSEric Cheng 		lp = &link_protect_types[i];
884*25ec3e3dSEric Cheng 		if (lp->lp_type == ptype) {
885*25ec3e3dSEric Cheng 			s = lp->lp_name;
886*25ec3e3dSEric Cheng 			break;
887*25ec3e3dSEric Cheng 		}
888*25ec3e3dSEric Cheng 	}
889*25ec3e3dSEric Cheng 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
890*25ec3e3dSEric Cheng 	return (buf);
891*25ec3e3dSEric Cheng }
892*25ec3e3dSEric Cheng 
893*25ec3e3dSEric Cheng /*
894*25ec3e3dSEric Cheng  * Convert an IPv4 address to/from a string.
895*25ec3e3dSEric Cheng  */
896*25ec3e3dSEric Cheng const char *
897*25ec3e3dSEric Cheng dladm_ipv4addr2str(void *addr, char *buf)
898*25ec3e3dSEric Cheng {
899*25ec3e3dSEric Cheng 	if (inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN) == NULL)
900*25ec3e3dSEric Cheng 		buf[0] = 0;
901*25ec3e3dSEric Cheng 
902*25ec3e3dSEric Cheng 	return (buf);
903*25ec3e3dSEric Cheng }
904*25ec3e3dSEric Cheng 
905*25ec3e3dSEric Cheng dladm_status_t
906*25ec3e3dSEric Cheng dladm_str2ipv4addr(char *token, void *addr)
907*25ec3e3dSEric Cheng {
908*25ec3e3dSEric Cheng 	return (inet_pton(AF_INET, token, addr) == 1 ?
909*25ec3e3dSEric Cheng 	    DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP);
910*25ec3e3dSEric Cheng }
911*25ec3e3dSEric Cheng 
912*25ec3e3dSEric Cheng /*
913*25ec3e3dSEric Cheng  * Find the set bits in a mask.
914*25ec3e3dSEric Cheng  * This is used for expanding a bitmask into individual sub-masks
915*25ec3e3dSEric Cheng  * which can be used for further processing.
916*25ec3e3dSEric Cheng  */
917*25ec3e3dSEric Cheng void
918*25ec3e3dSEric Cheng dladm_find_setbits32(uint32_t mask, uint32_t *list, uint32_t *cnt)
919*25ec3e3dSEric Cheng {
920*25ec3e3dSEric Cheng 	int	i, c = 0;
921*25ec3e3dSEric Cheng 
922*25ec3e3dSEric Cheng 	for (i = 0; i < 32; i++) {
923*25ec3e3dSEric Cheng 		if (((1 << i) & mask) != 0)
924*25ec3e3dSEric Cheng 			list[c++] = 1 << i;
925*25ec3e3dSEric Cheng 	}
926*25ec3e3dSEric Cheng 	*cnt = c;
927*25ec3e3dSEric Cheng }
928*25ec3e3dSEric Cheng 
929da14cebeSEric Cheng void
930da14cebeSEric Cheng dladm_free_args(dladm_arg_list_t *list)
931da14cebeSEric Cheng {
932da14cebeSEric Cheng 	if (list != NULL) {
933da14cebeSEric Cheng 		free(list->al_buf);
934da14cebeSEric Cheng 		free(list);
935da14cebeSEric Cheng 	}
936da14cebeSEric Cheng }
937da14cebeSEric Cheng 
938da14cebeSEric Cheng dladm_status_t
939da14cebeSEric Cheng dladm_parse_args(char *str, dladm_arg_list_t **listp, boolean_t novalues)
940da14cebeSEric Cheng {
941da14cebeSEric Cheng 	dladm_arg_list_t	*list;
942da14cebeSEric Cheng 	dladm_arg_info_t	*aip;
943da14cebeSEric Cheng 	char			*buf, *curr;
944da14cebeSEric Cheng 	int			len, i;
945da14cebeSEric Cheng 
94663a6526dSMichael Lim 	if (str == NULL)
94763a6526dSMichael Lim 		return (DLADM_STATUS_BADVAL);
94863a6526dSMichael Lim 
94963a6526dSMichael Lim 	if (str[0] == '\0')
95063a6526dSMichael Lim 		return (DLADM_STATUS_OK);
95163a6526dSMichael Lim 
952da14cebeSEric Cheng 	list = malloc(sizeof (dladm_arg_list_t));
953da14cebeSEric Cheng 	if (list == NULL)
954da14cebeSEric Cheng 		return (dladm_errno2status(errno));
955da14cebeSEric Cheng 
956da14cebeSEric Cheng 	list->al_count = 0;
957da14cebeSEric Cheng 	list->al_buf = buf = strdup(str);
958da14cebeSEric Cheng 	if (buf == NULL)
959da14cebeSEric Cheng 		return (dladm_errno2status(errno));
960da14cebeSEric Cheng 
961da14cebeSEric Cheng 	curr = buf;
962da14cebeSEric Cheng 	len = strlen(buf);
963da14cebeSEric Cheng 	aip = NULL;
964da14cebeSEric Cheng 	for (i = 0; i < len; i++) {
965da14cebeSEric Cheng 		char		c = buf[i];
966da14cebeSEric Cheng 		boolean_t	match = (c == '=' || c == ',');
967da14cebeSEric Cheng 
968da14cebeSEric Cheng 		if (!match && i != len - 1)
969da14cebeSEric Cheng 			continue;
970da14cebeSEric Cheng 
971da14cebeSEric Cheng 		if (match) {
972da14cebeSEric Cheng 			buf[i] = '\0';
973da14cebeSEric Cheng 			if (*curr == '\0')
974da14cebeSEric Cheng 				goto fail;
975da14cebeSEric Cheng 		}
976da14cebeSEric Cheng 
977da14cebeSEric Cheng 		if (aip != NULL && c != '=') {
978da14cebeSEric Cheng 			if (aip->ai_count > DLADM_MAX_ARG_VALS)
979da14cebeSEric Cheng 				goto fail;
980da14cebeSEric Cheng 
981da14cebeSEric Cheng 			if (novalues)
982da14cebeSEric Cheng 				goto fail;
983da14cebeSEric Cheng 
984da14cebeSEric Cheng 			aip->ai_val[aip->ai_count] = curr;
985da14cebeSEric Cheng 			aip->ai_count++;
986da14cebeSEric Cheng 		} else {
987da14cebeSEric Cheng 			if (list->al_count > DLADM_MAX_ARG_VALS)
988da14cebeSEric Cheng 				goto fail;
989da14cebeSEric Cheng 
990da14cebeSEric Cheng 			aip = &list->al_info[list->al_count];
991da14cebeSEric Cheng 			aip->ai_name = curr;
992da14cebeSEric Cheng 			aip->ai_count = 0;
993da14cebeSEric Cheng 			list->al_count++;
994da14cebeSEric Cheng 			if (c == ',')
995da14cebeSEric Cheng 				aip = NULL;
996da14cebeSEric Cheng 		}
997da14cebeSEric Cheng 		curr = buf + i + 1;
998da14cebeSEric Cheng 	}
999da14cebeSEric Cheng 
1000da14cebeSEric Cheng 	*listp = list;
1001da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
1002da14cebeSEric Cheng 
1003da14cebeSEric Cheng fail:
1004da14cebeSEric Cheng 	dladm_free_args(list);
1005da14cebeSEric Cheng 	return (DLADM_STATUS_FAILED);
1006da14cebeSEric Cheng }
1007