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