xref: /illumos-gate/usr/src/lib/libdladm/common/flowattr.c (revision d656abb5804319b33c85955a73ee450ef7ff9739)
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 (flow_attrlist_check(*listp) != DLADM_STATUS_OK) {
268 		dladm_free_attrs(*listp);
269 		return (DLADM_STATUS_ATTR_PARSE_ERR);
270 	}
271 
272 	return (DLADM_STATUS_OK);
273 }
274 
275 dladm_status_t
276 do_check_dsfield(char *str, flow_desc_t *fd)
277 {
278 	char		*mask_str, *endp = NULL;
279 	uint_t		mask = 0xff, value;
280 
281 	if ((mask_str = strchr(str, ':')) != NULL) {
282 		*mask_str++ = '\0';
283 		errno = 0;
284 		mask = strtoul(mask_str, &endp, 16);
285 		if (errno != 0 || mask == 0 || mask > 0xff ||
286 		    *endp != '\0')
287 			return (DLADM_STATUS_INVALID_DSFMASK);
288 	}
289 	errno = 0;
290 	endp = NULL;
291 	value = strtoul(str, &endp, 16);
292 	if (errno != 0 || value == 0 || value > 0xff || *endp != '\0')
293 		return (DLADM_STATUS_INVALID_DSF);
294 
295 	fd->fd_dsfield = (uint8_t)value;
296 	fd->fd_dsfield_mask = (uint8_t)mask;
297 	fd->fd_mask |= FLOW_IP_DSFIELD;
298 	return (DLADM_STATUS_OK);
299 }
300 
301 char *
302 dladm_proto2str(uint8_t protocol)
303 {
304 	if (protocol == IPPROTO_TCP)
305 		return ("tcp");
306 	if (protocol == IPPROTO_UDP)
307 		return ("udp");
308 	if (protocol == IPPROTO_SCTP)
309 		return ("sctp");
310 	if (protocol == IPPROTO_ICMPV6)
311 		return ("icmpv6");
312 	if (protocol == IPPROTO_ICMP)
313 		return ("icmp");
314 	else
315 		return ("");
316 }
317 
318 uint8_t
319 dladm_str2proto(const char *protostr)
320 {
321 	if (strncasecmp(protostr, "tcp", 3) == 0)
322 		return (IPPROTO_TCP);
323 	else if (strncasecmp(protostr, "udp", 3) == 0)
324 		return (IPPROTO_UDP);
325 	else if (strncasecmp(protostr, "sctp", 4) == 0)
326 		return (IPPROTO_SCTP);
327 	else if (strncasecmp(protostr, "icmpv6", 6) == 0)
328 		return (IPPROTO_ICMPV6);
329 	else if (strncasecmp(protostr, "icmp", 4) == 0)
330 		return (IPPROTO_ICMP);
331 
332 	return (0);
333 }
334 
335 void
336 dladm_flow_attr_ip2str(dladm_flow_attr_t *attrp, char *buf, size_t buf_len)
337 {
338 	flow_desc_t	fdesc = attrp->fa_flow_desc;
339 	struct in_addr	ipaddr;
340 	int		prefix_len, prefix_max;
341 	char		*cp, abuf[INET6_ADDRSTRLEN];
342 
343 	if (fdesc.fd_mask & FLOW_IP_LOCAL) {
344 		if (fdesc.fd_ipversion == IPV6_VERSION) {
345 			(void) inet_ntop(AF_INET6, &fdesc.fd_local_addr, abuf,
346 			    INET6_ADDRSTRLEN);
347 			cp = abuf;
348 			prefix_max = IPV6_ABITS;
349 		} else {
350 			ipaddr.s_addr = fdesc.fd_local_addr._S6_un._S6_u32[3];
351 			cp = inet_ntoa(ipaddr);
352 			prefix_max = IP_ABITS;
353 		}
354 		(void) dladm_mask2prefixlen(&fdesc.fd_local_netmask,
355 		    prefix_max, &prefix_len);
356 		(void) snprintf(buf, buf_len, "LCL:%s/%d  ", cp, prefix_len);
357 	} else if (fdesc.fd_mask & FLOW_IP_REMOTE) {
358 		if (fdesc.fd_ipversion == IPV6_VERSION) {
359 			(void) inet_ntop(AF_INET6, &fdesc.fd_remote_addr, abuf,
360 			    INET6_ADDRSTRLEN);
361 			cp = abuf;
362 			prefix_max = IPV6_ABITS;
363 		} else {
364 			ipaddr.s_addr = fdesc.fd_remote_addr._S6_un._S6_u32[3];
365 			cp = inet_ntoa(ipaddr);
366 			prefix_max = IP_ABITS;
367 		}
368 		(void) dladm_mask2prefixlen(&fdesc.fd_remote_netmask,
369 		    prefix_max, &prefix_len);
370 		(void) snprintf(buf, buf_len, "RMT:%s/%d  ", cp, prefix_len);
371 	} else {
372 		buf[0] = '\0';
373 	}
374 }
375 
376 void
377 dladm_flow_attr_proto2str(dladm_flow_attr_t *attrp, char *buf, size_t buf_len)
378 {
379 	flow_desc_t	fdesc = attrp->fa_flow_desc;
380 
381 	(void) snprintf(buf, buf_len, "%s",
382 	    dladm_proto2str(fdesc.fd_protocol));
383 }
384 
385 void
386 dladm_flow_attr_port2str(dladm_flow_attr_t *attrp, char *buf, size_t buf_len)
387 {
388 	flow_desc_t	fdesc = attrp->fa_flow_desc;
389 
390 	if (fdesc.fd_mask & FLOW_ULP_PORT_LOCAL) {
391 		(void) snprintf(buf, buf_len, "%d",
392 		    ntohs(fdesc.fd_local_port));
393 	} else {
394 		buf[0] = '\0';
395 	}
396 }
397 
398 void
399 dladm_flow_attr_dsfield2str(dladm_flow_attr_t *attrp, char *buf, size_t buf_len)
400 {
401 	flow_desc_t	fdesc = attrp->fa_flow_desc;
402 
403 	if (fdesc.fd_mask & FLOW_IP_DSFIELD) {
404 		(void) snprintf(buf, buf_len, "0x%x:0x%x",
405 		    fdesc.fd_dsfield, fdesc.fd_dsfield_mask);
406 	} else {
407 		buf[0] = '\0';
408 	}
409 }
410