xref: /titanic_52/usr/src/lib/libdladm/common/flowattr.c (revision 25ec3e3dd27cc1038c10efa18ed08f064eab5fbe)
1da14cebeSEric Cheng /*
2da14cebeSEric Cheng  * CDDL HEADER START
3da14cebeSEric Cheng  *
4da14cebeSEric Cheng  * The contents of this file are subject to the terms of the
5da14cebeSEric Cheng  * Common Development and Distribution License (the "License").
6da14cebeSEric Cheng  * You may not use this file except in compliance with the License.
7da14cebeSEric Cheng  *
8da14cebeSEric Cheng  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da14cebeSEric Cheng  * or http://www.opensolaris.org/os/licensing.
10da14cebeSEric Cheng  * See the License for the specific language governing permissions
11da14cebeSEric Cheng  * and limitations under the License.
12da14cebeSEric Cheng  *
13da14cebeSEric Cheng  * When distributing Covered Code, include this CDDL HEADER in each
14da14cebeSEric Cheng  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da14cebeSEric Cheng  * If applicable, add the following below this CDDL HEADER, with the
16da14cebeSEric Cheng  * fields enclosed by brackets "[]" replaced with your own identifying
17da14cebeSEric Cheng  * information: Portions Copyright [yyyy] [name of copyright owner]
18da14cebeSEric Cheng  *
19da14cebeSEric Cheng  * CDDL HEADER END
20da14cebeSEric Cheng  */
21da14cebeSEric Cheng /*
22da000602SGirish Moodalbail  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23da14cebeSEric Cheng  * Use is subject to license terms.
24da14cebeSEric Cheng  */
25da14cebeSEric Cheng 
26da14cebeSEric Cheng #include <errno.h>
27da14cebeSEric Cheng #include <stdlib.h>
28da14cebeSEric Cheng #include <strings.h>
29da14cebeSEric Cheng #include <sys/mac_flow.h>
30da14cebeSEric Cheng #include <sys/types.h>
31da14cebeSEric Cheng #include <sys/socket.h>
32da14cebeSEric Cheng #include <netinet/in.h>
33da14cebeSEric Cheng #include <arpa/inet.h>
34da14cebeSEric Cheng #include <netdb.h>
35da14cebeSEric Cheng #include <net/if_types.h>
36da14cebeSEric Cheng #include <net/if_dl.h>
37da14cebeSEric Cheng #include <inet/ip.h>
38da14cebeSEric Cheng #include <inet/ip6.h>
39da14cebeSEric Cheng 
40da14cebeSEric Cheng #include <libdladm.h>
41da14cebeSEric Cheng #include <libdlflow.h>
42da14cebeSEric Cheng #include <libdlflow_impl.h>
43da14cebeSEric Cheng 
44da14cebeSEric Cheng #define	V4_PART_OF_V6(v6)	((v6)._S6_un._S6_u32[3])
45da14cebeSEric Cheng 
46da14cebeSEric Cheng /* max port number for UDP, TCP & SCTP */
47da14cebeSEric Cheng #define	MAX_PORT	65535
48da14cebeSEric Cheng 
49da14cebeSEric Cheng static fad_checkf_t do_check_local_ip;
50da14cebeSEric Cheng static fad_checkf_t do_check_remote_ip;
51da14cebeSEric Cheng static fad_checkf_t do_check_protocol;
52da14cebeSEric Cheng static fad_checkf_t do_check_local_port;
53*25ec3e3dSEric Cheng static fad_checkf_t do_check_remote_port;
54da14cebeSEric Cheng 
55da14cebeSEric Cheng static dladm_status_t do_check_port(char *, boolean_t, flow_desc_t *);
56da14cebeSEric Cheng 
57da14cebeSEric Cheng static fattr_desc_t	attr_table[] = {
58da14cebeSEric Cheng 	{ "local_ip",		do_check_local_ip },
59da14cebeSEric Cheng 	{ "remote_ip",		do_check_remote_ip },
60da14cebeSEric Cheng 	{ "transport",		do_check_protocol },
61da14cebeSEric Cheng 	{ "local_port",		do_check_local_port },
62*25ec3e3dSEric Cheng 	{ "remote_port",	do_check_remote_port },
63da14cebeSEric Cheng 	{ "dsfield",		do_check_dsfield },
64da14cebeSEric Cheng };
65da14cebeSEric Cheng 
66da14cebeSEric Cheng #define	DLADM_MAX_FLOWATTRS	(sizeof (attr_table) / sizeof (fattr_desc_t))
67da14cebeSEric Cheng 
68da14cebeSEric Cheng static dladm_status_t
69da14cebeSEric Cheng do_check_local_ip(char *attr_val, flow_desc_t *fdesc)
70da14cebeSEric Cheng {
71da14cebeSEric Cheng 	return (do_check_ip_addr(attr_val, B_TRUE, fdesc));
72da14cebeSEric Cheng }
73da14cebeSEric Cheng 
74da14cebeSEric Cheng static dladm_status_t
75da14cebeSEric Cheng do_check_remote_ip(char *attr_val, flow_desc_t *fdesc)
76da14cebeSEric Cheng {
77da14cebeSEric Cheng 	return (do_check_ip_addr(attr_val, B_FALSE, fdesc));
78da14cebeSEric Cheng }
79da14cebeSEric Cheng 
80da14cebeSEric Cheng dladm_status_t
81da14cebeSEric Cheng do_check_ip_addr(char *addr_str, boolean_t local, flow_desc_t *fd)
82da14cebeSEric Cheng {
83da14cebeSEric Cheng 	dladm_status_t	status;
84da000602SGirish Moodalbail 	int		prefix_max, prefix_len = 0;
85da14cebeSEric Cheng 	char		*prefix_str, *endp = NULL;
86da14cebeSEric Cheng 	flow_mask_t	mask;
87da14cebeSEric Cheng 	in6_addr_t	*addr;
88da14cebeSEric Cheng 	uchar_t		*netmask;
89da000602SGirish Moodalbail 	struct in_addr	v4addr;
90da000602SGirish Moodalbail 	struct in6_addr	v6addr;
91da000602SGirish Moodalbail 	int		family;
92da14cebeSEric Cheng 
93da14cebeSEric Cheng 	if ((prefix_str = strchr(addr_str, '/')) != NULL) {
94da14cebeSEric Cheng 		*prefix_str++ = '\0';
95da14cebeSEric Cheng 		errno = 0;
96da14cebeSEric Cheng 		prefix_len = (int)strtol(prefix_str, &endp, 10);
97da14cebeSEric Cheng 		if (errno != 0 || prefix_len == 0 || *endp != '\0')
98da14cebeSEric Cheng 			return (DLADM_STATUS_INVALID_PREFIXLEN);
99da14cebeSEric Cheng 	}
100da000602SGirish Moodalbail 	if (inet_pton(AF_INET, addr_str, &v4addr.s_addr) == 1) {
101da000602SGirish Moodalbail 		family = AF_INET;
102da000602SGirish Moodalbail 	} else if (inet_pton(AF_INET6, addr_str, v6addr.s6_addr) == 1) {
103da000602SGirish Moodalbail 		family = AF_INET6;
104da000602SGirish Moodalbail 	} else {
105da14cebeSEric Cheng 		return (DLADM_STATUS_INVALID_IP);
106da000602SGirish Moodalbail 	}
107da14cebeSEric Cheng 
108da14cebeSEric Cheng 	mask = FLOW_IP_VERSION;
109da14cebeSEric Cheng 	if (local) {
110da14cebeSEric Cheng 		mask |= FLOW_IP_LOCAL;
111da14cebeSEric Cheng 		addr = &fd->fd_local_addr;
112da14cebeSEric Cheng 		netmask = (uchar_t *)&fd->fd_local_netmask;
113da14cebeSEric Cheng 	} else {
114da14cebeSEric Cheng 		mask |= FLOW_IP_REMOTE;
115da14cebeSEric Cheng 		addr = &fd->fd_remote_addr;
116da14cebeSEric Cheng 		netmask = (uchar_t *)&fd->fd_remote_netmask;
117da14cebeSEric Cheng 	}
118da14cebeSEric Cheng 
119da000602SGirish Moodalbail 	if (family == AF_INET) {
120da000602SGirish Moodalbail 		IN6_INADDR_TO_V4MAPPED(&v4addr, addr);
121da14cebeSEric Cheng 		prefix_max = IP_ABITS;
122da14cebeSEric Cheng 		fd->fd_ipversion = IPV4_VERSION;
123da14cebeSEric Cheng 		netmask = (uchar_t *)
124da14cebeSEric Cheng 		    &(V4_PART_OF_V6((*((in6_addr_t *)(void *)netmask))));
125da000602SGirish Moodalbail 	} else {
126da000602SGirish Moodalbail 		*addr = v6addr;
127da14cebeSEric Cheng 		prefix_max = IPV6_ABITS;
128da14cebeSEric Cheng 		fd->fd_ipversion = IPV6_VERSION;
129da14cebeSEric Cheng 	}
130da14cebeSEric Cheng 
131da14cebeSEric Cheng 	if (prefix_len == 0)
132da14cebeSEric Cheng 		prefix_len = prefix_max;
133da14cebeSEric Cheng 
134da14cebeSEric Cheng 	status = dladm_prefixlen2mask(prefix_len, prefix_max, netmask);
135da14cebeSEric Cheng 
136da14cebeSEric Cheng 	if (status != DLADM_STATUS_OK) {
137da14cebeSEric Cheng 		return (DLADM_STATUS_INVALID_PREFIXLEN);
138da14cebeSEric Cheng 	}
139da14cebeSEric Cheng 
140da14cebeSEric Cheng 	fd->fd_mask |= mask;
141da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
142da14cebeSEric Cheng }
143da14cebeSEric Cheng 
144da14cebeSEric Cheng dladm_status_t
145da14cebeSEric Cheng do_check_protocol(char *attr_val, flow_desc_t *fdesc)
146da14cebeSEric Cheng {
147da14cebeSEric Cheng 	uint8_t	protocol;
148da14cebeSEric Cheng 
149da14cebeSEric Cheng 	protocol = dladm_str2proto(attr_val);
150da14cebeSEric Cheng 
151da14cebeSEric Cheng 	if (protocol != 0) {
152da14cebeSEric Cheng 		fdesc->fd_mask |= FLOW_IP_PROTOCOL;
153da14cebeSEric Cheng 		fdesc->fd_protocol = protocol;
154da14cebeSEric Cheng 		return (DLADM_STATUS_OK);
155da14cebeSEric Cheng 	} else {
156da14cebeSEric Cheng 		return (DLADM_STATUS_INVALID_PROTOCOL);
157da14cebeSEric Cheng 	}
158da14cebeSEric Cheng }
159da14cebeSEric Cheng 
160da14cebeSEric Cheng dladm_status_t
161da14cebeSEric Cheng do_check_local_port(char *attr_val, flow_desc_t *fdesc)
162da14cebeSEric Cheng {
163da14cebeSEric Cheng 	return (do_check_port(attr_val, B_TRUE, fdesc));
164da14cebeSEric Cheng }
165da14cebeSEric Cheng 
166da14cebeSEric Cheng dladm_status_t
167*25ec3e3dSEric Cheng do_check_remote_port(char *attr_val, flow_desc_t *fdesc)
168*25ec3e3dSEric Cheng {
169*25ec3e3dSEric Cheng 	return (do_check_port(attr_val, B_FALSE, fdesc));
170*25ec3e3dSEric Cheng }
171*25ec3e3dSEric Cheng 
172*25ec3e3dSEric Cheng dladm_status_t
173da14cebeSEric Cheng do_check_port(char *attr_val, boolean_t local, flow_desc_t *fdesc)
174da14cebeSEric Cheng {
175da14cebeSEric Cheng 	char	*endp = NULL;
176da14cebeSEric Cheng 	long	val;
177da14cebeSEric Cheng 
178*25ec3e3dSEric Cheng 	val = strtol(attr_val, &endp, 10);
179*25ec3e3dSEric Cheng 	if (val < 1 || val > MAX_PORT || *endp != '\0')
180*25ec3e3dSEric Cheng 		return (DLADM_STATUS_INVALID_PORT);
181da14cebeSEric Cheng 	if (local) {
182da14cebeSEric Cheng 		fdesc->fd_mask |= FLOW_ULP_PORT_LOCAL;
183da14cebeSEric Cheng 		fdesc->fd_local_port = htons((uint16_t)val);
184da14cebeSEric Cheng 	} else {
185*25ec3e3dSEric Cheng 		fdesc->fd_mask |= FLOW_ULP_PORT_REMOTE;
186*25ec3e3dSEric Cheng 		fdesc->fd_remote_port = htons((uint16_t)val);
187da14cebeSEric Cheng 	}
188da14cebeSEric Cheng 
189da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
190da14cebeSEric Cheng }
191da14cebeSEric Cheng 
192da14cebeSEric Cheng /*
193da14cebeSEric Cheng  * Check for invalid and/or duplicate attribute specification
194da14cebeSEric Cheng  */
195da14cebeSEric Cheng static dladm_status_t
196da14cebeSEric Cheng flow_attrlist_check(dladm_arg_list_t *attrlist)
197da14cebeSEric Cheng {
198da14cebeSEric Cheng 	int		i, j;
199da14cebeSEric Cheng 	boolean_t	isset[DLADM_MAX_FLOWATTRS];
200da14cebeSEric Cheng 	boolean_t	matched;
201da14cebeSEric Cheng 
202da14cebeSEric Cheng 	for (j = 0; j < DLADM_MAX_FLOWATTRS; j++)
203da14cebeSEric Cheng 		isset[j] = B_FALSE;
204da14cebeSEric Cheng 
205da14cebeSEric Cheng 	for (i = 0; i < attrlist->al_count; i++) {
206da14cebeSEric Cheng 		matched = B_FALSE;
207da14cebeSEric Cheng 		for (j = 0; j < DLADM_MAX_FLOWATTRS; j++) {
208da14cebeSEric Cheng 			if (strcmp(attrlist->al_info[i].ai_name,
209da14cebeSEric Cheng 			    attr_table[j].ad_name) == 0) {
210da14cebeSEric Cheng 				if (isset[j])
211da14cebeSEric Cheng 					return (DLADM_STATUS_FLOW_INCOMPATIBLE);
212da14cebeSEric Cheng 				else
213da14cebeSEric Cheng 					isset[j] = B_TRUE;
214da14cebeSEric Cheng 				matched = B_TRUE;
215da14cebeSEric Cheng 			}
216da14cebeSEric Cheng 		}
217da14cebeSEric Cheng 		/*
218da14cebeSEric Cheng 		 * if the attribute did not match any of the attribute in
219da14cebeSEric Cheng 		 * attr_table, then it's an invalid attribute.
220da14cebeSEric Cheng 		 */
221da14cebeSEric Cheng 		if (!matched)
222da14cebeSEric Cheng 			return (DLADM_STATUS_BADARG);
223da14cebeSEric Cheng 	}
224da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
225da14cebeSEric Cheng }
226da14cebeSEric Cheng 
227da14cebeSEric Cheng /*
228da14cebeSEric Cheng  * Convert an attribute list to a flow_desc_t using the attribute ad_check()
229da14cebeSEric Cheng  * functions.
230da14cebeSEric Cheng  */
231da14cebeSEric Cheng dladm_status_t
232da14cebeSEric Cheng dladm_flow_attrlist_extract(dladm_arg_list_t *attrlist, flow_desc_t *flowdesc)
233da14cebeSEric Cheng {
234da14cebeSEric Cheng 	dladm_status_t	status = DLADM_STATUS_BADARG;
235da14cebeSEric Cheng 	int		i;
236da14cebeSEric Cheng 
237da14cebeSEric Cheng 	for (i = 0; i < attrlist->al_count; i++) {
238da14cebeSEric Cheng 		dladm_arg_info_t	*aip = &attrlist->al_info[i];
239da14cebeSEric Cheng 		int			j;
240da14cebeSEric Cheng 
241da14cebeSEric Cheng 		for (j = 0; j < DLADM_MAX_FLOWATTRS; j++) {
242da14cebeSEric Cheng 			fattr_desc_t	*adp = &attr_table[j];
243da14cebeSEric Cheng 
244da14cebeSEric Cheng 			if (strcasecmp(aip->ai_name, adp->ad_name) != 0)
245da14cebeSEric Cheng 				continue;
246da14cebeSEric Cheng 
247da14cebeSEric Cheng 			if ((aip->ai_val == NULL) || (*aip->ai_val == NULL))
248da14cebeSEric Cheng 				return (DLADM_STATUS_BADARG);
249da14cebeSEric Cheng 
250da14cebeSEric Cheng 			if (adp->ad_check != NULL)
251da14cebeSEric Cheng 				status = adp->ad_check(*aip->ai_val, flowdesc);
252da14cebeSEric Cheng 			else
253da14cebeSEric Cheng 				status = DLADM_STATUS_BADARG;
254da14cebeSEric Cheng 
255da14cebeSEric Cheng 			if (status != DLADM_STATUS_OK)
256da14cebeSEric Cheng 				return (status);
257da14cebeSEric Cheng 		}
258da14cebeSEric Cheng 	}
259da14cebeSEric Cheng 	return (status);
260da14cebeSEric Cheng }
261da14cebeSEric Cheng 
262da14cebeSEric Cheng void
263da14cebeSEric Cheng dladm_free_attrs(dladm_arg_list_t *list)
264da14cebeSEric Cheng {
265da14cebeSEric Cheng 	dladm_free_args(list);
266da14cebeSEric Cheng }
267da14cebeSEric Cheng 
268da14cebeSEric Cheng dladm_status_t
269da14cebeSEric Cheng dladm_parse_flow_attrs(char *str, dladm_arg_list_t **listp, boolean_t novalues)
270da14cebeSEric Cheng {
271da14cebeSEric Cheng 
272da14cebeSEric Cheng 	if (dladm_parse_args(str, listp, novalues)
273da14cebeSEric Cheng 	    != DLADM_STATUS_OK)
274da14cebeSEric Cheng 		return (DLADM_STATUS_ATTR_PARSE_ERR);
275da14cebeSEric Cheng 
27663a6526dSMichael Lim 	if (*listp != NULL && flow_attrlist_check(*listp)
27763a6526dSMichael Lim 	    != DLADM_STATUS_OK) {
278da14cebeSEric Cheng 		dladm_free_attrs(*listp);
279da14cebeSEric Cheng 		return (DLADM_STATUS_ATTR_PARSE_ERR);
280da14cebeSEric Cheng 	}
281da14cebeSEric Cheng 
282da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
283da14cebeSEric Cheng }
284da14cebeSEric Cheng 
285da14cebeSEric Cheng dladm_status_t
286da14cebeSEric Cheng do_check_dsfield(char *str, flow_desc_t *fd)
287da14cebeSEric Cheng {
288da14cebeSEric Cheng 	char		*mask_str, *endp = NULL;
289da14cebeSEric Cheng 	uint_t		mask = 0xff, value;
290da14cebeSEric Cheng 
291da14cebeSEric Cheng 	if ((mask_str = strchr(str, ':')) != NULL) {
292da14cebeSEric Cheng 		*mask_str++ = '\0';
293da14cebeSEric Cheng 		errno = 0;
294da14cebeSEric Cheng 		mask = strtoul(mask_str, &endp, 16);
295da14cebeSEric Cheng 		if (errno != 0 || mask == 0 || mask > 0xff ||
296da14cebeSEric Cheng 		    *endp != '\0')
297da14cebeSEric Cheng 			return (DLADM_STATUS_INVALID_DSFMASK);
298da14cebeSEric Cheng 	}
299da14cebeSEric Cheng 	errno = 0;
300da14cebeSEric Cheng 	endp = NULL;
301da14cebeSEric Cheng 	value = strtoul(str, &endp, 16);
302da14cebeSEric Cheng 	if (errno != 0 || value == 0 || value > 0xff || *endp != '\0')
303da14cebeSEric Cheng 		return (DLADM_STATUS_INVALID_DSF);
304da14cebeSEric Cheng 
305da14cebeSEric Cheng 	fd->fd_dsfield = (uint8_t)value;
306da14cebeSEric Cheng 	fd->fd_dsfield_mask = (uint8_t)mask;
307da14cebeSEric Cheng 	fd->fd_mask |= FLOW_IP_DSFIELD;
308da14cebeSEric Cheng 	return (DLADM_STATUS_OK);
309da14cebeSEric Cheng }
310da14cebeSEric Cheng 
311da14cebeSEric Cheng char *
312da14cebeSEric Cheng dladm_proto2str(uint8_t protocol)
313da14cebeSEric Cheng {
314da14cebeSEric Cheng 	if (protocol == IPPROTO_TCP)
315da14cebeSEric Cheng 		return ("tcp");
316da14cebeSEric Cheng 	if (protocol == IPPROTO_UDP)
317da14cebeSEric Cheng 		return ("udp");
318da14cebeSEric Cheng 	if (protocol == IPPROTO_SCTP)
319da14cebeSEric Cheng 		return ("sctp");
320da14cebeSEric Cheng 	if (protocol == IPPROTO_ICMPV6)
321da14cebeSEric Cheng 		return ("icmpv6");
322da14cebeSEric Cheng 	if (protocol == IPPROTO_ICMP)
323da14cebeSEric Cheng 		return ("icmp");
324da14cebeSEric Cheng 	else
325da14cebeSEric Cheng 		return ("");
326da14cebeSEric Cheng }
327da14cebeSEric Cheng 
328da14cebeSEric Cheng uint8_t
329da14cebeSEric Cheng dladm_str2proto(const char *protostr)
330da14cebeSEric Cheng {
331da14cebeSEric Cheng 	if (strncasecmp(protostr, "tcp", 3) == 0)
332da14cebeSEric Cheng 		return (IPPROTO_TCP);
333da14cebeSEric Cheng 	else if (strncasecmp(protostr, "udp", 3) == 0)
334da14cebeSEric Cheng 		return (IPPROTO_UDP);
335da14cebeSEric Cheng 	else if (strncasecmp(protostr, "sctp", 4) == 0)
336da14cebeSEric Cheng 		return (IPPROTO_SCTP);
337da14cebeSEric Cheng 	else if (strncasecmp(protostr, "icmpv6", 6) == 0)
338da14cebeSEric Cheng 		return (IPPROTO_ICMPV6);
339da14cebeSEric Cheng 	else if (strncasecmp(protostr, "icmp", 4) == 0)
340da14cebeSEric Cheng 		return (IPPROTO_ICMP);
341da14cebeSEric Cheng 
342da14cebeSEric Cheng 	return (0);
343da14cebeSEric Cheng }
344da14cebeSEric Cheng 
345da14cebeSEric Cheng void
346da14cebeSEric Cheng dladm_flow_attr_ip2str(dladm_flow_attr_t *attrp, char *buf, size_t buf_len)
347da14cebeSEric Cheng {
348da14cebeSEric Cheng 	flow_desc_t	fdesc = attrp->fa_flow_desc;
349da14cebeSEric Cheng 	struct in_addr	ipaddr;
350da14cebeSEric Cheng 	int		prefix_len, prefix_max;
351da14cebeSEric Cheng 	char		*cp, abuf[INET6_ADDRSTRLEN];
352da14cebeSEric Cheng 
353da14cebeSEric Cheng 	if (fdesc.fd_mask & FLOW_IP_LOCAL) {
354da14cebeSEric Cheng 		if (fdesc.fd_ipversion == IPV6_VERSION) {
355da14cebeSEric Cheng 			(void) inet_ntop(AF_INET6, &fdesc.fd_local_addr, abuf,
356da14cebeSEric Cheng 			    INET6_ADDRSTRLEN);
357da14cebeSEric Cheng 			cp = abuf;
358da14cebeSEric Cheng 			prefix_max = IPV6_ABITS;
359da14cebeSEric Cheng 		} else {
360da14cebeSEric Cheng 			ipaddr.s_addr = fdesc.fd_local_addr._S6_un._S6_u32[3];
361da14cebeSEric Cheng 			cp = inet_ntoa(ipaddr);
362da14cebeSEric Cheng 			prefix_max = IP_ABITS;
363da14cebeSEric Cheng 		}
364da14cebeSEric Cheng 		(void) dladm_mask2prefixlen(&fdesc.fd_local_netmask,
365da14cebeSEric Cheng 		    prefix_max, &prefix_len);
366da14cebeSEric Cheng 		(void) snprintf(buf, buf_len, "LCL:%s/%d  ", cp, prefix_len);
367da14cebeSEric Cheng 	} else if (fdesc.fd_mask & FLOW_IP_REMOTE) {
368da14cebeSEric Cheng 		if (fdesc.fd_ipversion == IPV6_VERSION) {
369da14cebeSEric Cheng 			(void) inet_ntop(AF_INET6, &fdesc.fd_remote_addr, abuf,
370da14cebeSEric Cheng 			    INET6_ADDRSTRLEN);
371da14cebeSEric Cheng 			cp = abuf;
372da14cebeSEric Cheng 			prefix_max = IPV6_ABITS;
373da14cebeSEric Cheng 		} else {
374da14cebeSEric Cheng 			ipaddr.s_addr = fdesc.fd_remote_addr._S6_un._S6_u32[3];
375da14cebeSEric Cheng 			cp = inet_ntoa(ipaddr);
376da14cebeSEric Cheng 			prefix_max = IP_ABITS;
377da14cebeSEric Cheng 		}
378da14cebeSEric Cheng 		(void) dladm_mask2prefixlen(&fdesc.fd_remote_netmask,
379da14cebeSEric Cheng 		    prefix_max, &prefix_len);
380da14cebeSEric Cheng 		(void) snprintf(buf, buf_len, "RMT:%s/%d  ", cp, prefix_len);
381da14cebeSEric Cheng 	} else {
382da14cebeSEric Cheng 		buf[0] = '\0';
383da14cebeSEric Cheng 	}
384da14cebeSEric Cheng }
385da14cebeSEric Cheng 
386da14cebeSEric Cheng void
387da14cebeSEric Cheng dladm_flow_attr_proto2str(dladm_flow_attr_t *attrp, char *buf, size_t buf_len)
388da14cebeSEric Cheng {
389da14cebeSEric Cheng 	flow_desc_t	fdesc = attrp->fa_flow_desc;
390da14cebeSEric Cheng 
391da14cebeSEric Cheng 	(void) snprintf(buf, buf_len, "%s",
392da14cebeSEric Cheng 	    dladm_proto2str(fdesc.fd_protocol));
393da14cebeSEric Cheng }
394da14cebeSEric Cheng 
395da14cebeSEric Cheng void
396da14cebeSEric Cheng dladm_flow_attr_port2str(dladm_flow_attr_t *attrp, char *buf, size_t buf_len)
397da14cebeSEric Cheng {
398da14cebeSEric Cheng 	flow_desc_t	fdesc = attrp->fa_flow_desc;
399da14cebeSEric Cheng 
400da14cebeSEric Cheng 	if (fdesc.fd_mask & FLOW_ULP_PORT_LOCAL) {
401da14cebeSEric Cheng 		(void) snprintf(buf, buf_len, "%d",
402da14cebeSEric Cheng 		    ntohs(fdesc.fd_local_port));
403*25ec3e3dSEric Cheng 	} else if (fdesc.fd_mask & FLOW_ULP_PORT_REMOTE) {
404*25ec3e3dSEric Cheng 		(void) snprintf(buf, buf_len, "%d",
405*25ec3e3dSEric Cheng 		    ntohs(fdesc.fd_remote_port));
406da14cebeSEric Cheng 	} else {
407da14cebeSEric Cheng 		buf[0] = '\0';
408da14cebeSEric Cheng 	}
409da14cebeSEric Cheng }
410da14cebeSEric Cheng 
411da14cebeSEric Cheng void
412da14cebeSEric Cheng dladm_flow_attr_dsfield2str(dladm_flow_attr_t *attrp, char *buf, size_t buf_len)
413da14cebeSEric Cheng {
414da14cebeSEric Cheng 	flow_desc_t	fdesc = attrp->fa_flow_desc;
415da14cebeSEric Cheng 
416da14cebeSEric Cheng 	if (fdesc.fd_mask & FLOW_IP_DSFIELD) {
417da14cebeSEric Cheng 		(void) snprintf(buf, buf_len, "0x%x:0x%x",
418da14cebeSEric Cheng 		    fdesc.fd_dsfield, fdesc.fd_dsfield_mask);
419da14cebeSEric Cheng 	} else {
420da14cebeSEric Cheng 		buf[0] = '\0';
421da14cebeSEric Cheng 	}
422da14cebeSEric Cheng }
423