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