]>
Commit | Line | Data |
---|---|---|
30eb304e JP |
1 | /* |
2 | * f_flower.c Flower Classifier | |
3 | * | |
4 | * This program is free software; you can distribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Jiri Pirko <jiri@resnulli.us> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <unistd.h> | |
30eb304e JP |
15 | #include <string.h> |
16 | #include <net/if.h> | |
28747146 | 17 | #include <linux/limits.h> |
f888f4e2 | 18 | #include <linux/if_arp.h> |
30eb304e JP |
19 | #include <linux/if_ether.h> |
20 | #include <linux/ip.h> | |
745d9172 | 21 | #include <linux/tc_act/tc_vlan.h> |
7638ee13 | 22 | #include <linux/mpls.h> |
30eb304e JP |
23 | |
24 | #include "utils.h" | |
25 | #include "tc_util.h" | |
26 | #include "rt_names.h" | |
27 | ||
08f66c80 PB |
28 | enum flower_matching_flags { |
29 | FLOWER_IP_FLAGS, | |
30 | }; | |
31 | ||
6910d656 SH |
32 | enum flower_endpoint { |
33 | FLOWER_ENDPOINT_SRC, | |
34 | FLOWER_ENDPOINT_DST | |
35 | }; | |
36 | ||
eb3b5696 SH |
37 | enum flower_icmp_field { |
38 | FLOWER_ICMP_FIELD_TYPE, | |
39 | FLOWER_ICMP_FIELD_CODE | |
40 | }; | |
41 | ||
30eb304e JP |
42 | static void explain(void) |
43 | { | |
512caeb2 | 44 | fprintf(stderr, |
8589eb4e MC |
45 | "Usage: ... flower [ MATCH-LIST ] [ verbose ]\n" |
46 | " [ skip_sw | skip_hw ]\n" | |
47 | " [ action ACTION-SPEC ] [ classid CLASSID ]\n" | |
512caeb2 SH |
48 | "\n" |
49 | "Where: MATCH-LIST := [ MATCH-LIST ] MATCH\n" | |
8589eb4e MC |
50 | " MATCH := { indev DEV-NAME |\n" |
51 | " vlan_id VID |\n" | |
52 | " vlan_prio PRIORITY |\n" | |
53 | " vlan_ethtype [ ipv4 | ipv6 | ETH-TYPE ] |\n" | |
54 | " cvlan_id VID |\n" | |
55 | " cvlan_prio PRIORITY |\n" | |
56 | " cvlan_ethtype [ ipv4 | ipv6 | ETH-TYPE ] |\n" | |
57 | " dst_mac MASKED-LLADDR |\n" | |
58 | " src_mac MASKED-LLADDR |\n" | |
59 | " ip_proto [tcp | udp | sctp | icmp | icmpv6 | IP-PROTO ] |\n" | |
60 | " ip_tos MASKED-IP_TOS |\n" | |
61 | " ip_ttl MASKED-IP_TTL |\n" | |
eb09a15c | 62 | " mpls LSE-LIST |\n" |
8589eb4e MC |
63 | " mpls_label LABEL |\n" |
64 | " mpls_tc TC |\n" | |
65 | " mpls_bos BOS |\n" | |
66 | " mpls_ttl TTL |\n" | |
67 | " dst_ip PREFIX |\n" | |
68 | " src_ip PREFIX |\n" | |
69 | " dst_port PORT-NUMBER |\n" | |
70 | " src_port PORT-NUMBER |\n" | |
71 | " tcp_flags MASKED-TCP_FLAGS |\n" | |
72 | " type MASKED-ICMP-TYPE |\n" | |
73 | " code MASKED-ICMP-CODE |\n" | |
74 | " arp_tip IPV4-PREFIX |\n" | |
75 | " arp_sip IPV4-PREFIX |\n" | |
76 | " arp_op [ request | reply | OP ] |\n" | |
77 | " arp_tha MASKED-LLADDR |\n" | |
78 | " arp_sha MASKED-LLADDR |\n" | |
79 | " enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n" | |
80 | " enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n" | |
81 | " enc_key_id [ KEY-ID ] |\n" | |
82 | " enc_tos MASKED-IP_TOS |\n" | |
83 | " enc_ttl MASKED-IP_TTL |\n" | |
84 | " geneve_opts MASKED-OPTIONS |\n" | |
93c8d5f7 | 85 | " vxlan_opts MASKED-OPTIONS |\n" |
4e578c78 | 86 | " erspan_opts MASKED-OPTIONS |\n" |
8589eb4e | 87 | " ip_flags IP-FLAGS | \n" |
2fffb1c0 PB |
88 | " enc_dst_port [ port_number ] |\n" |
89 | " ct_state MASKED_CT_STATE |\n" | |
90 | " ct_label MASKED_CT_LABEL |\n" | |
91 | " ct_mark MASKED_CT_MARK |\n" | |
92 | " ct_zone MASKED_CT_ZONE }\n" | |
eb09a15c GN |
93 | " LSE-LIST := [ LSE-LIST ] LSE\n" |
94 | " LSE := lse depth DEPTH { label LABEL | tc TC | bos BOS | ttl TTL }\n" | |
8589eb4e MC |
95 | " FILTERID := X:Y:Z\n" |
96 | " MASKED_LLADDR := { LLADDR | LLADDR/MASK | LLADDR/BITS }\n" | |
2fffb1c0 | 97 | " MASKED_CT_STATE := combination of {+|-} and flags trk,est,new\n" |
8589eb4e | 98 | " ACTION-SPEC := ... look at individual actions\n" |
512caeb2 | 99 | "\n" |
8589eb4e MC |
100 | "NOTE: CLASSID, IP-PROTO are parsed as hexadecimal input.\n" |
101 | "NOTE: There can be only used one mask per one prio. If user needs\n" | |
102 | " to specify different mask, he has to use different prio.\n"); | |
30eb304e JP |
103 | } |
104 | ||
105 | static int flower_parse_eth_addr(char *str, int addr_type, int mask_type, | |
106 | struct nlmsghdr *n) | |
107 | { | |
c2078f8d SH |
108 | int ret, err = -1; |
109 | char addr[ETH_ALEN], *slash; | |
110 | ||
111 | slash = strchr(str, '/'); | |
112 | if (slash) | |
113 | *slash = '\0'; | |
30eb304e JP |
114 | |
115 | ret = ll_addr_a2n(addr, sizeof(addr), str); | |
116 | if (ret < 0) | |
c2078f8d | 117 | goto err; |
30eb304e | 118 | addattr_l(n, MAX_MSG, addr_type, addr, sizeof(addr)); |
c2078f8d SH |
119 | |
120 | if (slash) { | |
121 | unsigned bits; | |
122 | ||
123 | if (!get_unsigned(&bits, slash + 1, 10)) { | |
124 | uint64_t mask; | |
125 | ||
126 | /* Extra 16 bit shift to push mac address into | |
127 | * high bits of uint64_t | |
128 | */ | |
129 | mask = htonll(0xffffffffffffULL << (16 + 48 - bits)); | |
130 | memcpy(addr, &mask, ETH_ALEN); | |
131 | } else { | |
132 | ret = ll_addr_a2n(addr, sizeof(addr), slash + 1); | |
133 | if (ret < 0) | |
134 | goto err; | |
135 | } | |
136 | } else { | |
137 | memset(addr, 0xff, ETH_ALEN); | |
138 | } | |
30eb304e | 139 | addattr_l(n, MAX_MSG, mask_type, addr, sizeof(addr)); |
c2078f8d SH |
140 | |
141 | err = 0; | |
142 | err: | |
143 | if (slash) | |
144 | *slash = '/'; | |
145 | return err; | |
30eb304e JP |
146 | } |
147 | ||
1f0a5dfd JL |
148 | static bool eth_type_vlan(__be16 ethertype) |
149 | { | |
150 | return ethertype == htons(ETH_P_8021Q) || | |
151 | ethertype == htons(ETH_P_8021AD); | |
152 | } | |
153 | ||
745d9172 | 154 | static int flower_parse_vlan_eth_type(char *str, __be16 eth_type, int type, |
512caeb2 SH |
155 | __be16 *p_vlan_eth_type, |
156 | struct nlmsghdr *n) | |
745d9172 HHZ |
157 | { |
158 | __be16 vlan_eth_type; | |
159 | ||
1f0a5dfd JL |
160 | if (!eth_type_vlan(eth_type)) { |
161 | fprintf(stderr, "Can't set \"%s\" if ethertype isn't 802.1Q or 802.1AD\n", | |
162 | type == TCA_FLOWER_KEY_VLAN_ETH_TYPE ? "vlan_ethtype" : "cvlan_ethtype"); | |
745d9172 HHZ |
163 | return -1; |
164 | } | |
165 | ||
166 | if (ll_proto_a2n(&vlan_eth_type, str)) | |
167 | invarg("invalid vlan_ethtype", str); | |
168 | addattr16(n, MAX_MSG, type, vlan_eth_type); | |
169 | *p_vlan_eth_type = vlan_eth_type; | |
170 | return 0; | |
171 | } | |
172 | ||
08f66c80 PB |
173 | struct flag_to_string { |
174 | int flag; | |
175 | enum flower_matching_flags type; | |
176 | char *string; | |
177 | }; | |
22a8f019 | 178 | |
08f66c80 PB |
179 | static struct flag_to_string flags_str[] = { |
180 | { TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOWER_IP_FLAGS, "frag" }, | |
fb4e6abf | 181 | { TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, FLOWER_IP_FLAGS, "firstfrag" }, |
08f66c80 | 182 | }; |
22a8f019 | 183 | |
08f66c80 PB |
184 | static int flower_parse_matching_flags(char *str, |
185 | enum flower_matching_flags type, | |
186 | __u32 *mtf, __u32 *mtf_mask) | |
187 | { | |
188 | char *token; | |
189 | bool no; | |
190 | bool found; | |
191 | int i; | |
22a8f019 | 192 | |
08f66c80 PB |
193 | token = strtok(str, "/"); |
194 | ||
195 | while (token) { | |
196 | if (!strncmp(token, "no", 2)) { | |
197 | no = true; | |
198 | token += 2; | |
199 | } else | |
200 | no = false; | |
201 | ||
202 | found = false; | |
203 | for (i = 0; i < ARRAY_SIZE(flags_str); i++) { | |
204 | if (type != flags_str[i].type) | |
205 | continue; | |
206 | ||
207 | if (!strcmp(token, flags_str[i].string)) { | |
208 | if (no) | |
209 | *mtf &= ~flags_str[i].flag; | |
210 | else | |
211 | *mtf |= flags_str[i].flag; | |
212 | ||
213 | *mtf_mask |= flags_str[i].flag; | |
214 | found = true; | |
215 | break; | |
216 | } | |
217 | } | |
218 | if (!found) | |
22a8f019 | 219 | return -1; |
08f66c80 PB |
220 | |
221 | token = strtok(NULL, "/"); | |
22a8f019 PB |
222 | } |
223 | ||
22a8f019 PB |
224 | return 0; |
225 | } | |
226 | ||
2fffb1c0 | 227 | static int flower_parse_u16(char *str, int value_type, int mask_type, |
b20dcd0b | 228 | struct nlmsghdr *n, bool be) |
2fffb1c0 PB |
229 | { |
230 | __u16 value, mask; | |
231 | char *slash; | |
232 | ||
233 | slash = strchr(str, '/'); | |
234 | if (slash) | |
235 | *slash = '\0'; | |
236 | ||
237 | if (get_u16(&value, str, 0)) | |
238 | return -1; | |
239 | ||
240 | if (slash) { | |
241 | if (get_u16(&mask, slash + 1, 0)) | |
242 | return -1; | |
243 | } else { | |
244 | mask = UINT16_MAX; | |
245 | } | |
246 | ||
b20dcd0b EB |
247 | if (be) { |
248 | value = htons(value); | |
249 | mask = htons(mask); | |
250 | } | |
2fffb1c0 PB |
251 | addattr16(n, MAX_MSG, value_type, value); |
252 | addattr16(n, MAX_MSG, mask_type, mask); | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
257 | static int flower_parse_u32(char *str, int value_type, int mask_type, | |
258 | struct nlmsghdr *n) | |
259 | { | |
260 | __u32 value, mask; | |
261 | char *slash; | |
262 | ||
263 | slash = strchr(str, '/'); | |
264 | if (slash) | |
265 | *slash = '\0'; | |
266 | ||
267 | if (get_u32(&value, str, 0)) | |
268 | return -1; | |
269 | ||
270 | if (slash) { | |
271 | if (get_u32(&mask, slash + 1, 0)) | |
272 | return -1; | |
273 | } else { | |
274 | mask = UINT32_MAX; | |
275 | } | |
276 | ||
277 | addattr32(n, MAX_MSG, value_type, value); | |
278 | addattr32(n, MAX_MSG, mask_type, mask); | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
283 | static int flower_parse_ct_mark(char *str, struct nlmsghdr *n) | |
284 | { | |
285 | return flower_parse_u32(str, | |
286 | TCA_FLOWER_KEY_CT_MARK, | |
287 | TCA_FLOWER_KEY_CT_MARK_MASK, | |
288 | n); | |
289 | } | |
290 | ||
291 | static int flower_parse_ct_zone(char *str, struct nlmsghdr *n) | |
292 | { | |
293 | return flower_parse_u16(str, | |
294 | TCA_FLOWER_KEY_CT_ZONE, | |
295 | TCA_FLOWER_KEY_CT_ZONE_MASK, | |
b20dcd0b EB |
296 | n, |
297 | false); | |
2fffb1c0 PB |
298 | } |
299 | ||
300 | static int flower_parse_ct_labels(char *str, struct nlmsghdr *n) | |
301 | { | |
302 | #define LABELS_SIZE 16 | |
303 | uint8_t labels[LABELS_SIZE], lmask[LABELS_SIZE]; | |
304 | char *slash, *mask = NULL; | |
305 | size_t slen, slen_mask = 0; | |
306 | ||
307 | slash = index(str, '/'); | |
308 | if (slash) { | |
309 | *slash = 0; | |
310 | mask = slash + 1; | |
311 | slen_mask = strlen(mask); | |
312 | } | |
313 | ||
314 | slen = strlen(str); | |
315 | if (slen > LABELS_SIZE * 2 || slen_mask > LABELS_SIZE * 2) { | |
316 | char errmsg[128]; | |
317 | ||
318 | snprintf(errmsg, sizeof(errmsg), | |
319 | "%zd Max allowed size %d", | |
320 | slen, LABELS_SIZE*2); | |
321 | invarg(errmsg, str); | |
322 | } | |
323 | ||
324 | if (hex2mem(str, labels, slen / 2) < 0) | |
325 | invarg("labels must be a hex string\n", str); | |
326 | addattr_l(n, MAX_MSG, TCA_FLOWER_KEY_CT_LABELS, labels, slen / 2); | |
327 | ||
328 | if (mask) { | |
329 | if (hex2mem(mask, lmask, slen_mask / 2) < 0) | |
330 | invarg("labels mask must be a hex string\n", mask); | |
331 | } else { | |
332 | memset(lmask, 0xff, sizeof(lmask)); | |
333 | slen_mask = sizeof(lmask) * 2; | |
334 | } | |
335 | addattr_l(n, MAX_MSG, TCA_FLOWER_KEY_CT_LABELS_MASK, lmask, | |
336 | slen_mask / 2); | |
337 | ||
338 | return 0; | |
339 | } | |
340 | ||
341 | static struct flower_ct_states { | |
342 | char *str; | |
343 | int flag; | |
344 | } flower_ct_states[] = { | |
345 | { "trk", TCA_FLOWER_KEY_CT_FLAGS_TRACKED }, | |
346 | { "new", TCA_FLOWER_KEY_CT_FLAGS_NEW }, | |
347 | { "est", TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED }, | |
c94fd71b | 348 | { "inv", TCA_FLOWER_KEY_CT_FLAGS_INVALID }, |
2fffb1c0 PB |
349 | }; |
350 | ||
351 | static int flower_parse_ct_state(char *str, struct nlmsghdr *n) | |
352 | { | |
353 | int flags = 0, mask = 0, len, i; | |
354 | bool p; | |
355 | ||
356 | while (*str != '\0') { | |
357 | if (*str == '+') | |
358 | p = true; | |
359 | else if (*str == '-') | |
360 | p = false; | |
361 | else | |
362 | return -1; | |
363 | ||
364 | for (i = 0; i < ARRAY_SIZE(flower_ct_states); i++) { | |
365 | len = strlen(flower_ct_states[i].str); | |
366 | if (strncmp(str + 1, flower_ct_states[i].str, len)) | |
367 | continue; | |
368 | ||
369 | if (p) | |
370 | flags |= flower_ct_states[i].flag; | |
371 | mask |= flower_ct_states[i].flag; | |
372 | break; | |
373 | } | |
374 | ||
375 | if (i == ARRAY_SIZE(flower_ct_states)) | |
376 | return -1; | |
377 | ||
378 | str += len + 1; | |
379 | } | |
380 | ||
381 | addattr16(n, MAX_MSG, TCA_FLOWER_KEY_CT_STATE, flags); | |
382 | addattr16(n, MAX_MSG, TCA_FLOWER_KEY_CT_STATE_MASK, mask); | |
383 | return 0; | |
384 | } | |
385 | ||
30eb304e JP |
386 | static int flower_parse_ip_proto(char *str, __be16 eth_type, int type, |
387 | __u8 *p_ip_proto, struct nlmsghdr *n) | |
388 | { | |
389 | int ret; | |
390 | __u8 ip_proto; | |
391 | ||
eb3b5696 SH |
392 | if (eth_type != htons(ETH_P_IP) && eth_type != htons(ETH_P_IPV6)) |
393 | goto err; | |
394 | ||
30eb304e JP |
395 | if (matches(str, "tcp") == 0) { |
396 | ip_proto = IPPROTO_TCP; | |
397 | } else if (matches(str, "udp") == 0) { | |
398 | ip_proto = IPPROTO_UDP; | |
a1fb0d48 SH |
399 | } else if (matches(str, "sctp") == 0) { |
400 | ip_proto = IPPROTO_SCTP; | |
eb3b5696 SH |
401 | } else if (matches(str, "icmp") == 0) { |
402 | if (eth_type != htons(ETH_P_IP)) | |
403 | goto err; | |
404 | ip_proto = IPPROTO_ICMP; | |
405 | } else if (matches(str, "icmpv6") == 0) { | |
406 | if (eth_type != htons(ETH_P_IPV6)) | |
407 | goto err; | |
408 | ip_proto = IPPROTO_ICMPV6; | |
30eb304e JP |
409 | } else { |
410 | ret = get_u8(&ip_proto, str, 16); | |
411 | if (ret) | |
412 | return -1; | |
413 | } | |
414 | addattr8(n, MAX_MSG, type, ip_proto); | |
415 | *p_ip_proto = ip_proto; | |
416 | return 0; | |
eb3b5696 SH |
417 | |
418 | err: | |
419 | fprintf(stderr, "Illegal \"eth_type\" for ip proto\n"); | |
420 | return -1; | |
30eb304e JP |
421 | } |
422 | ||
f888f4e2 SH |
423 | static int __flower_parse_ip_addr(char *str, int family, |
424 | int addr4_type, int mask4_type, | |
425 | int addr6_type, int mask6_type, | |
426 | struct nlmsghdr *n) | |
30eb304e JP |
427 | { |
428 | int ret; | |
429 | inet_prefix addr; | |
30eb304e JP |
430 | int bits; |
431 | int i; | |
432 | ||
30eb304e JP |
433 | ret = get_prefix(&addr, str, family); |
434 | if (ret) | |
435 | return -1; | |
436 | ||
bb9b63b1 AV |
437 | if (family && (addr.family != family)) { |
438 | fprintf(stderr, "Illegal \"eth_type\" for ip address\n"); | |
30eb304e | 439 | return -1; |
bb9b63b1 | 440 | } |
30eb304e JP |
441 | |
442 | addattr_l(n, MAX_MSG, addr.family == AF_INET ? addr4_type : addr6_type, | |
443 | addr.data, addr.bytelen); | |
444 | ||
445 | memset(addr.data, 0xff, addr.bytelen); | |
446 | bits = addr.bitlen; | |
447 | for (i = 0; i < addr.bytelen / 4; i++) { | |
448 | if (!bits) { | |
449 | addr.data[i] = 0; | |
450 | } else if (bits / 32 >= 1) { | |
451 | bits -= 32; | |
452 | } else { | |
453 | addr.data[i] <<= 32 - bits; | |
454 | addr.data[i] = htonl(addr.data[i]); | |
455 | bits = 0; | |
456 | } | |
457 | } | |
458 | ||
459 | addattr_l(n, MAX_MSG, addr.family == AF_INET ? mask4_type : mask6_type, | |
460 | addr.data, addr.bytelen); | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
f888f4e2 SH |
465 | static int flower_parse_ip_addr(char *str, __be16 eth_type, |
466 | int addr4_type, int mask4_type, | |
467 | int addr6_type, int mask6_type, | |
468 | struct nlmsghdr *n) | |
469 | { | |
470 | int family; | |
471 | ||
472 | if (eth_type == htons(ETH_P_IP)) { | |
473 | family = AF_INET; | |
474 | } else if (eth_type == htons(ETH_P_IPV6)) { | |
475 | family = AF_INET6; | |
476 | } else if (!eth_type) { | |
477 | family = AF_UNSPEC; | |
478 | } else { | |
479 | return -1; | |
480 | } | |
481 | ||
164a9ff4 RD |
482 | return __flower_parse_ip_addr(str, family, addr4_type, mask4_type, |
483 | addr6_type, mask6_type, n); | |
f888f4e2 SH |
484 | } |
485 | ||
486 | static bool flower_eth_type_arp(__be16 eth_type) | |
487 | { | |
488 | return eth_type == htons(ETH_P_ARP) || eth_type == htons(ETH_P_RARP); | |
489 | } | |
490 | ||
491 | static int flower_parse_arp_ip_addr(char *str, __be16 eth_type, | |
492 | int addr_type, int mask_type, | |
493 | struct nlmsghdr *n) | |
494 | { | |
495 | if (!flower_eth_type_arp(eth_type)) | |
496 | return -1; | |
497 | ||
498 | return __flower_parse_ip_addr(str, AF_INET, addr_type, mask_type, | |
499 | TCA_FLOWER_UNSPEC, TCA_FLOWER_UNSPEC, n); | |
500 | } | |
501 | ||
180136e5 SH |
502 | static int flower_parse_u8(char *str, int value_type, int mask_type, |
503 | int (*value_from_name)(const char *str, | |
504 | __u8 *value), | |
505 | bool (*value_validate)(__u8 value), | |
506 | struct nlmsghdr *n) | |
f888f4e2 SH |
507 | { |
508 | char *slash; | |
509 | int ret, err = -1; | |
180136e5 | 510 | __u8 value, mask; |
f888f4e2 SH |
511 | |
512 | slash = strchr(str, '/'); | |
513 | if (slash) | |
514 | *slash = '\0'; | |
515 | ||
180136e5 SH |
516 | ret = value_from_name ? value_from_name(str, &value) : -1; |
517 | if (ret < 0) { | |
f888f4e2 SH |
518 | ret = get_u8(&value, str, 10); |
519 | if (ret) | |
520 | goto err; | |
f888f4e2 SH |
521 | } |
522 | ||
180136e5 SH |
523 | if (value_validate && !value_validate(value)) |
524 | goto err; | |
525 | ||
f888f4e2 SH |
526 | if (slash) { |
527 | ret = get_u8(&mask, slash + 1, 10); | |
528 | if (ret) | |
529 | goto err; | |
530 | } | |
531 | else { | |
532 | mask = UINT8_MAX; | |
533 | } | |
534 | ||
180136e5 | 535 | addattr8(n, MAX_MSG, value_type, value); |
f888f4e2 SH |
536 | addattr8(n, MAX_MSG, mask_type, mask); |
537 | ||
538 | err = 0; | |
539 | err: | |
540 | if (slash) | |
541 | *slash = '/'; | |
542 | return err; | |
543 | } | |
544 | ||
9d36e54f SH |
545 | static const char *flower_print_arp_op_to_name(__u8 op) |
546 | { | |
547 | switch (op) { | |
548 | case ARPOP_REQUEST: | |
549 | return "request"; | |
550 | case ARPOP_REPLY: | |
551 | return "reply"; | |
552 | default: | |
553 | return NULL; | |
554 | } | |
555 | } | |
556 | ||
180136e5 SH |
557 | static int flower_arp_op_from_name(const char *name, __u8 *op) |
558 | { | |
559 | if (!strcmp(name, "request")) | |
560 | *op = ARPOP_REQUEST; | |
561 | else if (!strcmp(name, "reply")) | |
562 | *op = ARPOP_REPLY; | |
563 | else | |
564 | return -1; | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
569 | static bool flow_arp_op_validate(__u8 op) | |
570 | { | |
571 | return !op || op == ARPOP_REQUEST || op == ARPOP_REPLY; | |
572 | } | |
573 | ||
574 | static int flower_parse_arp_op(char *str, __be16 eth_type, | |
575 | int op_type, int mask_type, | |
576 | struct nlmsghdr *n) | |
577 | { | |
578 | if (!flower_eth_type_arp(eth_type)) | |
579 | return -1; | |
580 | ||
581 | return flower_parse_u8(str, op_type, mask_type, flower_arp_op_from_name, | |
582 | flow_arp_op_validate, n); | |
583 | } | |
584 | ||
eb3b5696 SH |
585 | static int flower_icmp_attr_type(__be16 eth_type, __u8 ip_proto, |
586 | enum flower_icmp_field field) | |
587 | { | |
588 | if (eth_type == htons(ETH_P_IP) && ip_proto == IPPROTO_ICMP) | |
589 | return field == FLOWER_ICMP_FIELD_CODE ? | |
590 | TCA_FLOWER_KEY_ICMPV4_CODE : | |
591 | TCA_FLOWER_KEY_ICMPV4_TYPE; | |
592 | else if (eth_type == htons(ETH_P_IPV6) && ip_proto == IPPROTO_ICMPV6) | |
593 | return field == FLOWER_ICMP_FIELD_CODE ? | |
594 | TCA_FLOWER_KEY_ICMPV6_CODE : | |
595 | TCA_FLOWER_KEY_ICMPV6_TYPE; | |
596 | ||
597 | return -1; | |
598 | } | |
599 | ||
6374961a SH |
600 | static int flower_icmp_attr_mask_type(__be16 eth_type, __u8 ip_proto, |
601 | enum flower_icmp_field field) | |
602 | { | |
603 | if (eth_type == htons(ETH_P_IP) && ip_proto == IPPROTO_ICMP) | |
604 | return field == FLOWER_ICMP_FIELD_CODE ? | |
605 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK : | |
606 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK; | |
607 | else if (eth_type == htons(ETH_P_IPV6) && ip_proto == IPPROTO_ICMPV6) | |
608 | return field == FLOWER_ICMP_FIELD_CODE ? | |
609 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK : | |
610 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK; | |
611 | ||
612 | return -1; | |
613 | } | |
614 | ||
eb3b5696 SH |
615 | static int flower_parse_icmp(char *str, __u16 eth_type, __u8 ip_proto, |
616 | enum flower_icmp_field field, struct nlmsghdr *n) | |
617 | { | |
6374961a | 618 | int value_type, mask_type; |
eb3b5696 | 619 | |
6374961a SH |
620 | value_type = flower_icmp_attr_type(eth_type, ip_proto, field); |
621 | mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto, field); | |
622 | if (value_type < 0 || mask_type < 0) | |
eb3b5696 SH |
623 | return -1; |
624 | ||
6374961a | 625 | return flower_parse_u8(str, value_type, mask_type, NULL, NULL, n); |
eb3b5696 SH |
626 | } |
627 | ||
6910d656 | 628 | static int flower_port_attr_type(__u8 ip_proto, enum flower_endpoint endpoint) |
30eb304e | 629 | { |
6bd5b80c | 630 | if (ip_proto == IPPROTO_TCP) |
6910d656 SH |
631 | return endpoint == FLOWER_ENDPOINT_SRC ? |
632 | TCA_FLOWER_KEY_TCP_SRC : | |
a1fb0d48 | 633 | TCA_FLOWER_KEY_TCP_DST; |
6bd5b80c | 634 | else if (ip_proto == IPPROTO_UDP) |
6910d656 SH |
635 | return endpoint == FLOWER_ENDPOINT_SRC ? |
636 | TCA_FLOWER_KEY_UDP_SRC : | |
a1fb0d48 | 637 | TCA_FLOWER_KEY_UDP_DST; |
6bd5b80c | 638 | else if (ip_proto == IPPROTO_SCTP) |
6910d656 SH |
639 | return endpoint == FLOWER_ENDPOINT_SRC ? |
640 | TCA_FLOWER_KEY_SCTP_SRC : | |
a1fb0d48 | 641 | TCA_FLOWER_KEY_SCTP_DST; |
6bd5b80c | 642 | else |
30eb304e | 643 | return -1; |
a1fb0d48 SH |
644 | } |
645 | ||
482fd40a EB |
646 | static int flower_port_attr_mask_type(__u8 ip_proto, |
647 | enum flower_endpoint endpoint) | |
648 | { | |
649 | switch (ip_proto) { | |
650 | case IPPROTO_TCP: | |
651 | return endpoint == FLOWER_ENDPOINT_SRC ? | |
652 | TCA_FLOWER_KEY_TCP_SRC_MASK : | |
653 | TCA_FLOWER_KEY_TCP_DST_MASK; | |
654 | case IPPROTO_UDP: | |
655 | return endpoint == FLOWER_ENDPOINT_SRC ? | |
656 | TCA_FLOWER_KEY_UDP_SRC_MASK : | |
657 | TCA_FLOWER_KEY_UDP_DST_MASK; | |
658 | case IPPROTO_SCTP: | |
659 | return endpoint == FLOWER_ENDPOINT_SRC ? | |
660 | TCA_FLOWER_KEY_SCTP_SRC_MASK : | |
661 | TCA_FLOWER_KEY_SCTP_DST_MASK; | |
662 | default: | |
663 | return -1; | |
664 | } | |
665 | } | |
666 | ||
8930840e AN |
667 | static int flower_port_range_attr_type(__u8 ip_proto, enum flower_endpoint type, |
668 | __be16 *min_port_type, | |
669 | __be16 *max_port_type) | |
670 | { | |
671 | if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP || | |
672 | ip_proto == IPPROTO_SCTP) { | |
673 | if (type == FLOWER_ENDPOINT_SRC) { | |
674 | *min_port_type = TCA_FLOWER_KEY_PORT_SRC_MIN; | |
675 | *max_port_type = TCA_FLOWER_KEY_PORT_SRC_MAX; | |
676 | } else { | |
677 | *min_port_type = TCA_FLOWER_KEY_PORT_DST_MIN; | |
678 | *max_port_type = TCA_FLOWER_KEY_PORT_DST_MAX; | |
679 | } | |
680 | } else { | |
681 | return -1; | |
682 | } | |
683 | return 0; | |
684 | } | |
685 | ||
767b6fd6 LC |
686 | /* parse range args in format 10-20 */ |
687 | static int parse_range(char *str, __be16 *min, __be16 *max) | |
688 | { | |
689 | char *sep; | |
690 | ||
691 | sep = strchr(str, '-'); | |
692 | if (sep) { | |
693 | *sep = '\0'; | |
694 | ||
695 | if (get_be16(min, str, 10)) | |
696 | return -1; | |
697 | ||
698 | if (get_be16(max, sep + 1, 10)) | |
699 | return -1; | |
700 | } else { | |
701 | if (get_be16(min, str, 10)) | |
702 | return -1; | |
703 | } | |
704 | return 0; | |
705 | } | |
706 | ||
6910d656 SH |
707 | static int flower_parse_port(char *str, __u8 ip_proto, |
708 | enum flower_endpoint endpoint, | |
a1fb0d48 SH |
709 | struct nlmsghdr *n) |
710 | { | |
482fd40a | 711 | char *slash = NULL; |
767b6fd6 LC |
712 | __be16 min = 0; |
713 | __be16 max = 0; | |
a1fb0d48 | 714 | int ret; |
a1fb0d48 | 715 | |
767b6fd6 | 716 | ret = parse_range(str, &min, &max); |
482fd40a EB |
717 | if (ret) { |
718 | slash = strchr(str, '/'); | |
719 | if (!slash) | |
720 | return -1; | |
721 | } | |
30eb304e | 722 | |
767b6fd6 | 723 | if (min && max) { |
8930840e | 724 | __be16 min_port_type, max_port_type; |
30eb304e | 725 | |
8930840e AN |
726 | if (max <= min) { |
727 | fprintf(stderr, "max value should be greater than min value\n"); | |
728 | return -1; | |
729 | } | |
730 | if (flower_port_range_attr_type(ip_proto, endpoint, | |
731 | &min_port_type, &max_port_type)) | |
732 | return -1; | |
733 | ||
767b6fd6 LC |
734 | addattr16(n, MAX_MSG, min_port_type, min); |
735 | addattr16(n, MAX_MSG, max_port_type, max); | |
482fd40a | 736 | } else if (slash || (min && !max)) { |
767b6fd6 LC |
737 | int type; |
738 | ||
739 | type = flower_port_attr_type(ip_proto, endpoint); | |
740 | if (type < 0) | |
741 | return -1; | |
482fd40a EB |
742 | |
743 | if (!slash) { | |
744 | addattr16(n, MAX_MSG, type, min); | |
745 | } else { | |
746 | int mask_type; | |
747 | ||
748 | mask_type = flower_port_attr_mask_type(ip_proto, | |
749 | endpoint); | |
750 | if (mask_type < 0) | |
751 | return -1; | |
752 | return flower_parse_u16(str, type, mask_type, n, true); | |
753 | } | |
8930840e AN |
754 | } else { |
755 | return -1; | |
756 | } | |
30eb304e JP |
757 | return 0; |
758 | } | |
759 | ||
0c30d14d JP |
760 | #define TCP_FLAGS_MAX_MASK 0xfff |
761 | ||
762 | static int flower_parse_tcp_flags(char *str, int flags_type, int mask_type, | |
763 | struct nlmsghdr *n) | |
764 | { | |
765 | char *slash; | |
766 | int ret, err = -1; | |
767 | __u16 flags; | |
768 | ||
769 | slash = strchr(str, '/'); | |
770 | if (slash) | |
771 | *slash = '\0'; | |
772 | ||
773 | ret = get_u16(&flags, str, 16); | |
774 | if (ret < 0 || flags & ~TCP_FLAGS_MAX_MASK) | |
775 | goto err; | |
776 | ||
777 | addattr16(n, MAX_MSG, flags_type, htons(flags)); | |
778 | ||
779 | if (slash) { | |
780 | ret = get_u16(&flags, slash + 1, 16); | |
781 | if (ret < 0 || flags & ~TCP_FLAGS_MAX_MASK) | |
782 | goto err; | |
783 | } else { | |
784 | flags = TCP_FLAGS_MAX_MASK; | |
785 | } | |
786 | addattr16(n, MAX_MSG, mask_type, htons(flags)); | |
787 | ||
788 | err = 0; | |
789 | err: | |
790 | if (slash) | |
791 | *slash = '/'; | |
792 | return err; | |
793 | } | |
794 | ||
6ea2c2b1 OG |
795 | static int flower_parse_ip_tos_ttl(char *str, int key_type, int mask_type, |
796 | struct nlmsghdr *n) | |
797 | { | |
798 | char *slash; | |
799 | int ret, err = -1; | |
800 | __u8 tos_ttl; | |
801 | ||
802 | slash = strchr(str, '/'); | |
803 | if (slash) | |
804 | *slash = '\0'; | |
805 | ||
806 | ret = get_u8(&tos_ttl, str, 10); | |
807 | if (ret < 0) | |
808 | ret = get_u8(&tos_ttl, str, 16); | |
809 | if (ret < 0) | |
810 | goto err; | |
811 | ||
812 | addattr8(n, MAX_MSG, key_type, tos_ttl); | |
813 | ||
814 | if (slash) { | |
815 | ret = get_u8(&tos_ttl, slash + 1, 16); | |
816 | if (ret < 0) | |
817 | goto err; | |
818 | } else { | |
819 | tos_ttl = 0xff; | |
820 | } | |
821 | addattr8(n, MAX_MSG, mask_type, tos_ttl); | |
822 | ||
823 | err = 0; | |
824 | err: | |
825 | if (slash) | |
826 | *slash = '/'; | |
827 | return err; | |
828 | } | |
829 | ||
bb9b63b1 AV |
830 | static int flower_parse_key_id(const char *str, int type, struct nlmsghdr *n) |
831 | { | |
832 | int ret; | |
833 | __be32 key_id; | |
834 | ||
835 | ret = get_be32(&key_id, str, 10); | |
836 | if (!ret) | |
837 | addattr32(n, MAX_MSG, type, key_id); | |
838 | ||
839 | return ret; | |
840 | } | |
841 | ||
41aa17ff HHZ |
842 | static int flower_parse_enc_port(char *str, int type, struct nlmsghdr *n) |
843 | { | |
844 | int ret; | |
845 | __be16 port; | |
846 | ||
847 | ret = get_be16(&port, str, 10); | |
848 | if (ret) | |
849 | return -1; | |
850 | ||
851 | addattr16(n, MAX_MSG, type, port); | |
852 | ||
853 | return 0; | |
854 | } | |
855 | ||
93c8d5f7 | 856 | static int flower_parse_geneve_opt(char *str, struct nlmsghdr *n) |
56155d4d PJV |
857 | { |
858 | struct rtattr *nest; | |
859 | char *token; | |
860 | int i, err; | |
861 | ||
862 | nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS_GENEVE); | |
863 | ||
864 | i = 1; | |
865 | token = strsep(&str, ":"); | |
866 | while (token) { | |
867 | switch (i) { | |
868 | case TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS: | |
869 | { | |
870 | __be16 opt_class; | |
871 | ||
872 | if (!strlen(token)) | |
873 | break; | |
874 | err = get_be16(&opt_class, token, 16); | |
875 | if (err) | |
876 | return err; | |
877 | ||
878 | addattr16(n, MAX_MSG, i, opt_class); | |
879 | break; | |
880 | } | |
881 | case TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE: | |
882 | { | |
883 | __u8 opt_type; | |
884 | ||
885 | if (!strlen(token)) | |
886 | break; | |
887 | err = get_u8(&opt_type, token, 16); | |
888 | if (err) | |
889 | return err; | |
890 | ||
891 | addattr8(n, MAX_MSG, i, opt_type); | |
892 | break; | |
893 | } | |
894 | case TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA: | |
895 | { | |
896 | size_t token_len = strlen(token); | |
897 | __u8 *opts; | |
898 | ||
899 | if (!token_len) | |
900 | break; | |
901 | opts = malloc(token_len / 2); | |
902 | if (!opts) | |
903 | return -1; | |
904 | if (hex2mem(token, opts, token_len / 2) < 0) { | |
905 | free(opts); | |
906 | return -1; | |
907 | } | |
908 | addattr_l(n, MAX_MSG, i, opts, token_len / 2); | |
909 | free(opts); | |
910 | ||
911 | break; | |
912 | } | |
913 | default: | |
914 | fprintf(stderr, "Unknown \"geneve_opts\" type\n"); | |
915 | return -1; | |
916 | } | |
917 | ||
918 | token = strsep(&str, ":"); | |
919 | i++; | |
920 | } | |
921 | addattr_nest_end(n, nest); | |
922 | ||
923 | return 0; | |
924 | } | |
925 | ||
93c8d5f7 XL |
926 | static int flower_parse_vxlan_opt(char *str, struct nlmsghdr *n) |
927 | { | |
928 | struct rtattr *nest; | |
929 | __u32 gbp; | |
930 | int err; | |
931 | ||
932 | nest = addattr_nest(n, MAX_MSG, | |
933 | TCA_FLOWER_KEY_ENC_OPTS_VXLAN | NLA_F_NESTED); | |
934 | ||
935 | err = get_u32(&gbp, str, 0); | |
936 | if (err) | |
937 | return err; | |
938 | addattr32(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, gbp); | |
939 | ||
940 | addattr_nest_end(n, nest); | |
941 | ||
942 | return 0; | |
943 | } | |
944 | ||
4e578c78 XL |
945 | static int flower_parse_erspan_opt(char *str, struct nlmsghdr *n) |
946 | { | |
947 | struct rtattr *nest; | |
948 | char *token; | |
949 | int i, err; | |
950 | ||
951 | nest = addattr_nest(n, MAX_MSG, | |
952 | TCA_FLOWER_KEY_ENC_OPTS_ERSPAN | NLA_F_NESTED); | |
953 | ||
954 | i = 1; | |
955 | token = strsep(&str, ":"); | |
956 | while (token) { | |
957 | switch (i) { | |
958 | case TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER: | |
959 | { | |
960 | __u8 opt_type; | |
961 | ||
962 | if (!strlen(token)) | |
963 | break; | |
964 | err = get_u8(&opt_type, token, 0); | |
965 | if (err) | |
966 | return err; | |
967 | ||
968 | addattr8(n, MAX_MSG, i, opt_type); | |
969 | break; | |
970 | } | |
971 | case TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX: | |
972 | { | |
973 | __be32 opt_index; | |
974 | ||
975 | if (!strlen(token)) | |
976 | break; | |
977 | err = get_be32(&opt_index, token, 0); | |
978 | if (err) | |
979 | return err; | |
980 | ||
981 | addattr32(n, MAX_MSG, i, opt_index); | |
982 | break; | |
983 | } | |
984 | case TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR: | |
985 | { | |
986 | __u8 opt_type; | |
987 | ||
988 | if (!strlen(token)) | |
989 | break; | |
990 | err = get_u8(&opt_type, token, 0); | |
991 | if (err) | |
992 | return err; | |
993 | ||
994 | addattr8(n, MAX_MSG, i, opt_type); | |
995 | break; | |
996 | } | |
997 | case TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID: | |
998 | { | |
999 | __u8 opt_type; | |
1000 | ||
1001 | if (!strlen(token)) | |
1002 | break; | |
1003 | err = get_u8(&opt_type, token, 0); | |
1004 | if (err) | |
1005 | return err; | |
1006 | ||
1007 | addattr8(n, MAX_MSG, i, opt_type); | |
1008 | break; | |
1009 | } | |
1010 | default: | |
1011 | fprintf(stderr, "Unknown \"geneve_opts\" type\n"); | |
1012 | return -1; | |
1013 | } | |
1014 | ||
1015 | token = strsep(&str, ":"); | |
1016 | i++; | |
1017 | } | |
1018 | addattr_nest_end(n, nest); | |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
93c8d5f7 | 1023 | static int flower_parse_geneve_opts(char *str, struct nlmsghdr *n) |
56155d4d PJV |
1024 | { |
1025 | char *token; | |
1026 | int err; | |
1027 | ||
1028 | token = strsep(&str, ","); | |
1029 | while (token) { | |
93c8d5f7 | 1030 | err = flower_parse_geneve_opt(token, n); |
56155d4d PJV |
1031 | if (err) |
1032 | return err; | |
1033 | ||
1034 | token = strsep(&str, ","); | |
1035 | } | |
1036 | ||
1037 | return 0; | |
1038 | } | |
1039 | ||
1040 | static int flower_check_enc_opt_key(char *key) | |
1041 | { | |
1042 | int key_len, col_cnt = 0; | |
1043 | ||
1044 | key_len = strlen(key); | |
1045 | while ((key = strchr(key, ':'))) { | |
1046 | if (strlen(key) == key_len) | |
1047 | return -1; | |
1048 | ||
1049 | key_len = strlen(key) - 1; | |
1050 | col_cnt++; | |
1051 | key++; | |
1052 | } | |
1053 | ||
1054 | if (col_cnt != 2 || !key_len) | |
1055 | return -1; | |
1056 | ||
1057 | return 0; | |
1058 | } | |
1059 | ||
93c8d5f7 | 1060 | static int flower_parse_enc_opts_geneve(char *str, struct nlmsghdr *n) |
56155d4d PJV |
1061 | { |
1062 | char key[XATTR_SIZE_MAX], mask[XATTR_SIZE_MAX]; | |
1063 | int data_len, key_len, mask_len, err; | |
1064 | char *token, *slash; | |
1065 | struct rtattr *nest; | |
1066 | ||
1067 | key_len = 0; | |
1068 | mask_len = 0; | |
1069 | token = strsep(&str, ","); | |
1070 | while (token) { | |
1071 | slash = strchr(token, '/'); | |
1072 | if (slash) | |
1073 | *slash = '\0'; | |
1074 | ||
1075 | if ((key_len + strlen(token) > XATTR_SIZE_MAX) || | |
1076 | flower_check_enc_opt_key(token)) | |
1077 | return -1; | |
1078 | ||
1079 | strcpy(&key[key_len], token); | |
1080 | key_len += strlen(token) + 1; | |
1081 | key[key_len - 1] = ','; | |
1082 | ||
1083 | if (!slash) { | |
1084 | /* Pad out mask when not provided */ | |
1085 | if (mask_len + strlen(token) > XATTR_SIZE_MAX) | |
1086 | return -1; | |
1087 | ||
1088 | data_len = strlen(rindex(token, ':')); | |
1089 | sprintf(&mask[mask_len], "ffff:ff:"); | |
1090 | mask_len += 8; | |
1091 | memset(&mask[mask_len], 'f', data_len - 1); | |
1092 | mask_len += data_len; | |
1093 | mask[mask_len - 1] = ','; | |
1094 | token = strsep(&str, ","); | |
1095 | continue; | |
1096 | } | |
1097 | ||
1098 | if (mask_len + strlen(slash + 1) > XATTR_SIZE_MAX) | |
1099 | return -1; | |
1100 | ||
1101 | strcpy(&mask[mask_len], slash + 1); | |
1102 | mask_len += strlen(slash + 1) + 1; | |
1103 | mask[mask_len - 1] = ','; | |
1104 | ||
1105 | *slash = '/'; | |
1106 | token = strsep(&str, ","); | |
1107 | } | |
1108 | key[key_len - 1] = '\0'; | |
1109 | mask[mask_len - 1] = '\0'; | |
1110 | ||
1111 | nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS); | |
93c8d5f7 | 1112 | err = flower_parse_geneve_opts(key, n); |
56155d4d PJV |
1113 | if (err) |
1114 | return err; | |
1115 | addattr_nest_end(n, nest); | |
1116 | ||
1117 | nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS_MASK); | |
93c8d5f7 XL |
1118 | err = flower_parse_geneve_opts(mask, n); |
1119 | if (err) | |
1120 | return err; | |
1121 | addattr_nest_end(n, nest); | |
1122 | ||
1123 | return 0; | |
1124 | } | |
1125 | ||
1126 | static int flower_parse_enc_opts_vxlan(char *str, struct nlmsghdr *n) | |
1127 | { | |
1128 | char key[XATTR_SIZE_MAX], mask[XATTR_SIZE_MAX]; | |
1129 | struct rtattr *nest; | |
1130 | char *slash; | |
1131 | int err; | |
1132 | ||
1133 | slash = strchr(str, '/'); | |
1134 | if (slash) { | |
1135 | *slash++ = '\0'; | |
1136 | if (strlen(slash) > XATTR_SIZE_MAX) | |
1137 | return -1; | |
1138 | strcpy(mask, slash); | |
1139 | } else { | |
1140 | strcpy(mask, "0xffffffff"); | |
1141 | } | |
1142 | ||
1143 | if (strlen(str) > XATTR_SIZE_MAX) | |
1144 | return -1; | |
1145 | strcpy(key, str); | |
1146 | ||
1147 | nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS | NLA_F_NESTED); | |
1148 | err = flower_parse_vxlan_opt(str, n); | |
1149 | if (err) | |
1150 | return err; | |
1151 | addattr_nest_end(n, nest); | |
1152 | ||
1153 | nest = addattr_nest(n, MAX_MSG, | |
1154 | TCA_FLOWER_KEY_ENC_OPTS_MASK | NLA_F_NESTED); | |
1155 | err = flower_parse_vxlan_opt(mask, n); | |
56155d4d PJV |
1156 | if (err) |
1157 | return err; | |
1158 | addattr_nest_end(n, nest); | |
1159 | ||
1160 | return 0; | |
1161 | } | |
1162 | ||
4e578c78 XL |
1163 | static int flower_parse_enc_opts_erspan(char *str, struct nlmsghdr *n) |
1164 | { | |
1165 | char key[XATTR_SIZE_MAX], mask[XATTR_SIZE_MAX]; | |
1166 | struct rtattr *nest; | |
1167 | char *slash; | |
1168 | int err; | |
1169 | ||
1170 | ||
1171 | slash = strchr(str, '/'); | |
1172 | if (slash) { | |
1173 | *slash++ = '\0'; | |
1174 | if (strlen(slash) > XATTR_SIZE_MAX) | |
1175 | return -1; | |
1176 | strcpy(mask, slash); | |
1177 | } else { | |
1178 | int index; | |
1179 | ||
1180 | slash = strchr(str, ':'); | |
1181 | index = (int)(slash - str); | |
1182 | memcpy(mask, str, index); | |
1183 | strcpy(mask + index, ":0xffffffff:0xff:0xff"); | |
1184 | } | |
1185 | ||
1186 | if (strlen(str) > XATTR_SIZE_MAX) | |
1187 | return -1; | |
1188 | strcpy(key, str); | |
1189 | ||
1190 | nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS | NLA_F_NESTED); | |
1191 | err = flower_parse_erspan_opt(key, n); | |
1192 | if (err) | |
1193 | return err; | |
1194 | addattr_nest_end(n, nest); | |
1195 | ||
1196 | nest = addattr_nest(n, MAX_MSG, | |
1197 | TCA_FLOWER_KEY_ENC_OPTS_MASK | NLA_F_NESTED); | |
1198 | err = flower_parse_erspan_opt(mask, n); | |
1199 | if (err) | |
1200 | return err; | |
1201 | addattr_nest_end(n, nest); | |
1202 | ||
1203 | return 0; | |
1204 | } | |
1205 | ||
eb09a15c GN |
1206 | static int flower_parse_mpls_lse(int *argc_p, char ***argv_p, |
1207 | struct nlmsghdr *nlh) | |
1208 | { | |
1209 | struct rtattr *lse_attr; | |
1210 | char **argv = *argv_p; | |
1211 | int argc = *argc_p; | |
1212 | __u8 depth = 0; | |
1213 | int ret; | |
1214 | ||
1215 | lse_attr = addattr_nest(nlh, MAX_MSG, | |
1216 | TCA_FLOWER_KEY_MPLS_OPTS_LSE | NLA_F_NESTED); | |
1217 | ||
1218 | while (argc > 0) { | |
1219 | if (matches(*argv, "depth") == 0) { | |
1220 | NEXT_ARG(); | |
1221 | ret = get_u8(&depth, *argv, 10); | |
1222 | if (ret < 0 || depth < 1) { | |
1223 | fprintf(stderr, "Illegal \"depth\"\n"); | |
1224 | return -1; | |
1225 | } | |
1226 | addattr8(nlh, MAX_MSG, | |
1227 | TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH, depth); | |
1228 | } else if (matches(*argv, "label") == 0) { | |
1229 | __u32 label; | |
1230 | ||
1231 | NEXT_ARG(); | |
1232 | ret = get_u32(&label, *argv, 10); | |
1233 | if (ret < 0 || | |
1234 | label & ~(MPLS_LS_LABEL_MASK >> MPLS_LS_LABEL_SHIFT)) { | |
1235 | fprintf(stderr, "Illegal \"label\"\n"); | |
1236 | return -1; | |
1237 | } | |
1238 | addattr32(nlh, MAX_MSG, | |
1239 | TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL, label); | |
1240 | } else if (matches(*argv, "tc") == 0) { | |
1241 | __u8 tc; | |
1242 | ||
1243 | NEXT_ARG(); | |
1244 | ret = get_u8(&tc, *argv, 10); | |
1245 | if (ret < 0 || | |
1246 | tc & ~(MPLS_LS_TC_MASK >> MPLS_LS_TC_SHIFT)) { | |
1247 | fprintf(stderr, "Illegal \"tc\"\n"); | |
1248 | return -1; | |
1249 | } | |
1250 | addattr8(nlh, MAX_MSG, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC, | |
1251 | tc); | |
1252 | } else if (matches(*argv, "bos") == 0) { | |
1253 | __u8 bos; | |
1254 | ||
1255 | NEXT_ARG(); | |
1256 | ret = get_u8(&bos, *argv, 10); | |
1257 | if (ret < 0 || bos & ~(MPLS_LS_S_MASK >> MPLS_LS_S_SHIFT)) { | |
1258 | fprintf(stderr, "Illegal \"bos\"\n"); | |
1259 | return -1; | |
1260 | } | |
1261 | addattr8(nlh, MAX_MSG, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS, | |
1262 | bos); | |
1263 | } else if (matches(*argv, "ttl") == 0) { | |
1264 | __u8 ttl; | |
1265 | ||
1266 | NEXT_ARG(); | |
1267 | ret = get_u8(&ttl, *argv, 10); | |
1268 | if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) { | |
1269 | fprintf(stderr, "Illegal \"ttl\"\n"); | |
1270 | return -1; | |
1271 | } | |
1272 | addattr8(nlh, MAX_MSG, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL, | |
1273 | ttl); | |
1274 | } else { | |
1275 | break; | |
1276 | } | |
1277 | argc--; argv++; | |
1278 | } | |
1279 | ||
1280 | if (!depth) { | |
1281 | missarg("depth"); | |
1282 | return -1; | |
1283 | } | |
1284 | ||
1285 | addattr_nest_end(nlh, lse_attr); | |
1286 | ||
1287 | *argc_p = argc; | |
1288 | *argv_p = argv; | |
1289 | ||
1290 | return 0; | |
1291 | } | |
1292 | ||
1293 | static int flower_parse_mpls(int *argc_p, char ***argv_p, struct nlmsghdr *nlh) | |
1294 | { | |
1295 | struct rtattr *mpls_attr; | |
1296 | char **argv = *argv_p; | |
1297 | int argc = *argc_p; | |
1298 | ||
1299 | mpls_attr = addattr_nest(nlh, MAX_MSG, | |
1300 | TCA_FLOWER_KEY_MPLS_OPTS | NLA_F_NESTED); | |
1301 | ||
1302 | while (argc > 0) { | |
1303 | if (matches(*argv, "lse") == 0) { | |
1304 | NEXT_ARG(); | |
1305 | if (flower_parse_mpls_lse(&argc, &argv, nlh) < 0) | |
1306 | return -1; | |
1307 | } else { | |
1308 | break; | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | addattr_nest_end(nlh, mpls_attr); | |
1313 | ||
1314 | *argc_p = argc; | |
1315 | *argv_p = argv; | |
1316 | ||
1317 | return 0; | |
1318 | } | |
1319 | ||
30eb304e JP |
1320 | static int flower_parse_opt(struct filter_util *qu, char *handle, |
1321 | int argc, char **argv, struct nlmsghdr *n) | |
1322 | { | |
1323 | int ret; | |
1324 | struct tcmsg *t = NLMSG_DATA(n); | |
eb09a15c GN |
1325 | bool mpls_format_old = false; |
1326 | bool mpls_format_new = false; | |
30eb304e | 1327 | struct rtattr *tail; |
4c551369 ZD |
1328 | __be16 tc_proto = TC_H_MIN(t->tcm_info); |
1329 | __be16 eth_type = tc_proto; | |
745d9172 | 1330 | __be16 vlan_ethtype = 0; |
30eb304e | 1331 | __u8 ip_proto = 0xff; |
cfcabf18 | 1332 | __u32 flags = 0; |
08f66c80 PB |
1333 | __u32 mtf = 0; |
1334 | __u32 mtf_mask = 0; | |
30eb304e | 1335 | |
30eb304e JP |
1336 | if (handle) { |
1337 | ret = get_u32(&t->tcm_handle, handle, 0); | |
1338 | if (ret) { | |
1339 | fprintf(stderr, "Illegal \"handle\"\n"); | |
1340 | return -1; | |
1341 | } | |
1342 | } | |
1343 | ||
1344 | tail = (struct rtattr *) (((void *) n) + NLMSG_ALIGN(n->nlmsg_len)); | |
1345 | addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0); | |
1346 | ||
488b41d0 JHS |
1347 | if (argc == 0) { |
1348 | /*at minimal we will match all ethertype packets */ | |
1349 | goto parse_done; | |
1350 | } | |
1351 | ||
30eb304e JP |
1352 | while (argc > 0) { |
1353 | if (matches(*argv, "classid") == 0 || | |
1354 | matches(*argv, "flowid") == 0) { | |
32a121cb | 1355 | unsigned int handle; |
30eb304e JP |
1356 | |
1357 | NEXT_ARG(); | |
1358 | ret = get_tc_classid(&handle, *argv); | |
1359 | if (ret) { | |
1360 | fprintf(stderr, "Illegal \"classid\"\n"); | |
1361 | return -1; | |
1362 | } | |
1363 | addattr_l(n, MAX_MSG, TCA_FLOWER_CLASSID, &handle, 4); | |
0d575c4d AN |
1364 | } else if (matches(*argv, "hw_tc") == 0) { |
1365 | unsigned int handle; | |
1366 | __u32 tc; | |
1367 | char *end; | |
1368 | ||
1369 | NEXT_ARG(); | |
1370 | tc = strtoul(*argv, &end, 0); | |
1371 | if (*end) { | |
1372 | fprintf(stderr, "Illegal TC index\n"); | |
1373 | return -1; | |
1374 | } | |
1375 | if (tc >= TC_QOPT_MAX_QUEUE) { | |
1376 | fprintf(stderr, "TC index exceeds max range\n"); | |
1377 | return -1; | |
1378 | } | |
1379 | handle = TC_H_MAKE(TC_H_MAJ(t->tcm_parent), | |
1380 | TC_H_MIN(tc + TC_H_MIN_PRIORITY)); | |
1381 | addattr_l(n, MAX_MSG, TCA_FLOWER_CLASSID, &handle, | |
1382 | sizeof(handle)); | |
08f66c80 | 1383 | } else if (matches(*argv, "ip_flags") == 0) { |
22a8f019 PB |
1384 | NEXT_ARG(); |
1385 | ret = flower_parse_matching_flags(*argv, | |
08f66c80 PB |
1386 | FLOWER_IP_FLAGS, |
1387 | &mtf, | |
1388 | &mtf_mask); | |
22a8f019 | 1389 | if (ret < 0) { |
08f66c80 | 1390 | fprintf(stderr, "Illegal \"ip_flags\"\n"); |
22a8f019 PB |
1391 | return -1; |
1392 | } | |
ac6a4c22 MRL |
1393 | } else if (matches(*argv, "verbose") == 0) { |
1394 | flags |= TCA_CLS_FLAGS_VERBOSE; | |
cfcabf18 AV |
1395 | } else if (matches(*argv, "skip_hw") == 0) { |
1396 | flags |= TCA_CLS_FLAGS_SKIP_HW; | |
1397 | } else if (matches(*argv, "skip_sw") == 0) { | |
1398 | flags |= TCA_CLS_FLAGS_SKIP_SW; | |
2fffb1c0 PB |
1399 | } else if (matches(*argv, "ct_state") == 0) { |
1400 | NEXT_ARG(); | |
1401 | ret = flower_parse_ct_state(*argv, n); | |
1402 | if (ret < 0) { | |
1403 | fprintf(stderr, "Illegal \"ct_state\"\n"); | |
1404 | return -1; | |
1405 | } | |
1406 | } else if (matches(*argv, "ct_zone") == 0) { | |
1407 | NEXT_ARG(); | |
1408 | ret = flower_parse_ct_zone(*argv, n); | |
1409 | if (ret < 0) { | |
1410 | fprintf(stderr, "Illegal \"ct_zone\"\n"); | |
1411 | return -1; | |
1412 | } | |
1413 | } else if (matches(*argv, "ct_mark") == 0) { | |
1414 | NEXT_ARG(); | |
1415 | ret = flower_parse_ct_mark(*argv, n); | |
1416 | if (ret < 0) { | |
1417 | fprintf(stderr, "Illegal \"ct_mark\"\n"); | |
1418 | return -1; | |
1419 | } | |
1420 | } else if (matches(*argv, "ct_label") == 0) { | |
1421 | NEXT_ARG(); | |
1422 | ret = flower_parse_ct_labels(*argv, n); | |
1423 | if (ret < 0) { | |
1424 | fprintf(stderr, "Illegal \"ct_label\"\n"); | |
1425 | return -1; | |
1426 | } | |
30eb304e | 1427 | } else if (matches(*argv, "indev") == 0) { |
30eb304e | 1428 | NEXT_ARG(); |
625df645 PS |
1429 | if (check_ifname(*argv)) |
1430 | invarg("\"indev\" not a valid ifname", *argv); | |
ee474849 | 1431 | addattrstrz(n, MAX_MSG, TCA_FLOWER_INDEV, *argv); |
745d9172 HHZ |
1432 | } else if (matches(*argv, "vlan_id") == 0) { |
1433 | __u16 vid; | |
1434 | ||
1435 | NEXT_ARG(); | |
ed40b7e2 | 1436 | if (!eth_type_vlan(tc_proto)) { |
1f0a5dfd | 1437 | fprintf(stderr, "Can't set \"vlan_id\" if ethertype isn't 802.1Q or 802.1AD\n"); |
745d9172 HHZ |
1438 | return -1; |
1439 | } | |
1440 | ret = get_u16(&vid, *argv, 10); | |
1441 | if (ret < 0 || vid & ~0xfff) { | |
1442 | fprintf(stderr, "Illegal \"vlan_id\"\n"); | |
1443 | return -1; | |
1444 | } | |
1445 | addattr16(n, MAX_MSG, TCA_FLOWER_KEY_VLAN_ID, vid); | |
1446 | } else if (matches(*argv, "vlan_prio") == 0) { | |
1447 | __u8 vlan_prio; | |
1448 | ||
1449 | NEXT_ARG(); | |
ed40b7e2 | 1450 | if (!eth_type_vlan(tc_proto)) { |
1f0a5dfd | 1451 | fprintf(stderr, "Can't set \"vlan_prio\" if ethertype isn't 802.1Q or 802.1AD\n"); |
745d9172 HHZ |
1452 | return -1; |
1453 | } | |
1454 | ret = get_u8(&vlan_prio, *argv, 10); | |
1455 | if (ret < 0 || vlan_prio & ~0x7) { | |
1456 | fprintf(stderr, "Illegal \"vlan_prio\"\n"); | |
1457 | return -1; | |
1458 | } | |
512caeb2 SH |
1459 | addattr8(n, MAX_MSG, |
1460 | TCA_FLOWER_KEY_VLAN_PRIO, vlan_prio); | |
745d9172 HHZ |
1461 | } else if (matches(*argv, "vlan_ethtype") == 0) { |
1462 | NEXT_ARG(); | |
1463 | ret = flower_parse_vlan_eth_type(*argv, eth_type, | |
512caeb2 SH |
1464 | TCA_FLOWER_KEY_VLAN_ETH_TYPE, |
1465 | &vlan_ethtype, n); | |
745d9172 HHZ |
1466 | if (ret < 0) |
1467 | return -1; | |
4c551369 ZD |
1468 | /* get new ethtype for later parsing */ |
1469 | eth_type = vlan_ethtype; | |
1f0a5dfd JL |
1470 | } else if (matches(*argv, "cvlan_id") == 0) { |
1471 | __u16 vid; | |
1472 | ||
1473 | NEXT_ARG(); | |
1474 | if (!eth_type_vlan(vlan_ethtype)) { | |
1475 | fprintf(stderr, "Can't set \"cvlan_id\" if inner vlan ethertype isn't 802.1Q or 802.1AD\n"); | |
1476 | return -1; | |
1477 | } | |
1478 | ret = get_u16(&vid, *argv, 10); | |
1479 | if (ret < 0 || vid & ~0xfff) { | |
1480 | fprintf(stderr, "Illegal \"cvlan_id\"\n"); | |
1481 | return -1; | |
1482 | } | |
1483 | addattr16(n, MAX_MSG, TCA_FLOWER_KEY_CVLAN_ID, vid); | |
1484 | } else if (matches(*argv, "cvlan_prio") == 0) { | |
1485 | __u8 cvlan_prio; | |
1486 | ||
1487 | NEXT_ARG(); | |
1488 | if (!eth_type_vlan(vlan_ethtype)) { | |
1489 | fprintf(stderr, "Can't set \"cvlan_prio\" if inner vlan ethertype isn't 802.1Q or 802.1AD\n"); | |
1490 | return -1; | |
1491 | } | |
1492 | ret = get_u8(&cvlan_prio, *argv, 10); | |
1493 | if (ret < 0 || cvlan_prio & ~0x7) { | |
1494 | fprintf(stderr, "Illegal \"cvlan_prio\"\n"); | |
1495 | return -1; | |
1496 | } | |
1497 | addattr8(n, MAX_MSG, | |
1498 | TCA_FLOWER_KEY_CVLAN_PRIO, cvlan_prio); | |
1499 | } else if (matches(*argv, "cvlan_ethtype") == 0) { | |
1500 | NEXT_ARG(); | |
4c551369 | 1501 | /* get new ethtype for later parsing */ |
1f0a5dfd JL |
1502 | ret = flower_parse_vlan_eth_type(*argv, vlan_ethtype, |
1503 | TCA_FLOWER_KEY_CVLAN_ETH_TYPE, | |
4c551369 | 1504 | ð_type, n); |
1f0a5dfd JL |
1505 | if (ret < 0) |
1506 | return -1; | |
eb09a15c GN |
1507 | } else if (matches(*argv, "mpls") == 0) { |
1508 | NEXT_ARG(); | |
1509 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1510 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1511 | fprintf(stderr, | |
1512 | "Can't set \"mpls\" if ethertype isn't MPLS\n"); | |
1513 | return -1; | |
1514 | } | |
1515 | if (mpls_format_old) { | |
1516 | fprintf(stderr, | |
1517 | "Can't set \"mpls\" if \"mpls_label\", \"mpls_tc\", \"mpls_bos\" or \"mpls_ttl\" is set\n"); | |
1518 | return -1; | |
1519 | } | |
1520 | mpls_format_new = true; | |
1521 | if (flower_parse_mpls(&argc, &argv, n) < 0) | |
1522 | return -1; | |
1523 | continue; | |
7638ee13 BL |
1524 | } else if (matches(*argv, "mpls_label") == 0) { |
1525 | __u32 label; | |
1526 | ||
1527 | NEXT_ARG(); | |
1528 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1529 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1530 | fprintf(stderr, | |
1531 | "Can't set \"mpls_label\" if ethertype isn't MPLS\n"); | |
1532 | return -1; | |
1533 | } | |
eb09a15c GN |
1534 | if (mpls_format_new) { |
1535 | fprintf(stderr, | |
1536 | "Can't set \"mpls_label\" if \"mpls\" is set\n"); | |
1537 | return -1; | |
1538 | } | |
1539 | mpls_format_old = true; | |
7638ee13 BL |
1540 | ret = get_u32(&label, *argv, 10); |
1541 | if (ret < 0 || label & ~(MPLS_LS_LABEL_MASK >> MPLS_LS_LABEL_SHIFT)) { | |
1542 | fprintf(stderr, "Illegal \"mpls_label\"\n"); | |
1543 | return -1; | |
1544 | } | |
1545 | addattr32(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_LABEL, label); | |
1546 | } else if (matches(*argv, "mpls_tc") == 0) { | |
1547 | __u8 tc; | |
1548 | ||
1549 | NEXT_ARG(); | |
1550 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1551 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1552 | fprintf(stderr, | |
1553 | "Can't set \"mpls_tc\" if ethertype isn't MPLS\n"); | |
1554 | return -1; | |
1555 | } | |
eb09a15c GN |
1556 | if (mpls_format_new) { |
1557 | fprintf(stderr, | |
1558 | "Can't set \"mpls_tc\" if \"mpls\" is set\n"); | |
1559 | return -1; | |
1560 | } | |
1561 | mpls_format_old = true; | |
7638ee13 BL |
1562 | ret = get_u8(&tc, *argv, 10); |
1563 | if (ret < 0 || tc & ~(MPLS_LS_TC_MASK >> MPLS_LS_TC_SHIFT)) { | |
1564 | fprintf(stderr, "Illegal \"mpls_tc\"\n"); | |
1565 | return -1; | |
1566 | } | |
1567 | addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_TC, tc); | |
1568 | } else if (matches(*argv, "mpls_bos") == 0) { | |
1569 | __u8 bos; | |
1570 | ||
1571 | NEXT_ARG(); | |
1572 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1573 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1574 | fprintf(stderr, | |
1575 | "Can't set \"mpls_bos\" if ethertype isn't MPLS\n"); | |
1576 | return -1; | |
1577 | } | |
eb09a15c GN |
1578 | if (mpls_format_new) { |
1579 | fprintf(stderr, | |
1580 | "Can't set \"mpls_bos\" if \"mpls\" is set\n"); | |
1581 | return -1; | |
1582 | } | |
1583 | mpls_format_old = true; | |
7638ee13 BL |
1584 | ret = get_u8(&bos, *argv, 10); |
1585 | if (ret < 0 || bos & ~(MPLS_LS_S_MASK >> MPLS_LS_S_SHIFT)) { | |
1586 | fprintf(stderr, "Illegal \"mpls_bos\"\n"); | |
1587 | return -1; | |
1588 | } | |
1589 | addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_BOS, bos); | |
1590 | } else if (matches(*argv, "mpls_ttl") == 0) { | |
1591 | __u8 ttl; | |
1592 | ||
1593 | NEXT_ARG(); | |
1594 | if (eth_type != htons(ETH_P_MPLS_UC) && | |
1595 | eth_type != htons(ETH_P_MPLS_MC)) { | |
1596 | fprintf(stderr, | |
1597 | "Can't set \"mpls_ttl\" if ethertype isn't MPLS\n"); | |
1598 | return -1; | |
1599 | } | |
eb09a15c GN |
1600 | if (mpls_format_new) { |
1601 | fprintf(stderr, | |
1602 | "Can't set \"mpls_ttl\" if \"mpls\" is set\n"); | |
1603 | return -1; | |
1604 | } | |
1605 | mpls_format_old = true; | |
7638ee13 BL |
1606 | ret = get_u8(&ttl, *argv, 10); |
1607 | if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) { | |
1608 | fprintf(stderr, "Illegal \"mpls_ttl\"\n"); | |
1609 | return -1; | |
1610 | } | |
1611 | addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_TTL, ttl); | |
30eb304e JP |
1612 | } else if (matches(*argv, "dst_mac") == 0) { |
1613 | NEXT_ARG(); | |
1614 | ret = flower_parse_eth_addr(*argv, | |
1615 | TCA_FLOWER_KEY_ETH_DST, | |
1616 | TCA_FLOWER_KEY_ETH_DST_MASK, | |
1617 | n); | |
1618 | if (ret < 0) { | |
1619 | fprintf(stderr, "Illegal \"dst_mac\"\n"); | |
1620 | return -1; | |
1621 | } | |
1622 | } else if (matches(*argv, "src_mac") == 0) { | |
1623 | NEXT_ARG(); | |
1624 | ret = flower_parse_eth_addr(*argv, | |
1625 | TCA_FLOWER_KEY_ETH_SRC, | |
1626 | TCA_FLOWER_KEY_ETH_SRC_MASK, | |
1627 | n); | |
1628 | if (ret < 0) { | |
1629 | fprintf(stderr, "Illegal \"src_mac\"\n"); | |
1630 | return -1; | |
1631 | } | |
30eb304e JP |
1632 | } else if (matches(*argv, "ip_proto") == 0) { |
1633 | NEXT_ARG(); | |
4c551369 | 1634 | ret = flower_parse_ip_proto(*argv, eth_type, |
30eb304e JP |
1635 | TCA_FLOWER_KEY_IP_PROTO, |
1636 | &ip_proto, n); | |
1637 | if (ret < 0) { | |
1638 | fprintf(stderr, "Illegal \"ip_proto\"\n"); | |
1639 | return -1; | |
1640 | } | |
6ea2c2b1 OG |
1641 | } else if (matches(*argv, "ip_tos") == 0) { |
1642 | NEXT_ARG(); | |
1643 | ret = flower_parse_ip_tos_ttl(*argv, | |
1644 | TCA_FLOWER_KEY_IP_TOS, | |
1645 | TCA_FLOWER_KEY_IP_TOS_MASK, | |
1646 | n); | |
1647 | if (ret < 0) { | |
1648 | fprintf(stderr, "Illegal \"ip_tos\"\n"); | |
1649 | return -1; | |
1650 | } | |
1651 | } else if (matches(*argv, "ip_ttl") == 0) { | |
1652 | NEXT_ARG(); | |
1653 | ret = flower_parse_ip_tos_ttl(*argv, | |
1654 | TCA_FLOWER_KEY_IP_TTL, | |
1655 | TCA_FLOWER_KEY_IP_TTL_MASK, | |
1656 | n); | |
1657 | if (ret < 0) { | |
1658 | fprintf(stderr, "Illegal \"ip_ttl\"\n"); | |
1659 | return -1; | |
1660 | } | |
30eb304e JP |
1661 | } else if (matches(*argv, "dst_ip") == 0) { |
1662 | NEXT_ARG(); | |
4c551369 | 1663 | ret = flower_parse_ip_addr(*argv, eth_type, |
30eb304e JP |
1664 | TCA_FLOWER_KEY_IPV4_DST, |
1665 | TCA_FLOWER_KEY_IPV4_DST_MASK, | |
1666 | TCA_FLOWER_KEY_IPV6_DST, | |
1667 | TCA_FLOWER_KEY_IPV6_DST_MASK, | |
1668 | n); | |
1669 | if (ret < 0) { | |
1670 | fprintf(stderr, "Illegal \"dst_ip\"\n"); | |
1671 | return -1; | |
1672 | } | |
1673 | } else if (matches(*argv, "src_ip") == 0) { | |
1674 | NEXT_ARG(); | |
4c551369 | 1675 | ret = flower_parse_ip_addr(*argv, eth_type, |
30eb304e JP |
1676 | TCA_FLOWER_KEY_IPV4_SRC, |
1677 | TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
1678 | TCA_FLOWER_KEY_IPV6_SRC, | |
1679 | TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
1680 | n); | |
1681 | if (ret < 0) { | |
1682 | fprintf(stderr, "Illegal \"src_ip\"\n"); | |
1683 | return -1; | |
1684 | } | |
1685 | } else if (matches(*argv, "dst_port") == 0) { | |
1686 | NEXT_ARG(); | |
dd7d522a DA |
1687 | ret = flower_parse_port(*argv, ip_proto, |
1688 | FLOWER_ENDPOINT_DST, n); | |
1689 | if (ret < 0) { | |
1690 | fprintf(stderr, "Illegal \"dst_port\"\n"); | |
1691 | return -1; | |
30eb304e JP |
1692 | } |
1693 | } else if (matches(*argv, "src_port") == 0) { | |
1694 | NEXT_ARG(); | |
dd7d522a DA |
1695 | ret = flower_parse_port(*argv, ip_proto, |
1696 | FLOWER_ENDPOINT_SRC, n); | |
1697 | if (ret < 0) { | |
1698 | fprintf(stderr, "Illegal \"src_port\"\n"); | |
1699 | return -1; | |
30eb304e | 1700 | } |
0c30d14d JP |
1701 | } else if (matches(*argv, "tcp_flags") == 0) { |
1702 | NEXT_ARG(); | |
1703 | ret = flower_parse_tcp_flags(*argv, | |
1704 | TCA_FLOWER_KEY_TCP_FLAGS, | |
1705 | TCA_FLOWER_KEY_TCP_FLAGS_MASK, | |
1706 | n); | |
1707 | if (ret < 0) { | |
1708 | fprintf(stderr, "Illegal \"tcp_flags\"\n"); | |
1709 | return -1; | |
1710 | } | |
eb3b5696 SH |
1711 | } else if (matches(*argv, "type") == 0) { |
1712 | NEXT_ARG(); | |
1713 | ret = flower_parse_icmp(*argv, eth_type, ip_proto, | |
1714 | FLOWER_ICMP_FIELD_TYPE, n); | |
1715 | if (ret < 0) { | |
1716 | fprintf(stderr, "Illegal \"icmp type\"\n"); | |
1717 | return -1; | |
1718 | } | |
1719 | } else if (matches(*argv, "code") == 0) { | |
1720 | NEXT_ARG(); | |
1721 | ret = flower_parse_icmp(*argv, eth_type, ip_proto, | |
1722 | FLOWER_ICMP_FIELD_CODE, n); | |
1723 | if (ret < 0) { | |
1724 | fprintf(stderr, "Illegal \"icmp code\"\n"); | |
1725 | return -1; | |
1726 | } | |
f888f4e2 SH |
1727 | } else if (matches(*argv, "arp_tip") == 0) { |
1728 | NEXT_ARG(); | |
4c551369 ZD |
1729 | ret = flower_parse_arp_ip_addr(*argv, eth_type, |
1730 | TCA_FLOWER_KEY_ARP_TIP, | |
1731 | TCA_FLOWER_KEY_ARP_TIP_MASK, | |
1732 | n); | |
f888f4e2 SH |
1733 | if (ret < 0) { |
1734 | fprintf(stderr, "Illegal \"arp_tip\"\n"); | |
1735 | return -1; | |
1736 | } | |
1737 | } else if (matches(*argv, "arp_sip") == 0) { | |
1738 | NEXT_ARG(); | |
4c551369 ZD |
1739 | ret = flower_parse_arp_ip_addr(*argv, eth_type, |
1740 | TCA_FLOWER_KEY_ARP_SIP, | |
1741 | TCA_FLOWER_KEY_ARP_SIP_MASK, | |
1742 | n); | |
f888f4e2 SH |
1743 | if (ret < 0) { |
1744 | fprintf(stderr, "Illegal \"arp_sip\"\n"); | |
1745 | return -1; | |
1746 | } | |
1747 | } else if (matches(*argv, "arp_op") == 0) { | |
1748 | NEXT_ARG(); | |
4c551369 ZD |
1749 | ret = flower_parse_arp_op(*argv, eth_type, |
1750 | TCA_FLOWER_KEY_ARP_OP, | |
1751 | TCA_FLOWER_KEY_ARP_OP_MASK, | |
1752 | n); | |
f888f4e2 SH |
1753 | if (ret < 0) { |
1754 | fprintf(stderr, "Illegal \"arp_op\"\n"); | |
1755 | return -1; | |
1756 | } | |
1757 | } else if (matches(*argv, "arp_tha") == 0) { | |
1758 | NEXT_ARG(); | |
1759 | ret = flower_parse_eth_addr(*argv, | |
1760 | TCA_FLOWER_KEY_ARP_THA, | |
1761 | TCA_FLOWER_KEY_ARP_THA_MASK, | |
1762 | n); | |
1763 | if (ret < 0) { | |
1764 | fprintf(stderr, "Illegal \"arp_tha\"\n"); | |
1765 | return -1; | |
1766 | } | |
1767 | } else if (matches(*argv, "arp_sha") == 0) { | |
1768 | NEXT_ARG(); | |
1769 | ret = flower_parse_eth_addr(*argv, | |
1770 | TCA_FLOWER_KEY_ARP_SHA, | |
1771 | TCA_FLOWER_KEY_ARP_SHA_MASK, | |
1772 | n); | |
1773 | if (ret < 0) { | |
1774 | fprintf(stderr, "Illegal \"arp_sha\"\n"); | |
1775 | return -1; | |
1776 | } | |
bb9b63b1 AV |
1777 | } else if (matches(*argv, "enc_dst_ip") == 0) { |
1778 | NEXT_ARG(); | |
1779 | ret = flower_parse_ip_addr(*argv, 0, | |
1780 | TCA_FLOWER_KEY_ENC_IPV4_DST, | |
1781 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
1782 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
1783 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
1784 | n); | |
1785 | if (ret < 0) { | |
1786 | fprintf(stderr, "Illegal \"enc_dst_ip\"\n"); | |
1787 | return -1; | |
1788 | } | |
1789 | } else if (matches(*argv, "enc_src_ip") == 0) { | |
1790 | NEXT_ARG(); | |
1791 | ret = flower_parse_ip_addr(*argv, 0, | |
1792 | TCA_FLOWER_KEY_ENC_IPV4_SRC, | |
1793 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
1794 | TCA_FLOWER_KEY_ENC_IPV6_SRC, | |
1795 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
1796 | n); | |
1797 | if (ret < 0) { | |
1798 | fprintf(stderr, "Illegal \"enc_src_ip\"\n"); | |
1799 | return -1; | |
1800 | } | |
1801 | } else if (matches(*argv, "enc_key_id") == 0) { | |
1802 | NEXT_ARG(); | |
1803 | ret = flower_parse_key_id(*argv, | |
1804 | TCA_FLOWER_KEY_ENC_KEY_ID, n); | |
1805 | if (ret < 0) { | |
1806 | fprintf(stderr, "Illegal \"enc_key_id\"\n"); | |
1807 | return -1; | |
1808 | } | |
41aa17ff HHZ |
1809 | } else if (matches(*argv, "enc_dst_port") == 0) { |
1810 | NEXT_ARG(); | |
1811 | ret = flower_parse_enc_port(*argv, | |
1812 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT, n); | |
1813 | if (ret < 0) { | |
1814 | fprintf(stderr, "Illegal \"enc_dst_port\"\n"); | |
1815 | return -1; | |
1816 | } | |
761ec9e2 OG |
1817 | } else if (matches(*argv, "enc_tos") == 0) { |
1818 | NEXT_ARG(); | |
1819 | ret = flower_parse_ip_tos_ttl(*argv, | |
1820 | TCA_FLOWER_KEY_ENC_IP_TOS, | |
1821 | TCA_FLOWER_KEY_ENC_IP_TOS_MASK, | |
1822 | n); | |
1823 | if (ret < 0) { | |
1824 | fprintf(stderr, "Illegal \"enc_tos\"\n"); | |
1825 | return -1; | |
1826 | } | |
1827 | } else if (matches(*argv, "enc_ttl") == 0) { | |
1828 | NEXT_ARG(); | |
1829 | ret = flower_parse_ip_tos_ttl(*argv, | |
1830 | TCA_FLOWER_KEY_ENC_IP_TTL, | |
1831 | TCA_FLOWER_KEY_ENC_IP_TTL_MASK, | |
1832 | n); | |
1833 | if (ret < 0) { | |
1834 | fprintf(stderr, "Illegal \"enc_ttl\"\n"); | |
1835 | return -1; | |
1836 | } | |
56155d4d PJV |
1837 | } else if (matches(*argv, "geneve_opts") == 0) { |
1838 | NEXT_ARG(); | |
93c8d5f7 | 1839 | ret = flower_parse_enc_opts_geneve(*argv, n); |
56155d4d PJV |
1840 | if (ret < 0) { |
1841 | fprintf(stderr, "Illegal \"geneve_opts\"\n"); | |
1842 | return -1; | |
1843 | } | |
93c8d5f7 XL |
1844 | } else if (matches(*argv, "vxlan_opts") == 0) { |
1845 | NEXT_ARG(); | |
1846 | ret = flower_parse_enc_opts_vxlan(*argv, n); | |
1847 | if (ret < 0) { | |
1848 | fprintf(stderr, "Illegal \"vxlan_opts\"\n"); | |
1849 | return -1; | |
1850 | } | |
4e578c78 XL |
1851 | } else if (matches(*argv, "erspan_opts") == 0) { |
1852 | NEXT_ARG(); | |
1853 | ret = flower_parse_enc_opts_erspan(*argv, n); | |
1854 | if (ret < 0) { | |
1855 | fprintf(stderr, "Illegal \"erspan_opts\"\n"); | |
1856 | return -1; | |
1857 | } | |
30eb304e JP |
1858 | } else if (matches(*argv, "action") == 0) { |
1859 | NEXT_ARG(); | |
1860 | ret = parse_action(&argc, &argv, TCA_FLOWER_ACT, n); | |
1861 | if (ret) { | |
1862 | fprintf(stderr, "Illegal \"action\"\n"); | |
1863 | return -1; | |
1864 | } | |
1865 | continue; | |
1866 | } else if (strcmp(*argv, "help") == 0) { | |
1867 | explain(); | |
1868 | return -1; | |
1869 | } else { | |
1870 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
1871 | explain(); | |
1872 | return -1; | |
1873 | } | |
1874 | argc--; argv++; | |
1875 | } | |
1876 | ||
488b41d0 | 1877 | parse_done: |
c85609b2 RD |
1878 | ret = addattr32(n, MAX_MSG, TCA_FLOWER_FLAGS, flags); |
1879 | if (ret) | |
1880 | return ret; | |
cfcabf18 | 1881 | |
08f66c80 PB |
1882 | if (mtf_mask) { |
1883 | ret = addattr32(n, MAX_MSG, TCA_FLOWER_KEY_FLAGS, htonl(mtf)); | |
1884 | if (ret) | |
1885 | return ret; | |
1886 | ||
1887 | ret = addattr32(n, MAX_MSG, TCA_FLOWER_KEY_FLAGS_MASK, htonl(mtf_mask)); | |
1888 | if (ret) | |
1889 | return ret; | |
1890 | } | |
1891 | ||
4c551369 ZD |
1892 | if (tc_proto != htons(ETH_P_ALL)) { |
1893 | ret = addattr16(n, MAX_MSG, TCA_FLOWER_KEY_ETH_TYPE, tc_proto); | |
4f7d406f BL |
1894 | if (ret) |
1895 | return ret; | |
1896 | } | |
488b41d0 | 1897 | |
32a121cb | 1898 | tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail; |
30eb304e JP |
1899 | |
1900 | return 0; | |
1901 | } | |
1902 | ||
1903 | static int __mask_bits(char *addr, size_t len) | |
1904 | { | |
1905 | int bits = 0; | |
1906 | bool hole = false; | |
1907 | int i; | |
1908 | int j; | |
1909 | ||
1910 | for (i = 0; i < len; i++, addr++) { | |
1911 | for (j = 7; j >= 0; j--) { | |
1912 | if (((*addr) >> j) & 0x1) { | |
1913 | if (hole) | |
1914 | return -1; | |
1915 | bits++; | |
1916 | } else if (bits) { | |
1917 | hole = true; | |
1918 | } else{ | |
1919 | return -1; | |
1920 | } | |
1921 | } | |
1922 | } | |
1923 | return bits; | |
1924 | } | |
1925 | ||
e28b88a4 | 1926 | static void flower_print_eth_addr(char *name, struct rtattr *addr_attr, |
30eb304e JP |
1927 | struct rtattr *mask_attr) |
1928 | { | |
e28b88a4 JP |
1929 | SPRINT_BUF(namefrm); |
1930 | SPRINT_BUF(out); | |
30eb304e | 1931 | SPRINT_BUF(b1); |
e28b88a4 | 1932 | size_t done; |
30eb304e JP |
1933 | int bits; |
1934 | ||
1935 | if (!addr_attr || RTA_PAYLOAD(addr_attr) != ETH_ALEN) | |
1936 | return; | |
e28b88a4 JP |
1937 | done = sprintf(out, "%s", |
1938 | ll_addr_n2a(RTA_DATA(addr_attr), ETH_ALEN, | |
1939 | 0, b1, sizeof(b1))); | |
1940 | if (mask_attr && RTA_PAYLOAD(mask_attr) == ETH_ALEN) { | |
1941 | bits = __mask_bits(RTA_DATA(mask_attr), ETH_ALEN); | |
1942 | if (bits < 0) | |
1943 | sprintf(out + done, "/%s", | |
1944 | ll_addr_n2a(RTA_DATA(mask_attr), ETH_ALEN, | |
1945 | 0, b1, sizeof(b1))); | |
1946 | else if (bits < ETH_ALEN * 8) | |
1947 | sprintf(out + done, "/%d", bits); | |
1948 | } | |
1949 | ||
919046d3 RD |
1950 | print_nl(); |
1951 | sprintf(namefrm, " %s %%s", name); | |
e28b88a4 | 1952 | print_string(PRINT_ANY, name, namefrm, out); |
30eb304e JP |
1953 | } |
1954 | ||
e28b88a4 | 1955 | static void flower_print_eth_type(__be16 *p_eth_type, |
30eb304e JP |
1956 | struct rtattr *eth_type_attr) |
1957 | { | |
e28b88a4 | 1958 | SPRINT_BUF(out); |
30eb304e JP |
1959 | __be16 eth_type; |
1960 | ||
1961 | if (!eth_type_attr) | |
1962 | return; | |
1963 | ||
1964 | eth_type = rta_getattr_u16(eth_type_attr); | |
30eb304e | 1965 | if (eth_type == htons(ETH_P_IP)) |
e28b88a4 | 1966 | sprintf(out, "ipv4"); |
30eb304e | 1967 | else if (eth_type == htons(ETH_P_IPV6)) |
e28b88a4 | 1968 | sprintf(out, "ipv6"); |
f888f4e2 | 1969 | else if (eth_type == htons(ETH_P_ARP)) |
e28b88a4 | 1970 | sprintf(out, "arp"); |
f888f4e2 | 1971 | else if (eth_type == htons(ETH_P_RARP)) |
e28b88a4 | 1972 | sprintf(out, "rarp"); |
30eb304e | 1973 | else |
e28b88a4 JP |
1974 | sprintf(out, "%04x", ntohs(eth_type)); |
1975 | ||
919046d3 RD |
1976 | print_nl(); |
1977 | print_string(PRINT_ANY, "eth_type", " eth_type %s", out); | |
30eb304e JP |
1978 | *p_eth_type = eth_type; |
1979 | } | |
1980 | ||
e28b88a4 | 1981 | static void flower_print_ip_proto(__u8 *p_ip_proto, |
30eb304e JP |
1982 | struct rtattr *ip_proto_attr) |
1983 | { | |
e28b88a4 | 1984 | SPRINT_BUF(out); |
30eb304e JP |
1985 | __u8 ip_proto; |
1986 | ||
1987 | if (!ip_proto_attr) | |
1988 | return; | |
1989 | ||
1990 | ip_proto = rta_getattr_u8(ip_proto_attr); | |
30eb304e | 1991 | if (ip_proto == IPPROTO_TCP) |
e28b88a4 | 1992 | sprintf(out, "tcp"); |
30eb304e | 1993 | else if (ip_proto == IPPROTO_UDP) |
e28b88a4 | 1994 | sprintf(out, "udp"); |
a1fb0d48 | 1995 | else if (ip_proto == IPPROTO_SCTP) |
e28b88a4 | 1996 | sprintf(out, "sctp"); |
eb3b5696 | 1997 | else if (ip_proto == IPPROTO_ICMP) |
e28b88a4 | 1998 | sprintf(out, "icmp"); |
eb3b5696 | 1999 | else if (ip_proto == IPPROTO_ICMPV6) |
e28b88a4 | 2000 | sprintf(out, "icmpv6"); |
30eb304e | 2001 | else |
e28b88a4 JP |
2002 | sprintf(out, "%02x", ip_proto); |
2003 | ||
919046d3 RD |
2004 | print_nl(); |
2005 | print_string(PRINT_ANY, "ip_proto", " ip_proto %s", out); | |
30eb304e JP |
2006 | *p_ip_proto = ip_proto; |
2007 | } | |
2008 | ||
e8bd3955 | 2009 | static void flower_print_ip_attr(const char *name, struct rtattr *key_attr, |
6ea2c2b1 OG |
2010 | struct rtattr *mask_attr) |
2011 | { | |
9479ec1e | 2012 | print_masked_u8(name, key_attr, mask_attr, true); |
6ea2c2b1 OG |
2013 | } |
2014 | ||
e28b88a4 | 2015 | static void flower_print_matching_flags(char *name, |
08f66c80 | 2016 | enum flower_matching_flags type, |
22a8f019 PB |
2017 | struct rtattr *attr, |
2018 | struct rtattr *mask_attr) | |
2019 | { | |
08f66c80 PB |
2020 | int i; |
2021 | int count = 0; | |
2022 | __u32 mtf; | |
2023 | __u32 mtf_mask; | |
2024 | ||
22a8f019 PB |
2025 | if (!mask_attr || RTA_PAYLOAD(mask_attr) != 4) |
2026 | return; | |
2027 | ||
08f66c80 PB |
2028 | mtf = ntohl(rta_getattr_u32(attr)); |
2029 | mtf_mask = ntohl(rta_getattr_u32(mask_attr)); | |
2030 | ||
2031 | for (i = 0; i < ARRAY_SIZE(flags_str); i++) { | |
2032 | if (type != flags_str[i].type) | |
2033 | continue; | |
2034 | if (mtf_mask & flags_str[i].flag) { | |
e28b88a4 | 2035 | if (++count == 1) { |
919046d3 RD |
2036 | print_nl(); |
2037 | print_string(PRINT_FP, NULL, " %s ", name); | |
e28b88a4 JP |
2038 | open_json_object(name); |
2039 | } else { | |
2040 | print_string(PRINT_FP, NULL, "/", NULL); | |
2041 | } | |
08f66c80 | 2042 | |
e28b88a4 JP |
2043 | print_bool(PRINT_JSON, flags_str[i].string, NULL, |
2044 | mtf & flags_str[i].flag); | |
08f66c80 | 2045 | if (mtf & flags_str[i].flag) |
e28b88a4 JP |
2046 | print_string(PRINT_FP, NULL, "%s", |
2047 | flags_str[i].string); | |
08f66c80 | 2048 | else |
e28b88a4 JP |
2049 | print_string(PRINT_FP, NULL, "no%s", |
2050 | flags_str[i].string); | |
08f66c80 PB |
2051 | } |
2052 | } | |
e28b88a4 JP |
2053 | if (count) |
2054 | close_json_object(); | |
22a8f019 PB |
2055 | } |
2056 | ||
e28b88a4 | 2057 | static void flower_print_ip_addr(char *name, __be16 eth_type, |
30eb304e JP |
2058 | struct rtattr *addr4_attr, |
2059 | struct rtattr *mask4_attr, | |
2060 | struct rtattr *addr6_attr, | |
2061 | struct rtattr *mask6_attr) | |
2062 | { | |
30eb304e JP |
2063 | struct rtattr *addr_attr; |
2064 | struct rtattr *mask_attr; | |
e28b88a4 JP |
2065 | SPRINT_BUF(namefrm); |
2066 | SPRINT_BUF(out); | |
2067 | size_t done; | |
30eb304e JP |
2068 | int family; |
2069 | size_t len; | |
2070 | int bits; | |
2071 | ||
2072 | if (eth_type == htons(ETH_P_IP)) { | |
2073 | family = AF_INET; | |
2074 | addr_attr = addr4_attr; | |
2075 | mask_attr = mask4_attr; | |
2076 | len = 4; | |
2077 | } else if (eth_type == htons(ETH_P_IPV6)) { | |
2078 | family = AF_INET6; | |
2079 | addr_attr = addr6_attr; | |
2080 | mask_attr = mask6_attr; | |
2081 | len = 16; | |
2082 | } else { | |
2083 | return; | |
2084 | } | |
2085 | if (!addr_attr || RTA_PAYLOAD(addr_attr) != len) | |
2086 | return; | |
30eb304e JP |
2087 | if (!mask_attr || RTA_PAYLOAD(mask_attr) != len) |
2088 | return; | |
e28b88a4 | 2089 | done = sprintf(out, "%s", rt_addr_n2a_rta(family, addr_attr)); |
30eb304e JP |
2090 | bits = __mask_bits(RTA_DATA(mask_attr), len); |
2091 | if (bits < 0) | |
e28b88a4 | 2092 | sprintf(out + done, "/%s", rt_addr_n2a_rta(family, mask_attr)); |
30eb304e | 2093 | else if (bits < len * 8) |
e28b88a4 JP |
2094 | sprintf(out + done, "/%d", bits); |
2095 | ||
919046d3 RD |
2096 | print_nl(); |
2097 | sprintf(namefrm, " %s %%s", name); | |
e28b88a4 | 2098 | print_string(PRINT_ANY, name, namefrm, out); |
30eb304e | 2099 | } |
e28b88a4 | 2100 | static void flower_print_ip4_addr(char *name, struct rtattr *addr_attr, |
f888f4e2 SH |
2101 | struct rtattr *mask_attr) |
2102 | { | |
e28b88a4 | 2103 | return flower_print_ip_addr(name, htons(ETH_P_IP), |
f888f4e2 SH |
2104 | addr_attr, mask_attr, 0, 0); |
2105 | } | |
30eb304e | 2106 | |
482fd40a EB |
2107 | static void flower_print_port(char *name, struct rtattr *attr, |
2108 | struct rtattr *mask_attr) | |
30eb304e | 2109 | { |
482fd40a | 2110 | print_masked_be16(name, attr, mask_attr, true); |
30eb304e JP |
2111 | } |
2112 | ||
8930840e AN |
2113 | static void flower_print_port_range(char *name, struct rtattr *min_attr, |
2114 | struct rtattr *max_attr) | |
2115 | { | |
2116 | if (!min_attr || !max_attr) | |
2117 | return; | |
2118 | ||
2119 | if (is_json_context()) { | |
2120 | open_json_object(name); | |
2121 | print_hu(PRINT_JSON, "start", NULL, rta_getattr_be16(min_attr)); | |
2122 | print_hu(PRINT_JSON, "end", NULL, rta_getattr_be16(max_attr)); | |
2123 | close_json_object(); | |
2124 | } else { | |
2125 | SPRINT_BUF(namefrm); | |
2126 | SPRINT_BUF(out); | |
2127 | size_t done; | |
2128 | ||
2129 | done = sprintf(out, "%u", rta_getattr_be16(min_attr)); | |
2130 | sprintf(out + done, "-%u", rta_getattr_be16(max_attr)); | |
919046d3 RD |
2131 | print_nl(); |
2132 | sprintf(namefrm, " %s %%s", name); | |
8930840e AN |
2133 | print_string(PRINT_ANY, name, namefrm, out); |
2134 | } | |
2135 | } | |
2136 | ||
e8bd3955 | 2137 | static void flower_print_tcp_flags(const char *name, struct rtattr *flags_attr, |
e28b88a4 | 2138 | struct rtattr *mask_attr) |
0c30d14d | 2139 | { |
e28b88a4 JP |
2140 | SPRINT_BUF(namefrm); |
2141 | SPRINT_BUF(out); | |
2142 | size_t done; | |
2143 | ||
0c30d14d JP |
2144 | if (!flags_attr) |
2145 | return; | |
e28b88a4 | 2146 | |
e8bd3955 | 2147 | done = sprintf(out, "0x%x", rta_getattr_be16(flags_attr)); |
e28b88a4 | 2148 | if (mask_attr) |
e8bd3955 | 2149 | sprintf(out + done, "/%x", rta_getattr_be16(mask_attr)); |
e28b88a4 | 2150 | |
919046d3 RD |
2151 | print_nl(); |
2152 | sprintf(namefrm, " %s %%s", name); | |
e28b88a4 | 2153 | print_string(PRINT_ANY, name, namefrm, out); |
0c30d14d JP |
2154 | } |
2155 | ||
2fffb1c0 PB |
2156 | static void flower_print_ct_state(struct rtattr *flags_attr, |
2157 | struct rtattr *mask_attr) | |
2158 | { | |
2159 | SPRINT_BUF(out); | |
2160 | uint16_t state; | |
2161 | uint16_t state_mask; | |
2162 | size_t done = 0; | |
2163 | int i; | |
2164 | ||
2165 | if (!flags_attr) | |
2166 | return; | |
2167 | ||
2168 | state = rta_getattr_u16(flags_attr); | |
2169 | if (mask_attr) | |
2170 | state_mask = rta_getattr_u16(mask_attr); | |
2171 | else | |
2172 | state_mask = UINT16_MAX; | |
2173 | ||
2174 | for (i = 0; i < ARRAY_SIZE(flower_ct_states); i++) { | |
2175 | if (!(state_mask & flower_ct_states[i].flag)) | |
2176 | continue; | |
2177 | ||
2178 | if (state & flower_ct_states[i].flag) | |
2179 | done += sprintf(out + done, "+%s", | |
2180 | flower_ct_states[i].str); | |
2181 | else | |
2182 | done += sprintf(out + done, "-%s", | |
2183 | flower_ct_states[i].str); | |
2184 | } | |
2185 | ||
919046d3 RD |
2186 | print_nl(); |
2187 | print_string(PRINT_ANY, "ct_state", " ct_state %s", out); | |
2fffb1c0 PB |
2188 | } |
2189 | ||
2190 | static void flower_print_ct_label(struct rtattr *attr, | |
2191 | struct rtattr *mask_attr) | |
2192 | { | |
2193 | const unsigned char *str; | |
2194 | bool print_mask = false; | |
2195 | int data_len, i; | |
2196 | SPRINT_BUF(out); | |
2197 | char *p; | |
2198 | ||
2199 | if (!attr) | |
2200 | return; | |
2201 | ||
2202 | data_len = RTA_PAYLOAD(attr); | |
2203 | hexstring_n2a(RTA_DATA(attr), data_len, out, sizeof(out)); | |
2204 | p = out + data_len*2; | |
2205 | ||
2206 | data_len = RTA_PAYLOAD(attr); | |
2207 | str = RTA_DATA(mask_attr); | |
2208 | if (data_len != 16) | |
2209 | print_mask = true; | |
2210 | for (i = 0; !print_mask && i < data_len; i++) { | |
2211 | if (str[i] != 0xff) | |
2212 | print_mask = true; | |
2213 | } | |
2214 | if (print_mask) { | |
2215 | *p++ = '/'; | |
2216 | hexstring_n2a(RTA_DATA(mask_attr), data_len, p, | |
2217 | sizeof(out)-(p-out)); | |
2218 | p += data_len*2; | |
2219 | } | |
2220 | *p = '\0'; | |
2221 | ||
919046d3 RD |
2222 | print_nl(); |
2223 | print_string(PRINT_ANY, "ct_label", " ct_label %s", out); | |
2fffb1c0 PB |
2224 | } |
2225 | ||
2226 | static void flower_print_ct_zone(struct rtattr *attr, | |
2227 | struct rtattr *mask_attr) | |
2228 | { | |
99d5ee83 | 2229 | print_masked_u16("ct_zone", attr, mask_attr, true); |
2fffb1c0 PB |
2230 | } |
2231 | ||
2232 | static void flower_print_ct_mark(struct rtattr *attr, | |
2233 | struct rtattr *mask_attr) | |
2234 | { | |
99d5ee83 | 2235 | print_masked_u32("ct_mark", attr, mask_attr, true); |
2fffb1c0 | 2236 | } |
0c30d14d | 2237 | |
e28b88a4 | 2238 | static void flower_print_key_id(const char *name, struct rtattr *attr) |
bb9b63b1 | 2239 | { |
e28b88a4 JP |
2240 | SPRINT_BUF(namefrm); |
2241 | ||
2242 | if (!attr) | |
2243 | return; | |
2244 | ||
919046d3 RD |
2245 | print_nl(); |
2246 | sprintf(namefrm, " %s %%u", name); | |
e28b88a4 | 2247 | print_uint(PRINT_ANY, name, namefrm, rta_getattr_be32(attr)); |
bb9b63b1 AV |
2248 | } |
2249 | ||
56155d4d PJV |
2250 | static void flower_print_geneve_opts(const char *name, struct rtattr *attr, |
2251 | char *strbuf) | |
2252 | { | |
2253 | struct rtattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1]; | |
2254 | int ii, data_len, offset = 0, slen = 0; | |
2255 | struct rtattr *i = RTA_DATA(attr); | |
2256 | int rem = RTA_PAYLOAD(attr); | |
2257 | __u8 type, data_r[rem]; | |
2258 | char data[rem * 2 + 1]; | |
2259 | __u16 class; | |
2260 | ||
2261 | open_json_array(PRINT_JSON, name); | |
2262 | while (rem) { | |
2263 | parse_rtattr(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX, i, rem); | |
2264 | class = rta_getattr_be16(tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]); | |
2265 | type = rta_getattr_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]); | |
2266 | data_len = RTA_PAYLOAD(tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]); | |
2267 | hexstring_n2a(RTA_DATA(tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]), | |
2268 | data_len, data, sizeof(data)); | |
2269 | hex2mem(data, data_r, data_len); | |
2270 | offset += data_len + 20; | |
2271 | rem -= data_len + 20; | |
2272 | i = RTA_DATA(attr) + offset; | |
2273 | ||
2274 | open_json_object(NULL); | |
2275 | print_uint(PRINT_JSON, "class", NULL, class); | |
2276 | print_uint(PRINT_JSON, "type", NULL, type); | |
2277 | open_json_array(PRINT_JSON, "data"); | |
2278 | for (ii = 0; ii < data_len; ii++) | |
2279 | print_uint(PRINT_JSON, NULL, NULL, data_r[ii]); | |
2280 | close_json_array(PRINT_JSON, "data"); | |
2281 | close_json_object(); | |
2282 | ||
2283 | slen += sprintf(strbuf + slen, "%04x:%02x:%s", | |
2284 | class, type, data); | |
2285 | if (rem) | |
2286 | slen += sprintf(strbuf + slen, ","); | |
2287 | } | |
2288 | close_json_array(PRINT_JSON, name); | |
2289 | } | |
2290 | ||
93c8d5f7 XL |
2291 | static void flower_print_vxlan_opts(const char *name, struct rtattr *attr, |
2292 | char *strbuf) | |
2293 | { | |
2294 | struct rtattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1]; | |
2295 | struct rtattr *i = RTA_DATA(attr); | |
2296 | int rem = RTA_PAYLOAD(attr); | |
2297 | __u32 gbp; | |
2298 | ||
2299 | parse_rtattr(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, i, rem); | |
2300 | gbp = rta_getattr_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]); | |
2301 | ||
2302 | open_json_array(PRINT_JSON, name); | |
2303 | open_json_object(NULL); | |
2304 | print_uint(PRINT_JSON, "gbp", NULL, gbp); | |
2305 | close_json_object(); | |
2306 | close_json_array(PRINT_JSON, name); | |
2307 | ||
2308 | sprintf(strbuf, "%u", gbp); | |
2309 | } | |
2310 | ||
4e578c78 XL |
2311 | static void flower_print_erspan_opts(const char *name, struct rtattr *attr, |
2312 | char *strbuf) | |
2313 | { | |
2314 | struct rtattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1]; | |
2315 | __u8 ver, hwid, dir; | |
2316 | __u32 idx; | |
2317 | ||
2318 | parse_rtattr(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, RTA_DATA(attr), | |
2319 | RTA_PAYLOAD(attr)); | |
2320 | ver = rta_getattr_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]); | |
2321 | if (ver == 1) { | |
2322 | idx = rta_getattr_be32(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]); | |
2323 | hwid = 0; | |
2324 | dir = 0; | |
2325 | } else { | |
2326 | idx = 0; | |
2327 | hwid = rta_getattr_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]); | |
2328 | dir = rta_getattr_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]); | |
2329 | } | |
2330 | ||
2331 | open_json_array(PRINT_JSON, name); | |
2332 | open_json_object(NULL); | |
2333 | print_uint(PRINT_JSON, "ver", NULL, ver); | |
2334 | print_uint(PRINT_JSON, "index", NULL, idx); | |
2335 | print_uint(PRINT_JSON, "dir", NULL, dir); | |
2336 | print_uint(PRINT_JSON, "hwid", NULL, hwid); | |
2337 | close_json_object(); | |
2338 | close_json_array(PRINT_JSON, name); | |
2339 | ||
2340 | sprintf(strbuf, "%u:%u:%u:%u", ver, idx, dir, hwid); | |
2341 | } | |
2342 | ||
93c8d5f7 XL |
2343 | static void flower_print_enc_parts(const char *name, const char *namefrm, |
2344 | struct rtattr *attr, char *key, char *mask) | |
56155d4d | 2345 | { |
56155d4d PJV |
2346 | char *key_token, *mask_token, *out; |
2347 | int len; | |
2348 | ||
2349 | out = malloc(RTA_PAYLOAD(attr) * 4 + 3); | |
2350 | if (!out) | |
2351 | return; | |
2352 | ||
2353 | len = 0; | |
2354 | key_token = strsep(&key, ","); | |
2355 | mask_token = strsep(&mask, ","); | |
2356 | while (key_token) { | |
2357 | len += sprintf(&out[len], "%s/%s,", key_token, mask_token); | |
2358 | mask_token = strsep(&mask, ","); | |
2359 | key_token = strsep(&key, ","); | |
2360 | } | |
2361 | ||
2362 | out[len - 1] = '\0'; | |
919046d3 | 2363 | print_nl(); |
56155d4d PJV |
2364 | print_string(PRINT_FP, name, namefrm, out); |
2365 | free(out); | |
2366 | } | |
2367 | ||
2368 | static void flower_print_enc_opts(const char *name, struct rtattr *attr, | |
2369 | struct rtattr *mask_attr) | |
2370 | { | |
2371 | struct rtattr *key_tb[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1]; | |
2372 | struct rtattr *msk_tb[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1]; | |
2373 | char *key, *msk; | |
2374 | ||
2375 | if (!attr) | |
2376 | return; | |
2377 | ||
2378 | key = malloc(RTA_PAYLOAD(attr) * 2 + 1); | |
2379 | if (!key) | |
2380 | return; | |
2381 | ||
2382 | msk = malloc(RTA_PAYLOAD(attr) * 2 + 1); | |
2383 | if (!msk) | |
2384 | goto err_key_free; | |
2385 | ||
2386 | parse_rtattr_nested(key_tb, TCA_FLOWER_KEY_ENC_OPTS_MAX, attr); | |
56155d4d | 2387 | parse_rtattr_nested(msk_tb, TCA_FLOWER_KEY_ENC_OPTS_MAX, mask_attr); |
56155d4d | 2388 | |
93c8d5f7 XL |
2389 | if (key_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE]) { |
2390 | flower_print_geneve_opts("geneve_opt_key", | |
2391 | key_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE], key); | |
2392 | ||
2393 | if (msk_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE]) | |
2394 | flower_print_geneve_opts("geneve_opt_mask", | |
2395 | msk_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE], msk); | |
2396 | ||
2397 | flower_print_enc_parts(name, " geneve_opts %s", attr, key, | |
2398 | msk); | |
2399 | } else if (key_tb[TCA_FLOWER_KEY_ENC_OPTS_VXLAN]) { | |
2400 | flower_print_vxlan_opts("vxlan_opt_key", | |
2401 | key_tb[TCA_FLOWER_KEY_ENC_OPTS_VXLAN], key); | |
2402 | ||
2403 | if (msk_tb[TCA_FLOWER_KEY_ENC_OPTS_VXLAN]) | |
2404 | flower_print_vxlan_opts("vxlan_opt_mask", | |
2405 | msk_tb[TCA_FLOWER_KEY_ENC_OPTS_VXLAN], msk); | |
2406 | ||
2407 | flower_print_enc_parts(name, " vxlan_opts %s", attr, key, | |
2408 | msk); | |
4e578c78 XL |
2409 | } else if (key_tb[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN]) { |
2410 | flower_print_erspan_opts("erspan_opt_key", | |
2411 | key_tb[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN], key); | |
2412 | ||
2413 | if (msk_tb[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN]) | |
2414 | flower_print_erspan_opts("erspan_opt_mask", | |
2415 | msk_tb[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN], msk); | |
2416 | ||
2417 | flower_print_enc_parts(name, " erspan_opts %s", attr, key, | |
2418 | msk); | |
93c8d5f7 | 2419 | } |
56155d4d PJV |
2420 | |
2421 | free(msk); | |
2422 | err_key_free: | |
2423 | free(key); | |
2424 | } | |
2425 | ||
e28b88a4 | 2426 | static void flower_print_masked_u8(const char *name, struct rtattr *attr, |
9d36e54f SH |
2427 | struct rtattr *mask_attr, |
2428 | const char *(*value_to_str)(__u8 value)) | |
f888f4e2 | 2429 | { |
9d36e54f SH |
2430 | const char *value_str = NULL; |
2431 | __u8 value, mask; | |
e28b88a4 JP |
2432 | SPRINT_BUF(namefrm); |
2433 | SPRINT_BUF(out); | |
2434 | size_t done; | |
f888f4e2 | 2435 | |
9d36e54f | 2436 | if (!attr) |
f888f4e2 SH |
2437 | return; |
2438 | ||
9d36e54f | 2439 | value = rta_getattr_u8(attr); |
f888f4e2 | 2440 | mask = mask_attr ? rta_getattr_u8(mask_attr) : UINT8_MAX; |
9d36e54f SH |
2441 | if (mask == UINT8_MAX && value_to_str) |
2442 | value_str = value_to_str(value); | |
f888f4e2 | 2443 | |
9d36e54f | 2444 | if (value_str) |
e28b88a4 | 2445 | done = sprintf(out, "%s", value_str); |
f888f4e2 | 2446 | else |
e28b88a4 | 2447 | done = sprintf(out, "%d", value); |
f888f4e2 SH |
2448 | |
2449 | if (mask != UINT8_MAX) | |
e28b88a4 JP |
2450 | sprintf(out + done, "/%d", mask); |
2451 | ||
919046d3 RD |
2452 | print_nl(); |
2453 | sprintf(namefrm, " %s %%s", name); | |
e28b88a4 | 2454 | print_string(PRINT_ANY, name, namefrm, out); |
f888f4e2 SH |
2455 | } |
2456 | ||
e28b88a4 | 2457 | static void flower_print_u8(const char *name, struct rtattr *attr) |
7638ee13 | 2458 | { |
e28b88a4 | 2459 | flower_print_masked_u8(name, attr, NULL, NULL); |
7638ee13 BL |
2460 | } |
2461 | ||
e28b88a4 | 2462 | static void flower_print_u32(const char *name, struct rtattr *attr) |
7638ee13 | 2463 | { |
e28b88a4 | 2464 | SPRINT_BUF(namefrm); |
7638ee13 BL |
2465 | |
2466 | if (!attr) | |
2467 | return; | |
2468 | ||
919046d3 RD |
2469 | print_nl(); |
2470 | sprintf(namefrm, " %s %%u", name); | |
e28b88a4 | 2471 | print_uint(PRINT_ANY, name, namefrm, rta_getattr_u32(attr)); |
7638ee13 BL |
2472 | } |
2473 | ||
676a1a70 | 2474 | static void flower_print_mpls_opt_lse(struct rtattr *lse) |
eb09a15c GN |
2475 | { |
2476 | struct rtattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1]; | |
2477 | struct rtattr *attr; | |
2478 | ||
2479 | if (lse->rta_type != (TCA_FLOWER_KEY_MPLS_OPTS_LSE | NLA_F_NESTED)) { | |
2480 | fprintf(stderr, "rta_type 0x%x, expecting 0x%x (0x%x & 0x%x)\n", | |
2481 | lse->rta_type, | |
2482 | TCA_FLOWER_KEY_MPLS_OPTS_LSE & NLA_F_NESTED, | |
2483 | TCA_FLOWER_KEY_MPLS_OPTS_LSE, NLA_F_NESTED); | |
2484 | return; | |
2485 | } | |
2486 | ||
2487 | parse_rtattr(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, RTA_DATA(lse), | |
2488 | RTA_PAYLOAD(lse)); | |
2489 | ||
2490 | print_nl(); | |
676a1a70 GN |
2491 | print_string(PRINT_FP, NULL, " lse", NULL); |
2492 | open_json_object(NULL); | |
eb09a15c GN |
2493 | attr = tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]; |
2494 | if (attr) | |
2495 | print_hhu(PRINT_ANY, "depth", " depth %u", | |
2496 | rta_getattr_u8(attr)); | |
2497 | attr = tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]; | |
2498 | if (attr) | |
2499 | print_uint(PRINT_ANY, "label", " label %u", | |
2500 | rta_getattr_u32(attr)); | |
2501 | attr = tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]; | |
2502 | if (attr) | |
2503 | print_hhu(PRINT_ANY, "tc", " tc %u", rta_getattr_u8(attr)); | |
2504 | attr = tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]; | |
2505 | if (attr) | |
2506 | print_hhu(PRINT_ANY, "bos", " bos %u", rta_getattr_u8(attr)); | |
2507 | attr = tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]; | |
2508 | if (attr) | |
2509 | print_hhu(PRINT_ANY, "ttl", " ttl %u", rta_getattr_u8(attr)); | |
676a1a70 | 2510 | close_json_object(); |
eb09a15c GN |
2511 | } |
2512 | ||
676a1a70 | 2513 | static void flower_print_mpls_opts(struct rtattr *attr) |
eb09a15c GN |
2514 | { |
2515 | struct rtattr *lse; | |
2516 | int rem; | |
2517 | ||
2518 | if (!attr || !(attr->rta_type & NLA_F_NESTED)) | |
2519 | return; | |
2520 | ||
2521 | print_nl(); | |
676a1a70 GN |
2522 | print_string(PRINT_FP, NULL, " mpls", NULL); |
2523 | open_json_array(PRINT_JSON, "mpls"); | |
eb09a15c GN |
2524 | rem = RTA_PAYLOAD(attr); |
2525 | lse = RTA_DATA(attr); | |
2526 | while (RTA_OK(lse, rem)) { | |
676a1a70 | 2527 | flower_print_mpls_opt_lse(lse); |
eb09a15c GN |
2528 | lse = RTA_NEXT(lse, rem); |
2529 | }; | |
2530 | if (rem) | |
2531 | fprintf(stderr, "!!!Deficit %d, rta_len=%d\n", | |
2532 | rem, lse->rta_len); | |
2533 | close_json_array(PRINT_JSON, NULL); | |
2534 | } | |
2535 | ||
e28b88a4 | 2536 | static void flower_print_arp_op(const char *name, |
9d36e54f SH |
2537 | struct rtattr *op_attr, |
2538 | struct rtattr *mask_attr) | |
2539 | { | |
e28b88a4 | 2540 | flower_print_masked_u8(name, op_attr, mask_attr, |
9d36e54f SH |
2541 | flower_print_arp_op_to_name); |
2542 | } | |
2543 | ||
30eb304e JP |
2544 | static int flower_print_opt(struct filter_util *qu, FILE *f, |
2545 | struct rtattr *opt, __u32 handle) | |
2546 | { | |
2547 | struct rtattr *tb[TCA_FLOWER_MAX + 1]; | |
8930840e | 2548 | __be16 min_port_type, max_port_type; |
6374961a | 2549 | int nl_type, nl_mask_type; |
30eb304e JP |
2550 | __be16 eth_type = 0; |
2551 | __u8 ip_proto = 0xff; | |
2552 | ||
2553 | if (!opt) | |
2554 | return 0; | |
2555 | ||
2556 | parse_rtattr_nested(tb, TCA_FLOWER_MAX, opt); | |
2557 | ||
2558 | if (handle) | |
e28b88a4 | 2559 | print_uint(PRINT_ANY, "handle", "handle 0x%x ", handle); |
30eb304e JP |
2560 | |
2561 | if (tb[TCA_FLOWER_CLASSID]) { | |
0d575c4d AN |
2562 | __u32 h = rta_getattr_u32(tb[TCA_FLOWER_CLASSID]); |
2563 | ||
2564 | if (TC_H_MIN(h) < TC_H_MIN_PRIORITY || | |
2565 | TC_H_MIN(h) > (TC_H_MIN_PRIORITY + TC_QOPT_MAX_QUEUE - 1)) { | |
2566 | SPRINT_BUF(b1); | |
e28b88a4 JP |
2567 | print_string(PRINT_ANY, "classid", "classid %s ", |
2568 | sprint_tc_classid(h, b1)); | |
0d575c4d | 2569 | } else { |
e28b88a4 JP |
2570 | print_uint(PRINT_ANY, "hw_tc", "hw_tc %u ", |
2571 | TC_H_MIN(h) - TC_H_MIN_PRIORITY); | |
0d575c4d | 2572 | } |
30eb304e JP |
2573 | } |
2574 | ||
2575 | if (tb[TCA_FLOWER_INDEV]) { | |
2576 | struct rtattr *attr = tb[TCA_FLOWER_INDEV]; | |
2577 | ||
919046d3 RD |
2578 | print_nl(); |
2579 | print_string(PRINT_ANY, "indev", " indev %s", | |
e28b88a4 | 2580 | rta_getattr_str(attr)); |
30eb304e JP |
2581 | } |
2582 | ||
e28b88a4 JP |
2583 | open_json_object("keys"); |
2584 | ||
745d9172 HHZ |
2585 | if (tb[TCA_FLOWER_KEY_VLAN_ID]) { |
2586 | struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_ID]; | |
2587 | ||
919046d3 RD |
2588 | print_nl(); |
2589 | print_uint(PRINT_ANY, "vlan_id", " vlan_id %u", | |
e28b88a4 | 2590 | rta_getattr_u16(attr)); |
745d9172 HHZ |
2591 | } |
2592 | ||
2593 | if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) { | |
2594 | struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_PRIO]; | |
2595 | ||
919046d3 RD |
2596 | print_nl(); |
2597 | print_uint(PRINT_ANY, "vlan_prio", " vlan_prio %d", | |
e28b88a4 | 2598 | rta_getattr_u8(attr)); |
745d9172 HHZ |
2599 | } |
2600 | ||
1f0a5dfd JL |
2601 | if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) { |
2602 | SPRINT_BUF(buf); | |
2603 | struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]; | |
2604 | ||
919046d3 RD |
2605 | print_nl(); |
2606 | print_string(PRINT_ANY, "vlan_ethtype", " vlan_ethtype %s", | |
1f0a5dfd JL |
2607 | ll_proto_n2a(rta_getattr_u16(attr), |
2608 | buf, sizeof(buf))); | |
2609 | } | |
2610 | ||
2611 | if (tb[TCA_FLOWER_KEY_CVLAN_ID]) { | |
2612 | struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_ID]; | |
2613 | ||
919046d3 RD |
2614 | print_nl(); |
2615 | print_uint(PRINT_ANY, "cvlan_id", " cvlan_id %u", | |
1f0a5dfd JL |
2616 | rta_getattr_u16(attr)); |
2617 | } | |
2618 | ||
2619 | if (tb[TCA_FLOWER_KEY_CVLAN_PRIO]) { | |
2620 | struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_PRIO]; | |
2621 | ||
919046d3 | 2622 | print_nl(); |
7b0d424a | 2623 | print_uint(PRINT_ANY, "cvlan_prio", " cvlan_prio %d", |
1f0a5dfd JL |
2624 | rta_getattr_u8(attr)); |
2625 | } | |
2626 | ||
2627 | if (tb[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]) { | |
2628 | SPRINT_BUF(buf); | |
2629 | struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]; | |
2630 | ||
919046d3 RD |
2631 | print_nl(); |
2632 | print_string(PRINT_ANY, "cvlan_ethtype", " cvlan_ethtype %s", | |
1f0a5dfd JL |
2633 | ll_proto_n2a(rta_getattr_u16(attr), |
2634 | buf, sizeof(buf))); | |
2635 | } | |
2636 | ||
e28b88a4 | 2637 | flower_print_eth_addr("dst_mac", tb[TCA_FLOWER_KEY_ETH_DST], |
30eb304e | 2638 | tb[TCA_FLOWER_KEY_ETH_DST_MASK]); |
e28b88a4 | 2639 | flower_print_eth_addr("src_mac", tb[TCA_FLOWER_KEY_ETH_SRC], |
30eb304e JP |
2640 | tb[TCA_FLOWER_KEY_ETH_SRC_MASK]); |
2641 | ||
e28b88a4 JP |
2642 | flower_print_eth_type(ð_type, tb[TCA_FLOWER_KEY_ETH_TYPE]); |
2643 | flower_print_ip_proto(&ip_proto, tb[TCA_FLOWER_KEY_IP_PROTO]); | |
30eb304e | 2644 | |
e28b88a4 | 2645 | flower_print_ip_attr("ip_tos", tb[TCA_FLOWER_KEY_IP_TOS], |
6ea2c2b1 | 2646 | tb[TCA_FLOWER_KEY_IP_TOS_MASK]); |
e28b88a4 | 2647 | flower_print_ip_attr("ip_ttl", tb[TCA_FLOWER_KEY_IP_TTL], |
6ea2c2b1 OG |
2648 | tb[TCA_FLOWER_KEY_IP_TTL_MASK]); |
2649 | ||
676a1a70 | 2650 | flower_print_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS]); |
e28b88a4 JP |
2651 | flower_print_u32("mpls_label", tb[TCA_FLOWER_KEY_MPLS_LABEL]); |
2652 | flower_print_u8("mpls_tc", tb[TCA_FLOWER_KEY_MPLS_TC]); | |
2653 | flower_print_u8("mpls_bos", tb[TCA_FLOWER_KEY_MPLS_BOS]); | |
2654 | flower_print_u8("mpls_ttl", tb[TCA_FLOWER_KEY_MPLS_TTL]); | |
7638ee13 | 2655 | |
e28b88a4 | 2656 | flower_print_ip_addr("dst_ip", eth_type, |
30eb304e JP |
2657 | tb[TCA_FLOWER_KEY_IPV4_DST], |
2658 | tb[TCA_FLOWER_KEY_IPV4_DST_MASK], | |
2659 | tb[TCA_FLOWER_KEY_IPV6_DST], | |
2660 | tb[TCA_FLOWER_KEY_IPV6_DST_MASK]); | |
2661 | ||
e28b88a4 | 2662 | flower_print_ip_addr("src_ip", eth_type, |
30eb304e JP |
2663 | tb[TCA_FLOWER_KEY_IPV4_SRC], |
2664 | tb[TCA_FLOWER_KEY_IPV4_SRC_MASK], | |
2665 | tb[TCA_FLOWER_KEY_IPV6_SRC], | |
2666 | tb[TCA_FLOWER_KEY_IPV6_SRC_MASK]); | |
2667 | ||
b2141de1 | 2668 | nl_type = flower_port_attr_type(ip_proto, FLOWER_ENDPOINT_DST); |
482fd40a | 2669 | nl_mask_type = flower_port_attr_mask_type(ip_proto, FLOWER_ENDPOINT_DST); |
6bd5b80c | 2670 | if (nl_type >= 0) |
482fd40a | 2671 | flower_print_port("dst_port", tb[nl_type], tb[nl_mask_type]); |
b2141de1 | 2672 | nl_type = flower_port_attr_type(ip_proto, FLOWER_ENDPOINT_SRC); |
482fd40a | 2673 | nl_mask_type = flower_port_attr_mask_type(ip_proto, FLOWER_ENDPOINT_SRC); |
6bd5b80c | 2674 | if (nl_type >= 0) |
482fd40a | 2675 | flower_print_port("src_port", tb[nl_type], tb[nl_mask_type]); |
30eb304e | 2676 | |
8930840e AN |
2677 | if (!flower_port_range_attr_type(ip_proto, FLOWER_ENDPOINT_DST, |
2678 | &min_port_type, &max_port_type)) | |
2679 | flower_print_port_range("dst_port", | |
2680 | tb[min_port_type], tb[max_port_type]); | |
2681 | ||
2682 | if (!flower_port_range_attr_type(ip_proto, FLOWER_ENDPOINT_SRC, | |
2683 | &min_port_type, &max_port_type)) | |
2684 | flower_print_port_range("src_port", | |
2685 | tb[min_port_type], tb[max_port_type]); | |
2686 | ||
e28b88a4 | 2687 | flower_print_tcp_flags("tcp_flags", tb[TCA_FLOWER_KEY_TCP_FLAGS], |
0c30d14d JP |
2688 | tb[TCA_FLOWER_KEY_TCP_FLAGS_MASK]); |
2689 | ||
81f6e5a7 SH |
2690 | nl_type = flower_icmp_attr_type(eth_type, ip_proto, |
2691 | FLOWER_ICMP_FIELD_TYPE); | |
6374961a SH |
2692 | nl_mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto, |
2693 | FLOWER_ICMP_FIELD_TYPE); | |
2694 | if (nl_type >= 0 && nl_mask_type >= 0) | |
e28b88a4 | 2695 | flower_print_masked_u8("icmp_type", tb[nl_type], |
6374961a SH |
2696 | tb[nl_mask_type], NULL); |
2697 | ||
81f6e5a7 SH |
2698 | nl_type = flower_icmp_attr_type(eth_type, ip_proto, |
2699 | FLOWER_ICMP_FIELD_CODE); | |
6374961a SH |
2700 | nl_mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto, |
2701 | FLOWER_ICMP_FIELD_CODE); | |
2702 | if (nl_type >= 0 && nl_mask_type >= 0) | |
e28b88a4 | 2703 | flower_print_masked_u8("icmp_code", tb[nl_type], |
6374961a | 2704 | tb[nl_mask_type], NULL); |
eb3b5696 | 2705 | |
e28b88a4 | 2706 | flower_print_ip4_addr("arp_sip", tb[TCA_FLOWER_KEY_ARP_SIP], |
f888f4e2 | 2707 | tb[TCA_FLOWER_KEY_ARP_SIP_MASK]); |
e28b88a4 | 2708 | flower_print_ip4_addr("arp_tip", tb[TCA_FLOWER_KEY_ARP_TIP], |
f888f4e2 | 2709 | tb[TCA_FLOWER_KEY_ARP_TIP_MASK]); |
e28b88a4 | 2710 | flower_print_arp_op("arp_op", tb[TCA_FLOWER_KEY_ARP_OP], |
f888f4e2 | 2711 | tb[TCA_FLOWER_KEY_ARP_OP_MASK]); |
e28b88a4 | 2712 | flower_print_eth_addr("arp_sha", tb[TCA_FLOWER_KEY_ARP_SHA], |
f888f4e2 | 2713 | tb[TCA_FLOWER_KEY_ARP_SHA_MASK]); |
e28b88a4 | 2714 | flower_print_eth_addr("arp_tha", tb[TCA_FLOWER_KEY_ARP_THA], |
f888f4e2 SH |
2715 | tb[TCA_FLOWER_KEY_ARP_THA_MASK]); |
2716 | ||
e28b88a4 | 2717 | flower_print_ip_addr("enc_dst_ip", |
bb9b63b1 AV |
2718 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] ? |
2719 | htons(ETH_P_IP) : htons(ETH_P_IPV6), | |
2720 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST], | |
2721 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK], | |
2722 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST], | |
2723 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK]); | |
2724 | ||
e28b88a4 | 2725 | flower_print_ip_addr("enc_src_ip", |
bb9b63b1 AV |
2726 | tb[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] ? |
2727 | htons(ETH_P_IP) : htons(ETH_P_IPV6), | |
2728 | tb[TCA_FLOWER_KEY_ENC_IPV4_SRC], | |
2729 | tb[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK], | |
2730 | tb[TCA_FLOWER_KEY_ENC_IPV6_SRC], | |
2731 | tb[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK]); | |
2732 | ||
e28b88a4 | 2733 | flower_print_key_id("enc_key_id", tb[TCA_FLOWER_KEY_ENC_KEY_ID]); |
bb9b63b1 | 2734 | |
482fd40a EB |
2735 | flower_print_port("enc_dst_port", tb[TCA_FLOWER_KEY_ENC_UDP_DST_PORT], |
2736 | tb[TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK]); | |
41aa17ff | 2737 | |
761ec9e2 OG |
2738 | flower_print_ip_attr("enc_tos", tb[TCA_FLOWER_KEY_ENC_IP_TOS], |
2739 | tb[TCA_FLOWER_KEY_ENC_IP_TOS_MASK]); | |
2740 | flower_print_ip_attr("enc_ttl", tb[TCA_FLOWER_KEY_ENC_IP_TTL], | |
2741 | tb[TCA_FLOWER_KEY_ENC_IP_TTL_MASK]); | |
56155d4d PJV |
2742 | flower_print_enc_opts("enc_opt", tb[TCA_FLOWER_KEY_ENC_OPTS], |
2743 | tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); | |
761ec9e2 | 2744 | |
e28b88a4 | 2745 | flower_print_matching_flags("ip_flags", FLOWER_IP_FLAGS, |
22a8f019 PB |
2746 | tb[TCA_FLOWER_KEY_FLAGS], |
2747 | tb[TCA_FLOWER_KEY_FLAGS_MASK]); | |
2748 | ||
2fffb1c0 PB |
2749 | flower_print_ct_state(tb[TCA_FLOWER_KEY_CT_STATE], |
2750 | tb[TCA_FLOWER_KEY_CT_STATE_MASK]); | |
2751 | flower_print_ct_zone(tb[TCA_FLOWER_KEY_CT_ZONE], | |
2752 | tb[TCA_FLOWER_KEY_CT_ZONE_MASK]); | |
2753 | flower_print_ct_mark(tb[TCA_FLOWER_KEY_CT_MARK], | |
2754 | tb[TCA_FLOWER_KEY_CT_MARK_MASK]); | |
2755 | flower_print_ct_label(tb[TCA_FLOWER_KEY_CT_LABELS], | |
2756 | tb[TCA_FLOWER_KEY_CT_LABELS_MASK]); | |
2757 | ||
e28b88a4 JP |
2758 | close_json_object(); |
2759 | ||
512caeb2 | 2760 | if (tb[TCA_FLOWER_FLAGS]) { |
cfcabf18 AV |
2761 | __u32 flags = rta_getattr_u32(tb[TCA_FLOWER_FLAGS]); |
2762 | ||
919046d3 RD |
2763 | if (flags & TCA_CLS_FLAGS_SKIP_HW) { |
2764 | print_nl(); | |
2765 | print_bool(PRINT_ANY, "skip_hw", " skip_hw", true); | |
2766 | } | |
2767 | if (flags & TCA_CLS_FLAGS_SKIP_SW) { | |
2768 | print_nl(); | |
2769 | print_bool(PRINT_ANY, "skip_sw", " skip_sw", true); | |
2770 | } | |
f6b498f9 | 2771 | if (flags & TCA_CLS_FLAGS_IN_HW) { |
919046d3 RD |
2772 | print_nl(); |
2773 | print_bool(PRINT_ANY, "in_hw", " in_hw", true); | |
f6b498f9 VB |
2774 | |
2775 | if (tb[TCA_FLOWER_IN_HW_COUNT]) { | |
2776 | __u32 count = rta_getattr_u32(tb[TCA_FLOWER_IN_HW_COUNT]); | |
2777 | ||
2778 | print_uint(PRINT_ANY, "in_hw_count", | |
2779 | " in_hw_count %u", count); | |
2780 | } | |
2781 | } | |
7b0d424a | 2782 | else if (flags & TCA_CLS_FLAGS_NOT_IN_HW) { |
919046d3 RD |
2783 | print_nl(); |
2784 | print_bool(PRINT_ANY, "not_in_hw", " not_in_hw", true); | |
f6b498f9 | 2785 | } |
cfcabf18 AV |
2786 | } |
2787 | ||
512caeb2 | 2788 | if (tb[TCA_FLOWER_ACT]) |
9e713525 | 2789 | tc_print_action(f, tb[TCA_FLOWER_ACT], 0); |
30eb304e JP |
2790 | |
2791 | return 0; | |
2792 | } | |
2793 | ||
2794 | struct filter_util flower_filter_util = { | |
2795 | .id = "flower", | |
2796 | .parse_fopt = flower_parse_opt, | |
2797 | .print_fopt = flower_print_opt, | |
2798 | }; |