xref: /titanic_44/usr/src/lib/libdladm/common/libdladm.c (revision 6ba597c56d749c61b4f783157f63196d7b2445f0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <unistd.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <fcntl.h>
30 #include <strings.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/dld.h>
38 #include <sys/dld_ioc.h>
39 #include <libdladm_impl.h>
40 #include <libintl.h>
41 #include <libdlpi.h>
42 
43 static char	dladm_rootdir[MAXPATHLEN] = "/";
44 
45 typedef struct media_type_desc {
46 	uint32_t	media_type;
47 #define	MAX_MEDIA_TYPE_STRING	32
48 	const char	media_type_str[MAX_MEDIA_TYPE_STRING];
49 } media_type_t;
50 
51 static media_type_t media_type_table[] =  {
52 	{ DL_ETHER,	"Ethernet" },
53 	{ DL_WIFI,	"WiFi" },
54 	{ DL_IB,	"Infiniband" },
55 	{ DL_IPV4,	"IPv4Tunnel" },
56 	{ DL_IPV6,	"IPv6Tunnel" },
57 	{ DL_6TO4,	"6to4Tunnel" },
58 	{ DL_CSMACD,	"CSMA/CD" },
59 	{ DL_TPB,	"TokenBus" },
60 	{ DL_TPR,	"TokenRing" },
61 	{ DL_METRO,	"MetroNet" },
62 	{ DL_HDLC,	"HDLC" },
63 	{ DL_CHAR,	"SyncCharacter" },
64 	{ DL_CTCA,	"CTCA" },
65 	{ DL_FDDI, 	"FDDI" },
66 	{ DL_FC, 	"FiberChannel" },
67 	{ DL_ATM, 	"ATM" },
68 	{ DL_IPATM, 	"ATM(ClassicIP)" },
69 	{ DL_X25, 	"X.25" },
70 	{ DL_IPX25, 	"X.25(ClassicIP)" },
71 	{ DL_ISDN, 	"ISDN" },
72 	{ DL_HIPPI, 	"HIPPI" },
73 	{ DL_100VG, 	"100BaseVGEthernet" },
74 	{ DL_100VGTPR, 	"100BaseVGTokenRing" },
75 	{ DL_ETH_CSMA, 	"IEEE802.3" },
76 	{ DL_100BT, 	"100BaseT" },
77 	{ DL_FRAME, 	"FrameRelay" },
78 	{ DL_MPFRAME, 	"MPFrameRelay" },
79 	{ DL_ASYNC, 	"AsyncCharacter" },
80 	{ DL_IPNET, 	"IPNET" },
81 	{ DL_OTHER, 	"Other" }
82 };
83 #define	MEDIATYPECOUNT	(sizeof (media_type_table) / sizeof (media_type_t))
84 
85 typedef struct {
86 	uint32_t	lp_type;
87 	char		*lp_name;
88 } link_protect_t;
89 
90 static link_protect_t link_protect_types[] = {
91 	{ MPT_MACNOSPOOF, "mac-nospoof" },
92 	{ MPT_IPNOSPOOF, "ip-nospoof" },
93 	{ MPT_RESTRICTED, "restricted" }
94 };
95 #define	LPTYPES	(sizeof (link_protect_types) / sizeof (link_protect_t))
96 
97 dladm_status_t
98 dladm_open(dladm_handle_t *handle)
99 {
100 	int dld_fd;
101 
102 	if (handle == NULL)
103 		return (DLADM_STATUS_BADARG);
104 
105 	if ((dld_fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
106 		return (dladm_errno2status(errno));
107 
108 	/*
109 	 * Don't open DLMGMT_DOOR now.  dlmgmtd(1M) is not able to
110 	 * open the door when the dladm handle is opened because the
111 	 * door hasn't been created yet at that time.  Thus, we must
112 	 * open it on-demand in dladm_door_fd().  Move the open()
113 	 * to dladm_door_fd() for all cases.
114 	 */
115 
116 	if ((*handle = malloc(sizeof (struct dladm_handle))) == NULL) {
117 		(void) close(dld_fd);
118 		return (DLADM_STATUS_NOMEM);
119 	}
120 
121 	(*handle)->dld_fd = dld_fd;
122 	(*handle)->door_fd = -1;
123 
124 	return (DLADM_STATUS_OK);
125 }
126 
127 void
128 dladm_close(dladm_handle_t handle)
129 {
130 	if (handle != NULL) {
131 		(void) close(handle->dld_fd);
132 		if (handle->door_fd != -1)
133 			(void) close(handle->door_fd);
134 		free(handle);
135 	}
136 }
137 
138 int
139 dladm_dld_fd(dladm_handle_t handle)
140 {
141 	return (handle->dld_fd);
142 }
143 
144 /*
145  * If DLMGMT_DOOR hasn't been opened in the handle yet, open it.
146  */
147 dladm_status_t
148 dladm_door_fd(dladm_handle_t handle, int *door_fd)
149 {
150 	int fd;
151 
152 	if (handle->door_fd == -1) {
153 		if ((fd = open(DLMGMT_DOOR, O_RDONLY)) < 0)
154 			return (dladm_errno2status(errno));
155 		handle->door_fd = fd;
156 	}
157 	*door_fd = handle->door_fd;
158 
159 	return (DLADM_STATUS_OK);
160 }
161 
162 const char *
163 dladm_status2str(dladm_status_t status, char *buf)
164 {
165 	const char	*s;
166 
167 	switch (status) {
168 	case DLADM_STATUS_OK:
169 		s = "ok";
170 		break;
171 	case DLADM_STATUS_BADARG:
172 		s = "invalid argument";
173 		break;
174 	case DLADM_STATUS_FAILED:
175 		s = "operation failed";
176 		break;
177 	case DLADM_STATUS_TOOSMALL:
178 		s = "buffer size too small";
179 		break;
180 	case DLADM_STATUS_NOTSUP:
181 		s = "operation not supported";
182 		break;
183 	case DLADM_STATUS_NOTFOUND:
184 		s = "object not found";
185 		break;
186 	case DLADM_STATUS_BADVAL:
187 		s = "invalid value";
188 		break;
189 	case DLADM_STATUS_NOMEM:
190 		s = "insufficient memory";
191 		break;
192 	case DLADM_STATUS_EXIST:
193 		s = "object already exists";
194 		break;
195 	case DLADM_STATUS_LINKINVAL:
196 		s = "invalid link";
197 		break;
198 	case DLADM_STATUS_PROPRDONLY:
199 		s = "read-only property";
200 		break;
201 	case DLADM_STATUS_BADVALCNT:
202 		s = "invalid number of values";
203 		break;
204 	case DLADM_STATUS_DBNOTFOUND:
205 		s = "database not found";
206 		break;
207 	case DLADM_STATUS_DENIED:
208 		s = "permission denied";
209 		break;
210 	case DLADM_STATUS_IOERR:
211 		s = "I/O error";
212 		break;
213 	case DLADM_STATUS_TEMPONLY:
214 		s = "change cannot be persistent";
215 		break;
216 	case DLADM_STATUS_TIMEDOUT:
217 		s = "operation timed out";
218 		break;
219 	case DLADM_STATUS_ISCONN:
220 		s = "already connected";
221 		break;
222 	case DLADM_STATUS_NOTCONN:
223 		s = "not connected";
224 		break;
225 	case DLADM_STATUS_REPOSITORYINVAL:
226 		s = "invalid configuration repository";
227 		break;
228 	case DLADM_STATUS_MACADDRINVAL:
229 		s = "invalid MAC address";
230 		break;
231 	case DLADM_STATUS_KEYINVAL:
232 		s = "invalid key";
233 		break;
234 	case DLADM_STATUS_INVALIDMACADDRLEN:
235 		s = "invalid MAC address length";
236 		break;
237 	case DLADM_STATUS_INVALIDMACADDRTYPE:
238 		s = "invalid MAC address type";
239 		break;
240 	case DLADM_STATUS_LINKBUSY:
241 		s = "link busy";
242 		break;
243 	case DLADM_STATUS_VIDINVAL:
244 		s = "invalid VLAN identifier";
245 		break;
246 	case DLADM_STATUS_TRYAGAIN:
247 		s = "try again later";
248 		break;
249 	case DLADM_STATUS_NONOTIF:
250 		s = "link notification is not supported";
251 		break;
252 	case DLADM_STATUS_BADTIMEVAL:
253 		s = "invalid time range";
254 		break;
255 	case DLADM_STATUS_INVALIDMACADDR:
256 		s = "invalid MAC address value";
257 		break;
258 	case DLADM_STATUS_INVALIDMACADDRNIC:
259 		s = "MAC address reserved for use by underlying data-link";
260 		break;
261 	case DLADM_STATUS_INVALIDMACADDRINUSE:
262 		s = "MAC address is already in use";
263 		break;
264 	case DLADM_STATUS_MACFACTORYSLOTINVALID:
265 		s = "invalid factory MAC address slot";
266 		break;
267 	case DLADM_STATUS_MACFACTORYSLOTUSED:
268 		s = "factory MAC address slot already used";
269 		break;
270 	case DLADM_STATUS_MACFACTORYSLOTALLUSED:
271 		s = "all factory MAC address slots are in use";
272 		break;
273 	case DLADM_STATUS_MACFACTORYNOTSUP:
274 		s = "factory MAC address slots not supported";
275 		break;
276 	case DLADM_STATUS_INVALIDMACPREFIX:
277 		s = "Invalid MAC address prefix value";
278 		break;
279 	case DLADM_STATUS_INVALIDMACPREFIXLEN:
280 		s = "Invalid MAC address prefix length";
281 		break;
282 	case DLADM_STATUS_CPUMAX:
283 		s = "non-existent processor ID";
284 		break;
285 	case DLADM_STATUS_CPUERR:
286 		s = "could not determine processor status";
287 		break;
288 	case DLADM_STATUS_CPUNOTONLINE:
289 		s = "processor not online";
290 		break;
291 	case DLADM_STATUS_DB_NOTFOUND:
292 		s = "database not found";
293 		break;
294 	case DLADM_STATUS_DB_PARSE_ERR:
295 		s = "database parse error";
296 		break;
297 	case DLADM_STATUS_PROP_PARSE_ERR:
298 		s = "property parse error";
299 		break;
300 	case DLADM_STATUS_ATTR_PARSE_ERR:
301 		s = "attribute parse error";
302 		break;
303 	case DLADM_STATUS_FLOW_DB_ERR:
304 		s = "flow database error";
305 		break;
306 	case DLADM_STATUS_FLOW_DB_OPEN_ERR:
307 		s = "flow database open error";
308 		break;
309 	case DLADM_STATUS_FLOW_DB_PARSE_ERR:
310 		s = "flow database parse error";
311 		break;
312 	case DLADM_STATUS_FLOWPROP_DB_PARSE_ERR:
313 		s = "flow property database parse error";
314 		break;
315 	case DLADM_STATUS_FLOW_ADD_ERR:
316 		s = "flow add error";
317 		break;
318 	case DLADM_STATUS_FLOW_WALK_ERR:
319 		s = "flow walk error";
320 		break;
321 	case DLADM_STATUS_FLOW_IDENTICAL:
322 		s = "a flow with identical attributes exists";
323 		break;
324 	case DLADM_STATUS_FLOW_INCOMPATIBLE:
325 		s = "flow(s) with incompatible attributes exists";
326 		break;
327 	case DLADM_STATUS_FLOW_EXISTS:
328 		s = "link still has flows";
329 		break;
330 	case DLADM_STATUS_PERSIST_FLOW_EXISTS:
331 		s = "persistent flow with the same name exists";
332 		break;
333 	case DLADM_STATUS_INVALID_IP:
334 		s = "invalid IP address";
335 		break;
336 	case DLADM_STATUS_INVALID_PREFIXLEN:
337 		s = "invalid IP prefix length";
338 		break;
339 	case DLADM_STATUS_INVALID_PROTOCOL:
340 		s = "invalid IP protocol";
341 		break;
342 	case DLADM_STATUS_INVALID_PORT:
343 		s = "invalid port number";
344 		break;
345 	case DLADM_STATUS_INVALID_DSF:
346 		s = "invalid dsfield";
347 		break;
348 	case DLADM_STATUS_INVALID_DSFMASK:
349 		s = "invalid dsfield mask";
350 		break;
351 	case DLADM_STATUS_INVALID_MACMARGIN:
352 		s = "MTU check failed, use lower MTU or -f option";
353 		break;
354 	case DLADM_STATUS_BADPROP:
355 		s = "invalid property";
356 		break;
357 	case DLADM_STATUS_MINMAXBW:
358 		s = "minimum value for maxbw is 1.2M";
359 		break;
360 	case DLADM_STATUS_NO_HWRINGS:
361 		s = "request hw rings failed";
362 		break;
363 	case DLADM_STATUS_PERMONLY:
364 		s = "change must be persistent";
365 		break;
366 	case DLADM_STATUS_OPTMISSING:
367 		s = "optional software not installed";
368 		break;
369 	case DLADM_STATUS_IPTUNTYPE:
370 		s = "invalid IP tunnel type";
371 		break;
372 	case DLADM_STATUS_IPTUNTYPEREQD:
373 		s = "IP tunnel type required";
374 		break;
375 	case DLADM_STATUS_BADIPTUNLADDR:
376 		s = "invalid local IP tunnel address";
377 		break;
378 	case DLADM_STATUS_BADIPTUNRADDR:
379 		s = "invalid remote IP tunnel address";
380 		break;
381 	case DLADM_STATUS_ADDRINUSE:
382 		s = "address already in use";
383 		break;
384 	default:
385 		s = "<unknown error>";
386 		break;
387 	}
388 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
389 	return (buf);
390 }
391 
392 /*
393  * Convert a unix errno to a dladm_status_t.
394  * We only convert errnos that are likely to be encountered. All others
395  * are mapped to DLADM_STATUS_FAILED.
396  */
397 dladm_status_t
398 dladm_errno2status(int err)
399 {
400 	switch (err) {
401 	case 0:
402 		return (DLADM_STATUS_OK);
403 	case EINVAL:
404 		return (DLADM_STATUS_BADARG);
405 	case EEXIST:
406 		return (DLADM_STATUS_EXIST);
407 	case ENOENT:
408 		return (DLADM_STATUS_NOTFOUND);
409 	case ENOSPC:
410 		return (DLADM_STATUS_TOOSMALL);
411 	case ENOMEM:
412 		return (DLADM_STATUS_NOMEM);
413 	case ENOTSUP:
414 		return (DLADM_STATUS_NOTSUP);
415 	case ENETDOWN:
416 		return (DLADM_STATUS_NONOTIF);
417 	case EACCES:
418 	case EPERM:
419 		return (DLADM_STATUS_DENIED);
420 	case EIO:
421 		return (DLADM_STATUS_IOERR);
422 	case EBUSY:
423 		return (DLADM_STATUS_LINKBUSY);
424 	case EAGAIN:
425 		return (DLADM_STATUS_TRYAGAIN);
426 	case ENOTEMPTY:
427 		return (DLADM_STATUS_FLOW_EXISTS);
428 	case EOPNOTSUPP:
429 		return (DLADM_STATUS_FLOW_INCOMPATIBLE);
430 	case EALREADY:
431 		return (DLADM_STATUS_FLOW_IDENTICAL);
432 	case EADDRINUSE:
433 		return (DLADM_STATUS_ADDRINUSE);
434 	default:
435 		return (DLADM_STATUS_FAILED);
436 	}
437 }
438 
439 boolean_t
440 dladm_str2interval(char *oarg, uint32_t *interval)
441 {
442 	int		val;
443 	char		*endp = NULL;
444 
445 	errno = 0;
446 	val = strtol(oarg, &endp, 10);
447 	if (errno != 0 || val <= 0 || *endp != '\0')
448 		return (B_FALSE);
449 
450 	*interval = val;
451 
452 	return (B_TRUE);
453 }
454 
455 dladm_status_t
456 dladm_str2bw(char *oarg, uint64_t *bw)
457 {
458 	char		*endp = NULL;
459 	int64_t		n;
460 	int		mult = 1;
461 
462 	n = strtoull(oarg, &endp, 10);
463 
464 	if ((errno != 0) || (strlen(endp) > 1))
465 		return (DLADM_STATUS_BADARG);
466 
467 	if (n < 0)
468 		return (DLADM_STATUS_BADVAL);
469 
470 	switch (*endp) {
471 	case 'k':
472 	case 'K':
473 		mult = 1000;
474 		break;
475 	case 'm':
476 	case 'M':
477 	case '\0':
478 		mult = 1000000;
479 		break;
480 	case 'g':
481 	case 'G':
482 		mult = 1000000000;
483 		break;
484 	case '%':
485 		/*
486 		 * percentages not supported for now,
487 		 * see RFE 6540675
488 		 */
489 		return (DLADM_STATUS_NOTSUP);
490 	default:
491 		return (DLADM_STATUS_BADVAL);
492 	}
493 
494 	*bw = n * mult;
495 
496 	/* check for overflow */
497 	if (*bw / mult != n)
498 		return (DLADM_STATUS_BADARG);
499 
500 	return (DLADM_STATUS_OK);
501 }
502 
503 /*
504  * Convert bandwidth in bps to a string in mpbs.  For values greater
505  * than 1mbps or 1000000, print a whole mbps value.  For values that
506  * have fractional Mbps in whole Kbps , print the bandwidth in a manner
507  * simlilar to a floating point format.
508  *
509  *        bps       string
510  *          0            0
511  *        100            0
512  *       2000        0.002
513  *     431000        0.431
514  *    1000000            1
515  *    1030000        1.030
516  *  100000000          100
517  */
518 const char *
519 dladm_bw2str(int64_t bw, char *buf)
520 {
521 	int kbps, mbps;
522 
523 	kbps = (bw%1000000)/1000;
524 	mbps = bw/1000000;
525 	if (kbps != 0) {
526 		if (mbps == 0)
527 			(void) snprintf(buf, DLADM_STRSIZE, "0.%03u", kbps);
528 		else
529 			(void) snprintf(buf, DLADM_STRSIZE, "%5u.%03u", mbps,
530 			    kbps);
531 	} else {
532 		(void) snprintf(buf, DLADM_STRSIZE, "%5u", mbps);
533 	}
534 
535 	return (buf);
536 }
537 
538 #define	LOCK_DB_PERMS	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
539 
540 static int
541 i_dladm_lock_db(const char *lock_file, short type)
542 {
543 	int	lock_fd;
544 	struct	flock lock;
545 
546 	if ((lock_fd = open(lock_file, O_RDWR | O_CREAT | O_TRUNC,
547 	    LOCK_DB_PERMS)) < 0)
548 		return (-1);
549 
550 	lock.l_type = type;
551 	lock.l_whence = SEEK_SET;
552 	lock.l_start = 0;
553 	lock.l_len = 0;
554 
555 	if (fcntl(lock_fd, F_SETLKW, &lock) < 0) {
556 		int err = errno;
557 
558 		(void) close(lock_fd);
559 		(void) unlink(lock_file);
560 		errno = err;
561 		return (-1);
562 	}
563 	return (lock_fd);
564 }
565 
566 static void
567 i_dladm_unlock_db(const char *lock_file, int fd)
568 {
569 	struct flock lock;
570 
571 	if (fd < 0)
572 		return;
573 
574 	lock.l_type = F_UNLCK;
575 	lock.l_whence = SEEK_SET;
576 	lock.l_start = 0;
577 	lock.l_len = 0;
578 
579 	(void) fcntl(fd, F_SETLKW, &lock);
580 	(void) close(fd);
581 	(void) unlink(lock_file);
582 }
583 
584 /*
585  * Given a link class, returns its class string.
586  */
587 const char *
588 dladm_class2str(datalink_class_t class, char *buf)
589 {
590 	const char *s;
591 
592 	switch (class) {
593 	case DATALINK_CLASS_PHYS:
594 		s = "phys";
595 		break;
596 	case DATALINK_CLASS_VLAN:
597 		s = "vlan";
598 		break;
599 	case DATALINK_CLASS_AGGR:
600 		s = "aggr";
601 		break;
602 	case DATALINK_CLASS_VNIC:
603 		s = "vnic";
604 		break;
605 	case DATALINK_CLASS_ETHERSTUB:
606 		s = "etherstub";
607 		break;
608 	case DATALINK_CLASS_IPTUN:
609 		s = "iptun";
610 		break;
611 	case DATALINK_CLASS_SIMNET:
612 		s = "simnet";
613 		break;
614 	case DATALINK_CLASS_BRIDGE:
615 		s = "bridge";
616 		break;
617 	default:
618 		s = "unknown";
619 		break;
620 	}
621 
622 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
623 	return (buf);
624 }
625 
626 /*
627  * Given a physical link media type, returns its media type string.
628  */
629 const char *
630 dladm_media2str(uint32_t media, char *buf)
631 {
632 	const char *s = "--";
633 	media_type_t *mt;
634 	int idx;
635 
636 	for (idx = 0; idx < MEDIATYPECOUNT; idx++) {
637 		mt = media_type_table + idx;
638 		if (mt->media_type == media) {
639 			s = mt->media_type_str;
640 			break;
641 		}
642 	}
643 
644 	(void) snprintf(buf, DLADM_STRSIZE, "%s", s);
645 	return (buf);
646 }
647 
648 /*
649  * Given a physical link media type string, returns its media type constant.
650  */
651 uint32_t
652 dladm_str2media(const char *buf)
653 {
654 	media_type_t *mt;
655 	int idx;
656 
657 	for (idx = 0; idx < MEDIATYPECOUNT; idx++) {
658 		mt = media_type_table + idx;
659 		if (strcasecmp(buf, mt->media_type_str) == 0)
660 			return (mt->media_type);
661 	}
662 
663 	return (DL_OTHER);
664 }
665 
666 dladm_status_t
667 i_dladm_rw_db(dladm_handle_t handle, const char *db_file, mode_t db_perms,
668     dladm_status_t (*process_db)(dladm_handle_t, void *, FILE *, FILE *),
669     void *arg, boolean_t writeop)
670 {
671 	dladm_status_t	status = DLADM_STATUS_OK;
672 	FILE		*fp, *nfp = NULL;
673 	char		lock[MAXPATHLEN];
674 	char		file[MAXPATHLEN];
675 	char		newfile[MAXPATHLEN];
676 	char		*db_basename;
677 	int		nfd, lock_fd;
678 
679 	/*
680 	 * If we are called from a boot script such as net-physical,
681 	 * it's quite likely that the root fs is still not writable.
682 	 * For this case, it's ok for the lock creation to fail since
683 	 * no one else could be accessing our configuration file.
684 	 */
685 	db_basename = strrchr(db_file, '/');
686 	if (db_basename == NULL || db_basename[1] == '\0')
687 		return (dladm_errno2status(EINVAL));
688 	db_basename++;
689 	(void) snprintf(lock, MAXPATHLEN, "/tmp/%s.lock", db_basename);
690 	if ((lock_fd = i_dladm_lock_db
691 	    (lock, (writeop ? F_WRLCK : F_RDLCK))) < 0 && errno != EROFS)
692 		return (dladm_errno2status(errno));
693 
694 	(void) snprintf(file, MAXPATHLEN, "%s/%s", dladm_rootdir, db_file);
695 	if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) {
696 		int	err = errno;
697 
698 		i_dladm_unlock_db(lock, lock_fd);
699 		if (err == ENOENT)
700 			return (DLADM_STATUS_DBNOTFOUND);
701 
702 		return (dladm_errno2status(err));
703 	}
704 
705 	if (writeop) {
706 		(void) snprintf(newfile, MAXPATHLEN, "%s/%s.new",
707 		    dladm_rootdir, db_file);
708 		if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC,
709 		    db_perms)) < 0) {
710 			(void) fclose(fp);
711 			i_dladm_unlock_db(lock, lock_fd);
712 			return (dladm_errno2status(errno));
713 		}
714 
715 		if ((nfp = fdopen(nfd, "w")) == NULL) {
716 			(void) close(nfd);
717 			(void) fclose(fp);
718 			(void) unlink(newfile);
719 			i_dladm_unlock_db(lock, lock_fd);
720 			return (dladm_errno2status(errno));
721 		}
722 	}
723 	status = (*process_db)(handle, arg, fp, nfp);
724 	if (!writeop || status != DLADM_STATUS_OK)
725 		goto done;
726 
727 	/* Set permissions on file to db_perms */
728 	if (fchmod(nfd, db_perms) < 0) {
729 		status = dladm_errno2status(errno);
730 		goto done;
731 	}
732 
733 	/*
734 	 * Configuration files need to be owned by the 'dladm' user and
735 	 * 'netadm' group.
736 	 */
737 	if (fchown(nfd, UID_DLADM, GID_NETADM) < 0) {
738 		status = dladm_errno2status(errno);
739 		goto done;
740 	}
741 
742 	if (fflush(nfp) == EOF) {
743 		status = dladm_errno2status(errno);
744 		goto done;
745 	}
746 	(void) fclose(fp);
747 	(void) fclose(nfp);
748 
749 	if (rename(newfile, file) < 0) {
750 		(void) unlink(newfile);
751 		i_dladm_unlock_db(lock, lock_fd);
752 		return (dladm_errno2status(errno));
753 	}
754 
755 	i_dladm_unlock_db(lock, lock_fd);
756 	return (DLADM_STATUS_OK);
757 
758 done:
759 	if (nfp != NULL) {
760 		(void) fclose(nfp);
761 		if (status != DLADM_STATUS_OK)
762 			(void) unlink(newfile);
763 	}
764 	(void) fclose(fp);
765 	i_dladm_unlock_db(lock, lock_fd);
766 	return (status);
767 }
768 
769 dladm_status_t
770 dladm_set_rootdir(const char *rootdir)
771 {
772 	DIR	*dp;
773 
774 	if (rootdir == NULL || *rootdir != '/' ||
775 	    (dp = opendir(rootdir)) == NULL)
776 		return (DLADM_STATUS_BADARG);
777 
778 	(void) strncpy(dladm_rootdir, rootdir, MAXPATHLEN);
779 	(void) closedir(dp);
780 	return (DLADM_STATUS_OK);
781 }
782 
783 boolean_t
784 dladm_valid_linkname(const char *link)
785 {
786 	size_t		len = strlen(link);
787 	const char	*cp;
788 
789 	if (len >= MAXLINKNAMELEN)
790 		return (B_FALSE);
791 
792 	/*
793 	 * The link name cannot start with a digit and must end with a digit.
794 	 */
795 	if ((isdigit(link[0]) != 0) || (isdigit(link[len - 1]) == 0))
796 		return (B_FALSE);
797 
798 	/*
799 	 * The legal characters in a link name are:
800 	 * alphanumeric (a-z,  A-Z,  0-9), underscore ('_'), and '.'.
801 	 */
802 	for (cp = link; *cp != '\0'; cp++) {
803 		if ((isalnum(*cp) == 0) && (*cp != '_') && (*cp != '.'))
804 			return (B_FALSE);
805 	}
806 
807 	return (B_TRUE);
808 }
809 
810 /*
811  * Convert priority string to a value.
812  */
813 dladm_status_t
814 dladm_str2pri(char *token, mac_priority_level_t *pri)
815 {
816 	if (strlen(token) == strlen("low") &&
817 	    strncasecmp(token, "low", strlen("low")) == 0) {
818 		*pri = MPL_LOW;
819 	} else if (strlen(token) == strlen("medium") &&
820 	    strncasecmp(token, "medium", strlen("medium")) == 0) {
821 		*pri = MPL_MEDIUM;
822 	} else if (strlen(token) == strlen("high") &&
823 	    strncasecmp(token, "high", strlen("high")) == 0) {
824 		*pri = MPL_HIGH;
825 	} else {
826 		return (DLADM_STATUS_BADVAL);
827 	}
828 	return (DLADM_STATUS_OK);
829 }
830 
831 /*
832  * Convert priority value to a string.
833  */
834 const char *
835 dladm_pri2str(mac_priority_level_t pri, char *buf)
836 {
837 	const char	*s;
838 
839 	switch (pri) {
840 	case MPL_LOW:
841 		s = "low";
842 		break;
843 	case MPL_MEDIUM:
844 		s = "medium";
845 		break;
846 	case MPL_HIGH:
847 		s = "high";
848 		break;
849 	default:
850 		s = "--";
851 		break;
852 	}
853 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
854 	return (buf);
855 }
856 
857 /*
858  * Convert protect string to a value.
859  */
860 dladm_status_t
861 dladm_str2protect(char *token, uint32_t *ptype)
862 {
863 	link_protect_t	*lp;
864 	int		i;
865 
866 	for (i = 0; i < LPTYPES; i++) {
867 		lp = &link_protect_types[i];
868 		if (strcmp(token, lp->lp_name) == 0) {
869 			*ptype = lp->lp_type;
870 			return (DLADM_STATUS_OK);
871 		}
872 	}
873 	return (DLADM_STATUS_BADVAL);
874 }
875 
876 /*
877  * Convert protect value to a string.
878  */
879 const char *
880 dladm_protect2str(uint32_t ptype, char *buf)
881 {
882 	const char	*s = "--";
883 	link_protect_t	*lp;
884 	int		i;
885 
886 	for (i = 0; i < LPTYPES; i++) {
887 		lp = &link_protect_types[i];
888 		if (lp->lp_type == ptype) {
889 			s = lp->lp_name;
890 			break;
891 		}
892 	}
893 	(void) snprintf(buf, DLADM_STRSIZE, "%s", dgettext(TEXT_DOMAIN, s));
894 	return (buf);
895 }
896 
897 /*
898  * Convert an IPv4 address to/from a string.
899  */
900 const char *
901 dladm_ipv4addr2str(void *addr, char *buf)
902 {
903 	if (inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN) == NULL)
904 		buf[0] = 0;
905 
906 	return (buf);
907 }
908 
909 dladm_status_t
910 dladm_str2ipv4addr(char *token, void *addr)
911 {
912 	return (inet_pton(AF_INET, token, addr) == 1 ?
913 	    DLADM_STATUS_OK : DLADM_STATUS_INVALID_IP);
914 }
915 
916 /*
917  * Find the set bits in a mask.
918  * This is used for expanding a bitmask into individual sub-masks
919  * which can be used for further processing.
920  */
921 void
922 dladm_find_setbits32(uint32_t mask, uint32_t *list, uint32_t *cnt)
923 {
924 	int	i, c = 0;
925 
926 	for (i = 0; i < 32; i++) {
927 		if (((1 << i) & mask) != 0)
928 			list[c++] = 1 << i;
929 	}
930 	*cnt = c;
931 }
932 
933 void
934 dladm_free_args(dladm_arg_list_t *list)
935 {
936 	if (list != NULL) {
937 		free(list->al_buf);
938 		free(list);
939 	}
940 }
941 
942 dladm_status_t
943 dladm_parse_args(char *str, dladm_arg_list_t **listp, boolean_t novalues)
944 {
945 	dladm_arg_list_t	*list;
946 	dladm_arg_info_t	*aip;
947 	char			*buf, *curr;
948 	int			len, i;
949 
950 	if (str == NULL)
951 		return (DLADM_STATUS_BADVAL);
952 
953 	if (str[0] == '\0')
954 		return (DLADM_STATUS_OK);
955 
956 	list = malloc(sizeof (dladm_arg_list_t));
957 	if (list == NULL)
958 		return (dladm_errno2status(errno));
959 
960 	list->al_count = 0;
961 	list->al_buf = buf = strdup(str);
962 	if (buf == NULL)
963 		return (dladm_errno2status(errno));
964 
965 	curr = buf;
966 	len = strlen(buf);
967 	aip = NULL;
968 	for (i = 0; i < len; i++) {
969 		char		c = buf[i];
970 		boolean_t	match = (c == '=' || c == ',');
971 
972 		if (!match && i != len - 1)
973 			continue;
974 
975 		if (match) {
976 			buf[i] = '\0';
977 			if (*curr == '\0')
978 				goto fail;
979 		}
980 
981 		if (aip != NULL && c != '=') {
982 			if (aip->ai_count > DLADM_MAX_ARG_VALS)
983 				goto fail;
984 
985 			if (novalues)
986 				goto fail;
987 
988 			aip->ai_val[aip->ai_count] = curr;
989 			aip->ai_count++;
990 		} else {
991 			if (list->al_count > DLADM_MAX_ARG_VALS)
992 				goto fail;
993 
994 			aip = &list->al_info[list->al_count];
995 			aip->ai_name = curr;
996 			aip->ai_count = 0;
997 			list->al_count++;
998 			if (c == ',')
999 				aip = NULL;
1000 		}
1001 		curr = buf + i + 1;
1002 	}
1003 
1004 	*listp = list;
1005 	return (DLADM_STATUS_OK);
1006 
1007 fail:
1008 	dladm_free_args(list);
1009 	return (DLADM_STATUS_FAILED);
1010 }
1011