xref: /illumos-gate/usr/src/lib/libdladm/common/libdlflow.c (revision 7a088f03b431bdffa96c3b2175964d4d38420caa)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/ethernet.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <sys/stat.h>
33 #include <sys/dld_ioc.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <stropts.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <strings.h>
41 #include <libintl.h>
42 #include <netdb.h>
43 #include <net/if_types.h>
44 #include <net/if_dl.h>
45 #include <inet/ip.h>
46 #include <inet/ip6.h>
47 #include <libdlflow.h>
48 #include <libdlflow_impl.h>
49 #include <libdladm_impl.h>
50 
51 /* minimum buffer size for DLDIOCWALKFLOW */
52 #define	MIN_INFO_SIZE	(4 * 1024)
53 
54 #define	DLADM_FLOW_DB		"/etc/dladm/flowadm.conf"
55 #define	DLADM_FLOW_DB_TMP	"/etc/dladm/flowadm.conf.new"
56 #define	DLADM_FLOW_DB_LOCK	"/tmp/flowadm.conf.lock"
57 
58 #define	DLADM_FLOW_DB_PERMS	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
59 #define	DLADM_FLOW_DB_OWNER	UID_DLADM
60 #define	DLADM_FLOW_DB_GROUP	GID_SYS
61 
62 #define	BLANK_LINE(s)	((s[0] == '\0') || (s[0] == '#') || (s[0] == '\n'))
63 #define	MAXLINELEN	1024
64 #define	MAXPATHLEN	1024
65 
66 #define	V4_PART_OF_V6(v6)	((v6)._S6_un._S6_u32[3])
67 
68 /* database file parameters */
69 static const char *BW_LIMIT = "bw_limit";
70 static const char *PRIORITY = "priority";
71 static const char *LOCAL_IP_ADDR = "local_ip";
72 static const char *REMOTE_IP_ADDR = "remote_ip";
73 static const char *TRANSPORT = "transport";
74 static const char *LOCAL_PORT = "local_port";
75 static const char *DSFIELD = "dsfield";
76 
77 /*
78  * Open and lock the flowadm configuration file lock. The lock is
79  * acquired as a reader (F_RDLCK) or writer (F_WRLCK).
80  */
81 static int
82 i_dladm_flow_lock_db(short type)
83 {
84 	int lock_fd;
85 	struct flock lock;
86 
87 	if ((lock_fd = open(DLADM_FLOW_DB_LOCK, O_RDWR | O_CREAT | O_TRUNC,
88 	    DLADM_FLOW_DB_PERMS)) < 0)
89 		return (-1);
90 
91 	lock.l_type = type;
92 	lock.l_whence = SEEK_SET;
93 	lock.l_start = 0;
94 	lock.l_len = 0;
95 
96 	if (fcntl(lock_fd, F_SETLKW, &lock) < 0) {
97 		(void) close(lock_fd);
98 		(void) unlink(DLADM_FLOW_DB_LOCK);
99 		return (-1);
100 	}
101 	return (lock_fd);
102 }
103 
104 /*
105  * Unlock and close the specified file.
106  */
107 static void
108 i_dladm_flow_unlock_db(int fd)
109 {
110 	struct flock lock;
111 
112 	if (fd < 0)
113 		return;
114 
115 	lock.l_type = F_UNLCK;
116 	lock.l_whence = SEEK_SET;
117 	lock.l_start = 0;
118 	lock.l_len = 0;
119 
120 	(void) fcntl(fd, F_SETLKW, &lock);
121 	(void) close(fd);
122 	(void) unlink(DLADM_FLOW_DB_LOCK);
123 }
124 
125 /*
126  * Parse one line of the link flowadm DB
127  * Returns -1 on failure, 0 on success.
128  */
129 dladm_status_t
130 dladm_flow_parse_db(char *line, dld_flowinfo_t *attr)
131 {
132 	char		*token;
133 	char		*value, *name = NULL;
134 	char		*endp = NULL;
135 	char		*lasts = NULL;
136 	dladm_status_t	status = DLADM_STATUS_FLOW_DB_PARSE_ERR;
137 
138 	bzero(attr, sizeof (*attr));
139 
140 	/* flow name */
141 	if ((token = strtok_r(line, " \t", &lasts)) == NULL)
142 		goto done;
143 
144 	if (strlcpy(attr->fi_flowname, token, MAXFLOWNAMELEN) >= MAXFLOWNAMELEN)
145 		goto done;
146 
147 	/* resource control and flow descriptor parameters */
148 	while ((token = strtok_r(NULL, " \t", &lasts)) != NULL) {
149 		if ((name = strdup(token)) == NULL)
150 			goto done;
151 
152 		(void) strtok(name, "=");
153 		value = strtok(NULL, "=");
154 		if (value == NULL)
155 			goto done;
156 
157 		if (strcmp(name, "linkid") == 0) {
158 			if ((attr->fi_linkid =
159 			    (uint32_t)strtol(value, &endp, 10)) ==
160 			    DATALINK_INVALID_LINKID)
161 				goto done;
162 
163 		} else if (strcmp(name, BW_LIMIT) == 0) {
164 			attr->fi_resource_props.mrp_mask |=
165 			    MRP_MAXBW;
166 			attr->fi_resource_props.mrp_maxbw =
167 			    (uint64_t)strtol(value, &endp, 0);
168 
169 		} else if (strcmp(name, PRIORITY) == 0) {
170 			attr->fi_resource_props.mrp_mask |= MRP_PRIORITY;
171 			status = dladm_str2pri(value,
172 			    &attr->fi_resource_props.mrp_priority);
173 			if (status != DLADM_STATUS_OK)
174 				goto done;
175 
176 		} else if (strcmp(name, DSFIELD) == 0) {
177 			status = do_check_dsfield(value,
178 			    &attr->fi_flow_desc);
179 			if (status != DLADM_STATUS_OK)
180 				goto done;
181 
182 		} else if (strcmp(name, LOCAL_IP_ADDR) == 0) {
183 			status = do_check_ip_addr(value, B_TRUE,
184 			    &attr->fi_flow_desc);
185 			if (status != DLADM_STATUS_OK)
186 				goto done;
187 
188 		} else if (strcmp(name, REMOTE_IP_ADDR) == 0) {
189 			status = do_check_ip_addr(value, B_FALSE,
190 			    &attr->fi_flow_desc);
191 			if (status != DLADM_STATUS_OK)
192 				goto done;
193 
194 		} else if (strcmp(name, TRANSPORT) == 0) {
195 			attr->fi_flow_desc.fd_mask |= FLOW_IP_PROTOCOL;
196 			attr->fi_flow_desc.fd_protocol =
197 			    (uint8_t)strtol(value, &endp, 0);
198 
199 		} else if (strcmp(name, LOCAL_PORT) == 0) {
200 			attr->fi_flow_desc.fd_mask |= FLOW_ULP_PORT_LOCAL;
201 			attr->fi_flow_desc.fd_local_port =
202 			    (uint16_t)strtol(value, &endp, 10);
203 			attr->fi_flow_desc.fd_local_port =
204 			    htons(attr->fi_flow_desc.fd_local_port);
205 		}
206 		free(name);
207 		name = NULL;
208 	}
209 	if (attr->fi_linkid != DATALINK_INVALID_LINKID)
210 		status = DLADM_STATUS_OK;
211 done:
212 	free(name);
213 	return (status);
214 }
215 
216 #define	FPRINTF_ERR(fcall) if ((fcall) < 0) return (-1);
217 
218 /*
219  * Write the attribute of a group to the specified file. Returns 0 on
220  * success, -1 on failure.
221  */
222 static int
223 i_dladm_flow_fput_grp(FILE *fp, dld_flowinfo_t *attr)
224 {
225 
226 	FPRINTF_ERR(fprintf(fp, "%s\tlinkid=%d\t",
227 	    attr->fi_flowname, attr->fi_linkid));
228 
229 	/* flow policy */
230 	if (attr->fi_resource_props.mrp_mask & MRP_MAXBW)
231 		FPRINTF_ERR(fprintf(fp, "%s=%" PRIu64 "\t", BW_LIMIT,
232 		    attr->fi_resource_props.mrp_maxbw));
233 
234 	if (attr->fi_resource_props.mrp_mask & MRP_PRIORITY)
235 		FPRINTF_ERR(fprintf(fp, "%s=%d\t", PRIORITY,
236 		    attr->fi_resource_props.mrp_priority));
237 
238 	/* flow descriptor */
239 	if (attr->fi_flow_desc.fd_mask & FLOW_IP_DSFIELD)
240 		FPRINTF_ERR(fprintf(fp, "%s=%x:%x\t", DSFIELD,
241 		    attr->fi_flow_desc.fd_dsfield,
242 		    attr->fi_flow_desc.fd_dsfield_mask));
243 
244 	if (attr->fi_flow_desc.fd_mask & FLOW_IP_LOCAL) {
245 		char abuf[INET6_ADDRSTRLEN], *ap;
246 		struct in_addr ipaddr;
247 		int prefix_len, prefix_max;
248 
249 		if (attr->fi_flow_desc.fd_ipversion != 6) {
250 			ipaddr.s_addr =
251 			    attr->fi_flow_desc.
252 			    fd_local_addr._S6_un._S6_u32[3];
253 
254 			ap = inet_ntoa(ipaddr);
255 			prefix_max = IP_ABITS;
256 		} else {
257 			(void) inet_ntop(AF_INET6,
258 			    &attr->fi_flow_desc.fd_local_addr,
259 			    abuf, INET6_ADDRSTRLEN);
260 
261 			ap = abuf;
262 			prefix_max = IPV6_ABITS;
263 		}
264 		(void) dladm_mask2prefixlen(
265 		    &attr->fi_flow_desc.fd_local_netmask, prefix_max,
266 		    &prefix_len);
267 
268 		FPRINTF_ERR(fprintf(fp, "%s=%s/%d\t", LOCAL_IP_ADDR,
269 		    ap, prefix_len));
270 	}
271 	if (attr->fi_flow_desc.fd_mask & FLOW_IP_REMOTE) {
272 		char abuf[INET6_ADDRSTRLEN], *ap;
273 		struct in_addr ipaddr;
274 		int prefix_len, prefix_max;
275 
276 		if (attr->fi_flow_desc.fd_ipversion != 6) {
277 			ipaddr.s_addr =
278 			    attr->fi_flow_desc.
279 			    fd_remote_addr._S6_un._S6_u32[3];
280 
281 			ap = inet_ntoa(ipaddr);
282 			prefix_max = IP_ABITS;
283 		} else {
284 			(void) inet_ntop(AF_INET6,
285 			    &(attr->fi_flow_desc.fd_remote_addr),
286 			    abuf, INET6_ADDRSTRLEN);
287 
288 			ap = abuf;
289 			prefix_max = IPV6_ABITS;
290 		}
291 		(void) dladm_mask2prefixlen(
292 		    &attr->fi_flow_desc.fd_remote_netmask, prefix_max,
293 		    &prefix_len);
294 
295 		FPRINTF_ERR(fprintf(fp, "%s=%s/%d\t", REMOTE_IP_ADDR,
296 		    ap, prefix_len));
297 	}
298 	if (attr->fi_flow_desc.fd_mask & FLOW_IP_PROTOCOL)
299 		FPRINTF_ERR(fprintf(fp, "%s=%d\t", TRANSPORT,
300 		    attr->fi_flow_desc.fd_protocol));
301 
302 	if (attr->fi_flow_desc.fd_mask & FLOW_ULP_PORT_LOCAL)
303 		FPRINTF_ERR(fprintf(fp, "%s=%d\t", LOCAL_PORT,
304 		    ntohs(attr->fi_flow_desc.fd_local_port)));
305 
306 	FPRINTF_ERR(fprintf(fp, "\n"));
307 
308 	return (0);
309 
310 }
311 
312 static dladm_status_t
313 i_dladm_flow_walk_rw_db(int (*fn)(void *, dld_flowinfo_t *),
314     void *arg,
315     const char *root)
316 {
317 	FILE *fp, *nfp;
318 	int nfd, fn_rc, lock_fd;
319 	char line[MAXLINELEN];
320 	dld_flowinfo_t attr;
321 	char *db_file, *tmp_db_file;
322 	char db_file_buf[MAXPATHLEN];
323 	char tmp_db_file_buf[MAXPATHLEN];
324 	dladm_status_t	status = DLADM_STATUS_FLOW_DB_ERR;
325 
326 	if (root == NULL) {
327 		db_file = DLADM_FLOW_DB;
328 		tmp_db_file = DLADM_FLOW_DB_TMP;
329 	} else {
330 		(void) snprintf(db_file_buf, MAXPATHLEN, "%s%s", root,
331 		    DLADM_FLOW_DB);
332 		(void) snprintf(tmp_db_file_buf, MAXPATHLEN, "%s%s", root,
333 		    DLADM_FLOW_DB_TMP);
334 		db_file = db_file_buf;
335 		tmp_db_file = tmp_db_file_buf;
336 	}
337 
338 	if ((lock_fd = i_dladm_flow_lock_db(F_WRLCK)) < 0)
339 		return (DLADM_STATUS_FLOW_DB_ERR);
340 
341 	if ((fp = fopen(db_file, "r")) == NULL) {
342 		i_dladm_flow_unlock_db(lock_fd);
343 		return (DLADM_STATUS_FLOW_DB_OPEN_ERR);
344 	}
345 
346 	if ((nfd = open(tmp_db_file, O_WRONLY|O_CREAT|O_TRUNC,
347 	    DLADM_FLOW_DB_PERMS)) == -1) {
348 		(void) fclose(fp);
349 		i_dladm_flow_unlock_db(lock_fd);
350 		return (DLADM_STATUS_FLOW_DB_OPEN_ERR);
351 	}
352 
353 	if ((nfp = fdopen(nfd, "w")) == NULL) {
354 		(void) close(nfd);
355 		(void) fclose(fp);
356 		(void) unlink(tmp_db_file);
357 		i_dladm_flow_unlock_db(lock_fd);
358 		return (DLADM_STATUS_FLOW_DB_OPEN_ERR);
359 	}
360 
361 	while (fgets(line, MAXLINELEN, fp) != NULL) {
362 
363 		/* skip comments */
364 		if (BLANK_LINE(line)) {
365 			if (fputs(line, nfp) == EOF)
366 				goto failed;
367 			continue;
368 		}
369 		(void) strtok(line, " \n");
370 
371 		if ((status = dladm_flow_parse_db(line, &attr)) !=
372 		    DLADM_STATUS_OK)
373 			goto failed;
374 
375 		fn_rc = fn(arg, &attr);
376 
377 		switch (fn_rc) {
378 		case -1:
379 			/* failure, stop walking */
380 			goto failed;
381 		case 0:
382 			/*
383 			 * Success, write group attributes, which could
384 			 * have been modified by fn().
385 			 */
386 			if (i_dladm_flow_fput_grp(nfp, &attr) != 0)
387 				goto failed;
388 			break;
389 		case 1:
390 			/* skip current group */
391 			break;
392 		}
393 	}
394 	if (fchmod(nfd, DLADM_FLOW_DB_PERMS) == -1)
395 		goto failed;
396 
397 	if (fchown(nfd, DLADM_FLOW_DB_OWNER, DLADM_FLOW_DB_GROUP) == -1)
398 		goto failed;
399 
400 	if (fflush(nfp) == EOF)
401 		goto failed;
402 
403 	(void) fclose(fp);
404 	(void) fclose(nfp);
405 
406 	if (rename(tmp_db_file, db_file) == -1) {
407 		(void) unlink(tmp_db_file);
408 		i_dladm_flow_unlock_db(lock_fd);
409 		return (DLADM_STATUS_FLOW_DB_ERR);
410 	}
411 	i_dladm_flow_unlock_db(lock_fd);
412 	return (DLADM_STATUS_OK);
413 
414 failed:
415 	(void) fclose(fp);
416 	(void) fclose(nfp);
417 	(void) unlink(tmp_db_file);
418 	i_dladm_flow_unlock_db(lock_fd);
419 
420 	return (status);
421 }
422 
423 /*
424  * Remove existing flow from DB.
425  */
426 
427 typedef struct remove_db_state {
428 	dld_flowinfo_t	rs_newattr;
429 	dld_flowinfo_t	rs_oldattr;
430 	boolean_t	rs_found;
431 } remove_db_state_t;
432 
433 static int
434 i_dladm_flow_remove_db_fn(void *arg, dld_flowinfo_t *grp)
435 {
436 	remove_db_state_t *state = (remove_db_state_t *)arg;
437 	dld_flowinfo_t *attr = &state->rs_newattr;
438 
439 	if ((strcmp(grp->fi_flowname, attr->fi_flowname)) != 0)
440 		return (0);
441 	else {
442 		bcopy(grp, &state->rs_oldattr,
443 		    sizeof (dld_flowinfo_t));
444 		state->rs_found = B_TRUE;
445 		return (1);
446 	}
447 }
448 
449 /* ARGSUSED */
450 static int
451 i_dladm_flow_remove_db(remove_db_state_t *state, const char *root)
452 {
453 	if (i_dladm_flow_walk_rw_db(i_dladm_flow_remove_db_fn, state, root)
454 	    != 0)
455 		return (-1);
456 
457 	if (!state->rs_found) {
458 		errno = ENOENT;
459 		return (-1);
460 	}
461 
462 	return (0);
463 }
464 
465 /*
466  * Create a flow in the DB.
467  */
468 
469 typedef struct modify_db_state {
470 	dld_flowinfo_t	ms_newattr;
471 	dld_flowinfo_t	ms_oldattr;
472 	boolean_t	ms_found;
473 } modify_db_state_t;
474 
475 static dladm_status_t
476 i_dladm_flow_create_db(dld_flowinfo_t *attr, const char *root)
477 {
478 	FILE 	*fp;
479 	char 	line[MAXLINELEN];
480 	char	*db_file;
481 	char	db_file_buf[MAXPATHLEN];
482 	int	lock_fd;
483 	dladm_status_t	status = DLADM_STATUS_OK;
484 
485 	if (root == NULL) {
486 		db_file = DLADM_FLOW_DB;
487 	} else {
488 		(void) snprintf(db_file_buf, MAXPATHLEN, "%s%s", root,
489 		    DLADM_FLOW_DB);
490 		db_file = db_file_buf;
491 	}
492 
493 	if ((lock_fd = i_dladm_flow_lock_db(F_WRLCK)) < 0)
494 		return (DLADM_STATUS_FLOW_DB_ERR);
495 
496 	if ((fp = fopen(db_file, "r+")) == NULL &&
497 	    (fp = fopen(db_file, "w")) == NULL) {
498 		i_dladm_flow_unlock_db(lock_fd);
499 		return (DLADM_STATUS_FLOW_DB_OPEN_ERR);
500 	}
501 
502 	/* look for existing group with same flowname */
503 	while (fgets(line, MAXLINELEN, fp) != NULL) {
504 		char *holder, *lasts;
505 
506 		/* skip comments */
507 		if (BLANK_LINE(line))
508 			continue;
509 
510 		/* ignore corrupted lines */
511 		holder = strtok_r(line, " \t", &lasts);
512 		if (holder == NULL)
513 			continue;
514 
515 		/* flow id */
516 		if (strcmp(holder, attr->fi_flowname) == 0) {
517 			/* group with flow id already exists */
518 			status = DLADM_STATUS_PERSIST_FLOW_EXISTS;
519 			goto failed;
520 		}
521 	}
522 	/*
523 	 * If we get here, we've verified that no existing group with
524 	 * the same flow id already exists. Its now time to add the new
525 	 * group to the DB.
526 	 */
527 	if (i_dladm_flow_fput_grp(fp, attr) != 0)
528 		status = DLADM_STATUS_FLOW_DB_PARSE_ERR;
529 
530 failed:
531 	(void) fclose(fp);
532 	i_dladm_flow_unlock_db(lock_fd);
533 	return (status);
534 }
535 
536 static dladm_status_t
537 i_dladm_flow_add(dladm_handle_t handle, char *flowname, datalink_id_t linkid,
538     flow_desc_t *flowdesc, mac_resource_props_t *mrp)
539 {
540 	dld_ioc_addflow_t	attr;
541 
542 	/* create flow */
543 	bzero(&attr, sizeof (attr));
544 	bcopy(flowdesc, &attr.af_flow_desc, sizeof (flow_desc_t));
545 	if (mrp != NULL) {
546 		bcopy(mrp, &attr.af_resource_props,
547 		    sizeof (mac_resource_props_t));
548 	}
549 
550 	(void) strlcpy(attr.af_name, flowname, sizeof (attr.af_name));
551 	attr.af_linkid = linkid;
552 
553 	if (ioctl(dladm_dld_fd(handle), DLDIOC_ADDFLOW, &attr) < 0)
554 		return (dladm_errno2status(errno));
555 
556 	return (DLADM_STATUS_OK);
557 }
558 
559 static dladm_status_t
560 i_dladm_flow_remove(dladm_handle_t handle, char *flowname)
561 {
562 	dld_ioc_removeflow_t	attr;
563 	dladm_status_t		status = DLADM_STATUS_OK;
564 
565 	(void) strlcpy(attr.rf_name, flowname,
566 	    sizeof (attr.rf_name));
567 
568 	if (ioctl(dladm_dld_fd(handle), DLDIOC_REMOVEFLOW, &attr) < 0)
569 		status = dladm_errno2status(errno);
570 
571 	return (status);
572 }
573 
574 
575 /* ARGSUSED */
576 dladm_status_t
577 dladm_flow_add(dladm_handle_t handle, datalink_id_t linkid,
578     dladm_arg_list_t *attrlist, dladm_arg_list_t *proplist, char *flowname,
579     boolean_t tempop, const char *root)
580 {
581 	dld_flowinfo_t		db_attr;
582 	flow_desc_t		flowdesc;
583 	mac_resource_props_t	mrp;
584 	dladm_status_t		status;
585 
586 	/* Extract flow attributes from attrlist */
587 	bzero(&flowdesc, sizeof (flow_desc_t));
588 	if (attrlist != NULL && (status = dladm_flow_attrlist_extract(attrlist,
589 	    &flowdesc)) != DLADM_STATUS_OK) {
590 		return (status);
591 	}
592 
593 	/* Extract resource_ctl and cpu_list from proplist */
594 	bzero(&mrp, sizeof (mac_resource_props_t));
595 	if (proplist != NULL && (status = dladm_flow_proplist_extract(proplist,
596 	    &mrp)) != DLADM_STATUS_OK) {
597 		return (status);
598 	}
599 
600 	/* Add flow in kernel */
601 	status = i_dladm_flow_add(handle, flowname, linkid, &flowdesc, &mrp);
602 	if (status != DLADM_STATUS_OK)
603 		return (status);
604 
605 	/* Add flow to DB */
606 	if (!tempop) {
607 		bzero(&db_attr, sizeof (db_attr));
608 		bcopy(&flowdesc, &db_attr.fi_flow_desc, sizeof (flow_desc_t));
609 		(void) strlcpy(db_attr.fi_flowname, flowname,
610 		    sizeof (db_attr.fi_flowname));
611 		db_attr.fi_linkid = linkid;
612 
613 		if ((status = i_dladm_flow_create_db(&db_attr, root)) !=
614 		    DLADM_STATUS_OK) {
615 			(void) i_dladm_flow_remove(handle, flowname);
616 			return (status);
617 		}
618 		/* set flow properties */
619 		if (proplist != NULL) {
620 			status = i_dladm_set_flow_proplist_db(handle, flowname,
621 			    proplist);
622 			if (status != DLADM_STATUS_OK) {
623 				(void) i_dladm_flow_remove(handle, flowname);
624 				return (status);
625 			}
626 		}
627 	}
628 	return (status);
629 }
630 
631 /*
632  * Remove a flow.
633  */
634 /* ARGSUSED */
635 dladm_status_t
636 dladm_flow_remove(dladm_handle_t handle, char *flowname, boolean_t tempop,
637     const char *root)
638 {
639 	remove_db_state_t		state;
640 	dladm_status_t			status = DLADM_STATUS_OK;
641 	dladm_status_t			s = DLADM_STATUS_OK;
642 
643 	/* remove flow */
644 	status = i_dladm_flow_remove(handle, flowname);
645 	if ((status != DLADM_STATUS_OK) &&
646 	    (tempop || status != DLADM_STATUS_NOTFOUND))
647 		goto done;
648 
649 	/* remove flow from DB */
650 	if (!tempop) {
651 		bzero(&state, sizeof (state));
652 		(void) strlcpy(state.rs_newattr.fi_flowname, flowname,
653 		    sizeof (state.rs_newattr.fi_flowname));
654 		state.rs_found = B_FALSE;
655 
656 		/* flow DB */
657 		if (i_dladm_flow_remove_db(&state, root) < 0) {
658 			s = dladm_errno2status(errno);
659 			goto done;
660 		}
661 
662 		/* flow prop DB */
663 		s = dladm_set_flowprop(handle, flowname, NULL, NULL, 0,
664 		    DLADM_OPT_PERSIST, NULL);
665 	}
666 
667 done:
668 	if (!tempop) {
669 		if (s == DLADM_STATUS_OK) {
670 			if (status == DLADM_STATUS_NOTFOUND)
671 				status = s;
672 		} else {
673 			if (s != DLADM_STATUS_NOTFOUND)
674 				status = s;
675 		}
676 	}
677 	return (status);
678 }
679 
680 /*
681  * Get an existing flow in the DB.
682  */
683 
684 typedef struct get_db_state {
685 	int		(*gs_fn)(dladm_flow_attr_t *, void *);
686 	void		*gs_arg;
687 	datalink_id_t	gs_linkid;
688 } get_db_state_t;
689 
690 /*
691  * For each flow which matches the linkid, copy all flow information
692  * to a new dladm_flow_attr_t structure and call the provided
693  * function.  This is used to display perisistent flows from
694  * the database.
695  */
696 
697 static int
698 i_dladm_flow_get_db_fn(void *arg, dld_flowinfo_t *grp)
699 {
700 	get_db_state_t		*state = (get_db_state_t *)arg;
701 	dladm_flow_attr_t	attr;
702 
703 	if (grp->fi_linkid == state->gs_linkid) {
704 		attr.fa_linkid = state->gs_linkid;
705 		bcopy(grp->fi_flowname, &attr.fa_flowname,
706 		    sizeof (attr.fa_flowname));
707 		bcopy(&grp->fi_flow_desc, &attr.fa_flow_desc,
708 		    sizeof (attr.fa_flow_desc));
709 		bcopy(&grp->fi_resource_props, &attr.fa_resource_props,
710 		    sizeof (attr.fa_resource_props));
711 		(void) state->gs_fn(&attr, state->gs_arg);
712 	}
713 	return (0);
714 }
715 
716 /*
717  * Walk through the flows defined on the system and for each flow
718  * invoke <fn>(<arg>, <flow>);
719  * Currently used for show-flow.
720  */
721 /* ARGSUSED */
722 dladm_status_t
723 dladm_walk_flow(int (*fn)(dladm_flow_attr_t *, void *), dladm_handle_t handle,
724     datalink_id_t linkid, void *arg, boolean_t persist)
725 {
726 	dld_flowinfo_t		*flow;
727 	int			i, bufsize;
728 	dld_ioc_walkflow_t	*ioc = NULL;
729 	dladm_flow_attr_t 	attr;
730 	dladm_status_t		status = DLADM_STATUS_OK;
731 
732 	if (fn == NULL)
733 		return (DLADM_STATUS_BADARG);
734 
735 	if (persist) {
736 		get_db_state_t state;
737 
738 		bzero(&state, sizeof (state));
739 
740 		state.gs_linkid = linkid;
741 		state.gs_fn = fn;
742 		state.gs_arg = arg;
743 		status = i_dladm_flow_walk_rw_db(i_dladm_flow_get_db_fn,
744 		    &state, NULL);
745 		if (status != DLADM_STATUS_OK)
746 			return (status);
747 	} else {
748 		bufsize = MIN_INFO_SIZE;
749 		if ((ioc = calloc(1, bufsize)) == NULL) {
750 			status = dladm_errno2status(errno);
751 			return (status);
752 		}
753 
754 		ioc->wf_linkid = linkid;
755 		ioc->wf_len = bufsize - sizeof (*ioc);
756 
757 		while (ioctl(dladm_dld_fd(handle), DLDIOC_WALKFLOW, ioc) < 0) {
758 			if (errno == ENOSPC) {
759 				bufsize *= 2;
760 				ioc = realloc(ioc, bufsize);
761 				if (ioc != NULL) {
762 					ioc->wf_linkid = linkid;
763 					ioc->wf_len = bufsize - sizeof (*ioc);
764 					continue;
765 				}
766 			}
767 			goto bail;
768 		}
769 
770 		flow = (dld_flowinfo_t *)(void *)(ioc + 1);
771 		for (i = 0; i < ioc->wf_nflows; i++, flow++) {
772 			bzero(&attr, sizeof (attr));
773 
774 			attr.fa_linkid = flow->fi_linkid;
775 			bcopy(&flow->fi_flowname, &attr.fa_flowname,
776 			    sizeof (attr.fa_flowname));
777 			bcopy(&flow->fi_flow_desc, &attr.fa_flow_desc,
778 			    sizeof (attr.fa_flow_desc));
779 			bcopy(&flow->fi_resource_props, &attr.fa_resource_props,
780 			    sizeof (attr.fa_resource_props));
781 
782 			if (fn(&attr, arg) == DLADM_WALK_TERMINATE)
783 				break;
784 		}
785 	}
786 
787 bail:
788 	free(ioc);
789 	return (status);
790 }
791 
792 dladm_status_t
793 dladm_flow_init(dladm_handle_t handle)
794 {
795 	flow_desc_t		flowdesc;
796 	datalink_id_t		linkid;
797 	dladm_status_t		s, status = DLADM_STATUS_OK;
798 	char			name[MAXFLOWNAMELEN];
799 	char			line[MAXLINELEN];
800 	dld_flowinfo_t		attr;
801 	FILE			*fp;
802 
803 	if ((fp = fopen(DLADM_FLOW_DB, "r")) == NULL)
804 		return (DLADM_STATUS_DB_NOTFOUND);
805 
806 	while (fgets(line, MAXLINELEN, fp) != NULL) {
807 		/* skip comments */
808 		if (BLANK_LINE(line))
809 			continue;
810 
811 		(void) strtok(line, " \n");
812 
813 		s = dladm_flow_parse_db(line, &attr);
814 		if (s != DLADM_STATUS_OK) {
815 			status = s;
816 			continue;
817 		}
818 		bzero(&flowdesc, sizeof (flowdesc));
819 		bcopy(&attr.fi_flow_desc, &flowdesc, sizeof (flow_desc_t));
820 		(void) strlcpy(name, attr.fi_flowname,
821 		    sizeof (attr.fi_flowname));
822 		linkid = attr.fi_linkid;
823 
824 		s = i_dladm_flow_add(handle, name, linkid, &flowdesc, NULL);
825 		if (s != DLADM_STATUS_OK)
826 			status = s;
827 	}
828 	s = i_dladm_init_flowprop_db(handle);
829 	if (s != DLADM_STATUS_OK)
830 		status = s;
831 
832 	(void) fclose(fp);
833 	return (status);
834 }
835 
836 dladm_status_t
837 dladm_prefixlen2mask(int prefixlen, int maxlen, uchar_t *mask)
838 {
839 	if (prefixlen < 0 || prefixlen > maxlen)
840 		return (DLADM_STATUS_BADARG);
841 
842 	while (prefixlen > 0) {
843 		if (prefixlen >= 8) {
844 			*mask++ = 0xFF;
845 			prefixlen -= 8;
846 			continue;
847 		}
848 		*mask |= 1 << (8 - prefixlen);
849 		prefixlen--;
850 	}
851 	return (DLADM_STATUS_OK);
852 }
853 
854 dladm_status_t
855 dladm_mask2prefixlen(in6_addr_t *mask, int plen, int *prefixlen)
856 {
857 	int		bits;
858 	int		i, end;
859 
860 	switch (plen) {
861 	case IP_ABITS:
862 		end = 3;
863 		break;
864 	case IPV6_ABITS:
865 		end = 0;
866 		break;
867 	default:
868 		return (DLADM_STATUS_BADARG);
869 	}
870 
871 	for (i = 3; i >= end; i--) {
872 		if (mask->_S6_un._S6_u32[i] == 0) {
873 			plen -= 32;
874 			continue;
875 		}
876 		bits = ffs(ntohl(mask->_S6_un._S6_u32[i])) - 1;
877 		if (bits == 0)
878 			break;
879 		plen -= bits;
880 	}
881 	*prefixlen = plen;
882 	return (DLADM_STATUS_OK);
883 }
884